xref: /openbmc/linux/arch/sparc/kernel/apc.c (revision 263291fa44ff0909b5b7c43ff40babc1c43362f2)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /* apc - Driver implementation for power management functions
31da177e4SLinus Torvalds  * of Aurora Personality Chip (APC) on SPARCstation-4/5 and
41da177e4SLinus Torvalds  * derivatives.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (c) 2002 Eric Brower (ebrower@usa.net)
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/kernel.h>
101da177e4SLinus Torvalds #include <linux/fs.h>
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/miscdevice.h>
141da177e4SLinus Torvalds #include <linux/pm.h>
157e7e2f03SDavid S. Miller #include <linux/of.h>
16*263291faSRob Herring #include <linux/platform_device.h>
17f060ac54SPaul Gortmaker #include <linux/module.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <asm/io.h>
201da177e4SLinus Torvalds #include <asm/oplib.h>
217c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
221da177e4SLinus Torvalds #include <asm/auxio.h>
231da177e4SLinus Torvalds #include <asm/apc.h>
24d472ba84SLen Brown #include <asm/processor.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /* Debugging
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * #define APC_DEBUG_LED
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #define APC_MINOR	MISC_DYNAMIC_MINOR
321da177e4SLinus Torvalds #define APC_OBPNAME	"power-management"
331da177e4SLinus Torvalds #define APC_DEVNAME "apc"
341da177e4SLinus Torvalds 
357e7e2f03SDavid S. Miller static u8 __iomem *regs;
367c9503b8SGreg Kroah-Hartman static int apc_no_idle = 0;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define apc_readb(offs)		(sbus_readb(regs+offs))
391da177e4SLinus Torvalds #define apc_writeb(val, offs) 	(sbus_writeb(val, regs+offs))
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /* Specify "apc=noidle" on the kernel command line to
421da177e4SLinus Torvalds  * disable APC CPU standby support.  Certain prototype
431da177e4SLinus Torvalds  * systems (SPARCstation-Fox) do not play well with APC
441da177e4SLinus Torvalds  * CPU idle, so disable this if your system has APC and
451da177e4SLinus Torvalds  * crashes randomly.
461da177e4SLinus Torvalds  */
471da177e4SLinus Torvalds static int __init apc_setup(char *str)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	if(!strncmp(str, "noidle", strlen("noidle"))) {
501da177e4SLinus Torvalds 		apc_no_idle = 1;
511da177e4SLinus Torvalds 		return 1;
521da177e4SLinus Torvalds 	}
531da177e4SLinus Torvalds 	return 0;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds __setup("apc=", apc_setup);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * CPU idle callback function
591da177e4SLinus Torvalds  * See .../arch/sparc/kernel/process.c
601da177e4SLinus Torvalds  */
61c61c65cdSAdrian Bunk static void apc_swift_idle(void)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds #ifdef APC_DEBUG_LED
641da177e4SLinus Torvalds 	set_auxio(0x00, AUXIO_LED);
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #ifdef APC_DEBUG_LED
701da177e4SLinus Torvalds 	set_auxio(AUXIO_LED, 0x00);
711da177e4SLinus Torvalds #endif
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
74cd4cd730SGrant Likely static inline void apc_free(struct platform_device *op)
751da177e4SLinus Torvalds {
767e7e2f03SDavid S. Miller 	of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static int apc_open(struct inode *inode, struct file *f)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	return 0;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds static int apc_release(struct inode *inode, struct file *f)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	return 0;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
89ab772027SStoyan Gaydarov static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
901da177e4SLinus Torvalds {
9149ab972aSThomas Gleixner 	__u8 inarg, __user *arg = (__u8 __user *) __arg;
92ab772027SStoyan Gaydarov 
931da177e4SLinus Torvalds 	switch (cmd) {
941da177e4SLinus Torvalds 	case APCIOCGFANCTL:
9549ab972aSThomas Gleixner 		if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
961da177e4SLinus Torvalds 			return -EFAULT;
971da177e4SLinus Torvalds 		break;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	case APCIOCGCPWR:
10049ab972aSThomas Gleixner 		if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
1011da177e4SLinus Torvalds 			return -EFAULT;
1021da177e4SLinus Torvalds 		break;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	case APCIOCGBPORT:
10549ab972aSThomas Gleixner 		if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
1061da177e4SLinus Torvalds 			return -EFAULT;
1071da177e4SLinus Torvalds 		break;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	case APCIOCSFANCTL:
11049ab972aSThomas Gleixner 		if (get_user(inarg, arg))
1111da177e4SLinus Torvalds 			return -EFAULT;
1121da177e4SLinus Torvalds 		apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
1131da177e4SLinus Torvalds 		break;
11449ab972aSThomas Gleixner 
1151da177e4SLinus Torvalds 	case APCIOCSCPWR:
11649ab972aSThomas Gleixner 		if (get_user(inarg, arg))
1171da177e4SLinus Torvalds 			return -EFAULT;
1181da177e4SLinus Torvalds 		apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
1191da177e4SLinus Torvalds 		break;
12049ab972aSThomas Gleixner 
1211da177e4SLinus Torvalds 	case APCIOCSBPORT:
12249ab972aSThomas Gleixner 		if (get_user(inarg, arg))
1231da177e4SLinus Torvalds 			return -EFAULT;
1241da177e4SLinus Torvalds 		apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
1251da177e4SLinus Torvalds 		break;
12649ab972aSThomas Gleixner 
1271da177e4SLinus Torvalds 	default:
1281da177e4SLinus Torvalds 		return -EINVAL;
1296cb79b3fSJoe Perches 	}
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	return 0;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1345dfe4c96SArjan van de Ven static const struct file_operations apc_fops = {
135ab772027SStoyan Gaydarov 	.unlocked_ioctl =	apc_ioctl,
1361da177e4SLinus Torvalds 	.open =			apc_open,
1371da177e4SLinus Torvalds 	.release =		apc_release,
1386038f373SArnd Bergmann 	.llseek =		noop_llseek,
1391da177e4SLinus Torvalds };
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
1421da177e4SLinus Torvalds 
1437c9503b8SGreg Kroah-Hartman static int apc_probe(struct platform_device *op)
1441da177e4SLinus Torvalds {
1457e7e2f03SDavid S. Miller 	int err;
1461da177e4SLinus Torvalds 
1477e7e2f03SDavid S. Miller 	regs = of_ioremap(&op->resource[0], 0,
1487e7e2f03SDavid S. Miller 			  resource_size(&op->resource[0]), APC_OBPNAME);
1491da177e4SLinus Torvalds 	if (!regs) {
1501da177e4SLinus Torvalds 		printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
1511da177e4SLinus Torvalds 		return -ENODEV;
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 
1547e7e2f03SDavid S. Miller 	err = misc_register(&apc_miscdev);
1557e7e2f03SDavid S. Miller 	if (err) {
1561da177e4SLinus Torvalds 		printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
1577e7e2f03SDavid S. Miller 		apc_free(op);
1581da177e4SLinus Torvalds 		return -ENODEV;
1591da177e4SLinus Torvalds 	}
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/* Assign power management IDLE handler */
1621da177e4SLinus Torvalds 	if (!apc_no_idle)
163d472ba84SLen Brown 		sparc_idle = apc_swift_idle;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	printk(KERN_INFO "%s: power management initialized%s\n",
1661da177e4SLinus Torvalds 	       APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
1677e7e2f03SDavid S. Miller 
1681da177e4SLinus Torvalds 	return 0;
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
17169f57978SArvind Yadav static const struct of_device_id apc_match[] = {
1727e7e2f03SDavid S. Miller 	{
1737e7e2f03SDavid S. Miller 		.name = APC_OBPNAME,
1747e7e2f03SDavid S. Miller 	},
1757e7e2f03SDavid S. Miller 	{},
1767e7e2f03SDavid S. Miller };
1777e7e2f03SDavid S. Miller MODULE_DEVICE_TABLE(of, apc_match);
1787e7e2f03SDavid S. Miller 
1794ebb24f7SGrant Likely static struct platform_driver apc_driver = {
1804018294bSGrant Likely 	.driver = {
1817e7e2f03SDavid S. Miller 		.name = "apc",
1824018294bSGrant Likely 		.of_match_table = apc_match,
1834018294bSGrant Likely 	},
1847e7e2f03SDavid S. Miller 	.probe		= apc_probe,
1857e7e2f03SDavid S. Miller };
1867e7e2f03SDavid S. Miller 
1877e7e2f03SDavid S. Miller static int __init apc_init(void)
1887e7e2f03SDavid S. Miller {
1894ebb24f7SGrant Likely 	return platform_driver_register(&apc_driver);
1907e7e2f03SDavid S. Miller }
1917e7e2f03SDavid S. Miller 
1921da177e4SLinus Torvalds /* This driver is not critical to the boot process
1931da177e4SLinus Torvalds  * and is easiest to ioremap when SBus is already
1941da177e4SLinus Torvalds  * initialized, so we install ourselves thusly:
1951da177e4SLinus Torvalds  */
1967e7e2f03SDavid S. Miller __initcall(apc_init);
197