ioremap.c 2.3 KB

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