Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* apc - Driver implementation for power management functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * of Aurora Personality Chip (APC) on SPARCstation-4/5 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * derivatives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2002 Eric Brower (ebrower@usa.net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/oplib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/auxio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/apc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) /* Debugging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * #define APC_DEBUG_LED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define APC_MINOR	MISC_DYNAMIC_MINOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define APC_OBPNAME	"power-management"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define APC_DEVNAME "apc"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static u8 __iomem *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static int apc_no_idle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define apc_readb(offs)		(sbus_readb(regs+offs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define apc_writeb(val, offs) 	(sbus_writeb(val, regs+offs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /* Specify "apc=noidle" on the kernel command line to 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * disable APC CPU standby support.  Certain prototype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * systems (SPARCstation-Fox) do not play well with APC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * CPU idle, so disable this if your system has APC and 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * crashes randomly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int __init apc_setup(char *str) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if(!strncmp(str, "noidle", strlen("noidle"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		apc_no_idle = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) __setup("apc=", apc_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /* 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * CPU idle callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * See .../arch/sparc/kernel/process.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static void apc_swift_idle(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #ifdef APC_DEBUG_LED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	set_auxio(0x00, AUXIO_LED); 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #ifdef APC_DEBUG_LED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	set_auxio(AUXIO_LED, 0x00); 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) } 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static inline void apc_free(struct platform_device *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static int apc_open(struct inode *inode, struct file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int apc_release(struct inode *inode, struct file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	__u8 inarg, __user *arg = (__u8 __user *) __arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	case APCIOCGFANCTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	case APCIOCGCPWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	case APCIOCGBPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	case APCIOCSFANCTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		if (get_user(inarg, arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	case APCIOCSCPWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		if (get_user(inarg, arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	case APCIOCSBPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (get_user(inarg, arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static const struct file_operations apc_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.unlocked_ioctl =	apc_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	.open =			apc_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	.release =		apc_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	.llseek =		noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int apc_probe(struct platform_device *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	regs = of_ioremap(&op->resource[0], 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			  resource_size(&op->resource[0]), APC_OBPNAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	err = misc_register(&apc_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		apc_free(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	/* Assign power management IDLE handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (!apc_no_idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		sparc_idle = apc_swift_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	printk(KERN_INFO "%s: power management initialized%s\n", 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	       APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static const struct of_device_id apc_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		.name = APC_OBPNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODULE_DEVICE_TABLE(of, apc_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static struct platform_driver apc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		.name = "apc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		.of_match_table = apc_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.probe		= apc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int __init apc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return platform_driver_register(&apc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* This driver is not critical to the boot process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * and is easiest to ioremap when SBus is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * initialized, so we install ourselves thusly:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) __initcall(apc_init);