apc.c 4.1 KB

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