hdpu_cpustate.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/pci.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/platform_device.h>
  23. #include <asm/uaccess.h>
  24. #include <linux/hdpu_features.h>
  25. #define SKY_CPUSTATE_VERSION "1.1"
  26. static int hdpu_cpustate_probe(struct platform_device *pdev);
  27. static int hdpu_cpustate_remove(struct platform_device *pdev);
  28. struct cpustate_t cpustate;
  29. static int cpustate_get_ref(int excl)
  30. {
  31. int retval = -EBUSY;
  32. spin_lock(&cpustate.lock);
  33. if (cpustate.excl)
  34. goto out_busy;
  35. if (excl) {
  36. if (cpustate.open_count)
  37. goto out_busy;
  38. cpustate.excl = 1;
  39. }
  40. cpustate.open_count++;
  41. retval = 0;
  42. out_busy:
  43. spin_unlock(&cpustate.lock);
  44. return retval;
  45. }
  46. static int cpustate_free_ref(void)
  47. {
  48. spin_lock(&cpustate.lock);
  49. cpustate.excl = 0;
  50. cpustate.open_count--;
  51. spin_unlock(&cpustate.lock);
  52. return 0;
  53. }
  54. unsigned char cpustate_get_state(void)
  55. {
  56. return cpustate.cached_val;
  57. }
  58. void cpustate_set_state(unsigned char new_state)
  59. {
  60. unsigned int state = (new_state << 21);
  61. #ifdef DEBUG_CPUSTATE
  62. printk("CPUSTATE -> 0x%x\n", new_state);
  63. #endif
  64. spin_lock(&cpustate.lock);
  65. cpustate.cached_val = new_state;
  66. writel((0xff << 21), cpustate.clr_addr);
  67. writel(state, cpustate.set_addr);
  68. spin_unlock(&cpustate.lock);
  69. }
  70. /*
  71. * Now all the various file operations that we export.
  72. */
  73. static ssize_t cpustate_read(struct file *file, char *buf,
  74. size_t count, loff_t * ppos)
  75. {
  76. unsigned char data;
  77. if (count < 0)
  78. return -EFAULT;
  79. if (count == 0)
  80. return 0;
  81. data = cpustate_get_state();
  82. if (copy_to_user(buf, &data, sizeof(unsigned char)))
  83. return -EFAULT;
  84. return sizeof(unsigned char);
  85. }
  86. static ssize_t cpustate_write(struct file *file, const char *buf,
  87. size_t count, loff_t * ppos)
  88. {
  89. unsigned char data;
  90. if (count < 0)
  91. return -EFAULT;
  92. if (count == 0)
  93. return 0;
  94. if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char)))
  95. return -EFAULT;
  96. cpustate_set_state(data);
  97. return sizeof(unsigned char);
  98. }
  99. static int cpustate_open(struct inode *inode, struct file *file)
  100. {
  101. return cpustate_get_ref((file->f_flags & O_EXCL));
  102. }
  103. static int cpustate_release(struct inode *inode, struct file *file)
  104. {
  105. return cpustate_free_ref();
  106. }
  107. /*
  108. * Info exported via "/proc/sky_cpustate".
  109. */
  110. static int cpustate_read_proc(char *page, char **start, off_t off,
  111. int count, int *eof, void *data)
  112. {
  113. char *p = page;
  114. int len = 0;
  115. p += sprintf(p, "CPU State: %04x\n", cpustate_get_state());
  116. len = p - page;
  117. if (len <= off + count)
  118. *eof = 1;
  119. *start = page + off;
  120. len -= off;
  121. if (len > count)
  122. len = count;
  123. if (len < 0)
  124. len = 0;
  125. return len;
  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 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. fasync:NULL,
  144. poll:NULL,
  145. ioctl:NULL,
  146. llseek:no_llseek,
  147. };
  148. static struct miscdevice cpustate_dev = {
  149. MISC_DYNAMIC_MINOR,
  150. "sky_cpustate",
  151. &cpustate_fops
  152. };
  153. static int hdpu_cpustate_probe(struct platform_device *pdev)
  154. {
  155. struct resource *res;
  156. struct proc_dir_entry *proc_de;
  157. int ret;
  158. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  159. cpustate.set_addr = (unsigned long *)res->start;
  160. cpustate.clr_addr = (unsigned long *)res->end - 1;
  161. ret = misc_register(&cpustate_dev);
  162. if (ret) {
  163. printk(KERN_WARNING "sky_cpustate: Unable to register misc "
  164. "device.\n");
  165. cpustate.set_addr = NULL;
  166. cpustate.clr_addr = NULL;
  167. return ret;
  168. }
  169. proc_de = create_proc_read_entry("sky_cpustate", 0, 0,
  170. cpustate_read_proc, NULL);
  171. if (proc_de == NULL)
  172. printk(KERN_WARNING "sky_cpustate: Unable to create proc "
  173. "dir entry\n");
  174. printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
  175. return 0;
  176. }
  177. static int hdpu_cpustate_remove(struct platform_device *pdev)
  178. {
  179. cpustate.set_addr = NULL;
  180. cpustate.clr_addr = NULL;
  181. remove_proc_entry("sky_cpustate", NULL);
  182. misc_deregister(&cpustate_dev);
  183. return 0;
  184. }
  185. static int __init cpustate_init(void)
  186. {
  187. int rc;
  188. rc = platform_driver_register(&hdpu_cpustate_driver);
  189. return rc;
  190. }
  191. static void __exit cpustate_exit(void)
  192. {
  193. platform_driver_unregister(&hdpu_cpustate_driver);
  194. }
  195. module_init(cpustate_init);
  196. module_exit(cpustate_exit);
  197. MODULE_AUTHOR("Brian Waite");
  198. MODULE_LICENSE("GPL");