digicolor-usart.c 14 KB

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