clps711x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for CLPS711x serial ports
  4. *
  5. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  6. *
  7. * Copyright 1999 ARM Limited
  8. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/console.h>
  13. #include <linux/serial_core.h>
  14. #include <linux/serial.h>
  15. #include <linux/clk.h>
  16. #include <linux/io.h>
  17. #include <linux/tty.h>
  18. #include <linux/tty_flip.h>
  19. #include <linux/ioport.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/mfd/syscon.h>
  24. #include <linux/mfd/syscon/clps711x.h>
  25. #include "serial_mctrl_gpio.h"
  26. #define UART_CLPS711X_DEVNAME "ttyCL"
  27. #define UART_CLPS711X_NR 2
  28. #define UART_CLPS711X_MAJOR 204
  29. #define UART_CLPS711X_MINOR 40
  30. #define UARTDR_OFFSET (0x00)
  31. #define UBRLCR_OFFSET (0x40)
  32. #define UARTDR_FRMERR (1 << 8)
  33. #define UARTDR_PARERR (1 << 9)
  34. #define UARTDR_OVERR (1 << 10)
  35. #define UBRLCR_BAUD_MASK ((1 << 12) - 1)
  36. #define UBRLCR_BREAK (1 << 12)
  37. #define UBRLCR_PRTEN (1 << 13)
  38. #define UBRLCR_EVENPRT (1 << 14)
  39. #define UBRLCR_XSTOP (1 << 15)
  40. #define UBRLCR_FIFOEN (1 << 16)
  41. #define UBRLCR_WRDLEN5 (0 << 17)
  42. #define UBRLCR_WRDLEN6 (1 << 17)
  43. #define UBRLCR_WRDLEN7 (2 << 17)
  44. #define UBRLCR_WRDLEN8 (3 << 17)
  45. #define UBRLCR_WRDLEN_MASK (3 << 17)
  46. struct clps711x_port {
  47. struct uart_port port;
  48. unsigned int tx_enabled;
  49. int rx_irq;
  50. struct regmap *syscon;
  51. struct mctrl_gpios *gpios;
  52. };
  53. static struct uart_driver clps711x_uart = {
  54. .owner = THIS_MODULE,
  55. .driver_name = UART_CLPS711X_DEVNAME,
  56. .dev_name = UART_CLPS711X_DEVNAME,
  57. .major = UART_CLPS711X_MAJOR,
  58. .minor = UART_CLPS711X_MINOR,
  59. .nr = UART_CLPS711X_NR,
  60. };
  61. static void uart_clps711x_stop_tx(struct uart_port *port)
  62. {
  63. struct clps711x_port *s = dev_get_drvdata(port->dev);
  64. if (s->tx_enabled) {
  65. disable_irq(port->irq);
  66. s->tx_enabled = 0;
  67. }
  68. }
  69. static void uart_clps711x_start_tx(struct uart_port *port)
  70. {
  71. struct clps711x_port *s = dev_get_drvdata(port->dev);
  72. if (!s->tx_enabled) {
  73. s->tx_enabled = 1;
  74. enable_irq(port->irq);
  75. }
  76. }
  77. static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id)
  78. {
  79. struct uart_port *port = dev_id;
  80. struct clps711x_port *s = dev_get_drvdata(port->dev);
  81. unsigned int status, flg;
  82. u16 ch;
  83. for (;;) {
  84. u32 sysflg = 0;
  85. regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
  86. if (sysflg & SYSFLG_URXFE)
  87. break;
  88. ch = readw(port->membase + UARTDR_OFFSET);
  89. status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR);
  90. ch &= 0xff;
  91. port->icount.rx++;
  92. flg = TTY_NORMAL;
  93. if (unlikely(status)) {
  94. if (status & UARTDR_PARERR)
  95. port->icount.parity++;
  96. else if (status & UARTDR_FRMERR)
  97. port->icount.frame++;
  98. else if (status & UARTDR_OVERR)
  99. port->icount.overrun++;
  100. status &= port->read_status_mask;
  101. if (status & UARTDR_PARERR)
  102. flg = TTY_PARITY;
  103. else if (status & UARTDR_FRMERR)
  104. flg = TTY_FRAME;
  105. else if (status & UARTDR_OVERR)
  106. flg = TTY_OVERRUN;
  107. }
  108. if (uart_handle_sysrq_char(port, ch))
  109. continue;
  110. if (status & port->ignore_status_mask)
  111. continue;
  112. uart_insert_char(port, status, UARTDR_OVERR, ch, flg);
  113. }
  114. tty_flip_buffer_push(&port->state->port);
  115. return IRQ_HANDLED;
  116. }
  117. static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id)
  118. {
  119. struct uart_port *port = dev_id;
  120. struct clps711x_port *s = dev_get_drvdata(port->dev);
  121. struct circ_buf *xmit = &port->state->xmit;
  122. if (port->x_char) {
  123. writew(port->x_char, port->membase + UARTDR_OFFSET);
  124. port->icount.tx++;
  125. port->x_char = 0;
  126. return IRQ_HANDLED;
  127. }
  128. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  129. if (s->tx_enabled) {
  130. disable_irq_nosync(port->irq);
  131. s->tx_enabled = 0;
  132. }
  133. return IRQ_HANDLED;
  134. }
  135. while (!uart_circ_empty(xmit)) {
  136. u32 sysflg = 0;
  137. writew(xmit->buf[xmit->tail], port->membase + UARTDR_OFFSET);
  138. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  139. port->icount.tx++;
  140. regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
  141. if (sysflg & SYSFLG_UTXFF)
  142. break;
  143. }
  144. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  145. uart_write_wakeup(port);
  146. return IRQ_HANDLED;
  147. }
  148. static unsigned int uart_clps711x_tx_empty(struct uart_port *port)
  149. {
  150. struct clps711x_port *s = dev_get_drvdata(port->dev);
  151. u32 sysflg = 0;
  152. regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
  153. return (sysflg & SYSFLG_UBUSY) ? 0 : TIOCSER_TEMT;
  154. }
  155. static unsigned int uart_clps711x_get_mctrl(struct uart_port *port)
  156. {
  157. unsigned int result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
  158. struct clps711x_port *s = dev_get_drvdata(port->dev);
  159. return mctrl_gpio_get(s->gpios, &result);
  160. }
  161. static void uart_clps711x_set_mctrl(struct uart_port *port, unsigned int mctrl)
  162. {
  163. struct clps711x_port *s = dev_get_drvdata(port->dev);
  164. mctrl_gpio_set(s->gpios, mctrl);
  165. }
  166. static void uart_clps711x_break_ctl(struct uart_port *port, int break_state)
  167. {
  168. unsigned int ubrlcr;
  169. ubrlcr = readl(port->membase + UBRLCR_OFFSET);
  170. if (break_state)
  171. ubrlcr |= UBRLCR_BREAK;
  172. else
  173. ubrlcr &= ~UBRLCR_BREAK;
  174. writel(ubrlcr, port->membase + UBRLCR_OFFSET);
  175. }
  176. static void uart_clps711x_set_ldisc(struct uart_port *port,
  177. struct ktermios *termios)
  178. {
  179. if (!port->line) {
  180. struct clps711x_port *s = dev_get_drvdata(port->dev);
  181. regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON1_SIREN,
  182. (termios->c_line == N_IRDA) ? SYSCON1_SIREN : 0);
  183. }
  184. }
  185. static int uart_clps711x_startup(struct uart_port *port)
  186. {
  187. struct clps711x_port *s = dev_get_drvdata(port->dev);
  188. /* Disable break */
  189. writel(readl(port->membase + UBRLCR_OFFSET) & ~UBRLCR_BREAK,
  190. port->membase + UBRLCR_OFFSET);
  191. /* Enable the port */
  192. return regmap_update_bits(s->syscon, SYSCON_OFFSET,
  193. SYSCON_UARTEN, SYSCON_UARTEN);
  194. }
  195. static void uart_clps711x_shutdown(struct uart_port *port)
  196. {
  197. struct clps711x_port *s = dev_get_drvdata(port->dev);
  198. /* Disable the port */
  199. regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
  200. }
  201. static void uart_clps711x_set_termios(struct uart_port *port,
  202. struct ktermios *termios,
  203. const struct ktermios *old)
  204. {
  205. u32 ubrlcr;
  206. unsigned int baud, quot;
  207. /* Mask termios capabilities we don't support */
  208. termios->c_cflag &= ~CMSPAR;
  209. termios->c_iflag &= ~(BRKINT | IGNBRK);
  210. /* Ask the core to calculate the divisor for us */
  211. baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096,
  212. port->uartclk / 16);
  213. quot = uart_get_divisor(port, baud);
  214. switch (termios->c_cflag & CSIZE) {
  215. case CS5:
  216. ubrlcr = UBRLCR_WRDLEN5;
  217. break;
  218. case CS6:
  219. ubrlcr = UBRLCR_WRDLEN6;
  220. break;
  221. case CS7:
  222. ubrlcr = UBRLCR_WRDLEN7;
  223. break;
  224. case CS8:
  225. default:
  226. ubrlcr = UBRLCR_WRDLEN8;
  227. break;
  228. }
  229. if (termios->c_cflag & CSTOPB)
  230. ubrlcr |= UBRLCR_XSTOP;
  231. if (termios->c_cflag & PARENB) {
  232. ubrlcr |= UBRLCR_PRTEN;
  233. if (!(termios->c_cflag & PARODD))
  234. ubrlcr |= UBRLCR_EVENPRT;
  235. }
  236. /* Enable FIFO */
  237. ubrlcr |= UBRLCR_FIFOEN;
  238. /* Set read status mask */
  239. port->read_status_mask = UARTDR_OVERR;
  240. if (termios->c_iflag & INPCK)
  241. port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
  242. /* Set status ignore mask */
  243. port->ignore_status_mask = 0;
  244. if (!(termios->c_cflag & CREAD))
  245. port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR |
  246. UARTDR_FRMERR;
  247. uart_update_timeout(port, termios->c_cflag, baud);
  248. writel(ubrlcr | (quot - 1), port->membase + UBRLCR_OFFSET);
  249. }
  250. static const char *uart_clps711x_type(struct uart_port *port)
  251. {
  252. return (port->type == PORT_CLPS711X) ? "CLPS711X" : NULL;
  253. }
  254. static void uart_clps711x_config_port(struct uart_port *port, int flags)
  255. {
  256. if (flags & UART_CONFIG_TYPE)
  257. port->type = PORT_CLPS711X;
  258. }
  259. static void uart_clps711x_nop_void(struct uart_port *port)
  260. {
  261. }
  262. static int uart_clps711x_nop_int(struct uart_port *port)
  263. {
  264. return 0;
  265. }
  266. static const struct uart_ops uart_clps711x_ops = {
  267. .tx_empty = uart_clps711x_tx_empty,
  268. .set_mctrl = uart_clps711x_set_mctrl,
  269. .get_mctrl = uart_clps711x_get_mctrl,
  270. .stop_tx = uart_clps711x_stop_tx,
  271. .start_tx = uart_clps711x_start_tx,
  272. .stop_rx = uart_clps711x_nop_void,
  273. .break_ctl = uart_clps711x_break_ctl,
  274. .set_ldisc = uart_clps711x_set_ldisc,
  275. .startup = uart_clps711x_startup,
  276. .shutdown = uart_clps711x_shutdown,
  277. .set_termios = uart_clps711x_set_termios,
  278. .type = uart_clps711x_type,
  279. .config_port = uart_clps711x_config_port,
  280. .release_port = uart_clps711x_nop_void,
  281. .request_port = uart_clps711x_nop_int,
  282. };
  283. #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
  284. static void uart_clps711x_console_putchar(struct uart_port *port, unsigned char ch)
  285. {
  286. struct clps711x_port *s = dev_get_drvdata(port->dev);
  287. u32 sysflg = 0;
  288. /* Wait for FIFO is not full */
  289. do {
  290. regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
  291. } while (sysflg & SYSFLG_UTXFF);
  292. writew(ch, port->membase + UARTDR_OFFSET);
  293. }
  294. static void uart_clps711x_console_write(struct console *co, const char *c,
  295. unsigned n)
  296. {
  297. struct uart_port *port = clps711x_uart.state[co->index].uart_port;
  298. struct clps711x_port *s = dev_get_drvdata(port->dev);
  299. u32 sysflg = 0;
  300. uart_console_write(port, c, n, uart_clps711x_console_putchar);
  301. /* Wait for transmitter to become empty */
  302. do {
  303. regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
  304. } while (sysflg & SYSFLG_UBUSY);
  305. }
  306. static int uart_clps711x_console_setup(struct console *co, char *options)
  307. {
  308. int baud = 38400, bits = 8, parity = 'n', flow = 'n';
  309. int ret, index = co->index;
  310. struct clps711x_port *s;
  311. struct uart_port *port;
  312. unsigned int quot;
  313. u32 ubrlcr;
  314. if (index < 0 || index >= UART_CLPS711X_NR)
  315. return -EINVAL;
  316. port = clps711x_uart.state[index].uart_port;
  317. if (!port)
  318. return -ENODEV;
  319. s = dev_get_drvdata(port->dev);
  320. if (!options) {
  321. u32 syscon = 0;
  322. regmap_read(s->syscon, SYSCON_OFFSET, &syscon);
  323. if (syscon & SYSCON_UARTEN) {
  324. ubrlcr = readl(port->membase + UBRLCR_OFFSET);
  325. if (ubrlcr & UBRLCR_PRTEN) {
  326. if (ubrlcr & UBRLCR_EVENPRT)
  327. parity = 'e';
  328. else
  329. parity = 'o';
  330. }
  331. if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
  332. bits = 7;
  333. quot = ubrlcr & UBRLCR_BAUD_MASK;
  334. baud = port->uartclk / (16 * (quot + 1));
  335. }
  336. } else
  337. uart_parse_options(options, &baud, &parity, &bits, &flow);
  338. ret = uart_set_options(port, co, baud, parity, bits, flow);
  339. if (ret)
  340. return ret;
  341. return regmap_update_bits(s->syscon, SYSCON_OFFSET,
  342. SYSCON_UARTEN, SYSCON_UARTEN);
  343. }
  344. static struct console clps711x_console = {
  345. .name = UART_CLPS711X_DEVNAME,
  346. .device = uart_console_device,
  347. .write = uart_clps711x_console_write,
  348. .setup = uart_clps711x_console_setup,
  349. .flags = CON_PRINTBUFFER,
  350. .index = -1,
  351. };
  352. #endif
  353. static int uart_clps711x_probe(struct platform_device *pdev)
  354. {
  355. struct device_node *np = pdev->dev.of_node;
  356. struct clps711x_port *s;
  357. struct resource *res;
  358. struct clk *uart_clk;
  359. int irq, ret;
  360. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  361. if (!s)
  362. return -ENOMEM;
  363. uart_clk = devm_clk_get(&pdev->dev, NULL);
  364. if (IS_ERR(uart_clk))
  365. return PTR_ERR(uart_clk);
  366. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  367. s->port.membase = devm_ioremap_resource(&pdev->dev, res);
  368. if (IS_ERR(s->port.membase))
  369. return PTR_ERR(s->port.membase);
  370. irq = platform_get_irq(pdev, 0);
  371. if (irq < 0)
  372. return irq;
  373. s->port.irq = irq;
  374. s->rx_irq = platform_get_irq(pdev, 1);
  375. if (s->rx_irq < 0)
  376. return s->rx_irq;
  377. s->syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
  378. if (IS_ERR(s->syscon))
  379. return PTR_ERR(s->syscon);
  380. s->port.line = of_alias_get_id(np, "serial");
  381. s->port.dev = &pdev->dev;
  382. s->port.iotype = UPIO_MEM32;
  383. s->port.mapbase = res->start;
  384. s->port.type = PORT_CLPS711X;
  385. s->port.fifosize = 16;
  386. s->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_CLPS711X_CONSOLE);
  387. s->port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
  388. s->port.uartclk = clk_get_rate(uart_clk);
  389. s->port.ops = &uart_clps711x_ops;
  390. platform_set_drvdata(pdev, s);
  391. s->gpios = mctrl_gpio_init_noauto(&pdev->dev, 0);
  392. if (IS_ERR(s->gpios))
  393. return PTR_ERR(s->gpios);
  394. ret = uart_add_one_port(&clps711x_uart, &s->port);
  395. if (ret)
  396. return ret;
  397. /* Disable port */
  398. if (!uart_console(&s->port))
  399. regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
  400. s->tx_enabled = 1;
  401. ret = devm_request_irq(&pdev->dev, s->port.irq, uart_clps711x_int_tx, 0,
  402. dev_name(&pdev->dev), &s->port);
  403. if (ret) {
  404. uart_remove_one_port(&clps711x_uart, &s->port);
  405. return ret;
  406. }
  407. ret = devm_request_irq(&pdev->dev, s->rx_irq, uart_clps711x_int_rx, 0,
  408. dev_name(&pdev->dev), &s->port);
  409. if (ret)
  410. uart_remove_one_port(&clps711x_uart, &s->port);
  411. return ret;
  412. }
  413. static int uart_clps711x_remove(struct platform_device *pdev)
  414. {
  415. struct clps711x_port *s = platform_get_drvdata(pdev);
  416. return uart_remove_one_port(&clps711x_uart, &s->port);
  417. }
  418. static const struct of_device_id __maybe_unused clps711x_uart_dt_ids[] = {
  419. { .compatible = "cirrus,ep7209-uart", },
  420. { }
  421. };
  422. MODULE_DEVICE_TABLE(of, clps711x_uart_dt_ids);
  423. static struct platform_driver clps711x_uart_platform = {
  424. .driver = {
  425. .name = "clps711x-uart",
  426. .of_match_table = of_match_ptr(clps711x_uart_dt_ids),
  427. },
  428. .probe = uart_clps711x_probe,
  429. .remove = uart_clps711x_remove,
  430. };
  431. static int __init uart_clps711x_init(void)
  432. {
  433. int ret;
  434. #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
  435. clps711x_uart.cons = &clps711x_console;
  436. clps711x_console.data = &clps711x_uart;
  437. #endif
  438. ret = uart_register_driver(&clps711x_uart);
  439. if (ret)
  440. return ret;
  441. return platform_driver_register(&clps711x_uart_platform);
  442. }
  443. module_init(uart_clps711x_init);
  444. static void __exit uart_clps711x_exit(void)
  445. {
  446. platform_driver_unregister(&clps711x_uart_platform);
  447. uart_unregister_driver(&clps711x_uart);
  448. }
  449. module_exit(uart_clps711x_exit);
  450. MODULE_AUTHOR("Deep Blue Solutions Ltd");
  451. MODULE_DESCRIPTION("CLPS711X serial driver");
  452. MODULE_LICENSE("GPL");