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