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