clps711x.c 14 KB

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