vt8500_serial.c 19 KB

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