1*6e54367aSthorpej /* $NetBSD: aaci_fdt.c,v 1.3 2021/01/27 03:10:19 thorpej Exp $ */
23d464c81Sjmcneill
33d464c81Sjmcneill /*-
43d464c81Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
53d464c81Sjmcneill * All rights reserved.
63d464c81Sjmcneill *
73d464c81Sjmcneill * Redistribution and use in source and binary forms, with or without
83d464c81Sjmcneill * modification, are permitted provided that the following conditions
93d464c81Sjmcneill * are met:
103d464c81Sjmcneill * 1. Redistributions of source code must retain the above copyright
113d464c81Sjmcneill * notice, this list of conditions and the following disclaimer.
123d464c81Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
133d464c81Sjmcneill * notice, this list of conditions and the following disclaimer in the
143d464c81Sjmcneill * documentation and/or other materials provided with the distribution.
153d464c81Sjmcneill *
163d464c81Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173d464c81Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183d464c81Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193d464c81Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203d464c81Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
213d464c81Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
223d464c81Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
233d464c81Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
243d464c81Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253d464c81Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263d464c81Sjmcneill * SUCH DAMAGE.
273d464c81Sjmcneill */
283d464c81Sjmcneill
293d464c81Sjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: aaci_fdt.c,v 1.3 2021/01/27 03:10:19 thorpej Exp $");
313d464c81Sjmcneill
323d464c81Sjmcneill #include <sys/param.h>
333d464c81Sjmcneill #include <sys/bus.h>
343d464c81Sjmcneill #include <sys/device.h>
353d464c81Sjmcneill #include <sys/systm.h>
363d464c81Sjmcneill #include <sys/kernel.h>
373d464c81Sjmcneill
383d464c81Sjmcneill #include <dev/fdt/fdtvar.h>
393d464c81Sjmcneill
403d464c81Sjmcneill #include <dev/ic/pl041var.h>
413d464c81Sjmcneill
423d464c81Sjmcneill static int aaci_fdt_match(device_t, cfdata_t, void *);
433d464c81Sjmcneill static void aaci_fdt_attach(device_t, device_t, void *);
443d464c81Sjmcneill
45*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
46*6e54367aSthorpej { .compat = "arm,pl041" },
47*6e54367aSthorpej DEVICE_COMPAT_EOL
483d464c81Sjmcneill };
493d464c81Sjmcneill
503d464c81Sjmcneill CFATTACH_DECL_NEW(aaci_fdt, sizeof(struct aaci_softc),
513d464c81Sjmcneill aaci_fdt_match, aaci_fdt_attach, NULL, NULL);
523d464c81Sjmcneill
533d464c81Sjmcneill static int
aaci_fdt_match(device_t parent,cfdata_t cf,void * aux)543d464c81Sjmcneill aaci_fdt_match(device_t parent, cfdata_t cf, void *aux)
553d464c81Sjmcneill {
563d464c81Sjmcneill struct fdt_attach_args * const faa = aux;
573d464c81Sjmcneill
58*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
593d464c81Sjmcneill }
603d464c81Sjmcneill
613d464c81Sjmcneill static void
aaci_fdt_attach(device_t parent,device_t self,void * aux)623d464c81Sjmcneill aaci_fdt_attach(device_t parent, device_t self, void *aux)
633d464c81Sjmcneill {
643d464c81Sjmcneill struct aaci_softc * const sc = device_private(self);
653d464c81Sjmcneill struct fdt_attach_args * const faa = aux;
663d464c81Sjmcneill const int phandle = faa->faa_phandle;
673d464c81Sjmcneill char intrstr[128];
683d464c81Sjmcneill struct clk *clk;
693d464c81Sjmcneill bus_addr_t addr;
703d464c81Sjmcneill bus_size_t size;
713d464c81Sjmcneill void *ih;
723d464c81Sjmcneill
733d464c81Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
743d464c81Sjmcneill aprint_error(": missing 'reg' property\n");
753d464c81Sjmcneill return;
763d464c81Sjmcneill }
773d464c81Sjmcneill
783d464c81Sjmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
793d464c81Sjmcneill aprint_error_dev(self, "failed to decode interrupt\n");
803d464c81Sjmcneill return;
813d464c81Sjmcneill }
823d464c81Sjmcneill
833d464c81Sjmcneill for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++)
843d464c81Sjmcneill if (clk_enable(clk) != 0) {
853d464c81Sjmcneill aprint_error(": couldn't enable clock #%d\n", i);
863d464c81Sjmcneill return;
873d464c81Sjmcneill }
883d464c81Sjmcneill
893d464c81Sjmcneill sc->sc_dev = self;
903d464c81Sjmcneill sc->sc_bst = faa->faa_bst;
913d464c81Sjmcneill if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_bsh)) {
923d464c81Sjmcneill aprint_error(": couldn't map device\n");
933d464c81Sjmcneill return;
943d464c81Sjmcneill }
953d464c81Sjmcneill
9664e248edSryo ih = fdtbus_intr_establish_xname(phandle, 0, IPL_AUDIO, FDT_INTR_MPSAFE,
9764e248edSryo aaci_intr, sc, device_xname(self));
983d464c81Sjmcneill if (ih == NULL) {
993d464c81Sjmcneill aprint_error_dev(self, "couldn't install interrupt handler\n");
1003d464c81Sjmcneill return;
1013d464c81Sjmcneill }
1023d464c81Sjmcneill
1033d464c81Sjmcneill aaci_attach(sc);
1043d464c81Sjmcneill
1053d464c81Sjmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1063d464c81Sjmcneill }
107