| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Arnd Bergmann | ee30d64 | 2008-05-20 19:16:48 +0200 | [diff] [blame] | 13 | #include <linux/smp_lock.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/pm.h> | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 15 | #include <linux/of.h> | 
|  | 16 | #include <linux/of_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 |  | 
|  | 18 | #include <asm/io.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/oplib.h> | 
|  | 20 | #include <asm/uaccess.h> | 
|  | 21 | #include <asm/auxio.h> | 
|  | 22 | #include <asm/apc.h> | 
|  | 23 |  | 
|  | 24 | /* Debugging | 
|  | 25 | * | 
|  | 26 | * #define APC_DEBUG_LED | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #define APC_MINOR	MISC_DYNAMIC_MINOR | 
|  | 30 | #define APC_OBPNAME	"power-management" | 
|  | 31 | #define APC_DEVNAME "apc" | 
|  | 32 |  | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 33 | static u8 __iomem *regs; | 
| Frederic Weisbecker | a1731e5 | 2008-10-21 21:55:33 -0700 | [diff] [blame] | 34 | static int apc_no_idle __devinitdata = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 36 | #define apc_readb(offs)		(sbus_readb(regs+offs)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ | 
|  | 45 | static 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 Bunk | c61c65c | 2008-06-05 11:40:58 -0700 | [diff] [blame] | 59 | static void apc_swift_idle(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { | 
|  | 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 |  | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 72 | static inline void apc_free(struct of_device *op) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 74 | of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
|  | 77 | static int apc_open(struct inode *inode, struct file *f) | 
|  | 78 | { | 
| Arnd Bergmann | ee30d64 | 2008-05-20 19:16:48 +0200 | [diff] [blame] | 79 | cycle_kernel_lock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return 0; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | static int apc_release(struct inode *inode, struct file *f) | 
|  | 84 | { | 
|  | 85 | return 0; | 
|  | 86 | } | 
|  | 87 |  | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 88 | static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { | 
|  | 90 | __u8 inarg, __user *arg; | 
|  | 91 |  | 
|  | 92 | arg = (__u8 __user *) __arg; | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 93 |  | 
|  | 94 | lock_kernel(); | 
|  | 95 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | switch (cmd) { | 
|  | 97 | case APCIOCGFANCTL: | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 98 | if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg)) { | 
|  | 99 | unlock_kernel(); | 
|  | 100 | return -EFAULT; | 
|  | 101 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | break; | 
|  | 103 |  | 
|  | 104 | case APCIOCGCPWR: | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 105 | if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg)) { | 
|  | 106 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return -EFAULT; | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 108 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | break; | 
|  | 110 |  | 
|  | 111 | case APCIOCGBPORT: | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 112 | if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg)) { | 
|  | 113 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return -EFAULT; | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 115 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | break; | 
|  | 117 |  | 
|  | 118 | case APCIOCSFANCTL: | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 119 | if (get_user(inarg, arg)) { | 
|  | 120 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return -EFAULT; | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 122 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG); | 
|  | 124 | break; | 
|  | 125 | case APCIOCSCPWR: | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 126 | if (get_user(inarg, arg)) { | 
|  | 127 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return -EFAULT; | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 129 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG); | 
|  | 131 | break; | 
|  | 132 | case APCIOCSBPORT: | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 133 | if (get_user(inarg, arg)) { | 
|  | 134 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return -EFAULT; | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 136 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG); | 
|  | 138 | break; | 
|  | 139 | default: | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 140 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return -EINVAL; | 
|  | 142 | }; | 
|  | 143 |  | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 144 | unlock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return 0; | 
|  | 146 | } | 
|  | 147 |  | 
| Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 148 | static const struct file_operations apc_fops = { | 
| Stoyan Gaydarov | ab77202 | 2008-07-14 22:12:29 -0700 | [diff] [blame] | 149 | .unlocked_ioctl =	apc_ioctl, | 
|  | 150 | .open =			apc_open, | 
|  | 151 | .release =		apc_release, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | }; | 
|  | 153 |  | 
|  | 154 | static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops }; | 
|  | 155 |  | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 156 | static int __devinit apc_probe(struct of_device *op, | 
|  | 157 | const struct of_device_id *match) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 159 | int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 |  | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 161 | regs = of_ioremap(&op->resource[0], 0, | 
|  | 162 | resource_size(&op->resource[0]), APC_OBPNAME); | 
|  | 163 | if (!regs) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME); | 
|  | 165 | return -ENODEV; | 
|  | 166 | } | 
|  | 167 |  | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 168 | err = misc_register(&apc_miscdev); | 
|  | 169 | if (err) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME); | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 171 | apc_free(op); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return -ENODEV; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | /* Assign power management IDLE handler */ | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 176 | if (!apc_no_idle) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | pm_idle = apc_swift_idle; | 
|  | 178 |  | 
|  | 179 | printk(KERN_INFO "%s: power management initialized%s\n", | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 180 | APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : ""); | 
|  | 181 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return 0; | 
|  | 183 | } | 
|  | 184 |  | 
| David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 185 | static struct of_device_id __initdata apc_match[] = { | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 186 | { | 
|  | 187 | .name = APC_OBPNAME, | 
|  | 188 | }, | 
|  | 189 | {}, | 
|  | 190 | }; | 
|  | 191 | MODULE_DEVICE_TABLE(of, apc_match); | 
|  | 192 |  | 
|  | 193 | static struct of_platform_driver apc_driver = { | 
|  | 194 | .name		= "apc", | 
|  | 195 | .match_table	= apc_match, | 
|  | 196 | .probe		= apc_probe, | 
|  | 197 | }; | 
|  | 198 |  | 
|  | 199 | static int __init apc_init(void) | 
|  | 200 | { | 
|  | 201 | return of_register_driver(&apc_driver, &of_bus_type); | 
|  | 202 | } | 
|  | 203 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | /* This driver is not critical to the boot process | 
|  | 205 | * and is easiest to ioremap when SBus is already | 
|  | 206 | * initialized, so we install ourselves thusly: | 
|  | 207 | */ | 
| David S. Miller | 7e7e2f0 | 2008-08-27 02:45:36 -0700 | [diff] [blame] | 208 | __initcall(apc_init); |