apbuart.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for GRLIB serial ports (APBUART)
  4. *
  5. * Based on linux/drivers/serial/amba.c
  6. *
  7. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  8. * Copyright (C) 2003 Konrad Eisele <[email protected]>
  9. * Copyright (C) 2006 Daniel Hellstrom <[email protected]>, Aeroflex Gaisler AB
  10. * Copyright (C) 2008 Gilead Kutnick <[email protected]>
  11. * Copyright (C) 2009 Kristoffer Glembo <[email protected]>, Aeroflex Gaisler AB
  12. */
  13. #include <linux/module.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/ioport.h>
  17. #include <linux/init.h>
  18. #include <linux/serial.h>
  19. #include <linux/console.h>
  20. #include <linux/sysrq.h>
  21. #include <linux/kthread.h>
  22. #include <linux/device.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/io.h>
  29. #include <linux/serial_core.h>
  30. #include <asm/irq.h>
  31. #include "apbuart.h"
  32. #define SERIAL_APBUART_MAJOR TTY_MAJOR
  33. #define SERIAL_APBUART_MINOR 64
  34. #define UART_DUMMY_RSR_RX 0x8000 /* for ignore all read */
  35. static void apbuart_tx_chars(struct uart_port *port);
  36. static void apbuart_stop_tx(struct uart_port *port)
  37. {
  38. unsigned int cr;
  39. cr = UART_GET_CTRL(port);
  40. cr &= ~UART_CTRL_TI;
  41. UART_PUT_CTRL(port, cr);
  42. }
  43. static void apbuart_start_tx(struct uart_port *port)
  44. {
  45. unsigned int cr;
  46. cr = UART_GET_CTRL(port);
  47. cr |= UART_CTRL_TI;
  48. UART_PUT_CTRL(port, cr);
  49. if (UART_GET_STATUS(port) & UART_STATUS_THE)
  50. apbuart_tx_chars(port);
  51. }
  52. static void apbuart_stop_rx(struct uart_port *port)
  53. {
  54. unsigned int cr;
  55. cr = UART_GET_CTRL(port);
  56. cr &= ~(UART_CTRL_RI);
  57. UART_PUT_CTRL(port, cr);
  58. }
  59. static void apbuart_rx_chars(struct uart_port *port)
  60. {
  61. unsigned int status, ch, rsr, flag;
  62. unsigned int max_chars = port->fifosize;
  63. status = UART_GET_STATUS(port);
  64. while (UART_RX_DATA(status) && (max_chars--)) {
  65. ch = UART_GET_CHAR(port);
  66. flag = TTY_NORMAL;
  67. port->icount.rx++;
  68. rsr = UART_GET_STATUS(port) | UART_DUMMY_RSR_RX;
  69. UART_PUT_STATUS(port, 0);
  70. if (rsr & UART_STATUS_ERR) {
  71. if (rsr & UART_STATUS_BR) {
  72. rsr &= ~(UART_STATUS_FE | UART_STATUS_PE);
  73. port->icount.brk++;
  74. if (uart_handle_break(port))
  75. goto ignore_char;
  76. } else if (rsr & UART_STATUS_PE) {
  77. port->icount.parity++;
  78. } else if (rsr & UART_STATUS_FE) {
  79. port->icount.frame++;
  80. }
  81. if (rsr & UART_STATUS_OE)
  82. port->icount.overrun++;
  83. rsr &= port->read_status_mask;
  84. if (rsr & UART_STATUS_PE)
  85. flag = TTY_PARITY;
  86. else if (rsr & UART_STATUS_FE)
  87. flag = TTY_FRAME;
  88. }
  89. if (uart_handle_sysrq_char(port, ch))
  90. goto ignore_char;
  91. uart_insert_char(port, rsr, UART_STATUS_OE, ch, flag);
  92. ignore_char:
  93. status = UART_GET_STATUS(port);
  94. }
  95. tty_flip_buffer_push(&port->state->port);
  96. }
  97. static void apbuart_tx_chars(struct uart_port *port)
  98. {
  99. struct circ_buf *xmit = &port->state->xmit;
  100. int count;
  101. if (port->x_char) {
  102. UART_PUT_CHAR(port, port->x_char);
  103. port->icount.tx++;
  104. port->x_char = 0;
  105. return;
  106. }
  107. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  108. apbuart_stop_tx(port);
  109. return;
  110. }
  111. /* amba: fill FIFO */
  112. count = port->fifosize >> 1;
  113. do {
  114. UART_PUT_CHAR(port, 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);
  120. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  121. uart_write_wakeup(port);
  122. if (uart_circ_empty(xmit))
  123. apbuart_stop_tx(port);
  124. }
  125. static irqreturn_t apbuart_int(int irq, void *dev_id)
  126. {
  127. struct uart_port *port = dev_id;
  128. unsigned int status;
  129. spin_lock(&port->lock);
  130. status = UART_GET_STATUS(port);
  131. if (status & UART_STATUS_DR)
  132. apbuart_rx_chars(port);
  133. if (status & UART_STATUS_THE)
  134. apbuart_tx_chars(port);
  135. spin_unlock(&port->lock);
  136. return IRQ_HANDLED;
  137. }
  138. static unsigned int apbuart_tx_empty(struct uart_port *port)
  139. {
  140. unsigned int status = UART_GET_STATUS(port);
  141. return status & UART_STATUS_THE ? TIOCSER_TEMT : 0;
  142. }
  143. static unsigned int apbuart_get_mctrl(struct uart_port *port)
  144. {
  145. /* The GRLIB APBUART handles flow control in hardware */
  146. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  147. }
  148. static void apbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  149. {
  150. /* The GRLIB APBUART handles flow control in hardware */
  151. }
  152. static void apbuart_break_ctl(struct uart_port *port, int break_state)
  153. {
  154. /* We don't support sending break */
  155. }
  156. static int apbuart_startup(struct uart_port *port)
  157. {
  158. int retval;
  159. unsigned int cr;
  160. /* Allocate the IRQ */
  161. retval = request_irq(port->irq, apbuart_int, 0, "apbuart", port);
  162. if (retval)
  163. return retval;
  164. /* Finally, enable interrupts */
  165. cr = UART_GET_CTRL(port);
  166. UART_PUT_CTRL(port,
  167. cr | UART_CTRL_RE | UART_CTRL_TE |
  168. UART_CTRL_RI | UART_CTRL_TI);
  169. return 0;
  170. }
  171. static void apbuart_shutdown(struct uart_port *port)
  172. {
  173. unsigned int cr;
  174. /* disable all interrupts, disable the port */
  175. cr = UART_GET_CTRL(port);
  176. UART_PUT_CTRL(port,
  177. cr & ~(UART_CTRL_RE | UART_CTRL_TE |
  178. UART_CTRL_RI | UART_CTRL_TI));
  179. /* Free the interrupt */
  180. free_irq(port->irq, port);
  181. }
  182. static void apbuart_set_termios(struct uart_port *port,
  183. struct ktermios *termios, const struct ktermios *old)
  184. {
  185. unsigned int cr;
  186. unsigned long flags;
  187. unsigned int baud, quot;
  188. /* Ask the core to calculate the divisor for us. */
  189. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  190. if (baud == 0)
  191. panic("invalid baudrate %i\n", port->uartclk / 16);
  192. /* uart_get_divisor calc a *16 uart freq, apbuart is *8 */
  193. quot = (uart_get_divisor(port, baud)) * 2;
  194. cr = UART_GET_CTRL(port);
  195. cr &= ~(UART_CTRL_PE | UART_CTRL_PS);
  196. if (termios->c_cflag & PARENB) {
  197. cr |= UART_CTRL_PE;
  198. if ((termios->c_cflag & PARODD))
  199. cr |= UART_CTRL_PS;
  200. }
  201. /* Enable flow control. */
  202. if (termios->c_cflag & CRTSCTS)
  203. cr |= UART_CTRL_FL;
  204. spin_lock_irqsave(&port->lock, flags);
  205. /* Update the per-port timeout. */
  206. uart_update_timeout(port, termios->c_cflag, baud);
  207. port->read_status_mask = UART_STATUS_OE;
  208. if (termios->c_iflag & INPCK)
  209. port->read_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
  210. /* Characters to ignore */
  211. port->ignore_status_mask = 0;
  212. if (termios->c_iflag & IGNPAR)
  213. port->ignore_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
  214. /* Ignore all characters if CREAD is not set. */
  215. if ((termios->c_cflag & CREAD) == 0)
  216. port->ignore_status_mask |= UART_DUMMY_RSR_RX;
  217. /* Set baud rate */
  218. quot -= 1;
  219. UART_PUT_SCAL(port, quot);
  220. UART_PUT_CTRL(port, cr);
  221. spin_unlock_irqrestore(&port->lock, flags);
  222. }
  223. static const char *apbuart_type(struct uart_port *port)
  224. {
  225. return port->type == PORT_APBUART ? "GRLIB/APBUART" : NULL;
  226. }
  227. static void apbuart_release_port(struct uart_port *port)
  228. {
  229. release_mem_region(port->mapbase, 0x100);
  230. }
  231. static int apbuart_request_port(struct uart_port *port)
  232. {
  233. return request_mem_region(port->mapbase, 0x100, "grlib-apbuart")
  234. != NULL ? 0 : -EBUSY;
  235. return 0;
  236. }
  237. /* Configure/autoconfigure the port */
  238. static void apbuart_config_port(struct uart_port *port, int flags)
  239. {
  240. if (flags & UART_CONFIG_TYPE) {
  241. port->type = PORT_APBUART;
  242. apbuart_request_port(port);
  243. }
  244. }
  245. /* Verify the new serial_struct (for TIOCSSERIAL) */
  246. static int apbuart_verify_port(struct uart_port *port,
  247. struct serial_struct *ser)
  248. {
  249. int ret = 0;
  250. if (ser->type != PORT_UNKNOWN && ser->type != PORT_APBUART)
  251. ret = -EINVAL;
  252. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  253. ret = -EINVAL;
  254. if (ser->baud_base < 9600)
  255. ret = -EINVAL;
  256. return ret;
  257. }
  258. static const struct uart_ops grlib_apbuart_ops = {
  259. .tx_empty = apbuart_tx_empty,
  260. .set_mctrl = apbuart_set_mctrl,
  261. .get_mctrl = apbuart_get_mctrl,
  262. .stop_tx = apbuart_stop_tx,
  263. .start_tx = apbuart_start_tx,
  264. .stop_rx = apbuart_stop_rx,
  265. .break_ctl = apbuart_break_ctl,
  266. .startup = apbuart_startup,
  267. .shutdown = apbuart_shutdown,
  268. .set_termios = apbuart_set_termios,
  269. .type = apbuart_type,
  270. .release_port = apbuart_release_port,
  271. .request_port = apbuart_request_port,
  272. .config_port = apbuart_config_port,
  273. .verify_port = apbuart_verify_port,
  274. };
  275. static struct uart_port grlib_apbuart_ports[UART_NR];
  276. static struct device_node *grlib_apbuart_nodes[UART_NR];
  277. static int apbuart_scan_fifo_size(struct uart_port *port, int portnumber)
  278. {
  279. int ctrl, loop = 0;
  280. int status;
  281. int fifosize;
  282. unsigned long flags;
  283. ctrl = UART_GET_CTRL(port);
  284. /*
  285. * Enable the transceiver and wait for it to be ready to send data.
  286. * Clear interrupts so that this process will not be externally
  287. * interrupted in the middle (which can cause the transceiver to
  288. * drain prematurely).
  289. */
  290. local_irq_save(flags);
  291. UART_PUT_CTRL(port, ctrl | UART_CTRL_TE);
  292. while (!UART_TX_READY(UART_GET_STATUS(port)))
  293. loop++;
  294. /*
  295. * Disable the transceiver so data isn't actually sent during the
  296. * actual test.
  297. */
  298. UART_PUT_CTRL(port, ctrl & ~(UART_CTRL_TE));
  299. fifosize = 1;
  300. UART_PUT_CHAR(port, 0);
  301. /*
  302. * So long as transmitting a character increments the tranceivier FIFO
  303. * length the FIFO must be at least that big. These bytes will
  304. * automatically drain off of the FIFO.
  305. */
  306. status = UART_GET_STATUS(port);
  307. while (((status >> 20) & 0x3F) == fifosize) {
  308. fifosize++;
  309. UART_PUT_CHAR(port, 0);
  310. status = UART_GET_STATUS(port);
  311. }
  312. fifosize--;
  313. UART_PUT_CTRL(port, ctrl);
  314. local_irq_restore(flags);
  315. if (fifosize == 0)
  316. fifosize = 1;
  317. return fifosize;
  318. }
  319. static void apbuart_flush_fifo(struct uart_port *port)
  320. {
  321. int i;
  322. for (i = 0; i < port->fifosize; i++)
  323. UART_GET_CHAR(port);
  324. }
  325. /* ======================================================================== */
  326. /* Console driver, if enabled */
  327. /* ======================================================================== */
  328. #ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
  329. static void apbuart_console_putchar(struct uart_port *port, unsigned char ch)
  330. {
  331. unsigned int status;
  332. do {
  333. status = UART_GET_STATUS(port);
  334. } while (!UART_TX_READY(status));
  335. UART_PUT_CHAR(port, ch);
  336. }
  337. static void
  338. apbuart_console_write(struct console *co, const char *s, unsigned int count)
  339. {
  340. struct uart_port *port = &grlib_apbuart_ports[co->index];
  341. unsigned int status, old_cr, new_cr;
  342. /* First save the CR then disable the interrupts */
  343. old_cr = UART_GET_CTRL(port);
  344. new_cr = old_cr & ~(UART_CTRL_RI | UART_CTRL_TI);
  345. UART_PUT_CTRL(port, new_cr);
  346. uart_console_write(port, s, count, apbuart_console_putchar);
  347. /*
  348. * Finally, wait for transmitter to become empty
  349. * and restore the TCR
  350. */
  351. do {
  352. status = UART_GET_STATUS(port);
  353. } while (!UART_TX_READY(status));
  354. UART_PUT_CTRL(port, old_cr);
  355. }
  356. static void __init
  357. apbuart_console_get_options(struct uart_port *port, int *baud,
  358. int *parity, int *bits)
  359. {
  360. if (UART_GET_CTRL(port) & (UART_CTRL_RE | UART_CTRL_TE)) {
  361. unsigned int quot, status;
  362. status = UART_GET_STATUS(port);
  363. *parity = 'n';
  364. if (status & UART_CTRL_PE) {
  365. if ((status & UART_CTRL_PS) == 0)
  366. *parity = 'e';
  367. else
  368. *parity = 'o';
  369. }
  370. *bits = 8;
  371. quot = UART_GET_SCAL(port) / 8;
  372. *baud = port->uartclk / (16 * (quot + 1));
  373. }
  374. }
  375. static int __init apbuart_console_setup(struct console *co, char *options)
  376. {
  377. struct uart_port *port;
  378. int baud = 38400;
  379. int bits = 8;
  380. int parity = 'n';
  381. int flow = 'n';
  382. pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
  383. co, co->index, options);
  384. /*
  385. * Check whether an invalid uart number has been specified, and
  386. * if so, search for the first available port that does have
  387. * console support.
  388. */
  389. if (co->index >= grlib_apbuart_port_nr)
  390. co->index = 0;
  391. port = &grlib_apbuart_ports[co->index];
  392. spin_lock_init(&port->lock);
  393. if (options)
  394. uart_parse_options(options, &baud, &parity, &bits, &flow);
  395. else
  396. apbuart_console_get_options(port, &baud, &parity, &bits);
  397. return uart_set_options(port, co, baud, parity, bits, flow);
  398. }
  399. static struct uart_driver grlib_apbuart_driver;
  400. static struct console grlib_apbuart_console = {
  401. .name = "ttyS",
  402. .write = apbuart_console_write,
  403. .device = uart_console_device,
  404. .setup = apbuart_console_setup,
  405. .flags = CON_PRINTBUFFER,
  406. .index = -1,
  407. .data = &grlib_apbuart_driver,
  408. };
  409. static int grlib_apbuart_configure(void);
  410. static int __init apbuart_console_init(void)
  411. {
  412. if (grlib_apbuart_configure())
  413. return -ENODEV;
  414. register_console(&grlib_apbuart_console);
  415. return 0;
  416. }
  417. console_initcall(apbuart_console_init);
  418. #define APBUART_CONSOLE (&grlib_apbuart_console)
  419. #else
  420. #define APBUART_CONSOLE NULL
  421. #endif
  422. static struct uart_driver grlib_apbuart_driver = {
  423. .owner = THIS_MODULE,
  424. .driver_name = "serial",
  425. .dev_name = "ttyS",
  426. .major = SERIAL_APBUART_MAJOR,
  427. .minor = SERIAL_APBUART_MINOR,
  428. .nr = UART_NR,
  429. .cons = APBUART_CONSOLE,
  430. };
  431. /* ======================================================================== */
  432. /* OF Platform Driver */
  433. /* ======================================================================== */
  434. static int apbuart_probe(struct platform_device *op)
  435. {
  436. int i;
  437. struct uart_port *port = NULL;
  438. for (i = 0; i < grlib_apbuart_port_nr; i++) {
  439. if (op->dev.of_node == grlib_apbuart_nodes[i])
  440. break;
  441. }
  442. port = &grlib_apbuart_ports[i];
  443. port->dev = &op->dev;
  444. port->irq = op->archdata.irqs[0];
  445. uart_add_one_port(&grlib_apbuart_driver, (struct uart_port *) port);
  446. apbuart_flush_fifo((struct uart_port *) port);
  447. printk(KERN_INFO "grlib-apbuart at 0x%llx, irq %d\n",
  448. (unsigned long long) port->mapbase, port->irq);
  449. return 0;
  450. }
  451. static const struct of_device_id apbuart_match[] = {
  452. {
  453. .name = "GAISLER_APBUART",
  454. },
  455. {
  456. .name = "01_00c",
  457. },
  458. {},
  459. };
  460. MODULE_DEVICE_TABLE(of, apbuart_match);
  461. static struct platform_driver grlib_apbuart_of_driver = {
  462. .probe = apbuart_probe,
  463. .driver = {
  464. .name = "grlib-apbuart",
  465. .of_match_table = apbuart_match,
  466. },
  467. };
  468. static int __init grlib_apbuart_configure(void)
  469. {
  470. struct device_node *np;
  471. int line = 0;
  472. for_each_matching_node(np, apbuart_match) {
  473. const int *ampopts;
  474. const u32 *freq_hz;
  475. const struct amba_prom_registers *regs;
  476. struct uart_port *port;
  477. unsigned long addr;
  478. ampopts = of_get_property(np, "ampopts", NULL);
  479. if (ampopts && (*ampopts == 0))
  480. continue; /* Ignore if used by another OS instance */
  481. regs = of_get_property(np, "reg", NULL);
  482. /* Frequency of APB Bus is frequency of UART */
  483. freq_hz = of_get_property(np, "freq", NULL);
  484. if (!regs || !freq_hz || (*freq_hz == 0))
  485. continue;
  486. grlib_apbuart_nodes[line] = np;
  487. addr = regs->phys_addr;
  488. port = &grlib_apbuart_ports[line];
  489. port->mapbase = addr;
  490. port->membase = ioremap(addr, sizeof(struct grlib_apbuart_regs_map));
  491. port->irq = 0;
  492. port->iotype = UPIO_MEM;
  493. port->ops = &grlib_apbuart_ops;
  494. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE);
  495. port->flags = UPF_BOOT_AUTOCONF;
  496. port->line = line;
  497. port->uartclk = *freq_hz;
  498. port->fifosize = apbuart_scan_fifo_size((struct uart_port *) port, line);
  499. line++;
  500. /* We support maximum UART_NR uarts ... */
  501. if (line == UART_NR)
  502. break;
  503. }
  504. grlib_apbuart_driver.nr = grlib_apbuart_port_nr = line;
  505. return line ? 0 : -ENODEV;
  506. }
  507. static int __init grlib_apbuart_init(void)
  508. {
  509. int ret;
  510. /* Find all APBUARTS in device the tree and initialize their ports */
  511. ret = grlib_apbuart_configure();
  512. if (ret)
  513. return ret;
  514. printk(KERN_INFO "Serial: GRLIB APBUART driver\n");
  515. ret = uart_register_driver(&grlib_apbuart_driver);
  516. if (ret) {
  517. printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
  518. __FILE__, ret);
  519. return ret;
  520. }
  521. ret = platform_driver_register(&grlib_apbuart_of_driver);
  522. if (ret) {
  523. printk(KERN_ERR
  524. "%s: platform_driver_register failed (%i)\n",
  525. __FILE__, ret);
  526. uart_unregister_driver(&grlib_apbuart_driver);
  527. return ret;
  528. }
  529. return ret;
  530. }
  531. static void __exit grlib_apbuart_exit(void)
  532. {
  533. int i;
  534. for (i = 0; i < grlib_apbuart_port_nr; i++)
  535. uart_remove_one_port(&grlib_apbuart_driver,
  536. &grlib_apbuart_ports[i]);
  537. uart_unregister_driver(&grlib_apbuart_driver);
  538. platform_driver_unregister(&grlib_apbuart_of_driver);
  539. }
  540. module_init(grlib_apbuart_init);
  541. module_exit(grlib_apbuart_exit);
  542. MODULE_AUTHOR("Aeroflex Gaisler AB");
  543. MODULE_DESCRIPTION("GRLIB APBUART serial driver");
  544. MODULE_VERSION("2.1");
  545. MODULE_LICENSE("GPL");