fsl_law.c 4.2 KB

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