fsl_law.c 6.8 KB

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