xref: /openbsd/sys/arch/armv7/marvell/mvagc.c (revision 73471bf0)
1 /* $OpenBSD: mvagc.c,v 1.2 2021/10/24 17:52:27 mpi Exp $ */
2 /*
3  * Copyright (c) 2016 Patrick Wildt <patrick@blueri.se>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 #include <machine/bus.h>
22 #include <machine/fdt.h>
23 
24 #include <dev/ofw/openfirm.h>
25 #include <dev/ofw/ofw_clock.h>
26 #include <dev/ofw/fdt.h>
27 
28 #define HREAD4(sc, reg)							\
29 	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
30 #define HWRITE4(sc, reg, val)						\
31 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
32 #define HSET4(sc, reg, bits)						\
33 	HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
34 #define HCLR4(sc, reg, bits)						\
35 	HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
36 
37 struct mvagc_softc {
38 	struct device		 sc_dev;
39 	bus_space_tag_t		 sc_iot;
40 	bus_space_handle_t	 sc_ioh;
41 	int			 sc_node;
42 	struct clock_device	 sc_cd;
43 };
44 
45 int	 mvagc_match(struct device *, void *, void *);
46 void	 mvagc_attach(struct device *, struct device *, void *);
47 
48 void	 mvagc_enable(void *, uint32_t *, int);
49 uint32_t mvagc_gen_get_frequency(void *, uint32_t *);
50 
51 const struct cfattach mvagc_ca = {
52 	sizeof (struct mvagc_softc), mvagc_match, mvagc_attach
53 };
54 
55 struct cfdriver mvagc_cd = {
56 	NULL, "mvagc", DV_DULL
57 };
58 
59 int
60 mvagc_match(struct device *parent, void *cfdata, void *aux)
61 {
62 	struct fdt_attach_args *faa = aux;
63 
64 	return (OF_is_compatible(faa->fa_node,
65 	    "marvell,armada-380-gating-clock"));
66 }
67 
68 void
69 mvagc_attach(struct device *parent, struct device *self, void *args)
70 {
71 	struct mvagc_softc *sc = (struct mvagc_softc *)self;
72 	struct fdt_attach_args *faa = args;
73 
74 	sc->sc_node = faa->fa_node;
75 	sc->sc_iot = faa->fa_iot;
76 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
77 	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
78 		panic("%s: bus_space_map failed!", __func__);
79 
80 	printf("\n");
81 
82 	sc->sc_cd.cd_node = sc->sc_node;
83 	sc->sc_cd.cd_cookie = sc;
84 	sc->sc_cd.cd_get_frequency = mvagc_gen_get_frequency;
85 	sc->sc_cd.cd_enable = mvagc_enable;
86 	clock_register(&sc->sc_cd);
87 }
88 
89 /*
90  * A "generic" function that simply gets the clock frequency from the
91  * parent clock.  Useful for clock gating devices that don't scale
92  * their clocks.
93  */
94 uint32_t
95 mvagc_gen_get_frequency(void *cookie, uint32_t *cells)
96 {
97 	struct mvagc_softc *sc = cookie;
98 
99 	return clock_get_frequency(sc->sc_node, NULL);
100 }
101 
102 void
103 mvagc_enable(void *cookie, uint32_t *cells, int on)
104 {
105 	struct mvagc_softc *sc = cookie;
106 	uint32_t id = cells[0];
107 
108 	if (id >= 32)
109 		return;
110 
111 	if (on)
112 		HSET4(sc, 0, (1 << id));
113 	else
114 		HCLR4(sc, 0, (1 << id));
115 }
116