blob: 9b3e0a544e478e00c4ec8cdc09ab2fce0d9dc2dc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kumar Gala63240ba2008-12-13 17:20:28 -06002/*
3 * Copyright 2008 Freescale Semiconductor, Inc.
Kumar Gala63240ba2008-12-13 17:20:28 -06004 */
5
6#include <common.h>
7#include <addr_map.h>
Simon Glass28199612022-07-13 06:06:58 -06008#include <mapmem.h>
Kumar Gala63240ba2008-12-13 17:20:28 -06009
Bin Mengb5615632021-02-25 17:22:33 +080010struct addrmap address_map[CONFIG_SYS_NUM_ADDR_MAP];
Kumar Gala63240ba2008-12-13 17:20:28 -060011
12phys_addr_t addrmap_virt_to_phys(void * vaddr)
13{
14 int i;
15
16 for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
17 u64 base, upper, addr;
18
19 if (address_map[i].size == 0)
20 continue;
21
Simon Glass28199612022-07-13 06:06:58 -060022 addr = map_to_sysmem(vaddr);
Kumar Gala63240ba2008-12-13 17:20:28 -060023 base = (u64)(address_map[i].vaddr);
24 upper = (u64)(address_map[i].size) + base - 1;
25
26 if (addr >= base && addr <= upper) {
27 return addr - address_map[i].vaddr + address_map[i].paddr;
28 }
29 }
30
31 return (phys_addr_t)(~0);
32}
33
Timur Tabi7b6e8052012-05-04 12:21:30 +000034void *addrmap_phys_to_virt(phys_addr_t paddr)
Kumar Gala63240ba2008-12-13 17:20:28 -060035{
36 int i;
37
38 for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
Timur Tabi7b6e8052012-05-04 12:21:30 +000039 phys_addr_t base, upper;
Kumar Gala63240ba2008-12-13 17:20:28 -060040
41 if (address_map[i].size == 0)
42 continue;
43
Timur Tabi7b6e8052012-05-04 12:21:30 +000044 base = address_map[i].paddr;
45 upper = address_map[i].size + base - 1;
Kumar Gala63240ba2008-12-13 17:20:28 -060046
Timur Tabi7b6e8052012-05-04 12:21:30 +000047 if (paddr >= base && paddr <= upper) {
48 phys_addr_t offset;
49
50 offset = address_map[i].paddr - address_map[i].vaddr;
51
Simon Glass28199612022-07-13 06:06:58 -060052 return map_sysmem(paddr - offset, 0);
Kumar Gala63240ba2008-12-13 17:20:28 -060053 }
54 }
55
Timur Tabi7b6e8052012-05-04 12:21:30 +000056 return (void *)(~0);
Kumar Gala63240ba2008-12-13 17:20:28 -060057}
58
59void addrmap_set_entry(unsigned long vaddr, phys_addr_t paddr,
60 phys_size_t size, int idx)
61{
62 if (idx > CONFIG_SYS_NUM_ADDR_MAP)
63 return;
64
65 address_map[idx].vaddr = vaddr;
66 address_map[idx].paddr = paddr;
67 address_map[idx].size = size;
68}