flash.c 4.7 KB

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