mvebu-uart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. }
  421. spin_unlock_irqrestore(&port->lock, flags);
  422. }
  423. static const char *mvebu_uart_type(struct uart_port *port)
  424. {
  425. return MVEBU_UART_TYPE;
  426. }
  427. static void mvebu_uart_release_port(struct uart_port *port)
  428. {
  429. /* Nothing to do here */
  430. }
  431. static int mvebu_uart_request_port(struct uart_port *port)
  432. {
  433. return 0;
  434. }
  435. #ifdef CONFIG_CONSOLE_POLL
  436. static int mvebu_uart_get_poll_char(struct uart_port *port)
  437. {
  438. unsigned int st = readl(port->membase + UART_STAT);
  439. if (!(st & STAT_RX_RDY(port)))
  440. return NO_POLL_CHAR;
  441. return readl(port->membase + UART_RBR(port));
  442. }
  443. static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
  444. {
  445. unsigned int st;
  446. for (;;) {
  447. st = readl(port->membase + UART_STAT);
  448. if (!(st & STAT_TX_FIFO_FUL))
  449. break;
  450. udelay(1);
  451. }
  452. writel(c, port->membase + UART_TSH(port));
  453. }
  454. #endif
  455. static const struct uart_ops mvebu_uart_ops = {
  456. .tx_empty = mvebu_uart_tx_empty,
  457. .set_mctrl = mvebu_uart_set_mctrl,
  458. .get_mctrl = mvebu_uart_get_mctrl,
  459. .stop_tx = mvebu_uart_stop_tx,
  460. .start_tx = mvebu_uart_start_tx,
  461. .stop_rx = mvebu_uart_stop_rx,
  462. .break_ctl = mvebu_uart_break_ctl,
  463. .startup = mvebu_uart_startup,
  464. .shutdown = mvebu_uart_shutdown,
  465. .set_termios = mvebu_uart_set_termios,
  466. .type = mvebu_uart_type,
  467. .release_port = mvebu_uart_release_port,
  468. .request_port = mvebu_uart_request_port,
  469. #ifdef CONFIG_CONSOLE_POLL
  470. .poll_get_char = mvebu_uart_get_poll_char,
  471. .poll_put_char = mvebu_uart_put_poll_char,
  472. #endif
  473. };
  474. /* Console Driver Operations */
  475. #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
  476. /* Early Console */
  477. static void mvebu_uart_putc(struct uart_port *port, int c)
  478. {
  479. unsigned int st;
  480. for (;;) {
  481. st = readl(port->membase + UART_STAT);
  482. if (!(st & STAT_TX_FIFO_FUL))
  483. break;
  484. }
  485. /* At early stage, DT is not parsed yet, only use UART0 */
  486. writel(c, port->membase + UART_STD_TSH);
  487. for (;;) {
  488. st = readl(port->membase + UART_STAT);
  489. if (st & STAT_TX_FIFO_EMP)
  490. break;
  491. }
  492. }
  493. static void mvebu_uart_putc_early_write(struct console *con,
  494. const char *s,
  495. unsigned n)
  496. {
  497. struct earlycon_device *dev = con->data;
  498. uart_console_write(&dev->port, s, n, mvebu_uart_putc);
  499. }
  500. static int __init
  501. mvebu_uart_early_console_setup(struct earlycon_device *device,
  502. const char *opt)
  503. {
  504. if (!device->port.membase)
  505. return -ENODEV;
  506. device->con->write = mvebu_uart_putc_early_write;
  507. return 0;
  508. }
  509. EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
  510. OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
  511. mvebu_uart_early_console_setup);
  512. static void wait_for_xmitr(struct uart_port *port)
  513. {
  514. u32 val;
  515. readl_poll_timeout_atomic(port->membase + UART_STAT, val,
  516. (val & STAT_TX_RDY(port)), 1, 10000);
  517. }
  518. static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
  519. {
  520. wait_for_xmitr(port);
  521. writel(ch, port->membase + UART_TSH(port));
  522. }
  523. static void mvebu_uart_console_write(struct console *co, const char *s,
  524. unsigned int count)
  525. {
  526. struct uart_port *port = &mvebu_uart_ports[co->index];
  527. unsigned long flags;
  528. unsigned int ier, intr, ctl;
  529. int locked = 1;
  530. if (oops_in_progress)
  531. locked = spin_trylock_irqsave(&port->lock, flags);
  532. else
  533. spin_lock_irqsave(&port->lock, flags);
  534. ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
  535. intr = readl(port->membase + UART_INTR(port)) &
  536. (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
  537. writel(0, port->membase + UART_CTRL(port));
  538. writel(0, port->membase + UART_INTR(port));
  539. uart_console_write(port, s, count, mvebu_uart_console_putchar);
  540. wait_for_xmitr(port);
  541. if (ier)
  542. writel(ier, port->membase + UART_CTRL(port));
  543. if (intr) {
  544. ctl = intr | readl(port->membase + UART_INTR(port));
  545. writel(ctl, port->membase + UART_INTR(port));
  546. }
  547. if (locked)
  548. spin_unlock_irqrestore(&port->lock, flags);
  549. }
  550. static int mvebu_uart_console_setup(struct console *co, char *options)
  551. {
  552. struct uart_port *port;
  553. int baud = 9600;
  554. int bits = 8;
  555. int parity = 'n';
  556. int flow = 'n';
  557. if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
  558. return -EINVAL;
  559. port = &mvebu_uart_ports[co->index];
  560. if (!port->mapbase || !port->membase) {
  561. pr_debug("console on ttyMV%i not present\n", co->index);
  562. return -ENODEV;
  563. }
  564. if (options)
  565. uart_parse_options(options, &baud, &parity, &bits, &flow);
  566. return uart_set_options(port, co, baud, parity, bits, flow);
  567. }
  568. static struct uart_driver mvebu_uart_driver;
  569. static struct console mvebu_uart_console = {
  570. .name = "ttyMV",
  571. .write = mvebu_uart_console_write,
  572. .device = uart_console_device,
  573. .setup = mvebu_uart_console_setup,
  574. .flags = CON_PRINTBUFFER,
  575. .index = -1,
  576. .data = &mvebu_uart_driver,
  577. };
  578. static int __init mvebu_uart_console_init(void)
  579. {
  580. register_console(&mvebu_uart_console);
  581. return 0;
  582. }
  583. console_initcall(mvebu_uart_console_init);
  584. #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
  585. static struct uart_driver mvebu_uart_driver = {
  586. .owner = THIS_MODULE,
  587. .driver_name = DRIVER_NAME,
  588. .dev_name = "ttyMV",
  589. .nr = MVEBU_NR_UARTS,
  590. #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
  591. .cons = &mvebu_uart_console,
  592. #endif
  593. };
  594. #if defined(CONFIG_PM)
  595. static int mvebu_uart_suspend(struct device *dev)
  596. {
  597. struct mvebu_uart *mvuart = dev_get_drvdata(dev);
  598. struct uart_port *port = mvuart->port;
  599. uart_suspend_port(&mvebu_uart_driver, port);
  600. mvuart->pm_regs.rbr = readl(port->membase + UART_RBR(port));
  601. mvuart->pm_regs.tsh = readl(port->membase + UART_TSH(port));
  602. mvuart->pm_regs.ctrl = readl(port->membase + UART_CTRL(port));
  603. mvuart->pm_regs.intr = readl(port->membase + UART_INTR(port));
  604. mvuart->pm_regs.stat = readl(port->membase + UART_STAT);
  605. mvuart->pm_regs.brdv = readl(port->membase + UART_BRDV);
  606. mvuart->pm_regs.osamp = readl(port->membase + UART_OSAMP);
  607. device_set_wakeup_enable(dev, true);
  608. return 0;
  609. }
  610. static int mvebu_uart_resume(struct device *dev)
  611. {
  612. struct mvebu_uart *mvuart = dev_get_drvdata(dev);
  613. struct uart_port *port = mvuart->port;
  614. writel(mvuart->pm_regs.rbr, port->membase + UART_RBR(port));
  615. writel(mvuart->pm_regs.tsh, port->membase + UART_TSH(port));
  616. writel(mvuart->pm_regs.ctrl, port->membase + UART_CTRL(port));
  617. writel(mvuart->pm_regs.intr, port->membase + UART_INTR(port));
  618. writel(mvuart->pm_regs.stat, port->membase + UART_STAT);
  619. writel(mvuart->pm_regs.brdv, port->membase + UART_BRDV);
  620. writel(mvuart->pm_regs.osamp, port->membase + UART_OSAMP);
  621. uart_resume_port(&mvebu_uart_driver, port);
  622. return 0;
  623. }
  624. static const struct dev_pm_ops mvebu_uart_pm_ops = {
  625. .suspend = mvebu_uart_suspend,
  626. .resume = mvebu_uart_resume,
  627. };
  628. #endif /* CONFIG_PM */
  629. static const struct of_device_id mvebu_uart_of_match[];
  630. /* Counter to keep track of each UART port id when not using CONFIG_OF */
  631. static int uart_num_counter;
  632. static int mvebu_uart_probe(struct platform_device *pdev)
  633. {
  634. struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  635. const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
  636. &pdev->dev);
  637. struct uart_port *port;
  638. struct mvebu_uart *mvuart;
  639. int ret, id, irq;
  640. if (!reg) {
  641. dev_err(&pdev->dev, "no registers defined\n");
  642. return -EINVAL;
  643. }
  644. /* Assume that all UART ports have a DT alias or none has */
  645. id = of_alias_get_id(pdev->dev.of_node, "serial");
  646. if (!pdev->dev.of_node || id < 0)
  647. pdev->id = uart_num_counter++;
  648. else
  649. pdev->id = id;
  650. if (pdev->id >= MVEBU_NR_UARTS) {
  651. dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
  652. MVEBU_NR_UARTS);
  653. return -EINVAL;
  654. }
  655. port = &mvebu_uart_ports[pdev->id];
  656. spin_lock_init(&port->lock);
  657. port->dev = &pdev->dev;
  658. port->type = PORT_MVEBU;
  659. port->ops = &mvebu_uart_ops;
  660. port->regshift = 0;
  661. port->fifosize = 32;
  662. port->iotype = UPIO_MEM32;
  663. port->flags = UPF_FIXED_PORT;
  664. port->line = pdev->id;
  665. /*
  666. * IRQ number is not stored in this structure because we may have two of
  667. * them per port (RX and TX). Instead, use the driver UART structure
  668. * array so called ->irq[].
  669. */
  670. port->irq = 0;
  671. port->irqflags = 0;
  672. port->mapbase = reg->start;
  673. port->membase = devm_ioremap_resource(&pdev->dev, reg);
  674. if (IS_ERR(port->membase))
  675. return -PTR_ERR(port->membase);
  676. mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
  677. GFP_KERNEL);
  678. if (!mvuart)
  679. return -ENOMEM;
  680. /* Get controller data depending on the compatible string */
  681. mvuart->data = (struct mvebu_uart_driver_data *)match->data;
  682. mvuart->port = port;
  683. port->private_data = mvuart;
  684. platform_set_drvdata(pdev, mvuart);
  685. /* Get fixed clock frequency */
  686. mvuart->clk = devm_clk_get(&pdev->dev, NULL);
  687. if (IS_ERR(mvuart->clk)) {
  688. if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
  689. return PTR_ERR(mvuart->clk);
  690. if (IS_EXTENDED(port)) {
  691. dev_err(&pdev->dev, "unable to get UART clock\n");
  692. return PTR_ERR(mvuart->clk);
  693. }
  694. } else {
  695. if (!clk_prepare_enable(mvuart->clk))
  696. port->uartclk = clk_get_rate(mvuart->clk);
  697. }
  698. /* Manage interrupts */
  699. if (platform_irq_count(pdev) == 1) {
  700. /* Old bindings: no name on the single unamed UART0 IRQ */
  701. irq = platform_get_irq(pdev, 0);
  702. if (irq < 0) {
  703. dev_err(&pdev->dev, "unable to get UART IRQ\n");
  704. return irq;
  705. }
  706. mvuart->irq[UART_IRQ_SUM] = irq;
  707. } else {
  708. /*
  709. * New bindings: named interrupts (RX, TX) for both UARTS,
  710. * only make use of uart-rx and uart-tx interrupts, do not use
  711. * uart-sum of UART0 port.
  712. */
  713. irq = platform_get_irq_byname(pdev, "uart-rx");
  714. if (irq < 0) {
  715. dev_err(&pdev->dev, "unable to get 'uart-rx' IRQ\n");
  716. return irq;
  717. }
  718. mvuart->irq[UART_RX_IRQ] = irq;
  719. irq = platform_get_irq_byname(pdev, "uart-tx");
  720. if (irq < 0) {
  721. dev_err(&pdev->dev, "unable to get 'uart-tx' IRQ\n");
  722. return irq;
  723. }
  724. mvuart->irq[UART_TX_IRQ] = irq;
  725. }
  726. /* UART Soft Reset*/
  727. writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
  728. udelay(1);
  729. writel(0, port->membase + UART_CTRL(port));
  730. ret = uart_add_one_port(&mvebu_uart_driver, port);
  731. if (ret)
  732. return ret;
  733. return 0;
  734. }
  735. static struct mvebu_uart_driver_data uart_std_driver_data = {
  736. .is_ext = false,
  737. .regs.rbr = UART_STD_RBR,
  738. .regs.tsh = UART_STD_TSH,
  739. .regs.ctrl = UART_STD_CTRL1,
  740. .regs.intr = UART_STD_CTRL2,
  741. .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
  742. .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
  743. .flags.stat_tx_rdy = STAT_STD_TX_RDY,
  744. .flags.stat_rx_rdy = STAT_STD_RX_RDY,
  745. };
  746. static struct mvebu_uart_driver_data uart_ext_driver_data = {
  747. .is_ext = true,
  748. .regs.rbr = UART_EXT_RBR,
  749. .regs.tsh = UART_EXT_TSH,
  750. .regs.ctrl = UART_EXT_CTRL1,
  751. .regs.intr = UART_EXT_CTRL2,
  752. .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
  753. .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
  754. .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
  755. .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
  756. };
  757. /* Match table for of_platform binding */
  758. static const struct of_device_id mvebu_uart_of_match[] = {
  759. {
  760. .compatible = "marvell,armada-3700-uart",
  761. .data = (void *)&uart_std_driver_data,
  762. },
  763. {
  764. .compatible = "marvell,armada-3700-uart-ext",
  765. .data = (void *)&uart_ext_driver_data,
  766. },
  767. {}
  768. };
  769. static struct platform_driver mvebu_uart_platform_driver = {
  770. .probe = mvebu_uart_probe,
  771. .driver = {
  772. .name = "mvebu-uart",
  773. .of_match_table = of_match_ptr(mvebu_uart_of_match),
  774. .suppress_bind_attrs = true,
  775. #if defined(CONFIG_PM)
  776. .pm = &mvebu_uart_pm_ops,
  777. #endif /* CONFIG_PM */
  778. },
  779. };
  780. static int __init mvebu_uart_init(void)
  781. {
  782. int ret;
  783. ret = uart_register_driver(&mvebu_uart_driver);
  784. if (ret)
  785. return ret;
  786. ret = platform_driver_register(&mvebu_uart_platform_driver);
  787. if (ret)
  788. uart_unregister_driver(&mvebu_uart_driver);
  789. return ret;
  790. }
  791. arch_initcall(mvebu_uart_init);