8250.c 65 KB

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