blob: 276ba3c5143fd9646e03a1e75b3e20a90b5a87fd [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 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -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,
181
182};
183
184static struct miscdevice cpustate_dev = {
185 MISC_DYNAMIC_MINOR,
186 "sky_cpustate",
187 &cpustate_fops
188};
189
Russell King3ae5eae2005-11-09 22:32:44 +0000190static int hdpu_cpustate_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct resource *res;
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700193 struct proc_dir_entry *proc_de;
194 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 cpustate.set_addr = (unsigned long *)res->start;
198 cpustate.clr_addr = (unsigned long *)res->end - 1;
199
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700200 ret = misc_register(&cpustate_dev);
201 if (ret) {
202 printk(KERN_WARNING "sky_cpustate: Unable to register misc "
203 "device.\n");
204 cpustate.set_addr = NULL;
205 cpustate.clr_addr = NULL;
206 return ret;
207 }
208
209 proc_de = create_proc_read_entry("sky_cpustate", 0, 0,
210 cpustate_read_proc, NULL);
211 if (proc_de == NULL)
212 printk(KERN_WARNING "sky_cpustate: Unable to create proc "
213 "dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
216 return 0;
217}
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700218
Russell King3ae5eae2005-11-09 22:32:44 +0000219static int hdpu_cpustate_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221
Christophe Lucas1ac19f42005-09-10 00:26:33 -0700222 cpustate.set_addr = NULL;
223 cpustate.clr_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 remove_proc_entry("sky_cpustate", NULL);
226 misc_deregister(&cpustate_dev);
227 return 0;
228
229}
230
231static int __init cpustate_init(void)
232{
233 int rc;
Russell King3ae5eae2005-11-09 22:32:44 +0000234 rc = platform_driver_register(&hdpu_cpustate_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return rc;
236}
237
238static void __exit cpustate_exit(void)
239{
Russell King3ae5eae2005-11-09 22:32:44 +0000240 platform_driver_unregister(&hdpu_cpustate_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243module_init(cpustate_init);
244module_exit(cpustate_exit);
245
246MODULE_AUTHOR("Brian Waite");
247MODULE_LICENSE("GPL");