timbuart.c 13 KB

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