Path: blob/main/sys/arm64/rockchip/rk3568_combphy.c
39478 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2021, 2022 Soren Schmidt <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*26*/2728#include <sys/param.h>29#include <sys/bus.h>30#include <sys/kernel.h>31#include <sys/module.h>32#include <sys/mutex.h>33#include <sys/rman.h>34#include <machine/bus.h>3536#include <dev/ofw/openfirm.h>37#include <dev/ofw/ofw_bus.h>38#include <dev/ofw/ofw_bus_subr.h>3940#include <dev/fdt/simple_mfd.h>4142#include <dev/clk/clk.h>43#include <dev/hwreset/hwreset.h>44#include <dev/regulator/regulator.h>45#include <dev/syscon/syscon.h>46#include <dev/phy/phy.h>4748#include <contrib/device-tree/include/dt-bindings/phy/phy.h>4950#include "syscon_if.h"51#include "phydev_if.h"52#include "phynode_if.h"535455static struct ofw_compat_data compat_data[] = {56{"rockchip,rk3568-naneng-combphy", 1},57{NULL, 0}58};5960struct rk3568_combphy_softc {61device_t dev;62phandle_t node;63struct resource *mem;64struct phynode *phynode;65struct syscon *pipe_grf;66struct syscon *pipe_phy_grf;67clk_t ref_clk;68clk_t apb_clk;69clk_t pipe_clk;70hwreset_t phy_reset;71int mode;72};7374#define PHYREG6 0x1475#define PHYREG6_PLL_DIV_MASK 0xc076#define PHYREG6_PLL_DIV_2 (1 << 6)77#define PHYREG7 0x1878#define PHYREG7_TX_RTERM_50OHM (8 << 4)79#define PHYREG7_RX_RTERM_44OHM (15 << 0)80#define PHYREG8 0x1c81#define PHYREG8_SSC_EN 0x1082#define PHYREG11 0x2883#define PHYREG11_SU_TRIM_0_7 0xf084#define PHYREG12 0x2c85#define PHYREG12_PLL_LPF_ADJ_VALUE 486#define PHYREG15 0x3887#define PHYREG15_CTLE_EN 0x0188#define PHYREG15_SSC_CNT_MASK 0xc089#define PHYREG15_SSC_CNT_VALUE (1 << 6)90#define PHYREG16 0x3c91#define PHYREG16_SSC_CNT_VALUE 0x5f92#define PHYREG18 0x4493#define PHYREG18_PLL_LOOP 0x3294#define PHYREG32 0x7c95#define PHYREG32_SSC_MASK 0xf096#define PHYREG32_SSC_UPWARD (0 << 4)97#define PHYREG32_SSC_DOWNWARD (1 << 4)98#define PHYREG32_SSC_OFFSET_500PPM (1 << 6)99#define PHYREG33 0x80100#define PHYREG33_PLL_KVCO_MASK 0x1c101#define PHYREG33_PLL_KVCO_VALUE (2 << 2)102103#define PIPE_MASK_ALL (0xffff << 16)104#define PIPE_PHY_GRF_PIPE_CON0 0x00105#define PIPE_DATABUSWIDTH_MASK 0x3106#define PIPE_DATABUSWIDTH_32BIT 0107#define PIPE_DATABUSWIDTH_16BIT 1108#define PIPE_PHYMODE_MASK (3 << 2)109#define PIPE_PHYMODE_PCIE (0 << 2)110#define PIPE_PHYMODE_USB3 (1 << 2)111#define PIPE_PHYMODE_SATA (2 << 2)112#define PIPE_RATE_MASK (3 << 4)113#define PIPE_RATE_PCIE_2_5GBPS (0 << 4)114#define PIPE_RATE_PCIE_5GBPS (1 << 4)115#define PIPE_RATE_USB3_5GBPS (0 << 4)116#define PIPE_RATE_SATA_1GBPS5 (0 << 4)117#define PIPE_RATE_SATA_3GBPS (1 << 4)118#define PIPE_RATE_SATA_6GBPS (2 << 4)119#define PIPE_MAC_PCLKREQ_N (1 << 8)120#define PIPE_L1SUB_ENTREQ (1 << 9)121#define PIPE_RXTERM (1 << 12)122#define PIPE_PHY_GRF_PIPE_CON1 0x04123#define PHY_CLK_SEL_MASK (3 << 13)124#define PHY_CLK_SEL_24M (0 << 13)125#define PHY_CLK_SEL_25M (1 << 13)126#define PHY_CLK_SEL_100M (2 << 13)127#define PIPE_PHY_GRF_PIPE_CON2 0x08128#define SEL_PIPE_TXCOMPLIANCE_I (1 << 15)129#define SEL_PIPE_TXELECIDLE (1 << 12)130#define SEL_PIPE_RXTERM (1 << 8)131#define SEL_PIPE_BYPASS_CODEC (1 << 7)132#define SEL_PIPE_PIPE_EBUF (1 << 6)133#define SEL_PIPE_PIPE_PHYMODE (1 << 1)134#define SEL_PIPE_DATABUSWIDTH (1 << 0)135#define PIPE_PHY_GRF_PIPE_CON3 0x0c136#define PIPE_SEL_MASK (3 << 13)137#define PIPE_SEL_PCIE (0 << 13)138#define PIPE_SEL_USB3 (1 << 13)139#define PIPE_SEL_SATA (2 << 13)140#define PIPE_CLK_REF_SRC_I_MASK (3 << 8)141#define PIPE_CLK_REF_SRC_I_PLL_CKREF_INNER (2 << 8)142#define PIPE_RXELECIDLE (1 << 10)143#define PIPE_FROM_PCIE_IO (1 << 11)144145#define PIPE_GRF_PIPE_CON0 0x00146#define SATA2_PHY_SPDMODE_1GBPS5 (0 << 12)147#define SATA2_PHY_SPDMODE_3GBPS (1 << 12)148#define SATA2_PHY_SPDMODE_6GBPS (2 << 12)149#define SATA1_PHY_SPDMODE_1GBPS5 (0 << 8)150#define SATA1_PHY_SPDMODE_3GBPS (1 << 8)151#define SATA1_PHY_SPDMODE_6GBPS (2 << 8)152#define SATA0_PHY_SPDMODE_1GBPS5 (0 << 4)153#define SATA0_PHY_SPDMODE_3GBPS (1 << 4)154#define SATA0_PHY_SPDMODE_6GBPS (2 << 4)155156#define PIPE_GRF_SATA_CON0 0x10157#define PIPE_GRF_SATA_CON1 0x14158#define PIPE_GRF_SATA_CON2 0x18159#define PIPE_GRF_XPCS_CON0 0x40160161162/* PHY class and methods */163static int164rk3568_combphy_enable(struct phynode *phynode, bool enable)165{166device_t dev = phynode_get_device(phynode);167struct rk3568_combphy_softc *sc = device_get_softc(dev);168uint64_t rate;169170if (enable == false)171return (0);172173switch (sc->mode) {174case PHY_TYPE_SATA:175device_printf(dev, "configuring for SATA");176177/* tx_rterm 50 ohm & rx_rterm 44 ohm */178bus_write_4(sc->mem, PHYREG7,179PHYREG7_TX_RTERM_50OHM | PHYREG7_RX_RTERM_44OHM);180181/* Adaptive CTLE */182bus_write_4(sc->mem, PHYREG15,183bus_read_4(sc->mem, PHYREG15) | PHYREG15_CTLE_EN);184185/* config grf_pipe for PCIe */186SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON3,187PIPE_MASK_ALL | PIPE_SEL_SATA | PIPE_RXELECIDLE | 0x7);188189SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON2,190PIPE_MASK_ALL | SEL_PIPE_TXCOMPLIANCE_I |191SEL_PIPE_DATABUSWIDTH | 0xc3);192193SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON0,194PIPE_MASK_ALL | PIPE_RXTERM | PIPE_DATABUSWIDTH_16BIT |195PIPE_RATE_SATA_3GBPS | PIPE_PHYMODE_SATA);196197SYSCON_WRITE_4(sc->pipe_grf, PIPE_GRF_PIPE_CON0,198PIPE_MASK_ALL | SATA0_PHY_SPDMODE_6GBPS |199SATA1_PHY_SPDMODE_6GBPS | SATA2_PHY_SPDMODE_6GBPS);200break;201202case PHY_TYPE_PCIE:203device_printf(dev, "configuring for PCIe");204205/* Set SSC downward spread spectrum */206bus_write_4(sc->mem, PHYREG32,207(bus_read_4(sc->mem, PHYREG32) & PHYREG32_SSC_MASK) |208PHYREG32_SSC_DOWNWARD);209210/* config grf_pipe for PCIe */211SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON3,212PIPE_MASK_ALL | PIPE_SEL_PCIE |213PIPE_CLK_REF_SRC_I_PLL_CKREF_INNER);214SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON2,215PIPE_MASK_ALL | SEL_PIPE_RXTERM | SEL_PIPE_DATABUSWIDTH);216SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON0,217PIPE_MASK_ALL | PIPE_RXTERM | PIPE_DATABUSWIDTH_32BIT |218PIPE_RATE_PCIE_2_5GBPS | PIPE_PHYMODE_PCIE);219break;220221case PHY_TYPE_USB3:222device_printf(dev, "configuring for USB3");223224/* Set SSC downward spread spectrum */225bus_write_4(sc->mem, PHYREG32,226(bus_read_4(sc->mem, PHYREG32) & PHYREG32_SSC_MASK) |227PHYREG32_SSC_DOWNWARD);228229/* Adaptive CTLE */230bus_write_4(sc->mem, PHYREG15,231bus_read_4(sc->mem, PHYREG15) | PHYREG15_CTLE_EN);232233/* Set PLL KVCO fine tuning signals */234bus_write_4(sc->mem, PHYREG33,235(bus_read_4(sc->mem, PHYREG33) & PHYREG33_PLL_KVCO_MASK) |236PHYREG33_PLL_KVCO_VALUE);237238/* Enable controlling random jitter. */239bus_write_4(sc->mem, PHYREG12, PHYREG12_PLL_LPF_ADJ_VALUE);240241/* Set PLL input clock divider 1/2 */242bus_write_4(sc->mem, PHYREG6,243(bus_read_4(sc->mem, PHYREG6) & PHYREG6_PLL_DIV_MASK) |244PHYREG6_PLL_DIV_2);245246/* Set PLL loop divider */247bus_write_4(sc->mem, PHYREG18, PHYREG18_PLL_LOOP);248249/* Set PLL LPF R1 to su_trim[0:7] */250bus_write_4(sc->mem, PHYREG11, PHYREG11_SU_TRIM_0_7);251252/* config grf_pipe for USB3 */253SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON3,254PIPE_MASK_ALL | PIPE_SEL_USB3);255SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON2,256PIPE_MASK_ALL);257SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON0,258PIPE_MASK_ALL | PIPE_DATABUSWIDTH_16BIT |259PIPE_PHYMODE_USB3 | PIPE_RATE_USB3_5GBPS);260break;261262default:263printf("Unsupported mode=%d\n", sc->mode);264return (-1);265}266267clk_get_freq(sc->ref_clk, &rate);268printf(" ref_clk=%lu\n", rate);269270switch (rate) {271case 24000000:272SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON1,273(PHY_CLK_SEL_MASK << 16) | PHY_CLK_SEL_24M);274275if (sc->mode == PHY_TYPE_USB3 || sc->mode == PHY_TYPE_SATA) {276/* Adaptive CTLE */277bus_write_4(sc->mem, PHYREG15,278(bus_read_4(sc->mem, PHYREG15) &279PHYREG15_SSC_CNT_MASK) | PHYREG15_SSC_CNT_VALUE);280281/* SSC control period */282bus_write_4(sc->mem, PHYREG16, PHYREG16_SSC_CNT_VALUE);283}284break;285286case 25000000:287SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON1,288(PHY_CLK_SEL_MASK << 16) | PHY_CLK_SEL_25M);289break;290291case 100000000:292SYSCON_WRITE_4(sc->pipe_phy_grf, PIPE_PHY_GRF_PIPE_CON1,293(PHY_CLK_SEL_MASK << 16) | PHY_CLK_SEL_100M);294295if (sc->mode == PHY_TYPE_PCIE) {296/* Set PLL KVCO fine tuning signals */297bus_write_4(sc->mem, PHYREG33,298(bus_read_4(sc->mem, PHYREG33) &299PHYREG33_PLL_KVCO_MASK) | PHYREG33_PLL_KVCO_VALUE);300301/* Enable controlling random jitter. */302bus_write_4(sc->mem, PHYREG12,303PHYREG12_PLL_LPF_ADJ_VALUE);304305/* Set PLL input clock divider 1/2 */306bus_write_4(sc->mem, PHYREG6,307(bus_read_4(sc->mem, PHYREG6) &308PHYREG6_PLL_DIV_MASK) | PHYREG6_PLL_DIV_2);309310/* Set PLL loop divider */311bus_write_4(sc->mem, PHYREG18, PHYREG18_PLL_LOOP);312313/* Set PLL LPF R1 to su_trim[0:7] */314bus_write_4(sc->mem, PHYREG11, PHYREG11_SU_TRIM_0_7);315}316if (sc->mode == PHY_TYPE_SATA) {317/* Set SSC downward spread spectrum */318bus_write_4(sc->mem, PHYREG32,319(bus_read_4(sc->mem, PHYREG32) & ~0x000000f0) |320PHYREG32_SSC_DOWNWARD | PHYREG32_SSC_OFFSET_500PPM);321}322break;323324default:325device_printf(dev, "unknown ref rate=%lu\n", rate);326break;327}328329if (OF_hasprop(sc->node, "rockchip,ext-refclk")) {330device_printf(dev, "UNSUPPORTED rockchip,ext-refclk\n");331}332if (OF_hasprop(sc->node, "rockchip,enable-ssc")) {333device_printf(dev, "setting rockchip,enable-ssc\n");334bus_write_4(sc->mem, PHYREG8,335bus_read_4(sc->mem, PHYREG8) | PHYREG8_SSC_EN);336}337338if (hwreset_deassert(sc->phy_reset))339device_printf(dev, "phy_reset failed to clear\n");340341return (0);342}343344static phynode_method_t rk3568_combphy_phynode_methods[] = {345PHYNODEMETHOD(phynode_enable, rk3568_combphy_enable),346347PHYNODEMETHOD_END348};349DEFINE_CLASS_1(rk3568_combphy_phynode, rk3568_combphy_phynode_class,350rk3568_combphy_phynode_methods, 0, phynode_class);351352353/* Device class and methods */354static int355rk3568_combphy_probe(device_t dev)356{357358if (!ofw_bus_status_okay(dev))359return (ENXIO);360if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)361return (ENXIO);362device_set_desc(dev, "RockChip combo PHY");363return (BUS_PROBE_DEFAULT);364}365366static int367rk3568_combphy_attach(device_t dev)368{369struct rk3568_combphy_softc *sc = device_get_softc(dev);370struct phynode_init_def phy_init;371struct phynode *phynode;372int rid = 0;373374sc->dev = dev;375sc->node = ofw_bus_get_node(dev);376377/* Get memory resource */378if (!(sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,379&rid, RF_ACTIVE))) {380device_printf(dev, "Cannot allocate memory resources\n");381return (ENXIO);382}383384/* Get syncons handles */385if (OF_hasprop(sc->node, "rockchip,pipe-grf") &&386syscon_get_by_ofw_property(dev, sc->node, "rockchip,pipe-grf",387&sc->pipe_grf))388return (ENXIO);389if (OF_hasprop(sc->node, "rockchip,pipe-phy-grf") &&390syscon_get_by_ofw_property(dev, sc->node, "rockchip,pipe-phy-grf",391&sc->pipe_phy_grf))392return (ENXIO);393394/* Get & enable clocks */395if (clk_get_by_ofw_name(dev, 0, "ref", &sc->ref_clk)) {396device_printf(dev, "getting ref failed\n");397return (ENXIO);398}399if (clk_enable(sc->ref_clk))400device_printf(dev, "enable ref failed\n");401if (clk_get_by_ofw_name(dev, 0, "apb", &sc->apb_clk)) {402device_printf(dev, "getting apb failed\n");403return (ENXIO);404}405if (clk_enable(sc->apb_clk))406device_printf(dev, "enable apb failed\n");407if (clk_get_by_ofw_name(dev, 0, "pipe", &sc->pipe_clk)) {408device_printf(dev, "getting pipe failed\n");409return (ENXIO);410}411if (clk_enable(sc->pipe_clk))412device_printf(dev, "enable pipe failed\n");413414/* get & assert reset */415if (hwreset_get_by_ofw_idx(dev, sc->node, 0, &sc->phy_reset)) {416device_printf(dev, "Cannot get reset\n");417return (ENXIO);418}419hwreset_assert(sc->phy_reset);420421bzero(&phy_init, sizeof(phy_init));422phy_init.id = 0;423phy_init.ofw_node = sc->node;424if (!(phynode = phynode_create(dev, &rk3568_combphy_phynode_class,425&phy_init))) {426device_printf(dev, "failed to create combphy PHY\n");427return (ENXIO);428}429if (!phynode_register(phynode)) {430device_printf(dev, "failed to register combphy PHY\n");431return (ENXIO);432}433sc->phynode = phynode;434sc->mode = 0;435436return (0);437}438439static int440rk3568_combphy_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,441intptr_t *id)442{443struct rk3568_combphy_softc *sc = device_get_softc(dev);444445if (phydev_default_ofw_map(dev, xref, ncells, cells, id))446return (ERANGE);447448/* Store the phy mode that is handed to us in id */449sc->mode = *id;450451/* Set our id to 0 so the std phy_get_*() works as usual */452*id = 0;453454return (0);455}456457static device_method_t rk3568_combphy_methods[] = {458DEVMETHOD(device_probe, rk3568_combphy_probe),459DEVMETHOD(device_attach, rk3568_combphy_attach),460DEVMETHOD(phydev_map, rk3568_combphy_map),461462DEVMETHOD_END463};464465DEFINE_CLASS_1(rk3568_combphy, rk3568_combphy_driver, rk3568_combphy_methods,466sizeof(struct simple_mfd_softc), simple_mfd_driver);467EARLY_DRIVER_MODULE(rk3568_combphy, simplebus, rk3568_combphy_driver,4680, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_LATE);469470471