blob: 80fdb995a8ce406abcfd207a5faccd28094a8636 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/cris/mm/ioremap.c
3 *
4 * Re-map IO memory to kernel address space so that we can access it.
5 * Needed for memory-mapped I/O devices mapped outside our normal DRAM
6 * window (that is, all memory-mapped I/O devices).
7 *
8 * (C) Copyright 1995 1996 Linus Torvalds
9 * CRIS-port by Axis Communications AB
10 */
11
12#include <linux/vmalloc.h>
Haavard Skinnemoene32cbc32006-09-30 23:29:17 -070013#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/pgalloc.h>
Jesper Nilsson556dcee2008-10-21 17:45:58 +020015#include <arch/memmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017/*
18 * Generic mapping function (not visible outside):
19 */
20
21/*
22 * Remap an arbitrary physical address space into the kernel virtual
23 * address space. Needed when the kernel wants to access high addresses
24 * directly.
25 *
26 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
27 * have to convert them into an offset in a page-aligned mapping, but the
28 * caller shouldn't need to know that small detail.
29 */
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070030void __iomem * __ioremap_prot(unsigned long phys_addr, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070032 void __iomem * addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 struct vm_struct * area;
34 unsigned long offset, last_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 * Mappings have to be page-aligned
43 */
44 offset = phys_addr & ~PAGE_MASK;
45 phys_addr &= PAGE_MASK;
46 size = PAGE_ALIGN(last_addr+1) - phys_addr;
47
48 /*
49 * Ok, go for it..
50 */
51 area = get_vm_area(size, VM_IOREMAP);
52 if (!area)
53 return NULL;
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070054 addr = (void __iomem *)area->addr;
Haavard Skinnemoene32cbc32006-09-30 23:29:17 -070055 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
56 phys_addr, prot)) {
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070057 vfree((void __force *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return NULL;
59 }
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070060 return (void __iomem *) (offset + (char __iomem *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070063void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
64{
65 return __ioremap_prot(phys_addr, size,
66 __pgprot(_PAGE_PRESENT | __READABLE |
67 __WRITEABLE | _PAGE_GLOBAL |
68 _PAGE_KERNEL | flags));
69}
70
71/**
72 * ioremap_nocache - map bus memory into CPU space
73 * @offset: bus address of the memory
74 * @size: size of the resource to map
75 *
76 * Must be freed with iounmap.
77 */
78
Jesper Nilsson82e6df12014-10-01 15:38:38 +020079void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070080{
81 return __ioremap(phys_addr | MEM_NON_CACHEABLE, size, 0);
82}
Jesper Nilsson82e6df12014-10-01 15:38:38 +020083EXPORT_SYMBOL(ioremap_nocache);
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070084
85void iounmap(volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 if (addr > high_memory)
88 return vfree((void *) (PAGE_MASK & (unsigned long) addr));
89}