amba-pl011.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for AMBA serial ports
  4. *
  5. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  6. *
  7. * Copyright 1999 ARM Limited
  8. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  9. * Copyright (C) 2010 ST-Ericsson SA
  10. *
  11. * This is a generic driver for ARM AMBA-type serial ports. They
  12. * have a lot of 16550-like features, but are not register compatible.
  13. * Note that although they do have CTS, DCD and DSR inputs, they do
  14. * not have an RI input, nor do they have DTR or RTS outputs. If
  15. * required, these have to be supplied via some other means (eg, GPIO)
  16. * and hooked into this driver.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/ioport.h>
  20. #include <linux/init.h>
  21. #include <linux/console.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/sysrq.h>
  24. #include <linux/device.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/serial_core.h>
  28. #include <linux/serial.h>
  29. #include <linux/amba/bus.h>
  30. #include <linux/amba/serial.h>
  31. #include <linux/clk.h>
  32. #include <linux/slab.h>
  33. #include <linux/dmaengine.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/scatterlist.h>
  36. #include <linux/delay.h>
  37. #include <linux/types.h>
  38. #include <linux/of.h>
  39. #include <linux/pinctrl/consumer.h>
  40. #include <linux/sizes.h>
  41. #include <linux/io.h>
  42. #include <linux/acpi.h>
  43. #define UART_NR 14
  44. #define SERIAL_AMBA_MAJOR 204
  45. #define SERIAL_AMBA_MINOR 64
  46. #define SERIAL_AMBA_NR UART_NR
  47. #define AMBA_ISR_PASS_LIMIT 256
  48. #define UART_DR_ERROR (UART011_DR_OE | UART011_DR_BE | UART011_DR_PE | UART011_DR_FE)
  49. #define UART_DUMMY_DR_RX BIT(16)
  50. enum {
  51. REG_DR,
  52. REG_ST_DMAWM,
  53. REG_ST_TIMEOUT,
  54. REG_FR,
  55. REG_LCRH_RX,
  56. REG_LCRH_TX,
  57. REG_IBRD,
  58. REG_FBRD,
  59. REG_CR,
  60. REG_IFLS,
  61. REG_IMSC,
  62. REG_RIS,
  63. REG_MIS,
  64. REG_ICR,
  65. REG_DMACR,
  66. REG_ST_XFCR,
  67. REG_ST_XON1,
  68. REG_ST_XON2,
  69. REG_ST_XOFF1,
  70. REG_ST_XOFF2,
  71. REG_ST_ITCR,
  72. REG_ST_ITIP,
  73. REG_ST_ABCR,
  74. REG_ST_ABIMSC,
  75. /* The size of the array - must be last */
  76. REG_ARRAY_SIZE,
  77. };
  78. static u16 pl011_std_offsets[REG_ARRAY_SIZE] = {
  79. [REG_DR] = UART01x_DR,
  80. [REG_FR] = UART01x_FR,
  81. [REG_LCRH_RX] = UART011_LCRH,
  82. [REG_LCRH_TX] = UART011_LCRH,
  83. [REG_IBRD] = UART011_IBRD,
  84. [REG_FBRD] = UART011_FBRD,
  85. [REG_CR] = UART011_CR,
  86. [REG_IFLS] = UART011_IFLS,
  87. [REG_IMSC] = UART011_IMSC,
  88. [REG_RIS] = UART011_RIS,
  89. [REG_MIS] = UART011_MIS,
  90. [REG_ICR] = UART011_ICR,
  91. [REG_DMACR] = UART011_DMACR,
  92. };
  93. /* There is by now at least one vendor with differing details, so handle it */
  94. struct vendor_data {
  95. const u16 *reg_offset;
  96. unsigned int ifls;
  97. unsigned int fr_busy;
  98. unsigned int fr_dsr;
  99. unsigned int fr_cts;
  100. unsigned int fr_ri;
  101. unsigned int inv_fr;
  102. bool access_32b;
  103. bool oversampling;
  104. bool dma_threshold;
  105. bool cts_event_workaround;
  106. bool always_enabled;
  107. bool fixed_options;
  108. unsigned int (*get_fifosize)(struct amba_device *dev);
  109. };
  110. static unsigned int get_fifosize_arm(struct amba_device *dev)
  111. {
  112. return amba_rev(dev) < 3 ? 16 : 32;
  113. }
  114. static struct vendor_data vendor_arm = {
  115. .reg_offset = pl011_std_offsets,
  116. .ifls = UART011_IFLS_RX4_8 | UART011_IFLS_TX4_8,
  117. .fr_busy = UART01x_FR_BUSY,
  118. .fr_dsr = UART01x_FR_DSR,
  119. .fr_cts = UART01x_FR_CTS,
  120. .fr_ri = UART011_FR_RI,
  121. .oversampling = false,
  122. .dma_threshold = false,
  123. .cts_event_workaround = false,
  124. .always_enabled = false,
  125. .fixed_options = false,
  126. .get_fifosize = get_fifosize_arm,
  127. };
  128. static const struct vendor_data vendor_sbsa = {
  129. .reg_offset = pl011_std_offsets,
  130. .fr_busy = UART01x_FR_BUSY,
  131. .fr_dsr = UART01x_FR_DSR,
  132. .fr_cts = UART01x_FR_CTS,
  133. .fr_ri = UART011_FR_RI,
  134. .access_32b = true,
  135. .oversampling = false,
  136. .dma_threshold = false,
  137. .cts_event_workaround = false,
  138. .always_enabled = true,
  139. .fixed_options = true,
  140. };
  141. #ifdef CONFIG_ACPI_SPCR_TABLE
  142. static const struct vendor_data vendor_qdt_qdf2400_e44 = {
  143. .reg_offset = pl011_std_offsets,
  144. .fr_busy = UART011_FR_TXFE,
  145. .fr_dsr = UART01x_FR_DSR,
  146. .fr_cts = UART01x_FR_CTS,
  147. .fr_ri = UART011_FR_RI,
  148. .inv_fr = UART011_FR_TXFE,
  149. .access_32b = true,
  150. .oversampling = false,
  151. .dma_threshold = false,
  152. .cts_event_workaround = false,
  153. .always_enabled = true,
  154. .fixed_options = true,
  155. };
  156. #endif
  157. static u16 pl011_st_offsets[REG_ARRAY_SIZE] = {
  158. [REG_DR] = UART01x_DR,
  159. [REG_ST_DMAWM] = ST_UART011_DMAWM,
  160. [REG_ST_TIMEOUT] = ST_UART011_TIMEOUT,
  161. [REG_FR] = UART01x_FR,
  162. [REG_LCRH_RX] = ST_UART011_LCRH_RX,
  163. [REG_LCRH_TX] = ST_UART011_LCRH_TX,
  164. [REG_IBRD] = UART011_IBRD,
  165. [REG_FBRD] = UART011_FBRD,
  166. [REG_CR] = UART011_CR,
  167. [REG_IFLS] = UART011_IFLS,
  168. [REG_IMSC] = UART011_IMSC,
  169. [REG_RIS] = UART011_RIS,
  170. [REG_MIS] = UART011_MIS,
  171. [REG_ICR] = UART011_ICR,
  172. [REG_DMACR] = UART011_DMACR,
  173. [REG_ST_XFCR] = ST_UART011_XFCR,
  174. [REG_ST_XON1] = ST_UART011_XON1,
  175. [REG_ST_XON2] = ST_UART011_XON2,
  176. [REG_ST_XOFF1] = ST_UART011_XOFF1,
  177. [REG_ST_XOFF2] = ST_UART011_XOFF2,
  178. [REG_ST_ITCR] = ST_UART011_ITCR,
  179. [REG_ST_ITIP] = ST_UART011_ITIP,
  180. [REG_ST_ABCR] = ST_UART011_ABCR,
  181. [REG_ST_ABIMSC] = ST_UART011_ABIMSC,
  182. };
  183. static unsigned int get_fifosize_st(struct amba_device *dev)
  184. {
  185. return 64;
  186. }
  187. static struct vendor_data vendor_st = {
  188. .reg_offset = pl011_st_offsets,
  189. .ifls = UART011_IFLS_RX_HALF | UART011_IFLS_TX_HALF,
  190. .fr_busy = UART01x_FR_BUSY,
  191. .fr_dsr = UART01x_FR_DSR,
  192. .fr_cts = UART01x_FR_CTS,
  193. .fr_ri = UART011_FR_RI,
  194. .oversampling = true,
  195. .dma_threshold = true,
  196. .cts_event_workaround = true,
  197. .always_enabled = false,
  198. .fixed_options = false,
  199. .get_fifosize = get_fifosize_st,
  200. };
  201. /* Deals with DMA transactions */
  202. struct pl011_dmabuf {
  203. dma_addr_t dma;
  204. size_t len;
  205. char *buf;
  206. };
  207. struct pl011_dmarx_data {
  208. struct dma_chan *chan;
  209. struct completion complete;
  210. bool use_buf_b;
  211. struct pl011_dmabuf dbuf_a;
  212. struct pl011_dmabuf dbuf_b;
  213. dma_cookie_t cookie;
  214. bool running;
  215. struct timer_list timer;
  216. unsigned int last_residue;
  217. unsigned long last_jiffies;
  218. bool auto_poll_rate;
  219. unsigned int poll_rate;
  220. unsigned int poll_timeout;
  221. };
  222. struct pl011_dmatx_data {
  223. struct dma_chan *chan;
  224. dma_addr_t dma;
  225. size_t len;
  226. char *buf;
  227. bool queued;
  228. };
  229. /*
  230. * We wrap our port structure around the generic uart_port.
  231. */
  232. struct uart_amba_port {
  233. struct uart_port port;
  234. const u16 *reg_offset;
  235. struct clk *clk;
  236. const struct vendor_data *vendor;
  237. unsigned int im; /* interrupt mask */
  238. unsigned int old_status;
  239. unsigned int fifosize; /* vendor-specific */
  240. unsigned int fixed_baud; /* vendor-set fixed baud rate */
  241. char type[12];
  242. bool rs485_tx_started;
  243. unsigned int rs485_tx_drain_interval; /* usecs */
  244. #ifdef CONFIG_DMA_ENGINE
  245. /* DMA stuff */
  246. unsigned int dmacr; /* dma control reg */
  247. bool using_tx_dma;
  248. bool using_rx_dma;
  249. struct pl011_dmarx_data dmarx;
  250. struct pl011_dmatx_data dmatx;
  251. bool dma_probed;
  252. #endif
  253. };
  254. static unsigned int pl011_tx_empty(struct uart_port *port);
  255. static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
  256. unsigned int reg)
  257. {
  258. return uap->reg_offset[reg];
  259. }
  260. static unsigned int pl011_read(const struct uart_amba_port *uap,
  261. unsigned int reg)
  262. {
  263. void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
  264. return (uap->port.iotype == UPIO_MEM32) ?
  265. readl_relaxed(addr) : readw_relaxed(addr);
  266. }
  267. static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
  268. unsigned int reg)
  269. {
  270. void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
  271. if (uap->port.iotype == UPIO_MEM32)
  272. writel_relaxed(val, addr);
  273. else
  274. writew_relaxed(val, addr);
  275. }
  276. /*
  277. * Reads up to 256 characters from the FIFO or until it's empty and
  278. * inserts them into the TTY layer. Returns the number of characters
  279. * read from the FIFO.
  280. */
  281. static int pl011_fifo_to_tty(struct uart_amba_port *uap)
  282. {
  283. unsigned int ch, fifotaken;
  284. int sysrq;
  285. u16 status;
  286. u8 flag;
  287. for (fifotaken = 0; fifotaken != 256; fifotaken++) {
  288. status = pl011_read(uap, REG_FR);
  289. if (status & UART01x_FR_RXFE)
  290. break;
  291. /* Take chars from the FIFO and update status */
  292. ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
  293. flag = TTY_NORMAL;
  294. uap->port.icount.rx++;
  295. if (unlikely(ch & UART_DR_ERROR)) {
  296. if (ch & UART011_DR_BE) {
  297. ch &= ~(UART011_DR_FE | UART011_DR_PE);
  298. uap->port.icount.brk++;
  299. if (uart_handle_break(&uap->port))
  300. continue;
  301. } else if (ch & UART011_DR_PE) {
  302. uap->port.icount.parity++;
  303. } else if (ch & UART011_DR_FE) {
  304. uap->port.icount.frame++;
  305. }
  306. if (ch & UART011_DR_OE)
  307. uap->port.icount.overrun++;
  308. ch &= uap->port.read_status_mask;
  309. if (ch & UART011_DR_BE)
  310. flag = TTY_BREAK;
  311. else if (ch & UART011_DR_PE)
  312. flag = TTY_PARITY;
  313. else if (ch & UART011_DR_FE)
  314. flag = TTY_FRAME;
  315. }
  316. sysrq = uart_prepare_sysrq_char(&uap->port, ch & 255);
  317. if (!sysrq)
  318. uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
  319. }
  320. return fifotaken;
  321. }
  322. /*
  323. * All the DMA operation mode stuff goes inside this ifdef.
  324. * This assumes that you have a generic DMA device interface,
  325. * no custom DMA interfaces are supported.
  326. */
  327. #ifdef CONFIG_DMA_ENGINE
  328. #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
  329. static int pl011_dmabuf_init(struct dma_chan *chan, struct pl011_dmabuf *db,
  330. enum dma_data_direction dir)
  331. {
  332. db->buf = dma_alloc_coherent(chan->device->dev, PL011_DMA_BUFFER_SIZE,
  333. &db->dma, GFP_KERNEL);
  334. if (!db->buf)
  335. return -ENOMEM;
  336. db->len = PL011_DMA_BUFFER_SIZE;
  337. return 0;
  338. }
  339. static void pl011_dmabuf_free(struct dma_chan *chan, struct pl011_dmabuf *db,
  340. enum dma_data_direction dir)
  341. {
  342. if (db->buf) {
  343. dma_free_coherent(chan->device->dev,
  344. PL011_DMA_BUFFER_SIZE, db->buf, db->dma);
  345. }
  346. }
  347. static void pl011_dma_probe(struct uart_amba_port *uap)
  348. {
  349. /* DMA is the sole user of the platform data right now */
  350. struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
  351. struct device *dev = uap->port.dev;
  352. struct dma_slave_config tx_conf = {
  353. .dst_addr = uap->port.mapbase +
  354. pl011_reg_to_offset(uap, REG_DR),
  355. .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  356. .direction = DMA_MEM_TO_DEV,
  357. .dst_maxburst = uap->fifosize >> 1,
  358. .device_fc = false,
  359. };
  360. struct dma_chan *chan;
  361. dma_cap_mask_t mask;
  362. uap->dma_probed = true;
  363. chan = dma_request_chan(dev, "tx");
  364. if (IS_ERR(chan)) {
  365. if (PTR_ERR(chan) == -EPROBE_DEFER) {
  366. uap->dma_probed = false;
  367. return;
  368. }
  369. /* We need platform data */
  370. if (!plat || !plat->dma_filter) {
  371. dev_dbg(uap->port.dev, "no DMA platform data\n");
  372. return;
  373. }
  374. /* Try to acquire a generic DMA engine slave TX channel */
  375. dma_cap_zero(mask);
  376. dma_cap_set(DMA_SLAVE, mask);
  377. chan = dma_request_channel(mask, plat->dma_filter,
  378. plat->dma_tx_param);
  379. if (!chan) {
  380. dev_err(uap->port.dev, "no TX DMA channel!\n");
  381. return;
  382. }
  383. }
  384. dmaengine_slave_config(chan, &tx_conf);
  385. uap->dmatx.chan = chan;
  386. dev_info(uap->port.dev, "DMA channel TX %s\n",
  387. dma_chan_name(uap->dmatx.chan));
  388. /* Optionally make use of an RX channel as well */
  389. chan = dma_request_chan(dev, "rx");
  390. if (IS_ERR(chan) && plat && plat->dma_rx_param) {
  391. chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
  392. if (!chan) {
  393. dev_err(uap->port.dev, "no RX DMA channel!\n");
  394. return;
  395. }
  396. }
  397. if (!IS_ERR(chan)) {
  398. struct dma_slave_config rx_conf = {
  399. .src_addr = uap->port.mapbase +
  400. pl011_reg_to_offset(uap, REG_DR),
  401. .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  402. .direction = DMA_DEV_TO_MEM,
  403. .src_maxburst = uap->fifosize >> 2,
  404. .device_fc = false,
  405. };
  406. struct dma_slave_caps caps;
  407. /*
  408. * Some DMA controllers provide information on their capabilities.
  409. * If the controller does, check for suitable residue processing
  410. * otherwise assime all is well.
  411. */
  412. if (dma_get_slave_caps(chan, &caps) == 0) {
  413. if (caps.residue_granularity ==
  414. DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
  415. dma_release_channel(chan);
  416. dev_info(uap->port.dev,
  417. "RX DMA disabled - no residue processing\n");
  418. return;
  419. }
  420. }
  421. dmaengine_slave_config(chan, &rx_conf);
  422. uap->dmarx.chan = chan;
  423. uap->dmarx.auto_poll_rate = false;
  424. if (plat && plat->dma_rx_poll_enable) {
  425. /* Set poll rate if specified. */
  426. if (plat->dma_rx_poll_rate) {
  427. uap->dmarx.auto_poll_rate = false;
  428. uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
  429. } else {
  430. /*
  431. * 100 ms defaults to poll rate if not
  432. * specified. This will be adjusted with
  433. * the baud rate at set_termios.
  434. */
  435. uap->dmarx.auto_poll_rate = true;
  436. uap->dmarx.poll_rate = 100;
  437. }
  438. /* 3 secs defaults poll_timeout if not specified. */
  439. if (plat->dma_rx_poll_timeout)
  440. uap->dmarx.poll_timeout =
  441. plat->dma_rx_poll_timeout;
  442. else
  443. uap->dmarx.poll_timeout = 3000;
  444. } else if (!plat && dev->of_node) {
  445. uap->dmarx.auto_poll_rate =
  446. of_property_read_bool(dev->of_node, "auto-poll");
  447. if (uap->dmarx.auto_poll_rate) {
  448. u32 x;
  449. if (of_property_read_u32(dev->of_node, "poll-rate-ms", &x) == 0)
  450. uap->dmarx.poll_rate = x;
  451. else
  452. uap->dmarx.poll_rate = 100;
  453. if (of_property_read_u32(dev->of_node, "poll-timeout-ms", &x) == 0)
  454. uap->dmarx.poll_timeout = x;
  455. else
  456. uap->dmarx.poll_timeout = 3000;
  457. }
  458. }
  459. dev_info(uap->port.dev, "DMA channel RX %s\n",
  460. dma_chan_name(uap->dmarx.chan));
  461. }
  462. }
  463. static void pl011_dma_remove(struct uart_amba_port *uap)
  464. {
  465. if (uap->dmatx.chan)
  466. dma_release_channel(uap->dmatx.chan);
  467. if (uap->dmarx.chan)
  468. dma_release_channel(uap->dmarx.chan);
  469. }
  470. /* Forward declare these for the refill routine */
  471. static int pl011_dma_tx_refill(struct uart_amba_port *uap);
  472. static void pl011_start_tx_pio(struct uart_amba_port *uap);
  473. /*
  474. * The current DMA TX buffer has been sent.
  475. * Try to queue up another DMA buffer.
  476. */
  477. static void pl011_dma_tx_callback(void *data)
  478. {
  479. struct uart_amba_port *uap = data;
  480. struct tty_port *tport = &uap->port.state->port;
  481. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  482. unsigned long flags;
  483. u16 dmacr;
  484. uart_port_lock_irqsave(&uap->port, &flags);
  485. if (uap->dmatx.queued)
  486. dma_unmap_single(dmatx->chan->device->dev, dmatx->dma,
  487. dmatx->len, DMA_TO_DEVICE);
  488. dmacr = uap->dmacr;
  489. uap->dmacr = dmacr & ~UART011_TXDMAE;
  490. pl011_write(uap->dmacr, uap, REG_DMACR);
  491. /*
  492. * If TX DMA was disabled, it means that we've stopped the DMA for
  493. * some reason (eg, XOFF received, or we want to send an X-char.)
  494. *
  495. * Note: we need to be careful here of a potential race between DMA
  496. * and the rest of the driver - if the driver disables TX DMA while
  497. * a TX buffer completing, we must update the tx queued status to
  498. * get further refills (hence we check dmacr).
  499. */
  500. if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
  501. kfifo_is_empty(&tport->xmit_fifo)) {
  502. uap->dmatx.queued = false;
  503. uart_port_unlock_irqrestore(&uap->port, flags);
  504. return;
  505. }
  506. if (pl011_dma_tx_refill(uap) <= 0)
  507. /*
  508. * We didn't queue a DMA buffer for some reason, but we
  509. * have data pending to be sent. Re-enable the TX IRQ.
  510. */
  511. pl011_start_tx_pio(uap);
  512. uart_port_unlock_irqrestore(&uap->port, flags);
  513. }
  514. /*
  515. * Try to refill the TX DMA buffer.
  516. * Locking: called with port lock held and IRQs disabled.
  517. * Returns:
  518. * 1 if we queued up a TX DMA buffer.
  519. * 0 if we didn't want to handle this by DMA
  520. * <0 on error
  521. */
  522. static int pl011_dma_tx_refill(struct uart_amba_port *uap)
  523. {
  524. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  525. struct dma_chan *chan = dmatx->chan;
  526. struct dma_device *dma_dev = chan->device;
  527. struct dma_async_tx_descriptor *desc;
  528. struct tty_port *tport = &uap->port.state->port;
  529. unsigned int count;
  530. /*
  531. * Try to avoid the overhead involved in using DMA if the
  532. * transaction fits in the first half of the FIFO, by using
  533. * the standard interrupt handling. This ensures that we
  534. * issue a uart_write_wakeup() at the appropriate time.
  535. */
  536. count = kfifo_len(&tport->xmit_fifo);
  537. if (count < (uap->fifosize >> 1)) {
  538. uap->dmatx.queued = false;
  539. return 0;
  540. }
  541. /*
  542. * Bodge: don't send the last character by DMA, as this
  543. * will prevent XON from notifying us to restart DMA.
  544. */
  545. count -= 1;
  546. /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
  547. if (count > PL011_DMA_BUFFER_SIZE)
  548. count = PL011_DMA_BUFFER_SIZE;
  549. count = kfifo_out_peek(&tport->xmit_fifo, dmatx->buf, count);
  550. dmatx->len = count;
  551. dmatx->dma = dma_map_single(dma_dev->dev, dmatx->buf, count,
  552. DMA_TO_DEVICE);
  553. if (dmatx->dma == DMA_MAPPING_ERROR) {
  554. uap->dmatx.queued = false;
  555. dev_dbg(uap->port.dev, "unable to map TX DMA\n");
  556. return -EBUSY;
  557. }
  558. desc = dmaengine_prep_slave_single(chan, dmatx->dma, dmatx->len, DMA_MEM_TO_DEV,
  559. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  560. if (!desc) {
  561. dma_unmap_single(dma_dev->dev, dmatx->dma, dmatx->len, DMA_TO_DEVICE);
  562. uap->dmatx.queued = false;
  563. /*
  564. * If DMA cannot be used right now, we complete this
  565. * transaction via IRQ and let the TTY layer retry.
  566. */
  567. dev_dbg(uap->port.dev, "TX DMA busy\n");
  568. return -EBUSY;
  569. }
  570. /* Some data to go along to the callback */
  571. desc->callback = pl011_dma_tx_callback;
  572. desc->callback_param = uap;
  573. /* All errors should happen at prepare time */
  574. dmaengine_submit(desc);
  575. /* Fire the DMA transaction */
  576. dma_dev->device_issue_pending(chan);
  577. uap->dmacr |= UART011_TXDMAE;
  578. pl011_write(uap->dmacr, uap, REG_DMACR);
  579. uap->dmatx.queued = true;
  580. /*
  581. * Now we know that DMA will fire, so advance the ring buffer
  582. * with the stuff we just dispatched.
  583. */
  584. uart_xmit_advance(&uap->port, count);
  585. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  586. uart_write_wakeup(&uap->port);
  587. return 1;
  588. }
  589. /*
  590. * We received a transmit interrupt without a pending X-char but with
  591. * pending characters.
  592. * Locking: called with port lock held and IRQs disabled.
  593. * Returns:
  594. * false if we want to use PIO to transmit
  595. * true if we queued a DMA buffer
  596. */
  597. static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  598. {
  599. if (!uap->using_tx_dma)
  600. return false;
  601. /*
  602. * If we already have a TX buffer queued, but received a
  603. * TX interrupt, it will be because we've just sent an X-char.
  604. * Ensure the TX DMA is enabled and the TX IRQ is disabled.
  605. */
  606. if (uap->dmatx.queued) {
  607. uap->dmacr |= UART011_TXDMAE;
  608. pl011_write(uap->dmacr, uap, REG_DMACR);
  609. uap->im &= ~UART011_TXIM;
  610. pl011_write(uap->im, uap, REG_IMSC);
  611. return true;
  612. }
  613. /*
  614. * We don't have a TX buffer queued, so try to queue one.
  615. * If we successfully queued a buffer, mask the TX IRQ.
  616. */
  617. if (pl011_dma_tx_refill(uap) > 0) {
  618. uap->im &= ~UART011_TXIM;
  619. pl011_write(uap->im, uap, REG_IMSC);
  620. return true;
  621. }
  622. return false;
  623. }
  624. /*
  625. * Stop the DMA transmit (eg, due to received XOFF).
  626. * Locking: called with port lock held and IRQs disabled.
  627. */
  628. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  629. {
  630. if (uap->dmatx.queued) {
  631. uap->dmacr &= ~UART011_TXDMAE;
  632. pl011_write(uap->dmacr, uap, REG_DMACR);
  633. }
  634. }
  635. /*
  636. * Try to start a DMA transmit, or in the case of an XON/OFF
  637. * character queued for send, try to get that character out ASAP.
  638. * Locking: called with port lock held and IRQs disabled.
  639. * Returns:
  640. * false if we want the TX IRQ to be enabled
  641. * true if we have a buffer queued
  642. */
  643. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  644. {
  645. u16 dmacr;
  646. if (!uap->using_tx_dma)
  647. return false;
  648. if (!uap->port.x_char) {
  649. /* no X-char, try to push chars out in DMA mode */
  650. bool ret = true;
  651. if (!uap->dmatx.queued) {
  652. if (pl011_dma_tx_refill(uap) > 0) {
  653. uap->im &= ~UART011_TXIM;
  654. pl011_write(uap->im, uap, REG_IMSC);
  655. } else {
  656. ret = false;
  657. }
  658. } else if (!(uap->dmacr & UART011_TXDMAE)) {
  659. uap->dmacr |= UART011_TXDMAE;
  660. pl011_write(uap->dmacr, uap, REG_DMACR);
  661. }
  662. return ret;
  663. }
  664. /*
  665. * We have an X-char to send. Disable DMA to prevent it loading
  666. * the TX fifo, and then see if we can stuff it into the FIFO.
  667. */
  668. dmacr = uap->dmacr;
  669. uap->dmacr &= ~UART011_TXDMAE;
  670. pl011_write(uap->dmacr, uap, REG_DMACR);
  671. if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
  672. /*
  673. * No space in the FIFO, so enable the transmit interrupt
  674. * so we know when there is space. Note that once we've
  675. * loaded the character, we should just re-enable DMA.
  676. */
  677. return false;
  678. }
  679. pl011_write(uap->port.x_char, uap, REG_DR);
  680. uap->port.icount.tx++;
  681. uap->port.x_char = 0;
  682. /* Success - restore the DMA state */
  683. uap->dmacr = dmacr;
  684. pl011_write(dmacr, uap, REG_DMACR);
  685. return true;
  686. }
  687. /*
  688. * Flush the transmit buffer.
  689. * Locking: called with port lock held and IRQs disabled.
  690. */
  691. static void pl011_dma_flush_buffer(struct uart_port *port)
  692. __releases(&uap->port.lock)
  693. __acquires(&uap->port.lock)
  694. {
  695. struct uart_amba_port *uap =
  696. container_of(port, struct uart_amba_port, port);
  697. if (!uap->using_tx_dma)
  698. return;
  699. dmaengine_terminate_async(uap->dmatx.chan);
  700. if (uap->dmatx.queued) {
  701. dma_unmap_single(uap->dmatx.chan->device->dev, uap->dmatx.dma,
  702. uap->dmatx.len, DMA_TO_DEVICE);
  703. uap->dmatx.queued = false;
  704. uap->dmacr &= ~UART011_TXDMAE;
  705. pl011_write(uap->dmacr, uap, REG_DMACR);
  706. }
  707. }
  708. static void pl011_dma_rx_callback(void *data);
  709. static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  710. {
  711. struct dma_chan *rxchan = uap->dmarx.chan;
  712. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  713. struct dma_async_tx_descriptor *desc;
  714. struct pl011_dmabuf *dbuf;
  715. if (!rxchan)
  716. return -EIO;
  717. /* Start the RX DMA job */
  718. dbuf = uap->dmarx.use_buf_b ?
  719. &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
  720. desc = dmaengine_prep_slave_single(rxchan, dbuf->dma, dbuf->len,
  721. DMA_DEV_TO_MEM,
  722. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  723. /*
  724. * If the DMA engine is busy and cannot prepare a
  725. * channel, no big deal, the driver will fall back
  726. * to interrupt mode as a result of this error code.
  727. */
  728. if (!desc) {
  729. uap->dmarx.running = false;
  730. dmaengine_terminate_all(rxchan);
  731. return -EBUSY;
  732. }
  733. /* Some data to go along to the callback */
  734. desc->callback = pl011_dma_rx_callback;
  735. desc->callback_param = uap;
  736. dmarx->cookie = dmaengine_submit(desc);
  737. dma_async_issue_pending(rxchan);
  738. uap->dmacr |= UART011_RXDMAE;
  739. pl011_write(uap->dmacr, uap, REG_DMACR);
  740. uap->dmarx.running = true;
  741. uap->im &= ~UART011_RXIM;
  742. pl011_write(uap->im, uap, REG_IMSC);
  743. return 0;
  744. }
  745. /*
  746. * This is called when either the DMA job is complete, or
  747. * the FIFO timeout interrupt occurred. This must be called
  748. * with the port spinlock uap->port.lock held.
  749. */
  750. static void pl011_dma_rx_chars(struct uart_amba_port *uap,
  751. u32 pending, bool use_buf_b,
  752. bool readfifo)
  753. {
  754. struct tty_port *port = &uap->port.state->port;
  755. struct pl011_dmabuf *dbuf = use_buf_b ?
  756. &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
  757. int dma_count = 0;
  758. u32 fifotaken = 0; /* only used for vdbg() */
  759. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  760. int dmataken = 0;
  761. if (uap->dmarx.poll_rate) {
  762. /* The data can be taken by polling */
  763. dmataken = dbuf->len - dmarx->last_residue;
  764. /* Recalculate the pending size */
  765. if (pending >= dmataken)
  766. pending -= dmataken;
  767. }
  768. /* Pick the remain data from the DMA */
  769. if (pending) {
  770. /*
  771. * First take all chars in the DMA pipe, then look in the FIFO.
  772. * Note that tty_insert_flip_buf() tries to take as many chars
  773. * as it can.
  774. */
  775. dma_count = tty_insert_flip_string(port, dbuf->buf + dmataken, pending);
  776. uap->port.icount.rx += dma_count;
  777. if (dma_count < pending)
  778. dev_warn(uap->port.dev,
  779. "couldn't insert all characters (TTY is full?)\n");
  780. }
  781. /* Reset the last_residue for Rx DMA poll */
  782. if (uap->dmarx.poll_rate)
  783. dmarx->last_residue = dbuf->len;
  784. /*
  785. * Only continue with trying to read the FIFO if all DMA chars have
  786. * been taken first.
  787. */
  788. if (dma_count == pending && readfifo) {
  789. /* Clear any error flags */
  790. pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
  791. UART011_FEIS, uap, REG_ICR);
  792. /*
  793. * If we read all the DMA'd characters, and we had an
  794. * incomplete buffer, that could be due to an rx error, or
  795. * maybe we just timed out. Read any pending chars and check
  796. * the error status.
  797. *
  798. * Error conditions will only occur in the FIFO, these will
  799. * trigger an immediate interrupt and stop the DMA job, so we
  800. * will always find the error in the FIFO, never in the DMA
  801. * buffer.
  802. */
  803. fifotaken = pl011_fifo_to_tty(uap);
  804. }
  805. dev_vdbg(uap->port.dev,
  806. "Took %d chars from DMA buffer and %d chars from the FIFO\n",
  807. dma_count, fifotaken);
  808. tty_flip_buffer_push(port);
  809. }
  810. static void pl011_dma_rx_irq(struct uart_amba_port *uap)
  811. {
  812. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  813. struct dma_chan *rxchan = dmarx->chan;
  814. struct pl011_dmabuf *dbuf = dmarx->use_buf_b ?
  815. &dmarx->dbuf_b : &dmarx->dbuf_a;
  816. size_t pending;
  817. struct dma_tx_state state;
  818. enum dma_status dmastat;
  819. /*
  820. * Pause the transfer so we can trust the current counter,
  821. * do this before we pause the PL011 block, else we may
  822. * overflow the FIFO.
  823. */
  824. if (dmaengine_pause(rxchan))
  825. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  826. dmastat = rxchan->device->device_tx_status(rxchan,
  827. dmarx->cookie, &state);
  828. if (dmastat != DMA_PAUSED)
  829. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  830. /* Disable RX DMA - incoming data will wait in the FIFO */
  831. uap->dmacr &= ~UART011_RXDMAE;
  832. pl011_write(uap->dmacr, uap, REG_DMACR);
  833. uap->dmarx.running = false;
  834. pending = dbuf->len - state.residue;
  835. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  836. /* Then we terminate the transfer - we now know our residue */
  837. dmaengine_terminate_all(rxchan);
  838. /*
  839. * This will take the chars we have so far and insert
  840. * into the framework.
  841. */
  842. pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
  843. /* Switch buffer & re-trigger DMA job */
  844. dmarx->use_buf_b = !dmarx->use_buf_b;
  845. if (pl011_dma_rx_trigger_dma(uap)) {
  846. dev_dbg(uap->port.dev,
  847. "could not retrigger RX DMA job fall back to interrupt mode\n");
  848. uap->im |= UART011_RXIM;
  849. pl011_write(uap->im, uap, REG_IMSC);
  850. }
  851. }
  852. static void pl011_dma_rx_callback(void *data)
  853. {
  854. struct uart_amba_port *uap = data;
  855. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  856. struct dma_chan *rxchan = dmarx->chan;
  857. bool lastbuf = dmarx->use_buf_b;
  858. struct pl011_dmabuf *dbuf = dmarx->use_buf_b ?
  859. &dmarx->dbuf_b : &dmarx->dbuf_a;
  860. size_t pending;
  861. struct dma_tx_state state;
  862. int ret;
  863. /*
  864. * This completion interrupt occurs typically when the
  865. * RX buffer is totally stuffed but no timeout has yet
  866. * occurred. When that happens, we just want the RX
  867. * routine to flush out the secondary DMA buffer while
  868. * we immediately trigger the next DMA job.
  869. */
  870. uart_port_lock_irq(&uap->port);
  871. /*
  872. * Rx data can be taken by the UART interrupts during
  873. * the DMA irq handler. So we check the residue here.
  874. */
  875. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  876. pending = dbuf->len - state.residue;
  877. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  878. /* Then we terminate the transfer - we now know our residue */
  879. dmaengine_terminate_all(rxchan);
  880. uap->dmarx.running = false;
  881. dmarx->use_buf_b = !lastbuf;
  882. ret = pl011_dma_rx_trigger_dma(uap);
  883. pl011_dma_rx_chars(uap, pending, lastbuf, false);
  884. uart_unlock_and_check_sysrq(&uap->port);
  885. /*
  886. * Do this check after we picked the DMA chars so we don't
  887. * get some IRQ immediately from RX.
  888. */
  889. if (ret) {
  890. dev_dbg(uap->port.dev,
  891. "could not retrigger RX DMA job fall back to interrupt mode\n");
  892. uap->im |= UART011_RXIM;
  893. pl011_write(uap->im, uap, REG_IMSC);
  894. }
  895. }
  896. /*
  897. * Stop accepting received characters, when we're shutting down or
  898. * suspending this port.
  899. * Locking: called with port lock held and IRQs disabled.
  900. */
  901. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  902. {
  903. if (!uap->using_rx_dma)
  904. return;
  905. /* FIXME. Just disable the DMA enable */
  906. uap->dmacr &= ~UART011_RXDMAE;
  907. pl011_write(uap->dmacr, uap, REG_DMACR);
  908. }
  909. /*
  910. * Timer handler for Rx DMA polling.
  911. * Every polling, It checks the residue in the dma buffer and transfer
  912. * data to the tty. Also, last_residue is updated for the next polling.
  913. */
  914. static void pl011_dma_rx_poll(struct timer_list *t)
  915. {
  916. struct uart_amba_port *uap = from_timer(uap, t, dmarx.timer);
  917. struct tty_port *port = &uap->port.state->port;
  918. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  919. struct dma_chan *rxchan = uap->dmarx.chan;
  920. unsigned long flags;
  921. unsigned int dmataken = 0;
  922. unsigned int size = 0;
  923. struct pl011_dmabuf *dbuf;
  924. int dma_count;
  925. struct dma_tx_state state;
  926. dbuf = dmarx->use_buf_b ? &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
  927. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  928. if (likely(state.residue < dmarx->last_residue)) {
  929. dmataken = dbuf->len - dmarx->last_residue;
  930. size = dmarx->last_residue - state.residue;
  931. dma_count = tty_insert_flip_string(port, dbuf->buf + dmataken,
  932. size);
  933. if (dma_count == size)
  934. dmarx->last_residue = state.residue;
  935. dmarx->last_jiffies = jiffies;
  936. }
  937. tty_flip_buffer_push(port);
  938. /*
  939. * If no data is received in poll_timeout, the driver will fall back
  940. * to interrupt mode. We will retrigger DMA at the first interrupt.
  941. */
  942. if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
  943. > uap->dmarx.poll_timeout) {
  944. uart_port_lock_irqsave(&uap->port, &flags);
  945. pl011_dma_rx_stop(uap);
  946. uap->im |= UART011_RXIM;
  947. pl011_write(uap->im, uap, REG_IMSC);
  948. uart_port_unlock_irqrestore(&uap->port, flags);
  949. uap->dmarx.running = false;
  950. dmaengine_terminate_all(rxchan);
  951. del_timer(&uap->dmarx.timer);
  952. } else {
  953. mod_timer(&uap->dmarx.timer,
  954. jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
  955. }
  956. }
  957. static void pl011_dma_startup(struct uart_amba_port *uap)
  958. {
  959. int ret;
  960. if (!uap->dma_probed)
  961. pl011_dma_probe(uap);
  962. if (!uap->dmatx.chan)
  963. return;
  964. uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
  965. if (!uap->dmatx.buf) {
  966. uap->port.fifosize = uap->fifosize;
  967. return;
  968. }
  969. uap->dmatx.len = PL011_DMA_BUFFER_SIZE;
  970. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  971. uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
  972. uap->using_tx_dma = true;
  973. if (!uap->dmarx.chan)
  974. goto skip_rx;
  975. /* Allocate and map DMA RX buffers */
  976. ret = pl011_dmabuf_init(uap->dmarx.chan, &uap->dmarx.dbuf_a,
  977. DMA_FROM_DEVICE);
  978. if (ret) {
  979. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  980. "RX buffer A", ret);
  981. goto skip_rx;
  982. }
  983. ret = pl011_dmabuf_init(uap->dmarx.chan, &uap->dmarx.dbuf_b,
  984. DMA_FROM_DEVICE);
  985. if (ret) {
  986. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  987. "RX buffer B", ret);
  988. pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_a,
  989. DMA_FROM_DEVICE);
  990. goto skip_rx;
  991. }
  992. uap->using_rx_dma = true;
  993. skip_rx:
  994. /* Turn on DMA error (RX/TX will be enabled on demand) */
  995. uap->dmacr |= UART011_DMAONERR;
  996. pl011_write(uap->dmacr, uap, REG_DMACR);
  997. /*
  998. * ST Micro variants has some specific dma burst threshold
  999. * compensation. Set this to 16 bytes, so burst will only
  1000. * be issued above/below 16 bytes.
  1001. */
  1002. if (uap->vendor->dma_threshold)
  1003. pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
  1004. uap, REG_ST_DMAWM);
  1005. if (uap->using_rx_dma) {
  1006. if (pl011_dma_rx_trigger_dma(uap))
  1007. dev_dbg(uap->port.dev,
  1008. "could not trigger initial RX DMA job, fall back to interrupt mode\n");
  1009. if (uap->dmarx.poll_rate) {
  1010. timer_setup(&uap->dmarx.timer, pl011_dma_rx_poll, 0);
  1011. mod_timer(&uap->dmarx.timer,
  1012. jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
  1013. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  1014. uap->dmarx.last_jiffies = jiffies;
  1015. }
  1016. }
  1017. }
  1018. static void pl011_dma_shutdown(struct uart_amba_port *uap)
  1019. {
  1020. if (!(uap->using_tx_dma || uap->using_rx_dma))
  1021. return;
  1022. /* Disable RX and TX DMA */
  1023. while (pl011_read(uap, REG_FR) & uap->vendor->fr_busy)
  1024. cpu_relax();
  1025. uart_port_lock_irq(&uap->port);
  1026. uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
  1027. pl011_write(uap->dmacr, uap, REG_DMACR);
  1028. uart_port_unlock_irq(&uap->port);
  1029. if (uap->using_tx_dma) {
  1030. /* In theory, this should already be done by pl011_dma_flush_buffer */
  1031. dmaengine_terminate_all(uap->dmatx.chan);
  1032. if (uap->dmatx.queued) {
  1033. dma_unmap_single(uap->dmatx.chan->device->dev,
  1034. uap->dmatx.dma, uap->dmatx.len,
  1035. DMA_TO_DEVICE);
  1036. uap->dmatx.queued = false;
  1037. }
  1038. kfree(uap->dmatx.buf);
  1039. uap->using_tx_dma = false;
  1040. }
  1041. if (uap->using_rx_dma) {
  1042. dmaengine_terminate_all(uap->dmarx.chan);
  1043. /* Clean up the RX DMA */
  1044. pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_a, DMA_FROM_DEVICE);
  1045. pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_b, DMA_FROM_DEVICE);
  1046. if (uap->dmarx.poll_rate)
  1047. del_timer_sync(&uap->dmarx.timer);
  1048. uap->using_rx_dma = false;
  1049. }
  1050. }
  1051. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  1052. {
  1053. return uap->using_rx_dma;
  1054. }
  1055. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  1056. {
  1057. return uap->using_rx_dma && uap->dmarx.running;
  1058. }
  1059. #else
  1060. /* Blank functions if the DMA engine is not available */
  1061. static inline void pl011_dma_remove(struct uart_amba_port *uap)
  1062. {
  1063. }
  1064. static inline void pl011_dma_startup(struct uart_amba_port *uap)
  1065. {
  1066. }
  1067. static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
  1068. {
  1069. }
  1070. static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  1071. {
  1072. return false;
  1073. }
  1074. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  1075. {
  1076. }
  1077. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  1078. {
  1079. return false;
  1080. }
  1081. static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
  1082. {
  1083. }
  1084. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  1085. {
  1086. }
  1087. static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  1088. {
  1089. return -EIO;
  1090. }
  1091. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  1092. {
  1093. return false;
  1094. }
  1095. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  1096. {
  1097. return false;
  1098. }
  1099. #define pl011_dma_flush_buffer NULL
  1100. #endif
  1101. static void pl011_rs485_tx_stop(struct uart_amba_port *uap)
  1102. {
  1103. /*
  1104. * To be on the safe side only time out after twice as many iterations
  1105. * as fifo size.
  1106. */
  1107. const int MAX_TX_DRAIN_ITERS = uap->port.fifosize * 2;
  1108. struct uart_port *port = &uap->port;
  1109. int i = 0;
  1110. u32 cr;
  1111. /* Wait until hardware tx queue is empty */
  1112. while (!pl011_tx_empty(port)) {
  1113. if (i > MAX_TX_DRAIN_ITERS) {
  1114. dev_warn(port->dev,
  1115. "timeout while draining hardware tx queue\n");
  1116. break;
  1117. }
  1118. udelay(uap->rs485_tx_drain_interval);
  1119. i++;
  1120. }
  1121. if (port->rs485.delay_rts_after_send)
  1122. mdelay(port->rs485.delay_rts_after_send);
  1123. cr = pl011_read(uap, REG_CR);
  1124. if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
  1125. cr &= ~UART011_CR_RTS;
  1126. else
  1127. cr |= UART011_CR_RTS;
  1128. /* Disable the transmitter and reenable the transceiver */
  1129. cr &= ~UART011_CR_TXE;
  1130. cr |= UART011_CR_RXE;
  1131. pl011_write(cr, uap, REG_CR);
  1132. uap->rs485_tx_started = false;
  1133. }
  1134. static void pl011_stop_tx(struct uart_port *port)
  1135. {
  1136. struct uart_amba_port *uap =
  1137. container_of(port, struct uart_amba_port, port);
  1138. uap->im &= ~UART011_TXIM;
  1139. pl011_write(uap->im, uap, REG_IMSC);
  1140. pl011_dma_tx_stop(uap);
  1141. if ((port->rs485.flags & SER_RS485_ENABLED) && uap->rs485_tx_started)
  1142. pl011_rs485_tx_stop(uap);
  1143. }
  1144. static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
  1145. /* Start TX with programmed I/O only (no DMA) */
  1146. static void pl011_start_tx_pio(struct uart_amba_port *uap)
  1147. {
  1148. if (pl011_tx_chars(uap, false)) {
  1149. uap->im |= UART011_TXIM;
  1150. pl011_write(uap->im, uap, REG_IMSC);
  1151. }
  1152. }
  1153. static void pl011_rs485_tx_start(struct uart_amba_port *uap)
  1154. {
  1155. struct uart_port *port = &uap->port;
  1156. u32 cr;
  1157. /* Enable transmitter */
  1158. cr = pl011_read(uap, REG_CR);
  1159. cr |= UART011_CR_TXE;
  1160. /* Disable receiver if half-duplex */
  1161. if (!(port->rs485.flags & SER_RS485_RX_DURING_TX))
  1162. cr &= ~UART011_CR_RXE;
  1163. if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
  1164. cr &= ~UART011_CR_RTS;
  1165. else
  1166. cr |= UART011_CR_RTS;
  1167. pl011_write(cr, uap, REG_CR);
  1168. if (port->rs485.delay_rts_before_send)
  1169. mdelay(port->rs485.delay_rts_before_send);
  1170. uap->rs485_tx_started = true;
  1171. }
  1172. static void pl011_start_tx(struct uart_port *port)
  1173. {
  1174. struct uart_amba_port *uap =
  1175. container_of(port, struct uart_amba_port, port);
  1176. if ((uap->port.rs485.flags & SER_RS485_ENABLED) &&
  1177. !uap->rs485_tx_started)
  1178. pl011_rs485_tx_start(uap);
  1179. if (!pl011_dma_tx_start(uap))
  1180. pl011_start_tx_pio(uap);
  1181. }
  1182. static void pl011_stop_rx(struct uart_port *port)
  1183. {
  1184. struct uart_amba_port *uap =
  1185. container_of(port, struct uart_amba_port, port);
  1186. uap->im &= ~(UART011_RXIM | UART011_RTIM | UART011_FEIM |
  1187. UART011_PEIM | UART011_BEIM | UART011_OEIM);
  1188. pl011_write(uap->im, uap, REG_IMSC);
  1189. pl011_dma_rx_stop(uap);
  1190. }
  1191. static void pl011_throttle_rx(struct uart_port *port)
  1192. {
  1193. unsigned long flags;
  1194. uart_port_lock_irqsave(port, &flags);
  1195. pl011_stop_rx(port);
  1196. uart_port_unlock_irqrestore(port, flags);
  1197. }
  1198. static void pl011_enable_ms(struct uart_port *port)
  1199. {
  1200. struct uart_amba_port *uap =
  1201. container_of(port, struct uart_amba_port, port);
  1202. uap->im |= UART011_RIMIM | UART011_CTSMIM | UART011_DCDMIM | UART011_DSRMIM;
  1203. pl011_write(uap->im, uap, REG_IMSC);
  1204. }
  1205. static void pl011_rx_chars(struct uart_amba_port *uap)
  1206. __releases(&uap->port.lock)
  1207. __acquires(&uap->port.lock)
  1208. {
  1209. pl011_fifo_to_tty(uap);
  1210. uart_port_unlock(&uap->port);
  1211. tty_flip_buffer_push(&uap->port.state->port);
  1212. /*
  1213. * If we were temporarily out of DMA mode for a while,
  1214. * attempt to switch back to DMA mode again.
  1215. */
  1216. if (pl011_dma_rx_available(uap)) {
  1217. if (pl011_dma_rx_trigger_dma(uap)) {
  1218. dev_dbg(uap->port.dev,
  1219. "could not trigger RX DMA job fall back to interrupt mode again\n");
  1220. uap->im |= UART011_RXIM;
  1221. pl011_write(uap->im, uap, REG_IMSC);
  1222. } else {
  1223. #ifdef CONFIG_DMA_ENGINE
  1224. /* Start Rx DMA poll */
  1225. if (uap->dmarx.poll_rate) {
  1226. uap->dmarx.last_jiffies = jiffies;
  1227. uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
  1228. mod_timer(&uap->dmarx.timer,
  1229. jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
  1230. }
  1231. #endif
  1232. }
  1233. }
  1234. uart_port_lock(&uap->port);
  1235. }
  1236. static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
  1237. bool from_irq)
  1238. {
  1239. if (unlikely(!from_irq) &&
  1240. pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1241. return false; /* unable to transmit character */
  1242. pl011_write(c, uap, REG_DR);
  1243. uap->port.icount.tx++;
  1244. return true;
  1245. }
  1246. /* Returns true if tx interrupts have to be (kept) enabled */
  1247. static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
  1248. {
  1249. struct tty_port *tport = &uap->port.state->port;
  1250. int count = uap->fifosize >> 1;
  1251. if (uap->port.x_char) {
  1252. if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
  1253. return true;
  1254. uap->port.x_char = 0;
  1255. --count;
  1256. }
  1257. if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(&uap->port)) {
  1258. pl011_stop_tx(&uap->port);
  1259. return false;
  1260. }
  1261. /* If we are using DMA mode, try to send some characters. */
  1262. if (pl011_dma_tx_irq(uap))
  1263. return true;
  1264. while (1) {
  1265. unsigned char c;
  1266. if (likely(from_irq) && count-- == 0)
  1267. break;
  1268. if (!kfifo_peek(&tport->xmit_fifo, &c))
  1269. break;
  1270. if (!pl011_tx_char(uap, c, from_irq))
  1271. break;
  1272. kfifo_skip(&tport->xmit_fifo);
  1273. }
  1274. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  1275. uart_write_wakeup(&uap->port);
  1276. if (kfifo_is_empty(&tport->xmit_fifo)) {
  1277. pl011_stop_tx(&uap->port);
  1278. return false;
  1279. }
  1280. return true;
  1281. }
  1282. static void pl011_modem_status(struct uart_amba_port *uap)
  1283. {
  1284. unsigned int status, delta;
  1285. status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
  1286. delta = status ^ uap->old_status;
  1287. uap->old_status = status;
  1288. if (!delta)
  1289. return;
  1290. if (delta & UART01x_FR_DCD)
  1291. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  1292. if (delta & uap->vendor->fr_dsr)
  1293. uap->port.icount.dsr++;
  1294. if (delta & uap->vendor->fr_cts)
  1295. uart_handle_cts_change(&uap->port,
  1296. status & uap->vendor->fr_cts);
  1297. wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
  1298. }
  1299. static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
  1300. {
  1301. if (!uap->vendor->cts_event_workaround)
  1302. return;
  1303. /* workaround to make sure that all bits are unlocked.. */
  1304. pl011_write(0x00, uap, REG_ICR);
  1305. /*
  1306. * WA: introduce 26ns(1 uart clk) delay before W1C;
  1307. * single apb access will incur 2 pclk(133.12Mhz) delay,
  1308. * so add 2 dummy reads
  1309. */
  1310. pl011_read(uap, REG_ICR);
  1311. pl011_read(uap, REG_ICR);
  1312. }
  1313. static irqreturn_t pl011_int(int irq, void *dev_id)
  1314. {
  1315. struct uart_amba_port *uap = dev_id;
  1316. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  1317. int handled = 0;
  1318. uart_port_lock(&uap->port);
  1319. status = pl011_read(uap, REG_RIS) & uap->im;
  1320. if (status) {
  1321. do {
  1322. check_apply_cts_event_workaround(uap);
  1323. pl011_write(status & ~(UART011_TXIS | UART011_RTIS | UART011_RXIS),
  1324. uap, REG_ICR);
  1325. if (status & (UART011_RTIS | UART011_RXIS)) {
  1326. if (pl011_dma_rx_running(uap))
  1327. pl011_dma_rx_irq(uap);
  1328. else
  1329. pl011_rx_chars(uap);
  1330. }
  1331. if (status & (UART011_DSRMIS | UART011_DCDMIS |
  1332. UART011_CTSMIS | UART011_RIMIS))
  1333. pl011_modem_status(uap);
  1334. if (status & UART011_TXIS)
  1335. pl011_tx_chars(uap, true);
  1336. if (pass_counter-- == 0)
  1337. break;
  1338. status = pl011_read(uap, REG_RIS) & uap->im;
  1339. } while (status != 0);
  1340. handled = 1;
  1341. }
  1342. uart_unlock_and_check_sysrq(&uap->port);
  1343. return IRQ_RETVAL(handled);
  1344. }
  1345. static unsigned int pl011_tx_empty(struct uart_port *port)
  1346. {
  1347. struct uart_amba_port *uap =
  1348. container_of(port, struct uart_amba_port, port);
  1349. /* Allow feature register bits to be inverted to work around errata */
  1350. unsigned int status = pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr;
  1351. return status & (uap->vendor->fr_busy | UART01x_FR_TXFF) ?
  1352. 0 : TIOCSER_TEMT;
  1353. }
  1354. static void pl011_maybe_set_bit(bool cond, unsigned int *ptr, unsigned int mask)
  1355. {
  1356. if (cond)
  1357. *ptr |= mask;
  1358. }
  1359. static unsigned int pl011_get_mctrl(struct uart_port *port)
  1360. {
  1361. struct uart_amba_port *uap =
  1362. container_of(port, struct uart_amba_port, port);
  1363. unsigned int result = 0;
  1364. unsigned int status = pl011_read(uap, REG_FR);
  1365. pl011_maybe_set_bit(status & UART01x_FR_DCD, &result, TIOCM_CAR);
  1366. pl011_maybe_set_bit(status & uap->vendor->fr_dsr, &result, TIOCM_DSR);
  1367. pl011_maybe_set_bit(status & uap->vendor->fr_cts, &result, TIOCM_CTS);
  1368. pl011_maybe_set_bit(status & uap->vendor->fr_ri, &result, TIOCM_RNG);
  1369. return result;
  1370. }
  1371. static void pl011_assign_bit(bool cond, unsigned int *ptr, unsigned int mask)
  1372. {
  1373. if (cond)
  1374. *ptr |= mask;
  1375. else
  1376. *ptr &= ~mask;
  1377. }
  1378. static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1379. {
  1380. struct uart_amba_port *uap =
  1381. container_of(port, struct uart_amba_port, port);
  1382. unsigned int cr;
  1383. cr = pl011_read(uap, REG_CR);
  1384. pl011_assign_bit(mctrl & TIOCM_RTS, &cr, UART011_CR_RTS);
  1385. pl011_assign_bit(mctrl & TIOCM_DTR, &cr, UART011_CR_DTR);
  1386. pl011_assign_bit(mctrl & TIOCM_OUT1, &cr, UART011_CR_OUT1);
  1387. pl011_assign_bit(mctrl & TIOCM_OUT2, &cr, UART011_CR_OUT2);
  1388. pl011_assign_bit(mctrl & TIOCM_LOOP, &cr, UART011_CR_LBE);
  1389. if (port->status & UPSTAT_AUTORTS) {
  1390. /* We need to disable auto-RTS if we want to turn RTS off */
  1391. pl011_assign_bit(mctrl & TIOCM_RTS, &cr, UART011_CR_RTSEN);
  1392. }
  1393. pl011_write(cr, uap, REG_CR);
  1394. }
  1395. static void pl011_break_ctl(struct uart_port *port, int break_state)
  1396. {
  1397. struct uart_amba_port *uap =
  1398. container_of(port, struct uart_amba_port, port);
  1399. unsigned long flags;
  1400. unsigned int lcr_h;
  1401. uart_port_lock_irqsave(&uap->port, &flags);
  1402. lcr_h = pl011_read(uap, REG_LCRH_TX);
  1403. if (break_state == -1)
  1404. lcr_h |= UART01x_LCRH_BRK;
  1405. else
  1406. lcr_h &= ~UART01x_LCRH_BRK;
  1407. pl011_write(lcr_h, uap, REG_LCRH_TX);
  1408. uart_port_unlock_irqrestore(&uap->port, flags);
  1409. }
  1410. #ifdef CONFIG_CONSOLE_POLL
  1411. static void pl011_quiesce_irqs(struct uart_port *port)
  1412. {
  1413. struct uart_amba_port *uap =
  1414. container_of(port, struct uart_amba_port, port);
  1415. pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
  1416. /*
  1417. * There is no way to clear TXIM as this is "ready to transmit IRQ", so
  1418. * we simply mask it. start_tx() will unmask it.
  1419. *
  1420. * Note we can race with start_tx(), and if the race happens, the
  1421. * polling user might get another interrupt just after we clear it.
  1422. * But it should be OK and can happen even w/o the race, e.g.
  1423. * controller immediately got some new data and raised the IRQ.
  1424. *
  1425. * And whoever uses polling routines assumes that it manages the device
  1426. * (including tx queue), so we're also fine with start_tx()'s caller
  1427. * side.
  1428. */
  1429. pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
  1430. REG_IMSC);
  1431. }
  1432. static int pl011_get_poll_char(struct uart_port *port)
  1433. {
  1434. struct uart_amba_port *uap =
  1435. container_of(port, struct uart_amba_port, port);
  1436. unsigned int status;
  1437. /*
  1438. * The caller might need IRQs lowered, e.g. if used with KDB NMI
  1439. * debugger.
  1440. */
  1441. pl011_quiesce_irqs(port);
  1442. status = pl011_read(uap, REG_FR);
  1443. if (status & UART01x_FR_RXFE)
  1444. return NO_POLL_CHAR;
  1445. return pl011_read(uap, REG_DR);
  1446. }
  1447. static void pl011_put_poll_char(struct uart_port *port, unsigned char ch)
  1448. {
  1449. struct uart_amba_port *uap =
  1450. container_of(port, struct uart_amba_port, port);
  1451. while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1452. cpu_relax();
  1453. pl011_write(ch, uap, REG_DR);
  1454. }
  1455. #endif /* CONFIG_CONSOLE_POLL */
  1456. static int pl011_hwinit(struct uart_port *port)
  1457. {
  1458. struct uart_amba_port *uap =
  1459. container_of(port, struct uart_amba_port, port);
  1460. int retval;
  1461. /* Optionaly enable pins to be muxed in and configured */
  1462. pinctrl_pm_select_default_state(port->dev);
  1463. /*
  1464. * Try to enable the clock producer.
  1465. */
  1466. retval = clk_prepare_enable(uap->clk);
  1467. if (retval)
  1468. return retval;
  1469. uap->port.uartclk = clk_get_rate(uap->clk);
  1470. /* Clear pending error and receive interrupts */
  1471. pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
  1472. UART011_FEIS | UART011_RTIS | UART011_RXIS,
  1473. uap, REG_ICR);
  1474. /*
  1475. * Save interrupts enable mask, and enable RX interrupts in case if
  1476. * the interrupt is used for NMI entry.
  1477. */
  1478. uap->im = pl011_read(uap, REG_IMSC);
  1479. pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
  1480. if (dev_get_platdata(uap->port.dev)) {
  1481. struct amba_pl011_data *plat;
  1482. plat = dev_get_platdata(uap->port.dev);
  1483. if (plat->init)
  1484. plat->init();
  1485. }
  1486. return 0;
  1487. }
  1488. static bool pl011_split_lcrh(const struct uart_amba_port *uap)
  1489. {
  1490. return pl011_reg_to_offset(uap, REG_LCRH_RX) !=
  1491. pl011_reg_to_offset(uap, REG_LCRH_TX);
  1492. }
  1493. static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
  1494. {
  1495. pl011_write(lcr_h, uap, REG_LCRH_RX);
  1496. if (pl011_split_lcrh(uap)) {
  1497. int i;
  1498. /*
  1499. * Wait 10 PCLKs before writing LCRH_TX register,
  1500. * to get this delay write read only register 10 times
  1501. */
  1502. for (i = 0; i < 10; ++i)
  1503. pl011_write(0xff, uap, REG_MIS);
  1504. pl011_write(lcr_h, uap, REG_LCRH_TX);
  1505. }
  1506. }
  1507. static int pl011_allocate_irq(struct uart_amba_port *uap)
  1508. {
  1509. pl011_write(uap->im, uap, REG_IMSC);
  1510. return request_irq(uap->port.irq, pl011_int, IRQF_SHARED, "uart-pl011", uap);
  1511. }
  1512. /*
  1513. * Enable interrupts, only timeouts when using DMA
  1514. * if initial RX DMA job failed, start in interrupt mode
  1515. * as well.
  1516. */
  1517. static void pl011_enable_interrupts(struct uart_amba_port *uap)
  1518. {
  1519. unsigned long flags;
  1520. unsigned int i;
  1521. uart_port_lock_irqsave(&uap->port, &flags);
  1522. /* Clear out any spuriously appearing RX interrupts */
  1523. pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
  1524. /*
  1525. * RXIS is asserted only when the RX FIFO transitions from below
  1526. * to above the trigger threshold. If the RX FIFO is already
  1527. * full to the threshold this can't happen and RXIS will now be
  1528. * stuck off. Drain the RX FIFO explicitly to fix this:
  1529. */
  1530. for (i = 0; i < uap->fifosize * 2; ++i) {
  1531. if (pl011_read(uap, REG_FR) & UART01x_FR_RXFE)
  1532. break;
  1533. pl011_read(uap, REG_DR);
  1534. }
  1535. uap->im = UART011_RTIM;
  1536. if (!pl011_dma_rx_running(uap))
  1537. uap->im |= UART011_RXIM;
  1538. pl011_write(uap->im, uap, REG_IMSC);
  1539. uart_port_unlock_irqrestore(&uap->port, flags);
  1540. }
  1541. static void pl011_unthrottle_rx(struct uart_port *port)
  1542. {
  1543. struct uart_amba_port *uap = container_of(port, struct uart_amba_port, port);
  1544. unsigned long flags;
  1545. uart_port_lock_irqsave(&uap->port, &flags);
  1546. uap->im = UART011_RTIM;
  1547. if (!pl011_dma_rx_running(uap))
  1548. uap->im |= UART011_RXIM;
  1549. pl011_write(uap->im, uap, REG_IMSC);
  1550. #ifdef CONFIG_DMA_ENGINE
  1551. if (uap->using_rx_dma) {
  1552. uap->dmacr |= UART011_RXDMAE;
  1553. pl011_write(uap->dmacr, uap, REG_DMACR);
  1554. }
  1555. #endif
  1556. uart_port_unlock_irqrestore(&uap->port, flags);
  1557. }
  1558. static int pl011_startup(struct uart_port *port)
  1559. {
  1560. struct uart_amba_port *uap =
  1561. container_of(port, struct uart_amba_port, port);
  1562. unsigned int cr;
  1563. int retval;
  1564. retval = pl011_hwinit(port);
  1565. if (retval)
  1566. goto clk_dis;
  1567. retval = pl011_allocate_irq(uap);
  1568. if (retval)
  1569. goto clk_dis;
  1570. pl011_write(uap->vendor->ifls, uap, REG_IFLS);
  1571. uart_port_lock_irq(&uap->port);
  1572. cr = pl011_read(uap, REG_CR);
  1573. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1574. cr |= UART01x_CR_UARTEN | UART011_CR_RXE;
  1575. if (!(port->rs485.flags & SER_RS485_ENABLED))
  1576. cr |= UART011_CR_TXE;
  1577. pl011_write(cr, uap, REG_CR);
  1578. uart_port_unlock_irq(&uap->port);
  1579. /*
  1580. * initialise the old status of the modem signals
  1581. */
  1582. uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
  1583. /* Startup DMA */
  1584. pl011_dma_startup(uap);
  1585. pl011_enable_interrupts(uap);
  1586. return 0;
  1587. clk_dis:
  1588. clk_disable_unprepare(uap->clk);
  1589. return retval;
  1590. }
  1591. static int sbsa_uart_startup(struct uart_port *port)
  1592. {
  1593. struct uart_amba_port *uap =
  1594. container_of(port, struct uart_amba_port, port);
  1595. int retval;
  1596. retval = pl011_hwinit(port);
  1597. if (retval)
  1598. return retval;
  1599. retval = pl011_allocate_irq(uap);
  1600. if (retval)
  1601. return retval;
  1602. /* The SBSA UART does not support any modem status lines. */
  1603. uap->old_status = 0;
  1604. pl011_enable_interrupts(uap);
  1605. return 0;
  1606. }
  1607. static void pl011_shutdown_channel(struct uart_amba_port *uap, unsigned int lcrh)
  1608. {
  1609. unsigned long val;
  1610. val = pl011_read(uap, lcrh);
  1611. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1612. pl011_write(val, uap, lcrh);
  1613. }
  1614. /*
  1615. * disable the port. It should not disable RTS and DTR.
  1616. * Also RTS and DTR state should be preserved to restore
  1617. * it during startup().
  1618. */
  1619. static void pl011_disable_uart(struct uart_amba_port *uap)
  1620. {
  1621. unsigned int cr;
  1622. uap->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
  1623. uart_port_lock_irq(&uap->port);
  1624. cr = pl011_read(uap, REG_CR);
  1625. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1626. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1627. pl011_write(cr, uap, REG_CR);
  1628. uart_port_unlock_irq(&uap->port);
  1629. /*
  1630. * disable break condition and fifos
  1631. */
  1632. pl011_shutdown_channel(uap, REG_LCRH_RX);
  1633. if (pl011_split_lcrh(uap))
  1634. pl011_shutdown_channel(uap, REG_LCRH_TX);
  1635. }
  1636. static void pl011_disable_interrupts(struct uart_amba_port *uap)
  1637. {
  1638. uart_port_lock_irq(&uap->port);
  1639. /* mask all interrupts and clear all pending ones */
  1640. uap->im = 0;
  1641. pl011_write(uap->im, uap, REG_IMSC);
  1642. pl011_write(0xffff, uap, REG_ICR);
  1643. uart_port_unlock_irq(&uap->port);
  1644. }
  1645. static void pl011_shutdown(struct uart_port *port)
  1646. {
  1647. struct uart_amba_port *uap =
  1648. container_of(port, struct uart_amba_port, port);
  1649. pl011_disable_interrupts(uap);
  1650. pl011_dma_shutdown(uap);
  1651. if ((port->rs485.flags & SER_RS485_ENABLED) && uap->rs485_tx_started)
  1652. pl011_rs485_tx_stop(uap);
  1653. free_irq(uap->port.irq, uap);
  1654. pl011_disable_uart(uap);
  1655. /*
  1656. * Shut down the clock producer
  1657. */
  1658. clk_disable_unprepare(uap->clk);
  1659. /* Optionally let pins go into sleep states */
  1660. pinctrl_pm_select_sleep_state(port->dev);
  1661. if (dev_get_platdata(uap->port.dev)) {
  1662. struct amba_pl011_data *plat;
  1663. plat = dev_get_platdata(uap->port.dev);
  1664. if (plat->exit)
  1665. plat->exit();
  1666. }
  1667. if (uap->port.ops->flush_buffer)
  1668. uap->port.ops->flush_buffer(port);
  1669. }
  1670. static void sbsa_uart_shutdown(struct uart_port *port)
  1671. {
  1672. struct uart_amba_port *uap =
  1673. container_of(port, struct uart_amba_port, port);
  1674. pl011_disable_interrupts(uap);
  1675. free_irq(uap->port.irq, uap);
  1676. if (uap->port.ops->flush_buffer)
  1677. uap->port.ops->flush_buffer(port);
  1678. }
  1679. static void
  1680. pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
  1681. {
  1682. port->read_status_mask = UART011_DR_OE | 255;
  1683. if (termios->c_iflag & INPCK)
  1684. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1685. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1686. port->read_status_mask |= UART011_DR_BE;
  1687. /*
  1688. * Characters to ignore
  1689. */
  1690. port->ignore_status_mask = 0;
  1691. if (termios->c_iflag & IGNPAR)
  1692. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1693. if (termios->c_iflag & IGNBRK) {
  1694. port->ignore_status_mask |= UART011_DR_BE;
  1695. /*
  1696. * If we're ignoring parity and break indicators,
  1697. * ignore overruns too (for real raw support).
  1698. */
  1699. if (termios->c_iflag & IGNPAR)
  1700. port->ignore_status_mask |= UART011_DR_OE;
  1701. }
  1702. /*
  1703. * Ignore all characters if CREAD is not set.
  1704. */
  1705. if ((termios->c_cflag & CREAD) == 0)
  1706. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1707. }
  1708. static void
  1709. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1710. const struct ktermios *old)
  1711. {
  1712. struct uart_amba_port *uap =
  1713. container_of(port, struct uart_amba_port, port);
  1714. unsigned int lcr_h, old_cr;
  1715. unsigned long flags;
  1716. unsigned int baud, quot, clkdiv;
  1717. unsigned int bits;
  1718. if (uap->vendor->oversampling)
  1719. clkdiv = 8;
  1720. else
  1721. clkdiv = 16;
  1722. /*
  1723. * Ask the core to calculate the divisor for us.
  1724. */
  1725. baud = uart_get_baud_rate(port, termios, old, 0,
  1726. port->uartclk / clkdiv);
  1727. #ifdef CONFIG_DMA_ENGINE
  1728. /*
  1729. * Adjust RX DMA polling rate with baud rate if not specified.
  1730. */
  1731. if (uap->dmarx.auto_poll_rate)
  1732. uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
  1733. #endif
  1734. if (baud > port->uartclk / 16)
  1735. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1736. else
  1737. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1738. switch (termios->c_cflag & CSIZE) {
  1739. case CS5:
  1740. lcr_h = UART01x_LCRH_WLEN_5;
  1741. break;
  1742. case CS6:
  1743. lcr_h = UART01x_LCRH_WLEN_6;
  1744. break;
  1745. case CS7:
  1746. lcr_h = UART01x_LCRH_WLEN_7;
  1747. break;
  1748. default: // CS8
  1749. lcr_h = UART01x_LCRH_WLEN_8;
  1750. break;
  1751. }
  1752. if (termios->c_cflag & CSTOPB)
  1753. lcr_h |= UART01x_LCRH_STP2;
  1754. if (termios->c_cflag & PARENB) {
  1755. lcr_h |= UART01x_LCRH_PEN;
  1756. if (!(termios->c_cflag & PARODD))
  1757. lcr_h |= UART01x_LCRH_EPS;
  1758. if (termios->c_cflag & CMSPAR)
  1759. lcr_h |= UART011_LCRH_SPS;
  1760. }
  1761. if (uap->fifosize > 1)
  1762. lcr_h |= UART01x_LCRH_FEN;
  1763. bits = tty_get_frame_size(termios->c_cflag);
  1764. uart_port_lock_irqsave(port, &flags);
  1765. /*
  1766. * Update the per-port timeout.
  1767. */
  1768. uart_update_timeout(port, termios->c_cflag, baud);
  1769. /*
  1770. * Calculate the approximated time it takes to transmit one character
  1771. * with the given baud rate. We use this as the poll interval when we
  1772. * wait for the tx queue to empty.
  1773. */
  1774. uap->rs485_tx_drain_interval = DIV_ROUND_UP(bits * 1000 * 1000, baud);
  1775. pl011_setup_status_masks(port, termios);
  1776. if (UART_ENABLE_MS(port, termios->c_cflag))
  1777. pl011_enable_ms(port);
  1778. if (port->rs485.flags & SER_RS485_ENABLED)
  1779. termios->c_cflag &= ~CRTSCTS;
  1780. old_cr = pl011_read(uap, REG_CR);
  1781. if (termios->c_cflag & CRTSCTS) {
  1782. if (old_cr & UART011_CR_RTS)
  1783. old_cr |= UART011_CR_RTSEN;
  1784. old_cr |= UART011_CR_CTSEN;
  1785. port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  1786. } else {
  1787. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1788. port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
  1789. }
  1790. if (uap->vendor->oversampling) {
  1791. if (baud > port->uartclk / 16)
  1792. old_cr |= ST_UART011_CR_OVSFACT;
  1793. else
  1794. old_cr &= ~ST_UART011_CR_OVSFACT;
  1795. }
  1796. /*
  1797. * Workaround for the ST Micro oversampling variants to
  1798. * increase the bitrate slightly, by lowering the divisor,
  1799. * to avoid delayed sampling of start bit at high speeds,
  1800. * else we see data corruption.
  1801. */
  1802. if (uap->vendor->oversampling) {
  1803. if (baud >= 3000000 && baud < 3250000 && quot > 1)
  1804. quot -= 1;
  1805. else if (baud > 3250000 && quot > 2)
  1806. quot -= 2;
  1807. }
  1808. /* Set baud rate */
  1809. pl011_write(quot & 0x3f, uap, REG_FBRD);
  1810. pl011_write(quot >> 6, uap, REG_IBRD);
  1811. /*
  1812. * ----------v----------v----------v----------v-----
  1813. * NOTE: REG_LCRH_TX and REG_LCRH_RX MUST BE WRITTEN AFTER
  1814. * REG_FBRD & REG_IBRD.
  1815. * ----------^----------^----------^----------^-----
  1816. */
  1817. pl011_write_lcr_h(uap, lcr_h);
  1818. /*
  1819. * Receive was disabled by pl011_disable_uart during shutdown.
  1820. * Need to reenable receive if you need to use a tty_driver
  1821. * returns from tty_find_polling_driver() after a port shutdown.
  1822. */
  1823. old_cr |= UART011_CR_RXE;
  1824. pl011_write(old_cr, uap, REG_CR);
  1825. uart_port_unlock_irqrestore(port, flags);
  1826. }
  1827. static void
  1828. sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
  1829. const struct ktermios *old)
  1830. {
  1831. struct uart_amba_port *uap =
  1832. container_of(port, struct uart_amba_port, port);
  1833. unsigned long flags;
  1834. tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
  1835. /* The SBSA UART only supports 8n1 without hardware flow control. */
  1836. termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
  1837. termios->c_cflag &= ~(CMSPAR | CRTSCTS);
  1838. termios->c_cflag |= CS8 | CLOCAL;
  1839. uart_port_lock_irqsave(port, &flags);
  1840. uart_update_timeout(port, CS8, uap->fixed_baud);
  1841. pl011_setup_status_masks(port, termios);
  1842. uart_port_unlock_irqrestore(port, flags);
  1843. }
  1844. static const char *pl011_type(struct uart_port *port)
  1845. {
  1846. struct uart_amba_port *uap =
  1847. container_of(port, struct uart_amba_port, port);
  1848. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1849. }
  1850. /*
  1851. * Configure/autoconfigure the port.
  1852. */
  1853. static void pl011_config_port(struct uart_port *port, int flags)
  1854. {
  1855. if (flags & UART_CONFIG_TYPE)
  1856. port->type = PORT_AMBA;
  1857. }
  1858. /*
  1859. * verify the new serial_struct (for TIOCSSERIAL).
  1860. */
  1861. static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
  1862. {
  1863. int ret = 0;
  1864. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1865. ret = -EINVAL;
  1866. if (ser->irq < 0 || ser->irq >= nr_irqs)
  1867. ret = -EINVAL;
  1868. if (ser->baud_base < 9600)
  1869. ret = -EINVAL;
  1870. if (port->mapbase != (unsigned long)ser->iomem_base)
  1871. ret = -EINVAL;
  1872. return ret;
  1873. }
  1874. static int pl011_rs485_config(struct uart_port *port, struct ktermios *termios,
  1875. struct serial_rs485 *rs485)
  1876. {
  1877. struct uart_amba_port *uap =
  1878. container_of(port, struct uart_amba_port, port);
  1879. if (port->rs485.flags & SER_RS485_ENABLED)
  1880. pl011_rs485_tx_stop(uap);
  1881. /* Make sure auto RTS is disabled */
  1882. if (rs485->flags & SER_RS485_ENABLED) {
  1883. u32 cr = pl011_read(uap, REG_CR);
  1884. cr &= ~UART011_CR_RTSEN;
  1885. pl011_write(cr, uap, REG_CR);
  1886. port->status &= ~UPSTAT_AUTORTS;
  1887. }
  1888. return 0;
  1889. }
  1890. static const struct uart_ops amba_pl011_pops = {
  1891. .tx_empty = pl011_tx_empty,
  1892. .set_mctrl = pl011_set_mctrl,
  1893. .get_mctrl = pl011_get_mctrl,
  1894. .stop_tx = pl011_stop_tx,
  1895. .start_tx = pl011_start_tx,
  1896. .stop_rx = pl011_stop_rx,
  1897. .throttle = pl011_throttle_rx,
  1898. .unthrottle = pl011_unthrottle_rx,
  1899. .enable_ms = pl011_enable_ms,
  1900. .break_ctl = pl011_break_ctl,
  1901. .startup = pl011_startup,
  1902. .shutdown = pl011_shutdown,
  1903. .flush_buffer = pl011_dma_flush_buffer,
  1904. .set_termios = pl011_set_termios,
  1905. .type = pl011_type,
  1906. .config_port = pl011_config_port,
  1907. .verify_port = pl011_verify_port,
  1908. #ifdef CONFIG_CONSOLE_POLL
  1909. .poll_init = pl011_hwinit,
  1910. .poll_get_char = pl011_get_poll_char,
  1911. .poll_put_char = pl011_put_poll_char,
  1912. #endif
  1913. };
  1914. static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1915. {
  1916. }
  1917. static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
  1918. {
  1919. return 0;
  1920. }
  1921. static const struct uart_ops sbsa_uart_pops = {
  1922. .tx_empty = pl011_tx_empty,
  1923. .set_mctrl = sbsa_uart_set_mctrl,
  1924. .get_mctrl = sbsa_uart_get_mctrl,
  1925. .stop_tx = pl011_stop_tx,
  1926. .start_tx = pl011_start_tx,
  1927. .stop_rx = pl011_stop_rx,
  1928. .startup = sbsa_uart_startup,
  1929. .shutdown = sbsa_uart_shutdown,
  1930. .set_termios = sbsa_uart_set_termios,
  1931. .type = pl011_type,
  1932. .config_port = pl011_config_port,
  1933. .verify_port = pl011_verify_port,
  1934. #ifdef CONFIG_CONSOLE_POLL
  1935. .poll_init = pl011_hwinit,
  1936. .poll_get_char = pl011_get_poll_char,
  1937. .poll_put_char = pl011_put_poll_char,
  1938. #endif
  1939. };
  1940. static struct uart_amba_port *amba_ports[UART_NR];
  1941. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1942. static void pl011_console_putchar(struct uart_port *port, unsigned char ch)
  1943. {
  1944. struct uart_amba_port *uap =
  1945. container_of(port, struct uart_amba_port, port);
  1946. while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
  1947. cpu_relax();
  1948. pl011_write(ch, uap, REG_DR);
  1949. }
  1950. static void
  1951. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1952. {
  1953. struct uart_amba_port *uap = amba_ports[co->index];
  1954. unsigned int old_cr = 0, new_cr;
  1955. unsigned long flags;
  1956. int locked = 1;
  1957. clk_enable(uap->clk);
  1958. if (oops_in_progress)
  1959. locked = uart_port_trylock_irqsave(&uap->port, &flags);
  1960. else
  1961. uart_port_lock_irqsave(&uap->port, &flags);
  1962. /*
  1963. * First save the CR then disable the interrupts
  1964. */
  1965. if (!uap->vendor->always_enabled) {
  1966. old_cr = pl011_read(uap, REG_CR);
  1967. new_cr = old_cr & ~UART011_CR_CTSEN;
  1968. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1969. pl011_write(new_cr, uap, REG_CR);
  1970. }
  1971. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1972. /*
  1973. * Finally, wait for transmitter to become empty and restore the
  1974. * TCR. Allow feature register bits to be inverted to work around
  1975. * errata.
  1976. */
  1977. while ((pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr)
  1978. & uap->vendor->fr_busy)
  1979. cpu_relax();
  1980. if (!uap->vendor->always_enabled)
  1981. pl011_write(old_cr, uap, REG_CR);
  1982. if (locked)
  1983. uart_port_unlock_irqrestore(&uap->port, flags);
  1984. clk_disable(uap->clk);
  1985. }
  1986. static void pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1987. int *parity, int *bits)
  1988. {
  1989. unsigned int lcr_h, ibrd, fbrd;
  1990. if (!(pl011_read(uap, REG_CR) & UART01x_CR_UARTEN))
  1991. return;
  1992. lcr_h = pl011_read(uap, REG_LCRH_TX);
  1993. *parity = 'n';
  1994. if (lcr_h & UART01x_LCRH_PEN) {
  1995. if (lcr_h & UART01x_LCRH_EPS)
  1996. *parity = 'e';
  1997. else
  1998. *parity = 'o';
  1999. }
  2000. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  2001. *bits = 7;
  2002. else
  2003. *bits = 8;
  2004. ibrd = pl011_read(uap, REG_IBRD);
  2005. fbrd = pl011_read(uap, REG_FBRD);
  2006. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  2007. if (uap->vendor->oversampling &&
  2008. (pl011_read(uap, REG_CR) & ST_UART011_CR_OVSFACT))
  2009. *baud *= 2;
  2010. }
  2011. static int pl011_console_setup(struct console *co, char *options)
  2012. {
  2013. struct uart_amba_port *uap;
  2014. int baud = 38400;
  2015. int bits = 8;
  2016. int parity = 'n';
  2017. int flow = 'n';
  2018. int ret;
  2019. /*
  2020. * Check whether an invalid uart number has been specified, and
  2021. * if so, search for the first available port that does have
  2022. * console support.
  2023. */
  2024. if (co->index >= UART_NR)
  2025. co->index = 0;
  2026. uap = amba_ports[co->index];
  2027. if (!uap)
  2028. return -ENODEV;
  2029. /* Allow pins to be muxed in and configured */
  2030. pinctrl_pm_select_default_state(uap->port.dev);
  2031. ret = clk_prepare(uap->clk);
  2032. if (ret)
  2033. return ret;
  2034. if (dev_get_platdata(uap->port.dev)) {
  2035. struct amba_pl011_data *plat;
  2036. plat = dev_get_platdata(uap->port.dev);
  2037. if (plat->init)
  2038. plat->init();
  2039. }
  2040. uap->port.uartclk = clk_get_rate(uap->clk);
  2041. if (uap->vendor->fixed_options) {
  2042. baud = uap->fixed_baud;
  2043. } else {
  2044. if (options)
  2045. uart_parse_options(options,
  2046. &baud, &parity, &bits, &flow);
  2047. else
  2048. pl011_console_get_options(uap, &baud, &parity, &bits);
  2049. }
  2050. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  2051. }
  2052. /**
  2053. * pl011_console_match - non-standard console matching
  2054. * @co: registering console
  2055. * @name: name from console command line
  2056. * @idx: index from console command line
  2057. * @options: ptr to option string from console command line
  2058. *
  2059. * Only attempts to match console command lines of the form:
  2060. * console=pl011,mmio|mmio32,<addr>[,<options>]
  2061. * console=pl011,0x<addr>[,<options>]
  2062. * This form is used to register an initial earlycon boot console and
  2063. * replace it with the amba_console at pl011 driver init.
  2064. *
  2065. * Performs console setup for a match (as required by interface)
  2066. * If no <options> are specified, then assume the h/w is already setup.
  2067. *
  2068. * Returns 0 if console matches; otherwise non-zero to use default matching
  2069. */
  2070. static int pl011_console_match(struct console *co, char *name, int idx,
  2071. char *options)
  2072. {
  2073. unsigned char iotype;
  2074. resource_size_t addr;
  2075. int i;
  2076. /*
  2077. * Systems affected by the Qualcomm Technologies QDF2400 E44 erratum
  2078. * have a distinct console name, so make sure we check for that.
  2079. * The actual implementation of the erratum occurs in the probe
  2080. * function.
  2081. */
  2082. if ((strcmp(name, "qdf2400_e44") != 0) && (strcmp(name, "pl011") != 0))
  2083. return -ENODEV;
  2084. if (uart_parse_earlycon(options, &iotype, &addr, &options))
  2085. return -ENODEV;
  2086. if (iotype != UPIO_MEM && iotype != UPIO_MEM32)
  2087. return -ENODEV;
  2088. /* try to match the port specified on the command line */
  2089. for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
  2090. struct uart_port *port;
  2091. if (!amba_ports[i])
  2092. continue;
  2093. port = &amba_ports[i]->port;
  2094. if (port->mapbase != addr)
  2095. continue;
  2096. co->index = i;
  2097. uart_port_set_cons(port, co);
  2098. return pl011_console_setup(co, options);
  2099. }
  2100. return -ENODEV;
  2101. }
  2102. static struct uart_driver amba_reg;
  2103. static struct console amba_console = {
  2104. .name = "ttyAMA",
  2105. .write = pl011_console_write,
  2106. .device = uart_console_device,
  2107. .setup = pl011_console_setup,
  2108. .match = pl011_console_match,
  2109. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  2110. .index = -1,
  2111. .data = &amba_reg,
  2112. };
  2113. #define AMBA_CONSOLE (&amba_console)
  2114. static void qdf2400_e44_putc(struct uart_port *port, unsigned char c)
  2115. {
  2116. while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
  2117. cpu_relax();
  2118. writel(c, port->membase + UART01x_DR);
  2119. while (!(readl(port->membase + UART01x_FR) & UART011_FR_TXFE))
  2120. cpu_relax();
  2121. }
  2122. static void qdf2400_e44_early_write(struct console *con, const char *s, unsigned int n)
  2123. {
  2124. struct earlycon_device *dev = con->data;
  2125. uart_console_write(&dev->port, s, n, qdf2400_e44_putc);
  2126. }
  2127. static void pl011_putc(struct uart_port *port, unsigned char c)
  2128. {
  2129. while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
  2130. cpu_relax();
  2131. if (port->iotype == UPIO_MEM32)
  2132. writel(c, port->membase + UART01x_DR);
  2133. else
  2134. writeb(c, port->membase + UART01x_DR);
  2135. while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
  2136. cpu_relax();
  2137. }
  2138. static void pl011_early_write(struct console *con, const char *s, unsigned int n)
  2139. {
  2140. struct earlycon_device *dev = con->data;
  2141. uart_console_write(&dev->port, s, n, pl011_putc);
  2142. }
  2143. #ifdef CONFIG_CONSOLE_POLL
  2144. static int pl011_getc(struct uart_port *port)
  2145. {
  2146. if (readl(port->membase + UART01x_FR) & UART01x_FR_RXFE)
  2147. return NO_POLL_CHAR;
  2148. if (port->iotype == UPIO_MEM32)
  2149. return readl(port->membase + UART01x_DR);
  2150. else
  2151. return readb(port->membase + UART01x_DR);
  2152. }
  2153. static int pl011_early_read(struct console *con, char *s, unsigned int n)
  2154. {
  2155. struct earlycon_device *dev = con->data;
  2156. int ch, num_read = 0;
  2157. while (num_read < n) {
  2158. ch = pl011_getc(&dev->port);
  2159. if (ch == NO_POLL_CHAR)
  2160. break;
  2161. s[num_read++] = ch;
  2162. }
  2163. return num_read;
  2164. }
  2165. #else
  2166. #define pl011_early_read NULL
  2167. #endif
  2168. /*
  2169. * On non-ACPI systems, earlycon is enabled by specifying
  2170. * "earlycon=pl011,<address>" on the kernel command line.
  2171. *
  2172. * On ACPI ARM64 systems, an "early" console is enabled via the SPCR table,
  2173. * by specifying only "earlycon" on the command line. Because it requires
  2174. * SPCR, the console starts after ACPI is parsed, which is later than a
  2175. * traditional early console.
  2176. *
  2177. * To get the traditional early console that starts before ACPI is parsed,
  2178. * specify the full "earlycon=pl011,<address>" option.
  2179. */
  2180. static int __init pl011_early_console_setup(struct earlycon_device *device,
  2181. const char *opt)
  2182. {
  2183. if (!device->port.membase)
  2184. return -ENODEV;
  2185. device->con->write = pl011_early_write;
  2186. device->con->read = pl011_early_read;
  2187. return 0;
  2188. }
  2189. OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
  2190. OF_EARLYCON_DECLARE(pl011, "arm,sbsa-uart", pl011_early_console_setup);
  2191. /*
  2192. * On Qualcomm Datacenter Technologies QDF2400 SOCs affected by
  2193. * Erratum 44, traditional earlycon can be enabled by specifying
  2194. * "earlycon=qdf2400_e44,<address>". Any options are ignored.
  2195. *
  2196. * Alternatively, you can just specify "earlycon", and the early console
  2197. * will be enabled with the information from the SPCR table. In this
  2198. * case, the SPCR code will detect the need for the E44 work-around,
  2199. * and set the console name to "qdf2400_e44".
  2200. */
  2201. static int __init
  2202. qdf2400_e44_early_console_setup(struct earlycon_device *device,
  2203. const char *opt)
  2204. {
  2205. if (!device->port.membase)
  2206. return -ENODEV;
  2207. device->con->write = qdf2400_e44_early_write;
  2208. return 0;
  2209. }
  2210. EARLYCON_DECLARE(qdf2400_e44, qdf2400_e44_early_console_setup);
  2211. #else
  2212. #define AMBA_CONSOLE NULL
  2213. #endif
  2214. static struct uart_driver amba_reg = {
  2215. .owner = THIS_MODULE,
  2216. .driver_name = "ttyAMA",
  2217. .dev_name = "ttyAMA",
  2218. .major = SERIAL_AMBA_MAJOR,
  2219. .minor = SERIAL_AMBA_MINOR,
  2220. .nr = UART_NR,
  2221. .cons = AMBA_CONSOLE,
  2222. };
  2223. static int pl011_probe_dt_alias(int index, struct device *dev)
  2224. {
  2225. struct device_node *np;
  2226. static bool seen_dev_with_alias;
  2227. static bool seen_dev_without_alias;
  2228. int ret = index;
  2229. if (!IS_ENABLED(CONFIG_OF))
  2230. return ret;
  2231. np = dev->of_node;
  2232. if (!np)
  2233. return ret;
  2234. ret = of_alias_get_id(np, "serial");
  2235. if (ret < 0) {
  2236. seen_dev_without_alias = true;
  2237. ret = index;
  2238. } else {
  2239. seen_dev_with_alias = true;
  2240. if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret]) {
  2241. dev_warn(dev, "requested serial port %d not available.\n", ret);
  2242. ret = index;
  2243. }
  2244. }
  2245. if (seen_dev_with_alias && seen_dev_without_alias)
  2246. dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
  2247. return ret;
  2248. }
  2249. /* unregisters the driver also if no more ports are left */
  2250. static void pl011_unregister_port(struct uart_amba_port *uap)
  2251. {
  2252. int i;
  2253. bool busy = false;
  2254. for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
  2255. if (amba_ports[i] == uap)
  2256. amba_ports[i] = NULL;
  2257. else if (amba_ports[i])
  2258. busy = true;
  2259. }
  2260. pl011_dma_remove(uap);
  2261. if (!busy)
  2262. uart_unregister_driver(&amba_reg);
  2263. }
  2264. static int pl011_find_free_port(void)
  2265. {
  2266. int i;
  2267. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  2268. if (!amba_ports[i])
  2269. return i;
  2270. return -EBUSY;
  2271. }
  2272. static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
  2273. struct resource *mmiobase, int index)
  2274. {
  2275. void __iomem *base;
  2276. int ret;
  2277. base = devm_ioremap_resource(dev, mmiobase);
  2278. if (IS_ERR(base))
  2279. return PTR_ERR(base);
  2280. index = pl011_probe_dt_alias(index, dev);
  2281. uap->port.dev = dev;
  2282. uap->port.mapbase = mmiobase->start;
  2283. uap->port.membase = base;
  2284. uap->port.fifosize = uap->fifosize;
  2285. uap->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_AMBA_PL011_CONSOLE);
  2286. uap->port.flags = UPF_BOOT_AUTOCONF;
  2287. uap->port.line = index;
  2288. ret = uart_get_rs485_mode(&uap->port);
  2289. if (ret)
  2290. return ret;
  2291. amba_ports[index] = uap;
  2292. return 0;
  2293. }
  2294. static int pl011_register_port(struct uart_amba_port *uap)
  2295. {
  2296. int ret, i;
  2297. /* Ensure interrupts from this UART are masked and cleared */
  2298. pl011_write(0, uap, REG_IMSC);
  2299. pl011_write(0xffff, uap, REG_ICR);
  2300. if (!amba_reg.state) {
  2301. ret = uart_register_driver(&amba_reg);
  2302. if (ret < 0) {
  2303. dev_err(uap->port.dev,
  2304. "Failed to register AMBA-PL011 driver\n");
  2305. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  2306. if (amba_ports[i] == uap)
  2307. amba_ports[i] = NULL;
  2308. return ret;
  2309. }
  2310. }
  2311. ret = uart_add_one_port(&amba_reg, &uap->port);
  2312. if (ret)
  2313. pl011_unregister_port(uap);
  2314. return ret;
  2315. }
  2316. static const struct serial_rs485 pl011_rs485_supported = {
  2317. .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
  2318. SER_RS485_RX_DURING_TX,
  2319. .delay_rts_before_send = 1,
  2320. .delay_rts_after_send = 1,
  2321. };
  2322. static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
  2323. {
  2324. struct uart_amba_port *uap;
  2325. struct vendor_data *vendor = id->data;
  2326. int portnr, ret;
  2327. u32 val;
  2328. portnr = pl011_find_free_port();
  2329. if (portnr < 0)
  2330. return portnr;
  2331. uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
  2332. GFP_KERNEL);
  2333. if (!uap)
  2334. return -ENOMEM;
  2335. uap->clk = devm_clk_get(&dev->dev, NULL);
  2336. if (IS_ERR(uap->clk))
  2337. return PTR_ERR(uap->clk);
  2338. uap->reg_offset = vendor->reg_offset;
  2339. uap->vendor = vendor;
  2340. uap->fifosize = vendor->get_fifosize(dev);
  2341. uap->port.iotype = vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
  2342. uap->port.irq = dev->irq[0];
  2343. uap->port.ops = &amba_pl011_pops;
  2344. uap->port.rs485_config = pl011_rs485_config;
  2345. uap->port.rs485_supported = pl011_rs485_supported;
  2346. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  2347. if (device_property_read_u32(&dev->dev, "reg-io-width", &val) == 0) {
  2348. switch (val) {
  2349. case 1:
  2350. uap->port.iotype = UPIO_MEM;
  2351. break;
  2352. case 4:
  2353. uap->port.iotype = UPIO_MEM32;
  2354. break;
  2355. default:
  2356. dev_warn(&dev->dev, "unsupported reg-io-width (%d)\n",
  2357. val);
  2358. return -EINVAL;
  2359. }
  2360. }
  2361. ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
  2362. if (ret)
  2363. return ret;
  2364. amba_set_drvdata(dev, uap);
  2365. return pl011_register_port(uap);
  2366. }
  2367. static void pl011_remove(struct amba_device *dev)
  2368. {
  2369. struct uart_amba_port *uap = amba_get_drvdata(dev);
  2370. uart_remove_one_port(&amba_reg, &uap->port);
  2371. pl011_unregister_port(uap);
  2372. }
  2373. #ifdef CONFIG_PM_SLEEP
  2374. static int pl011_suspend(struct device *dev)
  2375. {
  2376. struct uart_amba_port *uap = dev_get_drvdata(dev);
  2377. if (!uap)
  2378. return -EINVAL;
  2379. return uart_suspend_port(&amba_reg, &uap->port);
  2380. }
  2381. static int pl011_resume(struct device *dev)
  2382. {
  2383. struct uart_amba_port *uap = dev_get_drvdata(dev);
  2384. if (!uap)
  2385. return -EINVAL;
  2386. return uart_resume_port(&amba_reg, &uap->port);
  2387. }
  2388. #endif
  2389. static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
  2390. #ifdef CONFIG_ACPI_SPCR_TABLE
  2391. static void qpdf2400_erratum44_workaround(struct device *dev,
  2392. struct uart_amba_port *uap)
  2393. {
  2394. if (!qdf2400_e44_present)
  2395. return;
  2396. dev_info(dev, "working around QDF2400 SoC erratum 44\n");
  2397. uap->vendor = &vendor_qdt_qdf2400_e44;
  2398. }
  2399. #else
  2400. static void qpdf2400_erratum44_workaround(struct device *dev,
  2401. struct uart_amba_port *uap)
  2402. { /* empty */ }
  2403. #endif
  2404. static int sbsa_uart_probe(struct platform_device *pdev)
  2405. {
  2406. struct uart_amba_port *uap;
  2407. struct resource *r;
  2408. int portnr, ret;
  2409. int baudrate;
  2410. /*
  2411. * Check the mandatory baud rate parameter in the DT node early
  2412. * so that we can easily exit with the error.
  2413. */
  2414. if (pdev->dev.of_node) {
  2415. struct device_node *np = pdev->dev.of_node;
  2416. ret = of_property_read_u32(np, "current-speed", &baudrate);
  2417. if (ret)
  2418. return ret;
  2419. } else {
  2420. baudrate = 115200;
  2421. }
  2422. portnr = pl011_find_free_port();
  2423. if (portnr < 0)
  2424. return portnr;
  2425. uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
  2426. GFP_KERNEL);
  2427. if (!uap)
  2428. return -ENOMEM;
  2429. ret = platform_get_irq(pdev, 0);
  2430. if (ret < 0)
  2431. return ret;
  2432. uap->port.irq = ret;
  2433. uap->vendor = &vendor_sbsa;
  2434. qpdf2400_erratum44_workaround(&pdev->dev, uap);
  2435. uap->reg_offset = uap->vendor->reg_offset;
  2436. uap->fifosize = 32;
  2437. uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
  2438. uap->port.ops = &sbsa_uart_pops;
  2439. uap->fixed_baud = baudrate;
  2440. snprintf(uap->type, sizeof(uap->type), "SBSA");
  2441. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2442. ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
  2443. if (ret)
  2444. return ret;
  2445. platform_set_drvdata(pdev, uap);
  2446. return pl011_register_port(uap);
  2447. }
  2448. static void sbsa_uart_remove(struct platform_device *pdev)
  2449. {
  2450. struct uart_amba_port *uap = platform_get_drvdata(pdev);
  2451. uart_remove_one_port(&amba_reg, &uap->port);
  2452. pl011_unregister_port(uap);
  2453. }
  2454. static const struct of_device_id sbsa_uart_of_match[] = {
  2455. { .compatible = "arm,sbsa-uart", },
  2456. {},
  2457. };
  2458. MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
  2459. static const struct acpi_device_id __maybe_unused sbsa_uart_acpi_match[] = {
  2460. { "ARMH0011", 0 },
  2461. { "ARMHB000", 0 },
  2462. {},
  2463. };
  2464. MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
  2465. static struct platform_driver arm_sbsa_uart_platform_driver = {
  2466. .probe = sbsa_uart_probe,
  2467. .remove_new = sbsa_uart_remove,
  2468. .driver = {
  2469. .name = "sbsa-uart",
  2470. .pm = &pl011_dev_pm_ops,
  2471. .of_match_table = of_match_ptr(sbsa_uart_of_match),
  2472. .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
  2473. .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_AMBA_PL011),
  2474. },
  2475. };
  2476. static const struct amba_id pl011_ids[] = {
  2477. {
  2478. .id = 0x00041011,
  2479. .mask = 0x000fffff,
  2480. .data = &vendor_arm,
  2481. },
  2482. {
  2483. .id = 0x00380802,
  2484. .mask = 0x00ffffff,
  2485. .data = &vendor_st,
  2486. },
  2487. { 0, 0 },
  2488. };
  2489. MODULE_DEVICE_TABLE(amba, pl011_ids);
  2490. static struct amba_driver pl011_driver = {
  2491. .drv = {
  2492. .name = "uart-pl011",
  2493. .pm = &pl011_dev_pm_ops,
  2494. .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_AMBA_PL011),
  2495. },
  2496. .id_table = pl011_ids,
  2497. .probe = pl011_probe,
  2498. .remove = pl011_remove,
  2499. };
  2500. static int __init pl011_init(void)
  2501. {
  2502. pr_info("Serial: AMBA PL011 UART driver\n");
  2503. if (platform_driver_register(&arm_sbsa_uart_platform_driver))
  2504. pr_warn("could not register SBSA UART platform driver\n");
  2505. return amba_driver_register(&pl011_driver);
  2506. }
  2507. static void __exit pl011_exit(void)
  2508. {
  2509. platform_driver_unregister(&arm_sbsa_uart_platform_driver);
  2510. amba_driver_unregister(&pl011_driver);
  2511. }
  2512. /*
  2513. * While this can be a module, if builtin it's most likely the console
  2514. * So let's leave module_exit but move module_init to an earlier place
  2515. */
  2516. arch_initcall(pl011_init);
  2517. module_exit(pl011_exit);
  2518. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  2519. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  2520. MODULE_LICENSE("GPL");