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