xref: /openbsd/sys/dev/fdt/hireset.c (revision 4cfece93)
1 /*	$OpenBSD: hireset.c,v 1.1 2018/08/27 14:12:59 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2018 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/intr.h>
23 #include <machine/bus.h>
24 #include <machine/fdt.h>
25 
26 #include <dev/ofw/openfirm.h>
27 #include <dev/ofw/ofw_clock.h>
28 #include <dev/ofw/ofw_misc.h>
29 #include <dev/ofw/fdt.h>
30 
31 struct hireset_softc {
32 	struct device		sc_dev;
33 	uint32_t		sc_rst_syscon;
34 
35 	struct reset_device	sc_rd;
36 };
37 
38 int hireset_match(struct device *, void *, void *);
39 void hireset_attach(struct device *, struct device *, void *);
40 
41 struct cfattach	hireset_ca = {
42 	sizeof (struct hireset_softc), hireset_match, hireset_attach
43 };
44 
45 struct cfdriver hireset_cd = {
46 	NULL, "hireset", DV_DULL
47 };
48 
49 void	hireset_reset(void *, uint32_t *, int);
50 
51 int
52 hireset_match(struct device *parent, void *match, void *aux)
53 {
54 	struct fdt_attach_args *faa = aux;
55 
56 	return OF_is_compatible(faa->fa_node, "hisilicon,hi3660-reset");
57 }
58 
59 void
60 hireset_attach(struct device *parent, struct device *self, void *aux)
61 {
62 	struct hireset_softc *sc = (struct hireset_softc *)self;
63 	struct fdt_attach_args *faa = aux;
64 
65 	sc->sc_rst_syscon = OF_getpropint(faa->fa_node, "hisi,rst-syscon", 0);
66 
67 	printf("\n");
68 
69 	sc->sc_rd.rd_node = faa->fa_node;
70 	sc->sc_rd.rd_cookie = sc;
71 	sc->sc_rd.rd_reset = hireset_reset;
72 	reset_register(&sc->sc_rd);
73 }
74 
75 void
76 hireset_reset(void *cookie, uint32_t *cells, int on)
77 {
78 	struct hireset_softc *sc = cookie;
79 	struct regmap *rm;
80 	uint32_t offset = cells[0];
81 	uint32_t bit = cells[1];
82 
83 	rm = regmap_byphandle(sc->sc_rst_syscon);
84 	if (rm == NULL) {
85 		printf("%s: can't find regmap\n", sc->sc_dev.dv_xname);
86 		return;
87 	}
88 
89 	if (on)
90 		regmap_write_4(rm, offset + 0, (1 << bit));
91 	else
92 		regmap_write_4(rm, offset + 4, (1 << bit));
93 }
94