flash.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/slab.h>
  10. #include <linux/fcntl.h>
  11. #include <linux/poll.h>
  12. #include <linux/init.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/mm.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <asm/system.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/io.h>
  22. #include <asm/upa.h>
  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. lock_kernel();
  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. unlock_kernel();
  87. return -EINVAL;
  88. }
  89. unlock_kernel();
  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. unsigned long p = file->f_pos;
  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. file->f_pos += count;
  107. return count;
  108. }
  109. static int
  110. flash_open(struct inode *inode, struct file *file)
  111. {
  112. lock_kernel();
  113. if (test_and_set_bit(0, (void *)&flash.busy) != 0) {
  114. unlock_kernel();
  115. return -EBUSY;
  116. }
  117. unlock_kernel();
  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 of_device *op,
  141. const struct of_device_id *match)
  142. {
  143. struct device_node *dp = op->node;
  144. struct device_node *parent;
  145. parent = dp->parent;
  146. if (strcmp(parent->name, "sbus") &&
  147. strcmp(parent->name, "sbi") &&
  148. strcmp(parent->name, "ebus"))
  149. return -ENODEV;
  150. flash.read_base = op->resource[0].start;
  151. flash.read_size = resource_size(&op->resource[0]);
  152. if (op->resource[1].flags) {
  153. flash.write_base = op->resource[1].start;
  154. flash.write_size = resource_size(&op->resource[1]);
  155. } else {
  156. flash.write_base = op->resource[0].start;
  157. flash.write_size = resource_size(&op->resource[0]);
  158. }
  159. flash.busy = 0;
  160. printk(KERN_INFO "%s: OBP Flash, RD %lx[%lx] WR %lx[%lx]\n",
  161. op->node->full_name,
  162. flash.read_base, flash.read_size,
  163. flash.write_base, flash.write_size);
  164. return misc_register(&flash_dev);
  165. }
  166. static int __devexit flash_remove(struct of_device *op)
  167. {
  168. misc_deregister(&flash_dev);
  169. return 0;
  170. }
  171. static const struct of_device_id flash_match[] = {
  172. {
  173. .name = "flashprom",
  174. },
  175. {},
  176. };
  177. MODULE_DEVICE_TABLE(of, flash_match);
  178. static struct of_platform_driver flash_driver = {
  179. .name = "flash",
  180. .match_table = flash_match,
  181. .probe = flash_probe,
  182. .remove = __devexit_p(flash_remove),
  183. };
  184. static int __init flash_init(void)
  185. {
  186. return of_register_driver(&flash_driver, &of_bus_type);
  187. }
  188. static void __exit flash_cleanup(void)
  189. {
  190. of_unregister_driver(&flash_driver);
  191. }
  192. module_init(flash_init);
  193. module_exit(flash_cleanup);
  194. MODULE_LICENSE("GPL");