mvebu-uart.c 15 KB

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