mps2-uart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * MPS2 UART driver
  3. *
  4. * Copyright (C) 2015 ARM Limited
  5. *
  6. * Author: Vladimir Murzin <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * TODO: support for SysRq
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/bitops.h>
  16. #include <linux/clk.h>
  17. #include <linux/console.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/serial_core.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/types.h>
  26. #define SERIAL_NAME "ttyMPS"
  27. #define DRIVER_NAME "mps2-uart"
  28. #define MAKE_NAME(x) (DRIVER_NAME # x)
  29. #define UARTn_DATA 0x00
  30. #define UARTn_STATE 0x04
  31. #define UARTn_STATE_TX_FULL BIT(0)
  32. #define UARTn_STATE_RX_FULL BIT(1)
  33. #define UARTn_STATE_TX_OVERRUN BIT(2)
  34. #define UARTn_STATE_RX_OVERRUN BIT(3)
  35. #define UARTn_CTRL 0x08
  36. #define UARTn_CTRL_TX_ENABLE BIT(0)
  37. #define UARTn_CTRL_RX_ENABLE BIT(1)
  38. #define UARTn_CTRL_TX_INT_ENABLE BIT(2)
  39. #define UARTn_CTRL_RX_INT_ENABLE BIT(3)
  40. #define UARTn_CTRL_TX_OVERRUN_INT_ENABLE BIT(4)
  41. #define UARTn_CTRL_RX_OVERRUN_INT_ENABLE BIT(5)
  42. #define UARTn_INT 0x0c
  43. #define UARTn_INT_TX BIT(0)
  44. #define UARTn_INT_RX BIT(1)
  45. #define UARTn_INT_TX_OVERRUN BIT(2)
  46. #define UARTn_INT_RX_OVERRUN BIT(3)
  47. #define UARTn_BAUDDIV 0x10
  48. #define UARTn_BAUDDIV_MASK GENMASK(20, 0)
  49. /*
  50. * Helpers to make typical enable/disable operations more readable.
  51. */
  52. #define UARTn_CTRL_TX_GRP (UARTn_CTRL_TX_ENABLE |\
  53. UARTn_CTRL_TX_INT_ENABLE |\
  54. UARTn_CTRL_TX_OVERRUN_INT_ENABLE)
  55. #define UARTn_CTRL_RX_GRP (UARTn_CTRL_RX_ENABLE |\
  56. UARTn_CTRL_RX_INT_ENABLE |\
  57. UARTn_CTRL_RX_OVERRUN_INT_ENABLE)
  58. #define MPS2_MAX_PORTS 3
  59. struct mps2_uart_port {
  60. struct uart_port port;
  61. struct clk *clk;
  62. unsigned int tx_irq;
  63. unsigned int rx_irq;
  64. };
  65. static inline struct mps2_uart_port *to_mps2_port(struct uart_port *port)
  66. {
  67. return container_of(port, struct mps2_uart_port, port);
  68. }
  69. static void mps2_uart_write8(struct uart_port *port, u8 val, unsigned int off)
  70. {
  71. struct mps2_uart_port *mps_port = to_mps2_port(port);
  72. writeb(val, mps_port->port.membase + off);
  73. }
  74. static u8 mps2_uart_read8(struct uart_port *port, unsigned int off)
  75. {
  76. struct mps2_uart_port *mps_port = to_mps2_port(port);
  77. return readb(mps_port->port.membase + off);
  78. }
  79. static void mps2_uart_write32(struct uart_port *port, u32 val, unsigned int off)
  80. {
  81. struct mps2_uart_port *mps_port = to_mps2_port(port);
  82. writel_relaxed(val, mps_port->port.membase + off);
  83. }
  84. static unsigned int mps2_uart_tx_empty(struct uart_port *port)
  85. {
  86. u8 status = mps2_uart_read8(port, UARTn_STATE);
  87. return (status & UARTn_STATE_TX_FULL) ? 0 : TIOCSER_TEMT;
  88. }
  89. static void mps2_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  90. {
  91. }
  92. static unsigned int mps2_uart_get_mctrl(struct uart_port *port)
  93. {
  94. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
  95. }
  96. static void mps2_uart_stop_tx(struct uart_port *port)
  97. {
  98. u8 control = mps2_uart_read8(port, UARTn_CTRL);
  99. control &= ~UARTn_CTRL_TX_INT_ENABLE;
  100. mps2_uart_write8(port, control, UARTn_CTRL);
  101. }
  102. static void mps2_uart_tx_chars(struct uart_port *port)
  103. {
  104. struct circ_buf *xmit = &port->state->xmit;
  105. while (!(mps2_uart_read8(port, UARTn_STATE) & UARTn_STATE_TX_FULL)) {
  106. if (port->x_char) {
  107. mps2_uart_write8(port, port->x_char, UARTn_DATA);
  108. port->x_char = 0;
  109. port->icount.tx++;
  110. continue;
  111. }
  112. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  113. break;
  114. mps2_uart_write8(port, xmit->buf[xmit->tail], UARTn_DATA);
  115. xmit->tail = (xmit->tail + 1) % UART_XMIT_SIZE;
  116. port->icount.tx++;
  117. }
  118. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  119. uart_write_wakeup(port);
  120. if (uart_circ_empty(xmit))
  121. mps2_uart_stop_tx(port);
  122. }
  123. static void mps2_uart_start_tx(struct uart_port *port)
  124. {
  125. u8 control = mps2_uart_read8(port, UARTn_CTRL);
  126. control |= UARTn_CTRL_TX_INT_ENABLE;
  127. mps2_uart_write8(port, control, UARTn_CTRL);
  128. /*
  129. * We've just unmasked the TX IRQ and now slow-starting via
  130. * polling; if there is enough data to fill up the internal
  131. * write buffer in one go, the TX IRQ should assert, at which
  132. * point we switch to fully interrupt-driven TX.
  133. */
  134. mps2_uart_tx_chars(port);
  135. }
  136. static void mps2_uart_stop_rx(struct uart_port *port)
  137. {
  138. u8 control = mps2_uart_read8(port, UARTn_CTRL);
  139. control &= ~UARTn_CTRL_RX_GRP;
  140. mps2_uart_write8(port, control, UARTn_CTRL);
  141. }
  142. static void mps2_uart_break_ctl(struct uart_port *port, int ctl)
  143. {
  144. }
  145. static void mps2_uart_rx_chars(struct uart_port *port)
  146. {
  147. struct tty_port *tport = &port->state->port;
  148. while (mps2_uart_read8(port, UARTn_STATE) & UARTn_STATE_RX_FULL) {
  149. u8 rxdata = mps2_uart_read8(port, UARTn_DATA);
  150. port->icount.rx++;
  151. tty_insert_flip_char(&port->state->port, rxdata, TTY_NORMAL);
  152. }
  153. tty_flip_buffer_push(tport);
  154. }
  155. static irqreturn_t mps2_uart_rxirq(int irq, void *data)
  156. {
  157. struct uart_port *port = data;
  158. u8 irqflag = mps2_uart_read8(port, UARTn_INT);
  159. if (unlikely(!(irqflag & UARTn_INT_RX)))
  160. return IRQ_NONE;
  161. spin_lock(&port->lock);
  162. mps2_uart_write8(port, UARTn_INT_RX, UARTn_INT);
  163. mps2_uart_rx_chars(port);
  164. spin_unlock(&port->lock);
  165. return IRQ_HANDLED;
  166. }
  167. static irqreturn_t mps2_uart_txirq(int irq, void *data)
  168. {
  169. struct uart_port *port = data;
  170. u8 irqflag = mps2_uart_read8(port, UARTn_INT);
  171. if (unlikely(!(irqflag & UARTn_INT_TX)))
  172. return IRQ_NONE;
  173. spin_lock(&port->lock);
  174. mps2_uart_write8(port, UARTn_INT_TX, UARTn_INT);
  175. mps2_uart_tx_chars(port);
  176. spin_unlock(&port->lock);
  177. return IRQ_HANDLED;
  178. }
  179. static irqreturn_t mps2_uart_oerrirq(int irq, void *data)
  180. {
  181. irqreturn_t handled = IRQ_NONE;
  182. struct uart_port *port = data;
  183. u8 irqflag = mps2_uart_read8(port, UARTn_INT);
  184. spin_lock(&port->lock);
  185. if (irqflag & UARTn_INT_RX_OVERRUN) {
  186. struct tty_port *tport = &port->state->port;
  187. mps2_uart_write8(port, UARTn_INT_RX_OVERRUN, UARTn_INT);
  188. port->icount.overrun++;
  189. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  190. tty_flip_buffer_push(tport);
  191. handled = IRQ_HANDLED;
  192. }
  193. /*
  194. * It's never been seen in practice and it never *should* happen since
  195. * we check if there is enough room in TX buffer before sending data.
  196. * So we keep this check in case something suspicious has happened.
  197. */
  198. if (irqflag & UARTn_INT_TX_OVERRUN) {
  199. mps2_uart_write8(port, UARTn_INT_TX_OVERRUN, UARTn_INT);
  200. handled = IRQ_HANDLED;
  201. }
  202. spin_unlock(&port->lock);
  203. return handled;
  204. }
  205. static int mps2_uart_startup(struct uart_port *port)
  206. {
  207. struct mps2_uart_port *mps_port = to_mps2_port(port);
  208. u8 control = mps2_uart_read8(port, UARTn_CTRL);
  209. int ret;
  210. control &= ~(UARTn_CTRL_RX_GRP | UARTn_CTRL_TX_GRP);
  211. mps2_uart_write8(port, control, UARTn_CTRL);
  212. ret = request_irq(mps_port->rx_irq, mps2_uart_rxirq, 0,
  213. MAKE_NAME(-rx), mps_port);
  214. if (ret) {
  215. dev_err(port->dev, "failed to register rxirq (%d)\n", ret);
  216. return ret;
  217. }
  218. ret = request_irq(mps_port->tx_irq, mps2_uart_txirq, 0,
  219. MAKE_NAME(-tx), mps_port);
  220. if (ret) {
  221. dev_err(port->dev, "failed to register txirq (%d)\n", ret);
  222. goto err_free_rxirq;
  223. }
  224. ret = request_irq(port->irq, mps2_uart_oerrirq, IRQF_SHARED,
  225. MAKE_NAME(-overrun), mps_port);
  226. if (ret) {
  227. dev_err(port->dev, "failed to register oerrirq (%d)\n", ret);
  228. goto err_free_txirq;
  229. }
  230. control |= UARTn_CTRL_RX_GRP | UARTn_CTRL_TX_GRP;
  231. mps2_uart_write8(port, control, UARTn_CTRL);
  232. return 0;
  233. err_free_txirq:
  234. free_irq(mps_port->tx_irq, mps_port);
  235. err_free_rxirq:
  236. free_irq(mps_port->rx_irq, mps_port);
  237. return ret;
  238. }
  239. static void mps2_uart_shutdown(struct uart_port *port)
  240. {
  241. struct mps2_uart_port *mps_port = to_mps2_port(port);
  242. u8 control = mps2_uart_read8(port, UARTn_CTRL);
  243. control &= ~(UARTn_CTRL_RX_GRP | UARTn_CTRL_TX_GRP);
  244. mps2_uart_write8(port, control, UARTn_CTRL);
  245. free_irq(mps_port->rx_irq, mps_port);
  246. free_irq(mps_port->tx_irq, mps_port);
  247. free_irq(port->irq, mps_port);
  248. }
  249. static void
  250. mps2_uart_set_termios(struct uart_port *port, struct ktermios *termios,
  251. struct ktermios *old)
  252. {
  253. unsigned long flags;
  254. unsigned int baud, bauddiv;
  255. termios->c_cflag &= ~(CRTSCTS | CMSPAR);
  256. termios->c_cflag &= ~CSIZE;
  257. termios->c_cflag |= CS8;
  258. termios->c_cflag &= ~PARENB;
  259. termios->c_cflag &= ~CSTOPB;
  260. baud = uart_get_baud_rate(port, termios, old,
  261. DIV_ROUND_CLOSEST(port->uartclk, UARTn_BAUDDIV_MASK),
  262. DIV_ROUND_CLOSEST(port->uartclk, 16));
  263. bauddiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
  264. spin_lock_irqsave(&port->lock, flags);
  265. uart_update_timeout(port, termios->c_cflag, baud);
  266. mps2_uart_write32(port, bauddiv, UARTn_BAUDDIV);
  267. spin_unlock_irqrestore(&port->lock, flags);
  268. if (tty_termios_baud_rate(termios))
  269. tty_termios_encode_baud_rate(termios, baud, baud);
  270. }
  271. static const char *mps2_uart_type(struct uart_port *port)
  272. {
  273. return (port->type == PORT_MPS2UART) ? DRIVER_NAME : NULL;
  274. }
  275. static void mps2_uart_release_port(struct uart_port *port)
  276. {
  277. }
  278. static int mps2_uart_request_port(struct uart_port *port)
  279. {
  280. return 0;
  281. }
  282. static void mps2_uart_config_port(struct uart_port *port, int type)
  283. {
  284. if (type & UART_CONFIG_TYPE && !mps2_uart_request_port(port))
  285. port->type = PORT_MPS2UART;
  286. }
  287. static int mps2_uart_verify_port(struct uart_port *port, struct serial_struct *serinfo)
  288. {
  289. return -EINVAL;
  290. }
  291. static const struct uart_ops mps2_uart_pops = {
  292. .tx_empty = mps2_uart_tx_empty,
  293. .set_mctrl = mps2_uart_set_mctrl,
  294. .get_mctrl = mps2_uart_get_mctrl,
  295. .stop_tx = mps2_uart_stop_tx,
  296. .start_tx = mps2_uart_start_tx,
  297. .stop_rx = mps2_uart_stop_rx,
  298. .break_ctl = mps2_uart_break_ctl,
  299. .startup = mps2_uart_startup,
  300. .shutdown = mps2_uart_shutdown,
  301. .set_termios = mps2_uart_set_termios,
  302. .type = mps2_uart_type,
  303. .release_port = mps2_uart_release_port,
  304. .request_port = mps2_uart_request_port,
  305. .config_port = mps2_uart_config_port,
  306. .verify_port = mps2_uart_verify_port,
  307. };
  308. static struct mps2_uart_port mps2_uart_ports[MPS2_MAX_PORTS];
  309. #ifdef CONFIG_SERIAL_MPS2_UART_CONSOLE
  310. static void mps2_uart_console_putchar(struct uart_port *port, int ch)
  311. {
  312. while (mps2_uart_read8(port, UARTn_STATE) & UARTn_STATE_TX_FULL)
  313. cpu_relax();
  314. mps2_uart_write8(port, ch, UARTn_DATA);
  315. }
  316. static void mps2_uart_console_write(struct console *co, const char *s, unsigned int cnt)
  317. {
  318. struct uart_port *port = &mps2_uart_ports[co->index].port;
  319. uart_console_write(port, s, cnt, mps2_uart_console_putchar);
  320. }
  321. static int mps2_uart_console_setup(struct console *co, char *options)
  322. {
  323. struct mps2_uart_port *mps_port;
  324. int baud = 9600;
  325. int bits = 8;
  326. int parity = 'n';
  327. int flow = 'n';
  328. if (co->index < 0 || co->index >= MPS2_MAX_PORTS)
  329. return -ENODEV;
  330. mps_port = &mps2_uart_ports[co->index];
  331. if (options)
  332. uart_parse_options(options, &baud, &parity, &bits, &flow);
  333. return uart_set_options(&mps_port->port, co, baud, parity, bits, flow);
  334. }
  335. static struct uart_driver mps2_uart_driver;
  336. static struct console mps2_uart_console = {
  337. .name = SERIAL_NAME,
  338. .device = uart_console_device,
  339. .write = mps2_uart_console_write,
  340. .setup = mps2_uart_console_setup,
  341. .flags = CON_PRINTBUFFER,
  342. .index = -1,
  343. .data = &mps2_uart_driver,
  344. };
  345. #define MPS2_SERIAL_CONSOLE (&mps2_uart_console)
  346. static void mps2_early_putchar(struct uart_port *port, int ch)
  347. {
  348. while (readb(port->membase + UARTn_STATE) & UARTn_STATE_TX_FULL)
  349. cpu_relax();
  350. writeb((unsigned char)ch, port->membase + UARTn_DATA);
  351. }
  352. static void mps2_early_write(struct console *con, const char *s, unsigned int n)
  353. {
  354. struct earlycon_device *dev = con->data;
  355. uart_console_write(&dev->port, s, n, mps2_early_putchar);
  356. }
  357. static int __init mps2_early_console_setup(struct earlycon_device *device,
  358. const char *opt)
  359. {
  360. if (!device->port.membase)
  361. return -ENODEV;
  362. device->con->write = mps2_early_write;
  363. return 0;
  364. }
  365. OF_EARLYCON_DECLARE(mps2, "arm,mps2-uart", mps2_early_console_setup);
  366. #else
  367. #define MPS2_SERIAL_CONSOLE NULL
  368. #endif
  369. static struct uart_driver mps2_uart_driver = {
  370. .driver_name = DRIVER_NAME,
  371. .dev_name = SERIAL_NAME,
  372. .nr = MPS2_MAX_PORTS,
  373. .cons = MPS2_SERIAL_CONSOLE,
  374. };
  375. static struct mps2_uart_port *mps2_of_get_port(struct platform_device *pdev)
  376. {
  377. struct device_node *np = pdev->dev.of_node;
  378. int id;
  379. if (!np)
  380. return NULL;
  381. id = of_alias_get_id(np, "serial");
  382. if (id < 0)
  383. id = 0;
  384. if (WARN_ON(id >= MPS2_MAX_PORTS))
  385. return NULL;
  386. mps2_uart_ports[id].port.line = id;
  387. return &mps2_uart_ports[id];
  388. }
  389. static int mps2_init_port(struct mps2_uart_port *mps_port,
  390. struct platform_device *pdev)
  391. {
  392. struct resource *res;
  393. int ret;
  394. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  395. mps_port->port.membase = devm_ioremap_resource(&pdev->dev, res);
  396. if (IS_ERR(mps_port->port.membase))
  397. return PTR_ERR(mps_port->port.membase);
  398. mps_port->port.mapbase = res->start;
  399. mps_port->port.mapsize = resource_size(res);
  400. mps_port->rx_irq = platform_get_irq(pdev, 0);
  401. mps_port->tx_irq = platform_get_irq(pdev, 1);
  402. mps_port->port.irq = platform_get_irq(pdev, 2);
  403. mps_port->port.iotype = UPIO_MEM;
  404. mps_port->port.flags = UPF_BOOT_AUTOCONF;
  405. mps_port->port.fifosize = 1;
  406. mps_port->port.ops = &mps2_uart_pops;
  407. mps_port->port.dev = &pdev->dev;
  408. mps_port->clk = devm_clk_get(&pdev->dev, NULL);
  409. if (IS_ERR(mps_port->clk))
  410. return PTR_ERR(mps_port->clk);
  411. ret = clk_prepare_enable(mps_port->clk);
  412. if (ret)
  413. return ret;
  414. mps_port->port.uartclk = clk_get_rate(mps_port->clk);
  415. clk_disable_unprepare(mps_port->clk);
  416. return ret;
  417. }
  418. static int mps2_serial_probe(struct platform_device *pdev)
  419. {
  420. struct mps2_uart_port *mps_port;
  421. int ret;
  422. mps_port = mps2_of_get_port(pdev);
  423. if (!mps_port)
  424. return -ENODEV;
  425. ret = mps2_init_port(mps_port, pdev);
  426. if (ret)
  427. return ret;
  428. ret = uart_add_one_port(&mps2_uart_driver, &mps_port->port);
  429. if (ret)
  430. return ret;
  431. platform_set_drvdata(pdev, mps_port);
  432. return 0;
  433. }
  434. #ifdef CONFIG_OF
  435. static const struct of_device_id mps2_match[] = {
  436. { .compatible = "arm,mps2-uart", },
  437. {},
  438. };
  439. #endif
  440. static struct platform_driver mps2_serial_driver = {
  441. .probe = mps2_serial_probe,
  442. .driver = {
  443. .name = DRIVER_NAME,
  444. .of_match_table = of_match_ptr(mps2_match),
  445. .suppress_bind_attrs = true,
  446. },
  447. };
  448. static int __init mps2_uart_init(void)
  449. {
  450. int ret;
  451. ret = uart_register_driver(&mps2_uart_driver);
  452. if (ret)
  453. return ret;
  454. ret = platform_driver_register(&mps2_serial_driver);
  455. if (ret)
  456. uart_unregister_driver(&mps2_uart_driver);
  457. return ret;
  458. }
  459. arch_initcall(mps2_uart_init);