Path: blob/main/sys/arm64/rockchip/rk3568_pciephy.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"5354#define GRF_PCIE30PHY_CON1 0x0455#define GRF_PCIE30PHY_CON4 0x1056#define GRF_PCIE30PHY_CON5 0x1457#define GRF_PCIE30PHY_CON6 0x1858#define GRF_BIFURCATION_LANE_1 059#define GRF_BIFURCATION_LANE_2 160#define GRF_PCIE30PHY_WR_EN (0xf << 16)61#define GRF_PCIE30PHY_CON9 0x2462#define GRF_PCIE30PHY_DA_OCM_MASK (1 << (15 + 16))63#define GRF_PCIE30PHY_DA_OCM ((1 << 15) | GRF_PCIE30PHY_DA_OCM_MASK)64#define GRF_PCIE30PHY_STATUS0 0x8065#define SRAM_INIT_DONE (1 << 14)6667static struct ofw_compat_data compat_data[] = {68{"rockchip,rk3568-pcie3-phy", 1},69{NULL, 0}70};7172struct rk3568_pciephy_softc {73device_t dev;74phandle_t node;75struct resource *mem;76struct phynode *phynode;77struct syscon *phy_grf;78clk_t refclk_m;79clk_t refclk_n;80clk_t pclk;81hwreset_t phy_reset;82};838485static void86rk3568_pciephy_bifurcate(device_t dev, int control, uint32_t lane)87{88struct rk3568_pciephy_softc *sc = device_get_softc(dev);8990switch (lane) {91case 0:92SYSCON_WRITE_4(sc->phy_grf, control, GRF_PCIE30PHY_WR_EN);93return;94case 1:95SYSCON_WRITE_4(sc->phy_grf, control,96GRF_PCIE30PHY_WR_EN | GRF_BIFURCATION_LANE_1);97break;98case 2:99SYSCON_WRITE_4(sc->phy_grf, control,100GRF_PCIE30PHY_WR_EN | GRF_BIFURCATION_LANE_2);101break;102default:103device_printf(dev, "Illegal lane %d\n", lane);104return;105}106if (bootverbose)107device_printf(dev, "lane %d @ pcie3x%d\n", lane,108(control == GRF_PCIE30PHY_CON5) ? 1 : 2);109}110111/* PHY class and methods */112static int113rk3568_pciephy_enable(struct phynode *phynode, bool enable)114{115device_t dev = phynode_get_device(phynode);116struct rk3568_pciephy_softc *sc = device_get_softc(dev);117int count;118119if (enable) {120/* Pull PHY out of reset */121hwreset_deassert(sc->phy_reset);122123/* Poll for SRAM loaded and ready */124for (count = 100; count; count--) {125if (SYSCON_READ_4(sc->phy_grf, GRF_PCIE30PHY_STATUS0) &126SRAM_INIT_DONE)127break;128DELAY(10000);129if (count == 0) {130device_printf(dev, "SRAM init timeout!\n");131return (ENXIO);132}133}134}135return (0);136}137138static phynode_method_t rk3568_pciephy_phynode_methods[] = {139PHYNODEMETHOD(phynode_enable, rk3568_pciephy_enable),140141PHYNODEMETHOD_END142};143DEFINE_CLASS_1(rk3568_pciephy_phynode, rk3568_pciephy_phynode_class,144rk3568_pciephy_phynode_methods, 0, phynode_class);145146147/* Device class and methods */148static int149rk3568_pciephy_probe(device_t dev)150{151152if (!ofw_bus_status_okay(dev))153return (ENXIO);154if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)155return (ENXIO);156device_set_desc(dev, "RockChip PCIe PHY");157return (BUS_PROBE_DEFAULT);158}159160static int161rk3568_pciephy_attach(device_t dev)162{163struct rk3568_pciephy_softc *sc = device_get_softc(dev);164struct phynode_init_def phy_init;165struct phynode *phynode;166uint32_t data_lanes[2] = { 0, 0 };167int rid = 0;168169sc->dev = dev;170sc->node = ofw_bus_get_node(dev);171172/* Get memory resource */173if (!(sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,174RF_ACTIVE))) {175device_printf(dev, "Cannot allocate memory resources\n");176return (ENXIO);177}178179/* Get syncons handle */180if (OF_hasprop(sc->node, "rockchip,phy-grf") &&181syscon_get_by_ofw_property(dev, sc->node, "rockchip,phy-grf",182&sc->phy_grf))183return (ENXIO);184185/* Get & enable clocks */186if (clk_get_by_ofw_name(dev, 0, "refclk_m", &sc->refclk_m)) {187device_printf(dev, "getting refclk_m failed\n");188return (ENXIO);189}190if (clk_enable(sc->refclk_m))191device_printf(dev, "enable refclk_m failed\n");192if (clk_get_by_ofw_name(dev, 0, "refclk_n", &sc->refclk_n)) {193device_printf(dev, "getting refclk_n failed\n");194return (ENXIO);195}196if (clk_enable(sc->refclk_n))197device_printf(dev, "enable refclk_n failed\n");198if (clk_get_by_ofw_name(dev, 0, "pclk", &sc->pclk)) {199device_printf(dev, "getting pclk failed\n");200return (ENXIO);201}202if (clk_enable(sc->pclk))203device_printf(dev, "enable pclk failed\n");204205/* Get & assert reset */206if (hwreset_get_by_ofw_idx(dev, sc->node, 0, &sc->phy_reset)) {207device_printf(dev, "Cannot get reset\n");208} else209hwreset_assert(sc->phy_reset);210211/* Set RC/EP mode not implemented yet (RC mode only) */212213/* Set bifurcation according to "data-lanes" entry */214if (OF_hasprop(sc->node, "data-lanes")) {215OF_getencprop(sc->node, "data-lanes", data_lanes,216sizeof(data_lanes));217} else218if (bootverbose)219device_printf(dev, "lane 1 & 2 @pcie3x2\n");220221/* Deassert PCIe PMA output clamp mode */222SYSCON_WRITE_4(sc->phy_grf, GRF_PCIE30PHY_CON9, GRF_PCIE30PHY_DA_OCM);223224/* Configure PHY HW accordingly */225rk3568_pciephy_bifurcate(dev, GRF_PCIE30PHY_CON5, data_lanes[0]);226rk3568_pciephy_bifurcate(dev, GRF_PCIE30PHY_CON6, data_lanes[1]);227228if (data_lanes[0] || data_lanes[1])229SYSCON_WRITE_4(sc->phy_grf, GRF_PCIE30PHY_CON1,230GRF_PCIE30PHY_DA_OCM);231else232SYSCON_WRITE_4(sc->phy_grf, GRF_PCIE30PHY_CON1,233GRF_PCIE30PHY_DA_OCM_MASK);234235bzero(&phy_init, sizeof(phy_init));236phy_init.id = PHY_NONE;237phy_init.ofw_node = sc->node;238if (!(phynode = phynode_create(dev, &rk3568_pciephy_phynode_class,239&phy_init))) {240device_printf(dev, "failed to create pciephy PHY\n");241return (ENXIO);242}243if (!phynode_register(phynode)) {244device_printf(dev, "failed to register pciephy PHY\n");245return (ENXIO);246}247sc->phynode = phynode;248249return (0);250}251252static device_method_t rk3568_pciephy_methods[] = {253DEVMETHOD(device_probe, rk3568_pciephy_probe),254DEVMETHOD(device_attach, rk3568_pciephy_attach),255256DEVMETHOD_END257};258259DEFINE_CLASS_1(rk3568_pciephy, rk3568_pciephy_driver, rk3568_pciephy_methods,260sizeof(struct simple_mfd_softc), simple_mfd_driver);261EARLY_DRIVER_MODULE(rk3568_pciephy, simplebus, rk3568_pciephy_driver,2620, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_LATE);263264265