Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019 Stefan Roese <sr@denx.de> |
| 4 | * |
| 5 | * Derived from linux/drivers/phy/ralink/phy-ralink-usb.c |
| 6 | * Copyright (C) 2017 John Crispin <john@phrozen.org> |
| 7 | */ |
| 8 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 9 | #include <clk.h> |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <generic-phy.h> |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 13 | #include <reset.h> |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 16 | |
| 17 | #define OFS_U2_PHY_AC0 0x800 |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 18 | #define USBPLL_FBDIV_S 16 |
| 19 | #define USBPLL_FBDIV_M GENMASK(22, 16) |
| 20 | #define BG_TRIM_S 8 |
| 21 | #define BG_TRIM_M GENMASK(11, 8) |
| 22 | #define BG_RBSEL_S 6 |
| 23 | #define BG_RBSEL_M GENMASK(7, 6) |
| 24 | #define BG_RASEL_S 4 |
| 25 | #define BG_RASEL_M GENMASK(5, 4) |
| 26 | #define BGR_DIV_S 2 |
| 27 | #define BGR_DIV_M GENMASK(3, 2) |
| 28 | #define CHP_EN BIT(1) |
| 29 | |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 30 | #define OFS_U2_PHY_AC1 0x804 |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 31 | #define VRT_VREF_SEL_S 28 |
| 32 | #define VRT_VREF_SEL_M GENMASK(30, 28) |
| 33 | #define TERM_VREF_SEL_S 24 |
| 34 | #define TERM_VREF_SEL_M GENMASK(26, 24) |
| 35 | #define USBPLL_RSVD BIT(4) |
| 36 | #define USBPLL_ACCEN BIT(3) |
| 37 | #define USBPLL_LF BIT(2) |
| 38 | |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 39 | #define OFS_U2_PHY_AC2 0x808 |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 40 | |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 41 | #define OFS_U2_PHY_ACR0 0x810 |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 42 | #define HSTX_SRCAL_EN BIT(23) |
| 43 | #define HSTX_SRCTRL_S 16 |
| 44 | #define HSTX_SRCTRL_M GENMASK(18, 16) |
| 45 | |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 46 | #define OFS_U2_PHY_ACR3 0x81C |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 47 | #define HSTX_DBIST_S 28 |
| 48 | #define HSTX_DBIST_M GENMASK(31, 28) |
| 49 | #define HSRX_BIAS_EN_SEL_S 20 |
| 50 | #define HSRX_BIAS_EN_SEL_M GENMASK(21, 20) |
| 51 | |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 52 | #define OFS_U2_PHY_DCR0 0x860 |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 53 | #define PHYD_RESERVE_S 8 |
| 54 | #define PHYD_RESERVE_M GENMASK(23, 8) |
| 55 | #define CDR_FILT_S 0 |
| 56 | #define CDR_FILT_M GENMASK(3, 0) |
| 57 | |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 58 | #define OFS_U2_PHY_DTM0 0x868 |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 59 | #define FORCE_USB_CLKEN BIT(25) |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 60 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 61 | #define OFS_FM_CR0 0xf00 |
| 62 | #define FREQDET_EN BIT(24) |
| 63 | #define CYCLECNT_S 0 |
| 64 | #define CYCLECNT_M GENMASK(23, 0) |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 65 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 66 | #define OFS_FM_MONR0 0xf0c |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 67 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 68 | #define OFS_FM_MONR1 0xf10 |
| 69 | #define FRCK_EN BIT(8) |
| 70 | |
| 71 | #define U2_SR_COEF_7628 32 |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 72 | |
| 73 | struct mt76x8_usb_phy { |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 74 | void __iomem *base; |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 75 | struct clk cg; /* for clock gating */ |
| 76 | struct reset_ctl rst_phy; |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 79 | static void phy_w32(struct mt76x8_usb_phy *phy, u32 reg, u32 val) |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 80 | { |
| 81 | writel(val, phy->base + reg); |
| 82 | } |
| 83 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 84 | static u32 phy_r32(struct mt76x8_usb_phy *phy, u32 reg) |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 85 | { |
| 86 | return readl(phy->base + reg); |
| 87 | } |
| 88 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 89 | static void phy_rmw32(struct mt76x8_usb_phy *phy, u32 reg, u32 clr, u32 set) |
| 90 | { |
| 91 | clrsetbits_32(phy->base + reg, clr, set); |
| 92 | } |
| 93 | |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 94 | static void mt76x8_usb_phy_init(struct mt76x8_usb_phy *phy) |
| 95 | { |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 96 | phy_r32(phy, OFS_U2_PHY_AC2); |
| 97 | phy_r32(phy, OFS_U2_PHY_ACR0); |
| 98 | phy_r32(phy, OFS_U2_PHY_DCR0); |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 99 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 100 | phy_w32(phy, OFS_U2_PHY_DCR0, |
| 101 | (0xffff << PHYD_RESERVE_S) | (2 << CDR_FILT_S)); |
| 102 | phy_r32(phy, OFS_U2_PHY_DCR0); |
| 103 | |
| 104 | phy_w32(phy, OFS_U2_PHY_DCR0, |
| 105 | (0x5555 << PHYD_RESERVE_S) | (2 << CDR_FILT_S)); |
| 106 | phy_r32(phy, OFS_U2_PHY_DCR0); |
| 107 | |
| 108 | phy_w32(phy, OFS_U2_PHY_DCR0, |
| 109 | (0xaaaa << PHYD_RESERVE_S) | (2 << CDR_FILT_S)); |
| 110 | phy_r32(phy, OFS_U2_PHY_DCR0); |
| 111 | |
| 112 | phy_w32(phy, OFS_U2_PHY_DCR0, |
| 113 | (4 << PHYD_RESERVE_S) | (2 << CDR_FILT_S)); |
| 114 | phy_r32(phy, OFS_U2_PHY_DCR0); |
| 115 | |
| 116 | phy_w32(phy, OFS_U2_PHY_AC0, |
| 117 | (0x48 << USBPLL_FBDIV_S) | (8 << BG_TRIM_S) | |
| 118 | (1 << BG_RBSEL_S) | (2 << BG_RASEL_S) | (2 << BGR_DIV_S) | |
| 119 | CHP_EN); |
| 120 | |
| 121 | phy_w32(phy, OFS_U2_PHY_AC1, |
| 122 | (4 << VRT_VREF_SEL_S) | (4 << TERM_VREF_SEL_S) | USBPLL_RSVD | |
| 123 | USBPLL_ACCEN | USBPLL_LF); |
| 124 | |
| 125 | phy_w32(phy, OFS_U2_PHY_ACR3, |
| 126 | (12 << HSTX_DBIST_S) | (2 << HSRX_BIAS_EN_SEL_S)); |
| 127 | |
| 128 | phy_w32(phy, OFS_U2_PHY_DTM0, FORCE_USB_CLKEN); |
| 129 | } |
| 130 | |
| 131 | static void mt76x8_usb_phy_sr_calibrate(struct mt76x8_usb_phy *phy) |
| 132 | { |
| 133 | u32 fmout, tmp = 4; |
| 134 | int i; |
| 135 | |
| 136 | /* Enable HS TX SR calibration */ |
| 137 | phy_rmw32(phy, OFS_U2_PHY_ACR0, 0, HSTX_SRCAL_EN); |
| 138 | mdelay(1); |
| 139 | |
| 140 | /* Enable free run clock */ |
| 141 | phy_rmw32(phy, OFS_FM_MONR1, 0, FRCK_EN); |
| 142 | |
| 143 | /* Set cycle count = 0x400 */ |
| 144 | phy_rmw32(phy, OFS_FM_CR0, CYCLECNT_M, 0x400 << CYCLECNT_S); |
| 145 | |
| 146 | /* Enable frequency meter */ |
| 147 | phy_rmw32(phy, OFS_FM_CR0, 0, FREQDET_EN); |
| 148 | |
| 149 | /* Wait for FM detection done, set timeout to 10ms */ |
| 150 | for (i = 0; i < 10; i++) { |
| 151 | fmout = phy_r32(phy, OFS_FM_MONR0); |
| 152 | |
| 153 | if (fmout) |
| 154 | break; |
| 155 | |
| 156 | mdelay(1); |
| 157 | } |
| 158 | |
| 159 | /* Disable frequency meter */ |
| 160 | phy_rmw32(phy, OFS_FM_CR0, FREQDET_EN, 0); |
| 161 | |
| 162 | /* Disable free run clock */ |
| 163 | phy_rmw32(phy, OFS_FM_MONR1, FRCK_EN, 0); |
| 164 | |
| 165 | /* Disable HS TX SR calibration */ |
| 166 | phy_rmw32(phy, OFS_U2_PHY_ACR0, HSTX_SRCAL_EN, 0); |
| 167 | mdelay(1); |
| 168 | |
| 169 | if (fmout) { |
| 170 | /* |
| 171 | * set reg = (1024 / FM_OUT) * 25 * 0.028 |
| 172 | * (round to the nearest digits) |
| 173 | */ |
| 174 | tmp = (((1024 * 25 * U2_SR_COEF_7628) / fmout) + 500) / 1000; |
| 175 | } |
| 176 | |
| 177 | phy_rmw32(phy, OFS_U2_PHY_ACR0, HSTX_SRCTRL_M, |
| 178 | (tmp << HSTX_SRCTRL_S) & HSTX_SRCTRL_M); |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int mt76x8_usb_phy_power_on(struct phy *_phy) |
| 182 | { |
| 183 | struct mt76x8_usb_phy *phy = dev_get_priv(_phy->dev); |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 184 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 185 | clk_enable(&phy->cg); |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 186 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 187 | reset_deassert(&phy->rst_phy); |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * The SDK kernel had a delay of 100ms. however on device |
| 191 | * testing showed that 10ms is enough |
| 192 | */ |
| 193 | mdelay(10); |
| 194 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 195 | mt76x8_usb_phy_init(phy); |
| 196 | mt76x8_usb_phy_sr_calibrate(phy); |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int mt76x8_usb_phy_power_off(struct phy *_phy) |
| 202 | { |
| 203 | struct mt76x8_usb_phy *phy = dev_get_priv(_phy->dev); |
| 204 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 205 | clk_disable(&phy->cg); |
| 206 | |
| 207 | reset_assert(&phy->rst_phy); |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int mt76x8_usb_phy_probe(struct udevice *dev) |
| 213 | { |
| 214 | struct mt76x8_usb_phy *phy = dev_get_priv(dev); |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 215 | int ret; |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 216 | |
| 217 | phy->base = dev_read_addr_ptr(dev); |
| 218 | if (!phy->base) |
| 219 | return -EINVAL; |
| 220 | |
Weijie Gao | 4cce511 | 2019-09-25 17:45:31 +0800 | [diff] [blame] | 221 | /* clock gate */ |
| 222 | ret = clk_get_by_name(dev, "cg", &phy->cg); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | |
| 226 | ret = reset_get_by_name(dev, "phy", &phy->rst_phy); |
| 227 | if (ret) |
| 228 | return ret; |
| 229 | |
Stefan Roese | d7d7606 | 2019-04-05 13:44:43 +0200 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static struct phy_ops mt76x8_usb_phy_ops = { |
| 234 | .power_on = mt76x8_usb_phy_power_on, |
| 235 | .power_off = mt76x8_usb_phy_power_off, |
| 236 | }; |
| 237 | |
| 238 | static const struct udevice_id mt76x8_usb_phy_ids[] = { |
| 239 | { .compatible = "mediatek,mt7628-usbphy" }, |
| 240 | { } |
| 241 | }; |
| 242 | |
| 243 | U_BOOT_DRIVER(mt76x8_usb_phy) = { |
| 244 | .name = "mt76x8_usb_phy", |
| 245 | .id = UCLASS_PHY, |
| 246 | .of_match = mt76x8_usb_phy_ids, |
| 247 | .ops = &mt76x8_usb_phy_ops, |
| 248 | .probe = mt76x8_usb_phy_probe, |
| 249 | .priv_auto_alloc_size = sizeof(struct mt76x8_usb_phy), |
| 250 | }; |