21285.c 11 KB

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