Skip to content

Conversation

pamaury
Copy link

@pamaury pamaury commented Sep 10, 2025

Replaces #164. This creates a minimal implementation of the usbdev block. Currently, the only thing supported are interrupts and also VBUS handling through a dedicated chardev channel. This allows on to pass the usbdev_vbus_test (after some opentitantool changes). Since the usbdev driver will be quite complicated, I think it's better to start small and build up. I also plan to make it multi-file so I created a directory for it.

@pamaury pamaury changed the base branch from usbdev to ot-earlgrey-9.2.0 September 10, 2025 11:51
@pamaury pamaury force-pushed the usbdev branch 2 times, most recently from b3d7d26 to 49df0bb Compare September 10, 2025 12:48
Copy link

@rivos-eblot rivos-eblot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we want to merge a full dummy device.
It seems for now it does nothing really useful(?)

What does this bring vs. the OT_UNIMP device it replaces?

// IBEX_DEV_STRING_PROP("ot_id", "usbdev"),
// IBEX_DEV_UINT_PROP("irq-count", 18u),
// IBEX_DEV_UINT_PROP("alert-count", 1u),
IBEX_DEV_UINT_PROP("pclk", OT_EG_USBDEV_CLK_HZ)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an outdated way to configure the clock. USB device should be connected to the clock manager instead (see other peripherals for example, e.g. OT_UART)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may have commented on the old version of the PR? The current version does not have pclk anymore. Can you let me know if the code is correct though?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry I missed your comment. New code seems fine.

.gpio = IBEXGPIOCONNDEFS(
OT_EG_SOC_GPIO_ALERT(0, 21)
)
// IBEX_DEV_STRING_PROP("ot_id", "usbdev"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code to be removed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a comment on the outdated code

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a comment on the outdated code

Please let me know when it is ready to review: even when I refresh I see comments that seem more recent than the actual code which seems to move while I review it :-)

review

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum interesting ^^ I will move the PR to draft and set it to ready againa after I fixed everything

#include "hw/opentitan/ot_sram_ctrl.h"
#include "hw/opentitan/ot_timer.h"
#include "hw/opentitan/ot_uart.h"
#include "hw/opentitan/ot_usbdev.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems in wrong order, see scripts/opentitan/ot-format.sh

Note that we discovered earlier this week that the CI does not fail when this script is called (it is missing some extra commands that are part of the gitlab CI but did not make it to the github version, my mistake)

@pamaury pamaury marked this pull request as draft September 10, 2025 13:16
@pamaury pamaury force-pushed the usbdev branch 3 times, most recently from 8048aee to 48bf161 Compare September 16, 2025 12:18
@pamaury pamaury changed the title [opentitan, usbdev] Create a dummy usbdev block [opentitan, usbdev] Create initial usbdev block Sep 30, 2025
@pamaury pamaury requested a review from rivos-eblot September 30, 2025 09:52
@pamaury pamaury force-pushed the usbdev branch 2 times, most recently from 6f5515e to 521a3c0 Compare September 30, 2025 09:56
OT_EG_SOC_GPIO_SYSBUS_IRQ(16, PLIC, 151),
OT_EG_SOC_GPIO_SYSBUS_IRQ(17, PLIC, 152),
OT_EG_SOC_GPIO_ALERT(0, 21)
// TODO Model sense pin
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove this comments, the sense pin is modelled as a real pin because it doesn't make sense for a virtual USB bus.

@@ -0,0 +1,35 @@
/*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep a flat, single file hierarchy? This would be more consisten to all other OT implementation as well as QEMU.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The USB driver will be much too big for a single file I think, unless you are happy with 3000+ lines of code in a single file?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it is better (there are several other files that are longer). There is a couple of exceptions (OTP) which is even longer but has fully-generated C file as it was easier to maintain/upgrade as a separate file. They all stands in the same directory though. We try to keep all IPs with the same structure to it is easier to move from one to another. If the file is getting very big once implemented, we can review this policy and split it (in the same directory) if that makes sense; however for now, I think a single file is ok.


/* VBUS control mode. */
typedef enum OtUsbdevVbusMode {
OT_USBDEV_VBUS_OVERRIDE,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to get it, since there is only one value (== 0) in this enum, whatever the value of vbus_mode_str, vbus_mode is always assigned OT_USBDEV_VBUS_OVERRIDE.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's partly explained in usbdev.md: when we have real USB/IP support, we want to have two modes: one where VBUS is controlled explicitely (independently of the host), and one where VBUS corresponds to host presence (probably the sane default). At the moment we do not have "host" but we still want to control vbus. I can remove that if you want, but it will be added later anyway.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I get it, although it is a bit weird not to have a defaut non-override value, which could not be selected since you forced it to OVERRIDE through the property anyway.

BTW, as the property is called "vbus-mode", would it not be "override" rather than "vbus-override"? Or since I'm not sure to understand how many "vbus-mode" are planned to be supported, is it not boolean property, i.e. "vbus-override=on/off"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think we could make it a boolean property

REGS_SIZE);
sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);

s->link_state = OT_USBDEV_LINK_STATE_DISCONNECTED;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be part of the reset handler.
Everything is zero-initialized by QEMU

s->vbus_mode = OT_USBDEV_VBUS_OVERRIDE;
}
else {
error_setg(errp, "%s: %s: invalid VBUS mode: %s", __func__, s->ot_id, s->vbus_mode_str);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit pick: Prefer &error_fatal vs errp which defaults to error_abort, which in turn leaves the terminal is an awkward state as it is not properly restored when QEMU abruptly exits :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants