apbuart.c 15 KB

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