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