clps711x.c 13 KB

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