addr_map.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <common.h>
  19. #include <addr_map.h>
  20. static struct {
  21. phys_addr_t paddr;
  22. phys_size_t size;
  23. unsigned long vaddr;
  24. } address_map[CONFIG_SYS_NUM_ADDR_MAP];
  25. phys_addr_t addrmap_virt_to_phys(void * vaddr)
  26. {
  27. int i;
  28. for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
  29. u64 base, upper, addr;
  30. if (address_map[i].size == 0)
  31. continue;
  32. addr = (u64)((u32)vaddr);
  33. base = (u64)(address_map[i].vaddr);
  34. upper = (u64)(address_map[i].size) + base - 1;
  35. if (addr >= base && addr <= upper) {
  36. return addr - address_map[i].vaddr + address_map[i].paddr;
  37. }
  38. }
  39. return (phys_addr_t)(~0);
  40. }
  41. unsigned long addrmap_phys_to_virt(phys_addr_t paddr)
  42. {
  43. int i;
  44. for (i = 0; i < CONFIG_SYS_NUM_ADDR_MAP; i++) {
  45. u64 base, upper, addr;
  46. if (address_map[i].size == 0)
  47. continue;
  48. addr = (u64)paddr;
  49. base = (u64)(address_map[i].paddr);
  50. upper = (u64)(address_map[i].size) + base - 1;
  51. if (addr >= base && addr <= upper) {
  52. return paddr - address_map[i].paddr + address_map[i].vaddr;
  53. }
  54. }
  55. return (unsigned long)(~0);
  56. }
  57. void addrmap_set_entry(unsigned long vaddr, phys_addr_t paddr,
  58. phys_size_t size, int idx)
  59. {
  60. if (idx > CONFIG_SYS_NUM_ADDR_MAP)
  61. return;
  62. address_map[idx].vaddr = vaddr;
  63. address_map[idx].paddr = paddr;
  64. address_map[idx].size = size;
  65. }