blob: 09771f3a5a5933bb8b3cda940d80d3a099c9861c [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>
8
9static struct {
10 phys_addr_t paddr;
11 phys_size_t size;
12 unsigned long vaddr;
13} address_map[CONFIG_SYS_NUM_ADDR_MAP];
14
15phys_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
Timur Tabi7b6e8052012-05-04 12:21:30 +000037void *addrmap_phys_to_virt(phys_addr_t paddr)
Kumar Gala63240ba2008-12-13 17:20:28 -060038{
39 int i;
40
41 for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
Timur Tabi7b6e8052012-05-04 12:21:30 +000042 phys_addr_t base, upper;
Kumar Gala63240ba2008-12-13 17:20:28 -060043
44 if (address_map[i].size == 0)
45 continue;
46
Timur Tabi7b6e8052012-05-04 12:21:30 +000047 base = address_map[i].paddr;
48 upper = address_map[i].size + base - 1;
Kumar Gala63240ba2008-12-13 17:20:28 -060049
Timur Tabi7b6e8052012-05-04 12:21:30 +000050 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);
Kumar Gala63240ba2008-12-13 17:20:28 -060056 }
57 }
58
Timur Tabi7b6e8052012-05-04 12:21:30 +000059 return (void *)(~0);
Kumar Gala63240ba2008-12-13 17:20:28 -060060}
61
62void 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}