8250_port.c 89 KB

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