Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 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> |
Haavard Skinnemoen | 86c8eb3 | 2006-09-30 23:29:16 -0700 | [diff] [blame] | 10 | #include <linux/io.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 11 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 12 | #include <asm/pgtable.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 13 | #include <asm/addrspace.h> |
| 14 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 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 | { |
Haavard Skinnemoen | 86c8eb3 | 2006-09-30 23:29:16 -0700 | [diff] [blame] | 23 | unsigned long addr; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 24 | struct vm_struct *area; |
| 25 | unsigned long offset, last_addr; |
| 26 | pgprot_t prot; |
| 27 | |
| 28 | /* |
| 29 | * Check if we can simply use the P4 segment. This area is |
| 30 | * uncacheable, so if caching/buffering is requested, we can't |
| 31 | * use it. |
| 32 | */ |
| 33 | if ((phys_addr >= P4SEG) && (flags == 0)) |
| 34 | return (void __iomem *)phys_addr; |
| 35 | |
| 36 | /* Don't allow wraparound or zero size */ |
| 37 | last_addr = phys_addr + size - 1; |
| 38 | if (!size || last_addr < phys_addr) |
| 39 | return NULL; |
| 40 | |
| 41 | /* |
| 42 | * XXX: When mapping regular RAM, we'd better make damn sure |
| 43 | * it's never used for anything else. But this is really the |
| 44 | * caller's responsibility... |
| 45 | */ |
| 46 | if (PHYSADDR(P2SEGADDR(phys_addr)) == phys_addr) |
| 47 | return (void __iomem *)P2SEGADDR(phys_addr); |
| 48 | |
| 49 | /* Mappings have to be page-aligned */ |
| 50 | offset = phys_addr & ~PAGE_MASK; |
| 51 | phys_addr &= PAGE_MASK; |
| 52 | size = PAGE_ALIGN(last_addr + 1) - phys_addr; |
| 53 | |
Haavard Skinnemoen | 86c8eb3 | 2006-09-30 23:29:16 -0700 | [diff] [blame] | 54 | prot = __pgprot(_PAGE_PRESENT | _PAGE_GLOBAL | _PAGE_RW | _PAGE_DIRTY |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 55 | | _PAGE_ACCESSED | _PAGE_TYPE_SMALL | flags); |
| 56 | |
| 57 | /* |
| 58 | * Ok, go for it.. |
| 59 | */ |
| 60 | area = get_vm_area(size, VM_IOREMAP); |
| 61 | if (!area) |
| 62 | return NULL; |
| 63 | area->phys_addr = phys_addr; |
Haavard Skinnemoen | 86c8eb3 | 2006-09-30 23:29:16 -0700 | [diff] [blame] | 64 | addr = (unsigned long )area->addr; |
| 65 | if (ioremap_page_range(addr, addr + size, phys_addr, prot)) { |
| 66 | vunmap((void *)addr); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | return (void __iomem *)(offset + (char *)addr); |
| 71 | } |
| 72 | EXPORT_SYMBOL(__ioremap); |
| 73 | |
| 74 | void __iounmap(void __iomem *addr) |
| 75 | { |
| 76 | struct vm_struct *p; |
| 77 | |
| 78 | if ((unsigned long)addr >= P4SEG) |
| 79 | return; |
Haavard Skinnemoen | bee8ce8 | 2006-10-24 10:12:41 +0200 | [diff] [blame] | 80 | if (PXSEG(addr) == P2SEG) |
| 81 | return; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 82 | |
| 83 | p = remove_vm_area((void *)(PAGE_MASK & (unsigned long __force)addr)); |
| 84 | if (unlikely(!p)) { |
| 85 | printk (KERN_ERR "iounmap: bad address %p\n", addr); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | kfree (p); |
| 90 | } |
| 91 | EXPORT_SYMBOL(__iounmap); |