nwpserial.c 11 KB

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