Skip to content

Commit 30f1a99

Browse files
authored
Merge pull request #264 from tfc/master
Raspberry pi 4: Add dwc2 usb otg gadget support
2 parents c4399b9 + e6c84e7 commit 30f1a99

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

raspberry-pi/4/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
{
44
imports = [
5+
./dwc2.nix
56
./modesetting.nix
67
];
78

@@ -15,6 +16,7 @@
1516
};
1617
};
1718

19+
hardware.deviceTree.filter = "bcm2711-rpi-*.dtb";
1820

1921
# Required for the Wireless firmware
2022
hardware.enableRedistributableFirmware = true;

raspberry-pi/4/dwc2.nix

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

raspberry-pi/4/modesetting.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ in
2929
config = lib.mkIf cfg.enable {
3030
# Configure for modesetting in the device tree
3131
hardware.deviceTree = {
32-
filter = "bcm2711-rpi-*.dtb";
3332
overlays = [
3433
# Equivalent to:
3534
# https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/overlays/cma-overlay.dts

0 commit comments

Comments
 (0)