mvebu-uart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * ***************************************************************************
  3. * Marvell Armada-3700 Serial Driver
  4. * Author: Wilson Ding <[email protected]>
  5. * Copyright (C) 2015 Marvell International Ltd.
  6. * ***************************************************************************
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation, either version 2 of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * ***************************************************************************
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/console.h>
  22. #include <linux/delay.h>
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/io.h>
  26. #include <linux/iopoll.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_device.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/serial.h>
  34. #include <linux/serial_core.h>
  35. #include <linux/slab.h>
  36. #include <linux/tty.h>
  37. #include <linux/tty_flip.h>
  38. /* Register Map */
  39. #define UART_RBR 0x00
  40. #define RBR_BRK_DET BIT(15)
  41. #define RBR_FRM_ERR_DET BIT(14)
  42. #define RBR_PAR_ERR_DET BIT(13)
  43. #define RBR_OVR_ERR_DET BIT(12)
  44. #define UART_TSH 0x04
  45. #define UART_CTRL 0x08
  46. #define CTRL_SOFT_RST BIT(31)
  47. #define CTRL_TXFIFO_RST BIT(15)
  48. #define CTRL_RXFIFO_RST BIT(14)
  49. #define CTRL_ST_MIRR_EN BIT(13)
  50. #define CTRL_LPBK_EN BIT(12)
  51. #define CTRL_SND_BRK_SEQ BIT(11)
  52. #define CTRL_PAR_EN BIT(10)
  53. #define CTRL_TWO_STOP BIT(9)
  54. #define CTRL_TX_HFL_INT BIT(8)
  55. #define CTRL_RX_HFL_INT BIT(7)
  56. #define CTRL_TX_EMP_INT BIT(6)
  57. #define CTRL_TX_RDY_INT BIT(5)
  58. #define CTRL_RX_RDY_INT BIT(4)
  59. #define CTRL_BRK_DET_INT BIT(3)
  60. #define CTRL_FRM_ERR_INT BIT(2)
  61. #define CTRL_PAR_ERR_INT BIT(1)
  62. #define CTRL_OVR_ERR_INT BIT(0)
  63. #define CTRL_RX_INT (CTRL_RX_RDY_INT | CTRL_BRK_DET_INT |\
  64. CTRL_FRM_ERR_INT | CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
  65. #define UART_STAT 0x0c
  66. #define STAT_TX_FIFO_EMP BIT(13)
  67. #define STAT_RX_FIFO_EMP BIT(12)
  68. #define STAT_TX_FIFO_FUL BIT(11)
  69. #define STAT_TX_FIFO_HFL BIT(10)
  70. #define STAT_RX_TOGL BIT(9)
  71. #define STAT_RX_FIFO_FUL BIT(8)
  72. #define STAT_RX_FIFO_HFL BIT(7)
  73. #define STAT_TX_EMP BIT(6)
  74. #define STAT_TX_RDY BIT(5)
  75. #define STAT_RX_RDY BIT(4)
  76. #define STAT_BRK_DET BIT(3)
  77. #define STAT_FRM_ERR BIT(2)
  78. #define STAT_PAR_ERR BIT(1)
  79. #define STAT_OVR_ERR BIT(0)
  80. #define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR | STAT_FRM_ERR\
  81. | STAT_PAR_ERR | STAT_OVR_ERR)
  82. #define UART_BRDV 0x10
  83. #define MVEBU_NR_UARTS 1
  84. #define MVEBU_UART_TYPE "mvebu-uart"
  85. static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
  86. struct mvebu_uart_data {
  87. struct uart_port *port;
  88. struct clk *clk;
  89. };
  90. /* Core UART Driver Operations */
  91. static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
  92. {
  93. unsigned long flags;
  94. unsigned int st;
  95. spin_lock_irqsave(&port->lock, flags);
  96. st = readl(port->membase + UART_STAT);
  97. spin_unlock_irqrestore(&port->lock, flags);
  98. return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
  99. }
  100. static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
  101. {
  102. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  103. }
  104. static void mvebu_uart_set_mctrl(struct uart_port *port,
  105. unsigned int mctrl)
  106. {
  107. /*
  108. * Even if we do not support configuring the modem control lines, this
  109. * function must be proided to the serial core
  110. */
  111. }
  112. static void mvebu_uart_stop_tx(struct uart_port *port)
  113. {
  114. unsigned int ctl = readl(port->membase + UART_CTRL);
  115. ctl &= ~CTRL_TX_RDY_INT;
  116. writel(ctl, port->membase + UART_CTRL);
  117. }
  118. static void mvebu_uart_start_tx(struct uart_port *port)
  119. {
  120. unsigned int ctl = readl(port->membase + UART_CTRL);
  121. ctl |= CTRL_TX_RDY_INT;
  122. writel(ctl, port->membase + UART_CTRL);
  123. }
  124. static void mvebu_uart_stop_rx(struct uart_port *port)
  125. {
  126. unsigned int ctl = readl(port->membase + UART_CTRL);
  127. ctl &= ~CTRL_RX_INT;
  128. writel(ctl, port->membase + UART_CTRL);
  129. }
  130. static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
  131. {
  132. unsigned int ctl;
  133. unsigned long flags;
  134. spin_lock_irqsave(&port->lock, flags);
  135. ctl = readl(port->membase + UART_CTRL);
  136. if (brk == -1)
  137. ctl |= CTRL_SND_BRK_SEQ;
  138. else
  139. ctl &= ~CTRL_SND_BRK_SEQ;
  140. writel(ctl, port->membase + UART_CTRL);
  141. spin_unlock_irqrestore(&port->lock, flags);
  142. }
  143. static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
  144. {
  145. struct tty_port *tport = &port->state->port;
  146. unsigned char ch = 0;
  147. char flag = 0;
  148. do {
  149. if (status & STAT_RX_RDY) {
  150. ch = readl(port->membase + UART_RBR);
  151. ch &= 0xff;
  152. flag = TTY_NORMAL;
  153. port->icount.rx++;
  154. if (status & STAT_PAR_ERR)
  155. port->icount.parity++;
  156. }
  157. if (status & STAT_BRK_DET) {
  158. port->icount.brk++;
  159. status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
  160. if (uart_handle_break(port))
  161. goto ignore_char;
  162. }
  163. if (status & STAT_OVR_ERR)
  164. port->icount.overrun++;
  165. if (status & STAT_FRM_ERR)
  166. port->icount.frame++;
  167. if (uart_handle_sysrq_char(port, ch))
  168. goto ignore_char;
  169. if (status & port->ignore_status_mask & STAT_PAR_ERR)
  170. status &= ~STAT_RX_RDY;
  171. status &= port->read_status_mask;
  172. if (status & STAT_PAR_ERR)
  173. flag = TTY_PARITY;
  174. status &= ~port->ignore_status_mask;
  175. if (status & STAT_RX_RDY)
  176. tty_insert_flip_char(tport, ch, flag);
  177. if (status & STAT_BRK_DET)
  178. tty_insert_flip_char(tport, 0, TTY_BREAK);
  179. if (status & STAT_FRM_ERR)
  180. tty_insert_flip_char(tport, 0, TTY_FRAME);
  181. if (status & STAT_OVR_ERR)
  182. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  183. ignore_char:
  184. status = readl(port->membase + UART_STAT);
  185. } while (status & (STAT_RX_RDY | STAT_BRK_DET));
  186. tty_flip_buffer_push(tport);
  187. }
  188. static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
  189. {
  190. struct circ_buf *xmit = &port->state->xmit;
  191. unsigned int count;
  192. unsigned int st;
  193. if (port->x_char) {
  194. writel(port->x_char, port->membase + UART_TSH);
  195. port->icount.tx++;
  196. port->x_char = 0;
  197. return;
  198. }
  199. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  200. mvebu_uart_stop_tx(port);
  201. return;
  202. }
  203. for (count = 0; count < port->fifosize; count++) {
  204. writel(xmit->buf[xmit->tail], port->membase + UART_TSH);
  205. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  206. port->icount.tx++;
  207. if (uart_circ_empty(xmit))
  208. break;
  209. st = readl(port->membase + UART_STAT);
  210. if (st & STAT_TX_FIFO_FUL)
  211. break;
  212. }
  213. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  214. uart_write_wakeup(port);
  215. if (uart_circ_empty(xmit))
  216. mvebu_uart_stop_tx(port);
  217. }
  218. static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
  219. {
  220. struct uart_port *port = (struct uart_port *)dev_id;
  221. unsigned int st = readl(port->membase + UART_STAT);
  222. if (st & (STAT_RX_RDY | STAT_OVR_ERR | STAT_FRM_ERR | STAT_BRK_DET))
  223. mvebu_uart_rx_chars(port, st);
  224. if (st & STAT_TX_RDY)
  225. mvebu_uart_tx_chars(port, st);
  226. return IRQ_HANDLED;
  227. }
  228. static int mvebu_uart_startup(struct uart_port *port)
  229. {
  230. int ret;
  231. writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
  232. port->membase + UART_CTRL);
  233. udelay(1);
  234. writel(CTRL_RX_INT, port->membase + UART_CTRL);
  235. ret = request_irq(port->irq, mvebu_uart_isr, port->irqflags, "serial",
  236. port);
  237. if (ret) {
  238. dev_err(port->dev, "failed to request irq\n");
  239. return ret;
  240. }
  241. return 0;
  242. }
  243. static void mvebu_uart_shutdown(struct uart_port *port)
  244. {
  245. writel(0, port->membase + UART_CTRL);
  246. free_irq(port->irq, port);
  247. }
  248. static void mvebu_uart_set_termios(struct uart_port *port,
  249. struct ktermios *termios,
  250. struct ktermios *old)
  251. {
  252. unsigned long flags;
  253. unsigned int baud;
  254. spin_lock_irqsave(&port->lock, flags);
  255. port->read_status_mask = STAT_RX_RDY | STAT_OVR_ERR |
  256. STAT_TX_RDY | STAT_TX_FIFO_FUL;
  257. if (termios->c_iflag & INPCK)
  258. port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
  259. port->ignore_status_mask = 0;
  260. if (termios->c_iflag & IGNPAR)
  261. port->ignore_status_mask |=
  262. STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
  263. if ((termios->c_cflag & CREAD) == 0)
  264. port->ignore_status_mask |= STAT_RX_RDY | STAT_BRK_ERR;
  265. if (old) {
  266. tty_termios_copy_hw(termios, old);
  267. termios->c_cflag |= CS8;
  268. }
  269. baud = uart_get_baud_rate(port, termios, old, 0, 460800);
  270. uart_update_timeout(port, termios->c_cflag, baud);
  271. spin_unlock_irqrestore(&port->lock, flags);
  272. }
  273. static const char *mvebu_uart_type(struct uart_port *port)
  274. {
  275. return MVEBU_UART_TYPE;
  276. }
  277. static void mvebu_uart_release_port(struct uart_port *port)
  278. {
  279. /* Nothing to do here */
  280. }
  281. static int mvebu_uart_request_port(struct uart_port *port)
  282. {
  283. return 0;
  284. }
  285. #ifdef CONFIG_CONSOLE_POLL
  286. static int mvebu_uart_get_poll_char(struct uart_port *port)
  287. {
  288. unsigned int st = readl(port->membase + UART_STAT);
  289. if (!(st & STAT_RX_RDY))
  290. return NO_POLL_CHAR;
  291. return readl(port->membase + UART_RBR);
  292. }
  293. static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
  294. {
  295. unsigned int st;
  296. for (;;) {
  297. st = readl(port->membase + UART_STAT);
  298. if (!(st & STAT_TX_FIFO_FUL))
  299. break;
  300. udelay(1);
  301. }
  302. writel(c, port->membase + UART_TSH);
  303. }
  304. #endif
  305. static const struct uart_ops mvebu_uart_ops = {
  306. .tx_empty = mvebu_uart_tx_empty,
  307. .set_mctrl = mvebu_uart_set_mctrl,
  308. .get_mctrl = mvebu_uart_get_mctrl,
  309. .stop_tx = mvebu_uart_stop_tx,
  310. .start_tx = mvebu_uart_start_tx,
  311. .stop_rx = mvebu_uart_stop_rx,
  312. .break_ctl = mvebu_uart_break_ctl,
  313. .startup = mvebu_uart_startup,
  314. .shutdown = mvebu_uart_shutdown,
  315. .set_termios = mvebu_uart_set_termios,
  316. .type = mvebu_uart_type,
  317. .release_port = mvebu_uart_release_port,
  318. .request_port = mvebu_uart_request_port,
  319. #ifdef CONFIG_CONSOLE_POLL
  320. .poll_get_char = mvebu_uart_get_poll_char,
  321. .poll_put_char = mvebu_uart_put_poll_char,
  322. #endif
  323. };
  324. /* Console Driver Operations */
  325. #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
  326. /* Early Console */
  327. static void mvebu_uart_putc(struct uart_port *port, int c)
  328. {
  329. unsigned int st;
  330. for (;;) {
  331. st = readl(port->membase + UART_STAT);
  332. if (!(st & STAT_TX_FIFO_FUL))
  333. break;
  334. }
  335. writel(c, port->membase + UART_TSH);
  336. for (;;) {
  337. st = readl(port->membase + UART_STAT);
  338. if (st & STAT_TX_FIFO_EMP)
  339. break;
  340. }
  341. }
  342. static void mvebu_uart_putc_early_write(struct console *con,
  343. const char *s,
  344. unsigned n)
  345. {
  346. struct earlycon_device *dev = con->data;
  347. uart_console_write(&dev->port, s, n, mvebu_uart_putc);
  348. }
  349. static int __init
  350. mvebu_uart_early_console_setup(struct earlycon_device *device,
  351. const char *opt)
  352. {
  353. if (!device->port.membase)
  354. return -ENODEV;
  355. device->con->write = mvebu_uart_putc_early_write;
  356. return 0;
  357. }
  358. EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
  359. OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
  360. mvebu_uart_early_console_setup);
  361. static void wait_for_xmitr(struct uart_port *port)
  362. {
  363. u32 val;
  364. readl_poll_timeout_atomic(port->membase + UART_STAT, val,
  365. (val & STAT_TX_EMP), 1, 10000);
  366. }
  367. static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
  368. {
  369. wait_for_xmitr(port);
  370. writel(ch, port->membase + UART_TSH);
  371. }
  372. static void mvebu_uart_console_write(struct console *co, const char *s,
  373. unsigned int count)
  374. {
  375. struct uart_port *port = &mvebu_uart_ports[co->index];
  376. unsigned long flags;
  377. unsigned int ier;
  378. int locked = 1;
  379. if (oops_in_progress)
  380. locked = spin_trylock_irqsave(&port->lock, flags);
  381. else
  382. spin_lock_irqsave(&port->lock, flags);
  383. ier = readl(port->membase + UART_CTRL) &
  384. (CTRL_RX_INT | CTRL_TX_RDY_INT);
  385. writel(0, port->membase + UART_CTRL);
  386. uart_console_write(port, s, count, mvebu_uart_console_putchar);
  387. wait_for_xmitr(port);
  388. if (ier)
  389. writel(ier, port->membase + UART_CTRL);
  390. if (locked)
  391. spin_unlock_irqrestore(&port->lock, flags);
  392. }
  393. static int mvebu_uart_console_setup(struct console *co, char *options)
  394. {
  395. struct uart_port *port;
  396. int baud = 9600;
  397. int bits = 8;
  398. int parity = 'n';
  399. int flow = 'n';
  400. if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
  401. return -EINVAL;
  402. port = &mvebu_uart_ports[co->index];
  403. if (!port->mapbase || !port->membase) {
  404. pr_debug("console on ttyMV%i not present\n", co->index);
  405. return -ENODEV;
  406. }
  407. if (options)
  408. uart_parse_options(options, &baud, &parity, &bits, &flow);
  409. return uart_set_options(port, co, baud, parity, bits, flow);
  410. }
  411. static struct uart_driver mvebu_uart_driver;
  412. static struct console mvebu_uart_console = {
  413. .name = "ttyMV",
  414. .write = mvebu_uart_console_write,
  415. .device = uart_console_device,
  416. .setup = mvebu_uart_console_setup,
  417. .flags = CON_PRINTBUFFER,
  418. .index = -1,
  419. .data = &mvebu_uart_driver,
  420. };
  421. static int __init mvebu_uart_console_init(void)
  422. {
  423. register_console(&mvebu_uart_console);
  424. return 0;
  425. }
  426. console_initcall(mvebu_uart_console_init);
  427. #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
  428. static struct uart_driver mvebu_uart_driver = {
  429. .owner = THIS_MODULE,
  430. .driver_name = "mvebu_serial",
  431. .dev_name = "ttyMV",
  432. .nr = MVEBU_NR_UARTS,
  433. #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
  434. .cons = &mvebu_uart_console,
  435. #endif
  436. };
  437. static int mvebu_uart_probe(struct platform_device *pdev)
  438. {
  439. struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  440. struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  441. struct uart_port *port;
  442. struct mvebu_uart_data *data;
  443. int ret;
  444. if (!reg || !irq) {
  445. dev_err(&pdev->dev, "no registers/irq defined\n");
  446. return -EINVAL;
  447. }
  448. port = &mvebu_uart_ports[0];
  449. spin_lock_init(&port->lock);
  450. port->dev = &pdev->dev;
  451. port->type = PORT_MVEBU;
  452. port->ops = &mvebu_uart_ops;
  453. port->regshift = 0;
  454. port->fifosize = 32;
  455. port->iotype = UPIO_MEM32;
  456. port->flags = UPF_FIXED_PORT;
  457. port->line = 0; /* single port: force line number to 0 */
  458. port->irq = irq->start;
  459. port->irqflags = 0;
  460. port->mapbase = reg->start;
  461. port->membase = devm_ioremap_resource(&pdev->dev, reg);
  462. if (IS_ERR(port->membase))
  463. return -PTR_ERR(port->membase);
  464. data = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart_data),
  465. GFP_KERNEL);
  466. if (!data)
  467. return -ENOMEM;
  468. data->port = port;
  469. port->private_data = data;
  470. platform_set_drvdata(pdev, data);
  471. ret = uart_add_one_port(&mvebu_uart_driver, port);
  472. if (ret)
  473. return ret;
  474. return 0;
  475. }
  476. /* Match table for of_platform binding */
  477. static const struct of_device_id mvebu_uart_of_match[] = {
  478. { .compatible = "marvell,armada-3700-uart", },
  479. {}
  480. };
  481. static struct platform_driver mvebu_uart_platform_driver = {
  482. .probe = mvebu_uart_probe,
  483. .driver = {
  484. .name = "mvebu-uart",
  485. .of_match_table = of_match_ptr(mvebu_uart_of_match),
  486. .suppress_bind_attrs = true,
  487. },
  488. };
  489. static int __init mvebu_uart_init(void)
  490. {
  491. int ret;
  492. ret = uart_register_driver(&mvebu_uart_driver);
  493. if (ret)
  494. return ret;
  495. ret = platform_driver_register(&mvebu_uart_platform_driver);
  496. if (ret)
  497. uart_unregister_driver(&mvebu_uart_driver);
  498. return ret;
  499. }
  500. arch_initcall(mvebu_uart_init);