1c0474d58SKonstantin Porotchkin /* 2c0474d58SKonstantin Porotchkin * Copyright (C) 2016 - 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 /* IOW unit device driver for Marvell CP110 and CP115 SoCs */ 9c0474d58SKonstantin Porotchkin 10c0474d58SKonstantin Porotchkin #include <arch_helpers.h> 11*09d40e0eSAntonio Nino Diaz #include <common/debug.h> 12*09d40e0eSAntonio Nino Diaz #include <drivers/marvell/iob.h> 13*09d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 14*09d40e0eSAntonio Nino Diaz 15*09d40e0eSAntonio Nino Diaz #include <armada_common.h> 16c0474d58SKonstantin Porotchkin #include <mvebu.h> 17c0474d58SKonstantin Porotchkin #include <mvebu_def.h> 18c0474d58SKonstantin Porotchkin 19c0474d58SKonstantin Porotchkin #if LOG_LEVEL >= LOG_LEVEL_INFO 20c0474d58SKonstantin Porotchkin #define DEBUG_ADDR_MAP 21c0474d58SKonstantin Porotchkin #endif 22c0474d58SKonstantin Porotchkin 23c0474d58SKonstantin Porotchkin #define MVEBU_IOB_OFFSET (0x190000) 24c0474d58SKonstantin Porotchkin #define MVEBU_IOB_MAX_WINS 16 25c0474d58SKonstantin Porotchkin 26c0474d58SKonstantin Porotchkin /* common defines */ 27c0474d58SKonstantin Porotchkin #define WIN_ENABLE_BIT (0x1) 28c0474d58SKonstantin Porotchkin /* Physical address of the base of the window = {AddrLow[19:0],20`h0} */ 29c0474d58SKonstantin Porotchkin #define ADDRESS_SHIFT (20 - 4) 30c0474d58SKonstantin Porotchkin #define ADDRESS_MASK (0xFFFFFFF0) 31c0474d58SKonstantin Porotchkin #define IOB_WIN_ALIGNMENT (0x100000) 32c0474d58SKonstantin Porotchkin 33c0474d58SKonstantin Porotchkin /* IOB registers */ 34c0474d58SKonstantin Porotchkin #define IOB_WIN_CR_OFFSET(win) (iob_base + 0x0 + (0x20 * win)) 35c0474d58SKonstantin Porotchkin #define IOB_TARGET_ID_OFFSET (8) 36c0474d58SKonstantin Porotchkin #define IOB_TARGET_ID_MASK (0xF) 37c0474d58SKonstantin Porotchkin 38c0474d58SKonstantin Porotchkin #define IOB_WIN_SCR_OFFSET(win) (iob_base + 0x4 + (0x20 * win)) 39c0474d58SKonstantin Porotchkin #define IOB_WIN_ENA_CTRL_WRITE_SECURE (0x1) 40c0474d58SKonstantin Porotchkin #define IOB_WIN_ENA_CTRL_READ_SECURE (0x2) 41c0474d58SKonstantin Porotchkin #define IOB_WIN_ENA_WRITE_SECURE (0x4) 42c0474d58SKonstantin Porotchkin #define IOB_WIN_ENA_READ_SECURE (0x8) 43c0474d58SKonstantin Porotchkin 44c0474d58SKonstantin Porotchkin #define IOB_WIN_ALR_OFFSET(win) (iob_base + 0x8 + (0x20 * win)) 45c0474d58SKonstantin Porotchkin #define IOB_WIN_AHR_OFFSET(win) (iob_base + 0xC + (0x20 * win)) 46c0474d58SKonstantin Porotchkin 47c0474d58SKonstantin Porotchkin uintptr_t iob_base; 48c0474d58SKonstantin Porotchkin 49c0474d58SKonstantin Porotchkin static void iob_win_check(struct addr_map_win *win, uint32_t win_num) 50c0474d58SKonstantin Porotchkin { 51c0474d58SKonstantin Porotchkin /* check if address is aligned to the size */ 52c0474d58SKonstantin Porotchkin if (IS_NOT_ALIGN(win->base_addr, IOB_WIN_ALIGNMENT)) { 53c0474d58SKonstantin Porotchkin win->base_addr = ALIGN_UP(win->base_addr, IOB_WIN_ALIGNMENT); 54c0474d58SKonstantin Porotchkin ERROR("Window %d: base address unaligned to 0x%x\n", 55c0474d58SKonstantin Porotchkin win_num, IOB_WIN_ALIGNMENT); 5639b6cc66SAntonio Nino Diaz printf("Align up the base address to 0x%llx\n", 57c0474d58SKonstantin Porotchkin win->base_addr); 58c0474d58SKonstantin Porotchkin } 59c0474d58SKonstantin Porotchkin 60c0474d58SKonstantin Porotchkin /* size parameter validity check */ 61c0474d58SKonstantin Porotchkin if (IS_NOT_ALIGN(win->win_size, IOB_WIN_ALIGNMENT)) { 62c0474d58SKonstantin Porotchkin win->win_size = ALIGN_UP(win->win_size, IOB_WIN_ALIGNMENT); 63c0474d58SKonstantin Porotchkin ERROR("Window %d: window size unaligned to 0x%x\n", win_num, 64c0474d58SKonstantin Porotchkin IOB_WIN_ALIGNMENT); 6539b6cc66SAntonio Nino Diaz printf("Aligning size to 0x%llx\n", win->win_size); 66c0474d58SKonstantin Porotchkin } 67c0474d58SKonstantin Porotchkin } 68c0474d58SKonstantin Porotchkin 69c0474d58SKonstantin Porotchkin static void iob_enable_win(struct addr_map_win *win, uint32_t win_id) 70c0474d58SKonstantin Porotchkin { 71c0474d58SKonstantin Porotchkin uint32_t iob_win_reg; 72c0474d58SKonstantin Porotchkin uint32_t alr, ahr; 73c0474d58SKonstantin Porotchkin uint64_t end_addr; 74c0474d58SKonstantin Porotchkin 75c0474d58SKonstantin Porotchkin end_addr = (win->base_addr + win->win_size - 1); 76c0474d58SKonstantin Porotchkin alr = (uint32_t)((win->base_addr >> ADDRESS_SHIFT) & ADDRESS_MASK); 77c0474d58SKonstantin Porotchkin ahr = (uint32_t)((end_addr >> ADDRESS_SHIFT) & ADDRESS_MASK); 78c0474d58SKonstantin Porotchkin 79c0474d58SKonstantin Porotchkin mmio_write_32(IOB_WIN_ALR_OFFSET(win_id), alr); 80c0474d58SKonstantin Porotchkin mmio_write_32(IOB_WIN_AHR_OFFSET(win_id), ahr); 81c0474d58SKonstantin Porotchkin 82c0474d58SKonstantin Porotchkin iob_win_reg = WIN_ENABLE_BIT; 83c0474d58SKonstantin Porotchkin iob_win_reg |= (win->target_id & IOB_TARGET_ID_MASK) 84c0474d58SKonstantin Porotchkin << IOB_TARGET_ID_OFFSET; 85c0474d58SKonstantin Porotchkin mmio_write_32(IOB_WIN_CR_OFFSET(win_id), iob_win_reg); 86c0474d58SKonstantin Porotchkin 87c0474d58SKonstantin Porotchkin } 88c0474d58SKonstantin Porotchkin 89c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP 90c0474d58SKonstantin Porotchkin static void dump_iob(void) 91c0474d58SKonstantin Porotchkin { 92c0474d58SKonstantin Porotchkin uint32_t win_id, win_cr, alr, ahr; 93c0474d58SKonstantin Porotchkin uint8_t target_id; 94c0474d58SKonstantin Porotchkin uint64_t start, end; 95c0474d58SKonstantin Porotchkin char *iob_target_name[IOB_MAX_TID] = { 96c0474d58SKonstantin Porotchkin "CFG ", "MCI0 ", "PEX1 ", "PEX2 ", 97c0474d58SKonstantin Porotchkin "PEX0 ", "NAND ", "RUNIT", "MCI1 " }; 98c0474d58SKonstantin Porotchkin 99c0474d58SKonstantin Porotchkin /* Dump all IOB windows */ 10039b6cc66SAntonio Nino Diaz printf("bank id target start end\n"); 10139b6cc66SAntonio Nino Diaz printf("----------------------------------------------------\n"); 102c0474d58SKonstantin Porotchkin for (win_id = 0; win_id < MVEBU_IOB_MAX_WINS; win_id++) { 103c0474d58SKonstantin Porotchkin win_cr = mmio_read_32(IOB_WIN_CR_OFFSET(win_id)); 104c0474d58SKonstantin Porotchkin if (win_cr & WIN_ENABLE_BIT) { 105c0474d58SKonstantin Porotchkin target_id = (win_cr >> IOB_TARGET_ID_OFFSET) & 106c0474d58SKonstantin Porotchkin IOB_TARGET_ID_MASK; 107c0474d58SKonstantin Porotchkin alr = mmio_read_32(IOB_WIN_ALR_OFFSET(win_id)); 108c0474d58SKonstantin Porotchkin start = ((uint64_t)alr << ADDRESS_SHIFT); 109c0474d58SKonstantin Porotchkin if (win_id != 0) { 110c0474d58SKonstantin Porotchkin ahr = mmio_read_32(IOB_WIN_AHR_OFFSET(win_id)); 111c0474d58SKonstantin Porotchkin end = (((uint64_t)ahr + 0x10) << ADDRESS_SHIFT); 112c0474d58SKonstantin Porotchkin } else { 113c0474d58SKonstantin Porotchkin /* Window #0 size is hardcoded to 16MB, as it's 114c0474d58SKonstantin Porotchkin * reserved for CP configuration space. 115c0474d58SKonstantin Porotchkin */ 116c0474d58SKonstantin Porotchkin end = start + (16 << 20); 117c0474d58SKonstantin Porotchkin } 11839b6cc66SAntonio Nino Diaz printf("iob %02d %s 0x%016llx 0x%016llx\n", 119c0474d58SKonstantin Porotchkin win_id, iob_target_name[target_id], 120c0474d58SKonstantin Porotchkin start, end); 121c0474d58SKonstantin Porotchkin } 122c0474d58SKonstantin Porotchkin } 123c0474d58SKonstantin Porotchkin } 124c0474d58SKonstantin Porotchkin #endif 125c0474d58SKonstantin Porotchkin 126c0474d58SKonstantin Porotchkin void iob_cfg_space_update(int ap_idx, int cp_idx, uintptr_t base, 127c0474d58SKonstantin Porotchkin uintptr_t new_base) 128c0474d58SKonstantin Porotchkin { 129c0474d58SKonstantin Porotchkin debug_enter(); 130c0474d58SKonstantin Porotchkin 131c0474d58SKonstantin Porotchkin iob_base = base + MVEBU_IOB_OFFSET; 132c0474d58SKonstantin Porotchkin 133c0474d58SKonstantin Porotchkin NOTICE("Change the base address of AP%d-CP%d to %lx\n", 134c0474d58SKonstantin Porotchkin ap_idx, cp_idx, new_base); 135c0474d58SKonstantin Porotchkin mmio_write_32(IOB_WIN_ALR_OFFSET(0), new_base >> ADDRESS_SHIFT); 136c0474d58SKonstantin Porotchkin 137c0474d58SKonstantin Porotchkin iob_base = new_base + MVEBU_IOB_OFFSET; 138c0474d58SKonstantin Porotchkin 139c0474d58SKonstantin Porotchkin /* Make sure the address was configured by the CPU before 140c0474d58SKonstantin Porotchkin * any possible access to the CP. 141c0474d58SKonstantin Porotchkin */ 142c0474d58SKonstantin Porotchkin dsb(); 143c0474d58SKonstantin Porotchkin 144c0474d58SKonstantin Porotchkin debug_exit(); 145c0474d58SKonstantin Porotchkin } 146c0474d58SKonstantin Porotchkin 147c0474d58SKonstantin Porotchkin int init_iob(uintptr_t base) 148c0474d58SKonstantin Porotchkin { 149c0474d58SKonstantin Porotchkin struct addr_map_win *win; 150c0474d58SKonstantin Porotchkin uint32_t win_id, win_reg; 151c0474d58SKonstantin Porotchkin uint32_t win_count; 152c0474d58SKonstantin Porotchkin 153c0474d58SKonstantin Porotchkin INFO("Initializing IOB Address decoding\n"); 154c0474d58SKonstantin Porotchkin 155c0474d58SKonstantin Porotchkin /* Get the base address of the address decoding MBUS */ 156c0474d58SKonstantin Porotchkin iob_base = base + MVEBU_IOB_OFFSET; 157c0474d58SKonstantin Porotchkin 158c0474d58SKonstantin Porotchkin /* Get the array of the windows and fill the map data */ 159c0474d58SKonstantin Porotchkin marvell_get_iob_memory_map(&win, &win_count, base); 160c0474d58SKonstantin Porotchkin if (win_count <= 0) { 161c0474d58SKonstantin Porotchkin INFO("no windows configurations found\n"); 162c0474d58SKonstantin Porotchkin return 0; 163c0474d58SKonstantin Porotchkin } else if (win_count > (MVEBU_IOB_MAX_WINS - 1)) { 164c0474d58SKonstantin Porotchkin ERROR("IOB mem map array > than max available windows (%d)\n", 165c0474d58SKonstantin Porotchkin MVEBU_IOB_MAX_WINS); 166c0474d58SKonstantin Porotchkin win_count = MVEBU_IOB_MAX_WINS; 167c0474d58SKonstantin Porotchkin } 168c0474d58SKonstantin Porotchkin 169c0474d58SKonstantin Porotchkin /* disable all IOB windows, start from win_id = 1 170c0474d58SKonstantin Porotchkin * because can't disable internal register window 171c0474d58SKonstantin Porotchkin */ 172c0474d58SKonstantin Porotchkin for (win_id = 1; win_id < MVEBU_IOB_MAX_WINS; win_id++) { 173c0474d58SKonstantin Porotchkin win_reg = mmio_read_32(IOB_WIN_CR_OFFSET(win_id)); 174c0474d58SKonstantin Porotchkin win_reg &= ~WIN_ENABLE_BIT; 175c0474d58SKonstantin Porotchkin mmio_write_32(IOB_WIN_CR_OFFSET(win_id), win_reg); 176c0474d58SKonstantin Porotchkin 177c0474d58SKonstantin Porotchkin win_reg = ~IOB_WIN_ENA_CTRL_WRITE_SECURE; 178c0474d58SKonstantin Porotchkin win_reg &= ~IOB_WIN_ENA_CTRL_READ_SECURE; 179c0474d58SKonstantin Porotchkin win_reg &= ~IOB_WIN_ENA_WRITE_SECURE; 180c0474d58SKonstantin Porotchkin win_reg &= ~IOB_WIN_ENA_READ_SECURE; 181c0474d58SKonstantin Porotchkin mmio_write_32(IOB_WIN_SCR_OFFSET(win_id), win_reg); 182c0474d58SKonstantin Porotchkin } 183c0474d58SKonstantin Porotchkin 184c0474d58SKonstantin Porotchkin for (win_id = 1; win_id < win_count + 1; win_id++, win++) { 185c0474d58SKonstantin Porotchkin iob_win_check(win, win_id); 186c0474d58SKonstantin Porotchkin iob_enable_win(win, win_id); 187c0474d58SKonstantin Porotchkin } 188c0474d58SKonstantin Porotchkin 189c0474d58SKonstantin Porotchkin #ifdef DEBUG_ADDR_MAP 190c0474d58SKonstantin Porotchkin dump_iob(); 191c0474d58SKonstantin Porotchkin #endif 192c0474d58SKonstantin Porotchkin 193c0474d58SKonstantin Porotchkin INFO("Done IOB Address decoding Initializing\n"); 194c0474d58SKonstantin Porotchkin 195c0474d58SKonstantin Porotchkin return 0; 196c0474d58SKonstantin Porotchkin } 197