8250_port.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931
  1. /*
  2. * Base port operations for 8250/16550-type serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. * Split from 8250_core.c, Copyright (C) 2001 Russell King.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * A note about mapbase / membase
  13. *
  14. * mapbase is the physical address of the IO port.
  15. * membase is an 'ioremapped' cookie.
  16. */
  17. #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  18. #define SUPPORT_SYSRQ
  19. #endif
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/ioport.h>
  23. #include <linux/init.h>
  24. #include <linux/console.h>
  25. #include <linux/sysrq.h>
  26. #include <linux/delay.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/tty.h>
  29. #include <linux/ratelimit.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/serial.h>
  32. #include <linux/serial_8250.h>
  33. #include <linux/nmi.h>
  34. #include <linux/mutex.h>
  35. #include <linux/slab.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/pm_runtime.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include "8250.h"
  41. /*
  42. * Debugging.
  43. */
  44. #if 0
  45. #define DEBUG_AUTOCONF(fmt...) printk(fmt)
  46. #else
  47. #define DEBUG_AUTOCONF(fmt...) do { } while (0)
  48. #endif
  49. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  50. /*
  51. * Here we define the default xmit fifo size used for each type of UART.
  52. */
  53. static const struct serial8250_config uart_config[] = {
  54. [PORT_UNKNOWN] = {
  55. .name = "unknown",
  56. .fifo_size = 1,
  57. .tx_loadsz = 1,
  58. },
  59. [PORT_8250] = {
  60. .name = "8250",
  61. .fifo_size = 1,
  62. .tx_loadsz = 1,
  63. },
  64. [PORT_16450] = {
  65. .name = "16450",
  66. .fifo_size = 1,
  67. .tx_loadsz = 1,
  68. },
  69. [PORT_16550] = {
  70. .name = "16550",
  71. .fifo_size = 1,
  72. .tx_loadsz = 1,
  73. },
  74. [PORT_16550A] = {
  75. .name = "16550A",
  76. .fifo_size = 16,
  77. .tx_loadsz = 16,
  78. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  79. .rxtrig_bytes = {1, 4, 8, 14},
  80. .flags = UART_CAP_FIFO,
  81. },
  82. [PORT_CIRRUS] = {
  83. .name = "Cirrus",
  84. .fifo_size = 1,
  85. .tx_loadsz = 1,
  86. },
  87. [PORT_16650] = {
  88. .name = "ST16650",
  89. .fifo_size = 1,
  90. .tx_loadsz = 1,
  91. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  92. },
  93. [PORT_16650V2] = {
  94. .name = "ST16650V2",
  95. .fifo_size = 32,
  96. .tx_loadsz = 16,
  97. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
  98. UART_FCR_T_TRIG_00,
  99. .rxtrig_bytes = {8, 16, 24, 28},
  100. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  101. },
  102. [PORT_16750] = {
  103. .name = "TI16750",
  104. .fifo_size = 64,
  105. .tx_loadsz = 64,
  106. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
  107. UART_FCR7_64BYTE,
  108. .rxtrig_bytes = {1, 16, 32, 56},
  109. .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
  110. },
  111. [PORT_STARTECH] = {
  112. .name = "Startech",
  113. .fifo_size = 1,
  114. .tx_loadsz = 1,
  115. },
  116. [PORT_16C950] = {
  117. .name = "16C950/954",
  118. .fifo_size = 128,
  119. .tx_loadsz = 128,
  120. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  121. /* UART_CAP_EFR breaks billionon CF bluetooth card. */
  122. .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
  123. },
  124. [PORT_16654] = {
  125. .name = "ST16654",
  126. .fifo_size = 64,
  127. .tx_loadsz = 32,
  128. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
  129. UART_FCR_T_TRIG_10,
  130. .rxtrig_bytes = {8, 16, 56, 60},
  131. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  132. },
  133. [PORT_16850] = {
  134. .name = "XR16850",
  135. .fifo_size = 128,
  136. .tx_loadsz = 128,
  137. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  138. .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
  139. },
  140. [PORT_RSA] = {
  141. .name = "RSA",
  142. .fifo_size = 2048,
  143. .tx_loadsz = 2048,
  144. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
  145. .flags = UART_CAP_FIFO,
  146. },
  147. [PORT_NS16550A] = {
  148. .name = "NS16550A",
  149. .fifo_size = 16,
  150. .tx_loadsz = 16,
  151. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  152. .flags = UART_CAP_FIFO | UART_NATSEMI,
  153. },
  154. [PORT_XSCALE] = {
  155. .name = "XScale",
  156. .fifo_size = 32,
  157. .tx_loadsz = 32,
  158. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  159. .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
  160. },
  161. [PORT_OCTEON] = {
  162. .name = "OCTEON",
  163. .fifo_size = 64,
  164. .tx_loadsz = 64,
  165. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  166. .flags = UART_CAP_FIFO,
  167. },
  168. [PORT_AR7] = {
  169. .name = "AR7",
  170. .fifo_size = 16,
  171. .tx_loadsz = 16,
  172. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
  173. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  174. },
  175. [PORT_U6_16550A] = {
  176. .name = "U6_16550A",
  177. .fifo_size = 64,
  178. .tx_loadsz = 64,
  179. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  180. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  181. },
  182. [PORT_TEGRA] = {
  183. .name = "Tegra",
  184. .fifo_size = 32,
  185. .tx_loadsz = 8,
  186. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
  187. UART_FCR_T_TRIG_01,
  188. .rxtrig_bytes = {1, 4, 8, 14},
  189. .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
  190. },
  191. [PORT_XR17D15X] = {
  192. .name = "XR17D15X",
  193. .fifo_size = 64,
  194. .tx_loadsz = 64,
  195. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  196. .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
  197. UART_CAP_SLEEP,
  198. },
  199. [PORT_XR17V35X] = {
  200. .name = "XR17V35X",
  201. .fifo_size = 256,
  202. .tx_loadsz = 256,
  203. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
  204. UART_FCR_T_TRIG_11,
  205. .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
  206. UART_CAP_SLEEP,
  207. },
  208. [PORT_LPC3220] = {
  209. .name = "LPC3220",
  210. .fifo_size = 64,
  211. .tx_loadsz = 32,
  212. .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
  213. UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
  214. .flags = UART_CAP_FIFO,
  215. },
  216. [PORT_BRCM_TRUMANAGE] = {
  217. .name = "TruManage",
  218. .fifo_size = 1,
  219. .tx_loadsz = 1024,
  220. .flags = UART_CAP_HFIFO,
  221. },
  222. [PORT_8250_CIR] = {
  223. .name = "CIR port"
  224. },
  225. [PORT_ALTR_16550_F32] = {
  226. .name = "Altera 16550 FIFO32",
  227. .fifo_size = 32,
  228. .tx_loadsz = 32,
  229. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  230. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  231. },
  232. [PORT_ALTR_16550_F64] = {
  233. .name = "Altera 16550 FIFO64",
  234. .fifo_size = 64,
  235. .tx_loadsz = 64,
  236. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  237. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  238. },
  239. [PORT_ALTR_16550_F128] = {
  240. .name = "Altera 16550 FIFO128",
  241. .fifo_size = 128,
  242. .tx_loadsz = 128,
  243. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  244. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  245. },
  246. /* tx_loadsz is set to 63-bytes instead of 64-bytes to implement
  247. workaround of errata A-008006 which states that tx_loadsz should be
  248. configured less than Maximum supported fifo bytes */
  249. [PORT_16550A_FSL64] = {
  250. .name = "16550A_FSL64",
  251. .fifo_size = 64,
  252. .tx_loadsz = 63,
  253. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
  254. UART_FCR7_64BYTE,
  255. .flags = UART_CAP_FIFO,
  256. },
  257. [PORT_RT2880] = {
  258. .name = "Palmchip BK-3103",
  259. .fifo_size = 16,
  260. .tx_loadsz = 16,
  261. .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
  262. .rxtrig_bytes = {1, 4, 8, 14},
  263. .flags = UART_CAP_FIFO,
  264. },
  265. };
  266. /* Uart divisor latch read */
  267. static int default_serial_dl_read(struct uart_8250_port *up)
  268. {
  269. return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
  270. }
  271. /* Uart divisor latch write */
  272. static void default_serial_dl_write(struct uart_8250_port *up, int value)
  273. {
  274. serial_out(up, UART_DLL, value & 0xff);
  275. serial_out(up, UART_DLM, value >> 8 & 0xff);
  276. }
  277. #ifdef CONFIG_SERIAL_8250_RT288X
  278. /* Au1x00/RT288x UART hardware has a weird register layout */
  279. static const s8 au_io_in_map[8] = {
  280. 0, /* UART_RX */
  281. 2, /* UART_IER */
  282. 3, /* UART_IIR */
  283. 5, /* UART_LCR */
  284. 6, /* UART_MCR */
  285. 7, /* UART_LSR */
  286. 8, /* UART_MSR */
  287. -1, /* UART_SCR (unmapped) */
  288. };
  289. static const s8 au_io_out_map[8] = {
  290. 1, /* UART_TX */
  291. 2, /* UART_IER */
  292. 4, /* UART_FCR */
  293. 5, /* UART_LCR */
  294. 6, /* UART_MCR */
  295. -1, /* UART_LSR (unmapped) */
  296. -1, /* UART_MSR (unmapped) */
  297. -1, /* UART_SCR (unmapped) */
  298. };
  299. static unsigned int au_serial_in(struct uart_port *p, int offset)
  300. {
  301. if (offset >= ARRAY_SIZE(au_io_in_map))
  302. return UINT_MAX;
  303. offset = au_io_in_map[offset];
  304. if (offset < 0)
  305. return UINT_MAX;
  306. return __raw_readl(p->membase + (offset << p->regshift));
  307. }
  308. static void au_serial_out(struct uart_port *p, int offset, int value)
  309. {
  310. if (offset >= ARRAY_SIZE(au_io_out_map))
  311. return;
  312. offset = au_io_out_map[offset];
  313. if (offset < 0)
  314. return;
  315. __raw_writel(value, p->membase + (offset << p->regshift));
  316. }
  317. /* Au1x00 haven't got a standard divisor latch */
  318. static int au_serial_dl_read(struct uart_8250_port *up)
  319. {
  320. return __raw_readl(up->port.membase + 0x28);
  321. }
  322. static void au_serial_dl_write(struct uart_8250_port *up, int value)
  323. {
  324. __raw_writel(value, up->port.membase + 0x28);
  325. }
  326. #endif
  327. static unsigned int hub6_serial_in(struct uart_port *p, int offset)
  328. {
  329. offset = offset << p->regshift;
  330. outb(p->hub6 - 1 + offset, p->iobase);
  331. return inb(p->iobase + 1);
  332. }
  333. static void hub6_serial_out(struct uart_port *p, int offset, int value)
  334. {
  335. offset = offset << p->regshift;
  336. outb(p->hub6 - 1 + offset, p->iobase);
  337. outb(value, p->iobase + 1);
  338. }
  339. static unsigned int mem_serial_in(struct uart_port *p, int offset)
  340. {
  341. offset = offset << p->regshift;
  342. return readb(p->membase + offset);
  343. }
  344. static void mem_serial_out(struct uart_port *p, int offset, int value)
  345. {
  346. offset = offset << p->regshift;
  347. writeb(value, p->membase + offset);
  348. }
  349. static void mem32_serial_out(struct uart_port *p, int offset, int value)
  350. {
  351. offset = offset << p->regshift;
  352. writel(value, p->membase + offset);
  353. }
  354. static unsigned int mem32_serial_in(struct uart_port *p, int offset)
  355. {
  356. offset = offset << p->regshift;
  357. return readl(p->membase + offset);
  358. }
  359. static void mem32be_serial_out(struct uart_port *p, int offset, int value)
  360. {
  361. offset = offset << p->regshift;
  362. iowrite32be(value, p->membase + offset);
  363. }
  364. static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
  365. {
  366. offset = offset << p->regshift;
  367. return ioread32be(p->membase + offset);
  368. }
  369. static unsigned int io_serial_in(struct uart_port *p, int offset)
  370. {
  371. offset = offset << p->regshift;
  372. return inb(p->iobase + offset);
  373. }
  374. static void io_serial_out(struct uart_port *p, int offset, int value)
  375. {
  376. offset = offset << p->regshift;
  377. outb(value, p->iobase + offset);
  378. }
  379. static int serial8250_default_handle_irq(struct uart_port *port);
  380. static int exar_handle_irq(struct uart_port *port);
  381. static void set_io_from_upio(struct uart_port *p)
  382. {
  383. struct uart_8250_port *up = up_to_u8250p(p);
  384. up->dl_read = default_serial_dl_read;
  385. up->dl_write = default_serial_dl_write;
  386. switch (p->iotype) {
  387. case UPIO_HUB6:
  388. p->serial_in = hub6_serial_in;
  389. p->serial_out = hub6_serial_out;
  390. break;
  391. case UPIO_MEM:
  392. p->serial_in = mem_serial_in;
  393. p->serial_out = mem_serial_out;
  394. break;
  395. case UPIO_MEM32:
  396. p->serial_in = mem32_serial_in;
  397. p->serial_out = mem32_serial_out;
  398. break;
  399. case UPIO_MEM32BE:
  400. p->serial_in = mem32be_serial_in;
  401. p->serial_out = mem32be_serial_out;
  402. break;
  403. #ifdef CONFIG_SERIAL_8250_RT288X
  404. case UPIO_AU:
  405. p->serial_in = au_serial_in;
  406. p->serial_out = au_serial_out;
  407. up->dl_read = au_serial_dl_read;
  408. up->dl_write = au_serial_dl_write;
  409. break;
  410. #endif
  411. default:
  412. p->serial_in = io_serial_in;
  413. p->serial_out = io_serial_out;
  414. break;
  415. }
  416. /* Remember loaded iotype */
  417. up->cur_iotype = p->iotype;
  418. p->handle_irq = serial8250_default_handle_irq;
  419. }
  420. static void
  421. serial_port_out_sync(struct uart_port *p, int offset, int value)
  422. {
  423. switch (p->iotype) {
  424. case UPIO_MEM:
  425. case UPIO_MEM32:
  426. case UPIO_MEM32BE:
  427. case UPIO_AU:
  428. p->serial_out(p, offset, value);
  429. p->serial_in(p, UART_LCR); /* safe, no side-effects */
  430. break;
  431. default:
  432. p->serial_out(p, offset, value);
  433. }
  434. }
  435. /*
  436. * For the 16C950
  437. */
  438. static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
  439. {
  440. serial_out(up, UART_SCR, offset);
  441. serial_out(up, UART_ICR, value);
  442. }
  443. static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
  444. {
  445. unsigned int value;
  446. serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
  447. serial_out(up, UART_SCR, offset);
  448. value = serial_in(up, UART_ICR);
  449. serial_icr_write(up, UART_ACR, up->acr);
  450. return value;
  451. }
  452. /*
  453. * FIFO support.
  454. */
  455. static void serial8250_clear_fifos(struct uart_8250_port *p)
  456. {
  457. if (p->capabilities & UART_CAP_FIFO) {
  458. serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
  459. serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
  460. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  461. serial_out(p, UART_FCR, 0);
  462. }
  463. }
  464. void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
  465. {
  466. serial8250_clear_fifos(p);
  467. serial_out(p, UART_FCR, p->fcr);
  468. }
  469. EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
  470. void serial8250_rpm_get(struct uart_8250_port *p)
  471. {
  472. if (!(p->capabilities & UART_CAP_RPM))
  473. return;
  474. pm_runtime_get_sync(p->port.dev);
  475. }
  476. EXPORT_SYMBOL_GPL(serial8250_rpm_get);
  477. void serial8250_rpm_put(struct uart_8250_port *p)
  478. {
  479. if (!(p->capabilities & UART_CAP_RPM))
  480. return;
  481. pm_runtime_mark_last_busy(p->port.dev);
  482. pm_runtime_put_autosuspend(p->port.dev);
  483. }
  484. EXPORT_SYMBOL_GPL(serial8250_rpm_put);
  485. /*
  486. * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
  487. * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
  488. * empty and the HW can idle again.
  489. */
  490. static void serial8250_rpm_get_tx(struct uart_8250_port *p)
  491. {
  492. unsigned char rpm_active;
  493. if (!(p->capabilities & UART_CAP_RPM))
  494. return;
  495. rpm_active = xchg(&p->rpm_tx_active, 1);
  496. if (rpm_active)
  497. return;
  498. pm_runtime_get_sync(p->port.dev);
  499. }
  500. static void serial8250_rpm_put_tx(struct uart_8250_port *p)
  501. {
  502. unsigned char rpm_active;
  503. if (!(p->capabilities & UART_CAP_RPM))
  504. return;
  505. rpm_active = xchg(&p->rpm_tx_active, 0);
  506. if (!rpm_active)
  507. return;
  508. pm_runtime_mark_last_busy(p->port.dev);
  509. pm_runtime_put_autosuspend(p->port.dev);
  510. }
  511. /*
  512. * IER sleep support. UARTs which have EFRs need the "extended
  513. * capability" bit enabled. Note that on XR16C850s, we need to
  514. * reset LCR to write to IER.
  515. */
  516. static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
  517. {
  518. unsigned char lcr = 0, efr = 0;
  519. /*
  520. * Exar UARTs have a SLEEP register that enables or disables
  521. * each UART to enter sleep mode separately. On the XR17V35x the
  522. * register is accessible to each UART at the UART_EXAR_SLEEP
  523. * offset but the UART channel may only write to the corresponding
  524. * bit.
  525. */
  526. serial8250_rpm_get(p);
  527. if ((p->port.type == PORT_XR17V35X) ||
  528. (p->port.type == PORT_XR17D15X)) {
  529. serial_out(p, UART_EXAR_SLEEP, sleep ? 0xff : 0);
  530. goto out;
  531. }
  532. if (p->capabilities & UART_CAP_SLEEP) {
  533. if (p->capabilities & UART_CAP_EFR) {
  534. lcr = serial_in(p, UART_LCR);
  535. efr = serial_in(p, UART_EFR);
  536. serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
  537. serial_out(p, UART_EFR, UART_EFR_ECB);
  538. serial_out(p, UART_LCR, 0);
  539. }
  540. serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
  541. if (p->capabilities & UART_CAP_EFR) {
  542. serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
  543. serial_out(p, UART_EFR, efr);
  544. serial_out(p, UART_LCR, lcr);
  545. }
  546. }
  547. out:
  548. serial8250_rpm_put(p);
  549. }
  550. #ifdef CONFIG_SERIAL_8250_RSA
  551. /*
  552. * Attempts to turn on the RSA FIFO. Returns zero on failure.
  553. * We set the port uart clock rate if we succeed.
  554. */
  555. static int __enable_rsa(struct uart_8250_port *up)
  556. {
  557. unsigned char mode;
  558. int result;
  559. mode = serial_in(up, UART_RSA_MSR);
  560. result = mode & UART_RSA_MSR_FIFO;
  561. if (!result) {
  562. serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
  563. mode = serial_in(up, UART_RSA_MSR);
  564. result = mode & UART_RSA_MSR_FIFO;
  565. }
  566. if (result)
  567. up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
  568. return result;
  569. }
  570. static void enable_rsa(struct uart_8250_port *up)
  571. {
  572. if (up->port.type == PORT_RSA) {
  573. if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
  574. spin_lock_irq(&up->port.lock);
  575. __enable_rsa(up);
  576. spin_unlock_irq(&up->port.lock);
  577. }
  578. if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
  579. serial_out(up, UART_RSA_FRR, 0);
  580. }
  581. }
  582. /*
  583. * Attempts to turn off the RSA FIFO. Returns zero on failure.
  584. * It is unknown why interrupts were disabled in here. However,
  585. * the caller is expected to preserve this behaviour by grabbing
  586. * the spinlock before calling this function.
  587. */
  588. static void disable_rsa(struct uart_8250_port *up)
  589. {
  590. unsigned char mode;
  591. int result;
  592. if (up->port.type == PORT_RSA &&
  593. up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
  594. spin_lock_irq(&up->port.lock);
  595. mode = serial_in(up, UART_RSA_MSR);
  596. result = !(mode & UART_RSA_MSR_FIFO);
  597. if (!result) {
  598. serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
  599. mode = serial_in(up, UART_RSA_MSR);
  600. result = !(mode & UART_RSA_MSR_FIFO);
  601. }
  602. if (result)
  603. up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
  604. spin_unlock_irq(&up->port.lock);
  605. }
  606. }
  607. #endif /* CONFIG_SERIAL_8250_RSA */
  608. /*
  609. * This is a quickie test to see how big the FIFO is.
  610. * It doesn't work at all the time, more's the pity.
  611. */
  612. static int size_fifo(struct uart_8250_port *up)
  613. {
  614. unsigned char old_fcr, old_mcr, old_lcr;
  615. unsigned short old_dl;
  616. int count;
  617. old_lcr = serial_in(up, UART_LCR);
  618. serial_out(up, UART_LCR, 0);
  619. old_fcr = serial_in(up, UART_FCR);
  620. old_mcr = serial_in(up, UART_MCR);
  621. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  622. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  623. serial_out(up, UART_MCR, UART_MCR_LOOP);
  624. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  625. old_dl = serial_dl_read(up);
  626. serial_dl_write(up, 0x0001);
  627. serial_out(up, UART_LCR, 0x03);
  628. for (count = 0; count < 256; count++)
  629. serial_out(up, UART_TX, count);
  630. mdelay(20);/* FIXME - schedule_timeout */
  631. for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
  632. (count < 256); count++)
  633. serial_in(up, UART_RX);
  634. serial_out(up, UART_FCR, old_fcr);
  635. serial_out(up, UART_MCR, old_mcr);
  636. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  637. serial_dl_write(up, old_dl);
  638. serial_out(up, UART_LCR, old_lcr);
  639. return count;
  640. }
  641. /*
  642. * Read UART ID using the divisor method - set DLL and DLM to zero
  643. * and the revision will be in DLL and device type in DLM. We
  644. * preserve the device state across this.
  645. */
  646. static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
  647. {
  648. unsigned char old_lcr;
  649. unsigned int id, old_dl;
  650. old_lcr = serial_in(p, UART_LCR);
  651. serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
  652. old_dl = serial_dl_read(p);
  653. serial_dl_write(p, 0);
  654. id = serial_dl_read(p);
  655. serial_dl_write(p, old_dl);
  656. serial_out(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_out(up, UART_SCR, 0xa5);
  753. status1 = serial_in(up, UART_SCR);
  754. serial_out(up, UART_SCR, 0x5a);
  755. status2 = serial_in(up, UART_SCR);
  756. serial_out(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. /*
  772. * We know that the chip has FIFOs. Does it have an EFR? The
  773. * EFR is located in the same register position as the IIR and
  774. * we know the top two bits of the IIR are currently set. The
  775. * EFR should contain zero. Try to read the EFR.
  776. */
  777. static void autoconfig_16550a(struct uart_8250_port *up)
  778. {
  779. unsigned char status1, status2;
  780. unsigned int iersave;
  781. up->port.type = PORT_16550A;
  782. up->capabilities |= UART_CAP_FIFO;
  783. /*
  784. * XR17V35x UARTs have an extra divisor register, DLD
  785. * that gets enabled with when DLAB is set which will
  786. * cause the device to incorrectly match and assign
  787. * port type to PORT_16650. The EFR for this UART is
  788. * found at offset 0x09. Instead check the Deice ID (DVID)
  789. * register for a 2, 4 or 8 port UART.
  790. */
  791. if (up->port.flags & UPF_EXAR_EFR) {
  792. status1 = serial_in(up, UART_EXAR_DVID);
  793. if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
  794. DEBUG_AUTOCONF("Exar XR17V35x ");
  795. up->port.type = PORT_XR17V35X;
  796. up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
  797. UART_CAP_SLEEP;
  798. return;
  799. }
  800. }
  801. /*
  802. * Check for presence of the EFR when DLAB is set.
  803. * Only ST16C650V1 UARTs pass this test.
  804. */
  805. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  806. if (serial_in(up, UART_EFR) == 0) {
  807. serial_out(up, UART_EFR, 0xA8);
  808. if (serial_in(up, UART_EFR) != 0) {
  809. DEBUG_AUTOCONF("EFRv1 ");
  810. up->port.type = PORT_16650;
  811. up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
  812. } else {
  813. serial_out(up, UART_LCR, 0);
  814. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  815. UART_FCR7_64BYTE);
  816. status1 = serial_in(up, UART_IIR) >> 5;
  817. serial_out(up, UART_FCR, 0);
  818. serial_out(up, UART_LCR, 0);
  819. if (status1 == 7)
  820. up->port.type = PORT_16550A_FSL64;
  821. else
  822. DEBUG_AUTOCONF("Motorola 8xxx DUART ");
  823. }
  824. serial_out(up, UART_EFR, 0);
  825. return;
  826. }
  827. /*
  828. * Maybe it requires 0xbf to be written to the LCR.
  829. * (other ST16C650V2 UARTs, TI16C752A, etc)
  830. */
  831. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  832. if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
  833. DEBUG_AUTOCONF("EFRv2 ");
  834. autoconfig_has_efr(up);
  835. return;
  836. }
  837. /*
  838. * Check for a National Semiconductor SuperIO chip.
  839. * Attempt to switch to bank 2, read the value of the LOOP bit
  840. * from EXCR1. Switch back to bank 0, change it in MCR. Then
  841. * switch back to bank 2, read it from EXCR1 again and check
  842. * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
  843. */
  844. serial_out(up, UART_LCR, 0);
  845. status1 = serial_in(up, UART_MCR);
  846. serial_out(up, UART_LCR, 0xE0);
  847. status2 = serial_in(up, 0x02); /* EXCR1 */
  848. if (!((status2 ^ status1) & UART_MCR_LOOP)) {
  849. serial_out(up, UART_LCR, 0);
  850. serial_out(up, UART_MCR, status1 ^ UART_MCR_LOOP);
  851. serial_out(up, UART_LCR, 0xE0);
  852. status2 = serial_in(up, 0x02); /* EXCR1 */
  853. serial_out(up, UART_LCR, 0);
  854. serial_out(up, UART_MCR, status1);
  855. if ((status2 ^ status1) & UART_MCR_LOOP) {
  856. unsigned short quot;
  857. serial_out(up, UART_LCR, 0xE0);
  858. quot = serial_dl_read(up);
  859. quot <<= 3;
  860. if (ns16550a_goto_highspeed(up))
  861. serial_dl_write(up, quot);
  862. serial_out(up, UART_LCR, 0);
  863. up->port.uartclk = 921600*16;
  864. up->port.type = PORT_NS16550A;
  865. up->capabilities |= UART_NATSEMI;
  866. return;
  867. }
  868. }
  869. /*
  870. * No EFR. Try to detect a TI16750, which only sets bit 5 of
  871. * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
  872. * Try setting it with and without DLAB set. Cheap clones
  873. * set bit 5 without DLAB set.
  874. */
  875. serial_out(up, UART_LCR, 0);
  876. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
  877. status1 = serial_in(up, UART_IIR) >> 5;
  878. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  879. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
  880. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
  881. status2 = serial_in(up, UART_IIR) >> 5;
  882. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  883. serial_out(up, UART_LCR, 0);
  884. DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
  885. if (status1 == 6 && status2 == 7) {
  886. up->port.type = PORT_16750;
  887. up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
  888. return;
  889. }
  890. /*
  891. * Try writing and reading the UART_IER_UUE bit (b6).
  892. * If it works, this is probably one of the Xscale platform's
  893. * internal UARTs.
  894. * We're going to explicitly set the UUE bit to 0 before
  895. * trying to write and read a 1 just to make sure it's not
  896. * already a 1 and maybe locked there before we even start start.
  897. */
  898. iersave = serial_in(up, UART_IER);
  899. serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
  900. if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
  901. /*
  902. * OK it's in a known zero state, try writing and reading
  903. * without disturbing the current state of the other bits.
  904. */
  905. serial_out(up, UART_IER, iersave | UART_IER_UUE);
  906. if (serial_in(up, UART_IER) & UART_IER_UUE) {
  907. /*
  908. * It's an Xscale.
  909. * We'll leave the UART_IER_UUE bit set to 1 (enabled).
  910. */
  911. DEBUG_AUTOCONF("Xscale ");
  912. up->port.type = PORT_XSCALE;
  913. up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
  914. return;
  915. }
  916. } else {
  917. /*
  918. * If we got here we couldn't force the IER_UUE bit to 0.
  919. * Log it and continue.
  920. */
  921. DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
  922. }
  923. serial_out(up, UART_IER, iersave);
  924. /*
  925. * Exar uarts have EFR in a weird location
  926. */
  927. if (up->port.flags & UPF_EXAR_EFR) {
  928. DEBUG_AUTOCONF("Exar XR17D15x ");
  929. up->port.type = PORT_XR17D15X;
  930. up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
  931. UART_CAP_SLEEP;
  932. return;
  933. }
  934. /*
  935. * We distinguish between 16550A and U6 16550A by counting
  936. * how many bytes are in the FIFO.
  937. */
  938. if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
  939. up->port.type = PORT_U6_16550A;
  940. up->capabilities |= UART_CAP_AFE;
  941. }
  942. }
  943. /*
  944. * This routine is called by rs_init() to initialize a specific serial
  945. * port. It determines what type of UART chip this serial port is
  946. * using: 8250, 16450, 16550, 16550A. The important question is
  947. * whether or not this UART is a 16550A or not, since this will
  948. * determine whether or not we can use its FIFO features or not.
  949. */
  950. static void autoconfig(struct uart_8250_port *up)
  951. {
  952. unsigned char status1, scratch, scratch2, scratch3;
  953. unsigned char save_lcr, save_mcr;
  954. struct uart_port *port = &up->port;
  955. unsigned long flags;
  956. unsigned int old_capabilities;
  957. if (!port->iobase && !port->mapbase && !port->membase)
  958. return;
  959. DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
  960. serial_index(port), port->iobase, port->membase);
  961. /*
  962. * We really do need global IRQs disabled here - we're going to
  963. * be frobbing the chips IRQ enable register to see if it exists.
  964. */
  965. spin_lock_irqsave(&port->lock, flags);
  966. up->capabilities = 0;
  967. up->bugs = 0;
  968. if (!(port->flags & UPF_BUGGY_UART)) {
  969. /*
  970. * Do a simple existence test first; if we fail this,
  971. * there's no point trying anything else.
  972. *
  973. * 0x80 is used as a nonsense port to prevent against
  974. * false positives due to ISA bus float. The
  975. * assumption is that 0x80 is a non-existent port;
  976. * which should be safe since include/asm/io.h also
  977. * makes this assumption.
  978. *
  979. * Note: this is safe as long as MCR bit 4 is clear
  980. * and the device is in "PC" mode.
  981. */
  982. scratch = serial_in(up, UART_IER);
  983. serial_out(up, UART_IER, 0);
  984. #ifdef __i386__
  985. outb(0xff, 0x080);
  986. #endif
  987. /*
  988. * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
  989. * 16C754B) allow only to modify them if an EFR bit is set.
  990. */
  991. scratch2 = serial_in(up, UART_IER) & 0x0f;
  992. serial_out(up, UART_IER, 0x0F);
  993. #ifdef __i386__
  994. outb(0, 0x080);
  995. #endif
  996. scratch3 = serial_in(up, UART_IER) & 0x0f;
  997. serial_out(up, UART_IER, scratch);
  998. if (scratch2 != 0 || scratch3 != 0x0F) {
  999. /*
  1000. * We failed; there's nothing here
  1001. */
  1002. spin_unlock_irqrestore(&port->lock, flags);
  1003. DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
  1004. scratch2, scratch3);
  1005. goto out;
  1006. }
  1007. }
  1008. save_mcr = serial_in(up, UART_MCR);
  1009. save_lcr = serial_in(up, UART_LCR);
  1010. /*
  1011. * Check to see if a UART is really there. Certain broken
  1012. * internal modems based on the Rockwell chipset fail this
  1013. * test, because they apparently don't implement the loopback
  1014. * test mode. So this test is skipped on the COM 1 through
  1015. * COM 4 ports. This *should* be safe, since no board
  1016. * manufacturer would be stupid enough to design a board
  1017. * that conflicts with COM 1-4 --- we hope!
  1018. */
  1019. if (!(port->flags & UPF_SKIP_TEST)) {
  1020. serial_out(up, UART_MCR, UART_MCR_LOOP | 0x0A);
  1021. status1 = serial_in(up, UART_MSR) & 0xF0;
  1022. serial_out(up, UART_MCR, save_mcr);
  1023. if (status1 != 0x90) {
  1024. spin_unlock_irqrestore(&port->lock, flags);
  1025. DEBUG_AUTOCONF("LOOP test failed (%02x) ",
  1026. status1);
  1027. goto out;
  1028. }
  1029. }
  1030. /*
  1031. * We're pretty sure there's a port here. Lets find out what
  1032. * type of port it is. The IIR top two bits allows us to find
  1033. * out if it's 8250 or 16450, 16550, 16550A or later. This
  1034. * determines what we test for next.
  1035. *
  1036. * We also initialise the EFR (if any) to zero for later. The
  1037. * EFR occupies the same register location as the FCR and IIR.
  1038. */
  1039. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  1040. serial_out(up, UART_EFR, 0);
  1041. serial_out(up, UART_LCR, 0);
  1042. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  1043. scratch = serial_in(up, UART_IIR) >> 6;
  1044. switch (scratch) {
  1045. case 0:
  1046. autoconfig_8250(up);
  1047. break;
  1048. case 1:
  1049. port->type = PORT_UNKNOWN;
  1050. break;
  1051. case 2:
  1052. port->type = PORT_16550;
  1053. break;
  1054. case 3:
  1055. autoconfig_16550a(up);
  1056. break;
  1057. }
  1058. #ifdef CONFIG_SERIAL_8250_RSA
  1059. /*
  1060. * Only probe for RSA ports if we got the region.
  1061. */
  1062. if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
  1063. __enable_rsa(up))
  1064. port->type = PORT_RSA;
  1065. #endif
  1066. serial_out(up, UART_LCR, save_lcr);
  1067. port->fifosize = uart_config[up->port.type].fifo_size;
  1068. old_capabilities = up->capabilities;
  1069. up->capabilities = uart_config[port->type].flags;
  1070. up->tx_loadsz = uart_config[port->type].tx_loadsz;
  1071. if (port->type == PORT_UNKNOWN)
  1072. goto out_lock;
  1073. /*
  1074. * Reset the UART.
  1075. */
  1076. #ifdef CONFIG_SERIAL_8250_RSA
  1077. if (port->type == PORT_RSA)
  1078. serial_out(up, UART_RSA_FRR, 0);
  1079. #endif
  1080. serial_out(up, UART_MCR, save_mcr);
  1081. serial8250_clear_fifos(up);
  1082. serial_in(up, UART_RX);
  1083. if (up->capabilities & UART_CAP_UUE)
  1084. serial_out(up, UART_IER, UART_IER_UUE);
  1085. else
  1086. serial_out(up, UART_IER, 0);
  1087. out_lock:
  1088. spin_unlock_irqrestore(&port->lock, flags);
  1089. if (up->capabilities != old_capabilities) {
  1090. printk(KERN_WARNING
  1091. "ttyS%d: detected caps %08x should be %08x\n",
  1092. serial_index(port), old_capabilities,
  1093. up->capabilities);
  1094. }
  1095. out:
  1096. DEBUG_AUTOCONF("iir=%d ", scratch);
  1097. DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
  1098. }
  1099. static void autoconfig_irq(struct uart_8250_port *up)
  1100. {
  1101. struct uart_port *port = &up->port;
  1102. unsigned char save_mcr, save_ier;
  1103. unsigned char save_ICP = 0;
  1104. unsigned int ICP = 0;
  1105. unsigned long irqs;
  1106. int irq;
  1107. if (port->flags & UPF_FOURPORT) {
  1108. ICP = (port->iobase & 0xfe0) | 0x1f;
  1109. save_ICP = inb_p(ICP);
  1110. outb_p(0x80, ICP);
  1111. inb_p(ICP);
  1112. }
  1113. if (uart_console(port))
  1114. console_lock();
  1115. /* forget possible initially masked and pending IRQ */
  1116. probe_irq_off(probe_irq_on());
  1117. save_mcr = serial_in(up, UART_MCR);
  1118. save_ier = serial_in(up, UART_IER);
  1119. serial_out(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
  1120. irqs = probe_irq_on();
  1121. serial_out(up, UART_MCR, 0);
  1122. udelay(10);
  1123. if (port->flags & UPF_FOURPORT) {
  1124. serial_out(up, UART_MCR,
  1125. UART_MCR_DTR | UART_MCR_RTS);
  1126. } else {
  1127. serial_out(up, UART_MCR,
  1128. UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
  1129. }
  1130. serial_out(up, UART_IER, 0x0f); /* enable all intrs */
  1131. serial_in(up, UART_LSR);
  1132. serial_in(up, UART_RX);
  1133. serial_in(up, UART_IIR);
  1134. serial_in(up, UART_MSR);
  1135. serial_out(up, UART_TX, 0xFF);
  1136. udelay(20);
  1137. irq = probe_irq_off(irqs);
  1138. serial_out(up, UART_MCR, save_mcr);
  1139. serial_out(up, UART_IER, save_ier);
  1140. if (port->flags & UPF_FOURPORT)
  1141. outb_p(save_ICP, ICP);
  1142. if (uart_console(port))
  1143. console_unlock();
  1144. port->irq = (irq > 0) ? irq : 0;
  1145. }
  1146. static inline void __stop_tx(struct uart_8250_port *p)
  1147. {
  1148. if (p->ier & UART_IER_THRI) {
  1149. p->ier &= ~UART_IER_THRI;
  1150. serial_out(p, UART_IER, p->ier);
  1151. serial8250_rpm_put_tx(p);
  1152. }
  1153. }
  1154. static void serial8250_stop_tx(struct uart_port *port)
  1155. {
  1156. struct uart_8250_port *up = up_to_u8250p(port);
  1157. serial8250_rpm_get(up);
  1158. __stop_tx(up);
  1159. /*
  1160. * We really want to stop the transmitter from sending.
  1161. */
  1162. if (port->type == PORT_16C950) {
  1163. up->acr |= UART_ACR_TXDIS;
  1164. serial_icr_write(up, UART_ACR, up->acr);
  1165. }
  1166. serial8250_rpm_put(up);
  1167. }
  1168. static void serial8250_start_tx(struct uart_port *port)
  1169. {
  1170. struct uart_8250_port *up = up_to_u8250p(port);
  1171. serial8250_rpm_get_tx(up);
  1172. if (up->dma && !up->dma->tx_dma(up))
  1173. return;
  1174. if (!(up->ier & UART_IER_THRI)) {
  1175. up->ier |= UART_IER_THRI;
  1176. serial_port_out(port, UART_IER, up->ier);
  1177. if (up->bugs & UART_BUG_TXEN) {
  1178. unsigned char lsr;
  1179. lsr = serial_in(up, UART_LSR);
  1180. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1181. if (lsr & UART_LSR_THRE)
  1182. serial8250_tx_chars(up);
  1183. }
  1184. }
  1185. /*
  1186. * Re-enable the transmitter if we disabled it.
  1187. */
  1188. if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
  1189. up->acr &= ~UART_ACR_TXDIS;
  1190. serial_icr_write(up, UART_ACR, up->acr);
  1191. }
  1192. }
  1193. static void serial8250_throttle(struct uart_port *port)
  1194. {
  1195. port->throttle(port);
  1196. }
  1197. static void serial8250_unthrottle(struct uart_port *port)
  1198. {
  1199. port->unthrottle(port);
  1200. }
  1201. static void serial8250_stop_rx(struct uart_port *port)
  1202. {
  1203. struct uart_8250_port *up = up_to_u8250p(port);
  1204. serial8250_rpm_get(up);
  1205. up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
  1206. up->port.read_status_mask &= ~UART_LSR_DR;
  1207. serial_port_out(port, UART_IER, up->ier);
  1208. serial8250_rpm_put(up);
  1209. }
  1210. static void serial8250_disable_ms(struct uart_port *port)
  1211. {
  1212. struct uart_8250_port *up =
  1213. container_of(port, struct uart_8250_port, port);
  1214. /* no MSR capabilities */
  1215. if (up->bugs & UART_BUG_NOMSR)
  1216. return;
  1217. up->ier &= ~UART_IER_MSI;
  1218. serial_port_out(port, UART_IER, up->ier);
  1219. }
  1220. static void serial8250_enable_ms(struct uart_port *port)
  1221. {
  1222. struct uart_8250_port *up = up_to_u8250p(port);
  1223. /* no MSR capabilities */
  1224. if (up->bugs & UART_BUG_NOMSR)
  1225. return;
  1226. up->ier |= UART_IER_MSI;
  1227. serial8250_rpm_get(up);
  1228. serial_port_out(port, UART_IER, up->ier);
  1229. serial8250_rpm_put(up);
  1230. }
  1231. /*
  1232. * serial8250_rx_chars: processes according to the passed in LSR
  1233. * value, and returns the remaining LSR bits not handled
  1234. * by this Rx routine.
  1235. */
  1236. unsigned char
  1237. serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
  1238. {
  1239. struct uart_port *port = &up->port;
  1240. unsigned char ch;
  1241. int max_count = 256;
  1242. char flag;
  1243. do {
  1244. if (likely(lsr & UART_LSR_DR))
  1245. ch = serial_in(up, UART_RX);
  1246. else
  1247. /*
  1248. * Intel 82571 has a Serial Over Lan device that will
  1249. * set UART_LSR_BI without setting UART_LSR_DR when
  1250. * it receives a break. To avoid reading from the
  1251. * receive buffer without UART_LSR_DR bit set, we
  1252. * just force the read character to be 0
  1253. */
  1254. ch = 0;
  1255. flag = TTY_NORMAL;
  1256. port->icount.rx++;
  1257. lsr |= up->lsr_saved_flags;
  1258. up->lsr_saved_flags = 0;
  1259. if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
  1260. if (lsr & UART_LSR_BI) {
  1261. lsr &= ~(UART_LSR_FE | UART_LSR_PE);
  1262. port->icount.brk++;
  1263. /*
  1264. * We do the SysRQ and SAK checking
  1265. * here because otherwise the break
  1266. * may get masked by ignore_status_mask
  1267. * or read_status_mask.
  1268. */
  1269. if (uart_handle_break(port))
  1270. goto ignore_char;
  1271. } else if (lsr & UART_LSR_PE)
  1272. port->icount.parity++;
  1273. else if (lsr & UART_LSR_FE)
  1274. port->icount.frame++;
  1275. if (lsr & UART_LSR_OE)
  1276. port->icount.overrun++;
  1277. /*
  1278. * Mask off conditions which should be ignored.
  1279. */
  1280. lsr &= port->read_status_mask;
  1281. if (lsr & UART_LSR_BI) {
  1282. DEBUG_INTR("handling break....");
  1283. flag = TTY_BREAK;
  1284. } else if (lsr & UART_LSR_PE)
  1285. flag = TTY_PARITY;
  1286. else if (lsr & UART_LSR_FE)
  1287. flag = TTY_FRAME;
  1288. }
  1289. if (uart_handle_sysrq_char(port, ch))
  1290. goto ignore_char;
  1291. uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
  1292. ignore_char:
  1293. lsr = serial_in(up, UART_LSR);
  1294. } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (--max_count > 0));
  1295. spin_unlock(&port->lock);
  1296. tty_flip_buffer_push(&port->state->port);
  1297. spin_lock(&port->lock);
  1298. return lsr;
  1299. }
  1300. EXPORT_SYMBOL_GPL(serial8250_rx_chars);
  1301. void serial8250_tx_chars(struct uart_8250_port *up)
  1302. {
  1303. struct uart_port *port = &up->port;
  1304. struct circ_buf *xmit = &port->state->xmit;
  1305. int count;
  1306. if (port->x_char) {
  1307. serial_out(up, UART_TX, port->x_char);
  1308. port->icount.tx++;
  1309. port->x_char = 0;
  1310. return;
  1311. }
  1312. if (uart_tx_stopped(port)) {
  1313. serial8250_stop_tx(port);
  1314. return;
  1315. }
  1316. if (uart_circ_empty(xmit)) {
  1317. __stop_tx(up);
  1318. return;
  1319. }
  1320. count = up->tx_loadsz;
  1321. do {
  1322. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  1323. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  1324. port->icount.tx++;
  1325. if (uart_circ_empty(xmit))
  1326. break;
  1327. if (up->capabilities & UART_CAP_HFIFO) {
  1328. if ((serial_port_in(port, UART_LSR) & BOTH_EMPTY) !=
  1329. BOTH_EMPTY)
  1330. break;
  1331. }
  1332. } while (--count > 0);
  1333. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  1334. uart_write_wakeup(port);
  1335. DEBUG_INTR("THRE...");
  1336. /*
  1337. * With RPM enabled, we have to wait until the FIFO is empty before the
  1338. * HW can go idle. So we get here once again with empty FIFO and disable
  1339. * the interrupt and RPM in __stop_tx()
  1340. */
  1341. if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
  1342. __stop_tx(up);
  1343. }
  1344. EXPORT_SYMBOL_GPL(serial8250_tx_chars);
  1345. /* Caller holds uart port lock */
  1346. unsigned int serial8250_modem_status(struct uart_8250_port *up)
  1347. {
  1348. struct uart_port *port = &up->port;
  1349. unsigned int status = serial_in(up, UART_MSR);
  1350. status |= up->msr_saved_flags;
  1351. up->msr_saved_flags = 0;
  1352. if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
  1353. port->state != NULL) {
  1354. if (status & UART_MSR_TERI)
  1355. port->icount.rng++;
  1356. if (status & UART_MSR_DDSR)
  1357. port->icount.dsr++;
  1358. if (status & UART_MSR_DDCD)
  1359. uart_handle_dcd_change(port, status & UART_MSR_DCD);
  1360. if (status & UART_MSR_DCTS)
  1361. uart_handle_cts_change(port, status & UART_MSR_CTS);
  1362. wake_up_interruptible(&port->state->port.delta_msr_wait);
  1363. }
  1364. return status;
  1365. }
  1366. EXPORT_SYMBOL_GPL(serial8250_modem_status);
  1367. /*
  1368. * This handles the interrupt from one port.
  1369. */
  1370. int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
  1371. {
  1372. unsigned char status;
  1373. unsigned long flags;
  1374. struct uart_8250_port *up = up_to_u8250p(port);
  1375. int dma_err = 0;
  1376. if (iir & UART_IIR_NO_INT)
  1377. return 0;
  1378. spin_lock_irqsave(&port->lock, flags);
  1379. status = serial_port_in(port, UART_LSR);
  1380. DEBUG_INTR("status = %x...", status);
  1381. if (status & (UART_LSR_DR | UART_LSR_BI)) {
  1382. if (up->dma)
  1383. dma_err = up->dma->rx_dma(up, iir);
  1384. if (!up->dma || dma_err)
  1385. status = serial8250_rx_chars(up, status);
  1386. }
  1387. serial8250_modem_status(up);
  1388. if ((!up->dma || (up->dma && up->dma->tx_err)) &&
  1389. (status & UART_LSR_THRE))
  1390. serial8250_tx_chars(up);
  1391. spin_unlock_irqrestore(&port->lock, flags);
  1392. return 1;
  1393. }
  1394. EXPORT_SYMBOL_GPL(serial8250_handle_irq);
  1395. static int serial8250_default_handle_irq(struct uart_port *port)
  1396. {
  1397. struct uart_8250_port *up = up_to_u8250p(port);
  1398. unsigned int iir;
  1399. int ret;
  1400. serial8250_rpm_get(up);
  1401. iir = serial_port_in(port, UART_IIR);
  1402. ret = serial8250_handle_irq(port, iir);
  1403. serial8250_rpm_put(up);
  1404. return ret;
  1405. }
  1406. /*
  1407. * These Exar UARTs have an extra interrupt indicator that could
  1408. * fire for a few unimplemented interrupts. One of which is a
  1409. * wakeup event when coming out of sleep. Put this here just
  1410. * to be on the safe side that these interrupts don't go unhandled.
  1411. */
  1412. static int exar_handle_irq(struct uart_port *port)
  1413. {
  1414. unsigned char int0, int1, int2, int3;
  1415. unsigned int iir = serial_port_in(port, UART_IIR);
  1416. int ret;
  1417. ret = serial8250_handle_irq(port, iir);
  1418. if ((port->type == PORT_XR17V35X) ||
  1419. (port->type == PORT_XR17D15X)) {
  1420. int0 = serial_port_in(port, 0x80);
  1421. int1 = serial_port_in(port, 0x81);
  1422. int2 = serial_port_in(port, 0x82);
  1423. int3 = serial_port_in(port, 0x83);
  1424. }
  1425. return ret;
  1426. }
  1427. static unsigned int serial8250_tx_empty(struct uart_port *port)
  1428. {
  1429. struct uart_8250_port *up = up_to_u8250p(port);
  1430. unsigned long flags;
  1431. unsigned int lsr;
  1432. serial8250_rpm_get(up);
  1433. spin_lock_irqsave(&port->lock, flags);
  1434. lsr = serial_port_in(port, UART_LSR);
  1435. up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
  1436. spin_unlock_irqrestore(&port->lock, flags);
  1437. serial8250_rpm_put(up);
  1438. return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
  1439. }
  1440. static unsigned int serial8250_get_mctrl(struct uart_port *port)
  1441. {
  1442. struct uart_8250_port *up = up_to_u8250p(port);
  1443. unsigned int status;
  1444. unsigned int ret;
  1445. serial8250_rpm_get(up);
  1446. status = serial8250_modem_status(up);
  1447. serial8250_rpm_put(up);
  1448. ret = 0;
  1449. if (status & UART_MSR_DCD)
  1450. ret |= TIOCM_CAR;
  1451. if (status & UART_MSR_RI)
  1452. ret |= TIOCM_RNG;
  1453. if (status & UART_MSR_DSR)
  1454. ret |= TIOCM_DSR;
  1455. if (status & UART_MSR_CTS)
  1456. ret |= TIOCM_CTS;
  1457. return ret;
  1458. }
  1459. void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1460. {
  1461. struct uart_8250_port *up = up_to_u8250p(port);
  1462. unsigned char mcr = 0;
  1463. if (mctrl & TIOCM_RTS)
  1464. mcr |= UART_MCR_RTS;
  1465. if (mctrl & TIOCM_DTR)
  1466. mcr |= UART_MCR_DTR;
  1467. if (mctrl & TIOCM_OUT1)
  1468. mcr |= UART_MCR_OUT1;
  1469. if (mctrl & TIOCM_OUT2)
  1470. mcr |= UART_MCR_OUT2;
  1471. if (mctrl & TIOCM_LOOP)
  1472. mcr |= UART_MCR_LOOP;
  1473. mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
  1474. serial_port_out(port, UART_MCR, mcr);
  1475. }
  1476. EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
  1477. static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1478. {
  1479. if (port->set_mctrl)
  1480. port->set_mctrl(port, mctrl);
  1481. else
  1482. serial8250_do_set_mctrl(port, mctrl);
  1483. }
  1484. static void serial8250_break_ctl(struct uart_port *port, int break_state)
  1485. {
  1486. struct uart_8250_port *up = up_to_u8250p(port);
  1487. unsigned long flags;
  1488. serial8250_rpm_get(up);
  1489. spin_lock_irqsave(&port->lock, flags);
  1490. if (break_state == -1)
  1491. up->lcr |= UART_LCR_SBC;
  1492. else
  1493. up->lcr &= ~UART_LCR_SBC;
  1494. serial_port_out(port, UART_LCR, up->lcr);
  1495. spin_unlock_irqrestore(&port->lock, flags);
  1496. serial8250_rpm_put(up);
  1497. }
  1498. /*
  1499. * Wait for transmitter & holding register to empty
  1500. */
  1501. static void wait_for_xmitr(struct uart_8250_port *up, int bits)
  1502. {
  1503. unsigned int status, tmout = 10000;
  1504. /* Wait up to 10ms for the character(s) to be sent. */
  1505. for (;;) {
  1506. status = serial_in(up, UART_LSR);
  1507. up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
  1508. if ((status & bits) == bits)
  1509. break;
  1510. if (--tmout == 0)
  1511. break;
  1512. udelay(1);
  1513. }
  1514. /* Wait up to 1s for flow control if necessary */
  1515. if (up->port.flags & UPF_CONS_FLOW) {
  1516. unsigned int tmout;
  1517. for (tmout = 1000000; tmout; tmout--) {
  1518. unsigned int msr = serial_in(up, UART_MSR);
  1519. up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
  1520. if (msr & UART_MSR_CTS)
  1521. break;
  1522. udelay(1);
  1523. touch_nmi_watchdog();
  1524. }
  1525. }
  1526. }
  1527. #ifdef CONFIG_CONSOLE_POLL
  1528. /*
  1529. * Console polling routines for writing and reading from the uart while
  1530. * in an interrupt or debug context.
  1531. */
  1532. static int serial8250_get_poll_char(struct uart_port *port)
  1533. {
  1534. struct uart_8250_port *up = up_to_u8250p(port);
  1535. unsigned char lsr;
  1536. int status;
  1537. serial8250_rpm_get(up);
  1538. lsr = serial_port_in(port, UART_LSR);
  1539. if (!(lsr & UART_LSR_DR)) {
  1540. status = NO_POLL_CHAR;
  1541. goto out;
  1542. }
  1543. status = serial_port_in(port, UART_RX);
  1544. out:
  1545. serial8250_rpm_put(up);
  1546. return status;
  1547. }
  1548. static void serial8250_put_poll_char(struct uart_port *port,
  1549. unsigned char c)
  1550. {
  1551. unsigned int ier;
  1552. struct uart_8250_port *up = up_to_u8250p(port);
  1553. serial8250_rpm_get(up);
  1554. /*
  1555. * First save the IER then disable the interrupts
  1556. */
  1557. ier = serial_port_in(port, UART_IER);
  1558. if (up->capabilities & UART_CAP_UUE)
  1559. serial_port_out(port, UART_IER, UART_IER_UUE);
  1560. else
  1561. serial_port_out(port, UART_IER, 0);
  1562. wait_for_xmitr(up, BOTH_EMPTY);
  1563. /*
  1564. * Send the character out.
  1565. */
  1566. serial_port_out(port, UART_TX, c);
  1567. /*
  1568. * Finally, wait for transmitter to become empty
  1569. * and restore the IER
  1570. */
  1571. wait_for_xmitr(up, BOTH_EMPTY);
  1572. serial_port_out(port, UART_IER, ier);
  1573. serial8250_rpm_put(up);
  1574. }
  1575. #endif /* CONFIG_CONSOLE_POLL */
  1576. int serial8250_do_startup(struct uart_port *port)
  1577. {
  1578. struct uart_8250_port *up = up_to_u8250p(port);
  1579. unsigned long flags;
  1580. unsigned char lsr, iir;
  1581. int retval;
  1582. if (!port->fifosize)
  1583. port->fifosize = uart_config[port->type].fifo_size;
  1584. if (!up->tx_loadsz)
  1585. up->tx_loadsz = uart_config[port->type].tx_loadsz;
  1586. if (!up->capabilities)
  1587. up->capabilities = uart_config[port->type].flags;
  1588. up->mcr = 0;
  1589. if (port->iotype != up->cur_iotype)
  1590. set_io_from_upio(port);
  1591. serial8250_rpm_get(up);
  1592. if (port->type == PORT_16C950) {
  1593. /* Wake up and initialize UART */
  1594. up->acr = 0;
  1595. serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
  1596. serial_port_out(port, UART_EFR, UART_EFR_ECB);
  1597. serial_port_out(port, UART_IER, 0);
  1598. serial_port_out(port, UART_LCR, 0);
  1599. serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
  1600. serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
  1601. serial_port_out(port, UART_EFR, UART_EFR_ECB);
  1602. serial_port_out(port, UART_LCR, 0);
  1603. }
  1604. #ifdef CONFIG_SERIAL_8250_RSA
  1605. /*
  1606. * If this is an RSA port, see if we can kick it up to the
  1607. * higher speed clock.
  1608. */
  1609. enable_rsa(up);
  1610. #endif
  1611. if (port->type == PORT_XR17V35X) {
  1612. /*
  1613. * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
  1614. * MCR [7:5] and MSR [7:0]
  1615. */
  1616. serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
  1617. /*
  1618. * Make sure all interrups are masked until initialization is
  1619. * complete and the FIFOs are cleared
  1620. */
  1621. serial_port_out(port, UART_IER, 0);
  1622. }
  1623. /*
  1624. * Clear the FIFO buffers and disable them.
  1625. * (they will be reenabled in set_termios())
  1626. */
  1627. serial8250_clear_fifos(up);
  1628. /*
  1629. * Clear the interrupt registers.
  1630. */
  1631. serial_port_in(port, UART_LSR);
  1632. serial_port_in(port, UART_RX);
  1633. serial_port_in(port, UART_IIR);
  1634. serial_port_in(port, UART_MSR);
  1635. /*
  1636. * At this point, there's no way the LSR could still be 0xff;
  1637. * if it is, then bail out, because there's likely no UART
  1638. * here.
  1639. */
  1640. if (!(port->flags & UPF_BUGGY_UART) &&
  1641. (serial_port_in(port, UART_LSR) == 0xff)) {
  1642. printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
  1643. serial_index(port));
  1644. retval = -ENODEV;
  1645. goto out;
  1646. }
  1647. /*
  1648. * For a XR16C850, we need to set the trigger levels
  1649. */
  1650. if (port->type == PORT_16850) {
  1651. unsigned char fctr;
  1652. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  1653. fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
  1654. serial_port_out(port, UART_FCTR,
  1655. fctr | UART_FCTR_TRGD | UART_FCTR_RX);
  1656. serial_port_out(port, UART_TRG, UART_TRG_96);
  1657. serial_port_out(port, UART_FCTR,
  1658. fctr | UART_FCTR_TRGD | UART_FCTR_TX);
  1659. serial_port_out(port, UART_TRG, UART_TRG_96);
  1660. serial_port_out(port, UART_LCR, 0);
  1661. }
  1662. if (port->irq) {
  1663. unsigned char iir1;
  1664. /*
  1665. * Test for UARTs that do not reassert THRE when the
  1666. * transmitter is idle and the interrupt has already
  1667. * been cleared. Real 16550s should always reassert
  1668. * this interrupt whenever the transmitter is idle and
  1669. * the interrupt is enabled. Delays are necessary to
  1670. * allow register changes to become visible.
  1671. */
  1672. spin_lock_irqsave(&port->lock, flags);
  1673. if (up->port.irqflags & IRQF_SHARED)
  1674. disable_irq_nosync(port->irq);
  1675. wait_for_xmitr(up, UART_LSR_THRE);
  1676. serial_port_out_sync(port, UART_IER, UART_IER_THRI);
  1677. udelay(1); /* allow THRE to set */
  1678. iir1 = serial_port_in(port, UART_IIR);
  1679. serial_port_out(port, UART_IER, 0);
  1680. serial_port_out_sync(port, UART_IER, UART_IER_THRI);
  1681. udelay(1); /* allow a working UART time to re-assert THRE */
  1682. iir = serial_port_in(port, UART_IIR);
  1683. serial_port_out(port, UART_IER, 0);
  1684. if (port->irqflags & IRQF_SHARED)
  1685. enable_irq(port->irq);
  1686. spin_unlock_irqrestore(&port->lock, flags);
  1687. /*
  1688. * If the interrupt is not reasserted, or we otherwise
  1689. * don't trust the iir, setup a timer to kick the UART
  1690. * on a regular basis.
  1691. */
  1692. if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
  1693. up->port.flags & UPF_BUG_THRE) {
  1694. up->bugs |= UART_BUG_THRE;
  1695. }
  1696. }
  1697. retval = up->ops->setup_irq(up);
  1698. if (retval)
  1699. goto out;
  1700. /*
  1701. * Now, initialize the UART
  1702. */
  1703. serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
  1704. spin_lock_irqsave(&port->lock, flags);
  1705. if (up->port.flags & UPF_FOURPORT) {
  1706. if (!up->port.irq)
  1707. up->port.mctrl |= TIOCM_OUT1;
  1708. } else
  1709. /*
  1710. * Most PC uarts need OUT2 raised to enable interrupts.
  1711. */
  1712. if (port->irq)
  1713. up->port.mctrl |= TIOCM_OUT2;
  1714. serial8250_set_mctrl(port, port->mctrl);
  1715. /* Serial over Lan (SoL) hack:
  1716. Intel 8257x Gigabit ethernet chips have a
  1717. 16550 emulation, to be used for Serial Over Lan.
  1718. Those chips take a longer time than a normal
  1719. serial device to signalize that a transmission
  1720. data was queued. Due to that, the above test generally
  1721. fails. One solution would be to delay the reading of
  1722. iir. However, this is not reliable, since the timeout
  1723. is variable. So, let's just don't test if we receive
  1724. TX irq. This way, we'll never enable UART_BUG_TXEN.
  1725. */
  1726. if (up->port.flags & UPF_NO_TXEN_TEST)
  1727. goto dont_test_tx_en;
  1728. /*
  1729. * Do a quick test to see if we receive an
  1730. * interrupt when we enable the TX irq.
  1731. */
  1732. serial_port_out(port, UART_IER, UART_IER_THRI);
  1733. lsr = serial_port_in(port, UART_LSR);
  1734. iir = serial_port_in(port, UART_IIR);
  1735. serial_port_out(port, UART_IER, 0);
  1736. if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
  1737. if (!(up->bugs & UART_BUG_TXEN)) {
  1738. up->bugs |= UART_BUG_TXEN;
  1739. pr_debug("ttyS%d - enabling bad tx status workarounds\n",
  1740. serial_index(port));
  1741. }
  1742. } else {
  1743. up->bugs &= ~UART_BUG_TXEN;
  1744. }
  1745. dont_test_tx_en:
  1746. spin_unlock_irqrestore(&port->lock, flags);
  1747. /*
  1748. * Clear the interrupt registers again for luck, and clear the
  1749. * saved flags to avoid getting false values from polling
  1750. * routines or the previous session.
  1751. */
  1752. serial_port_in(port, UART_LSR);
  1753. serial_port_in(port, UART_RX);
  1754. serial_port_in(port, UART_IIR);
  1755. serial_port_in(port, UART_MSR);
  1756. up->lsr_saved_flags = 0;
  1757. up->msr_saved_flags = 0;
  1758. /*
  1759. * Request DMA channels for both RX and TX.
  1760. */
  1761. if (up->dma) {
  1762. retval = serial8250_request_dma(up);
  1763. if (retval) {
  1764. pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
  1765. serial_index(port));
  1766. up->dma = NULL;
  1767. }
  1768. }
  1769. /*
  1770. * Set the IER shadow for rx interrupts but defer actual interrupt
  1771. * enable until after the FIFOs are enabled; otherwise, an already-
  1772. * active sender can swamp the interrupt handler with "too much work".
  1773. */
  1774. up->ier = UART_IER_RLSI | UART_IER_RDI;
  1775. if (port->flags & UPF_FOURPORT) {
  1776. unsigned int icp;
  1777. /*
  1778. * Enable interrupts on the AST Fourport board
  1779. */
  1780. icp = (port->iobase & 0xfe0) | 0x01f;
  1781. outb_p(0x80, icp);
  1782. inb_p(icp);
  1783. }
  1784. retval = 0;
  1785. out:
  1786. serial8250_rpm_put(up);
  1787. return retval;
  1788. }
  1789. EXPORT_SYMBOL_GPL(serial8250_do_startup);
  1790. static int serial8250_startup(struct uart_port *port)
  1791. {
  1792. if (port->startup)
  1793. return port->startup(port);
  1794. return serial8250_do_startup(port);
  1795. }
  1796. void serial8250_do_shutdown(struct uart_port *port)
  1797. {
  1798. struct uart_8250_port *up = up_to_u8250p(port);
  1799. unsigned long flags;
  1800. serial8250_rpm_get(up);
  1801. /*
  1802. * Disable interrupts from this port
  1803. */
  1804. up->ier = 0;
  1805. serial_port_out(port, UART_IER, 0);
  1806. if (up->dma)
  1807. serial8250_release_dma(up);
  1808. spin_lock_irqsave(&port->lock, flags);
  1809. if (port->flags & UPF_FOURPORT) {
  1810. /* reset interrupts on the AST Fourport board */
  1811. inb((port->iobase & 0xfe0) | 0x1f);
  1812. port->mctrl |= TIOCM_OUT1;
  1813. } else
  1814. port->mctrl &= ~TIOCM_OUT2;
  1815. serial8250_set_mctrl(port, port->mctrl);
  1816. spin_unlock_irqrestore(&port->lock, flags);
  1817. /*
  1818. * Disable break condition and FIFOs
  1819. */
  1820. serial_port_out(port, UART_LCR,
  1821. serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
  1822. serial8250_clear_fifos(up);
  1823. #ifdef CONFIG_SERIAL_8250_RSA
  1824. /*
  1825. * Reset the RSA board back to 115kbps compat mode.
  1826. */
  1827. disable_rsa(up);
  1828. #endif
  1829. /*
  1830. * Read data port to reset things, and then unlink from
  1831. * the IRQ chain.
  1832. */
  1833. serial_port_in(port, UART_RX);
  1834. serial8250_rpm_put(up);
  1835. up->ops->release_irq(up);
  1836. }
  1837. EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
  1838. static void serial8250_shutdown(struct uart_port *port)
  1839. {
  1840. if (port->shutdown)
  1841. port->shutdown(port);
  1842. else
  1843. serial8250_do_shutdown(port);
  1844. }
  1845. /*
  1846. * XR17V35x UARTs have an extra fractional divisor register (DLD)
  1847. * Calculate divisor with extra 4-bit fractional portion
  1848. */
  1849. static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
  1850. unsigned int baud,
  1851. unsigned int *frac)
  1852. {
  1853. struct uart_port *port = &up->port;
  1854. unsigned int quot_16;
  1855. quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
  1856. *frac = quot_16 & 0x0f;
  1857. return quot_16 >> 4;
  1858. }
  1859. static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
  1860. unsigned int baud,
  1861. unsigned int *frac)
  1862. {
  1863. struct uart_port *port = &up->port;
  1864. unsigned int quot;
  1865. /*
  1866. * Handle magic divisors for baud rates above baud_base on
  1867. * SMSC SuperIO chips.
  1868. *
  1869. */
  1870. if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  1871. baud == (port->uartclk/4))
  1872. quot = 0x8001;
  1873. else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  1874. baud == (port->uartclk/8))
  1875. quot = 0x8002;
  1876. else if (up->port.type == PORT_XR17V35X)
  1877. quot = xr17v35x_get_divisor(up, baud, frac);
  1878. else
  1879. quot = uart_get_divisor(port, baud);
  1880. /*
  1881. * Oxford Semi 952 rev B workaround
  1882. */
  1883. if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
  1884. quot++;
  1885. return quot;
  1886. }
  1887. static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
  1888. tcflag_t c_cflag)
  1889. {
  1890. unsigned char cval;
  1891. switch (c_cflag & CSIZE) {
  1892. case CS5:
  1893. cval = UART_LCR_WLEN5;
  1894. break;
  1895. case CS6:
  1896. cval = UART_LCR_WLEN6;
  1897. break;
  1898. case CS7:
  1899. cval = UART_LCR_WLEN7;
  1900. break;
  1901. default:
  1902. case CS8:
  1903. cval = UART_LCR_WLEN8;
  1904. break;
  1905. }
  1906. if (c_cflag & CSTOPB)
  1907. cval |= UART_LCR_STOP;
  1908. if (c_cflag & PARENB) {
  1909. cval |= UART_LCR_PARITY;
  1910. if (up->bugs & UART_BUG_PARITY)
  1911. up->fifo_bug = true;
  1912. }
  1913. if (!(c_cflag & PARODD))
  1914. cval |= UART_LCR_EPAR;
  1915. #ifdef CMSPAR
  1916. if (c_cflag & CMSPAR)
  1917. cval |= UART_LCR_SPAR;
  1918. #endif
  1919. return cval;
  1920. }
  1921. static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
  1922. unsigned int quot, unsigned int quot_frac)
  1923. {
  1924. struct uart_8250_port *up = up_to_u8250p(port);
  1925. /* Workaround to enable 115200 baud on OMAP1510 internal ports */
  1926. if (is_omap1510_8250(up)) {
  1927. if (baud == 115200) {
  1928. quot = 1;
  1929. serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
  1930. } else
  1931. serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
  1932. }
  1933. /*
  1934. * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
  1935. * otherwise just set DLAB
  1936. */
  1937. if (up->capabilities & UART_NATSEMI)
  1938. serial_port_out(port, UART_LCR, 0xe0);
  1939. else
  1940. serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
  1941. serial_dl_write(up, quot);
  1942. /* XR17V35x UARTs have an extra fractional divisor register (DLD) */
  1943. if (up->port.type == PORT_XR17V35X) {
  1944. /* Preserve bits not related to baudrate; DLD[7:4]. */
  1945. quot_frac |= serial_port_in(port, 0x2) & 0xf0;
  1946. serial_port_out(port, 0x2, quot_frac);
  1947. }
  1948. }
  1949. static unsigned int
  1950. serial8250_get_baud_rate(struct uart_port *port, struct ktermios *termios,
  1951. struct ktermios *old)
  1952. {
  1953. unsigned int tolerance = port->uartclk / 100;
  1954. /*
  1955. * Ask the core to calculate the divisor for us.
  1956. * Allow 1% tolerance at the upper limit so uart clks marginally
  1957. * slower than nominal still match standard baud rates without
  1958. * causing transmission errors.
  1959. */
  1960. return uart_get_baud_rate(port, termios, old,
  1961. port->uartclk / 16 / 0xffff,
  1962. (port->uartclk + tolerance) / 16);
  1963. }
  1964. void
  1965. serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
  1966. struct ktermios *old)
  1967. {
  1968. struct uart_8250_port *up = up_to_u8250p(port);
  1969. unsigned char cval;
  1970. unsigned long flags;
  1971. unsigned int baud, quot, frac = 0;
  1972. cval = serial8250_compute_lcr(up, termios->c_cflag);
  1973. baud = serial8250_get_baud_rate(port, termios, old);
  1974. quot = serial8250_get_divisor(up, baud, &frac);
  1975. /*
  1976. * Ok, we're now changing the port state. Do it with
  1977. * interrupts disabled.
  1978. */
  1979. serial8250_rpm_get(up);
  1980. spin_lock_irqsave(&port->lock, flags);
  1981. up->lcr = cval; /* Save computed LCR */
  1982. if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
  1983. /* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
  1984. if ((baud < 2400 && !up->dma) || up->fifo_bug) {
  1985. up->fcr &= ~UART_FCR_TRIGGER_MASK;
  1986. up->fcr |= UART_FCR_TRIGGER_1;
  1987. }
  1988. }
  1989. /*
  1990. * MCR-based auto flow control. When AFE is enabled, RTS will be
  1991. * deasserted when the receive FIFO contains more characters than
  1992. * the trigger, or the MCR RTS bit is cleared. In the case where
  1993. * the remote UART is not using CTS auto flow control, we must
  1994. * have sufficient FIFO entries for the latency of the remote
  1995. * UART to respond. IOW, at least 32 bytes of FIFO.
  1996. */
  1997. if (up->capabilities & UART_CAP_AFE && port->fifosize >= 32) {
  1998. up->mcr &= ~UART_MCR_AFE;
  1999. if (termios->c_cflag & CRTSCTS)
  2000. up->mcr |= UART_MCR_AFE;
  2001. }
  2002. /*
  2003. * Update the per-port timeout.
  2004. */
  2005. uart_update_timeout(port, termios->c_cflag, baud);
  2006. port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  2007. if (termios->c_iflag & INPCK)
  2008. port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  2009. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  2010. port->read_status_mask |= UART_LSR_BI;
  2011. /*
  2012. * Characteres to ignore
  2013. */
  2014. port->ignore_status_mask = 0;
  2015. if (termios->c_iflag & IGNPAR)
  2016. port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  2017. if (termios->c_iflag & IGNBRK) {
  2018. port->ignore_status_mask |= UART_LSR_BI;
  2019. /*
  2020. * If we're ignoring parity and break indicators,
  2021. * ignore overruns too (for real raw support).
  2022. */
  2023. if (termios->c_iflag & IGNPAR)
  2024. port->ignore_status_mask |= UART_LSR_OE;
  2025. }
  2026. /*
  2027. * ignore all characters if CREAD is not set
  2028. */
  2029. if ((termios->c_cflag & CREAD) == 0)
  2030. port->ignore_status_mask |= UART_LSR_DR;
  2031. /*
  2032. * CTS flow control flag and modem status interrupts
  2033. */
  2034. up->ier &= ~UART_IER_MSI;
  2035. if (!(up->bugs & UART_BUG_NOMSR) &&
  2036. UART_ENABLE_MS(&up->port, termios->c_cflag))
  2037. up->ier |= UART_IER_MSI;
  2038. if (up->capabilities & UART_CAP_UUE)
  2039. up->ier |= UART_IER_UUE;
  2040. if (up->capabilities & UART_CAP_RTOIE)
  2041. up->ier |= UART_IER_RTOIE;
  2042. serial_port_out(port, UART_IER, up->ier);
  2043. if (up->capabilities & UART_CAP_EFR) {
  2044. unsigned char efr = 0;
  2045. /*
  2046. * TI16C752/Startech hardware flow control. FIXME:
  2047. * - TI16C752 requires control thresholds to be set.
  2048. * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
  2049. */
  2050. if (termios->c_cflag & CRTSCTS)
  2051. efr |= UART_EFR_CTS;
  2052. serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
  2053. if (port->flags & UPF_EXAR_EFR)
  2054. serial_port_out(port, UART_XR_EFR, efr);
  2055. else
  2056. serial_port_out(port, UART_EFR, efr);
  2057. }
  2058. serial8250_set_divisor(port, baud, quot, frac);
  2059. /*
  2060. * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
  2061. * is written without DLAB set, this mode will be disabled.
  2062. */
  2063. if (port->type == PORT_16750)
  2064. serial_port_out(port, UART_FCR, up->fcr);
  2065. serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */
  2066. if (port->type != PORT_16750) {
  2067. /* emulated UARTs (Lucent Venus 167x) need two steps */
  2068. if (up->fcr & UART_FCR_ENABLE_FIFO)
  2069. serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
  2070. serial_port_out(port, UART_FCR, up->fcr); /* set fcr */
  2071. }
  2072. serial8250_set_mctrl(port, port->mctrl);
  2073. spin_unlock_irqrestore(&port->lock, flags);
  2074. serial8250_rpm_put(up);
  2075. /* Don't rewrite B0 */
  2076. if (tty_termios_baud_rate(termios))
  2077. tty_termios_encode_baud_rate(termios, baud, baud);
  2078. }
  2079. EXPORT_SYMBOL(serial8250_do_set_termios);
  2080. static void
  2081. serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
  2082. struct ktermios *old)
  2083. {
  2084. if (port->set_termios)
  2085. port->set_termios(port, termios, old);
  2086. else
  2087. serial8250_do_set_termios(port, termios, old);
  2088. }
  2089. static void
  2090. serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
  2091. {
  2092. if (termios->c_line == N_PPS) {
  2093. port->flags |= UPF_HARDPPS_CD;
  2094. spin_lock_irq(&port->lock);
  2095. serial8250_enable_ms(port);
  2096. spin_unlock_irq(&port->lock);
  2097. } else {
  2098. port->flags &= ~UPF_HARDPPS_CD;
  2099. if (!UART_ENABLE_MS(port, termios->c_cflag)) {
  2100. spin_lock_irq(&port->lock);
  2101. serial8250_disable_ms(port);
  2102. spin_unlock_irq(&port->lock);
  2103. }
  2104. }
  2105. }
  2106. void serial8250_do_pm(struct uart_port *port, unsigned int state,
  2107. unsigned int oldstate)
  2108. {
  2109. struct uart_8250_port *p = up_to_u8250p(port);
  2110. serial8250_set_sleep(p, state != 0);
  2111. }
  2112. EXPORT_SYMBOL(serial8250_do_pm);
  2113. static void
  2114. serial8250_pm(struct uart_port *port, unsigned int state,
  2115. unsigned int oldstate)
  2116. {
  2117. if (port->pm)
  2118. port->pm(port, state, oldstate);
  2119. else
  2120. serial8250_do_pm(port, state, oldstate);
  2121. }
  2122. static unsigned int serial8250_port_size(struct uart_8250_port *pt)
  2123. {
  2124. if (pt->port.mapsize)
  2125. return pt->port.mapsize;
  2126. if (pt->port.iotype == UPIO_AU) {
  2127. if (pt->port.type == PORT_RT2880)
  2128. return 0x100;
  2129. return 0x1000;
  2130. }
  2131. if (is_omap1_8250(pt))
  2132. return 0x16 << pt->port.regshift;
  2133. return 8 << pt->port.regshift;
  2134. }
  2135. /*
  2136. * Resource handling.
  2137. */
  2138. static int serial8250_request_std_resource(struct uart_8250_port *up)
  2139. {
  2140. unsigned int size = serial8250_port_size(up);
  2141. struct uart_port *port = &up->port;
  2142. int ret = 0;
  2143. switch (port->iotype) {
  2144. case UPIO_AU:
  2145. case UPIO_TSI:
  2146. case UPIO_MEM32:
  2147. case UPIO_MEM32BE:
  2148. case UPIO_MEM:
  2149. if (!port->mapbase)
  2150. break;
  2151. if (!request_mem_region(port->mapbase, size, "serial")) {
  2152. ret = -EBUSY;
  2153. break;
  2154. }
  2155. if (port->flags & UPF_IOREMAP) {
  2156. port->membase = ioremap_nocache(port->mapbase, size);
  2157. if (!port->membase) {
  2158. release_mem_region(port->mapbase, size);
  2159. ret = -ENOMEM;
  2160. }
  2161. }
  2162. break;
  2163. case UPIO_HUB6:
  2164. case UPIO_PORT:
  2165. if (!request_region(port->iobase, size, "serial"))
  2166. ret = -EBUSY;
  2167. break;
  2168. }
  2169. return ret;
  2170. }
  2171. static void serial8250_release_std_resource(struct uart_8250_port *up)
  2172. {
  2173. unsigned int size = serial8250_port_size(up);
  2174. struct uart_port *port = &up->port;
  2175. switch (port->iotype) {
  2176. case UPIO_AU:
  2177. case UPIO_TSI:
  2178. case UPIO_MEM32:
  2179. case UPIO_MEM32BE:
  2180. case UPIO_MEM:
  2181. if (!port->mapbase)
  2182. break;
  2183. if (port->flags & UPF_IOREMAP) {
  2184. iounmap(port->membase);
  2185. port->membase = NULL;
  2186. }
  2187. release_mem_region(port->mapbase, size);
  2188. break;
  2189. case UPIO_HUB6:
  2190. case UPIO_PORT:
  2191. release_region(port->iobase, size);
  2192. break;
  2193. }
  2194. }
  2195. static void serial8250_release_port(struct uart_port *port)
  2196. {
  2197. struct uart_8250_port *up = up_to_u8250p(port);
  2198. serial8250_release_std_resource(up);
  2199. }
  2200. static int serial8250_request_port(struct uart_port *port)
  2201. {
  2202. struct uart_8250_port *up = up_to_u8250p(port);
  2203. return serial8250_request_std_resource(up);
  2204. }
  2205. static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
  2206. {
  2207. const struct serial8250_config *conf_type = &uart_config[up->port.type];
  2208. unsigned char bytes;
  2209. bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
  2210. return bytes ? bytes : -EOPNOTSUPP;
  2211. }
  2212. static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
  2213. {
  2214. const struct serial8250_config *conf_type = &uart_config[up->port.type];
  2215. int i;
  2216. if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
  2217. return -EOPNOTSUPP;
  2218. for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
  2219. if (bytes < conf_type->rxtrig_bytes[i])
  2220. /* Use the nearest lower value */
  2221. return (--i) << UART_FCR_R_TRIG_SHIFT;
  2222. }
  2223. return UART_FCR_R_TRIG_11;
  2224. }
  2225. static int do_get_rxtrig(struct tty_port *port)
  2226. {
  2227. struct uart_state *state = container_of(port, struct uart_state, port);
  2228. struct uart_port *uport = state->uart_port;
  2229. struct uart_8250_port *up =
  2230. container_of(uport, struct uart_8250_port, port);
  2231. if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
  2232. return -EINVAL;
  2233. return fcr_get_rxtrig_bytes(up);
  2234. }
  2235. static int do_serial8250_get_rxtrig(struct tty_port *port)
  2236. {
  2237. int rxtrig_bytes;
  2238. mutex_lock(&port->mutex);
  2239. rxtrig_bytes = do_get_rxtrig(port);
  2240. mutex_unlock(&port->mutex);
  2241. return rxtrig_bytes;
  2242. }
  2243. static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
  2244. struct device_attribute *attr, char *buf)
  2245. {
  2246. struct tty_port *port = dev_get_drvdata(dev);
  2247. int rxtrig_bytes;
  2248. rxtrig_bytes = do_serial8250_get_rxtrig(port);
  2249. if (rxtrig_bytes < 0)
  2250. return rxtrig_bytes;
  2251. return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
  2252. }
  2253. static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
  2254. {
  2255. struct uart_state *state = container_of(port, struct uart_state, port);
  2256. struct uart_port *uport = state->uart_port;
  2257. struct uart_8250_port *up =
  2258. container_of(uport, struct uart_8250_port, port);
  2259. int rxtrig;
  2260. if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
  2261. up->fifo_bug)
  2262. return -EINVAL;
  2263. rxtrig = bytes_to_fcr_rxtrig(up, bytes);
  2264. if (rxtrig < 0)
  2265. return rxtrig;
  2266. serial8250_clear_fifos(up);
  2267. up->fcr &= ~UART_FCR_TRIGGER_MASK;
  2268. up->fcr |= (unsigned char)rxtrig;
  2269. serial_out(up, UART_FCR, up->fcr);
  2270. return 0;
  2271. }
  2272. static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
  2273. {
  2274. int ret;
  2275. mutex_lock(&port->mutex);
  2276. ret = do_set_rxtrig(port, bytes);
  2277. mutex_unlock(&port->mutex);
  2278. return ret;
  2279. }
  2280. static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
  2281. struct device_attribute *attr, const char *buf, size_t count)
  2282. {
  2283. struct tty_port *port = dev_get_drvdata(dev);
  2284. unsigned char bytes;
  2285. int ret;
  2286. if (!count)
  2287. return -EINVAL;
  2288. ret = kstrtou8(buf, 10, &bytes);
  2289. if (ret < 0)
  2290. return ret;
  2291. ret = do_serial8250_set_rxtrig(port, bytes);
  2292. if (ret < 0)
  2293. return ret;
  2294. return count;
  2295. }
  2296. static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
  2297. serial8250_get_attr_rx_trig_bytes,
  2298. serial8250_set_attr_rx_trig_bytes);
  2299. static struct attribute *serial8250_dev_attrs[] = {
  2300. &dev_attr_rx_trig_bytes.attr,
  2301. NULL,
  2302. };
  2303. static struct attribute_group serial8250_dev_attr_group = {
  2304. .attrs = serial8250_dev_attrs,
  2305. };
  2306. static void register_dev_spec_attr_grp(struct uart_8250_port *up)
  2307. {
  2308. const struct serial8250_config *conf_type = &uart_config[up->port.type];
  2309. if (conf_type->rxtrig_bytes[0])
  2310. up->port.attr_group = &serial8250_dev_attr_group;
  2311. }
  2312. static void serial8250_config_port(struct uart_port *port, int flags)
  2313. {
  2314. struct uart_8250_port *up = up_to_u8250p(port);
  2315. int ret;
  2316. /*
  2317. * Find the region that we can probe for. This in turn
  2318. * tells us whether we can probe for the type of port.
  2319. */
  2320. ret = serial8250_request_std_resource(up);
  2321. if (ret < 0)
  2322. return;
  2323. if (port->iotype != up->cur_iotype)
  2324. set_io_from_upio(port);
  2325. if (flags & UART_CONFIG_TYPE)
  2326. autoconfig(up);
  2327. /* if access method is AU, it is a 16550 with a quirk */
  2328. if (port->type == PORT_16550A && port->iotype == UPIO_AU)
  2329. up->bugs |= UART_BUG_NOMSR;
  2330. /* HW bugs may trigger IRQ while IIR == NO_INT */
  2331. if (port->type == PORT_TEGRA)
  2332. up->bugs |= UART_BUG_NOMSR;
  2333. if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
  2334. autoconfig_irq(up);
  2335. if (port->type == PORT_UNKNOWN)
  2336. serial8250_release_std_resource(up);
  2337. /* Fixme: probably not the best place for this */
  2338. if ((port->type == PORT_XR17V35X) ||
  2339. (port->type == PORT_XR17D15X))
  2340. port->handle_irq = exar_handle_irq;
  2341. register_dev_spec_attr_grp(up);
  2342. up->fcr = uart_config[up->port.type].fcr;
  2343. }
  2344. static int
  2345. serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
  2346. {
  2347. if (ser->irq >= nr_irqs || ser->irq < 0 ||
  2348. ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
  2349. ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
  2350. ser->type == PORT_STARTECH)
  2351. return -EINVAL;
  2352. return 0;
  2353. }
  2354. static const char *
  2355. serial8250_type(struct uart_port *port)
  2356. {
  2357. int type = port->type;
  2358. if (type >= ARRAY_SIZE(uart_config))
  2359. type = 0;
  2360. return uart_config[type].name;
  2361. }
  2362. static const struct uart_ops serial8250_pops = {
  2363. .tx_empty = serial8250_tx_empty,
  2364. .set_mctrl = serial8250_set_mctrl,
  2365. .get_mctrl = serial8250_get_mctrl,
  2366. .stop_tx = serial8250_stop_tx,
  2367. .start_tx = serial8250_start_tx,
  2368. .throttle = serial8250_throttle,
  2369. .unthrottle = serial8250_unthrottle,
  2370. .stop_rx = serial8250_stop_rx,
  2371. .enable_ms = serial8250_enable_ms,
  2372. .break_ctl = serial8250_break_ctl,
  2373. .startup = serial8250_startup,
  2374. .shutdown = serial8250_shutdown,
  2375. .set_termios = serial8250_set_termios,
  2376. .set_ldisc = serial8250_set_ldisc,
  2377. .pm = serial8250_pm,
  2378. .type = serial8250_type,
  2379. .release_port = serial8250_release_port,
  2380. .request_port = serial8250_request_port,
  2381. .config_port = serial8250_config_port,
  2382. .verify_port = serial8250_verify_port,
  2383. #ifdef CONFIG_CONSOLE_POLL
  2384. .poll_get_char = serial8250_get_poll_char,
  2385. .poll_put_char = serial8250_put_poll_char,
  2386. #endif
  2387. };
  2388. void serial8250_init_port(struct uart_8250_port *up)
  2389. {
  2390. struct uart_port *port = &up->port;
  2391. spin_lock_init(&port->lock);
  2392. port->ops = &serial8250_pops;
  2393. up->cur_iotype = 0xFF;
  2394. }
  2395. EXPORT_SYMBOL_GPL(serial8250_init_port);
  2396. void serial8250_set_defaults(struct uart_8250_port *up)
  2397. {
  2398. struct uart_port *port = &up->port;
  2399. if (up->port.flags & UPF_FIXED_TYPE) {
  2400. unsigned int type = up->port.type;
  2401. if (!up->port.fifosize)
  2402. up->port.fifosize = uart_config[type].fifo_size;
  2403. if (!up->tx_loadsz)
  2404. up->tx_loadsz = uart_config[type].tx_loadsz;
  2405. if (!up->capabilities)
  2406. up->capabilities = uart_config[type].flags;
  2407. }
  2408. set_io_from_upio(port);
  2409. /* default dma handlers */
  2410. if (up->dma) {
  2411. if (!up->dma->tx_dma)
  2412. up->dma->tx_dma = serial8250_tx_dma;
  2413. if (!up->dma->rx_dma)
  2414. up->dma->rx_dma = serial8250_rx_dma;
  2415. }
  2416. }
  2417. EXPORT_SYMBOL_GPL(serial8250_set_defaults);
  2418. #ifdef CONFIG_SERIAL_8250_CONSOLE
  2419. static void serial8250_console_putchar(struct uart_port *port, int ch)
  2420. {
  2421. struct uart_8250_port *up = up_to_u8250p(port);
  2422. wait_for_xmitr(up, UART_LSR_THRE);
  2423. serial_port_out(port, UART_TX, ch);
  2424. }
  2425. /*
  2426. * Restore serial console when h/w power-off detected
  2427. */
  2428. static void serial8250_console_restore(struct uart_8250_port *up)
  2429. {
  2430. struct uart_port *port = &up->port;
  2431. struct ktermios termios;
  2432. unsigned int baud, quot, frac = 0;
  2433. termios.c_cflag = port->cons->cflag;
  2434. if (port->state->port.tty && termios.c_cflag == 0)
  2435. termios.c_cflag = port->state->port.tty->termios.c_cflag;
  2436. baud = serial8250_get_baud_rate(port, &termios, NULL);
  2437. quot = serial8250_get_divisor(up, baud, &frac);
  2438. serial8250_set_divisor(port, baud, quot, frac);
  2439. serial_port_out(port, UART_LCR, up->lcr);
  2440. serial_port_out(port, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
  2441. }
  2442. /*
  2443. * Print a string to the serial port trying not to disturb
  2444. * any possible real use of the port...
  2445. *
  2446. * The console_lock must be held when we get here.
  2447. */
  2448. void serial8250_console_write(struct uart_8250_port *up, const char *s,
  2449. unsigned int count)
  2450. {
  2451. struct uart_port *port = &up->port;
  2452. unsigned long flags;
  2453. unsigned int ier;
  2454. int locked = 1;
  2455. touch_nmi_watchdog();
  2456. serial8250_rpm_get(up);
  2457. if (port->sysrq)
  2458. locked = 0;
  2459. else if (oops_in_progress)
  2460. locked = spin_trylock_irqsave(&port->lock, flags);
  2461. else
  2462. spin_lock_irqsave(&port->lock, flags);
  2463. /*
  2464. * First save the IER then disable the interrupts
  2465. */
  2466. ier = serial_port_in(port, UART_IER);
  2467. if (up->capabilities & UART_CAP_UUE)
  2468. serial_port_out(port, UART_IER, UART_IER_UUE);
  2469. else
  2470. serial_port_out(port, UART_IER, 0);
  2471. /* check scratch reg to see if port powered off during system sleep */
  2472. if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
  2473. serial8250_console_restore(up);
  2474. up->canary = 0;
  2475. }
  2476. uart_console_write(port, s, count, serial8250_console_putchar);
  2477. /*
  2478. * Finally, wait for transmitter to become empty
  2479. * and restore the IER
  2480. */
  2481. wait_for_xmitr(up, BOTH_EMPTY);
  2482. serial_port_out(port, UART_IER, ier);
  2483. /*
  2484. * The receive handling will happen properly because the
  2485. * receive ready bit will still be set; it is not cleared
  2486. * on read. However, modem control will not, we must
  2487. * call it if we have saved something in the saved flags
  2488. * while processing with interrupts off.
  2489. */
  2490. if (up->msr_saved_flags)
  2491. serial8250_modem_status(up);
  2492. if (locked)
  2493. spin_unlock_irqrestore(&port->lock, flags);
  2494. serial8250_rpm_put(up);
  2495. }
  2496. static unsigned int probe_baud(struct uart_port *port)
  2497. {
  2498. unsigned char lcr, dll, dlm;
  2499. unsigned int quot;
  2500. lcr = serial_port_in(port, UART_LCR);
  2501. serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
  2502. dll = serial_port_in(port, UART_DLL);
  2503. dlm = serial_port_in(port, UART_DLM);
  2504. serial_port_out(port, UART_LCR, lcr);
  2505. quot = (dlm << 8) | dll;
  2506. return (port->uartclk / 16) / quot;
  2507. }
  2508. int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
  2509. {
  2510. int baud = 9600;
  2511. int bits = 8;
  2512. int parity = 'n';
  2513. int flow = 'n';
  2514. if (!port->iobase && !port->membase)
  2515. return -ENODEV;
  2516. if (options)
  2517. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2518. else if (probe)
  2519. baud = probe_baud(port);
  2520. return uart_set_options(port, port->cons, baud, parity, bits, flow);
  2521. }
  2522. #endif /* CONFIG_SERIAL_8250_CONSOLE */
  2523. MODULE_LICENSE("GPL");