amba-pl011.c 71 KB

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