amba-pl011.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. /*
  2. * Driver for AMBA serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. *
  6. * Copyright 1999 ARM Limited
  7. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  8. * Copyright (C) 2010 ST-Ericsson SA
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * This is a generic driver for ARM AMBA-type serial ports. They
  25. * have a lot of 16550-like features, but are not register compatible.
  26. * Note that although they do have CTS, DCD and DSR inputs, they do
  27. * not have an RI input, nor do they have DTR or RTS outputs. If
  28. * required, these have to be supplied via some other means (eg, GPIO)
  29. * and hooked into this driver.
  30. */
  31. #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  32. #define SUPPORT_SYSRQ
  33. #endif
  34. #include <linux/module.h>
  35. #include <linux/ioport.h>
  36. #include <linux/init.h>
  37. #include <linux/console.h>
  38. #include <linux/sysrq.h>
  39. #include <linux/device.h>
  40. #include <linux/tty.h>
  41. #include <linux/tty_flip.h>
  42. #include <linux/serial_core.h>
  43. #include <linux/serial.h>
  44. #include <linux/amba/bus.h>
  45. #include <linux/amba/serial.h>
  46. #include <linux/clk.h>
  47. #include <linux/slab.h>
  48. #include <linux/dmaengine.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/scatterlist.h>
  51. #include <linux/delay.h>
  52. #include <linux/types.h>
  53. #include <linux/of.h>
  54. #include <linux/of_device.h>
  55. #include <linux/pinctrl/consumer.h>
  56. #include <linux/sizes.h>
  57. #include <linux/io.h>
  58. #define UART_NR 14
  59. #define SERIAL_AMBA_MAJOR 204
  60. #define SERIAL_AMBA_MINOR 64
  61. #define SERIAL_AMBA_NR UART_NR
  62. #define AMBA_ISR_PASS_LIMIT 256
  63. #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
  64. #define UART_DUMMY_DR_RX (1 << 16)
  65. /* There is by now at least one vendor with differing details, so handle it */
  66. struct vendor_data {
  67. unsigned int ifls;
  68. unsigned int fifosize;
  69. unsigned int lcrh_tx;
  70. unsigned int lcrh_rx;
  71. bool oversampling;
  72. bool dma_threshold;
  73. bool cts_event_workaround;
  74. };
  75. static struct vendor_data vendor_arm = {
  76. .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
  77. .fifosize = 16,
  78. .lcrh_tx = UART011_LCRH,
  79. .lcrh_rx = UART011_LCRH,
  80. .oversampling = false,
  81. .dma_threshold = false,
  82. .cts_event_workaround = false,
  83. };
  84. static struct vendor_data vendor_st = {
  85. .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
  86. .fifosize = 64,
  87. .lcrh_tx = ST_UART011_LCRH_TX,
  88. .lcrh_rx = ST_UART011_LCRH_RX,
  89. .oversampling = true,
  90. .dma_threshold = true,
  91. .cts_event_workaround = true,
  92. };
  93. static struct uart_amba_port *amba_ports[UART_NR];
  94. /* Deals with DMA transactions */
  95. struct pl011_sgbuf {
  96. struct scatterlist sg;
  97. char *buf;
  98. };
  99. struct pl011_dmarx_data {
  100. struct dma_chan *chan;
  101. struct completion complete;
  102. bool use_buf_b;
  103. struct pl011_sgbuf sgbuf_a;
  104. struct pl011_sgbuf sgbuf_b;
  105. dma_cookie_t cookie;
  106. bool running;
  107. };
  108. struct pl011_dmatx_data {
  109. struct dma_chan *chan;
  110. struct scatterlist sg;
  111. char *buf;
  112. bool queued;
  113. };
  114. /*
  115. * We wrap our port structure around the generic uart_port.
  116. */
  117. struct uart_amba_port {
  118. struct uart_port port;
  119. struct clk *clk;
  120. /* Two optional pin states - default & sleep */
  121. struct pinctrl *pinctrl;
  122. struct pinctrl_state *pins_default;
  123. struct pinctrl_state *pins_sleep;
  124. const struct vendor_data *vendor;
  125. unsigned int dmacr; /* dma control reg */
  126. unsigned int im; /* interrupt mask */
  127. unsigned int old_status;
  128. unsigned int fifosize; /* vendor-specific */
  129. unsigned int lcrh_tx; /* vendor-specific */
  130. unsigned int lcrh_rx; /* vendor-specific */
  131. unsigned int old_cr; /* state during shutdown */
  132. bool autorts;
  133. char type[12];
  134. #ifdef CONFIG_DMA_ENGINE
  135. /* DMA stuff */
  136. bool using_tx_dma;
  137. bool using_rx_dma;
  138. struct pl011_dmarx_data dmarx;
  139. struct pl011_dmatx_data dmatx;
  140. #endif
  141. };
  142. /*
  143. * Reads up to 256 characters from the FIFO or until it's empty and
  144. * inserts them into the TTY layer. Returns the number of characters
  145. * read from the FIFO.
  146. */
  147. static int pl011_fifo_to_tty(struct uart_amba_port *uap)
  148. {
  149. u16 status, ch;
  150. unsigned int flag, max_count = 256;
  151. int fifotaken = 0;
  152. while (max_count--) {
  153. status = readw(uap->port.membase + UART01x_FR);
  154. if (status & UART01x_FR_RXFE)
  155. break;
  156. /* Take chars from the FIFO and update status */
  157. ch = readw(uap->port.membase + UART01x_DR) |
  158. UART_DUMMY_DR_RX;
  159. flag = TTY_NORMAL;
  160. uap->port.icount.rx++;
  161. fifotaken++;
  162. if (unlikely(ch & UART_DR_ERROR)) {
  163. if (ch & UART011_DR_BE) {
  164. ch &= ~(UART011_DR_FE | UART011_DR_PE);
  165. uap->port.icount.brk++;
  166. if (uart_handle_break(&uap->port))
  167. continue;
  168. } else if (ch & UART011_DR_PE)
  169. uap->port.icount.parity++;
  170. else if (ch & UART011_DR_FE)
  171. uap->port.icount.frame++;
  172. if (ch & UART011_DR_OE)
  173. uap->port.icount.overrun++;
  174. ch &= uap->port.read_status_mask;
  175. if (ch & UART011_DR_BE)
  176. flag = TTY_BREAK;
  177. else if (ch & UART011_DR_PE)
  178. flag = TTY_PARITY;
  179. else if (ch & UART011_DR_FE)
  180. flag = TTY_FRAME;
  181. }
  182. if (uart_handle_sysrq_char(&uap->port, ch & 255))
  183. continue;
  184. uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
  185. }
  186. return fifotaken;
  187. }
  188. /*
  189. * All the DMA operation mode stuff goes inside this ifdef.
  190. * This assumes that you have a generic DMA device interface,
  191. * no custom DMA interfaces are supported.
  192. */
  193. #ifdef CONFIG_DMA_ENGINE
  194. #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
  195. static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
  196. enum dma_data_direction dir)
  197. {
  198. sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
  199. if (!sg->buf)
  200. return -ENOMEM;
  201. sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
  202. if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
  203. kfree(sg->buf);
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
  209. enum dma_data_direction dir)
  210. {
  211. if (sg->buf) {
  212. dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
  213. kfree(sg->buf);
  214. }
  215. }
  216. static void pl011_dma_probe_initcall(struct device *dev, struct uart_amba_port *uap)
  217. {
  218. /* DMA is the sole user of the platform data right now */
  219. struct amba_pl011_data *plat = uap->port.dev->platform_data;
  220. struct dma_slave_config tx_conf = {
  221. .dst_addr = uap->port.mapbase + UART01x_DR,
  222. .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  223. .direction = DMA_MEM_TO_DEV,
  224. .dst_maxburst = uap->fifosize >> 1,
  225. .device_fc = false,
  226. };
  227. struct dma_chan *chan;
  228. dma_cap_mask_t mask;
  229. chan = dma_request_slave_channel(dev, "tx");
  230. if (!chan) {
  231. /* We need platform data */
  232. if (!plat || !plat->dma_filter) {
  233. dev_info(uap->port.dev, "no DMA platform data\n");
  234. return;
  235. }
  236. /* Try to acquire a generic DMA engine slave TX channel */
  237. dma_cap_zero(mask);
  238. dma_cap_set(DMA_SLAVE, mask);
  239. chan = dma_request_channel(mask, plat->dma_filter,
  240. plat->dma_tx_param);
  241. if (!chan) {
  242. dev_err(uap->port.dev, "no TX DMA channel!\n");
  243. return;
  244. }
  245. }
  246. dmaengine_slave_config(chan, &tx_conf);
  247. uap->dmatx.chan = chan;
  248. dev_info(uap->port.dev, "DMA channel TX %s\n",
  249. dma_chan_name(uap->dmatx.chan));
  250. /* Optionally make use of an RX channel as well */
  251. chan = dma_request_slave_channel(dev, "rx");
  252. if (!chan && plat->dma_rx_param) {
  253. chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
  254. if (!chan) {
  255. dev_err(uap->port.dev, "no RX DMA channel!\n");
  256. return;
  257. }
  258. }
  259. if (chan) {
  260. struct dma_slave_config rx_conf = {
  261. .src_addr = uap->port.mapbase + UART01x_DR,
  262. .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  263. .direction = DMA_DEV_TO_MEM,
  264. .src_maxburst = uap->fifosize >> 1,
  265. .device_fc = false,
  266. };
  267. dmaengine_slave_config(chan, &rx_conf);
  268. uap->dmarx.chan = chan;
  269. dev_info(uap->port.dev, "DMA channel RX %s\n",
  270. dma_chan_name(uap->dmarx.chan));
  271. }
  272. }
  273. #ifndef MODULE
  274. /*
  275. * Stack up the UARTs and let the above initcall be done at device
  276. * initcall time, because the serial driver is called as an arch
  277. * initcall, and at this time the DMA subsystem is not yet registered.
  278. * At this point the driver will switch over to using DMA where desired.
  279. */
  280. struct dma_uap {
  281. struct list_head node;
  282. struct uart_amba_port *uap;
  283. struct device *dev;
  284. };
  285. static LIST_HEAD(pl011_dma_uarts);
  286. static int __init pl011_dma_initcall(void)
  287. {
  288. struct list_head *node, *tmp;
  289. list_for_each_safe(node, tmp, &pl011_dma_uarts) {
  290. struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
  291. pl011_dma_probe_initcall(dmau->dev, dmau->uap);
  292. list_del(node);
  293. kfree(dmau);
  294. }
  295. return 0;
  296. }
  297. device_initcall(pl011_dma_initcall);
  298. static void pl011_dma_probe(struct device *dev, struct uart_amba_port *uap)
  299. {
  300. struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
  301. if (dmau) {
  302. dmau->uap = uap;
  303. dmau->dev = dev;
  304. list_add_tail(&dmau->node, &pl011_dma_uarts);
  305. }
  306. }
  307. #else
  308. static void pl011_dma_probe(struct device *dev, struct uart_amba_port *uap)
  309. {
  310. pl011_dma_probe_initcall(dev, uap);
  311. }
  312. #endif
  313. static void pl011_dma_remove(struct uart_amba_port *uap)
  314. {
  315. /* TODO: remove the initcall if it has not yet executed */
  316. if (uap->dmatx.chan)
  317. dma_release_channel(uap->dmatx.chan);
  318. if (uap->dmarx.chan)
  319. dma_release_channel(uap->dmarx.chan);
  320. }
  321. /* Forward declare this for the refill routine */
  322. static int pl011_dma_tx_refill(struct uart_amba_port *uap);
  323. /*
  324. * The current DMA TX buffer has been sent.
  325. * Try to queue up another DMA buffer.
  326. */
  327. static void pl011_dma_tx_callback(void *data)
  328. {
  329. struct uart_amba_port *uap = data;
  330. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  331. unsigned long flags;
  332. u16 dmacr;
  333. spin_lock_irqsave(&uap->port.lock, flags);
  334. if (uap->dmatx.queued)
  335. dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
  336. DMA_TO_DEVICE);
  337. dmacr = uap->dmacr;
  338. uap->dmacr = dmacr & ~UART011_TXDMAE;
  339. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  340. /*
  341. * If TX DMA was disabled, it means that we've stopped the DMA for
  342. * some reason (eg, XOFF received, or we want to send an X-char.)
  343. *
  344. * Note: we need to be careful here of a potential race between DMA
  345. * and the rest of the driver - if the driver disables TX DMA while
  346. * a TX buffer completing, we must update the tx queued status to
  347. * get further refills (hence we check dmacr).
  348. */
  349. if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
  350. uart_circ_empty(&uap->port.state->xmit)) {
  351. uap->dmatx.queued = false;
  352. spin_unlock_irqrestore(&uap->port.lock, flags);
  353. return;
  354. }
  355. if (pl011_dma_tx_refill(uap) <= 0) {
  356. /*
  357. * We didn't queue a DMA buffer for some reason, but we
  358. * have data pending to be sent. Re-enable the TX IRQ.
  359. */
  360. uap->im |= UART011_TXIM;
  361. writew(uap->im, uap->port.membase + UART011_IMSC);
  362. }
  363. spin_unlock_irqrestore(&uap->port.lock, flags);
  364. }
  365. /*
  366. * Try to refill the TX DMA buffer.
  367. * Locking: called with port lock held and IRQs disabled.
  368. * Returns:
  369. * 1 if we queued up a TX DMA buffer.
  370. * 0 if we didn't want to handle this by DMA
  371. * <0 on error
  372. */
  373. static int pl011_dma_tx_refill(struct uart_amba_port *uap)
  374. {
  375. struct pl011_dmatx_data *dmatx = &uap->dmatx;
  376. struct dma_chan *chan = dmatx->chan;
  377. struct dma_device *dma_dev = chan->device;
  378. struct dma_async_tx_descriptor *desc;
  379. struct circ_buf *xmit = &uap->port.state->xmit;
  380. unsigned int count;
  381. /*
  382. * Try to avoid the overhead involved in using DMA if the
  383. * transaction fits in the first half of the FIFO, by using
  384. * the standard interrupt handling. This ensures that we
  385. * issue a uart_write_wakeup() at the appropriate time.
  386. */
  387. count = uart_circ_chars_pending(xmit);
  388. if (count < (uap->fifosize >> 1)) {
  389. uap->dmatx.queued = false;
  390. return 0;
  391. }
  392. /*
  393. * Bodge: don't send the last character by DMA, as this
  394. * will prevent XON from notifying us to restart DMA.
  395. */
  396. count -= 1;
  397. /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
  398. if (count > PL011_DMA_BUFFER_SIZE)
  399. count = PL011_DMA_BUFFER_SIZE;
  400. if (xmit->tail < xmit->head)
  401. memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
  402. else {
  403. size_t first = UART_XMIT_SIZE - xmit->tail;
  404. size_t second = xmit->head;
  405. memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
  406. if (second)
  407. memcpy(&dmatx->buf[first], &xmit->buf[0], second);
  408. }
  409. dmatx->sg.length = count;
  410. if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
  411. uap->dmatx.queued = false;
  412. dev_dbg(uap->port.dev, "unable to map TX DMA\n");
  413. return -EBUSY;
  414. }
  415. desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
  416. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  417. if (!desc) {
  418. dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
  419. uap->dmatx.queued = false;
  420. /*
  421. * If DMA cannot be used right now, we complete this
  422. * transaction via IRQ and let the TTY layer retry.
  423. */
  424. dev_dbg(uap->port.dev, "TX DMA busy\n");
  425. return -EBUSY;
  426. }
  427. /* Some data to go along to the callback */
  428. desc->callback = pl011_dma_tx_callback;
  429. desc->callback_param = uap;
  430. /* All errors should happen at prepare time */
  431. dmaengine_submit(desc);
  432. /* Fire the DMA transaction */
  433. dma_dev->device_issue_pending(chan);
  434. uap->dmacr |= UART011_TXDMAE;
  435. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  436. uap->dmatx.queued = true;
  437. /*
  438. * Now we know that DMA will fire, so advance the ring buffer
  439. * with the stuff we just dispatched.
  440. */
  441. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  442. uap->port.icount.tx += count;
  443. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  444. uart_write_wakeup(&uap->port);
  445. return 1;
  446. }
  447. /*
  448. * We received a transmit interrupt without a pending X-char but with
  449. * pending characters.
  450. * Locking: called with port lock held and IRQs disabled.
  451. * Returns:
  452. * false if we want to use PIO to transmit
  453. * true if we queued a DMA buffer
  454. */
  455. static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  456. {
  457. if (!uap->using_tx_dma)
  458. return false;
  459. /*
  460. * If we already have a TX buffer queued, but received a
  461. * TX interrupt, it will be because we've just sent an X-char.
  462. * Ensure the TX DMA is enabled and the TX IRQ is disabled.
  463. */
  464. if (uap->dmatx.queued) {
  465. uap->dmacr |= UART011_TXDMAE;
  466. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  467. uap->im &= ~UART011_TXIM;
  468. writew(uap->im, uap->port.membase + UART011_IMSC);
  469. return true;
  470. }
  471. /*
  472. * We don't have a TX buffer queued, so try to queue one.
  473. * If we successfully queued a buffer, mask the TX IRQ.
  474. */
  475. if (pl011_dma_tx_refill(uap) > 0) {
  476. uap->im &= ~UART011_TXIM;
  477. writew(uap->im, uap->port.membase + UART011_IMSC);
  478. return true;
  479. }
  480. return false;
  481. }
  482. /*
  483. * Stop the DMA transmit (eg, due to received XOFF).
  484. * Locking: called with port lock held and IRQs disabled.
  485. */
  486. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  487. {
  488. if (uap->dmatx.queued) {
  489. uap->dmacr &= ~UART011_TXDMAE;
  490. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  491. }
  492. }
  493. /*
  494. * Try to start a DMA transmit, or in the case of an XON/OFF
  495. * character queued for send, try to get that character out ASAP.
  496. * Locking: called with port lock held and IRQs disabled.
  497. * Returns:
  498. * false if we want the TX IRQ to be enabled
  499. * true if we have a buffer queued
  500. */
  501. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  502. {
  503. u16 dmacr;
  504. if (!uap->using_tx_dma)
  505. return false;
  506. if (!uap->port.x_char) {
  507. /* no X-char, try to push chars out in DMA mode */
  508. bool ret = true;
  509. if (!uap->dmatx.queued) {
  510. if (pl011_dma_tx_refill(uap) > 0) {
  511. uap->im &= ~UART011_TXIM;
  512. ret = true;
  513. } else {
  514. uap->im |= UART011_TXIM;
  515. ret = false;
  516. }
  517. writew(uap->im, uap->port.membase + UART011_IMSC);
  518. } else if (!(uap->dmacr & UART011_TXDMAE)) {
  519. uap->dmacr |= UART011_TXDMAE;
  520. writew(uap->dmacr,
  521. uap->port.membase + UART011_DMACR);
  522. }
  523. return ret;
  524. }
  525. /*
  526. * We have an X-char to send. Disable DMA to prevent it loading
  527. * the TX fifo, and then see if we can stuff it into the FIFO.
  528. */
  529. dmacr = uap->dmacr;
  530. uap->dmacr &= ~UART011_TXDMAE;
  531. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  532. if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
  533. /*
  534. * No space in the FIFO, so enable the transmit interrupt
  535. * so we know when there is space. Note that once we've
  536. * loaded the character, we should just re-enable DMA.
  537. */
  538. return false;
  539. }
  540. writew(uap->port.x_char, uap->port.membase + UART01x_DR);
  541. uap->port.icount.tx++;
  542. uap->port.x_char = 0;
  543. /* Success - restore the DMA state */
  544. uap->dmacr = dmacr;
  545. writew(dmacr, uap->port.membase + UART011_DMACR);
  546. return true;
  547. }
  548. /*
  549. * Flush the transmit buffer.
  550. * Locking: called with port lock held and IRQs disabled.
  551. */
  552. static void pl011_dma_flush_buffer(struct uart_port *port)
  553. {
  554. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  555. if (!uap->using_tx_dma)
  556. return;
  557. /* Avoid deadlock with the DMA engine callback */
  558. spin_unlock(&uap->port.lock);
  559. dmaengine_terminate_all(uap->dmatx.chan);
  560. spin_lock(&uap->port.lock);
  561. if (uap->dmatx.queued) {
  562. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  563. DMA_TO_DEVICE);
  564. uap->dmatx.queued = false;
  565. uap->dmacr &= ~UART011_TXDMAE;
  566. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  567. }
  568. }
  569. static void pl011_dma_rx_callback(void *data);
  570. static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  571. {
  572. struct dma_chan *rxchan = uap->dmarx.chan;
  573. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  574. struct dma_async_tx_descriptor *desc;
  575. struct pl011_sgbuf *sgbuf;
  576. if (!rxchan)
  577. return -EIO;
  578. /* Start the RX DMA job */
  579. sgbuf = uap->dmarx.use_buf_b ?
  580. &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
  581. desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
  582. DMA_DEV_TO_MEM,
  583. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  584. /*
  585. * If the DMA engine is busy and cannot prepare a
  586. * channel, no big deal, the driver will fall back
  587. * to interrupt mode as a result of this error code.
  588. */
  589. if (!desc) {
  590. uap->dmarx.running = false;
  591. dmaengine_terminate_all(rxchan);
  592. return -EBUSY;
  593. }
  594. /* Some data to go along to the callback */
  595. desc->callback = pl011_dma_rx_callback;
  596. desc->callback_param = uap;
  597. dmarx->cookie = dmaengine_submit(desc);
  598. dma_async_issue_pending(rxchan);
  599. uap->dmacr |= UART011_RXDMAE;
  600. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  601. uap->dmarx.running = true;
  602. uap->im &= ~UART011_RXIM;
  603. writew(uap->im, uap->port.membase + UART011_IMSC);
  604. return 0;
  605. }
  606. /*
  607. * This is called when either the DMA job is complete, or
  608. * the FIFO timeout interrupt occurred. This must be called
  609. * with the port spinlock uap->port.lock held.
  610. */
  611. static void pl011_dma_rx_chars(struct uart_amba_port *uap,
  612. u32 pending, bool use_buf_b,
  613. bool readfifo)
  614. {
  615. struct tty_port *port = &uap->port.state->port;
  616. struct pl011_sgbuf *sgbuf = use_buf_b ?
  617. &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
  618. struct device *dev = uap->dmarx.chan->device->dev;
  619. int dma_count = 0;
  620. u32 fifotaken = 0; /* only used for vdbg() */
  621. /* Pick everything from the DMA first */
  622. if (pending) {
  623. /* Sync in buffer */
  624. dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
  625. /*
  626. * First take all chars in the DMA pipe, then look in the FIFO.
  627. * Note that tty_insert_flip_buf() tries to take as many chars
  628. * as it can.
  629. */
  630. dma_count = tty_insert_flip_string(port, sgbuf->buf, pending);
  631. /* Return buffer to device */
  632. dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
  633. uap->port.icount.rx += dma_count;
  634. if (dma_count < pending)
  635. dev_warn(uap->port.dev,
  636. "couldn't insert all characters (TTY is full?)\n");
  637. }
  638. /*
  639. * Only continue with trying to read the FIFO if all DMA chars have
  640. * been taken first.
  641. */
  642. if (dma_count == pending && readfifo) {
  643. /* Clear any error flags */
  644. writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
  645. uap->port.membase + UART011_ICR);
  646. /*
  647. * If we read all the DMA'd characters, and we had an
  648. * incomplete buffer, that could be due to an rx error, or
  649. * maybe we just timed out. Read any pending chars and check
  650. * the error status.
  651. *
  652. * Error conditions will only occur in the FIFO, these will
  653. * trigger an immediate interrupt and stop the DMA job, so we
  654. * will always find the error in the FIFO, never in the DMA
  655. * buffer.
  656. */
  657. fifotaken = pl011_fifo_to_tty(uap);
  658. }
  659. spin_unlock(&uap->port.lock);
  660. dev_vdbg(uap->port.dev,
  661. "Took %d chars from DMA buffer and %d chars from the FIFO\n",
  662. dma_count, fifotaken);
  663. tty_flip_buffer_push(port);
  664. spin_lock(&uap->port.lock);
  665. }
  666. static void pl011_dma_rx_irq(struct uart_amba_port *uap)
  667. {
  668. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  669. struct dma_chan *rxchan = dmarx->chan;
  670. struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
  671. &dmarx->sgbuf_b : &dmarx->sgbuf_a;
  672. size_t pending;
  673. struct dma_tx_state state;
  674. enum dma_status dmastat;
  675. /*
  676. * Pause the transfer so we can trust the current counter,
  677. * do this before we pause the PL011 block, else we may
  678. * overflow the FIFO.
  679. */
  680. if (dmaengine_pause(rxchan))
  681. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  682. dmastat = rxchan->device->device_tx_status(rxchan,
  683. dmarx->cookie, &state);
  684. if (dmastat != DMA_PAUSED)
  685. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  686. /* Disable RX DMA - incoming data will wait in the FIFO */
  687. uap->dmacr &= ~UART011_RXDMAE;
  688. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  689. uap->dmarx.running = false;
  690. pending = sgbuf->sg.length - state.residue;
  691. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  692. /* Then we terminate the transfer - we now know our residue */
  693. dmaengine_terminate_all(rxchan);
  694. /*
  695. * This will take the chars we have so far and insert
  696. * into the framework.
  697. */
  698. pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
  699. /* Switch buffer & re-trigger DMA job */
  700. dmarx->use_buf_b = !dmarx->use_buf_b;
  701. if (pl011_dma_rx_trigger_dma(uap)) {
  702. dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
  703. "fall back to interrupt mode\n");
  704. uap->im |= UART011_RXIM;
  705. writew(uap->im, uap->port.membase + UART011_IMSC);
  706. }
  707. }
  708. static void pl011_dma_rx_callback(void *data)
  709. {
  710. struct uart_amba_port *uap = data;
  711. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  712. struct dma_chan *rxchan = dmarx->chan;
  713. bool lastbuf = dmarx->use_buf_b;
  714. struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
  715. &dmarx->sgbuf_b : &dmarx->sgbuf_a;
  716. size_t pending;
  717. struct dma_tx_state state;
  718. int ret;
  719. /*
  720. * This completion interrupt occurs typically when the
  721. * RX buffer is totally stuffed but no timeout has yet
  722. * occurred. When that happens, we just want the RX
  723. * routine to flush out the secondary DMA buffer while
  724. * we immediately trigger the next DMA job.
  725. */
  726. spin_lock_irq(&uap->port.lock);
  727. /*
  728. * Rx data can be taken by the UART interrupts during
  729. * the DMA irq handler. So we check the residue here.
  730. */
  731. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  732. pending = sgbuf->sg.length - state.residue;
  733. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  734. /* Then we terminate the transfer - we now know our residue */
  735. dmaengine_terminate_all(rxchan);
  736. uap->dmarx.running = false;
  737. dmarx->use_buf_b = !lastbuf;
  738. ret = pl011_dma_rx_trigger_dma(uap);
  739. pl011_dma_rx_chars(uap, pending, lastbuf, false);
  740. spin_unlock_irq(&uap->port.lock);
  741. /*
  742. * Do this check after we picked the DMA chars so we don't
  743. * get some IRQ immediately from RX.
  744. */
  745. if (ret) {
  746. dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
  747. "fall back to interrupt mode\n");
  748. uap->im |= UART011_RXIM;
  749. writew(uap->im, uap->port.membase + UART011_IMSC);
  750. }
  751. }
  752. /*
  753. * Stop accepting received characters, when we're shutting down or
  754. * suspending this port.
  755. * Locking: called with port lock held and IRQs disabled.
  756. */
  757. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  758. {
  759. /* FIXME. Just disable the DMA enable */
  760. uap->dmacr &= ~UART011_RXDMAE;
  761. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  762. }
  763. static void pl011_dma_startup(struct uart_amba_port *uap)
  764. {
  765. int ret;
  766. if (!uap->dmatx.chan)
  767. return;
  768. uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
  769. if (!uap->dmatx.buf) {
  770. dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
  771. uap->port.fifosize = uap->fifosize;
  772. return;
  773. }
  774. sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
  775. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  776. uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
  777. uap->using_tx_dma = true;
  778. if (!uap->dmarx.chan)
  779. goto skip_rx;
  780. /* Allocate and map DMA RX buffers */
  781. ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
  782. DMA_FROM_DEVICE);
  783. if (ret) {
  784. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  785. "RX buffer A", ret);
  786. goto skip_rx;
  787. }
  788. ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
  789. DMA_FROM_DEVICE);
  790. if (ret) {
  791. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  792. "RX buffer B", ret);
  793. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
  794. DMA_FROM_DEVICE);
  795. goto skip_rx;
  796. }
  797. uap->using_rx_dma = true;
  798. skip_rx:
  799. /* Turn on DMA error (RX/TX will be enabled on demand) */
  800. uap->dmacr |= UART011_DMAONERR;
  801. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  802. /*
  803. * ST Micro variants has some specific dma burst threshold
  804. * compensation. Set this to 16 bytes, so burst will only
  805. * be issued above/below 16 bytes.
  806. */
  807. if (uap->vendor->dma_threshold)
  808. writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
  809. uap->port.membase + ST_UART011_DMAWM);
  810. if (uap->using_rx_dma) {
  811. if (pl011_dma_rx_trigger_dma(uap))
  812. dev_dbg(uap->port.dev, "could not trigger initial "
  813. "RX DMA job, fall back to interrupt mode\n");
  814. }
  815. }
  816. static void pl011_dma_shutdown(struct uart_amba_port *uap)
  817. {
  818. if (!(uap->using_tx_dma || uap->using_rx_dma))
  819. return;
  820. /* Disable RX and TX DMA */
  821. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  822. barrier();
  823. spin_lock_irq(&uap->port.lock);
  824. uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
  825. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  826. spin_unlock_irq(&uap->port.lock);
  827. if (uap->using_tx_dma) {
  828. /* In theory, this should already be done by pl011_dma_flush_buffer */
  829. dmaengine_terminate_all(uap->dmatx.chan);
  830. if (uap->dmatx.queued) {
  831. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  832. DMA_TO_DEVICE);
  833. uap->dmatx.queued = false;
  834. }
  835. kfree(uap->dmatx.buf);
  836. uap->using_tx_dma = false;
  837. }
  838. if (uap->using_rx_dma) {
  839. dmaengine_terminate_all(uap->dmarx.chan);
  840. /* Clean up the RX DMA */
  841. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
  842. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
  843. uap->using_rx_dma = false;
  844. }
  845. }
  846. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  847. {
  848. return uap->using_rx_dma;
  849. }
  850. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  851. {
  852. return uap->using_rx_dma && uap->dmarx.running;
  853. }
  854. #else
  855. /* Blank functions if the DMA engine is not available */
  856. static inline void pl011_dma_probe(struct uart_amba_port *uap)
  857. {
  858. }
  859. static inline void pl011_dma_remove(struct uart_amba_port *uap)
  860. {
  861. }
  862. static inline void pl011_dma_startup(struct uart_amba_port *uap)
  863. {
  864. }
  865. static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
  866. {
  867. }
  868. static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  869. {
  870. return false;
  871. }
  872. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  873. {
  874. }
  875. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  876. {
  877. return false;
  878. }
  879. static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
  880. {
  881. }
  882. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  883. {
  884. }
  885. static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  886. {
  887. return -EIO;
  888. }
  889. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  890. {
  891. return false;
  892. }
  893. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  894. {
  895. return false;
  896. }
  897. #define pl011_dma_flush_buffer NULL
  898. #endif
  899. static void pl011_stop_tx(struct uart_port *port)
  900. {
  901. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  902. uap->im &= ~UART011_TXIM;
  903. writew(uap->im, uap->port.membase + UART011_IMSC);
  904. pl011_dma_tx_stop(uap);
  905. }
  906. static void pl011_start_tx(struct uart_port *port)
  907. {
  908. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  909. if (!pl011_dma_tx_start(uap)) {
  910. uap->im |= UART011_TXIM;
  911. writew(uap->im, uap->port.membase + UART011_IMSC);
  912. }
  913. }
  914. static void pl011_stop_rx(struct uart_port *port)
  915. {
  916. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  917. uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
  918. UART011_PEIM|UART011_BEIM|UART011_OEIM);
  919. writew(uap->im, uap->port.membase + UART011_IMSC);
  920. pl011_dma_rx_stop(uap);
  921. }
  922. static void pl011_enable_ms(struct uart_port *port)
  923. {
  924. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  925. uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
  926. writew(uap->im, uap->port.membase + UART011_IMSC);
  927. }
  928. static void pl011_rx_chars(struct uart_amba_port *uap)
  929. {
  930. pl011_fifo_to_tty(uap);
  931. spin_unlock(&uap->port.lock);
  932. tty_flip_buffer_push(&uap->port.state->port);
  933. /*
  934. * If we were temporarily out of DMA mode for a while,
  935. * attempt to switch back to DMA mode again.
  936. */
  937. if (pl011_dma_rx_available(uap)) {
  938. if (pl011_dma_rx_trigger_dma(uap)) {
  939. dev_dbg(uap->port.dev, "could not trigger RX DMA job "
  940. "fall back to interrupt mode again\n");
  941. uap->im |= UART011_RXIM;
  942. } else
  943. uap->im &= ~UART011_RXIM;
  944. writew(uap->im, uap->port.membase + UART011_IMSC);
  945. }
  946. spin_lock(&uap->port.lock);
  947. }
  948. static void pl011_tx_chars(struct uart_amba_port *uap)
  949. {
  950. struct circ_buf *xmit = &uap->port.state->xmit;
  951. int count;
  952. if (uap->port.x_char) {
  953. writew(uap->port.x_char, uap->port.membase + UART01x_DR);
  954. uap->port.icount.tx++;
  955. uap->port.x_char = 0;
  956. return;
  957. }
  958. if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
  959. pl011_stop_tx(&uap->port);
  960. return;
  961. }
  962. /* If we are using DMA mode, try to send some characters. */
  963. if (pl011_dma_tx_irq(uap))
  964. return;
  965. count = uap->fifosize >> 1;
  966. do {
  967. writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
  968. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  969. uap->port.icount.tx++;
  970. if (uart_circ_empty(xmit))
  971. break;
  972. } while (--count > 0);
  973. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  974. uart_write_wakeup(&uap->port);
  975. if (uart_circ_empty(xmit))
  976. pl011_stop_tx(&uap->port);
  977. }
  978. static void pl011_modem_status(struct uart_amba_port *uap)
  979. {
  980. unsigned int status, delta;
  981. status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  982. delta = status ^ uap->old_status;
  983. uap->old_status = status;
  984. if (!delta)
  985. return;
  986. if (delta & UART01x_FR_DCD)
  987. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  988. if (delta & UART01x_FR_DSR)
  989. uap->port.icount.dsr++;
  990. if (delta & UART01x_FR_CTS)
  991. uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
  992. wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
  993. }
  994. static irqreturn_t pl011_int(int irq, void *dev_id)
  995. {
  996. struct uart_amba_port *uap = dev_id;
  997. unsigned long flags;
  998. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  999. int handled = 0;
  1000. unsigned int dummy_read;
  1001. spin_lock_irqsave(&uap->port.lock, flags);
  1002. status = readw(uap->port.membase + UART011_MIS);
  1003. if (status) {
  1004. do {
  1005. if (uap->vendor->cts_event_workaround) {
  1006. /* workaround to make sure that all bits are unlocked.. */
  1007. writew(0x00, uap->port.membase + UART011_ICR);
  1008. /*
  1009. * WA: introduce 26ns(1 uart clk) delay before W1C;
  1010. * single apb access will incur 2 pclk(133.12Mhz) delay,
  1011. * so add 2 dummy reads
  1012. */
  1013. dummy_read = readw(uap->port.membase + UART011_ICR);
  1014. dummy_read = readw(uap->port.membase + UART011_ICR);
  1015. }
  1016. writew(status & ~(UART011_TXIS|UART011_RTIS|
  1017. UART011_RXIS),
  1018. uap->port.membase + UART011_ICR);
  1019. if (status & (UART011_RTIS|UART011_RXIS)) {
  1020. if (pl011_dma_rx_running(uap))
  1021. pl011_dma_rx_irq(uap);
  1022. else
  1023. pl011_rx_chars(uap);
  1024. }
  1025. if (status & (UART011_DSRMIS|UART011_DCDMIS|
  1026. UART011_CTSMIS|UART011_RIMIS))
  1027. pl011_modem_status(uap);
  1028. if (status & UART011_TXIS)
  1029. pl011_tx_chars(uap);
  1030. if (pass_counter-- == 0)
  1031. break;
  1032. status = readw(uap->port.membase + UART011_MIS);
  1033. } while (status != 0);
  1034. handled = 1;
  1035. }
  1036. spin_unlock_irqrestore(&uap->port.lock, flags);
  1037. return IRQ_RETVAL(handled);
  1038. }
  1039. static unsigned int pl011_tx_empty(struct uart_port *port)
  1040. {
  1041. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1042. unsigned int status = readw(uap->port.membase + UART01x_FR);
  1043. return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
  1044. }
  1045. static unsigned int pl011_get_mctrl(struct uart_port *port)
  1046. {
  1047. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1048. unsigned int result = 0;
  1049. unsigned int status = readw(uap->port.membase + UART01x_FR);
  1050. #define TIOCMBIT(uartbit, tiocmbit) \
  1051. if (status & uartbit) \
  1052. result |= tiocmbit
  1053. TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
  1054. TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
  1055. TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
  1056. TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
  1057. #undef TIOCMBIT
  1058. return result;
  1059. }
  1060. static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1061. {
  1062. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1063. unsigned int cr;
  1064. cr = readw(uap->port.membase + UART011_CR);
  1065. #define TIOCMBIT(tiocmbit, uartbit) \
  1066. if (mctrl & tiocmbit) \
  1067. cr |= uartbit; \
  1068. else \
  1069. cr &= ~uartbit
  1070. TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
  1071. TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
  1072. TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
  1073. TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
  1074. TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
  1075. if (uap->autorts) {
  1076. /* We need to disable auto-RTS if we want to turn RTS off */
  1077. TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
  1078. }
  1079. #undef TIOCMBIT
  1080. writew(cr, uap->port.membase + UART011_CR);
  1081. }
  1082. static void pl011_break_ctl(struct uart_port *port, int break_state)
  1083. {
  1084. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1085. unsigned long flags;
  1086. unsigned int lcr_h;
  1087. spin_lock_irqsave(&uap->port.lock, flags);
  1088. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  1089. if (break_state == -1)
  1090. lcr_h |= UART01x_LCRH_BRK;
  1091. else
  1092. lcr_h &= ~UART01x_LCRH_BRK;
  1093. writew(lcr_h, uap->port.membase + uap->lcrh_tx);
  1094. spin_unlock_irqrestore(&uap->port.lock, flags);
  1095. }
  1096. #ifdef CONFIG_CONSOLE_POLL
  1097. static void pl011_quiesce_irqs(struct uart_port *port)
  1098. {
  1099. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1100. unsigned char __iomem *regs = uap->port.membase;
  1101. writew(readw(regs + UART011_MIS), regs + UART011_ICR);
  1102. /*
  1103. * There is no way to clear TXIM as this is "ready to transmit IRQ", so
  1104. * we simply mask it. start_tx() will unmask it.
  1105. *
  1106. * Note we can race with start_tx(), and if the race happens, the
  1107. * polling user might get another interrupt just after we clear it.
  1108. * But it should be OK and can happen even w/o the race, e.g.
  1109. * controller immediately got some new data and raised the IRQ.
  1110. *
  1111. * And whoever uses polling routines assumes that it manages the device
  1112. * (including tx queue), so we're also fine with start_tx()'s caller
  1113. * side.
  1114. */
  1115. writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
  1116. }
  1117. static int pl011_get_poll_char(struct uart_port *port)
  1118. {
  1119. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1120. unsigned int status;
  1121. /*
  1122. * The caller might need IRQs lowered, e.g. if used with KDB NMI
  1123. * debugger.
  1124. */
  1125. pl011_quiesce_irqs(port);
  1126. status = readw(uap->port.membase + UART01x_FR);
  1127. if (status & UART01x_FR_RXFE)
  1128. return NO_POLL_CHAR;
  1129. return readw(uap->port.membase + UART01x_DR);
  1130. }
  1131. static void pl011_put_poll_char(struct uart_port *port,
  1132. unsigned char ch)
  1133. {
  1134. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1135. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1136. barrier();
  1137. writew(ch, uap->port.membase + UART01x_DR);
  1138. }
  1139. #endif /* CONFIG_CONSOLE_POLL */
  1140. static int pl011_hwinit(struct uart_port *port)
  1141. {
  1142. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1143. int retval;
  1144. /* Optionaly enable pins to be muxed in and configured */
  1145. if (!IS_ERR(uap->pins_default)) {
  1146. retval = pinctrl_select_state(uap->pinctrl, uap->pins_default);
  1147. if (retval)
  1148. dev_err(port->dev,
  1149. "could not set default pins\n");
  1150. }
  1151. /*
  1152. * Try to enable the clock producer.
  1153. */
  1154. retval = clk_prepare_enable(uap->clk);
  1155. if (retval)
  1156. goto out;
  1157. uap->port.uartclk = clk_get_rate(uap->clk);
  1158. /* Clear pending error and receive interrupts */
  1159. writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
  1160. UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
  1161. /*
  1162. * Save interrupts enable mask, and enable RX interrupts in case if
  1163. * the interrupt is used for NMI entry.
  1164. */
  1165. uap->im = readw(uap->port.membase + UART011_IMSC);
  1166. writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
  1167. if (uap->port.dev->platform_data) {
  1168. struct amba_pl011_data *plat;
  1169. plat = uap->port.dev->platform_data;
  1170. if (plat->init)
  1171. plat->init();
  1172. }
  1173. return 0;
  1174. out:
  1175. return retval;
  1176. }
  1177. static int pl011_startup(struct uart_port *port)
  1178. {
  1179. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1180. unsigned int cr;
  1181. int retval;
  1182. retval = pl011_hwinit(port);
  1183. if (retval)
  1184. goto clk_dis;
  1185. writew(uap->im, uap->port.membase + UART011_IMSC);
  1186. /*
  1187. * Allocate the IRQ
  1188. */
  1189. retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
  1190. if (retval)
  1191. goto clk_dis;
  1192. writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
  1193. /*
  1194. * Provoke TX FIFO interrupt into asserting.
  1195. */
  1196. cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
  1197. writew(cr, uap->port.membase + UART011_CR);
  1198. writew(0, uap->port.membase + UART011_FBRD);
  1199. writew(1, uap->port.membase + UART011_IBRD);
  1200. writew(0, uap->port.membase + uap->lcrh_rx);
  1201. if (uap->lcrh_tx != uap->lcrh_rx) {
  1202. int i;
  1203. /*
  1204. * Wait 10 PCLKs before writing LCRH_TX register,
  1205. * to get this delay write read only register 10 times
  1206. */
  1207. for (i = 0; i < 10; ++i)
  1208. writew(0xff, uap->port.membase + UART011_MIS);
  1209. writew(0, uap->port.membase + uap->lcrh_tx);
  1210. }
  1211. writew(0, uap->port.membase + UART01x_DR);
  1212. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  1213. barrier();
  1214. /* restore RTS and DTR */
  1215. cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
  1216. cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  1217. writew(cr, uap->port.membase + UART011_CR);
  1218. /*
  1219. * initialise the old status of the modem signals
  1220. */
  1221. uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  1222. /* Startup DMA */
  1223. pl011_dma_startup(uap);
  1224. /*
  1225. * Finally, enable interrupts, only timeouts when using DMA
  1226. * if initial RX DMA job failed, start in interrupt mode
  1227. * as well.
  1228. */
  1229. spin_lock_irq(&uap->port.lock);
  1230. /* Clear out any spuriously appearing RX interrupts */
  1231. writew(UART011_RTIS | UART011_RXIS,
  1232. uap->port.membase + UART011_ICR);
  1233. uap->im = UART011_RTIM;
  1234. if (!pl011_dma_rx_running(uap))
  1235. uap->im |= UART011_RXIM;
  1236. writew(uap->im, uap->port.membase + UART011_IMSC);
  1237. spin_unlock_irq(&uap->port.lock);
  1238. return 0;
  1239. clk_dis:
  1240. clk_disable_unprepare(uap->clk);
  1241. return retval;
  1242. }
  1243. static void pl011_shutdown_channel(struct uart_amba_port *uap,
  1244. unsigned int lcrh)
  1245. {
  1246. unsigned long val;
  1247. val = readw(uap->port.membase + lcrh);
  1248. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1249. writew(val, uap->port.membase + lcrh);
  1250. }
  1251. static void pl011_shutdown(struct uart_port *port)
  1252. {
  1253. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1254. unsigned int cr;
  1255. int retval;
  1256. /*
  1257. * disable all interrupts
  1258. */
  1259. spin_lock_irq(&uap->port.lock);
  1260. uap->im = 0;
  1261. writew(uap->im, uap->port.membase + UART011_IMSC);
  1262. writew(0xffff, uap->port.membase + UART011_ICR);
  1263. spin_unlock_irq(&uap->port.lock);
  1264. pl011_dma_shutdown(uap);
  1265. /*
  1266. * Free the interrupt
  1267. */
  1268. free_irq(uap->port.irq, uap);
  1269. /*
  1270. * disable the port
  1271. * disable the port. It should not disable RTS and DTR.
  1272. * Also RTS and DTR state should be preserved to restore
  1273. * it during startup().
  1274. */
  1275. uap->autorts = false;
  1276. cr = readw(uap->port.membase + UART011_CR);
  1277. uap->old_cr = cr;
  1278. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1279. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1280. writew(cr, uap->port.membase + UART011_CR);
  1281. /*
  1282. * disable break condition and fifos
  1283. */
  1284. pl011_shutdown_channel(uap, uap->lcrh_rx);
  1285. if (uap->lcrh_rx != uap->lcrh_tx)
  1286. pl011_shutdown_channel(uap, uap->lcrh_tx);
  1287. /*
  1288. * Shut down the clock producer
  1289. */
  1290. clk_disable_unprepare(uap->clk);
  1291. /* Optionally let pins go into sleep states */
  1292. if (!IS_ERR(uap->pins_sleep)) {
  1293. retval = pinctrl_select_state(uap->pinctrl, uap->pins_sleep);
  1294. if (retval)
  1295. dev_err(port->dev,
  1296. "could not set pins to sleep state\n");
  1297. }
  1298. if (uap->port.dev->platform_data) {
  1299. struct amba_pl011_data *plat;
  1300. plat = uap->port.dev->platform_data;
  1301. if (plat->exit)
  1302. plat->exit();
  1303. }
  1304. }
  1305. static void
  1306. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1307. struct ktermios *old)
  1308. {
  1309. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1310. unsigned int lcr_h, old_cr;
  1311. unsigned long flags;
  1312. unsigned int baud, quot, clkdiv;
  1313. if (uap->vendor->oversampling)
  1314. clkdiv = 8;
  1315. else
  1316. clkdiv = 16;
  1317. /*
  1318. * Ask the core to calculate the divisor for us.
  1319. */
  1320. baud = uart_get_baud_rate(port, termios, old, 0,
  1321. port->uartclk / clkdiv);
  1322. if (baud > port->uartclk/16)
  1323. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1324. else
  1325. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1326. switch (termios->c_cflag & CSIZE) {
  1327. case CS5:
  1328. lcr_h = UART01x_LCRH_WLEN_5;
  1329. break;
  1330. case CS6:
  1331. lcr_h = UART01x_LCRH_WLEN_6;
  1332. break;
  1333. case CS7:
  1334. lcr_h = UART01x_LCRH_WLEN_7;
  1335. break;
  1336. default: // CS8
  1337. lcr_h = UART01x_LCRH_WLEN_8;
  1338. break;
  1339. }
  1340. if (termios->c_cflag & CSTOPB)
  1341. lcr_h |= UART01x_LCRH_STP2;
  1342. if (termios->c_cflag & PARENB) {
  1343. lcr_h |= UART01x_LCRH_PEN;
  1344. if (!(termios->c_cflag & PARODD))
  1345. lcr_h |= UART01x_LCRH_EPS;
  1346. }
  1347. if (uap->fifosize > 1)
  1348. lcr_h |= UART01x_LCRH_FEN;
  1349. spin_lock_irqsave(&port->lock, flags);
  1350. /*
  1351. * Update the per-port timeout.
  1352. */
  1353. uart_update_timeout(port, termios->c_cflag, baud);
  1354. port->read_status_mask = UART011_DR_OE | 255;
  1355. if (termios->c_iflag & INPCK)
  1356. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1357. if (termios->c_iflag & (BRKINT | PARMRK))
  1358. port->read_status_mask |= UART011_DR_BE;
  1359. /*
  1360. * Characters to ignore
  1361. */
  1362. port->ignore_status_mask = 0;
  1363. if (termios->c_iflag & IGNPAR)
  1364. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1365. if (termios->c_iflag & IGNBRK) {
  1366. port->ignore_status_mask |= UART011_DR_BE;
  1367. /*
  1368. * If we're ignoring parity and break indicators,
  1369. * ignore overruns too (for real raw support).
  1370. */
  1371. if (termios->c_iflag & IGNPAR)
  1372. port->ignore_status_mask |= UART011_DR_OE;
  1373. }
  1374. /*
  1375. * Ignore all characters if CREAD is not set.
  1376. */
  1377. if ((termios->c_cflag & CREAD) == 0)
  1378. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1379. if (UART_ENABLE_MS(port, termios->c_cflag))
  1380. pl011_enable_ms(port);
  1381. /* first, disable everything */
  1382. old_cr = readw(port->membase + UART011_CR);
  1383. writew(0, port->membase + UART011_CR);
  1384. if (termios->c_cflag & CRTSCTS) {
  1385. if (old_cr & UART011_CR_RTS)
  1386. old_cr |= UART011_CR_RTSEN;
  1387. old_cr |= UART011_CR_CTSEN;
  1388. uap->autorts = true;
  1389. } else {
  1390. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1391. uap->autorts = false;
  1392. }
  1393. if (uap->vendor->oversampling) {
  1394. if (baud > port->uartclk / 16)
  1395. old_cr |= ST_UART011_CR_OVSFACT;
  1396. else
  1397. old_cr &= ~ST_UART011_CR_OVSFACT;
  1398. }
  1399. /*
  1400. * Workaround for the ST Micro oversampling variants to
  1401. * increase the bitrate slightly, by lowering the divisor,
  1402. * to avoid delayed sampling of start bit at high speeds,
  1403. * else we see data corruption.
  1404. */
  1405. if (uap->vendor->oversampling) {
  1406. if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
  1407. quot -= 1;
  1408. else if ((baud > 3250000) && (quot > 2))
  1409. quot -= 2;
  1410. }
  1411. /* Set baud rate */
  1412. writew(quot & 0x3f, port->membase + UART011_FBRD);
  1413. writew(quot >> 6, port->membase + UART011_IBRD);
  1414. /*
  1415. * ----------v----------v----------v----------v-----
  1416. * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
  1417. * UART011_FBRD & UART011_IBRD.
  1418. * ----------^----------^----------^----------^-----
  1419. */
  1420. writew(lcr_h, port->membase + uap->lcrh_rx);
  1421. if (uap->lcrh_rx != uap->lcrh_tx) {
  1422. int i;
  1423. /*
  1424. * Wait 10 PCLKs before writing LCRH_TX register,
  1425. * to get this delay write read only register 10 times
  1426. */
  1427. for (i = 0; i < 10; ++i)
  1428. writew(0xff, uap->port.membase + UART011_MIS);
  1429. writew(lcr_h, port->membase + uap->lcrh_tx);
  1430. }
  1431. writew(old_cr, port->membase + UART011_CR);
  1432. spin_unlock_irqrestore(&port->lock, flags);
  1433. }
  1434. static const char *pl011_type(struct uart_port *port)
  1435. {
  1436. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1437. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1438. }
  1439. /*
  1440. * Release the memory region(s) being used by 'port'
  1441. */
  1442. static void pl011_release_port(struct uart_port *port)
  1443. {
  1444. release_mem_region(port->mapbase, SZ_4K);
  1445. }
  1446. /*
  1447. * Request the memory region(s) being used by 'port'
  1448. */
  1449. static int pl011_request_port(struct uart_port *port)
  1450. {
  1451. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  1452. != NULL ? 0 : -EBUSY;
  1453. }
  1454. /*
  1455. * Configure/autoconfigure the port.
  1456. */
  1457. static void pl011_config_port(struct uart_port *port, int flags)
  1458. {
  1459. if (flags & UART_CONFIG_TYPE) {
  1460. port->type = PORT_AMBA;
  1461. pl011_request_port(port);
  1462. }
  1463. }
  1464. /*
  1465. * verify the new serial_struct (for TIOCSSERIAL).
  1466. */
  1467. static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
  1468. {
  1469. int ret = 0;
  1470. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1471. ret = -EINVAL;
  1472. if (ser->irq < 0 || ser->irq >= nr_irqs)
  1473. ret = -EINVAL;
  1474. if (ser->baud_base < 9600)
  1475. ret = -EINVAL;
  1476. return ret;
  1477. }
  1478. static struct uart_ops amba_pl011_pops = {
  1479. .tx_empty = pl011_tx_empty,
  1480. .set_mctrl = pl011_set_mctrl,
  1481. .get_mctrl = pl011_get_mctrl,
  1482. .stop_tx = pl011_stop_tx,
  1483. .start_tx = pl011_start_tx,
  1484. .stop_rx = pl011_stop_rx,
  1485. .enable_ms = pl011_enable_ms,
  1486. .break_ctl = pl011_break_ctl,
  1487. .startup = pl011_startup,
  1488. .shutdown = pl011_shutdown,
  1489. .flush_buffer = pl011_dma_flush_buffer,
  1490. .set_termios = pl011_set_termios,
  1491. .type = pl011_type,
  1492. .release_port = pl011_release_port,
  1493. .request_port = pl011_request_port,
  1494. .config_port = pl011_config_port,
  1495. .verify_port = pl011_verify_port,
  1496. #ifdef CONFIG_CONSOLE_POLL
  1497. .poll_init = pl011_hwinit,
  1498. .poll_get_char = pl011_get_poll_char,
  1499. .poll_put_char = pl011_put_poll_char,
  1500. #endif
  1501. };
  1502. static struct uart_amba_port *amba_ports[UART_NR];
  1503. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1504. static void pl011_console_putchar(struct uart_port *port, int ch)
  1505. {
  1506. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1507. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1508. barrier();
  1509. writew(ch, uap->port.membase + UART01x_DR);
  1510. }
  1511. static void
  1512. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1513. {
  1514. struct uart_amba_port *uap = amba_ports[co->index];
  1515. unsigned int status, old_cr, new_cr;
  1516. unsigned long flags;
  1517. int locked = 1;
  1518. clk_enable(uap->clk);
  1519. local_irq_save(flags);
  1520. if (uap->port.sysrq)
  1521. locked = 0;
  1522. else if (oops_in_progress)
  1523. locked = spin_trylock(&uap->port.lock);
  1524. else
  1525. spin_lock(&uap->port.lock);
  1526. /*
  1527. * First save the CR then disable the interrupts
  1528. */
  1529. old_cr = readw(uap->port.membase + UART011_CR);
  1530. new_cr = old_cr & ~UART011_CR_CTSEN;
  1531. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1532. writew(new_cr, uap->port.membase + UART011_CR);
  1533. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1534. /*
  1535. * Finally, wait for transmitter to become empty
  1536. * and restore the TCR
  1537. */
  1538. do {
  1539. status = readw(uap->port.membase + UART01x_FR);
  1540. } while (status & UART01x_FR_BUSY);
  1541. writew(old_cr, uap->port.membase + UART011_CR);
  1542. if (locked)
  1543. spin_unlock(&uap->port.lock);
  1544. local_irq_restore(flags);
  1545. clk_disable(uap->clk);
  1546. }
  1547. static void __init
  1548. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1549. int *parity, int *bits)
  1550. {
  1551. if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
  1552. unsigned int lcr_h, ibrd, fbrd;
  1553. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  1554. *parity = 'n';
  1555. if (lcr_h & UART01x_LCRH_PEN) {
  1556. if (lcr_h & UART01x_LCRH_EPS)
  1557. *parity = 'e';
  1558. else
  1559. *parity = 'o';
  1560. }
  1561. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  1562. *bits = 7;
  1563. else
  1564. *bits = 8;
  1565. ibrd = readw(uap->port.membase + UART011_IBRD);
  1566. fbrd = readw(uap->port.membase + UART011_FBRD);
  1567. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  1568. if (uap->vendor->oversampling) {
  1569. if (readw(uap->port.membase + UART011_CR)
  1570. & ST_UART011_CR_OVSFACT)
  1571. *baud *= 2;
  1572. }
  1573. }
  1574. }
  1575. static int __init pl011_console_setup(struct console *co, char *options)
  1576. {
  1577. struct uart_amba_port *uap;
  1578. int baud = 38400;
  1579. int bits = 8;
  1580. int parity = 'n';
  1581. int flow = 'n';
  1582. int ret;
  1583. /*
  1584. * Check whether an invalid uart number has been specified, and
  1585. * if so, search for the first available port that does have
  1586. * console support.
  1587. */
  1588. if (co->index >= UART_NR)
  1589. co->index = 0;
  1590. uap = amba_ports[co->index];
  1591. if (!uap)
  1592. return -ENODEV;
  1593. /* Allow pins to be muxed in and configured */
  1594. if (!IS_ERR(uap->pins_default)) {
  1595. ret = pinctrl_select_state(uap->pinctrl, uap->pins_default);
  1596. if (ret)
  1597. dev_err(uap->port.dev,
  1598. "could not set default pins\n");
  1599. }
  1600. ret = clk_prepare(uap->clk);
  1601. if (ret)
  1602. return ret;
  1603. if (uap->port.dev->platform_data) {
  1604. struct amba_pl011_data *plat;
  1605. plat = uap->port.dev->platform_data;
  1606. if (plat->init)
  1607. plat->init();
  1608. }
  1609. uap->port.uartclk = clk_get_rate(uap->clk);
  1610. if (options)
  1611. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1612. else
  1613. pl011_console_get_options(uap, &baud, &parity, &bits);
  1614. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  1615. }
  1616. static struct uart_driver amba_reg;
  1617. static struct console amba_console = {
  1618. .name = "ttyAMA",
  1619. .write = pl011_console_write,
  1620. .device = uart_console_device,
  1621. .setup = pl011_console_setup,
  1622. .flags = CON_PRINTBUFFER,
  1623. .index = -1,
  1624. .data = &amba_reg,
  1625. };
  1626. #define AMBA_CONSOLE (&amba_console)
  1627. #else
  1628. #define AMBA_CONSOLE NULL
  1629. #endif
  1630. static struct uart_driver amba_reg = {
  1631. .owner = THIS_MODULE,
  1632. .driver_name = "ttyAMA",
  1633. .dev_name = "ttyAMA",
  1634. .major = SERIAL_AMBA_MAJOR,
  1635. .minor = SERIAL_AMBA_MINOR,
  1636. .nr = UART_NR,
  1637. .cons = AMBA_CONSOLE,
  1638. };
  1639. static int pl011_probe_dt_alias(int index, struct device *dev)
  1640. {
  1641. struct device_node *np;
  1642. static bool seen_dev_with_alias = false;
  1643. static bool seen_dev_without_alias = false;
  1644. int ret = index;
  1645. if (!IS_ENABLED(CONFIG_OF))
  1646. return ret;
  1647. np = dev->of_node;
  1648. if (!np)
  1649. return ret;
  1650. ret = of_alias_get_id(np, "serial");
  1651. if (IS_ERR_VALUE(ret)) {
  1652. seen_dev_without_alias = true;
  1653. ret = index;
  1654. } else {
  1655. seen_dev_with_alias = true;
  1656. if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
  1657. dev_warn(dev, "requested serial port %d not available.\n", ret);
  1658. ret = index;
  1659. }
  1660. }
  1661. if (seen_dev_with_alias && seen_dev_without_alias)
  1662. dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
  1663. return ret;
  1664. }
  1665. static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
  1666. {
  1667. struct uart_amba_port *uap;
  1668. struct vendor_data *vendor = id->data;
  1669. void __iomem *base;
  1670. int i, ret;
  1671. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1672. if (amba_ports[i] == NULL)
  1673. break;
  1674. if (i == ARRAY_SIZE(amba_ports)) {
  1675. ret = -EBUSY;
  1676. goto out;
  1677. }
  1678. uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
  1679. GFP_KERNEL);
  1680. if (uap == NULL) {
  1681. ret = -ENOMEM;
  1682. goto out;
  1683. }
  1684. i = pl011_probe_dt_alias(i, &dev->dev);
  1685. base = devm_ioremap(&dev->dev, dev->res.start,
  1686. resource_size(&dev->res));
  1687. if (!base) {
  1688. ret = -ENOMEM;
  1689. goto out;
  1690. }
  1691. uap->pinctrl = devm_pinctrl_get(&dev->dev);
  1692. if (IS_ERR(uap->pinctrl)) {
  1693. ret = PTR_ERR(uap->pinctrl);
  1694. goto out;
  1695. }
  1696. uap->pins_default = pinctrl_lookup_state(uap->pinctrl,
  1697. PINCTRL_STATE_DEFAULT);
  1698. if (IS_ERR(uap->pins_default))
  1699. dev_err(&dev->dev, "could not get default pinstate\n");
  1700. uap->pins_sleep = pinctrl_lookup_state(uap->pinctrl,
  1701. PINCTRL_STATE_SLEEP);
  1702. if (IS_ERR(uap->pins_sleep))
  1703. dev_dbg(&dev->dev, "could not get sleep pinstate\n");
  1704. uap->clk = devm_clk_get(&dev->dev, NULL);
  1705. if (IS_ERR(uap->clk)) {
  1706. ret = PTR_ERR(uap->clk);
  1707. goto out;
  1708. }
  1709. uap->vendor = vendor;
  1710. uap->lcrh_rx = vendor->lcrh_rx;
  1711. uap->lcrh_tx = vendor->lcrh_tx;
  1712. uap->old_cr = 0;
  1713. uap->fifosize = vendor->fifosize;
  1714. uap->port.dev = &dev->dev;
  1715. uap->port.mapbase = dev->res.start;
  1716. uap->port.membase = base;
  1717. uap->port.iotype = UPIO_MEM;
  1718. uap->port.irq = dev->irq[0];
  1719. uap->port.fifosize = uap->fifosize;
  1720. uap->port.ops = &amba_pl011_pops;
  1721. uap->port.flags = UPF_BOOT_AUTOCONF;
  1722. uap->port.line = i;
  1723. pl011_dma_probe(&dev->dev, uap);
  1724. /* Ensure interrupts from this UART are masked and cleared */
  1725. writew(0, uap->port.membase + UART011_IMSC);
  1726. writew(0xffff, uap->port.membase + UART011_ICR);
  1727. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  1728. amba_ports[i] = uap;
  1729. amba_set_drvdata(dev, uap);
  1730. ret = uart_add_one_port(&amba_reg, &uap->port);
  1731. if (ret) {
  1732. amba_set_drvdata(dev, NULL);
  1733. amba_ports[i] = NULL;
  1734. pl011_dma_remove(uap);
  1735. }
  1736. out:
  1737. return ret;
  1738. }
  1739. static int pl011_remove(struct amba_device *dev)
  1740. {
  1741. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1742. int i;
  1743. amba_set_drvdata(dev, NULL);
  1744. uart_remove_one_port(&amba_reg, &uap->port);
  1745. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1746. if (amba_ports[i] == uap)
  1747. amba_ports[i] = NULL;
  1748. pl011_dma_remove(uap);
  1749. return 0;
  1750. }
  1751. #ifdef CONFIG_PM
  1752. static int pl011_suspend(struct amba_device *dev, pm_message_t state)
  1753. {
  1754. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1755. if (!uap)
  1756. return -EINVAL;
  1757. return uart_suspend_port(&amba_reg, &uap->port);
  1758. }
  1759. static int pl011_resume(struct amba_device *dev)
  1760. {
  1761. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1762. if (!uap)
  1763. return -EINVAL;
  1764. return uart_resume_port(&amba_reg, &uap->port);
  1765. }
  1766. #endif
  1767. static struct amba_id pl011_ids[] = {
  1768. {
  1769. .id = 0x00041011,
  1770. .mask = 0x000fffff,
  1771. .data = &vendor_arm,
  1772. },
  1773. {
  1774. .id = 0x00380802,
  1775. .mask = 0x00ffffff,
  1776. .data = &vendor_st,
  1777. },
  1778. { 0, 0 },
  1779. };
  1780. MODULE_DEVICE_TABLE(amba, pl011_ids);
  1781. static struct amba_driver pl011_driver = {
  1782. .drv = {
  1783. .name = "uart-pl011",
  1784. },
  1785. .id_table = pl011_ids,
  1786. .probe = pl011_probe,
  1787. .remove = pl011_remove,
  1788. #ifdef CONFIG_PM
  1789. .suspend = pl011_suspend,
  1790. .resume = pl011_resume,
  1791. #endif
  1792. };
  1793. static int __init pl011_init(void)
  1794. {
  1795. int ret;
  1796. printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
  1797. ret = uart_register_driver(&amba_reg);
  1798. if (ret == 0) {
  1799. ret = amba_driver_register(&pl011_driver);
  1800. if (ret)
  1801. uart_unregister_driver(&amba_reg);
  1802. }
  1803. return ret;
  1804. }
  1805. static void __exit pl011_exit(void)
  1806. {
  1807. amba_driver_unregister(&pl011_driver);
  1808. uart_unregister_driver(&amba_reg);
  1809. }
  1810. /*
  1811. * While this can be a module, if builtin it's most likely the console
  1812. * So let's leave module_exit but move module_init to an earlier place
  1813. */
  1814. arch_initcall(pl011_init);
  1815. module_exit(pl011_exit);
  1816. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  1817. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  1818. MODULE_LICENSE("GPL");