apc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <asm/io.h>
  15. #include <asm/sbus.h>
  16. #include <asm/oplib.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/auxio.h>
  19. #include <asm/apc.h>
  20. /* Debugging
  21. *
  22. * #define APC_DEBUG_LED
  23. */
  24. #define APC_MINOR MISC_DYNAMIC_MINOR
  25. #define APC_OBPNAME "power-management"
  26. #define APC_DEVNAME "apc"
  27. volatile static u8 __iomem *regs;
  28. static int apc_regsize;
  29. static int apc_no_idle __initdata = 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(void)
  62. {
  63. sbus_iounmap(regs, apc_regsize);
  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 int apc_ioctl(struct inode *inode, struct file *f,
  75. unsigned int cmd, unsigned long __arg)
  76. {
  77. __u8 inarg, __user *arg;
  78. arg = (__u8 __user *) __arg;
  79. switch (cmd) {
  80. case APCIOCGFANCTL:
  81. if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
  82. return -EFAULT;
  83. break;
  84. case APCIOCGCPWR:
  85. if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
  86. return -EFAULT;
  87. break;
  88. case APCIOCGBPORT:
  89. if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
  90. return -EFAULT;
  91. break;
  92. case APCIOCSFANCTL:
  93. if (get_user(inarg, arg))
  94. return -EFAULT;
  95. apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
  96. break;
  97. case APCIOCSCPWR:
  98. if (get_user(inarg, arg))
  99. return -EFAULT;
  100. apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
  101. break;
  102. case APCIOCSBPORT:
  103. if (get_user(inarg, arg))
  104. return -EFAULT;
  105. apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
  106. break;
  107. default:
  108. return -EINVAL;
  109. };
  110. return 0;
  111. }
  112. static const struct file_operations apc_fops = {
  113. .ioctl = apc_ioctl,
  114. .open = apc_open,
  115. .release = apc_release,
  116. };
  117. static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
  118. static int __init apc_probe(void)
  119. {
  120. struct sbus_bus *sbus = NULL;
  121. struct sbus_dev *sdev = NULL;
  122. int iTmp = 0;
  123. for_each_sbus(sbus) {
  124. for_each_sbusdev(sdev, sbus) {
  125. if (!strcmp(sdev->prom_name, APC_OBPNAME)) {
  126. goto sbus_done;
  127. }
  128. }
  129. }
  130. sbus_done:
  131. if (!sdev) {
  132. return -ENODEV;
  133. }
  134. apc_regsize = sdev->reg_addrs[0].reg_size;
  135. regs = sbus_ioremap(&sdev->resource[0], 0,
  136. apc_regsize, APC_OBPNAME);
  137. if(!regs) {
  138. printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
  139. return -ENODEV;
  140. }
  141. iTmp = misc_register(&apc_miscdev);
  142. if (iTmp != 0) {
  143. printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
  144. apc_free();
  145. return -ENODEV;
  146. }
  147. /* Assign power management IDLE handler */
  148. if(!apc_no_idle)
  149. pm_idle = apc_swift_idle;
  150. printk(KERN_INFO "%s: power management initialized%s\n",
  151. APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
  152. return 0;
  153. }
  154. /* This driver is not critical to the boot process
  155. * and is easiest to ioremap when SBus is already
  156. * initialized, so we install ourselves thusly:
  157. */
  158. __initcall(apc_probe);