xref: /openbsd-src/sys/arch/riscv64/dev/sfcc.c (revision f1dd7b858388b4a23f4f67a4957ec5ff656ebbe8)
1 /*	$OpenBSD: sfcc.c,v 1.1 2021/05/05 19:26:51 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(vaddr_t, vsize_t);
54 
55 int
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 }
62 
63 void
64 sfcc_attach(struct device *parent, struct device *self, void *aux)
65 {
66 	struct sfcc_softc *sc = (struct sfcc_softc *)self;
67 	struct fdt_attach_args *faa = aux;
68 
69 	if (faa->fa_nreg < 1) {
70 		printf(": no registers\n");
71 		return;
72 	}
73 
74 	sc->sc_iot = faa->fa_iot;
75 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
76 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
77 		printf(": can't map registers\n");
78 		return;
79 	}
80 
81 	printf("\n");
82 
83 	sc->sc_line_size = OF_getpropint(faa->fa_node, "cache-block-size", 64);
84 	sfcc_sc = sc;
85 
86 	/*
87 	 * Cache lines can only be flushed, so we use the same
88 	 * operation everywhere.
89 	 */
90 	cpu_dcache_wbinv_range = sfcc_cache_wbinv_range;
91 	cpu_dcache_inv_range = sfcc_cache_wbinv_range;
92 	cpu_dcache_wb_range = sfcc_cache_wbinv_range;
93 }
94 
95 void
96 sfcc_cache_wbinv_range(paddr_t pa, paddr_t len)
97 {
98 	struct sfcc_softc *sc = sfcc_sc;
99 
100 	len += pa & (sc->sc_line_size - 1);
101 	pa &= ~(sc->sc_line_size - 1);
102 
103 	__asm volatile ("fence iorw,iorw" ::: "memory");
104 	while (len > 0) {
105 		bus_space_write_8(sc->sc_iot, sc->sc_ioh, SFCC_FLUSH64, pa);
106 		__asm volatile ("fence iorw,iorw" ::: "memory");
107 		pa += sc->sc_line_size;
108 		len -= sc->sc_line_size;
109 	}
110 }
111