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> 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <asm/io.h> 171da177e4SLinus Torvalds #include <asm/sbus.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 321da177e4SLinus Torvalds volatile static u8 __iomem *regs; 331da177e4SLinus Torvalds static int apc_regsize; 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 721da177e4SLinus Torvalds static inline void apc_free(void) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds sbus_iounmap(regs, apc_regsize); 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 88*ab772027SStoyan 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; 93*ab772027SStoyan Gaydarov 94*ab772027SStoyan Gaydarov lock_kernel(); 95*ab772027SStoyan Gaydarov 961da177e4SLinus Torvalds switch (cmd) { 971da177e4SLinus Torvalds case APCIOCGFANCTL: 98*ab772027SStoyan Gaydarov if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg)) { 99*ab772027SStoyan Gaydarov unlock_kernel(); 1001da177e4SLinus Torvalds return -EFAULT; 101*ab772027SStoyan Gaydarov } 1021da177e4SLinus Torvalds break; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds case APCIOCGCPWR: 105*ab772027SStoyan Gaydarov if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg)) { 106*ab772027SStoyan Gaydarov unlock_kernel(); 1071da177e4SLinus Torvalds return -EFAULT; 108*ab772027SStoyan Gaydarov } 1091da177e4SLinus Torvalds break; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds case APCIOCGBPORT: 112*ab772027SStoyan Gaydarov if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg)) { 113*ab772027SStoyan Gaydarov unlock_kernel(); 1141da177e4SLinus Torvalds return -EFAULT; 115*ab772027SStoyan Gaydarov } 1161da177e4SLinus Torvalds break; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds case APCIOCSFANCTL: 119*ab772027SStoyan Gaydarov if (get_user(inarg, arg)) { 120*ab772027SStoyan Gaydarov unlock_kernel(); 1211da177e4SLinus Torvalds return -EFAULT; 122*ab772027SStoyan Gaydarov } 1231da177e4SLinus Torvalds apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG); 1241da177e4SLinus Torvalds break; 1251da177e4SLinus Torvalds case APCIOCSCPWR: 126*ab772027SStoyan Gaydarov if (get_user(inarg, arg)) { 127*ab772027SStoyan Gaydarov unlock_kernel(); 1281da177e4SLinus Torvalds return -EFAULT; 129*ab772027SStoyan Gaydarov } 1301da177e4SLinus Torvalds apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG); 1311da177e4SLinus Torvalds break; 1321da177e4SLinus Torvalds case APCIOCSBPORT: 133*ab772027SStoyan Gaydarov if (get_user(inarg, arg)) { 134*ab772027SStoyan Gaydarov unlock_kernel(); 1351da177e4SLinus Torvalds return -EFAULT; 136*ab772027SStoyan Gaydarov } 1371da177e4SLinus Torvalds apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG); 1381da177e4SLinus Torvalds break; 1391da177e4SLinus Torvalds default: 140*ab772027SStoyan Gaydarov unlock_kernel(); 1411da177e4SLinus Torvalds return -EINVAL; 1421da177e4SLinus Torvalds }; 1431da177e4SLinus Torvalds 144*ab772027SStoyan Gaydarov unlock_kernel(); 1451da177e4SLinus Torvalds return 0; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1485dfe4c96SArjan van de Ven static const struct file_operations apc_fops = { 149*ab772027SStoyan 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 1561da177e4SLinus Torvalds static int __init apc_probe(void) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds struct sbus_bus *sbus = NULL; 1591da177e4SLinus Torvalds struct sbus_dev *sdev = NULL; 1601da177e4SLinus Torvalds int iTmp = 0; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds for_each_sbus(sbus) { 1631da177e4SLinus Torvalds for_each_sbusdev(sdev, sbus) { 1641da177e4SLinus Torvalds if (!strcmp(sdev->prom_name, APC_OBPNAME)) { 1651da177e4SLinus Torvalds goto sbus_done; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds sbus_done: 1711da177e4SLinus Torvalds if (!sdev) { 1721da177e4SLinus Torvalds return -ENODEV; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds apc_regsize = sdev->reg_addrs[0].reg_size; 1761da177e4SLinus Torvalds regs = sbus_ioremap(&sdev->resource[0], 0, 1771da177e4SLinus Torvalds apc_regsize, APC_OBPNAME); 1781da177e4SLinus Torvalds if(!regs) { 1791da177e4SLinus Torvalds printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME); 1801da177e4SLinus Torvalds return -ENODEV; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds iTmp = misc_register(&apc_miscdev); 1841da177e4SLinus Torvalds if (iTmp != 0) { 1851da177e4SLinus Torvalds printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME); 1861da177e4SLinus Torvalds apc_free(); 1871da177e4SLinus Torvalds return -ENODEV; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* Assign power management IDLE handler */ 1911da177e4SLinus Torvalds if(!apc_no_idle) 1921da177e4SLinus Torvalds pm_idle = apc_swift_idle; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds printk(KERN_INFO "%s: power management initialized%s\n", 1951da177e4SLinus Torvalds APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : ""); 1961da177e4SLinus Torvalds return 0; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds /* This driver is not critical to the boot process 2001da177e4SLinus Torvalds * and is easiest to ioremap when SBus is already 2011da177e4SLinus Torvalds * initialized, so we install ourselves thusly: 2021da177e4SLinus Torvalds */ 2031da177e4SLinus Torvalds __initcall(apc_probe); 2041da177e4SLinus Torvalds 205