1/* $NetBSD: cdnsiic_fdt.c,v 1.1 2022/11/05 17:31:37 jmcneill Exp $ */ 2 3/*- 4 * Copyright (c) 2022 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30 31__KERNEL_RCSID(0, "$NetBSD: cdnsiic_fdt.c,v 1.1 2022/11/05 17:31:37 jmcneill Exp $"); 32 33#include <sys/param.h> 34#include <sys/bus.h> 35#include <sys/device.h> 36#include <sys/intr.h> 37#include <sys/systm.h> 38#include <sys/time.h> 39#include <sys/kmem.h> 40 41#include <dev/i2c/i2cvar.h> 42 43#include <dev/fdt/fdtvar.h> 44#include <dev/ic/cdnsiicvar.h> 45 46static const struct device_compatible_entry compat_data[] = { 47 { .compat = "cdns,i2c-r1p10" }, 48 { .compat = "cdns,i2c-r1p14" }, 49 DEVICE_COMPAT_EOL 50}; 51 52static int 53cdnsiic_fdt_match(device_t parent, cfdata_t cf, void *aux) 54{ 55 struct fdt_attach_args * const faa = aux; 56 57 return of_compatible_match(faa->faa_phandle, compat_data); 58} 59 60static void 61cdnsiic_fdt_attach(device_t parent, device_t self, void *aux) 62{ 63 struct cdnsiic_softc * const sc = device_private(self); 64 struct fdt_attach_args * const faa = aux; 65 const int phandle = faa->faa_phandle; 66 bus_addr_t addr; 67 bus_size_t size; 68 int error; 69 70 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 71 aprint_error(": couldn't get registers\n"); 72 return; 73 } 74 75 sc->sc_pclk = fdtbus_clock_get_index(phandle, 0); 76 if (sc->sc_pclk == NULL || clk_enable(sc->sc_pclk) != 0) { 77 aprint_error(": couldn't enable pclk\n"); 78 return; 79 } 80 81 if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_bus_freq)) 82 sc->sc_bus_freq = 100000; 83 84 sc->sc_dev = self; 85 sc->sc_bst = faa->faa_bst; 86 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 87 aprint_error(": couldn't map registers\n"); 88 return; 89 } 90 91 error = cdnsiic_attach(sc); 92 if (error != 0) { 93 aprint_error_dev(self, "init failed, error = %d\n", error); 94 return; 95 } 96 97 fdtbus_register_i2c_controller(&sc->sc_ic, phandle); 98 99 fdtbus_attach_i2cbus(self, phandle, &sc->sc_ic, iicbus_print); 100} 101 102CFATTACH_DECL_NEW(cdnsiic_fdt, sizeof(struct cdnsiic_softc), 103 cdnsiic_fdt_match, cdnsiic_fdt_attach, NULL, NULL); 104