1*6e54367aSthorpej /* $NetBSD: plrtc_fdt.c,v 1.2 2021/01/27 03:10:19 thorpej Exp $ */
291452e84Sjmcneill
391452e84Sjmcneill /*-
491452e84Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
591452e84Sjmcneill * All rights reserved.
691452e84Sjmcneill *
791452e84Sjmcneill * Redistribution and use in source and binary forms, with or without
891452e84Sjmcneill * modification, are permitted provided that the following conditions
991452e84Sjmcneill * are met:
1091452e84Sjmcneill * 1. Redistributions of source code must retain the above copyright
1191452e84Sjmcneill * notice, this list of conditions and the following disclaimer.
1291452e84Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1391452e84Sjmcneill * notice, this list of conditions and the following disclaimer in the
1491452e84Sjmcneill * documentation and/or other materials provided with the distribution.
1591452e84Sjmcneill *
1691452e84Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1791452e84Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1891452e84Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1991452e84Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2091452e84Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2191452e84Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2291452e84Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2391452e84Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2491452e84Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2591452e84Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2691452e84Sjmcneill * SUCH DAMAGE.
2791452e84Sjmcneill */
2891452e84Sjmcneill
2991452e84Sjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: plrtc_fdt.c,v 1.2 2021/01/27 03:10:19 thorpej Exp $");
3191452e84Sjmcneill
3291452e84Sjmcneill #include <sys/param.h>
3391452e84Sjmcneill #include <sys/bus.h>
3491452e84Sjmcneill #include <sys/device.h>
3591452e84Sjmcneill #include <sys/systm.h>
3691452e84Sjmcneill #include <sys/kernel.h>
3791452e84Sjmcneill
3891452e84Sjmcneill #include <dev/fdt/fdtvar.h>
3991452e84Sjmcneill
4091452e84Sjmcneill #include <dev/ic/pl031var.h>
4191452e84Sjmcneill
4291452e84Sjmcneill static int plrtc_fdt_match(device_t, cfdata_t, void *);
4391452e84Sjmcneill static void plrtc_fdt_attach(device_t, device_t, void *);
4491452e84Sjmcneill
45*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
46*6e54367aSthorpej { .compat = "arm,pl031" },
47*6e54367aSthorpej DEVICE_COMPAT_EOL
4891452e84Sjmcneill };
4991452e84Sjmcneill
5091452e84Sjmcneill CFATTACH_DECL_NEW(plrtc_fdt, sizeof(struct plrtc_softc),
5191452e84Sjmcneill plrtc_fdt_match, plrtc_fdt_attach, NULL, NULL);
5291452e84Sjmcneill
5391452e84Sjmcneill static int
plrtc_fdt_match(device_t parent,cfdata_t cf,void * aux)5491452e84Sjmcneill plrtc_fdt_match(device_t parent, cfdata_t cf, void *aux)
5591452e84Sjmcneill {
5691452e84Sjmcneill struct fdt_attach_args * const faa = aux;
5791452e84Sjmcneill
58*6e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
5991452e84Sjmcneill }
6091452e84Sjmcneill
6191452e84Sjmcneill static void
plrtc_fdt_attach(device_t parent,device_t self,void * aux)6291452e84Sjmcneill plrtc_fdt_attach(device_t parent, device_t self, void *aux)
6391452e84Sjmcneill {
6491452e84Sjmcneill struct plrtc_softc * const sc = device_private(self);
6591452e84Sjmcneill struct fdt_attach_args * const faa = aux;
6691452e84Sjmcneill const int phandle = faa->faa_phandle;
6791452e84Sjmcneill bus_addr_t addr;
6891452e84Sjmcneill bus_size_t size;
6991452e84Sjmcneill
7091452e84Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
7191452e84Sjmcneill aprint_error(": missing 'reg' property\n");
7291452e84Sjmcneill return;
7391452e84Sjmcneill }
7491452e84Sjmcneill
7591452e84Sjmcneill sc->sc_dev = self;
7691452e84Sjmcneill sc->sc_bst = faa->faa_bst;
7791452e84Sjmcneill if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_bsh)) {
7891452e84Sjmcneill aprint_error(": couldn't map device\n");
7991452e84Sjmcneill return;
8091452e84Sjmcneill }
8191452e84Sjmcneill
8291452e84Sjmcneill plrtc_attach(sc);
8391452e84Sjmcneill
8491452e84Sjmcneill fdtbus_todr_attach(self, phandle, &sc->sc_todr);
8591452e84Sjmcneill }
86