1*90313c06Smsaitoh /* $NetBSD: mesong12_usb2phy.c,v 1.3 2024/02/07 04:20:26 msaitoh Exp $ */
28afae5d5Sryo
38afae5d5Sryo /*
4*90313c06Smsaitoh * Copyright (c) 2021 Ryo Shimizu
58afae5d5Sryo * All rights reserved.
68afae5d5Sryo *
78afae5d5Sryo * Redistribution and use in source and binary forms, with or without
88afae5d5Sryo * modification, are permitted provided that the following conditions
98afae5d5Sryo * are met:
108afae5d5Sryo * 1. Redistributions of source code must retain the above copyright
118afae5d5Sryo * notice, this list of conditions and the following disclaimer.
128afae5d5Sryo * 2. Redistributions in binary form must reproduce the above copyright
138afae5d5Sryo * notice, this list of conditions and the following disclaimer in the
148afae5d5Sryo * documentation and/or other materials provided with the distribution.
158afae5d5Sryo *
168afae5d5Sryo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
178afae5d5Sryo * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
188afae5d5Sryo * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198afae5d5Sryo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
208afae5d5Sryo * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
218afae5d5Sryo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
228afae5d5Sryo * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238afae5d5Sryo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
248afae5d5Sryo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
258afae5d5Sryo * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
268afae5d5Sryo * POSSIBILITY OF SUCH DAMAGE.
278afae5d5Sryo */
288afae5d5Sryo
298afae5d5Sryo #include <sys/cdefs.h>
30*90313c06Smsaitoh __KERNEL_RCSID(0, "$NetBSD: mesong12_usb2phy.c,v 1.3 2024/02/07 04:20:26 msaitoh Exp $");
318afae5d5Sryo
328afae5d5Sryo #include <sys/param.h>
338afae5d5Sryo #include <sys/types.h>
348afae5d5Sryo #include <sys/bus.h>
358afae5d5Sryo #include <sys/device.h>
368afae5d5Sryo
378afae5d5Sryo #include <dev/fdt/fdtvar.h>
388afae5d5Sryo
398afae5d5Sryo /*
408afae5d5Sryo * USB PHY 20: 0xff636000
418afae5d5Sryo * USB PHY 21: 0xff63a000
428afae5d5Sryo */
438afae5d5Sryo #define USB2PHY_R00_REG 0x00
448afae5d5Sryo #define USB2PHY_R01_REG 0x04
458afae5d5Sryo #define USB2PHY_R02_REG 0x08
468afae5d5Sryo #define USB2PHY_R03_REG 0x0c
478afae5d5Sryo #define USB2PHY_R03_DISC_THRESH __BITS(7,4)
488afae5d5Sryo #define USB2PHY_R03_HSDIC_REF __BITS(3,2)
498afae5d5Sryo #define USB2PHY_R03_SQUELCH_REF __BITS(1,0)
508afae5d5Sryo #define USB2PHY_R04_REG 0x10
518afae5d5Sryo #define USB2PHY_R04_I_C2L_BIAS_TRIM __BITS(31,28)
528afae5d5Sryo #define USB2PHY_R04_TEST_BYPASS_MODE_EN __BIT(27)
538afae5d5Sryo #define USB2PHY_R04_I_C2L_CAL_DONE __BIT(26)
548afae5d5Sryo #define USB2PHY_R04_I_C2L_CAL_RESET_N __BIT(25)
558afae5d5Sryo #define USB2PHY_R04_I_C2L_CAL_EN __BIT(24)
568afae5d5Sryo #define USB2PHY_R04_CALIBRATION_CODE_VALUE __BITS(23,0)
578afae5d5Sryo #define USB2PHY_R05_REG 0x14
588afae5d5Sryo #define USB2PHY_R06_REG 0x18
598afae5d5Sryo #define USB2PHY_R07_REG 0x1c
608afae5d5Sryo #define USB2PHY_R08_REG 0x20
618afae5d5Sryo #define USB2PHY_R09_REG 0x24
628afae5d5Sryo #define USB2PHY_R10_REG 0x28
638afae5d5Sryo #define USB2PHY_R11_REG 0x2c
648afae5d5Sryo #define USB2PHY_R12_REG 0x30
658afae5d5Sryo #define USB2PHY_R13_REG 0x34
668afae5d5Sryo #define USB2PHY_R13_I_C2L_FSLS_RX_EN __BIT(30)
678afae5d5Sryo #define USB2PHY_R13_I_C2L_HS_RX_EN __BIT(29)
688afae5d5Sryo #define USB2PHY_R13_I_C2L_FS_OE __BIT(28)
698afae5d5Sryo #define USB2PHY_R13_I_C2L_HS_OE __BIT(27)
708afae5d5Sryo #define USB2PHY_R13_I_C2L_LS_EN __BIT(26)
718afae5d5Sryo #define USB2PHY_R13_I_C2L_FS_EN __BIT(25)
728afae5d5Sryo #define USB2PHY_R13_I_C2L_HS_EN __BIT(24)
738afae5d5Sryo #define USB2PHY_R13_BYPASS_HOST_DISCONNECT_ENABLE __BIT(23)
748afae5d5Sryo #define USB2PHY_R13_BYPASS_HOST_DISCONNECT_VALUE __BIT(22)
758afae5d5Sryo #define USB2PHY_R13_CLEAR_HOLD_HS_DISCONNECT __BIT(21)
768afae5d5Sryo #define USB2PHY_R13_MINIMUM_COUNT_FOR_SYNC_DETECTION __BITS(20,16)
778afae5d5Sryo #define USB2PHY_R13_UPDATE_PMA_SIGNALS __BIT(15)
788afae5d5Sryo #define USB2PHY_R13_LOAD_STAT __BIT(14)
798afae5d5Sryo #define USB2PHY_R13_CUSTOM_PATTERN_19 __BITS(7,0)
808afae5d5Sryo #define USB2PHY_R14_REG 0x38
818afae5d5Sryo #define USB2PHY_R14_BYPASS_CTRL __BITS(23,8)
828afae5d5Sryo #define USB2PHY_R14_I_C2L_ASSERT_SINGLE_ENABLE_ZERO __BIT(6)
838afae5d5Sryo #define USB2PHY_R14_I_C2L_DATA_16_8 __BIT(5)
848afae5d5Sryo #define USB2PHY_R14_PG_RSTN __BIT(4)
858afae5d5Sryo #define USB2PHY_R14_I_RPU_SW2_EN __BITS(3,2)
868afae5d5Sryo #define USB2PHY_R14_I_RPU_SW1_EN __BIT(1)
878afae5d5Sryo #define USB2PHY_R14_I_RDP_EN __BIT(0)
888afae5d5Sryo #define USB2PHY_R15_REG 0x3c
898afae5d5Sryo #define USB2PHY_R16_REG 0x40
908afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_LOCK_DIG __BIT(31)
918afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_LOCK __BIT(30)
928afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_RESET __BIT(29)
938afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_EN __BIT(28)
948afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_FAST_LOCK __BIT(27)
958afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_LOCK_F __BIT(26)
968afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_LOCK_LONG __BITS(25,24)
978afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_DCO_SDM_EN __BIT(23)
988afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_LOAD __BIT(22)
998afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_SDM_EN __BIT(21)
1008afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_TDC_MODE __BIT(20)
1018afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_N __BITS(14,10)
1028afae5d5Sryo #define USB2PHY_R16_USB2_MPLL_M __BITS(8,0)
1038afae5d5Sryo #define USB2PHY_R17_REG 0x44
1048afae5d5Sryo #define USB2PHY_R17_USB2_MPLL_FILTER_PVT1 __BITS(31,28)
1058afae5d5Sryo #define USB2PHY_R17_USB2_MPLL_FILTER_PVT2 __BITS(27,24)
1068afae5d5Sryo #define USB2PHY_R17_USB2_MPLL_FILTER_MODE __BIT(23)
1078afae5d5Sryo #define USB2PHY_R17_USB2_MPLL_LAMBDA0 __BITS(22,20)
1088afae5d5Sryo #define USB2PHY_R17_USB2_MPLL_LAMBDA1 __BITS(19,17)
1098afae5d5Sryo #define USB2PHY_R17_USB2_MPLL_FIX_EN __BIT(16)
1108afae5d5Sryo #define USB2PHY_R17_USB2_MPLL_FRAC_IN __BITS(13,0)
1118afae5d5Sryo #define USB2PHY_R18_REG 0x48
1128afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_ACG_RANGE __BIT(31)
1138afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_ADJ_LDO __BITS(30,29)
1148afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_ALPHA __BITS(28,26)
1158afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_BB_MODE __BITS(25,24)
1168afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_BIAS_ADJ __BITS(23,22)
1178afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_DATA_SEL __BITS(21,19)
1188afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_ROU __BITS(18,16)
1198afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_PFD_GAIN __BITS(15,14)
1208afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_DCO_CLK_SEL __BIT(13)
1218afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_DCO_M_EN __BIT(12)
1228afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_LK_S __BITS(11,6)
1238afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_LK_W __BITS(5,2)
1248afae5d5Sryo #define USB2PHY_R18_USB2_MPLL_LKW_SEL __BITS(1,0)
1258afae5d5Sryo #define USB2PHY_R19_REG 0x4c
1268afae5d5Sryo #define USB2PHY_R20_REG 0x50
1278afae5d5Sryo #define USB2PHY_R20_BYPASS_CAL_DONE_R5 __BIT(31)
1288afae5d5Sryo #define USB2PHY_R20_USB2_BGR_DBG_1_0 __BITS(30,29)
1298afae5d5Sryo #define USB2PHY_R20_USB2_BGR_VREF_4_0 __BITS(28,24)
1308afae5d5Sryo #define USB2PHY_R20_USB2_BGR_START __BIT(21)
1318afae5d5Sryo #define USB2PHY_R20_USB2_BGR_ADJ_4_0 __BITS(20,16)
1328afae5d5Sryo #define USB2PHY_R20_USB2_EDGE_DRV_TRIM_1_0 __BITS(15,14)
1338afae5d5Sryo #define USB2PHY_R20_USB2_EDGE_DRV_EN __BIT(13)
1348afae5d5Sryo #define USB2PHY_R20_USB2_DMON_SEL_3_0 __BITS(12,9)
1358afae5d5Sryo #define USB2PHY_R20_USB2_DMON_EN __BIT(8)
1368afae5d5Sryo #define USB2PHY_R20_BYPASS_OTG_DET __BIT(7)
1378afae5d5Sryo #define USB2PHY_R20_USB2_CAL_CODE_R5 __BIT(6)
1388afae5d5Sryo #define USB2PHY_R20_USB2_AMON_EN __BIT(5)
1398afae5d5Sryo #define USB2PHY_R20_USB2_OTG_VBUSDET_EN __BIT(4)
1408afae5d5Sryo #define USB2PHY_R20_USB2_OTG_VBUS_TRIM_2_0 __BITS(3,1)
1418afae5d5Sryo #define USB2PHY_R20_USB2_IDDET_EN __BIT(0)
1428afae5d5Sryo #define USB2PHY_R21_REG 0x54
1438afae5d5Sryo #define USB2PHY_R21_BYPASS_UTMI_REG __BITS(25,20)
1448afae5d5Sryo #define USB2PHY_R21_BYPASS_UTMI_CNTR __BITS(15,6)
1458afae5d5Sryo #define USB2PHY_R21_USB2_OTG_ACA_TRIM_1_0 __BITS(5,4)
1468afae5d5Sryo #define USB2PHY_R21_USB2_TX_STRG_PD __BIT(3)
1478afae5d5Sryo #define USB2PHY_R21_USB2_OTG_ACA_EN __BIT(2)
1488afae5d5Sryo #define USB2PHY_R21_USB2_CAL_ACK_EN __BIT(1)
1498afae5d5Sryo #define USB2PHY_R21_USB2_BGR_FORCE __BIT(0)
1508afae5d5Sryo #define USB2PHY_R22_REG 0x58
1518afae5d5Sryo #define USB2PHY_R23_REG 0x5c
1528afae5d5Sryo
1538afae5d5Sryo #define USB2PHY_READ_REG(sc, reg) \
1548afae5d5Sryo bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
1558afae5d5Sryo #define USB2PHY_WRITE_REG(sc, reg, val) \
1568afae5d5Sryo bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
1578afae5d5Sryo
1588afae5d5Sryo struct mesong12_usb2phy_softc {
1598afae5d5Sryo device_t sc_dev;
1608afae5d5Sryo bus_space_tag_t sc_bst;
1618afae5d5Sryo bus_space_handle_t sc_bsh;
1628afae5d5Sryo struct clk *sc_clk;
1638afae5d5Sryo struct fdtbus_reset *sc_reset;
1648afae5d5Sryo struct fdtbus_regulator *sc_supply;
1658afae5d5Sryo int sc_phandle;
1668afae5d5Sryo };
1678afae5d5Sryo
1688afae5d5Sryo static void *
mesong12_usb2phy_acquire(device_t dev,const void * data,size_t len)1698afae5d5Sryo mesong12_usb2phy_acquire(device_t dev, const void *data, size_t len)
1708afae5d5Sryo {
1718afae5d5Sryo if (len != 0)
1728afae5d5Sryo return NULL;
1738afae5d5Sryo
1748afae5d5Sryo return (void *)(uintptr_t)1;
1758afae5d5Sryo }
1768afae5d5Sryo
1778afae5d5Sryo static void
mesong12_usb2phy_release(device_t dev,void * priv)1788afae5d5Sryo mesong12_usb2phy_release(device_t dev, void *priv)
1798afae5d5Sryo {
1808afae5d5Sryo __nothing;
1818afae5d5Sryo }
1828afae5d5Sryo
1838afae5d5Sryo static int
mesong12_usb2phy_enable(device_t dev,void * priv,bool enable)1848afae5d5Sryo mesong12_usb2phy_enable(device_t dev, void *priv, bool enable)
1858afae5d5Sryo {
1868afae5d5Sryo struct mesong12_usb2phy_softc * const sc = device_private(dev);
1878afae5d5Sryo
1888afae5d5Sryo if (sc->sc_reset != NULL) {
1898afae5d5Sryo fdtbus_reset_assert(sc->sc_reset);
1908afae5d5Sryo delay(10);
1918afae5d5Sryo fdtbus_reset_deassert(sc->sc_reset);
1928afae5d5Sryo delay(1000);
1938afae5d5Sryo }
1948afae5d5Sryo
1958afae5d5Sryo if (sc->sc_supply != NULL)
1968afae5d5Sryo fdtbus_regulator_enable(sc->sc_supply);
1978afae5d5Sryo
1988afae5d5Sryo if (!enable)
1998afae5d5Sryo return 0;
2008afae5d5Sryo
2018afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R21_REG,
2028afae5d5Sryo USB2PHY_READ_REG(sc, USB2PHY_R21_REG) |
2038afae5d5Sryo USB2PHY_R21_USB2_OTG_ACA_EN);
2048afae5d5Sryo
2058afae5d5Sryo /* set PLL to 480MHz */
2068afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R16_REG,
2078afae5d5Sryo USB2PHY_R16_USB2_MPLL_RESET |
2088afae5d5Sryo USB2PHY_R16_USB2_MPLL_EN |
2098afae5d5Sryo USB2PHY_R16_USB2_MPLL_FAST_LOCK |
2108afae5d5Sryo __SHIFTIN(1, USB2PHY_R16_USB2_MPLL_LOCK_LONG) |
2118afae5d5Sryo USB2PHY_R16_USB2_MPLL_LOAD |
2128afae5d5Sryo __SHIFTIN(1, USB2PHY_R16_USB2_MPLL_N) |
2138afae5d5Sryo __SHIFTIN(20, USB2PHY_R16_USB2_MPLL_M));
2148afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R17_REG,
2158afae5d5Sryo __SHIFTIN(0, USB2PHY_R17_USB2_MPLL_FILTER_PVT1) |
2168afae5d5Sryo __SHIFTIN(7, USB2PHY_R17_USB2_MPLL_FILTER_PVT2) |
2178afae5d5Sryo __SHIFTIN(7, USB2PHY_R17_USB2_MPLL_LAMBDA0) |
2188afae5d5Sryo __SHIFTIN(2, USB2PHY_R17_USB2_MPLL_LAMBDA1) |
2198afae5d5Sryo __SHIFTIN(9, USB2PHY_R17_USB2_MPLL_FRAC_IN));
2208afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R18_REG,
2218afae5d5Sryo USB2PHY_R18_USB2_MPLL_ACG_RANGE |
2228afae5d5Sryo __SHIFTIN(1, USB2PHY_R18_USB2_MPLL_ADJ_LDO) |
2238afae5d5Sryo __SHIFTIN(3, USB2PHY_R18_USB2_MPLL_ALPHA) |
2248afae5d5Sryo __SHIFTIN(0, USB2PHY_R18_USB2_MPLL_BB_MODE) |
2258afae5d5Sryo __SHIFTIN(1, USB2PHY_R18_USB2_MPLL_BIAS_ADJ) |
2268afae5d5Sryo __SHIFTIN(3, USB2PHY_R18_USB2_MPLL_DATA_SEL) |
2278afae5d5Sryo __SHIFTIN(7, USB2PHY_R18_USB2_MPLL_ROU) |
2288afae5d5Sryo __SHIFTIN(1, USB2PHY_R18_USB2_MPLL_PFD_GAIN) |
2298afae5d5Sryo __SHIFTIN(39, USB2PHY_R18_USB2_MPLL_LK_S) |
2308afae5d5Sryo __SHIFTIN(9, USB2PHY_R18_USB2_MPLL_LK_W) |
2318afae5d5Sryo __SHIFTIN(1, USB2PHY_R18_USB2_MPLL_LKW_SEL));
2328afae5d5Sryo delay(100);
2338afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R16_REG,
2348afae5d5Sryo USB2PHY_R16_USB2_MPLL_EN |
2358afae5d5Sryo USB2PHY_R16_USB2_MPLL_FAST_LOCK |
2368afae5d5Sryo __SHIFTIN(1, USB2PHY_R16_USB2_MPLL_LOCK_LONG) |
2378afae5d5Sryo USB2PHY_R16_USB2_MPLL_LOAD |
2388afae5d5Sryo __SHIFTIN(1, USB2PHY_R16_USB2_MPLL_N) |
2398afae5d5Sryo __SHIFTIN(20, USB2PHY_R16_USB2_MPLL_M));
2408afae5d5Sryo
2418afae5d5Sryo /* tune PHY */
2428afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R20_REG,
2438afae5d5Sryo __SHIFTIN(0, USB2PHY_R20_USB2_BGR_DBG_1_0) |
2448afae5d5Sryo __SHIFTIN(0, USB2PHY_R20_USB2_BGR_VREF_4_0) |
2458afae5d5Sryo __SHIFTIN(0, USB2PHY_R20_USB2_BGR_ADJ_4_0) |
2468afae5d5Sryo __SHIFTIN(3, USB2PHY_R20_USB2_EDGE_DRV_TRIM_1_0) |
2478afae5d5Sryo USB2PHY_R20_USB2_EDGE_DRV_EN |
2488afae5d5Sryo __SHIFTIN(15, USB2PHY_R20_USB2_DMON_SEL_3_0) |
2498afae5d5Sryo USB2PHY_R20_USB2_OTG_VBUSDET_EN |
2508afae5d5Sryo __SHIFTIN(4, USB2PHY_R20_USB2_OTG_VBUS_TRIM_2_0));
2518afae5d5Sryo
2528afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R04_REG,
2538afae5d5Sryo __SHIFTIN(0, USB2PHY_R04_I_C2L_BIAS_TRIM) |
2548afae5d5Sryo USB2PHY_R04_TEST_BYPASS_MODE_EN |
2558afae5d5Sryo __SHIFTIN(0xfff, USB2PHY_R04_CALIBRATION_CODE_VALUE));
2568afae5d5Sryo
2578afae5d5Sryo /* tune disconnect threshold */
2588afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R03_REG,
2598afae5d5Sryo __SHIFTIN(3, USB2PHY_R03_DISC_THRESH) |
2608afae5d5Sryo __SHIFTIN(1, USB2PHY_R03_HSDIC_REF) |
2618afae5d5Sryo __SHIFTIN(0, USB2PHY_R03_SQUELCH_REF));
2628afae5d5Sryo
2638afae5d5Sryo /* analog settings */
2648afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R14_REG,
2658afae5d5Sryo __SHIFTIN(0, USB2PHY_R14_BYPASS_CTRL) |
2668afae5d5Sryo __SHIFTIN(0, USB2PHY_R14_I_RPU_SW2_EN));
2678afae5d5Sryo USB2PHY_WRITE_REG(sc, USB2PHY_R13_REG,
2688afae5d5Sryo __SHIFTIN(7, USB2PHY_R13_MINIMUM_COUNT_FOR_SYNC_DETECTION) |
2698afae5d5Sryo USB2PHY_R13_UPDATE_PMA_SIGNALS);
2708afae5d5Sryo
2718afae5d5Sryo return 0;
2728afae5d5Sryo }
2738afae5d5Sryo
2746e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
2756e54367aSthorpej { .compat = "amlogic,g12a-usb2-phy" },
2766e54367aSthorpej DEVICE_COMPAT_EOL
2778afae5d5Sryo };
2788afae5d5Sryo
2798afae5d5Sryo static int
mesong12_usb2phy_match(device_t parent,cfdata_t cf,void * aux)2808afae5d5Sryo mesong12_usb2phy_match(device_t parent, cfdata_t cf, void *aux)
2818afae5d5Sryo {
2828afae5d5Sryo struct fdt_attach_args * const faa = aux;
2838afae5d5Sryo
2846e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
2858afae5d5Sryo }
2868afae5d5Sryo
2878afae5d5Sryo static const struct fdtbus_phy_controller_func mesong12_usb2phy_funcs = {
2888afae5d5Sryo .acquire = mesong12_usb2phy_acquire,
2898afae5d5Sryo .release = mesong12_usb2phy_release,
2908afae5d5Sryo .enable = mesong12_usb2phy_enable
2918afae5d5Sryo };
2928afae5d5Sryo
2938afae5d5Sryo static void
mesong12_usb2phy_attach(device_t parent,device_t self,void * aux)2948afae5d5Sryo mesong12_usb2phy_attach(device_t parent, device_t self, void *aux)
2958afae5d5Sryo {
2968afae5d5Sryo struct mesong12_usb2phy_softc * const sc = device_private(self);
2978afae5d5Sryo struct fdt_attach_args * const faa = aux;
2988afae5d5Sryo const int phandle = faa->faa_phandle;
2998afae5d5Sryo bus_addr_t addr;
3008afae5d5Sryo bus_size_t size;
3018afae5d5Sryo
3028afae5d5Sryo sc->sc_dev = self;
3038afae5d5Sryo sc->sc_bst = faa->faa_bst;
3048afae5d5Sryo sc->sc_phandle = phandle;
3058afae5d5Sryo
3068afae5d5Sryo if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
3078afae5d5Sryo aprint_error(": couldn't get registers\n");
3088afae5d5Sryo return;
3098afae5d5Sryo }
3108afae5d5Sryo if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
3118afae5d5Sryo aprint_error(": couldn't map registers\n");
3128afae5d5Sryo return;
3138afae5d5Sryo }
3148afae5d5Sryo
3158afae5d5Sryo sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
3168afae5d5Sryo if (sc->sc_clk == NULL) {
3178afae5d5Sryo aprint_error(": couldn't get clock\n");
3188afae5d5Sryo goto attach_failure;
3198afae5d5Sryo }
3208afae5d5Sryo if (clk_enable(sc->sc_clk) != 0) {
3218afae5d5Sryo aprint_error(": couldn't enable clock\n");
3228afae5d5Sryo goto attach_failure;
3238afae5d5Sryo }
3248afae5d5Sryo
3258afae5d5Sryo sc->sc_reset = fdtbus_reset_get_index(phandle, 0);
3268afae5d5Sryo sc->sc_supply = fdtbus_regulator_acquire(phandle, "phy-supply");
3278afae5d5Sryo
3288afae5d5Sryo aprint_naive("\n");
3298afae5d5Sryo aprint_normal(": USB2 PHY\n");
3308afae5d5Sryo
3318afae5d5Sryo fdtbus_register_phy_controller(self, phandle, &mesong12_usb2phy_funcs);
3328afae5d5Sryo return;
3338afae5d5Sryo
3348afae5d5Sryo attach_failure:
3358afae5d5Sryo bus_space_unmap(sc->sc_bst, sc->sc_bsh, size);
3368afae5d5Sryo return;
3378afae5d5Sryo }
3388afae5d5Sryo
3398afae5d5Sryo CFATTACH_DECL_NEW(mesong12_usb2phy, sizeof(struct mesong12_usb2phy_softc),
3408afae5d5Sryo mesong12_usb2phy_match, mesong12_usb2phy_attach, NULL, NULL);
341