8250_port.c 84 KB

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