21285.c 11 KB

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