maceps2.c 5.4 KB

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