uartlite.c 16 KB

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