clps711x.c 13 KB

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