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> 16f060ac54SPaul Gortmaker #include <linux/module.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <asm/io.h> 191da177e4SLinus Torvalds #include <asm/oplib.h> 207c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 211da177e4SLinus Torvalds #include <asm/auxio.h> 221da177e4SLinus Torvalds #include <asm/apc.h> 23d472ba84SLen Brown #include <asm/processor.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds /* Debugging 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * #define APC_DEBUG_LED 281da177e4SLinus Torvalds */ 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #define APC_MINOR MISC_DYNAMIC_MINOR 311da177e4SLinus Torvalds #define APC_OBPNAME "power-management" 321da177e4SLinus Torvalds #define APC_DEVNAME "apc" 331da177e4SLinus Torvalds 347e7e2f03SDavid S. Miller static u8 __iomem *regs; 357c9503b8SGreg Kroah-Hartman static int apc_no_idle = 0; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #define apc_readb(offs) (sbus_readb(regs+offs)) 381da177e4SLinus Torvalds #define apc_writeb(val, offs) (sbus_writeb(val, regs+offs)) 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds /* Specify "apc=noidle" on the kernel command line to 411da177e4SLinus Torvalds * disable APC CPU standby support. Certain prototype 421da177e4SLinus Torvalds * systems (SPARCstation-Fox) do not play well with APC 431da177e4SLinus Torvalds * CPU idle, so disable this if your system has APC and 441da177e4SLinus Torvalds * crashes randomly. 451da177e4SLinus Torvalds */ 461da177e4SLinus Torvalds static int __init apc_setup(char *str) 471da177e4SLinus Torvalds { 481da177e4SLinus Torvalds if(!strncmp(str, "noidle", strlen("noidle"))) { 491da177e4SLinus Torvalds apc_no_idle = 1; 501da177e4SLinus Torvalds return 1; 511da177e4SLinus Torvalds } 521da177e4SLinus Torvalds return 0; 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds __setup("apc=", apc_setup); 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* 571da177e4SLinus Torvalds * CPU idle callback function 581da177e4SLinus Torvalds * See .../arch/sparc/kernel/process.c 591da177e4SLinus Torvalds */ 60c61c65cdSAdrian Bunk static void apc_swift_idle(void) 611da177e4SLinus Torvalds { 621da177e4SLinus Torvalds #ifdef APC_DEBUG_LED 631da177e4SLinus Torvalds set_auxio(0x00, AUXIO_LED); 641da177e4SLinus Torvalds #endif 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG); 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #ifdef APC_DEBUG_LED 691da177e4SLinus Torvalds set_auxio(AUXIO_LED, 0x00); 701da177e4SLinus Torvalds #endif 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds 73cd4cd730SGrant Likely static inline void apc_free(struct platform_device *op) 741da177e4SLinus Torvalds { 757e7e2f03SDavid S. Miller of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0])); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds static int apc_open(struct inode *inode, struct file *f) 791da177e4SLinus Torvalds { 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 { 9049ab972aSThomas Gleixner __u8 inarg, __user *arg = (__u8 __user *) __arg; 91ab772027SStoyan Gaydarov 921da177e4SLinus Torvalds switch (cmd) { 931da177e4SLinus Torvalds case APCIOCGFANCTL: 9449ab972aSThomas Gleixner if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg)) 951da177e4SLinus Torvalds return -EFAULT; 961da177e4SLinus Torvalds break; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds case APCIOCGCPWR: 9949ab972aSThomas Gleixner if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg)) 1001da177e4SLinus Torvalds return -EFAULT; 1011da177e4SLinus Torvalds break; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds case APCIOCGBPORT: 10449ab972aSThomas Gleixner if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg)) 1051da177e4SLinus Torvalds return -EFAULT; 1061da177e4SLinus Torvalds break; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds case APCIOCSFANCTL: 10949ab972aSThomas Gleixner if (get_user(inarg, arg)) 1101da177e4SLinus Torvalds return -EFAULT; 1111da177e4SLinus Torvalds apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG); 1121da177e4SLinus Torvalds break; 11349ab972aSThomas Gleixner 1141da177e4SLinus Torvalds case APCIOCSCPWR: 11549ab972aSThomas Gleixner if (get_user(inarg, arg)) 1161da177e4SLinus Torvalds return -EFAULT; 1171da177e4SLinus Torvalds apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG); 1181da177e4SLinus Torvalds break; 11949ab972aSThomas Gleixner 1201da177e4SLinus Torvalds case APCIOCSBPORT: 12149ab972aSThomas Gleixner if (get_user(inarg, arg)) 1221da177e4SLinus Torvalds return -EFAULT; 1231da177e4SLinus Torvalds apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG); 1241da177e4SLinus Torvalds break; 12549ab972aSThomas Gleixner 1261da177e4SLinus Torvalds default: 1271da177e4SLinus Torvalds return -EINVAL; 1286cb79b3fSJoe Perches } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds return 0; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1335dfe4c96SArjan van de Ven static const struct file_operations apc_fops = { 134ab772027SStoyan Gaydarov .unlocked_ioctl = apc_ioctl, 1351da177e4SLinus Torvalds .open = apc_open, 1361da177e4SLinus Torvalds .release = apc_release, 1376038f373SArnd Bergmann .llseek = noop_llseek, 1381da177e4SLinus Torvalds }; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops }; 1411da177e4SLinus Torvalds 1427c9503b8SGreg Kroah-Hartman static int apc_probe(struct platform_device *op) 1431da177e4SLinus Torvalds { 1447e7e2f03SDavid S. Miller int err; 1451da177e4SLinus Torvalds 1467e7e2f03SDavid S. Miller regs = of_ioremap(&op->resource[0], 0, 1477e7e2f03SDavid S. Miller resource_size(&op->resource[0]), APC_OBPNAME); 1481da177e4SLinus Torvalds if (!regs) { 1491da177e4SLinus Torvalds printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME); 1501da177e4SLinus Torvalds return -ENODEV; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1537e7e2f03SDavid S. Miller err = misc_register(&apc_miscdev); 1547e7e2f03SDavid S. Miller if (err) { 1551da177e4SLinus Torvalds printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME); 1567e7e2f03SDavid S. Miller apc_free(op); 1571da177e4SLinus Torvalds return -ENODEV; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* Assign power management IDLE handler */ 1611da177e4SLinus Torvalds if (!apc_no_idle) 162d472ba84SLen Brown sparc_idle = apc_swift_idle; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds printk(KERN_INFO "%s: power management initialized%s\n", 1651da177e4SLinus Torvalds APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : ""); 1667e7e2f03SDavid S. Miller 1671da177e4SLinus Torvalds return 0; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 170*69f57978SArvind Yadav static const struct of_device_id apc_match[] = { 1717e7e2f03SDavid S. Miller { 1727e7e2f03SDavid S. Miller .name = APC_OBPNAME, 1737e7e2f03SDavid S. Miller }, 1747e7e2f03SDavid S. Miller {}, 1757e7e2f03SDavid S. Miller }; 1767e7e2f03SDavid S. Miller MODULE_DEVICE_TABLE(of, apc_match); 1777e7e2f03SDavid S. Miller 1784ebb24f7SGrant Likely static struct platform_driver apc_driver = { 1794018294bSGrant Likely .driver = { 1807e7e2f03SDavid S. Miller .name = "apc", 1814018294bSGrant Likely .of_match_table = apc_match, 1824018294bSGrant Likely }, 1837e7e2f03SDavid S. Miller .probe = apc_probe, 1847e7e2f03SDavid S. Miller }; 1857e7e2f03SDavid S. Miller 1867e7e2f03SDavid S. Miller static int __init apc_init(void) 1877e7e2f03SDavid S. Miller { 1884ebb24f7SGrant Likely return platform_driver_register(&apc_driver); 1897e7e2f03SDavid S. Miller } 1907e7e2f03SDavid S. Miller 1911da177e4SLinus Torvalds /* This driver is not critical to the boot process 1921da177e4SLinus Torvalds * and is easiest to ioremap when SBus is already 1931da177e4SLinus Torvalds * initialized, so we install ourselves thusly: 1941da177e4SLinus Torvalds */ 1957e7e2f03SDavid S. Miller __initcall(apc_init); 196