flash.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* $Id: flash.c,v 1.25 2001/12/21 04:56:16 davem Exp $
  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/config.h>
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/slab.h>
  12. #include <linux/fcntl.h>
  13. #include <linux/poll.h>
  14. #include <linux/init.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/spinlock.h>
  17. #include <asm/system.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/io.h>
  21. #include <asm/sbus.h>
  22. #include <asm/ebus.h>
  23. #include <asm/upa.h>
  24. static DEFINE_SPINLOCK(flash_lock);
  25. static struct {
  26. unsigned long read_base; /* Physical read address */
  27. unsigned long write_base; /* Physical write address */
  28. unsigned long read_size; /* Size of read area */
  29. unsigned long write_size; /* Size of write area */
  30. unsigned long busy; /* In use? */
  31. } flash;
  32. #define FLASH_MINOR 152
  33. static int
  34. flash_mmap(struct file *file, struct vm_area_struct *vma)
  35. {
  36. unsigned long addr;
  37. unsigned long size;
  38. spin_lock(&flash_lock);
  39. if (flash.read_base == flash.write_base) {
  40. addr = flash.read_base;
  41. size = flash.read_size;
  42. } else {
  43. if ((vma->vm_flags & VM_READ) &&
  44. (vma->vm_flags & VM_WRITE)) {
  45. spin_unlock(&flash_lock);
  46. return -EINVAL;
  47. }
  48. if (vma->vm_flags & VM_READ) {
  49. addr = flash.read_base;
  50. size = flash.read_size;
  51. } else if (vma->vm_flags & VM_WRITE) {
  52. addr = flash.write_base;
  53. size = flash.write_size;
  54. } else {
  55. spin_unlock(&flash_lock);
  56. return -ENXIO;
  57. }
  58. }
  59. spin_unlock(&flash_lock);
  60. if ((vma->vm_pgoff << PAGE_SHIFT) > size)
  61. return -ENXIO;
  62. addr = vma->vm_pgoff + (addr >> PAGE_SHIFT);
  63. if (vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT)) > size)
  64. size = vma->vm_end - (vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT));
  65. pgprot_val(vma->vm_page_prot) &= ~(_PAGE_CACHE);
  66. pgprot_val(vma->vm_page_prot) |= _PAGE_E;
  67. vma->vm_flags |= (VM_SHM | VM_LOCKED);
  68. if (io_remap_pfn_range(vma, vma->vm_start, addr, size, vma->vm_page_prot))
  69. return -EAGAIN;
  70. return 0;
  71. }
  72. static long long
  73. flash_llseek(struct file *file, long long offset, int origin)
  74. {
  75. lock_kernel();
  76. switch (origin) {
  77. case 0:
  78. file->f_pos = offset;
  79. break;
  80. case 1:
  81. file->f_pos += offset;
  82. if (file->f_pos > flash.read_size)
  83. file->f_pos = flash.read_size;
  84. break;
  85. case 2:
  86. file->f_pos = flash.read_size;
  87. break;
  88. default:
  89. unlock_kernel();
  90. return -EINVAL;
  91. }
  92. unlock_kernel();
  93. return file->f_pos;
  94. }
  95. static ssize_t
  96. flash_read(struct file * file, char __user * buf,
  97. size_t count, loff_t *ppos)
  98. {
  99. unsigned long p = file->f_pos;
  100. int i;
  101. if (count > flash.read_size - p)
  102. count = flash.read_size - p;
  103. for (i = 0; i < count; i++) {
  104. u8 data = upa_readb(flash.read_base + p + i);
  105. if (put_user(data, buf))
  106. return -EFAULT;
  107. buf++;
  108. }
  109. file->f_pos += count;
  110. return count;
  111. }
  112. static int
  113. flash_open(struct inode *inode, struct file *file)
  114. {
  115. if (test_and_set_bit(0, (void *)&flash.busy) != 0)
  116. return -EBUSY;
  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 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 __init flash_init(void)
  140. {
  141. struct sbus_bus *sbus;
  142. struct sbus_dev *sdev = NULL;
  143. #ifdef CONFIG_PCI
  144. struct linux_ebus *ebus;
  145. struct linux_ebus_device *edev = NULL;
  146. struct linux_prom_registers regs[2];
  147. int len, nregs;
  148. #endif
  149. int err;
  150. for_all_sbusdev(sdev, sbus) {
  151. if (!strcmp(sdev->prom_name, "flashprom")) {
  152. if (sdev->reg_addrs[0].phys_addr == sdev->reg_addrs[1].phys_addr) {
  153. flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
  154. (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
  155. flash.read_size = sdev->reg_addrs[0].reg_size;
  156. flash.write_base = flash.read_base;
  157. flash.write_size = flash.read_size;
  158. } else {
  159. flash.read_base = ((unsigned long)sdev->reg_addrs[0].phys_addr) |
  160. (((unsigned long)sdev->reg_addrs[0].which_io)<<32UL);
  161. flash.read_size = sdev->reg_addrs[0].reg_size;
  162. flash.write_base = ((unsigned long)sdev->reg_addrs[1].phys_addr) |
  163. (((unsigned long)sdev->reg_addrs[1].which_io)<<32UL);
  164. flash.write_size = sdev->reg_addrs[1].reg_size;
  165. }
  166. flash.busy = 0;
  167. break;
  168. }
  169. }
  170. if (!sdev) {
  171. #ifdef CONFIG_PCI
  172. for_each_ebus(ebus) {
  173. for_each_ebusdev(edev, ebus) {
  174. if (!strcmp(edev->prom_name, "flashprom"))
  175. goto ebus_done;
  176. }
  177. }
  178. ebus_done:
  179. if (!edev)
  180. return -ENODEV;
  181. len = prom_getproperty(edev->prom_node, "reg", (void *)regs, sizeof(regs));
  182. if ((len % sizeof(regs[0])) != 0) {
  183. printk("flash: Strange reg property size %d\n", len);
  184. return -ENODEV;
  185. }
  186. nregs = len / sizeof(regs[0]);
  187. flash.read_base = edev->resource[0].start;
  188. flash.read_size = regs[0].reg_size;
  189. if (nregs == 1) {
  190. flash.write_base = edev->resource[0].start;
  191. flash.write_size = regs[0].reg_size;
  192. } else if (nregs == 2) {
  193. flash.write_base = edev->resource[1].start;
  194. flash.write_size = regs[1].reg_size;
  195. } else {
  196. printk("flash: Strange number of regs %d\n", nregs);
  197. return -ENODEV;
  198. }
  199. flash.busy = 0;
  200. #else
  201. return -ENODEV;
  202. #endif
  203. }
  204. printk("OBP Flash: RD %lx[%lx] WR %lx[%lx]\n",
  205. flash.read_base, flash.read_size,
  206. flash.write_base, flash.write_size);
  207. err = misc_register(&flash_dev);
  208. if (err) {
  209. printk(KERN_ERR "flash: unable to get misc minor\n");
  210. return err;
  211. }
  212. return 0;
  213. }
  214. static void __exit flash_cleanup(void)
  215. {
  216. misc_deregister(&flash_dev);
  217. }
  218. module_init(flash_init);
  219. module_exit(flash_cleanup);
  220. MODULE_LICENSE("GPL");