xref: /netbsd-src/sys/dev/fdt/syscon.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: syscon.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $ */
25b0333ebSjmcneill 
35b0333ebSjmcneill /*-
45b0333ebSjmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
55b0333ebSjmcneill  * All rights reserved.
65b0333ebSjmcneill  *
75b0333ebSjmcneill  * Redistribution and use in source and binary forms, with or without
85b0333ebSjmcneill  * modification, are permitted provided that the following conditions
95b0333ebSjmcneill  * are met:
105b0333ebSjmcneill  * 1. Redistributions of source code must retain the above copyright
115b0333ebSjmcneill  *    notice, this list of conditions and the following disclaimer.
125b0333ebSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
135b0333ebSjmcneill  *    notice, this list of conditions and the following disclaimer in the
145b0333ebSjmcneill  *    documentation and/or other materials provided with the distribution.
155b0333ebSjmcneill  *
165b0333ebSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175b0333ebSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185b0333ebSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
195b0333ebSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
205b0333ebSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
215b0333ebSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
225b0333ebSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
235b0333ebSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
245b0333ebSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
255b0333ebSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265b0333ebSjmcneill  * SUCH DAMAGE.
275b0333ebSjmcneill  */
285b0333ebSjmcneill 
295b0333ebSjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: syscon.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $");
315b0333ebSjmcneill 
325b0333ebSjmcneill #include <sys/param.h>
335b0333ebSjmcneill #include <sys/bus.h>
345b0333ebSjmcneill #include <sys/device.h>
355b0333ebSjmcneill #include <sys/systm.h>
365b0333ebSjmcneill #include <sys/sysctl.h>
375b0333ebSjmcneill #include <sys/kmem.h>
385b0333ebSjmcneill #include <sys/gpio.h>
395b0333ebSjmcneill 
405b0333ebSjmcneill #include <dev/fdt/fdtvar.h>
415b0333ebSjmcneill #include <dev/fdt/syscon.h>
425b0333ebSjmcneill 
435b0333ebSjmcneill struct syscon_softc {
445b0333ebSjmcneill 	device_t		sc_dev;
455b0333ebSjmcneill 	bus_space_tag_t		sc_bst;
465b0333ebSjmcneill 	bus_space_handle_t	sc_bsh;
475b0333ebSjmcneill 	kmutex_t		sc_lock;
485b0333ebSjmcneill 
495b0333ebSjmcneill 	struct syscon		sc_syscon;
505b0333ebSjmcneill };
515b0333ebSjmcneill 
525b0333ebSjmcneill static int	syscon_match(device_t, cfdata_t, void *);
535b0333ebSjmcneill static void	syscon_attach(device_t, device_t, void *);
545b0333ebSjmcneill 
55*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
56*6e54367aSthorpej 	{ .compat = "syscon" },
57*6e54367aSthorpej 	{ .compat = "simple-mfd" },
58*6e54367aSthorpej 	DEVICE_COMPAT_EOL
595b0333ebSjmcneill };
605b0333ebSjmcneill 
615b0333ebSjmcneill CFATTACH_DECL_NEW(syscon, sizeof(struct syscon_softc),
625b0333ebSjmcneill 	syscon_match, syscon_attach, NULL, NULL);
635b0333ebSjmcneill 
645b0333ebSjmcneill static void
syscon_generic_lock(void * priv)655b0333ebSjmcneill syscon_generic_lock(void *priv)
665b0333ebSjmcneill {
675b0333ebSjmcneill 	struct syscon_softc * const sc = priv;
685b0333ebSjmcneill 
695b0333ebSjmcneill 	mutex_enter(&sc->sc_lock);
705b0333ebSjmcneill }
715b0333ebSjmcneill 
725b0333ebSjmcneill static void
syscon_generic_unlock(void * priv)735b0333ebSjmcneill syscon_generic_unlock(void *priv)
745b0333ebSjmcneill {
755b0333ebSjmcneill 	struct syscon_softc * const sc = priv;
765b0333ebSjmcneill 
775b0333ebSjmcneill 	mutex_exit(&sc->sc_lock);
785b0333ebSjmcneill }
795b0333ebSjmcneill 
805b0333ebSjmcneill static uint32_t
syscon_generic_read_4(void * priv,bus_size_t reg)815b0333ebSjmcneill syscon_generic_read_4(void *priv, bus_size_t reg)
825b0333ebSjmcneill {
835b0333ebSjmcneill 	struct syscon_softc * const sc = priv;
845b0333ebSjmcneill 
855b0333ebSjmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
865b0333ebSjmcneill 
875b0333ebSjmcneill 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, reg);
885b0333ebSjmcneill }
895b0333ebSjmcneill 
905b0333ebSjmcneill static void
syscon_generic_write_4(void * priv,bus_size_t reg,uint32_t val)915b0333ebSjmcneill syscon_generic_write_4(void *priv, bus_size_t reg, uint32_t val)
925b0333ebSjmcneill {
935b0333ebSjmcneill 	struct syscon_softc * const sc = priv;
945b0333ebSjmcneill 
955b0333ebSjmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
965b0333ebSjmcneill 
975b0333ebSjmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, val);
985b0333ebSjmcneill }
995b0333ebSjmcneill 
1005b0333ebSjmcneill static int
syscon_match(device_t parent,cfdata_t cf,void * aux)1015b0333ebSjmcneill syscon_match(device_t parent, cfdata_t cf, void *aux)
1025b0333ebSjmcneill {
1035b0333ebSjmcneill 	struct fdt_attach_args * const faa = aux;
1045b0333ebSjmcneill 
105*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
1065b0333ebSjmcneill }
1075b0333ebSjmcneill 
1085b0333ebSjmcneill static void
syscon_attach(device_t parent,device_t self,void * aux)1095b0333ebSjmcneill syscon_attach(device_t parent, device_t self, void *aux)
1105b0333ebSjmcneill {
1115b0333ebSjmcneill 	struct syscon_softc * const sc = device_private(self);
1125b0333ebSjmcneill 	struct fdt_attach_args * const faa = aux;
1135b0333ebSjmcneill 	const int phandle = faa->faa_phandle;
1145b0333ebSjmcneill 	bus_addr_t addr;
1155b0333ebSjmcneill 	bus_size_t size;
116a0504a86Sjmcneill 	int child;
1175b0333ebSjmcneill 
1185b0333ebSjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1195b0333ebSjmcneill 		aprint_error(": couldn't get registers\n");
1205b0333ebSjmcneill 		return;
1215b0333ebSjmcneill 	}
1225b0333ebSjmcneill 
1235b0333ebSjmcneill 	sc->sc_dev = self;
1245b0333ebSjmcneill 	sc->sc_bst = faa->faa_bst;
1255b0333ebSjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1265b0333ebSjmcneill 		aprint_error(": couldn't map registers\n");
1275b0333ebSjmcneill 		return;
1285b0333ebSjmcneill 	}
1295b0333ebSjmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
1305b0333ebSjmcneill 	sc->sc_syscon.priv = sc;
1315b0333ebSjmcneill 	sc->sc_syscon.lock = syscon_generic_lock;
1325b0333ebSjmcneill 	sc->sc_syscon.unlock = syscon_generic_unlock;
1335b0333ebSjmcneill 	sc->sc_syscon.read_4 = syscon_generic_read_4;
1345b0333ebSjmcneill 	sc->sc_syscon.write_4 = syscon_generic_write_4;
1355b0333ebSjmcneill 
1365b0333ebSjmcneill 	aprint_naive("\n");
1375b0333ebSjmcneill 	aprint_normal(": System Controller Registers\n");
1385b0333ebSjmcneill 
1395b0333ebSjmcneill 	fdtbus_register_syscon(self, phandle, &sc->sc_syscon);
14078c7f984Sjmcneill 
14178c7f984Sjmcneill 	fdt_add_bus(self, phandle, faa);
142a0504a86Sjmcneill 
143a0504a86Sjmcneill 	child = of_find_firstchild_byname(phandle, "clocks");
144a0504a86Sjmcneill 	if (child > 0)
145a0504a86Sjmcneill 		fdt_add_bus(self, child, faa);
1465b0333ebSjmcneill }
147