blob: 403640b95fc630309216a6d6b584d42d3f7fda8f [file] [log] [blame]
Kumar Gala63240ba2008-12-13 17:20:28 -06001/*
2 * Copyright 2008 Freescale Semiconductor, Inc.
3 *
Tom Rini5b8031c2016-01-14 22:05:13 -05004 * SPDX-License-Identifier: GPL-2.0
Kumar Gala63240ba2008-12-13 17:20:28 -06005 */
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
Timur Tabi7b6e8052012-05-04 12:21:30 +000038void *addrmap_phys_to_virt(phys_addr_t paddr)
Kumar Gala63240ba2008-12-13 17:20:28 -060039{
40 int i;
41
42 for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
Timur Tabi7b6e8052012-05-04 12:21:30 +000043 phys_addr_t base, upper;
Kumar Gala63240ba2008-12-13 17:20:28 -060044
45 if (address_map[i].size == 0)
46 continue;
47
Timur Tabi7b6e8052012-05-04 12:21:30 +000048 base = address_map[i].paddr;
49 upper = address_map[i].size + base - 1;
Kumar Gala63240ba2008-12-13 17:20:28 -060050
Timur Tabi7b6e8052012-05-04 12:21:30 +000051 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);
Kumar Gala63240ba2008-12-13 17:20:28 -060057 }
58 }
59
Timur Tabi7b6e8052012-05-04 12:21:30 +000060 return (void *)(~0);
Kumar Gala63240ba2008-12-13 17:20:28 -060061}
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}