clps711x.c 13 KB

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