sunplus-uart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sunplus SoC UART driver
  4. *
  5. * Author: Hammer Hsieh <hammerh0314@gmail.com>
  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. uart_port_lock_irqsave(port, &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. uart_port_unlock_irqrestore(port, flags);
  160. }
  161. static void transmit_chars(struct uart_port *port)
  162. {
  163. struct tty_port *tport = &port->state->port;
  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 (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) {
  171. sunplus_stop_tx(port);
  172. return;
  173. }
  174. do {
  175. unsigned char ch;
  176. if (!uart_fifo_get(port, &ch))
  177. break;
  178. sp_uart_put_char(port, ch);
  179. } while (sunplus_tx_buf_not_full(port));
  180. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  181. uart_write_wakeup(port);
  182. if (kfifo_is_empty(&tport->xmit_fifo))
  183. sunplus_stop_tx(port);
  184. }
  185. static void receive_chars(struct uart_port *port)
  186. {
  187. unsigned int lsr = readl(port->membase + SUP_UART_LSR);
  188. u8 ch, flag;
  189. do {
  190. ch = readl(port->membase + SUP_UART_DATA);
  191. flag = TTY_NORMAL;
  192. port->icount.rx++;
  193. if (unlikely(lsr & SUP_UART_LSR_BRK_ERROR_BITS)) {
  194. if (lsr & SUP_UART_LSR_BC) {
  195. lsr &= ~(SUP_UART_LSR_FE | SUP_UART_LSR_PE);
  196. port->icount.brk++;
  197. flag = TTY_BREAK;
  198. if (uart_handle_break(port))
  199. goto ignore_char;
  200. } else if (lsr & SUP_UART_LSR_PE) {
  201. port->icount.parity++;
  202. flag = TTY_PARITY;
  203. } else if (lsr & SUP_UART_LSR_FE) {
  204. port->icount.frame++;
  205. flag = TTY_FRAME;
  206. }
  207. if (lsr & SUP_UART_LSR_OE)
  208. port->icount.overrun++;
  209. }
  210. if (port->ignore_status_mask & SUP_DUMMY_READ)
  211. goto ignore_char;
  212. if (uart_prepare_sysrq_char(port, ch))
  213. goto ignore_char;
  214. uart_insert_char(port, lsr, SUP_UART_LSR_OE, ch, flag);
  215. ignore_char:
  216. lsr = readl(port->membase + SUP_UART_LSR);
  217. } while (lsr & SUP_UART_LSR_RX);
  218. tty_flip_buffer_push(&port->state->port);
  219. }
  220. static irqreturn_t sunplus_uart_irq(int irq, void *args)
  221. {
  222. struct uart_port *port = args;
  223. unsigned int isc;
  224. uart_port_lock(port);
  225. isc = readl(port->membase + SUP_UART_ISC);
  226. if (isc & SUP_UART_ISC_RX)
  227. receive_chars(port);
  228. if (isc & SUP_UART_ISC_TX)
  229. transmit_chars(port);
  230. uart_unlock_and_check_sysrq(port);
  231. return IRQ_HANDLED;
  232. }
  233. static int sunplus_startup(struct uart_port *port)
  234. {
  235. unsigned long flags;
  236. unsigned int isc = 0;
  237. int ret;
  238. ret = request_irq(port->irq, sunplus_uart_irq, 0, "sunplus_uart", port);
  239. if (ret)
  240. return ret;
  241. uart_port_lock_irqsave(port, &flags);
  242. /* isc define Bit[7:4] int setting, Bit[3:0] int status
  243. * isc register will clean Bit[3:0] int status after read
  244. * only do a write to Bit[7:4] int setting
  245. */
  246. isc |= SUP_UART_ISC_RXM;
  247. writel(isc, port->membase + SUP_UART_ISC);
  248. uart_port_unlock_irqrestore(port, flags);
  249. return 0;
  250. }
  251. static void sunplus_shutdown(struct uart_port *port)
  252. {
  253. unsigned long flags;
  254. uart_port_lock_irqsave(port, &flags);
  255. /* isc define Bit[7:4] int setting, Bit[3:0] int status
  256. * isc register will clean Bit[3:0] int status after read
  257. * only do a write to Bit[7:4] int setting
  258. */
  259. writel(0, port->membase + SUP_UART_ISC); /* disable all interrupt */
  260. uart_port_unlock_irqrestore(port, flags);
  261. free_irq(port->irq, port);
  262. }
  263. static void sunplus_set_termios(struct uart_port *port,
  264. struct ktermios *termios,
  265. const struct ktermios *oldtermios)
  266. {
  267. u32 ext, div, div_l, div_h, baud, lcr;
  268. u32 clk = port->uartclk;
  269. unsigned long flags;
  270. baud = uart_get_baud_rate(port, termios, oldtermios, 0, port->uartclk / 16);
  271. /* baud rate = uartclk / ((16 * divisor + 1) + divisor_ext) */
  272. clk += baud >> 1;
  273. div = clk / baud;
  274. ext = div & 0x0F;
  275. div = (div >> 4) - 1;
  276. div_l = (div & 0xFF) | (ext << 12);
  277. div_h = div >> 8;
  278. switch (termios->c_cflag & CSIZE) {
  279. case CS5:
  280. lcr = UART_LCR_WLEN5;
  281. break;
  282. case CS6:
  283. lcr = UART_LCR_WLEN6;
  284. break;
  285. case CS7:
  286. lcr = UART_LCR_WLEN7;
  287. break;
  288. default:
  289. lcr = UART_LCR_WLEN8;
  290. break;
  291. }
  292. if (termios->c_cflag & CSTOPB)
  293. lcr |= UART_LCR_STOP;
  294. if (termios->c_cflag & PARENB) {
  295. lcr |= UART_LCR_PARITY;
  296. if (!(termios->c_cflag & PARODD))
  297. lcr |= UART_LCR_EPAR;
  298. }
  299. uart_port_lock_irqsave(port, &flags);
  300. uart_update_timeout(port, termios->c_cflag, baud);
  301. port->read_status_mask = 0;
  302. if (termios->c_iflag & INPCK)
  303. port->read_status_mask |= SUP_UART_LSR_PE | SUP_UART_LSR_FE;
  304. if (termios->c_iflag & (BRKINT | PARMRK))
  305. port->read_status_mask |= SUP_UART_LSR_BC;
  306. /* Characters to ignore */
  307. port->ignore_status_mask = 0;
  308. if (termios->c_iflag & IGNPAR)
  309. port->ignore_status_mask |= SUP_UART_LSR_FE | SUP_UART_LSR_PE;
  310. if (termios->c_iflag & IGNBRK) {
  311. port->ignore_status_mask |= SUP_UART_LSR_BC;
  312. if (termios->c_iflag & IGNPAR)
  313. port->ignore_status_mask |= SUP_UART_LSR_OE;
  314. }
  315. /* Ignore all characters if CREAD is not set */
  316. if ((termios->c_cflag & CREAD) == 0) {
  317. port->ignore_status_mask |= SUP_DUMMY_READ;
  318. /* flush rx data FIFO */
  319. writel(0, port->membase + SUP_UART_RX_RESIDUE);
  320. }
  321. /* Settings for baud rate divisor and lcr */
  322. writel(div_h, port->membase + SUP_UART_DIV_H);
  323. writel(div_l, port->membase + SUP_UART_DIV_L);
  324. writel(lcr, port->membase + SUP_UART_LCR);
  325. uart_port_unlock_irqrestore(port, flags);
  326. }
  327. static void sunplus_set_ldisc(struct uart_port *port, struct ktermios *termios)
  328. {
  329. int new = termios->c_line;
  330. if (new == N_PPS)
  331. port->flags |= UPF_HARDPPS_CD;
  332. else
  333. port->flags &= ~UPF_HARDPPS_CD;
  334. }
  335. static const char *sunplus_type(struct uart_port *port)
  336. {
  337. return port->type == PORT_SUNPLUS ? "sunplus_uart" : NULL;
  338. }
  339. static void sunplus_config_port(struct uart_port *port, int type)
  340. {
  341. if (type & UART_CONFIG_TYPE)
  342. port->type = PORT_SUNPLUS;
  343. }
  344. static int sunplus_verify_port(struct uart_port *port, struct serial_struct *ser)
  345. {
  346. if (ser->type != PORT_UNKNOWN && ser->type != PORT_SUNPLUS)
  347. return -EINVAL;
  348. return 0;
  349. }
  350. #if defined(CONFIG_SERIAL_SUNPLUS_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  351. static void wait_for_xmitr(struct uart_port *port)
  352. {
  353. unsigned int val;
  354. int ret;
  355. /* Wait while FIFO is full or timeout */
  356. ret = readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,
  357. (val & SUP_UART_LSR_TX), 1, 10000);
  358. if (ret == -ETIMEDOUT) {
  359. dev_err(port->dev, "Timeout waiting while UART TX FULL\n");
  360. return;
  361. }
  362. }
  363. #endif
  364. #ifdef CONFIG_CONSOLE_POLL
  365. static void sunplus_poll_put_char(struct uart_port *port, unsigned char data)
  366. {
  367. wait_for_xmitr(port);
  368. sp_uart_put_char(port, data);
  369. }
  370. static int sunplus_poll_get_char(struct uart_port *port)
  371. {
  372. unsigned int lsr = readl(port->membase + SUP_UART_LSR);
  373. if (!(lsr & SUP_UART_LSR_RX))
  374. return NO_POLL_CHAR;
  375. return readl(port->membase + SUP_UART_DATA);
  376. }
  377. #endif
  378. static const struct uart_ops sunplus_uart_ops = {
  379. .tx_empty = sunplus_tx_empty,
  380. .set_mctrl = sunplus_set_mctrl,
  381. .get_mctrl = sunplus_get_mctrl,
  382. .stop_tx = sunplus_stop_tx,
  383. .start_tx = sunplus_start_tx,
  384. .stop_rx = sunplus_stop_rx,
  385. .break_ctl = sunplus_break_ctl,
  386. .startup = sunplus_startup,
  387. .shutdown = sunplus_shutdown,
  388. .set_termios = sunplus_set_termios,
  389. .set_ldisc = sunplus_set_ldisc,
  390. .type = sunplus_type,
  391. .config_port = sunplus_config_port,
  392. .verify_port = sunplus_verify_port,
  393. #ifdef CONFIG_CONSOLE_POLL
  394. .poll_put_char = sunplus_poll_put_char,
  395. .poll_get_char = sunplus_poll_get_char,
  396. #endif
  397. };
  398. #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
  399. static struct sunplus_uart_port *sunplus_console_ports[SUP_UART_NR];
  400. static void sunplus_uart_console_putchar(struct uart_port *port,
  401. unsigned char ch)
  402. {
  403. wait_for_xmitr(port);
  404. sp_uart_put_char(port, ch);
  405. }
  406. static void sunplus_console_write(struct console *co,
  407. const char *s,
  408. unsigned int count)
  409. {
  410. unsigned long flags;
  411. int locked = 1;
  412. if (oops_in_progress)
  413. locked = uart_port_trylock_irqsave(&sunplus_console_ports[co->index]->port, &flags);
  414. else
  415. uart_port_lock_irqsave(&sunplus_console_ports[co->index]->port, &flags);
  416. uart_console_write(&sunplus_console_ports[co->index]->port, s, count,
  417. sunplus_uart_console_putchar);
  418. if (locked)
  419. uart_port_unlock_irqrestore(&sunplus_console_ports[co->index]->port, flags);
  420. }
  421. static int __init sunplus_console_setup(struct console *co, char *options)
  422. {
  423. struct sunplus_uart_port *sup;
  424. int baud = 115200;
  425. int bits = 8;
  426. int parity = 'n';
  427. int flow = 'n';
  428. if (co->index < 0 || co->index >= SUP_UART_NR)
  429. return -EINVAL;
  430. sup = sunplus_console_ports[co->index];
  431. if (!sup)
  432. return -ENODEV;
  433. if (options)
  434. uart_parse_options(options, &baud, &parity, &bits, &flow);
  435. return uart_set_options(&sup->port, co, baud, parity, bits, flow);
  436. }
  437. static struct uart_driver sunplus_uart_driver;
  438. static struct console sunplus_uart_console = {
  439. .name = "ttySUP",
  440. .write = sunplus_console_write,
  441. .device = uart_console_device,
  442. .setup = sunplus_console_setup,
  443. .flags = CON_PRINTBUFFER,
  444. .index = -1,
  445. .data = &sunplus_uart_driver
  446. };
  447. #define SERIAL_SUNPLUS_CONSOLE (&sunplus_uart_console)
  448. #else
  449. #define SERIAL_SUNPLUS_CONSOLE NULL
  450. #endif
  451. static struct uart_driver sunplus_uart_driver = {
  452. .owner = THIS_MODULE,
  453. .driver_name = "sunplus_uart",
  454. .dev_name = "ttySUP",
  455. .major = TTY_MAJOR,
  456. .minor = 64,
  457. .nr = SUP_UART_NR,
  458. .cons = SERIAL_SUNPLUS_CONSOLE,
  459. };
  460. static void sunplus_uart_disable_unprepare(void *data)
  461. {
  462. clk_disable_unprepare(data);
  463. }
  464. static void sunplus_uart_reset_control_assert(void *data)
  465. {
  466. reset_control_assert(data);
  467. }
  468. static int sunplus_uart_probe(struct platform_device *pdev)
  469. {
  470. struct sunplus_uart_port *sup;
  471. struct uart_port *port;
  472. struct resource *res;
  473. int ret, irq;
  474. pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
  475. if (pdev->id < 0 || pdev->id >= SUP_UART_NR)
  476. return -EINVAL;
  477. sup = devm_kzalloc(&pdev->dev, sizeof(*sup), GFP_KERNEL);
  478. if (!sup)
  479. return -ENOMEM;
  480. sup->clk = devm_clk_get_optional(&pdev->dev, NULL);
  481. if (IS_ERR(sup->clk))
  482. return dev_err_probe(&pdev->dev, PTR_ERR(sup->clk), "clk not found\n");
  483. ret = clk_prepare_enable(sup->clk);
  484. if (ret)
  485. return ret;
  486. ret = devm_add_action_or_reset(&pdev->dev, sunplus_uart_disable_unprepare, sup->clk);
  487. if (ret)
  488. return ret;
  489. sup->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  490. if (IS_ERR(sup->rstc))
  491. return dev_err_probe(&pdev->dev, PTR_ERR(sup->rstc), "rstc not found\n");
  492. port = &sup->port;
  493. port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  494. if (IS_ERR(port->membase))
  495. return dev_err_probe(&pdev->dev, PTR_ERR(port->membase), "membase not found\n");
  496. irq = platform_get_irq(pdev, 0);
  497. if (irq < 0)
  498. return irq;
  499. port->mapbase = res->start;
  500. port->uartclk = clk_get_rate(sup->clk);
  501. port->line = pdev->id;
  502. port->irq = irq;
  503. port->dev = &pdev->dev;
  504. port->iotype = UPIO_MEM;
  505. port->ops = &sunplus_uart_ops;
  506. port->flags = UPF_BOOT_AUTOCONF;
  507. port->fifosize = 128;
  508. ret = reset_control_deassert(sup->rstc);
  509. if (ret)
  510. return ret;
  511. ret = devm_add_action_or_reset(&pdev->dev, sunplus_uart_reset_control_assert, sup->rstc);
  512. if (ret)
  513. return ret;
  514. #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
  515. sunplus_console_ports[sup->port.line] = sup;
  516. #endif
  517. platform_set_drvdata(pdev, &sup->port);
  518. ret = uart_add_one_port(&sunplus_uart_driver, &sup->port);
  519. #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
  520. if (ret)
  521. sunplus_console_ports[sup->port.line] = NULL;
  522. #endif
  523. return ret;
  524. }
  525. static void sunplus_uart_remove(struct platform_device *pdev)
  526. {
  527. struct sunplus_uart_port *sup = platform_get_drvdata(pdev);
  528. uart_remove_one_port(&sunplus_uart_driver, &sup->port);
  529. }
  530. static int __maybe_unused sunplus_uart_suspend(struct device *dev)
  531. {
  532. struct sunplus_uart_port *sup = dev_get_drvdata(dev);
  533. if (!uart_console(&sup->port))
  534. uart_suspend_port(&sunplus_uart_driver, &sup->port);
  535. return 0;
  536. }
  537. static int __maybe_unused sunplus_uart_resume(struct device *dev)
  538. {
  539. struct sunplus_uart_port *sup = dev_get_drvdata(dev);
  540. if (!uart_console(&sup->port))
  541. uart_resume_port(&sunplus_uart_driver, &sup->port);
  542. return 0;
  543. }
  544. static const struct dev_pm_ops sunplus_uart_pm_ops = {
  545. SET_SYSTEM_SLEEP_PM_OPS(sunplus_uart_suspend, sunplus_uart_resume)
  546. };
  547. static const struct of_device_id sp_uart_of_match[] = {
  548. { .compatible = "sunplus,sp7021-uart" },
  549. {}
  550. };
  551. MODULE_DEVICE_TABLE(of, sp_uart_of_match);
  552. static struct platform_driver sunplus_uart_platform_driver = {
  553. .probe = sunplus_uart_probe,
  554. .remove = sunplus_uart_remove,
  555. .driver = {
  556. .name = "sunplus_uart",
  557. .of_match_table = sp_uart_of_match,
  558. .pm = &sunplus_uart_pm_ops,
  559. }
  560. };
  561. static int __init sunplus_uart_init(void)
  562. {
  563. int ret;
  564. ret = uart_register_driver(&sunplus_uart_driver);
  565. if (ret)
  566. return ret;
  567. ret = platform_driver_register(&sunplus_uart_platform_driver);
  568. if (ret)
  569. uart_unregister_driver(&sunplus_uart_driver);
  570. return ret;
  571. }
  572. module_init(sunplus_uart_init);
  573. static void __exit sunplus_uart_exit(void)
  574. {
  575. platform_driver_unregister(&sunplus_uart_platform_driver);
  576. uart_unregister_driver(&sunplus_uart_driver);
  577. }
  578. module_exit(sunplus_uart_exit);
  579. #ifdef CONFIG_SERIAL_EARLYCON
  580. static void sunplus_uart_putc(struct uart_port *port, unsigned char c)
  581. {
  582. unsigned int val;
  583. int ret;
  584. ret = readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,
  585. (val & UART_LSR_TEMT), 1, 10000);
  586. if (ret)
  587. return;
  588. writel(c, port->membase + SUP_UART_DATA);
  589. }
  590. static void sunplus_uart_early_write(struct console *con, const char *s, unsigned int n)
  591. {
  592. struct earlycon_device *dev = con->data;
  593. uart_console_write(&dev->port, s, n, sunplus_uart_putc);
  594. }
  595. static int __init
  596. sunplus_uart_early_setup(struct earlycon_device *dev, const char *opt)
  597. {
  598. if (!(dev->port.membase || dev->port.iobase))
  599. return -ENODEV;
  600. dev->con->write = sunplus_uart_early_write;
  601. return 0;
  602. }
  603. OF_EARLYCON_DECLARE(sunplus_uart, "sunplus,sp7021-uart", sunplus_uart_early_setup);
  604. #endif
  605. MODULE_DESCRIPTION("Sunplus UART driver");
  606. MODULE_AUTHOR("Hammer Hsieh <hammerh0314@gmail.com>");
  607. MODULE_LICENSE("GPL v2");