4 * Copyright (c) 2020 Guenter Roeck <linux@roeck-us.net>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 * We need to implement basic reset control in the PHY control register.
10 * For everything else, it is sufficient to set whatever is written.
13 #include "qemu/osdep.h"
14 #include "hw/usb/imx-usb-phy.h"
15 #include "migration/vmstate.h"
17 #include "qemu/module.h"
19 static const VMStateDescription vmstate_imx_usbphy
= {
20 .name
= TYPE_IMX_USBPHY
,
22 .minimum_version_id
= 1,
23 .fields
= (VMStateField
[]) {
24 VMSTATE_UINT32_ARRAY(usbphy
, IMXUSBPHYState
, USBPHY_MAX
),
29 static void imx_usbphy_softreset(IMXUSBPHYState
*s
)
31 s
->usbphy
[USBPHY_PWD
] = 0x001e1c00;
32 s
->usbphy
[USBPHY_TX
] = 0x10060607;
33 s
->usbphy
[USBPHY_RX
] = 0x00000000;
34 s
->usbphy
[USBPHY_CTRL
] = 0xc0200000;
37 static void imx_usbphy_reset(DeviceState
*dev
)
39 IMXUSBPHYState
*s
= IMX_USBPHY(dev
);
41 s
->usbphy
[USBPHY_STATUS
] = 0x00000000;
42 s
->usbphy
[USBPHY_DEBUG
] = 0x7f180000;
43 s
->usbphy
[USBPHY_DEBUG0_STATUS
] = 0x00000000;
44 s
->usbphy
[USBPHY_DEBUG1
] = 0x00001000;
45 s
->usbphy
[USBPHY_VERSION
] = 0x04020000;
47 imx_usbphy_softreset(s
);
50 static uint64_t imx_usbphy_read(void *opaque
, hwaddr offset
, unsigned size
)
52 IMXUSBPHYState
*s
= (IMXUSBPHYState
*)opaque
;
53 uint32_t index
= offset
>> 2;
61 case USBPHY_DEBUG_SET
:
62 case USBPHY_DEBUG1_SET
:
64 * All REG_NAME_SET register access are in fact targeting the
67 value
= s
->usbphy
[index
- 1];
73 case USBPHY_DEBUG_CLR
:
74 case USBPHY_DEBUG1_CLR
:
76 * All REG_NAME_CLR register access are in fact targeting the
79 value
= s
->usbphy
[index
- 2];
85 case USBPHY_DEBUG_TOG
:
86 case USBPHY_DEBUG1_TOG
:
88 * All REG_NAME_TOG register access are in fact targeting the
91 value
= s
->usbphy
[index
- 3];
94 if (index
< USBPHY_MAX
) {
95 value
= s
->usbphy
[index
];
97 qemu_log_mask(LOG_GUEST_ERROR
,
98 "%s: Read from non-existing USB PHY register 0x%"
105 return (uint64_t)value
;
108 static void imx_usbphy_write(void *opaque
, hwaddr offset
, uint64_t value
,
111 IMXUSBPHYState
*s
= (IMXUSBPHYState
*)opaque
;
112 uint32_t index
= offset
>> 2;
116 s
->usbphy
[index
] = value
;
117 if (value
& USBPHY_CTRL_SFTRST
) {
118 imx_usbphy_softreset(s
);
127 s
->usbphy
[index
] = value
;
129 case USBPHY_CTRL_SET
:
130 s
->usbphy
[index
- 1] |= value
;
131 if (value
& USBPHY_CTRL_SFTRST
) {
132 imx_usbphy_softreset(s
);
138 case USBPHY_DEBUG_SET
:
139 case USBPHY_DEBUG1_SET
:
141 * All REG_NAME_SET register access are in fact targeting the
142 * REG_NAME register. So we change the value of the REG_NAME
143 * register, setting bits passed in the value.
145 s
->usbphy
[index
- 1] |= value
;
150 case USBPHY_CTRL_CLR
:
151 case USBPHY_DEBUG_CLR
:
152 case USBPHY_DEBUG1_CLR
:
154 * All REG_NAME_CLR register access are in fact targeting the
155 * REG_NAME register. So we change the value of the REG_NAME
156 * register, unsetting bits passed in the value.
158 s
->usbphy
[index
- 2] &= ~value
;
160 case USBPHY_CTRL_TOG
:
161 s
->usbphy
[index
- 3] ^= value
;
162 if ((value
& USBPHY_CTRL_SFTRST
) &&
163 (s
->usbphy
[index
- 3] & USBPHY_CTRL_SFTRST
)) {
164 imx_usbphy_softreset(s
);
170 case USBPHY_DEBUG_TOG
:
171 case USBPHY_DEBUG1_TOG
:
173 * All REG_NAME_TOG register access are in fact targeting the
174 * REG_NAME register. So we change the value of the REG_NAME
175 * register, toggling bits passed in the value.
177 s
->usbphy
[index
- 3] ^= value
;
180 /* Other registers are read-only or do not exist */
181 qemu_log_mask(LOG_GUEST_ERROR
,
182 "%s: Write to %s USB PHY register 0x%"
185 index
>= USBPHY_MAX
? "non-existing" : "read-only",
191 static const struct MemoryRegionOps imx_usbphy_ops
= {
192 .read
= imx_usbphy_read
,
193 .write
= imx_usbphy_write
,
194 .endianness
= DEVICE_NATIVE_ENDIAN
,
197 * Our device would not work correctly if the guest was doing
198 * unaligned access. This might not be a limitation on the real
199 * device but in practice there is no reason for a guest to access
200 * this device unaligned.
202 .min_access_size
= 4,
203 .max_access_size
= 4,
208 static void imx_usbphy_realize(DeviceState
*dev
, Error
**errp
)
210 IMXUSBPHYState
*s
= IMX_USBPHY(dev
);
212 memory_region_init_io(&s
->iomem
, OBJECT(s
), &imx_usbphy_ops
, s
,
213 "imx-usbphy", 0x1000);
214 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
217 static void imx_usbphy_class_init(ObjectClass
*klass
, void *data
)
219 DeviceClass
*dc
= DEVICE_CLASS(klass
);
221 dc
->reset
= imx_usbphy_reset
;
222 dc
->vmsd
= &vmstate_imx_usbphy
;
223 dc
->desc
= "i.MX USB PHY Module";
224 dc
->realize
= imx_usbphy_realize
;
227 static const TypeInfo imx_usbphy_info
= {
228 .name
= TYPE_IMX_USBPHY
,
229 .parent
= TYPE_SYS_BUS_DEVICE
,
230 .instance_size
= sizeof(IMXUSBPHYState
),
231 .class_init
= imx_usbphy_class_init
,
234 static void imx_usbphy_register_types(void)
236 type_register_static(&imx_usbphy_info
);
239 type_init(imx_usbphy_register_types
)