|
| 1 | +{ config, lib, pkgs, ... }: |
| 2 | + |
| 3 | +let |
| 4 | + cfg = config.hardware.raspberry-pi."4".dwc2; |
| 5 | +in |
| 6 | +{ |
| 7 | + options.hardware = { |
| 8 | + raspberry-pi."4".dwc2 = { |
| 9 | + enable = lib.mkEnableOption '' |
| 10 | + Enable the UDC controller to support USB OTG gadget functions. |
| 11 | +
|
| 12 | + In order to verify that this works, connect the Raspberry Pi with |
| 13 | + another computer via the USB C cable, and then do one of: |
| 14 | +
|
| 15 | + - `modprobe g_serial` |
| 16 | + - `modprobe g_mass_storage file=/path/to/some/iso-file.iso` |
| 17 | +
|
| 18 | + On the Raspberry Pi, `dmesg` should then show success-indicating output |
| 19 | + that is related to the dwc2 and g_serial/g_mass_storage modules. |
| 20 | + On the other computer, a serial/mass-storage device should pop up in |
| 21 | + the system logs. |
| 22 | +
|
| 23 | + For more information about what gadget functions exist along with handy |
| 24 | + guides on how to test them, please refer to: |
| 25 | + https://www.kernel.org/doc/Documentation/usb/gadget-testing.txt |
| 26 | + ''; |
| 27 | + dr_mode = lib.mkOption { |
| 28 | + type = lib.types.enum [ "host" "peripheral" "otg" ]; |
| 29 | + default = "otg"; |
| 30 | + description = '' |
| 31 | + Dual role mode setting for the dwc2 USB controller driver. |
| 32 | + ''; |
| 33 | + }; |
| 34 | + }; |
| 35 | + }; |
| 36 | + |
| 37 | + config = lib.mkIf cfg.enable { |
| 38 | + # Configure for modesetting in the device tree |
| 39 | + hardware.deviceTree = { |
| 40 | + overlays = [ |
| 41 | + # this *should* be equivalent to (which doesn't work): |
| 42 | + # https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/overlays/dwc2-overlay.dts |
| 43 | + # but actually it's obtained using |
| 44 | + # dtc -I dtb -O dts ${config.hardware.deviceTree.kernelPackage}/dtbs/overlays/dwc2.dtbo |
| 45 | + # (changes: modified top-level "compatible" field) |
| 46 | + # which is slightly different and works |
| 47 | + { |
| 48 | + name = "dwc2-overlay"; |
| 49 | + dtsText = '' |
| 50 | + /dts-v1/; |
| 51 | + /plugin/; |
| 52 | +
|
| 53 | + / { |
| 54 | + compatible = "brcm,bcm2711"; |
| 55 | +
|
| 56 | + fragment@0 { |
| 57 | + target = <&usb>; |
| 58 | + #address-cells = <0x01>; |
| 59 | + #size-cells = <0x01>; |
| 60 | +
|
| 61 | + __overlay__ { |
| 62 | + compatible = "brcm,bcm2835-usb"; |
| 63 | + dr_mode = "${cfg.dr_mode}"; |
| 64 | + g-np-tx-fifo-size = <0x20>; |
| 65 | + g-rx-fifo-size = <0x22e>; |
| 66 | + g-tx-fifo-size = <0x200 0x200 0x200 0x200 0x200 0x100 0x100>; |
| 67 | + status = "okay"; |
| 68 | + phandle = <0x01>; |
| 69 | + }; |
| 70 | + }; |
| 71 | + }; |
| 72 | + ''; |
| 73 | + } |
| 74 | + ]; |
| 75 | + }; |
| 76 | + }; |
| 77 | +} |
0 commit comments