8250.c 83 KB

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