apc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/pm.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/module.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. #include <asm/processor.h>
  22. /* Debugging
  23. *
  24. * #define APC_DEBUG_LED
  25. */
  26. #define APC_MINOR MISC_DYNAMIC_MINOR
  27. #define APC_OBPNAME "power-management"
  28. #define APC_DEVNAME "apc"
  29. static u8 __iomem *regs;
  30. static int apc_no_idle = 0;
  31. #define apc_readb(offs) (sbus_readb(regs+offs))
  32. #define apc_writeb(val, offs) (sbus_writeb(val, regs+offs))
  33. /* Specify "apc=noidle" on the kernel command line to
  34. * disable APC CPU standby support. Certain prototype
  35. * systems (SPARCstation-Fox) do not play well with APC
  36. * CPU idle, so disable this if your system has APC and
  37. * crashes randomly.
  38. */
  39. static int __init apc_setup(char *str)
  40. {
  41. if(!strncmp(str, "noidle", strlen("noidle"))) {
  42. apc_no_idle = 1;
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. __setup("apc=", apc_setup);
  48. /*
  49. * CPU idle callback function
  50. * See .../arch/sparc/kernel/process.c
  51. */
  52. static void apc_swift_idle(void)
  53. {
  54. #ifdef APC_DEBUG_LED
  55. set_auxio(0x00, AUXIO_LED);
  56. #endif
  57. apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
  58. #ifdef APC_DEBUG_LED
  59. set_auxio(AUXIO_LED, 0x00);
  60. #endif
  61. }
  62. static inline void apc_free(struct platform_device *op)
  63. {
  64. of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
  65. }
  66. static int apc_open(struct inode *inode, struct file *f)
  67. {
  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 = (__u8 __user *) __arg;
  77. switch (cmd) {
  78. case APCIOCGFANCTL:
  79. if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
  80. return -EFAULT;
  81. break;
  82. case APCIOCGCPWR:
  83. if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
  84. return -EFAULT;
  85. break;
  86. case APCIOCGBPORT:
  87. if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
  88. return -EFAULT;
  89. break;
  90. case APCIOCSFANCTL:
  91. if (get_user(inarg, arg))
  92. return -EFAULT;
  93. apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
  94. break;
  95. case APCIOCSCPWR:
  96. if (get_user(inarg, arg))
  97. return -EFAULT;
  98. apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
  99. break;
  100. case APCIOCSBPORT:
  101. if (get_user(inarg, arg))
  102. return -EFAULT;
  103. apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. return 0;
  109. }
  110. static const struct file_operations apc_fops = {
  111. .unlocked_ioctl = apc_ioctl,
  112. .open = apc_open,
  113. .release = apc_release,
  114. .llseek = noop_llseek,
  115. };
  116. static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
  117. static int apc_probe(struct platform_device *op)
  118. {
  119. int err;
  120. regs = of_ioremap(&op->resource[0], 0,
  121. resource_size(&op->resource[0]), APC_OBPNAME);
  122. if (!regs) {
  123. printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
  124. return -ENODEV;
  125. }
  126. err = misc_register(&apc_miscdev);
  127. if (err) {
  128. printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
  129. apc_free(op);
  130. return -ENODEV;
  131. }
  132. /* Assign power management IDLE handler */
  133. if (!apc_no_idle)
  134. sparc_idle = apc_swift_idle;
  135. printk(KERN_INFO "%s: power management initialized%s\n",
  136. APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
  137. return 0;
  138. }
  139. static struct of_device_id apc_match[] = {
  140. {
  141. .name = APC_OBPNAME,
  142. },
  143. {},
  144. };
  145. MODULE_DEVICE_TABLE(of, apc_match);
  146. static struct platform_driver apc_driver = {
  147. .driver = {
  148. .name = "apc",
  149. .of_match_table = apc_match,
  150. },
  151. .probe = apc_probe,
  152. };
  153. static int __init apc_init(void)
  154. {
  155. return platform_driver_register(&apc_driver);
  156. }
  157. /* This driver is not critical to the boot process
  158. * and is easiest to ioremap when SBus is already
  159. * initialized, so we install ourselves thusly:
  160. */
  161. __initcall(apc_init);