mvebu-uart.c 25 KB

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