8250_port.c 81 KB

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