clps711x.c 13 KB

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