bcm2835_bsc_fdt.c revision 1.1 1 1.1 jmcneill /* $NetBSD: bcm2835_bsc_fdt.c,v 1.1 2020/03/31 12:23:17 jmcneill 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc_fdt.c,v 1.1 2020/03/31 12:23:17 jmcneill 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.1 jmcneill static int bsciic_fdt_match(device_t, cfdata_t, void *);
50 1.1 jmcneill static void bsciic_fdt_attach(device_t, device_t, void *);
51 1.1 jmcneill
52 1.1 jmcneill CFATTACH_DECL_NEW(bsciic_fdt, sizeof(struct bsciic_softc),
53 1.1 jmcneill bsciic_fdt_match, bsciic_fdt_attach, NULL, NULL);
54 1.1 jmcneill
55 1.1 jmcneill static int
56 1.1 jmcneill bsciic_fdt_match(device_t parent, cfdata_t match, void *aux)
57 1.1 jmcneill {
58 1.1 jmcneill const char * const compatible[] = { "brcm,bcm2835-i2c", NULL };
59 1.1 jmcneill struct fdt_attach_args * const faa = aux;
60 1.1 jmcneill
61 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
62 1.1 jmcneill }
63 1.1 jmcneill
64 1.1 jmcneill static void
65 1.1 jmcneill bsciic_fdt_attach(device_t parent, device_t self, void *aux)
66 1.1 jmcneill {
67 1.1 jmcneill struct bsciic_softc * const sc = device_private(self);
68 1.1 jmcneill struct fdt_attach_args * const faa = aux;
69 1.1 jmcneill const int phandle = faa->faa_phandle;
70 1.1 jmcneill prop_dictionary_t prop = device_properties(self);
71 1.1 jmcneill bool disable = false;
72 1.1 jmcneill
73 1.1 jmcneill bus_addr_t addr;
74 1.1 jmcneill bus_size_t size;
75 1.1 jmcneill
76 1.1 jmcneill sc->sc_dev = self;
77 1.1 jmcneill sc->sc_iot = faa->faa_bst;
78 1.1 jmcneill
79 1.1 jmcneill int error = fdtbus_get_reg(phandle, 0, &addr, &size);
80 1.1 jmcneill if (error) {
81 1.1 jmcneill aprint_error(": unable to get device registers\n");
82 1.1 jmcneill return;
83 1.1 jmcneill }
84 1.1 jmcneill
85 1.1 jmcneill prop_dictionary_get_bool(prop, "disable", &disable);
86 1.1 jmcneill if (disable) {
87 1.1 jmcneill aprint_naive(": disabled\n");
88 1.1 jmcneill aprint_normal(": disabled\n");
89 1.1 jmcneill return;
90 1.1 jmcneill }
91 1.1 jmcneill
92 1.1 jmcneill /* Enable clock */
93 1.1 jmcneill sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
94 1.1 jmcneill if (sc->sc_clk == NULL) {
95 1.1 jmcneill aprint_error(": couldn't acquire clock\n");
96 1.1 jmcneill return;
97 1.1 jmcneill }
98 1.1 jmcneill
99 1.1 jmcneill if (clk_enable(sc->sc_clk) != 0) {
100 1.1 jmcneill aprint_error(": failed to enable clock\n");
101 1.1 jmcneill return;
102 1.1 jmcneill }
103 1.1 jmcneill
104 1.1 jmcneill sc->sc_frequency = clk_get_rate(sc->sc_clk);
105 1.1 jmcneill
106 1.1 jmcneill if (of_getprop_uint32(phandle, "clock-frequency",
107 1.1 jmcneill &sc->sc_clkrate) != 0) {
108 1.1 jmcneill sc->sc_clkrate = 100000;
109 1.1 jmcneill }
110 1.1 jmcneill
111 1.1 jmcneill if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
112 1.1 jmcneill aprint_error(": unable to map device\n");
113 1.1 jmcneill return;
114 1.1 jmcneill }
115 1.1 jmcneill
116 1.1 jmcneill aprint_naive("\n");
117 1.1 jmcneill aprint_normal(": Broadcom Serial Controller\n");
118 1.1 jmcneill
119 1.1 jmcneill bsciic_attach(sc);
120 1.1 jmcneill
121 1.1 jmcneill char intrstr[128];
122 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
123 1.1 jmcneill aprint_error_dev(sc->sc_dev, "failed to decode interrupt\n");
124 1.1 jmcneill return;
125 1.1 jmcneill }
126 1.1 jmcneill sc->sc_inth = fdtbus_intr_establish(phandle, 0, IPL_VM,
127 1.1 jmcneill FDT_INTR_MPSAFE, bsciic_intr, sc);
128 1.1 jmcneill if (sc->sc_inth == NULL) {
129 1.1 jmcneill aprint_error_dev(sc->sc_dev,
130 1.1 jmcneill "failed to establish interrupt %s\n", intrstr);
131 1.1 jmcneill return;
132 1.1 jmcneill }
133 1.1 jmcneill aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
134 1.1 jmcneill
135 1.1 jmcneill iic_tag_init(&sc->sc_i2c);
136 1.1 jmcneill sc->sc_i2c.ic_cookie = sc;
137 1.1 jmcneill sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
138 1.1 jmcneill sc->sc_i2c.ic_release_bus = bsciic_release_bus;
139 1.1 jmcneill sc->sc_i2c.ic_exec = bsciic_exec;
140 1.1 jmcneill
141 1.1 jmcneill fdtbus_attach_i2cbus(self, phandle, &sc->sc_i2c, iicbus_print);
142 1.1 jmcneill }
143