hdpu_cpustate.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/hdpu_features.h>
  23. #include <linux/platform_device.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/seq_file.h>
  26. #include <asm/io.h>
  27. #define SKY_CPUSTATE_VERSION "1.1"
  28. static int hdpu_cpustate_probe(struct platform_device *pdev);
  29. static int hdpu_cpustate_remove(struct platform_device *pdev);
  30. static unsigned char cpustate_get_state(void);
  31. static int cpustate_proc_open(struct inode *inode, struct file *file);
  32. static int cpustate_proc_read(struct seq_file *seq, void *offset);
  33. static struct cpustate_t cpustate;
  34. static const struct file_operations proc_cpustate = {
  35. .open = cpustate_proc_open,
  36. .read = seq_read,
  37. .llseek = seq_lseek,
  38. .release = single_release,
  39. .owner = THIS_MODULE,
  40. };
  41. static int cpustate_proc_open(struct inode *inode, struct file *file)
  42. {
  43. return single_open(file, cpustate_proc_read, NULL);
  44. }
  45. static int cpustate_proc_read(struct seq_file *seq, void *offset)
  46. {
  47. seq_printf(seq, "CPU State: %04x\n", cpustate_get_state());
  48. return 0;
  49. }
  50. static int cpustate_get_ref(int excl)
  51. {
  52. int retval = -EBUSY;
  53. spin_lock(&cpustate.lock);
  54. if (cpustate.excl)
  55. goto out_busy;
  56. if (excl) {
  57. if (cpustate.open_count)
  58. goto out_busy;
  59. cpustate.excl = 1;
  60. }
  61. cpustate.open_count++;
  62. retval = 0;
  63. out_busy:
  64. spin_unlock(&cpustate.lock);
  65. return retval;
  66. }
  67. static int cpustate_free_ref(void)
  68. {
  69. spin_lock(&cpustate.lock);
  70. cpustate.excl = 0;
  71. cpustate.open_count--;
  72. spin_unlock(&cpustate.lock);
  73. return 0;
  74. }
  75. static unsigned char cpustate_get_state(void)
  76. {
  77. return cpustate.cached_val;
  78. }
  79. static void cpustate_set_state(unsigned char new_state)
  80. {
  81. unsigned int state = (new_state << 21);
  82. #ifdef DEBUG_CPUSTATE
  83. printk("CPUSTATE -> 0x%x\n", new_state);
  84. #endif
  85. spin_lock(&cpustate.lock);
  86. cpustate.cached_val = new_state;
  87. writel((0xff << 21), cpustate.clr_addr);
  88. writel(state, cpustate.set_addr);
  89. spin_unlock(&cpustate.lock);
  90. }
  91. /*
  92. * Now all the various file operations that we export.
  93. */
  94. static ssize_t cpustate_read(struct file *file, char *buf,
  95. size_t count, loff_t * ppos)
  96. {
  97. unsigned char data;
  98. if (count < 0)
  99. return -EFAULT;
  100. if (count == 0)
  101. return 0;
  102. data = cpustate_get_state();
  103. if (copy_to_user(buf, &data, sizeof(unsigned char)))
  104. return -EFAULT;
  105. return sizeof(unsigned char);
  106. }
  107. static ssize_t cpustate_write(struct file *file, const char *buf,
  108. size_t count, loff_t * ppos)
  109. {
  110. unsigned char data;
  111. if (count < 0)
  112. return -EFAULT;
  113. if (count == 0)
  114. return 0;
  115. if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char)))
  116. return -EFAULT;
  117. cpustate_set_state(data);
  118. return sizeof(unsigned char);
  119. }
  120. static int cpustate_open(struct inode *inode, struct file *file)
  121. {
  122. int ret;
  123. lock_kernel();
  124. ret = cpustate_get_ref((file->f_flags & O_EXCL));
  125. unlock_kernel();
  126. return ret;
  127. }
  128. static int cpustate_release(struct inode *inode, struct file *file)
  129. {
  130. return cpustate_free_ref();
  131. }
  132. static struct platform_driver hdpu_cpustate_driver = {
  133. .probe = hdpu_cpustate_probe,
  134. .remove = hdpu_cpustate_remove,
  135. .driver = {
  136. .name = HDPU_CPUSTATE_NAME,
  137. .owner = THIS_MODULE,
  138. },
  139. };
  140. /*
  141. * The various file operations we support.
  142. */
  143. static const struct file_operations cpustate_fops = {
  144. .owner = THIS_MODULE,
  145. .open = cpustate_open,
  146. .release = cpustate_release,
  147. .read = cpustate_read,
  148. .write = cpustate_write,
  149. .llseek = no_llseek,
  150. };
  151. static struct miscdevice cpustate_dev = {
  152. .minor = MISC_DYNAMIC_MINOR,
  153. .name = "sky_cpustate",
  154. .fops = &cpustate_fops,
  155. };
  156. static int hdpu_cpustate_probe(struct platform_device *pdev)
  157. {
  158. struct resource *res;
  159. struct proc_dir_entry *proc_de;
  160. int ret;
  161. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. if (!res) {
  163. printk(KERN_ERR "sky_cpustate: "
  164. "Invalid memory resource.\n");
  165. return -EINVAL;
  166. }
  167. cpustate.set_addr = (unsigned long *)res->start;
  168. cpustate.clr_addr = (unsigned long *)res->end - 1;
  169. ret = misc_register(&cpustate_dev);
  170. if (ret) {
  171. printk(KERN_WARNING "sky_cpustate: "
  172. "Unable to register misc device.\n");
  173. cpustate.set_addr = NULL;
  174. cpustate.clr_addr = NULL;
  175. return ret;
  176. }
  177. proc_de = proc_create("sky_cpustate", 0666, NULL, &proc_cpustate);
  178. if (!proc_de) {
  179. printk(KERN_WARNING "sky_cpustate: "
  180. "Unable to create proc entry\n");
  181. }
  182. printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
  183. return 0;
  184. }
  185. static int hdpu_cpustate_remove(struct platform_device *pdev)
  186. {
  187. cpustate.set_addr = NULL;
  188. cpustate.clr_addr = NULL;
  189. remove_proc_entry("sky_cpustate", NULL);
  190. misc_deregister(&cpustate_dev);
  191. return 0;
  192. }
  193. static int __init cpustate_init(void)
  194. {
  195. return platform_driver_register(&hdpu_cpustate_driver);
  196. }
  197. static void __exit cpustate_exit(void)
  198. {
  199. platform_driver_unregister(&hdpu_cpustate_driver);
  200. }
  201. module_init(cpustate_init);
  202. module_exit(cpustate_exit);
  203. MODULE_AUTHOR("Brian Waite");
  204. MODULE_LICENSE("GPL");
  205. MODULE_ALIAS("platform:" HDPU_CPUSTATE_NAME);