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