USB: Add cadence USB3 gadget/host/phy driver
Subsystems (7)
v4 cover USB: Add cadence USB3 gadget/host/phy driver
These patches introduce new Cadence driver to U-Boot.
The first patch is to add the Cadence USB3 IP(CDNS3) core and driver for the usb gadget.
The second patch introduce the xhci-imx8 usb host driver separately. The third patch introduce the cdns3 phy driver which can be used for both cdns3 host driver and gadget driver.
The cdns3 usb gadget/host/phy driver are all used DM mode.
The current driver has been validated on i.MX8 platform.
If someone want to test it, please note that the additional dts nodes/ config macros/clock driver are also essential. You can also get my test patches at https://github.com/sherrysun1/u-boot-imx.git to start your test quickly.
Changes in v4:
- Keep all include list in files sorted.
- Delete the unnecessary registers read in code.
- Add ret return after wait_for_bit_le32() is failed.
- Add macros instead those magic values.
Sherry Sun (4):
USB: gadget: Add the cadence USB3 gadget driver
USB: host: Add the USB3 host driver
phy: Add USB PHY driver for the cadence USB3
USB: gadget: core: introduce ->udc_set_speed() method
Makefile | 1 + doc/device-tree-bindings/usb/cdns-usb3.txt | 39 + drivers/phy/Kconfig | 8 + drivers/phy/Makefile | 1 + drivers/phy/cdns3-usb-phy.c | 241 +++ drivers/usb/Kconfig | 2 + drivers/usb/cdns3/Kconfig | 20 + drivers/usb/cdns3/Makefile | 5 + drivers/usb/cdns3/cdns3-generic.c | 113 + drivers/usb/cdns3/cdns3-nxp-reg-def.h | 93 + drivers/usb/cdns3/core.c | 203 ++ drivers/usb/cdns3/core.h | 118 ++ drivers/usb/cdns3/dev-regs-macro.h | 116 + drivers/usb/cdns3/dev-regs-map.h | 117 ++ drivers/usb/cdns3/gadget-export.h | 26 + drivers/usb/cdns3/gadget.c | 2218 ++++++++++++++++++++ drivers/usb/cdns3/gadget.h | 225 ++ drivers/usb/cdns3/io.h | 30 + drivers/usb/gadget/epautoconf.c | 4 + drivers/usb/gadget/gadget_chips.h | 7 + drivers/usb/gadget/udc/Makefile | 1 + drivers/usb/gadget/udc/udc-core.c | 23 + drivers/usb/host/Kconfig | 9 + drivers/usb/host/Makefile | 1 + drivers/usb/host/xhci-imx8.c | 209 ++ include/linux/usb/gadget.h | 5 + scripts/Makefile.spl | 1 + 27 files changed, 3836 insertions(+) create mode 100644 doc/device-tree-bindings/usb/cdns-usb3.txt create mode 100644 drivers/phy/cdns3-usb-phy.c create mode 100644 drivers/usb/cdns3/Kconfig create mode 100644 drivers/usb/cdns3/Makefile create mode 100644 drivers/usb/cdns3/cdns3-generic.c create mode 100644 drivers/usb/cdns3/cdns3-nxp-reg-def.h create mode 100644 drivers/usb/cdns3/core.c create mode 100644 drivers/usb/cdns3/core.h create mode 100644 drivers/usb/cdns3/dev-regs-macro.h create mode 100644 drivers/usb/cdns3/dev-regs-map.h create mode 100644 drivers/usb/cdns3/gadget-export.h create mode 100644 drivers/usb/cdns3/gadget.c create mode 100644 drivers/usb/cdns3/gadget.h create mode 100644 drivers/usb/cdns3/io.h create mode 100644 drivers/usb/host/xhci-imx8.c
Signature · 1 line
2.17.1
PATCH v4 1/4 USB: gadget: Add the cadence USB3 gadget driver
This driver is ported from NXP i.MX U-Boot version imx_v2019.04 and some changes have also been made to adapt to U-Boot.
Add the Cadence USB3 IP(CDNS3) driver for the gadget (device mode). The CDNS3 gadget driver support DM mode. CONFIG_DM_USB_GADGET should be enabled when use this driver.
--- Makefile | 1 + doc/device-tree-bindings/usb/cdns-usb3.txt | 39 + drivers/usb/Kconfig | 2 + drivers/usb/cdns3/Kconfig | 20 + drivers/usb/cdns3/Makefile | 5 + drivers/usb/cdns3/cdns3-generic.c | 113 + drivers/usb/cdns3/cdns3-nxp-reg-def.h | 93 + drivers/usb/cdns3/core.c | 203 ++ drivers/usb/cdns3/core.h | 118 ++ drivers/usb/cdns3/dev-regs-macro.h | 116 + drivers/usb/cdns3/dev-regs-map.h | 117 ++ drivers/usb/cdns3/gadget-export.h | 26 + drivers/usb/cdns3/gadget.c | 2218 ++++++++++++++++++++ drivers/usb/cdns3/gadget.h | 225 ++ drivers/usb/cdns3/io.h | 30 + drivers/usb/gadget/epautoconf.c | 4 + drivers/usb/gadget/gadget_chips.h | 7 + drivers/usb/gadget/udc/Makefile | 1 + include/linux/usb/gadget.h | 3 + scripts/Makefile.spl | 1 + 20 files changed, 3342 insertions(+) create mode 100644 doc/device-tree-bindings/usb/cdns-usb3.txt create mode 100644 drivers/usb/cdns3/Kconfig create mode 100644 drivers/usb/cdns3/Makefile create mode 100644 drivers/usb/cdns3/cdns3-generic.c create mode 100644 drivers/usb/cdns3/cdns3-nxp-reg-def.h create mode 100644 drivers/usb/cdns3/core.c create mode 100644 drivers/usb/cdns3/core.h create mode 100644 drivers/usb/cdns3/dev-regs-macro.h create mode 100644 drivers/usb/cdns3/dev-regs-map.h create mode 100644 drivers/usb/cdns3/gadget-export.h create mode 100644 drivers/usb/cdns3/gadget.c create mode 100644 drivers/usb/cdns3/gadget.h create mode 100644 drivers/usb/cdns3/io.h
Makefile +1 -0
@@ -728,6 +728,7 @@ libs-$(CONFIG_SYS_FSL_DDR) += drivers/ddr/fsl/ libs-$(CONFIG_SYS_FSL_MMDC) += drivers/ddr/fsl/ libs-$(CONFIG_$(SPL_)ALTERA_SDRAM) += drivers/ddr/altera/ libs-y += drivers/serial/ +libs-y += drivers/usb/cdns3/ libs-y += drivers/usb/dwc3/ libs-y += drivers/usb/common/ libs-y += drivers/usb/emul/
doc/device-tree-bindings/usb/cdns-usb3.txt new file +39 -0
@@ -0,0 +1,39 @@ +* Cadence USB3 Controller + +Required properties: +- compatible: "cdns,usb3"; +- reg: base address and length of the registers +- interrupts: interrupt for the USB controller +- interrupt-parent: the interrupt parent for this module +- clocks: reference to the USB clock +- clock-names: the name of clocks +- phys: reference to the USB PHY + +Optional properties: +- dr_mode: One of "host", "peripheral" or "otg". Defaults to "otg" +- extcon: extcon phandler for cdns3 device +- power-domains: the power domain for cdns3 controller and phy + +Examples: + +usbotg3: cdns3 at 5b110000 { + compatible = "cdns,usb3"; + reg = <0x0 0x5B110000 0x0 0x10000>, + <0x0 0x5B130000 0x0 0x10000>, + <0x0 0x5B140000 0x0 0x10000>, + <0x0 0x5B160000 0x0 0x40000>; + interrupt-parent = <&gic>; + interrupts = <GIC_SPI 271 IRQ_TYPE_LEVEL_HIGH>; + clocks = <&clk IMX8QM_USB3_LPM_CLK>, + <&clk IMX8QM_USB3_BUS_CLK>, + <&clk IMX8QM_USB3_ACLK>, + <&clk IMX8QM_USB3_IPG_CLK>, + <&clk IMX8QM_USB3_CORE_PCLK>; + clock-names = "usb3_lpm_clk", "usb3_bus_clk", "usb3_aclk", + "usb3_ipg_clk", "usb3_core_pclk"; + power-domains = <&pd_conn_usb2>; + phys = <&usbphy1>; + dr_mode = "otg"; + extcon = <&typec_ptn5150>; + status = "disabled"; +};
drivers/usb/Kconfig +2 -0
@@ -70,6 +70,8 @@ source "drivers/usb/host/Kconfig" source "drivers/usb/dwc3/Kconfig" +source "drivers/usb/cdns3/Kconfig" + source "drivers/usb/musb/Kconfig" source "drivers/usb/musb-new/Kconfig"
drivers/usb/cdns3/Kconfig new file +20 -0
@@ -0,0 +1,20 @@ +config USB_CDNS3 + tristate "Cadence USB3 Dual-Role Controller" + depends on (USB && USB_GADGET) + help + Say Y here if your system has a cadence USB3 dual-role controller. + It supports: dual-role switch Host-only, and Peripheral-only. + + When compiled dynamically, the module will be called cdns3.ko. + +if USB_CDNS3 + +config USB_CDNS3_GADGET + bool "Cadence USB3 device controller" + depends on USB_GADGET + select USB_GADGET_DUALSPEED + help + Say Y here to enable device controller functionality of the + cadence usb3 driver. + +endif
drivers/usb/cdns3/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +obj-$(CONFIG_USB_CDNS3) += cdns3.o + +cdns3-y := core.o +cdns3-$(CONFIG_USB_CDNS3_GADGET) += gadget.o +cdns3-$(CONFIG_$(SPL_)DM_USB_GADGET) += cdns3-generic.o
drivers/usb/cdns3/cdns3-generic.c new file +113 -0
113 lines not on this page · raw message
drivers/usb/cdns3/cdns3-nxp-reg-def.h new file +93 -0
93 lines not on this page · raw message
drivers/usb/cdns3/core.c new file +203 -0
203 lines not on this page · raw message
drivers/usb/cdns3/core.h new file +118 -0
118 lines not on this page · raw message
drivers/usb/cdns3/dev-regs-macro.h new file +116 -0
116 lines not on this page · raw message
drivers/usb/cdns3/dev-regs-map.h new file +117 -0
117 lines not on this page · raw message
drivers/usb/cdns3/gadget-export.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Copyright 2019 NXP +*/ + +#ifndef __CDNS3_GADGET_EXPORT_H +#define __CDNS3_GADGET_EXPORT_H + +#ifdef CONFIG_USB_CDNS3_GADGET + +int cdns3_gadget_init (struct cdns3 *cdns); +void cdns3_gadget_remove (struct cdns3 *cdns); +#else + +static inline int cdns3_gadget_init (struct cdns3 *cdns) +{ +return -ENXIO ; +} + +static inline void cdns3_gadget_remove (struct cdns3 *cdns) +{ +} + +#endif + +#endif /* __CDNS3_GADGET_EXPORT_H */
drivers/usb/cdns3/gadget.c new file +2218 -0
2,135 lines not on this page · raw message
drivers/usb/cdns3/gadget.h new file +225 -0
225 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.17.1
Part of the diff is not shown here. View the raw message.
PATCH v4 1/4 USB: gadget: Add the cadence USB3 gadget driver
Quoted 8 lines · Sherry Sun
This driver is ported from NXP i.MX U-Boot version imx_v2019.04 and some changes have also been made to adapt to U-Boot.
Add the Cadence USB3 IP(CDNS3) driver for the gadget (device mode). The CDNS3 gadget driver support DM mode.
CONFIG_DM_USB_GADGETshould be enabled when use this driver.Signed-off-by Sherry Sun <[email protected]>
[...]
Quoted 19 lines
drivers/usb/cdns3/gadget.c new file +19
-0+static int cdns_req_ep0_set_configuration (struct usb_ss_dev *usb_ss, +struct usb_ctrlrequest *ctrl_req) +{ +enum usb_device_state device_state = usb_ss->gadget.state; +u32 config =le16_to_cpu (ctrl_req->wValue); +struct usb_ep *ep; +struct usb_ss_endpoint *usb_ss_ep, *temp_ss_ep; +int i, result =0 ; + +switch (device_state) { +case USB_STATE_ADDRESS : +/* Configure non-control EPs */ +list_for_each_entry_safe (usb_ss_ep, temp_ss_ep, + &usb_ss->ep_match_list, + ep_match_pending_list) { +cdns_ep_config (usb_ss_ep); +list_del (&usb_ss_ep->ep_match_pending_list); + } +
drivers/usb/cdns3/gadget.c line 664
Quoted 10 lines
drivers/usb/cdns3/gadget.c new file +10
-0+list_for_each_entry (ep, &usb_ss->gadget.ep_list, ep_list) { + usb_ss_ep =to_usb_ss_ep (ep); +if (usb_ss_ep->used) +cdns_ep_config (usb_ss_ep); + } + +#ifdef CDNS_THREADED_IRQ_HANDLING + usb_ss->ep_ien =cdns_readl (&usb_ss->regs->ep_ien) + |EP_IEN__EOUTEN0__MASK |EP_IEN__EINEN0__MASK ; +#endif
This is probably not needed ?
[...]
Quoted 15 lines
drivers/usb/cdns3/gadget.h new file +15
-0+/* Common TRB fields */ +#define TRB_SET_CYCLE_BIT 1uL +#define TRB_SET_CHAIN_BIT 0x10 + +/* offset 0 */ +#define TRB_DATA_BUFFER_POINTER_MASK 0xFFFFFFFF +#define TRB_SET_DATA_BUFFER_POINTER (p) (p & TRB_DATA_BUFFER_POINTER_MASK) + +/* offset 4 */ +#define TRB_TRANSFER_LENGTH_MASK 0x1FFFF +#define TRB_SET_TRANSFER_LENGTH (l) (l & TRB_TRANSFER_LENGTH_MASK) + +#define TRB_BURST_LENGTH_MASK 0xFF +#define TRB_SET_BURST_LENGTH (l) ((l & TRB_BURST_LENGTH_MASK) << 24) +
drivers/usb/cdns3/gadget.h line 55
Quoted 10 lines
drivers/usb/cdns3/gadget.h new file +10
-0+/* offset 8 */ +#define TRB_SET_INT_ON_SHORT_PACKET 0x04 +#define TRB_SET_FIFO_MODE 0x08 +#define TRB_SET_INT_ON_COMPLETION 0x20 + +#define TRB_TYPE_NORMAL 0x400 + +#define TRB_STREAM_ID_MASK 0xFFFF +#define TRB_SET_STREAM_ID (sid) ((sid & TRB_STREAM_ID_MASK) << 16) +
$sid needs parenthesis, that is (((sid) & TRB_STREAM_ID_MASK) << 16)
there are a few more such issues above, fix them too.
[...]
Quoted 11 lines
drivers/usb/cdns3/gadget.h new file +1
-0+#endif /* __DRIVERS_CDNS3_GADGET */ drivers/usb/cdns3/io.h new file +4
-0@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2016 Cadence Design Systems - https://www.cadence.com/ +* Copyright 2019 NXP
drivers/usb/cdns3/io.h line 14
Quoted 10 lines
drivers/usb/cdns3/io.h new file +10
-0+*/ + +#ifndef __DRIVERS_USB_CDNS_IO_H +#define __DRIVERS_USB_CDNS_IO_H + +#include <linux/io.h> + +static inline u32 cdns_readl (u32 __iomem *reg) +{ +u32 value =0 ;
return readl() ? The value is assigned twice here, for no reason.
drivers/usb/cdns3/io.h line 27
Quoted 12 lines
drivers/usb/cdns3/io.h new file +12
-0+ value =readl (reg); +return value; +} + +static inline void cdns_writel (u32 __iomem *reg,u32 value) +{ +writel (value, reg); +} + +static inline void cdns_flush_cache (uintptr_t addr,int length) +{ +flush_dcache_range (addr, addr +ROUND (length,ARCH_DMA_MINALIGN ));
Drop the ROUND() thing here, just use addr + length. If the start or end address of the cache flush is wrong, the arch code will warn you about it. Here, it will just hide possible bugs.
Quoted 8 lines
drivers/usb/cdns3/io.h new file +3
-0+} + +#endif /* __DRIVERS_USB_CDNS_IO_H */ drivers/usb/gadget/epautoconf.c +0
-0
drivers/usb/gadget/epautoconf.c static int ep_matches( · line 173
Quoted 7 lines
drivers/usb/gadget/epautoconf.c +4
-0@@ -167,6 +167,10 @@ static int ep_matches( size =64 ;put_unaligned (cpu_to_le16 (size), &desc->wMaxPacketSize); } + +if (gadget->ops->match_ep) +return gadget->ops->match_ep (gadget, ep, desc); +
Separate patch for core framework changes please.
PATCH v4 2/4 USB: host: Add the USB3 host driver
In reply to Sherry SunAdd the USB3 host driver for NXP imx8 platform, and the cadence IP is in it. The USB3 host driver support DM mode, it will probe USB3 host node in dts.
--- drivers/usb/host/Kconfig | 9 ++ drivers/usb/host/Makefile | 1 + drivers/usb/host/xhci-imx8.c | 209 +++++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 drivers/usb/host/xhci-imx8.c
drivers/usb/host/Kconfig +9 -0
@@ -95,6 +95,15 @@ config USB_XHCI_FSL depends on !SPL_NO_USB help Enables support for the on-chip xHCI controller on NXP Layerscape SoCs. + +config USB_XHCI_IMX8 + bool "XHCI support for i.MX8" + depends on ARCH_IMX8 + default y + help + Enables support for the on-chip xHCI controller on i.MX8QM and + i.MX8QXP SoCs. + endif # USB_XHCI_HCD config USB_EHCI_HCD
drivers/usb/host/Makefile +1 -0
@@ -55,6 +55,7 @@ obj-$(CONFIG_USB_XHCI_OMAP) += xhci-omap.o obj-$(CONFIG_USB_XHCI_PCI) += xhci-pci.o obj-$(CONFIG_USB_XHCI_RCAR) += xhci-rcar.o obj-$(CONFIG_USB_XHCI_STI) += dwc3-sti-glue.o +obj-$(CONFIG_USB_XHCI_IMX8) += xhci-imx8.o # designware obj-$(CONFIG_USB_DWC2) += dwc2.o
drivers/usb/host/xhci-imx8.c new file +209 -0
209 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 2/4 USB: host: Add the USB3 host driver
Quoted 5 lines · Sherry Sun
Add the USB3 host driver for NXP imx8 platform, and the cadence IP is in it. The USB3 host driver support DM mode, it will probe USB3 host node in dts.
Signed-off-by Sherry Sun <[email protected]>
[...]
drivers/usb/host/xhci-imx8.c line 128
Quoted 7 lines
drivers/usb/host/xhci-imx8.c new file +7
-0+static void xhci_imx8_get_reg_addr (struct udevice *dev) +{ + imx8_data.usb3_ctrl_base = + (void __iomem *)devfdt_get_addr_index (dev,0 ); + imx8_data.usb3_core_base = + (void __iomem *)devfdt_get_addr_index (dev,4 ); +}
Inline this.
Also, look at drivers/mtd/renesas_rpc_hf.c to handle both 32bit and 64bit DTs with multiple "reg" entries.
[...]
drivers/usb/host/xhci-imx8.c line 195
Quoted 2 lines
drivers/usb/host/xhci-imx8.c new file +2
-0+static const struct udevice_id xhci_usb_ids[] = { + { .compatible ="cdns,usb3-host" , },
https://lore.kernel.org/patchwork/patch/1059917/ would suggest that cdns,usb3-1.0.0 is the compatible. But I might be wrong.
drivers/usb/host/xhci-imx8.c line 208
Quoted 13 lines
drivers/usb/host/xhci-imx8.c new file +13
-0+ { } +}; + +U_BOOT_DRIVER (xhci_imx8) = { + .name ="xhci_imx8" , + .id =UCLASS_USB , + .of_match = xhci_usb_ids, + .probe = xhci_imx8_probe, + .remove = xhci_imx8_remove, + .ops = &xhci_usb_ops, + .platdata_auto_alloc_size =sizeof (struct usb_platdata ), + .priv_auto_alloc_size =sizeof (struct xhci_ctrl ), + .flags =DM_FLAG_ALLOC_PRIV_DMA ,
I think you also need DM_FLAG_OS_PREPARE to trigger .remove before booting Linux, but I might be wrong. Please check that.
PATCH v4 3/4 phy: Add USB PHY driver for the cadence USB3
In reply to Sherry SunThe cdns3-usb-phy driver supports both host and peripheral mode of usb driver which use cadence usb3 IP.
--- drivers/phy/Kconfig | 8 ++ drivers/phy/Makefile | 1 + drivers/phy/cdns3-usb-phy.c | 241 ++++++++++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+) create mode 100644 drivers/phy/cdns3-usb-phy.c
drivers/phy/Kconfig +8 -0
@@ -190,4 +190,12 @@ config MT76X8_USB_PHY This PHY is found on MT76x8 devices supporting USB. +config CDNS3_USB_PHY + bool "Support CDNS3 USB PHY" + depends on PHY + help + Support for the USB PHY in CDNS3 IP. + + This PHY is found on CDNS3 IP devices supporting USB. + endmenu
drivers/phy/Makefile +1 -0
@@ -21,3 +21,4 @@ obj-$(CONFIG_MSM8916_USB_PHY) += msm8916-usbh-phy.o obj-$(CONFIG_OMAP_USB2_PHY) += omap-usb2-phy.o obj-$(CONFIG_KEYSTONE_USB_PHY) += keystone-usb-phy.o obj-$(CONFIG_MT76X8_USB_PHY) += mt76x8-usb-phy.o +obj-$(CONFIG_CDNS3_USB_PHY) += cdns3-usb-phy.o
drivers/phy/cdns3-usb-phy.c new file +241 -0
241 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 4/4 USB: gadget: core: introduce ->udc_set_speed() method
In reply to Sherry SunThis patch was copied from kernel commit: 67fdfda4a99ed.
Sometimes, the gadget driver we want to run has max_speed lower than what the UDC supports. In such situations, UDC might want to make sure we don't try to connect on speeds not supported by the gadget driver because that will just fail.
So here introduce a new optional ->udc_set_speed() method which can be implemented by interested UDC drivers to achieve this purpose.
--- drivers/usb/gadget/udc/udc-core.c | 23 +++++++++++++++++++++++ include/linux/usb/gadget.h | 2 ++ 2 files changed, 25 insertions(+)
drivers/usb/gadget/udc/udc-core.c +23 -0
@@ -267,6 +267,27 @@ EXPORT_SYMBOL_GPL(usb_del_gadget_udc);/* ------------------------------------------------------------------------- */ +/** +* usb_gadget_udc_set_speed - tells usb device controller speed supported by +* current driver +* @udc: The device we want to set maximum speed +* @speed: The maximum speed to allowed to run +* +* This call is issued by the UDC Class driver before calling +* usb_gadget_udc_start() in order to make sure that we don't try to +* connect on speeds the gadget driver doesn't support. +*/ +static inline void usb_gadget_udc_set_speed (struct usb_udc *udc, +enum usb_device_speed speed) +{ +if (udc->gadget->ops->udc_set_speed) { +enum usb_device_speed s; + + s =min (speed, udc->gadget->max_speed); + udc->gadget->ops->udc_set_speed (udc->gadget, s); + } +} +static int udc_bind_to_driver (struct usb_udc *udc,struct usb_gadget_driver *driver) {int ret;
@@ -276,6 +297,8 @@ static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *dri udc->driver = driver; +usb_gadget_udc_set_speed (udc, driver->speed); + ret = driver->bind (udc->gadget);if (ret) goto err1;
include/linux/usb/gadget.h +2 -0
@@ -450,6 +450,8 @@ struct usb_gadget_ops {int (*match_ep)(struct usb_gadget *gadget,struct usb_ep *ep,struct usb_endpoint_descriptor *desc); +void (*udc_set_speed)(struct usb_gadget *gadget, +enum usb_device_speed ); };/**
Signature · 1 line
2.17.1