Path: blob/main/sys/arm/mv/clk/armada38x_coreclk.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2022 Semihalf.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 IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <sys/bus.h>28#include <sys/kernel.h>29#include <sys/module.h>30#include <sys/mutex.h>31#include <sys/rman.h>3233#include <machine/bus.h>34#include <machine/fdt.h>3536#include <dev/fdt/simplebus.h>37#include <dev/ofw/ofw_bus.h>38#include <dev/ofw/ofw_bus_subr.h>39#include <dev/clk/clk.h>4041#include <arm/mv/mvwin.h>42#include <arm/mv/mvreg.h>43#include <arm/mv/mvvar.h>4445#include <arm/mv/clk/armada38x_gen.h>4647#include "clkdev_if.h"4849#define ARMADA38X_CORECLK_MAXREG 05051static struct resource_spec armada38x_coreclk_specs[] = {52{ SYS_RES_MEMORY, 0, RF_ACTIVE },53{ -1, 0 }54};5556struct armada38x_coreclk_softc {57struct resource *res;58struct clkdom *clkdom;59struct mtx mtx;60};6162static int armada38x_coreclk_attach(device_t dev);63static int armada38x_coreclk_probe(device_t dev);6465static struct armada38x_gen_clknode_def gen_nodes[] =66{67{68.def = {69.name = "coreclk_0",70.id = 0,71.parent_cnt = 0,72},73},74{75.def = {76.name = "coreclk_2",77.id = 1,78.parent_cnt = 0,79},80}81};8283static int84armada38x_coreclk_read_4(device_t dev, bus_addr_t addr, uint32_t *val)85{86struct armada38x_coreclk_softc *sc;8788sc = device_get_softc(dev);8990if (addr > ARMADA38X_CORECLK_MAXREG)91return (EINVAL);9293*val = bus_read_4(sc->res, addr);9495return (0);96}9798static int99armada38x_coreclk_write_4(device_t dev, bus_addr_t addr, uint32_t val)100{101struct armada38x_coreclk_softc *sc;102103sc = device_get_softc(dev);104105if (addr > ARMADA38X_CORECLK_MAXREG)106return (EINVAL);107108bus_write_4(sc->res, addr, val);109110return (0);111}112113static void114armada38x_coreclk_device_lock(device_t dev)115{116struct armada38x_coreclk_softc *sc;117118sc = device_get_softc(dev);119mtx_lock(&sc->mtx);120}121122static void123armada38x_coreclk_device_unlock(device_t dev)124{125struct armada38x_coreclk_softc *sc;126127sc = device_get_softc(dev);128mtx_unlock(&sc->mtx);129}130131static int132armada38x_coreclk_probe(device_t dev)133{134135if (!ofw_bus_status_okay(dev))136return (ENXIO);137138if (!ofw_bus_is_compatible(dev, "marvell,armada-380-core-clock"))139return (ENXIO);140141device_set_desc(dev, "ARMADA38X core-clock");142143return (BUS_PROBE_DEFAULT);144}145146static int147armada38x_coreclk_create_coreclk(device_t dev)148{149struct armada38x_coreclk_softc *sc;150int rv, i;151152sc = device_get_softc(dev);153154for (i = 0; i < nitems(gen_nodes); ++i) {155rv = armada38x_gen_register(sc->clkdom, &gen_nodes[i]);156if (rv)157return (rv);158}159160return (rv);161}162163static int164armada38x_coreclk_attach(device_t dev)165{166struct armada38x_coreclk_softc *sc;167int error;168169sc = device_get_softc(dev);170171if (bus_alloc_resources(dev, armada38x_coreclk_specs, &sc->res) != 0) {172device_printf(dev, "Cannot allocate resources.\n");173return (ENXIO);174}175176mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);177178sc->clkdom = clkdom_create(dev);179if (NULL == sc->clkdom) {180device_printf(dev, "Cannot create clkdom\n");181return (ENXIO);182}183184error = armada38x_coreclk_create_coreclk(dev);185if (0 != error) {186device_printf(dev, "Cannot create coreclk.\n");187return (error);188}189190if (clkdom_finit(sc->clkdom) != 0)191panic("Cannot finalize clock domain initialization.\n");192193if (bootverbose)194clkdom_dump(sc->clkdom);195196return (0);197}198199static device_method_t amada38x_coreclk_methods[] = {200DEVMETHOD(clkdev_write_4, armada38x_coreclk_write_4),201DEVMETHOD(clkdev_read_4, armada38x_coreclk_read_4),202DEVMETHOD(clkdev_device_lock, armada38x_coreclk_device_lock),203DEVMETHOD(clkdev_device_unlock, armada38x_coreclk_device_unlock),204205DEVMETHOD(device_attach, armada38x_coreclk_attach),206DEVMETHOD(device_probe, armada38x_coreclk_probe),207208DEVMETHOD_END209};210211static driver_t armada38x_coreclk_driver = {212"armada38x_coreclk",213amada38x_coreclk_methods,214sizeof(struct armada38x_coreclk_softc),215};216217EARLY_DRIVER_MODULE(armada38x_coreclk, simplebus, armada38x_coreclk_driver, 0, 0,218BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);219220221