amba-pl011.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  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_port *port = &uap->port.state->port;
  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(port, sgbuf->buf, pending);
  622. /* Return buffer to device */
  623. dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
  624. uap->port.icount.rx += dma_count;
  625. if (dma_count < pending)
  626. dev_warn(uap->port.dev,
  627. "couldn't insert all characters (TTY is full?)\n");
  628. }
  629. /*
  630. * Only continue with trying to read the FIFO if all DMA chars have
  631. * been taken first.
  632. */
  633. if (dma_count == pending && readfifo) {
  634. /* Clear any error flags */
  635. writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
  636. uap->port.membase + UART011_ICR);
  637. /*
  638. * If we read all the DMA'd characters, and we had an
  639. * incomplete buffer, that could be due to an rx error, or
  640. * maybe we just timed out. Read any pending chars and check
  641. * the error status.
  642. *
  643. * Error conditions will only occur in the FIFO, these will
  644. * trigger an immediate interrupt and stop the DMA job, so we
  645. * will always find the error in the FIFO, never in the DMA
  646. * buffer.
  647. */
  648. fifotaken = pl011_fifo_to_tty(uap);
  649. }
  650. spin_unlock(&uap->port.lock);
  651. dev_vdbg(uap->port.dev,
  652. "Took %d chars from DMA buffer and %d chars from the FIFO\n",
  653. dma_count, fifotaken);
  654. tty_flip_buffer_push(port);
  655. spin_lock(&uap->port.lock);
  656. }
  657. static void pl011_dma_rx_irq(struct uart_amba_port *uap)
  658. {
  659. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  660. struct dma_chan *rxchan = dmarx->chan;
  661. struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
  662. &dmarx->sgbuf_b : &dmarx->sgbuf_a;
  663. size_t pending;
  664. struct dma_tx_state state;
  665. enum dma_status dmastat;
  666. /*
  667. * Pause the transfer so we can trust the current counter,
  668. * do this before we pause the PL011 block, else we may
  669. * overflow the FIFO.
  670. */
  671. if (dmaengine_pause(rxchan))
  672. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  673. dmastat = rxchan->device->device_tx_status(rxchan,
  674. dmarx->cookie, &state);
  675. if (dmastat != DMA_PAUSED)
  676. dev_err(uap->port.dev, "unable to pause DMA transfer\n");
  677. /* Disable RX DMA - incoming data will wait in the FIFO */
  678. uap->dmacr &= ~UART011_RXDMAE;
  679. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  680. uap->dmarx.running = false;
  681. pending = sgbuf->sg.length - state.residue;
  682. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  683. /* Then we terminate the transfer - we now know our residue */
  684. dmaengine_terminate_all(rxchan);
  685. /*
  686. * This will take the chars we have so far and insert
  687. * into the framework.
  688. */
  689. pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
  690. /* Switch buffer & re-trigger DMA job */
  691. dmarx->use_buf_b = !dmarx->use_buf_b;
  692. if (pl011_dma_rx_trigger_dma(uap)) {
  693. dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
  694. "fall back to interrupt mode\n");
  695. uap->im |= UART011_RXIM;
  696. writew(uap->im, uap->port.membase + UART011_IMSC);
  697. }
  698. }
  699. static void pl011_dma_rx_callback(void *data)
  700. {
  701. struct uart_amba_port *uap = data;
  702. struct pl011_dmarx_data *dmarx = &uap->dmarx;
  703. struct dma_chan *rxchan = dmarx->chan;
  704. bool lastbuf = dmarx->use_buf_b;
  705. struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
  706. &dmarx->sgbuf_b : &dmarx->sgbuf_a;
  707. size_t pending;
  708. struct dma_tx_state state;
  709. int ret;
  710. /*
  711. * This completion interrupt occurs typically when the
  712. * RX buffer is totally stuffed but no timeout has yet
  713. * occurred. When that happens, we just want the RX
  714. * routine to flush out the secondary DMA buffer while
  715. * we immediately trigger the next DMA job.
  716. */
  717. spin_lock_irq(&uap->port.lock);
  718. /*
  719. * Rx data can be taken by the UART interrupts during
  720. * the DMA irq handler. So we check the residue here.
  721. */
  722. rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
  723. pending = sgbuf->sg.length - state.residue;
  724. BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
  725. /* Then we terminate the transfer - we now know our residue */
  726. dmaengine_terminate_all(rxchan);
  727. uap->dmarx.running = false;
  728. dmarx->use_buf_b = !lastbuf;
  729. ret = pl011_dma_rx_trigger_dma(uap);
  730. pl011_dma_rx_chars(uap, pending, lastbuf, false);
  731. spin_unlock_irq(&uap->port.lock);
  732. /*
  733. * Do this check after we picked the DMA chars so we don't
  734. * get some IRQ immediately from RX.
  735. */
  736. if (ret) {
  737. dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
  738. "fall back to interrupt mode\n");
  739. uap->im |= UART011_RXIM;
  740. writew(uap->im, uap->port.membase + UART011_IMSC);
  741. }
  742. }
  743. /*
  744. * Stop accepting received characters, when we're shutting down or
  745. * suspending this port.
  746. * Locking: called with port lock held and IRQs disabled.
  747. */
  748. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  749. {
  750. /* FIXME. Just disable the DMA enable */
  751. uap->dmacr &= ~UART011_RXDMAE;
  752. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  753. }
  754. static void pl011_dma_startup(struct uart_amba_port *uap)
  755. {
  756. int ret;
  757. if (!uap->dmatx.chan)
  758. return;
  759. uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
  760. if (!uap->dmatx.buf) {
  761. dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
  762. uap->port.fifosize = uap->fifosize;
  763. return;
  764. }
  765. sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
  766. /* The DMA buffer is now the FIFO the TTY subsystem can use */
  767. uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
  768. uap->using_tx_dma = true;
  769. if (!uap->dmarx.chan)
  770. goto skip_rx;
  771. /* Allocate and map DMA RX buffers */
  772. ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
  773. DMA_FROM_DEVICE);
  774. if (ret) {
  775. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  776. "RX buffer A", ret);
  777. goto skip_rx;
  778. }
  779. ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
  780. DMA_FROM_DEVICE);
  781. if (ret) {
  782. dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
  783. "RX buffer B", ret);
  784. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
  785. DMA_FROM_DEVICE);
  786. goto skip_rx;
  787. }
  788. uap->using_rx_dma = true;
  789. skip_rx:
  790. /* Turn on DMA error (RX/TX will be enabled on demand) */
  791. uap->dmacr |= UART011_DMAONERR;
  792. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  793. /*
  794. * ST Micro variants has some specific dma burst threshold
  795. * compensation. Set this to 16 bytes, so burst will only
  796. * be issued above/below 16 bytes.
  797. */
  798. if (uap->vendor->dma_threshold)
  799. writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
  800. uap->port.membase + ST_UART011_DMAWM);
  801. if (uap->using_rx_dma) {
  802. if (pl011_dma_rx_trigger_dma(uap))
  803. dev_dbg(uap->port.dev, "could not trigger initial "
  804. "RX DMA job, fall back to interrupt mode\n");
  805. }
  806. }
  807. static void pl011_dma_shutdown(struct uart_amba_port *uap)
  808. {
  809. if (!(uap->using_tx_dma || uap->using_rx_dma))
  810. return;
  811. /* Disable RX and TX DMA */
  812. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  813. barrier();
  814. spin_lock_irq(&uap->port.lock);
  815. uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
  816. writew(uap->dmacr, uap->port.membase + UART011_DMACR);
  817. spin_unlock_irq(&uap->port.lock);
  818. if (uap->using_tx_dma) {
  819. /* In theory, this should already be done by pl011_dma_flush_buffer */
  820. dmaengine_terminate_all(uap->dmatx.chan);
  821. if (uap->dmatx.queued) {
  822. dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
  823. DMA_TO_DEVICE);
  824. uap->dmatx.queued = false;
  825. }
  826. kfree(uap->dmatx.buf);
  827. uap->using_tx_dma = false;
  828. }
  829. if (uap->using_rx_dma) {
  830. dmaengine_terminate_all(uap->dmarx.chan);
  831. /* Clean up the RX DMA */
  832. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
  833. pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
  834. uap->using_rx_dma = false;
  835. }
  836. }
  837. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  838. {
  839. return uap->using_rx_dma;
  840. }
  841. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  842. {
  843. return uap->using_rx_dma && uap->dmarx.running;
  844. }
  845. #else
  846. /* Blank functions if the DMA engine is not available */
  847. static inline void pl011_dma_probe(struct uart_amba_port *uap)
  848. {
  849. }
  850. static inline void pl011_dma_remove(struct uart_amba_port *uap)
  851. {
  852. }
  853. static inline void pl011_dma_startup(struct uart_amba_port *uap)
  854. {
  855. }
  856. static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
  857. {
  858. }
  859. static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
  860. {
  861. return false;
  862. }
  863. static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
  864. {
  865. }
  866. static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
  867. {
  868. return false;
  869. }
  870. static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
  871. {
  872. }
  873. static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
  874. {
  875. }
  876. static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
  877. {
  878. return -EIO;
  879. }
  880. static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
  881. {
  882. return false;
  883. }
  884. static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
  885. {
  886. return false;
  887. }
  888. #define pl011_dma_flush_buffer NULL
  889. #endif
  890. static void pl011_stop_tx(struct uart_port *port)
  891. {
  892. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  893. uap->im &= ~UART011_TXIM;
  894. writew(uap->im, uap->port.membase + UART011_IMSC);
  895. pl011_dma_tx_stop(uap);
  896. }
  897. static void pl011_start_tx(struct uart_port *port)
  898. {
  899. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  900. if (!pl011_dma_tx_start(uap)) {
  901. uap->im |= UART011_TXIM;
  902. writew(uap->im, uap->port.membase + UART011_IMSC);
  903. }
  904. }
  905. static void pl011_stop_rx(struct uart_port *port)
  906. {
  907. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  908. uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
  909. UART011_PEIM|UART011_BEIM|UART011_OEIM);
  910. writew(uap->im, uap->port.membase + UART011_IMSC);
  911. pl011_dma_rx_stop(uap);
  912. }
  913. static void pl011_enable_ms(struct uart_port *port)
  914. {
  915. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  916. uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
  917. writew(uap->im, uap->port.membase + UART011_IMSC);
  918. }
  919. static void pl011_rx_chars(struct uart_amba_port *uap)
  920. {
  921. pl011_fifo_to_tty(uap);
  922. spin_unlock(&uap->port.lock);
  923. tty_flip_buffer_push(&uap->port.state->port);
  924. /*
  925. * If we were temporarily out of DMA mode for a while,
  926. * attempt to switch back to DMA mode again.
  927. */
  928. if (pl011_dma_rx_available(uap)) {
  929. if (pl011_dma_rx_trigger_dma(uap)) {
  930. dev_dbg(uap->port.dev, "could not trigger RX DMA job "
  931. "fall back to interrupt mode again\n");
  932. uap->im |= UART011_RXIM;
  933. } else
  934. uap->im &= ~UART011_RXIM;
  935. writew(uap->im, uap->port.membase + UART011_IMSC);
  936. }
  937. spin_lock(&uap->port.lock);
  938. }
  939. static void pl011_tx_chars(struct uart_amba_port *uap)
  940. {
  941. struct circ_buf *xmit = &uap->port.state->xmit;
  942. int count;
  943. if (uap->port.x_char) {
  944. writew(uap->port.x_char, uap->port.membase + UART01x_DR);
  945. uap->port.icount.tx++;
  946. uap->port.x_char = 0;
  947. return;
  948. }
  949. if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
  950. pl011_stop_tx(&uap->port);
  951. return;
  952. }
  953. /* If we are using DMA mode, try to send some characters. */
  954. if (pl011_dma_tx_irq(uap))
  955. return;
  956. count = uap->fifosize >> 1;
  957. do {
  958. writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
  959. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  960. uap->port.icount.tx++;
  961. if (uart_circ_empty(xmit))
  962. break;
  963. } while (--count > 0);
  964. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  965. uart_write_wakeup(&uap->port);
  966. if (uart_circ_empty(xmit))
  967. pl011_stop_tx(&uap->port);
  968. }
  969. static void pl011_modem_status(struct uart_amba_port *uap)
  970. {
  971. unsigned int status, delta;
  972. status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  973. delta = status ^ uap->old_status;
  974. uap->old_status = status;
  975. if (!delta)
  976. return;
  977. if (delta & UART01x_FR_DCD)
  978. uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
  979. if (delta & UART01x_FR_DSR)
  980. uap->port.icount.dsr++;
  981. if (delta & UART01x_FR_CTS)
  982. uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
  983. wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
  984. }
  985. static irqreturn_t pl011_int(int irq, void *dev_id)
  986. {
  987. struct uart_amba_port *uap = dev_id;
  988. unsigned long flags;
  989. unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
  990. int handled = 0;
  991. unsigned int dummy_read;
  992. spin_lock_irqsave(&uap->port.lock, flags);
  993. status = readw(uap->port.membase + UART011_MIS);
  994. if (status) {
  995. do {
  996. if (uap->vendor->cts_event_workaround) {
  997. /* workaround to make sure that all bits are unlocked.. */
  998. writew(0x00, uap->port.membase + UART011_ICR);
  999. /*
  1000. * WA: introduce 26ns(1 uart clk) delay before W1C;
  1001. * single apb access will incur 2 pclk(133.12Mhz) delay,
  1002. * so add 2 dummy reads
  1003. */
  1004. dummy_read = readw(uap->port.membase + UART011_ICR);
  1005. dummy_read = readw(uap->port.membase + UART011_ICR);
  1006. }
  1007. writew(status & ~(UART011_TXIS|UART011_RTIS|
  1008. UART011_RXIS),
  1009. uap->port.membase + UART011_ICR);
  1010. if (status & (UART011_RTIS|UART011_RXIS)) {
  1011. if (pl011_dma_rx_running(uap))
  1012. pl011_dma_rx_irq(uap);
  1013. else
  1014. pl011_rx_chars(uap);
  1015. }
  1016. if (status & (UART011_DSRMIS|UART011_DCDMIS|
  1017. UART011_CTSMIS|UART011_RIMIS))
  1018. pl011_modem_status(uap);
  1019. if (status & UART011_TXIS)
  1020. pl011_tx_chars(uap);
  1021. if (pass_counter-- == 0)
  1022. break;
  1023. status = readw(uap->port.membase + UART011_MIS);
  1024. } while (status != 0);
  1025. handled = 1;
  1026. }
  1027. spin_unlock_irqrestore(&uap->port.lock, flags);
  1028. return IRQ_RETVAL(handled);
  1029. }
  1030. static unsigned int pl011_tx_empty(struct uart_port *port)
  1031. {
  1032. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1033. unsigned int status = readw(uap->port.membase + UART01x_FR);
  1034. return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
  1035. }
  1036. static unsigned int pl011_get_mctrl(struct uart_port *port)
  1037. {
  1038. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1039. unsigned int result = 0;
  1040. unsigned int status = readw(uap->port.membase + UART01x_FR);
  1041. #define TIOCMBIT(uartbit, tiocmbit) \
  1042. if (status & uartbit) \
  1043. result |= tiocmbit
  1044. TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
  1045. TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
  1046. TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
  1047. TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
  1048. #undef TIOCMBIT
  1049. return result;
  1050. }
  1051. static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1052. {
  1053. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1054. unsigned int cr;
  1055. cr = readw(uap->port.membase + UART011_CR);
  1056. #define TIOCMBIT(tiocmbit, uartbit) \
  1057. if (mctrl & tiocmbit) \
  1058. cr |= uartbit; \
  1059. else \
  1060. cr &= ~uartbit
  1061. TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
  1062. TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
  1063. TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
  1064. TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
  1065. TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
  1066. if (uap->autorts) {
  1067. /* We need to disable auto-RTS if we want to turn RTS off */
  1068. TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
  1069. }
  1070. #undef TIOCMBIT
  1071. writew(cr, uap->port.membase + UART011_CR);
  1072. }
  1073. static void pl011_break_ctl(struct uart_port *port, int break_state)
  1074. {
  1075. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1076. unsigned long flags;
  1077. unsigned int lcr_h;
  1078. spin_lock_irqsave(&uap->port.lock, flags);
  1079. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  1080. if (break_state == -1)
  1081. lcr_h |= UART01x_LCRH_BRK;
  1082. else
  1083. lcr_h &= ~UART01x_LCRH_BRK;
  1084. writew(lcr_h, uap->port.membase + uap->lcrh_tx);
  1085. spin_unlock_irqrestore(&uap->port.lock, flags);
  1086. }
  1087. #ifdef CONFIG_CONSOLE_POLL
  1088. static void pl011_quiesce_irqs(struct uart_port *port)
  1089. {
  1090. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1091. unsigned char __iomem *regs = uap->port.membase;
  1092. writew(readw(regs + UART011_MIS), regs + UART011_ICR);
  1093. /*
  1094. * There is no way to clear TXIM as this is "ready to transmit IRQ", so
  1095. * we simply mask it. start_tx() will unmask it.
  1096. *
  1097. * Note we can race with start_tx(), and if the race happens, the
  1098. * polling user might get another interrupt just after we clear it.
  1099. * But it should be OK and can happen even w/o the race, e.g.
  1100. * controller immediately got some new data and raised the IRQ.
  1101. *
  1102. * And whoever uses polling routines assumes that it manages the device
  1103. * (including tx queue), so we're also fine with start_tx()'s caller
  1104. * side.
  1105. */
  1106. writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
  1107. }
  1108. static int pl011_get_poll_char(struct uart_port *port)
  1109. {
  1110. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1111. unsigned int status;
  1112. /*
  1113. * The caller might need IRQs lowered, e.g. if used with KDB NMI
  1114. * debugger.
  1115. */
  1116. pl011_quiesce_irqs(port);
  1117. status = readw(uap->port.membase + UART01x_FR);
  1118. if (status & UART01x_FR_RXFE)
  1119. return NO_POLL_CHAR;
  1120. return readw(uap->port.membase + UART01x_DR);
  1121. }
  1122. static void pl011_put_poll_char(struct uart_port *port,
  1123. unsigned char ch)
  1124. {
  1125. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1126. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1127. barrier();
  1128. writew(ch, uap->port.membase + UART01x_DR);
  1129. }
  1130. #endif /* CONFIG_CONSOLE_POLL */
  1131. static int pl011_hwinit(struct uart_port *port)
  1132. {
  1133. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1134. int retval;
  1135. /* Optionaly enable pins to be muxed in and configured */
  1136. if (!IS_ERR(uap->pins_default)) {
  1137. retval = pinctrl_select_state(uap->pinctrl, uap->pins_default);
  1138. if (retval)
  1139. dev_err(port->dev,
  1140. "could not set default pins\n");
  1141. }
  1142. /*
  1143. * Try to enable the clock producer.
  1144. */
  1145. retval = clk_prepare_enable(uap->clk);
  1146. if (retval)
  1147. goto out;
  1148. uap->port.uartclk = clk_get_rate(uap->clk);
  1149. /* Clear pending error and receive interrupts */
  1150. writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
  1151. UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
  1152. /*
  1153. * Save interrupts enable mask, and enable RX interrupts in case if
  1154. * the interrupt is used for NMI entry.
  1155. */
  1156. uap->im = readw(uap->port.membase + UART011_IMSC);
  1157. writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
  1158. if (uap->port.dev->platform_data) {
  1159. struct amba_pl011_data *plat;
  1160. plat = uap->port.dev->platform_data;
  1161. if (plat->init)
  1162. plat->init();
  1163. }
  1164. return 0;
  1165. out:
  1166. return retval;
  1167. }
  1168. static int pl011_startup(struct uart_port *port)
  1169. {
  1170. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1171. unsigned int cr;
  1172. int retval;
  1173. retval = pl011_hwinit(port);
  1174. if (retval)
  1175. goto clk_dis;
  1176. writew(uap->im, uap->port.membase + UART011_IMSC);
  1177. /*
  1178. * Allocate the IRQ
  1179. */
  1180. retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
  1181. if (retval)
  1182. goto clk_dis;
  1183. writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
  1184. /*
  1185. * Provoke TX FIFO interrupt into asserting.
  1186. */
  1187. cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
  1188. writew(cr, uap->port.membase + UART011_CR);
  1189. writew(0, uap->port.membase + UART011_FBRD);
  1190. writew(1, uap->port.membase + UART011_IBRD);
  1191. writew(0, uap->port.membase + uap->lcrh_rx);
  1192. if (uap->lcrh_tx != uap->lcrh_rx) {
  1193. int i;
  1194. /*
  1195. * Wait 10 PCLKs before writing LCRH_TX register,
  1196. * to get this delay write read only register 10 times
  1197. */
  1198. for (i = 0; i < 10; ++i)
  1199. writew(0xff, uap->port.membase + UART011_MIS);
  1200. writew(0, uap->port.membase + uap->lcrh_tx);
  1201. }
  1202. writew(0, uap->port.membase + UART01x_DR);
  1203. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  1204. barrier();
  1205. /* restore RTS and DTR */
  1206. cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
  1207. cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  1208. writew(cr, uap->port.membase + UART011_CR);
  1209. /*
  1210. * initialise the old status of the modem signals
  1211. */
  1212. uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  1213. /* Startup DMA */
  1214. pl011_dma_startup(uap);
  1215. /*
  1216. * Finally, enable interrupts, only timeouts when using DMA
  1217. * if initial RX DMA job failed, start in interrupt mode
  1218. * as well.
  1219. */
  1220. spin_lock_irq(&uap->port.lock);
  1221. /* Clear out any spuriously appearing RX interrupts */
  1222. writew(UART011_RTIS | UART011_RXIS,
  1223. uap->port.membase + UART011_ICR);
  1224. uap->im = UART011_RTIM;
  1225. if (!pl011_dma_rx_running(uap))
  1226. uap->im |= UART011_RXIM;
  1227. writew(uap->im, uap->port.membase + UART011_IMSC);
  1228. spin_unlock_irq(&uap->port.lock);
  1229. return 0;
  1230. clk_dis:
  1231. clk_disable_unprepare(uap->clk);
  1232. return retval;
  1233. }
  1234. static void pl011_shutdown_channel(struct uart_amba_port *uap,
  1235. unsigned int lcrh)
  1236. {
  1237. unsigned long val;
  1238. val = readw(uap->port.membase + lcrh);
  1239. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1240. writew(val, uap->port.membase + lcrh);
  1241. }
  1242. static void pl011_shutdown(struct uart_port *port)
  1243. {
  1244. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1245. unsigned int cr;
  1246. int retval;
  1247. /*
  1248. * disable all interrupts
  1249. */
  1250. spin_lock_irq(&uap->port.lock);
  1251. uap->im = 0;
  1252. writew(uap->im, uap->port.membase + UART011_IMSC);
  1253. writew(0xffff, uap->port.membase + UART011_ICR);
  1254. spin_unlock_irq(&uap->port.lock);
  1255. pl011_dma_shutdown(uap);
  1256. /*
  1257. * Free the interrupt
  1258. */
  1259. free_irq(uap->port.irq, uap);
  1260. /*
  1261. * disable the port
  1262. * disable the port. It should not disable RTS and DTR.
  1263. * Also RTS and DTR state should be preserved to restore
  1264. * it during startup().
  1265. */
  1266. uap->autorts = false;
  1267. cr = readw(uap->port.membase + UART011_CR);
  1268. uap->old_cr = cr;
  1269. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1270. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1271. writew(cr, uap->port.membase + UART011_CR);
  1272. /*
  1273. * disable break condition and fifos
  1274. */
  1275. pl011_shutdown_channel(uap, uap->lcrh_rx);
  1276. if (uap->lcrh_rx != uap->lcrh_tx)
  1277. pl011_shutdown_channel(uap, uap->lcrh_tx);
  1278. /*
  1279. * Shut down the clock producer
  1280. */
  1281. clk_disable_unprepare(uap->clk);
  1282. /* Optionally let pins go into sleep states */
  1283. if (!IS_ERR(uap->pins_sleep)) {
  1284. retval = pinctrl_select_state(uap->pinctrl, uap->pins_sleep);
  1285. if (retval)
  1286. dev_err(port->dev,
  1287. "could not set pins to sleep state\n");
  1288. }
  1289. if (uap->port.dev->platform_data) {
  1290. struct amba_pl011_data *plat;
  1291. plat = uap->port.dev->platform_data;
  1292. if (plat->exit)
  1293. plat->exit();
  1294. }
  1295. }
  1296. static void
  1297. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1298. struct ktermios *old)
  1299. {
  1300. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1301. unsigned int lcr_h, old_cr;
  1302. unsigned long flags;
  1303. unsigned int baud, quot, clkdiv;
  1304. if (uap->vendor->oversampling)
  1305. clkdiv = 8;
  1306. else
  1307. clkdiv = 16;
  1308. /*
  1309. * Ask the core to calculate the divisor for us.
  1310. */
  1311. baud = uart_get_baud_rate(port, termios, old, 0,
  1312. port->uartclk / clkdiv);
  1313. if (baud > port->uartclk/16)
  1314. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1315. else
  1316. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1317. switch (termios->c_cflag & CSIZE) {
  1318. case CS5:
  1319. lcr_h = UART01x_LCRH_WLEN_5;
  1320. break;
  1321. case CS6:
  1322. lcr_h = UART01x_LCRH_WLEN_6;
  1323. break;
  1324. case CS7:
  1325. lcr_h = UART01x_LCRH_WLEN_7;
  1326. break;
  1327. default: // CS8
  1328. lcr_h = UART01x_LCRH_WLEN_8;
  1329. break;
  1330. }
  1331. if (termios->c_cflag & CSTOPB)
  1332. lcr_h |= UART01x_LCRH_STP2;
  1333. if (termios->c_cflag & PARENB) {
  1334. lcr_h |= UART01x_LCRH_PEN;
  1335. if (!(termios->c_cflag & PARODD))
  1336. lcr_h |= UART01x_LCRH_EPS;
  1337. }
  1338. if (uap->fifosize > 1)
  1339. lcr_h |= UART01x_LCRH_FEN;
  1340. spin_lock_irqsave(&port->lock, flags);
  1341. /*
  1342. * Update the per-port timeout.
  1343. */
  1344. uart_update_timeout(port, termios->c_cflag, baud);
  1345. port->read_status_mask = UART011_DR_OE | 255;
  1346. if (termios->c_iflag & INPCK)
  1347. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1348. if (termios->c_iflag & (BRKINT | PARMRK))
  1349. port->read_status_mask |= UART011_DR_BE;
  1350. /*
  1351. * Characters to ignore
  1352. */
  1353. port->ignore_status_mask = 0;
  1354. if (termios->c_iflag & IGNPAR)
  1355. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1356. if (termios->c_iflag & IGNBRK) {
  1357. port->ignore_status_mask |= UART011_DR_BE;
  1358. /*
  1359. * If we're ignoring parity and break indicators,
  1360. * ignore overruns too (for real raw support).
  1361. */
  1362. if (termios->c_iflag & IGNPAR)
  1363. port->ignore_status_mask |= UART011_DR_OE;
  1364. }
  1365. /*
  1366. * Ignore all characters if CREAD is not set.
  1367. */
  1368. if ((termios->c_cflag & CREAD) == 0)
  1369. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1370. if (UART_ENABLE_MS(port, termios->c_cflag))
  1371. pl011_enable_ms(port);
  1372. /* first, disable everything */
  1373. old_cr = readw(port->membase + UART011_CR);
  1374. writew(0, port->membase + UART011_CR);
  1375. if (termios->c_cflag & CRTSCTS) {
  1376. if (old_cr & UART011_CR_RTS)
  1377. old_cr |= UART011_CR_RTSEN;
  1378. old_cr |= UART011_CR_CTSEN;
  1379. uap->autorts = true;
  1380. } else {
  1381. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1382. uap->autorts = false;
  1383. }
  1384. if (uap->vendor->oversampling) {
  1385. if (baud > port->uartclk / 16)
  1386. old_cr |= ST_UART011_CR_OVSFACT;
  1387. else
  1388. old_cr &= ~ST_UART011_CR_OVSFACT;
  1389. }
  1390. /*
  1391. * Workaround for the ST Micro oversampling variants to
  1392. * increase the bitrate slightly, by lowering the divisor,
  1393. * to avoid delayed sampling of start bit at high speeds,
  1394. * else we see data corruption.
  1395. */
  1396. if (uap->vendor->oversampling) {
  1397. if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
  1398. quot -= 1;
  1399. else if ((baud > 3250000) && (quot > 2))
  1400. quot -= 2;
  1401. }
  1402. /* Set baud rate */
  1403. writew(quot & 0x3f, port->membase + UART011_FBRD);
  1404. writew(quot >> 6, port->membase + UART011_IBRD);
  1405. /*
  1406. * ----------v----------v----------v----------v-----
  1407. * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
  1408. * UART011_FBRD & UART011_IBRD.
  1409. * ----------^----------^----------^----------^-----
  1410. */
  1411. writew(lcr_h, port->membase + uap->lcrh_rx);
  1412. if (uap->lcrh_rx != uap->lcrh_tx) {
  1413. int i;
  1414. /*
  1415. * Wait 10 PCLKs before writing LCRH_TX register,
  1416. * to get this delay write read only register 10 times
  1417. */
  1418. for (i = 0; i < 10; ++i)
  1419. writew(0xff, uap->port.membase + UART011_MIS);
  1420. writew(lcr_h, port->membase + uap->lcrh_tx);
  1421. }
  1422. writew(old_cr, port->membase + UART011_CR);
  1423. spin_unlock_irqrestore(&port->lock, flags);
  1424. }
  1425. static const char *pl011_type(struct uart_port *port)
  1426. {
  1427. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1428. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1429. }
  1430. /*
  1431. * Release the memory region(s) being used by 'port'
  1432. */
  1433. static void pl011_release_port(struct uart_port *port)
  1434. {
  1435. release_mem_region(port->mapbase, SZ_4K);
  1436. }
  1437. /*
  1438. * Request the memory region(s) being used by 'port'
  1439. */
  1440. static int pl011_request_port(struct uart_port *port)
  1441. {
  1442. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  1443. != NULL ? 0 : -EBUSY;
  1444. }
  1445. /*
  1446. * Configure/autoconfigure the port.
  1447. */
  1448. static void pl011_config_port(struct uart_port *port, int flags)
  1449. {
  1450. if (flags & UART_CONFIG_TYPE) {
  1451. port->type = PORT_AMBA;
  1452. pl011_request_port(port);
  1453. }
  1454. }
  1455. /*
  1456. * verify the new serial_struct (for TIOCSSERIAL).
  1457. */
  1458. static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
  1459. {
  1460. int ret = 0;
  1461. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1462. ret = -EINVAL;
  1463. if (ser->irq < 0 || ser->irq >= nr_irqs)
  1464. ret = -EINVAL;
  1465. if (ser->baud_base < 9600)
  1466. ret = -EINVAL;
  1467. return ret;
  1468. }
  1469. static struct uart_ops amba_pl011_pops = {
  1470. .tx_empty = pl011_tx_empty,
  1471. .set_mctrl = pl011_set_mctrl,
  1472. .get_mctrl = pl011_get_mctrl,
  1473. .stop_tx = pl011_stop_tx,
  1474. .start_tx = pl011_start_tx,
  1475. .stop_rx = pl011_stop_rx,
  1476. .enable_ms = pl011_enable_ms,
  1477. .break_ctl = pl011_break_ctl,
  1478. .startup = pl011_startup,
  1479. .shutdown = pl011_shutdown,
  1480. .flush_buffer = pl011_dma_flush_buffer,
  1481. .set_termios = pl011_set_termios,
  1482. .type = pl011_type,
  1483. .release_port = pl011_release_port,
  1484. .request_port = pl011_request_port,
  1485. .config_port = pl011_config_port,
  1486. .verify_port = pl011_verify_port,
  1487. #ifdef CONFIG_CONSOLE_POLL
  1488. .poll_init = pl011_hwinit,
  1489. .poll_get_char = pl011_get_poll_char,
  1490. .poll_put_char = pl011_put_poll_char,
  1491. #endif
  1492. };
  1493. static struct uart_amba_port *amba_ports[UART_NR];
  1494. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1495. static void pl011_console_putchar(struct uart_port *port, int ch)
  1496. {
  1497. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1498. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1499. barrier();
  1500. writew(ch, uap->port.membase + UART01x_DR);
  1501. }
  1502. static void
  1503. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1504. {
  1505. struct uart_amba_port *uap = amba_ports[co->index];
  1506. unsigned int status, old_cr, new_cr;
  1507. unsigned long flags;
  1508. int locked = 1;
  1509. clk_enable(uap->clk);
  1510. local_irq_save(flags);
  1511. if (uap->port.sysrq)
  1512. locked = 0;
  1513. else if (oops_in_progress)
  1514. locked = spin_trylock(&uap->port.lock);
  1515. else
  1516. spin_lock(&uap->port.lock);
  1517. /*
  1518. * First save the CR then disable the interrupts
  1519. */
  1520. old_cr = readw(uap->port.membase + UART011_CR);
  1521. new_cr = old_cr & ~UART011_CR_CTSEN;
  1522. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1523. writew(new_cr, uap->port.membase + UART011_CR);
  1524. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1525. /*
  1526. * Finally, wait for transmitter to become empty
  1527. * and restore the TCR
  1528. */
  1529. do {
  1530. status = readw(uap->port.membase + UART01x_FR);
  1531. } while (status & UART01x_FR_BUSY);
  1532. writew(old_cr, uap->port.membase + UART011_CR);
  1533. if (locked)
  1534. spin_unlock(&uap->port.lock);
  1535. local_irq_restore(flags);
  1536. clk_disable(uap->clk);
  1537. }
  1538. static void __init
  1539. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1540. int *parity, int *bits)
  1541. {
  1542. if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
  1543. unsigned int lcr_h, ibrd, fbrd;
  1544. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  1545. *parity = 'n';
  1546. if (lcr_h & UART01x_LCRH_PEN) {
  1547. if (lcr_h & UART01x_LCRH_EPS)
  1548. *parity = 'e';
  1549. else
  1550. *parity = 'o';
  1551. }
  1552. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  1553. *bits = 7;
  1554. else
  1555. *bits = 8;
  1556. ibrd = readw(uap->port.membase + UART011_IBRD);
  1557. fbrd = readw(uap->port.membase + UART011_FBRD);
  1558. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  1559. if (uap->vendor->oversampling) {
  1560. if (readw(uap->port.membase + UART011_CR)
  1561. & ST_UART011_CR_OVSFACT)
  1562. *baud *= 2;
  1563. }
  1564. }
  1565. }
  1566. static int __init pl011_console_setup(struct console *co, char *options)
  1567. {
  1568. struct uart_amba_port *uap;
  1569. int baud = 38400;
  1570. int bits = 8;
  1571. int parity = 'n';
  1572. int flow = 'n';
  1573. int ret;
  1574. /*
  1575. * Check whether an invalid uart number has been specified, and
  1576. * if so, search for the first available port that does have
  1577. * console support.
  1578. */
  1579. if (co->index >= UART_NR)
  1580. co->index = 0;
  1581. uap = amba_ports[co->index];
  1582. if (!uap)
  1583. return -ENODEV;
  1584. /* Allow pins to be muxed in and configured */
  1585. if (!IS_ERR(uap->pins_default)) {
  1586. ret = pinctrl_select_state(uap->pinctrl, uap->pins_default);
  1587. if (ret)
  1588. dev_err(uap->port.dev,
  1589. "could not set default pins\n");
  1590. }
  1591. ret = clk_prepare(uap->clk);
  1592. if (ret)
  1593. return ret;
  1594. if (uap->port.dev->platform_data) {
  1595. struct amba_pl011_data *plat;
  1596. plat = uap->port.dev->platform_data;
  1597. if (plat->init)
  1598. plat->init();
  1599. }
  1600. uap->port.uartclk = clk_get_rate(uap->clk);
  1601. if (options)
  1602. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1603. else
  1604. pl011_console_get_options(uap, &baud, &parity, &bits);
  1605. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  1606. }
  1607. static struct uart_driver amba_reg;
  1608. static struct console amba_console = {
  1609. .name = "ttyAMA",
  1610. .write = pl011_console_write,
  1611. .device = uart_console_device,
  1612. .setup = pl011_console_setup,
  1613. .flags = CON_PRINTBUFFER,
  1614. .index = -1,
  1615. .data = &amba_reg,
  1616. };
  1617. #define AMBA_CONSOLE (&amba_console)
  1618. #else
  1619. #define AMBA_CONSOLE NULL
  1620. #endif
  1621. static struct uart_driver amba_reg = {
  1622. .owner = THIS_MODULE,
  1623. .driver_name = "ttyAMA",
  1624. .dev_name = "ttyAMA",
  1625. .major = SERIAL_AMBA_MAJOR,
  1626. .minor = SERIAL_AMBA_MINOR,
  1627. .nr = UART_NR,
  1628. .cons = AMBA_CONSOLE,
  1629. };
  1630. static int pl011_probe_dt_alias(int index, struct device *dev)
  1631. {
  1632. struct device_node *np;
  1633. static bool seen_dev_with_alias = false;
  1634. static bool seen_dev_without_alias = false;
  1635. int ret = index;
  1636. if (!IS_ENABLED(CONFIG_OF))
  1637. return ret;
  1638. np = dev->of_node;
  1639. if (!np)
  1640. return ret;
  1641. ret = of_alias_get_id(np, "serial");
  1642. if (IS_ERR_VALUE(ret)) {
  1643. seen_dev_without_alias = true;
  1644. ret = index;
  1645. } else {
  1646. seen_dev_with_alias = true;
  1647. if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
  1648. dev_warn(dev, "requested serial port %d not available.\n", ret);
  1649. ret = index;
  1650. }
  1651. }
  1652. if (seen_dev_with_alias && seen_dev_without_alias)
  1653. dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
  1654. return ret;
  1655. }
  1656. static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
  1657. {
  1658. struct uart_amba_port *uap;
  1659. struct vendor_data *vendor = id->data;
  1660. void __iomem *base;
  1661. int i, ret;
  1662. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1663. if (amba_ports[i] == NULL)
  1664. break;
  1665. if (i == ARRAY_SIZE(amba_ports)) {
  1666. ret = -EBUSY;
  1667. goto out;
  1668. }
  1669. uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
  1670. GFP_KERNEL);
  1671. if (uap == NULL) {
  1672. ret = -ENOMEM;
  1673. goto out;
  1674. }
  1675. i = pl011_probe_dt_alias(i, &dev->dev);
  1676. base = devm_ioremap(&dev->dev, dev->res.start,
  1677. resource_size(&dev->res));
  1678. if (!base) {
  1679. ret = -ENOMEM;
  1680. goto out;
  1681. }
  1682. uap->pinctrl = devm_pinctrl_get(&dev->dev);
  1683. if (IS_ERR(uap->pinctrl)) {
  1684. ret = PTR_ERR(uap->pinctrl);
  1685. goto out;
  1686. }
  1687. uap->pins_default = pinctrl_lookup_state(uap->pinctrl,
  1688. PINCTRL_STATE_DEFAULT);
  1689. if (IS_ERR(uap->pins_default))
  1690. dev_err(&dev->dev, "could not get default pinstate\n");
  1691. uap->pins_sleep = pinctrl_lookup_state(uap->pinctrl,
  1692. PINCTRL_STATE_SLEEP);
  1693. if (IS_ERR(uap->pins_sleep))
  1694. dev_dbg(&dev->dev, "could not get sleep pinstate\n");
  1695. uap->clk = devm_clk_get(&dev->dev, NULL);
  1696. if (IS_ERR(uap->clk)) {
  1697. ret = PTR_ERR(uap->clk);
  1698. goto out;
  1699. }
  1700. uap->vendor = vendor;
  1701. uap->lcrh_rx = vendor->lcrh_rx;
  1702. uap->lcrh_tx = vendor->lcrh_tx;
  1703. uap->old_cr = 0;
  1704. uap->fifosize = vendor->fifosize;
  1705. uap->port.dev = &dev->dev;
  1706. uap->port.mapbase = dev->res.start;
  1707. uap->port.membase = base;
  1708. uap->port.iotype = UPIO_MEM;
  1709. uap->port.irq = dev->irq[0];
  1710. uap->port.fifosize = uap->fifosize;
  1711. uap->port.ops = &amba_pl011_pops;
  1712. uap->port.flags = UPF_BOOT_AUTOCONF;
  1713. uap->port.line = i;
  1714. pl011_dma_probe(uap);
  1715. /* Ensure interrupts from this UART are masked and cleared */
  1716. writew(0, uap->port.membase + UART011_IMSC);
  1717. writew(0xffff, uap->port.membase + UART011_ICR);
  1718. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  1719. amba_ports[i] = uap;
  1720. amba_set_drvdata(dev, uap);
  1721. ret = uart_add_one_port(&amba_reg, &uap->port);
  1722. if (ret) {
  1723. amba_set_drvdata(dev, NULL);
  1724. amba_ports[i] = NULL;
  1725. pl011_dma_remove(uap);
  1726. }
  1727. out:
  1728. return ret;
  1729. }
  1730. static int pl011_remove(struct amba_device *dev)
  1731. {
  1732. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1733. int i;
  1734. amba_set_drvdata(dev, NULL);
  1735. uart_remove_one_port(&amba_reg, &uap->port);
  1736. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1737. if (amba_ports[i] == uap)
  1738. amba_ports[i] = NULL;
  1739. pl011_dma_remove(uap);
  1740. return 0;
  1741. }
  1742. #ifdef CONFIG_PM
  1743. static int pl011_suspend(struct amba_device *dev, pm_message_t state)
  1744. {
  1745. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1746. if (!uap)
  1747. return -EINVAL;
  1748. return uart_suspend_port(&amba_reg, &uap->port);
  1749. }
  1750. static int pl011_resume(struct amba_device *dev)
  1751. {
  1752. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1753. if (!uap)
  1754. return -EINVAL;
  1755. return uart_resume_port(&amba_reg, &uap->port);
  1756. }
  1757. #endif
  1758. static struct amba_id pl011_ids[] = {
  1759. {
  1760. .id = 0x00041011,
  1761. .mask = 0x000fffff,
  1762. .data = &vendor_arm,
  1763. },
  1764. {
  1765. .id = 0x00380802,
  1766. .mask = 0x00ffffff,
  1767. .data = &vendor_st,
  1768. },
  1769. { 0, 0 },
  1770. };
  1771. MODULE_DEVICE_TABLE(amba, pl011_ids);
  1772. static struct amba_driver pl011_driver = {
  1773. .drv = {
  1774. .name = "uart-pl011",
  1775. },
  1776. .id_table = pl011_ids,
  1777. .probe = pl011_probe,
  1778. .remove = pl011_remove,
  1779. #ifdef CONFIG_PM
  1780. .suspend = pl011_suspend,
  1781. .resume = pl011_resume,
  1782. #endif
  1783. };
  1784. static int __init pl011_init(void)
  1785. {
  1786. int ret;
  1787. printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
  1788. ret = uart_register_driver(&amba_reg);
  1789. if (ret == 0) {
  1790. ret = amba_driver_register(&pl011_driver);
  1791. if (ret)
  1792. uart_unregister_driver(&amba_reg);
  1793. }
  1794. return ret;
  1795. }
  1796. static void __exit pl011_exit(void)
  1797. {
  1798. amba_driver_unregister(&pl011_driver);
  1799. uart_unregister_driver(&amba_reg);
  1800. }
  1801. /*
  1802. * While this can be a module, if builtin it's most likely the console
  1803. * So let's leave module_exit but move module_init to an earlier place
  1804. */
  1805. arch_initcall(pl011_init);
  1806. module_exit(pl011_exit);
  1807. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  1808. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  1809. MODULE_LICENSE("GPL");