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" 28162abf1aSKONRAD Frederic 29162abf1aSKONRAD Frederic #define GRLIB_PNP_VENDOR_SHIFT (24) 30162abf1aSKONRAD Frederic #define GRLIB_PNP_VENDOR_SIZE (8) 31162abf1aSKONRAD Frederic #define GRLIB_PNP_DEV_SHIFT (12) 32162abf1aSKONRAD Frederic #define GRLIB_PNP_DEV_SIZE (12) 33162abf1aSKONRAD Frederic #define GRLIB_PNP_VER_SHIFT (5) 34162abf1aSKONRAD Frederic #define GRLIB_PNP_VER_SIZE (5) 35162abf1aSKONRAD Frederic #define GRLIB_PNP_IRQ_SHIFT (0) 36162abf1aSKONRAD Frederic #define GRLIB_PNP_IRQ_SIZE (5) 37162abf1aSKONRAD Frederic #define GRLIB_PNP_ADDR_SHIFT (20) 38162abf1aSKONRAD Frederic #define GRLIB_PNP_ADDR_SIZE (12) 39162abf1aSKONRAD Frederic #define GRLIB_PNP_MASK_SHIFT (4) 40162abf1aSKONRAD Frederic #define GRLIB_PNP_MASK_SIZE (12) 41162abf1aSKONRAD Frederic 42162abf1aSKONRAD Frederic #define GRLIB_AHB_DEV_ADDR_SHIFT (20) 43162abf1aSKONRAD Frederic #define GRLIB_AHB_DEV_ADDR_SIZE (12) 44162abf1aSKONRAD Frederic #define GRLIB_AHB_ENTRY_SIZE (0x20) 45162abf1aSKONRAD Frederic #define GRLIB_AHB_MAX_DEV (64) 46162abf1aSKONRAD Frederic #define GRLIB_AHB_SLAVE_OFFSET (0x800) 47162abf1aSKONRAD Frederic 48162abf1aSKONRAD Frederic #define GRLIB_APB_DEV_ADDR_SHIFT (8) 49162abf1aSKONRAD Frederic #define GRLIB_APB_DEV_ADDR_SIZE (12) 50162abf1aSKONRAD Frederic #define GRLIB_APB_ENTRY_SIZE (0x08) 51162abf1aSKONRAD Frederic #define GRLIB_APB_MAX_DEV (512) 52162abf1aSKONRAD Frederic 53162abf1aSKONRAD Frederic #define GRLIB_PNP_MAX_REGS (0x1000) 54162abf1aSKONRAD Frederic 55162abf1aSKONRAD Frederic typedef struct AHBPnp { 56162abf1aSKONRAD Frederic SysBusDevice parent_obj; 57162abf1aSKONRAD Frederic MemoryRegion iomem; 58162abf1aSKONRAD Frederic 59162abf1aSKONRAD Frederic uint32_t regs[GRLIB_PNP_MAX_REGS >> 2]; 60162abf1aSKONRAD Frederic uint8_t master_count; 61162abf1aSKONRAD Frederic uint8_t slave_count; 62162abf1aSKONRAD Frederic } AHBPnp; 63162abf1aSKONRAD Frederic 64162abf1aSKONRAD Frederic void grlib_ahb_pnp_add_entry(AHBPnp *dev, uint32_t address, uint32_t mask, 65162abf1aSKONRAD Frederic uint8_t vendor, uint16_t device, int slave, 66162abf1aSKONRAD Frederic int type) 67162abf1aSKONRAD Frederic { 68162abf1aSKONRAD Frederic unsigned int reg_start; 69162abf1aSKONRAD Frederic 70162abf1aSKONRAD Frederic /* 71162abf1aSKONRAD Frederic * AHB entries look like this: 72162abf1aSKONRAD Frederic * 73162abf1aSKONRAD Frederic * 31 -------- 23 -------- 11 ----- 9 -------- 4 --- 0 74162abf1aSKONRAD Frederic * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ | 75162abf1aSKONRAD Frederic * -------------------------------------------------- 76162abf1aSKONRAD Frederic * | USER | 77162abf1aSKONRAD Frederic * -------------------------------------------------- 78162abf1aSKONRAD Frederic * | USER | 79162abf1aSKONRAD Frederic * -------------------------------------------------- 80162abf1aSKONRAD Frederic * | USER | 81162abf1aSKONRAD Frederic * -------------------------------------------------- 82162abf1aSKONRAD Frederic * | USER | 83162abf1aSKONRAD Frederic * -------------------------------------------------- 84162abf1aSKONRAD Frederic * 31 ----------- 20 --- 15 ----------------- 3 ---- 0 85162abf1aSKONRAD Frederic * | ADDR[31..12] | 00PC | MASK | TYPE | 86162abf1aSKONRAD Frederic * -------------------------------------------------- 87162abf1aSKONRAD Frederic * 31 ----------- 20 --- 15 ----------------- 3 ---- 0 88162abf1aSKONRAD Frederic * | ADDR[31..12] | 00PC | MASK | TYPE | 89162abf1aSKONRAD Frederic * -------------------------------------------------- 90162abf1aSKONRAD Frederic * 31 ----------- 20 --- 15 ----------------- 3 ---- 0 91162abf1aSKONRAD Frederic * | ADDR[31..12] | 00PC | MASK | TYPE | 92162abf1aSKONRAD Frederic * -------------------------------------------------- 93162abf1aSKONRAD Frederic * 31 ----------- 20 --- 15 ----------------- 3 ---- 0 94162abf1aSKONRAD Frederic * | ADDR[31..12] | 00PC | MASK | TYPE | 95162abf1aSKONRAD Frederic * -------------------------------------------------- 96162abf1aSKONRAD Frederic */ 97162abf1aSKONRAD Frederic 98162abf1aSKONRAD Frederic if (slave) { 99162abf1aSKONRAD Frederic assert(dev->slave_count < GRLIB_AHB_MAX_DEV); 100162abf1aSKONRAD Frederic reg_start = (GRLIB_AHB_SLAVE_OFFSET 101162abf1aSKONRAD Frederic + (dev->slave_count * GRLIB_AHB_ENTRY_SIZE)) >> 2; 102162abf1aSKONRAD Frederic dev->slave_count++; 103162abf1aSKONRAD Frederic } else { 104162abf1aSKONRAD Frederic assert(dev->master_count < GRLIB_AHB_MAX_DEV); 105162abf1aSKONRAD Frederic reg_start = (dev->master_count * GRLIB_AHB_ENTRY_SIZE) >> 2; 106162abf1aSKONRAD Frederic dev->master_count++; 107162abf1aSKONRAD Frederic } 108162abf1aSKONRAD Frederic 109162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 110162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SHIFT, 111162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SIZE, 112162abf1aSKONRAD Frederic vendor); 113162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 114162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SHIFT, 115162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SIZE, 116162abf1aSKONRAD Frederic device); 117162abf1aSKONRAD Frederic reg_start += 4; 118162abf1aSKONRAD Frederic /* AHB Memory Space */ 119162abf1aSKONRAD Frederic dev->regs[reg_start] = type; 120162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 121162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SHIFT, 122162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SIZE, 123162abf1aSKONRAD Frederic extract32(address, 124162abf1aSKONRAD Frederic GRLIB_AHB_DEV_ADDR_SHIFT, 125162abf1aSKONRAD Frederic GRLIB_AHB_DEV_ADDR_SIZE)); 126162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 127162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SHIFT, 128162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SIZE, 129162abf1aSKONRAD Frederic mask); 130162abf1aSKONRAD Frederic } 131162abf1aSKONRAD Frederic 132162abf1aSKONRAD Frederic static uint64_t grlib_ahb_pnp_read(void *opaque, hwaddr offset, unsigned size) 133162abf1aSKONRAD Frederic { 134162abf1aSKONRAD Frederic AHBPnp *ahb_pnp = GRLIB_AHB_PNP(opaque); 135162abf1aSKONRAD Frederic 136162abf1aSKONRAD Frederic return ahb_pnp->regs[offset >> 2]; 137162abf1aSKONRAD Frederic } 138162abf1aSKONRAD Frederic 139*bb15013eSPhilippe Mathieu-Daudé static void grlib_ahb_pnp_write(void *opaque, hwaddr addr, 140*bb15013eSPhilippe Mathieu-Daudé uint64_t val, unsigned size) 141*bb15013eSPhilippe Mathieu-Daudé { 142*bb15013eSPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__); 143*bb15013eSPhilippe Mathieu-Daudé } 144*bb15013eSPhilippe Mathieu-Daudé 145162abf1aSKONRAD Frederic static const MemoryRegionOps grlib_ahb_pnp_ops = { 146162abf1aSKONRAD Frederic .read = grlib_ahb_pnp_read, 147*bb15013eSPhilippe Mathieu-Daudé .write = grlib_ahb_pnp_write, 148162abf1aSKONRAD Frederic .endianness = DEVICE_BIG_ENDIAN, 149162abf1aSKONRAD Frederic }; 150162abf1aSKONRAD Frederic 151162abf1aSKONRAD Frederic static void grlib_ahb_pnp_realize(DeviceState *dev, Error **errp) 152162abf1aSKONRAD Frederic { 153162abf1aSKONRAD Frederic AHBPnp *ahb_pnp = GRLIB_AHB_PNP(dev); 154162abf1aSKONRAD Frederic SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 155162abf1aSKONRAD Frederic 156162abf1aSKONRAD Frederic memory_region_init_io(&ahb_pnp->iomem, OBJECT(dev), &grlib_ahb_pnp_ops, 157162abf1aSKONRAD Frederic ahb_pnp, TYPE_GRLIB_AHB_PNP, GRLIB_PNP_MAX_REGS); 158162abf1aSKONRAD Frederic sysbus_init_mmio(sbd, &ahb_pnp->iomem); 159162abf1aSKONRAD Frederic } 160162abf1aSKONRAD Frederic 161162abf1aSKONRAD Frederic static void grlib_ahb_pnp_class_init(ObjectClass *klass, void *data) 162162abf1aSKONRAD Frederic { 163162abf1aSKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 164162abf1aSKONRAD Frederic 165162abf1aSKONRAD Frederic dc->realize = grlib_ahb_pnp_realize; 166162abf1aSKONRAD Frederic } 167162abf1aSKONRAD Frederic 168162abf1aSKONRAD Frederic static const TypeInfo grlib_ahb_pnp_info = { 169162abf1aSKONRAD Frederic .name = TYPE_GRLIB_AHB_PNP, 170162abf1aSKONRAD Frederic .parent = TYPE_SYS_BUS_DEVICE, 171162abf1aSKONRAD Frederic .instance_size = sizeof(AHBPnp), 172162abf1aSKONRAD Frederic .class_init = grlib_ahb_pnp_class_init, 173162abf1aSKONRAD Frederic }; 174162abf1aSKONRAD Frederic 175162abf1aSKONRAD Frederic /* APBPnp */ 176162abf1aSKONRAD Frederic 177162abf1aSKONRAD Frederic typedef struct APBPnp { 178162abf1aSKONRAD Frederic SysBusDevice parent_obj; 179162abf1aSKONRAD Frederic MemoryRegion iomem; 180162abf1aSKONRAD Frederic 181162abf1aSKONRAD Frederic uint32_t regs[GRLIB_PNP_MAX_REGS >> 2]; 182162abf1aSKONRAD Frederic uint32_t entry_count; 183162abf1aSKONRAD Frederic } APBPnp; 184162abf1aSKONRAD Frederic 185162abf1aSKONRAD Frederic void grlib_apb_pnp_add_entry(APBPnp *dev, uint32_t address, uint32_t mask, 186162abf1aSKONRAD Frederic uint8_t vendor, uint16_t device, uint8_t version, 187162abf1aSKONRAD Frederic uint8_t irq, int type) 188162abf1aSKONRAD Frederic { 189162abf1aSKONRAD Frederic unsigned int reg_start; 190162abf1aSKONRAD Frederic 191162abf1aSKONRAD Frederic /* 192162abf1aSKONRAD Frederic * APB entries look like this: 193162abf1aSKONRAD Frederic * 194162abf1aSKONRAD Frederic * 31 -------- 23 -------- 11 ----- 9 ------- 4 --- 0 195162abf1aSKONRAD Frederic * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ | 196162abf1aSKONRAD Frederic * 197162abf1aSKONRAD Frederic * 31 ---------- 20 --- 15 ----------------- 3 ---- 0 198162abf1aSKONRAD Frederic * | ADDR[20..8] | 0000 | MASK | TYPE | 199162abf1aSKONRAD Frederic */ 200162abf1aSKONRAD Frederic 201162abf1aSKONRAD Frederic assert(dev->entry_count < GRLIB_APB_MAX_DEV); 202162abf1aSKONRAD Frederic reg_start = (dev->entry_count * GRLIB_APB_ENTRY_SIZE) >> 2; 203162abf1aSKONRAD Frederic dev->entry_count++; 204162abf1aSKONRAD Frederic 205162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 206162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SHIFT, 207162abf1aSKONRAD Frederic GRLIB_PNP_VENDOR_SIZE, 208162abf1aSKONRAD Frederic vendor); 209162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 210162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SHIFT, 211162abf1aSKONRAD Frederic GRLIB_PNP_DEV_SIZE, 212162abf1aSKONRAD Frederic device); 213162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 214162abf1aSKONRAD Frederic GRLIB_PNP_VER_SHIFT, 215162abf1aSKONRAD Frederic GRLIB_PNP_VER_SIZE, 216162abf1aSKONRAD Frederic version); 217162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 218162abf1aSKONRAD Frederic GRLIB_PNP_IRQ_SHIFT, 219162abf1aSKONRAD Frederic GRLIB_PNP_IRQ_SIZE, 220162abf1aSKONRAD Frederic irq); 221162abf1aSKONRAD Frederic reg_start += 1; 222162abf1aSKONRAD Frederic dev->regs[reg_start] = type; 223162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 224162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SHIFT, 225162abf1aSKONRAD Frederic GRLIB_PNP_ADDR_SIZE, 226162abf1aSKONRAD Frederic extract32(address, 227162abf1aSKONRAD Frederic GRLIB_APB_DEV_ADDR_SHIFT, 228162abf1aSKONRAD Frederic GRLIB_APB_DEV_ADDR_SIZE)); 229162abf1aSKONRAD Frederic dev->regs[reg_start] = deposit32(dev->regs[reg_start], 230162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SHIFT, 231162abf1aSKONRAD Frederic GRLIB_PNP_MASK_SIZE, 232162abf1aSKONRAD Frederic mask); 233162abf1aSKONRAD Frederic } 234162abf1aSKONRAD Frederic 235162abf1aSKONRAD Frederic static uint64_t grlib_apb_pnp_read(void *opaque, hwaddr offset, unsigned size) 236162abf1aSKONRAD Frederic { 237162abf1aSKONRAD Frederic APBPnp *apb_pnp = GRLIB_APB_PNP(opaque); 238162abf1aSKONRAD Frederic 239162abf1aSKONRAD Frederic return apb_pnp->regs[offset >> 2]; 240162abf1aSKONRAD Frederic } 241162abf1aSKONRAD Frederic 242158b6594SPhilippe Mathieu-Daudé static void grlib_apb_pnp_write(void *opaque, hwaddr addr, 243158b6594SPhilippe Mathieu-Daudé uint64_t val, unsigned size) 244158b6594SPhilippe Mathieu-Daudé { 245158b6594SPhilippe Mathieu-Daudé qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__); 246158b6594SPhilippe Mathieu-Daudé } 247158b6594SPhilippe Mathieu-Daudé 248162abf1aSKONRAD Frederic static const MemoryRegionOps grlib_apb_pnp_ops = { 249162abf1aSKONRAD Frederic .read = grlib_apb_pnp_read, 250158b6594SPhilippe Mathieu-Daudé .write = grlib_apb_pnp_write, 251162abf1aSKONRAD Frederic .endianness = DEVICE_BIG_ENDIAN, 2520fbe394aSPhilippe Mathieu-Daudé .impl = { 2530fbe394aSPhilippe Mathieu-Daudé .min_access_size = 4, 2540fbe394aSPhilippe Mathieu-Daudé .max_access_size = 4, 2550fbe394aSPhilippe Mathieu-Daudé }, 256162abf1aSKONRAD Frederic }; 257162abf1aSKONRAD Frederic 258162abf1aSKONRAD Frederic static void grlib_apb_pnp_realize(DeviceState *dev, Error **errp) 259162abf1aSKONRAD Frederic { 260162abf1aSKONRAD Frederic APBPnp *apb_pnp = GRLIB_APB_PNP(dev); 261162abf1aSKONRAD Frederic SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 262162abf1aSKONRAD Frederic 263162abf1aSKONRAD Frederic memory_region_init_io(&apb_pnp->iomem, OBJECT(dev), &grlib_apb_pnp_ops, 264162abf1aSKONRAD Frederic apb_pnp, TYPE_GRLIB_APB_PNP, GRLIB_PNP_MAX_REGS); 265162abf1aSKONRAD Frederic sysbus_init_mmio(sbd, &apb_pnp->iomem); 266162abf1aSKONRAD Frederic } 267162abf1aSKONRAD Frederic 268162abf1aSKONRAD Frederic static void grlib_apb_pnp_class_init(ObjectClass *klass, void *data) 269162abf1aSKONRAD Frederic { 270162abf1aSKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 271162abf1aSKONRAD Frederic 272162abf1aSKONRAD Frederic dc->realize = grlib_apb_pnp_realize; 273162abf1aSKONRAD Frederic } 274162abf1aSKONRAD Frederic 275162abf1aSKONRAD Frederic static const TypeInfo grlib_apb_pnp_info = { 276162abf1aSKONRAD Frederic .name = TYPE_GRLIB_APB_PNP, 277162abf1aSKONRAD Frederic .parent = TYPE_SYS_BUS_DEVICE, 278162abf1aSKONRAD Frederic .instance_size = sizeof(APBPnp), 279162abf1aSKONRAD Frederic .class_init = grlib_apb_pnp_class_init, 280162abf1aSKONRAD Frederic }; 281162abf1aSKONRAD Frederic 282162abf1aSKONRAD Frederic static void grlib_ahb_apb_pnp_register_types(void) 283162abf1aSKONRAD Frederic { 284162abf1aSKONRAD Frederic type_register_static(&grlib_ahb_pnp_info); 285162abf1aSKONRAD Frederic type_register_static(&grlib_apb_pnp_info); 286162abf1aSKONRAD Frederic } 287162abf1aSKONRAD Frederic 288162abf1aSKONRAD Frederic type_init(grlib_ahb_apb_pnp_register_types) 289