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. /* 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 platform_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. return 0;
  68. }
  69. static int apc_release(struct inode *inode, struct file *f)
  70. {
  71. return 0;
  72. }
  73. static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
  74. {
  75. __u8 inarg, __user *arg = (__u8 __user *) __arg;
  76. switch (cmd) {
  77. case APCIOCGFANCTL:
  78. if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
  79. return -EFAULT;
  80. break;
  81. case APCIOCGCPWR:
  82. if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
  83. return -EFAULT;
  84. break;
  85. case APCIOCGBPORT:
  86. if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
  87. return -EFAULT;
  88. break;
  89. case APCIOCSFANCTL:
  90. if (get_user(inarg, arg))
  91. return -EFAULT;
  92. apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
  93. break;
  94. case APCIOCSCPWR:
  95. if (get_user(inarg, arg))
  96. return -EFAULT;
  97. apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
  98. break;
  99. case APCIOCSBPORT:
  100. if (get_user(inarg, arg))
  101. return -EFAULT;
  102. apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
  103. break;
  104. default:
  105. return -EINVAL;
  106. }
  107. return 0;
  108. }
  109. static const struct file_operations apc_fops = {
  110. .unlocked_ioctl = apc_ioctl,
  111. .open = apc_open,
  112. .release = apc_release,
  113. .llseek = noop_llseek,
  114. };
  115. static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
  116. static int __devinit apc_probe(struct platform_device *op)
  117. {
  118. int err;
  119. regs = of_ioremap(&op->resource[0], 0,
  120. resource_size(&op->resource[0]), APC_OBPNAME);
  121. if (!regs) {
  122. printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
  123. return -ENODEV;
  124. }
  125. err = misc_register(&apc_miscdev);
  126. if (err) {
  127. printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
  128. apc_free(op);
  129. return -ENODEV;
  130. }
  131. /* Assign power management IDLE handler */
  132. if (!apc_no_idle)
  133. pm_idle = apc_swift_idle;
  134. printk(KERN_INFO "%s: power management initialized%s\n",
  135. APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
  136. return 0;
  137. }
  138. static struct of_device_id apc_match[] = {
  139. {
  140. .name = APC_OBPNAME,
  141. },
  142. {},
  143. };
  144. MODULE_DEVICE_TABLE(of, apc_match);
  145. static struct platform_driver apc_driver = {
  146. .driver = {
  147. .name = "apc",
  148. .owner = THIS_MODULE,
  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);