8250_port.c 83 KB

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