8250.c 65 KB

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