timbuart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * timbuart.c timberdale FPGA UART driver
  4. * Copyright (c) 2009 Intel Corporation
  5. */
  6. /* Supports:
  7. * Timberdale FPGA UART
  8. */
  9. #include <linux/pci.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/serial_core.h>
  12. #include <linux/tty.h>
  13. #include <linux/tty_flip.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/ioport.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include "timbuart.h"
  20. struct timbuart_port {
  21. struct uart_port port;
  22. struct tasklet_struct tasklet;
  23. int usedma;
  24. u32 last_ier;
  25. struct platform_device *dev;
  26. };
  27. static int baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800,
  28. 921600, 1843200, 3250000};
  29. static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier);
  30. static irqreturn_t timbuart_handleinterrupt(int irq, void *devid);
  31. static void timbuart_stop_rx(struct uart_port *port)
  32. {
  33. /* spin lock held by upper layer, disable all RX interrupts */
  34. u32 ier = ioread32(port->membase + TIMBUART_IER) & ~RXFLAGS;
  35. iowrite32(ier, port->membase + TIMBUART_IER);
  36. }
  37. static void timbuart_stop_tx(struct uart_port *port)
  38. {
  39. /* spinlock held by upper layer, disable TX interrupt */
  40. u32 ier = ioread32(port->membase + TIMBUART_IER) & ~TXBAE;
  41. iowrite32(ier, port->membase + TIMBUART_IER);
  42. }
  43. static void timbuart_start_tx(struct uart_port *port)
  44. {
  45. struct timbuart_port *uart =
  46. container_of(port, struct timbuart_port, port);
  47. /* do not transfer anything here -> fire off the tasklet */
  48. tasklet_schedule(&uart->tasklet);
  49. }
  50. static unsigned int timbuart_tx_empty(struct uart_port *port)
  51. {
  52. u32 isr = ioread32(port->membase + TIMBUART_ISR);
  53. return (isr & TXBE) ? TIOCSER_TEMT : 0;
  54. }
  55. static void timbuart_flush_buffer(struct uart_port *port)
  56. {
  57. if (!timbuart_tx_empty(port)) {
  58. u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
  59. TIMBUART_CTRL_FLSHTX;
  60. iowrite8(ctl, port->membase + TIMBUART_CTRL);
  61. iowrite32(TXBF, port->membase + TIMBUART_ISR);
  62. }
  63. }
  64. static void timbuart_rx_chars(struct uart_port *port)
  65. {
  66. struct tty_port *tport = &port->state->port;
  67. while (ioread32(port->membase + TIMBUART_ISR) & RXDP) {
  68. u8 ch = ioread8(port->membase + TIMBUART_RXFIFO);
  69. port->icount.rx++;
  70. tty_insert_flip_char(tport, ch, TTY_NORMAL);
  71. }
  72. tty_flip_buffer_push(tport);
  73. dev_dbg(port->dev, "%s - total read %d bytes\n",
  74. __func__, port->icount.rx);
  75. }
  76. static void timbuart_tx_chars(struct uart_port *port)
  77. {
  78. struct circ_buf *xmit = &port->state->xmit;
  79. while (!(ioread32(port->membase + TIMBUART_ISR) & TXBF) &&
  80. !uart_circ_empty(xmit)) {
  81. iowrite8(xmit->buf[xmit->tail],
  82. port->membase + TIMBUART_TXFIFO);
  83. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  84. port->icount.tx++;
  85. }
  86. dev_dbg(port->dev,
  87. "%s - total written %d bytes, CTL: %x, RTS: %x, baud: %x\n",
  88. __func__,
  89. port->icount.tx,
  90. ioread8(port->membase + TIMBUART_CTRL),
  91. port->mctrl & TIOCM_RTS,
  92. ioread8(port->membase + TIMBUART_BAUDRATE));
  93. }
  94. static void timbuart_handle_tx_port(struct uart_port *port, u32 isr, u32 *ier)
  95. {
  96. struct timbuart_port *uart =
  97. container_of(port, struct timbuart_port, port);
  98. struct circ_buf *xmit = &port->state->xmit;
  99. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  100. return;
  101. if (port->x_char)
  102. return;
  103. if (isr & TXFLAGS) {
  104. timbuart_tx_chars(port);
  105. /* clear all TX interrupts */
  106. iowrite32(TXFLAGS, port->membase + TIMBUART_ISR);
  107. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  108. uart_write_wakeup(port);
  109. } else
  110. /* Re-enable any tx interrupt */
  111. *ier |= uart->last_ier & TXFLAGS;
  112. /* enable interrupts if there are chars in the transmit buffer,
  113. * Or if we delivered some bytes and want the almost empty interrupt
  114. * we wake up the upper layer later when we got the interrupt
  115. * to give it some time to go out...
  116. */
  117. if (!uart_circ_empty(xmit))
  118. *ier |= TXBAE;
  119. dev_dbg(port->dev, "%s - leaving\n", __func__);
  120. }
  121. static void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
  122. {
  123. if (isr & RXFLAGS) {
  124. /* Some RX status is set */
  125. if (isr & RXBF) {
  126. u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
  127. TIMBUART_CTRL_FLSHRX;
  128. iowrite8(ctl, port->membase + TIMBUART_CTRL);
  129. port->icount.overrun++;
  130. } else if (isr & (RXDP))
  131. timbuart_rx_chars(port);
  132. /* ack all RX interrupts */
  133. iowrite32(RXFLAGS, port->membase + TIMBUART_ISR);
  134. }
  135. /* always have the RX interrupts enabled */
  136. *ier |= RXBAF | RXBF | RXTT;
  137. dev_dbg(port->dev, "%s - leaving\n", __func__);
  138. }
  139. static void timbuart_tasklet(struct tasklet_struct *t)
  140. {
  141. struct timbuart_port *uart = from_tasklet(uart, t, tasklet);
  142. u32 isr, ier = 0;
  143. spin_lock(&uart->port.lock);
  144. isr = ioread32(uart->port.membase + TIMBUART_ISR);
  145. dev_dbg(uart->port.dev, "%s ISR: %x\n", __func__, isr);
  146. if (!uart->usedma)
  147. timbuart_handle_tx_port(&uart->port, isr, &ier);
  148. timbuart_mctrl_check(&uart->port, isr, &ier);
  149. if (!uart->usedma)
  150. timbuart_handle_rx_port(&uart->port, isr, &ier);
  151. iowrite32(ier, uart->port.membase + TIMBUART_IER);
  152. spin_unlock(&uart->port.lock);
  153. dev_dbg(uart->port.dev, "%s leaving\n", __func__);
  154. }
  155. static unsigned int timbuart_get_mctrl(struct uart_port *port)
  156. {
  157. u8 cts = ioread8(port->membase + TIMBUART_CTRL);
  158. dev_dbg(port->dev, "%s - cts %x\n", __func__, cts);
  159. if (cts & TIMBUART_CTRL_CTS)
  160. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  161. else
  162. return TIOCM_DSR | TIOCM_CAR;
  163. }
  164. static void timbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  165. {
  166. dev_dbg(port->dev, "%s - %x\n", __func__, mctrl);
  167. if (mctrl & TIOCM_RTS)
  168. iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
  169. else
  170. iowrite8(0, port->membase + TIMBUART_CTRL);
  171. }
  172. static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier)
  173. {
  174. unsigned int cts;
  175. if (isr & CTS_DELTA) {
  176. /* ack */
  177. iowrite32(CTS_DELTA, port->membase + TIMBUART_ISR);
  178. cts = timbuart_get_mctrl(port);
  179. uart_handle_cts_change(port, cts & TIOCM_CTS);
  180. wake_up_interruptible(&port->state->port.delta_msr_wait);
  181. }
  182. *ier |= CTS_DELTA;
  183. }
  184. static void timbuart_break_ctl(struct uart_port *port, int ctl)
  185. {
  186. /* N/A */
  187. }
  188. static int timbuart_startup(struct uart_port *port)
  189. {
  190. struct timbuart_port *uart =
  191. container_of(port, struct timbuart_port, port);
  192. dev_dbg(port->dev, "%s\n", __func__);
  193. iowrite8(TIMBUART_CTRL_FLSHRX, port->membase + TIMBUART_CTRL);
  194. iowrite32(0x1ff, port->membase + TIMBUART_ISR);
  195. /* Enable all but TX interrupts */
  196. iowrite32(RXBAF | RXBF | RXTT | CTS_DELTA,
  197. port->membase + TIMBUART_IER);
  198. return request_irq(port->irq, timbuart_handleinterrupt, IRQF_SHARED,
  199. "timb-uart", uart);
  200. }
  201. static void timbuart_shutdown(struct uart_port *port)
  202. {
  203. struct timbuart_port *uart =
  204. container_of(port, struct timbuart_port, port);
  205. dev_dbg(port->dev, "%s\n", __func__);
  206. free_irq(port->irq, uart);
  207. iowrite32(0, port->membase + TIMBUART_IER);
  208. timbuart_flush_buffer(port);
  209. }
  210. static int get_bindex(int baud)
  211. {
  212. int i;
  213. for (i = 0; i < ARRAY_SIZE(baudrates); i++)
  214. if (baud <= baudrates[i])
  215. return i;
  216. return -1;
  217. }
  218. static void timbuart_set_termios(struct uart_port *port,
  219. struct ktermios *termios,
  220. const struct ktermios *old)
  221. {
  222. unsigned int baud;
  223. short bindex;
  224. unsigned long flags;
  225. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  226. bindex = get_bindex(baud);
  227. dev_dbg(port->dev, "%s - bindex %d\n", __func__, bindex);
  228. if (bindex < 0)
  229. bindex = 0;
  230. baud = baudrates[bindex];
  231. /* The serial layer calls into this once with old = NULL when setting
  232. up initially */
  233. if (old)
  234. tty_termios_copy_hw(termios, old);
  235. tty_termios_encode_baud_rate(termios, baud, baud);
  236. spin_lock_irqsave(&port->lock, flags);
  237. iowrite8((u8)bindex, port->membase + TIMBUART_BAUDRATE);
  238. uart_update_timeout(port, termios->c_cflag, baud);
  239. spin_unlock_irqrestore(&port->lock, flags);
  240. }
  241. static const char *timbuart_type(struct uart_port *port)
  242. {
  243. return port->type == PORT_UNKNOWN ? "timbuart" : NULL;
  244. }
  245. /* We do not request/release mappings of the registers here,
  246. * currently it's done in the proble function.
  247. */
  248. static void timbuart_release_port(struct uart_port *port)
  249. {
  250. struct platform_device *pdev = to_platform_device(port->dev);
  251. int size =
  252. resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
  253. if (port->flags & UPF_IOREMAP) {
  254. iounmap(port->membase);
  255. port->membase = NULL;
  256. }
  257. release_mem_region(port->mapbase, size);
  258. }
  259. static int timbuart_request_port(struct uart_port *port)
  260. {
  261. struct platform_device *pdev = to_platform_device(port->dev);
  262. int size =
  263. resource_size(platform_get_resource(pdev, IORESOURCE_MEM, 0));
  264. if (!request_mem_region(port->mapbase, size, "timb-uart"))
  265. return -EBUSY;
  266. if (port->flags & UPF_IOREMAP) {
  267. port->membase = ioremap(port->mapbase, size);
  268. if (port->membase == NULL) {
  269. release_mem_region(port->mapbase, size);
  270. return -ENOMEM;
  271. }
  272. }
  273. return 0;
  274. }
  275. static irqreturn_t timbuart_handleinterrupt(int irq, void *devid)
  276. {
  277. struct timbuart_port *uart = (struct timbuart_port *)devid;
  278. if (ioread8(uart->port.membase + TIMBUART_IPR)) {
  279. uart->last_ier = ioread32(uart->port.membase + TIMBUART_IER);
  280. /* disable interrupts, the tasklet enables them again */
  281. iowrite32(0, uart->port.membase + TIMBUART_IER);
  282. /* fire off bottom half */
  283. tasklet_schedule(&uart->tasklet);
  284. return IRQ_HANDLED;
  285. } else
  286. return IRQ_NONE;
  287. }
  288. /*
  289. * Configure/autoconfigure the port.
  290. */
  291. static void timbuart_config_port(struct uart_port *port, int flags)
  292. {
  293. if (flags & UART_CONFIG_TYPE) {
  294. port->type = PORT_TIMBUART;
  295. timbuart_request_port(port);
  296. }
  297. }
  298. static int timbuart_verify_port(struct uart_port *port,
  299. struct serial_struct *ser)
  300. {
  301. /* we don't want the core code to modify any port params */
  302. return -EINVAL;
  303. }
  304. static const struct uart_ops timbuart_ops = {
  305. .tx_empty = timbuart_tx_empty,
  306. .set_mctrl = timbuart_set_mctrl,
  307. .get_mctrl = timbuart_get_mctrl,
  308. .stop_tx = timbuart_stop_tx,
  309. .start_tx = timbuart_start_tx,
  310. .flush_buffer = timbuart_flush_buffer,
  311. .stop_rx = timbuart_stop_rx,
  312. .break_ctl = timbuart_break_ctl,
  313. .startup = timbuart_startup,
  314. .shutdown = timbuart_shutdown,
  315. .set_termios = timbuart_set_termios,
  316. .type = timbuart_type,
  317. .release_port = timbuart_release_port,
  318. .request_port = timbuart_request_port,
  319. .config_port = timbuart_config_port,
  320. .verify_port = timbuart_verify_port
  321. };
  322. static struct uart_driver timbuart_driver = {
  323. .owner = THIS_MODULE,
  324. .driver_name = "timberdale_uart",
  325. .dev_name = "ttyTU",
  326. .major = TIMBUART_MAJOR,
  327. .minor = TIMBUART_MINOR,
  328. .nr = 1
  329. };
  330. static int timbuart_probe(struct platform_device *dev)
  331. {
  332. int err, irq;
  333. struct timbuart_port *uart;
  334. struct resource *iomem;
  335. dev_dbg(&dev->dev, "%s\n", __func__);
  336. uart = kzalloc(sizeof(*uart), GFP_KERNEL);
  337. if (!uart) {
  338. err = -EINVAL;
  339. goto err_mem;
  340. }
  341. uart->usedma = 0;
  342. uart->port.uartclk = 3250000 * 16;
  343. uart->port.fifosize = TIMBUART_FIFO_SIZE;
  344. uart->port.regshift = 2;
  345. uart->port.iotype = UPIO_MEM;
  346. uart->port.ops = &timbuart_ops;
  347. uart->port.irq = 0;
  348. uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  349. uart->port.line = 0;
  350. uart->port.dev = &dev->dev;
  351. iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  352. if (!iomem) {
  353. err = -ENOMEM;
  354. goto err_register;
  355. }
  356. uart->port.mapbase = iomem->start;
  357. uart->port.membase = NULL;
  358. irq = platform_get_irq(dev, 0);
  359. if (irq < 0) {
  360. err = -EINVAL;
  361. goto err_register;
  362. }
  363. uart->port.irq = irq;
  364. tasklet_setup(&uart->tasklet, timbuart_tasklet);
  365. err = uart_register_driver(&timbuart_driver);
  366. if (err)
  367. goto err_register;
  368. err = uart_add_one_port(&timbuart_driver, &uart->port);
  369. if (err)
  370. goto err_add_port;
  371. platform_set_drvdata(dev, uart);
  372. return 0;
  373. err_add_port:
  374. uart_unregister_driver(&timbuart_driver);
  375. err_register:
  376. kfree(uart);
  377. err_mem:
  378. printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n",
  379. err);
  380. return err;
  381. }
  382. static int timbuart_remove(struct platform_device *dev)
  383. {
  384. struct timbuart_port *uart = platform_get_drvdata(dev);
  385. tasklet_kill(&uart->tasklet);
  386. uart_remove_one_port(&timbuart_driver, &uart->port);
  387. uart_unregister_driver(&timbuart_driver);
  388. kfree(uart);
  389. return 0;
  390. }
  391. static struct platform_driver timbuart_platform_driver = {
  392. .driver = {
  393. .name = "timb-uart",
  394. },
  395. .probe = timbuart_probe,
  396. .remove = timbuart_remove,
  397. };
  398. module_platform_driver(timbuart_platform_driver);
  399. MODULE_DESCRIPTION("Timberdale UART driver");
  400. MODULE_LICENSE("GPL v2");
  401. MODULE_ALIAS("platform:timb-uart");