fsl_law.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2008 Freescale Semiconductor, Inc.
  3. *
  4. * (C) Copyright 2000
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/fsl_law.h>
  27. #include <asm/io.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #define LAWAR_EN 0x80000000
  30. /* number of LAWs in the hw implementation */
  31. #if defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || \
  32. defined(CONFIG_MPC8560) || defined(CONFIG_MPC8555)
  33. #define FSL_HW_NUM_LAWS 8
  34. #elif defined(CONFIG_MPC8548) || defined(CONFIG_MPC8544) || \
  35. defined(CONFIG_MPC8568) || \
  36. defined(CONFIG_MPC8641) || defined(CONFIG_MPC8610)
  37. #define FSL_HW_NUM_LAWS 10
  38. #elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \
  39. defined(CONFIG_P2020)
  40. #define FSL_HW_NUM_LAWS 12
  41. #else
  42. #error FSL_HW_NUM_LAWS not defined for this platform
  43. #endif
  44. void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  45. {
  46. volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
  47. volatile u32 *lawbar = base + 8 * idx;
  48. volatile u32 *lawar = base + 8 * idx + 2;
  49. gd->used_laws |= (1 << idx);
  50. out_be32(lawar, 0);
  51. out_be32(lawbar, addr >> 12);
  52. out_be32(lawar, LAWAR_EN | ((u32)id << 20) | (u32)sz);
  53. return ;
  54. }
  55. int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  56. {
  57. u32 idx = ffz(gd->used_laws);
  58. if (idx >= FSL_HW_NUM_LAWS)
  59. return -1;
  60. set_law(idx, addr, sz, id);
  61. return idx;
  62. }
  63. int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  64. {
  65. u32 idx;
  66. /* we have no LAWs free */
  67. if (gd->used_laws == -1)
  68. return -1;
  69. /* grab the last free law */
  70. idx = __ilog2(~(gd->used_laws));
  71. if (idx >= FSL_HW_NUM_LAWS)
  72. return -1;
  73. set_law(idx, addr, sz, id);
  74. return idx;
  75. }
  76. void disable_law(u8 idx)
  77. {
  78. volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
  79. volatile u32 *lawbar = base + 8 * idx;
  80. volatile u32 *lawar = base + 8 * idx + 2;
  81. gd->used_laws &= ~(1 << idx);
  82. out_be32(lawar, 0);
  83. out_be32(lawbar, 0);
  84. return;
  85. }
  86. void print_laws(void)
  87. {
  88. volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
  89. volatile u32 *lawbar = base;
  90. volatile u32 *lawar = base + 2;
  91. int i;
  92. printf("\nLocal Access Window Configuration\n");
  93. for(i = 0; i < FSL_HW_NUM_LAWS; i++) {
  94. printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n",
  95. i, in_be32(lawbar), i, in_be32(lawar));
  96. lawbar += 8;
  97. lawar += 8;
  98. }
  99. return;
  100. }
  101. /* use up to 2 LAWs for DDR, used the last available LAWs */
  102. int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
  103. {
  104. u64 start_align, law_sz;
  105. int law_sz_enc;
  106. if (start == 0)
  107. start_align = 1ull << (LAW_SIZE_32G + 1);
  108. else
  109. start_align = 1ull << (ffs64(start) - 1);
  110. law_sz = min(start_align, sz);
  111. law_sz_enc = __ilog2_u64(law_sz) - 1;
  112. if (set_last_law(start, law_sz_enc, id) < 0)
  113. return -1;
  114. /* do we still have anything to map */
  115. sz = sz - law_sz;
  116. if (sz) {
  117. start += law_sz;
  118. start_align = 1ull << (ffs64(start) - 1);
  119. law_sz = min(start_align, sz);
  120. law_sz_enc = __ilog2_u64(law_sz) - 1;
  121. if (set_last_law(start, law_sz_enc, id) < 0)
  122. return -1;
  123. } else {
  124. return 0;
  125. }
  126. /* do we still have anything to map */
  127. sz = sz - law_sz;
  128. if (sz)
  129. return 1;
  130. return 0;
  131. }
  132. void init_laws(void)
  133. {
  134. int i;
  135. gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
  136. for (i = 0; i < num_law_entries; i++) {
  137. if (law_table[i].index == -1)
  138. set_next_law(law_table[i].addr, law_table[i].size,
  139. law_table[i].trgt_id);
  140. else
  141. set_law(law_table[i].index, law_table[i].addr,
  142. law_table[i].size, law_table[i].trgt_id);
  143. }
  144. return ;
  145. }