fsl_law.c 6.9 KB

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