clps711x.c 13 KB

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