mvebu-uart.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ***************************************************************************
  4. * Marvell Armada-3700 Serial Driver
  5. * Author: Wilson Ding <dingwei@marvell.com>
  6. * Copyright (C) 2015 Marvell International Ltd.
  7. * ***************************************************************************
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/console.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/serial.h>
  23. #include <linux/serial_core.h>
  24. #include <linux/slab.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. /* Register Map */
  28. #define UART_STD_RBR 0x00
  29. #define UART_EXT_RBR 0x18
  30. #define UART_STD_TSH 0x04
  31. #define UART_EXT_TSH 0x1C
  32. #define UART_STD_CTRL1 0x08
  33. #define UART_EXT_CTRL1 0x04
  34. #define CTRL_SOFT_RST BIT(31)
  35. #define CTRL_TXFIFO_RST BIT(15)
  36. #define CTRL_RXFIFO_RST BIT(14)
  37. #define CTRL_SND_BRK_SEQ BIT(11)
  38. #define CTRL_BRK_DET_INT BIT(3)
  39. #define CTRL_FRM_ERR_INT BIT(2)
  40. #define CTRL_PAR_ERR_INT BIT(1)
  41. #define CTRL_OVR_ERR_INT BIT(0)
  42. #define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
  43. CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
  44. #define UART_STD_CTRL2 UART_STD_CTRL1
  45. #define UART_EXT_CTRL2 0x20
  46. #define CTRL_STD_TX_RDY_INT BIT(5)
  47. #define CTRL_EXT_TX_RDY_INT BIT(6)
  48. #define CTRL_STD_RX_RDY_INT BIT(4)
  49. #define CTRL_EXT_RX_RDY_INT BIT(5)
  50. #define UART_STAT 0x0C
  51. #define STAT_TX_FIFO_EMP BIT(13)
  52. #define STAT_TX_FIFO_FUL BIT(11)
  53. #define STAT_TX_EMP BIT(6)
  54. #define STAT_STD_TX_RDY BIT(5)
  55. #define STAT_EXT_TX_RDY BIT(15)
  56. #define STAT_STD_RX_RDY BIT(4)
  57. #define STAT_EXT_RX_RDY BIT(14)
  58. #define STAT_BRK_DET BIT(3)
  59. #define STAT_FRM_ERR BIT(2)
  60. #define STAT_PAR_ERR BIT(1)
  61. #define STAT_OVR_ERR BIT(0)
  62. #define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR \
  63. | STAT_PAR_ERR | STAT_OVR_ERR)
  64. #define UART_BRDV 0x10
  65. #define BRDV_BAUD_MASK 0x3FF
  66. #define MVEBU_NR_UARTS 2
  67. #define MVEBU_UART_TYPE "mvebu-uart"
  68. #define DRIVER_NAME "mvebu_serial"
  69. enum {
  70. /* Either there is only one summed IRQ... */
  71. UART_IRQ_SUM = 0,
  72. /* ...or there are two separate IRQ for RX and TX */
  73. UART_RX_IRQ = 0,
  74. UART_TX_IRQ,
  75. UART_IRQ_COUNT
  76. };
  77. /* Diverging register offsets */
  78. struct uart_regs_layout {
  79. unsigned int rbr;
  80. unsigned int tsh;
  81. unsigned int ctrl;
  82. unsigned int intr;
  83. };
  84. /* Diverging flags */
  85. struct uart_flags {
  86. unsigned int ctrl_tx_rdy_int;
  87. unsigned int ctrl_rx_rdy_int;
  88. unsigned int stat_tx_rdy;
  89. unsigned int stat_rx_rdy;
  90. };
  91. /* Driver data, a structure for each UART port */
  92. struct mvebu_uart_driver_data {
  93. bool is_ext;
  94. struct uart_regs_layout regs;
  95. struct uart_flags flags;
  96. };
  97. /* MVEBU UART driver structure */
  98. struct mvebu_uart {
  99. struct uart_port *port;
  100. struct clk *clk;
  101. int irq[UART_IRQ_COUNT];
  102. unsigned char __iomem *nb;
  103. struct mvebu_uart_driver_data *data;
  104. };
  105. static struct mvebu_uart *to_mvuart(struct uart_port *port)
  106. {
  107. return (struct mvebu_uart *)port->private_data;
  108. }
  109. #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
  110. #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
  111. #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
  112. #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
  113. #define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
  114. #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
  115. #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
  116. #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
  117. #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
  118. static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
  119. /* Core UART Driver Operations */
  120. static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
  121. {
  122. unsigned long flags;
  123. unsigned int st;
  124. spin_lock_irqsave(&port->lock, flags);
  125. st = readl(port->membase + UART_STAT);
  126. spin_unlock_irqrestore(&port->lock, flags);
  127. return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
  128. }
  129. static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
  130. {
  131. return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  132. }
  133. static void mvebu_uart_set_mctrl(struct uart_port *port,
  134. unsigned int mctrl)
  135. {
  136. /*
  137. * Even if we do not support configuring the modem control lines, this
  138. * function must be proided to the serial core
  139. */
  140. }
  141. static void mvebu_uart_stop_tx(struct uart_port *port)
  142. {
  143. unsigned int ctl = readl(port->membase + UART_INTR(port));
  144. ctl &= ~CTRL_TX_RDY_INT(port);
  145. writel(ctl, port->membase + UART_INTR(port));
  146. }
  147. static void mvebu_uart_start_tx(struct uart_port *port)
  148. {
  149. unsigned int ctl;
  150. struct circ_buf *xmit = &port->state->xmit;
  151. if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
  152. writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
  153. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  154. port->icount.tx++;
  155. }
  156. ctl = readl(port->membase + UART_INTR(port));
  157. ctl |= CTRL_TX_RDY_INT(port);
  158. writel(ctl, port->membase + UART_INTR(port));
  159. }
  160. static void mvebu_uart_stop_rx(struct uart_port *port)
  161. {
  162. unsigned int ctl;
  163. ctl = readl(port->membase + UART_CTRL(port));
  164. ctl &= ~CTRL_BRK_INT;
  165. writel(ctl, port->membase + UART_CTRL(port));
  166. ctl = readl(port->membase + UART_INTR(port));
  167. ctl &= ~CTRL_RX_RDY_INT(port);
  168. writel(ctl, port->membase + UART_INTR(port));
  169. }
  170. static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
  171. {
  172. unsigned int ctl;
  173. unsigned long flags;
  174. spin_lock_irqsave(&port->lock, flags);
  175. ctl = readl(port->membase + UART_CTRL(port));
  176. if (brk == -1)
  177. ctl |= CTRL_SND_BRK_SEQ;
  178. else
  179. ctl &= ~CTRL_SND_BRK_SEQ;
  180. writel(ctl, port->membase + UART_CTRL(port));
  181. spin_unlock_irqrestore(&port->lock, flags);
  182. }
  183. static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
  184. {
  185. struct tty_port *tport = &port->state->port;
  186. unsigned char ch = 0;
  187. char flag = 0;
  188. do {
  189. if (status & STAT_RX_RDY(port)) {
  190. ch = readl(port->membase + UART_RBR(port));
  191. ch &= 0xff;
  192. flag = TTY_NORMAL;
  193. port->icount.rx++;
  194. if (status & STAT_PAR_ERR)
  195. port->icount.parity++;
  196. }
  197. if (status & STAT_BRK_DET) {
  198. port->icount.brk++;
  199. status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
  200. if (uart_handle_break(port))
  201. goto ignore_char;
  202. }
  203. if (status & STAT_OVR_ERR)
  204. port->icount.overrun++;
  205. if (status & STAT_FRM_ERR)
  206. port->icount.frame++;
  207. if (uart_handle_sysrq_char(port, ch))
  208. goto ignore_char;
  209. if (status & port->ignore_status_mask & STAT_PAR_ERR)
  210. status &= ~STAT_RX_RDY(port);
  211. status &= port->read_status_mask;
  212. if (status & STAT_PAR_ERR)
  213. flag = TTY_PARITY;
  214. status &= ~port->ignore_status_mask;
  215. if (status & STAT_RX_RDY(port))
  216. tty_insert_flip_char(tport, ch, flag);
  217. if (status & STAT_BRK_DET)
  218. tty_insert_flip_char(tport, 0, TTY_BREAK);
  219. if (status & STAT_FRM_ERR)
  220. tty_insert_flip_char(tport, 0, TTY_FRAME);
  221. if (status & STAT_OVR_ERR)
  222. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  223. ignore_char:
  224. status = readl(port->membase + UART_STAT);
  225. } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
  226. tty_flip_buffer_push(tport);
  227. }
  228. static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
  229. {
  230. struct circ_buf *xmit = &port->state->xmit;
  231. unsigned int count;
  232. unsigned int st;
  233. if (port->x_char) {
  234. writel(port->x_char, port->membase + UART_TSH(port));
  235. port->icount.tx++;
  236. port->x_char = 0;
  237. return;
  238. }
  239. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  240. mvebu_uart_stop_tx(port);
  241. return;
  242. }
  243. for (count = 0; count < port->fifosize; count++) {
  244. writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
  245. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  246. port->icount.tx++;
  247. if (uart_circ_empty(xmit))
  248. break;
  249. st = readl(port->membase + UART_STAT);
  250. if (st & STAT_TX_FIFO_FUL)
  251. break;
  252. }
  253. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  254. uart_write_wakeup(port);
  255. if (uart_circ_empty(xmit))
  256. mvebu_uart_stop_tx(port);
  257. }
  258. static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
  259. {
  260. struct uart_port *port = (struct uart_port *)dev_id;
  261. unsigned int st = readl(port->membase + UART_STAT);
  262. if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
  263. STAT_BRK_DET))
  264. mvebu_uart_rx_chars(port, st);
  265. if (st & STAT_TX_RDY(port))
  266. mvebu_uart_tx_chars(port, st);
  267. return IRQ_HANDLED;
  268. }
  269. static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
  270. {
  271. struct uart_port *port = (struct uart_port *)dev_id;
  272. unsigned int st = readl(port->membase + UART_STAT);
  273. if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
  274. STAT_BRK_DET))
  275. mvebu_uart_rx_chars(port, st);
  276. return IRQ_HANDLED;
  277. }
  278. static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
  279. {
  280. struct uart_port *port = (struct uart_port *)dev_id;
  281. unsigned int st = readl(port->membase + UART_STAT);
  282. if (st & STAT_TX_RDY(port))
  283. mvebu_uart_tx_chars(port, st);
  284. return IRQ_HANDLED;
  285. }
  286. static int mvebu_uart_startup(struct uart_port *port)
  287. {
  288. struct mvebu_uart *mvuart = to_mvuart(port);
  289. unsigned int ctl;
  290. int ret;
  291. writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
  292. port->membase + UART_CTRL(port));
  293. udelay(1);
  294. /* Clear the error bits of state register before IRQ request */
  295. ret = readl(port->membase + UART_STAT);
  296. ret |= STAT_BRK_ERR;
  297. writel(ret, port->membase + UART_STAT);
  298. writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
  299. ctl = readl(port->membase + UART_INTR(port));
  300. ctl |= CTRL_RX_RDY_INT(port);
  301. writel(ctl, port->membase + UART_INTR(port));
  302. if (!mvuart->irq[UART_TX_IRQ]) {
  303. /* Old bindings with just one interrupt (UART0 only) */
  304. ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
  305. mvebu_uart_isr, port->irqflags,
  306. dev_name(port->dev), port);
  307. if (ret) {
  308. dev_err(port->dev, "unable to request IRQ %d\n",
  309. mvuart->irq[UART_IRQ_SUM]);
  310. return ret;
  311. }
  312. } else {
  313. /* New bindings with an IRQ for RX and TX (both UART) */
  314. ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
  315. mvebu_uart_rx_isr, port->irqflags,
  316. dev_name(port->dev), port);
  317. if (ret) {
  318. dev_err(port->dev, "unable to request IRQ %d\n",
  319. mvuart->irq[UART_RX_IRQ]);
  320. return ret;
  321. }
  322. ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
  323. mvebu_uart_tx_isr, port->irqflags,
  324. dev_name(port->dev),
  325. port);
  326. if (ret) {
  327. dev_err(port->dev, "unable to request IRQ %d\n",
  328. mvuart->irq[UART_TX_IRQ]);
  329. devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
  330. port);
  331. return ret;
  332. }
  333. }
  334. return 0;
  335. }
  336. static void mvebu_uart_shutdown(struct uart_port *port)
  337. {
  338. struct mvebu_uart *mvuart = to_mvuart(port);
  339. writel(0, port->membase + UART_INTR(port));
  340. if (!mvuart->irq[UART_TX_IRQ]) {
  341. devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
  342. } else {
  343. devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
  344. devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
  345. }
  346. }
  347. static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
  348. {
  349. struct mvebu_uart *mvuart = to_mvuart(port);
  350. unsigned int baud_rate_div;
  351. u32 brdv;
  352. if (IS_ERR(mvuart->clk))
  353. return -PTR_ERR(mvuart->clk);
  354. /*
  355. * The UART clock is divided by the value of the divisor to generate
  356. * UCLK_OUT clock, which is 16 times faster than the baudrate.
  357. * This prescaler can achieve all standard baudrates until 230400.
  358. * Higher baudrates could be achieved for the extended UART by using the
  359. * programmable oversampling stack (also called fractional divisor).
  360. */
  361. baud_rate_div = DIV_ROUND_UP(port->uartclk, baud * 16);
  362. brdv = readl(port->membase + UART_BRDV);
  363. brdv &= ~BRDV_BAUD_MASK;
  364. brdv |= baud_rate_div;
  365. writel(brdv, port->membase + UART_BRDV);
  366. return 0;
  367. }
  368. static void mvebu_uart_set_termios(struct uart_port *port,
  369. struct ktermios *termios,
  370. struct ktermios *old)
  371. {
  372. unsigned long flags;
  373. unsigned int baud;
  374. spin_lock_irqsave(&port->lock, flags);
  375. port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
  376. STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
  377. if (termios->c_iflag & INPCK)
  378. port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
  379. port->ignore_status_mask = 0;
  380. if (termios->c_iflag & IGNPAR)
  381. port->ignore_status_mask |=
  382. STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
  383. if ((termios->c_cflag & CREAD) == 0)
  384. port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
  385. /*
  386. * Maximum achievable frequency with simple baudrate divisor is 230400.
  387. * Since the error per bit frame would be of more than 15%, achieving
  388. * higher frequencies would require to implement the fractional divisor
  389. * feature.
  390. */
  391. baud = uart_get_baud_rate(port, termios, old, 0, 230400);
  392. if (mvebu_uart_baud_rate_set(port, baud)) {
  393. /* No clock available, baudrate cannot be changed */
  394. if (old)
  395. baud = uart_get_baud_rate(port, old, NULL, 0, 230400);
  396. } else {
  397. tty_termios_encode_baud_rate(termios, baud, baud);
  398. uart_update_timeout(port, termios->c_cflag, baud);
  399. }
  400. /* Only the following flag changes are supported */
  401. if (old) {
  402. termios->c_iflag &= INPCK | IGNPAR;
  403. termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
  404. termios->c_cflag &= CREAD | CBAUD;
  405. termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
  406. }
  407. spin_unlock_irqrestore(&port->lock, flags);
  408. }
  409. static const char *mvebu_uart_type(struct uart_port *port)
  410. {
  411. return MVEBU_UART_TYPE;
  412. }
  413. static void mvebu_uart_release_port(struct uart_port *port)
  414. {
  415. /* Nothing to do here */
  416. }
  417. static int mvebu_uart_request_port(struct uart_port *port)
  418. {
  419. return 0;
  420. }
  421. #ifdef CONFIG_CONSOLE_POLL
  422. static int mvebu_uart_get_poll_char(struct uart_port *port)
  423. {
  424. unsigned int st = readl(port->membase + UART_STAT);
  425. if (!(st & STAT_RX_RDY(port)))
  426. return NO_POLL_CHAR;
  427. return readl(port->membase + UART_RBR(port));
  428. }
  429. static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
  430. {
  431. unsigned int st;
  432. for (;;) {
  433. st = readl(port->membase + UART_STAT);
  434. if (!(st & STAT_TX_FIFO_FUL))
  435. break;
  436. udelay(1);
  437. }
  438. writel(c, port->membase + UART_TSH(port));
  439. }
  440. #endif
  441. static const struct uart_ops mvebu_uart_ops = {
  442. .tx_empty = mvebu_uart_tx_empty,
  443. .set_mctrl = mvebu_uart_set_mctrl,
  444. .get_mctrl = mvebu_uart_get_mctrl,
  445. .stop_tx = mvebu_uart_stop_tx,
  446. .start_tx = mvebu_uart_start_tx,
  447. .stop_rx = mvebu_uart_stop_rx,
  448. .break_ctl = mvebu_uart_break_ctl,
  449. .startup = mvebu_uart_startup,
  450. .shutdown = mvebu_uart_shutdown,
  451. .set_termios = mvebu_uart_set_termios,
  452. .type = mvebu_uart_type,
  453. .release_port = mvebu_uart_release_port,
  454. .request_port = mvebu_uart_request_port,
  455. #ifdef CONFIG_CONSOLE_POLL
  456. .poll_get_char = mvebu_uart_get_poll_char,
  457. .poll_put_char = mvebu_uart_put_poll_char,
  458. #endif
  459. };
  460. /* Console Driver Operations */
  461. #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
  462. /* Early Console */
  463. static void mvebu_uart_putc(struct uart_port *port, int c)
  464. {
  465. unsigned int st;
  466. for (;;) {
  467. st = readl(port->membase + UART_STAT);
  468. if (!(st & STAT_TX_FIFO_FUL))
  469. break;
  470. }
  471. /* At early stage, DT is not parsed yet, only use UART0 */
  472. writel(c, port->membase + UART_STD_TSH);
  473. for (;;) {
  474. st = readl(port->membase + UART_STAT);
  475. if (st & STAT_TX_FIFO_EMP)
  476. break;
  477. }
  478. }
  479. static void mvebu_uart_putc_early_write(struct console *con,
  480. const char *s,
  481. unsigned n)
  482. {
  483. struct earlycon_device *dev = con->data;
  484. uart_console_write(&dev->port, s, n, mvebu_uart_putc);
  485. }
  486. static int __init
  487. mvebu_uart_early_console_setup(struct earlycon_device *device,
  488. const char *opt)
  489. {
  490. if (!device->port.membase)
  491. return -ENODEV;
  492. device->con->write = mvebu_uart_putc_early_write;
  493. return 0;
  494. }
  495. EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
  496. OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
  497. mvebu_uart_early_console_setup);
  498. static void wait_for_xmitr(struct uart_port *port)
  499. {
  500. u32 val;
  501. readl_poll_timeout_atomic(port->membase + UART_STAT, val,
  502. (val & STAT_TX_RDY(port)), 1, 10000);
  503. }
  504. static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
  505. {
  506. wait_for_xmitr(port);
  507. writel(ch, port->membase + UART_TSH(port));
  508. }
  509. static void mvebu_uart_console_write(struct console *co, const char *s,
  510. unsigned int count)
  511. {
  512. struct uart_port *port = &mvebu_uart_ports[co->index];
  513. unsigned long flags;
  514. unsigned int ier, intr, ctl;
  515. int locked = 1;
  516. if (oops_in_progress)
  517. locked = spin_trylock_irqsave(&port->lock, flags);
  518. else
  519. spin_lock_irqsave(&port->lock, flags);
  520. ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
  521. intr = readl(port->membase + UART_INTR(port)) &
  522. (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
  523. writel(0, port->membase + UART_CTRL(port));
  524. writel(0, port->membase + UART_INTR(port));
  525. uart_console_write(port, s, count, mvebu_uart_console_putchar);
  526. wait_for_xmitr(port);
  527. if (ier)
  528. writel(ier, port->membase + UART_CTRL(port));
  529. if (intr) {
  530. ctl = intr | readl(port->membase + UART_INTR(port));
  531. writel(ctl, port->membase + UART_INTR(port));
  532. }
  533. if (locked)
  534. spin_unlock_irqrestore(&port->lock, flags);
  535. }
  536. static int mvebu_uart_console_setup(struct console *co, char *options)
  537. {
  538. struct uart_port *port;
  539. int baud = 9600;
  540. int bits = 8;
  541. int parity = 'n';
  542. int flow = 'n';
  543. if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
  544. return -EINVAL;
  545. port = &mvebu_uart_ports[co->index];
  546. if (!port->mapbase || !port->membase) {
  547. pr_debug("console on ttyMV%i not present\n", co->index);
  548. return -ENODEV;
  549. }
  550. if (options)
  551. uart_parse_options(options, &baud, &parity, &bits, &flow);
  552. return uart_set_options(port, co, baud, parity, bits, flow);
  553. }
  554. static struct uart_driver mvebu_uart_driver;
  555. static struct console mvebu_uart_console = {
  556. .name = "ttyMV",
  557. .write = mvebu_uart_console_write,
  558. .device = uart_console_device,
  559. .setup = mvebu_uart_console_setup,
  560. .flags = CON_PRINTBUFFER,
  561. .index = -1,
  562. .data = &mvebu_uart_driver,
  563. };
  564. static int __init mvebu_uart_console_init(void)
  565. {
  566. register_console(&mvebu_uart_console);
  567. return 0;
  568. }
  569. console_initcall(mvebu_uart_console_init);
  570. #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
  571. static struct uart_driver mvebu_uart_driver = {
  572. .owner = THIS_MODULE,
  573. .driver_name = DRIVER_NAME,
  574. .dev_name = "ttyMV",
  575. .nr = MVEBU_NR_UARTS,
  576. #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
  577. .cons = &mvebu_uart_console,
  578. #endif
  579. };
  580. static const struct of_device_id mvebu_uart_of_match[];
  581. /* Counter to keep track of each UART port id when not using CONFIG_OF */
  582. static int uart_num_counter;
  583. static int mvebu_uart_probe(struct platform_device *pdev)
  584. {
  585. struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  586. const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
  587. &pdev->dev);
  588. struct uart_port *port;
  589. struct mvebu_uart *mvuart;
  590. int ret, id, irq;
  591. if (!reg) {
  592. dev_err(&pdev->dev, "no registers defined\n");
  593. return -EINVAL;
  594. }
  595. /* Assume that all UART ports have a DT alias or none has */
  596. id = of_alias_get_id(pdev->dev.of_node, "serial");
  597. if (!pdev->dev.of_node || id < 0)
  598. pdev->id = uart_num_counter++;
  599. else
  600. pdev->id = id;
  601. if (pdev->id >= MVEBU_NR_UARTS) {
  602. dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
  603. MVEBU_NR_UARTS);
  604. return -EINVAL;
  605. }
  606. port = &mvebu_uart_ports[pdev->id];
  607. spin_lock_init(&port->lock);
  608. port->dev = &pdev->dev;
  609. port->type = PORT_MVEBU;
  610. port->ops = &mvebu_uart_ops;
  611. port->regshift = 0;
  612. port->fifosize = 32;
  613. port->iotype = UPIO_MEM32;
  614. port->flags = UPF_FIXED_PORT;
  615. port->line = pdev->id;
  616. /*
  617. * IRQ number is not stored in this structure because we may have two of
  618. * them per port (RX and TX). Instead, use the driver UART structure
  619. * array so called ->irq[].
  620. */
  621. port->irq = 0;
  622. port->irqflags = 0;
  623. port->mapbase = reg->start;
  624. port->membase = devm_ioremap_resource(&pdev->dev, reg);
  625. if (IS_ERR(port->membase))
  626. return -PTR_ERR(port->membase);
  627. mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
  628. GFP_KERNEL);
  629. if (!mvuart)
  630. return -ENOMEM;
  631. /* Get controller data depending on the compatible string */
  632. mvuart->data = (struct mvebu_uart_driver_data *)match->data;
  633. mvuart->port = port;
  634. port->private_data = mvuart;
  635. platform_set_drvdata(pdev, mvuart);
  636. /* Get fixed clock frequency */
  637. mvuart->clk = devm_clk_get(&pdev->dev, NULL);
  638. if (IS_ERR(mvuart->clk)) {
  639. if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
  640. return PTR_ERR(mvuart->clk);
  641. if (IS_EXTENDED(port)) {
  642. dev_err(&pdev->dev, "unable to get UART clock\n");
  643. return PTR_ERR(mvuart->clk);
  644. }
  645. } else {
  646. if (!clk_prepare_enable(mvuart->clk))
  647. port->uartclk = clk_get_rate(mvuart->clk);
  648. }
  649. /* Manage interrupts */
  650. if (platform_irq_count(pdev) == 1) {
  651. /* Old bindings: no name on the single unamed UART0 IRQ */
  652. irq = platform_get_irq(pdev, 0);
  653. if (irq < 0) {
  654. dev_err(&pdev->dev, "unable to get UART IRQ\n");
  655. return irq;
  656. }
  657. mvuart->irq[UART_IRQ_SUM] = irq;
  658. } else {
  659. /*
  660. * New bindings: named interrupts (RX, TX) for both UARTS,
  661. * only make use of uart-rx and uart-tx interrupts, do not use
  662. * uart-sum of UART0 port.
  663. */
  664. irq = platform_get_irq_byname(pdev, "uart-rx");
  665. if (irq < 0) {
  666. dev_err(&pdev->dev, "unable to get 'uart-rx' IRQ\n");
  667. return irq;
  668. }
  669. mvuart->irq[UART_RX_IRQ] = irq;
  670. irq = platform_get_irq_byname(pdev, "uart-tx");
  671. if (irq < 0) {
  672. dev_err(&pdev->dev, "unable to get 'uart-tx' IRQ\n");
  673. return irq;
  674. }
  675. mvuart->irq[UART_TX_IRQ] = irq;
  676. }
  677. /* UART Soft Reset*/
  678. writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
  679. udelay(1);
  680. writel(0, port->membase + UART_CTRL(port));
  681. ret = uart_add_one_port(&mvebu_uart_driver, port);
  682. if (ret)
  683. return ret;
  684. return 0;
  685. }
  686. static struct mvebu_uart_driver_data uart_std_driver_data = {
  687. .is_ext = false,
  688. .regs.rbr = UART_STD_RBR,
  689. .regs.tsh = UART_STD_TSH,
  690. .regs.ctrl = UART_STD_CTRL1,
  691. .regs.intr = UART_STD_CTRL2,
  692. .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
  693. .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
  694. .flags.stat_tx_rdy = STAT_STD_TX_RDY,
  695. .flags.stat_rx_rdy = STAT_STD_RX_RDY,
  696. };
  697. static struct mvebu_uart_driver_data uart_ext_driver_data = {
  698. .is_ext = true,
  699. .regs.rbr = UART_EXT_RBR,
  700. .regs.tsh = UART_EXT_TSH,
  701. .regs.ctrl = UART_EXT_CTRL1,
  702. .regs.intr = UART_EXT_CTRL2,
  703. .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
  704. .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
  705. .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
  706. .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
  707. };
  708. /* Match table for of_platform binding */
  709. static const struct of_device_id mvebu_uart_of_match[] = {
  710. {
  711. .compatible = "marvell,armada-3700-uart",
  712. .data = (void *)&uart_std_driver_data,
  713. },
  714. {
  715. .compatible = "marvell,armada-3700-uart-ext",
  716. .data = (void *)&uart_ext_driver_data,
  717. },
  718. {}
  719. };
  720. static struct platform_driver mvebu_uart_platform_driver = {
  721. .probe = mvebu_uart_probe,
  722. .driver = {
  723. .name = "mvebu-uart",
  724. .of_match_table = of_match_ptr(mvebu_uart_of_match),
  725. .suppress_bind_attrs = true,
  726. },
  727. };
  728. static int __init mvebu_uart_init(void)
  729. {
  730. int ret;
  731. ret = uart_register_driver(&mvebu_uart_driver);
  732. if (ret)
  733. return ret;
  734. ret = platform_driver_register(&mvebu_uart_platform_driver);
  735. if (ret)
  736. uart_unregister_driver(&mvebu_uart_driver);
  737. return ret;
  738. }
  739. arch_initcall(mvebu_uart_init);