Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * Version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 16 | * MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <common.h> |
| 20 | #include <addr_map.h> |
| 21 | |
| 22 | static struct { |
| 23 | phys_addr_t paddr; |
| 24 | phys_size_t size; |
| 25 | unsigned long vaddr; |
| 26 | } address_map[CONFIG_SYS_NUM_ADDR_MAP]; |
| 27 | |
| 28 | phys_addr_t addrmap_virt_to_phys(void * vaddr) |
| 29 | { |
| 30 | int i; |
| 31 | |
| 32 | for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) { |
| 33 | u64 base, upper, addr; |
| 34 | |
| 35 | if (address_map[i].size == 0) |
| 36 | continue; |
| 37 | |
| 38 | addr = (u64)((u32)vaddr); |
| 39 | base = (u64)(address_map[i].vaddr); |
| 40 | upper = (u64)(address_map[i].size) + base - 1; |
| 41 | |
| 42 | if (addr >= base && addr <= upper) { |
| 43 | return addr - address_map[i].vaddr + address_map[i].paddr; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return (phys_addr_t)(~0); |
| 48 | } |
| 49 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 50 | void *addrmap_phys_to_virt(phys_addr_t paddr) |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 51 | { |
| 52 | int i; |
| 53 | |
| 54 | for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) { |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 55 | phys_addr_t base, upper; |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 56 | |
| 57 | if (address_map[i].size == 0) |
| 58 | continue; |
| 59 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 60 | base = address_map[i].paddr; |
| 61 | upper = address_map[i].size + base - 1; |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 62 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 63 | if (paddr >= base && paddr <= upper) { |
| 64 | phys_addr_t offset; |
| 65 | |
| 66 | offset = address_map[i].paddr - address_map[i].vaddr; |
| 67 | |
| 68 | return (void *)(unsigned long)(paddr - offset); |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Timur Tabi | f11b08a | 2012-05-04 12:21:30 +0000 | [diff] [blame] | 72 | return (void *)(~0); |
Kumar Gala | 0949bd1 | 2008-12-13 17:20:28 -0600 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void addrmap_set_entry(unsigned long vaddr, phys_addr_t paddr, |
| 76 | phys_size_t size, int idx) |
| 77 | { |
| 78 | if (idx > CONFIG_SYS_NUM_ADDR_MAP) |
| 79 | return; |
| 80 | |
| 81 | address_map[idx].vaddr = vaddr; |
| 82 | address_map[idx].paddr = paddr; |
| 83 | address_map[idx].size = size; |
| 84 | } |