flash.c 4.7 KB

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