flash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* flash.c: Allow mmap access to the OBP Flash, for OBP updates.
  2. *
  3. * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/types.h>
  7. #include <linux/errno.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/fcntl.h>
  10. #include <linux/poll.h>
  11. #include <linux/init.h>
  12. #include <linux/mutex.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/mm.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <asm/system.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/io.h>
  21. #include <asm/upa.h>
  22. static DEFINE_MUTEX(flash_mutex);
  23. static DEFINE_SPINLOCK(flash_lock);
  24. static struct {
  25. unsigned long read_base; /* Physical read address */
  26. unsigned long write_base; /* Physical write address */
  27. unsigned long read_size; /* Size of read area */
  28. unsigned long write_size; /* Size of write area */
  29. unsigned long busy; /* In use? */
  30. } flash;
  31. #define FLASH_MINOR 152
  32. static int
  33. flash_mmap(struct file *file, struct vm_area_struct *vma)
  34. {
  35. unsigned long addr;
  36. unsigned long size;
  37. spin_lock(&flash_lock);
  38. if (flash.read_base == flash.write_base) {
  39. addr = flash.read_base;
  40. size = flash.read_size;
  41. } else {
  42. if ((vma->vm_flags & VM_READ) &&
  43. (vma->vm_flags & VM_WRITE)) {
  44. spin_unlock(&flash_lock);
  45. return -EINVAL;
  46. }
  47. if (vma->vm_flags & VM_READ) {
  48. addr = flash.read_base;
  49. size = flash.read_size;
  50. } else if (vma->vm_flags & VM_WRITE) {
  51. addr = flash.write_base;
  52. size = flash.write_size;
  53. } else {
  54. spin_unlock(&flash_lock);
  55. return -ENXIO;
  56. }
  57. }
  58. spin_unlock(&flash_lock);
  59. if ((vma->vm_pgoff << PAGE_SHIFT) > size)
  60. return -ENXIO;
  61. addr = vma->vm_pgoff + (addr >> PAGE_SHIFT);
  62. if (vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT)) > size)
  63. size = vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT));
  64. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  65. if (io_remap_pfn_range(vma, vma->vm_start, addr, size, vma->vm_page_prot))
  66. return -EAGAIN;
  67. return 0;
  68. }
  69. static long long
  70. flash_llseek(struct file *file, long long offset, int origin)
  71. {
  72. mutex_lock(&flash_mutex);
  73. switch (origin) {
  74. case 0:
  75. file->f_pos = offset;
  76. break;
  77. case 1:
  78. file->f_pos += offset;
  79. if (file->f_pos > flash.read_size)
  80. file->f_pos = flash.read_size;
  81. break;
  82. case 2:
  83. file->f_pos = flash.read_size;
  84. break;
  85. default:
  86. mutex_unlock(&flash_mutex);
  87. return -EINVAL;
  88. }
  89. mutex_unlock(&flash_mutex);
  90. return file->f_pos;
  91. }
  92. static ssize_t
  93. flash_read(struct file * file, char __user * buf,
  94. size_t count, loff_t *ppos)
  95. {
  96. loff_t p = *ppos;
  97. int i;
  98. if (count > flash.read_size - p)
  99. count = flash.read_size - p;
  100. for (i = 0; i < count; i++) {
  101. u8 data = upa_readb(flash.read_base + p + i);
  102. if (put_user(data, buf))
  103. return -EFAULT;
  104. buf++;
  105. }
  106. *ppos += count;
  107. return count;
  108. }
  109. static int
  110. flash_open(struct inode *inode, struct file *file)
  111. {
  112. mutex_lock(&flash_mutex);
  113. if (test_and_set_bit(0, (void *)&flash.busy) != 0) {
  114. mutex_unlock(&flash_mutex);
  115. return -EBUSY;
  116. }
  117. mutex_unlock(&flash_mutex);
  118. return 0;
  119. }
  120. static int
  121. flash_release(struct inode *inode, struct file *file)
  122. {
  123. spin_lock(&flash_lock);
  124. flash.busy = 0;
  125. spin_unlock(&flash_lock);
  126. return 0;
  127. }
  128. static const struct file_operations flash_fops = {
  129. /* no write to the Flash, use mmap
  130. * and play flash dependent tricks.
  131. */
  132. .owner = THIS_MODULE,
  133. .llseek = flash_llseek,
  134. .read = flash_read,
  135. .mmap = flash_mmap,
  136. .open = flash_open,
  137. .release = flash_release,
  138. };
  139. static struct miscdevice flash_dev = { FLASH_MINOR, "flash", &flash_fops };
  140. static int __devinit flash_probe(struct platform_device *op)
  141. {
  142. struct device_node *dp = op->dev.of_node;
  143. struct device_node *parent;
  144. parent = dp->parent;
  145. if (strcmp(parent->name, "sbus") &&
  146. strcmp(parent->name, "sbi") &&
  147. strcmp(parent->name, "ebus"))
  148. return -ENODEV;
  149. flash.read_base = op->resource[0].start;
  150. flash.read_size = resource_size(&op->resource[0]);
  151. if (op->resource[1].flags) {
  152. flash.write_base = op->resource[1].start;
  153. flash.write_size = resource_size(&op->resource[1]);
  154. } else {
  155. flash.write_base = op->resource[0].start;
  156. flash.write_size = resource_size(&op->resource[0]);
  157. }
  158. flash.busy = 0;
  159. printk(KERN_INFO "%s: OBP Flash, RD %lx[%lx] WR %lx[%lx]\n",
  160. op->dev.of_node->full_name,
  161. flash.read_base, flash.read_size,
  162. flash.write_base, flash.write_size);
  163. return misc_register(&flash_dev);
  164. }
  165. static int __devexit flash_remove(struct platform_device *op)
  166. {
  167. misc_deregister(&flash_dev);
  168. return 0;
  169. }
  170. static const struct of_device_id flash_match[] = {
  171. {
  172. .name = "flashprom",
  173. },
  174. {},
  175. };
  176. MODULE_DEVICE_TABLE(of, flash_match);
  177. static struct platform_driver flash_driver = {
  178. .driver = {
  179. .name = "flash",
  180. .owner = THIS_MODULE,
  181. .of_match_table = flash_match,
  182. },
  183. .probe = flash_probe,
  184. .remove = __devexit_p(flash_remove),
  185. };
  186. static int __init flash_init(void)
  187. {
  188. return platform_driver_register(&flash_driver);
  189. }
  190. static void __exit flash_cleanup(void)
  191. {
  192. platform_driver_unregister(&flash_driver);
  193. }
  194. module_init(flash_init);
  195. module_exit(flash_cleanup);
  196. MODULE_LICENSE("GPL");