clps711x.c 14 KB

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