blob: f85fb0c7fb1b66f97980e1639dc66021ed503696 [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
Kumar Gala63240ba2008-12-13 17:20:28 -06006#include <addr_map.h>
Simon Glass28199612022-07-13 06:06:58 -06007#include <mapmem.h>
Kumar Gala63240ba2008-12-13 17:20:28 -06008
Bin Mengb5615632021-02-25 17:22:33 +08009struct addrmap address_map[CONFIG_SYS_NUM_ADDR_MAP];
Kumar Gala63240ba2008-12-13 17:20:28 -060010
11phys_addr_t addrmap_virt_to_phys(void * vaddr)
12{
13 int i;
14
15 for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
16 u64 base, upper, addr;
17
18 if (address_map[i].size == 0)
19 continue;
20
Simon Glass28199612022-07-13 06:06:58 -060021 addr = map_to_sysmem(vaddr);
Kumar Gala63240ba2008-12-13 17:20:28 -060022 base = (u64)(address_map[i].vaddr);
23 upper = (u64)(address_map[i].size) + base - 1;
24
25 if (addr >= base && addr <= upper) {
26 return addr - address_map[i].vaddr + address_map[i].paddr;
27 }
28 }
29
30 return (phys_addr_t)(~0);
31}
32
Timur Tabi7b6e8052012-05-04 12:21:30 +000033void *addrmap_phys_to_virt(phys_addr_t paddr)
Kumar Gala63240ba2008-12-13 17:20:28 -060034{
35 int i;
36
37 for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
Timur Tabi7b6e8052012-05-04 12:21:30 +000038 phys_addr_t base, upper;
Kumar Gala63240ba2008-12-13 17:20:28 -060039
40 if (address_map[i].size == 0)
41 continue;
42
Timur Tabi7b6e8052012-05-04 12:21:30 +000043 base = address_map[i].paddr;
44 upper = address_map[i].size + base - 1;
Kumar Gala63240ba2008-12-13 17:20:28 -060045
Timur Tabi7b6e8052012-05-04 12:21:30 +000046 if (paddr >= base && paddr <= upper) {
47 phys_addr_t offset;
48
49 offset = address_map[i].paddr - address_map[i].vaddr;
50
Simon Glass28199612022-07-13 06:06:58 -060051 return map_sysmem(paddr - offset, 0);
Kumar Gala63240ba2008-12-13 17:20:28 -060052 }
53 }
54
Timur Tabi7b6e8052012-05-04 12:21:30 +000055 return (void *)(~0);
Kumar Gala63240ba2008-12-13 17:20:28 -060056}
57
58void addrmap_set_entry(unsigned long vaddr, phys_addr_t paddr,
59 phys_size_t size, int idx)
60{
Dan Carpenter7dd06b12023-07-25 09:50:40 +030061 if (idx >= CONFIG_SYS_NUM_ADDR_MAP)
Kumar Gala63240ba2008-12-13 17:20:28 -060062 return;
63
64 address_map[idx].vaddr = vaddr;
65 address_map[idx].paddr = paddr;
66 address_map[idx].size = size;
67}