194bc2117SOleksandr Tymoshenko /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
394bc2117SOleksandr Tymoshenko *
494bc2117SOleksandr Tymoshenko * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
594bc2117SOleksandr Tymoshenko * All rights reserved.
694bc2117SOleksandr Tymoshenko *
794bc2117SOleksandr Tymoshenko * Redistribution and use in source and binary forms, with or without
894bc2117SOleksandr Tymoshenko * modification, are permitted provided that the following conditions
994bc2117SOleksandr Tymoshenko * are met:
1094bc2117SOleksandr Tymoshenko * 1. Redistributions of source code must retain the above copyright
1194bc2117SOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer.
1294bc2117SOleksandr Tymoshenko * 2. Redistributions in binary form must reproduce the above copyright
1394bc2117SOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer in the
1494bc2117SOleksandr Tymoshenko * documentation and/or other materials provided with the distribution.
1594bc2117SOleksandr Tymoshenko *
1694bc2117SOleksandr Tymoshenko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1794bc2117SOleksandr Tymoshenko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1894bc2117SOleksandr Tymoshenko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1994bc2117SOleksandr Tymoshenko * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2094bc2117SOleksandr Tymoshenko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2194bc2117SOleksandr Tymoshenko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2294bc2117SOleksandr Tymoshenko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2394bc2117SOleksandr Tymoshenko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2494bc2117SOleksandr Tymoshenko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2594bc2117SOleksandr Tymoshenko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2694bc2117SOleksandr Tymoshenko * SUCH DAMAGE.
2794bc2117SOleksandr Tymoshenko */
2894bc2117SOleksandr Tymoshenko
2994bc2117SOleksandr Tymoshenko #include <sys/param.h>
3094bc2117SOleksandr Tymoshenko #include <sys/conf.h>
3194bc2117SOleksandr Tymoshenko #include <sys/bus.h>
3294bc2117SOleksandr Tymoshenko #include <sys/kernel.h>
3394bc2117SOleksandr Tymoshenko #include <sys/systm.h>
3494bc2117SOleksandr Tymoshenko
3594bc2117SOleksandr Tymoshenko #include <machine/bus.h>
3694bc2117SOleksandr Tymoshenko
37*be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
3894bc2117SOleksandr Tymoshenko
3994bc2117SOleksandr Tymoshenko #include <arm64/freescale/imx/clk/imx_clk_mux.h>
4094bc2117SOleksandr Tymoshenko
4194bc2117SOleksandr Tymoshenko #include "clkdev_if.h"
4294bc2117SOleksandr Tymoshenko
4394bc2117SOleksandr Tymoshenko #define WR4(_clk, off, val) \
4494bc2117SOleksandr Tymoshenko CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
4594bc2117SOleksandr Tymoshenko #define RD4(_clk, off, val) \
4694bc2117SOleksandr Tymoshenko CLKDEV_READ_4(clknode_get_device(_clk), off, val)
4794bc2117SOleksandr Tymoshenko #define MD4(_clk, off, clr, set ) \
4894bc2117SOleksandr Tymoshenko CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
4994bc2117SOleksandr Tymoshenko #define DEVICE_LOCK(_clk) \
5094bc2117SOleksandr Tymoshenko CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
5194bc2117SOleksandr Tymoshenko #define DEVICE_UNLOCK(_clk) \
5294bc2117SOleksandr Tymoshenko CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
5394bc2117SOleksandr Tymoshenko
5494bc2117SOleksandr Tymoshenko static int imx_clk_mux_init(struct clknode *clk, device_t dev);
5594bc2117SOleksandr Tymoshenko static int imx_clk_mux_set_mux(struct clknode *clk, int idx);
5694bc2117SOleksandr Tymoshenko
5794bc2117SOleksandr Tymoshenko struct imx_clk_mux_sc {
5894bc2117SOleksandr Tymoshenko uint32_t offset;
5994bc2117SOleksandr Tymoshenko uint32_t shift;
6094bc2117SOleksandr Tymoshenko uint32_t mask;
6194bc2117SOleksandr Tymoshenko int mux_flags;
6294bc2117SOleksandr Tymoshenko };
6394bc2117SOleksandr Tymoshenko
6494bc2117SOleksandr Tymoshenko static clknode_method_t imx_clk_mux_methods[] = {
6594bc2117SOleksandr Tymoshenko /* Device interface */
6694bc2117SOleksandr Tymoshenko CLKNODEMETHOD(clknode_init, imx_clk_mux_init),
6794bc2117SOleksandr Tymoshenko CLKNODEMETHOD(clknode_set_mux, imx_clk_mux_set_mux),
6894bc2117SOleksandr Tymoshenko CLKNODEMETHOD_END
6994bc2117SOleksandr Tymoshenko };
7094bc2117SOleksandr Tymoshenko DEFINE_CLASS_1(imx_clk_mux, imx_clk_mux_class, imx_clk_mux_methods,
7194bc2117SOleksandr Tymoshenko sizeof(struct imx_clk_mux_sc), clknode_class);
7294bc2117SOleksandr Tymoshenko
7394bc2117SOleksandr Tymoshenko static int
imx_clk_mux_init(struct clknode * clk,device_t dev)7494bc2117SOleksandr Tymoshenko imx_clk_mux_init(struct clknode *clk, device_t dev)
7594bc2117SOleksandr Tymoshenko {
7694bc2117SOleksandr Tymoshenko uint32_t reg;
7794bc2117SOleksandr Tymoshenko struct imx_clk_mux_sc *sc;
7894bc2117SOleksandr Tymoshenko int rv;
7994bc2117SOleksandr Tymoshenko
8094bc2117SOleksandr Tymoshenko sc = clknode_get_softc(clk);
8194bc2117SOleksandr Tymoshenko
8294bc2117SOleksandr Tymoshenko DEVICE_LOCK(clk);
8394bc2117SOleksandr Tymoshenko rv = RD4(clk, sc->offset, ®);
8494bc2117SOleksandr Tymoshenko DEVICE_UNLOCK(clk);
8594bc2117SOleksandr Tymoshenko if (rv != 0) {
8694bc2117SOleksandr Tymoshenko return (rv);
8794bc2117SOleksandr Tymoshenko }
8894bc2117SOleksandr Tymoshenko reg = (reg >> sc->shift) & sc->mask;
8994bc2117SOleksandr Tymoshenko clknode_init_parent_idx(clk, reg);
9094bc2117SOleksandr Tymoshenko return(0);
9194bc2117SOleksandr Tymoshenko }
9294bc2117SOleksandr Tymoshenko
9394bc2117SOleksandr Tymoshenko static int
imx_clk_mux_set_mux(struct clknode * clk,int idx)9494bc2117SOleksandr Tymoshenko imx_clk_mux_set_mux(struct clknode *clk, int idx)
9594bc2117SOleksandr Tymoshenko {
9694bc2117SOleksandr Tymoshenko uint32_t reg;
9794bc2117SOleksandr Tymoshenko struct imx_clk_mux_sc *sc;
9894bc2117SOleksandr Tymoshenko int rv;
9994bc2117SOleksandr Tymoshenko
10094bc2117SOleksandr Tymoshenko sc = clknode_get_softc(clk);
10194bc2117SOleksandr Tymoshenko
10294bc2117SOleksandr Tymoshenko DEVICE_LOCK(clk);
10394bc2117SOleksandr Tymoshenko rv = MD4(clk, sc->offset, sc->mask << sc->shift,
10494bc2117SOleksandr Tymoshenko ((idx & sc->mask) << sc->shift));
10594bc2117SOleksandr Tymoshenko if (rv != 0) {
10694bc2117SOleksandr Tymoshenko DEVICE_UNLOCK(clk);
10794bc2117SOleksandr Tymoshenko return (rv);
10894bc2117SOleksandr Tymoshenko }
10994bc2117SOleksandr Tymoshenko RD4(clk, sc->offset, ®);
11094bc2117SOleksandr Tymoshenko DEVICE_UNLOCK(clk);
11194bc2117SOleksandr Tymoshenko
11294bc2117SOleksandr Tymoshenko return(0);
11394bc2117SOleksandr Tymoshenko }
11494bc2117SOleksandr Tymoshenko
11594bc2117SOleksandr Tymoshenko int
imx_clk_mux_register(struct clkdom * clkdom,struct imx_clk_mux_def * clkdef)11694bc2117SOleksandr Tymoshenko imx_clk_mux_register(struct clkdom *clkdom, struct imx_clk_mux_def *clkdef)
11794bc2117SOleksandr Tymoshenko {
11894bc2117SOleksandr Tymoshenko struct clknode *clk;
11994bc2117SOleksandr Tymoshenko struct imx_clk_mux_sc *sc;
12094bc2117SOleksandr Tymoshenko
12194bc2117SOleksandr Tymoshenko clk = clknode_create(clkdom, &imx_clk_mux_class, &clkdef->clkdef);
12294bc2117SOleksandr Tymoshenko if (clk == NULL)
12394bc2117SOleksandr Tymoshenko return (1);
12494bc2117SOleksandr Tymoshenko
12594bc2117SOleksandr Tymoshenko sc = clknode_get_softc(clk);
12694bc2117SOleksandr Tymoshenko sc->offset = clkdef->offset;
12794bc2117SOleksandr Tymoshenko sc->shift = clkdef->shift;
12894bc2117SOleksandr Tymoshenko sc->mask = (1 << clkdef->width) - 1;
12994bc2117SOleksandr Tymoshenko sc->mux_flags = clkdef->mux_flags;
13094bc2117SOleksandr Tymoshenko
13194bc2117SOleksandr Tymoshenko clknode_register(clkdom, clk);
13294bc2117SOleksandr Tymoshenko return (0);
13394bc2117SOleksandr Tymoshenko }
134