apc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* apc - Driver implementation for power management functions
  2. * of Aurora Personality Chip (APC) on SPARCstation-4/5 and
  3. * derivatives.
  4. *
  5. * Copyright (c) 2002 Eric Brower (ebrower@usa.net)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/fs.h>
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/pm.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <asm/io.h>
  17. #include <asm/oplib.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/auxio.h>
  20. #include <asm/apc.h>
  21. /* Debugging
  22. *
  23. * #define APC_DEBUG_LED
  24. */
  25. #define APC_MINOR MISC_DYNAMIC_MINOR
  26. #define APC_OBPNAME "power-management"
  27. #define APC_DEVNAME "apc"
  28. static u8 __iomem *regs;
  29. static int apc_no_idle __devinitdata = 0;
  30. #define apc_readb(offs) (sbus_readb(regs+offs))
  31. #define apc_writeb(val, offs) (sbus_writeb(val, regs+offs))
  32. /* Specify "apc=noidle" on the kernel command line to
  33. * disable APC CPU standby support. Certain prototype
  34. * systems (SPARCstation-Fox) do not play well with APC
  35. * CPU idle, so disable this if your system has APC and
  36. * crashes randomly.
  37. */
  38. static int __init apc_setup(char *str)
  39. {
  40. if(!strncmp(str, "noidle", strlen("noidle"))) {
  41. apc_no_idle = 1;
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. __setup("apc=", apc_setup);
  47. /*
  48. * CPU idle callback function
  49. * See .../arch/sparc/kernel/process.c
  50. */
  51. static void apc_swift_idle(void)
  52. {
  53. #ifdef APC_DEBUG_LED
  54. set_auxio(0x00, AUXIO_LED);
  55. #endif
  56. apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
  57. #ifdef APC_DEBUG_LED
  58. set_auxio(AUXIO_LED, 0x00);
  59. #endif
  60. }
  61. static inline void apc_free(struct of_device *op)
  62. {
  63. of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
  64. }
  65. static int apc_open(struct inode *inode, struct file *f)
  66. {
  67. cycle_kernel_lock();
  68. return 0;
  69. }
  70. static int apc_release(struct inode *inode, struct file *f)
  71. {
  72. return 0;
  73. }
  74. static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
  75. {
  76. __u8 inarg, __user *arg;
  77. arg = (__u8 __user *) __arg;
  78. lock_kernel();
  79. switch (cmd) {
  80. case APCIOCGFANCTL:
  81. if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg)) {
  82. unlock_kernel();
  83. return -EFAULT;
  84. }
  85. break;
  86. case APCIOCGCPWR:
  87. if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg)) {
  88. unlock_kernel();
  89. return -EFAULT;
  90. }
  91. break;
  92. case APCIOCGBPORT:
  93. if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg)) {
  94. unlock_kernel();
  95. return -EFAULT;
  96. }
  97. break;
  98. case APCIOCSFANCTL:
  99. if (get_user(inarg, arg)) {
  100. unlock_kernel();
  101. return -EFAULT;
  102. }
  103. apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
  104. break;
  105. case APCIOCSCPWR:
  106. if (get_user(inarg, arg)) {
  107. unlock_kernel();
  108. return -EFAULT;
  109. }
  110. apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
  111. break;
  112. case APCIOCSBPORT:
  113. if (get_user(inarg, arg)) {
  114. unlock_kernel();
  115. return -EFAULT;
  116. }
  117. apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
  118. break;
  119. default:
  120. unlock_kernel();
  121. return -EINVAL;
  122. };
  123. unlock_kernel();
  124. return 0;
  125. }
  126. static const struct file_operations apc_fops = {
  127. .unlocked_ioctl = apc_ioctl,
  128. .open = apc_open,
  129. .release = apc_release,
  130. };
  131. static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
  132. static int __devinit apc_probe(struct of_device *op,
  133. const struct of_device_id *match)
  134. {
  135. int err;
  136. regs = of_ioremap(&op->resource[0], 0,
  137. resource_size(&op->resource[0]), APC_OBPNAME);
  138. if (!regs) {
  139. printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
  140. return -ENODEV;
  141. }
  142. err = misc_register(&apc_miscdev);
  143. if (err) {
  144. printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
  145. apc_free(op);
  146. return -ENODEV;
  147. }
  148. /* Assign power management IDLE handler */
  149. if (!apc_no_idle)
  150. pm_idle = apc_swift_idle;
  151. printk(KERN_INFO "%s: power management initialized%s\n",
  152. APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
  153. return 0;
  154. }
  155. static struct of_device_id __initdata apc_match[] = {
  156. {
  157. .name = APC_OBPNAME,
  158. },
  159. {},
  160. };
  161. MODULE_DEVICE_TABLE(of, apc_match);
  162. static struct of_platform_driver apc_driver = {
  163. .name = "apc",
  164. .match_table = apc_match,
  165. .probe = apc_probe,
  166. };
  167. static int __init apc_init(void)
  168. {
  169. return of_register_driver(&apc_driver, &of_bus_type);
  170. }
  171. /* This driver is not critical to the boot process
  172. * and is easiest to ioremap when SBus is already
  173. * initialized, so we install ourselves thusly:
  174. */
  175. __initcall(apc_init);