mvebu-uart.c 25 KB

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