clps711x.c 14 KB

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