8250.c 72 KB

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