1c0474d58SKonstantin Porotchkin /* 2c0474d58SKonstantin Porotchkin * Copyright (C) 2018 Marvell International Ltd. 3c0474d58SKonstantin Porotchkin * 4c0474d58SKonstantin Porotchkin * SPDX-License-Identifier: BSD-3-Clause 5c0474d58SKonstantin Porotchkin * https://spdx.org/licenses 6c0474d58SKonstantin Porotchkin */ 7c0474d58SKonstantin Porotchkin 8c0474d58SKonstantin Porotchkin /* GWIN unit device driver for Marvell AP810 SoC */ 9c0474d58SKonstantin Porotchkin 10*94d6dd67SKonstantin Porotchkin #include <armada_common.h> 11c0474d58SKonstantin Porotchkin #include <debug.h> 12c0474d58SKonstantin Porotchkin #include <gwin.h> 13c0474d58SKonstantin Porotchkin #include <mmio.h> 14c0474d58SKonstantin Porotchkin #include <mvebu.h> 15c0474d58SKonstantin Porotchkin #include <mvebu_def.h> 16c0474d58SKonstantin Porotchkin 17c0474d58SKonstantin Porotchkin #if LOG_LEVEL >= LOG_LEVEL_INFO 18c0474d58SKonstantin Porotchkin #define DEBUG_ADDR_MAP 19c0474d58SKonstantin Porotchkin #endif 20c0474d58SKonstantin Porotchkin 21c0474d58SKonstantin Porotchkin /* common defines */ 22c0474d58SKonstantin Porotchkin #define WIN_ENABLE_BIT (0x1) 23c0474d58SKonstantin Porotchkin #define WIN_TARGET_MASK (0xF) 24c0474d58SKonstantin Porotchkin #define WIN_TARGET_SHIFT (0x8) 25c0474d58SKonstantin Porotchkin #define WIN_TARGET(tgt) (((tgt) & WIN_TARGET_MASK) \ 26c0474d58SKonstantin Porotchkin << WIN_TARGET_SHIFT) 27c0474d58SKonstantin Porotchkin 28c0474d58SKonstantin Porotchkin /* Bits[43:26] of the physical address are the window base, 29c0474d58SKonstantin Porotchkin * which is aligned to 64MB 30c0474d58SKonstantin Porotchkin */ 31c0474d58SKonstantin Porotchkin #define ADDRESS_RSHIFT (26) 32c0474d58SKonstantin Porotchkin #define ADDRESS_LSHIFT (10) 33c0474d58SKonstantin Porotchkin #define GWIN_ALIGNMENT_64M (0x4000000) 34c0474d58SKonstantin Porotchkin 35c0474d58SKonstantin Porotchkin /* AP registers */ 36c0474d58SKonstantin Porotchkin #define GWIN_CR_OFFSET(ap, win) (MVEBU_GWIN_BASE(ap) + 0x0 + \ 37c0474d58SKonstantin Porotchkin (0x10 * (win))) 38c0474d58SKonstantin Porotchkin #define GWIN_ALR_OFFSET(ap, win) (MVEBU_GWIN_BASE(ap) + 0x8 + \ 39c0474d58SKonstantin Porotchkin (0x10 * (win))) 40c0474d58SKonstantin Porotchkin #define GWIN_AHR_OFFSET(ap, win) (MVEBU_GWIN_BASE(ap) + 0xc + \ 41c0474d58SKonstantin Porotchkin (0x10 * (win))) 42c0474d58SKonstantin Porotchkin 43c0474d58SKonstantin Porotchkin #define CCU_GRU_CR_OFFSET(ap) (MVEBU_CCU_GRU_BASE(ap)) 44c0474d58SKonstantin Porotchkin #define CCR_GRU_CR_GWIN_MBYPASS (1 << 1) 45c0474d58SKonstantin Porotchkin 46c0474d58SKonstantin Porotchkin static void gwin_check(struct addr_map_win *win) 47c0474d58SKonstantin Porotchkin { 48c0474d58SKonstantin Porotchkin /* The base is always 64M aligned */ 49c0474d58SKonstantin Porotchkin if (IS_NOT_ALIGN(win->base_addr, GWIN_ALIGNMENT_64M)) { 50c0474d58SKonstantin Porotchkin win->base_addr &= ~(GWIN_ALIGNMENT_64M - 1); 51c0474d58SKonstantin Porotchkin NOTICE("%s: Align the base address to 0x%llx\n", 52c0474d58SKonstantin Porotchkin __func__, win->base_addr); 53c0474d58SKonstantin Porotchkin } 54c0474d58SKonstantin Porotchkin 55c0474d58SKonstantin Porotchkin /* size parameter validity check */ 56c0474d58SKonstantin Porotchkin if (IS_NOT_ALIGN(win->win_size, GWIN_ALIGNMENT_64M)) { 57c0474d58SKonstantin Porotchkin win->win_size = ALIGN_UP(win->win_size, GWIN_ALIGNMENT_64M); 58c0474d58SKonstantin Porotchkin NOTICE("%s: Aligning window size to 0x%llx\n", 59c0474d58SKonstantin Porotchkin __func__, win->win_size); 60c0474d58SKonstantin Porotchkin } 61c0474d58SKonstantin Porotchkin } 62c0474d58SKonstantin Porotchkin 63c0474d58SKonstantin Porotchkin static void gwin_enable_window(int ap_index, struct addr_map_win *win, 64c0474d58SKonstantin Porotchkin uint32_t win_num) 65c0474d58SKonstantin Porotchkin { 66c0474d58SKonstantin Porotchkin uint32_t alr, ahr; 67c0474d58SKonstantin Porotchkin uint64_t end_addr; 68c0474d58SKonstantin Porotchkin 69c0474d58SKonstantin Porotchkin if ((win->target_id & WIN_TARGET_MASK) != win->target_id) { 70c0474d58SKonstantin Porotchkin ERROR("target ID = %d, is invalid\n", win->target_id); 71c0474d58SKonstantin Porotchkin return; 72c0474d58SKonstantin Porotchkin } 73c0474d58SKonstantin Porotchkin 74c0474d58SKonstantin Porotchkin /* calculate 64bit end-address */ 75c0474d58SKonstantin Porotchkin end_addr = (win->base_addr + win->win_size - 1); 76c0474d58SKonstantin Porotchkin 77c0474d58SKonstantin Porotchkin alr = (uint32_t)((win->base_addr >> ADDRESS_RSHIFT) << ADDRESS_LSHIFT); 78c0474d58SKonstantin Porotchkin ahr = (uint32_t)((end_addr >> ADDRESS_RSHIFT) << ADDRESS_LSHIFT); 79c0474d58SKonstantin Porotchkin 80c0474d58SKonstantin Porotchkin /* write start address and end address for GWIN */ 81c0474d58SKonstantin Porotchkin mmio_write_32(GWIN_ALR_OFFSET(ap_index, win_num), alr); 82c0474d58SKonstantin Porotchkin mmio_write_32(GWIN_AHR_OFFSET(ap_index, win_num), ahr); 83c0474d58SKonstantin Porotchkin 84c0474d58SKonstantin Porotchkin /* write the target ID and enable the window */ 85c0474d58SKonstantin Porotchkin mmio_write_32(GWIN_CR_OFFSET(ap_index, win_num), 86c0474d58SKonstantin Porotchkin WIN_TARGET(win->target_id) | WIN_ENABLE_BIT); 87c0474d58SKonstantin Porotchkin } 88c0474d58SKonstantin Porotchkin 89c0474d58SKonstantin Porotchkin static void gwin_disable_window(int ap_index, uint32_t win_num) 90c0474d58SKonstantin Porotchkin { 91c0474d58SKonstantin Porotchkin uint32_t win_reg; 92c0474d58SKonstantin Porotchkin 93c0474d58SKonstantin Porotchkin win_reg = mmio_read_32(GWIN_CR_OFFSET(ap_index, win_num)); 94c0474d58SKonstantin Porotchkin win_reg &= ~WIN_ENABLE_BIT; 95c0474d58SKonstantin Porotchkin mmio_write_32(GWIN_CR_OFFSET(ap_index, win_num), win_reg); 96c0474d58SKonstantin Porotchkin } 97c0474d58SKonstantin Porotchkin 98c0474d58SKonstantin Porotchkin /* Insert/Remove temporary window for using the out-of reset default 99c0474d58SKonstantin Porotchkin * CPx base address to access the CP configuration space prior to 100c0474d58SKonstantin Porotchkin * the further base address update in accordance with address mapping 101c0474d58SKonstantin Porotchkin * design. 102c0474d58SKonstantin Porotchkin * 103c0474d58SKonstantin Porotchkin * NOTE: Use the same window array for insertion and removal of 104c0474d58SKonstantin Porotchkin * temporary windows. 105c0474d58SKonstantin Porotchkin */ 106c0474d58SKonstantin Porotchkin void gwin_temp_win_insert(int ap_index, struct addr_map_win *win, int size) 107c0474d58SKonstantin Porotchkin { 108c0474d58SKonstantin Porotchkin uint32_t win_id; 109c0474d58SKonstantin Porotchkin 110c0474d58SKonstantin Porotchkin for (int i = 0; i < size; i++) { 111c0474d58SKonstantin Porotchkin win_id = MVEBU_GWIN_MAX_WINS - i - 1; 112c0474d58SKonstantin Porotchkin gwin_check(win); 113c0474d58SKonstantin Porotchkin gwin_enable_window(ap_index, win, win_id); 114c0474d58SKonstantin Porotchkin win++; 115c0474d58SKonstantin Porotchkin } 116c0474d58SKonstantin Porotchkin } 117c0474d58SKonstantin Porotchkin 118c0474d58SKonstantin Porotchkin /* 119c0474d58SKonstantin Porotchkin * NOTE: Use the same window array for insertion and removal of 120c0474d58SKonstantin Porotchkin * temporary windows. 121c0474d58SKonstantin Porotchkin */ 122c0474d58SKonstantin Porotchkin void gwin_temp_win_remove(int ap_index, struct addr_map_win *win, int size) 123c0474d58SKonstantin Porotchkin { 124c0474d58SKonstantin Porotchkin uint32_t win_id; 125c0474d58SKonstantin Porotchkin 126c0474d58SKonstantin Porotchkin for (int i = 0; i < size; i++) { 127c0474d58SKonstantin Porotchkin uint64_t base; 128c0474d58SKonstantin Porotchkin uint32_t target; 129c0474d58SKonstantin Porotchkin 130c0474d58SKonstantin Porotchkin win_id = MVEBU_GWIN_MAX_WINS - i - 1; 131c0474d58SKonstantin Porotchkin 132c0474d58SKonstantin Porotchkin target = mmio_read_32(GWIN_CR_OFFSET(ap_index, win_id)); 133c0474d58SKonstantin Porotchkin target >>= WIN_TARGET_SHIFT; 134c0474d58SKonstantin Porotchkin target &= WIN_TARGET_MASK; 135c0474d58SKonstantin Porotchkin 136c0474d58SKonstantin Porotchkin base = mmio_read_32(GWIN_ALR_OFFSET(ap_index, win_id)); 137c0474d58SKonstantin Porotchkin base >>= ADDRESS_LSHIFT; 138c0474d58SKonstantin Porotchkin base <<= ADDRESS_RSHIFT; 139c0474d58SKonstantin Porotchkin 140c0474d58SKonstantin Porotchkin if (win->target_id != target) { 141c0474d58SKonstantin Porotchkin ERROR("%s: Trying to remove bad window-%d!\n", 142c0474d58SKonstantin Porotchkin __func__, win_id); 143c0474d58SKonstantin Porotchkin continue; 144c0474d58SKonstantin Porotchkin } 145c0474d58SKonstantin Porotchkin gwin_disable_window(ap_index, win_id); 146c0474d58SKonstantin Porotchkin win++; 147c0474d58SKonstantin Porotchkin } 148c0474d58SKonstantin Porotchkin } 149c0474d58SKonstantin Porotchkin 150c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP 151c0474d58SKonstantin Porotchkin static void dump_gwin(int ap_index) 152c0474d58SKonstantin Porotchkin { 153c0474d58SKonstantin Porotchkin uint32_t win_num; 154c0474d58SKonstantin Porotchkin 155c0474d58SKonstantin Porotchkin /* Dump all GWIN windows */ 15639b6cc66SAntonio Nino Diaz printf("\tbank target start end\n"); 15739b6cc66SAntonio Nino Diaz printf("\t----------------------------------------------------\n"); 158c0474d58SKonstantin Porotchkin for (win_num = 0; win_num < MVEBU_GWIN_MAX_WINS; win_num++) { 159c0474d58SKonstantin Porotchkin uint32_t cr; 160c0474d58SKonstantin Porotchkin uint64_t alr, ahr; 161c0474d58SKonstantin Porotchkin 162c0474d58SKonstantin Porotchkin cr = mmio_read_32(GWIN_CR_OFFSET(ap_index, win_num)); 163c0474d58SKonstantin Porotchkin /* Window enabled */ 164c0474d58SKonstantin Porotchkin if (cr & WIN_ENABLE_BIT) { 165c0474d58SKonstantin Porotchkin alr = mmio_read_32(GWIN_ALR_OFFSET(ap_index, win_num)); 166c0474d58SKonstantin Porotchkin alr = (alr >> ADDRESS_LSHIFT) << ADDRESS_RSHIFT; 167c0474d58SKonstantin Porotchkin ahr = mmio_read_32(GWIN_AHR_OFFSET(ap_index, win_num)); 168c0474d58SKonstantin Porotchkin ahr = (ahr >> ADDRESS_LSHIFT) << ADDRESS_RSHIFT; 16939b6cc66SAntonio Nino Diaz printf("\tgwin %d 0x%016llx 0x%016llx\n", 170c0474d58SKonstantin Porotchkin (cr >> 8) & 0xF, alr, ahr); 171c0474d58SKonstantin Porotchkin } 172c0474d58SKonstantin Porotchkin } 173c0474d58SKonstantin Porotchkin } 174c0474d58SKonstantin Porotchkin #endif 175c0474d58SKonstantin Porotchkin 176c0474d58SKonstantin Porotchkin int init_gwin(int ap_index) 177c0474d58SKonstantin Porotchkin { 178c0474d58SKonstantin Porotchkin struct addr_map_win *win; 179c0474d58SKonstantin Porotchkin uint32_t win_id; 180c0474d58SKonstantin Porotchkin uint32_t win_count; 181c0474d58SKonstantin Porotchkin uint32_t win_reg; 182c0474d58SKonstantin Porotchkin 183c0474d58SKonstantin Porotchkin INFO("Initializing GWIN Address decoding\n"); 184c0474d58SKonstantin Porotchkin 185c0474d58SKonstantin Porotchkin /* Get the array of the windows and its size */ 186c0474d58SKonstantin Porotchkin marvell_get_gwin_memory_map(ap_index, &win, &win_count); 187c0474d58SKonstantin Porotchkin if (win_count <= 0) { 188c0474d58SKonstantin Porotchkin INFO("no windows configurations found\n"); 189c0474d58SKonstantin Porotchkin return 0; 190c0474d58SKonstantin Porotchkin } 191c0474d58SKonstantin Porotchkin 192c0474d58SKonstantin Porotchkin if (win_count > MVEBU_GWIN_MAX_WINS) { 193c0474d58SKonstantin Porotchkin ERROR("number of windows is bigger than %d\n", 194c0474d58SKonstantin Porotchkin MVEBU_GWIN_MAX_WINS); 195c0474d58SKonstantin Porotchkin return 0; 196c0474d58SKonstantin Porotchkin } 197c0474d58SKonstantin Porotchkin 198c0474d58SKonstantin Porotchkin /* disable all windows */ 199c0474d58SKonstantin Porotchkin for (win_id = 0; win_id < MVEBU_GWIN_MAX_WINS; win_id++) 200c0474d58SKonstantin Porotchkin gwin_disable_window(ap_index, win_id); 201c0474d58SKonstantin Porotchkin 202c0474d58SKonstantin Porotchkin /* enable relevant windows */ 203c0474d58SKonstantin Porotchkin for (win_id = 0; win_id < win_count; win_id++, win++) { 204c0474d58SKonstantin Porotchkin gwin_check(win); 205c0474d58SKonstantin Porotchkin gwin_enable_window(ap_index, win, win_id); 206c0474d58SKonstantin Porotchkin } 207c0474d58SKonstantin Porotchkin 208c0474d58SKonstantin Porotchkin /* GWIN Miss feature has not verified, therefore any access towards 209c0474d58SKonstantin Porotchkin * remote AP should be accompanied with proper configuration to 210c0474d58SKonstantin Porotchkin * GWIN registers group and therefore the GWIN Miss feature 211c0474d58SKonstantin Porotchkin * should be set into Bypass mode, need to make sure all GWIN regions 212c0474d58SKonstantin Porotchkin * are defined correctly that will assure no GWIN miss occurrance 213c0474d58SKonstantin Porotchkin * JIRA-AURORA2-1630 214c0474d58SKonstantin Porotchkin */ 215c0474d58SKonstantin Porotchkin INFO("Update GWIN miss bypass\n"); 216c0474d58SKonstantin Porotchkin win_reg = mmio_read_32(CCU_GRU_CR_OFFSET(ap_index)); 217c0474d58SKonstantin Porotchkin win_reg |= CCR_GRU_CR_GWIN_MBYPASS; 218c0474d58SKonstantin Porotchkin mmio_write_32(CCU_GRU_CR_OFFSET(ap_index), win_reg); 219c0474d58SKonstantin Porotchkin 220c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP 221c0474d58SKonstantin Porotchkin dump_gwin(ap_index); 222c0474d58SKonstantin Porotchkin #endif 223c0474d58SKonstantin Porotchkin 224c0474d58SKonstantin Porotchkin INFO("Done GWIN Address decoding Initializing\n"); 225c0474d58SKonstantin Porotchkin 226c0474d58SKonstantin Porotchkin return 0; 227c0474d58SKonstantin Porotchkin } 228