apc.c 4.1 KB

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