116df8bcbSNeil Armstrong // SPDX-License-Identifier: GPL-2.0
216df8bcbSNeil Armstrong /*
316df8bcbSNeil Armstrong * Meson G12A USB2 PHY driver
416df8bcbSNeil Armstrong *
516df8bcbSNeil Armstrong * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
616df8bcbSNeil Armstrong * Copyright (C) 2017 Amlogic, Inc. All rights reserved
716df8bcbSNeil Armstrong * Copyright (C) 2019 BayLibre, SAS
816df8bcbSNeil Armstrong * Author: Neil Armstrong <narmstrong@baylibre.com>
916df8bcbSNeil Armstrong */
1016df8bcbSNeil Armstrong
1116df8bcbSNeil Armstrong #include <linux/bitfield.h>
1216df8bcbSNeil Armstrong #include <linux/bitops.h>
1316df8bcbSNeil Armstrong #include <linux/clk.h>
1416df8bcbSNeil Armstrong #include <linux/delay.h>
1516df8bcbSNeil Armstrong #include <linux/io.h>
1616df8bcbSNeil Armstrong #include <linux/module.h>
177559e757SRob Herring #include <linux/of.h>
1816df8bcbSNeil Armstrong #include <linux/regmap.h>
1916df8bcbSNeil Armstrong #include <linux/reset.h>
2016df8bcbSNeil Armstrong #include <linux/phy/phy.h>
2116df8bcbSNeil Armstrong #include <linux/platform_device.h>
2216df8bcbSNeil Armstrong
2316df8bcbSNeil Armstrong #define PHY_CTRL_R0 0x0
2416df8bcbSNeil Armstrong #define PHY_CTRL_R1 0x4
2516df8bcbSNeil Armstrong #define PHY_CTRL_R2 0x8
2616df8bcbSNeil Armstrong #define PHY_CTRL_R3 0xc
2716df8bcbSNeil Armstrong #define PHY_CTRL_R3_SQUELCH_REF GENMASK(1, 0)
2816df8bcbSNeil Armstrong #define PHY_CTRL_R3_HSDIC_REF GENMASK(3, 2)
2916df8bcbSNeil Armstrong #define PHY_CTRL_R3_DISC_THRESH GENMASK(7, 4)
3016df8bcbSNeil Armstrong
3116df8bcbSNeil Armstrong #define PHY_CTRL_R4 0x10
3216df8bcbSNeil Armstrong #define PHY_CTRL_R4_CALIB_CODE_7_0 GENMASK(7, 0)
3316df8bcbSNeil Armstrong #define PHY_CTRL_R4_CALIB_CODE_15_8 GENMASK(15, 8)
3416df8bcbSNeil Armstrong #define PHY_CTRL_R4_CALIB_CODE_23_16 GENMASK(23, 16)
3516df8bcbSNeil Armstrong #define PHY_CTRL_R4_I_C2L_CAL_EN BIT(24)
3616df8bcbSNeil Armstrong #define PHY_CTRL_R4_I_C2L_CAL_RESET_N BIT(25)
3716df8bcbSNeil Armstrong #define PHY_CTRL_R4_I_C2L_CAL_DONE BIT(26)
3816df8bcbSNeil Armstrong #define PHY_CTRL_R4_TEST_BYPASS_MODE_EN BIT(27)
3916df8bcbSNeil Armstrong #define PHY_CTRL_R4_I_C2L_BIAS_TRIM_1_0 GENMASK(29, 28)
4016df8bcbSNeil Armstrong #define PHY_CTRL_R4_I_C2L_BIAS_TRIM_3_2 GENMASK(31, 30)
4116df8bcbSNeil Armstrong
4216df8bcbSNeil Armstrong #define PHY_CTRL_R5 0x14
4316df8bcbSNeil Armstrong #define PHY_CTRL_R6 0x18
4416df8bcbSNeil Armstrong #define PHY_CTRL_R7 0x1c
4516df8bcbSNeil Armstrong #define PHY_CTRL_R8 0x20
4616df8bcbSNeil Armstrong #define PHY_CTRL_R9 0x24
4716df8bcbSNeil Armstrong #define PHY_CTRL_R10 0x28
4816df8bcbSNeil Armstrong #define PHY_CTRL_R11 0x2c
4916df8bcbSNeil Armstrong #define PHY_CTRL_R12 0x30
5016df8bcbSNeil Armstrong #define PHY_CTRL_R13 0x34
5116df8bcbSNeil Armstrong #define PHY_CTRL_R13_CUSTOM_PATTERN_19 GENMASK(7, 0)
5216df8bcbSNeil Armstrong #define PHY_CTRL_R13_LOAD_STAT BIT(14)
5316df8bcbSNeil Armstrong #define PHY_CTRL_R13_UPDATE_PMA_SIGNALS BIT(15)
5416df8bcbSNeil Armstrong #define PHY_CTRL_R13_MIN_COUNT_FOR_SYNC_DET GENMASK(20, 16)
5516df8bcbSNeil Armstrong #define PHY_CTRL_R13_CLEAR_HOLD_HS_DISCONNECT BIT(21)
5616df8bcbSNeil Armstrong #define PHY_CTRL_R13_BYPASS_HOST_DISCONNECT_VAL BIT(22)
5716df8bcbSNeil Armstrong #define PHY_CTRL_R13_BYPASS_HOST_DISCONNECT_EN BIT(23)
5816df8bcbSNeil Armstrong #define PHY_CTRL_R13_I_C2L_HS_EN BIT(24)
5916df8bcbSNeil Armstrong #define PHY_CTRL_R13_I_C2L_FS_EN BIT(25)
6016df8bcbSNeil Armstrong #define PHY_CTRL_R13_I_C2L_LS_EN BIT(26)
6116df8bcbSNeil Armstrong #define PHY_CTRL_R13_I_C2L_HS_OE BIT(27)
6216df8bcbSNeil Armstrong #define PHY_CTRL_R13_I_C2L_FS_OE BIT(28)
6316df8bcbSNeil Armstrong #define PHY_CTRL_R13_I_C2L_HS_RX_EN BIT(29)
6416df8bcbSNeil Armstrong #define PHY_CTRL_R13_I_C2L_FSLS_RX_EN BIT(30)
6516df8bcbSNeil Armstrong
6616df8bcbSNeil Armstrong #define PHY_CTRL_R14 0x38
6716df8bcbSNeil Armstrong #define PHY_CTRL_R14_I_RDP_EN BIT(0)
6816df8bcbSNeil Armstrong #define PHY_CTRL_R14_I_RPU_SW1_EN BIT(1)
69014b35d9SJoe Perches #define PHY_CTRL_R14_I_RPU_SW2_EN GENMASK(3, 2)
7016df8bcbSNeil Armstrong #define PHY_CTRL_R14_PG_RSTN BIT(4)
7116df8bcbSNeil Armstrong #define PHY_CTRL_R14_I_C2L_DATA_16_8 BIT(5)
7216df8bcbSNeil Armstrong #define PHY_CTRL_R14_I_C2L_ASSERT_SINGLE_EN_ZERO BIT(6)
7316df8bcbSNeil Armstrong #define PHY_CTRL_R14_BYPASS_CTRL_7_0 GENMASK(15, 8)
7416df8bcbSNeil Armstrong #define PHY_CTRL_R14_BYPASS_CTRL_15_8 GENMASK(23, 16)
7516df8bcbSNeil Armstrong
7616df8bcbSNeil Armstrong #define PHY_CTRL_R15 0x3c
7716df8bcbSNeil Armstrong #define PHY_CTRL_R16 0x40
7816df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_M GENMASK(8, 0)
7916df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_N GENMASK(14, 10)
8016df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_TDC_MODE BIT(20)
8116df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_SDM_EN BIT(21)
8216df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_LOAD BIT(22)
8316df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_DCO_SDM_EN BIT(23)
8416df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_LOCK_LONG GENMASK(25, 24)
8516df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_LOCK_F BIT(26)
8616df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_FAST_LOCK BIT(27)
8716df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_EN BIT(28)
8816df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_RESET BIT(29)
8916df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_LOCK BIT(30)
9016df8bcbSNeil Armstrong #define PHY_CTRL_R16_MPLL_LOCK_DIG BIT(31)
9116df8bcbSNeil Armstrong
9216df8bcbSNeil Armstrong #define PHY_CTRL_R17 0x44
9316df8bcbSNeil Armstrong #define PHY_CTRL_R17_MPLL_FRAC_IN GENMASK(13, 0)
9416df8bcbSNeil Armstrong #define PHY_CTRL_R17_MPLL_FIX_EN BIT(16)
9516df8bcbSNeil Armstrong #define PHY_CTRL_R17_MPLL_LAMBDA1 GENMASK(19, 17)
9616df8bcbSNeil Armstrong #define PHY_CTRL_R17_MPLL_LAMBDA0 GENMASK(22, 20)
9716df8bcbSNeil Armstrong #define PHY_CTRL_R17_MPLL_FILTER_MODE BIT(23)
9816df8bcbSNeil Armstrong #define PHY_CTRL_R17_MPLL_FILTER_PVT2 GENMASK(27, 24)
9916df8bcbSNeil Armstrong #define PHY_CTRL_R17_MPLL_FILTER_PVT1 GENMASK(31, 28)
10016df8bcbSNeil Armstrong
10116df8bcbSNeil Armstrong #define PHY_CTRL_R18 0x48
10216df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_LKW_SEL GENMASK(1, 0)
10316df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_LK_W GENMASK(5, 2)
10416df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_LK_S GENMASK(11, 6)
10516df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_DCO_M_EN BIT(12)
10616df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_DCO_CLK_SEL BIT(13)
10716df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_PFD_GAIN GENMASK(15, 14)
10816df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_ROU GENMASK(18, 16)
10916df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_DATA_SEL GENMASK(21, 19)
11016df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_BIAS_ADJ GENMASK(23, 22)
11116df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_BB_MODE GENMASK(25, 24)
11216df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_ALPHA GENMASK(28, 26)
11316df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_ADJ_LDO GENMASK(30, 29)
11416df8bcbSNeil Armstrong #define PHY_CTRL_R18_MPLL_ACG_RANGE BIT(31)
11516df8bcbSNeil Armstrong
11616df8bcbSNeil Armstrong #define PHY_CTRL_R19 0x4c
11716df8bcbSNeil Armstrong #define PHY_CTRL_R20 0x50
11816df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_IDDET_EN BIT(0)
11916df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_OTG_VBUS_TRIM_2_0 GENMASK(3, 1)
12016df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_OTG_VBUSDET_EN BIT(4)
12116df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_AMON_EN BIT(5)
12216df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_CAL_CODE_R5 BIT(6)
12316df8bcbSNeil Armstrong #define PHY_CTRL_R20_BYPASS_OTG_DET BIT(7)
12416df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_DMON_EN BIT(8)
12516df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_DMON_SEL_3_0 GENMASK(12, 9)
12616df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_EDGE_DRV_EN BIT(13)
12716df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_EDGE_DRV_TRIM_1_0 GENMASK(15, 14)
12816df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_BGR_ADJ_4_0 GENMASK(20, 16)
12916df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_BGR_START BIT(21)
13016df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_BGR_VREF_4_0 GENMASK(28, 24)
13116df8bcbSNeil Armstrong #define PHY_CTRL_R20_USB2_BGR_DBG_1_0 GENMASK(30, 29)
13216df8bcbSNeil Armstrong #define PHY_CTRL_R20_BYPASS_CAL_DONE_R5 BIT(31)
13316df8bcbSNeil Armstrong
13416df8bcbSNeil Armstrong #define PHY_CTRL_R21 0x54
13516df8bcbSNeil Armstrong #define PHY_CTRL_R21_USB2_BGR_FORCE BIT(0)
13616df8bcbSNeil Armstrong #define PHY_CTRL_R21_USB2_CAL_ACK_EN BIT(1)
13716df8bcbSNeil Armstrong #define PHY_CTRL_R21_USB2_OTG_ACA_EN BIT(2)
13816df8bcbSNeil Armstrong #define PHY_CTRL_R21_USB2_TX_STRG_PD BIT(3)
13916df8bcbSNeil Armstrong #define PHY_CTRL_R21_USB2_OTG_ACA_TRIM_1_0 GENMASK(5, 4)
14016df8bcbSNeil Armstrong #define PHY_CTRL_R21_BYPASS_UTMI_CNTR GENMASK(15, 6)
14116df8bcbSNeil Armstrong #define PHY_CTRL_R21_BYPASS_UTMI_REG GENMASK(25, 20)
14216df8bcbSNeil Armstrong
14316df8bcbSNeil Armstrong #define PHY_CTRL_R22 0x58
14416df8bcbSNeil Armstrong #define PHY_CTRL_R23 0x5c
14516df8bcbSNeil Armstrong
14616df8bcbSNeil Armstrong #define RESET_COMPLETE_TIME 1000
14716df8bcbSNeil Armstrong #define PLL_RESET_COMPLETE_TIME 100
14816df8bcbSNeil Armstrong
14923bcbb41SHanjie Lin enum meson_soc_id {
15023bcbb41SHanjie Lin MESON_SOC_G12A = 0,
15123bcbb41SHanjie Lin MESON_SOC_A1,
15223bcbb41SHanjie Lin };
15323bcbb41SHanjie Lin
15416df8bcbSNeil Armstrong struct phy_meson_g12a_usb2_priv {
15516df8bcbSNeil Armstrong struct device *dev;
15616df8bcbSNeil Armstrong struct regmap *regmap;
15716df8bcbSNeil Armstrong struct clk *clk;
15816df8bcbSNeil Armstrong struct reset_control *reset;
15923bcbb41SHanjie Lin int soc_id;
16016df8bcbSNeil Armstrong };
16116df8bcbSNeil Armstrong
16216df8bcbSNeil Armstrong static const struct regmap_config phy_meson_g12a_usb2_regmap_conf = {
16316df8bcbSNeil Armstrong .reg_bits = 8,
16416df8bcbSNeil Armstrong .val_bits = 32,
16516df8bcbSNeil Armstrong .reg_stride = 4,
16616df8bcbSNeil Armstrong .max_register = PHY_CTRL_R23,
16716df8bcbSNeil Armstrong };
16816df8bcbSNeil Armstrong
phy_meson_g12a_usb2_init(struct phy * phy)16916df8bcbSNeil Armstrong static int phy_meson_g12a_usb2_init(struct phy *phy)
17016df8bcbSNeil Armstrong {
17116df8bcbSNeil Armstrong struct phy_meson_g12a_usb2_priv *priv = phy_get_drvdata(phy);
17216df8bcbSNeil Armstrong int ret;
17323bcbb41SHanjie Lin unsigned int value;
17416df8bcbSNeil Armstrong
17558247b9fSDmitry Rokosov ret = clk_prepare_enable(priv->clk);
17616df8bcbSNeil Armstrong if (ret)
17716df8bcbSNeil Armstrong return ret;
17816df8bcbSNeil Armstrong
17958247b9fSDmitry Rokosov ret = reset_control_reset(priv->reset);
18058247b9fSDmitry Rokosov if (ret) {
18158247b9fSDmitry Rokosov clk_disable_unprepare(priv->clk);
18258247b9fSDmitry Rokosov return ret;
18358247b9fSDmitry Rokosov }
18458247b9fSDmitry Rokosov
18516df8bcbSNeil Armstrong udelay(RESET_COMPLETE_TIME);
18616df8bcbSNeil Armstrong
18716df8bcbSNeil Armstrong /* usb2_otg_aca_en == 0 */
18816df8bcbSNeil Armstrong regmap_update_bits(priv->regmap, PHY_CTRL_R21,
18916df8bcbSNeil Armstrong PHY_CTRL_R21_USB2_OTG_ACA_EN, 0);
19016df8bcbSNeil Armstrong
19116df8bcbSNeil Armstrong /* PLL Setup : 24MHz * 20 / 1 = 480MHz */
19216df8bcbSNeil Armstrong regmap_write(priv->regmap, PHY_CTRL_R16,
19316df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R16_MPLL_M, 20) |
19416df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R16_MPLL_N, 1) |
19516df8bcbSNeil Armstrong PHY_CTRL_R16_MPLL_LOAD |
19616df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R16_MPLL_LOCK_LONG, 1) |
19716df8bcbSNeil Armstrong PHY_CTRL_R16_MPLL_FAST_LOCK |
19816df8bcbSNeil Armstrong PHY_CTRL_R16_MPLL_EN |
19916df8bcbSNeil Armstrong PHY_CTRL_R16_MPLL_RESET);
20016df8bcbSNeil Armstrong
20116df8bcbSNeil Armstrong regmap_write(priv->regmap, PHY_CTRL_R17,
20216df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R17_MPLL_FRAC_IN, 0) |
20316df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R17_MPLL_LAMBDA1, 7) |
20416df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R17_MPLL_LAMBDA0, 7) |
20516df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R17_MPLL_FILTER_PVT2, 2) |
20616df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R17_MPLL_FILTER_PVT1, 9));
20716df8bcbSNeil Armstrong
20823bcbb41SHanjie Lin value = FIELD_PREP(PHY_CTRL_R18_MPLL_LKW_SEL, 1) |
20916df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_LK_W, 9) |
21016df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_LK_S, 0x27) |
21116df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_PFD_GAIN, 1) |
21216df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_ROU, 7) |
21316df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_DATA_SEL, 3) |
21416df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_BIAS_ADJ, 1) |
21516df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_BB_MODE, 0) |
21616df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_ALPHA, 3) |
21716df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R18_MPLL_ADJ_LDO, 1) |
21823bcbb41SHanjie Lin PHY_CTRL_R18_MPLL_ACG_RANGE;
21923bcbb41SHanjie Lin
22023bcbb41SHanjie Lin if (priv->soc_id == MESON_SOC_A1)
22123bcbb41SHanjie Lin value |= PHY_CTRL_R18_MPLL_DCO_CLK_SEL;
22223bcbb41SHanjie Lin
22323bcbb41SHanjie Lin regmap_write(priv->regmap, PHY_CTRL_R18, value);
22416df8bcbSNeil Armstrong
22516df8bcbSNeil Armstrong udelay(PLL_RESET_COMPLETE_TIME);
22616df8bcbSNeil Armstrong
22716df8bcbSNeil Armstrong /* UnReset PLL */
22816df8bcbSNeil Armstrong regmap_write(priv->regmap, PHY_CTRL_R16,
22916df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R16_MPLL_M, 20) |
23016df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R16_MPLL_N, 1) |
23116df8bcbSNeil Armstrong PHY_CTRL_R16_MPLL_LOAD |
23216df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R16_MPLL_LOCK_LONG, 1) |
23316df8bcbSNeil Armstrong PHY_CTRL_R16_MPLL_FAST_LOCK |
23416df8bcbSNeil Armstrong PHY_CTRL_R16_MPLL_EN);
23516df8bcbSNeil Armstrong
23616df8bcbSNeil Armstrong /* PHY Tuning */
23716df8bcbSNeil Armstrong regmap_write(priv->regmap, PHY_CTRL_R20,
23816df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R20_USB2_OTG_VBUS_TRIM_2_0, 4) |
23916df8bcbSNeil Armstrong PHY_CTRL_R20_USB2_OTG_VBUSDET_EN |
24016df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R20_USB2_DMON_SEL_3_0, 15) |
24116df8bcbSNeil Armstrong PHY_CTRL_R20_USB2_EDGE_DRV_EN |
24216df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R20_USB2_EDGE_DRV_TRIM_1_0, 3) |
24316df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R20_USB2_BGR_ADJ_4_0, 0) |
24416df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R20_USB2_BGR_VREF_4_0, 0) |
24516df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R20_USB2_BGR_DBG_1_0, 0));
24616df8bcbSNeil Armstrong
24723bcbb41SHanjie Lin if (priv->soc_id == MESON_SOC_G12A)
24816df8bcbSNeil Armstrong regmap_write(priv->regmap, PHY_CTRL_R4,
24916df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_7_0, 0xf) |
25016df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_15_8, 0xf) |
25116df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R4_CALIB_CODE_23_16, 0xf) |
25216df8bcbSNeil Armstrong PHY_CTRL_R4_TEST_BYPASS_MODE_EN |
25316df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R4_I_C2L_BIAS_TRIM_1_0, 0) |
25416df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R4_I_C2L_BIAS_TRIM_3_2, 0));
25523bcbb41SHanjie Lin else if (priv->soc_id == MESON_SOC_A1) {
25623bcbb41SHanjie Lin regmap_write(priv->regmap, PHY_CTRL_R21,
25723bcbb41SHanjie Lin PHY_CTRL_R21_USB2_CAL_ACK_EN |
25823bcbb41SHanjie Lin PHY_CTRL_R21_USB2_TX_STRG_PD |
25923bcbb41SHanjie Lin FIELD_PREP(PHY_CTRL_R21_USB2_OTG_ACA_TRIM_1_0, 2));
26023bcbb41SHanjie Lin
26123bcbb41SHanjie Lin /* Analog Settings */
26223bcbb41SHanjie Lin regmap_write(priv->regmap, PHY_CTRL_R13,
26323bcbb41SHanjie Lin FIELD_PREP(PHY_CTRL_R13_MIN_COUNT_FOR_SYNC_DET, 7));
26423bcbb41SHanjie Lin }
26516df8bcbSNeil Armstrong
26616df8bcbSNeil Armstrong /* Tuning Disconnect Threshold */
26716df8bcbSNeil Armstrong regmap_write(priv->regmap, PHY_CTRL_R3,
26816df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R3_SQUELCH_REF, 0) |
26916df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R3_HSDIC_REF, 1) |
27016df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R3_DISC_THRESH, 3));
27116df8bcbSNeil Armstrong
27223bcbb41SHanjie Lin if (priv->soc_id == MESON_SOC_G12A) {
27316df8bcbSNeil Armstrong /* Analog Settings */
27416df8bcbSNeil Armstrong regmap_write(priv->regmap, PHY_CTRL_R14, 0);
27516df8bcbSNeil Armstrong regmap_write(priv->regmap, PHY_CTRL_R13,
27616df8bcbSNeil Armstrong PHY_CTRL_R13_UPDATE_PMA_SIGNALS |
27716df8bcbSNeil Armstrong FIELD_PREP(PHY_CTRL_R13_MIN_COUNT_FOR_SYNC_DET, 7));
27823bcbb41SHanjie Lin }
27916df8bcbSNeil Armstrong
28016df8bcbSNeil Armstrong return 0;
28116df8bcbSNeil Armstrong }
28216df8bcbSNeil Armstrong
phy_meson_g12a_usb2_exit(struct phy * phy)28316df8bcbSNeil Armstrong static int phy_meson_g12a_usb2_exit(struct phy *phy)
28416df8bcbSNeil Armstrong {
28516df8bcbSNeil Armstrong struct phy_meson_g12a_usb2_priv *priv = phy_get_drvdata(phy);
28658247b9fSDmitry Rokosov int ret;
28716df8bcbSNeil Armstrong
28858247b9fSDmitry Rokosov ret = reset_control_reset(priv->reset);
28958247b9fSDmitry Rokosov if (!ret)
29058247b9fSDmitry Rokosov clk_disable_unprepare(priv->clk);
29158247b9fSDmitry Rokosov
29258247b9fSDmitry Rokosov return ret;
29316df8bcbSNeil Armstrong }
29416df8bcbSNeil Armstrong
29516df8bcbSNeil Armstrong /* set_mode is not needed, mode setting is handled via the UTMI bus */
29616df8bcbSNeil Armstrong static const struct phy_ops phy_meson_g12a_usb2_ops = {
29716df8bcbSNeil Armstrong .init = phy_meson_g12a_usb2_init,
29816df8bcbSNeil Armstrong .exit = phy_meson_g12a_usb2_exit,
29916df8bcbSNeil Armstrong .owner = THIS_MODULE,
30016df8bcbSNeil Armstrong };
30116df8bcbSNeil Armstrong
phy_meson_g12a_usb2_probe(struct platform_device * pdev)30216df8bcbSNeil Armstrong static int phy_meson_g12a_usb2_probe(struct platform_device *pdev)
30316df8bcbSNeil Armstrong {
30416df8bcbSNeil Armstrong struct device *dev = &pdev->dev;
30516df8bcbSNeil Armstrong struct phy_provider *phy_provider;
30616df8bcbSNeil Armstrong struct phy_meson_g12a_usb2_priv *priv;
30716df8bcbSNeil Armstrong struct phy *phy;
30816df8bcbSNeil Armstrong void __iomem *base;
30916df8bcbSNeil Armstrong int ret;
31016df8bcbSNeil Armstrong
31116df8bcbSNeil Armstrong priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
31216df8bcbSNeil Armstrong if (!priv)
31316df8bcbSNeil Armstrong return -ENOMEM;
31416df8bcbSNeil Armstrong
31516df8bcbSNeil Armstrong priv->dev = dev;
31616df8bcbSNeil Armstrong platform_set_drvdata(pdev, priv);
31716df8bcbSNeil Armstrong
318202de025SChunfeng Yun base = devm_platform_ioremap_resource(pdev, 0);
31916df8bcbSNeil Armstrong if (IS_ERR(base))
32016df8bcbSNeil Armstrong return PTR_ERR(base);
32116df8bcbSNeil Armstrong
322*ffc1786cSKrzysztof Kozlowski priv->soc_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
32323bcbb41SHanjie Lin
32416df8bcbSNeil Armstrong priv->regmap = devm_regmap_init_mmio(dev, base,
32516df8bcbSNeil Armstrong &phy_meson_g12a_usb2_regmap_conf);
32616df8bcbSNeil Armstrong if (IS_ERR(priv->regmap))
32716df8bcbSNeil Armstrong return PTR_ERR(priv->regmap);
32816df8bcbSNeil Armstrong
32916df8bcbSNeil Armstrong priv->clk = devm_clk_get(dev, "xtal");
33016df8bcbSNeil Armstrong if (IS_ERR(priv->clk))
33116df8bcbSNeil Armstrong return PTR_ERR(priv->clk);
33216df8bcbSNeil Armstrong
33316df8bcbSNeil Armstrong priv->reset = devm_reset_control_get(dev, "phy");
33416df8bcbSNeil Armstrong if (IS_ERR(priv->reset))
33516df8bcbSNeil Armstrong return PTR_ERR(priv->reset);
33616df8bcbSNeil Armstrong
33716df8bcbSNeil Armstrong ret = reset_control_deassert(priv->reset);
33816df8bcbSNeil Armstrong if (ret)
33916df8bcbSNeil Armstrong return ret;
34016df8bcbSNeil Armstrong
34116df8bcbSNeil Armstrong phy = devm_phy_create(dev, NULL, &phy_meson_g12a_usb2_ops);
34216df8bcbSNeil Armstrong if (IS_ERR(phy)) {
34316df8bcbSNeil Armstrong ret = PTR_ERR(phy);
34416df8bcbSNeil Armstrong if (ret != -EPROBE_DEFER)
34516df8bcbSNeil Armstrong dev_err(dev, "failed to create PHY\n");
34616df8bcbSNeil Armstrong
34716df8bcbSNeil Armstrong return ret;
34816df8bcbSNeil Armstrong }
34916df8bcbSNeil Armstrong
35016df8bcbSNeil Armstrong phy_set_bus_width(phy, 8);
35116df8bcbSNeil Armstrong phy_set_drvdata(phy, priv);
35216df8bcbSNeil Armstrong
35316df8bcbSNeil Armstrong phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
35416df8bcbSNeil Armstrong
35516df8bcbSNeil Armstrong return PTR_ERR_OR_ZERO(phy_provider);
35616df8bcbSNeil Armstrong }
35716df8bcbSNeil Armstrong
35816df8bcbSNeil Armstrong static const struct of_device_id phy_meson_g12a_usb2_of_match[] = {
35923bcbb41SHanjie Lin {
36023bcbb41SHanjie Lin .compatible = "amlogic,g12a-usb2-phy",
36123bcbb41SHanjie Lin .data = (void *)MESON_SOC_G12A,
36223bcbb41SHanjie Lin },
36323bcbb41SHanjie Lin {
36423bcbb41SHanjie Lin .compatible = "amlogic,a1-usb2-phy",
36523bcbb41SHanjie Lin .data = (void *)MESON_SOC_A1,
36623bcbb41SHanjie Lin },
36723bcbb41SHanjie Lin { /* Sentinel */ }
36816df8bcbSNeil Armstrong };
36916df8bcbSNeil Armstrong MODULE_DEVICE_TABLE(of, phy_meson_g12a_usb2_of_match);
37016df8bcbSNeil Armstrong
37116df8bcbSNeil Armstrong static struct platform_driver phy_meson_g12a_usb2_driver = {
37216df8bcbSNeil Armstrong .probe = phy_meson_g12a_usb2_probe,
37316df8bcbSNeil Armstrong .driver = {
37416df8bcbSNeil Armstrong .name = "phy-meson-g12a-usb2",
37516df8bcbSNeil Armstrong .of_match_table = phy_meson_g12a_usb2_of_match,
37616df8bcbSNeil Armstrong },
37716df8bcbSNeil Armstrong };
37816df8bcbSNeil Armstrong module_platform_driver(phy_meson_g12a_usb2_driver);
37916df8bcbSNeil Armstrong
38016df8bcbSNeil Armstrong MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
38116df8bcbSNeil Armstrong MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
38216df8bcbSNeil Armstrong MODULE_DESCRIPTION("Meson G12A USB2 PHY driver");
38316df8bcbSNeil Armstrong MODULE_LICENSE("GPL v2");
384