21285.c 11 KB

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