8250.c 66 KB

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