2 * QEMU model of the VersalUsb2CtrlRegs Register control/Status block for
5 * This module should control phy_reset, permanent device plugs, frame length
6 * time adjust & setting of coherency paths. None of which are emulated in
9 * Copyright (c) 2020 Xilinx Inc. Vikram Garhwal <fnu.vikram@xilinx.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 #include "qemu/osdep.h"
31 #include "hw/sysbus.h"
33 #include "hw/register.h"
34 #include "qemu/bitops.h"
35 #include "qom/object.h"
36 #include "migration/vmstate.h"
37 #include "hw/usb/xlnx-versal-usb2-ctrl-regs.h"
39 #ifndef XILINX_VERSAL_USB2_CTRL_REGS_ERR_DEBUG
40 #define XILINX_VERSAL_USB2_CTRL_REGS_ERR_DEBUG 0
43 REG32(BUS_FILTER
, 0x30)
44 FIELD(BUS_FILTER
, BYPASS
, 0, 4)
46 FIELD(PORT
, HOST_SMI_BAR_WR
, 4, 1)
47 FIELD(PORT
, HOST_SMI_PCI_CMD_REG_WR
, 3, 1)
48 FIELD(PORT
, HOST_MSI_ENABLE
, 2, 1)
49 FIELD(PORT
, PWR_CTRL_PRSNT
, 1, 1)
50 FIELD(PORT
, HUB_PERM_ATTACH
, 0, 1)
51 REG32(JITTER_ADJUST
, 0x38)
52 FIELD(JITTER_ADJUST
, FLADJ
, 0, 6)
53 REG32(BIGENDIAN
, 0x40)
54 FIELD(BIGENDIAN
, ENDIAN_GS
, 0, 1)
55 REG32(COHERENCY
, 0x44)
56 FIELD(COHERENCY
, USB_COHERENCY
, 0, 1)
58 FIELD(XHC_BME
, XHC_BME
, 0, 1)
60 FIELD(REG_CTRL
, SLVERR_ENABLE
, 0, 1)
61 REG32(IR_STATUS
, 0x64)
62 FIELD(IR_STATUS
, HOST_SYS_ERR
, 1, 1)
63 FIELD(IR_STATUS
, ADDR_DEC_ERR
, 0, 1)
65 FIELD(IR_MASK
, HOST_SYS_ERR
, 1, 1)
66 FIELD(IR_MASK
, ADDR_DEC_ERR
, 0, 1)
67 REG32(IR_ENABLE
, 0x6c)
68 FIELD(IR_ENABLE
, HOST_SYS_ERR
, 1, 1)
69 FIELD(IR_ENABLE
, ADDR_DEC_ERR
, 0, 1)
70 REG32(IR_DISABLE
, 0x70)
71 FIELD(IR_DISABLE
, HOST_SYS_ERR
, 1, 1)
72 FIELD(IR_DISABLE
, ADDR_DEC_ERR
, 0, 1)
75 static void ir_update_irq(VersalUsb2CtrlRegs
*s
)
77 bool pending
= s
->regs
[R_IR_STATUS
] & ~s
->regs
[R_IR_MASK
];
78 qemu_set_irq(s
->irq_ir
, pending
);
81 static void ir_status_postw(RegisterInfo
*reg
, uint64_t val64
)
83 VersalUsb2CtrlRegs
*s
= XILINX_VERSAL_USB2_CTRL_REGS(reg
->opaque
);
85 * TODO: This should also clear USBSTS.HSE field in USB XHCI register.
86 * May be combine both the modules.
91 static uint64_t ir_enable_prew(RegisterInfo
*reg
, uint64_t val64
)
93 VersalUsb2CtrlRegs
*s
= XILINX_VERSAL_USB2_CTRL_REGS(reg
->opaque
);
96 s
->regs
[R_IR_MASK
] &= ~val
;
101 static uint64_t ir_disable_prew(RegisterInfo
*reg
, uint64_t val64
)
103 VersalUsb2CtrlRegs
*s
= XILINX_VERSAL_USB2_CTRL_REGS(reg
->opaque
);
104 uint32_t val
= val64
;
106 s
->regs
[R_IR_MASK
] |= val
;
111 static const RegisterAccessInfo usb2_ctrl_regs_regs_info
[] = {
112 { .name
= "BUS_FILTER", .addr
= A_BUS_FILTER
,
114 },{ .name
= "PORT", .addr
= A_PORT
,
116 },{ .name
= "JITTER_ADJUST", .addr
= A_JITTER_ADJUST
,
119 },{ .name
= "BIGENDIAN", .addr
= A_BIGENDIAN
,
121 },{ .name
= "COHERENCY", .addr
= A_COHERENCY
,
123 },{ .name
= "XHC_BME", .addr
= A_XHC_BME
,
126 },{ .name
= "REG_CTRL", .addr
= A_REG_CTRL
,
128 },{ .name
= "IR_STATUS", .addr
= A_IR_STATUS
,
131 .post_write
= ir_status_postw
,
132 },{ .name
= "IR_MASK", .addr
= A_IR_MASK
,
136 },{ .name
= "IR_ENABLE", .addr
= A_IR_ENABLE
,
138 .pre_write
= ir_enable_prew
,
139 },{ .name
= "IR_DISABLE", .addr
= A_IR_DISABLE
,
141 .pre_write
= ir_disable_prew
,
142 },{ .name
= "USB3", .addr
= A_USB3
,
146 static void usb2_ctrl_regs_reset_init(Object
*obj
, ResetType type
)
148 VersalUsb2CtrlRegs
*s
= XILINX_VERSAL_USB2_CTRL_REGS(obj
);
151 for (i
= 0; i
< ARRAY_SIZE(s
->regs_info
); ++i
) {
152 register_reset(&s
->regs_info
[i
]);
156 static void usb2_ctrl_regs_reset_hold(Object
*obj
)
158 VersalUsb2CtrlRegs
*s
= XILINX_VERSAL_USB2_CTRL_REGS(obj
);
163 static const MemoryRegionOps usb2_ctrl_regs_ops
= {
164 .read
= register_read_memory
,
165 .write
= register_write_memory
,
166 .endianness
= DEVICE_LITTLE_ENDIAN
,
168 .min_access_size
= 4,
169 .max_access_size
= 4,
173 static void usb2_ctrl_regs_init(Object
*obj
)
175 VersalUsb2CtrlRegs
*s
= XILINX_VERSAL_USB2_CTRL_REGS(obj
);
176 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
177 RegisterInfoArray
*reg_array
;
179 memory_region_init(&s
->iomem
, obj
, TYPE_XILINX_VERSAL_USB2_CTRL_REGS
,
180 USB2_REGS_R_MAX
* 4);
182 register_init_block32(DEVICE(obj
), usb2_ctrl_regs_regs_info
,
183 ARRAY_SIZE(usb2_ctrl_regs_regs_info
),
184 s
->regs_info
, s
->regs
,
186 XILINX_VERSAL_USB2_CTRL_REGS_ERR_DEBUG
,
187 USB2_REGS_R_MAX
* 4);
188 memory_region_add_subregion(&s
->iomem
,
191 sysbus_init_mmio(sbd
, &s
->iomem
);
192 sysbus_init_irq(sbd
, &s
->irq_ir
);
195 static const VMStateDescription vmstate_usb2_ctrl_regs
= {
196 .name
= TYPE_XILINX_VERSAL_USB2_CTRL_REGS
,
198 .minimum_version_id
= 1,
199 .fields
= (VMStateField
[]) {
200 VMSTATE_UINT32_ARRAY(regs
, VersalUsb2CtrlRegs
, USB2_REGS_R_MAX
),
201 VMSTATE_END_OF_LIST(),
205 static void usb2_ctrl_regs_class_init(ObjectClass
*klass
, void *data
)
207 DeviceClass
*dc
= DEVICE_CLASS(klass
);
208 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
210 rc
->phases
.enter
= usb2_ctrl_regs_reset_init
;
211 rc
->phases
.hold
= usb2_ctrl_regs_reset_hold
;
212 dc
->vmsd
= &vmstate_usb2_ctrl_regs
;
215 static const TypeInfo usb2_ctrl_regs_info
= {
216 .name
= TYPE_XILINX_VERSAL_USB2_CTRL_REGS
,
217 .parent
= TYPE_SYS_BUS_DEVICE
,
218 .instance_size
= sizeof(VersalUsb2CtrlRegs
),
219 .class_init
= usb2_ctrl_regs_class_init
,
220 .instance_init
= usb2_ctrl_regs_init
,
223 static void usb2_ctrl_regs_register_types(void)
225 type_register_static(&usb2_ctrl_regs_info
);
228 type_init(usb2_ctrl_regs_register_types
)