PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/tty/serial/amba-pl011.c

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