uartlite.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * uartlite.c: Serial driver for Xilinx uartlite serial controller
  3. *
  4. * Copyright (C) 2006 Peter Korsgaard <jacmet@sunsite.dk>
  5. * Copyright (C) 2007 Secret Lab Technologies Ltd.
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. */
  11. #include <linux/platform_device.h>
  12. #include <linux/module.h>
  13. #include <linux/console.h>
  14. #include <linux/serial.h>
  15. #include <linux/serial_core.h>
  16. #include <linux/tty.h>
  17. #include <linux/delay.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/init.h>
  20. #include <asm/io.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_platform.h>
  25. #define ULITE_NAME "ttyUL"
  26. #define ULITE_MAJOR 204
  27. #define ULITE_MINOR 187
  28. #define ULITE_NR_UARTS 4
  29. /* ---------------------------------------------------------------------
  30. * Register definitions
  31. *
  32. * For register details see datasheet:
  33. * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
  34. */
  35. #define ULITE_RX 0x00
  36. #define ULITE_TX 0x04
  37. #define ULITE_STATUS 0x08
  38. #define ULITE_CONTROL 0x0c
  39. #define ULITE_REGION 16
  40. #define ULITE_STATUS_RXVALID 0x01
  41. #define ULITE_STATUS_RXFULL 0x02
  42. #define ULITE_STATUS_TXEMPTY 0x04
  43. #define ULITE_STATUS_TXFULL 0x08
  44. #define ULITE_STATUS_IE 0x10
  45. #define ULITE_STATUS_OVERRUN 0x20
  46. #define ULITE_STATUS_FRAME 0x40
  47. #define ULITE_STATUS_PARITY 0x80
  48. #define ULITE_CONTROL_RST_TX 0x01
  49. #define ULITE_CONTROL_RST_RX 0x02
  50. #define ULITE_CONTROL_IE 0x10
  51. static struct uart_port ulite_ports[ULITE_NR_UARTS];
  52. /* ---------------------------------------------------------------------
  53. * Core UART driver operations
  54. */
  55. static int ulite_receive(struct uart_port *port, int stat)
  56. {
  57. struct tty_struct *tty = port->state->port.tty;
  58. unsigned char ch = 0;
  59. char flag = TTY_NORMAL;
  60. if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
  61. | ULITE_STATUS_FRAME)) == 0)
  62. return 0;
  63. /* stats */
  64. if (stat & ULITE_STATUS_RXVALID) {
  65. port->icount.rx++;
  66. ch = ioread32be(port->membase + ULITE_RX);
  67. if (stat & ULITE_STATUS_PARITY)
  68. port->icount.parity++;
  69. }
  70. if (stat & ULITE_STATUS_OVERRUN)
  71. port->icount.overrun++;
  72. if (stat & ULITE_STATUS_FRAME)
  73. port->icount.frame++;
  74. /* drop byte with parity error if IGNPAR specificed */
  75. if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
  76. stat &= ~ULITE_STATUS_RXVALID;
  77. stat &= port->read_status_mask;
  78. if (stat & ULITE_STATUS_PARITY)
  79. flag = TTY_PARITY;
  80. stat &= ~port->ignore_status_mask;
  81. if (stat & ULITE_STATUS_RXVALID)
  82. tty_insert_flip_char(tty, ch, flag);
  83. if (stat & ULITE_STATUS_FRAME)
  84. tty_insert_flip_char(tty, 0, TTY_FRAME);
  85. if (stat & ULITE_STATUS_OVERRUN)
  86. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  87. return 1;
  88. }
  89. static int ulite_transmit(struct uart_port *port, int stat)
  90. {
  91. struct circ_buf *xmit = &port->state->xmit;
  92. if (stat & ULITE_STATUS_TXFULL)
  93. return 0;
  94. if (port->x_char) {
  95. iowrite32be(port->x_char, port->membase + ULITE_TX);
  96. port->x_char = 0;
  97. port->icount.tx++;
  98. return 1;
  99. }
  100. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  101. return 0;
  102. iowrite32be(xmit->buf[xmit->tail], port->membase + ULITE_TX);
  103. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
  104. port->icount.tx++;
  105. /* wake up */
  106. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  107. uart_write_wakeup(port);
  108. return 1;
  109. }
  110. static irqreturn_t ulite_isr(int irq, void *dev_id)
  111. {
  112. struct uart_port *port = dev_id;
  113. int busy, n = 0;
  114. do {
  115. int stat = ioread32be(port->membase + ULITE_STATUS);
  116. busy = ulite_receive(port, stat);
  117. busy |= ulite_transmit(port, stat);
  118. n++;
  119. } while (busy);
  120. /* work done? */
  121. if (n > 1) {
  122. tty_flip_buffer_push(port->state->port.tty);
  123. return IRQ_HANDLED;
  124. } else {
  125. return IRQ_NONE;
  126. }
  127. }
  128. static unsigned int ulite_tx_empty(struct uart_port *port)
  129. {
  130. unsigned long flags;
  131. unsigned int ret;
  132. spin_lock_irqsave(&port->lock, flags);
  133. ret = ioread32be(port->membase + ULITE_STATUS);
  134. spin_unlock_irqrestore(&port->lock, flags);
  135. return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
  136. }
  137. static unsigned int ulite_get_mctrl(struct uart_port *port)
  138. {
  139. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  140. }
  141. static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
  142. {
  143. /* N/A */
  144. }
  145. static void ulite_stop_tx(struct uart_port *port)
  146. {
  147. /* N/A */
  148. }
  149. static void ulite_start_tx(struct uart_port *port)
  150. {
  151. ulite_transmit(port, ioread32be(port->membase + ULITE_STATUS));
  152. }
  153. static void ulite_stop_rx(struct uart_port *port)
  154. {
  155. /* don't forward any more data (like !CREAD) */
  156. port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
  157. | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
  158. }
  159. static void ulite_enable_ms(struct uart_port *port)
  160. {
  161. /* N/A */
  162. }
  163. static void ulite_break_ctl(struct uart_port *port, int ctl)
  164. {
  165. /* N/A */
  166. }
  167. static int ulite_startup(struct uart_port *port)
  168. {
  169. int ret;
  170. ret = request_irq(port->irq, ulite_isr,
  171. IRQF_SHARED | IRQF_SAMPLE_RANDOM, "uartlite", port);
  172. if (ret)
  173. return ret;
  174. iowrite32be(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
  175. port->membase + ULITE_CONTROL);
  176. iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
  177. return 0;
  178. }
  179. static void ulite_shutdown(struct uart_port *port)
  180. {
  181. iowrite32be(0, port->membase + ULITE_CONTROL);
  182. ioread32be(port->membase + ULITE_CONTROL); /* dummy */
  183. free_irq(port->irq, port);
  184. }
  185. static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
  186. struct ktermios *old)
  187. {
  188. unsigned long flags;
  189. unsigned int baud;
  190. spin_lock_irqsave(&port->lock, flags);
  191. port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
  192. | ULITE_STATUS_TXFULL;
  193. if (termios->c_iflag & INPCK)
  194. port->read_status_mask |=
  195. ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
  196. port->ignore_status_mask = 0;
  197. if (termios->c_iflag & IGNPAR)
  198. port->ignore_status_mask |= ULITE_STATUS_PARITY
  199. | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
  200. /* ignore all characters if CREAD is not set */
  201. if ((termios->c_cflag & CREAD) == 0)
  202. port->ignore_status_mask |=
  203. ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
  204. | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
  205. /* update timeout */
  206. baud = uart_get_baud_rate(port, termios, old, 0, 460800);
  207. uart_update_timeout(port, termios->c_cflag, baud);
  208. spin_unlock_irqrestore(&port->lock, flags);
  209. }
  210. static const char *ulite_type(struct uart_port *port)
  211. {
  212. return port->type == PORT_UARTLITE ? "uartlite" : NULL;
  213. }
  214. static void ulite_release_port(struct uart_port *port)
  215. {
  216. release_mem_region(port->mapbase, ULITE_REGION);
  217. iounmap(port->membase);
  218. port->membase = NULL;
  219. }
  220. static int ulite_request_port(struct uart_port *port)
  221. {
  222. pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
  223. port, (unsigned long long) port->mapbase);
  224. if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
  225. dev_err(port->dev, "Memory region busy\n");
  226. return -EBUSY;
  227. }
  228. port->membase = ioremap(port->mapbase, ULITE_REGION);
  229. if (!port->membase) {
  230. dev_err(port->dev, "Unable to map registers\n");
  231. release_mem_region(port->mapbase, ULITE_REGION);
  232. return -EBUSY;
  233. }
  234. return 0;
  235. }
  236. static void ulite_config_port(struct uart_port *port, int flags)
  237. {
  238. if (!ulite_request_port(port))
  239. port->type = PORT_UARTLITE;
  240. }
  241. static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
  242. {
  243. /* we don't want the core code to modify any port params */
  244. return -EINVAL;
  245. }
  246. #ifdef CONFIG_CONSOLE_POLL
  247. static int ulite_get_poll_char(struct uart_port *port)
  248. {
  249. if (!(ioread32be(port->membase + ULITE_STATUS)
  250. & ULITE_STATUS_RXVALID))
  251. return NO_POLL_CHAR;
  252. return ioread32be(port->membase + ULITE_RX);
  253. }
  254. static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
  255. {
  256. while (ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_TXFULL)
  257. cpu_relax();
  258. /* write char to device */
  259. iowrite32be(ch, port->membase + ULITE_TX);
  260. }
  261. #endif
  262. static struct uart_ops ulite_ops = {
  263. .tx_empty = ulite_tx_empty,
  264. .set_mctrl = ulite_set_mctrl,
  265. .get_mctrl = ulite_get_mctrl,
  266. .stop_tx = ulite_stop_tx,
  267. .start_tx = ulite_start_tx,
  268. .stop_rx = ulite_stop_rx,
  269. .enable_ms = ulite_enable_ms,
  270. .break_ctl = ulite_break_ctl,
  271. .startup = ulite_startup,
  272. .shutdown = ulite_shutdown,
  273. .set_termios = ulite_set_termios,
  274. .type = ulite_type,
  275. .release_port = ulite_release_port,
  276. .request_port = ulite_request_port,
  277. .config_port = ulite_config_port,
  278. .verify_port = ulite_verify_port,
  279. #ifdef CONFIG_CONSOLE_POLL
  280. .poll_get_char = ulite_get_poll_char,
  281. .poll_put_char = ulite_put_poll_char,
  282. #endif
  283. };
  284. /* ---------------------------------------------------------------------
  285. * Console driver operations
  286. */
  287. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  288. static void ulite_console_wait_tx(struct uart_port *port)
  289. {
  290. int i;
  291. u8 val;
  292. /* Spin waiting for TX fifo to have space available */
  293. for (i = 0; i < 100000; i++) {
  294. val = ioread32be(port->membase + ULITE_STATUS);
  295. if ((val & ULITE_STATUS_TXFULL) == 0)
  296. break;
  297. cpu_relax();
  298. }
  299. }
  300. static void ulite_console_putchar(struct uart_port *port, int ch)
  301. {
  302. ulite_console_wait_tx(port);
  303. iowrite32be(ch, port->membase + ULITE_TX);
  304. }
  305. static void ulite_console_write(struct console *co, const char *s,
  306. unsigned int count)
  307. {
  308. struct uart_port *port = &ulite_ports[co->index];
  309. unsigned long flags;
  310. unsigned int ier;
  311. int locked = 1;
  312. if (oops_in_progress) {
  313. locked = spin_trylock_irqsave(&port->lock, flags);
  314. } else
  315. spin_lock_irqsave(&port->lock, flags);
  316. /* save and disable interrupt */
  317. ier = ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_IE;
  318. iowrite32be(0, port->membase + ULITE_CONTROL);
  319. uart_console_write(port, s, count, ulite_console_putchar);
  320. ulite_console_wait_tx(port);
  321. /* restore interrupt state */
  322. if (ier)
  323. iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
  324. if (locked)
  325. spin_unlock_irqrestore(&port->lock, flags);
  326. }
  327. static int __devinit ulite_console_setup(struct console *co, char *options)
  328. {
  329. struct uart_port *port;
  330. int baud = 9600;
  331. int bits = 8;
  332. int parity = 'n';
  333. int flow = 'n';
  334. if (co->index < 0 || co->index >= ULITE_NR_UARTS)
  335. return -EINVAL;
  336. port = &ulite_ports[co->index];
  337. /* Has the device been initialized yet? */
  338. if (!port->mapbase) {
  339. pr_debug("console on ttyUL%i not present\n", co->index);
  340. return -ENODEV;
  341. }
  342. /* not initialized yet? */
  343. if (!port->membase) {
  344. if (ulite_request_port(port))
  345. return -ENODEV;
  346. }
  347. if (options)
  348. uart_parse_options(options, &baud, &parity, &bits, &flow);
  349. return uart_set_options(port, co, baud, parity, bits, flow);
  350. }
  351. static struct uart_driver ulite_uart_driver;
  352. static struct console ulite_console = {
  353. .name = ULITE_NAME,
  354. .write = ulite_console_write,
  355. .device = uart_console_device,
  356. .setup = ulite_console_setup,
  357. .flags = CON_PRINTBUFFER,
  358. .index = -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
  359. .data = &ulite_uart_driver,
  360. };
  361. static int __init ulite_console_init(void)
  362. {
  363. register_console(&ulite_console);
  364. return 0;
  365. }
  366. console_initcall(ulite_console_init);
  367. #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
  368. static struct uart_driver ulite_uart_driver = {
  369. .owner = THIS_MODULE,
  370. .driver_name = "uartlite",
  371. .dev_name = ULITE_NAME,
  372. .major = ULITE_MAJOR,
  373. .minor = ULITE_MINOR,
  374. .nr = ULITE_NR_UARTS,
  375. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  376. .cons = &ulite_console,
  377. #endif
  378. };
  379. /* ---------------------------------------------------------------------
  380. * Port assignment functions (mapping devices to uart_port structures)
  381. */
  382. /** ulite_assign: register a uartlite device with the driver
  383. *
  384. * @dev: pointer to device structure
  385. * @id: requested id number. Pass -1 for automatic port assignment
  386. * @base: base address of uartlite registers
  387. * @irq: irq number for uartlite
  388. *
  389. * Returns: 0 on success, <0 otherwise
  390. */
  391. static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
  392. {
  393. struct uart_port *port;
  394. int rc;
  395. /* if id = -1; then scan for a free id and use that */
  396. if (id < 0) {
  397. for (id = 0; id < ULITE_NR_UARTS; id++)
  398. if (ulite_ports[id].mapbase == 0)
  399. break;
  400. }
  401. if (id < 0 || id >= ULITE_NR_UARTS) {
  402. dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
  403. return -EINVAL;
  404. }
  405. if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
  406. dev_err(dev, "cannot assign to %s%i; it is already in use\n",
  407. ULITE_NAME, id);
  408. return -EBUSY;
  409. }
  410. port = &ulite_ports[id];
  411. spin_lock_init(&port->lock);
  412. port->fifosize = 16;
  413. port->regshift = 2;
  414. port->iotype = UPIO_MEM;
  415. port->iobase = 1; /* mark port in use */
  416. port->mapbase = base;
  417. port->membase = NULL;
  418. port->ops = &ulite_ops;
  419. port->irq = irq;
  420. port->flags = UPF_BOOT_AUTOCONF;
  421. port->dev = dev;
  422. port->type = PORT_UNKNOWN;
  423. port->line = id;
  424. dev_set_drvdata(dev, port);
  425. /* Register the port */
  426. rc = uart_add_one_port(&ulite_uart_driver, port);
  427. if (rc) {
  428. dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
  429. port->mapbase = 0;
  430. dev_set_drvdata(dev, NULL);
  431. return rc;
  432. }
  433. return 0;
  434. }
  435. /** ulite_release: register a uartlite device with the driver
  436. *
  437. * @dev: pointer to device structure
  438. */
  439. static int __devexit ulite_release(struct device *dev)
  440. {
  441. struct uart_port *port = dev_get_drvdata(dev);
  442. int rc = 0;
  443. if (port) {
  444. rc = uart_remove_one_port(&ulite_uart_driver, port);
  445. dev_set_drvdata(dev, NULL);
  446. port->mapbase = 0;
  447. }
  448. return rc;
  449. }
  450. /* ---------------------------------------------------------------------
  451. * Platform bus binding
  452. */
  453. #if defined(CONFIG_OF)
  454. /* Match table for of_platform binding */
  455. static struct of_device_id ulite_of_match[] __devinitdata = {
  456. { .compatible = "xlnx,opb-uartlite-1.00.b", },
  457. { .compatible = "xlnx,xps-uartlite-1.00.a", },
  458. {}
  459. };
  460. MODULE_DEVICE_TABLE(of, ulite_of_match);
  461. #else /* CONFIG_OF */
  462. #define ulite_of_match NULL
  463. #endif /* CONFIG_OF */
  464. static int __devinit ulite_probe(struct platform_device *pdev)
  465. {
  466. struct resource *res, *res2;
  467. int id = pdev->id;
  468. #ifdef CONFIG_OF
  469. const __be32 *prop;
  470. prop = of_get_property(pdev->dev.of_node, "port-number", NULL);
  471. if (prop)
  472. id = be32_to_cpup(prop);
  473. #endif
  474. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  475. if (!res)
  476. return -ENODEV;
  477. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  478. if (!res2)
  479. return -ENODEV;
  480. return ulite_assign(&pdev->dev, id, res->start, res2->start);
  481. }
  482. static int __devexit ulite_remove(struct platform_device *pdev)
  483. {
  484. return ulite_release(&pdev->dev);
  485. }
  486. /* work with hotplug and coldplug */
  487. MODULE_ALIAS("platform:uartlite");
  488. static struct platform_driver ulite_platform_driver = {
  489. .probe = ulite_probe,
  490. .remove = __devexit_p(ulite_remove),
  491. .driver = {
  492. .owner = THIS_MODULE,
  493. .name = "uartlite",
  494. .of_match_table = ulite_of_match,
  495. },
  496. };
  497. /* ---------------------------------------------------------------------
  498. * Module setup/teardown
  499. */
  500. int __init ulite_init(void)
  501. {
  502. int ret;
  503. pr_debug("uartlite: calling uart_register_driver()\n");
  504. ret = uart_register_driver(&ulite_uart_driver);
  505. if (ret)
  506. goto err_uart;
  507. pr_debug("uartlite: calling platform_driver_register()\n");
  508. ret = platform_driver_register(&ulite_platform_driver);
  509. if (ret)
  510. goto err_plat;
  511. return 0;
  512. err_plat:
  513. uart_unregister_driver(&ulite_uart_driver);
  514. err_uart:
  515. printk(KERN_ERR "registering uartlite driver failed: err=%i", ret);
  516. return ret;
  517. }
  518. void __exit ulite_exit(void)
  519. {
  520. platform_driver_unregister(&ulite_platform_driver);
  521. uart_unregister_driver(&ulite_uart_driver);
  522. }
  523. module_init(ulite_init);
  524. module_exit(ulite_exit);
  525. MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
  526. MODULE_DESCRIPTION("Xilinx uartlite serial driver");
  527. MODULE_LICENSE("GPL");