xref: /openbmc/u-boot/lib/addr_map.c (revision 274bced8)
1 /*
2  * Copyright 2008 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0
5  */
6 
7 #include <common.h>
8 #include <addr_map.h>
9 
10 static struct {
11 	phys_addr_t paddr;
12 	phys_size_t size;
13 	unsigned long vaddr;
14 } address_map[CONFIG_SYS_NUM_ADDR_MAP];
15 
16 phys_addr_t addrmap_virt_to_phys(void * vaddr)
17 {
18 	int i;
19 
20 	for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
21 		u64 base, upper, addr;
22 
23 		if (address_map[i].size == 0)
24 			continue;
25 
26 		addr = (u64)((u32)vaddr);
27 		base = (u64)(address_map[i].vaddr);
28 		upper = (u64)(address_map[i].size) + base - 1;
29 
30 		if (addr >= base && addr <= upper) {
31 			return addr - address_map[i].vaddr + address_map[i].paddr;
32 		}
33 	}
34 
35 	return (phys_addr_t)(~0);
36 }
37 
38 void *addrmap_phys_to_virt(phys_addr_t paddr)
39 {
40 	int i;
41 
42 	for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
43 		phys_addr_t base, upper;
44 
45 		if (address_map[i].size == 0)
46 			continue;
47 
48 		base = address_map[i].paddr;
49 		upper = address_map[i].size + base - 1;
50 
51 		if (paddr >= base && paddr <= upper) {
52 			phys_addr_t offset;
53 
54 			offset = address_map[i].paddr - address_map[i].vaddr;
55 
56 			return (void *)(unsigned long)(paddr - offset);
57 		}
58 	}
59 
60 	return (void *)(~0);
61 }
62 
63 void addrmap_set_entry(unsigned long vaddr, phys_addr_t paddr,
64 			phys_size_t size, int idx)
65 {
66 	if (idx > CONFIG_SYS_NUM_ADDR_MAP)
67 		return;
68 
69 	address_map[idx].vaddr = vaddr;
70 	address_map[idx].paddr = paddr;
71 	address_map[idx].size  = size;
72 }
73