arc_ps2.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Driver is originally developed by Pavel Sokolov <psokolov@synopsys.com>
  9. */
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/input.h>
  14. #include <linux/serio.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #define ARC_PS2_PORTS 2
  21. #define ARC_ARC_PS2_ID 0x0001f609
  22. #define STAT_TIMEOUT 128
  23. #define PS2_STAT_RX_FRM_ERR (1)
  24. #define PS2_STAT_RX_BUF_OVER (1 << 1)
  25. #define PS2_STAT_RX_INT_EN (1 << 2)
  26. #define PS2_STAT_RX_VAL (1 << 3)
  27. #define PS2_STAT_TX_ISNOT_FUL (1 << 4)
  28. #define PS2_STAT_TX_INT_EN (1 << 5)
  29. struct arc_ps2_port {
  30. void __iomem *data_addr;
  31. void __iomem *status_addr;
  32. struct serio *io;
  33. };
  34. struct arc_ps2_data {
  35. struct arc_ps2_port port[ARC_PS2_PORTS];
  36. void __iomem *addr;
  37. unsigned int frame_error;
  38. unsigned int buf_overflow;
  39. unsigned int total_int;
  40. };
  41. static void arc_ps2_check_rx(struct arc_ps2_data *arc_ps2,
  42. struct arc_ps2_port *port)
  43. {
  44. unsigned int timeout = 1000;
  45. unsigned int flag, status;
  46. unsigned char data;
  47. do {
  48. status = ioread32(port->status_addr);
  49. if (!(status & PS2_STAT_RX_VAL))
  50. return;
  51. data = ioread32(port->data_addr) & 0xff;
  52. flag = 0;
  53. arc_ps2->total_int++;
  54. if (status & PS2_STAT_RX_FRM_ERR) {
  55. arc_ps2->frame_error++;
  56. flag |= SERIO_PARITY;
  57. } else if (status & PS2_STAT_RX_BUF_OVER) {
  58. arc_ps2->buf_overflow++;
  59. flag |= SERIO_FRAME;
  60. }
  61. serio_interrupt(port->io, data, flag);
  62. } while (--timeout);
  63. dev_err(&port->io->dev, "PS/2 hardware stuck\n");
  64. }
  65. static irqreturn_t arc_ps2_interrupt(int irq, void *dev)
  66. {
  67. struct arc_ps2_data *arc_ps2 = dev;
  68. int i;
  69. for (i = 0; i < ARC_PS2_PORTS; i++)
  70. arc_ps2_check_rx(arc_ps2, &arc_ps2->port[i]);
  71. return IRQ_HANDLED;
  72. }
  73. static int arc_ps2_write(struct serio *io, unsigned char val)
  74. {
  75. unsigned status;
  76. struct arc_ps2_port *port = io->port_data;
  77. int timeout = STAT_TIMEOUT;
  78. do {
  79. status = ioread32(port->status_addr);
  80. cpu_relax();
  81. if (status & PS2_STAT_TX_ISNOT_FUL) {
  82. iowrite32(val & 0xff, port->data_addr);
  83. return 0;
  84. }
  85. } while (--timeout);
  86. dev_err(&io->dev, "write timeout\n");
  87. return -ETIMEDOUT;
  88. }
  89. static int arc_ps2_open(struct serio *io)
  90. {
  91. struct arc_ps2_port *port = io->port_data;
  92. iowrite32(PS2_STAT_RX_INT_EN, port->status_addr);
  93. return 0;
  94. }
  95. static void arc_ps2_close(struct serio *io)
  96. {
  97. struct arc_ps2_port *port = io->port_data;
  98. iowrite32(ioread32(port->status_addr) & ~PS2_STAT_RX_INT_EN,
  99. port->status_addr);
  100. }
  101. static void __iomem *arc_ps2_calc_addr(struct arc_ps2_data *arc_ps2,
  102. int index, bool status)
  103. {
  104. void __iomem *addr;
  105. addr = arc_ps2->addr + 4 + 4 * index;
  106. if (status)
  107. addr += ARC_PS2_PORTS * 4;
  108. return addr;
  109. }
  110. static void arc_ps2_inhibit_ports(struct arc_ps2_data *arc_ps2)
  111. {
  112. void __iomem *addr;
  113. u32 val;
  114. int i;
  115. for (i = 0; i < ARC_PS2_PORTS; i++) {
  116. addr = arc_ps2_calc_addr(arc_ps2, i, true);
  117. val = ioread32(addr);
  118. val &= ~(PS2_STAT_RX_INT_EN | PS2_STAT_TX_INT_EN);
  119. iowrite32(val, addr);
  120. }
  121. }
  122. static int arc_ps2_create_port(struct platform_device *pdev,
  123. struct arc_ps2_data *arc_ps2,
  124. int index)
  125. {
  126. struct arc_ps2_port *port = &arc_ps2->port[index];
  127. struct serio *io;
  128. io = kzalloc(sizeof(struct serio), GFP_KERNEL);
  129. if (!io)
  130. return -ENOMEM;
  131. io->id.type = SERIO_8042;
  132. io->write = arc_ps2_write;
  133. io->open = arc_ps2_open;
  134. io->close = arc_ps2_close;
  135. snprintf(io->name, sizeof(io->name), "ARC PS/2 port%d", index);
  136. snprintf(io->phys, sizeof(io->phys), "arc/serio%d", index);
  137. io->port_data = port;
  138. port->io = io;
  139. port->data_addr = arc_ps2_calc_addr(arc_ps2, index, false);
  140. port->status_addr = arc_ps2_calc_addr(arc_ps2, index, true);
  141. dev_dbg(&pdev->dev, "port%d is allocated (data = 0x%p, status = 0x%p)\n",
  142. index, port->data_addr, port->status_addr);
  143. serio_register_port(port->io);
  144. return 0;
  145. }
  146. static int arc_ps2_probe(struct platform_device *pdev)
  147. {
  148. struct arc_ps2_data *arc_ps2;
  149. struct resource *res;
  150. int irq;
  151. int error, id, i;
  152. irq = platform_get_irq_byname(pdev, "arc_ps2_irq");
  153. if (irq < 0) {
  154. dev_err(&pdev->dev, "no IRQ defined\n");
  155. return -EINVAL;
  156. }
  157. arc_ps2 = devm_kzalloc(&pdev->dev, sizeof(struct arc_ps2_data),
  158. GFP_KERNEL);
  159. if (!arc_ps2) {
  160. dev_err(&pdev->dev, "out of memory\n");
  161. return -ENOMEM;
  162. }
  163. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  164. arc_ps2->addr = devm_ioremap_resource(&pdev->dev, res);
  165. if (IS_ERR(arc_ps2->addr))
  166. return PTR_ERR(arc_ps2->addr);
  167. dev_info(&pdev->dev, "irq = %d, address = 0x%p, ports = %i\n",
  168. irq, arc_ps2->addr, ARC_PS2_PORTS);
  169. id = ioread32(arc_ps2->addr);
  170. if (id != ARC_ARC_PS2_ID) {
  171. dev_err(&pdev->dev, "device id does not match\n");
  172. return -ENXIO;
  173. }
  174. arc_ps2_inhibit_ports(arc_ps2);
  175. error = devm_request_irq(&pdev->dev, irq, arc_ps2_interrupt,
  176. 0, "arc_ps2", arc_ps2);
  177. if (error) {
  178. dev_err(&pdev->dev, "Could not allocate IRQ\n");
  179. return error;
  180. }
  181. for (i = 0; i < ARC_PS2_PORTS; i++) {
  182. error = arc_ps2_create_port(pdev, arc_ps2, i);
  183. if (error) {
  184. while (--i >= 0)
  185. serio_unregister_port(arc_ps2->port[i].io);
  186. return error;
  187. }
  188. }
  189. platform_set_drvdata(pdev, arc_ps2);
  190. return 0;
  191. }
  192. static int arc_ps2_remove(struct platform_device *pdev)
  193. {
  194. struct arc_ps2_data *arc_ps2 = platform_get_drvdata(pdev);
  195. int i;
  196. for (i = 0; i < ARC_PS2_PORTS; i++)
  197. serio_unregister_port(arc_ps2->port[i].io);
  198. dev_dbg(&pdev->dev, "interrupt count = %i\n", arc_ps2->total_int);
  199. dev_dbg(&pdev->dev, "frame error count = %i\n", arc_ps2->frame_error);
  200. dev_dbg(&pdev->dev, "buffer overflow count = %i\n",
  201. arc_ps2->buf_overflow);
  202. return 0;
  203. }
  204. #ifdef CONFIG_OF
  205. static const struct of_device_id arc_ps2_match[] = {
  206. { .compatible = "snps,arc_ps2" },
  207. { },
  208. };
  209. MODULE_DEVICE_TABLE(of, arc_ps2_match);
  210. #endif
  211. static struct platform_driver arc_ps2_driver = {
  212. .driver = {
  213. .name = "arc_ps2",
  214. .of_match_table = of_match_ptr(arc_ps2_match),
  215. },
  216. .probe = arc_ps2_probe,
  217. .remove = arc_ps2_remove,
  218. };
  219. module_platform_driver(arc_ps2_driver);
  220. MODULE_LICENSE("GPL");
  221. MODULE_AUTHOR("Pavel Sokolov <psokolov@synopsys.com>");
  222. MODULE_DESCRIPTION("ARC PS/2 Driver");