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