imx6_pcie.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: imx6_pcie.c,v 1.1.2.2 2021/01/03 16:34:52 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.2 thorpej __KERNEL_RCSID(0, "$NetBSD: imx6_pcie.c,v 1.1.2.2 2021/01/03 16:34:52 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.2 thorpej static const struct of_compat_data compat_data[] = {
93 1.1.2.2 thorpej { "fsl,imx6q-pcie", false },
94 1.1.2.2 thorpej { "fsl,imx6qp-pcie", true },
95 1.1.2.2 thorpej { NULL }
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.2 thorpej return of_match_compat_data(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.2 thorpej sc->sc_have_sw_reset = of_search_compatible(phandle, compat_data)->data;
130 1.1.2.2 thorpej
131 1.1.2.2 thorpej if (fdtbus_get_reg_byname(phandle, "dbi", &addr, &size) != 0) {
132 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
133 1.1.2.2 thorpej return;
134 1.1.2.2 thorpej }
135 1.1.2.2 thorpej if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
136 1.1.2.2 thorpej aprint_error_dev(self, "Cannot map registers\n");
137 1.1.2.2 thorpej return;
138 1.1.2.2 thorpej }
139 1.1.2.2 thorpej if (fdtbus_get_reg_byname(phandle, "config", &addr, &size) != 0) {
140 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
141 1.1.2.2 thorpej return;
142 1.1.2.2 thorpej }
143 1.1.2.2 thorpej sc->sc_root_addr = addr;
144 1.1.2.2 thorpej sc->sc_root_size = size;
145 1.1.2.2 thorpej
146 1.1.2.2 thorpej const int gpr_phandle = OF_finddevice("/soc/aips-bus/iomuxc-gpr");
147 1.1.2.2 thorpej fdtbus_get_reg(gpr_phandle, 0, &addr, &size);
148 1.1.2.2 thorpej if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_gpr_ioh)) {
149 1.1.2.2 thorpej aprint_error_dev(self, "Cannot map registers\n");
150 1.1.2.2 thorpej return;
151 1.1.2.2 thorpej }
152 1.1.2.2 thorpej
153 1.1.2.2 thorpej ifsc->sc_pin_reset = fdtbus_gpio_acquire(phandle, "reset-gpio",
154 1.1.2.2 thorpej GPIO_PIN_OUTPUT);
155 1.1.2.2 thorpej if (!ifsc->sc_pin_reset) {
156 1.1.2.2 thorpej aprint_error(": couldn't acquire reset gpio\n");
157 1.1.2.2 thorpej return;
158 1.1.2.2 thorpej }
159 1.1.2.2 thorpej
160 1.1.2.2 thorpej sc->sc_clk_pcie = fdtbus_clock_get(phandle, "pcie");
161 1.1.2.2 thorpej if (sc->sc_clk_pcie == NULL) {
162 1.1.2.2 thorpej aprint_error(": couldn't get clock pcie_axi\n");
163 1.1.2.2 thorpej return;
164 1.1.2.2 thorpej }
165 1.1.2.2 thorpej sc->sc_clk_pcie_bus = fdtbus_clock_get(phandle, "pcie_bus");
166 1.1.2.2 thorpej if (sc->sc_clk_pcie_bus == NULL) {
167 1.1.2.2 thorpej aprint_error(": couldn't get clock lvds1_gate\n");
168 1.1.2.2 thorpej return;
169 1.1.2.2 thorpej }
170 1.1.2.2 thorpej sc->sc_clk_pcie_phy = fdtbus_clock_get(phandle, "pcie_phy");
171 1.1.2.2 thorpej if (sc->sc_clk_pcie_phy == NULL) {
172 1.1.2.2 thorpej aprint_error(": couldn't get clock pcie_ref\n");
173 1.1.2.2 thorpej return;
174 1.1.2.2 thorpej }
175 1.1.2.2 thorpej
176 1.1.2.2 thorpej if (of_hasprop(phandle, "vpcie-supply")) {
177 1.1.2.2 thorpej ifsc->sc_reg_vpcie = fdtbus_regulator_acquire(phandle, "vpcie-supply");
178 1.1.2.2 thorpej if (ifsc->sc_reg_vpcie == NULL) {
179 1.1.2.2 thorpej aprint_error(": couldn't acquire regulator\n");
180 1.1.2.2 thorpej return;
181 1.1.2.2 thorpej }
182 1.1.2.2 thorpej aprint_normal_dev(self, "regulator On\n");
183 1.1.2.2 thorpej fdtbus_regulator_enable(ifsc->sc_reg_vpcie);
184 1.1.2.2 thorpej }
185 1.1.2.2 thorpej
186 1.1.2.2 thorpej if (of_hasprop(phandle, "ext_osc")) {
187 1.1.2.2 thorpej aprint_normal_dev(self, "Use external OSC\n");
188 1.1.2.2 thorpej sc->sc_ext_osc = true;
189 1.1.2.2 thorpej
190 1.1.2.2 thorpej sc->sc_clk_pcie_ext = fdtbus_clock_get(phandle, "pcie_ext");
191 1.1.2.2 thorpej if (sc->sc_clk_pcie_ext == NULL) {
192 1.1.2.2 thorpej aprint_error(": couldn't get clock pcie_ext\n");
193 1.1.2.2 thorpej return;
194 1.1.2.2 thorpej }
195 1.1.2.2 thorpej sc->sc_clk_pcie_ext_src = fdtbus_clock_get(phandle, "pcie_ext_src");
196 1.1.2.2 thorpej if (sc->sc_clk_pcie_ext_src == NULL) {
197 1.1.2.2 thorpej aprint_error(": couldn't get clock pcie_ext_src\n");
198 1.1.2.2 thorpej return;
199 1.1.2.2 thorpej }
200 1.1.2.2 thorpej } else {
201 1.1.2.2 thorpej sc->sc_ext_osc = false;
202 1.1.2.2 thorpej sc->sc_clk_pcie_ext = NULL;
203 1.1.2.2 thorpej sc->sc_clk_pcie_ext_src = NULL;
204 1.1.2.2 thorpej }
205 1.1.2.2 thorpej
206 1.1.2.2 thorpej if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
207 1.1.2.2 thorpej aprint_error_dev(self, "failed to decode interrupt\n");
208 1.1.2.2 thorpej return;
209 1.1.2.2 thorpej }
210 1.1.2.2 thorpej
211 1.1.2.2 thorpej sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM,
212 1.1.2.2 thorpej FDT_INTR_MPSAFE, imxpcie_intr, sc);
213 1.1.2.2 thorpej if (sc->sc_ih == NULL) {
214 1.1.2.2 thorpej aprint_error_dev(self, "failed to establish interrupt on %s\n",
215 1.1.2.2 thorpej intrstr);
216 1.1.2.2 thorpej return;
217 1.1.2.2 thorpej }
218 1.1.2.2 thorpej aprint_normal_dev(self, "interrupting on %s\n", intrstr);
219 1.1.2.2 thorpej
220 1.1.2.2 thorpej imxpcie_attach_common(sc);
221 1.1.2.2 thorpej }
222 1.1.2.2 thorpej
223 1.1.2.2 thorpej static void
224 1.1.2.2 thorpej imx6_pcie_configure(void *cookie)
225 1.1.2.2 thorpej {
226 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = cookie;
227 1.1.2.2 thorpej struct imxpcie_softc * const sc = &ifsc->sc_imxpcie;
228 1.1.2.2 thorpej
229 1.1.2.2 thorpej #ifdef PCI_NETBSD_CONFIGURE
230 1.1.2.2 thorpej struct pciconf_resources *pcires = pciconf_resource_init();
231 1.1.2.2 thorpej
232 1.1.2.2 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
233 1.1.2.2 thorpej IMX6_PCIE_IO_BASE, IMX6_PCIE_IO_SIZE);
234 1.1.2.2 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
235 1.1.2.2 thorpej IMX6_PCIE_MEM_BASE, IMX6_PCIE_MEM_SIZE);
236 1.1.2.2 thorpej
237 1.1.2.2 thorpej int error = pci_configure_bus(&sc->sc_pc, pcires, 0, arm_dcache_align);
238 1.1.2.2 thorpej
239 1.1.2.2 thorpej pciconf_resource_fini(pcires);
240 1.1.2.2 thorpej
241 1.1.2.2 thorpej if (error) {
242 1.1.2.2 thorpej aprint_error_dev(sc->sc_dev, "configuration failed (%d)\n",
243 1.1.2.2 thorpej error);
244 1.1.2.2 thorpej }
245 1.1.2.2 thorpej #endif
246 1.1.2.2 thorpej }
247 1.1.2.2 thorpej
248 1.1.2.2 thorpej static uint32_t
249 1.1.2.2 thorpej imx6_pcie_gpr_read(void *cookie, uint32_t reg)
250 1.1.2.2 thorpej {
251 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = cookie;
252 1.1.2.2 thorpej struct imxpcie_softc * const sc = &ifsc->sc_imxpcie;
253 1.1.2.2 thorpej return bus_space_read_4(sc->sc_iot, sc->sc_gpr_ioh, reg);
254 1.1.2.2 thorpej }
255 1.1.2.2 thorpej
256 1.1.2.2 thorpej static void
257 1.1.2.2 thorpej imx6_pcie_gpr_write(void *cookie, uint32_t reg, uint32_t val)
258 1.1.2.2 thorpej {
259 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = cookie;
260 1.1.2.2 thorpej struct imxpcie_softc * const sc = &ifsc->sc_imxpcie;
261 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_gpr_ioh, reg, val);
262 1.1.2.2 thorpej }
263 1.1.2.2 thorpej
264 1.1.2.2 thorpej static void
265 1.1.2.2 thorpej imx6_pcie_reset(void *cookie)
266 1.1.2.2 thorpej {
267 1.1.2.2 thorpej struct imxpcie_fdt_softc * const ifsc = cookie;
268 1.1.2.2 thorpej
269 1.1.2.2 thorpej fdtbus_gpio_write(ifsc->sc_pin_reset, 1);
270 1.1.2.2 thorpej delay(20 * 1000);
271 1.1.2.2 thorpej fdtbus_gpio_write(ifsc->sc_pin_reset, 0);
272 1.1.2.2 thorpej }
273