xref: /openbmc/linux/arch/sparc/kernel/apc.c (revision 4ebb24f707187196937607c60810d42f7112d7aa)
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>
131da177e4SLinus Torvalds #include <linux/pm.h>
147e7e2f03SDavid S. Miller #include <linux/of.h>
157e7e2f03SDavid S. Miller #include <linux/of_device.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <asm/io.h>
181da177e4SLinus Torvalds #include <asm/oplib.h>
191da177e4SLinus Torvalds #include <asm/uaccess.h>
201da177e4SLinus Torvalds #include <asm/auxio.h>
211da177e4SLinus Torvalds #include <asm/apc.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /* Debugging
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * #define APC_DEBUG_LED
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #define APC_MINOR	MISC_DYNAMIC_MINOR
291da177e4SLinus Torvalds #define APC_OBPNAME	"power-management"
301da177e4SLinus Torvalds #define APC_DEVNAME "apc"
311da177e4SLinus Torvalds 
327e7e2f03SDavid S. Miller static u8 __iomem *regs;
33a1731e5bSFrederic Weisbecker static int apc_no_idle __devinitdata = 0;
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define apc_readb(offs)		(sbus_readb(regs+offs))
361da177e4SLinus Torvalds #define apc_writeb(val, offs) 	(sbus_writeb(val, regs+offs))
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /* Specify "apc=noidle" on the kernel command line to
391da177e4SLinus Torvalds  * disable APC CPU standby support.  Certain prototype
401da177e4SLinus Torvalds  * systems (SPARCstation-Fox) do not play well with APC
411da177e4SLinus Torvalds  * CPU idle, so disable this if your system has APC and
421da177e4SLinus Torvalds  * crashes randomly.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds static int __init apc_setup(char *str)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	if(!strncmp(str, "noidle", strlen("noidle"))) {
471da177e4SLinus Torvalds 		apc_no_idle = 1;
481da177e4SLinus Torvalds 		return 1;
491da177e4SLinus Torvalds 	}
501da177e4SLinus Torvalds 	return 0;
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds __setup("apc=", apc_setup);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  * CPU idle callback function
561da177e4SLinus Torvalds  * See .../arch/sparc/kernel/process.c
571da177e4SLinus Torvalds  */
58c61c65cdSAdrian Bunk static void apc_swift_idle(void)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds #ifdef APC_DEBUG_LED
611da177e4SLinus Torvalds 	set_auxio(0x00, AUXIO_LED);
621da177e4SLinus Torvalds #endif
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #ifdef APC_DEBUG_LED
671da177e4SLinus Torvalds 	set_auxio(AUXIO_LED, 0x00);
681da177e4SLinus Torvalds #endif
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
71cd4cd730SGrant Likely static inline void apc_free(struct platform_device *op)
721da177e4SLinus Torvalds {
737e7e2f03SDavid S. Miller 	of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static int apc_open(struct inode *inode, struct file *f)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	return 0;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds static int apc_release(struct inode *inode, struct file *f)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	return 0;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
86ab772027SStoyan Gaydarov static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
871da177e4SLinus Torvalds {
8849ab972aSThomas Gleixner 	__u8 inarg, __user *arg = (__u8 __user *) __arg;
89ab772027SStoyan Gaydarov 
901da177e4SLinus Torvalds 	switch (cmd) {
911da177e4SLinus Torvalds 	case APCIOCGFANCTL:
9249ab972aSThomas Gleixner 		if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
931da177e4SLinus Torvalds 			return -EFAULT;
941da177e4SLinus Torvalds 		break;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	case APCIOCGCPWR:
9749ab972aSThomas Gleixner 		if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
981da177e4SLinus Torvalds 			return -EFAULT;
991da177e4SLinus Torvalds 		break;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	case APCIOCGBPORT:
10249ab972aSThomas Gleixner 		if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
1031da177e4SLinus Torvalds 			return -EFAULT;
1041da177e4SLinus Torvalds 		break;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	case APCIOCSFANCTL:
10749ab972aSThomas Gleixner 		if (get_user(inarg, arg))
1081da177e4SLinus Torvalds 			return -EFAULT;
1091da177e4SLinus Torvalds 		apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
1101da177e4SLinus Torvalds 		break;
11149ab972aSThomas Gleixner 
1121da177e4SLinus Torvalds 	case APCIOCSCPWR:
11349ab972aSThomas Gleixner 		if (get_user(inarg, arg))
1141da177e4SLinus Torvalds 			return -EFAULT;
1151da177e4SLinus Torvalds 		apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
1161da177e4SLinus Torvalds 		break;
11749ab972aSThomas Gleixner 
1181da177e4SLinus Torvalds 	case APCIOCSBPORT:
11949ab972aSThomas Gleixner 		if (get_user(inarg, arg))
1201da177e4SLinus Torvalds 			return -EFAULT;
1211da177e4SLinus Torvalds 		apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
1221da177e4SLinus Torvalds 		break;
12349ab972aSThomas Gleixner 
1241da177e4SLinus Torvalds 	default:
1251da177e4SLinus Torvalds 		return -EINVAL;
1261da177e4SLinus Torvalds 	};
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	return 0;
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1315dfe4c96SArjan van de Ven static const struct file_operations apc_fops = {
132ab772027SStoyan Gaydarov 	.unlocked_ioctl =	apc_ioctl,
1331da177e4SLinus Torvalds 	.open =			apc_open,
1341da177e4SLinus Torvalds 	.release =		apc_release,
1356038f373SArnd Bergmann 	.llseek =		noop_llseek,
1361da177e4SLinus Torvalds };
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
1391da177e4SLinus Torvalds 
140*4ebb24f7SGrant Likely static int __devinit apc_probe(struct platform_device *op)
1411da177e4SLinus Torvalds {
1427e7e2f03SDavid S. Miller 	int err;
1431da177e4SLinus Torvalds 
1447e7e2f03SDavid S. Miller 	regs = of_ioremap(&op->resource[0], 0,
1457e7e2f03SDavid S. Miller 			  resource_size(&op->resource[0]), APC_OBPNAME);
1461da177e4SLinus Torvalds 	if (!regs) {
1471da177e4SLinus Torvalds 		printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
1481da177e4SLinus Torvalds 		return -ENODEV;
1491da177e4SLinus Torvalds 	}
1501da177e4SLinus Torvalds 
1517e7e2f03SDavid S. Miller 	err = misc_register(&apc_miscdev);
1527e7e2f03SDavid S. Miller 	if (err) {
1531da177e4SLinus Torvalds 		printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
1547e7e2f03SDavid S. Miller 		apc_free(op);
1551da177e4SLinus Torvalds 		return -ENODEV;
1561da177e4SLinus Torvalds 	}
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	/* Assign power management IDLE handler */
1591da177e4SLinus Torvalds 	if (!apc_no_idle)
1601da177e4SLinus Torvalds 		pm_idle = apc_swift_idle;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	printk(KERN_INFO "%s: power management initialized%s\n",
1631da177e4SLinus Torvalds 	       APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
1647e7e2f03SDavid S. Miller 
1651da177e4SLinus Torvalds 	return 0;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
168fd098316SDavid S. Miller static struct of_device_id __initdata apc_match[] = {
1697e7e2f03SDavid S. Miller 	{
1707e7e2f03SDavid S. Miller 		.name = APC_OBPNAME,
1717e7e2f03SDavid S. Miller 	},
1727e7e2f03SDavid S. Miller 	{},
1737e7e2f03SDavid S. Miller };
1747e7e2f03SDavid S. Miller MODULE_DEVICE_TABLE(of, apc_match);
1757e7e2f03SDavid S. Miller 
176*4ebb24f7SGrant Likely static struct platform_driver apc_driver = {
1774018294bSGrant Likely 	.driver = {
1787e7e2f03SDavid S. Miller 		.name = "apc",
1794018294bSGrant Likely 		.owner = THIS_MODULE,
1804018294bSGrant Likely 		.of_match_table = apc_match,
1814018294bSGrant Likely 	},
1827e7e2f03SDavid S. Miller 	.probe		= apc_probe,
1837e7e2f03SDavid S. Miller };
1847e7e2f03SDavid S. Miller 
1857e7e2f03SDavid S. Miller static int __init apc_init(void)
1867e7e2f03SDavid S. Miller {
187*4ebb24f7SGrant Likely 	return platform_driver_register(&apc_driver);
1887e7e2f03SDavid S. Miller }
1897e7e2f03SDavid S. Miller 
1901da177e4SLinus Torvalds /* This driver is not critical to the boot process
1911da177e4SLinus Torvalds  * and is easiest to ioremap when SBus is already
1921da177e4SLinus Torvalds  * initialized, so we install ourselves thusly:
1931da177e4SLinus Torvalds  */
1947e7e2f03SDavid S. Miller __initcall(apc_init);
195