vt8500_serial.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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/platform_device.h>
  35. /*
  36. * UART Register offsets
  37. */
  38. #define VT8500_URTDR 0x0000 /* Transmit data */
  39. #define VT8500_URRDR 0x0004 /* Receive data */
  40. #define VT8500_URDIV 0x0008 /* Clock/Baud rate divisor */
  41. #define VT8500_URLCR 0x000C /* Line control */
  42. #define VT8500_URICR 0x0010 /* IrDA control */
  43. #define VT8500_URIER 0x0014 /* Interrupt enable */
  44. #define VT8500_URISR 0x0018 /* Interrupt status */
  45. #define VT8500_URUSR 0x001c /* UART status */
  46. #define VT8500_URFCR 0x0020 /* FIFO control */
  47. #define VT8500_URFIDX 0x0024 /* FIFO index */
  48. #define VT8500_URBKR 0x0028 /* Break signal count */
  49. #define VT8500_URTOD 0x002c /* Time out divisor */
  50. #define VT8500_TXFIFO 0x1000 /* Transmit FIFO (16x8) */
  51. #define VT8500_RXFIFO 0x1020 /* Receive FIFO (16x10) */
  52. /*
  53. * Interrupt enable and status bits
  54. */
  55. #define TXDE (1 << 0) /* Tx Data empty */
  56. #define RXDF (1 << 1) /* Rx Data full */
  57. #define TXFAE (1 << 2) /* Tx FIFO almost empty */
  58. #define TXFE (1 << 3) /* Tx FIFO empty */
  59. #define RXFAF (1 << 4) /* Rx FIFO almost full */
  60. #define RXFF (1 << 5) /* Rx FIFO full */
  61. #define TXUDR (1 << 6) /* Tx underrun */
  62. #define RXOVER (1 << 7) /* Rx overrun */
  63. #define PER (1 << 8) /* Parity error */
  64. #define FER (1 << 9) /* Frame error */
  65. #define TCTS (1 << 10) /* Toggle of CTS */
  66. #define RXTOUT (1 << 11) /* Rx timeout */
  67. #define BKDONE (1 << 12) /* Break signal done */
  68. #define ERR (1 << 13) /* AHB error response */
  69. #define RX_FIFO_INTS (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
  70. #define TX_FIFO_INTS (TXFAE | TXFE | TXUDR)
  71. struct vt8500_port {
  72. struct uart_port uart;
  73. char name[16];
  74. struct clk *clk;
  75. unsigned int ier;
  76. };
  77. static inline void vt8500_write(struct uart_port *port, unsigned int val,
  78. unsigned int off)
  79. {
  80. writel(val, port->membase + off);
  81. }
  82. static inline unsigned int vt8500_read(struct uart_port *port, unsigned int off)
  83. {
  84. return readl(port->membase + off);
  85. }
  86. static void vt8500_stop_tx(struct uart_port *port)
  87. {
  88. struct vt8500_port *vt8500_port = container_of(port,
  89. struct vt8500_port,
  90. uart);
  91. vt8500_port->ier &= ~TX_FIFO_INTS;
  92. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  93. }
  94. static void vt8500_stop_rx(struct uart_port *port)
  95. {
  96. struct vt8500_port *vt8500_port = container_of(port,
  97. struct vt8500_port,
  98. uart);
  99. vt8500_port->ier &= ~RX_FIFO_INTS;
  100. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  101. }
  102. static void vt8500_enable_ms(struct uart_port *port)
  103. {
  104. struct vt8500_port *vt8500_port = container_of(port,
  105. struct vt8500_port,
  106. uart);
  107. vt8500_port->ier |= TCTS;
  108. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  109. }
  110. static void handle_rx(struct uart_port *port)
  111. {
  112. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  113. if (!tty) {
  114. /* Discard data: no tty available */
  115. int count = (vt8500_read(port, VT8500_URFIDX) & 0x1f00) >> 8;
  116. u16 ch;
  117. while (count--)
  118. ch = readw(port->membase + VT8500_RXFIFO);
  119. return;
  120. }
  121. /*
  122. * Handle overrun
  123. */
  124. if ((vt8500_read(port, VT8500_URISR) & RXOVER)) {
  125. port->icount.overrun++;
  126. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  127. }
  128. /* and now the main RX loop */
  129. while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) {
  130. unsigned int c;
  131. char flag = TTY_NORMAL;
  132. c = readw(port->membase + VT8500_RXFIFO) & 0x3ff;
  133. /* Mask conditions we're ignorning. */
  134. c &= ~port->read_status_mask;
  135. if (c & FER) {
  136. port->icount.frame++;
  137. flag = TTY_FRAME;
  138. } else if (c & PER) {
  139. port->icount.parity++;
  140. flag = TTY_PARITY;
  141. }
  142. port->icount.rx++;
  143. if (!uart_handle_sysrq_char(port, c))
  144. tty_insert_flip_char(tty, c, flag);
  145. }
  146. tty_flip_buffer_push(tty);
  147. tty_kref_put(tty);
  148. }
  149. static void handle_tx(struct uart_port *port)
  150. {
  151. struct circ_buf *xmit = &port->state->xmit;
  152. if (port->x_char) {
  153. writeb(port->x_char, port->membase + VT8500_TXFIFO);
  154. port->icount.tx++;
  155. port->x_char = 0;
  156. }
  157. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  158. vt8500_stop_tx(port);
  159. return;
  160. }
  161. while ((vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16) {
  162. if (uart_circ_empty(xmit))
  163. break;
  164. writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO);
  165. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  166. port->icount.tx++;
  167. }
  168. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  169. uart_write_wakeup(port);
  170. if (uart_circ_empty(xmit))
  171. vt8500_stop_tx(port);
  172. }
  173. static void vt8500_start_tx(struct uart_port *port)
  174. {
  175. struct vt8500_port *vt8500_port = container_of(port,
  176. struct vt8500_port,
  177. uart);
  178. vt8500_port->ier &= ~TX_FIFO_INTS;
  179. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  180. handle_tx(port);
  181. vt8500_port->ier |= TX_FIFO_INTS;
  182. vt8500_write(port, vt8500_port->ier, VT8500_URIER);
  183. }
  184. static void handle_delta_cts(struct uart_port *port)
  185. {
  186. port->icount.cts++;
  187. wake_up_interruptible(&port->state->port.delta_msr_wait);
  188. }
  189. static irqreturn_t vt8500_irq(int irq, void *dev_id)
  190. {
  191. struct uart_port *port = dev_id;
  192. unsigned long isr;
  193. spin_lock(&port->lock);
  194. isr = vt8500_read(port, VT8500_URISR);
  195. /* Acknowledge active status bits */
  196. vt8500_write(port, isr, VT8500_URISR);
  197. if (isr & RX_FIFO_INTS)
  198. handle_rx(port);
  199. if (isr & TX_FIFO_INTS)
  200. handle_tx(port);
  201. if (isr & TCTS)
  202. handle_delta_cts(port);
  203. spin_unlock(&port->lock);
  204. return IRQ_HANDLED;
  205. }
  206. static unsigned int vt8500_tx_empty(struct uart_port *port)
  207. {
  208. return (vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16 ?
  209. TIOCSER_TEMT : 0;
  210. }
  211. static unsigned int vt8500_get_mctrl(struct uart_port *port)
  212. {
  213. unsigned int usr;
  214. usr = vt8500_read(port, VT8500_URUSR);
  215. if (usr & (1 << 4))
  216. return TIOCM_CTS;
  217. else
  218. return 0;
  219. }
  220. static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl)
  221. {
  222. }
  223. static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
  224. {
  225. if (break_ctl)
  226. vt8500_write(port, vt8500_read(port, VT8500_URLCR) | (1 << 9),
  227. VT8500_URLCR);
  228. }
  229. static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
  230. {
  231. unsigned long div;
  232. unsigned int loops = 1000;
  233. div = vt8500_read(port, VT8500_URDIV) & ~(0x3ff);
  234. if (unlikely((baud < 900) || (baud > 921600)))
  235. div |= 7;
  236. else
  237. div |= (921600 / baud) - 1;
  238. while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
  239. cpu_relax();
  240. vt8500_write(port, div, VT8500_URDIV);
  241. return baud;
  242. }
  243. static int vt8500_startup(struct uart_port *port)
  244. {
  245. struct vt8500_port *vt8500_port =
  246. container_of(port, struct vt8500_port, uart);
  247. int ret;
  248. snprintf(vt8500_port->name, sizeof(vt8500_port->name),
  249. "vt8500_serial%d", port->line);
  250. ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
  251. vt8500_port->name, port);
  252. if (unlikely(ret))
  253. return ret;
  254. vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
  255. return 0;
  256. }
  257. static void vt8500_shutdown(struct uart_port *port)
  258. {
  259. struct vt8500_port *vt8500_port =
  260. container_of(port, struct vt8500_port, uart);
  261. vt8500_port->ier = 0;
  262. /* disable interrupts and FIFOs */
  263. vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
  264. vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
  265. free_irq(port->irq, port);
  266. }
  267. static void vt8500_set_termios(struct uart_port *port,
  268. struct ktermios *termios,
  269. struct ktermios *old)
  270. {
  271. struct vt8500_port *vt8500_port =
  272. container_of(port, struct vt8500_port, uart);
  273. unsigned long flags;
  274. unsigned int baud, lcr;
  275. unsigned int loops = 1000;
  276. spin_lock_irqsave(&port->lock, flags);
  277. /* calculate and set baud rate */
  278. baud = uart_get_baud_rate(port, termios, old, 900, 921600);
  279. baud = vt8500_set_baud_rate(port, baud);
  280. if (tty_termios_baud_rate(termios))
  281. tty_termios_encode_baud_rate(termios, baud, baud);
  282. /* calculate parity */
  283. lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
  284. lcr &= ~((1 << 5) | (1 << 4));
  285. if (termios->c_cflag & PARENB) {
  286. lcr |= (1 << 4);
  287. termios->c_cflag &= ~CMSPAR;
  288. if (termios->c_cflag & PARODD)
  289. lcr |= (1 << 5);
  290. }
  291. /* calculate bits per char */
  292. lcr &= ~(1 << 2);
  293. switch (termios->c_cflag & CSIZE) {
  294. case CS7:
  295. break;
  296. case CS8:
  297. default:
  298. lcr |= (1 << 2);
  299. termios->c_cflag &= ~CSIZE;
  300. termios->c_cflag |= CS8;
  301. break;
  302. }
  303. /* calculate stop bits */
  304. lcr &= ~(1 << 3);
  305. if (termios->c_cflag & CSTOPB)
  306. lcr |= (1 << 3);
  307. /* set parity, bits per char, and stop bit */
  308. vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
  309. /* Configure status bits to ignore based on termio flags. */
  310. port->read_status_mask = 0;
  311. if (termios->c_iflag & IGNPAR)
  312. port->read_status_mask = FER | PER;
  313. uart_update_timeout(port, termios->c_cflag, baud);
  314. /* Reset FIFOs */
  315. vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
  316. while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
  317. && --loops)
  318. cpu_relax();
  319. /* Every possible FIFO-related interrupt */
  320. vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
  321. /*
  322. * CTS flow control
  323. */
  324. if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
  325. vt8500_port->ier |= TCTS;
  326. vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
  327. vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
  328. spin_unlock_irqrestore(&port->lock, flags);
  329. }
  330. static const char *vt8500_type(struct uart_port *port)
  331. {
  332. struct vt8500_port *vt8500_port =
  333. container_of(port, struct vt8500_port, uart);
  334. return vt8500_port->name;
  335. }
  336. static void vt8500_release_port(struct uart_port *port)
  337. {
  338. }
  339. static int vt8500_request_port(struct uart_port *port)
  340. {
  341. return 0;
  342. }
  343. static void vt8500_config_port(struct uart_port *port, int flags)
  344. {
  345. port->type = PORT_VT8500;
  346. }
  347. static int vt8500_verify_port(struct uart_port *port,
  348. struct serial_struct *ser)
  349. {
  350. if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
  351. return -EINVAL;
  352. if (unlikely(port->irq != ser->irq))
  353. return -EINVAL;
  354. return 0;
  355. }
  356. static struct vt8500_port *vt8500_uart_ports[4];
  357. static struct uart_driver vt8500_uart_driver;
  358. #ifdef CONFIG_SERIAL_VT8500_CONSOLE
  359. static inline void wait_for_xmitr(struct uart_port *port)
  360. {
  361. unsigned int status, tmout = 10000;
  362. /* Wait up to 10ms for the character(s) to be sent. */
  363. do {
  364. status = vt8500_read(port, VT8500_URFIDX);
  365. if (--tmout == 0)
  366. break;
  367. udelay(1);
  368. } while (status & 0x10);
  369. }
  370. static void vt8500_console_putchar(struct uart_port *port, int c)
  371. {
  372. wait_for_xmitr(port);
  373. writeb(c, port->membase + VT8500_TXFIFO);
  374. }
  375. static void vt8500_console_write(struct console *co, const char *s,
  376. unsigned int count)
  377. {
  378. struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
  379. unsigned long ier;
  380. BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
  381. ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
  382. vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
  383. uart_console_write(&vt8500_port->uart, s, count,
  384. vt8500_console_putchar);
  385. /*
  386. * Finally, wait for transmitter to become empty
  387. * and switch back to FIFO
  388. */
  389. wait_for_xmitr(&vt8500_port->uart);
  390. vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
  391. }
  392. static int __init vt8500_console_setup(struct console *co, char *options)
  393. {
  394. struct vt8500_port *vt8500_port;
  395. int baud = 9600;
  396. int bits = 8;
  397. int parity = 'n';
  398. int flow = 'n';
  399. if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
  400. return -ENXIO;
  401. vt8500_port = vt8500_uart_ports[co->index];
  402. if (!vt8500_port)
  403. return -ENODEV;
  404. if (options)
  405. uart_parse_options(options, &baud, &parity, &bits, &flow);
  406. return uart_set_options(&vt8500_port->uart,
  407. co, baud, parity, bits, flow);
  408. }
  409. static struct console vt8500_console = {
  410. .name = "ttyWMT",
  411. .write = vt8500_console_write,
  412. .device = uart_console_device,
  413. .setup = vt8500_console_setup,
  414. .flags = CON_PRINTBUFFER,
  415. .index = -1,
  416. .data = &vt8500_uart_driver,
  417. };
  418. #define VT8500_CONSOLE (&vt8500_console)
  419. #else
  420. #define VT8500_CONSOLE NULL
  421. #endif
  422. static struct uart_ops vt8500_uart_pops = {
  423. .tx_empty = vt8500_tx_empty,
  424. .set_mctrl = vt8500_set_mctrl,
  425. .get_mctrl = vt8500_get_mctrl,
  426. .stop_tx = vt8500_stop_tx,
  427. .start_tx = vt8500_start_tx,
  428. .stop_rx = vt8500_stop_rx,
  429. .enable_ms = vt8500_enable_ms,
  430. .break_ctl = vt8500_break_ctl,
  431. .startup = vt8500_startup,
  432. .shutdown = vt8500_shutdown,
  433. .set_termios = vt8500_set_termios,
  434. .type = vt8500_type,
  435. .release_port = vt8500_release_port,
  436. .request_port = vt8500_request_port,
  437. .config_port = vt8500_config_port,
  438. .verify_port = vt8500_verify_port,
  439. };
  440. static struct uart_driver vt8500_uart_driver = {
  441. .owner = THIS_MODULE,
  442. .driver_name = "vt8500_serial",
  443. .dev_name = "ttyWMT",
  444. .nr = 6,
  445. .cons = VT8500_CONSOLE,
  446. };
  447. static int __init vt8500_serial_probe(struct platform_device *pdev)
  448. {
  449. struct vt8500_port *vt8500_port;
  450. struct resource *mmres, *irqres;
  451. int ret;
  452. mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  453. irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  454. if (!mmres || !irqres)
  455. return -ENODEV;
  456. vt8500_port = kzalloc(sizeof(struct vt8500_port), GFP_KERNEL);
  457. if (!vt8500_port)
  458. return -ENOMEM;
  459. vt8500_port->uart.type = PORT_VT8500;
  460. vt8500_port->uart.iotype = UPIO_MEM;
  461. vt8500_port->uart.mapbase = mmres->start;
  462. vt8500_port->uart.irq = irqres->start;
  463. vt8500_port->uart.fifosize = 16;
  464. vt8500_port->uart.ops = &vt8500_uart_pops;
  465. vt8500_port->uart.line = pdev->id;
  466. vt8500_port->uart.dev = &pdev->dev;
  467. vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
  468. vt8500_port->uart.uartclk = 24000000;
  469. snprintf(vt8500_port->name, sizeof(vt8500_port->name),
  470. "VT8500 UART%d", pdev->id);
  471. vt8500_port->uart.membase = ioremap(mmres->start,
  472. mmres->end - mmres->start + 1);
  473. if (!vt8500_port->uart.membase) {
  474. ret = -ENOMEM;
  475. goto err;
  476. }
  477. vt8500_uart_ports[pdev->id] = vt8500_port;
  478. uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
  479. platform_set_drvdata(pdev, vt8500_port);
  480. return 0;
  481. err:
  482. kfree(vt8500_port);
  483. return ret;
  484. }
  485. static int __devexit vt8500_serial_remove(struct platform_device *pdev)
  486. {
  487. struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
  488. platform_set_drvdata(pdev, NULL);
  489. uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
  490. kfree(vt8500_port);
  491. return 0;
  492. }
  493. static struct platform_driver vt8500_platform_driver = {
  494. .probe = vt8500_serial_probe,
  495. .remove = vt8500_serial_remove,
  496. .driver = {
  497. .name = "vt8500_serial",
  498. .owner = THIS_MODULE,
  499. },
  500. };
  501. static int __init vt8500_serial_init(void)
  502. {
  503. int ret;
  504. ret = uart_register_driver(&vt8500_uart_driver);
  505. if (unlikely(ret))
  506. return ret;
  507. ret = platform_driver_register(&vt8500_platform_driver);
  508. if (unlikely(ret))
  509. uart_unregister_driver(&vt8500_uart_driver);
  510. return ret;
  511. }
  512. static void __exit vt8500_serial_exit(void)
  513. {
  514. #ifdef CONFIG_SERIAL_VT8500_CONSOLE
  515. unregister_console(&vt8500_console);
  516. #endif
  517. platform_driver_unregister(&vt8500_platform_driver);
  518. uart_unregister_driver(&vt8500_uart_driver);
  519. }
  520. module_init(vt8500_serial_init);
  521. module_exit(vt8500_serial_exit);
  522. MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
  523. MODULE_DESCRIPTION("Driver for vt8500 serial device");
  524. MODULE_LICENSE("GPL");