8250.c 81 KB

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