clps711x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Driver for CLPS711x serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. *
  6. * Copyright 1999 ARM Limited
  7. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  24. #define SUPPORT_SYSRQ
  25. #endif
  26. #include <linux/module.h>
  27. #include <linux/ioport.h>
  28. #include <linux/init.h>
  29. #include <linux/console.h>
  30. #include <linux/sysrq.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/device.h>
  33. #include <linux/tty.h>
  34. #include <linux/tty_flip.h>
  35. #include <linux/serial_core.h>
  36. #include <linux/serial.h>
  37. #include <linux/io.h>
  38. #include <linux/platform_device.h>
  39. #include <mach/hardware.h>
  40. #include <asm/irq.h>
  41. #define UART_CLPS711X_NAME "uart-clps711x"
  42. #define UART_NR 2
  43. #define SERIAL_CLPS711X_MAJOR 204
  44. #define SERIAL_CLPS711X_MINOR 40
  45. #define SERIAL_CLPS711X_NR UART_NR
  46. /*
  47. * We use the relevant SYSCON register as a base address for these ports.
  48. */
  49. #define UBRLCR(port) ((port)->iobase + UBRLCR1 - SYSCON1)
  50. #define UARTDR(port) ((port)->iobase + UARTDR1 - SYSCON1)
  51. #define SYSFLG(port) ((port)->iobase + SYSFLG1 - SYSCON1)
  52. #define SYSCON(port) ((port)->iobase + SYSCON1 - SYSCON1)
  53. #define TX_IRQ(port) ((port)->irq)
  54. #define RX_IRQ(port) ((port)->irq + 1)
  55. #define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)
  56. #define tx_enabled(port) ((port)->unused[0])
  57. static void clps711xuart_stop_tx(struct uart_port *port)
  58. {
  59. if (tx_enabled(port)) {
  60. disable_irq(TX_IRQ(port));
  61. tx_enabled(port) = 0;
  62. }
  63. }
  64. static void clps711xuart_start_tx(struct uart_port *port)
  65. {
  66. if (!tx_enabled(port)) {
  67. enable_irq(TX_IRQ(port));
  68. tx_enabled(port) = 1;
  69. }
  70. }
  71. static void clps711xuart_stop_rx(struct uart_port *port)
  72. {
  73. disable_irq(RX_IRQ(port));
  74. }
  75. static void clps711xuart_enable_ms(struct uart_port *port)
  76. {
  77. }
  78. static irqreturn_t clps711xuart_int_rx(int irq, void *dev_id)
  79. {
  80. struct uart_port *port = dev_id;
  81. struct tty_struct *tty = port->state->port.tty;
  82. unsigned int status, ch, flg;
  83. status = clps_readl(SYSFLG(port));
  84. while (!(status & SYSFLG_URXFE)) {
  85. ch = clps_readl(UARTDR(port));
  86. port->icount.rx++;
  87. flg = TTY_NORMAL;
  88. /*
  89. * Note that the error handling code is
  90. * out of the main execution path
  91. */
  92. if (unlikely(ch & UART_ANY_ERR)) {
  93. if (ch & UARTDR_PARERR)
  94. port->icount.parity++;
  95. else if (ch & UARTDR_FRMERR)
  96. port->icount.frame++;
  97. if (ch & UARTDR_OVERR)
  98. port->icount.overrun++;
  99. ch &= port->read_status_mask;
  100. if (ch & UARTDR_PARERR)
  101. flg = TTY_PARITY;
  102. else if (ch & UARTDR_FRMERR)
  103. flg = TTY_FRAME;
  104. #ifdef SUPPORT_SYSRQ
  105. port->sysrq = 0;
  106. #endif
  107. }
  108. if (uart_handle_sysrq_char(port, ch))
  109. goto ignore_char;
  110. /*
  111. * CHECK: does overrun affect the current character?
  112. * ASSUMPTION: it does not.
  113. */
  114. uart_insert_char(port, ch, UARTDR_OVERR, ch, flg);
  115. ignore_char:
  116. status = clps_readl(SYSFLG(port));
  117. }
  118. tty_flip_buffer_push(tty);
  119. return IRQ_HANDLED;
  120. }
  121. static irqreturn_t clps711xuart_int_tx(int irq, void *dev_id)
  122. {
  123. struct uart_port *port = dev_id;
  124. struct circ_buf *xmit = &port->state->xmit;
  125. int count;
  126. if (port->x_char) {
  127. clps_writel(port->x_char, UARTDR(port));
  128. port->icount.tx++;
  129. port->x_char = 0;
  130. return IRQ_HANDLED;
  131. }
  132. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  133. goto disable_tx_irq;
  134. count = port->fifosize >> 1;
  135. do {
  136. clps_writel(xmit->buf[xmit->tail], UARTDR(port));
  137. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  138. port->icount.tx++;
  139. if (uart_circ_empty(xmit))
  140. break;
  141. } while (--count > 0);
  142. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  143. uart_write_wakeup(port);
  144. if (uart_circ_empty(xmit)) {
  145. disable_tx_irq:
  146. disable_irq_nosync(TX_IRQ(port));
  147. tx_enabled(port) = 0;
  148. }
  149. return IRQ_HANDLED;
  150. }
  151. static unsigned int clps711xuart_tx_empty(struct uart_port *port)
  152. {
  153. unsigned int status = clps_readl(SYSFLG(port));
  154. return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
  155. }
  156. static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
  157. {
  158. unsigned int port_addr;
  159. unsigned int result = 0;
  160. unsigned int status;
  161. port_addr = SYSFLG(port);
  162. if (port_addr == SYSFLG1) {
  163. status = clps_readl(SYSFLG1);
  164. if (status & SYSFLG1_DCD)
  165. result |= TIOCM_CAR;
  166. if (status & SYSFLG1_DSR)
  167. result |= TIOCM_DSR;
  168. if (status & SYSFLG1_CTS)
  169. result |= TIOCM_CTS;
  170. }
  171. return result;
  172. }
  173. static void
  174. clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
  175. {
  176. }
  177. static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
  178. {
  179. unsigned long flags;
  180. unsigned int ubrlcr;
  181. spin_lock_irqsave(&port->lock, flags);
  182. ubrlcr = clps_readl(UBRLCR(port));
  183. if (break_state == -1)
  184. ubrlcr |= UBRLCR_BREAK;
  185. else
  186. ubrlcr &= ~UBRLCR_BREAK;
  187. clps_writel(ubrlcr, UBRLCR(port));
  188. spin_unlock_irqrestore(&port->lock, flags);
  189. }
  190. static int clps711xuart_startup(struct uart_port *port)
  191. {
  192. unsigned int syscon;
  193. int retval;
  194. tx_enabled(port) = 1;
  195. /*
  196. * Allocate the IRQs
  197. */
  198. retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0,
  199. "clps711xuart_tx", port);
  200. if (retval)
  201. return retval;
  202. retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0,
  203. "clps711xuart_rx", port);
  204. if (retval) {
  205. free_irq(TX_IRQ(port), port);
  206. return retval;
  207. }
  208. /*
  209. * enable the port
  210. */
  211. syscon = clps_readl(SYSCON(port));
  212. syscon |= SYSCON_UARTEN;
  213. clps_writel(syscon, SYSCON(port));
  214. return 0;
  215. }
  216. static void clps711xuart_shutdown(struct uart_port *port)
  217. {
  218. unsigned int ubrlcr, syscon;
  219. /*
  220. * Free the interrupt
  221. */
  222. free_irq(TX_IRQ(port), port); /* TX interrupt */
  223. free_irq(RX_IRQ(port), port); /* RX interrupt */
  224. /*
  225. * disable the port
  226. */
  227. syscon = clps_readl(SYSCON(port));
  228. syscon &= ~SYSCON_UARTEN;
  229. clps_writel(syscon, SYSCON(port));
  230. /*
  231. * disable break condition and fifos
  232. */
  233. ubrlcr = clps_readl(UBRLCR(port));
  234. ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
  235. clps_writel(ubrlcr, UBRLCR(port));
  236. }
  237. static void
  238. clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios,
  239. struct ktermios *old)
  240. {
  241. unsigned int ubrlcr, baud, quot;
  242. unsigned long flags;
  243. /*
  244. * We don't implement CREAD.
  245. */
  246. termios->c_cflag |= CREAD;
  247. /*
  248. * Ask the core to calculate the divisor for us.
  249. */
  250. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  251. quot = uart_get_divisor(port, baud);
  252. switch (termios->c_cflag & CSIZE) {
  253. case CS5:
  254. ubrlcr = UBRLCR_WRDLEN5;
  255. break;
  256. case CS6:
  257. ubrlcr = UBRLCR_WRDLEN6;
  258. break;
  259. case CS7:
  260. ubrlcr = UBRLCR_WRDLEN7;
  261. break;
  262. default: // CS8
  263. ubrlcr = UBRLCR_WRDLEN8;
  264. break;
  265. }
  266. if (termios->c_cflag & CSTOPB)
  267. ubrlcr |= UBRLCR_XSTOP;
  268. if (termios->c_cflag & PARENB) {
  269. ubrlcr |= UBRLCR_PRTEN;
  270. if (!(termios->c_cflag & PARODD))
  271. ubrlcr |= UBRLCR_EVENPRT;
  272. }
  273. if (port->fifosize > 1)
  274. ubrlcr |= UBRLCR_FIFOEN;
  275. spin_lock_irqsave(&port->lock, flags);
  276. /*
  277. * Update the per-port timeout.
  278. */
  279. uart_update_timeout(port, termios->c_cflag, baud);
  280. port->read_status_mask = UARTDR_OVERR;
  281. if (termios->c_iflag & INPCK)
  282. port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
  283. /*
  284. * Characters to ignore
  285. */
  286. port->ignore_status_mask = 0;
  287. if (termios->c_iflag & IGNPAR)
  288. port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
  289. if (termios->c_iflag & IGNBRK) {
  290. /*
  291. * If we're ignoring parity and break indicators,
  292. * ignore overruns to (for real raw support).
  293. */
  294. if (termios->c_iflag & IGNPAR)
  295. port->ignore_status_mask |= UARTDR_OVERR;
  296. }
  297. quot -= 1;
  298. clps_writel(ubrlcr | quot, UBRLCR(port));
  299. spin_unlock_irqrestore(&port->lock, flags);
  300. }
  301. static const char *clps711xuart_type(struct uart_port *port)
  302. {
  303. return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
  304. }
  305. /*
  306. * Configure/autoconfigure the port.
  307. */
  308. static void clps711xuart_config_port(struct uart_port *port, int flags)
  309. {
  310. if (flags & UART_CONFIG_TYPE)
  311. port->type = PORT_CLPS711X;
  312. }
  313. static void clps711xuart_release_port(struct uart_port *port)
  314. {
  315. }
  316. static int clps711xuart_request_port(struct uart_port *port)
  317. {
  318. return 0;
  319. }
  320. static struct uart_ops clps711x_pops = {
  321. .tx_empty = clps711xuart_tx_empty,
  322. .set_mctrl = clps711xuart_set_mctrl_null,
  323. .get_mctrl = clps711xuart_get_mctrl,
  324. .stop_tx = clps711xuart_stop_tx,
  325. .start_tx = clps711xuart_start_tx,
  326. .stop_rx = clps711xuart_stop_rx,
  327. .enable_ms = clps711xuart_enable_ms,
  328. .break_ctl = clps711xuart_break_ctl,
  329. .startup = clps711xuart_startup,
  330. .shutdown = clps711xuart_shutdown,
  331. .set_termios = clps711xuart_set_termios,
  332. .type = clps711xuart_type,
  333. .config_port = clps711xuart_config_port,
  334. .release_port = clps711xuart_release_port,
  335. .request_port = clps711xuart_request_port,
  336. };
  337. static struct uart_port clps711x_ports[UART_NR] = {
  338. {
  339. .iobase = SYSCON1,
  340. .irq = IRQ_UTXINT1, /* IRQ_URXINT1, IRQ_UMSINT */
  341. .uartclk = 3686400,
  342. .fifosize = 16,
  343. .ops = &clps711x_pops,
  344. .line = 0,
  345. .flags = UPF_BOOT_AUTOCONF,
  346. },
  347. {
  348. .iobase = SYSCON2,
  349. .irq = IRQ_UTXINT2, /* IRQ_URXINT2 */
  350. .uartclk = 3686400,
  351. .fifosize = 16,
  352. .ops = &clps711x_pops,
  353. .line = 1,
  354. .flags = UPF_BOOT_AUTOCONF,
  355. }
  356. };
  357. #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
  358. static void clps711xuart_console_putchar(struct uart_port *port, int ch)
  359. {
  360. while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
  361. barrier();
  362. clps_writel(ch, UARTDR(port));
  363. }
  364. /*
  365. * Print a string to the serial port trying not to disturb
  366. * any possible real use of the port...
  367. *
  368. * The console_lock must be held when we get here.
  369. *
  370. * Note that this is called with interrupts already disabled
  371. */
  372. static void
  373. clps711xuart_console_write(struct console *co, const char *s,
  374. unsigned int count)
  375. {
  376. struct uart_port *port = clps711x_ports + co->index;
  377. unsigned int status, syscon;
  378. /*
  379. * Ensure that the port is enabled.
  380. */
  381. syscon = clps_readl(SYSCON(port));
  382. clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
  383. uart_console_write(port, s, count, clps711xuart_console_putchar);
  384. /*
  385. * Finally, wait for transmitter to become empty
  386. * and restore the uart state.
  387. */
  388. do {
  389. status = clps_readl(SYSFLG(port));
  390. } while (status & SYSFLG_UBUSY);
  391. clps_writel(syscon, SYSCON(port));
  392. }
  393. static void __init
  394. clps711xuart_console_get_options(struct uart_port *port, int *baud,
  395. int *parity, int *bits)
  396. {
  397. if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
  398. unsigned int ubrlcr, quot;
  399. ubrlcr = clps_readl(UBRLCR(port));
  400. *parity = 'n';
  401. if (ubrlcr & UBRLCR_PRTEN) {
  402. if (ubrlcr & UBRLCR_EVENPRT)
  403. *parity = 'e';
  404. else
  405. *parity = 'o';
  406. }
  407. if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
  408. *bits = 7;
  409. else
  410. *bits = 8;
  411. quot = ubrlcr & UBRLCR_BAUD_MASK;
  412. *baud = port->uartclk / (16 * (quot + 1));
  413. }
  414. }
  415. static int __init clps711xuart_console_setup(struct console *co, char *options)
  416. {
  417. struct uart_port *port;
  418. int baud = 38400;
  419. int bits = 8;
  420. int parity = 'n';
  421. int flow = 'n';
  422. /*
  423. * Check whether an invalid uart number has been specified, and
  424. * if so, search for the first available port that does have
  425. * console support.
  426. */
  427. port = uart_get_console(clps711x_ports, UART_NR, co);
  428. if (options)
  429. uart_parse_options(options, &baud, &parity, &bits, &flow);
  430. else
  431. clps711xuart_console_get_options(port, &baud, &parity, &bits);
  432. return uart_set_options(port, co, baud, parity, bits, flow);
  433. }
  434. static struct uart_driver clps711x_reg;
  435. static struct console clps711x_console = {
  436. .name = "ttyCL",
  437. .write = clps711xuart_console_write,
  438. .device = uart_console_device,
  439. .setup = clps711xuart_console_setup,
  440. .flags = CON_PRINTBUFFER,
  441. .index = -1,
  442. .data = &clps711x_reg,
  443. };
  444. static int __init clps711xuart_console_init(void)
  445. {
  446. register_console(&clps711x_console);
  447. return 0;
  448. }
  449. console_initcall(clps711xuart_console_init);
  450. #define CLPS711X_CONSOLE &clps711x_console
  451. #else
  452. #define CLPS711X_CONSOLE NULL
  453. #endif
  454. static struct uart_driver clps711x_reg = {
  455. .driver_name = "ttyCL",
  456. .dev_name = "ttyCL",
  457. .major = SERIAL_CLPS711X_MAJOR,
  458. .minor = SERIAL_CLPS711X_MINOR,
  459. .nr = UART_NR,
  460. .cons = CLPS711X_CONSOLE,
  461. };
  462. static int __devinit uart_clps711x_probe(struct platform_device *pdev)
  463. {
  464. int ret, i;
  465. printk(KERN_INFO "Serial: CLPS711x driver\n");
  466. ret = uart_register_driver(&clps711x_reg);
  467. if (ret)
  468. return ret;
  469. for (i = 0; i < UART_NR; i++)
  470. uart_add_one_port(&clps711x_reg, &clps711x_ports[i]);
  471. return 0;
  472. }
  473. static int __devexit uart_clps711x_remove(struct platform_device *pdev)
  474. {
  475. int i;
  476. for (i = 0; i < UART_NR; i++)
  477. uart_remove_one_port(&clps711x_reg, &clps711x_ports[i]);
  478. uart_unregister_driver(&clps711x_reg);
  479. return 0;
  480. }
  481. static struct platform_driver clps711x_uart_driver = {
  482. .driver = {
  483. .name = UART_CLPS711X_NAME,
  484. .owner = THIS_MODULE,
  485. },
  486. .probe = uart_clps711x_probe,
  487. .remove = __devexit_p(uart_clps711x_remove),
  488. };
  489. module_platform_driver(clps711x_uart_driver);
  490. static struct platform_device clps711x_uart_device = {
  491. .name = UART_CLPS711X_NAME,
  492. };
  493. static int __init uart_clps711x_init(void)
  494. {
  495. return platform_device_register(&clps711x_uart_device);
  496. }
  497. module_init(uart_clps711x_init);
  498. static void __exit uart_clps711x_exit(void)
  499. {
  500. platform_device_unregister(&clps711x_uart_device);
  501. }
  502. module_exit(uart_clps711x_exit);
  503. MODULE_AUTHOR("Deep Blue Solutions Ltd");
  504. MODULE_DESCRIPTION("CLPS711X serial driver");
  505. MODULE_LICENSE("GPL");