vt8500_serial.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  4. *
  5. * Based on msm_serial.c, which is:
  6. * Copyright (C) 2007 Google, Inc.
  7. * Author: Robert Love <rlove@google.com>
  8. */
  9. #if defined(CONFIG_SERIAL_VT8500_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  10. # define SUPPORT_SYSRQ
  11. #endif
  12. #include <linux/hrtimer.h>
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/irq.h>
  17. #include <linux/init.h>
  18. #include <linux/console.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/serial.h>
  23. #include <linux/slab.h>
  24. #include <linux/clk.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/err.h>
  28. /*
  29. * UART Register offsets
  30. */
  31. #define VT8500_URTDR 0x0000 /* Transmit data */
  32. #define VT8500_URRDR 0x0004 /* Receive data */
  33. #define VT8500_URDIV 0x0008 /* Clock/Baud rate divisor */
  34. #define VT8500_URLCR 0x000C /* Line control */
  35. #define VT8500_URICR 0x0010 /* IrDA control */
  36. #define VT8500_URIER 0x0014 /* Interrupt enable */
  37. #define VT8500_URISR 0x0018 /* Interrupt status */
  38. #define VT8500_URUSR 0x001c /* UART status */
  39. #define VT8500_URFCR 0x0020 /* FIFO control */
  40. #define VT8500_URFIDX 0x0024 /* FIFO index */
  41. #define VT8500_URBKR 0x0028 /* Break signal count */
  42. #define VT8500_URTOD 0x002c /* Time out divisor */
  43. #define VT8500_TXFIFO 0x1000 /* Transmit FIFO (16x8) */
  44. #define VT8500_RXFIFO 0x1020 /* Receive FIFO (16x10) */
  45. /*
  46. * Interrupt enable and status bits
  47. */
  48. #define TXDE (1 << 0) /* Tx Data empty */
  49. #define RXDF (1 << 1) /* Rx Data full */
  50. #define TXFAE (1 << 2) /* Tx FIFO almost empty */
  51. #define TXFE (1 << 3) /* Tx FIFO empty */
  52. #define RXFAF (1 << 4) /* Rx FIFO almost full */
  53. #define RXFF (1 << 5) /* Rx FIFO full */
  54. #define TXUDR (1 << 6) /* Tx underrun */
  55. #define RXOVER (1 << 7) /* Rx overrun */
  56. #define PER (1 << 8) /* Parity error */
  57. #define FER (1 << 9) /* Frame error */
  58. #define TCTS (1 << 10) /* Toggle of CTS */
  59. #define RXTOUT (1 << 11) /* Rx timeout */
  60. #define BKDONE (1 << 12) /* Break signal done */
  61. #define ERR (1 << 13) /* AHB error response */
  62. #define RX_FIFO_INTS (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
  63. #define TX_FIFO_INTS (TXFAE | TXFE | TXUDR)
  64. /*
  65. * Line control bits
  66. */
  67. #define VT8500_TXEN (1 << 0) /* Enable transmit logic */
  68. #define VT8500_RXEN (1 << 1) /* Enable receive logic */
  69. #define VT8500_CS8 (1 << 2) /* 8-bit data length (vs. 7-bit) */
  70. #define VT8500_CSTOPB (1 << 3) /* 2 stop bits (vs. 1) */
  71. #define VT8500_PARENB (1 << 4) /* Enable parity */
  72. #define VT8500_PARODD (1 << 5) /* Odd parity (vs. even) */
  73. #define VT8500_RTS (1 << 6) /* Ready to send */
  74. #define VT8500_LOOPBK (1 << 7) /* Enable internal loopback */
  75. #define VT8500_DMA (1 << 8) /* Enable DMA mode (needs FIFO) */
  76. #define VT8500_BREAK (1 << 9) /* Initiate break signal */
  77. #define VT8500_PSLVERR (1 << 10) /* APB error upon empty RX FIFO read */
  78. #define VT8500_SWRTSCTS (1 << 11) /* Software-controlled RTS/CTS */
  79. /*
  80. * Capability flags (driver-internal)
  81. */
  82. #define VT8500_HAS_SWRTSCTS_SWITCH (1 << 1)
  83. #define VT8500_RECOMMENDED_CLK 12000000
  84. #define VT8500_OVERSAMPLING_DIVISOR 13
  85. #define VT8500_MAX_PORTS 6
  86. struct vt8500_port {
  87. struct uart_port uart;
  88. char name[16];
  89. struct clk *clk;
  90. unsigned int clk_predivisor;
  91. unsigned int ier;
  92. unsigned int vt8500_uart_flags;
  93. };
  94. /*
  95. * we use this variable to keep track of which ports
  96. * have been allocated as we can't use pdev->id in
  97. * devicetree
  98. */
  99. static DECLARE_BITMAP(vt8500_ports_in_use, VT8500_MAX_PORTS);
  100. static inline void vt8500_write(struct uart_port *port, unsigned int val,
  101. unsigned int off)
  102. {
  103. writel(val, port->membase + off);
  104. }
  105. static inline unsigned int vt8500_read(struct uart_port *port, unsigned int off)
  106. {
  107. return readl(port->membase + off);
  108. }
  109. static void vt8500_stop_tx(struct uart_port *port)
  110. {
  111. struct vt8500_port *vt8500_port = container_of(port,
  112. struct vt8500_port,
  113. uart);
  114. vt8500_port->ier &= ~TX_FIFO_INTS;
  115. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  116. }
  117. static void vt8500_stop_rx(struct uart_port *port)
  118. {
  119. struct vt8500_port *vt8500_port = container_of(port,
  120. struct vt8500_port,
  121. uart);
  122. vt8500_port->ier &= ~RX_FIFO_INTS;
  123. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  124. }
  125. static void vt8500_enable_ms(struct uart_port *port)
  126. {
  127. struct vt8500_port *vt8500_port = container_of(port,
  128. struct vt8500_port,
  129. uart);
  130. vt8500_port->ier |= TCTS;
  131. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  132. }
  133. static void handle_rx(struct uart_port *port)
  134. {
  135. struct tty_port *tport = &port->state->port;
  136. /*
  137. * Handle overrun
  138. */
  139. if ((vt8500_read(port, VT8500_URISR) & RXOVER)) {
  140. port->icount.overrun++;
  141. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  142. }
  143. /* and now the main RX loop */
  144. while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) {
  145. unsigned int c;
  146. char flag = TTY_NORMAL;
  147. c = readw(port->membase + VT8500_RXFIFO) & 0x3ff;
  148. /* Mask conditions we're ignorning. */
  149. c &= ~port->read_status_mask;
  150. if (c & FER) {
  151. port->icount.frame++;
  152. flag = TTY_FRAME;
  153. } else if (c & PER) {
  154. port->icount.parity++;
  155. flag = TTY_PARITY;
  156. }
  157. port->icount.rx++;
  158. if (!uart_handle_sysrq_char(port, c))
  159. tty_insert_flip_char(tport, c, flag);
  160. }
  161. spin_unlock(&port->lock);
  162. tty_flip_buffer_push(tport);
  163. spin_lock(&port->lock);
  164. }
  165. static void handle_tx(struct uart_port *port)
  166. {
  167. struct circ_buf *xmit = &port->state->xmit;
  168. if (port->x_char) {
  169. writeb(port->x_char, port->membase + VT8500_TXFIFO);
  170. port->icount.tx++;
  171. port->x_char = 0;
  172. }
  173. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  174. vt8500_stop_tx(port);
  175. return;
  176. }
  177. while ((vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16) {
  178. if (uart_circ_empty(xmit))
  179. break;
  180. writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO);
  181. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  182. port->icount.tx++;
  183. }
  184. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  185. uart_write_wakeup(port);
  186. if (uart_circ_empty(xmit))
  187. vt8500_stop_tx(port);
  188. }
  189. static void vt8500_start_tx(struct uart_port *port)
  190. {
  191. struct vt8500_port *vt8500_port = container_of(port,
  192. struct vt8500_port,
  193. uart);
  194. vt8500_port->ier &= ~TX_FIFO_INTS;
  195. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  196. handle_tx(port);
  197. vt8500_port->ier |= TX_FIFO_INTS;
  198. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  199. }
  200. static void handle_delta_cts(struct uart_port *port)
  201. {
  202. port->icount.cts++;
  203. wake_up_interruptible(&port->state->port.delta_msr_wait);
  204. }
  205. static irqreturn_t vt8500_irq(int irq, void *dev_id)
  206. {
  207. struct uart_port *port = dev_id;
  208. unsigned long isr;
  209. spin_lock(&port->lock);
  210. isr = vt8500_read(port, VT8500_URISR);
  211. /* Acknowledge active status bits */
  212. vt8500_write(port, isr, VT8500_URISR);
  213. if (isr & RX_FIFO_INTS)
  214. handle_rx(port);
  215. if (isr & TX_FIFO_INTS)
  216. handle_tx(port);
  217. if (isr & TCTS)
  218. handle_delta_cts(port);
  219. spin_unlock(&port->lock);
  220. return IRQ_HANDLED;
  221. }
  222. static unsigned int vt8500_tx_empty(struct uart_port *port)
  223. {
  224. return (vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16 ?
  225. TIOCSER_TEMT : 0;
  226. }
  227. static unsigned int vt8500_get_mctrl(struct uart_port *port)
  228. {
  229. unsigned int usr;
  230. usr = vt8500_read(port, VT8500_URUSR);
  231. if (usr & (1 << 4))
  232. return TIOCM_CTS;
  233. else
  234. return 0;
  235. }
  236. static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl)
  237. {
  238. unsigned int lcr = vt8500_read(port, VT8500_URLCR);
  239. if (mctrl & TIOCM_RTS)
  240. lcr |= VT8500_RTS;
  241. else
  242. lcr &= ~VT8500_RTS;
  243. vt8500_write(port, lcr, VT8500_URLCR);
  244. }
  245. static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
  246. {
  247. if (break_ctl)
  248. vt8500_write(port,
  249. vt8500_read(port, VT8500_URLCR) | VT8500_BREAK,
  250. VT8500_URLCR);
  251. }
  252. static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
  253. {
  254. struct vt8500_port *vt8500_port =
  255. container_of(port, struct vt8500_port, uart);
  256. unsigned long div;
  257. unsigned int loops = 1000;
  258. div = ((vt8500_port->clk_predivisor - 1) & 0xf) << 16;
  259. div |= (uart_get_divisor(port, baud) - 1) & 0x3ff;
  260. /* Effective baud rate */
  261. baud = port->uartclk / 16 / ((div & 0x3ff) + 1);
  262. while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
  263. cpu_relax();
  264. vt8500_write(port, div, VT8500_URDIV);
  265. /* Break signal timing depends on baud rate, update accordingly */
  266. vt8500_write(port, mult_frac(baud, 4096, 1000000), VT8500_URBKR);
  267. return baud;
  268. }
  269. static int vt8500_startup(struct uart_port *port)
  270. {
  271. struct vt8500_port *vt8500_port =
  272. container_of(port, struct vt8500_port, uart);
  273. int ret;
  274. snprintf(vt8500_port->name, sizeof(vt8500_port->name),
  275. "vt8500_serial%d", port->line);
  276. ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
  277. vt8500_port->name, port);
  278. if (unlikely(ret))
  279. return ret;
  280. vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
  281. return 0;
  282. }
  283. static void vt8500_shutdown(struct uart_port *port)
  284. {
  285. struct vt8500_port *vt8500_port =
  286. container_of(port, struct vt8500_port, uart);
  287. vt8500_port->ier = 0;
  288. /* disable interrupts and FIFOs */
  289. vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
  290. vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
  291. free_irq(port->irq, port);
  292. }
  293. static void vt8500_set_termios(struct uart_port *port,
  294. struct ktermios *termios,
  295. struct ktermios *old)
  296. {
  297. struct vt8500_port *vt8500_port =
  298. container_of(port, struct vt8500_port, uart);
  299. unsigned long flags;
  300. unsigned int baud, lcr;
  301. unsigned int loops = 1000;
  302. spin_lock_irqsave(&port->lock, flags);
  303. /* calculate and set baud rate */
  304. baud = uart_get_baud_rate(port, termios, old, 900, 921600);
  305. baud = vt8500_set_baud_rate(port, baud);
  306. if (tty_termios_baud_rate(termios))
  307. tty_termios_encode_baud_rate(termios, baud, baud);
  308. /* calculate parity */
  309. lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
  310. lcr &= ~(VT8500_PARENB | VT8500_PARODD);
  311. if (termios->c_cflag & PARENB) {
  312. lcr |= VT8500_PARENB;
  313. termios->c_cflag &= ~CMSPAR;
  314. if (termios->c_cflag & PARODD)
  315. lcr |= VT8500_PARODD;
  316. }
  317. /* calculate bits per char */
  318. lcr &= ~VT8500_CS8;
  319. switch (termios->c_cflag & CSIZE) {
  320. case CS7:
  321. break;
  322. case CS8:
  323. default:
  324. lcr |= VT8500_CS8;
  325. termios->c_cflag &= ~CSIZE;
  326. termios->c_cflag |= CS8;
  327. break;
  328. }
  329. /* calculate stop bits */
  330. lcr &= ~VT8500_CSTOPB;
  331. if (termios->c_cflag & CSTOPB)
  332. lcr |= VT8500_CSTOPB;
  333. lcr &= ~VT8500_SWRTSCTS;
  334. if (vt8500_port->vt8500_uart_flags & VT8500_HAS_SWRTSCTS_SWITCH)
  335. lcr |= VT8500_SWRTSCTS;
  336. /* set parity, bits per char, and stop bit */
  337. vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
  338. /* Configure status bits to ignore based on termio flags. */
  339. port->read_status_mask = 0;
  340. if (termios->c_iflag & IGNPAR)
  341. port->read_status_mask = FER | PER;
  342. uart_update_timeout(port, termios->c_cflag, baud);
  343. /* Reset FIFOs */
  344. vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
  345. while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
  346. && --loops)
  347. cpu_relax();
  348. /* Every possible FIFO-related interrupt */
  349. vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
  350. /*
  351. * CTS flow control
  352. */
  353. if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
  354. vt8500_port->ier |= TCTS;
  355. vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
  356. vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
  357. spin_unlock_irqrestore(&port->lock, flags);
  358. }
  359. static const char *vt8500_type(struct uart_port *port)
  360. {
  361. struct vt8500_port *vt8500_port =
  362. container_of(port, struct vt8500_port, uart);
  363. return vt8500_port->name;
  364. }
  365. static void vt8500_release_port(struct uart_port *port)
  366. {
  367. }
  368. static int vt8500_request_port(struct uart_port *port)
  369. {
  370. return 0;
  371. }
  372. static void vt8500_config_port(struct uart_port *port, int flags)
  373. {
  374. port->type = PORT_VT8500;
  375. }
  376. static int vt8500_verify_port(struct uart_port *port,
  377. struct serial_struct *ser)
  378. {
  379. if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
  380. return -EINVAL;
  381. if (unlikely(port->irq != ser->irq))
  382. return -EINVAL;
  383. return 0;
  384. }
  385. static struct vt8500_port *vt8500_uart_ports[VT8500_MAX_PORTS];
  386. static struct uart_driver vt8500_uart_driver;
  387. #ifdef CONFIG_SERIAL_VT8500_CONSOLE
  388. static void wait_for_xmitr(struct uart_port *port)
  389. {
  390. unsigned int status, tmout = 10000;
  391. /* Wait up to 10ms for the character(s) to be sent. */
  392. do {
  393. status = vt8500_read(port, VT8500_URFIDX);
  394. if (--tmout == 0)
  395. break;
  396. udelay(1);
  397. } while (status & 0x10);
  398. }
  399. static void vt8500_console_putchar(struct uart_port *port, int c)
  400. {
  401. wait_for_xmitr(port);
  402. writeb(c, port->membase + VT8500_TXFIFO);
  403. }
  404. static void vt8500_console_write(struct console *co, const char *s,
  405. unsigned int count)
  406. {
  407. struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
  408. unsigned long ier;
  409. BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
  410. ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
  411. vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
  412. uart_console_write(&vt8500_port->uart, s, count,
  413. vt8500_console_putchar);
  414. /*
  415. * Finally, wait for transmitter to become empty
  416. * and switch back to FIFO
  417. */
  418. wait_for_xmitr(&vt8500_port->uart);
  419. vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
  420. }
  421. static int __init vt8500_console_setup(struct console *co, char *options)
  422. {
  423. struct vt8500_port *vt8500_port;
  424. int baud = 9600;
  425. int bits = 8;
  426. int parity = 'n';
  427. int flow = 'n';
  428. if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
  429. return -ENXIO;
  430. vt8500_port = vt8500_uart_ports[co->index];
  431. if (!vt8500_port)
  432. return -ENODEV;
  433. if (options)
  434. uart_parse_options(options, &baud, &parity, &bits, &flow);
  435. return uart_set_options(&vt8500_port->uart,
  436. co, baud, parity, bits, flow);
  437. }
  438. static struct console vt8500_console = {
  439. .name = "ttyWMT",
  440. .write = vt8500_console_write,
  441. .device = uart_console_device,
  442. .setup = vt8500_console_setup,
  443. .flags = CON_PRINTBUFFER,
  444. .index = -1,
  445. .data = &vt8500_uart_driver,
  446. };
  447. #define VT8500_CONSOLE (&vt8500_console)
  448. #else
  449. #define VT8500_CONSOLE NULL
  450. #endif
  451. #ifdef CONFIG_CONSOLE_POLL
  452. static int vt8500_get_poll_char(struct uart_port *port)
  453. {
  454. unsigned int status = vt8500_read(port, VT8500_URFIDX);
  455. if (!(status & 0x1f00))
  456. return NO_POLL_CHAR;
  457. return vt8500_read(port, VT8500_RXFIFO) & 0xff;
  458. }
  459. static void vt8500_put_poll_char(struct uart_port *port, unsigned char c)
  460. {
  461. unsigned int status, tmout = 10000;
  462. do {
  463. status = vt8500_read(port, VT8500_URFIDX);
  464. if (--tmout == 0)
  465. break;
  466. udelay(1);
  467. } while (status & 0x10);
  468. vt8500_write(port, c, VT8500_TXFIFO);
  469. }
  470. #endif
  471. static const struct uart_ops vt8500_uart_pops = {
  472. .tx_empty = vt8500_tx_empty,
  473. .set_mctrl = vt8500_set_mctrl,
  474. .get_mctrl = vt8500_get_mctrl,
  475. .stop_tx = vt8500_stop_tx,
  476. .start_tx = vt8500_start_tx,
  477. .stop_rx = vt8500_stop_rx,
  478. .enable_ms = vt8500_enable_ms,
  479. .break_ctl = vt8500_break_ctl,
  480. .startup = vt8500_startup,
  481. .shutdown = vt8500_shutdown,
  482. .set_termios = vt8500_set_termios,
  483. .type = vt8500_type,
  484. .release_port = vt8500_release_port,
  485. .request_port = vt8500_request_port,
  486. .config_port = vt8500_config_port,
  487. .verify_port = vt8500_verify_port,
  488. #ifdef CONFIG_CONSOLE_POLL
  489. .poll_get_char = vt8500_get_poll_char,
  490. .poll_put_char = vt8500_put_poll_char,
  491. #endif
  492. };
  493. static struct uart_driver vt8500_uart_driver = {
  494. .owner = THIS_MODULE,
  495. .driver_name = "vt8500_serial",
  496. .dev_name = "ttyWMT",
  497. .nr = 6,
  498. .cons = VT8500_CONSOLE,
  499. };
  500. static unsigned int vt8500_flags; /* none required so far */
  501. static unsigned int wm8880_flags = VT8500_HAS_SWRTSCTS_SWITCH;
  502. static const struct of_device_id wmt_dt_ids[] = {
  503. { .compatible = "via,vt8500-uart", .data = &vt8500_flags},
  504. { .compatible = "wm,wm8880-uart", .data = &wm8880_flags},
  505. {}
  506. };
  507. static int vt8500_serial_probe(struct platform_device *pdev)
  508. {
  509. struct vt8500_port *vt8500_port;
  510. struct resource *mmres, *irqres;
  511. struct device_node *np = pdev->dev.of_node;
  512. const struct of_device_id *match;
  513. const unsigned int *flags;
  514. int ret;
  515. int port;
  516. match = of_match_device(wmt_dt_ids, &pdev->dev);
  517. if (!match)
  518. return -EINVAL;
  519. flags = match->data;
  520. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  521. irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  522. if (!mmres || !irqres)
  523. return -ENODEV;
  524. if (np) {
  525. port = of_alias_get_id(np, "serial");
  526. if (port >= VT8500_MAX_PORTS)
  527. port = -1;
  528. } else {
  529. port = -1;
  530. }
  531. if (port < 0) {
  532. /* calculate the port id */
  533. port = find_first_zero_bit(vt8500_ports_in_use,
  534. VT8500_MAX_PORTS);
  535. }
  536. if (port >= VT8500_MAX_PORTS)
  537. return -ENODEV;
  538. /* reserve the port id */
  539. if (test_and_set_bit(port, vt8500_ports_in_use)) {
  540. /* port already in use - shouldn't really happen */
  541. return -EBUSY;
  542. }
  543. vt8500_port = devm_kzalloc(&pdev->dev, sizeof(struct vt8500_port),
  544. GFP_KERNEL);
  545. if (!vt8500_port)
  546. return -ENOMEM;
  547. vt8500_port->uart.membase = devm_ioremap_resource(&pdev->dev, mmres);
  548. if (IS_ERR(vt8500_port->uart.membase))
  549. return PTR_ERR(vt8500_port->uart.membase);
  550. vt8500_port->clk = of_clk_get(pdev->dev.of_node, 0);
  551. if (IS_ERR(vt8500_port->clk)) {
  552. dev_err(&pdev->dev, "failed to get clock\n");
  553. return -EINVAL;
  554. }
  555. ret = clk_prepare_enable(vt8500_port->clk);
  556. if (ret) {
  557. dev_err(&pdev->dev, "failed to enable clock\n");
  558. return ret;
  559. }
  560. vt8500_port->vt8500_uart_flags = *flags;
  561. vt8500_port->clk_predivisor = DIV_ROUND_CLOSEST(
  562. clk_get_rate(vt8500_port->clk),
  563. VT8500_RECOMMENDED_CLK
  564. );
  565. vt8500_port->uart.type = PORT_VT8500;
  566. vt8500_port->uart.iotype = UPIO_MEM;
  567. vt8500_port->uart.mapbase = mmres->start;
  568. vt8500_port->uart.irq = irqres->start;
  569. vt8500_port->uart.fifosize = 16;
  570. vt8500_port->uart.ops = &vt8500_uart_pops;
  571. vt8500_port->uart.line = port;
  572. vt8500_port->uart.dev = &pdev->dev;
  573. vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
  574. /* Serial core uses the magic "16" everywhere - adjust for it */
  575. vt8500_port->uart.uartclk = 16 * clk_get_rate(vt8500_port->clk) /
  576. vt8500_port->clk_predivisor /
  577. VT8500_OVERSAMPLING_DIVISOR;
  578. snprintf(vt8500_port->name, sizeof(vt8500_port->name),
  579. "VT8500 UART%d", pdev->id);
  580. vt8500_uart_ports[port] = vt8500_port;
  581. uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
  582. platform_set_drvdata(pdev, vt8500_port);
  583. return 0;
  584. }
  585. static struct platform_driver vt8500_platform_driver = {
  586. .probe = vt8500_serial_probe,
  587. .driver = {
  588. .name = "vt8500_serial",
  589. .of_match_table = wmt_dt_ids,
  590. .suppress_bind_attrs = true,
  591. },
  592. };
  593. static int __init vt8500_serial_init(void)
  594. {
  595. int ret;
  596. ret = uart_register_driver(&vt8500_uart_driver);
  597. if (unlikely(ret))
  598. return ret;
  599. ret = platform_driver_register(&vt8500_platform_driver);
  600. if (unlikely(ret))
  601. uart_unregister_driver(&vt8500_uart_driver);
  602. return ret;
  603. }
  604. device_initcall(vt8500_serial_init);