blob: 1874b0740f2b12e01208416eae0a058ea52c4480 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Sky CPU State Driver
3 *
4 * Copyright (C) 2002 Brian Waite
5 *
6 * This driver allows use of the CPU state bits
7 * It exports the /dev/sky_cpustate and also
8 * /proc/sky_cpustate pseudo-file for status information.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/spinlock.h>
20#include <linux/miscdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/proc_fs.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <linux/hdpu_features.h>
25
26#define SKY_CPUSTATE_VERSION "1.1"
27
Russell King3ae5eae2005-11-09 22:32:44 +000028static int hdpu_cpustate_probe(struct platform_device *pdev);
29static int hdpu_cpustate_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct cpustate_t cpustate;
32
33static int cpustate_get_ref(int excl)
34{
35
36 int retval = -EBUSY;
37
38 spin_lock(&cpustate.lock);
39
40 if (cpustate.excl)
41 goto out_busy;
42
43 if (excl) {
44 if (cpustate.open_count)
45 goto out_busy;
46 cpustate.excl = 1;
47 }
48
49 cpustate.open_count++;
50 retval = 0;
51
52 out_busy:
53 spin_unlock(&cpustate.lock);
54 return retval;
55}
56
57static int cpustate_free_ref(void)
58{
59
60 spin_lock(&cpustate.lock);
61
62 cpustate.excl = 0;
63 cpustate.open_count--;
64
65 spin_unlock(&cpustate.lock);
66 return 0;
67}
68
69unsigned char cpustate_get_state(void)
70{
71
72 return cpustate.cached_val;
73}
74
75void cpustate_set_state(unsigned char new_state)
76{
77 unsigned int state = (new_state << 21);
78
79#ifdef DEBUG_CPUSTATE
80 printk("CPUSTATE -> 0x%x\n", new_state);
81#endif
82 spin_lock(&cpustate.lock);
83 cpustate.cached_val = new_state;
84 writel((0xff << 21), cpustate.clr_addr);
85 writel(state, cpustate.set_addr);
86 spin_unlock(&cpustate.lock);
87}
88
89/*
90 * Now all the various file operations that we export.
91 */
92
93static ssize_t cpustate_read(struct file *file, char *buf,
94 size_t count, loff_t * ppos)
95{
96 unsigned char data;
97
98 if (count < 0)
99 return -EFAULT;
100 if (count == 0)
101 return 0;
102
103 data = cpustate_get_state();
104 if (copy_to_user(buf, &data, sizeof(unsigned char)))
105 return -EFAULT;
106 return sizeof(unsigned char);
107}
108
109static ssize_t cpustate_write(struct file *file, const char *buf,
110 size_t count, loff_t * ppos)
111{
112 unsigned char data;
113
114 if (count < 0)
115 return -EFAULT;
116
117 if (count == 0)
118 return 0;
119
120 if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char)))
121 return -EFAULT;
122
123 cpustate_set_state(data);
124 return sizeof(unsigned char);
125}
126
127static int cpustate_open(struct inode *inode, struct file *file)
128{
129 return cpustate_get_ref((file->f_flags & O_EXCL));
130}
131
132static int cpustate_release(struct inode *inode, struct file *file)
133{
134 return cpustate_free_ref();
135}
136
137/*
138 * Info exported via "/proc/sky_cpustate".
139 */
140static int cpustate_read_proc(char *page, char **start, off_t off,
141 int count, int *eof, void *data)
142{
143 char *p = page;
144 int len = 0;
145
146 p += sprintf(p, "CPU State: %04x\n", cpustate_get_state());
147 len = p - page;
148
149 if (len <= off + count)
150 *eof = 1;
151 *start = page + off;
152 len -= off;
153 if (len > count)
154 len = count;
155 if (len < 0)
156 len = 0;
157 return len;
158}
159
Russell King3ae5eae2005-11-09 22:32:44 +0000160static struct platform_driver hdpu_cpustate_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 .probe = hdpu_cpustate_probe,
162 .remove = hdpu_cpustate_remove,
Russell King3ae5eae2005-11-09 22:32:44 +0000163 .driver = {
164 .name = HDPU_CPUSTATE_NAME,
165 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
168/*
169 * The various file operations we support.
170 */
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800171static const struct file_operations cpustate_fops = {
Cyrill Gorcunova4e32b52007-10-02 13:30:05 -0700172 owner: THIS_MODULE,
173 open: cpustate_open,
174 release: cpustate_release,
175 read: cpustate_read,
176 write: cpustate_write,
177 fasync: NULL,
178 poll: NULL,
179 ioctl: NULL,
180 llseek: no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
183static struct miscdevice cpustate_dev = {
184 MISC_DYNAMIC_MINOR,
185 "sky_cpustate",
Cyrill Gorcunova4e32b52007-10-02 13:30:05 -0700186 &cpustate_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Russell King3ae5eae2005-11-09 22:32:44 +0000189static int hdpu_cpustate_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct resource *res;
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700192 struct proc_dir_entry *proc_de;
193 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
196 cpustate.set_addr = (unsigned long *)res->start;
197 cpustate.clr_addr = (unsigned long *)res->end - 1;
198
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700199 ret = misc_register(&cpustate_dev);
200 if (ret) {
Cyrill Gorcunova4e32b52007-10-02 13:30:05 -0700201 printk(KERN_WARNING "sky_cpustate: "
202 "Unable to register misc device.\n");
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700203 cpustate.set_addr = NULL;
204 cpustate.clr_addr = NULL;
205 return ret;
206 }
207
208 proc_de = create_proc_read_entry("sky_cpustate", 0, 0,
Cyrill Gorcunova4e32b52007-10-02 13:30:05 -0700209 cpustate_read_proc, NULL);
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700210 if (proc_de == NULL)
Cyrill Gorcunova4e32b52007-10-02 13:30:05 -0700211 printk(KERN_WARNING "sky_cpustate: "
212 "Unable to create proc dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
215 return 0;
216}
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700217
Russell King3ae5eae2005-11-09 22:32:44 +0000218static int hdpu_cpustate_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700220 cpustate.set_addr = NULL;
221 cpustate.clr_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 remove_proc_entry("sky_cpustate", NULL);
224 misc_deregister(&cpustate_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Cyrill Gorcunova4e32b52007-10-02 13:30:05 -0700226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229static int __init cpustate_init(void)
230{
Cyrill Gorcunova4e32b52007-10-02 13:30:05 -0700231 return platform_driver_register(&hdpu_cpustate_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static void __exit cpustate_exit(void)
235{
Russell King3ae5eae2005-11-09 22:32:44 +0000236 platform_driver_unregister(&hdpu_cpustate_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239module_init(cpustate_init);
240module_exit(cpustate_exit);
241
242MODULE_AUTHOR("Brian Waite");
243MODULE_LICENSE("GPL");