8250.c 84 KB

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