blob: ecd05bc0a10454a440e0b362d0c413903363fbab [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* apc - Driver implementation for power management functions
3 * of Aurora Personality Chip (APC) on SPARCstation-4/5 and
4 * derivatives.
5 *
6 * Copyright (c) 2002 Eric Brower (ebrower@usa.net)
7 */
8
9#include <linux/kernel.h>
10#include <linux/fs.h>
11#include <linux/errno.h>
12#include <linux/init.h>
13#include <linux/miscdevice.h>
14#include <linux/pm.h>
David S. Miller7e7e2f02008-08-27 02:45:36 -070015#include <linux/of.h>
16#include <linux/of_device.h>
Paul Gortmakerf060ac52011-07-31 01:50:31 -040017#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/oplib.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/auxio.h>
23#include <asm/apc.h>
Len Brownd472ba82013-02-09 23:27:26 -050024#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/* Debugging
27 *
28 * #define APC_DEBUG_LED
29 */
30
31#define APC_MINOR MISC_DYNAMIC_MINOR
32#define APC_OBPNAME "power-management"
33#define APC_DEVNAME "apc"
34
David S. Miller7e7e2f02008-08-27 02:45:36 -070035static u8 __iomem *regs;
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -080036static int apc_no_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David S. Miller7e7e2f02008-08-27 02:45:36 -070038#define apc_readb(offs) (sbus_readb(regs+offs))
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define apc_writeb(val, offs) (sbus_writeb(val, regs+offs))
40
41/* Specify "apc=noidle" on the kernel command line to
42 * disable APC CPU standby support. Certain prototype
43 * systems (SPARCstation-Fox) do not play well with APC
44 * CPU idle, so disable this if your system has APC and
45 * crashes randomly.
46 */
47static int __init apc_setup(char *str)
48{
49 if(!strncmp(str, "noidle", strlen("noidle"))) {
50 apc_no_idle = 1;
51 return 1;
52 }
53 return 0;
54}
55__setup("apc=", apc_setup);
56
57/*
58 * CPU idle callback function
59 * See .../arch/sparc/kernel/process.c
60 */
Adrian Bunkc61c65c2008-06-05 11:40:58 -070061static void apc_swift_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63#ifdef APC_DEBUG_LED
64 set_auxio(0x00, AUXIO_LED);
65#endif
66
67 apc_writeb(apc_readb(APC_IDLE_REG) | APC_IDLE_ON, APC_IDLE_REG);
68
69#ifdef APC_DEBUG_LED
70 set_auxio(AUXIO_LED, 0x00);
71#endif
72}
73
Grant Likelycd4cd732010-07-22 16:04:30 -060074static inline void apc_free(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
David S. Miller7e7e2f02008-08-27 02:45:36 -070076 of_iounmap(&op->resource[0], regs, resource_size(&op->resource[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static int apc_open(struct inode *inode, struct file *f)
80{
81 return 0;
82}
83
84static int apc_release(struct inode *inode, struct file *f)
85{
86 return 0;
87}
88
Stoyan Gaydarovab772022008-07-14 22:12:29 -070089static long apc_ioctl(struct file *f, unsigned int cmd, unsigned long __arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Thomas Gleixner49ab9722009-11-02 21:15:59 -080091 __u8 inarg, __user *arg = (__u8 __user *) __arg;
Stoyan Gaydarovab772022008-07-14 22:12:29 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 switch (cmd) {
94 case APCIOCGFANCTL:
Thomas Gleixner49ab9722009-11-02 21:15:59 -080095 if (put_user(apc_readb(APC_FANCTL_REG) & APC_REGMASK, arg))
Stoyan Gaydarovab772022008-07-14 22:12:29 -070096 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 break;
98
99 case APCIOCGCPWR:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800100 if (put_user(apc_readb(APC_CPOWER_REG) & APC_REGMASK, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return -EFAULT;
102 break;
103
104 case APCIOCGBPORT:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800105 if (put_user(apc_readb(APC_BPORT_REG) & APC_BPMASK, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -EFAULT;
107 break;
108
109 case APCIOCSFANCTL:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800110 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -EFAULT;
112 apc_writeb(inarg & APC_REGMASK, APC_FANCTL_REG);
113 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 case APCIOCSCPWR:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800116 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -EFAULT;
118 apc_writeb(inarg & APC_REGMASK, APC_CPOWER_REG);
119 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 case APCIOCSBPORT:
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800122 if (get_user(inarg, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return -EFAULT;
124 apc_writeb(inarg & APC_BPMASK, APC_BPORT_REG);
125 break;
Thomas Gleixner49ab9722009-11-02 21:15:59 -0800126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 default:
128 return -EINVAL;
Joe Perches6cb79b32011-06-03 14:45:23 +0000129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return 0;
132}
133
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800134static const struct file_operations apc_fops = {
Stoyan Gaydarovab772022008-07-14 22:12:29 -0700135 .unlocked_ioctl = apc_ioctl,
136 .open = apc_open,
137 .release = apc_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200138 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141static struct miscdevice apc_miscdev = { APC_MINOR, APC_DEVNAME, &apc_fops };
142
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -0800143static int apc_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
David S. Miller7e7e2f02008-08-27 02:45:36 -0700145 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
David S. Miller7e7e2f02008-08-27 02:45:36 -0700147 regs = of_ioremap(&op->resource[0], 0,
148 resource_size(&op->resource[0]), APC_OBPNAME);
149 if (!regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 printk(KERN_ERR "%s: unable to map registers\n", APC_DEVNAME);
151 return -ENODEV;
152 }
153
David S. Miller7e7e2f02008-08-27 02:45:36 -0700154 err = misc_register(&apc_miscdev);
155 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 printk(KERN_ERR "%s: unable to register device\n", APC_DEVNAME);
David S. Miller7e7e2f02008-08-27 02:45:36 -0700157 apc_free(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -ENODEV;
159 }
160
161 /* Assign power management IDLE handler */
David S. Miller7e7e2f02008-08-27 02:45:36 -0700162 if (!apc_no_idle)
Len Brownd472ba82013-02-09 23:27:26 -0500163 sparc_idle = apc_swift_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 printk(KERN_INFO "%s: power management initialized%s\n",
David S. Miller7e7e2f02008-08-27 02:45:36 -0700166 APC_DEVNAME, apc_no_idle ? " (CPU idle disabled)" : "");
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return 0;
169}
170
Arvind Yadav69f57972017-06-23 14:51:35 +0530171static const struct of_device_id apc_match[] = {
David S. Miller7e7e2f02008-08-27 02:45:36 -0700172 {
173 .name = APC_OBPNAME,
174 },
175 {},
176};
177MODULE_DEVICE_TABLE(of, apc_match);
178
Grant Likely4ebb24f2011-02-22 20:01:33 -0700179static struct platform_driver apc_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700180 .driver = {
181 .name = "apc",
Grant Likely40182942010-04-13 16:13:02 -0700182 .of_match_table = apc_match,
183 },
David S. Miller7e7e2f02008-08-27 02:45:36 -0700184 .probe = apc_probe,
185};
186
187static int __init apc_init(void)
188{
Grant Likely4ebb24f2011-02-22 20:01:33 -0700189 return platform_driver_register(&apc_driver);
David S. Miller7e7e2f02008-08-27 02:45:36 -0700190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* This driver is not critical to the boot process
193 * and is easiest to ioremap when SBus is already
194 * initialized, so we install ourselves thusly:
195 */
David S. Miller7e7e2f02008-08-27 02:45:36 -0700196__initcall(apc_init);