1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
278acc472SPeter Tyser /*
378acc472SPeter Tyser * Copyright 2008 Freescale Semiconductor, Inc.
478acc472SPeter Tyser */
578acc472SPeter Tyser
678acc472SPeter Tyser #include <common.h>
778acc472SPeter Tyser #include <addr_map.h>
878acc472SPeter Tyser
978acc472SPeter Tyser static struct {
1078acc472SPeter Tyser phys_addr_t paddr;
1178acc472SPeter Tyser phys_size_t size;
1278acc472SPeter Tyser unsigned long vaddr;
1378acc472SPeter Tyser } address_map[CONFIG_SYS_NUM_ADDR_MAP];
1478acc472SPeter Tyser
addrmap_virt_to_phys(void * vaddr)1578acc472SPeter Tyser phys_addr_t addrmap_virt_to_phys(void * vaddr)
1678acc472SPeter Tyser {
1778acc472SPeter Tyser int i;
1878acc472SPeter Tyser
1978acc472SPeter Tyser for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
2078acc472SPeter Tyser u64 base, upper, addr;
2178acc472SPeter Tyser
2278acc472SPeter Tyser if (address_map[i].size == 0)
2378acc472SPeter Tyser continue;
2478acc472SPeter Tyser
2578acc472SPeter Tyser addr = (u64)((u32)vaddr);
2678acc472SPeter Tyser base = (u64)(address_map[i].vaddr);
2778acc472SPeter Tyser upper = (u64)(address_map[i].size) + base - 1;
2878acc472SPeter Tyser
2978acc472SPeter Tyser if (addr >= base && addr <= upper) {
3078acc472SPeter Tyser return addr - address_map[i].vaddr + address_map[i].paddr;
3178acc472SPeter Tyser }
3278acc472SPeter Tyser }
3378acc472SPeter Tyser
3478acc472SPeter Tyser return (phys_addr_t)(~0);
3578acc472SPeter Tyser }
3678acc472SPeter Tyser
addrmap_phys_to_virt(phys_addr_t paddr)377b6e8053STimur Tabi void *addrmap_phys_to_virt(phys_addr_t paddr)
3878acc472SPeter Tyser {
3978acc472SPeter Tyser int i;
4078acc472SPeter Tyser
4178acc472SPeter Tyser for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
427b6e8053STimur Tabi phys_addr_t base, upper;
4378acc472SPeter Tyser
4478acc472SPeter Tyser if (address_map[i].size == 0)
4578acc472SPeter Tyser continue;
4678acc472SPeter Tyser
477b6e8053STimur Tabi base = address_map[i].paddr;
487b6e8053STimur Tabi upper = address_map[i].size + base - 1;
4978acc472SPeter Tyser
507b6e8053STimur Tabi if (paddr >= base && paddr <= upper) {
517b6e8053STimur Tabi phys_addr_t offset;
527b6e8053STimur Tabi
537b6e8053STimur Tabi offset = address_map[i].paddr - address_map[i].vaddr;
547b6e8053STimur Tabi
557b6e8053STimur Tabi return (void *)(unsigned long)(paddr - offset);
5678acc472SPeter Tyser }
5778acc472SPeter Tyser }
5878acc472SPeter Tyser
597b6e8053STimur Tabi return (void *)(~0);
6078acc472SPeter Tyser }
6178acc472SPeter Tyser
addrmap_set_entry(unsigned long vaddr,phys_addr_t paddr,phys_size_t size,int idx)6278acc472SPeter Tyser void addrmap_set_entry(unsigned long vaddr, phys_addr_t paddr,
6378acc472SPeter Tyser phys_size_t size, int idx)
6478acc472SPeter Tyser {
6578acc472SPeter Tyser if (idx > CONFIG_SYS_NUM_ADDR_MAP)
6678acc472SPeter Tyser return;
6778acc472SPeter Tyser
6878acc472SPeter Tyser address_map[idx].vaddr = vaddr;
6978acc472SPeter Tyser address_map[idx].paddr = paddr;
7078acc472SPeter Tyser address_map[idx].size = size;
7178acc472SPeter Tyser }
72