* [PATCH v6 0/2] Add USB2.0 PHY support for SpacemiT K1
@ 2025-10-17 14:49 Ze Huang
2025-10-17 14:49 ` [PATCH v6 1/2] dt-bindings: phy: spacemit: add K1 USB2 PHY Ze Huang
2025-10-17 14:49 ` [PATCH v6 2/2] phy: spacemit: support K1 USB2.0 PHY controller Ze Huang
0 siblings, 2 replies; 4+ messages in thread
From: Ze Huang @ 2025-10-17 14:49 UTC (permalink / raw)
To: Vinod Koul, Kishon Vijay Abraham I, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, Yixun Lan, Ze Huang
Cc: linux-phy, devicetree, linux-riscv, spacemit, linux-kernel,
Ze Huang
This patch series introduces support for the USB2.0 PHY on the SpacemiT
K1 SoC. The implementation has been tested on the Milk-V Jupiter and
BananaPi-f3.
K1 includes three USB ports as follows[1]:
- A USB2.0 OTG Port
- A USB2.0 Host Only Port
- A USB3.0 Port with a USB2.0 DRD interface
Each of these ports is connected to a USB2.0 PHY responsible for USB2
transmission.
This series is based on 6.18-rc1.
Link: https://developer.spacemit.com/documentation?token=AjHDwrW78igAAEkiHracBI9HnTb#part5 [1]
Signed-off-by: Ze Huang <huangze@whut.edu.cn>
---
Changes in v6:
- phy: spacemit: k1-usb2-phy:
- Fix clock API usage and ensure proper clock disabling in error paths.
- Refactor Register Definitions: Replace address/number-based macros with
feature-based names
- Link to v5: https://lore.kernel.org/all/20250527-b4-k1-usb3-phy-v2-v5-3-2d366b0af999@whut.edu.cn
Changes in v5:
- phy driver for usb2:
- convert readl/writel to regmap
- fix typo: sentinal -> sentinel
- Link to v4: https://lore.kernel.org/r/20250526-b4-k1-usb3-phy-v2-v4-0-eca668fc16a2@whut.edu.cn
Changes in v4:
- combphy driver:
- add in-code comments to indicate that PCIe mode is not yet supported.
- replace custom spacemit_reg_update() with standard regmap API.
- drop spacemit_combphy_wait_ready helper function as only used once.
- Fix PHY init timeout handling: ensure proper error reporting when PLL
lock fails during USB3 PHY initialization
- Link to v3: https://lore.kernel.org/r/20250517-b4-k1-usb3-phy-v2-v3-0-e0655613a163@whut.edu.cn
Changes in v3:
- improve commit message, provide more info about phy hardware
- drop superfluous local variable in `spacemit_combphy_wait_ready`
- replace devm_reset_control_get with devm_reset_control_get_exclusive
- Link to v2: https://lore.kernel.org/r/20250418-b4-k1-usb3-phy-v2-v2-0-b69e02da84eb@whut.edu.cn
Changes in v2:
- combphy dt-bindings:
- fix reg-names
- describe reg
- describe #phy-cells argument
- drop stale ".owner" in driver struct
- add support for usb lfps_thres in combphy
- fix Kconfig depends on
- Link to v1: https://lore.kernel.org/all/20250407-b4-k1-usb3-v3-2-v1-0-bf0bcc41c9ba@whut.edu.cn
Signed-off-by: Ze Huang <huang.ze@linux.dev>
---
Ze Huang (2):
dt-bindings: phy: spacemit: add K1 USB2 PHY
phy: spacemit: support K1 USB2.0 PHY controller
.../devicetree/bindings/phy/spacemit,usb2-phy.yaml | 40 +++++
drivers/phy/Kconfig | 1 +
drivers/phy/Makefile | 1 +
drivers/phy/spacemit/Kconfig | 13 ++
drivers/phy/spacemit/Makefile | 2 +
drivers/phy/spacemit/phy-k1-usb2.c | 200 +++++++++++++++++++++
6 files changed, 257 insertions(+)
---
base-commit: cb6649f6217c0331b885cf787f1d175963e2a1d2
change-id: 20251017-k1-usb2phy-a53ed4ea51b0
Best regards,
--
Ze Huang <huang.ze@linux.dev>
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v6 1/2] dt-bindings: phy: spacemit: add K1 USB2 PHY 2025-10-17 14:49 [PATCH v6 0/2] Add USB2.0 PHY support for SpacemiT K1 Ze Huang @ 2025-10-17 14:49 ` Ze Huang 2025-10-17 14:49 ` [PATCH v6 2/2] phy: spacemit: support K1 USB2.0 PHY controller Ze Huang 1 sibling, 0 replies; 4+ messages in thread From: Ze Huang @ 2025-10-17 14:49 UTC (permalink / raw) To: Vinod Koul, Kishon Vijay Abraham I, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Yixun Lan, Ze Huang Cc: linux-phy, devicetree, linux-riscv, spacemit, linux-kernel, Ze Huang Add support for USB2 PHY found on SpacemiT K1 SoC. Reviewed-by: Rob Herring (Arm) <robh@kernel.org> Signed-off-by: Ze Huang <huang.ze@linux.dev> --- .../devicetree/bindings/phy/spacemit,usb2-phy.yaml | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Documentation/devicetree/bindings/phy/spacemit,usb2-phy.yaml b/Documentation/devicetree/bindings/phy/spacemit,usb2-phy.yaml new file mode 100644 index 000000000000..43eaca90d88c --- /dev/null +++ b/Documentation/devicetree/bindings/phy/spacemit,usb2-phy.yaml @@ -0,0 +1,40 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/phy/spacemit,usb2-phy.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: SpacemiT K1 SoC USB 2.0 PHY + +maintainers: + - Ze Huang <huang.ze@linux.dev> + +properties: + compatible: + const: spacemit,k1-usb2-phy + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + + "#phy-cells": + const: 0 + +required: + - compatible + - reg + - clocks + - "#phy-cells" + +additionalProperties: false + +examples: + - | + usb-phy@c09c0000 { + compatible = "spacemit,k1-usb2-phy"; + reg = <0xc09c0000 0x200>; + clocks = <&syscon_apmu 15>; + #phy-cells = <0>; + }; -- 2.51.1.dirty ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v6 2/2] phy: spacemit: support K1 USB2.0 PHY controller 2025-10-17 14:49 [PATCH v6 0/2] Add USB2.0 PHY support for SpacemiT K1 Ze Huang 2025-10-17 14:49 ` [PATCH v6 1/2] dt-bindings: phy: spacemit: add K1 USB2 PHY Ze Huang @ 2025-10-17 14:49 ` Ze Huang 2025-12-01 14:17 ` Ze Huang 1 sibling, 1 reply; 4+ messages in thread From: Ze Huang @ 2025-10-17 14:49 UTC (permalink / raw) To: Vinod Koul, Kishon Vijay Abraham I, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Yixun Lan, Ze Huang Cc: linux-phy, devicetree, linux-riscv, spacemit, linux-kernel, Ze Huang The SpacemiT K1 SoC includes three USB ports: - One USB2.0 OTG port - One USB2.0 host-only port - One USB3.0 port with an integrated USB2.0 DRD interface Each of these ports is connected to a USB2.0 PHY responsible for USB2 transmission. This commit adds support for the SpacemiT K1 USB2.0 PHY, which is compliant with the USB 2.0 specification and supports both 8-bit 60MHz and 16-bit 30MHz parallel interfaces. Signed-off-by: Ze Huang <huang.ze@linux.dev> --- drivers/phy/Kconfig | 1 + drivers/phy/Makefile | 1 + drivers/phy/spacemit/Kconfig | 13 +++ drivers/phy/spacemit/Makefile | 2 + drivers/phy/spacemit/phy-k1-usb2.c | 200 +++++++++++++++++++++++++++++++++++++ 5 files changed, 217 insertions(+) diff --git a/drivers/phy/Kconfig b/drivers/phy/Kconfig index 1984c2e56122..95ee47f0fbc7 100644 --- a/drivers/phy/Kconfig +++ b/drivers/phy/Kconfig @@ -134,6 +134,7 @@ source "drivers/phy/rockchip/Kconfig" source "drivers/phy/samsung/Kconfig" source "drivers/phy/socionext/Kconfig" source "drivers/phy/sophgo/Kconfig" +source "drivers/phy/spacemit/Kconfig" source "drivers/phy/st/Kconfig" source "drivers/phy/starfive/Kconfig" source "drivers/phy/sunplus/Kconfig" diff --git a/drivers/phy/Makefile b/drivers/phy/Makefile index a206133a3515..950dd4f14372 100644 --- a/drivers/phy/Makefile +++ b/drivers/phy/Makefile @@ -37,6 +37,7 @@ obj-y += allwinner/ \ samsung/ \ socionext/ \ sophgo/ \ + spacemit/ \ st/ \ starfive/ \ sunplus/ \ diff --git a/drivers/phy/spacemit/Kconfig b/drivers/phy/spacemit/Kconfig new file mode 100644 index 000000000000..0136aee2e8a2 --- /dev/null +++ b/drivers/phy/spacemit/Kconfig @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Phy drivers for SpacemiT platforms +# +config PHY_SPACEMIT_K1_USB2 + tristate "SpacemiT K1 USB 2.0 PHY support" + depends on (ARCH_SPACEMIT || COMPILE_TEST) && OF + depends on COMMON_CLK + depends on USB_COMMON + select GENERIC_PHY + help + Enable this to support K1 USB 2.0 PHY driver. This driver takes care of + enabling and clock setup and will be used by K1 udc/ehci/otg/xhci driver. diff --git a/drivers/phy/spacemit/Makefile b/drivers/phy/spacemit/Makefile new file mode 100644 index 000000000000..fec0b425a948 --- /dev/null +++ b/drivers/phy/spacemit/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_PHY_SPACEMIT_K1_USB2) += phy-k1-usb2.o diff --git a/drivers/phy/spacemit/phy-k1-usb2.c b/drivers/phy/spacemit/phy-k1-usb2.c new file mode 100644 index 000000000000..342061380012 --- /dev/null +++ b/drivers/phy/spacemit/phy-k1-usb2.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * SpacemiT K1 USB 2.0 PHY driver + * + * Copyright (C) 2025 SpacemiT (Hangzhou) Technology Co. Ltd + * Copyright (C) 2025 Ze Huang <huang.ze@linux.dev> + */ + +#include <linux/bitfield.h> +#include <linux/clk.h> +#include <linux/iopoll.h> +#include <linux/platform_device.h> +#include <linux/regmap.h> +#include <linux/usb/of.h> + +#define PHY_RST_MODE_CTRL 0x04 +#define PHY_PLL_RDY BIT(0) +#define PHY_CLK_CDR_EN BIT(1) +#define PHY_CLK_PLL_EN BIT(2) +#define PHY_CLK_MAC_EN BIT(3) +#define PHY_MAC_RSTN BIT(5) +#define PHY_CDR_RSTN BIT(6) +#define PHY_PLL_RSTN BIT(7) +/* + * hs line state sel (Bit 13): + * - 1 (Default): Internal HS line state is set to 01 when usb_hs_tx_en is valid. + * - 0: Internal HS line state is always driven by usb_hs_lstate. + * + * fs line state sel (Bit 14): + * - 1 (Default): FS line state is determined by the output data + * (usb_fs_datain/b). + * - 0: FS line state is always determined by the input data (dmo/dpo). + */ +#define PHY_HS_LINE_TX_MODE BIT(13) +#define PHY_FS_LINE_TX_MODE BIT(14) + +#define PHY_INIT_MODE_BITS (PHY_FS_LINE_TX_MODE | PHY_HS_LINE_TX_MODE) +#define PHY_CLK_ENABLE_BITS (PHY_CLK_PLL_EN | PHY_CLK_CDR_EN | \ + PHY_CLK_MAC_EN) +#define PHY_DEASSERT_RST_BITS (PHY_PLL_RSTN | PHY_CDR_RSTN | \ + PHY_MAC_RSTN) + +#define PHY_TX_HOST_CTRL 0x10 +#define PHY_HST_DISC_AUTO_CLR BIT(2) /* autoclear hs host disc when re-connect */ + +#define PHY_HSTXP_HW_CTRL 0x34 +#define PHY_HSTXP_RSTN BIT(2) /* generate reset for clock hstxp */ +#define PHY_CLK_HSTXP_EN BIT(3) /* clock hstxp enable */ +#define PHY_HSTXP_MODE BIT(4) /* 0: force en_txp to be 1; 1: no force */ + +#define PHY_PLL_DIV_CFG 0x98 +#define PHY_FDIV_FRACT_8_15 GENMASK(7, 0) +#define PHY_FDIV_FRACT_16_19 GENMASK(11, 8) +#define PHY_FDIV_FRACT_20_21 BIT(12) /* fdiv_reg<21>, <20>, bit21 == bit20 */ +/* + * freq_sel<1:0> + * if ref clk freq=24.0MHz-->freq_sel<2:0> == 3b'001, then internal divider value == 80 + */ +#define PHY_FDIV_FRACT_0_1 GENMASK(14, 13) +/* + * pll divider value selection + * 1: divider value will choose internal default value ,dependent on freq_sel<1:0> + * 0: divider value will be over ride by fdiv_reg<21:0> + */ +#define PHY_DIV_LOCAL_EN BIT(15) + +#define PHY_SEL_FREQ_24MHZ 0x01 +#define FDIV_REG_MASK (PHY_FDIV_FRACT_20_21 | PHY_FDIV_FRACT_16_19 | \ + PHY_FDIV_FRACT_8_15) +#define FDIV_REG_VAL 0x1ec4 /* 0x100 selects 24MHz, rest are default */ + +#define K1_USB2PHY_RESET_TIME_MS 50 + +struct spacemit_usb2phy { + struct phy *phy; + struct clk *clk; + struct regmap *regmap_base; +}; + +static const struct regmap_config phy_regmap_config = { + .reg_bits = 32, + .val_bits = 32, + .reg_stride = 4, + .max_register = 0x200, +}; + +static int spacemit_usb2phy_init(struct phy *phy) +{ + struct spacemit_usb2phy *sphy = phy_get_drvdata(phy); + struct regmap *map = sphy->regmap_base; + u32 val; + int ret; + + ret = clk_enable(sphy->clk); + if (ret) { + dev_err(&phy->dev, "failed to enable clock\n"); + clk_disable(sphy->clk); + return ret; + } + + /* + * make sure the usb controller is not under reset process before + * any configuration + */ + usleep_range(150, 200); + + /* 24M ref clk */ + val = FIELD_PREP(FDIV_REG_MASK, FDIV_REG_VAL) | + FIELD_PREP(PHY_FDIV_FRACT_0_1, PHY_SEL_FREQ_24MHZ) | + PHY_DIV_LOCAL_EN; + regmap_write(map, PHY_PLL_DIV_CFG, val); + + ret = regmap_read_poll_timeout(map, PHY_RST_MODE_CTRL, val, + (val & PHY_PLL_RDY), + 500, K1_USB2PHY_RESET_TIME_MS * 1000); + if (ret) { + dev_err(&phy->dev, "wait PLLREADY timeout\n"); + clk_disable(sphy->clk); + return ret; + } + + /* release usb2 phy internal reset and enable clock gating */ + val = (PHY_INIT_MODE_BITS | PHY_CLK_ENABLE_BITS | PHY_DEASSERT_RST_BITS); + regmap_write(map, PHY_RST_MODE_CTRL, val); + + val = (PHY_HSTXP_RSTN | PHY_CLK_HSTXP_EN | PHY_HSTXP_MODE); + regmap_write(map, PHY_HSTXP_HW_CTRL, val); + + /* auto clear host disc */ + regmap_update_bits(map, PHY_TX_HOST_CTRL, PHY_HST_DISC_AUTO_CLR, + PHY_HST_DISC_AUTO_CLR); + + return 0; +} + +static int spacemit_usb2phy_exit(struct phy *phy) +{ + struct spacemit_usb2phy *sphy = phy_get_drvdata(phy); + + clk_disable(sphy->clk); + + return 0; +} + +static const struct phy_ops spacemit_usb2phy_ops = { + .init = spacemit_usb2phy_init, + .exit = spacemit_usb2phy_exit, + .owner = THIS_MODULE, +}; + +static int spacemit_usb2phy_probe(struct platform_device *pdev) +{ + struct phy_provider *phy_provider; + struct device *dev = &pdev->dev; + struct spacemit_usb2phy *sphy; + void __iomem *base; + + sphy = devm_kzalloc(dev, sizeof(*sphy), GFP_KERNEL); + if (!sphy) + return -ENOMEM; + + sphy->clk = devm_clk_get_prepared(&pdev->dev, NULL); + if (IS_ERR(sphy->clk)) + return dev_err_probe(dev, PTR_ERR(sphy->clk), "Failed to get clock\n"); + + base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(base)) + return PTR_ERR(base); + + sphy->regmap_base = devm_regmap_init_mmio(dev, base, &phy_regmap_config); + if (IS_ERR(sphy->regmap_base)) + return dev_err_probe(dev, PTR_ERR(sphy->regmap_base), "Failed to init regmap\n"); + + sphy->phy = devm_phy_create(dev, NULL, &spacemit_usb2phy_ops); + if (IS_ERR(sphy->phy)) + return dev_err_probe(dev, PTR_ERR(sphy->phy), "Failed to create phy\n"); + + phy_set_drvdata(sphy->phy, sphy); + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); + + return PTR_ERR_OR_ZERO(phy_provider); +} + +static const struct of_device_id spacemit_usb2phy_dt_match[] = { + { .compatible = "spacemit,k1-usb2-phy", }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, spacemit_usb2phy_dt_match); + +static struct platform_driver spacemit_usb2_phy_driver = { + .probe = spacemit_usb2phy_probe, + .driver = { + .name = "spacemit-usb2-phy", + .of_match_table = spacemit_usb2phy_dt_match, + }, +}; +module_platform_driver(spacemit_usb2_phy_driver); + +MODULE_DESCRIPTION("Spacemit USB 2.0 PHY driver"); +MODULE_LICENSE("GPL"); -- 2.51.1.dirty ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v6 2/2] phy: spacemit: support K1 USB2.0 PHY controller 2025-10-17 14:49 ` [PATCH v6 2/2] phy: spacemit: support K1 USB2.0 PHY controller Ze Huang @ 2025-12-01 14:17 ` Ze Huang 0 siblings, 0 replies; 4+ messages in thread From: Ze Huang @ 2025-12-01 14:17 UTC (permalink / raw) To: Vinod Koul, Kishon Vijay Abraham I, Rob Herring, Krzysztof Kozlowski, Conor Dooley, Yixun Lan, Ze Huang Cc: linux-phy, devicetree, linux-riscv, spacemit, linux-kernel, Ze Huang On Fri, Oct 17, 2025 at 10:49:53PM +0800, Ze Huang wrote: > The SpacemiT K1 SoC includes three USB ports: > > - One USB2.0 OTG port > - One USB2.0 host-only port > - One USB3.0 port with an integrated USB2.0 DRD interface > > Each of these ports is connected to a USB2.0 PHY responsible for USB2 > transmission. > > This commit adds support for the SpacemiT K1 USB2.0 PHY, which is > compliant with the USB 2.0 specification and supports both 8-bit 60MHz > and 16-bit 30MHz parallel interfaces. > Gentle ping This patch still needs review. Any feedback to help move it forward would be appreciated! > Signed-off-by: Ze Huang <huang.ze@linux.dev> > --- > drivers/phy/Kconfig | 1 + > drivers/phy/Makefile | 1 + > drivers/phy/spacemit/Kconfig | 13 +++ > drivers/phy/spacemit/Makefile | 2 + > drivers/phy/spacemit/phy-k1-usb2.c | 200 +++++++++++++++++++++++++++++++++++++ > 5 files changed, 217 insertions(+) > > diff --git a/drivers/phy/Kconfig b/drivers/phy/Kconfig > index 1984c2e56122..95ee47f0fbc7 100644 > --- a/drivers/phy/Kconfig > +++ b/drivers/phy/Kconfig > @@ -134,6 +134,7 @@ source "drivers/phy/rockchip/Kconfig" > source "drivers/phy/samsung/Kconfig" > source "drivers/phy/socionext/Kconfig" > source "drivers/phy/sophgo/Kconfig" > +source "drivers/phy/spacemit/Kconfig" > source "drivers/phy/st/Kconfig" > source "drivers/phy/starfive/Kconfig" > source "drivers/phy/sunplus/Kconfig" > diff --git a/drivers/phy/Makefile b/drivers/phy/Makefile > index a206133a3515..950dd4f14372 100644 > --- a/drivers/phy/Makefile > +++ b/drivers/phy/Makefile > @@ -37,6 +37,7 @@ obj-y += allwinner/ \ > samsung/ \ > socionext/ \ > sophgo/ \ > + spacemit/ \ > st/ \ > starfive/ \ > sunplus/ \ > diff --git a/drivers/phy/spacemit/Kconfig b/drivers/phy/spacemit/Kconfig > new file mode 100644 > index 000000000000..0136aee2e8a2 > --- /dev/null > +++ b/drivers/phy/spacemit/Kconfig > @@ -0,0 +1,13 @@ > +# SPDX-License-Identifier: GPL-2.0-only > +# > +# Phy drivers for SpacemiT platforms > +# > +config PHY_SPACEMIT_K1_USB2 > + tristate "SpacemiT K1 USB 2.0 PHY support" > + depends on (ARCH_SPACEMIT || COMPILE_TEST) && OF > + depends on COMMON_CLK > + depends on USB_COMMON > + select GENERIC_PHY > + help > + Enable this to support K1 USB 2.0 PHY driver. This driver takes care of > + enabling and clock setup and will be used by K1 udc/ehci/otg/xhci driver. > diff --git a/drivers/phy/spacemit/Makefile b/drivers/phy/spacemit/Makefile > new file mode 100644 > index 000000000000..fec0b425a948 > --- /dev/null > +++ b/drivers/phy/spacemit/Makefile > @@ -0,0 +1,2 @@ > +# SPDX-License-Identifier: GPL-2.0-only > +obj-$(CONFIG_PHY_SPACEMIT_K1_USB2) += phy-k1-usb2.o > diff --git a/drivers/phy/spacemit/phy-k1-usb2.c b/drivers/phy/spacemit/phy-k1-usb2.c > new file mode 100644 > index 000000000000..342061380012 > --- /dev/null > +++ b/drivers/phy/spacemit/phy-k1-usb2.c > @@ -0,0 +1,200 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * SpacemiT K1 USB 2.0 PHY driver > + * > + * Copyright (C) 2025 SpacemiT (Hangzhou) Technology Co. Ltd > + * Copyright (C) 2025 Ze Huang <huang.ze@linux.dev> > + */ > + > +#include <linux/bitfield.h> > +#include <linux/clk.h> > +#include <linux/iopoll.h> > +#include <linux/platform_device.h> > +#include <linux/regmap.h> > +#include <linux/usb/of.h> > + > +#define PHY_RST_MODE_CTRL 0x04 > +#define PHY_PLL_RDY BIT(0) > +#define PHY_CLK_CDR_EN BIT(1) > +#define PHY_CLK_PLL_EN BIT(2) > +#define PHY_CLK_MAC_EN BIT(3) > +#define PHY_MAC_RSTN BIT(5) > +#define PHY_CDR_RSTN BIT(6) > +#define PHY_PLL_RSTN BIT(7) > +/* > + * hs line state sel (Bit 13): > + * - 1 (Default): Internal HS line state is set to 01 when usb_hs_tx_en is valid. > + * - 0: Internal HS line state is always driven by usb_hs_lstate. > + * > + * fs line state sel (Bit 14): > + * - 1 (Default): FS line state is determined by the output data > + * (usb_fs_datain/b). > + * - 0: FS line state is always determined by the input data (dmo/dpo). > + */ > +#define PHY_HS_LINE_TX_MODE BIT(13) > +#define PHY_FS_LINE_TX_MODE BIT(14) > + > +#define PHY_INIT_MODE_BITS (PHY_FS_LINE_TX_MODE | PHY_HS_LINE_TX_MODE) > +#define PHY_CLK_ENABLE_BITS (PHY_CLK_PLL_EN | PHY_CLK_CDR_EN | \ > + PHY_CLK_MAC_EN) > +#define PHY_DEASSERT_RST_BITS (PHY_PLL_RSTN | PHY_CDR_RSTN | \ > + PHY_MAC_RSTN) > + > +#define PHY_TX_HOST_CTRL 0x10 > +#define PHY_HST_DISC_AUTO_CLR BIT(2) /* autoclear hs host disc when re-connect */ > + > +#define PHY_HSTXP_HW_CTRL 0x34 > +#define PHY_HSTXP_RSTN BIT(2) /* generate reset for clock hstxp */ > +#define PHY_CLK_HSTXP_EN BIT(3) /* clock hstxp enable */ > +#define PHY_HSTXP_MODE BIT(4) /* 0: force en_txp to be 1; 1: no force */ > + > +#define PHY_PLL_DIV_CFG 0x98 > +#define PHY_FDIV_FRACT_8_15 GENMASK(7, 0) > +#define PHY_FDIV_FRACT_16_19 GENMASK(11, 8) > +#define PHY_FDIV_FRACT_20_21 BIT(12) /* fdiv_reg<21>, <20>, bit21 == bit20 */ > +/* > + * freq_sel<1:0> > + * if ref clk freq=24.0MHz-->freq_sel<2:0> == 3b'001, then internal divider value == 80 > + */ > +#define PHY_FDIV_FRACT_0_1 GENMASK(14, 13) > +/* > + * pll divider value selection > + * 1: divider value will choose internal default value ,dependent on freq_sel<1:0> > + * 0: divider value will be over ride by fdiv_reg<21:0> > + */ > +#define PHY_DIV_LOCAL_EN BIT(15) > + > +#define PHY_SEL_FREQ_24MHZ 0x01 > +#define FDIV_REG_MASK (PHY_FDIV_FRACT_20_21 | PHY_FDIV_FRACT_16_19 | \ > + PHY_FDIV_FRACT_8_15) > +#define FDIV_REG_VAL 0x1ec4 /* 0x100 selects 24MHz, rest are default */ > + > +#define K1_USB2PHY_RESET_TIME_MS 50 > + > +struct spacemit_usb2phy { > + struct phy *phy; > + struct clk *clk; > + struct regmap *regmap_base; > +}; > + > +static const struct regmap_config phy_regmap_config = { > + .reg_bits = 32, > + .val_bits = 32, > + .reg_stride = 4, > + .max_register = 0x200, > +}; > + > +static int spacemit_usb2phy_init(struct phy *phy) > +{ > + struct spacemit_usb2phy *sphy = phy_get_drvdata(phy); > + struct regmap *map = sphy->regmap_base; > + u32 val; > + int ret; > + > + ret = clk_enable(sphy->clk); > + if (ret) { > + dev_err(&phy->dev, "failed to enable clock\n"); > + clk_disable(sphy->clk); > + return ret; > + } > + > + /* > + * make sure the usb controller is not under reset process before > + * any configuration > + */ > + usleep_range(150, 200); > + > + /* 24M ref clk */ > + val = FIELD_PREP(FDIV_REG_MASK, FDIV_REG_VAL) | > + FIELD_PREP(PHY_FDIV_FRACT_0_1, PHY_SEL_FREQ_24MHZ) | > + PHY_DIV_LOCAL_EN; > + regmap_write(map, PHY_PLL_DIV_CFG, val); > + > + ret = regmap_read_poll_timeout(map, PHY_RST_MODE_CTRL, val, > + (val & PHY_PLL_RDY), > + 500, K1_USB2PHY_RESET_TIME_MS * 1000); > + if (ret) { > + dev_err(&phy->dev, "wait PLLREADY timeout\n"); > + clk_disable(sphy->clk); > + return ret; > + } > + > + /* release usb2 phy internal reset and enable clock gating */ > + val = (PHY_INIT_MODE_BITS | PHY_CLK_ENABLE_BITS | PHY_DEASSERT_RST_BITS); > + regmap_write(map, PHY_RST_MODE_CTRL, val); > + > + val = (PHY_HSTXP_RSTN | PHY_CLK_HSTXP_EN | PHY_HSTXP_MODE); > + regmap_write(map, PHY_HSTXP_HW_CTRL, val); > + > + /* auto clear host disc */ > + regmap_update_bits(map, PHY_TX_HOST_CTRL, PHY_HST_DISC_AUTO_CLR, > + PHY_HST_DISC_AUTO_CLR); > + > + return 0; > +} > + > +static int spacemit_usb2phy_exit(struct phy *phy) > +{ > + struct spacemit_usb2phy *sphy = phy_get_drvdata(phy); > + > + clk_disable(sphy->clk); > + > + return 0; > +} > + > +static const struct phy_ops spacemit_usb2phy_ops = { > + .init = spacemit_usb2phy_init, > + .exit = spacemit_usb2phy_exit, > + .owner = THIS_MODULE, > +}; > + > +static int spacemit_usb2phy_probe(struct platform_device *pdev) > +{ > + struct phy_provider *phy_provider; > + struct device *dev = &pdev->dev; > + struct spacemit_usb2phy *sphy; > + void __iomem *base; > + > + sphy = devm_kzalloc(dev, sizeof(*sphy), GFP_KERNEL); > + if (!sphy) > + return -ENOMEM; > + > + sphy->clk = devm_clk_get_prepared(&pdev->dev, NULL); > + if (IS_ERR(sphy->clk)) > + return dev_err_probe(dev, PTR_ERR(sphy->clk), "Failed to get clock\n"); > + > + base = devm_platform_ioremap_resource(pdev, 0); > + if (IS_ERR(base)) > + return PTR_ERR(base); > + > + sphy->regmap_base = devm_regmap_init_mmio(dev, base, &phy_regmap_config); > + if (IS_ERR(sphy->regmap_base)) > + return dev_err_probe(dev, PTR_ERR(sphy->regmap_base), "Failed to init regmap\n"); > + > + sphy->phy = devm_phy_create(dev, NULL, &spacemit_usb2phy_ops); > + if (IS_ERR(sphy->phy)) > + return dev_err_probe(dev, PTR_ERR(sphy->phy), "Failed to create phy\n"); > + > + phy_set_drvdata(sphy->phy, sphy); > + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); > + > + return PTR_ERR_OR_ZERO(phy_provider); > +} > + > +static const struct of_device_id spacemit_usb2phy_dt_match[] = { > + { .compatible = "spacemit,k1-usb2-phy", }, > + { /* sentinel */ } > +}; > +MODULE_DEVICE_TABLE(of, spacemit_usb2phy_dt_match); > + > +static struct platform_driver spacemit_usb2_phy_driver = { > + .probe = spacemit_usb2phy_probe, > + .driver = { > + .name = "spacemit-usb2-phy", > + .of_match_table = spacemit_usb2phy_dt_match, > + }, > +}; > +module_platform_driver(spacemit_usb2_phy_driver); > + > +MODULE_DESCRIPTION("Spacemit USB 2.0 PHY driver"); > +MODULE_LICENSE("GPL"); > > -- > 2.51.1.dirty > ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-12-01 14:18 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-10-17 14:49 [PATCH v6 0/2] Add USB2.0 PHY support for SpacemiT K1 Ze Huang 2025-10-17 14:49 ` [PATCH v6 1/2] dt-bindings: phy: spacemit: add K1 USB2 PHY Ze Huang 2025-10-17 14:49 ` [PATCH v6 2/2] phy: spacemit: support K1 USB2.0 PHY controller Ze Huang 2025-12-01 14:17 ` Ze Huang
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).