Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008 Freescale Semiconductor, Inc. |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <addr_map.h> |
| 8 | |
| 9 | static struct { |
| 10 | phys_addr_t paddr; |
| 11 | phys_size_t size; |
| 12 | unsigned long vaddr; |
| 13 | } address_map[CONFIG_SYS_NUM_ADDR_MAP]; |
| 14 | |
| 15 | phys_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 Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 37 | void *addrmap_phys_to_virt(phys_addr_t paddr) |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 38 | { |
| 39 | int i; |
| 40 | |
| 41 | for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) { |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 42 | phys_addr_t base, upper; |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 43 | |
| 44 | if (address_map[i].size == 0) |
| 45 | continue; |
| 46 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 47 | base = address_map[i].paddr; |
| 48 | upper = address_map[i].size + base - 1; |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 49 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 50 | 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 Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 59 | return (void *)(~0); |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void 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 | } |