opentitan icon indicating copy to clipboard operation
opentitan copied to clipboard

[USB] usbdev RTL simulation

Open stefanlippuner opened this issue 7 years ago • 7 comments

Hi all,

I'm interested in your USB FS peripheral, as we'd like to add USB functionality to our PULP-based SoC. Pirmin mentioned that you already have such an IP in your project. It looks quite good, thanks for putting all the effort into the documentation :)

As a first step, I would like to run an RTL simulation of your usbdev peripheral. From what I can see, there is no block-level testbench, but there is software and a top-level with an USB host.

What I've tried so far: I clone your repository at e825e6c37abdbdcca92e2174bc80374816f58883 and I've set up Verilator and your RISC-V toolchain. Then I followed the guide at https://docs.opentitan.org/doc/ug/getting_started_verilator/ and was able to get the Hello World output on the UART:

fusesoc --cores-root . run --target=sim --setup --build lowrisc:systems:top_earlgrey_verilator
make -C sw/device SIM=1 SW_DIR=examples/hello_world \
  SW_BUILD_DIR=sim_hello_world clean all
build/lowrisc_systems_top_earlgrey_verilator_0.1/sim-verilator/Vtop_earlgrey_verilator   --rominit=sw/device/sim_boot_rom/rom.vmem   --flashinit=sw/device/sim_hello_world/sw.vmem

Based on this, I wanted to run the USB simulation with top_earlgrey_usb_verilator and the program examples/hello_usbdev. I was able to compile the hello_usbdev software example, but I was not able to build the USB simulation target: usbdev_log_1.txt. Using some minor changes (usbdev_diff.txt), I was able to fix the package errors, but the build did still not pass: usbdev_log_2.txt. It seems, as if top_earlgrey_usb.sv is not compatible with the (current version?) of the ibex_core.

Could you perhaps point me in the right direction on how to run an RTL simulation for the usbdev block?

Cheers Stefan

stefanlippuner avatar Nov 18 '19 14:11 stefanlippuner

Hello @stefanlippuner, thanks for reaching out, and thank you for your interest in our project.

The history of USB in project is mostly one of experimentation. You probably noticed that it is not listed in our dashboard. At the moment, there is not a designated product use case for it, but we expect that that request will come. Therefore we are interested in seeing it developed, if you also have an interest.

You also found top_earlgrey_usb* which are top levels that experiment with its inclusion, but there has not been a lot of activity of late to keep it up to date with the primary top level netlists. Thus it is currently in a bit of a caveat emptor status.

The code was mostly vendored in from tiny-fpga. From that point we swapped in our primitives, put some wrappers around it, and got it working at the top level, but that was months ago. The upstream code is not code style compliant, and not heavily verified.

We are interested in seeing this developed but currently don't have it prioritized highly. If you wish to move it forward we can work with you on it, but true DV resources aren't available. We would encourage you to get it back alive on verilator and/or FPGA, and hopefully you can see a path to that by contrasting the top levels and making the appropriate wiring changes. If you're able to get it to that point, feel free to submit a PR and we can continue to maintain it here. If things have diverged too far, and you see another path forward (including it in the primary top_earlgrey for instance), we can discuss that. Also, if you're willing to contribute DV resources on it that would be ideal! :smile:

In terms of primary contacts, I believe @vogelpi is offering some assistance, thanks Pirmin! Also @mdhayter has been the one primarily experimenting with the IP. He is not full time on the project but has offered to answer questions that might arise.

As with all development in our program, please consult the style guides and Comportability specification for how to conform with the rest of the project. As mentioned above, this IP isn't completely conformant, but for it to be truly included we would need that to be the case.

Thanks again

sjgitty avatar Nov 19 '19 23:11 sjgitty

Thanks for your reply!

With some help from @vogelpi I was able to update the top-level module with USB. After fixing some minor mismatches, I was able to simulate 20 USB Frames with the hello_usb application. This should allow me to start adding it to our system. Link: https://github.com/stefanlippuner/opentitan/tree/usb

I'd also like to upstream my fixes, but I'm not quite sure on how to proceed there. Pirmin also mentioned, that you would be interested in keeping the USB toplevel up to date and adding it to the CI. This would require making the USB toplevel auto-generated, which I should be able to do (maybe with some light support). We saw multiple ways to create this second toplevel:

  • Duplicate the top and xbar configuration and keep the files in the top_earlgrey directory (similar to the existing version)
  • Duplicate the entire top_earlgrey directory
  • Add an option to the configs to include the USB device
  • ...?

I feel that this is a decision that I should leave up to you. If you'd like me to help with the auto-generated USB toplevel, let me know how I should structure it.

stefanlippuner avatar Nov 20 '19 15:11 stefanlippuner

Nice work @stefanlippuner . I suggest the following procedure:

  1. Prepare a PR to bring in the current changes, i.e., the manual re-alignment of the top with USB support.
  2. Generate this top with our auto-gen tools, i.e., create the required template files. Create a PR for that.
  3. Add this version of the top to CI to make sure it does not break again.

Regarding the question of where to store the USB version: This should remain separate and I suggest to create a new directory under hw called top_earlgrey_usb. This would allow us to keep a different set of auto-generated IPs under top_earlgrey_usb/ip (for example the crossbar needs to be different unless we want to stick selecting either SPI or USB using a parameter as before). But maybe @sjgitty can share his opinion here.

Best, Pirmin

vogelpi avatar Nov 22 '19 14:11 vogelpi

Alright, the first PR re-aligning the USB top with the current repo state has finally landed. The next step is to move to auto-generate for this.

Before doing that, we should discuss, how to integrate the USB module. There are basically two options 1) have a separate top with the USB included (but don't include it in the normal top), 2) include it in the normal top.

For option 2), we would IMO need to further clean up the code, in particular the code vendored in from tinyFPGA in hw/vendor/tinyfpga_tinyfpga-bootloader. For example, this code is currently Verilog and registers cannot be reset. As far as I understood, @stefanlippuner already did that and would be willing to contribute this code. In addition, we should probably find someone inside the project willing to maintain the module, and maybe plan DV? WDYT @sjgitty , @imphil @tjaychen?

vogelpi avatar Dec 03 '19 08:12 vogelpi

Thanks for merging the PR!

Yes, I've cleaned up the tinyfpga files to the point where they match our own standards (SV, no initial values, async. reset). As far as I can see, only three files from tinyfpga are actually used (in usbdev). I could upstream these changes to you if you'd like.

On my side I've gotten the IP + demo software to work in our system. The virtual serial port works quite reliably now with only some minor changes. I actually ran the USB protocol tests and they actually passed, but I'm still somwhat suspicious of that result. @mdhayter did you already test against USBCV?

stefanlippuner avatar Dec 05 '19 08:12 stefanlippuner

Slowly the tinyfpga files got replaced (when I moved from the PE with buffers integrated to the unbuffered version) apart from those few physical layer files. If they are still compatible I think the tinyfpga would accept PRs, although we may need to check they work with the Lattice workflow they use.

I didn't do any formal USB checking (mostly tested with FPGA connected to Linux or ChromeOS)

mdhayter avatar Dec 05 '19 19:12 mdhayter

If you're volunteering to upstream the changes to tinyfpga, sure. I'm willing to port the changes back to your system, but I can't really justify setting up yet another project and getting a PR accepted there.

With the current master (probably since #903 ) the USB top simulation is broken again. I think this is a good example why the usb_top should be aligned in a more lasting way.

stefanlippuner avatar Dec 09 '19 08:12 stefanlippuner