* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / arch / arm / mm / ioremap.c
blobe4c84fa71d1daa15a3839b6a44a375b085abb507
1 /*
2 * arch/arm/mm/ioremap.c
4 * Re-map IO memory to kernel address space so that we can access it.
6 * (C) Copyright 1995 1996 Linus Torvalds
8 * Hacked for ARM by Phil Blundell <philb@gnu.org>
9 * Hacked to allow all architectures to build, and various cleanups
10 * by Russell King
14 * This allows a driver to remap an arbitrary region of bus memory into
15 * virtual space. One should *only* use readl, writel, memcpy_toio and
16 * so on with such remapped areas.
18 * Because the ARM only has a 32-bit address space we can't address the
19 * whole of the (physical) PCI space at once. PCI huge-mode addressing
20 * allows us to circumvent this restriction by splitting PCI space into
21 * two 2GB chunks and mapping only one at a time into processor memory.
22 * We use MMU protection domains to trap any attempt to access the bank
23 * that is not currently mapped. (This isn't fully implemented yet.)
25 * DC21285 currently has a bug in that the PCI address extension
26 * register affects the address of any writes waiting in the outbound
27 * FIFO. Unfortunately, it is not possible to tell the DC21285 to
28 * flush this - flushing the area causes the bus to lock.
31 #include <linux/vmalloc.h>
32 #include <asm/io.h>
34 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
35 unsigned long phys_addr, pgprot_t pgprot)
37 unsigned long end;
39 address &= ~PMD_MASK;
40 end = address + size;
41 if (end > PMD_SIZE)
42 end = PMD_SIZE;
43 do {
44 if (!pte_none(*pte))
45 printk("remap_area_pte: page already exists\n");
46 set_pte(pte, mk_pte_phys(phys_addr, pgprot));
47 address += PAGE_SIZE;
48 phys_addr += PAGE_SIZE;
49 pte++;
50 } while (address < end);
53 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
54 unsigned long phys_addr, unsigned long flags)
56 unsigned long end;
57 pgprot_t pgprot;
59 address &= ~PGDIR_MASK;
60 end = address + size;
62 if (end > PGDIR_SIZE)
63 end = PGDIR_SIZE;
65 phys_addr -= address;
66 pgprot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_WRITE | flags);
67 do {
68 pte_t * pte = pte_alloc_kernel(pmd, address);
69 if (!pte)
70 return -ENOMEM;
71 remap_area_pte(pte, address, end - address, address + phys_addr, pgprot);
72 address = (address + PMD_SIZE) & PMD_MASK;
73 pmd++;
74 } while (address < end);
75 return 0;
78 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
79 unsigned long size, unsigned long flags)
81 pgd_t * dir;
82 unsigned long end = address + size;
84 phys_addr -= address;
85 dir = pgd_offset(&init_mm, address);
86 flush_cache_all();
87 while (address < end) {
88 pmd_t *pmd = pmd_alloc_kernel(dir, address);
89 if (!pmd)
90 return -ENOMEM;
91 if (remap_area_pmd(pmd, address, end - address,
92 phys_addr + address, flags))
93 return -ENOMEM;
94 set_pgdir(address, *dir);
95 address = (address + PGDIR_SIZE) & PGDIR_MASK;
96 dir++;
98 flush_tlb_all();
99 return 0;
103 * Remap an arbitrary physical address space into the kernel virtual
104 * address space. Needed when the kernel wants to access high addresses
105 * directly.
107 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
108 * have to convert them into an offset in a page-aligned mapping, but the
109 * caller shouldn't need to know that small detail.
111 * 'flags' are the extra L_PTE_ flags that you want to specify for this
112 * mapping. See include/asm-arm/proc-armv/pgtable.h for more information.
114 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
116 void * addr;
117 struct vm_struct * area;
118 unsigned long offset, last_addr;
120 /* Don't allow wraparound or zero size */
121 last_addr = phys_addr + size - 1;
122 if (!size || last_addr < phys_addr)
123 return NULL;
126 * Mappings have to be page-aligned
128 offset = phys_addr & ~PAGE_MASK;
129 phys_addr &= PAGE_MASK;
130 size = PAGE_ALIGN(last_addr) - phys_addr;
133 * Ok, go for it..
135 area = get_vm_area(size);
136 if (!area)
137 return NULL;
138 addr = area->addr;
139 if (remap_area_pages(VMALLOC_VMADDR(addr), phys_addr, size, flags)) {
140 vfree(addr);
141 return NULL;
143 return (void *) (offset + (char *)addr);
146 void __iounmap(void *addr)
148 return vfree((void *) (PAGE_MASK & (unsigned long) addr));