blob: 5267d48fb2c6a36e5a777f4b51041c58944d95bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
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>
Arnd Bergmannee30d642008-05-20 19:16:48 +020013#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/pm.h>
15
16#include <asm/io.h>
17#include <asm/sbus.h>
18#include <asm/oplib.h>
19#include <asm/uaccess.h>
20#include <asm/auxio.h>
21#include <asm/apc.h>
22
23/* Debugging
24 *
25 * #define APC_DEBUG_LED
26 */
27
28#define APC_MINOR MISC_DYNAMIC_MINOR
29#define APC_OBPNAME "power-management"
30#define APC_DEVNAME "apc"
31
32volatile static u8 __iomem *regs;
33static int apc_regsize;
34static int apc_no_idle __initdata = 0;
35
36#define apc_readb(offs) (sbus_readb(regs+offs))
37#define apc_writeb(val, offs) (sbus_writeb(val, regs+offs))
38
39/* Specify "apc=noidle" on the kernel command line to
40 * disable APC CPU standby support. Certain prototype
41 * systems (SPARCstation-Fox) do not play well with APC
42 * CPU idle, so disable this if your system has APC and
43 * crashes randomly.
44 */
45static int __init apc_setup(char *str)
46{
47 if(!strncmp(str, "noidle", strlen("noidle"))) {
48 apc_no_idle = 1;
49 return 1;
50 }
51 return 0;
52}
53__setup("apc=", apc_setup);
54
55/*
56 * CPU idle callback function
57 * See .../arch/sparc/kernel/process.c
58 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -070059static void apc_swift_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61#ifdef APC_DEBUG_LED
62 set_auxio(0x00, AUXIO_LED);
63#endif
64
65 apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
66
67#ifdef APC_DEBUG_LED
68 set_auxio(AUXIO_LED, 0x00);
69#endif
70}
71
72static inline void apc_free(void)
73{
74 sbus_iounmap(regs, apc_regsize);
75}
76
77static int apc_open(struct inode *inode, struct file *f)
78{
Arnd Bergmannee30d642008-05-20 19:16:48 +020079 cycle_kernel_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return 0;
81}
82
83static int apc_release(struct inode *inode, struct file *f)
84{
85 return 0;
86}
87
Stoyan Gaydarovab772022008-07-14 22:12:29 -070088static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 __u8 inarg, __user *arg;
91
92 arg = (__u8 __user *) __arg;
Stoyan Gaydarovab772022008-07-14 22:12:29 -070093
94 lock_kernel();
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 switch (cmd) {
97 case APCIOCGFANCTL:
Stoyan Gaydarovab772022008-07-14 22:12:29 -070098 if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg)) {
99 unlock_kernel();
100 return -EFAULT;
101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 break;
103
104 case APCIOCGCPWR:
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700105 if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg)) {
106 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -EFAULT;
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
110
111 case APCIOCGBPORT:
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700112 if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg)) {
113 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EFAULT;
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 break;
117
118 case APCIOCSFANCTL:
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700119 if (get_user(inarg, arg)) {
120 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return -EFAULT;
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
124 break;
125 case APCIOCSCPWR:
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700126 if (get_user(inarg, arg)) {
127 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -EFAULT;
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
131 break;
132 case APCIOCSBPORT:
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700133 if (get_user(inarg, arg)) {
134 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -EFAULT;
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
138 break;
139 default:
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700140 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EINVAL;
142 };
143
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700144 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146}
147
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800148static const struct file_operations apc_fops = {
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700149 .unlocked_ioctl = apc_ioctl,
150 .open = apc_open,
151 .release = apc_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
155
156static int __init apc_probe(void)
157{
158 struct sbus_bus *sbus = NULL;
159 struct sbus_dev *sdev = NULL;
160 int iTmp = 0;
161
162 for_each_sbus(sbus) {
163 for_each_sbusdev(sdev, sbus) {
164 if (!strcmp(sdev->prom_name, APC_OBPNAME)) {
165 goto sbus_done;
166 }
167 }
168 }
169
170sbus_done:
171 if (!sdev) {
172 return -ENODEV;
173 }
174
175 apc_regsize = sdev->reg_addrs[0].reg_size;
176 regs = sbus_ioremap(&sdev->resource[0], 0,
177 apc_regsize, APC_OBPNAME);
178 if(!regs) {
179 printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
180 return -ENODEV;
181 }
182
183 iTmp = misc_register(&apc_miscdev);
184 if (iTmp != 0) {
185 printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
186 apc_free();
187 return -ENODEV;
188 }
189
190 /* Assign power management IDLE handler */
191 if(!apc_no_idle)
192 pm_idle = apc_swift_idle;
193
194 printk(KERN_INFO "%s: power management initialized%s\n",
195 APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
196 return 0;
197}
198
199/* This driver is not critical to the boot process
200 * and is easiest to ioremap when SBus is already
201 * initialized, so we install ourselves thusly:
202 */
203__initcall(apc_probe);
204