21285.c 12 KB

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