blob: 812e10bbb0b332cf648f747884f93d1b3bc22073 [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>
13#include <linux/pm.h>
David S. Miller7e7e2f02008-08-27 02:45:36 -070014#include <linux/of.h>
15#include <linux/of_device.h>
Paul Gortmakerf060ac52011-07-31 01:50:31 -040016#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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. Miller7e7e2f02008-08-27 02:45:36 -070033static u8 __iomem *regs;
Frederic Weisbeckera1731e52008-10-21 21:55:33 -070034static int apc_no_idle __devinitdata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David S. Miller7e7e2f02008-08-27 02:45:36 -070036#define apc_readb(offs) (sbus_readb(regs+offs))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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
Grant Likelycd4cd732010-07-22 16:04:30 -060072static inline void apc_free(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
David S. Miller7e7e2f02008-08-27 02:45:36 -070074 of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static int apc_open(struct inode *inode, struct file *f)
78{
79 return 0;
80}
81
82static int apc_release(struct inode *inode, struct file *f)
83{
84 return 0;
85}
86
Stoyan Gaydarovab772022008-07-14 22:12:29 -070087static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Thomas Gleixner49ab9722009-11-02 21:15:59 -080089 __u8 inarg, __user *arg = (__u8 __user *) __arg;
Stoyan Gaydarovab772022008-07-14 22:12:29 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 switch (cmd) {
92 case APCIOCGFANCTL:
Thomas Gleixner49ab9722009-11-02 21:15:59 -080093 if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
Stoyan Gaydarovab772022008-07-14 22:12:29 -070094 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 break;
96
97 case APCIOCGCPWR:
Thomas Gleixner49ab9722009-11-02 21:15:59 -080098 if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EFAULT;
100 break;
101
102 case APCIOCGBPORT:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800103 if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -EFAULT;
105 break;
106
107 case APCIOCSFANCTL:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800108 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -EFAULT;
110 apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
111 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 case APCIOCSCPWR:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800114 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -EFAULT;
116 apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
117 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 case APCIOCSBPORT:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800120 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return -EFAULT;
122 apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
123 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 default:
126 return -EINVAL;
Joe Perches6cb79b32011-06-03 14:45:23 +0000127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 return 0;
130}
131
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800132static const struct file_operations apc_fops = {
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700133 .unlocked_ioctl = apc_ioctl,
134 .open = apc_open,
135 .release = apc_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200136 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
140
Grant Likely4ebb24f2011-02-22 20:01:33 -0700141static int __devinit apc_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
David S. Miller7e7e2f02008-08-27 02:45:36 -0700143 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David S. Miller7e7e2f02008-08-27 02:45:36 -0700145 regs = of_ioremap(&op->resource[0], 0,
146 resource_size(&op->resource[0]), APC_OBPNAME);
147 if (!regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
149 return -ENODEV;
150 }
151
David S. Miller7e7e2f02008-08-27 02:45:36 -0700152 err = misc_register(&apc_miscdev);
153 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
David S. Miller7e7e2f02008-08-27 02:45:36 -0700155 apc_free(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -ENODEV;
157 }
158
159 /* Assign power management IDLE handler */
David S. Miller7e7e2f02008-08-27 02:45:36 -0700160 if (!apc_no_idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 pm_idle = apc_swift_idle;
162
163 printk(KERN_INFO "%s: power management initialized%s\n",
David S. Miller7e7e2f02008-08-27 02:45:36 -0700164 APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
167}
168
Sam Ravnborg505d9142011-04-21 15:37:20 -0700169static struct of_device_id apc_match[] = {
David S. Miller7e7e2f02008-08-27 02:45:36 -0700170 {
171 .name = APC_OBPNAME,
172 },
173 {},
174};
175MODULE_DEVICE_TABLE(of, apc_match);
176
Grant Likely4ebb24f2011-02-22 20:01:33 -0700177static struct platform_driver apc_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700178 .driver = {
179 .name = "apc",
180 .owner = THIS_MODULE,
181 .of_match_table = apc_match,
182 },
David S. Miller7e7e2f02008-08-27 02:45:36 -0700183 .probe = apc_probe,
184};
185
186static int __init apc_init(void)
187{
Grant Likely4ebb24f2011-02-22 20:01:33 -0700188 return platform_driver_register(&apc_driver);
David S. Miller7e7e2f02008-08-27 02:45:36 -0700189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* 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. Miller7e7e2f02008-08-27 02:45:36 -0700195__initcall(apc_init);