timbuart.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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/kernel.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/ioport.h>
  27. #include <linux/slab.h>
  28. #include "timbuart.h"
  29. struct timbuart_port {
  30. struct uart_port port;
  31. struct tasklet_struct tasklet;
  32. int usedma;
  33. u32 last_ier;
  34. struct platform_device *dev;
  35. };
  36. static int baudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800,
  37. 921600, 1843200, 3250000};
  38. static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier);
  39. static irqreturn_t timbuart_handleinterrupt(int irq, void *devid);
  40. static void timbuart_stop_rx(struct uart_port *port)
  41. {
  42. /* spin lock held by upper layer, disable all RX interrupts */
  43. u32 ier = ioread32(port->membase + TIMBUART_IER) & ~RXFLAGS;
  44. iowrite32(ier, port->membase + TIMBUART_IER);
  45. }
  46. static void timbuart_stop_tx(struct uart_port *port)
  47. {
  48. /* spinlock held by upper layer, disable TX interrupt */
  49. u32 ier = ioread32(port->membase + TIMBUART_IER) & ~TXBAE;
  50. iowrite32(ier, port->membase + TIMBUART_IER);
  51. }
  52. static void timbuart_start_tx(struct uart_port *port)
  53. {
  54. struct timbuart_port *uart =
  55. container_of(port, struct timbuart_port, port);
  56. /* do not transfer anything here -> fire off the tasklet */
  57. tasklet_schedule(&uart->tasklet);
  58. }
  59. static unsigned int timbuart_tx_empty(struct uart_port *port)
  60. {
  61. u32 isr = ioread32(port->membase + TIMBUART_ISR);
  62. return (isr & TXBE) ? TIOCSER_TEMT : 0;
  63. }
  64. static void timbuart_flush_buffer(struct uart_port *port)
  65. {
  66. if (!timbuart_tx_empty(port)) {
  67. u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
  68. TIMBUART_CTRL_FLSHTX;
  69. iowrite8(ctl, port->membase + TIMBUART_CTRL);
  70. iowrite32(TXBF, port->membase + TIMBUART_ISR);
  71. }
  72. }
  73. static void timbuart_rx_chars(struct uart_port *port)
  74. {
  75. struct tty_struct *tty = port->state->port.tty;
  76. while (ioread32(port->membase + TIMBUART_ISR) & RXDP) {
  77. u8 ch = ioread8(port->membase + TIMBUART_RXFIFO);
  78. port->icount.rx++;
  79. tty_insert_flip_char(tty, ch, TTY_NORMAL);
  80. }
  81. spin_unlock(&port->lock);
  82. tty_flip_buffer_push(port->state->port.tty);
  83. spin_lock(&port->lock);
  84. dev_dbg(port->dev, "%s - total read %d bytes\n",
  85. __func__, port->icount.rx);
  86. }
  87. static void timbuart_tx_chars(struct uart_port *port)
  88. {
  89. struct circ_buf *xmit = &port->state->xmit;
  90. while (!(ioread32(port->membase + TIMBUART_ISR) & TXBF) &&
  91. !uart_circ_empty(xmit)) {
  92. iowrite8(xmit->buf[xmit->tail],
  93. port->membase + TIMBUART_TXFIFO);
  94. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  95. port->icount.tx++;
  96. }
  97. dev_dbg(port->dev,
  98. "%s - total written %d bytes, CTL: %x, RTS: %x, baud: %x\n",
  99. __func__,
  100. port->icount.tx,
  101. ioread8(port->membase + TIMBUART_CTRL),
  102. port->mctrl & TIOCM_RTS,
  103. ioread8(port->membase + TIMBUART_BAUDRATE));
  104. }
  105. static void timbuart_handle_tx_port(struct uart_port *port, u32 isr, u32 *ier)
  106. {
  107. struct timbuart_port *uart =
  108. container_of(port, struct timbuart_port, port);
  109. struct circ_buf *xmit = &port->state->xmit;
  110. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  111. return;
  112. if (port->x_char)
  113. return;
  114. if (isr & TXFLAGS) {
  115. timbuart_tx_chars(port);
  116. /* clear all TX interrupts */
  117. iowrite32(TXFLAGS, port->membase + TIMBUART_ISR);
  118. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  119. uart_write_wakeup(port);
  120. } else
  121. /* Re-enable any tx interrupt */
  122. *ier |= uart->last_ier & TXFLAGS;
  123. /* enable interrupts if there are chars in the transmit buffer,
  124. * Or if we delivered some bytes and want the almost empty interrupt
  125. * we wake up the upper layer later when we got the interrupt
  126. * to give it some time to go out...
  127. */
  128. if (!uart_circ_empty(xmit))
  129. *ier |= TXBAE;
  130. dev_dbg(port->dev, "%s - leaving\n", __func__);
  131. }
  132. void timbuart_handle_rx_port(struct uart_port *port, u32 isr, u32 *ier)
  133. {
  134. if (isr & RXFLAGS) {
  135. /* Some RX status is set */
  136. if (isr & RXBF) {
  137. u8 ctl = ioread8(port->membase + TIMBUART_CTRL) |
  138. TIMBUART_CTRL_FLSHRX;
  139. iowrite8(ctl, port->membase + TIMBUART_CTRL);
  140. port->icount.overrun++;
  141. } else if (isr & (RXDP))
  142. timbuart_rx_chars(port);
  143. /* ack all RX interrupts */
  144. iowrite32(RXFLAGS, port->membase + TIMBUART_ISR);
  145. }
  146. /* always have the RX interrupts enabled */
  147. *ier |= RXBAF | RXBF | RXTT;
  148. dev_dbg(port->dev, "%s - leaving\n", __func__);
  149. }
  150. void timbuart_tasklet(unsigned long arg)
  151. {
  152. struct timbuart_port *uart = (struct timbuart_port *)arg;
  153. u32 isr, ier = 0;
  154. spin_lock(&uart->port.lock);
  155. isr = ioread32(uart->port.membase + TIMBUART_ISR);
  156. dev_dbg(uart->port.dev, "%s ISR: %x\n", __func__, isr);
  157. if (!uart->usedma)
  158. timbuart_handle_tx_port(&uart->port, isr, &ier);
  159. timbuart_mctrl_check(&uart->port, isr, &ier);
  160. if (!uart->usedma)
  161. timbuart_handle_rx_port(&uart->port, isr, &ier);
  162. iowrite32(ier, uart->port.membase + TIMBUART_IER);
  163. spin_unlock(&uart->port.lock);
  164. dev_dbg(uart->port.dev, "%s leaving\n", __func__);
  165. }
  166. static unsigned int timbuart_get_mctrl(struct uart_port *port)
  167. {
  168. u8 cts = ioread8(port->membase + TIMBUART_CTRL);
  169. dev_dbg(port->dev, "%s - cts %x\n", __func__, cts);
  170. if (cts & TIMBUART_CTRL_CTS)
  171. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  172. else
  173. return TIOCM_DSR | TIOCM_CAR;
  174. }
  175. static void timbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  176. {
  177. dev_dbg(port->dev, "%s - %x\n", __func__, mctrl);
  178. if (mctrl & TIOCM_RTS)
  179. iowrite8(TIMBUART_CTRL_RTS, port->membase + TIMBUART_CTRL);
  180. else
  181. iowrite8(0, port->membase + TIMBUART_CTRL);
  182. }
  183. static void timbuart_mctrl_check(struct uart_port *port, u32 isr, u32 *ier)
  184. {
  185. unsigned int cts;
  186. if (isr & CTS_DELTA) {
  187. /* ack */
  188. iowrite32(CTS_DELTA, port->membase + TIMBUART_ISR);
  189. cts = timbuart_get_mctrl(port);
  190. uart_handle_cts_change(port, cts & TIOCM_CTS);
  191. wake_up_interruptible(&port->state->port.delta_msr_wait);
  192. }
  193. *ier |= CTS_DELTA;
  194. }
  195. static void timbuart_enable_ms(struct uart_port *port)
  196. {
  197. /* N/A */
  198. }
  199. static void timbuart_break_ctl(struct uart_port *port, int ctl)
  200. {
  201. /* N/A */
  202. }
  203. static int timbuart_startup(struct uart_port *port)
  204. {
  205. struct timbuart_port *uart =
  206. container_of(port, struct timbuart_port, port);
  207. dev_dbg(port->dev, "%s\n", __func__);
  208. iowrite8(TIMBUART_CTRL_FLSHRX, port->membase + TIMBUART_CTRL);
  209. iowrite32(0x1ff, port->membase + TIMBUART_ISR);
  210. /* Enable all but TX interrupts */
  211. iowrite32(RXBAF | RXBF | RXTT | CTS_DELTA,
  212. port->membase + TIMBUART_IER);
  213. return request_irq(port->irq, timbuart_handleinterrupt, IRQF_SHARED,
  214. "timb-uart", uart);
  215. }
  216. static void timbuart_shutdown(struct uart_port *port)
  217. {
  218. struct timbuart_port *uart =
  219. container_of(port, struct timbuart_port, port);
  220. dev_dbg(port->dev, "%s\n", __func__);
  221. free_irq(port->irq, uart);
  222. iowrite32(0, port->membase + TIMBUART_IER);
  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 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. .enable_ms = timbuart_enable_ms,
  327. .break_ctl = timbuart_break_ctl,
  328. .startup = timbuart_startup,
  329. .shutdown = timbuart_shutdown,
  330. .set_termios = timbuart_set_termios,
  331. .type = timbuart_type,
  332. .release_port = timbuart_release_port,
  333. .request_port = timbuart_request_port,
  334. .config_port = timbuart_config_port,
  335. .verify_port = timbuart_verify_port
  336. };
  337. static struct uart_driver timbuart_driver = {
  338. .owner = THIS_MODULE,
  339. .driver_name = "timberdale_uart",
  340. .dev_name = "ttyTU",
  341. .major = TIMBUART_MAJOR,
  342. .minor = TIMBUART_MINOR,
  343. .nr = 1
  344. };
  345. static int __devinit timbuart_probe(struct platform_device *dev)
  346. {
  347. int err, irq;
  348. struct timbuart_port *uart;
  349. struct resource *iomem;
  350. dev_dbg(&dev->dev, "%s\n", __func__);
  351. uart = kzalloc(sizeof(*uart), GFP_KERNEL);
  352. if (!uart) {
  353. err = -EINVAL;
  354. goto err_mem;
  355. }
  356. uart->usedma = 0;
  357. uart->port.uartclk = 3250000 * 16;
  358. uart->port.fifosize = TIMBUART_FIFO_SIZE;
  359. uart->port.regshift = 2;
  360. uart->port.iotype = UPIO_MEM;
  361. uart->port.ops = &timbuart_ops;
  362. uart->port.irq = 0;
  363. uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  364. uart->port.line = 0;
  365. uart->port.dev = &dev->dev;
  366. iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  367. if (!iomem) {
  368. err = -ENOMEM;
  369. goto err_register;
  370. }
  371. uart->port.mapbase = iomem->start;
  372. uart->port.membase = NULL;
  373. irq = platform_get_irq(dev, 0);
  374. if (irq < 0) {
  375. err = -EINVAL;
  376. goto err_register;
  377. }
  378. uart->port.irq = irq;
  379. tasklet_init(&uart->tasklet, timbuart_tasklet, (unsigned long)uart);
  380. err = uart_register_driver(&timbuart_driver);
  381. if (err)
  382. goto err_register;
  383. err = uart_add_one_port(&timbuart_driver, &uart->port);
  384. if (err)
  385. goto err_add_port;
  386. platform_set_drvdata(dev, uart);
  387. return 0;
  388. err_add_port:
  389. uart_unregister_driver(&timbuart_driver);
  390. err_register:
  391. kfree(uart);
  392. err_mem:
  393. printk(KERN_ERR "timberdale: Failed to register Timberdale UART: %d\n",
  394. err);
  395. return err;
  396. }
  397. static int __devexit timbuart_remove(struct platform_device *dev)
  398. {
  399. struct timbuart_port *uart = platform_get_drvdata(dev);
  400. tasklet_kill(&uart->tasklet);
  401. uart_remove_one_port(&timbuart_driver, &uart->port);
  402. uart_unregister_driver(&timbuart_driver);
  403. kfree(uart);
  404. return 0;
  405. }
  406. static struct platform_driver timbuart_platform_driver = {
  407. .driver = {
  408. .name = "timb-uart",
  409. .owner = THIS_MODULE,
  410. },
  411. .probe = timbuart_probe,
  412. .remove = __devexit_p(timbuart_remove),
  413. };
  414. /*--------------------------------------------------------------------------*/
  415. static int __init timbuart_init(void)
  416. {
  417. return platform_driver_register(&timbuart_platform_driver);
  418. }
  419. static void __exit timbuart_exit(void)
  420. {
  421. platform_driver_unregister(&timbuart_platform_driver);
  422. }
  423. module_init(timbuart_init);
  424. module_exit(timbuart_exit);
  425. MODULE_DESCRIPTION("Timberdale UART driver");
  426. MODULE_LICENSE("GPL v2");
  427. MODULE_ALIAS("platform:timb-uart");