Path: blob/main/sys/arm64/broadcom/brcmmdio/mdio_ns2_pcie_phy.c
39481 views
/*-1* Copyright (c) 2019 Juniper Networks, Inc.2* Copyright (c) 2019 Semihalf.3* All rights reserved.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 ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,18* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES19* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR20* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,22* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN23* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE24* POSSIBILITY OF SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/bus.h>29#include <sys/kernel.h>30#include <sys/module.h>31#include <sys/rman.h>32#include <sys/systm.h>3334#include <dev/fdt/simplebus.h>35#include <dev/ofw/ofw_bus_subr.h>36#include <dev/ofw/ofw_bus.h>3738#include <machine/bus.h>39#include <machine/resource.h>4041#include "mdio_if.h"4243#define BLK_ADDR_REG_OFFSET 0x1f44#define PLL_AFE1_100MHZ_BLK 0x210045#define PLL_CLK_AMP_OFFSET 0x0346#define PLL_CLK_AMP_2P05V 0x2b184748struct ns2_pcie_phy_softc {49uint32_t phy_id;50};5152static device_probe_t ns2_pcie_phy_fdt_probe;53static device_attach_t ns2_pcie_phy_fdt_attach;5455static int ns2_pci_phy_init(device_t dev);5657static device_method_t ns2_pcie_phy_fdt_methods[] = {58/* Device interface */59DEVMETHOD(device_probe, ns2_pcie_phy_fdt_probe),60DEVMETHOD(device_attach, ns2_pcie_phy_fdt_attach),6162DEVMETHOD_END63};6465DEFINE_CLASS_0(ns2_pcie_phy, ns2_pcie_phy_fdt_driver, ns2_pcie_phy_fdt_methods,66sizeof(struct ns2_pcie_phy_softc));6768static driver_t ns2_pcie_phy_driver = {69"ns2_pcie_phy",70ns2_pcie_phy_fdt_methods,71sizeof(struct ns2_pcie_phy_softc)72};7374EARLY_DRIVER_MODULE(ns2_pcie_phy, brcm_mdionexus, ns2_pcie_phy_driver,75NULL, NULL, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);7677static int78ns2_pci_phy_init(device_t dev)79{80struct ns2_pcie_phy_softc *sc;81int err;8283sc = device_get_softc(dev);8485/* select the AFE 100MHz block page */86err = MDIO_WRITEREG(device_get_parent(dev), sc->phy_id,87BLK_ADDR_REG_OFFSET, PLL_AFE1_100MHZ_BLK);88if (err)89goto err;9091/* set the 100 MHz reference clock amplitude to 2.05 v */92err = MDIO_WRITEREG(device_get_parent(dev), sc->phy_id,93PLL_CLK_AMP_OFFSET, PLL_CLK_AMP_2P05V);94if (err)95goto err;9697return 0;9899err:100device_printf(dev, "Error %d writing to phy\n", err);101return (err);102}103104static __inline void105get_addr_size_cells(phandle_t node, pcell_t *addr_cells, pcell_t *size_cells)106{107108*addr_cells = 2;109/* Find address cells if present */110OF_getencprop(node, "#address-cells", addr_cells, sizeof(*addr_cells));111112*size_cells = 2;113/* Find size cells if present */114OF_getencprop(node, "#size-cells", size_cells, sizeof(*size_cells));115}116117static int118ns2_pcie_phy_fdt_probe(device_t dev)119{120121if (!ofw_bus_status_okay(dev))122return (ENXIO);123124if (!ofw_bus_is_compatible(dev, "brcm,ns2-pcie-phy"))125return (ENXIO);126127device_set_desc(dev, "Broadcom NS2 PCIe PHY");128return (BUS_PROBE_SPECIFIC);129}130131static int132ns2_pcie_phy_fdt_attach(device_t dev)133{134struct ns2_pcie_phy_softc *sc;135pcell_t addr_cells, size_cells, buf[2];136phandle_t node;137138sc = device_get_softc(dev);139140node = ofw_bus_get_node(dev);141get_addr_size_cells(OF_parent(node), &addr_cells, &size_cells);142if ((addr_cells != 1) || (size_cells != 0)) {143device_printf(dev,144"Only addr_cells=1 and size_cells=0 are supported\n");145return (EINVAL);146}147148if (OF_getencprop(node, "reg", buf, sizeof(pcell_t)) < 0)149return (ENXIO);150151sc->phy_id = buf[0];152153if (ns2_pci_phy_init(dev) < 0)154return (EINVAL);155156bus_attach_children(dev);157return (0);158}159160161