ioremap.c 2.3 KB

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