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