digicolor-usart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Driver for Conexant Digicolor serial ports (USART)
  3. *
  4. * Author: Baruch Siach <baruch@tkos.co.il>
  5. *
  6. * Copyright (C) 2014 Paradox Innovation Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/console.h>
  15. #include <linux/serial_core.h>
  16. #include <linux/serial.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/workqueue.h>
  24. #define UA_ENABLE 0x00
  25. #define UA_ENABLE_ENABLE BIT(0)
  26. #define UA_CONTROL 0x01
  27. #define UA_CONTROL_RX_ENABLE BIT(0)
  28. #define UA_CONTROL_TX_ENABLE BIT(1)
  29. #define UA_CONTROL_SOFT_RESET BIT(2)
  30. #define UA_STATUS 0x02
  31. #define UA_STATUS_PARITY_ERR BIT(0)
  32. #define UA_STATUS_FRAME_ERR BIT(1)
  33. #define UA_STATUS_OVERRUN_ERR BIT(2)
  34. #define UA_STATUS_TX_READY BIT(6)
  35. #define UA_CONFIG 0x03
  36. #define UA_CONFIG_CHAR_LEN BIT(0)
  37. #define UA_CONFIG_STOP_BITS BIT(1)
  38. #define UA_CONFIG_PARITY BIT(2)
  39. #define UA_CONFIG_ODD_PARITY BIT(4)
  40. #define UA_EMI_REC 0x04
  41. #define UA_HBAUD_LO 0x08
  42. #define UA_HBAUD_HI 0x09
  43. #define UA_STATUS_FIFO 0x0a
  44. #define UA_STATUS_FIFO_RX_EMPTY BIT(2)
  45. #define UA_STATUS_FIFO_RX_INT_ALMOST BIT(3)
  46. #define UA_STATUS_FIFO_TX_FULL BIT(4)
  47. #define UA_STATUS_FIFO_TX_INT_ALMOST BIT(7)
  48. #define UA_CONFIG_FIFO 0x0b
  49. #define UA_CONFIG_FIFO_RX_THRESH 7
  50. #define UA_CONFIG_FIFO_RX_FIFO_MODE BIT(3)
  51. #define UA_CONFIG_FIFO_TX_FIFO_MODE BIT(7)
  52. #define UA_INTFLAG_CLEAR 0x1c
  53. #define UA_INTFLAG_SET 0x1d
  54. #define UA_INT_ENABLE 0x1e
  55. #define UA_INT_STATUS 0x1f
  56. #define UA_INT_TX BIT(0)
  57. #define UA_INT_RX BIT(1)
  58. #define DIGICOLOR_USART_NR 3
  59. /*
  60. * We use the 16 bytes hardware FIFO to buffer Rx traffic. Rx interrupt is
  61. * only produced when the FIFO is filled more than a certain configurable
  62. * threshold. Unfortunately, there is no way to set this threshold below half
  63. * FIFO. This means that we must periodically poll the FIFO status register to
  64. * see whether there are waiting Rx bytes.
  65. */
  66. struct digicolor_port {
  67. struct uart_port port;
  68. struct delayed_work rx_poll_work;
  69. };
  70. static struct uart_port *digicolor_ports[DIGICOLOR_USART_NR];
  71. static bool digicolor_uart_tx_full(struct uart_port *port)
  72. {
  73. return !!(readb_relaxed(port->membase + UA_STATUS_FIFO) &
  74. UA_STATUS_FIFO_TX_FULL);
  75. }
  76. static bool digicolor_uart_rx_empty(struct uart_port *port)
  77. {
  78. return !!(readb_relaxed(port->membase + UA_STATUS_FIFO) &
  79. UA_STATUS_FIFO_RX_EMPTY);
  80. }
  81. static void digicolor_uart_stop_tx(struct uart_port *port)
  82. {
  83. u8 int_enable = readb_relaxed(port->membase + UA_INT_ENABLE);
  84. int_enable &= ~UA_INT_TX;
  85. writeb_relaxed(int_enable, port->membase + UA_INT_ENABLE);
  86. }
  87. static void digicolor_uart_start_tx(struct uart_port *port)
  88. {
  89. u8 int_enable = readb_relaxed(port->membase + UA_INT_ENABLE);
  90. int_enable |= UA_INT_TX;
  91. writeb_relaxed(int_enable, port->membase + UA_INT_ENABLE);
  92. }
  93. static void digicolor_uart_stop_rx(struct uart_port *port)
  94. {
  95. u8 int_enable = readb_relaxed(port->membase + UA_INT_ENABLE);
  96. int_enable &= ~UA_INT_RX;
  97. writeb_relaxed(int_enable, port->membase + UA_INT_ENABLE);
  98. }
  99. static void digicolor_rx_poll(struct work_struct *work)
  100. {
  101. struct digicolor_port *dp =
  102. container_of(to_delayed_work(work),
  103. struct digicolor_port, rx_poll_work);
  104. if (!digicolor_uart_rx_empty(&dp->port))
  105. /* force RX interrupt */
  106. writeb_relaxed(UA_INT_RX, dp->port.membase + UA_INTFLAG_SET);
  107. schedule_delayed_work(&dp->rx_poll_work, msecs_to_jiffies(100));
  108. }
  109. static void digicolor_uart_rx(struct uart_port *port)
  110. {
  111. unsigned long flags;
  112. spin_lock_irqsave(&port->lock, flags);
  113. while (1) {
  114. u8 status, ch;
  115. unsigned int ch_flag;
  116. if (digicolor_uart_rx_empty(port))
  117. break;
  118. ch = readb_relaxed(port->membase + UA_EMI_REC);
  119. status = readb_relaxed(port->membase + UA_STATUS);
  120. port->icount.rx++;
  121. ch_flag = TTY_NORMAL;
  122. if (status) {
  123. if (status & UA_STATUS_PARITY_ERR)
  124. port->icount.parity++;
  125. else if (status & UA_STATUS_FRAME_ERR)
  126. port->icount.frame++;
  127. else if (status & UA_STATUS_OVERRUN_ERR)
  128. port->icount.overrun++;
  129. status &= port->read_status_mask;
  130. if (status & UA_STATUS_PARITY_ERR)
  131. ch_flag = TTY_PARITY;
  132. else if (status & UA_STATUS_FRAME_ERR)
  133. ch_flag = TTY_FRAME;
  134. else if (status & UA_STATUS_OVERRUN_ERR)
  135. ch_flag = TTY_OVERRUN;
  136. }
  137. if (status & port->ignore_status_mask)
  138. continue;
  139. uart_insert_char(port, status, UA_STATUS_OVERRUN_ERR, ch,
  140. ch_flag);
  141. }
  142. spin_unlock_irqrestore(&port->lock, flags);
  143. tty_flip_buffer_push(&port->state->port);
  144. }
  145. static void digicolor_uart_tx(struct uart_port *port)
  146. {
  147. struct circ_buf *xmit = &port->state->xmit;
  148. unsigned long flags;
  149. if (digicolor_uart_tx_full(port))
  150. return;
  151. spin_lock_irqsave(&port->lock, flags);
  152. if (port->x_char) {
  153. writeb_relaxed(port->x_char, port->membase + UA_EMI_REC);
  154. port->icount.tx++;
  155. port->x_char = 0;
  156. goto out;
  157. }
  158. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  159. digicolor_uart_stop_tx(port);
  160. goto out;
  161. }
  162. while (!uart_circ_empty(xmit)) {
  163. writeb(xmit->buf[xmit->tail], port->membase + UA_EMI_REC);
  164. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  165. port->icount.tx++;
  166. if (digicolor_uart_tx_full(port))
  167. break;
  168. }
  169. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  170. uart_write_wakeup(port);
  171. out:
  172. spin_unlock_irqrestore(&port->lock, flags);
  173. }
  174. static irqreturn_t digicolor_uart_int(int irq, void *dev_id)
  175. {
  176. struct uart_port *port = dev_id;
  177. u8 int_status = readb_relaxed(port->membase + UA_INT_STATUS);
  178. writeb_relaxed(UA_INT_RX | UA_INT_TX,
  179. port->membase + UA_INTFLAG_CLEAR);
  180. if (int_status & UA_INT_RX)
  181. digicolor_uart_rx(port);
  182. if (int_status & UA_INT_TX)
  183. digicolor_uart_tx(port);
  184. return IRQ_HANDLED;
  185. }
  186. static unsigned int digicolor_uart_tx_empty(struct uart_port *port)
  187. {
  188. u8 status = readb_relaxed(port->membase + UA_STATUS);
  189. return (status & UA_STATUS_TX_READY) ? TIOCSER_TEMT : 0;
  190. }
  191. static unsigned int digicolor_uart_get_mctrl(struct uart_port *port)
  192. {
  193. return TIOCM_CTS;
  194. }
  195. static void digicolor_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  196. {
  197. }
  198. static void digicolor_uart_break_ctl(struct uart_port *port, int state)
  199. {
  200. }
  201. static int digicolor_uart_startup(struct uart_port *port)
  202. {
  203. struct digicolor_port *dp =
  204. container_of(port, struct digicolor_port, port);
  205. writeb_relaxed(UA_ENABLE_ENABLE, port->membase + UA_ENABLE);
  206. writeb_relaxed(UA_CONTROL_SOFT_RESET, port->membase + UA_CONTROL);
  207. writeb_relaxed(0, port->membase + UA_CONTROL);
  208. writeb_relaxed(UA_CONFIG_FIFO_RX_FIFO_MODE
  209. | UA_CONFIG_FIFO_TX_FIFO_MODE | UA_CONFIG_FIFO_RX_THRESH,
  210. port->membase + UA_CONFIG_FIFO);
  211. writeb_relaxed(UA_STATUS_FIFO_RX_INT_ALMOST,
  212. port->membase + UA_STATUS_FIFO);
  213. writeb_relaxed(UA_CONTROL_RX_ENABLE | UA_CONTROL_TX_ENABLE,
  214. port->membase + UA_CONTROL);
  215. writeb_relaxed(UA_INT_TX | UA_INT_RX,
  216. port->membase + UA_INT_ENABLE);
  217. schedule_delayed_work(&dp->rx_poll_work, msecs_to_jiffies(100));
  218. return 0;
  219. }
  220. static void digicolor_uart_shutdown(struct uart_port *port)
  221. {
  222. struct digicolor_port *dp =
  223. container_of(port, struct digicolor_port, port);
  224. writeb_relaxed(0, port->membase + UA_ENABLE);
  225. cancel_delayed_work_sync(&dp->rx_poll_work);
  226. }
  227. static void digicolor_uart_set_termios(struct uart_port *port,
  228. struct ktermios *termios,
  229. struct ktermios *old)
  230. {
  231. unsigned int baud, divisor;
  232. u8 config = 0;
  233. unsigned long flags;
  234. /* Mask termios capabilities we don't support */
  235. termios->c_cflag &= ~CMSPAR;
  236. termios->c_iflag &= ~(BRKINT | IGNBRK);
  237. /* Limit baud rates so that we don't need the fractional divider */
  238. baud = uart_get_baud_rate(port, termios, old,
  239. port->uartclk / (0x10000*16),
  240. port->uartclk / 256);
  241. divisor = uart_get_divisor(port, baud) - 1;
  242. switch (termios->c_cflag & CSIZE) {
  243. case CS7:
  244. break;
  245. case CS8:
  246. default:
  247. config |= UA_CONFIG_CHAR_LEN;
  248. break;
  249. }
  250. if (termios->c_cflag & CSTOPB)
  251. config |= UA_CONFIG_STOP_BITS;
  252. if (termios->c_cflag & PARENB) {
  253. config |= UA_CONFIG_PARITY;
  254. if (termios->c_cflag & PARODD)
  255. config |= UA_CONFIG_ODD_PARITY;
  256. }
  257. /* Set read status mask */
  258. port->read_status_mask = UA_STATUS_OVERRUN_ERR;
  259. if (termios->c_iflag & INPCK)
  260. port->read_status_mask |= UA_STATUS_PARITY_ERR
  261. | UA_STATUS_FRAME_ERR;
  262. /* Set status ignore mask */
  263. port->ignore_status_mask = 0;
  264. if (!(termios->c_cflag & CREAD))
  265. port->ignore_status_mask |= UA_STATUS_OVERRUN_ERR
  266. | UA_STATUS_PARITY_ERR | UA_STATUS_FRAME_ERR;
  267. spin_lock_irqsave(&port->lock, flags);
  268. uart_update_timeout(port, termios->c_cflag, baud);
  269. writeb_relaxed(config, port->membase + UA_CONFIG);
  270. writeb_relaxed(divisor & 0xff, port->membase + UA_HBAUD_LO);
  271. writeb_relaxed(divisor >> 8, port->membase + UA_HBAUD_HI);
  272. spin_unlock_irqrestore(&port->lock, flags);
  273. }
  274. static const char *digicolor_uart_type(struct uart_port *port)
  275. {
  276. return (port->type == PORT_DIGICOLOR) ? "DIGICOLOR USART" : NULL;
  277. }
  278. static void digicolor_uart_config_port(struct uart_port *port, int flags)
  279. {
  280. if (flags & UART_CONFIG_TYPE)
  281. port->type = PORT_DIGICOLOR;
  282. }
  283. static void digicolor_uart_release_port(struct uart_port *port)
  284. {
  285. }
  286. static int digicolor_uart_request_port(struct uart_port *port)
  287. {
  288. return 0;
  289. }
  290. static const struct uart_ops digicolor_uart_ops = {
  291. .tx_empty = digicolor_uart_tx_empty,
  292. .set_mctrl = digicolor_uart_set_mctrl,
  293. .get_mctrl = digicolor_uart_get_mctrl,
  294. .stop_tx = digicolor_uart_stop_tx,
  295. .start_tx = digicolor_uart_start_tx,
  296. .stop_rx = digicolor_uart_stop_rx,
  297. .break_ctl = digicolor_uart_break_ctl,
  298. .startup = digicolor_uart_startup,
  299. .shutdown = digicolor_uart_shutdown,
  300. .set_termios = digicolor_uart_set_termios,
  301. .type = digicolor_uart_type,
  302. .config_port = digicolor_uart_config_port,
  303. .release_port = digicolor_uart_release_port,
  304. .request_port = digicolor_uart_request_port,
  305. };
  306. static void digicolor_uart_console_putchar(struct uart_port *port, int ch)
  307. {
  308. while (digicolor_uart_tx_full(port))
  309. cpu_relax();
  310. writeb_relaxed(ch, port->membase + UA_EMI_REC);
  311. }
  312. static void digicolor_uart_console_write(struct console *co, const char *c,
  313. unsigned n)
  314. {
  315. struct uart_port *port = digicolor_ports[co->index];
  316. u8 status;
  317. unsigned long flags;
  318. int locked = 1;
  319. if (oops_in_progress)
  320. locked = spin_trylock_irqsave(&port->lock, flags);
  321. else
  322. spin_lock_irqsave(&port->lock, flags);
  323. uart_console_write(port, c, n, digicolor_uart_console_putchar);
  324. if (locked)
  325. spin_unlock_irqrestore(&port->lock, flags);
  326. /* Wait for transmitter to become empty */
  327. do {
  328. status = readb_relaxed(port->membase + UA_STATUS);
  329. } while ((status & UA_STATUS_TX_READY) == 0);
  330. }
  331. static int digicolor_uart_console_setup(struct console *co, char *options)
  332. {
  333. int baud = 115200, bits = 8, parity = 'n', flow = 'n';
  334. struct uart_port *port;
  335. if (co->index < 0 || co->index >= DIGICOLOR_USART_NR)
  336. return -EINVAL;
  337. port = digicolor_ports[co->index];
  338. if (!port)
  339. return -ENODEV;
  340. if (options)
  341. uart_parse_options(options, &baud, &parity, &bits, &flow);
  342. return uart_set_options(port, co, baud, parity, bits, flow);
  343. }
  344. static struct console digicolor_console = {
  345. .name = "ttyS",
  346. .device = uart_console_device,
  347. .write = digicolor_uart_console_write,
  348. .setup = digicolor_uart_console_setup,
  349. .flags = CON_PRINTBUFFER,
  350. .index = -1,
  351. };
  352. static struct uart_driver digicolor_uart = {
  353. .driver_name = "digicolor-usart",
  354. .dev_name = "ttyS",
  355. .nr = DIGICOLOR_USART_NR,
  356. };
  357. static int digicolor_uart_probe(struct platform_device *pdev)
  358. {
  359. struct device_node *np = pdev->dev.of_node;
  360. int ret, index;
  361. struct digicolor_port *dp;
  362. struct resource *res;
  363. struct clk *uart_clk;
  364. if (!np) {
  365. dev_err(&pdev->dev, "Missing device tree node\n");
  366. return -ENXIO;
  367. }
  368. index = of_alias_get_id(np, "serial");
  369. if (index < 0 || index >= DIGICOLOR_USART_NR)
  370. return -EINVAL;
  371. dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
  372. if (!dp)
  373. return -ENOMEM;
  374. uart_clk = devm_clk_get(&pdev->dev, NULL);
  375. if (IS_ERR(uart_clk))
  376. return PTR_ERR(uart_clk);
  377. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  378. dp->port.mapbase = res->start;
  379. dp->port.membase = devm_ioremap_resource(&pdev->dev, res);
  380. if (IS_ERR(dp->port.membase))
  381. return PTR_ERR(dp->port.membase);
  382. dp->port.irq = platform_get_irq(pdev, 0);
  383. if (IS_ERR_VALUE(dp->port.irq))
  384. return dp->port.irq;
  385. dp->port.iotype = UPIO_MEM;
  386. dp->port.uartclk = clk_get_rate(uart_clk);
  387. dp->port.fifosize = 16;
  388. dp->port.dev = &pdev->dev;
  389. dp->port.ops = &digicolor_uart_ops;
  390. dp->port.line = index;
  391. dp->port.type = PORT_DIGICOLOR;
  392. spin_lock_init(&dp->port.lock);
  393. digicolor_ports[index] = &dp->port;
  394. platform_set_drvdata(pdev, &dp->port);
  395. INIT_DELAYED_WORK(&dp->rx_poll_work, digicolor_rx_poll);
  396. ret = devm_request_irq(&pdev->dev, dp->port.irq, digicolor_uart_int, 0,
  397. dev_name(&pdev->dev), &dp->port);
  398. if (ret)
  399. return ret;
  400. return uart_add_one_port(&digicolor_uart, &dp->port);
  401. }
  402. static int digicolor_uart_remove(struct platform_device *pdev)
  403. {
  404. struct uart_port *port = platform_get_drvdata(pdev);
  405. uart_remove_one_port(&digicolor_uart, port);
  406. return 0;
  407. }
  408. static const struct of_device_id digicolor_uart_dt_ids[] = {
  409. { .compatible = "cnxt,cx92755-usart", },
  410. { }
  411. };
  412. MODULE_DEVICE_TABLE(of, digicolor_uart_dt_ids);
  413. static struct platform_driver digicolor_uart_platform = {
  414. .driver = {
  415. .name = "digicolor-usart",
  416. .of_match_table = of_match_ptr(digicolor_uart_dt_ids),
  417. },
  418. .probe = digicolor_uart_probe,
  419. .remove = digicolor_uart_remove,
  420. };
  421. static int __init digicolor_uart_init(void)
  422. {
  423. int ret;
  424. if (IS_ENABLED(CONFIG_SERIAL_CONEXANT_DIGICOLOR_CONSOLE)) {
  425. digicolor_uart.cons = &digicolor_console;
  426. digicolor_console.data = &digicolor_uart;
  427. }
  428. ret = uart_register_driver(&digicolor_uart);
  429. if (ret)
  430. return ret;
  431. return platform_driver_register(&digicolor_uart_platform);
  432. }
  433. module_init(digicolor_uart_init);
  434. static void __exit digicolor_uart_exit(void)
  435. {
  436. platform_driver_unregister(&digicolor_uart_platform);
  437. uart_unregister_driver(&digicolor_uart);
  438. }
  439. module_exit(digicolor_uart_exit);
  440. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  441. MODULE_DESCRIPTION("Conexant Digicolor USART serial driver");
  442. MODULE_LICENSE("GPL");