ccu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (C) 2018 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. /* CCU unit device driver for Marvell AP807, AP807 and AP810 SoCs */
  8. #include <inttypes.h>
  9. #include <stdint.h>
  10. #include <common/debug.h>
  11. #include <drivers/marvell/ccu.h>
  12. #include <lib/mmio.h>
  13. #include <armada_common.h>
  14. #include <mvebu.h>
  15. #include <mvebu_def.h>
  16. #if LOG_LEVEL >= LOG_LEVEL_INFO
  17. #define DEBUG_ADDR_MAP
  18. #endif
  19. /* common defines */
  20. #define WIN_ENABLE_BIT (0x1)
  21. /* Physical address of the base of the window = {AddrLow[19:0],20'h0} */
  22. #define ADDRESS_SHIFT (20 - 4)
  23. #define ADDRESS_MASK (0xFFFFFFF0)
  24. #define CCU_WIN_ALIGNMENT (0x100000)
  25. /*
  26. * Physical address of the highest address of window bits[31:19] = 0x6FF
  27. * Physical address of the lowest address of window bits[18:6] = 0x6E0
  28. * Unit Id bits [5:2] = 2
  29. * RGF Window Enable bit[0] = 1
  30. * 0x37f9b809 - 11011111111 0011011100000 0010 0 1
  31. */
  32. #define ERRATA_WA_CCU_WIN4 0x37f9b809U
  33. /*
  34. * Physical address of the highest address of window bits[31:19] = 0xFFF
  35. * Physical address of the lowest address of window bits[18:6] = 0x800
  36. * Unit Id bits [5:2] = 2
  37. * RGF Window Enable bit[0] = 1
  38. * 0x7ffa0009 - 111111111111 0100000000000 0010 0 1
  39. */
  40. #define ERRATA_WA_CCU_WIN5 0x7ffa0009U
  41. /*
  42. * Physical address of the highest address of window bits[31:19] = 0x1FFF
  43. * Physical address of the lowest address of window bits[18:6] = 0x1000
  44. * Unit Id bits [5:2] = 2
  45. * RGF Window Enable bit[0] = 1
  46. * 0xfffc000d - 1111111111111 1000000000000 0011 0 1
  47. */
  48. #define ERRATA_WA_CCU_WIN6 0xfffc000dU
  49. #define IS_DRAM_TARGET(tgt) ((((tgt) == DRAM_0_TID) || \
  50. ((tgt) == DRAM_1_TID) || \
  51. ((tgt) == RAR_TID)) ? 1 : 0)
  52. #define CCU_RGF(win) (MVEBU_CCU_BASE(MVEBU_AP0) + \
  53. 0x90 + 4 * (win))
  54. /* For storage of CR, SCR, ALR, AHR abd GCR */
  55. static uint32_t ccu_regs_save[MVEBU_CCU_MAX_WINS * 4 + 1];
  56. #ifdef DEBUG_ADDR_MAP
  57. static void dump_ccu(int ap_index)
  58. {
  59. uint32_t win_id, win_cr, alr, ahr;
  60. uint8_t target_id;
  61. uint64_t start, end;
  62. /* Dump all AP windows */
  63. printf("\tbank target start end\n");
  64. printf("\t----------------------------------------------------\n");
  65. for (win_id = 0; win_id < MVEBU_CCU_MAX_WINS; win_id++) {
  66. win_cr = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
  67. if (win_cr & WIN_ENABLE_BIT) {
  68. target_id = (win_cr >> CCU_TARGET_ID_OFFSET) &
  69. CCU_TARGET_ID_MASK;
  70. alr = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_index,
  71. win_id));
  72. ahr = mmio_read_32(CCU_WIN_AHR_OFFSET(ap_index,
  73. win_id));
  74. start = ((uint64_t)alr << ADDRESS_SHIFT);
  75. end = (((uint64_t)ahr + 0x10) << ADDRESS_SHIFT);
  76. printf("\tccu%d %02x 0x%016" PRIx64 " 0x%016" PRIx64 "\n",
  77. win_id, target_id, start, end);
  78. }
  79. }
  80. win_cr = mmio_read_32(CCU_WIN_GCR_OFFSET(ap_index));
  81. target_id = (win_cr >> CCU_GCR_TARGET_OFFSET) & CCU_GCR_TARGET_MASK;
  82. printf("\tccu GCR %d - all other transactions\n", target_id);
  83. }
  84. #endif
  85. void ccu_win_check(struct addr_map_win *win)
  86. {
  87. /* check if address is aligned to 1M */
  88. if (IS_NOT_ALIGN(win->base_addr, CCU_WIN_ALIGNMENT)) {
  89. win->base_addr = ALIGN_UP(win->base_addr, CCU_WIN_ALIGNMENT);
  90. NOTICE("%s: Align up the base address to 0x%" PRIx64 "\n",
  91. __func__, win->base_addr);
  92. }
  93. /* size parameter validity check */
  94. if (IS_NOT_ALIGN(win->win_size, CCU_WIN_ALIGNMENT)) {
  95. win->win_size = ALIGN_UP(win->win_size, CCU_WIN_ALIGNMENT);
  96. NOTICE("%s: Aligning size to 0x%" PRIx64 "\n",
  97. __func__, win->win_size);
  98. }
  99. }
  100. int ccu_is_win_enabled(int ap_index, uint32_t win_id)
  101. {
  102. return mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id)) &
  103. WIN_ENABLE_BIT;
  104. }
  105. void ccu_enable_win(int ap_index, struct addr_map_win *win, uint32_t win_id)
  106. {
  107. uint32_t ccu_win_reg;
  108. uint32_t alr, ahr;
  109. uint64_t end_addr;
  110. if ((win_id == 0) || (win_id > MVEBU_CCU_MAX_WINS)) {
  111. ERROR("Enabling wrong CCU window %d!\n", win_id);
  112. return;
  113. }
  114. end_addr = (win->base_addr + win->win_size - 1);
  115. alr = (uint32_t)((win->base_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
  116. ahr = (uint32_t)((end_addr >> ADDRESS_SHIFT) & ADDRESS_MASK);
  117. mmio_write_32(CCU_WIN_ALR_OFFSET(ap_index, win_id), alr);
  118. mmio_write_32(CCU_WIN_AHR_OFFSET(ap_index, win_id), ahr);
  119. ccu_win_reg = WIN_ENABLE_BIT;
  120. ccu_win_reg |= (win->target_id & CCU_TARGET_ID_MASK)
  121. << CCU_TARGET_ID_OFFSET;
  122. mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), ccu_win_reg);
  123. }
  124. static void ccu_disable_win(int ap_index, uint32_t win_id)
  125. {
  126. uint32_t win_reg;
  127. if ((win_id == 0) || (win_id > MVEBU_CCU_MAX_WINS)) {
  128. ERROR("Disabling wrong CCU window %d!\n", win_id);
  129. return;
  130. }
  131. win_reg = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
  132. win_reg &= ~WIN_ENABLE_BIT;
  133. mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), win_reg);
  134. }
  135. /* Insert/Remove temporary window for using the out-of reset default
  136. * CPx base address to access the CP configuration space prior to
  137. * the further base address update in accordance with address mapping
  138. * design.
  139. *
  140. * NOTE: Use the same window array for insertion and removal of
  141. * temporary windows.
  142. */
  143. void ccu_temp_win_insert(int ap_index, struct addr_map_win *win, int size)
  144. {
  145. uint32_t win_id;
  146. for (int i = 0; i < size; i++) {
  147. win_id = MVEBU_CCU_MAX_WINS - 1 - i;
  148. ccu_win_check(win);
  149. ccu_enable_win(ap_index, win, win_id);
  150. win++;
  151. }
  152. }
  153. /*
  154. * NOTE: Use the same window array for insertion and removal of
  155. * temporary windows.
  156. */
  157. void ccu_temp_win_remove(int ap_index, struct addr_map_win *win, int size)
  158. {
  159. uint32_t win_id;
  160. for (int i = 0; i < size; i++) {
  161. uint64_t base;
  162. uint32_t target;
  163. win_id = MVEBU_CCU_MAX_WINS - 1 - i;
  164. target = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
  165. target >>= CCU_TARGET_ID_OFFSET;
  166. target &= CCU_TARGET_ID_MASK;
  167. base = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_index, win_id));
  168. base <<= ADDRESS_SHIFT;
  169. if ((win->target_id != target) || (win->base_addr != base)) {
  170. ERROR("%s: Trying to remove bad window-%d!\n",
  171. __func__, win_id);
  172. continue;
  173. }
  174. ccu_disable_win(ap_index, win_id);
  175. win++;
  176. }
  177. }
  178. /* Returns current DRAM window target (DRAM_0_TID, DRAM_1_TID, RAR_TID)
  179. * NOTE: Call only once for each AP.
  180. * The AP0 DRAM window is located at index 2 only at the BL31 execution start.
  181. * Then it relocated to index 1 for matching the rest of APs DRAM settings.
  182. * Calling this function after relocation will produce wrong results on AP0
  183. */
  184. static uint32_t ccu_dram_target_get(int ap_index)
  185. {
  186. /* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
  187. * All the rest of detected APs will use window at index 1.
  188. * The AP0 DRAM window is moved from index 2 to 1 during
  189. * init_ccu() execution.
  190. */
  191. const uint32_t win_id = (ap_index == 0) ? 2 : 1;
  192. uint32_t target;
  193. target = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
  194. target >>= CCU_TARGET_ID_OFFSET;
  195. target &= CCU_TARGET_ID_MASK;
  196. return target;
  197. }
  198. void ccu_dram_target_set(int ap_index, uint32_t target)
  199. {
  200. /* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
  201. * All the rest of detected APs will use window at index 1.
  202. * The AP0 DRAM window is moved from index 2 to 1
  203. * during init_ccu() execution.
  204. */
  205. const uint32_t win_id = (ap_index == 0) ? 2 : 1;
  206. uint32_t dram_cr;
  207. dram_cr = mmio_read_32(CCU_WIN_CR_OFFSET(ap_index, win_id));
  208. dram_cr &= ~(CCU_TARGET_ID_MASK << CCU_TARGET_ID_OFFSET);
  209. dram_cr |= (target & CCU_TARGET_ID_MASK) << CCU_TARGET_ID_OFFSET;
  210. mmio_write_32(CCU_WIN_CR_OFFSET(ap_index, win_id), dram_cr);
  211. }
  212. /* Setup CCU DRAM window and enable it */
  213. void ccu_dram_win_config(int ap_index, struct addr_map_win *win)
  214. {
  215. #if IMAGE_BLE /* BLE */
  216. /* On BLE stage the AP0 DRAM window is opened by the BootROM at index 2.
  217. * Since the BootROM is not accessing DRAM at BLE stage,
  218. * the DRAM window can be temporarely disabled.
  219. */
  220. const uint32_t win_id = (ap_index == 0) ? 2 : 1;
  221. #else /* end of BLE */
  222. /* At the ccu_init() execution stage, DRAM windows of all APs
  223. * are arranged at index 1.
  224. * The AP0 still has the old window BootROM DRAM at index 2, so
  225. * the window-1 can be safely disabled without breaking the DRAM access.
  226. */
  227. const uint32_t win_id = 1;
  228. #endif
  229. ccu_disable_win(ap_index, win_id);
  230. /* enable write secure (and clear read secure) */
  231. mmio_write_32(CCU_WIN_SCR_OFFSET(ap_index, win_id),
  232. CCU_WIN_ENA_WRITE_SECURE);
  233. ccu_win_check(win);
  234. ccu_enable_win(ap_index, win, win_id);
  235. }
  236. /* Save content of CCU window + GCR */
  237. static void ccu_save_win_range(int ap_id, int win_first,
  238. int win_last, uint32_t *buffer)
  239. {
  240. int win_id, idx;
  241. /* Save CCU */
  242. for (idx = 0, win_id = win_first; win_id <= win_last; win_id++) {
  243. buffer[idx++] = mmio_read_32(CCU_WIN_CR_OFFSET(ap_id, win_id));
  244. buffer[idx++] = mmio_read_32(CCU_WIN_SCR_OFFSET(ap_id, win_id));
  245. buffer[idx++] = mmio_read_32(CCU_WIN_ALR_OFFSET(ap_id, win_id));
  246. buffer[idx++] = mmio_read_32(CCU_WIN_AHR_OFFSET(ap_id, win_id));
  247. }
  248. buffer[idx] = mmio_read_32(CCU_WIN_GCR_OFFSET(ap_id));
  249. }
  250. /* Restore content of CCU window + GCR */
  251. static void ccu_restore_win_range(int ap_id, int win_first,
  252. int win_last, uint32_t *buffer)
  253. {
  254. int win_id, idx;
  255. /* Restore CCU */
  256. for (idx = 0, win_id = win_first; win_id <= win_last; win_id++) {
  257. mmio_write_32(CCU_WIN_CR_OFFSET(ap_id, win_id), buffer[idx++]);
  258. mmio_write_32(CCU_WIN_SCR_OFFSET(ap_id, win_id), buffer[idx++]);
  259. mmio_write_32(CCU_WIN_ALR_OFFSET(ap_id, win_id), buffer[idx++]);
  260. mmio_write_32(CCU_WIN_AHR_OFFSET(ap_id, win_id), buffer[idx++]);
  261. }
  262. mmio_write_32(CCU_WIN_GCR_OFFSET(ap_id), buffer[idx]);
  263. }
  264. void ccu_save_win_all(int ap_id)
  265. {
  266. ccu_save_win_range(ap_id, 0, MVEBU_CCU_MAX_WINS - 1, ccu_regs_save);
  267. }
  268. void ccu_restore_win_all(int ap_id)
  269. {
  270. ccu_restore_win_range(ap_id, 0, MVEBU_CCU_MAX_WINS - 1, ccu_regs_save);
  271. }
  272. int init_ccu(int ap_index)
  273. {
  274. struct addr_map_win *win, *dram_win;
  275. uint32_t win_id, win_reg;
  276. uint32_t win_count, array_id;
  277. uint32_t dram_target;
  278. #if IMAGE_BLE
  279. /* In BootROM context CCU Window-1
  280. * has SRAM_TID target and should not be disabled
  281. */
  282. const uint32_t win_start = 2;
  283. #else
  284. const uint32_t win_start = 1;
  285. #endif
  286. INFO("Initializing CCU Address decoding\n");
  287. /* Get the array of the windows and fill the map data */
  288. marvell_get_ccu_memory_map(ap_index, &win, &win_count);
  289. if (win_count <= 0) {
  290. INFO("No windows configurations found\n");
  291. } else if (win_count > (MVEBU_CCU_MAX_WINS - 1)) {
  292. ERROR("CCU mem map array > than max available windows (%d)\n",
  293. MVEBU_CCU_MAX_WINS);
  294. win_count = MVEBU_CCU_MAX_WINS;
  295. }
  296. /* Need to set GCR to DRAM before all CCU windows are disabled for
  297. * securing the normal access to DRAM location, which the ATF is running
  298. * from. Once all CCU windows are set, which have to include the
  299. * dedicated DRAM window as well, the GCR can be switched to the target
  300. * defined by the platform configuration.
  301. */
  302. dram_target = ccu_dram_target_get(ap_index);
  303. win_reg = (dram_target & CCU_GCR_TARGET_MASK) << CCU_GCR_TARGET_OFFSET;
  304. mmio_write_32(CCU_WIN_GCR_OFFSET(ap_index), win_reg);
  305. /* If the DRAM window was already configured at the BLE stage,
  306. * only the window target considered valid, the address range should be
  307. * updated according to the platform configuration.
  308. */
  309. for (dram_win = win, array_id = 0; array_id < win_count;
  310. array_id++, dram_win++) {
  311. if (IS_DRAM_TARGET(dram_win->target_id)) {
  312. dram_win->target_id = dram_target;
  313. break;
  314. }
  315. }
  316. /* Disable all AP CCU windows
  317. * Window-0 is always bypassed since it already contains
  318. * data allowing the internal configuration space access
  319. */
  320. for (win_id = win_start; win_id < MVEBU_CCU_MAX_WINS; win_id++) {
  321. ccu_disable_win(ap_index, win_id);
  322. /* enable write secure (and clear read secure) */
  323. mmio_write_32(CCU_WIN_SCR_OFFSET(ap_index, win_id),
  324. CCU_WIN_ENA_WRITE_SECURE);
  325. }
  326. /* win_id is the index of the current ccu window
  327. * array_id is the index of the current memory map window entry
  328. */
  329. for (win_id = win_start, array_id = 0;
  330. ((win_id < MVEBU_CCU_MAX_WINS) && (array_id < win_count));
  331. win_id++) {
  332. ccu_win_check(win);
  333. ccu_enable_win(ap_index, win, win_id);
  334. win++;
  335. array_id++;
  336. }
  337. /* Get & set the default target according to board topology */
  338. win_reg = (marvell_get_ccu_gcr_target(ap_index) & CCU_GCR_TARGET_MASK)
  339. << CCU_GCR_TARGET_OFFSET;
  340. mmio_write_32(CCU_WIN_GCR_OFFSET(ap_index), win_reg);
  341. #ifdef DEBUG_ADDR_MAP
  342. dump_ccu(ap_index);
  343. #endif
  344. INFO("Done CCU Address decoding Initializing\n");
  345. return 0;
  346. }
  347. void errata_wa_init(void)
  348. {
  349. /*
  350. * EERATA ID: RES-3033912 - Internal Address Space Init state causes
  351. * a hang upon accesses to [0xf070_0000, 0xf07f_ffff]
  352. * Workaround: Boot Firmware (ATF) should configure CCU_RGF_WIN(4) to
  353. * split [0x6e_0000, 0x1ff_ffff] to values [0x6e_0000, 0x6f_ffff] and
  354. * [0x80_0000, 0xff_ffff] and [0x100_0000, 0x1ff_ffff],that cause
  355. * accesses to the segment of [0xf070_0000, 0xf1ff_ffff]
  356. * to act as RAZWI.
  357. */
  358. mmio_write_32(CCU_RGF(4), ERRATA_WA_CCU_WIN4);
  359. mmio_write_32(CCU_RGF(5), ERRATA_WA_CCU_WIN5);
  360. mmio_write_32(CCU_RGF(6), ERRATA_WA_CCU_WIN6);
  361. }