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