1*6e54367aSthorpej /* $NetBSD: plmmc_fdt.c,v 1.4 2021/01/27 03:10:19 thorpej Exp $ */
20bd802dfSjmcneill
30bd802dfSjmcneill /*-
40bd802dfSjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
50bd802dfSjmcneill * All rights reserved.
60bd802dfSjmcneill *
70bd802dfSjmcneill * Redistribution and use in source and binary forms, with or without
80bd802dfSjmcneill * modification, are permitted provided that the following conditions
90bd802dfSjmcneill * are met:
100bd802dfSjmcneill * 1. Redistributions of source code must retain the above copyright
110bd802dfSjmcneill * notice, this list of conditions and the following disclaimer.
120bd802dfSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
130bd802dfSjmcneill * notice, this list of conditions and the following disclaimer in the
140bd802dfSjmcneill * documentation and/or other materials provided with the distribution.
150bd802dfSjmcneill *
160bd802dfSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
170bd802dfSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
180bd802dfSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
190bd802dfSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
200bd802dfSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
210bd802dfSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
220bd802dfSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
230bd802dfSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
240bd802dfSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250bd802dfSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260bd802dfSjmcneill * SUCH DAMAGE.
270bd802dfSjmcneill */
280bd802dfSjmcneill
290bd802dfSjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: plmmc_fdt.c,v 1.4 2021/01/27 03:10:19 thorpej Exp $");
310bd802dfSjmcneill
320bd802dfSjmcneill #include <sys/param.h>
330bd802dfSjmcneill #include <sys/bus.h>
340bd802dfSjmcneill #include <sys/device.h>
350bd802dfSjmcneill #include <sys/systm.h>
360bd802dfSjmcneill #include <sys/kernel.h>
370bd802dfSjmcneill
380bd802dfSjmcneill #include <dev/fdt/fdtvar.h>
390bd802dfSjmcneill
400bd802dfSjmcneill #include <dev/ic/pl181reg.h>
410bd802dfSjmcneill #include <dev/ic/pl181var.h>
420bd802dfSjmcneill
430bd802dfSjmcneill static int plmmc_fdt_match(device_t, cfdata_t, void *);
440bd802dfSjmcneill static void plmmc_fdt_attach(device_t, device_t, void *);
450bd802dfSjmcneill
46*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
47*6e54367aSthorpej { .compat = "arm,pl180" },
48*6e54367aSthorpej { .compat = "arm,pl181" },
49*6e54367aSthorpej DEVICE_COMPAT_EOL
500bd802dfSjmcneill };
510bd802dfSjmcneill
520bd802dfSjmcneill CFATTACH_DECL_NEW(plmmc_fdt, sizeof(struct plmmc_softc),
530bd802dfSjmcneill plmmc_fdt_match, plmmc_fdt_attach, NULL, NULL);
540bd802dfSjmcneill
550bd802dfSjmcneill static int
plmmc_fdt_match(device_t parent,cfdata_t cf,void * aux)560bd802dfSjmcneill plmmc_fdt_match(device_t parent, cfdata_t cf, void *aux)
570bd802dfSjmcneill {
580bd802dfSjmcneill struct fdt_attach_args * const faa = aux;
590bd802dfSjmcneill
60*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
610bd802dfSjmcneill }
620bd802dfSjmcneill
630bd802dfSjmcneill static void
plmmc_fdt_attach(device_t parent,device_t self,void * aux)640bd802dfSjmcneill plmmc_fdt_attach(device_t parent, device_t self, void *aux)
650bd802dfSjmcneill {
660bd802dfSjmcneill struct plmmc_softc * const sc = device_private(self);
670bd802dfSjmcneill struct fdt_attach_args * const faa = aux;
680bd802dfSjmcneill const int phandle = faa->faa_phandle;
690bd802dfSjmcneill struct clk *clk;
700bd802dfSjmcneill bus_addr_t addr;
710bd802dfSjmcneill bus_size_t size;
720bd802dfSjmcneill void *ih;
730bd802dfSjmcneill
740bd802dfSjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
750bd802dfSjmcneill aprint_error(": missing 'reg' property\n");
760bd802dfSjmcneill return;
770bd802dfSjmcneill }
780bd802dfSjmcneill
790bd802dfSjmcneill clk = fdtbus_clock_get_index(phandle, 0);
800bd802dfSjmcneill if (clk == NULL) {
810bd802dfSjmcneill aprint_error(": couldn't get clock\n");
820bd802dfSjmcneill return;
830bd802dfSjmcneill }
840bd802dfSjmcneill
850bd802dfSjmcneill if (clk_enable(clk) != 0) {
860bd802dfSjmcneill aprint_error(": couldn't enable clock\n");
870bd802dfSjmcneill return;
880bd802dfSjmcneill }
890bd802dfSjmcneill
90526c0da6Sskrll char intrstr[128];
91526c0da6Sskrll if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
92526c0da6Sskrll aprint_error(": failed to decode interrupt\n");
93526c0da6Sskrll return;
94526c0da6Sskrll }
95526c0da6Sskrll
960bd802dfSjmcneill sc->sc_dev = self;
970bd802dfSjmcneill sc->sc_clock_freq = clk_get_rate(clk);
980bd802dfSjmcneill of_getprop_uint32(phandle, "max-frequency", &sc->sc_max_freq);
990bd802dfSjmcneill sc->sc_bst = faa->faa_bst;
1000bd802dfSjmcneill if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_bsh)) {
1010bd802dfSjmcneill aprint_error(": couldn't map device\n");
1020bd802dfSjmcneill return;
1030bd802dfSjmcneill }
1040bd802dfSjmcneill
1050bd802dfSjmcneill aprint_naive("\n");
1060bd802dfSjmcneill aprint_normal("\n");
1070bd802dfSjmcneill
10864e248edSryo ih = fdtbus_intr_establish_xname(phandle, 0, IPL_BIO, 0, plmmc_intr, sc,
10964e248edSryo device_xname(self));
1100bd802dfSjmcneill if (ih == NULL) {
1110bd802dfSjmcneill aprint_error_dev(self, "couldn't install interrupt handler\n");
1120bd802dfSjmcneill return;
1130bd802dfSjmcneill }
114526c0da6Sskrll aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1150bd802dfSjmcneill
1160bd802dfSjmcneill plmmc_init(sc);
1170bd802dfSjmcneill }
118