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" 28*d15188ddSPhilippe 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); 136*d15188ddSPhilippe Mathieu-Daudé uint32_t val; 137162abf1aSKONRAD Frederic 138*d15188ddSPhilippe Mathieu-Daudé val = ahb_pnp->regs[offset >> 2]; 139*d15188ddSPhilippe Mathieu-Daudé trace_grlib_ahb_pnp_read(offset, val); 140*d15188ddSPhilippe Mathieu-Daudé 141*d15188ddSPhilippe Mathieu-Daudé return val; 142162abf1aSKONRAD Frederic } 143162abf1aSKONRAD Frederic 144bb15013eSPhilippe Mathieu-Daudé static void grlib_ahb_pnp_write(void *opaque, hwaddr addr, 145bb15013eSPhilippe Mathieu-Daudé uint64_t val, unsigned size) 146bb15013eSPhilippe Mathieu-Daudé { 147bb15013eSPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__); 148bb15013eSPhilippe Mathieu-Daudé } 149bb15013eSPhilippe Mathieu-Daudé 150162abf1aSKONRAD Frederic static const MemoryRegionOps grlib_ahb_pnp_ops = { 151162abf1aSKONRAD Frederic .read = grlib_ahb_pnp_read, 152bb15013eSPhilippe Mathieu-Daudé .write = grlib_ahb_pnp_write, 153162abf1aSKONRAD Frederic .endianness = DEVICE_BIG_ENDIAN, 1541a5a5570SPhilippe Mathieu-Daudé .impl = { 1551a5a5570SPhilippe Mathieu-Daudé .min_access_size = 4, 1561a5a5570SPhilippe Mathieu-Daudé .max_access_size = 4, 1571a5a5570SPhilippe Mathieu-Daudé }, 158162abf1aSKONRAD Frederic }; 159162abf1aSKONRAD Frederic 160162abf1aSKONRAD Frederic static void grlib_ahb_pnp_realize(DeviceState *dev, Error **errp) 161162abf1aSKONRAD Frederic { 162162abf1aSKONRAD Frederic AHBPnp *ahb_pnp = GRLIB_AHB_PNP(dev); 163162abf1aSKONRAD Frederic SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 164162abf1aSKONRAD Frederic 165162abf1aSKONRAD Frederic memory_region_init_io(&ahb_pnp->iomem, OBJECT(dev), &grlib_ahb_pnp_ops, 166162abf1aSKONRAD Frederic ahb_pnp, TYPE_GRLIB_AHB_PNP, GRLIB_PNP_MAX_REGS); 167162abf1aSKONRAD Frederic sysbus_init_mmio(sbd, &ahb_pnp->iomem); 168162abf1aSKONRAD Frederic } 169162abf1aSKONRAD Frederic 170162abf1aSKONRAD Frederic static void grlib_ahb_pnp_class_init(ObjectClass *klass, void *data) 171162abf1aSKONRAD Frederic { 172162abf1aSKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 173162abf1aSKONRAD Frederic 174162abf1aSKONRAD Frederic dc->realize = grlib_ahb_pnp_realize; 175162abf1aSKONRAD Frederic } 176162abf1aSKONRAD Frederic 177162abf1aSKONRAD Frederic static const TypeInfo grlib_ahb_pnp_info = { 178162abf1aSKONRAD Frederic .name = TYPE_GRLIB_AHB_PNP, 179162abf1aSKONRAD Frederic .parent = TYPE_SYS_BUS_DEVICE, 180162abf1aSKONRAD Frederic .instance_size = sizeof(AHBPnp), 181162abf1aSKONRAD Frederic .class_init = grlib_ahb_pnp_class_init, 182162abf1aSKONRAD Frederic }; 183162abf1aSKONRAD Frederic 184162abf1aSKONRAD Frederic /* APBPnp */ 185162abf1aSKONRAD Frederic 186162abf1aSKONRAD Frederic typedef struct APBPnp { 187162abf1aSKONRAD Frederic SysBusDevice parent_obj; 188162abf1aSKONRAD Frederic MemoryRegion iomem; 189162abf1aSKONRAD Frederic 190162abf1aSKONRAD Frederic uint32_t regs[GRLIB_PNP_MAX_REGS >> 2]; 191162abf1aSKONRAD Frederic uint32_t entry_count; 192162abf1aSKONRAD Frederic } APBPnp; 193162abf1aSKONRAD Frederic 194162abf1aSKONRAD Frederic void grlib_apb_pnp_add_entry(APBPnp *dev, uint32_t address, uint32_t mask, 195162abf1aSKONRAD Frederic uint8_t vendor, uint16_t device, uint8_t version, 196162abf1aSKONRAD Frederic uint8_t irq, int type) 197162abf1aSKONRAD Frederic { 198162abf1aSKONRAD Frederic unsigned int reg_start; 199162abf1aSKONRAD Frederic 200162abf1aSKONRAD Frederic /* 201162abf1aSKONRAD Frederic * APB entries look like this: 202162abf1aSKONRAD Frederic * 203162abf1aSKONRAD Frederic * 31 -------- 23 -------- 11 ----- 9 ------- 4 --- 0 204162abf1aSKONRAD Frederic * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ | 205162abf1aSKONRAD Frederic * 206162abf1aSKONRAD Frederic * 31 ---------- 20 --- 15 ----------------- 3 ---- 0 207162abf1aSKONRAD Frederic * | ADDR[20..8] | 0000 | MASK | TYPE | 208162abf1aSKONRAD Frederic */ 209162abf1aSKONRAD Frederic 210162abf1aSKONRAD Frederic assert(dev->entry_count < GRLIB_APB_MAX_DEV); 211162abf1aSKONRAD Frederic reg_start = (dev->entry_count * GRLIB_APB_ENTRY_SIZE) >> 2; 212162abf1aSKONRAD Frederic dev->entry_count++; 213162abf1aSKONRAD Frederic 214162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 215162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SHIFT, 216162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SIZE, 217162abf1aSKONRAD Frederic vendor); 218162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 219162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SHIFT, 220162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SIZE, 221162abf1aSKONRAD Frederic device); 222162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 223162abf1aSKONRAD Frederic GRLIB_PNP_VER_SHIFT, 224162abf1aSKONRAD Frederic GRLIB_PNP_VER_SIZE, 225162abf1aSKONRAD Frederic version); 226162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 227162abf1aSKONRAD Frederic GRLIB_PNP_IRQ_SHIFT, 228162abf1aSKONRAD Frederic GRLIB_PNP_IRQ_SIZE, 229162abf1aSKONRAD Frederic irq); 230162abf1aSKONRAD Frederic reg_start += 1; 231162abf1aSKONRAD Frederic dev->regs[reg_start] = type; 232162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 233162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SHIFT, 234162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SIZE, 235162abf1aSKONRAD Frederic extract32(address, 236162abf1aSKONRAD Frederic GRLIB_APB_DEV_ADDR_SHIFT, 237162abf1aSKONRAD Frederic GRLIB_APB_DEV_ADDR_SIZE)); 238162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 239162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SHIFT, 240162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SIZE, 241162abf1aSKONRAD Frederic mask); 242162abf1aSKONRAD Frederic } 243162abf1aSKONRAD Frederic 244162abf1aSKONRAD Frederic static uint64_t grlib_apb_pnp_read(void *opaque, hwaddr offset, unsigned size) 245162abf1aSKONRAD Frederic { 246162abf1aSKONRAD Frederic APBPnp *apb_pnp = GRLIB_APB_PNP(opaque); 247*d15188ddSPhilippe Mathieu-Daudé uint32_t val; 248162abf1aSKONRAD Frederic 249*d15188ddSPhilippe Mathieu-Daudé val = apb_pnp->regs[offset >> 2]; 250*d15188ddSPhilippe Mathieu-Daudé trace_grlib_apb_pnp_read(offset, val); 251*d15188ddSPhilippe Mathieu-Daudé 252*d15188ddSPhilippe Mathieu-Daudé return val; 253162abf1aSKONRAD Frederic } 254162abf1aSKONRAD Frederic 255158b6594SPhilippe Mathieu-Daudé static void grlib_apb_pnp_write(void *opaque, hwaddr addr, 256158b6594SPhilippe Mathieu-Daudé uint64_t val, unsigned size) 257158b6594SPhilippe Mathieu-Daudé { 258158b6594SPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__); 259158b6594SPhilippe Mathieu-Daudé } 260158b6594SPhilippe Mathieu-Daudé 261162abf1aSKONRAD Frederic static const MemoryRegionOps grlib_apb_pnp_ops = { 262162abf1aSKONRAD Frederic .read = grlib_apb_pnp_read, 263158b6594SPhilippe Mathieu-Daudé .write = grlib_apb_pnp_write, 264162abf1aSKONRAD Frederic .endianness = DEVICE_BIG_ENDIAN, 2650fbe394aSPhilippe Mathieu-Daudé .impl = { 2660fbe394aSPhilippe Mathieu-Daudé .min_access_size = 4, 2670fbe394aSPhilippe Mathieu-Daudé .max_access_size = 4, 2680fbe394aSPhilippe Mathieu-Daudé }, 269162abf1aSKONRAD Frederic }; 270162abf1aSKONRAD Frederic 271162abf1aSKONRAD Frederic static void grlib_apb_pnp_realize(DeviceState *dev, Error **errp) 272162abf1aSKONRAD Frederic { 273162abf1aSKONRAD Frederic APBPnp *apb_pnp = GRLIB_APB_PNP(dev); 274162abf1aSKONRAD Frederic SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 275162abf1aSKONRAD Frederic 276162abf1aSKONRAD Frederic memory_region_init_io(&apb_pnp->iomem, OBJECT(dev), &grlib_apb_pnp_ops, 277162abf1aSKONRAD Frederic apb_pnp, TYPE_GRLIB_APB_PNP, GRLIB_PNP_MAX_REGS); 278162abf1aSKONRAD Frederic sysbus_init_mmio(sbd, &apb_pnp->iomem); 279162abf1aSKONRAD Frederic } 280162abf1aSKONRAD Frederic 281162abf1aSKONRAD Frederic static void grlib_apb_pnp_class_init(ObjectClass *klass, void *data) 282162abf1aSKONRAD Frederic { 283162abf1aSKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 284162abf1aSKONRAD Frederic 285162abf1aSKONRAD Frederic dc->realize = grlib_apb_pnp_realize; 286162abf1aSKONRAD Frederic } 287162abf1aSKONRAD Frederic 288162abf1aSKONRAD Frederic static const TypeInfo grlib_apb_pnp_info = { 289162abf1aSKONRAD Frederic .name = TYPE_GRLIB_APB_PNP, 290162abf1aSKONRAD Frederic .parent = TYPE_SYS_BUS_DEVICE, 291162abf1aSKONRAD Frederic .instance_size = sizeof(APBPnp), 292162abf1aSKONRAD Frederic .class_init = grlib_apb_pnp_class_init, 293162abf1aSKONRAD Frederic }; 294162abf1aSKONRAD Frederic 295162abf1aSKONRAD Frederic static void grlib_ahb_apb_pnp_register_types(void) 296162abf1aSKONRAD Frederic { 297162abf1aSKONRAD Frederic type_register_static(&grlib_ahb_pnp_info); 298162abf1aSKONRAD Frederic type_register_static(&grlib_apb_pnp_info); 299162abf1aSKONRAD Frederic } 300162abf1aSKONRAD Frederic 301162abf1aSKONRAD Frederic type_init(grlib_ahb_apb_pnp_register_types) 302