apbuart.c 16 KB

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