xref: /netbsd-src/sys/arch/arm/fdt/plkmi_fdt.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: plkmi_fdt.c,v 1.3 2021/01/27 03:10:19 thorpej Exp $ */
210227d7eSjmcneill 
310227d7eSjmcneill /*-
410227d7eSjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
510227d7eSjmcneill  * All rights reserved.
610227d7eSjmcneill  *
710227d7eSjmcneill  * Redistribution and use in source and binary forms, with or without
810227d7eSjmcneill  * modification, are permitted provided that the following conditions
910227d7eSjmcneill  * are met:
1010227d7eSjmcneill  * 1. Redistributions of source code must retain the above copyright
1110227d7eSjmcneill  *    notice, this list of conditions and the following disclaimer.
1210227d7eSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
1310227d7eSjmcneill  *    notice, this list of conditions and the following disclaimer in the
1410227d7eSjmcneill  *    documentation and/or other materials provided with the distribution.
1510227d7eSjmcneill  *
1610227d7eSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1710227d7eSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1810227d7eSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1910227d7eSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2010227d7eSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2110227d7eSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2210227d7eSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2310227d7eSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2410227d7eSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2510227d7eSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2610227d7eSjmcneill  * SUCH DAMAGE.
2710227d7eSjmcneill  */
2810227d7eSjmcneill 
2910227d7eSjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: plkmi_fdt.c,v 1.3 2021/01/27 03:10:19 thorpej Exp $");
3110227d7eSjmcneill 
3210227d7eSjmcneill #include <sys/param.h>
3310227d7eSjmcneill #include <sys/bus.h>
3410227d7eSjmcneill #include <sys/device.h>
3510227d7eSjmcneill #include <sys/systm.h>
3610227d7eSjmcneill #include <sys/kernel.h>
3710227d7eSjmcneill 
3810227d7eSjmcneill #include <dev/fdt/fdtvar.h>
3910227d7eSjmcneill 
4010227d7eSjmcneill #include <dev/ic/pl050var.h>
4110227d7eSjmcneill 
4210227d7eSjmcneill static int	plkmi_fdt_match(device_t, cfdata_t, void *);
4310227d7eSjmcneill static void	plkmi_fdt_attach(device_t, device_t, void *);
4410227d7eSjmcneill 
45*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
46*6e54367aSthorpej 	{ .compat = "arm,pl050" },
47*6e54367aSthorpej 	DEVICE_COMPAT_EOL
4810227d7eSjmcneill };
4910227d7eSjmcneill 
5010227d7eSjmcneill CFATTACH_DECL_NEW(plkmi_fdt, sizeof(struct plkmi_softc),
5110227d7eSjmcneill 	plkmi_fdt_match, plkmi_fdt_attach, NULL, NULL);
5210227d7eSjmcneill 
5310227d7eSjmcneill static int
plkmi_fdt_match(device_t parent,cfdata_t cf,void * aux)5410227d7eSjmcneill plkmi_fdt_match(device_t parent, cfdata_t cf, void *aux)
5510227d7eSjmcneill {
5610227d7eSjmcneill 	struct fdt_attach_args * const faa = aux;
5710227d7eSjmcneill 
58*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
5910227d7eSjmcneill }
6010227d7eSjmcneill 
6110227d7eSjmcneill static void
plkmi_fdt_attach(device_t parent,device_t self,void * aux)6210227d7eSjmcneill plkmi_fdt_attach(device_t parent, device_t self, void *aux)
6310227d7eSjmcneill {
6410227d7eSjmcneill 	struct plkmi_softc * const sc = device_private(self);
6510227d7eSjmcneill 	struct fdt_attach_args * const faa = aux;
6610227d7eSjmcneill 	const int phandle = faa->faa_phandle;
6710227d7eSjmcneill 	char intrstr[128];
6810227d7eSjmcneill 	struct clk *clk;
6910227d7eSjmcneill 	bus_addr_t addr;
7010227d7eSjmcneill 	bus_size_t size;
7110227d7eSjmcneill 	void *ih;
7210227d7eSjmcneill 
7310227d7eSjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
7410227d7eSjmcneill 		aprint_error(": missing 'reg' property\n");
7510227d7eSjmcneill 		return;
7610227d7eSjmcneill 	}
7710227d7eSjmcneill 
7810227d7eSjmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
7910227d7eSjmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
8010227d7eSjmcneill 		return;
8110227d7eSjmcneill 	}
8210227d7eSjmcneill 
8310227d7eSjmcneill 
8410227d7eSjmcneill 	for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++)
8510227d7eSjmcneill 		if (clk_enable(clk) != 0) {
8610227d7eSjmcneill 			aprint_error(": couldn't enable clock #%d\n", i);
8710227d7eSjmcneill 			return;
8810227d7eSjmcneill 		}
8910227d7eSjmcneill 
9010227d7eSjmcneill 	sc->sc_dev = self;
9110227d7eSjmcneill 	sc->sc_bst = faa->faa_bst;
9210227d7eSjmcneill 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_bsh)) {
9310227d7eSjmcneill 		aprint_error(": couldn't map device\n");
9410227d7eSjmcneill 		return;
9510227d7eSjmcneill 	}
9610227d7eSjmcneill 
9710227d7eSjmcneill 	plkmi_attach(sc);
9810227d7eSjmcneill 
9964e248edSryo 	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_TTY, 0, plkmi_intr, sc,
10064e248edSryo 	    device_xname(self));
10110227d7eSjmcneill 	if (ih == NULL) {
10210227d7eSjmcneill 		aprint_error_dev(self, "couldn't install interrupt handler\n");
10310227d7eSjmcneill 		return;
10410227d7eSjmcneill 	}
10510227d7eSjmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
10610227d7eSjmcneill }
107