flash.c 4.8 KB

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