xref: /openbsd-src/sys/arch/riscv64/dev/sfcc.c (revision a3051b0765ef7e23e858ba5a4d944cb86a56081e)
1 /*	$OpenBSD: sfcc.c,v 1.4 2022/12/27 21:13:25 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2021 Mark Kettenis <kettenis@openbsd.org>
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 
22 #include <machine/bus.h>
23 #include <machine/cpufunc.h>
24 #include <machine/fdt.h>
25 
26 #include <dev/ofw/openfirm.h>
27 #include <dev/ofw/fdt.h>
28 
29 /* Registers. */
30 #define SFCC_FLUSH64	0x200
31 
32 struct sfcc_softc {
33 	struct device		sc_dev;
34 	bus_space_tag_t		sc_iot;
35 	bus_space_handle_t	sc_ioh;
36 
37 	uint32_t		sc_line_size;
38 };
39 
40 struct sfcc_softc *sfcc_sc;
41 
42 int	sfcc_match(struct device *, void *, void *);
43 void	sfcc_attach(struct device *, struct device *, void *);
44 
45 const struct cfattach sfcc_ca = {
46 	sizeof (struct sfcc_softc), sfcc_match, sfcc_attach
47 };
48 
49 struct cfdriver sfcc_cd = {
50 	NULL, "sfcc", DV_DULL
51 };
52 
53 void	sfcc_cache_wbinv_range(paddr_t, psize_t);
54 
55 int
sfcc_match(struct device * parent,void * match,void * aux)56 sfcc_match(struct device *parent, void *match, void *aux)
57 {
58 	struct fdt_attach_args *faa = aux;
59 
60 	return (OF_is_compatible(faa->fa_node, "sifive,fu540-c000-ccache") ||
61 	    OF_is_compatible(faa->fa_node, "starfive,jh7100-ccache"));
62 }
63 
64 void
sfcc_attach(struct device * parent,struct device * self,void * aux)65 sfcc_attach(struct device *parent, struct device *self, void *aux)
66 {
67 	struct sfcc_softc *sc = (struct sfcc_softc *)self;
68 	struct fdt_attach_args *faa = aux;
69 
70 	if (faa->fa_nreg < 1) {
71 		printf(": no registers\n");
72 		return;
73 	}
74 
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 		printf(": can't map registers\n");
79 		return;
80 	}
81 
82 	printf("\n");
83 
84 	sc->sc_line_size = OF_getpropint(faa->fa_node, "cache-block-size", 64);
85 	sfcc_sc = sc;
86 
87 	/*
88 	 * Cache lines can only be flushed, so we use the same
89 	 * operation everywhere.
90 	 */
91 	cpu_dcache_wbinv_range = sfcc_cache_wbinv_range;
92 	cpu_dcache_inv_range = sfcc_cache_wbinv_range;
93 	cpu_dcache_wb_range = sfcc_cache_wbinv_range;
94 }
95 
96 void
sfcc_cache_wbinv_range(paddr_t pa,psize_t len)97 sfcc_cache_wbinv_range(paddr_t pa, psize_t len)
98 {
99 	struct sfcc_softc *sc = sfcc_sc;
100 	paddr_t end, mask;
101 
102 	mask = sc->sc_line_size - 1;
103 	end = (pa + len + mask) & ~mask;
104 	pa &= ~mask;
105 
106 	__asm volatile ("fence iorw,iorw" ::: "memory");
107 	while (pa != end) {
108 		bus_space_write_8(sc->sc_iot, sc->sc_ioh, SFCC_FLUSH64, pa);
109 		__asm volatile ("fence iorw,iorw" ::: "memory");
110 		pa += sc->sc_line_size;
111 	}
112 }
113