8250.c 66 KB

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