1162abf1aSKONRAD Frederic /* 2162abf1aSKONRAD Frederic * GRLIB AHB APB PNP 3162abf1aSKONRAD Frederic * 4162abf1aSKONRAD Frederic * Copyright (C) 2019 AdaCore 5162abf1aSKONRAD Frederic * 6162abf1aSKONRAD Frederic * Developed by : 7162abf1aSKONRAD Frederic * Frederic Konrad <frederic.konrad@adacore.com> 8162abf1aSKONRAD Frederic * 9162abf1aSKONRAD Frederic * This program is free software; you can redistribute it and/or modify 10162abf1aSKONRAD Frederic * it under the terms of the GNU General Public License as published by 11162abf1aSKONRAD Frederic * the Free Software Foundation, either version 2 of the License, or 12162abf1aSKONRAD Frederic * (at your option) any later version. 13162abf1aSKONRAD Frederic * 14162abf1aSKONRAD Frederic * This program is distributed in the hope that it will be useful, 15162abf1aSKONRAD Frederic * but WITHOUT ANY WARRANTY; without even the implied warranty of 16162abf1aSKONRAD Frederic * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17162abf1aSKONRAD Frederic * GNU General Public License for more details. 18162abf1aSKONRAD Frederic * 19162abf1aSKONRAD Frederic * You should have received a copy of the GNU General Public License along 20162abf1aSKONRAD Frederic * with this program; if not, see <http://www.gnu.org/licenses/>. 21162abf1aSKONRAD Frederic * 22162abf1aSKONRAD Frederic */ 23162abf1aSKONRAD Frederic 24162abf1aSKONRAD Frederic #include "qemu/osdep.h" 25158b6594SPhilippe Mathieu-Daudé #include "qemu/log.h" 26162abf1aSKONRAD Frederic #include "hw/sysbus.h" 27162abf1aSKONRAD Frederic #include "hw/misc/grlib_ahb_apb_pnp.h" 28d15188ddSPhilippe Mathieu-Daudé #include "trace.h" 29162abf1aSKONRAD Frederic 30162abf1aSKONRAD Frederic #define GRLIB_PNP_VENDOR_SHIFT (24) 31162abf1aSKONRAD Frederic #define GRLIB_PNP_VENDOR_SIZE (8) 32162abf1aSKONRAD Frederic #define GRLIB_PNP_DEV_SHIFT (12) 33162abf1aSKONRAD Frederic #define GRLIB_PNP_DEV_SIZE (12) 34162abf1aSKONRAD Frederic #define GRLIB_PNP_VER_SHIFT (5) 35162abf1aSKONRAD Frederic #define GRLIB_PNP_VER_SIZE (5) 36162abf1aSKONRAD Frederic #define GRLIB_PNP_IRQ_SHIFT (0) 37162abf1aSKONRAD Frederic #define GRLIB_PNP_IRQ_SIZE (5) 38162abf1aSKONRAD Frederic #define GRLIB_PNP_ADDR_SHIFT (20) 39162abf1aSKONRAD Frederic #define GRLIB_PNP_ADDR_SIZE (12) 40162abf1aSKONRAD Frederic #define GRLIB_PNP_MASK_SHIFT (4) 41162abf1aSKONRAD Frederic #define GRLIB_PNP_MASK_SIZE (12) 42162abf1aSKONRAD Frederic 43162abf1aSKONRAD Frederic #define GRLIB_AHB_DEV_ADDR_SHIFT (20) 44162abf1aSKONRAD Frederic #define GRLIB_AHB_DEV_ADDR_SIZE (12) 45162abf1aSKONRAD Frederic #define GRLIB_AHB_ENTRY_SIZE (0x20) 46162abf1aSKONRAD Frederic #define GRLIB_AHB_MAX_DEV (64) 47162abf1aSKONRAD Frederic #define GRLIB_AHB_SLAVE_OFFSET (0x800) 48162abf1aSKONRAD Frederic 49162abf1aSKONRAD Frederic #define GRLIB_APB_DEV_ADDR_SHIFT (8) 50162abf1aSKONRAD Frederic #define GRLIB_APB_DEV_ADDR_SIZE (12) 51162abf1aSKONRAD Frederic #define GRLIB_APB_ENTRY_SIZE (0x08) 52162abf1aSKONRAD Frederic #define GRLIB_APB_MAX_DEV (512) 53162abf1aSKONRAD Frederic 54162abf1aSKONRAD Frederic #define GRLIB_PNP_MAX_REGS (0x1000) 55162abf1aSKONRAD Frederic 56162abf1aSKONRAD Frederic typedef struct AHBPnp { 57162abf1aSKONRAD Frederic SysBusDevice parent_obj; 58162abf1aSKONRAD Frederic MemoryRegion iomem; 59162abf1aSKONRAD Frederic 60162abf1aSKONRAD Frederic uint32_t regs[GRLIB_PNP_MAX_REGS >> 2]; 61162abf1aSKONRAD Frederic uint8_t master_count; 62162abf1aSKONRAD Frederic uint8_t slave_count; 63162abf1aSKONRAD Frederic } AHBPnp; 64162abf1aSKONRAD Frederic 65162abf1aSKONRAD Frederic void grlib_ahb_pnp_add_entry(AHBPnp *dev, uint32_t address, uint32_t mask, 66162abf1aSKONRAD Frederic uint8_t vendor, uint16_t device, int slave, 67162abf1aSKONRAD Frederic int type) 68162abf1aSKONRAD Frederic { 69162abf1aSKONRAD Frederic unsigned int reg_start; 70162abf1aSKONRAD Frederic 71162abf1aSKONRAD Frederic /* 72162abf1aSKONRAD Frederic * AHB entries look like this: 73162abf1aSKONRAD Frederic * 74162abf1aSKONRAD Frederic * 31 -------- 23 -------- 11 ----- 9 -------- 4 --- 0 75162abf1aSKONRAD Frederic * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ | 76162abf1aSKONRAD Frederic * -------------------------------------------------- 77162abf1aSKONRAD Frederic * | USER | 78162abf1aSKONRAD Frederic * -------------------------------------------------- 79162abf1aSKONRAD Frederic * | USER | 80162abf1aSKONRAD Frederic * -------------------------------------------------- 81162abf1aSKONRAD Frederic * | USER | 82162abf1aSKONRAD Frederic * -------------------------------------------------- 83162abf1aSKONRAD Frederic * | USER | 84162abf1aSKONRAD Frederic * -------------------------------------------------- 85162abf1aSKONRAD Frederic * 31 ----------- 20 --- 15 ----------------- 3 ---- 0 86162abf1aSKONRAD Frederic * | ADDR[31..12] | 00PC | MASK | TYPE | 87162abf1aSKONRAD Frederic * -------------------------------------------------- 88162abf1aSKONRAD Frederic * 31 ----------- 20 --- 15 ----------------- 3 ---- 0 89162abf1aSKONRAD Frederic * | ADDR[31..12] | 00PC | MASK | TYPE | 90162abf1aSKONRAD Frederic * -------------------------------------------------- 91162abf1aSKONRAD Frederic * 31 ----------- 20 --- 15 ----------------- 3 ---- 0 92162abf1aSKONRAD Frederic * | ADDR[31..12] | 00PC | MASK | TYPE | 93162abf1aSKONRAD Frederic * -------------------------------------------------- 94162abf1aSKONRAD Frederic * 31 ----------- 20 --- 15 ----------------- 3 ---- 0 95162abf1aSKONRAD Frederic * | ADDR[31..12] | 00PC | MASK | TYPE | 96162abf1aSKONRAD Frederic * -------------------------------------------------- 97162abf1aSKONRAD Frederic */ 98162abf1aSKONRAD Frederic 99162abf1aSKONRAD Frederic if (slave) { 100162abf1aSKONRAD Frederic assert(dev->slave_count < GRLIB_AHB_MAX_DEV); 101162abf1aSKONRAD Frederic reg_start = (GRLIB_AHB_SLAVE_OFFSET 102162abf1aSKONRAD Frederic + (dev->slave_count * GRLIB_AHB_ENTRY_SIZE)) >> 2; 103162abf1aSKONRAD Frederic dev->slave_count++; 104162abf1aSKONRAD Frederic } else { 105162abf1aSKONRAD Frederic assert(dev->master_count < GRLIB_AHB_MAX_DEV); 106162abf1aSKONRAD Frederic reg_start = (dev->master_count * GRLIB_AHB_ENTRY_SIZE) >> 2; 107162abf1aSKONRAD Frederic dev->master_count++; 108162abf1aSKONRAD Frederic } 109162abf1aSKONRAD Frederic 110162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 111162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SHIFT, 112162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SIZE, 113162abf1aSKONRAD Frederic vendor); 114162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 115162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SHIFT, 116162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SIZE, 117162abf1aSKONRAD Frederic device); 118162abf1aSKONRAD Frederic reg_start += 4; 119162abf1aSKONRAD Frederic /* AHB Memory Space */ 120162abf1aSKONRAD Frederic dev->regs[reg_start] = type; 121162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 122162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SHIFT, 123162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SIZE, 124162abf1aSKONRAD Frederic extract32(address, 125162abf1aSKONRAD Frederic GRLIB_AHB_DEV_ADDR_SHIFT, 126162abf1aSKONRAD Frederic GRLIB_AHB_DEV_ADDR_SIZE)); 127162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 128162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SHIFT, 129162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SIZE, 130162abf1aSKONRAD Frederic mask); 131162abf1aSKONRAD Frederic } 132162abf1aSKONRAD Frederic 133162abf1aSKONRAD Frederic static uint64_t grlib_ahb_pnp_read(void *opaque, hwaddr offset, unsigned size) 134162abf1aSKONRAD Frederic { 135162abf1aSKONRAD Frederic AHBPnp *ahb_pnp = GRLIB_AHB_PNP(opaque); 136d15188ddSPhilippe Mathieu-Daudé uint32_t val; 137162abf1aSKONRAD Frederic 138d15188ddSPhilippe Mathieu-Daudé val = ahb_pnp->regs[offset >> 2]; 13909d12c81SPeter Maydell val = extract32(val, (4 - (offset & 3) - size) * 8, size * 8); 14009d12c81SPeter Maydell trace_grlib_ahb_pnp_read(offset, size, val); 141d15188ddSPhilippe Mathieu-Daudé 142d15188ddSPhilippe Mathieu-Daudé return val; 143162abf1aSKONRAD Frederic } 144162abf1aSKONRAD Frederic 145bb15013eSPhilippe Mathieu-Daudé static void grlib_ahb_pnp_write(void *opaque, hwaddr addr, 146bb15013eSPhilippe Mathieu-Daudé uint64_t val, unsigned size) 147bb15013eSPhilippe Mathieu-Daudé { 148bb15013eSPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__); 149bb15013eSPhilippe Mathieu-Daudé } 150bb15013eSPhilippe Mathieu-Daudé 151162abf1aSKONRAD Frederic static const MemoryRegionOps grlib_ahb_pnp_ops = { 152162abf1aSKONRAD Frederic .read = grlib_ahb_pnp_read, 153bb15013eSPhilippe Mathieu-Daudé .write = grlib_ahb_pnp_write, 154162abf1aSKONRAD Frederic .endianness = DEVICE_BIG_ENDIAN, 1551a5a5570SPhilippe Mathieu-Daudé .impl = { 15609d12c81SPeter Maydell .min_access_size = 1, 1571a5a5570SPhilippe Mathieu-Daudé .max_access_size = 4, 1581a5a5570SPhilippe Mathieu-Daudé }, 159162abf1aSKONRAD Frederic }; 160162abf1aSKONRAD Frederic 161162abf1aSKONRAD Frederic static void grlib_ahb_pnp_realize(DeviceState *dev, Error **errp) 162162abf1aSKONRAD Frederic { 163162abf1aSKONRAD Frederic AHBPnp *ahb_pnp = GRLIB_AHB_PNP(dev); 164162abf1aSKONRAD Frederic SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 165162abf1aSKONRAD Frederic 166162abf1aSKONRAD Frederic memory_region_init_io(&ahb_pnp->iomem, OBJECT(dev), &grlib_ahb_pnp_ops, 167162abf1aSKONRAD Frederic ahb_pnp, TYPE_GRLIB_AHB_PNP, GRLIB_PNP_MAX_REGS); 168162abf1aSKONRAD Frederic sysbus_init_mmio(sbd, &ahb_pnp->iomem); 169162abf1aSKONRAD Frederic } 170162abf1aSKONRAD Frederic 171*12d1a768SPhilippe Mathieu-Daudé static void grlib_ahb_pnp_class_init(ObjectClass *klass, const void *data) 172162abf1aSKONRAD Frederic { 173162abf1aSKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 174162abf1aSKONRAD Frederic 175162abf1aSKONRAD Frederic dc->realize = grlib_ahb_pnp_realize; 176162abf1aSKONRAD Frederic } 177162abf1aSKONRAD Frederic 178162abf1aSKONRAD Frederic static const TypeInfo grlib_ahb_pnp_info = { 179162abf1aSKONRAD Frederic .name = TYPE_GRLIB_AHB_PNP, 180162abf1aSKONRAD Frederic .parent = TYPE_SYS_BUS_DEVICE, 181162abf1aSKONRAD Frederic .instance_size = sizeof(AHBPnp), 182162abf1aSKONRAD Frederic .class_init = grlib_ahb_pnp_class_init, 183162abf1aSKONRAD Frederic }; 184162abf1aSKONRAD Frederic 185162abf1aSKONRAD Frederic /* APBPnp */ 186162abf1aSKONRAD Frederic 187162abf1aSKONRAD Frederic typedef struct APBPnp { 188162abf1aSKONRAD Frederic SysBusDevice parent_obj; 189162abf1aSKONRAD Frederic MemoryRegion iomem; 190162abf1aSKONRAD Frederic 191162abf1aSKONRAD Frederic uint32_t regs[GRLIB_PNP_MAX_REGS >> 2]; 192162abf1aSKONRAD Frederic uint32_t entry_count; 193162abf1aSKONRAD Frederic } APBPnp; 194162abf1aSKONRAD Frederic 195162abf1aSKONRAD Frederic void grlib_apb_pnp_add_entry(APBPnp *dev, uint32_t address, uint32_t mask, 196162abf1aSKONRAD Frederic uint8_t vendor, uint16_t device, uint8_t version, 197162abf1aSKONRAD Frederic uint8_t irq, int type) 198162abf1aSKONRAD Frederic { 199162abf1aSKONRAD Frederic unsigned int reg_start; 200162abf1aSKONRAD Frederic 201162abf1aSKONRAD Frederic /* 202162abf1aSKONRAD Frederic * APB entries look like this: 203162abf1aSKONRAD Frederic * 204162abf1aSKONRAD Frederic * 31 -------- 23 -------- 11 ----- 9 ------- 4 --- 0 205162abf1aSKONRAD Frederic * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ | 206162abf1aSKONRAD Frederic * 207162abf1aSKONRAD Frederic * 31 ---------- 20 --- 15 ----------------- 3 ---- 0 208162abf1aSKONRAD Frederic * | ADDR[20..8] | 0000 | MASK | TYPE | 209162abf1aSKONRAD Frederic */ 210162abf1aSKONRAD Frederic 211162abf1aSKONRAD Frederic assert(dev->entry_count < GRLIB_APB_MAX_DEV); 212162abf1aSKONRAD Frederic reg_start = (dev->entry_count * GRLIB_APB_ENTRY_SIZE) >> 2; 213162abf1aSKONRAD Frederic dev->entry_count++; 214162abf1aSKONRAD Frederic 215162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 216162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SHIFT, 217162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SIZE, 218162abf1aSKONRAD Frederic vendor); 219162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 220162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SHIFT, 221162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SIZE, 222162abf1aSKONRAD Frederic device); 223162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 224162abf1aSKONRAD Frederic GRLIB_PNP_VER_SHIFT, 225162abf1aSKONRAD Frederic GRLIB_PNP_VER_SIZE, 226162abf1aSKONRAD Frederic version); 227162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 228162abf1aSKONRAD Frederic GRLIB_PNP_IRQ_SHIFT, 229162abf1aSKONRAD Frederic GRLIB_PNP_IRQ_SIZE, 230162abf1aSKONRAD Frederic irq); 231162abf1aSKONRAD Frederic reg_start += 1; 232162abf1aSKONRAD Frederic dev->regs[reg_start] = type; 233162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 234162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SHIFT, 235162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SIZE, 236162abf1aSKONRAD Frederic extract32(address, 237162abf1aSKONRAD Frederic GRLIB_APB_DEV_ADDR_SHIFT, 238162abf1aSKONRAD Frederic GRLIB_APB_DEV_ADDR_SIZE)); 239162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 240162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SHIFT, 241162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SIZE, 242162abf1aSKONRAD Frederic mask); 243162abf1aSKONRAD Frederic } 244162abf1aSKONRAD Frederic 245162abf1aSKONRAD Frederic static uint64_t grlib_apb_pnp_read(void *opaque, hwaddr offset, unsigned size) 246162abf1aSKONRAD Frederic { 247162abf1aSKONRAD Frederic APBPnp *apb_pnp = GRLIB_APB_PNP(opaque); 248d15188ddSPhilippe Mathieu-Daudé uint32_t val; 249162abf1aSKONRAD Frederic 250d15188ddSPhilippe Mathieu-Daudé val = apb_pnp->regs[offset >> 2]; 25109d12c81SPeter Maydell val = extract32(val, (4 - (offset & 3) - size) * 8, size * 8); 25209d12c81SPeter Maydell trace_grlib_apb_pnp_read(offset, size, val); 253d15188ddSPhilippe Mathieu-Daudé 254d15188ddSPhilippe Mathieu-Daudé return val; 255162abf1aSKONRAD Frederic } 256162abf1aSKONRAD Frederic 257158b6594SPhilippe Mathieu-Daudé static void grlib_apb_pnp_write(void *opaque, hwaddr addr, 258158b6594SPhilippe Mathieu-Daudé uint64_t val, unsigned size) 259158b6594SPhilippe Mathieu-Daudé { 260158b6594SPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__); 261158b6594SPhilippe Mathieu-Daudé } 262158b6594SPhilippe Mathieu-Daudé 263162abf1aSKONRAD Frederic static const MemoryRegionOps grlib_apb_pnp_ops = { 264162abf1aSKONRAD Frederic .read = grlib_apb_pnp_read, 265158b6594SPhilippe Mathieu-Daudé .write = grlib_apb_pnp_write, 266162abf1aSKONRAD Frederic .endianness = DEVICE_BIG_ENDIAN, 2670fbe394aSPhilippe Mathieu-Daudé .impl = { 26809d12c81SPeter Maydell .min_access_size = 1, 2690fbe394aSPhilippe Mathieu-Daudé .max_access_size = 4, 2700fbe394aSPhilippe Mathieu-Daudé }, 271162abf1aSKONRAD Frederic }; 272162abf1aSKONRAD Frederic 273162abf1aSKONRAD Frederic static void grlib_apb_pnp_realize(DeviceState *dev, Error **errp) 274162abf1aSKONRAD Frederic { 275162abf1aSKONRAD Frederic APBPnp *apb_pnp = GRLIB_APB_PNP(dev); 276162abf1aSKONRAD Frederic SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 277162abf1aSKONRAD Frederic 278162abf1aSKONRAD Frederic memory_region_init_io(&apb_pnp->iomem, OBJECT(dev), &grlib_apb_pnp_ops, 279162abf1aSKONRAD Frederic apb_pnp, TYPE_GRLIB_APB_PNP, GRLIB_PNP_MAX_REGS); 280162abf1aSKONRAD Frederic sysbus_init_mmio(sbd, &apb_pnp->iomem); 281162abf1aSKONRAD Frederic } 282162abf1aSKONRAD Frederic 283*12d1a768SPhilippe Mathieu-Daudé static void grlib_apb_pnp_class_init(ObjectClass *klass, const void *data) 284162abf1aSKONRAD Frederic { 285162abf1aSKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 286162abf1aSKONRAD Frederic 287162abf1aSKONRAD Frederic dc->realize = grlib_apb_pnp_realize; 288162abf1aSKONRAD Frederic } 289162abf1aSKONRAD Frederic 290162abf1aSKONRAD Frederic static const TypeInfo grlib_apb_pnp_info = { 291162abf1aSKONRAD Frederic .name = TYPE_GRLIB_APB_PNP, 292162abf1aSKONRAD Frederic .parent = TYPE_SYS_BUS_DEVICE, 293162abf1aSKONRAD Frederic .instance_size = sizeof(APBPnp), 294162abf1aSKONRAD Frederic .class_init = grlib_apb_pnp_class_init, 295162abf1aSKONRAD Frederic }; 296162abf1aSKONRAD Frederic 297162abf1aSKONRAD Frederic static void grlib_ahb_apb_pnp_register_types(void) 298162abf1aSKONRAD Frederic { 299162abf1aSKONRAD Frederic type_register_static(&grlib_ahb_pnp_info); 300162abf1aSKONRAD Frederic type_register_static(&grlib_apb_pnp_info); 301162abf1aSKONRAD Frederic } 302162abf1aSKONRAD Frederic 303162abf1aSKONRAD Frederic type_init(grlib_ahb_apb_pnp_register_types) 304