8250.c 75 KB

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