blob: eefda32b595ef6ff1344d21ebc664ca4a7f9f0fa [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>
Len Brownd472ba82013-02-09 23:27:26 -050023#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/* Debugging
26 *
27 * #define APC_DEBUG_LED
28 */
29
30#define APC_MINOR MISC_DYNAMIC_MINOR
31#define APC_OBPNAME "power-management"
32#define APC_DEVNAME "apc"
33
David S. Miller7e7e2f02008-08-27 02:45:36 -070034static u8 __iomem *regs;
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -080035static int apc_no_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
David S. Miller7e7e2f02008-08-27 02:45:36 -070037#define apc_readb(offs) (sbus_readb(regs+offs))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define apc_writeb(val, offs) (sbus_writeb(val, regs+offs))
39
40/* Specify "apc=noidle" on the kernel command line to
41 * disable APC CPU standby support. Certain prototype
42 * systems (SPARCstation-Fox) do not play well with APC
43 * CPU idle, so disable this if your system has APC and
44 * crashes randomly.
45 */
46static int __init apc_setup(char *str)
47{
48 if(!strncmp(str, "noidle", strlen("noidle"))) {
49 apc_no_idle = 1;
50 return 1;
51 }
52 return 0;
53}
54__setup("apc=", apc_setup);
55
56/*
57 * CPU idle callback function
58 * See .../arch/sparc/kernel/process.c
59 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -070060static void apc_swift_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62#ifdef APC_DEBUG_LED
63 set_auxio(0x00, AUXIO_LED);
64#endif
65
66 apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
67
68#ifdef APC_DEBUG_LED
69 set_auxio(AUXIO_LED, 0x00);
70#endif
71}
72
Grant Likelycd4cd732010-07-22 16:04:30 -060073static inline void apc_free(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
David S. Miller7e7e2f02008-08-27 02:45:36 -070075 of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78static int apc_open(struct inode *inode, struct file *f)
79{
80 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{
Thomas Gleixner49ab9722009-11-02 21:15:59 -080090 __u8 inarg, __user *arg = (__u8 __user *) __arg;
Stoyan Gaydarovab772022008-07-14 22:12:29 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 switch (cmd) {
93 case APCIOCGFANCTL:
Thomas Gleixner49ab9722009-11-02 21:15:59 -080094 if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
Stoyan Gaydarovab772022008-07-14 22:12:29 -070095 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 break;
97
98 case APCIOCGCPWR:
Thomas Gleixner49ab9722009-11-02 21:15:59 -080099 if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -EFAULT;
101 break;
102
103 case APCIOCGBPORT:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800104 if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EFAULT;
106 break;
107
108 case APCIOCSFANCTL:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800109 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return -EFAULT;
111 apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
112 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 case APCIOCSCPWR:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800115 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return -EFAULT;
117 apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
118 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 case APCIOCSBPORT:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800121 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return -EFAULT;
123 apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
124 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 default:
127 return -EINVAL;
Joe Perches6cb79b32011-06-03 14:45:23 +0000128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 return 0;
131}
132
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800133static const struct file_operations apc_fops = {
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700134 .unlocked_ioctl = apc_ioctl,
135 .open = apc_open,
136 .release = apc_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200137 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
141
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -0800142static int apc_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
David S. Miller7e7e2f02008-08-27 02:45:36 -0700144 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David S. Miller7e7e2f02008-08-27 02:45:36 -0700146 regs = of_ioremap(&op->resource[0], 0,
147 resource_size(&op->resource[0]), APC_OBPNAME);
148 if (!regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
150 return -ENODEV;
151 }
152
David S. Miller7e7e2f02008-08-27 02:45:36 -0700153 err = misc_register(&apc_miscdev);
154 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
David S. Miller7e7e2f02008-08-27 02:45:36 -0700156 apc_free(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -ENODEV;
158 }
159
160 /* Assign power management IDLE handler */
David S. Miller7e7e2f02008-08-27 02:45:36 -0700161 if (!apc_no_idle)
Len Brownd472ba82013-02-09 23:27:26 -0500162 sparc_idle = apc_swift_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 printk(KERN_INFO "%s: power management initialized%s\n",
David S. Miller7e7e2f02008-08-27 02:45:36 -0700165 APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168}
169
Sam Ravnborg505d9142011-04-21 15:37:20 -0700170static struct of_device_id apc_match[] = {
David S. Miller7e7e2f02008-08-27 02:45:36 -0700171 {
172 .name = APC_OBPNAME,
173 },
174 {},
175};
176MODULE_DEVICE_TABLE(of, apc_match);
177
Grant Likely4ebb24f2011-02-22 20:01:33 -0700178static struct platform_driver apc_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700179 .driver = {
180 .name = "apc",
181 .owner = THIS_MODULE,
182 .of_match_table = apc_match,
183 },
David S. Miller7e7e2f02008-08-27 02:45:36 -0700184 .probe = apc_probe,
185};
186
187static int __init apc_init(void)
188{
Grant Likely4ebb24f2011-02-22 20:01:33 -0700189 return platform_driver_register(&apc_driver);
David S. Miller7e7e2f02008-08-27 02:45:36 -0700190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* This driver is not critical to the boot process
193 * and is easiest to ioremap when SBus is already
194 * initialized, so we install ourselves thusly:
195 */
David S. Miller7e7e2f02008-08-27 02:45:36 -0700196__initcall(apc_init);