imx6_spi.c revision 1.1 1 /* $NetBSD: imx6_spi.c,v 1.1 2020/12/23 14:42:38 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Genetec Corporation. All rights reserved.
5 * Written by Hashimoto Kenichi for Genetec Corporation.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: imx6_spi.c,v 1.1 2020/12/23 14:42:38 skrll Exp $");
31
32 #include "opt_imxspi.h"
33
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/kmem.h>
38 #include <sys/gpio.h>
39
40 #include <arm/imx/imxspivar.h>
41
42 #include <dev/fdt/fdtvar.h>
43
44 struct imxspi_fdt_softc {
45 struct imxspi_softc sc_imxspi; /* Must be first */
46
47 struct spi_chipset_tag sc_tag;
48 struct clk *sc_clk;
49
50 struct fdtbus_gpio_pin **sc_pin_cs;
51 };
52
53 struct imx_spi_config {
54 bool enhanced;
55 enum imxspi_type type;
56 };
57
58 static const struct imx_spi_config imx6q_spi_config = {
59 .enhanced = true,
60 .type = IMX51_ECSPI,
61 };
62
63 static const struct of_compat_data compat_data[] = {
64 { "fsl,imx6q-ecspi", (uintptr_t)&imx6q_spi_config },
65 { NULL }
66 };
67
68 CFATTACH_DECL_NEW(imxspi_fdt, sizeof(struct imxspi_fdt_softc),
69 imxspi_match, imxspi_attach, NULL, NULL);
70
71 static int
72 imxspi_cs_enable(void *arg, int slave)
73 {
74 struct imxspi_fdt_softc * const sc = arg;
75 fdtbus_gpio_write(sc->sc_pin_cs[slave], 1);
76 return 0;
77 }
78
79 static int
80 imxspi_cs_disable(void *arg, int slave)
81 {
82 struct imxspi_fdt_softc * const sc = arg;
83 fdtbus_gpio_write(sc->sc_pin_cs[slave], 0);
84 return 0;
85 }
86
87 int
88 imxspi_match(device_t parent, cfdata_t cf, void *aux)
89 {
90 struct fdt_attach_args * const faa = aux;
91
92 return of_match_compat_data(faa->faa_phandle, compat_data);
93 }
94
95 void
96 imxspi_attach(device_t parent, device_t self, void *aux)
97 {
98 struct imxspi_fdt_softc * const ifsc = device_private(self);
99 struct imxspi_softc * const sc = &ifsc->sc_imxspi;
100 struct fdt_attach_args * const faa = aux;
101 char intrstr[128];
102 const int phandle = faa->faa_phandle;
103 bus_addr_t addr;
104 bus_size_t size;
105 int error;
106
107 u_int nslaves;
108 error = of_getprop_uint32(phandle, "fsl,spi-num-chipselects", &nslaves);
109 if (error)
110 nslaves = 4;
111
112 ifsc->sc_pin_cs = kmem_alloc(sizeof(struct fdtbus_gpio_pin *) * nslaves, KM_SLEEP);
113
114 for (int i = 0; i < nslaves; i++) {
115 ifsc->sc_pin_cs[i] = fdtbus_gpio_acquire_index(phandle, "cs-gpios", i,
116 GPIO_PIN_OUTPUT);
117 }
118
119 ifsc->sc_clk = fdtbus_clock_get_index(phandle, 0);
120 if (ifsc->sc_clk == NULL) {
121 aprint_error(": couldn't get clock\n");
122 return;
123 }
124
125 error = clk_enable(ifsc->sc_clk);
126 if (error) {
127 aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error);
128 return;
129 }
130
131 ifsc->sc_tag.cookie = ifsc;
132 ifsc->sc_tag.spi_cs_enable = imxspi_cs_enable;
133 ifsc->sc_tag.spi_cs_disable = imxspi_cs_disable;
134
135 sc->sc_phandle = phandle;
136 sc->sc_iot = faa->faa_bst;
137
138 struct imx_spi_config *config = (void *)of_search_compatible(phandle, compat_data)->data;
139 sc->sc_enhanced = config->enhanced;
140 sc->sc_type = config->type;
141
142 sc->sc_nslaves = nslaves;
143 sc->sc_freq = clk_get_rate(ifsc->sc_clk);
144 sc->sc_tag = &ifsc->sc_tag;
145
146 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
147 aprint_error(": couldn't get iomux registers\n");
148 return;
149 }
150
151 if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
152 aprint_error_dev(sc->sc_dev, "couldn't map registers\n");
153 return;
154 }
155
156 if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
157 aprint_error_dev(self, "failed to decode interrupt\n");
158 return;
159 }
160
161 sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM,
162 0, imxspi_intr, &ifsc->sc_imxspi);
163 if (sc->sc_ih == NULL) {
164 aprint_error_dev(self, "couldn't establish interrupt on %s\n",
165 intrstr);
166 return;
167 }
168 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
169
170 imxspi_attach_common(self);
171 }
172