8250_port.c 82 KB

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