clps711x.c 13 KB

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