Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

phy: spacemit: support K1 USB2.0 PHY controller

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>
Tested-by: Aurelien Jarno <aurelien@aurel32.net>
Tested-by: Junzhong Pan <panjunzhong@linux.spacemit.com>
Link: https://patch.msgid.link/20251017-k1-usb2phy-v6-2-7cf9ea2477a1@linux.dev
Signed-off-by: Vinod Koul <vkoul@kernel.org>

authored by

Ze Huang and committed by
Vinod Koul
fe4bc1a0 61b84d5b

+217
+1
drivers/phy/Kconfig
··· 136 136 source "drivers/phy/samsung/Kconfig" 137 137 source "drivers/phy/socionext/Kconfig" 138 138 source "drivers/phy/sophgo/Kconfig" 139 + source "drivers/phy/spacemit/Kconfig" 139 140 source "drivers/phy/st/Kconfig" 140 141 source "drivers/phy/starfive/Kconfig" 141 142 source "drivers/phy/sunplus/Kconfig"
+1
drivers/phy/Makefile
··· 38 38 samsung/ \ 39 39 socionext/ \ 40 40 sophgo/ \ 41 + spacemit/ \ 41 42 st/ \ 42 43 starfive/ \ 43 44 sunplus/ \
+13
drivers/phy/spacemit/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + # 3 + # Phy drivers for SpacemiT platforms 4 + # 5 + config PHY_SPACEMIT_K1_USB2 6 + tristate "SpacemiT K1 USB 2.0 PHY support" 7 + depends on (ARCH_SPACEMIT || COMPILE_TEST) && OF 8 + depends on COMMON_CLK 9 + depends on USB_COMMON 10 + select GENERIC_PHY 11 + help 12 + Enable this to support K1 USB 2.0 PHY driver. This driver takes care of 13 + enabling and clock setup and will be used by K1 udc/ehci/otg/xhci driver.
+2
drivers/phy/spacemit/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + obj-$(CONFIG_PHY_SPACEMIT_K1_USB2) += phy-k1-usb2.o
+200
drivers/phy/spacemit/phy-k1-usb2.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * SpacemiT K1 USB 2.0 PHY driver 4 + * 5 + * Copyright (C) 2025 SpacemiT (Hangzhou) Technology Co. Ltd 6 + * Copyright (C) 2025 Ze Huang <huang.ze@linux.dev> 7 + */ 8 + 9 + #include <linux/bitfield.h> 10 + #include <linux/clk.h> 11 + #include <linux/iopoll.h> 12 + #include <linux/platform_device.h> 13 + #include <linux/regmap.h> 14 + #include <linux/usb/of.h> 15 + 16 + #define PHY_RST_MODE_CTRL 0x04 17 + #define PHY_PLL_RDY BIT(0) 18 + #define PHY_CLK_CDR_EN BIT(1) 19 + #define PHY_CLK_PLL_EN BIT(2) 20 + #define PHY_CLK_MAC_EN BIT(3) 21 + #define PHY_MAC_RSTN BIT(5) 22 + #define PHY_CDR_RSTN BIT(6) 23 + #define PHY_PLL_RSTN BIT(7) 24 + /* 25 + * hs line state sel (Bit 13): 26 + * - 1 (Default): Internal HS line state is set to 01 when usb_hs_tx_en is valid. 27 + * - 0: Internal HS line state is always driven by usb_hs_lstate. 28 + * 29 + * fs line state sel (Bit 14): 30 + * - 1 (Default): FS line state is determined by the output data 31 + * (usb_fs_datain/b). 32 + * - 0: FS line state is always determined by the input data (dmo/dpo). 33 + */ 34 + #define PHY_HS_LINE_TX_MODE BIT(13) 35 + #define PHY_FS_LINE_TX_MODE BIT(14) 36 + 37 + #define PHY_INIT_MODE_BITS (PHY_FS_LINE_TX_MODE | PHY_HS_LINE_TX_MODE) 38 + #define PHY_CLK_ENABLE_BITS (PHY_CLK_PLL_EN | PHY_CLK_CDR_EN | \ 39 + PHY_CLK_MAC_EN) 40 + #define PHY_DEASSERT_RST_BITS (PHY_PLL_RSTN | PHY_CDR_RSTN | \ 41 + PHY_MAC_RSTN) 42 + 43 + #define PHY_TX_HOST_CTRL 0x10 44 + #define PHY_HST_DISC_AUTO_CLR BIT(2) /* autoclear hs host disc when re-connect */ 45 + 46 + #define PHY_HSTXP_HW_CTRL 0x34 47 + #define PHY_HSTXP_RSTN BIT(2) /* generate reset for clock hstxp */ 48 + #define PHY_CLK_HSTXP_EN BIT(3) /* clock hstxp enable */ 49 + #define PHY_HSTXP_MODE BIT(4) /* 0: force en_txp to be 1; 1: no force */ 50 + 51 + #define PHY_PLL_DIV_CFG 0x98 52 + #define PHY_FDIV_FRACT_8_15 GENMASK(7, 0) 53 + #define PHY_FDIV_FRACT_16_19 GENMASK(11, 8) 54 + #define PHY_FDIV_FRACT_20_21 BIT(12) /* fdiv_reg<21>, <20>, bit21 == bit20 */ 55 + /* 56 + * freq_sel<1:0> 57 + * if ref clk freq=24.0MHz-->freq_sel<2:0> == 3b'001, then internal divider value == 80 58 + */ 59 + #define PHY_FDIV_FRACT_0_1 GENMASK(14, 13) 60 + /* 61 + * pll divider value selection 62 + * 1: divider value will choose internal default value ,dependent on freq_sel<1:0> 63 + * 0: divider value will be over ride by fdiv_reg<21:0> 64 + */ 65 + #define PHY_DIV_LOCAL_EN BIT(15) 66 + 67 + #define PHY_SEL_FREQ_24MHZ 0x01 68 + #define FDIV_REG_MASK (PHY_FDIV_FRACT_20_21 | PHY_FDIV_FRACT_16_19 | \ 69 + PHY_FDIV_FRACT_8_15) 70 + #define FDIV_REG_VAL 0x1ec4 /* 0x100 selects 24MHz, rest are default */ 71 + 72 + #define K1_USB2PHY_RESET_TIME_MS 50 73 + 74 + struct spacemit_usb2phy { 75 + struct phy *phy; 76 + struct clk *clk; 77 + struct regmap *regmap_base; 78 + }; 79 + 80 + static const struct regmap_config phy_regmap_config = { 81 + .reg_bits = 32, 82 + .val_bits = 32, 83 + .reg_stride = 4, 84 + .max_register = 0x200, 85 + }; 86 + 87 + static int spacemit_usb2phy_init(struct phy *phy) 88 + { 89 + struct spacemit_usb2phy *sphy = phy_get_drvdata(phy); 90 + struct regmap *map = sphy->regmap_base; 91 + u32 val; 92 + int ret; 93 + 94 + ret = clk_enable(sphy->clk); 95 + if (ret) { 96 + dev_err(&phy->dev, "failed to enable clock\n"); 97 + clk_disable(sphy->clk); 98 + return ret; 99 + } 100 + 101 + /* 102 + * make sure the usb controller is not under reset process before 103 + * any configuration 104 + */ 105 + usleep_range(150, 200); 106 + 107 + /* 24M ref clk */ 108 + val = FIELD_PREP(FDIV_REG_MASK, FDIV_REG_VAL) | 109 + FIELD_PREP(PHY_FDIV_FRACT_0_1, PHY_SEL_FREQ_24MHZ) | 110 + PHY_DIV_LOCAL_EN; 111 + regmap_write(map, PHY_PLL_DIV_CFG, val); 112 + 113 + ret = regmap_read_poll_timeout(map, PHY_RST_MODE_CTRL, val, 114 + (val & PHY_PLL_RDY), 115 + 500, K1_USB2PHY_RESET_TIME_MS * 1000); 116 + if (ret) { 117 + dev_err(&phy->dev, "wait PLLREADY timeout\n"); 118 + clk_disable(sphy->clk); 119 + return ret; 120 + } 121 + 122 + /* release usb2 phy internal reset and enable clock gating */ 123 + val = (PHY_INIT_MODE_BITS | PHY_CLK_ENABLE_BITS | PHY_DEASSERT_RST_BITS); 124 + regmap_write(map, PHY_RST_MODE_CTRL, val); 125 + 126 + val = (PHY_HSTXP_RSTN | PHY_CLK_HSTXP_EN | PHY_HSTXP_MODE); 127 + regmap_write(map, PHY_HSTXP_HW_CTRL, val); 128 + 129 + /* auto clear host disc */ 130 + regmap_update_bits(map, PHY_TX_HOST_CTRL, PHY_HST_DISC_AUTO_CLR, 131 + PHY_HST_DISC_AUTO_CLR); 132 + 133 + return 0; 134 + } 135 + 136 + static int spacemit_usb2phy_exit(struct phy *phy) 137 + { 138 + struct spacemit_usb2phy *sphy = phy_get_drvdata(phy); 139 + 140 + clk_disable(sphy->clk); 141 + 142 + return 0; 143 + } 144 + 145 + static const struct phy_ops spacemit_usb2phy_ops = { 146 + .init = spacemit_usb2phy_init, 147 + .exit = spacemit_usb2phy_exit, 148 + .owner = THIS_MODULE, 149 + }; 150 + 151 + static int spacemit_usb2phy_probe(struct platform_device *pdev) 152 + { 153 + struct phy_provider *phy_provider; 154 + struct device *dev = &pdev->dev; 155 + struct spacemit_usb2phy *sphy; 156 + void __iomem *base; 157 + 158 + sphy = devm_kzalloc(dev, sizeof(*sphy), GFP_KERNEL); 159 + if (!sphy) 160 + return -ENOMEM; 161 + 162 + sphy->clk = devm_clk_get_prepared(&pdev->dev, NULL); 163 + if (IS_ERR(sphy->clk)) 164 + return dev_err_probe(dev, PTR_ERR(sphy->clk), "Failed to get clock\n"); 165 + 166 + base = devm_platform_ioremap_resource(pdev, 0); 167 + if (IS_ERR(base)) 168 + return PTR_ERR(base); 169 + 170 + sphy->regmap_base = devm_regmap_init_mmio(dev, base, &phy_regmap_config); 171 + if (IS_ERR(sphy->regmap_base)) 172 + return dev_err_probe(dev, PTR_ERR(sphy->regmap_base), "Failed to init regmap\n"); 173 + 174 + sphy->phy = devm_phy_create(dev, NULL, &spacemit_usb2phy_ops); 175 + if (IS_ERR(sphy->phy)) 176 + return dev_err_probe(dev, PTR_ERR(sphy->phy), "Failed to create phy\n"); 177 + 178 + phy_set_drvdata(sphy->phy, sphy); 179 + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 180 + 181 + return PTR_ERR_OR_ZERO(phy_provider); 182 + } 183 + 184 + static const struct of_device_id spacemit_usb2phy_dt_match[] = { 185 + { .compatible = "spacemit,k1-usb2-phy", }, 186 + { /* sentinel */ } 187 + }; 188 + MODULE_DEVICE_TABLE(of, spacemit_usb2phy_dt_match); 189 + 190 + static struct platform_driver spacemit_usb2_phy_driver = { 191 + .probe = spacemit_usb2phy_probe, 192 + .driver = { 193 + .name = "spacemit-usb2-phy", 194 + .of_match_table = spacemit_usb2phy_dt_match, 195 + }, 196 + }; 197 + module_platform_driver(spacemit_usb2_phy_driver); 198 + 199 + MODULE_DESCRIPTION("Spacemit USB 2.0 PHY driver"); 200 + MODULE_LICENSE("GPL");