sunplus-uart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sunplus SoC UART driver
  4. *
  5. * Author: Hammer Hsieh <[email protected]>
  6. *
  7. * Note1: This driver is 8250-like uart, but are not register compatible.
  8. *
  9. * Note2: On some buses, for preventing data incoherence, must do a read
  10. * for ensure write made it to hardware. In this driver, function startup
  11. * and shutdown did not do a read but only do a write directly. For what?
  12. * In Sunplus bus communication between memory bus and peripheral bus with
  13. * posted write, it will send a specific command after last write command
  14. * to make sure write done. Then memory bus identify the specific command
  15. * and send done signal back to master device. After master device received
  16. * done signal, then proceed next write command. It is no need to do a read
  17. * before write.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/console.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/iopoll.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/reset.h>
  29. #include <linux/serial_core.h>
  30. #include <linux/serial_reg.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/tty.h>
  33. #include <linux/tty_flip.h>
  34. #include <asm/irq.h>
  35. /* Register offsets */
  36. #define SUP_UART_DATA 0x00
  37. #define SUP_UART_LSR 0x04
  38. #define SUP_UART_MSR 0x08
  39. #define SUP_UART_LCR 0x0C
  40. #define SUP_UART_MCR 0x10
  41. #define SUP_UART_DIV_L 0x14
  42. #define SUP_UART_DIV_H 0x18
  43. #define SUP_UART_ISC 0x1C
  44. #define SUP_UART_TX_RESIDUE 0x20
  45. #define SUP_UART_RX_RESIDUE 0x24
  46. /* Line Status Register bits */
  47. #define SUP_UART_LSR_BC BIT(5) /* break condition status */
  48. #define SUP_UART_LSR_FE BIT(4) /* frame error status */
  49. #define SUP_UART_LSR_OE BIT(3) /* overrun error status */
  50. #define SUP_UART_LSR_PE BIT(2) /* parity error status */
  51. #define SUP_UART_LSR_RX BIT(1) /* 1: receive fifo not empty */
  52. #define SUP_UART_LSR_TX BIT(0) /* 1: transmit fifo is not full */
  53. #define SUP_UART_LSR_TX_NOT_FULL 1
  54. #define SUP_UART_LSR_BRK_ERROR_BITS GENMASK(5, 2)
  55. /* Line Control Register bits */
  56. #define SUP_UART_LCR_SBC BIT(5) /* select break condition */
  57. /* Modem Control Register bits */
  58. #define SUP_UART_MCR_RI BIT(3) /* ring indicator */
  59. #define SUP_UART_MCR_DCD BIT(2) /* data carrier detect */
  60. /* Interrupt Status/Control Register bits */
  61. #define SUP_UART_ISC_RXM BIT(5) /* RX interrupt enable */
  62. #define SUP_UART_ISC_TXM BIT(4) /* TX interrupt enable */
  63. #define SUP_UART_ISC_RX BIT(1) /* RX interrupt status */
  64. #define SUP_UART_ISC_TX BIT(0) /* TX interrupt status */
  65. #define SUP_DUMMY_READ BIT(16) /* drop bytes received on a !CREAD port */
  66. #define SUP_UART_NR 5
  67. struct sunplus_uart_port {
  68. struct uart_port port;
  69. struct clk *clk;
  70. struct reset_control *rstc;
  71. };
  72. static void sp_uart_put_char(struct uart_port *port, unsigned int ch)
  73. {
  74. writel(ch, port->membase + SUP_UART_DATA);
  75. }
  76. static u32 sunplus_tx_buf_not_full(struct uart_port *port)
  77. {
  78. unsigned int lsr = readl(port->membase + SUP_UART_LSR);
  79. return (lsr & SUP_UART_LSR_TX) ? SUP_UART_LSR_TX_NOT_FULL : 0;
  80. }
  81. static unsigned int sunplus_tx_empty(struct uart_port *port)
  82. {
  83. unsigned int lsr = readl(port->membase + SUP_UART_LSR);
  84. return (lsr & UART_LSR_TEMT) ? TIOCSER_TEMT : 0;
  85. }
  86. static void sunplus_set_mctrl(struct uart_port *port, unsigned int mctrl)
  87. {
  88. unsigned int mcr = readl(port->membase + SUP_UART_MCR);
  89. if (mctrl & TIOCM_DTR)
  90. mcr |= UART_MCR_DTR;
  91. else
  92. mcr &= ~UART_MCR_DTR;
  93. if (mctrl & TIOCM_RTS)
  94. mcr |= UART_MCR_RTS;
  95. else
  96. mcr &= ~UART_MCR_RTS;
  97. if (mctrl & TIOCM_CAR)
  98. mcr |= SUP_UART_MCR_DCD;
  99. else
  100. mcr &= ~SUP_UART_MCR_DCD;
  101. if (mctrl & TIOCM_RI)
  102. mcr |= SUP_UART_MCR_RI;
  103. else
  104. mcr &= ~SUP_UART_MCR_RI;
  105. if (mctrl & TIOCM_LOOP)
  106. mcr |= UART_MCR_LOOP;
  107. else
  108. mcr &= ~UART_MCR_LOOP;
  109. writel(mcr, port->membase + SUP_UART_MCR);
  110. }
  111. static unsigned int sunplus_get_mctrl(struct uart_port *port)
  112. {
  113. unsigned int mcr, ret = 0;
  114. mcr = readl(port->membase + SUP_UART_MCR);
  115. if (mcr & UART_MCR_DTR)
  116. ret |= TIOCM_DTR;
  117. if (mcr & UART_MCR_RTS)
  118. ret |= TIOCM_RTS;
  119. if (mcr & SUP_UART_MCR_DCD)
  120. ret |= TIOCM_CAR;
  121. if (mcr & SUP_UART_MCR_RI)
  122. ret |= TIOCM_RI;
  123. if (mcr & UART_MCR_LOOP)
  124. ret |= TIOCM_LOOP;
  125. return ret;
  126. }
  127. static void sunplus_stop_tx(struct uart_port *port)
  128. {
  129. unsigned int isc;
  130. isc = readl(port->membase + SUP_UART_ISC);
  131. isc &= ~SUP_UART_ISC_TXM;
  132. writel(isc, port->membase + SUP_UART_ISC);
  133. }
  134. static void sunplus_start_tx(struct uart_port *port)
  135. {
  136. unsigned int isc;
  137. isc = readl(port->membase + SUP_UART_ISC);
  138. isc |= SUP_UART_ISC_TXM;
  139. writel(isc, port->membase + SUP_UART_ISC);
  140. }
  141. static void sunplus_stop_rx(struct uart_port *port)
  142. {
  143. unsigned int isc;
  144. isc = readl(port->membase + SUP_UART_ISC);
  145. isc &= ~SUP_UART_ISC_RXM;
  146. writel(isc, port->membase + SUP_UART_ISC);
  147. }
  148. static void sunplus_break_ctl(struct uart_port *port, int ctl)
  149. {
  150. unsigned long flags;
  151. unsigned int lcr;
  152. spin_lock_irqsave(&port->lock, flags);
  153. lcr = readl(port->membase + SUP_UART_LCR);
  154. if (ctl)
  155. lcr |= SUP_UART_LCR_SBC; /* start break */
  156. else
  157. lcr &= ~SUP_UART_LCR_SBC; /* stop break */
  158. writel(lcr, port->membase + SUP_UART_LCR);
  159. spin_unlock_irqrestore(&port->lock, flags);
  160. }
  161. static void transmit_chars(struct uart_port *port)
  162. {
  163. struct circ_buf *xmit = &port->state->xmit;
  164. if (port->x_char) {
  165. sp_uart_put_char(port, port->x_char);
  166. port->icount.tx++;
  167. port->x_char = 0;
  168. return;
  169. }
  170. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  171. sunplus_stop_tx(port);
  172. return;
  173. }
  174. do {
  175. sp_uart_put_char(port, xmit->buf[xmit->tail]);
  176. xmit->tail = (xmit->tail + 1) % UART_XMIT_SIZE;
  177. port->icount.tx++;
  178. if (uart_circ_empty(xmit))
  179. break;
  180. } while (sunplus_tx_buf_not_full(port));
  181. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  182. uart_write_wakeup(port);
  183. if (uart_circ_empty(xmit))
  184. sunplus_stop_tx(port);
  185. }
  186. static void receive_chars(struct uart_port *port)
  187. {
  188. unsigned int lsr = readl(port->membase + SUP_UART_LSR);
  189. unsigned int ch, flag;
  190. do {
  191. ch = readl(port->membase + SUP_UART_DATA);
  192. flag = TTY_NORMAL;
  193. port->icount.rx++;
  194. if (unlikely(lsr & SUP_UART_LSR_BRK_ERROR_BITS)) {
  195. if (lsr & SUP_UART_LSR_BC) {
  196. lsr &= ~(SUP_UART_LSR_FE | SUP_UART_LSR_PE);
  197. port->icount.brk++;
  198. flag = TTY_BREAK;
  199. if (uart_handle_break(port))
  200. goto ignore_char;
  201. } else if (lsr & SUP_UART_LSR_PE) {
  202. port->icount.parity++;
  203. flag = TTY_PARITY;
  204. } else if (lsr & SUP_UART_LSR_FE) {
  205. port->icount.frame++;
  206. flag = TTY_FRAME;
  207. }
  208. if (lsr & SUP_UART_LSR_OE)
  209. port->icount.overrun++;
  210. }
  211. if (port->ignore_status_mask & SUP_DUMMY_READ)
  212. goto ignore_char;
  213. if (uart_handle_sysrq_char(port, ch))
  214. goto ignore_char;
  215. uart_insert_char(port, lsr, SUP_UART_LSR_OE, ch, flag);
  216. ignore_char:
  217. lsr = readl(port->membase + SUP_UART_LSR);
  218. } while (lsr & SUP_UART_LSR_RX);
  219. tty_flip_buffer_push(&port->state->port);
  220. }
  221. static irqreturn_t sunplus_uart_irq(int irq, void *args)
  222. {
  223. struct uart_port *port = args;
  224. unsigned int isc;
  225. spin_lock(&port->lock);
  226. isc = readl(port->membase + SUP_UART_ISC);
  227. if (isc & SUP_UART_ISC_RX)
  228. receive_chars(port);
  229. if (isc & SUP_UART_ISC_TX)
  230. transmit_chars(port);
  231. spin_unlock(&port->lock);
  232. return IRQ_HANDLED;
  233. }
  234. static int sunplus_startup(struct uart_port *port)
  235. {
  236. unsigned long flags;
  237. unsigned int isc = 0;
  238. int ret;
  239. ret = request_irq(port->irq, sunplus_uart_irq, 0, "sunplus_uart", port);
  240. if (ret)
  241. return ret;
  242. spin_lock_irqsave(&port->lock, flags);
  243. /* isc define Bit[7:4] int setting, Bit[3:0] int status
  244. * isc register will clean Bit[3:0] int status after read
  245. * only do a write to Bit[7:4] int setting
  246. */
  247. isc |= SUP_UART_ISC_RXM;
  248. writel(isc, port->membase + SUP_UART_ISC);
  249. spin_unlock_irqrestore(&port->lock, flags);
  250. return 0;
  251. }
  252. static void sunplus_shutdown(struct uart_port *port)
  253. {
  254. unsigned long flags;
  255. spin_lock_irqsave(&port->lock, flags);
  256. /* isc define Bit[7:4] int setting, Bit[3:0] int status
  257. * isc register will clean Bit[3:0] int status after read
  258. * only do a write to Bit[7:4] int setting
  259. */
  260. writel(0, port->membase + SUP_UART_ISC); /* disable all interrupt */
  261. spin_unlock_irqrestore(&port->lock, flags);
  262. free_irq(port->irq, port);
  263. }
  264. static void sunplus_set_termios(struct uart_port *port,
  265. struct ktermios *termios,
  266. const struct ktermios *oldtermios)
  267. {
  268. u32 ext, div, div_l, div_h, baud, lcr;
  269. u32 clk = port->uartclk;
  270. unsigned long flags;
  271. baud = uart_get_baud_rate(port, termios, oldtermios, 0, port->uartclk / 16);
  272. /* baud rate = uartclk / ((16 * divisor + 1) + divisor_ext) */
  273. clk += baud >> 1;
  274. div = clk / baud;
  275. ext = div & 0x0F;
  276. div = (div >> 4) - 1;
  277. div_l = (div & 0xFF) | (ext << 12);
  278. div_h = div >> 8;
  279. switch (termios->c_cflag & CSIZE) {
  280. case CS5:
  281. lcr = UART_LCR_WLEN5;
  282. break;
  283. case CS6:
  284. lcr = UART_LCR_WLEN6;
  285. break;
  286. case CS7:
  287. lcr = UART_LCR_WLEN7;
  288. break;
  289. default:
  290. lcr = UART_LCR_WLEN8;
  291. break;
  292. }
  293. if (termios->c_cflag & CSTOPB)
  294. lcr |= UART_LCR_STOP;
  295. if (termios->c_cflag & PARENB) {
  296. lcr |= UART_LCR_PARITY;
  297. if (!(termios->c_cflag & PARODD))
  298. lcr |= UART_LCR_EPAR;
  299. }
  300. spin_lock_irqsave(&port->lock, flags);
  301. uart_update_timeout(port, termios->c_cflag, baud);
  302. port->read_status_mask = 0;
  303. if (termios->c_iflag & INPCK)
  304. port->read_status_mask |= SUP_UART_LSR_PE | SUP_UART_LSR_FE;
  305. if (termios->c_iflag & (BRKINT | PARMRK))
  306. port->read_status_mask |= SUP_UART_LSR_BC;
  307. /* Characters to ignore */
  308. port->ignore_status_mask = 0;
  309. if (termios->c_iflag & IGNPAR)
  310. port->ignore_status_mask |= SUP_UART_LSR_FE | SUP_UART_LSR_PE;
  311. if (termios->c_iflag & IGNBRK) {
  312. port->ignore_status_mask |= SUP_UART_LSR_BC;
  313. if (termios->c_iflag & IGNPAR)
  314. port->ignore_status_mask |= SUP_UART_LSR_OE;
  315. }
  316. /* Ignore all characters if CREAD is not set */
  317. if ((termios->c_cflag & CREAD) == 0) {
  318. port->ignore_status_mask |= SUP_DUMMY_READ;
  319. /* flush rx data FIFO */
  320. writel(0, port->membase + SUP_UART_RX_RESIDUE);
  321. }
  322. /* Settings for baud rate divisor and lcr */
  323. writel(div_h, port->membase + SUP_UART_DIV_H);
  324. writel(div_l, port->membase + SUP_UART_DIV_L);
  325. writel(lcr, port->membase + SUP_UART_LCR);
  326. spin_unlock_irqrestore(&port->lock, flags);
  327. }
  328. static void sunplus_set_ldisc(struct uart_port *port, struct ktermios *termios)
  329. {
  330. int new = termios->c_line;
  331. if (new == N_PPS)
  332. port->flags |= UPF_HARDPPS_CD;
  333. else
  334. port->flags &= ~UPF_HARDPPS_CD;
  335. }
  336. static const char *sunplus_type(struct uart_port *port)
  337. {
  338. return port->type == PORT_SUNPLUS ? "sunplus_uart" : NULL;
  339. }
  340. static void sunplus_config_port(struct uart_port *port, int type)
  341. {
  342. if (type & UART_CONFIG_TYPE)
  343. port->type = PORT_SUNPLUS;
  344. }
  345. static int sunplus_verify_port(struct uart_port *port, struct serial_struct *ser)
  346. {
  347. if (ser->type != PORT_UNKNOWN && ser->type != PORT_SUNPLUS)
  348. return -EINVAL;
  349. return 0;
  350. }
  351. #if defined(CONFIG_SERIAL_SUNPLUS_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  352. static void wait_for_xmitr(struct uart_port *port)
  353. {
  354. unsigned int val;
  355. int ret;
  356. /* Wait while FIFO is full or timeout */
  357. ret = readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,
  358. (val & SUP_UART_LSR_TX), 1, 10000);
  359. if (ret == -ETIMEDOUT) {
  360. dev_err(port->dev, "Timeout waiting while UART TX FULL\n");
  361. return;
  362. }
  363. }
  364. #endif
  365. #ifdef CONFIG_CONSOLE_POLL
  366. static void sunplus_poll_put_char(struct uart_port *port, unsigned char data)
  367. {
  368. wait_for_xmitr(port);
  369. sp_uart_put_char(port, data);
  370. }
  371. static int sunplus_poll_get_char(struct uart_port *port)
  372. {
  373. unsigned int lsr = readl(port->membase + SUP_UART_LSR);
  374. if (!(lsr & SUP_UART_LSR_RX))
  375. return NO_POLL_CHAR;
  376. return readl(port->membase + SUP_UART_DATA);
  377. }
  378. #endif
  379. static const struct uart_ops sunplus_uart_ops = {
  380. .tx_empty = sunplus_tx_empty,
  381. .set_mctrl = sunplus_set_mctrl,
  382. .get_mctrl = sunplus_get_mctrl,
  383. .stop_tx = sunplus_stop_tx,
  384. .start_tx = sunplus_start_tx,
  385. .stop_rx = sunplus_stop_rx,
  386. .break_ctl = sunplus_break_ctl,
  387. .startup = sunplus_startup,
  388. .shutdown = sunplus_shutdown,
  389. .set_termios = sunplus_set_termios,
  390. .set_ldisc = sunplus_set_ldisc,
  391. .type = sunplus_type,
  392. .config_port = sunplus_config_port,
  393. .verify_port = sunplus_verify_port,
  394. #ifdef CONFIG_CONSOLE_POLL
  395. .poll_put_char = sunplus_poll_put_char,
  396. .poll_get_char = sunplus_poll_get_char,
  397. #endif
  398. };
  399. #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
  400. static struct sunplus_uart_port *sunplus_console_ports[SUP_UART_NR];
  401. static void sunplus_uart_console_putchar(struct uart_port *port,
  402. unsigned char ch)
  403. {
  404. wait_for_xmitr(port);
  405. sp_uart_put_char(port, ch);
  406. }
  407. static void sunplus_console_write(struct console *co,
  408. const char *s,
  409. unsigned int count)
  410. {
  411. unsigned long flags;
  412. int locked = 1;
  413. local_irq_save(flags);
  414. if (sunplus_console_ports[co->index]->port.sysrq)
  415. locked = 0;
  416. else if (oops_in_progress)
  417. locked = spin_trylock(&sunplus_console_ports[co->index]->port.lock);
  418. else
  419. spin_lock(&sunplus_console_ports[co->index]->port.lock);
  420. uart_console_write(&sunplus_console_ports[co->index]->port, s, count,
  421. sunplus_uart_console_putchar);
  422. if (locked)
  423. spin_unlock(&sunplus_console_ports[co->index]->port.lock);
  424. local_irq_restore(flags);
  425. }
  426. static int __init sunplus_console_setup(struct console *co, char *options)
  427. {
  428. struct sunplus_uart_port *sup;
  429. int baud = 115200;
  430. int bits = 8;
  431. int parity = 'n';
  432. int flow = 'n';
  433. if (co->index < 0 || co->index >= SUP_UART_NR)
  434. return -EINVAL;
  435. sup = sunplus_console_ports[co->index];
  436. if (!sup)
  437. return -ENODEV;
  438. if (options)
  439. uart_parse_options(options, &baud, &parity, &bits, &flow);
  440. return uart_set_options(&sup->port, co, baud, parity, bits, flow);
  441. }
  442. static struct uart_driver sunplus_uart_driver;
  443. static struct console sunplus_uart_console = {
  444. .name = "ttySUP",
  445. .write = sunplus_console_write,
  446. .device = uart_console_device,
  447. .setup = sunplus_console_setup,
  448. .flags = CON_PRINTBUFFER,
  449. .index = -1,
  450. .data = &sunplus_uart_driver
  451. };
  452. #define SERIAL_SUNPLUS_CONSOLE (&sunplus_uart_console)
  453. #else
  454. #define SERIAL_SUNPLUS_CONSOLE NULL
  455. #endif
  456. static struct uart_driver sunplus_uart_driver = {
  457. .owner = THIS_MODULE,
  458. .driver_name = "sunplus_uart",
  459. .dev_name = "ttySUP",
  460. .major = TTY_MAJOR,
  461. .minor = 64,
  462. .nr = SUP_UART_NR,
  463. .cons = SERIAL_SUNPLUS_CONSOLE,
  464. };
  465. static void sunplus_uart_disable_unprepare(void *data)
  466. {
  467. clk_disable_unprepare(data);
  468. }
  469. static void sunplus_uart_reset_control_assert(void *data)
  470. {
  471. reset_control_assert(data);
  472. }
  473. static int sunplus_uart_probe(struct platform_device *pdev)
  474. {
  475. struct sunplus_uart_port *sup;
  476. struct uart_port *port;
  477. struct resource *res;
  478. int ret, irq;
  479. pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
  480. if (pdev->id < 0 || pdev->id >= SUP_UART_NR)
  481. return -EINVAL;
  482. sup = devm_kzalloc(&pdev->dev, sizeof(*sup), GFP_KERNEL);
  483. if (!sup)
  484. return -ENOMEM;
  485. sup->clk = devm_clk_get_optional(&pdev->dev, NULL);
  486. if (IS_ERR(sup->clk))
  487. return dev_err_probe(&pdev->dev, PTR_ERR(sup->clk), "clk not found\n");
  488. ret = clk_prepare_enable(sup->clk);
  489. if (ret)
  490. return ret;
  491. ret = devm_add_action_or_reset(&pdev->dev, sunplus_uart_disable_unprepare, sup->clk);
  492. if (ret)
  493. return ret;
  494. sup->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  495. if (IS_ERR(sup->rstc))
  496. return dev_err_probe(&pdev->dev, PTR_ERR(sup->rstc), "rstc not found\n");
  497. port = &sup->port;
  498. port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  499. if (IS_ERR(port->membase))
  500. return dev_err_probe(&pdev->dev, PTR_ERR(port->membase), "membase not found\n");
  501. irq = platform_get_irq(pdev, 0);
  502. if (irq < 0)
  503. return irq;
  504. port->mapbase = res->start;
  505. port->uartclk = clk_get_rate(sup->clk);
  506. port->line = pdev->id;
  507. port->irq = irq;
  508. port->dev = &pdev->dev;
  509. port->iotype = UPIO_MEM;
  510. port->ops = &sunplus_uart_ops;
  511. port->flags = UPF_BOOT_AUTOCONF;
  512. port->fifosize = 128;
  513. ret = reset_control_deassert(sup->rstc);
  514. if (ret)
  515. return ret;
  516. ret = devm_add_action_or_reset(&pdev->dev, sunplus_uart_reset_control_assert, sup->rstc);
  517. if (ret)
  518. return ret;
  519. #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
  520. sunplus_console_ports[sup->port.line] = sup;
  521. #endif
  522. platform_set_drvdata(pdev, &sup->port);
  523. ret = uart_add_one_port(&sunplus_uart_driver, &sup->port);
  524. #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
  525. if (ret)
  526. sunplus_console_ports[sup->port.line] = NULL;
  527. #endif
  528. return ret;
  529. }
  530. static int sunplus_uart_remove(struct platform_device *pdev)
  531. {
  532. struct sunplus_uart_port *sup = platform_get_drvdata(pdev);
  533. uart_remove_one_port(&sunplus_uart_driver, &sup->port);
  534. return 0;
  535. }
  536. static int __maybe_unused sunplus_uart_suspend(struct device *dev)
  537. {
  538. struct sunplus_uart_port *sup = dev_get_drvdata(dev);
  539. if (!uart_console(&sup->port))
  540. uart_suspend_port(&sunplus_uart_driver, &sup->port);
  541. return 0;
  542. }
  543. static int __maybe_unused sunplus_uart_resume(struct device *dev)
  544. {
  545. struct sunplus_uart_port *sup = dev_get_drvdata(dev);
  546. if (!uart_console(&sup->port))
  547. uart_resume_port(&sunplus_uart_driver, &sup->port);
  548. return 0;
  549. }
  550. static const struct dev_pm_ops sunplus_uart_pm_ops = {
  551. SET_SYSTEM_SLEEP_PM_OPS(sunplus_uart_suspend, sunplus_uart_resume)
  552. };
  553. static const struct of_device_id sp_uart_of_match[] = {
  554. { .compatible = "sunplus,sp7021-uart" },
  555. {}
  556. };
  557. MODULE_DEVICE_TABLE(of, sp_uart_of_match);
  558. static struct platform_driver sunplus_uart_platform_driver = {
  559. .probe = sunplus_uart_probe,
  560. .remove = sunplus_uart_remove,
  561. .driver = {
  562. .name = "sunplus_uart",
  563. .of_match_table = sp_uart_of_match,
  564. .pm = &sunplus_uart_pm_ops,
  565. }
  566. };
  567. static int __init sunplus_uart_init(void)
  568. {
  569. int ret;
  570. ret = uart_register_driver(&sunplus_uart_driver);
  571. if (ret)
  572. return ret;
  573. ret = platform_driver_register(&sunplus_uart_platform_driver);
  574. if (ret)
  575. uart_unregister_driver(&sunplus_uart_driver);
  576. return ret;
  577. }
  578. module_init(sunplus_uart_init);
  579. static void __exit sunplus_uart_exit(void)
  580. {
  581. platform_driver_unregister(&sunplus_uart_platform_driver);
  582. uart_unregister_driver(&sunplus_uart_driver);
  583. }
  584. module_exit(sunplus_uart_exit);
  585. #ifdef CONFIG_SERIAL_EARLYCON
  586. static void sunplus_uart_putc(struct uart_port *port, unsigned char c)
  587. {
  588. unsigned int val;
  589. int ret;
  590. ret = readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,
  591. (val & UART_LSR_TEMT), 1, 10000);
  592. if (ret)
  593. return;
  594. writel(c, port->membase + SUP_UART_DATA);
  595. }
  596. static void sunplus_uart_early_write(struct console *con, const char *s, unsigned int n)
  597. {
  598. struct earlycon_device *dev = con->data;
  599. uart_console_write(&dev->port, s, n, sunplus_uart_putc);
  600. }
  601. static int __init
  602. sunplus_uart_early_setup(struct earlycon_device *dev, const char *opt)
  603. {
  604. if (!(dev->port.membase || dev->port.iobase))
  605. return -ENODEV;
  606. dev->con->write = sunplus_uart_early_write;
  607. return 0;
  608. }
  609. OF_EARLYCON_DECLARE(sunplus_uart, "sunplus,sp7021-uart", sunplus_uart_early_setup);
  610. #endif
  611. MODULE_DESCRIPTION("Sunplus UART driver");
  612. MODULE_AUTHOR("Hammer Hsieh <[email protected]>");
  613. MODULE_LICENSE("GPL v2");