apbps2.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2013 Aeroflex Gaisler
  3. *
  4. * This driver supports the APBPS2 PS/2 core available in the GRLIB
  5. * VHDL IP core library.
  6. *
  7. * Full documentation of the APBPS2 core can be found here:
  8. * http://www.gaisler.com/products/grlib/grip.pdf
  9. *
  10. * See "Documentation/devicetree/bindings/input/ps2keyb-mouse-apbps2.txt" for
  11. * information on open firmware properties.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * Contributors: Daniel Hellstrom <daniel@gaisler.com>
  19. */
  20. #include <linux/platform_device.h>
  21. #include <linux/of_device.h>
  22. #include <linux/module.h>
  23. #include <linux/serio.h>
  24. #include <linux/errno.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/err.h>
  30. #include <linux/slab.h>
  31. #include <linux/string.h>
  32. #include <linux/kernel.h>
  33. #include <linux/io.h>
  34. struct apbps2_regs {
  35. u32 __iomem data; /* 0x00 */
  36. u32 __iomem status; /* 0x04 */
  37. u32 __iomem ctrl; /* 0x08 */
  38. u32 __iomem reload; /* 0x0c */
  39. };
  40. #define APBPS2_STATUS_DR (1<<0)
  41. #define APBPS2_STATUS_PE (1<<1)
  42. #define APBPS2_STATUS_FE (1<<2)
  43. #define APBPS2_STATUS_KI (1<<3)
  44. #define APBPS2_STATUS_RF (1<<4)
  45. #define APBPS2_STATUS_TF (1<<5)
  46. #define APBPS2_STATUS_TCNT (0x1f<<22)
  47. #define APBPS2_STATUS_RCNT (0x1f<<27)
  48. #define APBPS2_CTRL_RE (1<<0)
  49. #define APBPS2_CTRL_TE (1<<1)
  50. #define APBPS2_CTRL_RI (1<<2)
  51. #define APBPS2_CTRL_TI (1<<3)
  52. struct apbps2_priv {
  53. struct serio *io;
  54. struct apbps2_regs *regs;
  55. };
  56. static int apbps2_idx;
  57. static irqreturn_t apbps2_isr(int irq, void *dev_id)
  58. {
  59. struct apbps2_priv *priv = dev_id;
  60. unsigned long status, data, rxflags;
  61. irqreturn_t ret = IRQ_NONE;
  62. while ((status = ioread32be(&priv->regs->status)) & APBPS2_STATUS_DR) {
  63. data = ioread32be(&priv->regs->data);
  64. rxflags = (status & APBPS2_STATUS_PE) ? SERIO_PARITY : 0;
  65. rxflags |= (status & APBPS2_STATUS_FE) ? SERIO_FRAME : 0;
  66. /* clear error bits? */
  67. if (rxflags)
  68. iowrite32be(0, &priv->regs->status);
  69. serio_interrupt(priv->io, data, rxflags);
  70. ret = IRQ_HANDLED;
  71. }
  72. return ret;
  73. }
  74. static int apbps2_write(struct serio *io, unsigned char val)
  75. {
  76. struct apbps2_priv *priv = io->port_data;
  77. unsigned int tleft = 10000; /* timeout in 100ms */
  78. /* delay until PS/2 controller has room for more chars */
  79. while ((ioread32be(&priv->regs->status) & APBPS2_STATUS_TF) && tleft--)
  80. udelay(10);
  81. if ((ioread32be(&priv->regs->status) & APBPS2_STATUS_TF) == 0) {
  82. iowrite32be(val, &priv->regs->data);
  83. iowrite32be(APBPS2_CTRL_RE | APBPS2_CTRL_RI | APBPS2_CTRL_TE,
  84. &priv->regs->ctrl);
  85. return 0;
  86. }
  87. return -ETIMEDOUT;
  88. }
  89. static int apbps2_open(struct serio *io)
  90. {
  91. struct apbps2_priv *priv = io->port_data;
  92. int limit;
  93. unsigned long tmp;
  94. /* clear error flags */
  95. iowrite32be(0, &priv->regs->status);
  96. /* Clear old data if available (unlikely) */
  97. limit = 1024;
  98. while ((ioread32be(&priv->regs->status) & APBPS2_STATUS_DR) && --limit)
  99. tmp = ioread32be(&priv->regs->data);
  100. /* Enable reciever and it's interrupt */
  101. iowrite32be(APBPS2_CTRL_RE | APBPS2_CTRL_RI, &priv->regs->ctrl);
  102. return 0;
  103. }
  104. static void apbps2_close(struct serio *io)
  105. {
  106. struct apbps2_priv *priv = io->port_data;
  107. /* stop interrupts at PS/2 HW level */
  108. iowrite32be(0, &priv->regs->ctrl);
  109. }
  110. /* Initialize one APBPS2 PS/2 core */
  111. static int apbps2_of_probe(struct platform_device *ofdev)
  112. {
  113. struct apbps2_priv *priv;
  114. int irq, err;
  115. u32 freq_hz;
  116. struct resource *res;
  117. priv = devm_kzalloc(&ofdev->dev, sizeof(*priv), GFP_KERNEL);
  118. if (!priv) {
  119. dev_err(&ofdev->dev, "memory allocation failed\n");
  120. return -ENOMEM;
  121. }
  122. /* Find Device Address */
  123. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  124. priv->regs = devm_request_and_ioremap(&ofdev->dev, res);
  125. if (!priv->regs) {
  126. dev_err(&ofdev->dev, "io-regs mapping failed\n");
  127. return -EBUSY;
  128. }
  129. /* Reset hardware, disable interrupt */
  130. iowrite32be(0, &priv->regs->ctrl);
  131. /* IRQ */
  132. irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  133. err = devm_request_irq(&ofdev->dev, irq, apbps2_isr,
  134. IRQF_SHARED, "apbps2", priv);
  135. if (err) {
  136. dev_err(&ofdev->dev, "request IRQ%d failed\n", irq);
  137. return err;
  138. }
  139. /* Get core frequency */
  140. if (of_property_read_u32(ofdev->dev.of_node, "freq", &freq_hz)) {
  141. dev_err(&ofdev->dev, "unable to get core frequency\n");
  142. return -EINVAL;
  143. }
  144. /* Set reload register to core freq in kHz/10 */
  145. iowrite32be(freq_hz / 10000, &priv->regs->reload);
  146. priv->io = kzalloc(sizeof(struct serio), GFP_KERNEL);
  147. if (!priv->io)
  148. return -ENOMEM;
  149. priv->io->id.type = SERIO_8042;
  150. priv->io->open = apbps2_open;
  151. priv->io->close = apbps2_close;
  152. priv->io->write = apbps2_write;
  153. priv->io->port_data = priv;
  154. strlcpy(priv->io->name, "APBPS2 PS/2", sizeof(priv->io->name));
  155. snprintf(priv->io->phys, sizeof(priv->io->phys),
  156. "apbps2_%d", apbps2_idx++);
  157. dev_info(&ofdev->dev, "irq = %d, base = 0x%p\n", irq, priv->regs);
  158. serio_register_port(priv->io);
  159. platform_set_drvdata(ofdev, priv);
  160. return 0;
  161. }
  162. static int apbps2_of_remove(struct platform_device *of_dev)
  163. {
  164. struct apbps2_priv *priv = platform_get_drvdata(of_dev);
  165. serio_unregister_port(priv->io);
  166. return 0;
  167. }
  168. static struct of_device_id apbps2_of_match[] = {
  169. { .name = "GAISLER_APBPS2", },
  170. { .name = "01_060", },
  171. {}
  172. };
  173. MODULE_DEVICE_TABLE(of, apbps2_of_match);
  174. static struct platform_driver apbps2_of_driver = {
  175. .driver = {
  176. .name = "grlib-apbps2",
  177. .owner = THIS_MODULE,
  178. .of_match_table = apbps2_of_match,
  179. },
  180. .probe = apbps2_of_probe,
  181. .remove = apbps2_of_remove,
  182. };
  183. module_platform_driver(apbps2_of_driver);
  184. MODULE_AUTHOR("Aeroflex Gaisler AB.");
  185. MODULE_DESCRIPTION("GRLIB APBPS2 PS/2 serial I/O");
  186. MODULE_LICENSE("GPL");