8250.c 67 KB

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