timbuart.c 12 KB

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