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