21285.c 12 KB

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