clps711x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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,
  184. struct ktermios *termios)
  185. {
  186. if (!port->line) {
  187. struct clps711x_port *s = dev_get_drvdata(port->dev);
  188. regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON1_SIREN,
  189. (termios->c_line == N_IRDA) ? SYSCON1_SIREN : 0);
  190. }
  191. }
  192. static int uart_clps711x_startup(struct uart_port *port)
  193. {
  194. struct clps711x_port *s = dev_get_drvdata(port->dev);
  195. /* Disable break */
  196. writel(readl(port->membase + UBRLCR_OFFSET) & ~UBRLCR_BREAK,
  197. port->membase + UBRLCR_OFFSET);
  198. /* Enable the port */
  199. return regmap_update_bits(s->syscon, SYSCON_OFFSET,
  200. SYSCON_UARTEN, SYSCON_UARTEN);
  201. }
  202. static void uart_clps711x_shutdown(struct uart_port *port)
  203. {
  204. struct clps711x_port *s = dev_get_drvdata(port->dev);
  205. /* Disable the port */
  206. regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
  207. }
  208. static void uart_clps711x_set_termios(struct uart_port *port,
  209. struct ktermios *termios,
  210. struct ktermios *old)
  211. {
  212. u32 ubrlcr;
  213. unsigned int baud, quot;
  214. /* Mask termios capabilities we don't support */
  215. termios->c_cflag &= ~CMSPAR;
  216. termios->c_iflag &= ~(BRKINT | IGNBRK);
  217. /* Ask the core to calculate the divisor for us */
  218. baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096,
  219. port->uartclk / 16);
  220. quot = uart_get_divisor(port, baud);
  221. switch (termios->c_cflag & CSIZE) {
  222. case CS5:
  223. ubrlcr = UBRLCR_WRDLEN5;
  224. break;
  225. case CS6:
  226. ubrlcr = UBRLCR_WRDLEN6;
  227. break;
  228. case CS7:
  229. ubrlcr = UBRLCR_WRDLEN7;
  230. break;
  231. case CS8:
  232. default:
  233. ubrlcr = UBRLCR_WRDLEN8;
  234. break;
  235. }
  236. if (termios->c_cflag & CSTOPB)
  237. ubrlcr |= UBRLCR_XSTOP;
  238. if (termios->c_cflag & PARENB) {
  239. ubrlcr |= UBRLCR_PRTEN;
  240. if (!(termios->c_cflag & PARODD))
  241. ubrlcr |= UBRLCR_EVENPRT;
  242. }
  243. /* Enable FIFO */
  244. ubrlcr |= UBRLCR_FIFOEN;
  245. /* Set read status mask */
  246. port->read_status_mask = UARTDR_OVERR;
  247. if (termios->c_iflag & INPCK)
  248. port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
  249. /* Set status ignore mask */
  250. port->ignore_status_mask = 0;
  251. if (!(termios->c_cflag & CREAD))
  252. port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR |
  253. UARTDR_FRMERR;
  254. uart_update_timeout(port, termios->c_cflag, baud);
  255. writel(ubrlcr | (quot - 1), port->membase + UBRLCR_OFFSET);
  256. }
  257. static const char *uart_clps711x_type(struct uart_port *port)
  258. {
  259. return (port->type == PORT_CLPS711X) ? "CLPS711X" : NULL;
  260. }
  261. static void uart_clps711x_config_port(struct uart_port *port, int flags)
  262. {
  263. if (flags & UART_CONFIG_TYPE)
  264. port->type = PORT_CLPS711X;
  265. }
  266. static void uart_clps711x_nop_void(struct uart_port *port)
  267. {
  268. }
  269. static int uart_clps711x_nop_int(struct uart_port *port)
  270. {
  271. return 0;
  272. }
  273. static const struct uart_ops uart_clps711x_ops = {
  274. .tx_empty = uart_clps711x_tx_empty,
  275. .set_mctrl = uart_clps711x_set_mctrl,
  276. .get_mctrl = uart_clps711x_get_mctrl,
  277. .stop_tx = uart_clps711x_stop_tx,
  278. .start_tx = uart_clps711x_start_tx,
  279. .stop_rx = uart_clps711x_nop_void,
  280. .break_ctl = uart_clps711x_break_ctl,
  281. .set_ldisc = uart_clps711x_set_ldisc,
  282. .startup = uart_clps711x_startup,
  283. .shutdown = uart_clps711x_shutdown,
  284. .set_termios = uart_clps711x_set_termios,
  285. .type = uart_clps711x_type,
  286. .config_port = uart_clps711x_config_port,
  287. .release_port = uart_clps711x_nop_void,
  288. .request_port = uart_clps711x_nop_int,
  289. };
  290. #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
  291. static void uart_clps711x_console_putchar(struct uart_port *port, int ch)
  292. {
  293. struct clps711x_port *s = dev_get_drvdata(port->dev);
  294. u32 sysflg = 0;
  295. /* Wait for FIFO is not full */
  296. do {
  297. regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
  298. } while (sysflg & SYSFLG_UTXFF);
  299. writew(ch, port->membase + UARTDR_OFFSET);
  300. }
  301. static void uart_clps711x_console_write(struct console *co, const char *c,
  302. unsigned n)
  303. {
  304. struct uart_port *port = clps711x_uart.state[co->index].uart_port;
  305. struct clps711x_port *s = dev_get_drvdata(port->dev);
  306. u32 sysflg = 0;
  307. uart_console_write(port, c, n, uart_clps711x_console_putchar);
  308. /* Wait for transmitter to become empty */
  309. do {
  310. regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
  311. } while (sysflg & SYSFLG_UBUSY);
  312. }
  313. static int uart_clps711x_console_setup(struct console *co, char *options)
  314. {
  315. int baud = 38400, bits = 8, parity = 'n', flow = 'n';
  316. int ret, index = co->index;
  317. struct clps711x_port *s;
  318. struct uart_port *port;
  319. unsigned int quot;
  320. u32 ubrlcr;
  321. if (index < 0 || index >= UART_CLPS711X_NR)
  322. return -EINVAL;
  323. port = clps711x_uart.state[index].uart_port;
  324. if (!port)
  325. return -ENODEV;
  326. s = dev_get_drvdata(port->dev);
  327. if (!options) {
  328. u32 syscon = 0;
  329. regmap_read(s->syscon, SYSCON_OFFSET, &syscon);
  330. if (syscon & SYSCON_UARTEN) {
  331. ubrlcr = readl(port->membase + UBRLCR_OFFSET);
  332. if (ubrlcr & UBRLCR_PRTEN) {
  333. if (ubrlcr & UBRLCR_EVENPRT)
  334. parity = 'e';
  335. else
  336. parity = 'o';
  337. }
  338. if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
  339. bits = 7;
  340. quot = ubrlcr & UBRLCR_BAUD_MASK;
  341. baud = port->uartclk / (16 * (quot + 1));
  342. }
  343. } else
  344. uart_parse_options(options, &baud, &parity, &bits, &flow);
  345. ret = uart_set_options(port, co, baud, parity, bits, flow);
  346. if (ret)
  347. return ret;
  348. return regmap_update_bits(s->syscon, SYSCON_OFFSET,
  349. SYSCON_UARTEN, SYSCON_UARTEN);
  350. }
  351. static struct console clps711x_console = {
  352. .name = UART_CLPS711X_DEVNAME,
  353. .device = uart_console_device,
  354. .write = uart_clps711x_console_write,
  355. .setup = uart_clps711x_console_setup,
  356. .flags = CON_PRINTBUFFER,
  357. .index = -1,
  358. };
  359. #endif
  360. static int uart_clps711x_probe(struct platform_device *pdev)
  361. {
  362. struct device_node *np = pdev->dev.of_node;
  363. int ret, index = np ? of_alias_get_id(np, "serial") : pdev->id;
  364. struct clps711x_port *s;
  365. struct resource *res;
  366. struct clk *uart_clk;
  367. if (index < 0 || index >= UART_CLPS711X_NR)
  368. return -EINVAL;
  369. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  370. if (!s)
  371. return -ENOMEM;
  372. uart_clk = devm_clk_get(&pdev->dev, NULL);
  373. if (IS_ERR(uart_clk))
  374. return PTR_ERR(uart_clk);
  375. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  376. s->port.membase = devm_ioremap_resource(&pdev->dev, res);
  377. if (IS_ERR(s->port.membase))
  378. return PTR_ERR(s->port.membase);
  379. s->port.irq = platform_get_irq(pdev, 0);
  380. if (IS_ERR_VALUE(s->port.irq))
  381. return s->port.irq;
  382. s->rx_irq = platform_get_irq(pdev, 1);
  383. if (IS_ERR_VALUE(s->rx_irq))
  384. return s->rx_irq;
  385. if (!np) {
  386. char syscon_name[9];
  387. sprintf(syscon_name, "syscon.%i", index + 1);
  388. s->syscon = syscon_regmap_lookup_by_pdevname(syscon_name);
  389. if (IS_ERR(s->syscon))
  390. return PTR_ERR(s->syscon);
  391. } else {
  392. s->syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
  393. if (IS_ERR(s->syscon))
  394. return PTR_ERR(s->syscon);
  395. }
  396. s->port.line = index;
  397. s->port.dev = &pdev->dev;
  398. s->port.iotype = UPIO_MEM32;
  399. s->port.mapbase = res->start;
  400. s->port.type = PORT_CLPS711X;
  401. s->port.fifosize = 16;
  402. s->port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
  403. s->port.uartclk = clk_get_rate(uart_clk);
  404. s->port.ops = &uart_clps711x_ops;
  405. platform_set_drvdata(pdev, s);
  406. s->gpios = mctrl_gpio_init_noauto(&pdev->dev, 0);
  407. if (IS_ERR(s->gpios))
  408. return PTR_ERR(s->gpios);
  409. ret = uart_add_one_port(&clps711x_uart, &s->port);
  410. if (ret)
  411. return ret;
  412. /* Disable port */
  413. if (!uart_console(&s->port))
  414. regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
  415. s->tx_enabled = 1;
  416. ret = devm_request_irq(&pdev->dev, s->port.irq, uart_clps711x_int_tx, 0,
  417. dev_name(&pdev->dev), &s->port);
  418. if (ret) {
  419. uart_remove_one_port(&clps711x_uart, &s->port);
  420. return ret;
  421. }
  422. ret = devm_request_irq(&pdev->dev, s->rx_irq, uart_clps711x_int_rx, 0,
  423. dev_name(&pdev->dev), &s->port);
  424. if (ret)
  425. uart_remove_one_port(&clps711x_uart, &s->port);
  426. return ret;
  427. }
  428. static int uart_clps711x_remove(struct platform_device *pdev)
  429. {
  430. struct clps711x_port *s = platform_get_drvdata(pdev);
  431. return uart_remove_one_port(&clps711x_uart, &s->port);
  432. }
  433. static const struct of_device_id __maybe_unused clps711x_uart_dt_ids[] = {
  434. { .compatible = "cirrus,clps711x-uart", },
  435. { }
  436. };
  437. MODULE_DEVICE_TABLE(of, clps711x_uart_dt_ids);
  438. static struct platform_driver clps711x_uart_platform = {
  439. .driver = {
  440. .name = "clps711x-uart",
  441. .of_match_table = of_match_ptr(clps711x_uart_dt_ids),
  442. },
  443. .probe = uart_clps711x_probe,
  444. .remove = uart_clps711x_remove,
  445. };
  446. static int __init uart_clps711x_init(void)
  447. {
  448. int ret;
  449. #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
  450. clps711x_uart.cons = &clps711x_console;
  451. clps711x_console.data = &clps711x_uart;
  452. #endif
  453. ret = uart_register_driver(&clps711x_uart);
  454. if (ret)
  455. return ret;
  456. return platform_driver_register(&clps711x_uart_platform);
  457. }
  458. module_init(uart_clps711x_init);
  459. static void __exit uart_clps711x_exit(void)
  460. {
  461. platform_driver_unregister(&clps711x_uart_platform);
  462. uart_unregister_driver(&clps711x_uart);
  463. }
  464. module_exit(uart_clps711x_exit);
  465. MODULE_AUTHOR("Deep Blue Solutions Ltd");
  466. MODULE_DESCRIPTION("CLPS711X serial driver");
  467. MODULE_LICENSE("GPL");