fsl_law.c 6.5 KB

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