ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sparc / kernel / apc.c
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
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
15 #include <asm/io.h>
16 #include <asm/sbus.h>
17 #include <asm/oplib.h>
18 #include <asm/uaccess.h>
19 #include <asm/auxio.h>
20 #include <asm/apc.h>
21
22 /* Debugging
23  * 
24  * #define APC_DEBUG_LED
25  */
26
27 #define APC_MINOR       MISC_DYNAMIC_MINOR
28 #define APC_OBPNAME     "power-management"
29 #define APC_DEVNAME "apc"
30
31 volatile static u8 *regs; 
32 static int apc_regsize;
33 static int apc_no_idle __initdata = 0;
34
35 #define apc_readb(offs)                 (sbus_readb(regs+offs))
36 #define apc_writeb(val, offs)   (sbus_writeb(val, regs+offs))
37
38 /* Specify "apc=noidle" on the kernel command line to 
39  * disable APC CPU standby support.  Certain prototype
40  * systems (SPARCstation-Fox) do not play well with APC
41  * CPU idle, so disable this if your system has APC and 
42  * crashes randomly.
43  */
44 static int __init apc_setup(char *str) 
45 {
46         if(!strncmp(str, "noidle", strlen("noidle"))) {
47                 apc_no_idle = 1;
48                 return 1;
49         }
50         return 0;
51 }
52 __setup("apc=", apc_setup);
53
54 /* 
55  * CPU idle callback function
56  * See .../arch/sparc/kernel/process.c
57  */
58 void apc_swift_idle(void)
59 {
60 #ifdef APC_DEBUG_LED
61         set_auxio(0x00, AUXIO_LED); 
62 #endif
63
64         apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
65
66 #ifdef APC_DEBUG_LED
67         set_auxio(AUXIO_LED, 0x00); 
68 #endif
69
70
71 static inline void apc_free(void)
72 {
73         sbus_iounmap((unsigned long)regs, apc_regsize);
74 }
75
76 static int apc_open(struct inode *inode, struct file *f)
77 {
78         return 0;
79 }
80
81 static int apc_release(struct inode *inode, struct file *f)
82 {
83         return 0;
84 }
85
86 static int apc_ioctl(struct inode *inode, struct file *f, 
87                      unsigned int cmd, unsigned long arg)
88 {
89         __u8 inarg;
90
91         switch (cmd) {
92                 case APCIOCGFANCTL:
93                         if(put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, (__u8*) arg)) {
94                                 return -EFAULT;
95                         }
96                         break;
97                 case APCIOCGCPWR:
98                         if(put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, (__u8*) arg)) {
99                                 return -EFAULT;
100                         }
101                         break;
102                 case APCIOCGBPORT:
103                         if(put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, (__u8*) arg)) {
104                                 return -EFAULT;
105                         }
106                         break;
107
108                 case APCIOCSFANCTL:
109                         if(get_user(inarg, (__u8*) arg)) {
110                                 return -EFAULT;
111                         }
112                         apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
113                         break;
114                 case APCIOCSCPWR:
115                         if(get_user(inarg, (__u8*) arg)) {
116                                 return -EFAULT;
117                         }
118                         apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
119                         break;
120                 case APCIOCSBPORT:
121                         if(get_user(inarg, (__u8*) arg)) {
122                                 return -EFAULT;
123                         }
124                         apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
125                         break;
126                 default:
127                         return -EINVAL;
128         };
129
130         return 0;
131 }
132
133 static struct file_operations apc_fops = {
134         .ioctl =        apc_ioctl,
135         .open =         apc_open,
136         .release =      apc_release,
137 };
138
139 static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
140
141 static int __init apc_probe(void)
142 {
143         struct sbus_bus *sbus = NULL;
144         struct sbus_dev *sdev = NULL;
145         int iTmp = 0;
146
147         for_each_sbus(sbus) {
148                 for_each_sbusdev(sdev, sbus) {
149                         if (!strcmp(sdev->prom_name, APC_OBPNAME)) {
150                                 goto sbus_done;
151                         }
152                 }
153         }
154
155 sbus_done:
156         if (!sdev) {
157                 return -ENODEV;
158         }
159
160         apc_regsize = sdev->reg_addrs[0].reg_size;
161         regs = (u8*) sbus_ioremap(&sdev->resource[0], 0, 
162                                    apc_regsize, APC_OBPNAME);
163         if(NULL == regs) {
164                 printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
165                 return -ENODEV;
166         }
167
168         iTmp = misc_register(&apc_miscdev);
169         if (iTmp != 0) {
170                 printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
171                 apc_free();
172                 return -ENODEV;
173         }
174
175         /* Assign power management IDLE handler */
176         if(!apc_no_idle)
177                 pm_idle = apc_swift_idle;       
178
179         printk(KERN_INFO "%s: power management initialized%s\n", 
180                 APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
181         return 0;
182 }
183
184 /* This driver is not critical to the boot process
185  * and is easiest to ioremap when SBus is already
186  * initialized, so we install ourselves thusly:
187  */
188 __initcall(apc_probe);
189