]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/addr_map.c
lib: div64: sync with Linux
[people/ms/u-boot.git] / lib / addr_map.c
CommitLineData
63240ba8
KG
1/*
2 * Copyright 2008 Freescale Semiconductor, Inc.
3 *
5b8031cc 4 * SPDX-License-Identifier: GPL-2.0
63240ba8
KG
5 */
6
7#include <common.h>
8#include <addr_map.h>
9
10static struct {
11 phys_addr_t paddr;
12 phys_size_t size;
13 unsigned long vaddr;
14} address_map[CONFIG_SYS_NUM_ADDR_MAP];
15
16phys_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
7b6e8053 38void *addrmap_phys_to_virt(phys_addr_t paddr)
63240ba8
KG
39{
40 int i;
41
42 for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
7b6e8053 43 phys_addr_t base, upper;
63240ba8
KG
44
45 if (address_map[i].size == 0)
46 continue;
47
7b6e8053
TT
48 base = address_map[i].paddr;
49 upper = address_map[i].size + base - 1;
63240ba8 50
7b6e8053
TT
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);
63240ba8
KG
57 }
58 }
59
7b6e8053 60 return (void *)(~0);
63240ba8
KG
61}
62
63void 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}