fsl_law.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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) || defined(CONFIG_MPC8569) || \
  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_P1011) || defined(CONFIG_P1020) || \
  40. defined(CONFIG_P2010) || defined(CONFIG_P2020)
  41. #define FSL_HW_NUM_LAWS 12
  42. #else
  43. #error FSL_HW_NUM_LAWS not defined for this platform
  44. #endif
  45. void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  46. {
  47. volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
  48. volatile u32 *lawbar = base + 8 * idx;
  49. volatile u32 *lawar = base + 8 * idx + 2;
  50. gd->used_laws |= (1 << idx);
  51. out_be32(lawar, 0);
  52. out_be32(lawbar, addr >> 12);
  53. out_be32(lawar, LAWAR_EN | ((u32)id << 20) | (u32)sz);
  54. /* Read back so that we sync the writes */
  55. in_be32(lawar);
  56. }
  57. int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  58. {
  59. u32 idx = ffz(gd->used_laws);
  60. if (idx >= FSL_HW_NUM_LAWS)
  61. return -1;
  62. set_law(idx, addr, sz, id);
  63. return idx;
  64. }
  65. int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
  66. {
  67. u32 idx;
  68. /* we have no LAWs free */
  69. if (gd->used_laws == -1)
  70. return -1;
  71. /* grab the last free law */
  72. idx = __ilog2(~(gd->used_laws));
  73. if (idx >= FSL_HW_NUM_LAWS)
  74. return -1;
  75. set_law(idx, addr, sz, id);
  76. return idx;
  77. }
  78. void disable_law(u8 idx)
  79. {
  80. volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
  81. volatile u32 *lawbar = base + 8 * idx;
  82. volatile u32 *lawar = base + 8 * idx + 2;
  83. gd->used_laws &= ~(1 << idx);
  84. out_be32(lawar, 0);
  85. out_be32(lawbar, 0);
  86. return;
  87. }
  88. void print_laws(void)
  89. {
  90. volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
  91. volatile u32 *lawbar = base;
  92. volatile u32 *lawar = base + 2;
  93. int i;
  94. printf("\nLocal Access Window Configuration\n");
  95. for(i = 0; i < FSL_HW_NUM_LAWS; i++) {
  96. printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n",
  97. i, in_be32(lawbar), i, in_be32(lawar));
  98. lawbar += 8;
  99. lawar += 8;
  100. }
  101. return;
  102. }
  103. /* use up to 2 LAWs for DDR, used the last available LAWs */
  104. int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
  105. {
  106. u64 start_align, law_sz;
  107. int law_sz_enc;
  108. if (start == 0)
  109. start_align = 1ull << (LAW_SIZE_32G + 1);
  110. else
  111. start_align = 1ull << (ffs64(start) - 1);
  112. law_sz = min(start_align, sz);
  113. law_sz_enc = __ilog2_u64(law_sz) - 1;
  114. if (set_last_law(start, law_sz_enc, id) < 0)
  115. return -1;
  116. /* recalculate size based on what was actually covered by the law */
  117. law_sz = 1ull << __ilog2_u64(law_sz);
  118. /* do we still have anything to map */
  119. sz = sz - law_sz;
  120. if (sz) {
  121. start += law_sz;
  122. start_align = 1ull << (ffs64(start) - 1);
  123. law_sz = min(start_align, sz);
  124. law_sz_enc = __ilog2_u64(law_sz) - 1;
  125. if (set_last_law(start, law_sz_enc, id) < 0)
  126. return -1;
  127. } else {
  128. return 0;
  129. }
  130. /* do we still have anything to map */
  131. sz = sz - law_sz;
  132. if (sz)
  133. return 1;
  134. return 0;
  135. }
  136. void init_laws(void)
  137. {
  138. int i;
  139. gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
  140. for (i = 0; i < num_law_entries; i++) {
  141. if (law_table[i].index == -1)
  142. set_next_law(law_table[i].addr, law_table[i].size,
  143. law_table[i].trgt_id);
  144. else
  145. set_law(law_table[i].index, law_table[i].addr,
  146. law_table[i].size, law_table[i].trgt_id);
  147. }
  148. return ;
  149. }