apbps2.c 5.5 KB

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