21285.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * linux/drivers/serial/21285.c
  3. *
  4. * Driver for the serial port on the 21285 StrongArm-110 core logic chip.
  5. *
  6. * Based on drivers/char/serial.c
  7. *
  8. * $Id: 21285.c,v 1.37 2002/07/28 10:03:27 rmk Exp $
  9. */
  10. #include <linux/module.h>
  11. #include <linux/tty.h>
  12. #include <linux/ioport.h>
  13. #include <linux/init.h>
  14. #include <linux/console.h>
  15. #include <linux/device.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/serial_core.h>
  18. #include <linux/serial.h>
  19. #include <asm/io.h>
  20. #include <asm/irq.h>
  21. #include <asm/mach-types.h>
  22. #include <asm/hardware/dec21285.h>
  23. #include <asm/hardware.h>
  24. #define BAUD_BASE (mem_fclk_21285/64)
  25. #define SERIAL_21285_NAME "ttyFB"
  26. #define SERIAL_21285_MAJOR 204
  27. #define SERIAL_21285_MINOR 4
  28. #define RXSTAT_DUMMY_READ 0x80000000
  29. #define RXSTAT_FRAME (1 << 0)
  30. #define RXSTAT_PARITY (1 << 1)
  31. #define RXSTAT_OVERRUN (1 << 2)
  32. #define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
  33. #define H_UBRLCR_BREAK (1 << 0)
  34. #define H_UBRLCR_PARENB (1 << 1)
  35. #define H_UBRLCR_PAREVN (1 << 2)
  36. #define H_UBRLCR_STOPB (1 << 3)
  37. #define H_UBRLCR_FIFO (1 << 4)
  38. static const char serial21285_name[] = "Footbridge UART";
  39. #define tx_enabled(port) ((port)->unused[0])
  40. #define rx_enabled(port) ((port)->unused[1])
  41. /*
  42. * The documented expression for selecting the divisor is:
  43. * BAUD_BASE / baud - 1
  44. * However, typically BAUD_BASE is not divisible by baud, so
  45. * we want to select the divisor that gives us the minimum
  46. * error. Therefore, we want:
  47. * int(BAUD_BASE / baud - 0.5) ->
  48. * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
  49. * int((BAUD_BASE - (baud >> 1)) / baud)
  50. */
  51. static void serial21285_stop_tx(struct uart_port *port)
  52. {
  53. if (tx_enabled(port)) {
  54. disable_irq(IRQ_CONTX);
  55. tx_enabled(port) = 0;
  56. }
  57. }
  58. static void serial21285_start_tx(struct uart_port *port)
  59. {
  60. if (!tx_enabled(port)) {
  61. enable_irq(IRQ_CONTX);
  62. tx_enabled(port) = 1;
  63. }
  64. }
  65. static void serial21285_stop_rx(struct uart_port *port)
  66. {
  67. if (rx_enabled(port)) {
  68. disable_irq(IRQ_CONRX);
  69. rx_enabled(port) = 0;
  70. }
  71. }
  72. static void serial21285_enable_ms(struct uart_port *port)
  73. {
  74. }
  75. static irqreturn_t serial21285_rx_chars(int irq, void *dev_id)
  76. {
  77. struct uart_port *port = dev_id;
  78. struct tty_struct *tty = port->info->tty;
  79. unsigned int status, ch, flag, rxs, max_count = 256;
  80. status = *CSR_UARTFLG;
  81. while (!(status & 0x10) && max_count--) {
  82. ch = *CSR_UARTDR;
  83. flag = TTY_NORMAL;
  84. port->icount.rx++;
  85. rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
  86. if (unlikely(rxs & RXSTAT_ANYERR)) {
  87. if (rxs & RXSTAT_PARITY)
  88. port->icount.parity++;
  89. else if (rxs & RXSTAT_FRAME)
  90. port->icount.frame++;
  91. if (rxs & RXSTAT_OVERRUN)
  92. port->icount.overrun++;
  93. rxs &= port->read_status_mask;
  94. if (rxs & RXSTAT_PARITY)
  95. flag = TTY_PARITY;
  96. else if (rxs & RXSTAT_FRAME)
  97. flag = TTY_FRAME;
  98. }
  99. uart_insert_char(port, rxs, RXSTAT_OVERRUN, ch, flag);
  100. status = *CSR_UARTFLG;
  101. }
  102. tty_flip_buffer_push(tty);
  103. return IRQ_HANDLED;
  104. }
  105. static irqreturn_t serial21285_tx_chars(int irq, void *dev_id)
  106. {
  107. struct uart_port *port = dev_id;
  108. struct circ_buf *xmit = &port->info->xmit;
  109. int count = 256;
  110. if (port->x_char) {
  111. *CSR_UARTDR = port->x_char;
  112. port->icount.tx++;
  113. port->x_char = 0;
  114. goto out;
  115. }
  116. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  117. serial21285_stop_tx(port);
  118. goto out;
  119. }
  120. do {
  121. *CSR_UARTDR = xmit->buf[xmit->tail];
  122. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  123. port->icount.tx++;
  124. if (uart_circ_empty(xmit))
  125. break;
  126. } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
  127. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  128. uart_write_wakeup(port);
  129. if (uart_circ_empty(xmit))
  130. serial21285_stop_tx(port);
  131. out:
  132. return IRQ_HANDLED;
  133. }
  134. static unsigned int serial21285_tx_empty(struct uart_port *port)
  135. {
  136. return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
  137. }
  138. /* no modem control lines */
  139. static unsigned int serial21285_get_mctrl(struct uart_port *port)
  140. {
  141. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  142. }
  143. static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
  144. {
  145. }
  146. static void serial21285_break_ctl(struct uart_port *port, int break_state)
  147. {
  148. unsigned long flags;
  149. unsigned int h_lcr;
  150. spin_lock_irqsave(&port->lock, flags);
  151. h_lcr = *CSR_H_UBRLCR;
  152. if (break_state)
  153. h_lcr |= H_UBRLCR_BREAK;
  154. else
  155. h_lcr &= ~H_UBRLCR_BREAK;
  156. *CSR_H_UBRLCR = h_lcr;
  157. spin_unlock_irqrestore(&port->lock, flags);
  158. }
  159. static int serial21285_startup(struct uart_port *port)
  160. {
  161. int ret;
  162. tx_enabled(port) = 1;
  163. rx_enabled(port) = 1;
  164. ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
  165. serial21285_name, port);
  166. if (ret == 0) {
  167. ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
  168. serial21285_name, port);
  169. if (ret)
  170. free_irq(IRQ_CONRX, port);
  171. }
  172. return ret;
  173. }
  174. static void serial21285_shutdown(struct uart_port *port)
  175. {
  176. free_irq(IRQ_CONTX, port);
  177. free_irq(IRQ_CONRX, port);
  178. }
  179. static void
  180. serial21285_set_termios(struct uart_port *port, struct ktermios *termios,
  181. struct ktermios *old)
  182. {
  183. unsigned long flags;
  184. unsigned int baud, quot, h_lcr;
  185. /*
  186. * We don't support modem control lines.
  187. */
  188. termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
  189. termios->c_cflag |= CLOCAL;
  190. /*
  191. * We don't support BREAK character recognition.
  192. */
  193. termios->c_iflag &= ~(IGNBRK | BRKINT);
  194. /*
  195. * Ask the core to calculate the divisor for us.
  196. */
  197. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  198. quot = uart_get_divisor(port, baud);
  199. switch (termios->c_cflag & CSIZE) {
  200. case CS5:
  201. h_lcr = 0x00;
  202. break;
  203. case CS6:
  204. h_lcr = 0x20;
  205. break;
  206. case CS7:
  207. h_lcr = 0x40;
  208. break;
  209. default: /* CS8 */
  210. h_lcr = 0x60;
  211. break;
  212. }
  213. if (termios->c_cflag & CSTOPB)
  214. h_lcr |= H_UBRLCR_STOPB;
  215. if (termios->c_cflag & PARENB) {
  216. h_lcr |= H_UBRLCR_PARENB;
  217. if (!(termios->c_cflag & PARODD))
  218. h_lcr |= H_UBRLCR_PAREVN;
  219. }
  220. if (port->fifosize)
  221. h_lcr |= H_UBRLCR_FIFO;
  222. spin_lock_irqsave(&port->lock, flags);
  223. /*
  224. * Update the per-port timeout.
  225. */
  226. uart_update_timeout(port, termios->c_cflag, baud);
  227. /*
  228. * Which character status flags are we interested in?
  229. */
  230. port->read_status_mask = RXSTAT_OVERRUN;
  231. if (termios->c_iflag & INPCK)
  232. port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
  233. tty_encode_baud_rate(tty, baud, baud);
  234. /*
  235. * Which character status flags should we ignore?
  236. */
  237. port->ignore_status_mask = 0;
  238. if (termios->c_iflag & IGNPAR)
  239. port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
  240. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  241. port->ignore_status_mask |= RXSTAT_OVERRUN;
  242. /*
  243. * Ignore all characters if CREAD is not set.
  244. */
  245. if ((termios->c_cflag & CREAD) == 0)
  246. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  247. quot -= 1;
  248. *CSR_UARTCON = 0;
  249. *CSR_L_UBRLCR = quot & 0xff;
  250. *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
  251. *CSR_H_UBRLCR = h_lcr;
  252. *CSR_UARTCON = 1;
  253. spin_unlock_irqrestore(&port->lock, flags);
  254. }
  255. static const char *serial21285_type(struct uart_port *port)
  256. {
  257. return port->type == PORT_21285 ? "DC21285" : NULL;
  258. }
  259. static void serial21285_release_port(struct uart_port *port)
  260. {
  261. release_mem_region(port->mapbase, 32);
  262. }
  263. static int serial21285_request_port(struct uart_port *port)
  264. {
  265. return request_mem_region(port->mapbase, 32, serial21285_name)
  266. != NULL ? 0 : -EBUSY;
  267. }
  268. static void serial21285_config_port(struct uart_port *port, int flags)
  269. {
  270. if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
  271. port->type = PORT_21285;
  272. }
  273. /*
  274. * verify the new serial_struct (for TIOCSSERIAL).
  275. */
  276. static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
  277. {
  278. int ret = 0;
  279. if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
  280. ret = -EINVAL;
  281. if (ser->irq != NO_IRQ)
  282. ret = -EINVAL;
  283. if (ser->baud_base != port->uartclk / 16)
  284. ret = -EINVAL;
  285. return ret;
  286. }
  287. static struct uart_ops serial21285_ops = {
  288. .tx_empty = serial21285_tx_empty,
  289. .get_mctrl = serial21285_get_mctrl,
  290. .set_mctrl = serial21285_set_mctrl,
  291. .stop_tx = serial21285_stop_tx,
  292. .start_tx = serial21285_start_tx,
  293. .stop_rx = serial21285_stop_rx,
  294. .enable_ms = serial21285_enable_ms,
  295. .break_ctl = serial21285_break_ctl,
  296. .startup = serial21285_startup,
  297. .shutdown = serial21285_shutdown,
  298. .set_termios = serial21285_set_termios,
  299. .type = serial21285_type,
  300. .release_port = serial21285_release_port,
  301. .request_port = serial21285_request_port,
  302. .config_port = serial21285_config_port,
  303. .verify_port = serial21285_verify_port,
  304. };
  305. static struct uart_port serial21285_port = {
  306. .mapbase = 0x42000160,
  307. .iotype = UPIO_MEM,
  308. .irq = NO_IRQ,
  309. .fifosize = 16,
  310. .ops = &serial21285_ops,
  311. .flags = UPF_BOOT_AUTOCONF,
  312. };
  313. static void serial21285_setup_ports(void)
  314. {
  315. serial21285_port.uartclk = mem_fclk_21285 / 4;
  316. }
  317. #ifdef CONFIG_SERIAL_21285_CONSOLE
  318. static void serial21285_console_putchar(struct uart_port *port, int ch)
  319. {
  320. while (*CSR_UARTFLG & 0x20)
  321. barrier();
  322. *CSR_UARTDR = ch;
  323. }
  324. static void
  325. serial21285_console_write(struct console *co, const char *s,
  326. unsigned int count)
  327. {
  328. uart_console_write(&serial21285_port, s, count, serial21285_console_putchar);
  329. }
  330. static void __init
  331. serial21285_get_options(struct uart_port *port, int *baud,
  332. int *parity, int *bits)
  333. {
  334. if (*CSR_UARTCON == 1) {
  335. unsigned int tmp;
  336. tmp = *CSR_H_UBRLCR;
  337. switch (tmp & 0x60) {
  338. case 0x00:
  339. *bits = 5;
  340. break;
  341. case 0x20:
  342. *bits = 6;
  343. break;
  344. case 0x40:
  345. *bits = 7;
  346. break;
  347. default:
  348. case 0x60:
  349. *bits = 8;
  350. break;
  351. }
  352. if (tmp & H_UBRLCR_PARENB) {
  353. *parity = 'o';
  354. if (tmp & H_UBRLCR_PAREVN)
  355. *parity = 'e';
  356. }
  357. tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
  358. *baud = port->uartclk / (16 * (tmp + 1));
  359. }
  360. }
  361. static int __init serial21285_console_setup(struct console *co, char *options)
  362. {
  363. struct uart_port *port = &serial21285_port;
  364. int baud = 9600;
  365. int bits = 8;
  366. int parity = 'n';
  367. int flow = 'n';
  368. if (machine_is_personal_server())
  369. baud = 57600;
  370. /*
  371. * Check whether an invalid uart number has been specified, and
  372. * if so, search for the first available port that does have
  373. * console support.
  374. */
  375. if (options)
  376. uart_parse_options(options, &baud, &parity, &bits, &flow);
  377. else
  378. serial21285_get_options(port, &baud, &parity, &bits);
  379. return uart_set_options(port, co, baud, parity, bits, flow);
  380. }
  381. static struct uart_driver serial21285_reg;
  382. static struct console serial21285_console =
  383. {
  384. .name = SERIAL_21285_NAME,
  385. .write = serial21285_console_write,
  386. .device = uart_console_device,
  387. .setup = serial21285_console_setup,
  388. .flags = CON_PRINTBUFFER,
  389. .index = -1,
  390. .data = &serial21285_reg,
  391. };
  392. static int __init rs285_console_init(void)
  393. {
  394. serial21285_setup_ports();
  395. register_console(&serial21285_console);
  396. return 0;
  397. }
  398. console_initcall(rs285_console_init);
  399. #define SERIAL_21285_CONSOLE &serial21285_console
  400. #else
  401. #define SERIAL_21285_CONSOLE NULL
  402. #endif
  403. static struct uart_driver serial21285_reg = {
  404. .owner = THIS_MODULE,
  405. .driver_name = "ttyFB",
  406. .dev_name = "ttyFB",
  407. .major = SERIAL_21285_MAJOR,
  408. .minor = SERIAL_21285_MINOR,
  409. .nr = 1,
  410. .cons = SERIAL_21285_CONSOLE,
  411. };
  412. static int __init serial21285_init(void)
  413. {
  414. int ret;
  415. printk(KERN_INFO "Serial: 21285 driver $Revision: 1.37 $\n");
  416. serial21285_setup_ports();
  417. ret = uart_register_driver(&serial21285_reg);
  418. if (ret == 0)
  419. uart_add_one_port(&serial21285_reg, &serial21285_port);
  420. return ret;
  421. }
  422. static void __exit serial21285_exit(void)
  423. {
  424. uart_remove_one_port(&serial21285_reg, &serial21285_port);
  425. uart_unregister_driver(&serial21285_reg);
  426. }
  427. module_init(serial21285_init);
  428. module_exit(serial21285_exit);
  429. MODULE_LICENSE("GPL");
  430. MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver $Revision: 1.37 $");
  431. MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR, SERIAL_21285_MINOR);