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