21285.c 11 KB

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