imx6_pcie.c revision 1.1.2.3 1 1.1.2.3 thorpej /* $NetBSD: imx6_pcie.c,v 1.1.2.3 2021/04/03 22:28:17 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 2019 Genetec Corporation. All rights reserved.
5 1.1.2.2 thorpej * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.2.2 thorpej * are met:
10 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.2.2 thorpej *
16 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.2.2 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.2.2 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.2.2 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.2.2 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 thorpej * SUCH DAMAGE.
27 1.1.2.2 thorpej */
28 1.1.2.2 thorpej
29 1.1.2.2 thorpej #include <sys/cdefs.h>
30 1.1.2.3 thorpej __KERNEL_RCSID(0, "$NetBSD: imx6_pcie.c,v 1.1.2.3 2021/04/03 22:28:17 thorpej Exp $");
31 1.1.2.2 thorpej
32 1.1.2.2 thorpej #include "opt_pci.h"
33 1.1.2.2 thorpej #include "opt_fdt.h"
34 1.1.2.2 thorpej
35 1.1.2.2 thorpej #include "pci.h"
36 1.1.2.2 thorpej #include "locators.h"
37 1.1.2.2 thorpej
38 1.1.2.2 thorpej #define _INTR_PRIVATE
39 1.1.2.2 thorpej
40 1.1.2.2 thorpej #include <sys/bus.h>
41 1.1.2.2 thorpej #include <sys/device.h>
42 1.1.2.2 thorpej #include <sys/intr.h>
43 1.1.2.2 thorpej #include <sys/systm.h>
44 1.1.2.2 thorpej #include <sys/param.h>
45 1.1.2.2 thorpej #include <sys/kernel.h>
46 1.1.2.2 thorpej #include <sys/queue.h>
47 1.1.2.2 thorpej #include <sys/mutex.h>
48 1.1.2.2 thorpej #include <sys/kmem.h>
49 1.1.2.2 thorpej #include <sys/gpio.h>
50 1.1.2.2 thorpej
51 1.1.2.2 thorpej #include <machine/frame.h>
52 1.1.2.2 thorpej #include <arm/cpufunc.h>
53 1.1.2.2 thorpej
54 1.1.2.2 thorpej #include <dev/fdt/fdtvar.h>
55 1.1.2.2 thorpej
56 1.1.2.2 thorpej #include <dev/pci/pcireg.h>
57 1.1.2.2 thorpej #include <dev/pci/pcivar.h>
58 1.1.2.2 thorpej #include <dev/pci/pciconf.h>
59 1.1.2.2 thorpej
60 1.1.2.2 thorpej #include <arm/imx/imxpcievar.h>
61 1.1.2.2 thorpej #include <arm/imx/imxgpioreg.h>
62 1.1.2.2 thorpej #include <arm/imx/imxgpiovar.h>
63 1.1.2.2 thorpej #include <arm/nxp/imx6_iomuxreg.h>
64 1.1.2.2 thorpej #include <arm/nxp/imx6_ccmreg.h>
65 1.1.2.2 thorpej #include <arm/nxp/imx6_ccmvar.h>
66 1.1.2.2 thorpej
67 1.1.2.2 thorpej struct imxpcie_fdt_softc {
68 1.1.2.2 thorpej struct imxpcie_softc sc_imxpcie;
69 1.1.2.2 thorpej
70 1.1.2.2 thorpej struct fdtbus_gpio_pin *sc_pin_reset;
71 1.1.2.2 thorpej struct fdtbus_regulator *sc_reg_vpcie;
72 1.1.2.2 thorpej };
73 1.1.2.2 thorpej
74 1.1.2.2 thorpej static int imx6_pcie_match(device_t, cfdata_t, void *);
75 1.1.2.2 thorpej static void imx6_pcie_attach(device_t, device_t, void *);
76 1.1.2.2 thorpej
77 1.1.2.2 thorpej static void imx6_pcie_configure(void *);
78 1.1.2.2 thorpej static uint32_t imx6_pcie_gpr_read(void *, uint32_t);
79 1.1.2.2 thorpej static void imx6_pcie_gpr_write(void *, uint32_t, uint32_t);
80 1.1.2.2 thorpej static void imx6_pcie_reset(void *);
81 1.1.2.2 thorpej
82 1.1.2.2 thorpej #define IMX6_PCIE_MEM_BASE 0x01000000
83 1.1.2.2 thorpej #define IMX6_PCIE_MEM_SIZE 0x00f00000 /* 15MB */
84 1.1.2.2 thorpej #define IMX6_PCIE_ROOT_BASE 0x01f00000
85 1.1.2.2 thorpej #define IMX6_PCIE_ROOT_SIZE 0x00080000 /* 512KB */
86 1.1.2.2 thorpej #define IMX6_PCIE_IO_BASE 0x01f80000
87 1.1.2.2 thorpej #define IMX6_PCIE_IO_SIZE 0x00010000 /* 64KB */
88 1.1.2.2 thorpej
89 1.1.2.2 thorpej CFATTACH_DECL_NEW(imxpcie_fdt, sizeof(struct imxpcie_fdt_softc),
90 1.1.2.2 thorpej imx6_pcie_match, imx6_pcie_attach, NULL, NULL);
91 1.1.2.2 thorpej
92 1.1.2.3 thorpej static const struct device_compatible_entry compat_data[] = {
93 1.1.2.3 thorpej { .compat = "fsl,imx6q-pcie", .value = false },
94 1.1.2.3 thorpej { .compat = "fsl,imx6qp-pcie", .value = true },
95 1.1.2.3 thorpej DEVICE_COMPAT_EOL
96 1.1.2.2 thorpej };
97 1.1.2.2 thorpej
98 1.1.2.2 thorpej static int
99 1.1.2.2 thorpej imx6_pcie_match(device_t parent, cfdata_t cf, void *aux)
100 1.1.2.2 thorpej {
101 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
102 1.1.2.2 thorpej
103 1.1.2.3 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
104 1.1.2.2 thorpej }
105 1.1.2.2 thorpej
106 1.1.2.2 thorpej static void
107 1.1.2.2 thorpej imx6_pcie_attach(device_t parent, device_t self, void *aux)
108 1.1.2.2 thorpej {
109 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = device_private(self);
110 1.1.2.2 thorpej struct imxpcie_softc * const sc = &ifsc->sc_imxpcie;
111 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
112 1.1.2.2 thorpej const int phandle = faa->faa_phandle;
113 1.1.2.2 thorpej bus_space_tag_t bst = faa->faa_bst;
114 1.1.2.2 thorpej char intrstr[128];
115 1.1.2.2 thorpej bus_addr_t addr;
116 1.1.2.2 thorpej bus_size_t size;
117 1.1.2.2 thorpej
118 1.1.2.2 thorpej aprint_naive("\n");
119 1.1.2.2 thorpej aprint_normal(": PCI Express Controller\n");
120 1.1.2.2 thorpej
121 1.1.2.2 thorpej sc->sc_dev = self;
122 1.1.2.2 thorpej sc->sc_iot = bst;
123 1.1.2.2 thorpej sc->sc_dmat = faa->faa_dmat;
124 1.1.2.2 thorpej sc->sc_cookie = ifsc;
125 1.1.2.2 thorpej sc->sc_pci_netbsd_configure = imx6_pcie_configure;
126 1.1.2.2 thorpej sc->sc_gpr_read = imx6_pcie_gpr_read;
127 1.1.2.2 thorpej sc->sc_gpr_write = imx6_pcie_gpr_write;
128 1.1.2.2 thorpej sc->sc_reset = imx6_pcie_reset;
129 1.1.2.3 thorpej sc->sc_have_sw_reset =
130 1.1.2.3 thorpej (bool)of_compatible_lookup(phandle, compat_data)->value;
131 1.1.2.2 thorpej
132 1.1.2.2 thorpej if (fdtbus_get_reg_byname(phandle, "dbi", &addr, &size) != 0) {
133 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
134 1.1.2.2 thorpej return;
135 1.1.2.2 thorpej }
136 1.1.2.2 thorpej if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
137 1.1.2.2 thorpej aprint_error_dev(self, "Cannot map registers\n");
138 1.1.2.2 thorpej return;
139 1.1.2.2 thorpej }
140 1.1.2.2 thorpej if (fdtbus_get_reg_byname(phandle, "config", &addr, &size) != 0) {
141 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
142 1.1.2.2 thorpej return;
143 1.1.2.2 thorpej }
144 1.1.2.2 thorpej sc->sc_root_addr = addr;
145 1.1.2.2 thorpej sc->sc_root_size = size;
146 1.1.2.2 thorpej
147 1.1.2.2 thorpej const int gpr_phandle = OF_finddevice("/soc/aips-bus/iomuxc-gpr");
148 1.1.2.2 thorpej fdtbus_get_reg(gpr_phandle, 0, &addr, &size);
149 1.1.2.2 thorpej if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_gpr_ioh)) {
150 1.1.2.2 thorpej aprint_error_dev(self, "Cannot map registers\n");
151 1.1.2.2 thorpej return;
152 1.1.2.2 thorpej }
153 1.1.2.2 thorpej
154 1.1.2.2 thorpej ifsc->sc_pin_reset = fdtbus_gpio_acquire(phandle, "reset-gpio",
155 1.1.2.2 thorpej GPIO_PIN_OUTPUT);
156 1.1.2.2 thorpej if (!ifsc->sc_pin_reset) {
157 1.1.2.2 thorpej aprint_error(": couldn't acquire reset gpio\n");
158 1.1.2.2 thorpej return;
159 1.1.2.2 thorpej }
160 1.1.2.2 thorpej
161 1.1.2.2 thorpej sc->sc_clk_pcie = fdtbus_clock_get(phandle, "pcie");
162 1.1.2.2 thorpej if (sc->sc_clk_pcie == NULL) {
163 1.1.2.2 thorpej aprint_error(": couldn't get clock pcie_axi\n");
164 1.1.2.2 thorpej return;
165 1.1.2.2 thorpej }
166 1.1.2.2 thorpej sc->sc_clk_pcie_bus = fdtbus_clock_get(phandle, "pcie_bus");
167 1.1.2.2 thorpej if (sc->sc_clk_pcie_bus == NULL) {
168 1.1.2.2 thorpej aprint_error(": couldn't get clock lvds1_gate\n");
169 1.1.2.2 thorpej return;
170 1.1.2.2 thorpej }
171 1.1.2.2 thorpej sc->sc_clk_pcie_phy = fdtbus_clock_get(phandle, "pcie_phy");
172 1.1.2.2 thorpej if (sc->sc_clk_pcie_phy == NULL) {
173 1.1.2.2 thorpej aprint_error(": couldn't get clock pcie_ref\n");
174 1.1.2.2 thorpej return;
175 1.1.2.2 thorpej }
176 1.1.2.2 thorpej
177 1.1.2.2 thorpej if (of_hasprop(phandle, "vpcie-supply")) {
178 1.1.2.2 thorpej ifsc->sc_reg_vpcie = fdtbus_regulator_acquire(phandle, "vpcie-supply");
179 1.1.2.2 thorpej if (ifsc->sc_reg_vpcie == NULL) {
180 1.1.2.2 thorpej aprint_error(": couldn't acquire regulator\n");
181 1.1.2.2 thorpej return;
182 1.1.2.2 thorpej }
183 1.1.2.2 thorpej aprint_normal_dev(self, "regulator On\n");
184 1.1.2.2 thorpej fdtbus_regulator_enable(ifsc->sc_reg_vpcie);
185 1.1.2.2 thorpej }
186 1.1.2.2 thorpej
187 1.1.2.2 thorpej if (of_hasprop(phandle, "ext_osc")) {
188 1.1.2.2 thorpej aprint_normal_dev(self, "Use external OSC\n");
189 1.1.2.2 thorpej sc->sc_ext_osc = true;
190 1.1.2.2 thorpej
191 1.1.2.2 thorpej sc->sc_clk_pcie_ext = fdtbus_clock_get(phandle, "pcie_ext");
192 1.1.2.2 thorpej if (sc->sc_clk_pcie_ext == NULL) {
193 1.1.2.2 thorpej aprint_error(": couldn't get clock pcie_ext\n");
194 1.1.2.2 thorpej return;
195 1.1.2.2 thorpej }
196 1.1.2.2 thorpej sc->sc_clk_pcie_ext_src = fdtbus_clock_get(phandle, "pcie_ext_src");
197 1.1.2.2 thorpej if (sc->sc_clk_pcie_ext_src == NULL) {
198 1.1.2.2 thorpej aprint_error(": couldn't get clock pcie_ext_src\n");
199 1.1.2.2 thorpej return;
200 1.1.2.2 thorpej }
201 1.1.2.2 thorpej } else {
202 1.1.2.2 thorpej sc->sc_ext_osc = false;
203 1.1.2.2 thorpej sc->sc_clk_pcie_ext = NULL;
204 1.1.2.2 thorpej sc->sc_clk_pcie_ext_src = NULL;
205 1.1.2.2 thorpej }
206 1.1.2.2 thorpej
207 1.1.2.2 thorpej if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
208 1.1.2.2 thorpej aprint_error_dev(self, "failed to decode interrupt\n");
209 1.1.2.2 thorpej return;
210 1.1.2.2 thorpej }
211 1.1.2.2 thorpej
212 1.1.2.3 thorpej sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
213 1.1.2.3 thorpej FDT_INTR_MPSAFE, imxpcie_intr, sc, device_xname(self));
214 1.1.2.2 thorpej if (sc->sc_ih == NULL) {
215 1.1.2.2 thorpej aprint_error_dev(self, "failed to establish interrupt on %s\n",
216 1.1.2.2 thorpej intrstr);
217 1.1.2.2 thorpej return;
218 1.1.2.2 thorpej }
219 1.1.2.2 thorpej aprint_normal_dev(self, "interrupting on %s\n", intrstr);
220 1.1.2.2 thorpej
221 1.1.2.2 thorpej imxpcie_attach_common(sc);
222 1.1.2.2 thorpej }
223 1.1.2.2 thorpej
224 1.1.2.2 thorpej static void
225 1.1.2.2 thorpej imx6_pcie_configure(void *cookie)
226 1.1.2.2 thorpej {
227 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = cookie;
228 1.1.2.2 thorpej struct imxpcie_softc * const sc = &ifsc->sc_imxpcie;
229 1.1.2.2 thorpej
230 1.1.2.2 thorpej #ifdef PCI_NETBSD_CONFIGURE
231 1.1.2.2 thorpej struct pciconf_resources *pcires = pciconf_resource_init();
232 1.1.2.2 thorpej
233 1.1.2.2 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
234 1.1.2.2 thorpej IMX6_PCIE_IO_BASE, IMX6_PCIE_IO_SIZE);
235 1.1.2.2 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
236 1.1.2.2 thorpej IMX6_PCIE_MEM_BASE, IMX6_PCIE_MEM_SIZE);
237 1.1.2.2 thorpej
238 1.1.2.2 thorpej int error = pci_configure_bus(&sc->sc_pc, pcires, 0, arm_dcache_align);
239 1.1.2.2 thorpej
240 1.1.2.2 thorpej pciconf_resource_fini(pcires);
241 1.1.2.2 thorpej
242 1.1.2.2 thorpej if (error) {
243 1.1.2.2 thorpej aprint_error_dev(sc->sc_dev, "configuration failed (%d)\n",
244 1.1.2.2 thorpej error);
245 1.1.2.2 thorpej }
246 1.1.2.2 thorpej #endif
247 1.1.2.2 thorpej }
248 1.1.2.2 thorpej
249 1.1.2.2 thorpej static uint32_t
250 1.1.2.2 thorpej imx6_pcie_gpr_read(void *cookie, uint32_t reg)
251 1.1.2.2 thorpej {
252 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = cookie;
253 1.1.2.2 thorpej struct imxpcie_softc * const sc = &ifsc->sc_imxpcie;
254 1.1.2.2 thorpej return bus_space_read_4(sc->sc_iot, sc->sc_gpr_ioh, reg);
255 1.1.2.2 thorpej }
256 1.1.2.2 thorpej
257 1.1.2.2 thorpej static void
258 1.1.2.2 thorpej imx6_pcie_gpr_write(void *cookie, uint32_t reg, uint32_t val)
259 1.1.2.2 thorpej {
260 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = cookie;
261 1.1.2.2 thorpej struct imxpcie_softc * const sc = &ifsc->sc_imxpcie;
262 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_gpr_ioh, reg, val);
263 1.1.2.2 thorpej }
264 1.1.2.2 thorpej
265 1.1.2.2 thorpej static void
266 1.1.2.2 thorpej imx6_pcie_reset(void *cookie)
267 1.1.2.2 thorpej {
268 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = cookie;
269 1.1.2.2 thorpej
270 1.1.2.2 thorpej fdtbus_gpio_write(ifsc->sc_pin_reset, 1);
271 1.1.2.2 thorpej delay(20 * 1000);
272 1.1.2.2 thorpej fdtbus_gpio_write(ifsc->sc_pin_reset, 0);
273 1.1.2.2 thorpej }
274