hdpu_cpustate.c 5.4 KB

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