fsl_law.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2008-2010 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. /* number of LAWs in the hw implementation */
  30. #if defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || \
  31. defined(CONFIG_MPC8560) || defined(CONFIG_MPC8555)
  32. #define FSL_HW_NUM_LAWS 8
  33. #elif defined(CONFIG_MPC8548) || defined(CONFIG_MPC8544) || \
  34. defined(CONFIG_MPC8568) || defined(CONFIG_MPC8569) || \
  35. defined(CONFIG_MPC8641) || defined(CONFIG_MPC8610)
  36. #define FSL_HW_NUM_LAWS 10
  37. #elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \
  38. defined(CONFIG_P1011) || defined(CONFIG_P1020) || \
  39. defined(CONFIG_P1012) || defined(CONFIG_P1021) || \
  40. defined(CONFIG_P1013) || defined(CONFIG_P1022) || \
  41. defined(CONFIG_P2010) || defined(CONFIG_P2020)
  42. #define FSL_HW_NUM_LAWS 12
  43. #elif defined(CONFIG_PPC_P3041) || defined(CONFIG_PPC_P4080) || \
  44. defined(CONFIG_PPC_P5020)
  45. #define FSL_HW_NUM_LAWS 32
  46. #else
  47. #error FSL_HW_NUM_LAWS not defined for this platform
  48. #endif
  49. #ifdef CONFIG_FSL_CORENET
  50. #define LAW_BASE (CONFIG_SYS_FSL_CORENET_CCM_ADDR)
  51. #define LAWAR_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawar)
  52. #define LAWBARH_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarh)
  53. #define LAWBARL_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarl)
  54. #define LAWBAR_SHIFT 0
  55. #else
  56. #define LAW_BASE (CONFIG_SYS_IMMR + 0xc08)
  57. #define LAWAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x + 2)
  58. #define LAWBAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x)
  59. #define LAWBAR_SHIFT 12
  60. #endif
  61. static inline phys_addr_t get_law_base_addr(int idx)
  62. {
  63. #ifdef CONFIG_FSL_CORENET
  64. return (phys_addr_t)
  65. ((u64)in_be32(LAWBARH_ADDR(idx)) << 32) |
  66. in_be32(LAWBARL_ADDR(idx));
  67. #else
  68. return (phys_addr_t)in_be32(LAWBAR_ADDR(idx)) << LAWBAR_SHIFT;
  69. #endif
  70. }
  71. static inline void set_law_base_addr(int idx, phys_addr_t addr)
  72. {
  73. #ifdef CONFIG_FSL_CORENET
  74. out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff);
  75. out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32);
  76. #else
  77. out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT);
  78. #endif
  79. }
  80. void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  81. {
  82. gd->used_laws |= (1 << idx);
  83. out_be32(LAWAR_ADDR(idx), 0);
  84. set_law_base_addr(idx, addr);
  85. out_be32(LAWAR_ADDR(idx), LAW_EN | ((u32)id << 20) | (u32)sz);
  86. /* Read back so that we sync the writes */
  87. in_be32(LAWAR_ADDR(idx));
  88. }
  89. void disable_law(u8 idx)
  90. {
  91. gd->used_laws &= ~(1 << idx);
  92. out_be32(LAWAR_ADDR(idx), 0);
  93. set_law_base_addr(idx, 0);
  94. /* Read back so that we sync the writes */
  95. in_be32(LAWAR_ADDR(idx));
  96. return;
  97. }
  98. #ifndef CONFIG_NAND_SPL
  99. static int get_law_entry(u8 i, struct law_entry *e)
  100. {
  101. u32 lawar;
  102. lawar = in_be32(LAWAR_ADDR(i));
  103. if (!(lawar & LAW_EN))
  104. return 0;
  105. e->addr = get_law_base_addr(i);
  106. e->size = lawar & 0x3f;
  107. e->trgt_id = (lawar >> 20) & 0xff;
  108. return 1;
  109. }
  110. #endif
  111. int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  112. {
  113. u32 idx = ffz(gd->used_laws);
  114. if (idx >= FSL_HW_NUM_LAWS)
  115. return -1;
  116. set_law(idx, addr, sz, id);
  117. return idx;
  118. }
  119. #ifndef CONFIG_NAND_SPL
  120. int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  121. {
  122. u32 idx;
  123. /* we have no LAWs free */
  124. if (gd->used_laws == -1)
  125. return -1;
  126. /* grab the last free law */
  127. idx = __ilog2(~(gd->used_laws));
  128. if (idx >= FSL_HW_NUM_LAWS)
  129. return -1;
  130. set_law(idx, addr, sz, id);
  131. return idx;
  132. }
  133. struct law_entry find_law(phys_addr_t addr)
  134. {
  135. struct law_entry entry;
  136. int i;
  137. entry.index = -1;
  138. entry.addr = 0;
  139. entry.size = 0;
  140. entry.trgt_id = 0;
  141. for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
  142. u64 upper;
  143. if (!get_law_entry(i, &entry))
  144. continue;
  145. upper = entry.addr + (2ull << entry.size);
  146. if ((addr >= entry.addr) && (addr < upper)) {
  147. entry.index = i;
  148. break;
  149. }
  150. }
  151. return entry;
  152. }
  153. void print_laws(void)
  154. {
  155. int i;
  156. u32 lawar;
  157. printf("\nLocal Access Window Configuration\n");
  158. for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
  159. lawar = in_be32(LAWAR_ADDR(i));
  160. #ifdef CONFIG_FSL_CORENET
  161. printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x",
  162. i, in_be32(LAWBARH_ADDR(i)),
  163. i, in_be32(LAWBARL_ADDR(i)));
  164. #else
  165. printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i)));
  166. #endif
  167. printf(" LAWAR0x%02d: 0x%08x\n", i, lawar);
  168. printf("\t(EN: %d TGT: 0x%02x SIZE: ",
  169. (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff);
  170. print_size(lawar_size(lawar), ")\n");
  171. }
  172. return;
  173. }
  174. /* use up to 2 LAWs for DDR, used the last available LAWs */
  175. int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
  176. {
  177. u64 start_align, law_sz;
  178. int law_sz_enc;
  179. if (start == 0)
  180. start_align = 1ull << (LAW_SIZE_32G + 1);
  181. else
  182. start_align = 1ull << (ffs64(start) - 1);
  183. law_sz = min(start_align, sz);
  184. law_sz_enc = __ilog2_u64(law_sz) - 1;
  185. if (set_last_law(start, law_sz_enc, id) < 0)
  186. return -1;
  187. /* recalculate size based on what was actually covered by the law */
  188. law_sz = 1ull << __ilog2_u64(law_sz);
  189. /* do we still have anything to map */
  190. sz = sz - law_sz;
  191. if (sz) {
  192. start += law_sz;
  193. start_align = 1ull << (ffs64(start) - 1);
  194. law_sz = min(start_align, sz);
  195. law_sz_enc = __ilog2_u64(law_sz) - 1;
  196. if (set_last_law(start, law_sz_enc, id) < 0)
  197. return -1;
  198. } else {
  199. return 0;
  200. }
  201. /* do we still have anything to map */
  202. sz = sz - law_sz;
  203. if (sz)
  204. return 1;
  205. return 0;
  206. }
  207. #endif
  208. void init_laws(void)
  209. {
  210. int i;
  211. #if FSL_HW_NUM_LAWS < 32
  212. gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
  213. #elif FSL_HW_NUM_LAWS == 32
  214. gd->used_laws = 0;
  215. #else
  216. #error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
  217. #endif
  218. for (i = 0; i < num_law_entries; i++) {
  219. if (law_table[i].index == -1)
  220. set_next_law(law_table[i].addr, law_table[i].size,
  221. law_table[i].trgt_id);
  222. else
  223. set_law(law_table[i].index, law_table[i].addr,
  224. law_table[i].size, law_table[i].trgt_id);
  225. }
  226. return ;
  227. }