vt8500_serial.c 19 KB

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