8250.c 64 KB

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