ioremap.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/vmalloc.h>
  9. #include <linux/module.h>
  10. #include <linux/io.h>
  11. #include <asm/pgtable.h>
  12. #include <asm/addrspace.h>
  13. /*
  14. * Re-map an arbitrary physical address space into the kernel virtual
  15. * address space. Needed when the kernel wants to access physical
  16. * memory directly.
  17. */
  18. void __iomem *__ioremap(unsigned long phys_addr, size_t size,
  19. unsigned long flags)
  20. {
  21. unsigned long addr;
  22. struct vm_struct *area;
  23. unsigned long offset, last_addr;
  24. pgprot_t prot;
  25. /*
  26. * Check if we can simply use the P4 segment. This area is
  27. * uncacheable, so if caching/buffering is requested, we can't
  28. * use it.
  29. */
  30. if ((phys_addr >= P4SEG) && (flags == 0))
  31. return (void __iomem *)phys_addr;
  32. /* Don't allow wraparound or zero size */
  33. last_addr = phys_addr + size - 1;
  34. if (!size || last_addr < phys_addr)
  35. return NULL;
  36. /*
  37. * XXX: When mapping regular RAM, we'd better make damn sure
  38. * it's never used for anything else. But this is really the
  39. * caller's responsibility...
  40. */
  41. if (PHYSADDR(P2SEGADDR(phys_addr)) == phys_addr)
  42. return (void __iomem *)P2SEGADDR(phys_addr);
  43. /* Mappings have to be page-aligned */
  44. offset = phys_addr & ~PAGE_MASK;
  45. phys_addr &= PAGE_MASK;
  46. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  47. prot = __pgprot(_PAGE_PRESENT | _PAGE_GLOBAL | _PAGE_RW | _PAGE_DIRTY
  48. | _PAGE_ACCESSED | _PAGE_TYPE_SMALL | flags);
  49. /*
  50. * Ok, go for it..
  51. */
  52. area = get_vm_area(size, VM_IOREMAP);
  53. if (!area)
  54. return NULL;
  55. area->phys_addr = phys_addr;
  56. addr = (unsigned long )area->addr;
  57. if (ioremap_page_range(addr, addr + size, phys_addr, prot)) {
  58. vunmap((void *)addr);
  59. return NULL;
  60. }
  61. return (void __iomem *)(offset + (char *)addr);
  62. }
  63. EXPORT_SYMBOL(__ioremap);
  64. void __iounmap(void __iomem *addr)
  65. {
  66. struct vm_struct *p;
  67. if ((unsigned long)addr >= P4SEG)
  68. return;
  69. if (PXSEG(addr) == P2SEG)
  70. return;
  71. p = remove_vm_area((void *)(PAGE_MASK & (unsigned long __force)addr));
  72. if (unlikely(!p)) {
  73. printk (KERN_ERR "iounmap: bad address %p\n", addr);
  74. return;
  75. }
  76. kfree (p);
  77. }
  78. EXPORT_SYMBOL(__iounmap);