1*6e54367aSthorpej /* $NetBSD: bcm2835_aux.c,v 1.3 2021/01/27 03:10:19 thorpej Exp $ */
2ee91b1e5Sskrll
3ee91b1e5Sskrll /*-
4ee91b1e5Sskrll * Copyright (c) 2017 Jared D. McNeill <jmcneill@invisible.ca>
5ee91b1e5Sskrll * All rights reserved.
6ee91b1e5Sskrll *
7ee91b1e5Sskrll * Redistribution and use in source and binary forms, with or without
8ee91b1e5Sskrll * modification, are permitted provided that the following conditions
9ee91b1e5Sskrll * are met:
10ee91b1e5Sskrll * 1. Redistributions of source code must retain the above copyright
11ee91b1e5Sskrll * notice, this list of conditions and the following disclaimer.
12ee91b1e5Sskrll * 2. Redistributions in binary form must reproduce the above copyright
13ee91b1e5Sskrll * notice, this list of conditions and the following disclaimer in the
14ee91b1e5Sskrll * documentation and/or other materials provided with the distribution.
15ee91b1e5Sskrll *
16ee91b1e5Sskrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ee91b1e5Sskrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ee91b1e5Sskrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ee91b1e5Sskrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ee91b1e5Sskrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21ee91b1e5Sskrll * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22ee91b1e5Sskrll * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23ee91b1e5Sskrll * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24ee91b1e5Sskrll * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ee91b1e5Sskrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ee91b1e5Sskrll * SUCH DAMAGE.
27ee91b1e5Sskrll */
28ee91b1e5Sskrll
29ee91b1e5Sskrll #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_aux.c,v 1.3 2021/01/27 03:10:19 thorpej Exp $");
31ee91b1e5Sskrll
32ee91b1e5Sskrll #include <sys/param.h>
33ee91b1e5Sskrll #include <sys/systm.h>
34ee91b1e5Sskrll #include <sys/device.h>
35ee91b1e5Sskrll #include <sys/kmem.h>
36ee91b1e5Sskrll #include <sys/bus.h>
37ee91b1e5Sskrll
38ee91b1e5Sskrll #include <dev/clk/clk_backend.h>
39ee91b1e5Sskrll
40ee91b1e5Sskrll #include <dev/fdt/fdtvar.h>
41ee91b1e5Sskrll
42ee91b1e5Sskrll /* Registers */
43ee91b1e5Sskrll #define BCMAUX_AUXIRQ_REG 0x00
44ee91b1e5Sskrll #define BCMAUX_AUXENB_REG 0x04
45ee91b1e5Sskrll
46ee91b1e5Sskrll /* Clock IDs */
47ee91b1e5Sskrll #define BCMAUX_CLOCK_UART 0
48ee91b1e5Sskrll #define BCMAUX_CLOCK_SPI1 1
49ee91b1e5Sskrll #define BCMAUX_CLOCK_SPI2 2
50ee91b1e5Sskrll #define BCMAUX_NCLOCK 3
51ee91b1e5Sskrll
52ee91b1e5Sskrll static int bcmaux_match(device_t, cfdata_t, void *);
53ee91b1e5Sskrll static void bcmaux_attach(device_t, device_t, void *);
54ee91b1e5Sskrll
5551b425efSaymeric static struct clk *bcmaux_decode(device_t, int, const void *, size_t);
56ee91b1e5Sskrll
57ee91b1e5Sskrll static const struct fdtbus_clock_controller_func bcmaux_fdt_funcs = {
58ee91b1e5Sskrll .decode = bcmaux_decode
59ee91b1e5Sskrll };
60ee91b1e5Sskrll
61ee91b1e5Sskrll static struct clk *bcmaux_get(void *, const char *);
62ee91b1e5Sskrll static void bcmaux_put(void *, struct clk *);
63ee91b1e5Sskrll static u_int bcmaux_get_rate(void *, struct clk *);
64ee91b1e5Sskrll static int bcmaux_enable(void *, struct clk *);
65ee91b1e5Sskrll static int bcmaux_disable(void *, struct clk *);
66ee91b1e5Sskrll
67ee91b1e5Sskrll static const struct clk_funcs bcmaux_clk_funcs = {
68ee91b1e5Sskrll .get = bcmaux_get,
69ee91b1e5Sskrll .put = bcmaux_put,
70ee91b1e5Sskrll .get_rate = bcmaux_get_rate,
71ee91b1e5Sskrll .enable = bcmaux_enable,
72ee91b1e5Sskrll .disable = bcmaux_disable,
73ee91b1e5Sskrll };
74ee91b1e5Sskrll
75ee91b1e5Sskrll struct bcmaux_clk {
76ee91b1e5Sskrll struct clk base;
77ee91b1e5Sskrll uint32_t mask;
78ee91b1e5Sskrll };
79ee91b1e5Sskrll
80ee91b1e5Sskrll struct bcmaux_softc {
81ee91b1e5Sskrll device_t sc_dev;
82ee91b1e5Sskrll int sc_phandle;
83ee91b1e5Sskrll bus_space_tag_t sc_bst;
84ee91b1e5Sskrll bus_space_handle_t sc_bsh;
85ee91b1e5Sskrll
86ee91b1e5Sskrll struct clk *sc_pclk;
87ee91b1e5Sskrll
88ee91b1e5Sskrll struct clk_domain sc_clkdom;
89ee91b1e5Sskrll struct bcmaux_clk sc_clk[BCMAUX_NCLOCK];
90ee91b1e5Sskrll };
91ee91b1e5Sskrll
92ee91b1e5Sskrll #define BCMAUX_READ(sc, reg) \
93ee91b1e5Sskrll bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
94ee91b1e5Sskrll #define BCMAUX_WRITE(sc, reg, val) \
95ee91b1e5Sskrll bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
96ee91b1e5Sskrll
97ee91b1e5Sskrll CFATTACH_DECL_NEW(bcmaux_fdt, sizeof(struct bcmaux_softc),
98ee91b1e5Sskrll bcmaux_match, bcmaux_attach, NULL, NULL);
99ee91b1e5Sskrll
100*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
101*6e54367aSthorpej { .compat = "brcm,bcm2835-aux" },
102*6e54367aSthorpej DEVICE_COMPAT_EOL
103*6e54367aSthorpej };
104*6e54367aSthorpej
105ee91b1e5Sskrll static int
bcmaux_match(device_t parent,cfdata_t cf,void * aux)106ee91b1e5Sskrll bcmaux_match(device_t parent, cfdata_t cf, void *aux)
107ee91b1e5Sskrll {
108ee91b1e5Sskrll const struct fdt_attach_args *faa = aux;
109ee91b1e5Sskrll
110*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
111ee91b1e5Sskrll }
112ee91b1e5Sskrll
113ee91b1e5Sskrll static void
bcmaux_attach(device_t parent,device_t self,void * aux)114ee91b1e5Sskrll bcmaux_attach(device_t parent, device_t self, void *aux)
115ee91b1e5Sskrll {
116ee91b1e5Sskrll struct bcmaux_softc * const sc = device_private(self);
117ee91b1e5Sskrll const struct fdt_attach_args *faa = aux;
118ee91b1e5Sskrll const int phandle = faa->faa_phandle;
119ee91b1e5Sskrll bus_addr_t addr;
120ee91b1e5Sskrll bus_size_t size;
121ee91b1e5Sskrll
122ee91b1e5Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
123ee91b1e5Sskrll aprint_error(": couldn't get registers\n");
124ee91b1e5Sskrll return;
125ee91b1e5Sskrll }
126ee91b1e5Sskrll
127ee91b1e5Sskrll sc->sc_dev = self;
128ee91b1e5Sskrll sc->sc_phandle = phandle;
129ee91b1e5Sskrll sc->sc_clkdom.funcs = &bcmaux_clk_funcs;
130ee91b1e5Sskrll sc->sc_clkdom.priv = sc;
131ee91b1e5Sskrll sc->sc_pclk = fdtbus_clock_get_index(phandle, 0);
132ee91b1e5Sskrll if (sc->sc_pclk == NULL) {
133ee91b1e5Sskrll aprint_error(": couldn't get parent clock\n");
134ee91b1e5Sskrll return;
135ee91b1e5Sskrll }
136ee91b1e5Sskrll sc->sc_bst = faa->faa_bst;
137ee91b1e5Sskrll if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
138ee91b1e5Sskrll aprint_error(": couldn't map registers\n");
139ee91b1e5Sskrll return;
140ee91b1e5Sskrll }
141ee91b1e5Sskrll
142ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_UART].base.domain = &sc->sc_clkdom;
143ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_UART].base.name = "aux_uart";
144ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_UART].mask = __BIT(0);
145ee91b1e5Sskrll
146ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_SPI1].base.domain = &sc->sc_clkdom;
147ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_SPI1].base.name = "aux_spi1";
148ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_SPI1].mask = __BIT(1);
149ee91b1e5Sskrll
150ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_SPI2].base.domain = &sc->sc_clkdom;
151ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_SPI2].base.name = "aux_spi2";
152ee91b1e5Sskrll sc->sc_clk[BCMAUX_CLOCK_SPI2].mask = __BIT(2);
153ee91b1e5Sskrll
154ee91b1e5Sskrll aprint_naive("\n");
155ee91b1e5Sskrll aprint_normal("\n");
156ee91b1e5Sskrll
157ee91b1e5Sskrll fdtbus_register_clock_controller(self, phandle, &bcmaux_fdt_funcs);
158ee91b1e5Sskrll }
159ee91b1e5Sskrll
160ee91b1e5Sskrll static struct clk *
bcmaux_decode(device_t dev,int cc_phandle,const void * data,size_t len)16151b425efSaymeric bcmaux_decode(device_t dev, int cc_phandle, const void *data, size_t len)
162ee91b1e5Sskrll {
163ee91b1e5Sskrll struct bcmaux_softc * const sc = device_private(dev);
164ee91b1e5Sskrll u_int clkid;
165ee91b1e5Sskrll
166ee91b1e5Sskrll if (len != 4)
167ee91b1e5Sskrll return NULL;
168ee91b1e5Sskrll
169ee91b1e5Sskrll clkid = be32dec(data);
170ee91b1e5Sskrll if (clkid >= BCMAUX_NCLOCK)
171ee91b1e5Sskrll return NULL;
172ee91b1e5Sskrll
173ee91b1e5Sskrll return &sc->sc_clk[clkid].base;
174ee91b1e5Sskrll }
175ee91b1e5Sskrll
176ee91b1e5Sskrll static struct clk *
bcmaux_get(void * priv,const char * name)177ee91b1e5Sskrll bcmaux_get(void *priv, const char *name)
178ee91b1e5Sskrll {
179ee91b1e5Sskrll struct bcmaux_softc * const sc = priv;
180ee91b1e5Sskrll
181ee91b1e5Sskrll for (size_t i = 0; i < BCMAUX_NCLOCK; i++) {
182ee91b1e5Sskrll if (strcmp(name, sc->sc_clk[i].base.name) == 0)
183ee91b1e5Sskrll return &sc->sc_clk[i].base;
184ee91b1e5Sskrll }
185ee91b1e5Sskrll
186ee91b1e5Sskrll return NULL;
187ee91b1e5Sskrll }
188ee91b1e5Sskrll
189ee91b1e5Sskrll static void
bcmaux_put(void * priv,struct clk * clk)190ee91b1e5Sskrll bcmaux_put(void *priv, struct clk *clk)
191ee91b1e5Sskrll {
192ee91b1e5Sskrll }
193ee91b1e5Sskrll
194ee91b1e5Sskrll static u_int
bcmaux_get_rate(void * priv,struct clk * clk)195ee91b1e5Sskrll bcmaux_get_rate(void *priv, struct clk *clk)
196ee91b1e5Sskrll {
197ee91b1e5Sskrll struct bcmaux_softc * const sc = priv;
198ee91b1e5Sskrll
199ee91b1e5Sskrll return clk_get_rate(sc->sc_pclk);
200ee91b1e5Sskrll }
201ee91b1e5Sskrll
202ee91b1e5Sskrll static int
bcmaux_enable(void * priv,struct clk * clk)203ee91b1e5Sskrll bcmaux_enable(void *priv, struct clk *clk)
204ee91b1e5Sskrll {
205ee91b1e5Sskrll struct bcmaux_softc * const sc = priv;
206ee91b1e5Sskrll struct bcmaux_clk *auxclk = (struct bcmaux_clk *)clk;
207ee91b1e5Sskrll uint32_t val;
208ee91b1e5Sskrll
209ee91b1e5Sskrll val = BCMAUX_READ(sc, BCMAUX_AUXENB_REG);
210ee91b1e5Sskrll val |= auxclk->mask;
211ee91b1e5Sskrll BCMAUX_WRITE(sc, BCMAUX_AUXENB_REG, val);
212ee91b1e5Sskrll
213ee91b1e5Sskrll return 0;
214ee91b1e5Sskrll }
215ee91b1e5Sskrll
216ee91b1e5Sskrll static int
bcmaux_disable(void * priv,struct clk * clk)217ee91b1e5Sskrll bcmaux_disable(void *priv, struct clk *clk)
218ee91b1e5Sskrll {
219ee91b1e5Sskrll struct bcmaux_softc * const sc = priv;
220ee91b1e5Sskrll struct bcmaux_clk *auxclk = (struct bcmaux_clk *)clk;
221ee91b1e5Sskrll uint32_t val;
222ee91b1e5Sskrll
223ee91b1e5Sskrll val = BCMAUX_READ(sc, BCMAUX_AUXENB_REG);
224ee91b1e5Sskrll val &= ~auxclk->mask;
225ee91b1e5Sskrll BCMAUX_WRITE(sc, BCMAUX_AUXENB_REG, val);
226ee91b1e5Sskrll
227ee91b1e5Sskrll return 0;
228ee91b1e5Sskrll }
229