8250.c 84 KB

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