Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/miscdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/proc_fs.h> |
Cyrill Gorcunov | 3049ea7 | 2007-10-02 13:30:09 -0700 | [diff] [blame] | 22 | #include <linux/hdpu_features.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/uaccess.h> |
Cyrill Gorcunov | 3049ea7 | 2007-10-02 13:30:09 -0700 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Cyrill Gorcunov | d2ceb47a | 2007-10-02 13:30:06 -0700 | [diff] [blame] | 26 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #define SKY_CPUSTATE_VERSION "1.1" |
| 29 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 30 | static int hdpu_cpustate_probe(struct platform_device *pdev); |
| 31 | static int hdpu_cpustate_remove(struct platform_device *pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Cyrill Gorcunov | 3049ea7 | 2007-10-02 13:30:09 -0700 | [diff] [blame] | 33 | static unsigned char cpustate_get_state(void); |
| 34 | static int cpustate_proc_open(struct inode *inode, struct file *file); |
| 35 | static int cpustate_proc_read(struct seq_file *seq, void *offset); |
| 36 | |
| 37 | static struct cpustate_t cpustate; |
| 38 | |
| 39 | static const struct file_operations proc_cpustate = { |
| 40 | .open = cpustate_proc_open, |
| 41 | .read = seq_read, |
| 42 | .llseek = seq_lseek, |
| 43 | .release = single_release, |
| 44 | .owner = THIS_MODULE, |
| 45 | }; |
| 46 | |
| 47 | static int cpustate_proc_open(struct inode *inode, struct file *file) |
| 48 | { |
| 49 | return single_open(file, cpustate_proc_read, NULL); |
| 50 | } |
| 51 | |
| 52 | static int cpustate_proc_read(struct seq_file *seq, void *offset) |
| 53 | { |
| 54 | seq_printf(seq, "CPU State: %04x\n", cpustate_get_state()); |
| 55 | return 0; |
| 56 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | static int cpustate_get_ref(int excl) |
| 59 | { |
| 60 | |
| 61 | int retval = -EBUSY; |
| 62 | |
| 63 | spin_lock(&cpustate.lock); |
| 64 | |
| 65 | if (cpustate.excl) |
| 66 | goto out_busy; |
| 67 | |
| 68 | if (excl) { |
| 69 | if (cpustate.open_count) |
| 70 | goto out_busy; |
| 71 | cpustate.excl = 1; |
| 72 | } |
| 73 | |
| 74 | cpustate.open_count++; |
| 75 | retval = 0; |
| 76 | |
| 77 | out_busy: |
| 78 | spin_unlock(&cpustate.lock); |
| 79 | return retval; |
| 80 | } |
| 81 | |
| 82 | static int cpustate_free_ref(void) |
| 83 | { |
| 84 | |
| 85 | spin_lock(&cpustate.lock); |
| 86 | |
| 87 | cpustate.excl = 0; |
| 88 | cpustate.open_count--; |
| 89 | |
| 90 | spin_unlock(&cpustate.lock); |
| 91 | return 0; |
| 92 | } |
| 93 | |
Cyrill Gorcunov | 3049ea7 | 2007-10-02 13:30:09 -0700 | [diff] [blame] | 94 | static unsigned char cpustate_get_state(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | |
| 97 | return cpustate.cached_val; |
| 98 | } |
| 99 | |
Cyrill Gorcunov | 3049ea7 | 2007-10-02 13:30:09 -0700 | [diff] [blame] | 100 | static void cpustate_set_state(unsigned char new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | unsigned int state = (new_state << 21); |
| 103 | |
| 104 | #ifdef DEBUG_CPUSTATE |
| 105 | printk("CPUSTATE -> 0x%x\n", new_state); |
| 106 | #endif |
| 107 | spin_lock(&cpustate.lock); |
| 108 | cpustate.cached_val = new_state; |
| 109 | writel((0xff << 21), cpustate.clr_addr); |
| 110 | writel(state, cpustate.set_addr); |
| 111 | spin_unlock(&cpustate.lock); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Now all the various file operations that we export. |
| 116 | */ |
| 117 | |
| 118 | static ssize_t cpustate_read(struct file *file, char *buf, |
| 119 | size_t count, loff_t * ppos) |
| 120 | { |
| 121 | unsigned char data; |
| 122 | |
| 123 | if (count < 0) |
| 124 | return -EFAULT; |
| 125 | if (count == 0) |
| 126 | return 0; |
| 127 | |
| 128 | data = cpustate_get_state(); |
| 129 | if (copy_to_user(buf, &data, sizeof(unsigned char))) |
| 130 | return -EFAULT; |
| 131 | return sizeof(unsigned char); |
| 132 | } |
| 133 | |
| 134 | static ssize_t cpustate_write(struct file *file, const char *buf, |
| 135 | size_t count, loff_t * ppos) |
| 136 | { |
| 137 | unsigned char data; |
| 138 | |
| 139 | if (count < 0) |
| 140 | return -EFAULT; |
| 141 | |
| 142 | if (count == 0) |
| 143 | return 0; |
| 144 | |
| 145 | if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char))) |
| 146 | return -EFAULT; |
| 147 | |
| 148 | cpustate_set_state(data); |
| 149 | return sizeof(unsigned char); |
| 150 | } |
| 151 | |
| 152 | static int cpustate_open(struct inode *inode, struct file *file) |
| 153 | { |
| 154 | return cpustate_get_ref((file->f_flags & O_EXCL)); |
| 155 | } |
| 156 | |
| 157 | static int cpustate_release(struct inode *inode, struct file *file) |
| 158 | { |
| 159 | return cpustate_free_ref(); |
| 160 | } |
| 161 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 162 | static struct platform_driver hdpu_cpustate_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | .probe = hdpu_cpustate_probe, |
| 164 | .remove = hdpu_cpustate_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 165 | .driver = { |
| 166 | .name = HDPU_CPUSTATE_NAME, |
| 167 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | /* |
| 171 | * The various file operations we support. |
| 172 | */ |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 173 | static const struct file_operations cpustate_fops = { |
Cyrill Gorcunov | 8b70da1 | 2007-10-02 13:30:08 -0700 | [diff] [blame] | 174 | .owner = THIS_MODULE, |
| 175 | .open = cpustate_open, |
| 176 | .release = cpustate_release, |
| 177 | .read = cpustate_read, |
| 178 | .write = cpustate_write, |
| 179 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static struct miscdevice cpustate_dev = { |
Cyrill Gorcunov | 8b70da1 | 2007-10-02 13:30:08 -0700 | [diff] [blame] | 183 | .minor = MISC_DYNAMIC_MINOR, |
| 184 | .name = "sky_cpustate", |
| 185 | .fops = &cpustate_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 188 | static int hdpu_cpustate_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | struct resource *res; |
Christophe Lucas | 1ac19f4 | 2005-09-10 00:26:33 -0700 | [diff] [blame] | 191 | struct proc_dir_entry *proc_de; |
| 192 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Cyrill Gorcunov | 7472fd3 | 2007-10-02 13:30:06 -0700 | [diff] [blame] | 195 | if (!res) { |
| 196 | printk(KERN_ERR "sky_cpustate: " |
| 197 | "Invalid memory resource.\n"); |
| 198 | return -EINVAL; |
| 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | cpustate.set_addr = (unsigned long *)res->start; |
| 201 | cpustate.clr_addr = (unsigned long *)res->end - 1; |
| 202 | |
Christophe Lucas | 1ac19f4 | 2005-09-10 00:26:33 -0700 | [diff] [blame] | 203 | ret = misc_register(&cpustate_dev); |
| 204 | if (ret) { |
Cyrill Gorcunov | a4e32b5 | 2007-10-02 13:30:05 -0700 | [diff] [blame] | 205 | printk(KERN_WARNING "sky_cpustate: " |
| 206 | "Unable to register misc device.\n"); |
Christophe Lucas | 1ac19f4 | 2005-09-10 00:26:33 -0700 | [diff] [blame] | 207 | cpustate.set_addr = NULL; |
| 208 | cpustate.clr_addr = NULL; |
| 209 | return ret; |
| 210 | } |
| 211 | |
Cyrill Gorcunov | 3049ea7 | 2007-10-02 13:30:09 -0700 | [diff] [blame] | 212 | proc_de = create_proc_entry("sky_cpustate", 0666, &proc_root); |
| 213 | if (!proc_de) { |
Cyrill Gorcunov | a4e32b5 | 2007-10-02 13:30:05 -0700 | [diff] [blame] | 214 | printk(KERN_WARNING "sky_cpustate: " |
Cyrill Gorcunov | 3049ea7 | 2007-10-02 13:30:09 -0700 | [diff] [blame] | 215 | "Unable to create proc entry\n"); |
| 216 | } else { |
| 217 | proc_de->proc_fops = &proc_cpustate; |
| 218 | proc_de->owner = THIS_MODULE; |
| 219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n"); |
| 222 | return 0; |
| 223 | } |
Christophe Lucas | 1ac19f4 | 2005-09-10 00:26:33 -0700 | [diff] [blame] | 224 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 225 | static int hdpu_cpustate_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Christophe Lucas | 1ac19f4 | 2005-09-10 00:26:33 -0700 | [diff] [blame] | 227 | cpustate.set_addr = NULL; |
| 228 | cpustate.clr_addr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | remove_proc_entry("sky_cpustate", NULL); |
| 231 | misc_deregister(&cpustate_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Cyrill Gorcunov | a4e32b5 | 2007-10-02 13:30:05 -0700 | [diff] [blame] | 233 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static int __init cpustate_init(void) |
| 237 | { |
Cyrill Gorcunov | a4e32b5 | 2007-10-02 13:30:05 -0700 | [diff] [blame] | 238 | return platform_driver_register(&hdpu_cpustate_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static void __exit cpustate_exit(void) |
| 242 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 243 | platform_driver_unregister(&hdpu_cpustate_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | module_init(cpustate_init); |
| 247 | module_exit(cpustate_exit); |
| 248 | |
| 249 | MODULE_AUTHOR("Brian Waite"); |
| 250 | MODULE_LICENSE("GPL"); |