amba-pl011.c 54 KB

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