clps711x.c 13 KB

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