clps711x.c 13 KB

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