xref: /openbmc/linux/arch/sparc/kernel/apc.c (revision 7e7e2f035663c5ceb029bfb9d91e75099b0a5625)
11da177e4SLinus Torvalds /* apc - Driver implementation for power management functions
21da177e4SLinus Torvalds  * of Aurora Personality Chip (APC) on SPARCstation-4/5 and
31da177e4SLinus Torvalds  * derivatives.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2002 Eric Brower (ebrower@usa.net)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/fs.h>
101da177e4SLinus Torvalds #include <linux/errno.h>
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/miscdevice.h>
13ee30d64eSArnd Bergmann #include <linux/smp_lock.h>
141da177e4SLinus Torvalds #include <linux/pm.h>
15*7e7e2f03SDavid S. Miller #include <linux/of.h>
16*7e7e2f03SDavid S. Miller #include <linux/of_device.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <asm/io.h>
191da177e4SLinus Torvalds #include <asm/oplib.h>
201da177e4SLinus Torvalds #include <asm/uaccess.h>
211da177e4SLinus Torvalds #include <asm/auxio.h>
221da177e4SLinus Torvalds #include <asm/apc.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds /* Debugging
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * #define APC_DEBUG_LED
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #define APC_MINOR	MISC_DYNAMIC_MINOR
301da177e4SLinus Torvalds #define APC_OBPNAME	"power-management"
311da177e4SLinus Torvalds #define APC_DEVNAME "apc"
321da177e4SLinus Torvalds 
33*7e7e2f03SDavid S. Miller static u8 __iomem *regs;
341da177e4SLinus Torvalds static int apc_no_idle __initdata = 0;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define apc_readb(offs)		(sbus_readb(regs+offs))
371da177e4SLinus Torvalds #define apc_writeb(val, offs) 	(sbus_writeb(val, regs+offs))
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /* Specify "apc=noidle" on the kernel command line to
401da177e4SLinus Torvalds  * disable APC CPU standby support.  Certain prototype
411da177e4SLinus Torvalds  * systems (SPARCstation-Fox) do not play well with APC
421da177e4SLinus Torvalds  * CPU idle, so disable this if your system has APC and
431da177e4SLinus Torvalds  * crashes randomly.
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds static int __init apc_setup(char *str)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	if(!strncmp(str, "noidle", strlen("noidle"))) {
481da177e4SLinus Torvalds 		apc_no_idle = 1;
491da177e4SLinus Torvalds 		return 1;
501da177e4SLinus Torvalds 	}
511da177e4SLinus Torvalds 	return 0;
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds __setup("apc=", apc_setup);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * CPU idle callback function
571da177e4SLinus Torvalds  * See .../arch/sparc/kernel/process.c
581da177e4SLinus Torvalds  */
59c61c65cdSAdrian Bunk static void apc_swift_idle(void)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds #ifdef APC_DEBUG_LED
621da177e4SLinus Torvalds 	set_auxio(0x00, AUXIO_LED);
631da177e4SLinus Torvalds #endif
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #ifdef APC_DEBUG_LED
681da177e4SLinus Torvalds 	set_auxio(AUXIO_LED, 0x00);
691da177e4SLinus Torvalds #endif
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
72*7e7e2f03SDavid S. Miller static inline void apc_free(struct of_device *op)
731da177e4SLinus Torvalds {
74*7e7e2f03SDavid S. Miller 	of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static int apc_open(struct inode *inode, struct file *f)
781da177e4SLinus Torvalds {
79ee30d64eSArnd Bergmann 	cycle_kernel_lock();
801da177e4SLinus Torvalds 	return 0;
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds static int apc_release(struct inode *inode, struct file *f)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds 	return 0;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
88ab772027SStoyan Gaydarov static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds 	__u8 inarg, __user *arg;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	arg = (__u8 __user *) __arg;
93ab772027SStoyan Gaydarov 
94ab772027SStoyan Gaydarov 	lock_kernel();
95ab772027SStoyan Gaydarov 
961da177e4SLinus Torvalds 	switch (cmd) {
971da177e4SLinus Torvalds 	case APCIOCGFANCTL:
98ab772027SStoyan Gaydarov 		if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg)) {
99ab772027SStoyan Gaydarov 			unlock_kernel();
1001da177e4SLinus Torvalds 			return -EFAULT;
101ab772027SStoyan Gaydarov 		}
1021da177e4SLinus Torvalds 		break;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	case APCIOCGCPWR:
105ab772027SStoyan Gaydarov 		if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg)) {
106ab772027SStoyan Gaydarov 			unlock_kernel();
1071da177e4SLinus Torvalds 			return -EFAULT;
108ab772027SStoyan Gaydarov 		}
1091da177e4SLinus Torvalds 		break;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	case APCIOCGBPORT:
112ab772027SStoyan Gaydarov 		if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg)) {
113ab772027SStoyan Gaydarov 			unlock_kernel();
1141da177e4SLinus Torvalds 			return -EFAULT;
115ab772027SStoyan Gaydarov 		}
1161da177e4SLinus Torvalds 		break;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	case APCIOCSFANCTL:
119ab772027SStoyan Gaydarov 		if (get_user(inarg, arg)) {
120ab772027SStoyan Gaydarov 			unlock_kernel();
1211da177e4SLinus Torvalds 			return -EFAULT;
122ab772027SStoyan Gaydarov 		}
1231da177e4SLinus Torvalds 		apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
1241da177e4SLinus Torvalds 		break;
1251da177e4SLinus Torvalds 	case APCIOCSCPWR:
126ab772027SStoyan Gaydarov 		if (get_user(inarg, arg)) {
127ab772027SStoyan Gaydarov 			unlock_kernel();
1281da177e4SLinus Torvalds 			return -EFAULT;
129ab772027SStoyan Gaydarov 		}
1301da177e4SLinus Torvalds 		apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
1311da177e4SLinus Torvalds 		break;
1321da177e4SLinus Torvalds 	case APCIOCSBPORT:
133ab772027SStoyan Gaydarov 		if (get_user(inarg, arg)) {
134ab772027SStoyan Gaydarov 			unlock_kernel();
1351da177e4SLinus Torvalds 			return -EFAULT;
136ab772027SStoyan Gaydarov 		}
1371da177e4SLinus Torvalds 		apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
1381da177e4SLinus Torvalds 		break;
1391da177e4SLinus Torvalds 	default:
140ab772027SStoyan Gaydarov 		unlock_kernel();
1411da177e4SLinus Torvalds 		return -EINVAL;
1421da177e4SLinus Torvalds 	};
1431da177e4SLinus Torvalds 
144ab772027SStoyan Gaydarov 	unlock_kernel();
1451da177e4SLinus Torvalds 	return 0;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1485dfe4c96SArjan van de Ven static const struct file_operations apc_fops = {
149ab772027SStoyan Gaydarov 	.unlocked_ioctl =	apc_ioctl,
1501da177e4SLinus Torvalds 	.open =			apc_open,
1511da177e4SLinus Torvalds 	.release =		apc_release,
1521da177e4SLinus Torvalds };
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
1551da177e4SLinus Torvalds 
156*7e7e2f03SDavid S. Miller static int __devinit apc_probe(struct of_device *op,
157*7e7e2f03SDavid S. Miller 			       const struct of_device_id *match)
1581da177e4SLinus Torvalds {
159*7e7e2f03SDavid S. Miller 	int err;
1601da177e4SLinus Torvalds 
161*7e7e2f03SDavid S. Miller 	regs = of_ioremap(&op->resource[0], 0,
162*7e7e2f03SDavid S. Miller 			  resource_size(&op->resource[0]), APC_OBPNAME);
1631da177e4SLinus Torvalds 	if (!regs) {
1641da177e4SLinus Torvalds 		printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
1651da177e4SLinus Torvalds 		return -ENODEV;
1661da177e4SLinus Torvalds 	}
1671da177e4SLinus Torvalds 
168*7e7e2f03SDavid S. Miller 	err = misc_register(&apc_miscdev);
169*7e7e2f03SDavid S. Miller 	if (err) {
1701da177e4SLinus Torvalds 		printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
171*7e7e2f03SDavid S. Miller 		apc_free(op);
1721da177e4SLinus Torvalds 		return -ENODEV;
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	/* Assign power management IDLE handler */
1761da177e4SLinus Torvalds 	if (!apc_no_idle)
1771da177e4SLinus Torvalds 		pm_idle = apc_swift_idle;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	printk(KERN_INFO "%s: power management initialized%s\n",
1801da177e4SLinus Torvalds 	       APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
181*7e7e2f03SDavid S. Miller 
1821da177e4SLinus Torvalds 	return 0;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
185*7e7e2f03SDavid S. Miller static struct of_device_id apc_match[] = {
186*7e7e2f03SDavid S. Miller 	{
187*7e7e2f03SDavid S. Miller 		.name = APC_OBPNAME,
188*7e7e2f03SDavid S. Miller 	},
189*7e7e2f03SDavid S. Miller 	{},
190*7e7e2f03SDavid S. Miller };
191*7e7e2f03SDavid S. Miller MODULE_DEVICE_TABLE(of, apc_match);
192*7e7e2f03SDavid S. Miller 
193*7e7e2f03SDavid S. Miller static struct of_platform_driver apc_driver = {
194*7e7e2f03SDavid S. Miller 	.name		= "apc",
195*7e7e2f03SDavid S. Miller 	.match_table	= apc_match,
196*7e7e2f03SDavid S. Miller 	.probe		= apc_probe,
197*7e7e2f03SDavid S. Miller };
198*7e7e2f03SDavid S. Miller 
199*7e7e2f03SDavid S. Miller static int __init apc_init(void)
200*7e7e2f03SDavid S. Miller {
201*7e7e2f03SDavid S. Miller 	return of_register_driver(&apc_driver, &of_bus_type);
202*7e7e2f03SDavid S. Miller }
203*7e7e2f03SDavid S. Miller 
2041da177e4SLinus Torvalds /* This driver is not critical to the boot process
2051da177e4SLinus Torvalds  * and is easiest to ioremap when SBus is already
2061da177e4SLinus Torvalds  * initialized, so we install ourselves thusly:
2071da177e4SLinus Torvalds  */
208*7e7e2f03SDavid S. Miller __initcall(apc_init);
209