amba-pl011.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  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 <asm/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 int pl011_get_poll_char(struct uart_port *port)
  1091. {
  1092. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1093. unsigned int status;
  1094. status = readw(uap->port.membase + UART01x_FR);
  1095. if (status & UART01x_FR_RXFE)
  1096. return NO_POLL_CHAR;
  1097. return readw(uap->port.membase + UART01x_DR);
  1098. }
  1099. static void pl011_put_poll_char(struct uart_port *port,
  1100. unsigned char ch)
  1101. {
  1102. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1103. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1104. barrier();
  1105. writew(ch, uap->port.membase + UART01x_DR);
  1106. }
  1107. #endif /* CONFIG_CONSOLE_POLL */
  1108. static int pl011_startup(struct uart_port *port)
  1109. {
  1110. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1111. unsigned int cr;
  1112. int retval;
  1113. /* Optionaly enable pins to be muxed in and configured */
  1114. if (!IS_ERR(uap->pins_default)) {
  1115. retval = pinctrl_select_state(uap->pinctrl, uap->pins_default);
  1116. if (retval)
  1117. dev_err(port->dev,
  1118. "could not set default pins\n");
  1119. }
  1120. /*
  1121. * Try to enable the clock producer.
  1122. */
  1123. retval = clk_prepare_enable(uap->clk);
  1124. if (retval)
  1125. goto out;
  1126. uap->port.uartclk = clk_get_rate(uap->clk);
  1127. /* Clear pending error and receive interrupts */
  1128. writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
  1129. UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
  1130. /*
  1131. * Allocate the IRQ
  1132. */
  1133. retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
  1134. if (retval)
  1135. goto clk_dis;
  1136. writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
  1137. /*
  1138. * Provoke TX FIFO interrupt into asserting.
  1139. */
  1140. cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
  1141. writew(cr, uap->port.membase + UART011_CR);
  1142. writew(0, uap->port.membase + UART011_FBRD);
  1143. writew(1, uap->port.membase + UART011_IBRD);
  1144. writew(0, uap->port.membase + uap->lcrh_rx);
  1145. if (uap->lcrh_tx != uap->lcrh_rx) {
  1146. int i;
  1147. /*
  1148. * Wait 10 PCLKs before writing LCRH_TX register,
  1149. * to get this delay write read only register 10 times
  1150. */
  1151. for (i = 0; i < 10; ++i)
  1152. writew(0xff, uap->port.membase + UART011_MIS);
  1153. writew(0, uap->port.membase + uap->lcrh_tx);
  1154. }
  1155. writew(0, uap->port.membase + UART01x_DR);
  1156. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
  1157. barrier();
  1158. /* restore RTS and DTR */
  1159. cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
  1160. cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
  1161. writew(cr, uap->port.membase + UART011_CR);
  1162. /*
  1163. * initialise the old status of the modem signals
  1164. */
  1165. uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
  1166. /* Startup DMA */
  1167. pl011_dma_startup(uap);
  1168. /*
  1169. * Finally, enable interrupts, only timeouts when using DMA
  1170. * if initial RX DMA job failed, start in interrupt mode
  1171. * as well.
  1172. */
  1173. spin_lock_irq(&uap->port.lock);
  1174. /* Clear out any spuriously appearing RX interrupts */
  1175. writew(UART011_RTIS | UART011_RXIS,
  1176. uap->port.membase + UART011_ICR);
  1177. uap->im = UART011_RTIM;
  1178. if (!pl011_dma_rx_running(uap))
  1179. uap->im |= UART011_RXIM;
  1180. writew(uap->im, uap->port.membase + UART011_IMSC);
  1181. spin_unlock_irq(&uap->port.lock);
  1182. if (uap->port.dev->platform_data) {
  1183. struct amba_pl011_data *plat;
  1184. plat = uap->port.dev->platform_data;
  1185. if (plat->init)
  1186. plat->init();
  1187. }
  1188. return 0;
  1189. clk_dis:
  1190. clk_disable_unprepare(uap->clk);
  1191. out:
  1192. return retval;
  1193. }
  1194. static void pl011_shutdown_channel(struct uart_amba_port *uap,
  1195. unsigned int lcrh)
  1196. {
  1197. unsigned long val;
  1198. val = readw(uap->port.membase + lcrh);
  1199. val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
  1200. writew(val, uap->port.membase + lcrh);
  1201. }
  1202. static void pl011_shutdown(struct uart_port *port)
  1203. {
  1204. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1205. unsigned int cr;
  1206. int retval;
  1207. /*
  1208. * disable all interrupts
  1209. */
  1210. spin_lock_irq(&uap->port.lock);
  1211. uap->im = 0;
  1212. writew(uap->im, uap->port.membase + UART011_IMSC);
  1213. writew(0xffff, uap->port.membase + UART011_ICR);
  1214. spin_unlock_irq(&uap->port.lock);
  1215. pl011_dma_shutdown(uap);
  1216. /*
  1217. * Free the interrupt
  1218. */
  1219. free_irq(uap->port.irq, uap);
  1220. /*
  1221. * disable the port
  1222. * disable the port. It should not disable RTS and DTR.
  1223. * Also RTS and DTR state should be preserved to restore
  1224. * it during startup().
  1225. */
  1226. uap->autorts = false;
  1227. cr = readw(uap->port.membase + UART011_CR);
  1228. uap->old_cr = cr;
  1229. cr &= UART011_CR_RTS | UART011_CR_DTR;
  1230. cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1231. writew(cr, uap->port.membase + UART011_CR);
  1232. /*
  1233. * disable break condition and fifos
  1234. */
  1235. pl011_shutdown_channel(uap, uap->lcrh_rx);
  1236. if (uap->lcrh_rx != uap->lcrh_tx)
  1237. pl011_shutdown_channel(uap, uap->lcrh_tx);
  1238. /*
  1239. * Shut down the clock producer
  1240. */
  1241. clk_disable_unprepare(uap->clk);
  1242. /* Optionally let pins go into sleep states */
  1243. if (!IS_ERR(uap->pins_sleep)) {
  1244. retval = pinctrl_select_state(uap->pinctrl, uap->pins_sleep);
  1245. if (retval)
  1246. dev_err(port->dev,
  1247. "could not set pins to sleep state\n");
  1248. }
  1249. if (uap->port.dev->platform_data) {
  1250. struct amba_pl011_data *plat;
  1251. plat = uap->port.dev->platform_data;
  1252. if (plat->exit)
  1253. plat->exit();
  1254. }
  1255. }
  1256. static void
  1257. pl011_set_termios(struct uart_port *port, struct ktermios *termios,
  1258. struct ktermios *old)
  1259. {
  1260. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1261. unsigned int lcr_h, old_cr;
  1262. unsigned long flags;
  1263. unsigned int baud, quot, clkdiv;
  1264. if (uap->vendor->oversampling)
  1265. clkdiv = 8;
  1266. else
  1267. clkdiv = 16;
  1268. /*
  1269. * Ask the core to calculate the divisor for us.
  1270. */
  1271. baud = uart_get_baud_rate(port, termios, old, 0,
  1272. port->uartclk / clkdiv);
  1273. if (baud > port->uartclk/16)
  1274. quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
  1275. else
  1276. quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
  1277. switch (termios->c_cflag & CSIZE) {
  1278. case CS5:
  1279. lcr_h = UART01x_LCRH_WLEN_5;
  1280. break;
  1281. case CS6:
  1282. lcr_h = UART01x_LCRH_WLEN_6;
  1283. break;
  1284. case CS7:
  1285. lcr_h = UART01x_LCRH_WLEN_7;
  1286. break;
  1287. default: // CS8
  1288. lcr_h = UART01x_LCRH_WLEN_8;
  1289. break;
  1290. }
  1291. if (termios->c_cflag & CSTOPB)
  1292. lcr_h |= UART01x_LCRH_STP2;
  1293. if (termios->c_cflag & PARENB) {
  1294. lcr_h |= UART01x_LCRH_PEN;
  1295. if (!(termios->c_cflag & PARODD))
  1296. lcr_h |= UART01x_LCRH_EPS;
  1297. }
  1298. if (uap->fifosize > 1)
  1299. lcr_h |= UART01x_LCRH_FEN;
  1300. spin_lock_irqsave(&port->lock, flags);
  1301. /*
  1302. * Update the per-port timeout.
  1303. */
  1304. uart_update_timeout(port, termios->c_cflag, baud);
  1305. port->read_status_mask = UART011_DR_OE | 255;
  1306. if (termios->c_iflag & INPCK)
  1307. port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1308. if (termios->c_iflag & (BRKINT | PARMRK))
  1309. port->read_status_mask |= UART011_DR_BE;
  1310. /*
  1311. * Characters to ignore
  1312. */
  1313. port->ignore_status_mask = 0;
  1314. if (termios->c_iflag & IGNPAR)
  1315. port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
  1316. if (termios->c_iflag & IGNBRK) {
  1317. port->ignore_status_mask |= UART011_DR_BE;
  1318. /*
  1319. * If we're ignoring parity and break indicators,
  1320. * ignore overruns too (for real raw support).
  1321. */
  1322. if (termios->c_iflag & IGNPAR)
  1323. port->ignore_status_mask |= UART011_DR_OE;
  1324. }
  1325. /*
  1326. * Ignore all characters if CREAD is not set.
  1327. */
  1328. if ((termios->c_cflag & CREAD) == 0)
  1329. port->ignore_status_mask |= UART_DUMMY_DR_RX;
  1330. if (UART_ENABLE_MS(port, termios->c_cflag))
  1331. pl011_enable_ms(port);
  1332. /* first, disable everything */
  1333. old_cr = readw(port->membase + UART011_CR);
  1334. writew(0, port->membase + UART011_CR);
  1335. if (termios->c_cflag & CRTSCTS) {
  1336. if (old_cr & UART011_CR_RTS)
  1337. old_cr |= UART011_CR_RTSEN;
  1338. old_cr |= UART011_CR_CTSEN;
  1339. uap->autorts = true;
  1340. } else {
  1341. old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
  1342. uap->autorts = false;
  1343. }
  1344. if (uap->vendor->oversampling) {
  1345. if (baud > port->uartclk / 16)
  1346. old_cr |= ST_UART011_CR_OVSFACT;
  1347. else
  1348. old_cr &= ~ST_UART011_CR_OVSFACT;
  1349. }
  1350. /* Set baud rate */
  1351. writew(quot & 0x3f, port->membase + UART011_FBRD);
  1352. writew(quot >> 6, port->membase + UART011_IBRD);
  1353. /*
  1354. * ----------v----------v----------v----------v-----
  1355. * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
  1356. * ----------^----------^----------^----------^-----
  1357. */
  1358. writew(lcr_h, port->membase + uap->lcrh_rx);
  1359. if (uap->lcrh_rx != uap->lcrh_tx) {
  1360. int i;
  1361. /*
  1362. * Wait 10 PCLKs before writing LCRH_TX register,
  1363. * to get this delay write read only register 10 times
  1364. */
  1365. for (i = 0; i < 10; ++i)
  1366. writew(0xff, uap->port.membase + UART011_MIS);
  1367. writew(lcr_h, port->membase + uap->lcrh_tx);
  1368. }
  1369. writew(old_cr, port->membase + UART011_CR);
  1370. spin_unlock_irqrestore(&port->lock, flags);
  1371. }
  1372. static const char *pl011_type(struct uart_port *port)
  1373. {
  1374. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1375. return uap->port.type == PORT_AMBA ? uap->type : NULL;
  1376. }
  1377. /*
  1378. * Release the memory region(s) being used by 'port'
  1379. */
  1380. static void pl011_release_port(struct uart_port *port)
  1381. {
  1382. release_mem_region(port->mapbase, SZ_4K);
  1383. }
  1384. /*
  1385. * Request the memory region(s) being used by 'port'
  1386. */
  1387. static int pl011_request_port(struct uart_port *port)
  1388. {
  1389. return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
  1390. != NULL ? 0 : -EBUSY;
  1391. }
  1392. /*
  1393. * Configure/autoconfigure the port.
  1394. */
  1395. static void pl011_config_port(struct uart_port *port, int flags)
  1396. {
  1397. if (flags & UART_CONFIG_TYPE) {
  1398. port->type = PORT_AMBA;
  1399. pl011_request_port(port);
  1400. }
  1401. }
  1402. /*
  1403. * verify the new serial_struct (for TIOCSSERIAL).
  1404. */
  1405. static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
  1406. {
  1407. int ret = 0;
  1408. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
  1409. ret = -EINVAL;
  1410. if (ser->irq < 0 || ser->irq >= nr_irqs)
  1411. ret = -EINVAL;
  1412. if (ser->baud_base < 9600)
  1413. ret = -EINVAL;
  1414. return ret;
  1415. }
  1416. static struct uart_ops amba_pl011_pops = {
  1417. .tx_empty = pl011_tx_empty,
  1418. .set_mctrl = pl011_set_mctrl,
  1419. .get_mctrl = pl011_get_mctrl,
  1420. .stop_tx = pl011_stop_tx,
  1421. .start_tx = pl011_start_tx,
  1422. .stop_rx = pl011_stop_rx,
  1423. .enable_ms = pl011_enable_ms,
  1424. .break_ctl = pl011_break_ctl,
  1425. .startup = pl011_startup,
  1426. .shutdown = pl011_shutdown,
  1427. .flush_buffer = pl011_dma_flush_buffer,
  1428. .set_termios = pl011_set_termios,
  1429. .type = pl011_type,
  1430. .release_port = pl011_release_port,
  1431. .request_port = pl011_request_port,
  1432. .config_port = pl011_config_port,
  1433. .verify_port = pl011_verify_port,
  1434. #ifdef CONFIG_CONSOLE_POLL
  1435. .poll_get_char = pl011_get_poll_char,
  1436. .poll_put_char = pl011_put_poll_char,
  1437. #endif
  1438. };
  1439. static struct uart_amba_port *amba_ports[UART_NR];
  1440. #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
  1441. static void pl011_console_putchar(struct uart_port *port, int ch)
  1442. {
  1443. struct uart_amba_port *uap = (struct uart_amba_port *)port;
  1444. while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
  1445. barrier();
  1446. writew(ch, uap->port.membase + UART01x_DR);
  1447. }
  1448. static void
  1449. pl011_console_write(struct console *co, const char *s, unsigned int count)
  1450. {
  1451. struct uart_amba_port *uap = amba_ports[co->index];
  1452. unsigned int status, old_cr, new_cr;
  1453. unsigned long flags;
  1454. int locked = 1;
  1455. clk_enable(uap->clk);
  1456. local_irq_save(flags);
  1457. if (uap->port.sysrq)
  1458. locked = 0;
  1459. else if (oops_in_progress)
  1460. locked = spin_trylock(&uap->port.lock);
  1461. else
  1462. spin_lock(&uap->port.lock);
  1463. /*
  1464. * First save the CR then disable the interrupts
  1465. */
  1466. old_cr = readw(uap->port.membase + UART011_CR);
  1467. new_cr = old_cr & ~UART011_CR_CTSEN;
  1468. new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
  1469. writew(new_cr, uap->port.membase + UART011_CR);
  1470. uart_console_write(&uap->port, s, count, pl011_console_putchar);
  1471. /*
  1472. * Finally, wait for transmitter to become empty
  1473. * and restore the TCR
  1474. */
  1475. do {
  1476. status = readw(uap->port.membase + UART01x_FR);
  1477. } while (status & UART01x_FR_BUSY);
  1478. writew(old_cr, uap->port.membase + UART011_CR);
  1479. if (locked)
  1480. spin_unlock(&uap->port.lock);
  1481. local_irq_restore(flags);
  1482. clk_disable(uap->clk);
  1483. }
  1484. static void __init
  1485. pl011_console_get_options(struct uart_amba_port *uap, int *baud,
  1486. int *parity, int *bits)
  1487. {
  1488. if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
  1489. unsigned int lcr_h, ibrd, fbrd;
  1490. lcr_h = readw(uap->port.membase + uap->lcrh_tx);
  1491. *parity = 'n';
  1492. if (lcr_h & UART01x_LCRH_PEN) {
  1493. if (lcr_h & UART01x_LCRH_EPS)
  1494. *parity = 'e';
  1495. else
  1496. *parity = 'o';
  1497. }
  1498. if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
  1499. *bits = 7;
  1500. else
  1501. *bits = 8;
  1502. ibrd = readw(uap->port.membase + UART011_IBRD);
  1503. fbrd = readw(uap->port.membase + UART011_FBRD);
  1504. *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
  1505. if (uap->vendor->oversampling) {
  1506. if (readw(uap->port.membase + UART011_CR)
  1507. & ST_UART011_CR_OVSFACT)
  1508. *baud *= 2;
  1509. }
  1510. }
  1511. }
  1512. static int __init pl011_console_setup(struct console *co, char *options)
  1513. {
  1514. struct uart_amba_port *uap;
  1515. int baud = 38400;
  1516. int bits = 8;
  1517. int parity = 'n';
  1518. int flow = 'n';
  1519. int ret;
  1520. /*
  1521. * Check whether an invalid uart number has been specified, and
  1522. * if so, search for the first available port that does have
  1523. * console support.
  1524. */
  1525. if (co->index >= UART_NR)
  1526. co->index = 0;
  1527. uap = amba_ports[co->index];
  1528. if (!uap)
  1529. return -ENODEV;
  1530. /* Allow pins to be muxed in and configured */
  1531. if (!IS_ERR(uap->pins_default)) {
  1532. ret = pinctrl_select_state(uap->pinctrl, uap->pins_default);
  1533. if (ret)
  1534. dev_err(uap->port.dev,
  1535. "could not set default pins\n");
  1536. }
  1537. ret = clk_prepare(uap->clk);
  1538. if (ret)
  1539. return ret;
  1540. if (uap->port.dev->platform_data) {
  1541. struct amba_pl011_data *plat;
  1542. plat = uap->port.dev->platform_data;
  1543. if (plat->init)
  1544. plat->init();
  1545. }
  1546. uap->port.uartclk = clk_get_rate(uap->clk);
  1547. if (options)
  1548. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1549. else
  1550. pl011_console_get_options(uap, &baud, &parity, &bits);
  1551. return uart_set_options(&uap->port, co, baud, parity, bits, flow);
  1552. }
  1553. static struct uart_driver amba_reg;
  1554. static struct console amba_console = {
  1555. .name = "ttyAMA",
  1556. .write = pl011_console_write,
  1557. .device = uart_console_device,
  1558. .setup = pl011_console_setup,
  1559. .flags = CON_PRINTBUFFER,
  1560. .index = -1,
  1561. .data = &amba_reg,
  1562. };
  1563. #define AMBA_CONSOLE (&amba_console)
  1564. #else
  1565. #define AMBA_CONSOLE NULL
  1566. #endif
  1567. static struct uart_driver amba_reg = {
  1568. .owner = THIS_MODULE,
  1569. .driver_name = "ttyAMA",
  1570. .dev_name = "ttyAMA",
  1571. .major = SERIAL_AMBA_MAJOR,
  1572. .minor = SERIAL_AMBA_MINOR,
  1573. .nr = UART_NR,
  1574. .cons = AMBA_CONSOLE,
  1575. };
  1576. static int pl011_probe_dt_alias(int index, struct device *dev)
  1577. {
  1578. struct device_node *np;
  1579. static bool seen_dev_with_alias = false;
  1580. static bool seen_dev_without_alias = false;
  1581. int ret = index;
  1582. if (!IS_ENABLED(CONFIG_OF))
  1583. return ret;
  1584. np = dev->of_node;
  1585. if (!np)
  1586. return ret;
  1587. ret = of_alias_get_id(np, "serial");
  1588. if (IS_ERR_VALUE(ret)) {
  1589. seen_dev_without_alias = true;
  1590. ret = index;
  1591. } else {
  1592. seen_dev_with_alias = true;
  1593. if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
  1594. dev_warn(dev, "requested serial port %d not available.\n", ret);
  1595. ret = index;
  1596. }
  1597. }
  1598. if (seen_dev_with_alias && seen_dev_without_alias)
  1599. dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
  1600. return ret;
  1601. }
  1602. static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
  1603. {
  1604. struct uart_amba_port *uap;
  1605. struct vendor_data *vendor = id->data;
  1606. void __iomem *base;
  1607. int i, ret;
  1608. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1609. if (amba_ports[i] == NULL)
  1610. break;
  1611. if (i == ARRAY_SIZE(amba_ports)) {
  1612. ret = -EBUSY;
  1613. goto out;
  1614. }
  1615. uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
  1616. if (uap == NULL) {
  1617. ret = -ENOMEM;
  1618. goto out;
  1619. }
  1620. i = pl011_probe_dt_alias(i, &dev->dev);
  1621. base = ioremap(dev->res.start, resource_size(&dev->res));
  1622. if (!base) {
  1623. ret = -ENOMEM;
  1624. goto free;
  1625. }
  1626. uap->pinctrl = devm_pinctrl_get(&dev->dev);
  1627. if (IS_ERR(uap->pinctrl)) {
  1628. ret = PTR_ERR(uap->pinctrl);
  1629. goto unmap;
  1630. }
  1631. uap->pins_default = pinctrl_lookup_state(uap->pinctrl,
  1632. PINCTRL_STATE_DEFAULT);
  1633. if (IS_ERR(uap->pins_default))
  1634. dev_err(&dev->dev, "could not get default pinstate\n");
  1635. uap->pins_sleep = pinctrl_lookup_state(uap->pinctrl,
  1636. PINCTRL_STATE_SLEEP);
  1637. if (IS_ERR(uap->pins_sleep))
  1638. dev_dbg(&dev->dev, "could not get sleep pinstate\n");
  1639. uap->clk = clk_get(&dev->dev, NULL);
  1640. if (IS_ERR(uap->clk)) {
  1641. ret = PTR_ERR(uap->clk);
  1642. goto unmap;
  1643. }
  1644. uap->vendor = vendor;
  1645. uap->lcrh_rx = vendor->lcrh_rx;
  1646. uap->lcrh_tx = vendor->lcrh_tx;
  1647. uap->old_cr = 0;
  1648. uap->fifosize = vendor->fifosize;
  1649. uap->port.dev = &dev->dev;
  1650. uap->port.mapbase = dev->res.start;
  1651. uap->port.membase = base;
  1652. uap->port.iotype = UPIO_MEM;
  1653. uap->port.irq = dev->irq[0];
  1654. uap->port.fifosize = uap->fifosize;
  1655. uap->port.ops = &amba_pl011_pops;
  1656. uap->port.flags = UPF_BOOT_AUTOCONF;
  1657. uap->port.line = i;
  1658. pl011_dma_probe(uap);
  1659. /* Ensure interrupts from this UART are masked and cleared */
  1660. writew(0, uap->port.membase + UART011_IMSC);
  1661. writew(0xffff, uap->port.membase + UART011_ICR);
  1662. snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
  1663. amba_ports[i] = uap;
  1664. amba_set_drvdata(dev, uap);
  1665. ret = uart_add_one_port(&amba_reg, &uap->port);
  1666. if (ret) {
  1667. amba_set_drvdata(dev, NULL);
  1668. amba_ports[i] = NULL;
  1669. pl011_dma_remove(uap);
  1670. clk_put(uap->clk);
  1671. unmap:
  1672. iounmap(base);
  1673. free:
  1674. kfree(uap);
  1675. }
  1676. out:
  1677. return ret;
  1678. }
  1679. static int pl011_remove(struct amba_device *dev)
  1680. {
  1681. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1682. int i;
  1683. amba_set_drvdata(dev, NULL);
  1684. uart_remove_one_port(&amba_reg, &uap->port);
  1685. for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
  1686. if (amba_ports[i] == uap)
  1687. amba_ports[i] = NULL;
  1688. pl011_dma_remove(uap);
  1689. iounmap(uap->port.membase);
  1690. clk_put(uap->clk);
  1691. kfree(uap);
  1692. return 0;
  1693. }
  1694. #ifdef CONFIG_PM
  1695. static int pl011_suspend(struct amba_device *dev, pm_message_t state)
  1696. {
  1697. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1698. if (!uap)
  1699. return -EINVAL;
  1700. return uart_suspend_port(&amba_reg, &uap->port);
  1701. }
  1702. static int pl011_resume(struct amba_device *dev)
  1703. {
  1704. struct uart_amba_port *uap = amba_get_drvdata(dev);
  1705. if (!uap)
  1706. return -EINVAL;
  1707. return uart_resume_port(&amba_reg, &uap->port);
  1708. }
  1709. #endif
  1710. static struct amba_id pl011_ids[] = {
  1711. {
  1712. .id = 0x00041011,
  1713. .mask = 0x000fffff,
  1714. .data = &vendor_arm,
  1715. },
  1716. {
  1717. .id = 0x00380802,
  1718. .mask = 0x00ffffff,
  1719. .data = &vendor_st,
  1720. },
  1721. { 0, 0 },
  1722. };
  1723. MODULE_DEVICE_TABLE(amba, pl011_ids);
  1724. static struct amba_driver pl011_driver = {
  1725. .drv = {
  1726. .name = "uart-pl011",
  1727. },
  1728. .id_table = pl011_ids,
  1729. .probe = pl011_probe,
  1730. .remove = pl011_remove,
  1731. #ifdef CONFIG_PM
  1732. .suspend = pl011_suspend,
  1733. .resume = pl011_resume,
  1734. #endif
  1735. };
  1736. static int __init pl011_init(void)
  1737. {
  1738. int ret;
  1739. printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
  1740. ret = uart_register_driver(&amba_reg);
  1741. if (ret == 0) {
  1742. ret = amba_driver_register(&pl011_driver);
  1743. if (ret)
  1744. uart_unregister_driver(&amba_reg);
  1745. }
  1746. return ret;
  1747. }
  1748. static void __exit pl011_exit(void)
  1749. {
  1750. amba_driver_unregister(&pl011_driver);
  1751. uart_unregister_driver(&amba_reg);
  1752. }
  1753. /*
  1754. * While this can be a module, if builtin it's most likely the console
  1755. * So let's leave module_exit but move module_init to an earlier place
  1756. */
  1757. arch_initcall(pl011_init);
  1758. module_exit(pl011_exit);
  1759. MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
  1760. MODULE_DESCRIPTION("ARM AMBA serial port driver");
  1761. MODULE_LICENSE("GPL");