8250_port.c 80 KB

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