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> |
Simon Glass | f2d59c0 | 2022-07-13 06:06:58 -0600 | [diff] [blame] | 8 | #include <mapmem.h> |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 9 | |
Bin Meng | e151403 | 2021-02-25 17:22:33 +0800 | [diff] [blame] | 10 | struct addrmap address_map[CONFIG_SYS_NUM_ADDR_MAP]; |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 11 | |
| 12 | phys_addr_t addrmap_virt_to_phys(void * vaddr) |
| 13 | { |
| 14 | int i; |
| 15 | |
| 16 | for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) { |
| 17 | u64 base, upper, addr; |
| 18 | |
| 19 | if (address_map[i].size == 0) |
| 20 | continue; |
| 21 | |
Simon Glass | f2d59c0 | 2022-07-13 06:06:58 -0600 | [diff] [blame] | 22 | addr = map_to_sysmem(vaddr); |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 23 | base = (u64)(address_map[i].vaddr); |
| 24 | upper = (u64)(address_map[i].size) + base - 1; |
| 25 | |
| 26 | if (addr >= base && addr <= upper) { |
| 27 | return addr - address_map[i].vaddr + address_map[i].paddr; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | return (phys_addr_t)(~0); |
| 32 | } |
| 33 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 34 | void *addrmap_phys_to_virt(phys_addr_t paddr) |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 35 | { |
| 36 | int i; |
| 37 | |
| 38 | for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) { |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 39 | phys_addr_t base, upper; |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 40 | |
| 41 | if (address_map[i].size == 0) |
| 42 | continue; |
| 43 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 44 | base = address_map[i].paddr; |
| 45 | upper = address_map[i].size + base - 1; |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 46 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 47 | if (paddr >= base && paddr <= upper) { |
| 48 | phys_addr_t offset; |
| 49 | |
| 50 | offset = address_map[i].paddr - address_map[i].vaddr; |
| 51 | |
Simon Glass | f2d59c0 | 2022-07-13 06:06:58 -0600 | [diff] [blame] | 52 | return map_sysmem(paddr - offset, 0); |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 56 | return (void *)(~0); |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void addrmap_set_entry(unsigned long vaddr, phys_addr_t paddr, |
| 60 | phys_size_t size, int idx) |
| 61 | { |
| 62 | if (idx > CONFIG_SYS_NUM_ADDR_MAP) |
| 63 | return; |
| 64 | |
| 65 | address_map[idx].vaddr = vaddr; |
| 66 | address_map[idx].paddr = paddr; |
| 67 | address_map[idx].size = size; |
| 68 | } |