serial_ks8695.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * drivers/serial/serial_ks8695.c
  3. *
  4. * Driver for KS8695 serial ports
  5. *
  6. * Based on drivers/serial/serial_amba.c, by Kam Lee.
  7. *
  8. * Copyright 2002-2005 Micrel Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/tty.h>
  18. #include <linux/ioport.h>
  19. #include <linux/init.h>
  20. #include <linux/serial.h>
  21. #include <linux/console.h>
  22. #include <linux/sysrq.h>
  23. #include <linux/device.h>
  24. #include <asm/io.h>
  25. #include <asm/irq.h>
  26. #include <asm/mach/irq.h>
  27. #include <asm/arch/regs-uart.h>
  28. #include <asm/arch/regs-irq.h>
  29. #if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  30. #define SUPPORT_SYSRQ
  31. #endif
  32. #include <linux/serial_core.h>
  33. #define SERIAL_KS8695_MAJOR 204
  34. #define SERIAL_KS8695_MINOR 16
  35. #define SERIAL_KS8695_DEVNAME "ttyAM"
  36. #define SERIAL_KS8695_NR 1
  37. /*
  38. * Access macros for the KS8695 UART
  39. */
  40. #define UART_GET_CHAR(p) (__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
  41. #define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + KS8695_URTH)
  42. #define UART_GET_FCR(p) __raw_readl((p)->membase + KS8695_URFC)
  43. #define UART_PUT_FCR(p, c) __raw_writel((c), (p)->membase + KS8695_URFC)
  44. #define UART_GET_MSR(p) __raw_readl((p)->membase + KS8695_URMS)
  45. #define UART_GET_LSR(p) __raw_readl((p)->membase + KS8695_URLS)
  46. #define UART_GET_LCR(p) __raw_readl((p)->membase + KS8695_URLC)
  47. #define UART_PUT_LCR(p, c) __raw_writel((c), (p)->membase + KS8695_URLC)
  48. #define UART_GET_MCR(p) __raw_readl((p)->membase + KS8695_URMC)
  49. #define UART_PUT_MCR(p, c) __raw_writel((c), (p)->membase + KS8695_URMC)
  50. #define UART_GET_BRDR(p) __raw_readl((p)->membase + KS8695_URBD)
  51. #define UART_PUT_BRDR(p, c) __raw_writel((c), (p)->membase + KS8695_URBD)
  52. #define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
  53. #define UART_DUMMY_LSR_RX 0x100
  54. #define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
  55. #define tx_enabled(port) ((port)->unused[0])
  56. #define rx_enabled(port) ((port)->unused[1])
  57. #ifdef SUPPORT_SYSRQ
  58. static struct console ks8695_console;
  59. #endif
  60. static void ks8695uart_stop_tx(struct uart_port *port)
  61. {
  62. if (tx_enabled(port)) {
  63. disable_irq(KS8695_IRQ_UART_TX);
  64. tx_enabled(port) = 0;
  65. }
  66. }
  67. static void ks8695uart_start_tx(struct uart_port *port)
  68. {
  69. if (!tx_enabled(port)) {
  70. enable_irq(KS8695_IRQ_UART_TX);
  71. tx_enabled(port) = 1;
  72. }
  73. }
  74. static void ks8695uart_stop_rx(struct uart_port *port)
  75. {
  76. if (rx_enabled(port)) {
  77. disable_irq(KS8695_IRQ_UART_RX);
  78. rx_enabled(port) = 0;
  79. }
  80. }
  81. static void ks8695uart_enable_ms(struct uart_port *port)
  82. {
  83. enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
  84. }
  85. static void ks8695uart_disable_ms(struct uart_port *port)
  86. {
  87. disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
  88. }
  89. static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
  90. {
  91. struct uart_port *port = dev_id;
  92. struct tty_struct *tty = port->info->tty;
  93. unsigned int status, ch, lsr, flg, max_count = 256;
  94. status = UART_GET_LSR(port); /* clears pending LSR interrupts */
  95. while ((status & URLS_URDR) && max_count--) {
  96. ch = UART_GET_CHAR(port);
  97. flg = TTY_NORMAL;
  98. port->icount.rx++;
  99. /*
  100. * Note that the error handling code is
  101. * out of the main execution path
  102. */
  103. lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
  104. if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
  105. if (lsr & URLS_URBI) {
  106. lsr &= ~(URLS_URFE | URLS_URPE);
  107. port->icount.brk++;
  108. if (uart_handle_break(port))
  109. goto ignore_char;
  110. }
  111. if (lsr & URLS_URPE)
  112. port->icount.parity++;
  113. if (lsr & URLS_URFE)
  114. port->icount.frame++;
  115. if (lsr & URLS_URROE)
  116. port->icount.overrun++;
  117. lsr &= port->read_status_mask;
  118. if (lsr & URLS_URBI)
  119. flg = TTY_BREAK;
  120. else if (lsr & URLS_URPE)
  121. flg = TTY_PARITY;
  122. else if (lsr & URLS_URFE)
  123. flg = TTY_FRAME;
  124. }
  125. if (uart_handle_sysrq_char(port, ch))
  126. goto ignore_char;
  127. uart_insert_char(port, lsr, URLS_URROE, ch, flg);
  128. ignore_char:
  129. status = UART_GET_LSR(port);
  130. }
  131. tty_flip_buffer_push(tty);
  132. return IRQ_HANDLED;
  133. }
  134. static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
  135. {
  136. struct uart_port *port = dev_id;
  137. struct circ_buf *xmit = &port->info->xmit;
  138. unsigned int count;
  139. if (port->x_char) {
  140. KS8695_CLR_TX_INT();
  141. UART_PUT_CHAR(port, port->x_char);
  142. port->icount.tx++;
  143. port->x_char = 0;
  144. return IRQ_HANDLED;
  145. }
  146. if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
  147. ks8695uart_stop_tx(port);
  148. return IRQ_HANDLED;
  149. }
  150. count = 16; /* fifo size */
  151. while (!uart_circ_empty(xmit) && (count-- > 0)) {
  152. KS8695_CLR_TX_INT();
  153. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  154. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  155. port->icount.tx++;
  156. }
  157. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  158. uart_write_wakeup(port);
  159. if (uart_circ_empty(xmit))
  160. ks8695uart_stop_tx(port);
  161. return IRQ_HANDLED;
  162. }
  163. static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
  164. {
  165. struct uart_port *port = dev_id;
  166. unsigned int status;
  167. /*
  168. * clear modem interrupt by reading MSR
  169. */
  170. status = UART_GET_MSR(port);
  171. if (status & URMS_URDDCD)
  172. uart_handle_dcd_change(port, status & URMS_URDDCD);
  173. if (status & URMS_URDDST)
  174. port->icount.dsr++;
  175. if (status & URMS_URDCTS)
  176. uart_handle_cts_change(port, status & URMS_URDCTS);
  177. if (status & URMS_URTERI)
  178. port->icount.rng++;
  179. wake_up_interruptible(&port->info->delta_msr_wait);
  180. return IRQ_HANDLED;
  181. }
  182. static unsigned int ks8695uart_tx_empty(struct uart_port *port)
  183. {
  184. return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
  185. }
  186. static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
  187. {
  188. unsigned int result = 0;
  189. unsigned int status;
  190. status = UART_GET_MSR(port);
  191. if (status & URMS_URDCD)
  192. result |= TIOCM_CAR;
  193. if (status & URMS_URDSR)
  194. result |= TIOCM_DSR;
  195. if (status & URMS_URCTS)
  196. result |= TIOCM_CTS;
  197. if (status & URMS_URRI)
  198. result |= TIOCM_RI;
  199. return result;
  200. }
  201. static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
  202. {
  203. unsigned int mcr;
  204. mcr = UART_GET_MCR(port);
  205. if (mctrl & TIOCM_RTS)
  206. mcr |= URMC_URRTS;
  207. else
  208. mcr &= ~URMC_URRTS;
  209. if (mctrl & TIOCM_DTR)
  210. mcr |= URMC_URDTR;
  211. else
  212. mcr &= ~URMC_URDTR;
  213. UART_PUT_MCR(port, mcr);
  214. }
  215. static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
  216. {
  217. unsigned int lcr;
  218. lcr = UART_GET_LCR(port);
  219. if (break_state == -1)
  220. lcr |= URLC_URSBC;
  221. else
  222. lcr &= ~URLC_URSBC;
  223. UART_PUT_LCR(port, lcr);
  224. }
  225. static int ks8695uart_startup(struct uart_port *port)
  226. {
  227. int retval;
  228. set_irq_flags(KS8695_IRQ_UART_TX, IRQF_VALID | IRQF_NOAUTOEN);
  229. tx_enabled(port) = 0;
  230. rx_enabled(port) = 1;
  231. /*
  232. * Allocate the IRQ
  233. */
  234. retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, IRQF_DISABLED, "UART TX", port);
  235. if (retval)
  236. goto err_tx;
  237. retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, IRQF_DISABLED, "UART RX", port);
  238. if (retval)
  239. goto err_rx;
  240. retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, IRQF_DISABLED, "UART LineStatus", port);
  241. if (retval)
  242. goto err_ls;
  243. retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, IRQF_DISABLED, "UART ModemStatus", port);
  244. if (retval)
  245. goto err_ms;
  246. return 0;
  247. err_ms:
  248. free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
  249. err_ls:
  250. free_irq(KS8695_IRQ_UART_RX, port);
  251. err_rx:
  252. free_irq(KS8695_IRQ_UART_TX, port);
  253. err_tx:
  254. return retval;
  255. }
  256. static void ks8695uart_shutdown(struct uart_port *port)
  257. {
  258. /*
  259. * Free the interrupt
  260. */
  261. free_irq(KS8695_IRQ_UART_RX, port);
  262. free_irq(KS8695_IRQ_UART_TX, port);
  263. free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
  264. free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
  265. /* disable break condition and fifos */
  266. UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
  267. UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
  268. }
  269. static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
  270. {
  271. unsigned int lcr, fcr = 0;
  272. unsigned long flags;
  273. unsigned int baud, quot;
  274. /*
  275. * Ask the core to calculate the divisor for us.
  276. */
  277. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  278. quot = uart_get_divisor(port, baud);
  279. switch (termios->c_cflag & CSIZE) {
  280. case CS5:
  281. lcr = URCL_5;
  282. break;
  283. case CS6:
  284. lcr = URCL_6;
  285. break;
  286. case CS7:
  287. lcr = URCL_7;
  288. break;
  289. default:
  290. lcr = URCL_8;
  291. break;
  292. }
  293. /* stop bits */
  294. if (termios->c_cflag & CSTOPB)
  295. lcr |= URLC_URSB;
  296. /* parity */
  297. if (termios->c_cflag & PARENB) {
  298. if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
  299. if (termios->c_cflag & PARODD)
  300. lcr |= URPE_MARK;
  301. else
  302. lcr |= URPE_SPACE;
  303. }
  304. else if (termios->c_cflag & PARODD)
  305. lcr |= URPE_ODD;
  306. else
  307. lcr |= URPE_EVEN;
  308. }
  309. if (port->fifosize > 1)
  310. fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
  311. spin_lock_irqsave(&port->lock, flags);
  312. /*
  313. * Update the per-port timeout.
  314. */
  315. uart_update_timeout(port, termios->c_cflag, baud);
  316. port->read_status_mask = URLS_URROE;
  317. if (termios->c_iflag & INPCK)
  318. port->read_status_mask |= (URLS_URFE | URLS_URPE);
  319. if (termios->c_iflag & (BRKINT | PARMRK))
  320. port->read_status_mask |= URLS_URBI;
  321. /*
  322. * Characters to ignore
  323. */
  324. port->ignore_status_mask = 0;
  325. if (termios->c_iflag & IGNPAR)
  326. port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
  327. if (termios->c_iflag & IGNBRK) {
  328. port->ignore_status_mask |= URLS_URBI;
  329. /*
  330. * If we're ignoring parity and break indicators,
  331. * ignore overruns too (for real raw support).
  332. */
  333. if (termios->c_iflag & IGNPAR)
  334. port->ignore_status_mask |= URLS_URROE;
  335. }
  336. /*
  337. * Ignore all characters if CREAD is not set.
  338. */
  339. if ((termios->c_cflag & CREAD) == 0)
  340. port->ignore_status_mask |= UART_DUMMY_LSR_RX;
  341. /* first, disable everything */
  342. if (UART_ENABLE_MS(port, termios->c_cflag))
  343. ks8695uart_enable_ms(port);
  344. else
  345. ks8695uart_disable_ms(port);
  346. /* Set baud rate */
  347. UART_PUT_BRDR(port, quot);
  348. UART_PUT_LCR(port, lcr);
  349. UART_PUT_FCR(port, fcr);
  350. spin_unlock_irqrestore(&port->lock, flags);
  351. }
  352. static const char *ks8695uart_type(struct uart_port *port)
  353. {
  354. return port->type == PORT_KS8695 ? "KS8695" : NULL;
  355. }
  356. /*
  357. * Release the memory region(s) being used by 'port'
  358. */
  359. static void ks8695uart_release_port(struct uart_port *port)
  360. {
  361. release_mem_region(port->mapbase, UART_PORT_SIZE);
  362. }
  363. /*
  364. * Request the memory region(s) being used by 'port'
  365. */
  366. static int ks8695uart_request_port(struct uart_port *port)
  367. {
  368. return request_mem_region(port->mapbase, UART_PORT_SIZE,
  369. "serial_ks8695") != NULL ? 0 : -EBUSY;
  370. }
  371. /*
  372. * Configure/autoconfigure the port.
  373. */
  374. static void ks8695uart_config_port(struct uart_port *port, int flags)
  375. {
  376. if (flags & UART_CONFIG_TYPE) {
  377. port->type = PORT_KS8695;
  378. ks8695uart_request_port(port);
  379. }
  380. }
  381. /*
  382. * verify the new serial_struct (for TIOCSSERIAL).
  383. */
  384. static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  385. {
  386. int ret = 0;
  387. if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
  388. ret = -EINVAL;
  389. if (ser->irq != port->irq)
  390. ret = -EINVAL;
  391. if (ser->baud_base < 9600)
  392. ret = -EINVAL;
  393. return ret;
  394. }
  395. static struct uart_ops ks8695uart_pops = {
  396. .tx_empty = ks8695uart_tx_empty,
  397. .set_mctrl = ks8695uart_set_mctrl,
  398. .get_mctrl = ks8695uart_get_mctrl,
  399. .stop_tx = ks8695uart_stop_tx,
  400. .start_tx = ks8695uart_start_tx,
  401. .stop_rx = ks8695uart_stop_rx,
  402. .enable_ms = ks8695uart_enable_ms,
  403. .break_ctl = ks8695uart_break_ctl,
  404. .startup = ks8695uart_startup,
  405. .shutdown = ks8695uart_shutdown,
  406. .set_termios = ks8695uart_set_termios,
  407. .type = ks8695uart_type,
  408. .release_port = ks8695uart_release_port,
  409. .request_port = ks8695uart_request_port,
  410. .config_port = ks8695uart_config_port,
  411. .verify_port = ks8695uart_verify_port,
  412. };
  413. static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
  414. {
  415. .membase = (void *) KS8695_UART_VA,
  416. .mapbase = KS8695_UART_VA,
  417. .iotype = SERIAL_IO_MEM,
  418. .irq = KS8695_IRQ_UART_TX,
  419. .uartclk = CLOCK_TICK_RATE * 16,
  420. .fifosize = 16,
  421. .ops = &ks8695uart_pops,
  422. .flags = ASYNC_BOOT_AUTOCONF,
  423. .line = 0,
  424. }
  425. };
  426. #ifdef CONFIG_SERIAL_KS8695_CONSOLE
  427. static void ks8695_console_putchar(struct uart_port *port, int ch)
  428. {
  429. while (!(UART_GET_LSR(port) & URLS_URTHRE))
  430. barrier();
  431. UART_PUT_CHAR(port, ch);
  432. }
  433. static void ks8695_console_write(struct console *co, const char *s, u_int count)
  434. {
  435. struct uart_port *port = ks8695uart_ports + co->index;
  436. uart_console_write(port, s, count, ks8695_console_putchar);
  437. }
  438. static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
  439. {
  440. unsigned int lcr;
  441. lcr = UART_GET_LCR(port);
  442. switch (lcr & URLC_PARITY) {
  443. case URPE_ODD:
  444. *parity = 'o';
  445. break;
  446. case URPE_EVEN:
  447. *parity = 'e';
  448. break;
  449. default:
  450. *parity = 'n';
  451. }
  452. switch (lcr & URLC_URCL) {
  453. case URCL_5:
  454. *bits = 5;
  455. break;
  456. case URCL_6:
  457. *bits = 6;
  458. break;
  459. case URCL_7:
  460. *bits = 7;
  461. break;
  462. default:
  463. *bits = 8;
  464. }
  465. *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
  466. *baud /= 16;
  467. *baud &= 0xFFFFFFF0;
  468. }
  469. static int __init ks8695_console_setup(struct console *co, char *options)
  470. {
  471. struct uart_port *port;
  472. int baud = 115200;
  473. int bits = 8;
  474. int parity = 'n';
  475. int flow = 'n';
  476. /*
  477. * Check whether an invalid uart number has been specified, and
  478. * if so, search for the first available port that does have
  479. * console support.
  480. */
  481. port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
  482. if (options)
  483. uart_parse_options(options, &baud, &parity, &bits, &flow);
  484. else
  485. ks8695_console_get_options(port, &baud, &parity, &bits);
  486. return uart_set_options(port, co, baud, parity, bits, flow);
  487. }
  488. static struct uart_driver ks8695_reg;
  489. static struct console ks8695_console = {
  490. .name = SERIAL_KS8695_DEVNAME,
  491. .write = ks8695_console_write,
  492. .device = uart_console_device,
  493. .setup = ks8695_console_setup,
  494. .flags = CON_PRINTBUFFER,
  495. .index = -1,
  496. .data = &ks8695_reg,
  497. };
  498. static int __init ks8695_console_init(void)
  499. {
  500. register_console(&ks8695_console);
  501. return 0;
  502. }
  503. console_initcall(ks8695_console_init);
  504. #define KS8695_CONSOLE &ks8695_console
  505. #else
  506. #define KS8695_CONSOLE NULL
  507. #endif
  508. static struct uart_driver ks8695_reg = {
  509. .owner = THIS_MODULE,
  510. .driver_name = "serial_ks8695",
  511. .dev_name = SERIAL_KS8695_DEVNAME,
  512. .major = SERIAL_KS8695_MAJOR,
  513. .minor = SERIAL_KS8695_MINOR,
  514. .nr = SERIAL_KS8695_NR,
  515. .cons = KS8695_CONSOLE,
  516. };
  517. static int __init ks8695uart_init(void)
  518. {
  519. int i, ret;
  520. printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
  521. ret = uart_register_driver(&ks8695_reg);
  522. if (ret)
  523. return ret;
  524. for (i = 0; i < SERIAL_KS8695_NR; i++)
  525. uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
  526. return 0;
  527. }
  528. static void __exit ks8695uart_exit(void)
  529. {
  530. int i;
  531. for (i = 0; i < SERIAL_KS8695_NR; i++)
  532. uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
  533. uart_unregister_driver(&ks8695_reg);
  534. }
  535. module_init(ks8695uart_init);
  536. module_exit(ks8695uart_exit);
  537. MODULE_DESCRIPTION("KS8695 serial port driver");
  538. MODULE_AUTHOR("Micrel Inc.");
  539. MODULE_LICENSE("GPL");