plmmc_fdt.c revision 1.2
11.2Sskrll/* $NetBSD: plmmc_fdt.c,v 1.2 2019/06/12 10:19:27 skrll Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.2Sskrll__KERNEL_RCSID(0, "$NetBSD: plmmc_fdt.c,v 1.2 2019/06/12 10:19:27 skrll Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/systm.h>
361.1Sjmcneill#include <sys/kernel.h>
371.1Sjmcneill
381.1Sjmcneill#include <dev/fdt/fdtvar.h>
391.1Sjmcneill
401.1Sjmcneill#include <dev/ic/pl181reg.h>
411.1Sjmcneill#include <dev/ic/pl181var.h>
421.1Sjmcneill
431.1Sjmcneillstatic int	plmmc_fdt_match(device_t, cfdata_t, void *);
441.1Sjmcneillstatic void	plmmc_fdt_attach(device_t, device_t, void *);
451.1Sjmcneill
461.1Sjmcneillstatic const char * const compatible[] = {
471.1Sjmcneill	"arm,pl180",
481.1Sjmcneill	"arm,pl181",
491.1Sjmcneill	NULL
501.1Sjmcneill};
511.1Sjmcneill
521.1SjmcneillCFATTACH_DECL_NEW(plmmc_fdt, sizeof(struct plmmc_softc),
531.1Sjmcneill	plmmc_fdt_match, plmmc_fdt_attach, NULL, NULL);
541.1Sjmcneill
551.1Sjmcneillstatic int
561.1Sjmcneillplmmc_fdt_match(device_t parent, cfdata_t cf, void *aux)
571.1Sjmcneill{
581.1Sjmcneill	struct fdt_attach_args * const faa = aux;
591.1Sjmcneill
601.1Sjmcneill	return of_compatible(faa->faa_phandle, compatible) >= 0;
611.1Sjmcneill}
621.1Sjmcneill
631.1Sjmcneillstatic void
641.1Sjmcneillplmmc_fdt_attach(device_t parent, device_t self, void *aux)
651.1Sjmcneill{
661.1Sjmcneill	struct plmmc_softc * const sc = device_private(self);
671.1Sjmcneill	struct fdt_attach_args * const faa = aux;
681.1Sjmcneill	const int phandle = faa->faa_phandle;
691.1Sjmcneill	struct clk *clk;
701.1Sjmcneill	bus_addr_t addr;
711.1Sjmcneill	bus_size_t size;
721.1Sjmcneill	void *ih;
731.1Sjmcneill
741.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
751.1Sjmcneill		aprint_error(": missing 'reg' property\n");
761.1Sjmcneill		return;
771.1Sjmcneill	}
781.1Sjmcneill
791.1Sjmcneill	clk = fdtbus_clock_get_index(phandle, 0);
801.1Sjmcneill	if (clk == NULL) {
811.1Sjmcneill		aprint_error(": couldn't get clock\n");
821.1Sjmcneill		return;
831.1Sjmcneill	}
841.1Sjmcneill
851.1Sjmcneill	if (clk_enable(clk) != 0) {
861.1Sjmcneill		aprint_error(": couldn't enable clock\n");
871.1Sjmcneill		return;
881.1Sjmcneill	}
891.1Sjmcneill
901.2Sskrll	char intrstr[128];
911.2Sskrll	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
921.2Sskrll		aprint_error(": failed to decode interrupt\n");
931.2Sskrll		return;
941.2Sskrll	}
951.2Sskrll
961.1Sjmcneill	sc->sc_dev = self;
971.1Sjmcneill	sc->sc_clock_freq = clk_get_rate(clk);
981.1Sjmcneill	of_getprop_uint32(phandle, "max-frequency", &sc->sc_max_freq);
991.1Sjmcneill	sc->sc_bst = faa->faa_bst;
1001.1Sjmcneill	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_bsh)) {
1011.1Sjmcneill		aprint_error(": couldn't map device\n");
1021.1Sjmcneill		return;
1031.1Sjmcneill	}
1041.1Sjmcneill
1051.1Sjmcneill	aprint_naive("\n");
1061.1Sjmcneill	aprint_normal("\n");
1071.1Sjmcneill
1081.1Sjmcneill	ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, 0, plmmc_intr, sc);
1091.1Sjmcneill	if (ih == NULL) {
1101.1Sjmcneill		aprint_error_dev(self, "couldn't install interrupt handler\n");
1111.1Sjmcneill		return;
1121.1Sjmcneill	}
1131.2Sskrll	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1141.1Sjmcneill
1151.1Sjmcneill	plmmc_init(sc);
1161.1Sjmcneill}
117