maceps2.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * SGI O2 MACE PS2 controller driver for linux
  3. *
  4. * Copyright (C) 2002 Vivien Chappelier
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/serio.h>
  13. #include <linux/errno.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/delay.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/err.h>
  21. #include <asm/io.h>
  22. #include <asm/irq.h>
  23. #include <asm/ip32/mace.h>
  24. #include <asm/ip32/ip32_ints.h>
  25. MODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org");
  26. MODULE_DESCRIPTION("SGI O2 MACE PS2 controller driver");
  27. MODULE_LICENSE("GPL");
  28. #define MACE_PS2_TIMEOUT 10000 /* in 50us unit */
  29. #define PS2_STATUS_CLOCK_SIGNAL BIT(0) /* external clock signal */
  30. #define PS2_STATUS_CLOCK_INHIBIT BIT(1) /* clken output signal */
  31. #define PS2_STATUS_TX_INPROGRESS BIT(2) /* transmission in progress */
  32. #define PS2_STATUS_TX_EMPTY BIT(3) /* empty transmit buffer */
  33. #define PS2_STATUS_RX_FULL BIT(4) /* full receive buffer */
  34. #define PS2_STATUS_RX_INPROGRESS BIT(5) /* reception in progress */
  35. #define PS2_STATUS_ERROR_PARITY BIT(6) /* parity error */
  36. #define PS2_STATUS_ERROR_FRAMING BIT(7) /* framing error */
  37. #define PS2_CONTROL_TX_CLOCK_DISABLE BIT(0) /* inhibit clock signal after TX */
  38. #define PS2_CONTROL_TX_ENABLE BIT(1) /* transmit enable */
  39. #define PS2_CONTROL_TX_INT_ENABLE BIT(2) /* enable transmit interrupt */
  40. #define PS2_CONTROL_RX_INT_ENABLE BIT(3) /* enable receive interrupt */
  41. #define PS2_CONTROL_RX_CLOCK_ENABLE BIT(4) /* pause reception if set to 0 */
  42. #define PS2_CONTROL_RESET BIT(5) /* reset */
  43. struct maceps2_data {
  44. struct mace_ps2port *port;
  45. int irq;
  46. };
  47. static struct maceps2_data port_data[2];
  48. static struct serio *maceps2_port[2];
  49. static struct platform_device *maceps2_device;
  50. static int maceps2_write(struct serio *dev, unsigned char val)
  51. {
  52. struct mace_ps2port *port = ((struct maceps2_data *)dev->port_data)->port;
  53. unsigned int timeout = MACE_PS2_TIMEOUT;
  54. do {
  55. if (port->status & PS2_STATUS_TX_EMPTY) {
  56. port->tx = val;
  57. return 0;
  58. }
  59. udelay(50);
  60. } while (timeout--);
  61. return -1;
  62. }
  63. static irqreturn_t maceps2_interrupt(int irq, void *dev_id)
  64. {
  65. struct serio *dev = dev_id;
  66. struct mace_ps2port *port = ((struct maceps2_data *)dev->port_data)->port;
  67. unsigned long byte;
  68. if (port->status & PS2_STATUS_RX_FULL) {
  69. byte = port->rx;
  70. serio_interrupt(dev, byte & 0xff, 0);
  71. }
  72. return IRQ_HANDLED;
  73. }
  74. static int maceps2_open(struct serio *dev)
  75. {
  76. struct maceps2_data *data = (struct maceps2_data *)dev->port_data;
  77. if (request_irq(data->irq, maceps2_interrupt, 0, "PS2 port", dev)) {
  78. printk(KERN_ERR "Could not allocate PS/2 IRQ\n");
  79. return -EBUSY;
  80. }
  81. /* Reset port */
  82. data->port->control = PS2_CONTROL_TX_CLOCK_DISABLE | PS2_CONTROL_RESET;
  83. udelay(100);
  84. /* Enable interrupts */
  85. data->port->control = PS2_CONTROL_RX_CLOCK_ENABLE |
  86. PS2_CONTROL_TX_ENABLE |
  87. PS2_CONTROL_RX_INT_ENABLE;
  88. return 0;
  89. }
  90. static void maceps2_close(struct serio *dev)
  91. {
  92. struct maceps2_data *data = (struct maceps2_data *)dev->port_data;
  93. data->port->control = PS2_CONTROL_TX_CLOCK_DISABLE | PS2_CONTROL_RESET;
  94. udelay(100);
  95. free_irq(data->irq, dev);
  96. }
  97. static struct serio *maceps2_allocate_port(int idx)
  98. {
  99. struct serio *serio;
  100. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  101. if (serio) {
  102. serio->id.type = SERIO_8042;
  103. serio->write = maceps2_write;
  104. serio->open = maceps2_open;
  105. serio->close = maceps2_close;
  106. snprintf(serio->name, sizeof(serio->name), "MACE PS/2 port%d", idx);
  107. snprintf(serio->phys, sizeof(serio->phys), "mace/serio%d", idx);
  108. serio->port_data = &port_data[idx];
  109. serio->dev.parent = &maceps2_device->dev;
  110. }
  111. return serio;
  112. }
  113. static int maceps2_probe(struct platform_device *dev)
  114. {
  115. maceps2_port[0] = maceps2_allocate_port(0);
  116. maceps2_port[1] = maceps2_allocate_port(1);
  117. if (!maceps2_port[0] || !maceps2_port[1]) {
  118. kfree(maceps2_port[0]);
  119. kfree(maceps2_port[1]);
  120. return -ENOMEM;
  121. }
  122. serio_register_port(maceps2_port[0]);
  123. serio_register_port(maceps2_port[1]);
  124. return 0;
  125. }
  126. static int maceps2_remove(struct platform_device *dev)
  127. {
  128. serio_unregister_port(maceps2_port[0]);
  129. serio_unregister_port(maceps2_port[1]);
  130. return 0;
  131. }
  132. static struct platform_driver maceps2_driver = {
  133. .driver = {
  134. .name = "maceps2",
  135. },
  136. .probe = maceps2_probe,
  137. .remove = maceps2_remove,
  138. };
  139. static int __init maceps2_init(void)
  140. {
  141. int error;
  142. error = platform_driver_register(&maceps2_driver);
  143. if (error)
  144. return error;
  145. maceps2_device = platform_device_alloc("maceps2", -1);
  146. if (!maceps2_device) {
  147. error = -ENOMEM;
  148. goto err_unregister_driver;
  149. }
  150. port_data[0].port = &mace->perif.ps2.keyb;
  151. port_data[0].irq = MACEISA_KEYB_IRQ;
  152. port_data[1].port = &mace->perif.ps2.mouse;
  153. port_data[1].irq = MACEISA_MOUSE_IRQ;
  154. error = platform_device_add(maceps2_device);
  155. if (error)
  156. goto err_free_device;
  157. return 0;
  158. err_free_device:
  159. platform_device_put(maceps2_device);
  160. err_unregister_driver:
  161. platform_driver_unregister(&maceps2_driver);
  162. return error;
  163. }
  164. static void __exit maceps2_exit(void)
  165. {
  166. platform_device_unregister(maceps2_device);
  167. platform_driver_unregister(&maceps2_driver);
  168. }
  169. module_init(maceps2_init);
  170. module_exit(maceps2_exit);