nwpserial.c 11 KB

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