8250.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273
  1. /*
  2. * linux/drivers/char/8250.c
  3. *
  4. * Driver for 8250/16550-type serial ports
  5. *
  6. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  7. *
  8. * Copyright (C) 2001 Russell King.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * A note about mapbase / membase
  16. *
  17. * mapbase is the physical address of the IO port.
  18. * membase is an 'ioremapped' cookie.
  19. */
  20. #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  21. #define SUPPORT_SYSRQ
  22. #endif
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/ioport.h>
  26. #include <linux/init.h>
  27. #include <linux/console.h>
  28. #include <linux/sysrq.h>
  29. #include <linux/delay.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/tty.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/serial_core.h>
  35. #include <linux/serial.h>
  36. #include <linux/serial_8250.h>
  37. #include <linux/nmi.h>
  38. #include <linux/mutex.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. #include "8250.h"
  42. #ifdef CONFIG_SPARC
  43. #include "suncore.h"
  44. #endif
  45. /*
  46. * Configuration:
  47. * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
  48. * is unsafe when used on edge-triggered interrupts.
  49. */
  50. static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
  51. static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
  52. static struct uart_driver serial8250_reg;
  53. static int serial_index(struct uart_port *port)
  54. {
  55. return (serial8250_reg.minor - 64) + port->line;
  56. }
  57. static unsigned int skip_txen_test; /* force skip of txen test at init time */
  58. /*
  59. * Debugging.
  60. */
  61. #if 0
  62. #define DEBUG_AUTOCONF(fmt...) printk(fmt)
  63. #else
  64. #define DEBUG_AUTOCONF(fmt...) do { } while (0)
  65. #endif
  66. #if 0
  67. #define DEBUG_INTR(fmt...) printk(fmt)
  68. #else
  69. #define DEBUG_INTR(fmt...) do { } while (0)
  70. #endif
  71. #define PASS_LIMIT 256
  72. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  73. /*
  74. * We default to IRQ0 for the "no irq" hack. Some
  75. * machine types want others as well - they're free
  76. * to redefine this in their header file.
  77. */
  78. #define is_real_interrupt(irq) ((irq) != 0)
  79. #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
  80. #define CONFIG_SERIAL_DETECT_IRQ 1
  81. #endif
  82. #ifdef CONFIG_SERIAL_8250_MANY_PORTS
  83. #define CONFIG_SERIAL_MANY_PORTS 1
  84. #endif
  85. /*
  86. * HUB6 is always on. This will be removed once the header
  87. * files have been cleaned.
  88. */
  89. #define CONFIG_HUB6 1
  90. #include <asm/serial.h>
  91. /*
  92. * SERIAL_PORT_DFNS tells us about built-in ports that have no
  93. * standard enumeration mechanism. Platforms that can find all
  94. * serial ports via mechanisms like ACPI or PCI need not supply it.
  95. */
  96. #ifndef SERIAL_PORT_DFNS
  97. #define SERIAL_PORT_DFNS
  98. #endif
  99. static const struct old_serial_port old_serial_port[] = {
  100. SERIAL_PORT_DFNS /* defined in asm/serial.h */
  101. };
  102. #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
  103. #ifdef CONFIG_SERIAL_8250_RSA
  104. #define PORT_RSA_MAX 4
  105. static unsigned long probe_rsa[PORT_RSA_MAX];
  106. static unsigned int probe_rsa_count;
  107. #endif /* CONFIG_SERIAL_8250_RSA */
  108. struct uart_8250_port {
  109. struct uart_port port;
  110. struct timer_list timer; /* "no irq" timer */
  111. struct list_head list; /* ports on this IRQ */
  112. unsigned short capabilities; /* port capabilities */
  113. unsigned short bugs; /* port bugs */
  114. unsigned int tx_loadsz; /* transmit fifo load size */
  115. unsigned char acr;
  116. unsigned char ier;
  117. unsigned char lcr;
  118. unsigned char mcr;
  119. unsigned char mcr_mask; /* mask of user bits */
  120. unsigned char mcr_force; /* mask of forced bits */
  121. unsigned char cur_iotype; /* Running I/O type */
  122. /*
  123. * Some bits in registers are cleared on a read, so they must
  124. * be saved whenever the register is read but the bits will not
  125. * be immediately processed.
  126. */
  127. #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
  128. unsigned char lsr_saved_flags;
  129. #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
  130. unsigned char msr_saved_flags;
  131. /*
  132. * We provide a per-port pm hook.
  133. */
  134. void (*pm)(struct uart_port *port,
  135. unsigned int state, unsigned int old);
  136. };
  137. struct irq_info {
  138. struct hlist_node node;
  139. int irq;
  140. spinlock_t lock; /* Protects list not the hash */
  141. struct list_head *head;
  142. };
  143. #define NR_IRQ_HASH 32 /* Can be adjusted later */
  144. static struct hlist_head irq_lists[NR_IRQ_HASH];
  145. static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
  146. /*
  147. * Here we define the default xmit fifo size used for each type of UART.
  148. */
  149. static const struct serial8250_config uart_config[] = {
  150. [PORT_UNKNOWN] = {
  151. .name = "unknown",
  152. .fifo_size = 1,
  153. .tx_loadsz = 1,
  154. },
  155. [PORT_8250] = {
  156. .name = "8250",
  157. .fifo_size = 1,
  158. .tx_loadsz = 1,
  159. },
  160. [PORT_16450] = {
  161. .name = "16450",
  162. .fifo_size = 1,
  163. .tx_loadsz = 1,
  164. },
  165. [PORT_16550] = {
  166. .name = "16550",
  167. .fifo_size = 1,
  168. .tx_loadsz = 1,
  169. },
  170. [PORT_16550A] = {
  171. .name = "16550A",
  172. .fifo_size = 16,
  173. .tx_loadsz = 16,
  174. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  175. .flags = UART_CAP_FIFO,
  176. },
  177. [PORT_CIRRUS] = {
  178. .name = "Cirrus",
  179. .fifo_size = 1,
  180. .tx_loadsz = 1,
  181. },
  182. [PORT_16650] = {
  183. .name = "ST16650",
  184. .fifo_size = 1,
  185. .tx_loadsz = 1,
  186. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  187. },
  188. [PORT_16650V2] = {
  189. .name = "ST16650V2",
  190. .fifo_size = 32,
  191. .tx_loadsz = 16,
  192. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
  193. UART_FCR_T_TRIG_00,
  194. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  195. },
  196. [PORT_16750] = {
  197. .name = "TI16750",
  198. .fifo_size = 64,
  199. .tx_loadsz = 64,
  200. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
  201. UART_FCR7_64BYTE,
  202. .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
  203. },
  204. [PORT_STARTECH] = {
  205. .name = "Startech",
  206. .fifo_size = 1,
  207. .tx_loadsz = 1,
  208. },
  209. [PORT_16C950] = {
  210. .name = "16C950/954",
  211. .fifo_size = 128,
  212. .tx_loadsz = 128,
  213. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  214. .flags = UART_CAP_FIFO,
  215. },
  216. [PORT_16654] = {
  217. .name = "ST16654",
  218. .fifo_size = 64,
  219. .tx_loadsz = 32,
  220. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
  221. UART_FCR_T_TRIG_10,
  222. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  223. },
  224. [PORT_16850] = {
  225. .name = "XR16850",
  226. .fifo_size = 128,
  227. .tx_loadsz = 128,
  228. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  229. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  230. },
  231. [PORT_RSA] = {
  232. .name = "RSA",
  233. .fifo_size = 2048,
  234. .tx_loadsz = 2048,
  235. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
  236. .flags = UART_CAP_FIFO,
  237. },
  238. [PORT_NS16550A] = {
  239. .name = "NS16550A",
  240. .fifo_size = 16,
  241. .tx_loadsz = 16,
  242. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  243. .flags = UART_CAP_FIFO | UART_NATSEMI,
  244. },
  245. [PORT_XSCALE] = {
  246. .name = "XScale",
  247. .fifo_size = 32,
  248. .tx_loadsz = 32,
  249. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  250. .flags = UART_CAP_FIFO | UART_CAP_UUE,
  251. },
  252. [PORT_RM9000] = {
  253. .name = "RM9000",
  254. .fifo_size = 16,
  255. .tx_loadsz = 16,
  256. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  257. .flags = UART_CAP_FIFO,
  258. },
  259. [PORT_OCTEON] = {
  260. .name = "OCTEON",
  261. .fifo_size = 64,
  262. .tx_loadsz = 64,
  263. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  264. .flags = UART_CAP_FIFO,
  265. },
  266. [PORT_AR7] = {
  267. .name = "AR7",
  268. .fifo_size = 16,
  269. .tx_loadsz = 16,
  270. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
  271. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  272. },
  273. };
  274. #if defined (CONFIG_SERIAL_8250_AU1X00)
  275. /* Au1x00 UART hardware has a weird register layout */
  276. static const u8 au_io_in_map[] = {
  277. [UART_RX] = 0,
  278. [UART_IER] = 2,
  279. [UART_IIR] = 3,
  280. [UART_LCR] = 5,
  281. [UART_MCR] = 6,
  282. [UART_LSR] = 7,
  283. [UART_MSR] = 8,
  284. };
  285. static const u8 au_io_out_map[] = {
  286. [UART_TX] = 1,
  287. [UART_IER] = 2,
  288. [UART_FCR] = 4,
  289. [UART_LCR] = 5,
  290. [UART_MCR] = 6,
  291. };
  292. /* sane hardware needs no mapping */
  293. static inline int map_8250_in_reg(struct uart_port *p, int offset)
  294. {
  295. if (p->iotype != UPIO_AU)
  296. return offset;
  297. return au_io_in_map[offset];
  298. }
  299. static inline int map_8250_out_reg(struct uart_port *p, int offset)
  300. {
  301. if (p->iotype != UPIO_AU)
  302. return offset;
  303. return au_io_out_map[offset];
  304. }
  305. #elif defined(CONFIG_SERIAL_8250_RM9K)
  306. static const u8
  307. regmap_in[8] = {
  308. [UART_RX] = 0x00,
  309. [UART_IER] = 0x0c,
  310. [UART_IIR] = 0x14,
  311. [UART_LCR] = 0x1c,
  312. [UART_MCR] = 0x20,
  313. [UART_LSR] = 0x24,
  314. [UART_MSR] = 0x28,
  315. [UART_SCR] = 0x2c
  316. },
  317. regmap_out[8] = {
  318. [UART_TX] = 0x04,
  319. [UART_IER] = 0x0c,
  320. [UART_FCR] = 0x18,
  321. [UART_LCR] = 0x1c,
  322. [UART_MCR] = 0x20,
  323. [UART_LSR] = 0x24,
  324. [UART_MSR] = 0x28,
  325. [UART_SCR] = 0x2c
  326. };
  327. static inline int map_8250_in_reg(struct uart_port *p, int offset)
  328. {
  329. if (p->iotype != UPIO_RM9000)
  330. return offset;
  331. return regmap_in[offset];
  332. }
  333. static inline int map_8250_out_reg(struct uart_port *p, int offset)
  334. {
  335. if (p->iotype != UPIO_RM9000)
  336. return offset;
  337. return regmap_out[offset];
  338. }
  339. #else
  340. /* sane hardware needs no mapping */
  341. #define map_8250_in_reg(up, offset) (offset)
  342. #define map_8250_out_reg(up, offset) (offset)
  343. #endif
  344. static unsigned int hub6_serial_in(struct uart_port *p, int offset)
  345. {
  346. offset = map_8250_in_reg(p, offset) << p->regshift;
  347. outb(p->hub6 - 1 + offset, p->iobase);
  348. return inb(p->iobase + 1);
  349. }
  350. static void hub6_serial_out(struct uart_port *p, int offset, int value)
  351. {
  352. offset = map_8250_out_reg(p, offset) << p->regshift;
  353. outb(p->hub6 - 1 + offset, p->iobase);
  354. outb(value, p->iobase + 1);
  355. }
  356. static unsigned int mem_serial_in(struct uart_port *p, int offset)
  357. {
  358. offset = map_8250_in_reg(p, offset) << p->regshift;
  359. return readb(p->membase + offset);
  360. }
  361. static void mem_serial_out(struct uart_port *p, int offset, int value)
  362. {
  363. offset = map_8250_out_reg(p, offset) << p->regshift;
  364. writeb(value, p->membase + offset);
  365. }
  366. static void mem32_serial_out(struct uart_port *p, int offset, int value)
  367. {
  368. offset = map_8250_out_reg(p, offset) << p->regshift;
  369. writel(value, p->membase + offset);
  370. }
  371. static unsigned int mem32_serial_in(struct uart_port *p, int offset)
  372. {
  373. offset = map_8250_in_reg(p, offset) << p->regshift;
  374. return readl(p->membase + offset);
  375. }
  376. #ifdef CONFIG_SERIAL_8250_AU1X00
  377. static unsigned int au_serial_in(struct uart_port *p, int offset)
  378. {
  379. offset = map_8250_in_reg(p, offset) << p->regshift;
  380. return __raw_readl(p->membase + offset);
  381. }
  382. static void au_serial_out(struct uart_port *p, int offset, int value)
  383. {
  384. offset = map_8250_out_reg(p, offset) << p->regshift;
  385. __raw_writel(value, p->membase + offset);
  386. }
  387. #endif
  388. static unsigned int tsi_serial_in(struct uart_port *p, int offset)
  389. {
  390. unsigned int tmp;
  391. offset = map_8250_in_reg(p, offset) << p->regshift;
  392. if (offset == UART_IIR) {
  393. tmp = readl(p->membase + (UART_IIR & ~3));
  394. return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
  395. } else
  396. return readb(p->membase + offset);
  397. }
  398. static void tsi_serial_out(struct uart_port *p, int offset, int value)
  399. {
  400. offset = map_8250_out_reg(p, offset) << p->regshift;
  401. if (!((offset == UART_IER) && (value & UART_IER_UUE)))
  402. writeb(value, p->membase + offset);
  403. }
  404. static void dwapb_serial_out(struct uart_port *p, int offset, int value)
  405. {
  406. int save_offset = offset;
  407. offset = map_8250_out_reg(p, offset) << p->regshift;
  408. /* Save the LCR value so it can be re-written when a
  409. * Busy Detect interrupt occurs. */
  410. if (save_offset == UART_LCR) {
  411. struct uart_8250_port *up = (struct uart_8250_port *)p;
  412. up->lcr = value;
  413. }
  414. writeb(value, p->membase + offset);
  415. /* Read the IER to ensure any interrupt is cleared before
  416. * returning from ISR. */
  417. if (save_offset == UART_TX || save_offset == UART_IER)
  418. value = p->serial_in(p, UART_IER);
  419. }
  420. static unsigned int io_serial_in(struct uart_port *p, int offset)
  421. {
  422. offset = map_8250_in_reg(p, offset) << p->regshift;
  423. return inb(p->iobase + offset);
  424. }
  425. static void io_serial_out(struct uart_port *p, int offset, int value)
  426. {
  427. offset = map_8250_out_reg(p, offset) << p->regshift;
  428. outb(value, p->iobase + offset);
  429. }
  430. static void set_io_from_upio(struct uart_port *p)
  431. {
  432. struct uart_8250_port *up = (struct uart_8250_port *)p;
  433. switch (p->iotype) {
  434. case UPIO_HUB6:
  435. p->serial_in = hub6_serial_in;
  436. p->serial_out = hub6_serial_out;
  437. break;
  438. case UPIO_MEM:
  439. p->serial_in = mem_serial_in;
  440. p->serial_out = mem_serial_out;
  441. break;
  442. case UPIO_RM9000:
  443. case UPIO_MEM32:
  444. p->serial_in = mem32_serial_in;
  445. p->serial_out = mem32_serial_out;
  446. break;
  447. #ifdef CONFIG_SERIAL_8250_AU1X00
  448. case UPIO_AU:
  449. p->serial_in = au_serial_in;
  450. p->serial_out = au_serial_out;
  451. break;
  452. #endif
  453. case UPIO_TSI:
  454. p->serial_in = tsi_serial_in;
  455. p->serial_out = tsi_serial_out;
  456. break;
  457. case UPIO_DWAPB:
  458. p->serial_in = mem_serial_in;
  459. p->serial_out = dwapb_serial_out;
  460. break;
  461. default:
  462. p->serial_in = io_serial_in;
  463. p->serial_out = io_serial_out;
  464. break;
  465. }
  466. /* Remember loaded iotype */
  467. up->cur_iotype = p->iotype;
  468. }
  469. static void
  470. serial_out_sync(struct uart_8250_port *up, int offset, int value)
  471. {
  472. struct uart_port *p = &up->port;
  473. switch (p->iotype) {
  474. case UPIO_MEM:
  475. case UPIO_MEM32:
  476. #ifdef CONFIG_SERIAL_8250_AU1X00
  477. case UPIO_AU:
  478. #endif
  479. case UPIO_DWAPB:
  480. p->serial_out(p, offset, value);
  481. p->serial_in(p, UART_LCR); /* safe, no side-effects */
  482. break;
  483. default:
  484. p->serial_out(p, offset, value);
  485. }
  486. }
  487. #define serial_in(up, offset) \
  488. (up->port.serial_in(&(up)->port, (offset)))
  489. #define serial_out(up, offset, value) \
  490. (up->port.serial_out(&(up)->port, (offset), (value)))
  491. /*
  492. * We used to support using pause I/O for certain machines. We
  493. * haven't supported this for a while, but just in case it's badly
  494. * needed for certain old 386 machines, I've left these #define's
  495. * in....
  496. */
  497. #define serial_inp(up, offset) serial_in(up, offset)
  498. #define serial_outp(up, offset, value) serial_out(up, offset, value)
  499. /* Uart divisor latch read */
  500. static inline int _serial_dl_read(struct uart_8250_port *up)
  501. {
  502. return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
  503. }
  504. /* Uart divisor latch write */
  505. static inline void _serial_dl_write(struct uart_8250_port *up, int value)
  506. {
  507. serial_outp(up, UART_DLL, value & 0xff);
  508. serial_outp(up, UART_DLM, value >> 8 & 0xff);
  509. }
  510. #if defined(CONFIG_SERIAL_8250_AU1X00)
  511. /* Au1x00 haven't got a standard divisor latch */
  512. static int serial_dl_read(struct uart_8250_port *up)
  513. {
  514. if (up->port.iotype == UPIO_AU)
  515. return __raw_readl(up->port.membase + 0x28);
  516. else
  517. return _serial_dl_read(up);
  518. }
  519. static void serial_dl_write(struct uart_8250_port *up, int value)
  520. {
  521. if (up->port.iotype == UPIO_AU)
  522. __raw_writel(value, up->port.membase + 0x28);
  523. else
  524. _serial_dl_write(up, value);
  525. }
  526. #elif defined(CONFIG_SERIAL_8250_RM9K)
  527. static int serial_dl_read(struct uart_8250_port *up)
  528. {
  529. return (up->port.iotype == UPIO_RM9000) ?
  530. (((__raw_readl(up->port.membase + 0x10) << 8) |
  531. (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
  532. _serial_dl_read(up);
  533. }
  534. static void serial_dl_write(struct uart_8250_port *up, int value)
  535. {
  536. if (up->port.iotype == UPIO_RM9000) {
  537. __raw_writel(value, up->port.membase + 0x08);
  538. __raw_writel(value >> 8, up->port.membase + 0x10);
  539. } else {
  540. _serial_dl_write(up, value);
  541. }
  542. }
  543. #else
  544. #define serial_dl_read(up) _serial_dl_read(up)
  545. #define serial_dl_write(up, value) _serial_dl_write(up, value)
  546. #endif
  547. /*
  548. * For the 16C950
  549. */
  550. static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
  551. {
  552. serial_out(up, UART_SCR, offset);
  553. serial_out(up, UART_ICR, value);
  554. }
  555. static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
  556. {
  557. unsigned int value;
  558. serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
  559. serial_out(up, UART_SCR, offset);
  560. value = serial_in(up, UART_ICR);
  561. serial_icr_write(up, UART_ACR, up->acr);
  562. return value;
  563. }
  564. /*
  565. * FIFO support.
  566. */
  567. static void serial8250_clear_fifos(struct uart_8250_port *p)
  568. {
  569. if (p->capabilities & UART_CAP_FIFO) {
  570. serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
  571. serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
  572. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  573. serial_outp(p, UART_FCR, 0);
  574. }
  575. }
  576. /*
  577. * IER sleep support. UARTs which have EFRs need the "extended
  578. * capability" bit enabled. Note that on XR16C850s, we need to
  579. * reset LCR to write to IER.
  580. */
  581. static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
  582. {
  583. if (p->capabilities & UART_CAP_SLEEP) {
  584. if (p->capabilities & UART_CAP_EFR) {
  585. serial_outp(p, UART_LCR, 0xBF);
  586. serial_outp(p, UART_EFR, UART_EFR_ECB);
  587. serial_outp(p, UART_LCR, 0);
  588. }
  589. serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
  590. if (p->capabilities & UART_CAP_EFR) {
  591. serial_outp(p, UART_LCR, 0xBF);
  592. serial_outp(p, UART_EFR, 0);
  593. serial_outp(p, UART_LCR, 0);
  594. }
  595. }
  596. }
  597. #ifdef CONFIG_SERIAL_8250_RSA
  598. /*
  599. * Attempts to turn on the RSA FIFO. Returns zero on failure.
  600. * We set the port uart clock rate if we succeed.
  601. */
  602. static int __enable_rsa(struct uart_8250_port *up)
  603. {
  604. unsigned char mode;
  605. int result;
  606. mode = serial_inp(up, UART_RSA_MSR);
  607. result = mode & UART_RSA_MSR_FIFO;
  608. if (!result) {
  609. serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
  610. mode = serial_inp(up, UART_RSA_MSR);
  611. result = mode & UART_RSA_MSR_FIFO;
  612. }
  613. if (result)
  614. up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
  615. return result;
  616. }
  617. static void enable_rsa(struct uart_8250_port *up)
  618. {
  619. if (up->port.type == PORT_RSA) {
  620. if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
  621. spin_lock_irq(&up->port.lock);
  622. __enable_rsa(up);
  623. spin_unlock_irq(&up->port.lock);
  624. }
  625. if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
  626. serial_outp(up, UART_RSA_FRR, 0);
  627. }
  628. }
  629. /*
  630. * Attempts to turn off the RSA FIFO. Returns zero on failure.
  631. * It is unknown why interrupts were disabled in here. However,
  632. * the caller is expected to preserve this behaviour by grabbing
  633. * the spinlock before calling this function.
  634. */
  635. static void disable_rsa(struct uart_8250_port *up)
  636. {
  637. unsigned char mode;
  638. int result;
  639. if (up->port.type == PORT_RSA &&
  640. up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
  641. spin_lock_irq(&up->port.lock);
  642. mode = serial_inp(up, UART_RSA_MSR);
  643. result = !(mode & UART_RSA_MSR_FIFO);
  644. if (!result) {
  645. serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
  646. mode = serial_inp(up, UART_RSA_MSR);
  647. result = !(mode & UART_RSA_MSR_FIFO);
  648. }
  649. if (result)
  650. up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
  651. spin_unlock_irq(&up->port.lock);
  652. }
  653. }
  654. #endif /* CONFIG_SERIAL_8250_RSA */
  655. /*
  656. * This is a quickie test to see how big the FIFO is.
  657. * It doesn't work at all the time, more's the pity.
  658. */
  659. static int size_fifo(struct uart_8250_port *up)
  660. {
  661. unsigned char old_fcr, old_mcr, old_lcr;
  662. unsigned short old_dl;
  663. int count;
  664. old_lcr = serial_inp(up, UART_LCR);
  665. serial_outp(up, UART_LCR, 0);
  666. old_fcr = serial_inp(up, UART_FCR);
  667. old_mcr = serial_inp(up, UART_MCR);
  668. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  669. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  670. serial_outp(up, UART_MCR, UART_MCR_LOOP);
  671. serial_outp(up, UART_LCR, UART_LCR_DLAB);
  672. old_dl = serial_dl_read(up);
  673. serial_dl_write(up, 0x0001);
  674. serial_outp(up, UART_LCR, 0x03);
  675. for (count = 0; count < 256; count++)
  676. serial_outp(up, UART_TX, count);
  677. mdelay(20);/* FIXME - schedule_timeout */
  678. for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
  679. (count < 256); count++)
  680. serial_inp(up, UART_RX);
  681. serial_outp(up, UART_FCR, old_fcr);
  682. serial_outp(up, UART_MCR, old_mcr);
  683. serial_outp(up, UART_LCR, UART_LCR_DLAB);
  684. serial_dl_write(up, old_dl);
  685. serial_outp(up, UART_LCR, old_lcr);
  686. return count;
  687. }
  688. /*
  689. * Read UART ID using the divisor method - set DLL and DLM to zero
  690. * and the revision will be in DLL and device type in DLM. We
  691. * preserve the device state across this.
  692. */
  693. static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
  694. {
  695. unsigned char old_dll, old_dlm, old_lcr;
  696. unsigned int id;
  697. old_lcr = serial_inp(p, UART_LCR);
  698. serial_outp(p, UART_LCR, UART_LCR_DLAB);
  699. old_dll = serial_inp(p, UART_DLL);
  700. old_dlm = serial_inp(p, UART_DLM);
  701. serial_outp(p, UART_DLL, 0);
  702. serial_outp(p, UART_DLM, 0);
  703. id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
  704. serial_outp(p, UART_DLL, old_dll);
  705. serial_outp(p, UART_DLM, old_dlm);
  706. serial_outp(p, UART_LCR, old_lcr);
  707. return id;
  708. }
  709. /*
  710. * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
  711. * When this function is called we know it is at least a StarTech
  712. * 16650 V2, but it might be one of several StarTech UARTs, or one of
  713. * its clones. (We treat the broken original StarTech 16650 V1 as a
  714. * 16550, and why not? Startech doesn't seem to even acknowledge its
  715. * existence.)
  716. *
  717. * What evil have men's minds wrought...
  718. */
  719. static void autoconfig_has_efr(struct uart_8250_port *up)
  720. {
  721. unsigned int id1, id2, id3, rev;
  722. /*
  723. * Everything with an EFR has SLEEP
  724. */
  725. up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
  726. /*
  727. * First we check to see if it's an Oxford Semiconductor UART.
  728. *
  729. * If we have to do this here because some non-National
  730. * Semiconductor clone chips lock up if you try writing to the
  731. * LSR register (which serial_icr_read does)
  732. */
  733. /*
  734. * Check for Oxford Semiconductor 16C950.
  735. *
  736. * EFR [4] must be set else this test fails.
  737. *
  738. * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
  739. * claims that it's needed for 952 dual UART's (which are not
  740. * recommended for new designs).
  741. */
  742. up->acr = 0;
  743. serial_out(up, UART_LCR, 0xBF);
  744. serial_out(up, UART_EFR, UART_EFR_ECB);
  745. serial_out(up, UART_LCR, 0x00);
  746. id1 = serial_icr_read(up, UART_ID1);
  747. id2 = serial_icr_read(up, UART_ID2);
  748. id3 = serial_icr_read(up, UART_ID3);
  749. rev = serial_icr_read(up, UART_REV);
  750. DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
  751. if (id1 == 0x16 && id2 == 0xC9 &&
  752. (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
  753. up->port.type = PORT_16C950;
  754. /*
  755. * Enable work around for the Oxford Semiconductor 952 rev B
  756. * chip which causes it to seriously miscalculate baud rates
  757. * when DLL is 0.
  758. */
  759. if (id3 == 0x52 && rev == 0x01)
  760. up->bugs |= UART_BUG_QUOT;
  761. return;
  762. }
  763. /*
  764. * We check for a XR16C850 by setting DLL and DLM to 0, and then
  765. * reading back DLL and DLM. The chip type depends on the DLM
  766. * value read back:
  767. * 0x10 - XR16C850 and the DLL contains the chip revision.
  768. * 0x12 - XR16C2850.
  769. * 0x14 - XR16C854.
  770. */
  771. id1 = autoconfig_read_divisor_id(up);
  772. DEBUG_AUTOCONF("850id=%04x ", id1);
  773. id2 = id1 >> 8;
  774. if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
  775. up->port.type = PORT_16850;
  776. return;
  777. }
  778. /*
  779. * It wasn't an XR16C850.
  780. *
  781. * We distinguish between the '654 and the '650 by counting
  782. * how many bytes are in the FIFO. I'm using this for now,
  783. * since that's the technique that was sent to me in the
  784. * serial driver update, but I'm not convinced this works.
  785. * I've had problems doing this in the past. -TYT
  786. */
  787. if (size_fifo(up) == 64)
  788. up->port.type = PORT_16654;
  789. else
  790. up->port.type = PORT_16650V2;
  791. }
  792. /*
  793. * We detected a chip without a FIFO. Only two fall into
  794. * this category - the original 8250 and the 16450. The
  795. * 16450 has a scratch register (accessible with LCR=0)
  796. */
  797. static void autoconfig_8250(struct uart_8250_port *up)
  798. {
  799. unsigned char scratch, status1, status2;
  800. up->port.type = PORT_8250;
  801. scratch = serial_in(up, UART_SCR);
  802. serial_outp(up, UART_SCR, 0xa5);
  803. status1 = serial_in(up, UART_SCR);
  804. serial_outp(up, UART_SCR, 0x5a);
  805. status2 = serial_in(up, UART_SCR);
  806. serial_outp(up, UART_SCR, scratch);
  807. if (status1 == 0xa5 && status2 == 0x5a)
  808. up->port.type = PORT_16450;
  809. }
  810. static int broken_efr(struct uart_8250_port *up)
  811. {
  812. /*
  813. * Exar ST16C2550 "A2" devices incorrectly detect as
  814. * having an EFR, and report an ID of 0x0201. See
  815. * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
  816. */
  817. if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
  818. return 1;
  819. return 0;
  820. }
  821. /*
  822. * We know that the chip has FIFOs. Does it have an EFR? The
  823. * EFR is located in the same register position as the IIR and
  824. * we know the top two bits of the IIR are currently set. The
  825. * EFR should contain zero. Try to read the EFR.
  826. */
  827. static void autoconfig_16550a(struct uart_8250_port *up)
  828. {
  829. unsigned char status1, status2;
  830. unsigned int iersave;
  831. up->port.type = PORT_16550A;
  832. up->capabilities |= UART_CAP_FIFO;
  833. /*
  834. * Check for presence of the EFR when DLAB is set.
  835. * Only ST16C650V1 UARTs pass this test.
  836. */
  837. serial_outp(up, UART_LCR, UART_LCR_DLAB);
  838. if (serial_in(up, UART_EFR) == 0) {
  839. serial_outp(up, UART_EFR, 0xA8);
  840. if (serial_in(up, UART_EFR) != 0) {
  841. DEBUG_AUTOCONF("EFRv1 ");
  842. up->port.type = PORT_16650;
  843. up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
  844. } else {
  845. DEBUG_AUTOCONF("Motorola 8xxx DUART ");
  846. }
  847. serial_outp(up, UART_EFR, 0);
  848. return;
  849. }
  850. /*
  851. * Maybe it requires 0xbf to be written to the LCR.
  852. * (other ST16C650V2 UARTs, TI16C752A, etc)
  853. */
  854. serial_outp(up, UART_LCR, 0xBF);
  855. if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
  856. DEBUG_AUTOCONF("EFRv2 ");
  857. autoconfig_has_efr(up);
  858. return;
  859. }
  860. /*
  861. * Check for a National Semiconductor SuperIO chip.
  862. * Attempt to switch to bank 2, read the value of the LOOP bit
  863. * from EXCR1. Switch back to bank 0, change it in MCR. Then
  864. * switch back to bank 2, read it from EXCR1 again and check
  865. * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
  866. */
  867. serial_outp(up, UART_LCR, 0);
  868. status1 = serial_in(up, UART_MCR);
  869. serial_outp(up, UART_LCR, 0xE0);
  870. status2 = serial_in(up, 0x02); /* EXCR1 */
  871. if (!((status2 ^ status1) & UART_MCR_LOOP)) {
  872. serial_outp(up, UART_LCR, 0);
  873. serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
  874. serial_outp(up, UART_LCR, 0xE0);
  875. status2 = serial_in(up, 0x02); /* EXCR1 */
  876. serial_outp(up, UART_LCR, 0);
  877. serial_outp(up, UART_MCR, status1);
  878. if ((status2 ^ status1) & UART_MCR_LOOP) {
  879. unsigned short quot;
  880. serial_outp(up, UART_LCR, 0xE0);
  881. quot = serial_dl_read(up);
  882. quot <<= 3;
  883. status1 = serial_in(up, 0x04); /* EXCR2 */
  884. status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
  885. status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
  886. serial_outp(up, 0x04, status1);
  887. serial_dl_write(up, quot);
  888. serial_outp(up, UART_LCR, 0);
  889. up->port.uartclk = 921600*16;
  890. up->port.type = PORT_NS16550A;
  891. up->capabilities |= UART_NATSEMI;
  892. return;
  893. }
  894. }
  895. /*
  896. * No EFR. Try to detect a TI16750, which only sets bit 5 of
  897. * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
  898. * Try setting it with and without DLAB set. Cheap clones
  899. * set bit 5 without DLAB set.
  900. */
  901. serial_outp(up, UART_LCR, 0);
  902. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
  903. status1 = serial_in(up, UART_IIR) >> 5;
  904. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  905. serial_outp(up, UART_LCR, UART_LCR_DLAB);
  906. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
  907. status2 = serial_in(up, UART_IIR) >> 5;
  908. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  909. serial_outp(up, UART_LCR, 0);
  910. DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
  911. if (status1 == 6 && status2 == 7) {
  912. up->port.type = PORT_16750;
  913. up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
  914. return;
  915. }
  916. /*
  917. * Try writing and reading the UART_IER_UUE bit (b6).
  918. * If it works, this is probably one of the Xscale platform's
  919. * internal UARTs.
  920. * We're going to explicitly set the UUE bit to 0 before
  921. * trying to write and read a 1 just to make sure it's not
  922. * already a 1 and maybe locked there before we even start start.
  923. */
  924. iersave = serial_in(up, UART_IER);
  925. serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
  926. if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
  927. /*
  928. * OK it's in a known zero state, try writing and reading
  929. * without disturbing the current state of the other bits.
  930. */
  931. serial_outp(up, UART_IER, iersave | UART_IER_UUE);
  932. if (serial_in(up, UART_IER) & UART_IER_UUE) {
  933. /*
  934. * It's an Xscale.
  935. * We'll leave the UART_IER_UUE bit set to 1 (enabled).
  936. */
  937. DEBUG_AUTOCONF("Xscale ");
  938. up->port.type = PORT_XSCALE;
  939. up->capabilities |= UART_CAP_UUE;
  940. return;
  941. }
  942. } else {
  943. /*
  944. * If we got here we couldn't force the IER_UUE bit to 0.
  945. * Log it and continue.
  946. */
  947. DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
  948. }
  949. serial_outp(up, UART_IER, iersave);
  950. }
  951. /*
  952. * This routine is called by rs_init() to initialize a specific serial
  953. * port. It determines what type of UART chip this serial port is
  954. * using: 8250, 16450, 16550, 16550A. The important question is
  955. * whether or not this UART is a 16550A or not, since this will
  956. * determine whether or not we can use its FIFO features or not.
  957. */
  958. static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
  959. {
  960. unsigned char status1, scratch, scratch2, scratch3;
  961. unsigned char save_lcr, save_mcr;
  962. unsigned long flags;
  963. if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
  964. return;
  965. DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
  966. serial_index(&up->port), up->port.iobase, up->port.membase);
  967. /*
  968. * We really do need global IRQs disabled here - we're going to
  969. * be frobbing the chips IRQ enable register to see if it exists.
  970. */
  971. spin_lock_irqsave(&up->port.lock, flags);
  972. up->capabilities = 0;
  973. up->bugs = 0;
  974. if (!(up->port.flags & UPF_BUGGY_UART)) {
  975. /*
  976. * Do a simple existence test first; if we fail this,
  977. * there's no point trying anything else.
  978. *
  979. * 0x80 is used as a nonsense port to prevent against
  980. * false positives due to ISA bus float. The
  981. * assumption is that 0x80 is a non-existent port;
  982. * which should be safe since include/asm/io.h also
  983. * makes this assumption.
  984. *
  985. * Note: this is safe as long as MCR bit 4 is clear
  986. * and the device is in "PC" mode.
  987. */
  988. scratch = serial_inp(up, UART_IER);
  989. serial_outp(up, UART_IER, 0);
  990. #ifdef __i386__
  991. outb(0xff, 0x080);
  992. #endif
  993. /*
  994. * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
  995. * 16C754B) allow only to modify them if an EFR bit is set.
  996. */
  997. scratch2 = serial_inp(up, UART_IER) & 0x0f;
  998. serial_outp(up, UART_IER, 0x0F);
  999. #ifdef __i386__
  1000. outb(0, 0x080);
  1001. #endif
  1002. scratch3 = serial_inp(up, UART_IER) & 0x0f;
  1003. serial_outp(up, UART_IER, scratch);
  1004. if (scratch2 != 0 || scratch3 != 0x0F) {
  1005. /*
  1006. * We failed; there's nothing here
  1007. */
  1008. DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
  1009. scratch2, scratch3);
  1010. goto out;
  1011. }
  1012. }
  1013. save_mcr = serial_in(up, UART_MCR);
  1014. save_lcr = serial_in(up, UART_LCR);
  1015. /*
  1016. * Check to see if a UART is really there. Certain broken
  1017. * internal modems based on the Rockwell chipset fail this
  1018. * test, because they apparently don't implement the loopback
  1019. * test mode. So this test is skipped on the COM 1 through
  1020. * COM 4 ports. This *should* be safe, since no board
  1021. * manufacturer would be stupid enough to design a board
  1022. * that conflicts with COM 1-4 --- we hope!
  1023. */
  1024. if (!(up->port.flags & UPF_SKIP_TEST)) {
  1025. serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
  1026. status1 = serial_inp(up, UART_MSR) & 0xF0;
  1027. serial_outp(up, UART_MCR, save_mcr);
  1028. if (status1 != 0x90) {
  1029. DEBUG_AUTOCONF("LOOP test failed (%02x) ",
  1030. status1);
  1031. goto out;
  1032. }
  1033. }
  1034. /*
  1035. * We're pretty sure there's a port here. Lets find out what
  1036. * type of port it is. The IIR top two bits allows us to find
  1037. * out if it's 8250 or 16450, 16550, 16550A or later. This
  1038. * determines what we test for next.
  1039. *
  1040. * We also initialise the EFR (if any) to zero for later. The
  1041. * EFR occupies the same register location as the FCR and IIR.
  1042. */
  1043. serial_outp(up, UART_LCR, 0xBF);
  1044. serial_outp(up, UART_EFR, 0);
  1045. serial_outp(up, UART_LCR, 0);
  1046. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  1047. scratch = serial_in(up, UART_IIR) >> 6;
  1048. DEBUG_AUTOCONF("iir=%d ", scratch);
  1049. switch (scratch) {
  1050. case 0:
  1051. autoconfig_8250(up);
  1052. break;
  1053. case 1:
  1054. up->port.type = PORT_UNKNOWN;
  1055. break;
  1056. case 2:
  1057. up->port.type = PORT_16550;
  1058. break;
  1059. case 3:
  1060. autoconfig_16550a(up);
  1061. break;
  1062. }
  1063. #ifdef CONFIG_SERIAL_8250_RSA
  1064. /*
  1065. * Only probe for RSA ports if we got the region.
  1066. */
  1067. if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
  1068. int i;
  1069. for (i = 0 ; i < probe_rsa_count; ++i) {
  1070. if (probe_rsa[i] == up->port.iobase &&
  1071. __enable_rsa(up)) {
  1072. up->port.type = PORT_RSA;
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. #endif
  1078. serial_outp(up, UART_LCR, save_lcr);
  1079. if (up->capabilities != uart_config[up->port.type].flags) {
  1080. printk(KERN_WARNING
  1081. "ttyS%d: detected caps %08x should be %08x\n",
  1082. serial_index(&up->port), up->capabilities,
  1083. uart_config[up->port.type].flags);
  1084. }
  1085. up->port.fifosize = uart_config[up->port.type].fifo_size;
  1086. up->capabilities = uart_config[up->port.type].flags;
  1087. up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
  1088. if (up->port.type == PORT_UNKNOWN)
  1089. goto out;
  1090. /*
  1091. * Reset the UART.
  1092. */
  1093. #ifdef CONFIG_SERIAL_8250_RSA
  1094. if (up->port.type == PORT_RSA)
  1095. serial_outp(up, UART_RSA_FRR, 0);
  1096. #endif
  1097. serial_outp(up, UART_MCR, save_mcr);
  1098. serial8250_clear_fifos(up);
  1099. serial_in(up, UART_RX);
  1100. if (up->capabilities & UART_CAP_UUE)
  1101. serial_outp(up, UART_IER, UART_IER_UUE);
  1102. else
  1103. serial_outp(up, UART_IER, 0);
  1104. out:
  1105. spin_unlock_irqrestore(&up->port.lock, flags);
  1106. DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
  1107. }
  1108. static void autoconfig_irq(struct uart_8250_port *up)
  1109. {
  1110. unsigned char save_mcr, save_ier;
  1111. unsigned char save_ICP = 0;
  1112. unsigned int ICP = 0;
  1113. unsigned long irqs;
  1114. int irq;
  1115. if (up->port.flags & UPF_FOURPORT) {
  1116. ICP = (up->port.iobase & 0xfe0) | 0x1f;
  1117. save_ICP = inb_p(ICP);
  1118. outb_p(0x80, ICP);
  1119. (void) inb_p(ICP);
  1120. }
  1121. /* forget possible initially masked and pending IRQ */
  1122. probe_irq_off(probe_irq_on());
  1123. save_mcr = serial_inp(up, UART_MCR);
  1124. save_ier = serial_inp(up, UART_IER);
  1125. serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
  1126. irqs = probe_irq_on();
  1127. serial_outp(up, UART_MCR, 0);
  1128. udelay(10);
  1129. if (up->port.flags & UPF_FOURPORT) {
  1130. serial_outp(up, UART_MCR,
  1131. UART_MCR_DTR | UART_MCR_RTS);
  1132. } else {
  1133. serial_outp(up, UART_MCR,
  1134. UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
  1135. }
  1136. serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
  1137. (void)serial_inp(up, UART_LSR);
  1138. (void)serial_inp(up, UART_RX);
  1139. (void)serial_inp(up, UART_IIR);
  1140. (void)serial_inp(up, UART_MSR);
  1141. serial_outp(up, UART_TX, 0xFF);
  1142. udelay(20);
  1143. irq = probe_irq_off(irqs);
  1144. serial_outp(up, UART_MCR, save_mcr);
  1145. serial_outp(up, UART_IER, save_ier);
  1146. if (up->port.flags & UPF_FOURPORT)
  1147. outb_p(save_ICP, ICP);
  1148. up->port.irq = (irq > 0) ? irq : 0;
  1149. }
  1150. static inline void __stop_tx(struct uart_8250_port *p)
  1151. {
  1152. if (p->ier & UART_IER_THRI) {
  1153. p->ier &= ~UART_IER_THRI;
  1154. serial_out(p, UART_IER, p->ier);
  1155. }
  1156. }
  1157. static void serial8250_stop_tx(struct uart_port *port)
  1158. {
  1159. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1160. __stop_tx(up);
  1161. /*
  1162. * We really want to stop the transmitter from sending.
  1163. */
  1164. if (up->port.type == PORT_16C950) {
  1165. up->acr |= UART_ACR_TXDIS;
  1166. serial_icr_write(up, UART_ACR, up->acr);
  1167. }
  1168. }
  1169. static void transmit_chars(struct uart_8250_port *up);
  1170. static void serial8250_start_tx(struct uart_port *port)
  1171. {
  1172. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1173. if (!(up->ier & UART_IER_THRI)) {
  1174. up->ier |= UART_IER_THRI;
  1175. serial_out(up, UART_IER, up->ier);
  1176. if (up->bugs & UART_BUG_TXEN) {
  1177. unsigned char lsr;
  1178. lsr = serial_in(up, UART_LSR);
  1179. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1180. if ((up->port.type == PORT_RM9000) ?
  1181. (lsr & UART_LSR_THRE) :
  1182. (lsr & UART_LSR_TEMT))
  1183. transmit_chars(up);
  1184. }
  1185. }
  1186. /*
  1187. * Re-enable the transmitter if we disabled it.
  1188. */
  1189. if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
  1190. up->acr &= ~UART_ACR_TXDIS;
  1191. serial_icr_write(up, UART_ACR, up->acr);
  1192. }
  1193. }
  1194. static void serial8250_stop_rx(struct uart_port *port)
  1195. {
  1196. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1197. up->ier &= ~UART_IER_RLSI;
  1198. up->port.read_status_mask &= ~UART_LSR_DR;
  1199. serial_out(up, UART_IER, up->ier);
  1200. }
  1201. static void serial8250_enable_ms(struct uart_port *port)
  1202. {
  1203. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1204. /* no MSR capabilities */
  1205. if (up->bugs & UART_BUG_NOMSR)
  1206. return;
  1207. up->ier |= UART_IER_MSI;
  1208. serial_out(up, UART_IER, up->ier);
  1209. }
  1210. static void
  1211. receive_chars(struct uart_8250_port *up, unsigned int *status)
  1212. {
  1213. struct tty_struct *tty = up->port.state->port.tty;
  1214. unsigned char ch, lsr = *status;
  1215. int max_count = 256;
  1216. char flag;
  1217. do {
  1218. if (likely(lsr & UART_LSR_DR))
  1219. ch = serial_inp(up, UART_RX);
  1220. else
  1221. /*
  1222. * Intel 82571 has a Serial Over Lan device that will
  1223. * set UART_LSR_BI without setting UART_LSR_DR when
  1224. * it receives a break. To avoid reading from the
  1225. * receive buffer without UART_LSR_DR bit set, we
  1226. * just force the read character to be 0
  1227. */
  1228. ch = 0;
  1229. flag = TTY_NORMAL;
  1230. up->port.icount.rx++;
  1231. lsr |= up->lsr_saved_flags;
  1232. up->lsr_saved_flags = 0;
  1233. if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
  1234. /*
  1235. * For statistics only
  1236. */
  1237. if (lsr & UART_LSR_BI) {
  1238. lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  1239. up->port.icount.brk++;
  1240. /*
  1241. * We do the SysRQ and SAK checking
  1242. * here because otherwise the break
  1243. * may get masked by ignore_status_mask
  1244. * or read_status_mask.
  1245. */
  1246. if (uart_handle_break(&up->port))
  1247. goto ignore_char;
  1248. } else if (lsr & UART_LSR_PE)
  1249. up->port.icount.parity++;
  1250. else if (lsr & UART_LSR_FE)
  1251. up->port.icount.frame++;
  1252. if (lsr & UART_LSR_OE)
  1253. up->port.icount.overrun++;
  1254. /*
  1255. * Mask off conditions which should be ignored.
  1256. */
  1257. lsr &= up->port.read_status_mask;
  1258. if (lsr & UART_LSR_BI) {
  1259. DEBUG_INTR("handling break....");
  1260. flag = TTY_BREAK;
  1261. } else if (lsr & UART_LSR_PE)
  1262. flag = TTY_PARITY;
  1263. else if (lsr & UART_LSR_FE)
  1264. flag = TTY_FRAME;
  1265. }
  1266. if (uart_handle_sysrq_char(&up->port, ch))
  1267. goto ignore_char;
  1268. uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
  1269. ignore_char:
  1270. lsr = serial_inp(up, UART_LSR);
  1271. } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
  1272. spin_unlock(&up->port.lock);
  1273. tty_flip_buffer_push(tty);
  1274. spin_lock(&up->port.lock);
  1275. *status = lsr;
  1276. }
  1277. static void transmit_chars(struct uart_8250_port *up)
  1278. {
  1279. struct circ_buf *xmit = &up->port.state->xmit;
  1280. int count;
  1281. if (up->port.x_char) {
  1282. serial_outp(up, UART_TX, up->port.x_char);
  1283. up->port.icount.tx++;
  1284. up->port.x_char = 0;
  1285. return;
  1286. }
  1287. if (uart_tx_stopped(&up->port)) {
  1288. serial8250_stop_tx(&up->port);
  1289. return;
  1290. }
  1291. if (uart_circ_empty(xmit)) {
  1292. __stop_tx(up);
  1293. return;
  1294. }
  1295. count = up->tx_loadsz;
  1296. do {
  1297. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  1298. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  1299. up->port.icount.tx++;
  1300. if (uart_circ_empty(xmit))
  1301. break;
  1302. } while (--count > 0);
  1303. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1304. uart_write_wakeup(&up->port);
  1305. DEBUG_INTR("THRE...");
  1306. if (uart_circ_empty(xmit))
  1307. __stop_tx(up);
  1308. }
  1309. static unsigned int check_modem_status(struct uart_8250_port *up)
  1310. {
  1311. unsigned int status = serial_in(up, UART_MSR);
  1312. status |= up->msr_saved_flags;
  1313. up->msr_saved_flags = 0;
  1314. if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
  1315. up->port.state != NULL) {
  1316. if (status & UART_MSR_TERI)
  1317. up->port.icount.rng++;
  1318. if (status & UART_MSR_DDSR)
  1319. up->port.icount.dsr++;
  1320. if (status & UART_MSR_DDCD)
  1321. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  1322. if (status & UART_MSR_DCTS)
  1323. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  1324. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  1325. }
  1326. return status;
  1327. }
  1328. /*
  1329. * This handles the interrupt from one port.
  1330. */
  1331. static void serial8250_handle_port(struct uart_8250_port *up)
  1332. {
  1333. unsigned int status;
  1334. unsigned long flags;
  1335. spin_lock_irqsave(&up->port.lock, flags);
  1336. status = serial_inp(up, UART_LSR);
  1337. DEBUG_INTR("status = %x...", status);
  1338. if (status & (UART_LSR_DR | UART_LSR_BI))
  1339. receive_chars(up, &status);
  1340. check_modem_status(up);
  1341. if (status & UART_LSR_THRE)
  1342. transmit_chars(up);
  1343. spin_unlock_irqrestore(&up->port.lock, flags);
  1344. }
  1345. /*
  1346. * This is the serial driver's interrupt routine.
  1347. *
  1348. * Arjan thinks the old way was overly complex, so it got simplified.
  1349. * Alan disagrees, saying that need the complexity to handle the weird
  1350. * nature of ISA shared interrupts. (This is a special exception.)
  1351. *
  1352. * In order to handle ISA shared interrupts properly, we need to check
  1353. * that all ports have been serviced, and therefore the ISA interrupt
  1354. * line has been de-asserted.
  1355. *
  1356. * This means we need to loop through all ports. checking that they
  1357. * don't have an interrupt pending.
  1358. */
  1359. static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
  1360. {
  1361. struct irq_info *i = dev_id;
  1362. struct list_head *l, *end = NULL;
  1363. int pass_counter = 0, handled = 0;
  1364. DEBUG_INTR("serial8250_interrupt(%d)...", irq);
  1365. spin_lock(&i->lock);
  1366. l = i->head;
  1367. do {
  1368. struct uart_8250_port *up;
  1369. unsigned int iir;
  1370. up = list_entry(l, struct uart_8250_port, list);
  1371. iir = serial_in(up, UART_IIR);
  1372. if (!(iir & UART_IIR_NO_INT)) {
  1373. serial8250_handle_port(up);
  1374. handled = 1;
  1375. end = NULL;
  1376. } else if (up->port.iotype == UPIO_DWAPB &&
  1377. (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
  1378. /* The DesignWare APB UART has an Busy Detect (0x07)
  1379. * interrupt meaning an LCR write attempt occured while the
  1380. * UART was busy. The interrupt must be cleared by reading
  1381. * the UART status register (USR) and the LCR re-written. */
  1382. unsigned int status;
  1383. status = *(volatile u32 *)up->port.private_data;
  1384. serial_out(up, UART_LCR, up->lcr);
  1385. handled = 1;
  1386. end = NULL;
  1387. } else if (end == NULL)
  1388. end = l;
  1389. l = l->next;
  1390. if (l == i->head && pass_counter++ > PASS_LIMIT) {
  1391. /* If we hit this, we're dead. */
  1392. printk(KERN_ERR "serial8250: too much work for "
  1393. "irq%d\n", irq);
  1394. break;
  1395. }
  1396. } while (l != end);
  1397. spin_unlock(&i->lock);
  1398. DEBUG_INTR("end.\n");
  1399. return IRQ_RETVAL(handled);
  1400. }
  1401. /*
  1402. * To support ISA shared interrupts, we need to have one interrupt
  1403. * handler that ensures that the IRQ line has been deasserted
  1404. * before returning. Failing to do this will result in the IRQ
  1405. * line being stuck active, and, since ISA irqs are edge triggered,
  1406. * no more IRQs will be seen.
  1407. */
  1408. static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
  1409. {
  1410. spin_lock_irq(&i->lock);
  1411. if (!list_empty(i->head)) {
  1412. if (i->head == &up->list)
  1413. i->head = i->head->next;
  1414. list_del(&up->list);
  1415. } else {
  1416. BUG_ON(i->head != &up->list);
  1417. i->head = NULL;
  1418. }
  1419. spin_unlock_irq(&i->lock);
  1420. /* List empty so throw away the hash node */
  1421. if (i->head == NULL) {
  1422. hlist_del(&i->node);
  1423. kfree(i);
  1424. }
  1425. }
  1426. static int serial_link_irq_chain(struct uart_8250_port *up)
  1427. {
  1428. struct hlist_head *h;
  1429. struct hlist_node *n;
  1430. struct irq_info *i;
  1431. int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
  1432. mutex_lock(&hash_mutex);
  1433. h = &irq_lists[up->port.irq % NR_IRQ_HASH];
  1434. hlist_for_each(n, h) {
  1435. i = hlist_entry(n, struct irq_info, node);
  1436. if (i->irq == up->port.irq)
  1437. break;
  1438. }
  1439. if (n == NULL) {
  1440. i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
  1441. if (i == NULL) {
  1442. mutex_unlock(&hash_mutex);
  1443. return -ENOMEM;
  1444. }
  1445. spin_lock_init(&i->lock);
  1446. i->irq = up->port.irq;
  1447. hlist_add_head(&i->node, h);
  1448. }
  1449. mutex_unlock(&hash_mutex);
  1450. spin_lock_irq(&i->lock);
  1451. if (i->head) {
  1452. list_add(&up->list, i->head);
  1453. spin_unlock_irq(&i->lock);
  1454. ret = 0;
  1455. } else {
  1456. INIT_LIST_HEAD(&up->list);
  1457. i->head = &up->list;
  1458. spin_unlock_irq(&i->lock);
  1459. irq_flags |= up->port.irqflags;
  1460. ret = request_irq(up->port.irq, serial8250_interrupt,
  1461. irq_flags, "serial", i);
  1462. if (ret < 0)
  1463. serial_do_unlink(i, up);
  1464. }
  1465. return ret;
  1466. }
  1467. static void serial_unlink_irq_chain(struct uart_8250_port *up)
  1468. {
  1469. struct irq_info *i;
  1470. struct hlist_node *n;
  1471. struct hlist_head *h;
  1472. mutex_lock(&hash_mutex);
  1473. h = &irq_lists[up->port.irq % NR_IRQ_HASH];
  1474. hlist_for_each(n, h) {
  1475. i = hlist_entry(n, struct irq_info, node);
  1476. if (i->irq == up->port.irq)
  1477. break;
  1478. }
  1479. BUG_ON(n == NULL);
  1480. BUG_ON(i->head == NULL);
  1481. if (list_empty(i->head))
  1482. free_irq(up->port.irq, i);
  1483. serial_do_unlink(i, up);
  1484. mutex_unlock(&hash_mutex);
  1485. }
  1486. /* Base timer interval for polling */
  1487. static inline int poll_timeout(int timeout)
  1488. {
  1489. return timeout > 6 ? (timeout / 2 - 2) : 1;
  1490. }
  1491. /*
  1492. * This function is used to handle ports that do not have an
  1493. * interrupt. This doesn't work very well for 16450's, but gives
  1494. * barely passable results for a 16550A. (Although at the expense
  1495. * of much CPU overhead).
  1496. */
  1497. static void serial8250_timeout(unsigned long data)
  1498. {
  1499. struct uart_8250_port *up = (struct uart_8250_port *)data;
  1500. unsigned int iir;
  1501. iir = serial_in(up, UART_IIR);
  1502. if (!(iir & UART_IIR_NO_INT))
  1503. serial8250_handle_port(up);
  1504. mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
  1505. }
  1506. static void serial8250_backup_timeout(unsigned long data)
  1507. {
  1508. struct uart_8250_port *up = (struct uart_8250_port *)data;
  1509. unsigned int iir, ier = 0, lsr;
  1510. unsigned long flags;
  1511. /*
  1512. * Must disable interrupts or else we risk racing with the interrupt
  1513. * based handler.
  1514. */
  1515. if (is_real_interrupt(up->port.irq)) {
  1516. ier = serial_in(up, UART_IER);
  1517. serial_out(up, UART_IER, 0);
  1518. }
  1519. iir = serial_in(up, UART_IIR);
  1520. /*
  1521. * This should be a safe test for anyone who doesn't trust the
  1522. * IIR bits on their UART, but it's specifically designed for
  1523. * the "Diva" UART used on the management processor on many HP
  1524. * ia64 and parisc boxes.
  1525. */
  1526. spin_lock_irqsave(&up->port.lock, flags);
  1527. lsr = serial_in(up, UART_LSR);
  1528. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1529. spin_unlock_irqrestore(&up->port.lock, flags);
  1530. if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
  1531. (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
  1532. (lsr & UART_LSR_THRE)) {
  1533. iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
  1534. iir |= UART_IIR_THRI;
  1535. }
  1536. if (!(iir & UART_IIR_NO_INT))
  1537. serial8250_handle_port(up);
  1538. if (is_real_interrupt(up->port.irq))
  1539. serial_out(up, UART_IER, ier);
  1540. /* Standard timer interval plus 0.2s to keep the port running */
  1541. mod_timer(&up->timer,
  1542. jiffies + poll_timeout(up->port.timeout) + HZ / 5);
  1543. }
  1544. static unsigned int serial8250_tx_empty(struct uart_port *port)
  1545. {
  1546. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1547. unsigned long flags;
  1548. unsigned int lsr;
  1549. spin_lock_irqsave(&up->port.lock, flags);
  1550. lsr = serial_in(up, UART_LSR);
  1551. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1552. spin_unlock_irqrestore(&up->port.lock, flags);
  1553. return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
  1554. }
  1555. static unsigned int serial8250_get_mctrl(struct uart_port *port)
  1556. {
  1557. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1558. unsigned int status;
  1559. unsigned int ret;
  1560. status = check_modem_status(up);
  1561. ret = 0;
  1562. if (status & UART_MSR_DCD)
  1563. ret |= TIOCM_CAR;
  1564. if (status & UART_MSR_RI)
  1565. ret |= TIOCM_RNG;
  1566. if (status & UART_MSR_DSR)
  1567. ret |= TIOCM_DSR;
  1568. if (status & UART_MSR_CTS)
  1569. ret |= TIOCM_CTS;
  1570. return ret;
  1571. }
  1572. static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1573. {
  1574. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1575. unsigned char mcr = 0;
  1576. if (mctrl & TIOCM_RTS)
  1577. mcr |= UART_MCR_RTS;
  1578. if (mctrl & TIOCM_DTR)
  1579. mcr |= UART_MCR_DTR;
  1580. if (mctrl & TIOCM_OUT1)
  1581. mcr |= UART_MCR_OUT1;
  1582. if (mctrl & TIOCM_OUT2)
  1583. mcr |= UART_MCR_OUT2;
  1584. if (mctrl & TIOCM_LOOP)
  1585. mcr |= UART_MCR_LOOP;
  1586. mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
  1587. serial_out(up, UART_MCR, mcr);
  1588. }
  1589. static void serial8250_break_ctl(struct uart_port *port, int break_state)
  1590. {
  1591. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1592. unsigned long flags;
  1593. spin_lock_irqsave(&up->port.lock, flags);
  1594. if (break_state == -1)
  1595. up->lcr |= UART_LCR_SBC;
  1596. else
  1597. up->lcr &= ~UART_LCR_SBC;
  1598. serial_out(up, UART_LCR, up->lcr);
  1599. spin_unlock_irqrestore(&up->port.lock, flags);
  1600. }
  1601. /*
  1602. * Wait for transmitter & holding register to empty
  1603. */
  1604. static void wait_for_xmitr(struct uart_8250_port *up, int bits)
  1605. {
  1606. unsigned int status, tmout = 10000;
  1607. /* Wait up to 10ms for the character(s) to be sent. */
  1608. do {
  1609. status = serial_in(up, UART_LSR);
  1610. up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
  1611. if (--tmout == 0)
  1612. break;
  1613. udelay(1);
  1614. } while ((status & bits) != bits);
  1615. /* Wait up to 1s for flow control if necessary */
  1616. if (up->port.flags & UPF_CONS_FLOW) {
  1617. unsigned int tmout;
  1618. for (tmout = 1000000; tmout; tmout--) {
  1619. unsigned int msr = serial_in(up, UART_MSR);
  1620. up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
  1621. if (msr & UART_MSR_CTS)
  1622. break;
  1623. udelay(1);
  1624. touch_nmi_watchdog();
  1625. }
  1626. }
  1627. }
  1628. #ifdef CONFIG_CONSOLE_POLL
  1629. /*
  1630. * Console polling routines for writing and reading from the uart while
  1631. * in an interrupt or debug context.
  1632. */
  1633. static int serial8250_get_poll_char(struct uart_port *port)
  1634. {
  1635. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1636. unsigned char lsr = serial_inp(up, UART_LSR);
  1637. while (!(lsr & UART_LSR_DR))
  1638. lsr = serial_inp(up, UART_LSR);
  1639. return serial_inp(up, UART_RX);
  1640. }
  1641. static void serial8250_put_poll_char(struct uart_port *port,
  1642. unsigned char c)
  1643. {
  1644. unsigned int ier;
  1645. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1646. /*
  1647. * First save the IER then disable the interrupts
  1648. */
  1649. ier = serial_in(up, UART_IER);
  1650. if (up->capabilities & UART_CAP_UUE)
  1651. serial_out(up, UART_IER, UART_IER_UUE);
  1652. else
  1653. serial_out(up, UART_IER, 0);
  1654. wait_for_xmitr(up, BOTH_EMPTY);
  1655. /*
  1656. * Send the character out.
  1657. * If a LF, also do CR...
  1658. */
  1659. serial_out(up, UART_TX, c);
  1660. if (c == 10) {
  1661. wait_for_xmitr(up, BOTH_EMPTY);
  1662. serial_out(up, UART_TX, 13);
  1663. }
  1664. /*
  1665. * Finally, wait for transmitter to become empty
  1666. * and restore the IER
  1667. */
  1668. wait_for_xmitr(up, BOTH_EMPTY);
  1669. serial_out(up, UART_IER, ier);
  1670. }
  1671. #endif /* CONFIG_CONSOLE_POLL */
  1672. static int serial8250_startup(struct uart_port *port)
  1673. {
  1674. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1675. unsigned long flags;
  1676. unsigned char lsr, iir;
  1677. int retval;
  1678. up->capabilities = uart_config[up->port.type].flags;
  1679. up->mcr = 0;
  1680. if (up->port.iotype != up->cur_iotype)
  1681. set_io_from_upio(port);
  1682. if (up->port.type == PORT_16C950) {
  1683. /* Wake up and initialize UART */
  1684. up->acr = 0;
  1685. serial_outp(up, UART_LCR, 0xBF);
  1686. serial_outp(up, UART_EFR, UART_EFR_ECB);
  1687. serial_outp(up, UART_IER, 0);
  1688. serial_outp(up, UART_LCR, 0);
  1689. serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
  1690. serial_outp(up, UART_LCR, 0xBF);
  1691. serial_outp(up, UART_EFR, UART_EFR_ECB);
  1692. serial_outp(up, UART_LCR, 0);
  1693. }
  1694. #ifdef CONFIG_SERIAL_8250_RSA
  1695. /*
  1696. * If this is an RSA port, see if we can kick it up to the
  1697. * higher speed clock.
  1698. */
  1699. enable_rsa(up);
  1700. #endif
  1701. /*
  1702. * Clear the FIFO buffers and disable them.
  1703. * (they will be reenabled in set_termios())
  1704. */
  1705. serial8250_clear_fifos(up);
  1706. /*
  1707. * Clear the interrupt registers.
  1708. */
  1709. (void) serial_inp(up, UART_LSR);
  1710. (void) serial_inp(up, UART_RX);
  1711. (void) serial_inp(up, UART_IIR);
  1712. (void) serial_inp(up, UART_MSR);
  1713. /*
  1714. * At this point, there's no way the LSR could still be 0xff;
  1715. * if it is, then bail out, because there's likely no UART
  1716. * here.
  1717. */
  1718. if (!(up->port.flags & UPF_BUGGY_UART) &&
  1719. (serial_inp(up, UART_LSR) == 0xff)) {
  1720. printk(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
  1721. serial_index(&up->port));
  1722. return -ENODEV;
  1723. }
  1724. /*
  1725. * For a XR16C850, we need to set the trigger levels
  1726. */
  1727. if (up->port.type == PORT_16850) {
  1728. unsigned char fctr;
  1729. serial_outp(up, UART_LCR, 0xbf);
  1730. fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
  1731. serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
  1732. serial_outp(up, UART_TRG, UART_TRG_96);
  1733. serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
  1734. serial_outp(up, UART_TRG, UART_TRG_96);
  1735. serial_outp(up, UART_LCR, 0);
  1736. }
  1737. if (is_real_interrupt(up->port.irq)) {
  1738. unsigned char iir1;
  1739. /*
  1740. * Test for UARTs that do not reassert THRE when the
  1741. * transmitter is idle and the interrupt has already
  1742. * been cleared. Real 16550s should always reassert
  1743. * this interrupt whenever the transmitter is idle and
  1744. * the interrupt is enabled. Delays are necessary to
  1745. * allow register changes to become visible.
  1746. */
  1747. spin_lock_irqsave(&up->port.lock, flags);
  1748. if (up->port.irqflags & IRQF_SHARED)
  1749. disable_irq_nosync(up->port.irq);
  1750. wait_for_xmitr(up, UART_LSR_THRE);
  1751. serial_out_sync(up, UART_IER, UART_IER_THRI);
  1752. udelay(1); /* allow THRE to set */
  1753. iir1 = serial_in(up, UART_IIR);
  1754. serial_out(up, UART_IER, 0);
  1755. serial_out_sync(up, UART_IER, UART_IER_THRI);
  1756. udelay(1); /* allow a working UART time to re-assert THRE */
  1757. iir = serial_in(up, UART_IIR);
  1758. serial_out(up, UART_IER, 0);
  1759. if (up->port.irqflags & IRQF_SHARED)
  1760. enable_irq(up->port.irq);
  1761. spin_unlock_irqrestore(&up->port.lock, flags);
  1762. /*
  1763. * If the interrupt is not reasserted, setup a timer to
  1764. * kick the UART on a regular basis.
  1765. */
  1766. if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
  1767. up->bugs |= UART_BUG_THRE;
  1768. pr_debug("ttyS%d - using backup timer\n",
  1769. serial_index(port));
  1770. }
  1771. }
  1772. /*
  1773. * The above check will only give an accurate result the first time
  1774. * the port is opened so this value needs to be preserved.
  1775. */
  1776. if (up->bugs & UART_BUG_THRE) {
  1777. up->timer.function = serial8250_backup_timeout;
  1778. up->timer.data = (unsigned long)up;
  1779. mod_timer(&up->timer, jiffies +
  1780. poll_timeout(up->port.timeout) + HZ / 5);
  1781. }
  1782. /*
  1783. * If the "interrupt" for this port doesn't correspond with any
  1784. * hardware interrupt, we use a timer-based system. The original
  1785. * driver used to do this with IRQ0.
  1786. */
  1787. if (!is_real_interrupt(up->port.irq)) {
  1788. up->timer.data = (unsigned long)up;
  1789. mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
  1790. } else {
  1791. retval = serial_link_irq_chain(up);
  1792. if (retval)
  1793. return retval;
  1794. }
  1795. /*
  1796. * Now, initialize the UART
  1797. */
  1798. serial_outp(up, UART_LCR, UART_LCR_WLEN8);
  1799. spin_lock_irqsave(&up->port.lock, flags);
  1800. if (up->port.flags & UPF_FOURPORT) {
  1801. if (!is_real_interrupt(up->port.irq))
  1802. up->port.mctrl |= TIOCM_OUT1;
  1803. } else
  1804. /*
  1805. * Most PC uarts need OUT2 raised to enable interrupts.
  1806. */
  1807. if (is_real_interrupt(up->port.irq))
  1808. up->port.mctrl |= TIOCM_OUT2;
  1809. serial8250_set_mctrl(&up->port, up->port.mctrl);
  1810. /* Serial over Lan (SoL) hack:
  1811. Intel 8257x Gigabit ethernet chips have a
  1812. 16550 emulation, to be used for Serial Over Lan.
  1813. Those chips take a longer time than a normal
  1814. serial device to signalize that a transmission
  1815. data was queued. Due to that, the above test generally
  1816. fails. One solution would be to delay the reading of
  1817. iir. However, this is not reliable, since the timeout
  1818. is variable. So, let's just don't test if we receive
  1819. TX irq. This way, we'll never enable UART_BUG_TXEN.
  1820. */
  1821. if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
  1822. goto dont_test_tx_en;
  1823. /*
  1824. * Do a quick test to see if we receive an
  1825. * interrupt when we enable the TX irq.
  1826. */
  1827. serial_outp(up, UART_IER, UART_IER_THRI);
  1828. lsr = serial_in(up, UART_LSR);
  1829. iir = serial_in(up, UART_IIR);
  1830. serial_outp(up, UART_IER, 0);
  1831. if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
  1832. if (!(up->bugs & UART_BUG_TXEN)) {
  1833. up->bugs |= UART_BUG_TXEN;
  1834. pr_debug("ttyS%d - enabling bad tx status workarounds\n",
  1835. serial_index(port));
  1836. }
  1837. } else {
  1838. up->bugs &= ~UART_BUG_TXEN;
  1839. }
  1840. dont_test_tx_en:
  1841. spin_unlock_irqrestore(&up->port.lock, flags);
  1842. /*
  1843. * Clear the interrupt registers again for luck, and clear the
  1844. * saved flags to avoid getting false values from polling
  1845. * routines or the previous session.
  1846. */
  1847. serial_inp(up, UART_LSR);
  1848. serial_inp(up, UART_RX);
  1849. serial_inp(up, UART_IIR);
  1850. serial_inp(up, UART_MSR);
  1851. up->lsr_saved_flags = 0;
  1852. up->msr_saved_flags = 0;
  1853. /*
  1854. * Finally, enable interrupts. Note: Modem status interrupts
  1855. * are set via set_termios(), which will be occurring imminently
  1856. * anyway, so we don't enable them here.
  1857. */
  1858. up->ier = UART_IER_RLSI | UART_IER_RDI;
  1859. serial_outp(up, UART_IER, up->ier);
  1860. if (up->port.flags & UPF_FOURPORT) {
  1861. unsigned int icp;
  1862. /*
  1863. * Enable interrupts on the AST Fourport board
  1864. */
  1865. icp = (up->port.iobase & 0xfe0) | 0x01f;
  1866. outb_p(0x80, icp);
  1867. (void) inb_p(icp);
  1868. }
  1869. return 0;
  1870. }
  1871. static void serial8250_shutdown(struct uart_port *port)
  1872. {
  1873. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1874. unsigned long flags;
  1875. /*
  1876. * Disable interrupts from this port
  1877. */
  1878. up->ier = 0;
  1879. serial_outp(up, UART_IER, 0);
  1880. spin_lock_irqsave(&up->port.lock, flags);
  1881. if (up->port.flags & UPF_FOURPORT) {
  1882. /* reset interrupts on the AST Fourport board */
  1883. inb((up->port.iobase & 0xfe0) | 0x1f);
  1884. up->port.mctrl |= TIOCM_OUT1;
  1885. } else
  1886. up->port.mctrl &= ~TIOCM_OUT2;
  1887. serial8250_set_mctrl(&up->port, up->port.mctrl);
  1888. spin_unlock_irqrestore(&up->port.lock, flags);
  1889. /*
  1890. * Disable break condition and FIFOs
  1891. */
  1892. serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
  1893. serial8250_clear_fifos(up);
  1894. #ifdef CONFIG_SERIAL_8250_RSA
  1895. /*
  1896. * Reset the RSA board back to 115kbps compat mode.
  1897. */
  1898. disable_rsa(up);
  1899. #endif
  1900. /*
  1901. * Read data port to reset things, and then unlink from
  1902. * the IRQ chain.
  1903. */
  1904. (void) serial_in(up, UART_RX);
  1905. del_timer_sync(&up->timer);
  1906. up->timer.function = serial8250_timeout;
  1907. if (is_real_interrupt(up->port.irq))
  1908. serial_unlink_irq_chain(up);
  1909. }
  1910. static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
  1911. {
  1912. unsigned int quot;
  1913. /*
  1914. * Handle magic divisors for baud rates above baud_base on
  1915. * SMSC SuperIO chips.
  1916. */
  1917. if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  1918. baud == (port->uartclk/4))
  1919. quot = 0x8001;
  1920. else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  1921. baud == (port->uartclk/8))
  1922. quot = 0x8002;
  1923. else
  1924. quot = uart_get_divisor(port, baud);
  1925. return quot;
  1926. }
  1927. static void
  1928. serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
  1929. struct ktermios *old)
  1930. {
  1931. struct uart_8250_port *up = (struct uart_8250_port *)port;
  1932. unsigned char cval, fcr = 0;
  1933. unsigned long flags;
  1934. unsigned int baud, quot;
  1935. switch (termios->c_cflag & CSIZE) {
  1936. case CS5:
  1937. cval = UART_LCR_WLEN5;
  1938. break;
  1939. case CS6:
  1940. cval = UART_LCR_WLEN6;
  1941. break;
  1942. case CS7:
  1943. cval = UART_LCR_WLEN7;
  1944. break;
  1945. default:
  1946. case CS8:
  1947. cval = UART_LCR_WLEN8;
  1948. break;
  1949. }
  1950. if (termios->c_cflag & CSTOPB)
  1951. cval |= UART_LCR_STOP;
  1952. if (termios->c_cflag & PARENB)
  1953. cval |= UART_LCR_PARITY;
  1954. if (!(termios->c_cflag & PARODD))
  1955. cval |= UART_LCR_EPAR;
  1956. #ifdef CMSPAR
  1957. if (termios->c_cflag & CMSPAR)
  1958. cval |= UART_LCR_SPAR;
  1959. #endif
  1960. /*
  1961. * Ask the core to calculate the divisor for us.
  1962. */
  1963. baud = uart_get_baud_rate(port, termios, old,
  1964. port->uartclk / 16 / 0xffff,
  1965. port->uartclk / 16);
  1966. quot = serial8250_get_divisor(port, baud);
  1967. /*
  1968. * Oxford Semi 952 rev B workaround
  1969. */
  1970. if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
  1971. quot++;
  1972. if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
  1973. if (baud < 2400)
  1974. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
  1975. else
  1976. fcr = uart_config[up->port.type].fcr;
  1977. }
  1978. /*
  1979. * MCR-based auto flow control. When AFE is enabled, RTS will be
  1980. * deasserted when the receive FIFO contains more characters than
  1981. * the trigger, or the MCR RTS bit is cleared. In the case where
  1982. * the remote UART is not using CTS auto flow control, we must
  1983. * have sufficient FIFO entries for the latency of the remote
  1984. * UART to respond. IOW, at least 32 bytes of FIFO.
  1985. */
  1986. if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
  1987. up->mcr &= ~UART_MCR_AFE;
  1988. if (termios->c_cflag & CRTSCTS)
  1989. up->mcr |= UART_MCR_AFE;
  1990. }
  1991. /*
  1992. * Ok, we're now changing the port state. Do it with
  1993. * interrupts disabled.
  1994. */
  1995. spin_lock_irqsave(&up->port.lock, flags);
  1996. /*
  1997. * Update the per-port timeout.
  1998. */
  1999. uart_update_timeout(port, termios->c_cflag, baud);
  2000. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  2001. if (termios->c_iflag & INPCK)
  2002. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  2003. if (termios->c_iflag & (BRKINT | PARMRK))
  2004. up->port.read_status_mask |= UART_LSR_BI;
  2005. /*
  2006. * Characteres to ignore
  2007. */
  2008. up->port.ignore_status_mask = 0;
  2009. if (termios->c_iflag & IGNPAR)
  2010. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  2011. if (termios->c_iflag & IGNBRK) {
  2012. up->port.ignore_status_mask |= UART_LSR_BI;
  2013. /*
  2014. * If we're ignoring parity and break indicators,
  2015. * ignore overruns too (for real raw support).
  2016. */
  2017. if (termios->c_iflag & IGNPAR)
  2018. up->port.ignore_status_mask |= UART_LSR_OE;
  2019. }
  2020. /*
  2021. * ignore all characters if CREAD is not set
  2022. */
  2023. if ((termios->c_cflag & CREAD) == 0)
  2024. up->port.ignore_status_mask |= UART_LSR_DR;
  2025. /*
  2026. * CTS flow control flag and modem status interrupts
  2027. */
  2028. up->ier &= ~UART_IER_MSI;
  2029. if (!(up->bugs & UART_BUG_NOMSR) &&
  2030. UART_ENABLE_MS(&up->port, termios->c_cflag))
  2031. up->ier |= UART_IER_MSI;
  2032. if (up->capabilities & UART_CAP_UUE)
  2033. up->ier |= UART_IER_UUE | UART_IER_RTOIE;
  2034. serial_out(up, UART_IER, up->ier);
  2035. if (up->capabilities & UART_CAP_EFR) {
  2036. unsigned char efr = 0;
  2037. /*
  2038. * TI16C752/Startech hardware flow control. FIXME:
  2039. * - TI16C752 requires control thresholds to be set.
  2040. * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
  2041. */
  2042. if (termios->c_cflag & CRTSCTS)
  2043. efr |= UART_EFR_CTS;
  2044. serial_outp(up, UART_LCR, 0xBF);
  2045. serial_outp(up, UART_EFR, efr);
  2046. }
  2047. #ifdef CONFIG_ARCH_OMAP
  2048. /* Workaround to enable 115200 baud on OMAP1510 internal ports */
  2049. if (cpu_is_omap1510() && is_omap_port(up)) {
  2050. if (baud == 115200) {
  2051. quot = 1;
  2052. serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
  2053. } else
  2054. serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
  2055. }
  2056. #endif
  2057. if (up->capabilities & UART_NATSEMI) {
  2058. /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
  2059. serial_outp(up, UART_LCR, 0xe0);
  2060. } else {
  2061. serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
  2062. }
  2063. serial_dl_write(up, quot);
  2064. /*
  2065. * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
  2066. * is written without DLAB set, this mode will be disabled.
  2067. */
  2068. if (up->port.type == PORT_16750)
  2069. serial_outp(up, UART_FCR, fcr);
  2070. serial_outp(up, UART_LCR, cval); /* reset DLAB */
  2071. up->lcr = cval; /* Save LCR */
  2072. if (up->port.type != PORT_16750) {
  2073. if (fcr & UART_FCR_ENABLE_FIFO) {
  2074. /* emulated UARTs (Lucent Venus 167x) need two steps */
  2075. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  2076. }
  2077. serial_outp(up, UART_FCR, fcr); /* set fcr */
  2078. }
  2079. serial8250_set_mctrl(&up->port, up->port.mctrl);
  2080. spin_unlock_irqrestore(&up->port.lock, flags);
  2081. /* Don't rewrite B0 */
  2082. if (tty_termios_baud_rate(termios))
  2083. tty_termios_encode_baud_rate(termios, baud, baud);
  2084. }
  2085. static void
  2086. serial8250_pm(struct uart_port *port, unsigned int state,
  2087. unsigned int oldstate)
  2088. {
  2089. struct uart_8250_port *p = (struct uart_8250_port *)port;
  2090. serial8250_set_sleep(p, state != 0);
  2091. if (p->pm)
  2092. p->pm(port, state, oldstate);
  2093. }
  2094. static unsigned int serial8250_port_size(struct uart_8250_port *pt)
  2095. {
  2096. if (pt->port.iotype == UPIO_AU)
  2097. return 0x1000;
  2098. #ifdef CONFIG_ARCH_OMAP
  2099. if (is_omap_port(pt))
  2100. return 0x16 << pt->port.regshift;
  2101. #endif
  2102. return 8 << pt->port.regshift;
  2103. }
  2104. /*
  2105. * Resource handling.
  2106. */
  2107. static int serial8250_request_std_resource(struct uart_8250_port *up)
  2108. {
  2109. unsigned int size = serial8250_port_size(up);
  2110. int ret = 0;
  2111. switch (up->port.iotype) {
  2112. case UPIO_AU:
  2113. case UPIO_TSI:
  2114. case UPIO_MEM32:
  2115. case UPIO_MEM:
  2116. case UPIO_DWAPB:
  2117. if (!up->port.mapbase)
  2118. break;
  2119. if (!request_mem_region(up->port.mapbase, size, "serial")) {
  2120. ret = -EBUSY;
  2121. break;
  2122. }
  2123. if (up->port.flags & UPF_IOREMAP) {
  2124. up->port.membase = ioremap_nocache(up->port.mapbase,
  2125. size);
  2126. if (!up->port.membase) {
  2127. release_mem_region(up->port.mapbase, size);
  2128. ret = -ENOMEM;
  2129. }
  2130. }
  2131. break;
  2132. case UPIO_HUB6:
  2133. case UPIO_PORT:
  2134. if (!request_region(up->port.iobase, size, "serial"))
  2135. ret = -EBUSY;
  2136. break;
  2137. }
  2138. return ret;
  2139. }
  2140. static void serial8250_release_std_resource(struct uart_8250_port *up)
  2141. {
  2142. unsigned int size = serial8250_port_size(up);
  2143. switch (up->port.iotype) {
  2144. case UPIO_AU:
  2145. case UPIO_TSI:
  2146. case UPIO_MEM32:
  2147. case UPIO_MEM:
  2148. case UPIO_DWAPB:
  2149. if (!up->port.mapbase)
  2150. break;
  2151. if (up->port.flags & UPF_IOREMAP) {
  2152. iounmap(up->port.membase);
  2153. up->port.membase = NULL;
  2154. }
  2155. release_mem_region(up->port.mapbase, size);
  2156. break;
  2157. case UPIO_HUB6:
  2158. case UPIO_PORT:
  2159. release_region(up->port.iobase, size);
  2160. break;
  2161. }
  2162. }
  2163. static int serial8250_request_rsa_resource(struct uart_8250_port *up)
  2164. {
  2165. unsigned long start = UART_RSA_BASE << up->port.regshift;
  2166. unsigned int size = 8 << up->port.regshift;
  2167. int ret = -EINVAL;
  2168. switch (up->port.iotype) {
  2169. case UPIO_HUB6:
  2170. case UPIO_PORT:
  2171. start += up->port.iobase;
  2172. if (request_region(start, size, "serial-rsa"))
  2173. ret = 0;
  2174. else
  2175. ret = -EBUSY;
  2176. break;
  2177. }
  2178. return ret;
  2179. }
  2180. static void serial8250_release_rsa_resource(struct uart_8250_port *up)
  2181. {
  2182. unsigned long offset = UART_RSA_BASE << up->port.regshift;
  2183. unsigned int size = 8 << up->port.regshift;
  2184. switch (up->port.iotype) {
  2185. case UPIO_HUB6:
  2186. case UPIO_PORT:
  2187. release_region(up->port.iobase + offset, size);
  2188. break;
  2189. }
  2190. }
  2191. static void serial8250_release_port(struct uart_port *port)
  2192. {
  2193. struct uart_8250_port *up = (struct uart_8250_port *)port;
  2194. serial8250_release_std_resource(up);
  2195. if (up->port.type == PORT_RSA)
  2196. serial8250_release_rsa_resource(up);
  2197. }
  2198. static int serial8250_request_port(struct uart_port *port)
  2199. {
  2200. struct uart_8250_port *up = (struct uart_8250_port *)port;
  2201. int ret = 0;
  2202. ret = serial8250_request_std_resource(up);
  2203. if (ret == 0 && up->port.type == PORT_RSA) {
  2204. ret = serial8250_request_rsa_resource(up);
  2205. if (ret < 0)
  2206. serial8250_release_std_resource(up);
  2207. }
  2208. return ret;
  2209. }
  2210. static void serial8250_config_port(struct uart_port *port, int flags)
  2211. {
  2212. struct uart_8250_port *up = (struct uart_8250_port *)port;
  2213. int probeflags = PROBE_ANY;
  2214. int ret;
  2215. /*
  2216. * Find the region that we can probe for. This in turn
  2217. * tells us whether we can probe for the type of port.
  2218. */
  2219. ret = serial8250_request_std_resource(up);
  2220. if (ret < 0)
  2221. return;
  2222. ret = serial8250_request_rsa_resource(up);
  2223. if (ret < 0)
  2224. probeflags &= ~PROBE_RSA;
  2225. if (up->port.iotype != up->cur_iotype)
  2226. set_io_from_upio(port);
  2227. if (flags & UART_CONFIG_TYPE)
  2228. autoconfig(up, probeflags);
  2229. #ifdef CONFIG_SERIAL_8250_AU1X00
  2230. /* if access method is AU, it is a 16550 with a quirk */
  2231. if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
  2232. up->bugs |= UART_BUG_NOMSR;
  2233. #endif
  2234. if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
  2235. autoconfig_irq(up);
  2236. if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
  2237. serial8250_release_rsa_resource(up);
  2238. if (up->port.type == PORT_UNKNOWN)
  2239. serial8250_release_std_resource(up);
  2240. }
  2241. static int
  2242. serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
  2243. {
  2244. if (ser->irq >= nr_irqs || ser->irq < 0 ||
  2245. ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
  2246. ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
  2247. ser->type == PORT_STARTECH)
  2248. return -EINVAL;
  2249. return 0;
  2250. }
  2251. static const char *
  2252. serial8250_type(struct uart_port *port)
  2253. {
  2254. int type = port->type;
  2255. if (type >= ARRAY_SIZE(uart_config))
  2256. type = 0;
  2257. return uart_config[type].name;
  2258. }
  2259. static struct uart_ops serial8250_pops = {
  2260. .tx_empty = serial8250_tx_empty,
  2261. .set_mctrl = serial8250_set_mctrl,
  2262. .get_mctrl = serial8250_get_mctrl,
  2263. .stop_tx = serial8250_stop_tx,
  2264. .start_tx = serial8250_start_tx,
  2265. .stop_rx = serial8250_stop_rx,
  2266. .enable_ms = serial8250_enable_ms,
  2267. .break_ctl = serial8250_break_ctl,
  2268. .startup = serial8250_startup,
  2269. .shutdown = serial8250_shutdown,
  2270. .set_termios = serial8250_set_termios,
  2271. .pm = serial8250_pm,
  2272. .type = serial8250_type,
  2273. .release_port = serial8250_release_port,
  2274. .request_port = serial8250_request_port,
  2275. .config_port = serial8250_config_port,
  2276. .verify_port = serial8250_verify_port,
  2277. #ifdef CONFIG_CONSOLE_POLL
  2278. .poll_get_char = serial8250_get_poll_char,
  2279. .poll_put_char = serial8250_put_poll_char,
  2280. #endif
  2281. };
  2282. static struct uart_8250_port serial8250_ports[UART_NR];
  2283. static void __init serial8250_isa_init_ports(void)
  2284. {
  2285. struct uart_8250_port *up;
  2286. static int first = 1;
  2287. int i, irqflag = 0;
  2288. if (!first)
  2289. return;
  2290. first = 0;
  2291. for (i = 0; i < nr_uarts; i++) {
  2292. struct uart_8250_port *up = &serial8250_ports[i];
  2293. up->port.line = i;
  2294. spin_lock_init(&up->port.lock);
  2295. init_timer(&up->timer);
  2296. up->timer.function = serial8250_timeout;
  2297. /*
  2298. * ALPHA_KLUDGE_MCR needs to be killed.
  2299. */
  2300. up->mcr_mask = ~ALPHA_KLUDGE_MCR;
  2301. up->mcr_force = ALPHA_KLUDGE_MCR;
  2302. up->port.ops = &serial8250_pops;
  2303. }
  2304. if (share_irqs)
  2305. irqflag = IRQF_SHARED;
  2306. for (i = 0, up = serial8250_ports;
  2307. i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
  2308. i++, up++) {
  2309. up->port.iobase = old_serial_port[i].port;
  2310. up->port.irq = irq_canonicalize(old_serial_port[i].irq);
  2311. up->port.irqflags = old_serial_port[i].irqflags;
  2312. up->port.uartclk = old_serial_port[i].baud_base * 16;
  2313. up->port.flags = old_serial_port[i].flags;
  2314. up->port.hub6 = old_serial_port[i].hub6;
  2315. up->port.membase = old_serial_port[i].iomem_base;
  2316. up->port.iotype = old_serial_port[i].io_type;
  2317. up->port.regshift = old_serial_port[i].iomem_reg_shift;
  2318. set_io_from_upio(&up->port);
  2319. up->port.irqflags |= irqflag;
  2320. }
  2321. }
  2322. static void
  2323. serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
  2324. {
  2325. up->port.type = type;
  2326. up->port.fifosize = uart_config[type].fifo_size;
  2327. up->capabilities = uart_config[type].flags;
  2328. up->tx_loadsz = uart_config[type].tx_loadsz;
  2329. }
  2330. static void __init
  2331. serial8250_register_ports(struct uart_driver *drv, struct device *dev)
  2332. {
  2333. int i;
  2334. for (i = 0; i < nr_uarts; i++) {
  2335. struct uart_8250_port *up = &serial8250_ports[i];
  2336. up->cur_iotype = 0xFF;
  2337. }
  2338. serial8250_isa_init_ports();
  2339. for (i = 0; i < nr_uarts; i++) {
  2340. struct uart_8250_port *up = &serial8250_ports[i];
  2341. up->port.dev = dev;
  2342. if (up->port.flags & UPF_FIXED_TYPE)
  2343. serial8250_init_fixed_type_port(up, up->port.type);
  2344. uart_add_one_port(drv, &up->port);
  2345. }
  2346. }
  2347. #ifdef CONFIG_SERIAL_8250_CONSOLE
  2348. static void serial8250_console_putchar(struct uart_port *port, int ch)
  2349. {
  2350. struct uart_8250_port *up = (struct uart_8250_port *)port;
  2351. wait_for_xmitr(up, UART_LSR_THRE);
  2352. serial_out(up, UART_TX, ch);
  2353. }
  2354. /*
  2355. * Print a string to the serial port trying not to disturb
  2356. * any possible real use of the port...
  2357. *
  2358. * The console_lock must be held when we get here.
  2359. */
  2360. static void
  2361. serial8250_console_write(struct console *co, const char *s, unsigned int count)
  2362. {
  2363. struct uart_8250_port *up = &serial8250_ports[co->index];
  2364. unsigned long flags;
  2365. unsigned int ier;
  2366. int locked = 1;
  2367. touch_nmi_watchdog();
  2368. local_irq_save(flags);
  2369. if (up->port.sysrq) {
  2370. /* serial8250_handle_port() already took the lock */
  2371. locked = 0;
  2372. } else if (oops_in_progress) {
  2373. locked = spin_trylock(&up->port.lock);
  2374. } else
  2375. spin_lock(&up->port.lock);
  2376. /*
  2377. * First save the IER then disable the interrupts
  2378. */
  2379. ier = serial_in(up, UART_IER);
  2380. if (up->capabilities & UART_CAP_UUE)
  2381. serial_out(up, UART_IER, UART_IER_UUE);
  2382. else
  2383. serial_out(up, UART_IER, 0);
  2384. uart_console_write(&up->port, s, count, serial8250_console_putchar);
  2385. /*
  2386. * Finally, wait for transmitter to become empty
  2387. * and restore the IER
  2388. */
  2389. wait_for_xmitr(up, BOTH_EMPTY);
  2390. serial_out(up, UART_IER, ier);
  2391. /*
  2392. * The receive handling will happen properly because the
  2393. * receive ready bit will still be set; it is not cleared
  2394. * on read. However, modem control will not, we must
  2395. * call it if we have saved something in the saved flags
  2396. * while processing with interrupts off.
  2397. */
  2398. if (up->msr_saved_flags)
  2399. check_modem_status(up);
  2400. if (locked)
  2401. spin_unlock(&up->port.lock);
  2402. local_irq_restore(flags);
  2403. }
  2404. static int __init serial8250_console_setup(struct console *co, char *options)
  2405. {
  2406. struct uart_port *port;
  2407. int baud = 9600;
  2408. int bits = 8;
  2409. int parity = 'n';
  2410. int flow = 'n';
  2411. /*
  2412. * Check whether an invalid uart number has been specified, and
  2413. * if so, search for the first available port that does have
  2414. * console support.
  2415. */
  2416. if (co->index >= nr_uarts)
  2417. co->index = 0;
  2418. port = &serial8250_ports[co->index].port;
  2419. if (!port->iobase && !port->membase)
  2420. return -ENODEV;
  2421. if (options)
  2422. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2423. return uart_set_options(port, co, baud, parity, bits, flow);
  2424. }
  2425. static int serial8250_console_early_setup(void)
  2426. {
  2427. return serial8250_find_port_for_earlycon();
  2428. }
  2429. static struct console serial8250_console = {
  2430. .name = "ttyS",
  2431. .write = serial8250_console_write,
  2432. .device = uart_console_device,
  2433. .setup = serial8250_console_setup,
  2434. .early_setup = serial8250_console_early_setup,
  2435. .flags = CON_PRINTBUFFER,
  2436. .index = -1,
  2437. .data = &serial8250_reg,
  2438. };
  2439. static int __init serial8250_console_init(void)
  2440. {
  2441. if (nr_uarts > UART_NR)
  2442. nr_uarts = UART_NR;
  2443. serial8250_isa_init_ports();
  2444. register_console(&serial8250_console);
  2445. return 0;
  2446. }
  2447. console_initcall(serial8250_console_init);
  2448. int serial8250_find_port(struct uart_port *p)
  2449. {
  2450. int line;
  2451. struct uart_port *port;
  2452. for (line = 0; line < nr_uarts; line++) {
  2453. port = &serial8250_ports[line].port;
  2454. if (uart_match_port(p, port))
  2455. return line;
  2456. }
  2457. return -ENODEV;
  2458. }
  2459. #define SERIAL8250_CONSOLE &serial8250_console
  2460. #else
  2461. #define SERIAL8250_CONSOLE NULL
  2462. #endif
  2463. static struct uart_driver serial8250_reg = {
  2464. .owner = THIS_MODULE,
  2465. .driver_name = "serial",
  2466. .dev_name = "ttyS",
  2467. .major = TTY_MAJOR,
  2468. .minor = 64,
  2469. .cons = SERIAL8250_CONSOLE,
  2470. };
  2471. /*
  2472. * early_serial_setup - early registration for 8250 ports
  2473. *
  2474. * Setup an 8250 port structure prior to console initialisation. Use
  2475. * after console initialisation will cause undefined behaviour.
  2476. */
  2477. int __init early_serial_setup(struct uart_port *port)
  2478. {
  2479. struct uart_port *p;
  2480. if (port->line >= ARRAY_SIZE(serial8250_ports))
  2481. return -ENODEV;
  2482. serial8250_isa_init_ports();
  2483. p = &serial8250_ports[port->line].port;
  2484. p->iobase = port->iobase;
  2485. p->membase = port->membase;
  2486. p->irq = port->irq;
  2487. p->irqflags = port->irqflags;
  2488. p->uartclk = port->uartclk;
  2489. p->fifosize = port->fifosize;
  2490. p->regshift = port->regshift;
  2491. p->iotype = port->iotype;
  2492. p->flags = port->flags;
  2493. p->mapbase = port->mapbase;
  2494. p->private_data = port->private_data;
  2495. p->type = port->type;
  2496. p->line = port->line;
  2497. set_io_from_upio(p);
  2498. if (port->serial_in)
  2499. p->serial_in = port->serial_in;
  2500. if (port->serial_out)
  2501. p->serial_out = port->serial_out;
  2502. return 0;
  2503. }
  2504. /**
  2505. * serial8250_suspend_port - suspend one serial port
  2506. * @line: serial line number
  2507. *
  2508. * Suspend one serial port.
  2509. */
  2510. void serial8250_suspend_port(int line)
  2511. {
  2512. uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
  2513. }
  2514. /**
  2515. * serial8250_resume_port - resume one serial port
  2516. * @line: serial line number
  2517. *
  2518. * Resume one serial port.
  2519. */
  2520. void serial8250_resume_port(int line)
  2521. {
  2522. struct uart_8250_port *up = &serial8250_ports[line];
  2523. if (up->capabilities & UART_NATSEMI) {
  2524. unsigned char tmp;
  2525. /* Ensure it's still in high speed mode */
  2526. serial_outp(up, UART_LCR, 0xE0);
  2527. tmp = serial_in(up, 0x04); /* EXCR2 */
  2528. tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
  2529. tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
  2530. serial_outp(up, 0x04, tmp);
  2531. serial_outp(up, UART_LCR, 0);
  2532. }
  2533. uart_resume_port(&serial8250_reg, &up->port);
  2534. }
  2535. /*
  2536. * Register a set of serial devices attached to a platform device. The
  2537. * list is terminated with a zero flags entry, which means we expect
  2538. * all entries to have at least UPF_BOOT_AUTOCONF set.
  2539. */
  2540. static int __devinit serial8250_probe(struct platform_device *dev)
  2541. {
  2542. struct plat_serial8250_port *p = dev->dev.platform_data;
  2543. struct uart_port port;
  2544. int ret, i, irqflag = 0;
  2545. memset(&port, 0, sizeof(struct uart_port));
  2546. if (share_irqs)
  2547. irqflag = IRQF_SHARED;
  2548. for (i = 0; p && p->flags != 0; p++, i++) {
  2549. port.iobase = p->iobase;
  2550. port.membase = p->membase;
  2551. port.irq = p->irq;
  2552. port.irqflags = p->irqflags;
  2553. port.uartclk = p->uartclk;
  2554. port.regshift = p->regshift;
  2555. port.iotype = p->iotype;
  2556. port.flags = p->flags;
  2557. port.mapbase = p->mapbase;
  2558. port.hub6 = p->hub6;
  2559. port.private_data = p->private_data;
  2560. port.type = p->type;
  2561. port.serial_in = p->serial_in;
  2562. port.serial_out = p->serial_out;
  2563. port.dev = &dev->dev;
  2564. port.irqflags |= irqflag;
  2565. ret = serial8250_register_port(&port);
  2566. if (ret < 0) {
  2567. dev_err(&dev->dev, "unable to register port at index %d "
  2568. "(IO%lx MEM%llx IRQ%d): %d\n", i,
  2569. p->iobase, (unsigned long long)p->mapbase,
  2570. p->irq, ret);
  2571. }
  2572. }
  2573. return 0;
  2574. }
  2575. /*
  2576. * Remove serial ports registered against a platform device.
  2577. */
  2578. static int __devexit serial8250_remove(struct platform_device *dev)
  2579. {
  2580. int i;
  2581. for (i = 0; i < nr_uarts; i++) {
  2582. struct uart_8250_port *up = &serial8250_ports[i];
  2583. if (up->port.dev == &dev->dev)
  2584. serial8250_unregister_port(i);
  2585. }
  2586. return 0;
  2587. }
  2588. static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
  2589. {
  2590. int i;
  2591. for (i = 0; i < UART_NR; i++) {
  2592. struct uart_8250_port *up = &serial8250_ports[i];
  2593. if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
  2594. uart_suspend_port(&serial8250_reg, &up->port);
  2595. }
  2596. return 0;
  2597. }
  2598. static int serial8250_resume(struct platform_device *dev)
  2599. {
  2600. int i;
  2601. for (i = 0; i < UART_NR; i++) {
  2602. struct uart_8250_port *up = &serial8250_ports[i];
  2603. if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
  2604. serial8250_resume_port(i);
  2605. }
  2606. return 0;
  2607. }
  2608. static struct platform_driver serial8250_isa_driver = {
  2609. .probe = serial8250_probe,
  2610. .remove = __devexit_p(serial8250_remove),
  2611. .suspend = serial8250_suspend,
  2612. .resume = serial8250_resume,
  2613. .driver = {
  2614. .name = "serial8250",
  2615. .owner = THIS_MODULE,
  2616. },
  2617. };
  2618. /*
  2619. * This "device" covers _all_ ISA 8250-compatible serial devices listed
  2620. * in the table in include/asm/serial.h
  2621. */
  2622. static struct platform_device *serial8250_isa_devs;
  2623. /*
  2624. * serial8250_register_port and serial8250_unregister_port allows for
  2625. * 16x50 serial ports to be configured at run-time, to support PCMCIA
  2626. * modems and PCI multiport cards.
  2627. */
  2628. static DEFINE_MUTEX(serial_mutex);
  2629. static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
  2630. {
  2631. int i;
  2632. /*
  2633. * First, find a port entry which matches.
  2634. */
  2635. for (i = 0; i < nr_uarts; i++)
  2636. if (uart_match_port(&serial8250_ports[i].port, port))
  2637. return &serial8250_ports[i];
  2638. /*
  2639. * We didn't find a matching entry, so look for the first
  2640. * free entry. We look for one which hasn't been previously
  2641. * used (indicated by zero iobase).
  2642. */
  2643. for (i = 0; i < nr_uarts; i++)
  2644. if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
  2645. serial8250_ports[i].port.iobase == 0)
  2646. return &serial8250_ports[i];
  2647. /*
  2648. * That also failed. Last resort is to find any entry which
  2649. * doesn't have a real port associated with it.
  2650. */
  2651. for (i = 0; i < nr_uarts; i++)
  2652. if (serial8250_ports[i].port.type == PORT_UNKNOWN)
  2653. return &serial8250_ports[i];
  2654. return NULL;
  2655. }
  2656. /**
  2657. * serial8250_register_port - register a serial port
  2658. * @port: serial port template
  2659. *
  2660. * Configure the serial port specified by the request. If the
  2661. * port exists and is in use, it is hung up and unregistered
  2662. * first.
  2663. *
  2664. * The port is then probed and if necessary the IRQ is autodetected
  2665. * If this fails an error is returned.
  2666. *
  2667. * On success the port is ready to use and the line number is returned.
  2668. */
  2669. int serial8250_register_port(struct uart_port *port)
  2670. {
  2671. struct uart_8250_port *uart;
  2672. int ret = -ENOSPC;
  2673. if (port->uartclk == 0)
  2674. return -EINVAL;
  2675. mutex_lock(&serial_mutex);
  2676. uart = serial8250_find_match_or_unused(port);
  2677. if (uart) {
  2678. uart_remove_one_port(&serial8250_reg, &uart->port);
  2679. uart->port.iobase = port->iobase;
  2680. uart->port.membase = port->membase;
  2681. uart->port.irq = port->irq;
  2682. uart->port.irqflags = port->irqflags;
  2683. uart->port.uartclk = port->uartclk;
  2684. uart->port.fifosize = port->fifosize;
  2685. uart->port.regshift = port->regshift;
  2686. uart->port.iotype = port->iotype;
  2687. uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
  2688. uart->port.mapbase = port->mapbase;
  2689. uart->port.private_data = port->private_data;
  2690. if (port->dev)
  2691. uart->port.dev = port->dev;
  2692. if (port->flags & UPF_FIXED_TYPE)
  2693. serial8250_init_fixed_type_port(uart, port->type);
  2694. set_io_from_upio(&uart->port);
  2695. /* Possibly override default I/O functions. */
  2696. if (port->serial_in)
  2697. uart->port.serial_in = port->serial_in;
  2698. if (port->serial_out)
  2699. uart->port.serial_out = port->serial_out;
  2700. ret = uart_add_one_port(&serial8250_reg, &uart->port);
  2701. if (ret == 0)
  2702. ret = uart->port.line;
  2703. }
  2704. mutex_unlock(&serial_mutex);
  2705. return ret;
  2706. }
  2707. EXPORT_SYMBOL(serial8250_register_port);
  2708. /**
  2709. * serial8250_unregister_port - remove a 16x50 serial port at runtime
  2710. * @line: serial line number
  2711. *
  2712. * Remove one serial port. This may not be called from interrupt
  2713. * context. We hand the port back to the our control.
  2714. */
  2715. void serial8250_unregister_port(int line)
  2716. {
  2717. struct uart_8250_port *uart = &serial8250_ports[line];
  2718. mutex_lock(&serial_mutex);
  2719. uart_remove_one_port(&serial8250_reg, &uart->port);
  2720. if (serial8250_isa_devs) {
  2721. uart->port.flags &= ~UPF_BOOT_AUTOCONF;
  2722. uart->port.type = PORT_UNKNOWN;
  2723. uart->port.dev = &serial8250_isa_devs->dev;
  2724. uart_add_one_port(&serial8250_reg, &uart->port);
  2725. } else {
  2726. uart->port.dev = NULL;
  2727. }
  2728. mutex_unlock(&serial_mutex);
  2729. }
  2730. EXPORT_SYMBOL(serial8250_unregister_port);
  2731. static int __init serial8250_init(void)
  2732. {
  2733. int ret;
  2734. if (nr_uarts > UART_NR)
  2735. nr_uarts = UART_NR;
  2736. printk(KERN_INFO "Serial: 8250/16550 driver, "
  2737. "%d ports, IRQ sharing %sabled\n", nr_uarts,
  2738. share_irqs ? "en" : "dis");
  2739. #ifdef CONFIG_SPARC
  2740. ret = sunserial_register_minors(&serial8250_reg, UART_NR);
  2741. #else
  2742. serial8250_reg.nr = UART_NR;
  2743. ret = uart_register_driver(&serial8250_reg);
  2744. #endif
  2745. if (ret)
  2746. goto out;
  2747. serial8250_isa_devs = platform_device_alloc("serial8250",
  2748. PLAT8250_DEV_LEGACY);
  2749. if (!serial8250_isa_devs) {
  2750. ret = -ENOMEM;
  2751. goto unreg_uart_drv;
  2752. }
  2753. ret = platform_device_add(serial8250_isa_devs);
  2754. if (ret)
  2755. goto put_dev;
  2756. serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
  2757. ret = platform_driver_register(&serial8250_isa_driver);
  2758. if (ret == 0)
  2759. goto out;
  2760. platform_device_del(serial8250_isa_devs);
  2761. put_dev:
  2762. platform_device_put(serial8250_isa_devs);
  2763. unreg_uart_drv:
  2764. #ifdef CONFIG_SPARC
  2765. sunserial_unregister_minors(&serial8250_reg, UART_NR);
  2766. #else
  2767. uart_unregister_driver(&serial8250_reg);
  2768. #endif
  2769. out:
  2770. return ret;
  2771. }
  2772. static void __exit serial8250_exit(void)
  2773. {
  2774. struct platform_device *isa_dev = serial8250_isa_devs;
  2775. /*
  2776. * This tells serial8250_unregister_port() not to re-register
  2777. * the ports (thereby making serial8250_isa_driver permanently
  2778. * in use.)
  2779. */
  2780. serial8250_isa_devs = NULL;
  2781. platform_driver_unregister(&serial8250_isa_driver);
  2782. platform_device_unregister(isa_dev);
  2783. #ifdef CONFIG_SPARC
  2784. sunserial_unregister_minors(&serial8250_reg, UART_NR);
  2785. #else
  2786. uart_unregister_driver(&serial8250_reg);
  2787. #endif
  2788. }
  2789. module_init(serial8250_init);
  2790. module_exit(serial8250_exit);
  2791. EXPORT_SYMBOL(serial8250_suspend_port);
  2792. EXPORT_SYMBOL(serial8250_resume_port);
  2793. MODULE_LICENSE("GPL");
  2794. MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
  2795. module_param(share_irqs, uint, 0644);
  2796. MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
  2797. " (unsafe)");
  2798. module_param(nr_uarts, uint, 0644);
  2799. MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
  2800. module_param(skip_txen_test, uint, 0644);
  2801. MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
  2802. #ifdef CONFIG_SERIAL_8250_RSA
  2803. module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
  2804. MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
  2805. #endif
  2806. MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);