dwiic_fdt.c revision 1.6 1 /* $NetBSD: dwiic_fdt.c,v 1.6 2025/01/02 07:54:41 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Manuel Bouyer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Synopsys DesignWare I2C controller, FDT front-end
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: dwiic_fdt.c,v 1.6 2025/01/02 07:54:41 skrll Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40
41 #include <dev/fdt/fdtvar.h>
42 #include <dev/ic/dwiic_var.h>
43
44 static const struct device_compatible_entry compat_data[] = {
45 { .compat = "snps,designware-i2c" },
46 DEVICE_COMPAT_EOL
47 };
48
49 struct dwiic_fdt_softc {
50 struct dwiic_softc sc_dwiic;
51 };
52
53 static int dwiic_fdt_match(device_t, cfdata_t, void *);
54 static void dwiic_fdt_attach(device_t, device_t, void *);
55
56 CFATTACH_DECL_NEW(dwiic_fdt, sizeof(struct dwiic_fdt_softc),
57 dwiic_fdt_match, dwiic_fdt_attach, dwiic_detach, NULL);
58
59 int
60 dwiic_fdt_match(device_t parent, cfdata_t match, void *aux)
61 {
62 struct fdt_attach_args * const faa = aux;
63
64 return of_compatible_match(faa->faa_phandle, compat_data);
65 }
66
67 void
68 dwiic_fdt_attach(device_t parent, device_t self, void *aux)
69 {
70 struct dwiic_fdt_softc * const sc = device_private(self);
71 struct fdt_attach_args * const faa = aux;
72 const int phandle = faa->faa_phandle;
73 bus_addr_t addr;
74 bus_size_t size;
75 char intrstr[128];
76
77 sc->sc_dwiic.sc_dev = self;
78 sc->sc_dwiic.sc_power = NULL;
79 sc->sc_dwiic.sc_type = dwiic_type_generic;
80
81 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
82 aprint_error(": couldn't get registers\n");
83 return;
84 }
85
86 sc->sc_dwiic.sc_iot = faa->faa_bst;
87 if (bus_space_map(sc->sc_dwiic.sc_iot, addr, size, 0, &sc->sc_dwiic.sc_ioh) != 0) {
88 aprint_error(": couldn't map registers\n");
89 return;
90 }
91
92 /* enable the clock */
93 struct clk *clk = fdtbus_clock_get_index(phandle, 0);
94 if (clk_enable(clk) != 0) {
95 aprint_error(": couldn't enable the clock\n");
96 goto failed_clock;
97 }
98
99 /* de-assert resets */
100 struct fdtbus_reset *rst = fdtbus_reset_get_index(phandle, 0);
101 if (fdtbus_reset_deassert(rst) != 0) {
102 aprint_error(": couldn't de-assert the reset\n");
103 goto failed_reset;
104 }
105
106 aprint_naive(": I2C controller\n");
107 aprint_normal(": I2C controller\n");
108
109 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
110 aprint_error_dev(self, "failed to decode interrupt\n");
111 goto failed_intrstr;
112 }
113 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
114
115 sc->sc_dwiic.sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
116 dwiic_intr, &sc->sc_dwiic, device_xname(self));
117 if (sc->sc_dwiic.sc_ih == NULL) {
118 aprint_error_dev(self, "couldn't establish interrupt\n");
119 goto failed_intr;
120 }
121
122 if (!dwiic_attach(&sc->sc_dwiic))
123 goto failed_attach;
124
125 pmf_device_register(self, dwiic_suspend, dwiic_resume);
126
127 fdtbus_register_i2c_controller(&sc->sc_dwiic.sc_i2c_tag, phandle);
128
129 fdtbus_attach_i2cbus(self, phandle, &sc->sc_dwiic.sc_i2c_tag, iicbus_print);
130
131 return;
132
133 failed_attach:
134 fdtbus_intr_disestablish(phandle, sc->sc_dwiic.sc_ih);
135
136 failed_intrstr:
137 failed_intr:
138 failed_reset:
139 failed_clock:
140 bus_space_unmap(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh, size);
141 }
142