maceps2.c 5.5 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. struct pt_regs *regs)
  66. {
  67. struct serio *dev = dev_id;
  68. struct mace_ps2port *port = ((struct maceps2_data *)dev->port_data)->port;
  69. unsigned long byte;
  70. if (port->status & PS2_STATUS_RX_FULL) {
  71. byte = port->rx;
  72. serio_interrupt(dev, byte & 0xff, 0, regs);
  73. }
  74. return IRQ_HANDLED;
  75. }
  76. static int maceps2_open(struct serio *dev)
  77. {
  78. struct maceps2_data *data = (struct maceps2_data *)dev->port_data;
  79. if (request_irq(data->irq, maceps2_interrupt, 0, "PS2 port", dev)) {
  80. printk(KERN_ERR "Could not allocate PS/2 IRQ\n");
  81. return -EBUSY;
  82. }
  83. /* Reset port */
  84. data->port->control = PS2_CONTROL_TX_CLOCK_DISABLE | PS2_CONTROL_RESET;
  85. udelay(100);
  86. /* Enable interrupts */
  87. data->port->control = PS2_CONTROL_RX_CLOCK_ENABLE |
  88. PS2_CONTROL_TX_ENABLE |
  89. PS2_CONTROL_RX_INT_ENABLE;
  90. return 0;
  91. }
  92. static void maceps2_close(struct serio *dev)
  93. {
  94. struct maceps2_data *data = (struct maceps2_data *)dev->port_data;
  95. data->port->control = PS2_CONTROL_TX_CLOCK_DISABLE | PS2_CONTROL_RESET;
  96. udelay(100);
  97. free_irq(data->irq, dev);
  98. }
  99. static struct serio * __devinit maceps2_allocate_port(int idx)
  100. {
  101. struct serio *serio;
  102. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  103. if (serio) {
  104. serio->id.type = SERIO_8042;
  105. serio->write = maceps2_write;
  106. serio->open = maceps2_open;
  107. serio->close = maceps2_close;
  108. snprintf(serio->name, sizeof(serio->name), "MACE PS/2 port%d", idx);
  109. snprintf(serio->phys, sizeof(serio->phys), "mace/serio%d", idx);
  110. serio->port_data = &port_data[idx];
  111. serio->dev.parent = &maceps2_device->dev;
  112. }
  113. return serio;
  114. }
  115. static int __devinit maceps2_probe(struct platform_device *dev)
  116. {
  117. maceps2_port[0] = maceps2_allocate_port(0);
  118. maceps2_port[1] = maceps2_allocate_port(1);
  119. if (!maceps2_port[0] || !maceps2_port[1]) {
  120. kfree(maceps2_port[0]);
  121. kfree(maceps2_port[1]);
  122. return -ENOMEM;
  123. }
  124. serio_register_port(maceps2_port[0]);
  125. serio_register_port(maceps2_port[1]);
  126. return 0;
  127. }
  128. static int __devexit maceps2_remove(struct platform_device *dev)
  129. {
  130. serio_unregister_port(maceps2_port[0]);
  131. serio_unregister_port(maceps2_port[1]);
  132. return 0;
  133. }
  134. static struct platform_driver maceps2_driver = {
  135. .driver = {
  136. .name = "maceps2",
  137. .owner = THIS_MODULE,
  138. },
  139. .probe = maceps2_probe,
  140. .remove = __devexit_p(maceps2_remove),
  141. };
  142. static int __init maceps2_init(void)
  143. {
  144. int error;
  145. error = platform_driver_register(&maceps2_driver);
  146. if (error)
  147. return error;
  148. maceps2_device = platform_device_alloc("maceps2", -1);
  149. if (!maceps2_device) {
  150. error = -ENOMEM;
  151. goto err_unregister_driver;
  152. }
  153. port_data[0].port = &mace->perif.ps2.keyb;
  154. port_data[0].irq = MACEISA_KEYB_IRQ;
  155. port_data[1].port = &mace->perif.ps2.mouse;
  156. port_data[1].irq = MACEISA_MOUSE_IRQ;
  157. error = platform_device_add(maceps2_device);
  158. if (error)
  159. goto err_free_device;
  160. return 0;
  161. err_free_device:
  162. platform_device_put(maceps2_device);
  163. err_unregister_driver:
  164. platform_driver_unregister(&maceps2_driver);
  165. return error;
  166. }
  167. static void __exit maceps2_exit(void)
  168. {
  169. platform_device_unregister(maceps2_device);
  170. platform_driver_unregister(&maceps2_driver);
  171. }
  172. module_init(maceps2_init);
  173. module_exit(maceps2_exit);