1/*- 2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD$"); 29 30 31#include <sys/param.h> 32#include <sys/conf.h> 33#include <sys/bus.h> 34#include <sys/kernel.h> 35#include <sys/systm.h> 36 37#include <machine/bus.h> 38 39#include <dev/extres/clk/clk_gate.h> 40 41#include "clkdev_if.h" 42 43#define WR4(_clk, off, val) \ 44 CLKDEV_WRITE_4(clknode_get_device(_clk), off, val) 45#define RD4(_clk, off, val) \ 46 CLKDEV_READ_4(clknode_get_device(_clk), off, val) 47#define MD4(_clk, off, clr, set ) \ 48 CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set) 49#define DEVICE_LOCK(_clk) \ 50 CLKDEV_DEVICE_LOCK(clknode_get_device(_clk)) 51#define DEVICE_UNLOCK(_clk) \ 52 CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk)) 53 54static int clknode_gate_init(struct clknode *clk, device_t dev); 55static int clknode_gate_set_gate(struct clknode *clk, bool enable); 56struct clknode_gate_sc { 57 uint32_t offset; 58 uint32_t shift; 59 uint32_t mask; 60 uint32_t on_value; 61 uint32_t off_value; 62 int gate_flags; 63 bool ungated; 64}; 65 66static clknode_method_t clknode_gate_methods[] = { 67 /* Device interface */ 68 CLKNODEMETHOD(clknode_init, clknode_gate_init), 69 CLKNODEMETHOD(clknode_set_gate, clknode_gate_set_gate), 70 CLKNODEMETHOD_END 71}; 72DEFINE_CLASS_1(clknode_gate, clknode_gate_class, clknode_gate_methods, 73 sizeof(struct clknode_gate_sc), clknode_class); 74 75static int 76clknode_gate_init(struct clknode *clk, device_t dev) 77{ 78 uint32_t reg; 79 struct clknode_gate_sc *sc; 80 int rv; 81 82 sc = clknode_get_softc(clk); 83 DEVICE_LOCK(clk); 84 rv = RD4(clk, sc->offset, ®); 85 DEVICE_UNLOCK(clk); 86 if (rv != 0) 87 return (rv); 88 reg = (reg >> sc->shift) & sc->mask; 89 sc->ungated = reg == sc->on_value ? 1 : 0; 90 clknode_init_parent_idx(clk, 0); 91 return(0); 92} 93 94static int 95clknode_gate_set_gate(struct clknode *clk, bool enable) 96{ 97 uint32_t reg; 98 struct clknode_gate_sc *sc; 99 int rv; 100 101 sc = clknode_get_softc(clk); 102 sc->ungated = enable; 103 DEVICE_LOCK(clk); 104 rv = MD4(clk, sc->offset, sc->mask << sc->shift, 105 (sc->ungated ? sc->on_value : sc->off_value) << sc->shift); 106 if (rv != 0) { 107 DEVICE_UNLOCK(clk); 108 return (rv); 109 } 110 RD4(clk, sc->offset, ®); 111 DEVICE_UNLOCK(clk); 112 return(0); 113} 114 115int 116clknode_gate_register(struct clkdom *clkdom, struct clk_gate_def *clkdef) 117{ 118 struct clknode *clk; 119 struct clknode_gate_sc *sc; 120 121 clk = clknode_create(clkdom, &clknode_gate_class, &clkdef->clkdef); 122 if (clk == NULL) 123 return (1); 124 125 sc = clknode_get_softc(clk); 126 sc->offset = clkdef->offset; 127 sc->shift = clkdef->shift; 128 sc->mask = clkdef->mask; 129 sc->on_value = clkdef->on_value; 130 sc->off_value = clkdef->off_value; 131 sc->gate_flags = clkdef->gate_flags; 132 133 clknode_register(clkdom, clk); 134 return (0); 135} 136