21285.c 11 KB

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