rda-uart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * RDA8810PL serial device driver
  4. *
  5. * Copyright RDA Microelectronics Company Limited
  6. * Copyright (c) 2017 Andreas Färber
  7. * Copyright (c) 2018 Manivannan Sadhasivam
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/console.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/serial.h>
  17. #include <linux/serial_core.h>
  18. #include <linux/tty.h>
  19. #include <linux/tty_flip.h>
  20. #define RDA_UART_PORT_NUM 3
  21. #define RDA_UART_DEV_NAME "ttyRDA"
  22. #define RDA_UART_CTRL 0x00
  23. #define RDA_UART_STATUS 0x04
  24. #define RDA_UART_RXTX_BUFFER 0x08
  25. #define RDA_UART_IRQ_MASK 0x0c
  26. #define RDA_UART_IRQ_CAUSE 0x10
  27. #define RDA_UART_IRQ_TRIGGERS 0x14
  28. #define RDA_UART_CMD_SET 0x18
  29. #define RDA_UART_CMD_CLR 0x1c
  30. /* UART_CTRL Bits */
  31. #define RDA_UART_ENABLE BIT(0)
  32. #define RDA_UART_DBITS_8 BIT(1)
  33. #define RDA_UART_TX_SBITS_2 BIT(2)
  34. #define RDA_UART_PARITY_EN BIT(3)
  35. #define RDA_UART_PARITY(x) (((x) & 0x3) << 4)
  36. #define RDA_UART_PARITY_ODD RDA_UART_PARITY(0)
  37. #define RDA_UART_PARITY_EVEN RDA_UART_PARITY(1)
  38. #define RDA_UART_PARITY_SPACE RDA_UART_PARITY(2)
  39. #define RDA_UART_PARITY_MARK RDA_UART_PARITY(3)
  40. #define RDA_UART_DIV_MODE BIT(20)
  41. #define RDA_UART_IRDA_EN BIT(21)
  42. #define RDA_UART_DMA_EN BIT(22)
  43. #define RDA_UART_FLOW_CNT_EN BIT(23)
  44. #define RDA_UART_LOOP_BACK_EN BIT(24)
  45. #define RDA_UART_RX_LOCK_ERR BIT(25)
  46. #define RDA_UART_RX_BREAK_LEN(x) (((x) & 0xf) << 28)
  47. /* UART_STATUS Bits */
  48. #define RDA_UART_RX_FIFO(x) (((x) & 0x7f) << 0)
  49. #define RDA_UART_RX_FIFO_MASK (0x7f << 0)
  50. #define RDA_UART_TX_FIFO(x) (((x) & 0x1f) << 8)
  51. #define RDA_UART_TX_FIFO_MASK (0x1f << 8)
  52. #define RDA_UART_TX_ACTIVE BIT(14)
  53. #define RDA_UART_RX_ACTIVE BIT(15)
  54. #define RDA_UART_RX_OVERFLOW_ERR BIT(16)
  55. #define RDA_UART_TX_OVERFLOW_ERR BIT(17)
  56. #define RDA_UART_RX_PARITY_ERR BIT(18)
  57. #define RDA_UART_RX_FRAMING_ERR BIT(19)
  58. #define RDA_UART_RX_BREAK_INT BIT(20)
  59. #define RDA_UART_DCTS BIT(24)
  60. #define RDA_UART_CTS BIT(25)
  61. #define RDA_UART_DTR BIT(28)
  62. #define RDA_UART_CLK_ENABLED BIT(31)
  63. /* UART_RXTX_BUFFER Bits */
  64. #define RDA_UART_RX_DATA(x) (((x) & 0xff) << 0)
  65. #define RDA_UART_TX_DATA(x) (((x) & 0xff) << 0)
  66. /* UART_IRQ_MASK Bits */
  67. #define RDA_UART_TX_MODEM_STATUS BIT(0)
  68. #define RDA_UART_RX_DATA_AVAILABLE BIT(1)
  69. #define RDA_UART_TX_DATA_NEEDED BIT(2)
  70. #define RDA_UART_RX_TIMEOUT BIT(3)
  71. #define RDA_UART_RX_LINE_ERR BIT(4)
  72. #define RDA_UART_TX_DMA_DONE BIT(5)
  73. #define RDA_UART_RX_DMA_DONE BIT(6)
  74. #define RDA_UART_RX_DMA_TIMEOUT BIT(7)
  75. #define RDA_UART_DTR_RISE BIT(8)
  76. #define RDA_UART_DTR_FALL BIT(9)
  77. /* UART_IRQ_CAUSE Bits */
  78. #define RDA_UART_TX_MODEM_STATUS_U BIT(16)
  79. #define RDA_UART_RX_DATA_AVAILABLE_U BIT(17)
  80. #define RDA_UART_TX_DATA_NEEDED_U BIT(18)
  81. #define RDA_UART_RX_TIMEOUT_U BIT(19)
  82. #define RDA_UART_RX_LINE_ERR_U BIT(20)
  83. #define RDA_UART_TX_DMA_DONE_U BIT(21)
  84. #define RDA_UART_RX_DMA_DONE_U BIT(22)
  85. #define RDA_UART_RX_DMA_TIMEOUT_U BIT(23)
  86. #define RDA_UART_DTR_RISE_U BIT(24)
  87. #define RDA_UART_DTR_FALL_U BIT(25)
  88. /* UART_TRIGGERS Bits */
  89. #define RDA_UART_RX_TRIGGER(x) (((x) & 0x1f) << 0)
  90. #define RDA_UART_TX_TRIGGER(x) (((x) & 0xf) << 8)
  91. #define RDA_UART_AFC_LEVEL(x) (((x) & 0x1f) << 16)
  92. /* UART_CMD_SET Bits */
  93. #define RDA_UART_RI BIT(0)
  94. #define RDA_UART_DCD BIT(1)
  95. #define RDA_UART_DSR BIT(2)
  96. #define RDA_UART_TX_BREAK_CONTROL BIT(3)
  97. #define RDA_UART_TX_FINISH_N_WAIT BIT(4)
  98. #define RDA_UART_RTS BIT(5)
  99. #define RDA_UART_RX_FIFO_RESET BIT(6)
  100. #define RDA_UART_TX_FIFO_RESET BIT(7)
  101. #define RDA_UART_TX_FIFO_SIZE 16
  102. static struct uart_driver rda_uart_driver;
  103. struct rda_uart_port {
  104. struct uart_port port;
  105. struct clk *clk;
  106. };
  107. #define to_rda_uart_port(port) container_of(port, struct rda_uart_port, port)
  108. static struct rda_uart_port *rda_uart_ports[RDA_UART_PORT_NUM];
  109. static inline void rda_uart_write(struct uart_port *port, u32 val,
  110. unsigned int off)
  111. {
  112. writel(val, port->membase + off);
  113. }
  114. static inline u32 rda_uart_read(struct uart_port *port, unsigned int off)
  115. {
  116. return readl(port->membase + off);
  117. }
  118. static unsigned int rda_uart_tx_empty(struct uart_port *port)
  119. {
  120. unsigned long flags;
  121. unsigned int ret;
  122. u32 val;
  123. spin_lock_irqsave(&port->lock, flags);
  124. val = rda_uart_read(port, RDA_UART_STATUS);
  125. ret = (val & RDA_UART_TX_FIFO_MASK) ? TIOCSER_TEMT : 0;
  126. spin_unlock_irqrestore(&port->lock, flags);
  127. return ret;
  128. }
  129. static unsigned int rda_uart_get_mctrl(struct uart_port *port)
  130. {
  131. unsigned int mctrl = 0;
  132. u32 cmd_set, status;
  133. cmd_set = rda_uart_read(port, RDA_UART_CMD_SET);
  134. status = rda_uart_read(port, RDA_UART_STATUS);
  135. if (cmd_set & RDA_UART_RTS)
  136. mctrl |= TIOCM_RTS;
  137. if (!(status & RDA_UART_CTS))
  138. mctrl |= TIOCM_CTS;
  139. return mctrl;
  140. }
  141. static void rda_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  142. {
  143. u32 val;
  144. if (mctrl & TIOCM_RTS) {
  145. val = rda_uart_read(port, RDA_UART_CMD_SET);
  146. rda_uart_write(port, (val | RDA_UART_RTS), RDA_UART_CMD_SET);
  147. } else {
  148. /* Clear RTS to stop to receive. */
  149. val = rda_uart_read(port, RDA_UART_CMD_CLR);
  150. rda_uart_write(port, (val | RDA_UART_RTS), RDA_UART_CMD_CLR);
  151. }
  152. val = rda_uart_read(port, RDA_UART_CTRL);
  153. if (mctrl & TIOCM_LOOP)
  154. val |= RDA_UART_LOOP_BACK_EN;
  155. else
  156. val &= ~RDA_UART_LOOP_BACK_EN;
  157. rda_uart_write(port, val, RDA_UART_CTRL);
  158. }
  159. static void rda_uart_stop_tx(struct uart_port *port)
  160. {
  161. u32 val;
  162. val = rda_uart_read(port, RDA_UART_IRQ_MASK);
  163. val &= ~RDA_UART_TX_DATA_NEEDED;
  164. rda_uart_write(port, val, RDA_UART_IRQ_MASK);
  165. val = rda_uart_read(port, RDA_UART_CMD_SET);
  166. val |= RDA_UART_TX_FIFO_RESET;
  167. rda_uart_write(port, val, RDA_UART_CMD_SET);
  168. }
  169. static void rda_uart_stop_rx(struct uart_port *port)
  170. {
  171. u32 val;
  172. val = rda_uart_read(port, RDA_UART_IRQ_MASK);
  173. val &= ~(RDA_UART_RX_DATA_AVAILABLE | RDA_UART_RX_TIMEOUT);
  174. rda_uart_write(port, val, RDA_UART_IRQ_MASK);
  175. /* Read Rx buffer before reset to avoid Rx timeout interrupt */
  176. val = rda_uart_read(port, RDA_UART_RXTX_BUFFER);
  177. val = rda_uart_read(port, RDA_UART_CMD_SET);
  178. val |= RDA_UART_RX_FIFO_RESET;
  179. rda_uart_write(port, val, RDA_UART_CMD_SET);
  180. }
  181. static void rda_uart_start_tx(struct uart_port *port)
  182. {
  183. u32 val;
  184. if (uart_tx_stopped(port)) {
  185. rda_uart_stop_tx(port);
  186. return;
  187. }
  188. val = rda_uart_read(port, RDA_UART_IRQ_MASK);
  189. val |= RDA_UART_TX_DATA_NEEDED;
  190. rda_uart_write(port, val, RDA_UART_IRQ_MASK);
  191. }
  192. static void rda_uart_change_baudrate(struct rda_uart_port *rda_port,
  193. unsigned long baud)
  194. {
  195. clk_set_rate(rda_port->clk, baud * 8);
  196. }
  197. static void rda_uart_set_termios(struct uart_port *port,
  198. struct ktermios *termios,
  199. struct ktermios *old)
  200. {
  201. struct rda_uart_port *rda_port = to_rda_uart_port(port);
  202. unsigned long flags;
  203. unsigned int ctrl, cmd_set, cmd_clr, triggers;
  204. unsigned int baud;
  205. u32 irq_mask;
  206. spin_lock_irqsave(&port->lock, flags);
  207. baud = uart_get_baud_rate(port, termios, old, 9600, port->uartclk / 4);
  208. rda_uart_change_baudrate(rda_port, baud);
  209. ctrl = rda_uart_read(port, RDA_UART_CTRL);
  210. cmd_set = rda_uart_read(port, RDA_UART_CMD_SET);
  211. cmd_clr = rda_uart_read(port, RDA_UART_CMD_CLR);
  212. switch (termios->c_cflag & CSIZE) {
  213. case CS5:
  214. case CS6:
  215. dev_warn(port->dev, "bit size not supported, using 7 bits\n");
  216. /* Fall through */
  217. case CS7:
  218. ctrl &= ~RDA_UART_DBITS_8;
  219. break;
  220. default:
  221. ctrl |= RDA_UART_DBITS_8;
  222. break;
  223. }
  224. /* stop bits */
  225. if (termios->c_cflag & CSTOPB)
  226. ctrl |= RDA_UART_TX_SBITS_2;
  227. else
  228. ctrl &= ~RDA_UART_TX_SBITS_2;
  229. /* parity check */
  230. if (termios->c_cflag & PARENB) {
  231. ctrl |= RDA_UART_PARITY_EN;
  232. /* Mark or Space parity */
  233. if (termios->c_cflag & CMSPAR) {
  234. if (termios->c_cflag & PARODD)
  235. ctrl |= RDA_UART_PARITY_MARK;
  236. else
  237. ctrl |= RDA_UART_PARITY_SPACE;
  238. } else if (termios->c_cflag & PARODD) {
  239. ctrl |= RDA_UART_PARITY_ODD;
  240. } else {
  241. ctrl |= RDA_UART_PARITY_EVEN;
  242. }
  243. } else {
  244. ctrl &= ~RDA_UART_PARITY_EN;
  245. }
  246. /* Hardware handshake (RTS/CTS) */
  247. if (termios->c_cflag & CRTSCTS) {
  248. ctrl |= RDA_UART_FLOW_CNT_EN;
  249. cmd_set |= RDA_UART_RTS;
  250. } else {
  251. ctrl &= ~RDA_UART_FLOW_CNT_EN;
  252. cmd_clr |= RDA_UART_RTS;
  253. }
  254. ctrl |= RDA_UART_ENABLE;
  255. ctrl &= ~RDA_UART_DMA_EN;
  256. triggers = (RDA_UART_AFC_LEVEL(20) | RDA_UART_RX_TRIGGER(16));
  257. irq_mask = rda_uart_read(port, RDA_UART_IRQ_MASK);
  258. rda_uart_write(port, 0, RDA_UART_IRQ_MASK);
  259. rda_uart_write(port, triggers, RDA_UART_IRQ_TRIGGERS);
  260. rda_uart_write(port, ctrl, RDA_UART_CTRL);
  261. rda_uart_write(port, cmd_set, RDA_UART_CMD_SET);
  262. rda_uart_write(port, cmd_clr, RDA_UART_CMD_CLR);
  263. rda_uart_write(port, irq_mask, RDA_UART_IRQ_MASK);
  264. /* Don't rewrite B0 */
  265. if (tty_termios_baud_rate(termios))
  266. tty_termios_encode_baud_rate(termios, baud, baud);
  267. /* update the per-port timeout */
  268. uart_update_timeout(port, termios->c_cflag, baud);
  269. spin_unlock_irqrestore(&port->lock, flags);
  270. }
  271. static void rda_uart_send_chars(struct uart_port *port)
  272. {
  273. struct circ_buf *xmit = &port->state->xmit;
  274. unsigned int ch;
  275. u32 val;
  276. if (uart_tx_stopped(port))
  277. return;
  278. if (port->x_char) {
  279. while (!(rda_uart_read(port, RDA_UART_STATUS) &
  280. RDA_UART_TX_FIFO_MASK))
  281. cpu_relax();
  282. rda_uart_write(port, port->x_char, RDA_UART_RXTX_BUFFER);
  283. port->icount.tx++;
  284. port->x_char = 0;
  285. }
  286. while (rda_uart_read(port, RDA_UART_STATUS) & RDA_UART_TX_FIFO_MASK) {
  287. if (uart_circ_empty(xmit))
  288. break;
  289. ch = xmit->buf[xmit->tail];
  290. rda_uart_write(port, ch, RDA_UART_RXTX_BUFFER);
  291. xmit->tail = (xmit->tail + 1) & (SERIAL_XMIT_SIZE - 1);
  292. port->icount.tx++;
  293. }
  294. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  295. uart_write_wakeup(port);
  296. if (!uart_circ_empty(xmit)) {
  297. /* Re-enable Tx FIFO interrupt */
  298. val = rda_uart_read(port, RDA_UART_IRQ_MASK);
  299. val |= RDA_UART_TX_DATA_NEEDED;
  300. rda_uart_write(port, val, RDA_UART_IRQ_MASK);
  301. }
  302. }
  303. static void rda_uart_receive_chars(struct uart_port *port)
  304. {
  305. u32 status, val;
  306. status = rda_uart_read(port, RDA_UART_STATUS);
  307. while ((status & RDA_UART_RX_FIFO_MASK)) {
  308. char flag = TTY_NORMAL;
  309. if (status & RDA_UART_RX_PARITY_ERR) {
  310. port->icount.parity++;
  311. flag = TTY_PARITY;
  312. }
  313. if (status & RDA_UART_RX_FRAMING_ERR) {
  314. port->icount.frame++;
  315. flag = TTY_FRAME;
  316. }
  317. if (status & RDA_UART_RX_OVERFLOW_ERR) {
  318. port->icount.overrun++;
  319. flag = TTY_OVERRUN;
  320. }
  321. val = rda_uart_read(port, RDA_UART_RXTX_BUFFER);
  322. val &= 0xff;
  323. port->icount.rx++;
  324. tty_insert_flip_char(&port->state->port, val, flag);
  325. status = rda_uart_read(port, RDA_UART_STATUS);
  326. }
  327. spin_unlock(&port->lock);
  328. tty_flip_buffer_push(&port->state->port);
  329. spin_lock(&port->lock);
  330. }
  331. static irqreturn_t rda_interrupt(int irq, void *dev_id)
  332. {
  333. struct uart_port *port = dev_id;
  334. unsigned long flags;
  335. u32 val, irq_mask;
  336. spin_lock_irqsave(&port->lock, flags);
  337. /* Clear IRQ cause */
  338. val = rda_uart_read(port, RDA_UART_IRQ_CAUSE);
  339. rda_uart_write(port, val, RDA_UART_IRQ_CAUSE);
  340. if (val & (RDA_UART_RX_DATA_AVAILABLE | RDA_UART_RX_TIMEOUT))
  341. rda_uart_receive_chars(port);
  342. if (val & (RDA_UART_TX_DATA_NEEDED)) {
  343. irq_mask = rda_uart_read(port, RDA_UART_IRQ_MASK);
  344. irq_mask &= ~RDA_UART_TX_DATA_NEEDED;
  345. rda_uart_write(port, irq_mask, RDA_UART_IRQ_MASK);
  346. rda_uart_send_chars(port);
  347. }
  348. spin_unlock_irqrestore(&port->lock, flags);
  349. return IRQ_HANDLED;
  350. }
  351. static int rda_uart_startup(struct uart_port *port)
  352. {
  353. unsigned long flags;
  354. int ret;
  355. u32 val;
  356. spin_lock_irqsave(&port->lock, flags);
  357. rda_uart_write(port, 0, RDA_UART_IRQ_MASK);
  358. spin_unlock_irqrestore(&port->lock, flags);
  359. ret = request_irq(port->irq, rda_interrupt, IRQF_NO_SUSPEND,
  360. "rda-uart", port);
  361. if (ret)
  362. return ret;
  363. spin_lock_irqsave(&port->lock, flags);
  364. val = rda_uart_read(port, RDA_UART_CTRL);
  365. val |= RDA_UART_ENABLE;
  366. rda_uart_write(port, val, RDA_UART_CTRL);
  367. /* enable rx interrupt */
  368. val = rda_uart_read(port, RDA_UART_IRQ_MASK);
  369. val |= (RDA_UART_RX_DATA_AVAILABLE | RDA_UART_RX_TIMEOUT);
  370. rda_uart_write(port, val, RDA_UART_IRQ_MASK);
  371. spin_unlock_irqrestore(&port->lock, flags);
  372. return 0;
  373. }
  374. static void rda_uart_shutdown(struct uart_port *port)
  375. {
  376. unsigned long flags;
  377. u32 val;
  378. spin_lock_irqsave(&port->lock, flags);
  379. rda_uart_stop_tx(port);
  380. rda_uart_stop_rx(port);
  381. val = rda_uart_read(port, RDA_UART_CTRL);
  382. val &= ~RDA_UART_ENABLE;
  383. rda_uart_write(port, val, RDA_UART_CTRL);
  384. spin_unlock_irqrestore(&port->lock, flags);
  385. }
  386. static const char *rda_uart_type(struct uart_port *port)
  387. {
  388. return (port->type == PORT_RDA) ? "rda-uart" : NULL;
  389. }
  390. static int rda_uart_request_port(struct uart_port *port)
  391. {
  392. struct platform_device *pdev = to_platform_device(port->dev);
  393. struct resource *res;
  394. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  395. if (!res)
  396. return -ENXIO;
  397. if (!devm_request_mem_region(port->dev, port->mapbase,
  398. resource_size(res), dev_name(port->dev)))
  399. return -EBUSY;
  400. if (port->flags & UPF_IOREMAP) {
  401. port->membase = devm_ioremap_nocache(port->dev, port->mapbase,
  402. resource_size(res));
  403. if (!port->membase)
  404. return -EBUSY;
  405. }
  406. return 0;
  407. }
  408. static void rda_uart_config_port(struct uart_port *port, int flags)
  409. {
  410. unsigned long irq_flags;
  411. if (flags & UART_CONFIG_TYPE) {
  412. port->type = PORT_RDA;
  413. rda_uart_request_port(port);
  414. }
  415. spin_lock_irqsave(&port->lock, irq_flags);
  416. /* Clear mask, so no surprise interrupts. */
  417. rda_uart_write(port, 0, RDA_UART_IRQ_MASK);
  418. /* Clear status register */
  419. rda_uart_write(port, 0, RDA_UART_STATUS);
  420. spin_unlock_irqrestore(&port->lock, irq_flags);
  421. }
  422. static void rda_uart_release_port(struct uart_port *port)
  423. {
  424. struct platform_device *pdev = to_platform_device(port->dev);
  425. struct resource *res;
  426. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  427. if (!res)
  428. return;
  429. if (port->flags & UPF_IOREMAP) {
  430. devm_release_mem_region(port->dev, port->mapbase,
  431. resource_size(res));
  432. devm_iounmap(port->dev, port->membase);
  433. port->membase = NULL;
  434. }
  435. }
  436. static int rda_uart_verify_port(struct uart_port *port,
  437. struct serial_struct *ser)
  438. {
  439. if (port->type != PORT_RDA)
  440. return -EINVAL;
  441. if (port->irq != ser->irq)
  442. return -EINVAL;
  443. return 0;
  444. }
  445. static const struct uart_ops rda_uart_ops = {
  446. .tx_empty = rda_uart_tx_empty,
  447. .get_mctrl = rda_uart_get_mctrl,
  448. .set_mctrl = rda_uart_set_mctrl,
  449. .start_tx = rda_uart_start_tx,
  450. .stop_tx = rda_uart_stop_tx,
  451. .stop_rx = rda_uart_stop_rx,
  452. .startup = rda_uart_startup,
  453. .shutdown = rda_uart_shutdown,
  454. .set_termios = rda_uart_set_termios,
  455. .type = rda_uart_type,
  456. .request_port = rda_uart_request_port,
  457. .release_port = rda_uart_release_port,
  458. .config_port = rda_uart_config_port,
  459. .verify_port = rda_uart_verify_port,
  460. };
  461. #ifdef CONFIG_SERIAL_RDA_CONSOLE
  462. static void rda_console_putchar(struct uart_port *port, int ch)
  463. {
  464. if (!port->membase)
  465. return;
  466. while (!(rda_uart_read(port, RDA_UART_STATUS) & RDA_UART_TX_FIFO_MASK))
  467. cpu_relax();
  468. rda_uart_write(port, ch, RDA_UART_RXTX_BUFFER);
  469. }
  470. static void rda_uart_port_write(struct uart_port *port, const char *s,
  471. u_int count)
  472. {
  473. u32 old_irq_mask;
  474. unsigned long flags;
  475. int locked;
  476. local_irq_save(flags);
  477. if (port->sysrq) {
  478. locked = 0;
  479. } else if (oops_in_progress) {
  480. locked = spin_trylock(&port->lock);
  481. } else {
  482. spin_lock(&port->lock);
  483. locked = 1;
  484. }
  485. old_irq_mask = rda_uart_read(port, RDA_UART_IRQ_MASK);
  486. rda_uart_write(port, 0, RDA_UART_IRQ_MASK);
  487. uart_console_write(port, s, count, rda_console_putchar);
  488. /* wait until all contents have been sent out */
  489. while (!(rda_uart_read(port, RDA_UART_STATUS) & RDA_UART_TX_FIFO_MASK))
  490. cpu_relax();
  491. rda_uart_write(port, old_irq_mask, RDA_UART_IRQ_MASK);
  492. if (locked)
  493. spin_unlock(&port->lock);
  494. local_irq_restore(flags);
  495. }
  496. static void rda_uart_console_write(struct console *co, const char *s,
  497. u_int count)
  498. {
  499. struct rda_uart_port *rda_port;
  500. rda_port = rda_uart_ports[co->index];
  501. if (!rda_port)
  502. return;
  503. rda_uart_port_write(&rda_port->port, s, count);
  504. }
  505. static int rda_uart_console_setup(struct console *co, char *options)
  506. {
  507. struct rda_uart_port *rda_port;
  508. int baud = 921600;
  509. int bits = 8;
  510. int parity = 'n';
  511. int flow = 'n';
  512. if (co->index < 0 || co->index >= RDA_UART_PORT_NUM)
  513. return -EINVAL;
  514. rda_port = rda_uart_ports[co->index];
  515. if (!rda_port || !rda_port->port.membase)
  516. return -ENODEV;
  517. if (options)
  518. uart_parse_options(options, &baud, &parity, &bits, &flow);
  519. return uart_set_options(&rda_port->port, co, baud, parity, bits, flow);
  520. }
  521. static struct console rda_uart_console = {
  522. .name = RDA_UART_DEV_NAME,
  523. .write = rda_uart_console_write,
  524. .device = uart_console_device,
  525. .setup = rda_uart_console_setup,
  526. .flags = CON_PRINTBUFFER,
  527. .index = -1,
  528. .data = &rda_uart_driver,
  529. };
  530. static int __init rda_uart_console_init(void)
  531. {
  532. register_console(&rda_uart_console);
  533. return 0;
  534. }
  535. console_initcall(rda_uart_console_init);
  536. static void rda_uart_early_console_write(struct console *co,
  537. const char *s,
  538. u_int count)
  539. {
  540. struct earlycon_device *dev = co->data;
  541. rda_uart_port_write(&dev->port, s, count);
  542. }
  543. static int __init
  544. rda_uart_early_console_setup(struct earlycon_device *device, const char *opt)
  545. {
  546. if (!device->port.membase)
  547. return -ENODEV;
  548. device->con->write = rda_uart_early_console_write;
  549. return 0;
  550. }
  551. OF_EARLYCON_DECLARE(rda, "rda,8810pl-uart",
  552. rda_uart_early_console_setup);
  553. #define RDA_UART_CONSOLE (&rda_uart_console)
  554. #else
  555. #define RDA_UART_CONSOLE NULL
  556. #endif /* CONFIG_SERIAL_RDA_CONSOLE */
  557. static struct uart_driver rda_uart_driver = {
  558. .owner = THIS_MODULE,
  559. .driver_name = "rda-uart",
  560. .dev_name = RDA_UART_DEV_NAME,
  561. .nr = RDA_UART_PORT_NUM,
  562. .cons = RDA_UART_CONSOLE,
  563. };
  564. static const struct of_device_id rda_uart_dt_matches[] = {
  565. { .compatible = "rda,8810pl-uart" },
  566. { }
  567. };
  568. MODULE_DEVICE_TABLE(of, rda_uart_dt_matches);
  569. static int rda_uart_probe(struct platform_device *pdev)
  570. {
  571. struct resource *res_mem;
  572. struct rda_uart_port *rda_port;
  573. int ret, irq;
  574. if (pdev->dev.of_node)
  575. pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
  576. if (pdev->id < 0 || pdev->id >= RDA_UART_PORT_NUM) {
  577. dev_err(&pdev->dev, "id %d out of range\n", pdev->id);
  578. return -EINVAL;
  579. }
  580. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  581. if (!res_mem) {
  582. dev_err(&pdev->dev, "could not get mem\n");
  583. return -ENODEV;
  584. }
  585. irq = platform_get_irq(pdev, 0);
  586. if (irq < 0)
  587. return irq;
  588. if (rda_uart_ports[pdev->id]) {
  589. dev_err(&pdev->dev, "port %d already allocated\n", pdev->id);
  590. return -EBUSY;
  591. }
  592. rda_port = devm_kzalloc(&pdev->dev, sizeof(*rda_port), GFP_KERNEL);
  593. if (!rda_port)
  594. return -ENOMEM;
  595. rda_port->clk = devm_clk_get(&pdev->dev, NULL);
  596. if (IS_ERR(rda_port->clk)) {
  597. dev_err(&pdev->dev, "could not get clk\n");
  598. return PTR_ERR(rda_port->clk);
  599. }
  600. rda_port->port.dev = &pdev->dev;
  601. rda_port->port.regshift = 0;
  602. rda_port->port.line = pdev->id;
  603. rda_port->port.type = PORT_RDA;
  604. rda_port->port.iotype = UPIO_MEM;
  605. rda_port->port.mapbase = res_mem->start;
  606. rda_port->port.irq = irq;
  607. rda_port->port.uartclk = clk_get_rate(rda_port->clk);
  608. if (rda_port->port.uartclk == 0) {
  609. dev_err(&pdev->dev, "clock rate is zero\n");
  610. return -EINVAL;
  611. }
  612. rda_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP |
  613. UPF_LOW_LATENCY;
  614. rda_port->port.x_char = 0;
  615. rda_port->port.fifosize = RDA_UART_TX_FIFO_SIZE;
  616. rda_port->port.ops = &rda_uart_ops;
  617. rda_uart_ports[pdev->id] = rda_port;
  618. platform_set_drvdata(pdev, rda_port);
  619. ret = uart_add_one_port(&rda_uart_driver, &rda_port->port);
  620. if (ret)
  621. rda_uart_ports[pdev->id] = NULL;
  622. return ret;
  623. }
  624. static int rda_uart_remove(struct platform_device *pdev)
  625. {
  626. struct rda_uart_port *rda_port = platform_get_drvdata(pdev);
  627. uart_remove_one_port(&rda_uart_driver, &rda_port->port);
  628. rda_uart_ports[pdev->id] = NULL;
  629. return 0;
  630. }
  631. static struct platform_driver rda_uart_platform_driver = {
  632. .probe = rda_uart_probe,
  633. .remove = rda_uart_remove,
  634. .driver = {
  635. .name = "rda-uart",
  636. .of_match_table = rda_uart_dt_matches,
  637. },
  638. };
  639. static int __init rda_uart_init(void)
  640. {
  641. int ret;
  642. ret = uart_register_driver(&rda_uart_driver);
  643. if (ret)
  644. return ret;
  645. ret = platform_driver_register(&rda_uart_platform_driver);
  646. if (ret)
  647. uart_unregister_driver(&rda_uart_driver);
  648. return ret;
  649. }
  650. static void __exit rda_uart_exit(void)
  651. {
  652. platform_driver_unregister(&rda_uart_platform_driver);
  653. uart_unregister_driver(&rda_uart_driver);
  654. }
  655. module_init(rda_uart_init);
  656. module_exit(rda_uart_exit);
  657. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  658. MODULE_DESCRIPTION("RDA8810PL serial device driver");
  659. MODULE_LICENSE("GPL");