nwpserial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Serial Port driver for a NWP uart device
  3. *
  4. * Copyright (C) 2008 IBM Corp., Benjamin Krill <ben@codiert.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/console.h>
  14. #include <linux/serial.h>
  15. #include <linux/serial_reg.h>
  16. #include <linux/serial_core.h>
  17. #include <linux/tty.h>
  18. #include <linux/tty_flip.h>
  19. #include <linux/irqreturn.h>
  20. #include <linux/mutex.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/of_device.h>
  23. #include <linux/nwpserial.h>
  24. #include <asm/prom.h>
  25. #include <asm/dcr.h>
  26. #define NWPSERIAL_NR 2
  27. #define NWPSERIAL_STATUS_RXVALID 0x1
  28. #define NWPSERIAL_STATUS_TXFULL 0x2
  29. struct nwpserial_port {
  30. struct uart_port port;
  31. dcr_host_t dcr_host;
  32. unsigned int ier;
  33. unsigned int mcr;
  34. };
  35. static DEFINE_MUTEX(nwpserial_mutex);
  36. static struct nwpserial_port nwpserial_ports[NWPSERIAL_NR];
  37. static void wait_for_bits(struct nwpserial_port *up, int bits)
  38. {
  39. unsigned int status, tmout = 10000;
  40. /* Wait up to 10ms for the character(s) to be sent. */
  41. do {
  42. status = dcr_read(up->dcr_host, UART_LSR);
  43. if (--tmout == 0)
  44. break;
  45. udelay(1);
  46. } while ((status & bits) != bits);
  47. }
  48. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
  49. static void nwpserial_console_putchar(struct uart_port *port, int c)
  50. {
  51. struct nwpserial_port *up;
  52. up = container_of(port, struct nwpserial_port, port);
  53. /* check if tx buffer is full */
  54. wait_for_bits(up, UART_LSR_THRE);
  55. dcr_write(up->dcr_host, UART_TX, c);
  56. up->port.icount.tx++;
  57. }
  58. static void
  59. nwpserial_console_write(struct console *co, const char *s, unsigned int count)
  60. {
  61. struct nwpserial_port *up = &nwpserial_ports[co->index];
  62. unsigned long flags;
  63. int locked = 1;
  64. if (oops_in_progress)
  65. locked = spin_trylock_irqsave(&up->port.lock, flags);
  66. else
  67. spin_lock_irqsave(&up->port.lock, flags);
  68. /* save and disable interrupt */
  69. up->ier = dcr_read(up->dcr_host, UART_IER);
  70. dcr_write(up->dcr_host, UART_IER, up->ier & ~UART_IER_RDI);
  71. uart_console_write(&up->port, s, count, nwpserial_console_putchar);
  72. /* wait for transmitter to become empty */
  73. while ((dcr_read(up->dcr_host, UART_LSR) & UART_LSR_THRE) == 0)
  74. cpu_relax();
  75. /* restore interrupt state */
  76. dcr_write(up->dcr_host, UART_IER, up->ier);
  77. if (locked)
  78. spin_unlock_irqrestore(&up->port.lock, flags);
  79. }
  80. static struct uart_driver nwpserial_reg;
  81. static struct console nwpserial_console = {
  82. .name = "ttySQ",
  83. .write = nwpserial_console_write,
  84. .device = uart_console_device,
  85. .flags = CON_PRINTBUFFER,
  86. .index = -1,
  87. .data = &nwpserial_reg,
  88. };
  89. #define NWPSERIAL_CONSOLE (&nwpserial_console)
  90. #else
  91. #define NWPSERIAL_CONSOLE NULL
  92. #endif /* CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE */
  93. /**************************************************************************/
  94. static int nwpserial_request_port(struct uart_port *port)
  95. {
  96. return 0;
  97. }
  98. static void nwpserial_release_port(struct uart_port *port)
  99. {
  100. /* N/A */
  101. }
  102. static void nwpserial_config_port(struct uart_port *port, int flags)
  103. {
  104. port->type = PORT_NWPSERIAL;
  105. }
  106. static irqreturn_t nwpserial_interrupt(int irq, void *dev_id)
  107. {
  108. struct nwpserial_port *up = dev_id;
  109. struct tty_struct *tty = up->port.state->port.tty;
  110. irqreturn_t ret;
  111. unsigned int iir;
  112. unsigned char ch;
  113. spin_lock(&up->port.lock);
  114. /* check if the uart was the interrupt source. */
  115. iir = dcr_read(up->dcr_host, UART_IIR);
  116. if (!iir) {
  117. ret = IRQ_NONE;
  118. goto out;
  119. }
  120. do {
  121. up->port.icount.rx++;
  122. ch = dcr_read(up->dcr_host, UART_RX);
  123. if (up->port.ignore_status_mask != NWPSERIAL_STATUS_RXVALID)
  124. tty_insert_flip_char(tty, ch, TTY_NORMAL);
  125. } while (dcr_read(up->dcr_host, UART_LSR) & UART_LSR_DR);
  126. tty_flip_buffer_push(tty);
  127. ret = IRQ_HANDLED;
  128. /* clear interrupt */
  129. dcr_write(up->dcr_host, UART_IIR, 1);
  130. out:
  131. spin_unlock(&up->port.lock);
  132. return ret;
  133. }
  134. static int nwpserial_startup(struct uart_port *port)
  135. {
  136. struct nwpserial_port *up;
  137. int err;
  138. up = container_of(port, struct nwpserial_port, port);
  139. /* disable flow control by default */
  140. up->mcr = dcr_read(up->dcr_host, UART_MCR) & ~UART_MCR_AFE;
  141. dcr_write(up->dcr_host, UART_MCR, up->mcr);
  142. /* register interrupt handler */
  143. err = request_irq(up->port.irq, nwpserial_interrupt,
  144. IRQF_SHARED, "nwpserial", up);
  145. if (err)
  146. return err;
  147. /* enable interrupts */
  148. up->ier = UART_IER_RDI;
  149. dcr_write(up->dcr_host, UART_IER, up->ier);
  150. /* enable receiving */
  151. up->port.ignore_status_mask &= ~NWPSERIAL_STATUS_RXVALID;
  152. return 0;
  153. }
  154. static void nwpserial_shutdown(struct uart_port *port)
  155. {
  156. struct nwpserial_port *up;
  157. up = container_of(port, struct nwpserial_port, port);
  158. /* disable receiving */
  159. up->port.ignore_status_mask |= NWPSERIAL_STATUS_RXVALID;
  160. /* disable interrupts from this port */
  161. up->ier = 0;
  162. dcr_write(up->dcr_host, UART_IER, up->ier);
  163. /* free irq */
  164. free_irq(up->port.irq, port);
  165. }
  166. static int nwpserial_verify_port(struct uart_port *port,
  167. struct serial_struct *ser)
  168. {
  169. return -EINVAL;
  170. }
  171. static const char *nwpserial_type(struct uart_port *port)
  172. {
  173. return port->type == PORT_NWPSERIAL ? "nwpserial" : NULL;
  174. }
  175. static void nwpserial_set_termios(struct uart_port *port,
  176. struct ktermios *termios, struct ktermios *old)
  177. {
  178. struct nwpserial_port *up;
  179. up = container_of(port, struct nwpserial_port, port);
  180. up->port.read_status_mask = NWPSERIAL_STATUS_RXVALID
  181. | NWPSERIAL_STATUS_TXFULL;
  182. up->port.ignore_status_mask = 0;
  183. /* ignore all characters if CREAD is not set */
  184. if ((termios->c_cflag & CREAD) == 0)
  185. up->port.ignore_status_mask |= NWPSERIAL_STATUS_RXVALID;
  186. /* Copy back the old hardware settings */
  187. if (old)
  188. tty_termios_copy_hw(termios, old);
  189. }
  190. static void nwpserial_break_ctl(struct uart_port *port, int ctl)
  191. {
  192. /* N/A */
  193. }
  194. static void nwpserial_enable_ms(struct uart_port *port)
  195. {
  196. /* N/A */
  197. }
  198. static void nwpserial_stop_rx(struct uart_port *port)
  199. {
  200. struct nwpserial_port *up;
  201. up = container_of(port, struct nwpserial_port, port);
  202. /* don't forward any more data (like !CREAD) */
  203. up->port.ignore_status_mask = NWPSERIAL_STATUS_RXVALID;
  204. }
  205. static void nwpserial_putchar(struct nwpserial_port *up, unsigned char c)
  206. {
  207. /* check if tx buffer is full */
  208. wait_for_bits(up, UART_LSR_THRE);
  209. dcr_write(up->dcr_host, UART_TX, c);
  210. up->port.icount.tx++;
  211. }
  212. static void nwpserial_start_tx(struct uart_port *port)
  213. {
  214. struct nwpserial_port *up;
  215. struct circ_buf *xmit;
  216. up = container_of(port, struct nwpserial_port, port);
  217. xmit = &up->port.state->xmit;
  218. if (port->x_char) {
  219. nwpserial_putchar(up, up->port.x_char);
  220. port->x_char = 0;
  221. }
  222. while (!(uart_circ_empty(xmit) || uart_tx_stopped(&up->port))) {
  223. nwpserial_putchar(up, xmit->buf[xmit->tail]);
  224. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
  225. }
  226. }
  227. static unsigned int nwpserial_get_mctrl(struct uart_port *port)
  228. {
  229. return 0;
  230. }
  231. static void nwpserial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  232. {
  233. /* N/A */
  234. }
  235. static void nwpserial_stop_tx(struct uart_port *port)
  236. {
  237. /* N/A */
  238. }
  239. static unsigned int nwpserial_tx_empty(struct uart_port *port)
  240. {
  241. struct nwpserial_port *up;
  242. unsigned long flags;
  243. int ret;
  244. up = container_of(port, struct nwpserial_port, port);
  245. spin_lock_irqsave(&up->port.lock, flags);
  246. ret = dcr_read(up->dcr_host, UART_LSR);
  247. spin_unlock_irqrestore(&up->port.lock, flags);
  248. return ret & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  249. }
  250. static struct uart_ops nwpserial_pops = {
  251. .tx_empty = nwpserial_tx_empty,
  252. .set_mctrl = nwpserial_set_mctrl,
  253. .get_mctrl = nwpserial_get_mctrl,
  254. .stop_tx = nwpserial_stop_tx,
  255. .start_tx = nwpserial_start_tx,
  256. .stop_rx = nwpserial_stop_rx,
  257. .enable_ms = nwpserial_enable_ms,
  258. .break_ctl = nwpserial_break_ctl,
  259. .startup = nwpserial_startup,
  260. .shutdown = nwpserial_shutdown,
  261. .set_termios = nwpserial_set_termios,
  262. .type = nwpserial_type,
  263. .release_port = nwpserial_release_port,
  264. .request_port = nwpserial_request_port,
  265. .config_port = nwpserial_config_port,
  266. .verify_port = nwpserial_verify_port,
  267. };
  268. static struct uart_driver nwpserial_reg = {
  269. .owner = THIS_MODULE,
  270. .driver_name = "nwpserial",
  271. .dev_name = "ttySQ",
  272. .major = TTY_MAJOR,
  273. .minor = 68,
  274. .nr = NWPSERIAL_NR,
  275. .cons = NWPSERIAL_CONSOLE,
  276. };
  277. int nwpserial_register_port(struct uart_port *port)
  278. {
  279. struct nwpserial_port *up = NULL;
  280. int ret = -1;
  281. int i;
  282. static int first = 1;
  283. int dcr_len;
  284. int dcr_base;
  285. struct device_node *dn;
  286. mutex_lock(&nwpserial_mutex);
  287. dn = port->dev->of_node;
  288. if (dn == NULL)
  289. goto out;
  290. /* get dcr base. */
  291. dcr_base = dcr_resource_start(dn, 0);
  292. /* find matching entry */
  293. for (i = 0; i < NWPSERIAL_NR; i++)
  294. if (nwpserial_ports[i].port.iobase == dcr_base) {
  295. up = &nwpserial_ports[i];
  296. break;
  297. }
  298. /* we didn't find a mtching entry, search for a free port */
  299. if (up == NULL)
  300. for (i = 0; i < NWPSERIAL_NR; i++)
  301. if (nwpserial_ports[i].port.type == PORT_UNKNOWN &&
  302. nwpserial_ports[i].port.iobase == 0) {
  303. up = &nwpserial_ports[i];
  304. break;
  305. }
  306. if (up == NULL) {
  307. ret = -EBUSY;
  308. goto out;
  309. }
  310. if (first)
  311. uart_register_driver(&nwpserial_reg);
  312. first = 0;
  313. up->port.membase = port->membase;
  314. up->port.irq = port->irq;
  315. up->port.uartclk = port->uartclk;
  316. up->port.fifosize = port->fifosize;
  317. up->port.regshift = port->regshift;
  318. up->port.iotype = port->iotype;
  319. up->port.flags = port->flags;
  320. up->port.mapbase = port->mapbase;
  321. up->port.private_data = port->private_data;
  322. if (port->dev)
  323. up->port.dev = port->dev;
  324. if (up->port.iobase != dcr_base) {
  325. up->port.ops = &nwpserial_pops;
  326. up->port.fifosize = 16;
  327. spin_lock_init(&up->port.lock);
  328. up->port.iobase = dcr_base;
  329. dcr_len = dcr_resource_len(dn, 0);
  330. up->dcr_host = dcr_map(dn, dcr_base, dcr_len);
  331. if (!DCR_MAP_OK(up->dcr_host)) {
  332. printk(KERN_ERR "Cannot map DCR resources for NWPSERIAL");
  333. goto out;
  334. }
  335. }
  336. ret = uart_add_one_port(&nwpserial_reg, &up->port);
  337. if (ret == 0)
  338. ret = up->port.line;
  339. out:
  340. mutex_unlock(&nwpserial_mutex);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL(nwpserial_register_port);
  344. void nwpserial_unregister_port(int line)
  345. {
  346. struct nwpserial_port *up = &nwpserial_ports[line];
  347. mutex_lock(&nwpserial_mutex);
  348. uart_remove_one_port(&nwpserial_reg, &up->port);
  349. up->port.type = PORT_UNKNOWN;
  350. mutex_unlock(&nwpserial_mutex);
  351. }
  352. EXPORT_SYMBOL(nwpserial_unregister_port);
  353. #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
  354. static int __init nwpserial_console_init(void)
  355. {
  356. struct nwpserial_port *up = NULL;
  357. struct device_node *dn;
  358. const char *name;
  359. int dcr_base;
  360. int dcr_len;
  361. int i;
  362. /* search for a free port */
  363. for (i = 0; i < NWPSERIAL_NR; i++)
  364. if (nwpserial_ports[i].port.type == PORT_UNKNOWN) {
  365. up = &nwpserial_ports[i];
  366. break;
  367. }
  368. if (up == NULL)
  369. return -1;
  370. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  371. if (name == NULL)
  372. return -1;
  373. dn = of_find_node_by_path(name);
  374. if (!dn)
  375. return -1;
  376. spin_lock_init(&up->port.lock);
  377. up->port.ops = &nwpserial_pops;
  378. up->port.type = PORT_NWPSERIAL;
  379. up->port.fifosize = 16;
  380. dcr_base = dcr_resource_start(dn, 0);
  381. dcr_len = dcr_resource_len(dn, 0);
  382. up->port.iobase = dcr_base;
  383. up->dcr_host = dcr_map(dn, dcr_base, dcr_len);
  384. if (!DCR_MAP_OK(up->dcr_host)) {
  385. printk("Cannot map DCR resources for SERIAL");
  386. return -1;
  387. }
  388. register_console(&nwpserial_console);
  389. return 0;
  390. }
  391. console_initcall(nwpserial_console_init);
  392. #endif /* CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE */