bcm2835_bsc_fdt.c revision 1.3 1 /* $NetBSD: bcm2835_bsc_fdt.c,v 1.3 2020/12/23 02:56:11 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2019 Jason R. Thorpe
5 * Copyright (c) 2012 Jonathan A. Kollasch
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc_fdt.c,v 1.3 2020/12/23 02:56:11 thorpej Exp $");
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/kernhist.h>
37 #include <sys/intr.h>
38 #include <sys/mutex.h>
39 #include <sys/systm.h>
40
41 #include <dev/i2c/i2cvar.h>
42
43 #include <arm/broadcom/bcm2835reg.h>
44 #include <arm/broadcom/bcm2835_bscreg.h>
45 #include <arm/broadcom/bcm2835_bscvar.h>
46
47 #include <dev/fdt/fdtvar.h>
48
49 static i2c_tag_t
50 bsciic_fdt_get_tag(device_t dev)
51 {
52 struct bsciic_softc * const sc = device_private(dev);
53
54 return &sc->sc_i2c;
55 }
56
57 static const struct fdtbus_i2c_controller_func bsciic_fdt_funcs = {
58 .get_tag = bsciic_fdt_get_tag,
59 };
60
61 static int bsciic_fdt_match(device_t, cfdata_t, void *);
62 static void bsciic_fdt_attach(device_t, device_t, void *);
63
64 CFATTACH_DECL_NEW(bsciic_fdt, sizeof(struct bsciic_softc),
65 bsciic_fdt_match, bsciic_fdt_attach, NULL, NULL);
66
67 static int
68 bsciic_fdt_match(device_t parent, cfdata_t match, void *aux)
69 {
70 const char * const compatible[] = { "brcm,bcm2835-i2c", NULL };
71 struct fdt_attach_args * const faa = aux;
72
73 return of_match_compatible(faa->faa_phandle, compatible);
74 }
75
76 static void
77 bsciic_fdt_attach(device_t parent, device_t self, void *aux)
78 {
79 struct bsciic_softc * const sc = device_private(self);
80 struct fdt_attach_args * const faa = aux;
81 const int phandle = faa->faa_phandle;
82
83 bus_addr_t addr;
84 bus_size_t size;
85
86 sc->sc_dev = self;
87 sc->sc_iot = faa->faa_bst;
88
89 int error = fdtbus_get_reg(phandle, 0, &addr, &size);
90 if (error) {
91 aprint_error(": unable to get device registers\n");
92 return;
93 }
94
95 /* Enable clock */
96 sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
97 if (sc->sc_clk == NULL) {
98 aprint_error(": couldn't acquire clock\n");
99 return;
100 }
101
102 if (clk_enable(sc->sc_clk) != 0) {
103 aprint_error(": failed to enable clock\n");
104 return;
105 }
106
107 sc->sc_frequency = clk_get_rate(sc->sc_clk);
108
109 if (of_getprop_uint32(phandle, "clock-frequency",
110 &sc->sc_clkrate) != 0) {
111 sc->sc_clkrate = 100000;
112 }
113
114 if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
115 aprint_error(": unable to map device\n");
116 return;
117 }
118
119 aprint_naive("\n");
120 aprint_normal(": Broadcom Serial Controller\n");
121
122 bsciic_attach(sc);
123
124 char intrstr[128];
125 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
126 aprint_error_dev(sc->sc_dev, "failed to decode interrupt\n");
127 return;
128 }
129 sc->sc_inth = fdtbus_intr_establish(phandle, 0, IPL_VM,
130 FDT_INTR_MPSAFE, bsciic_intr, sc);
131 if (sc->sc_inth == NULL) {
132 aprint_error_dev(sc->sc_dev,
133 "failed to establish interrupt %s\n", intrstr);
134 return;
135 }
136 aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
137
138 iic_tag_init(&sc->sc_i2c);
139 sc->sc_i2c.ic_cookie = sc;
140 sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
141 sc->sc_i2c.ic_release_bus = bsciic_release_bus;
142 sc->sc_i2c.ic_exec = bsciic_exec;
143
144 fdtbus_register_i2c_controller(self, phandle, &bsciic_fdt_funcs);
145
146 fdtbus_attach_i2cbus(self, phandle, &sc->sc_i2c, iicbus_print);
147 }
148