1896e217aSJessica Clarke /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3896e217aSJessica Clarke *
4896e217aSJessica Clarke * Copyright 2021 Jessica Clarke <jrtc27@FreeBSD.org>
5896e217aSJessica Clarke *
6896e217aSJessica Clarke * Redistribution and use in source and binary forms, with or without
7896e217aSJessica Clarke * modification, are permitted provided that the following conditions
8896e217aSJessica Clarke * are met:
9896e217aSJessica Clarke * 1. Redistributions of source code must retain the above copyright
10896e217aSJessica Clarke * notice, this list of conditions and the following disclaimer.
11896e217aSJessica Clarke * 2. Redistributions in binary form must reproduce the above copyright
12896e217aSJessica Clarke * notice, this list of conditions and the following disclaimer in the
13896e217aSJessica Clarke * documentation and/or other materials provided with the distribution.
14896e217aSJessica Clarke *
15896e217aSJessica Clarke * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16896e217aSJessica Clarke * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17896e217aSJessica Clarke * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18896e217aSJessica Clarke * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19896e217aSJessica Clarke * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20896e217aSJessica Clarke * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21896e217aSJessica Clarke * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22896e217aSJessica Clarke * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23896e217aSJessica Clarke * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24896e217aSJessica Clarke * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25896e217aSJessica Clarke * SUCH DAMAGE.
26896e217aSJessica Clarke *
27896e217aSJessica Clarke */
28896e217aSJessica Clarke
29896e217aSJessica Clarke /* SiFive FU740 DesignWare PCIe driver */
30896e217aSJessica Clarke
31896e217aSJessica Clarke #include <sys/param.h>
32896e217aSJessica Clarke #include <sys/systm.h>
33896e217aSJessica Clarke #include <sys/bus.h>
34896e217aSJessica Clarke #include <sys/gpio.h>
35896e217aSJessica Clarke #include <sys/kernel.h>
36896e217aSJessica Clarke #include <sys/module.h>
37896e217aSJessica Clarke #include <sys/rman.h>
38896e217aSJessica Clarke
39896e217aSJessica Clarke #include <machine/bus.h>
40896e217aSJessica Clarke #include <machine/intr.h>
41896e217aSJessica Clarke #include <machine/resource.h>
42896e217aSJessica Clarke
43be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
441f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
45896e217aSJessica Clarke #include <dev/gpio/gpiobusvar.h>
46896e217aSJessica Clarke #include <dev/ofw/ofw_bus.h>
47896e217aSJessica Clarke #include <dev/ofw/ofw_bus_subr.h>
48896e217aSJessica Clarke #include <dev/ofw/ofw_pci.h>
49896e217aSJessica Clarke #include <dev/ofw/ofwpci.h>
50896e217aSJessica Clarke #include <dev/pci/pcivar.h>
51896e217aSJessica Clarke #include <dev/pci/pcireg.h>
52896e217aSJessica Clarke #include <dev/pci/pcib_private.h>
53896e217aSJessica Clarke #include <dev/pci/pci_dw.h>
54896e217aSJessica Clarke
55896e217aSJessica Clarke #include "pcib_if.h"
56896e217aSJessica Clarke #include "pci_dw_if.h"
57896e217aSJessica Clarke
58896e217aSJessica Clarke #define FUDW_PHYS 2
59896e217aSJessica Clarke #define FUDW_LANES_PER_PHY 4
60896e217aSJessica Clarke
61896e217aSJessica Clarke #define FUDW_MGMT_PERST_N 0x0
62896e217aSJessica Clarke #define FUDW_MGMT_LTSSM_EN 0x10
63896e217aSJessica Clarke #define FUDW_MGMT_HOLD_PHY_RST 0x18
64896e217aSJessica Clarke #define FUDW_MGMT_DEVICE_TYPE 0x708
65896e217aSJessica Clarke #define FUDW_MGMT_DEVICE_TYPE_RC 0x4
66896e217aSJessica Clarke #define FUDW_MGMT_PHY_CR_PARA_REG(_n, _r) \
67896e217aSJessica Clarke (0x860 + (_n) * 0x40 + FUDW_MGMT_PHY_CR_PARA_##_r)
68896e217aSJessica Clarke #define FUDW_MGMT_PHY_CR_PARA_ADDR 0x0
69896e217aSJessica Clarke #define FUDW_MGMT_PHY_CR_PARA_READ_EN 0x10
70896e217aSJessica Clarke #define FUDW_MGMT_PHY_CR_PARA_READ_DATA 0x18
71896e217aSJessica Clarke #define FUDW_MGMT_PHY_CR_PARA_SEL 0x20
72896e217aSJessica Clarke #define FUDW_MGMT_PHY_CR_PARA_WRITE_DATA 0x28
73896e217aSJessica Clarke #define FUDW_MGMT_PHY_CR_PARA_WRITE_EN 0x30
74896e217aSJessica Clarke #define FUDW_MGMT_PHY_CR_PARA_ACK 0x38
75896e217aSJessica Clarke
76896e217aSJessica Clarke #define FUDW_MGMT_PHY_LANE(_n) (0x1008 + (_n) * 0x100)
77896e217aSJessica Clarke #define FUDW_MGMT_PHY_LANE_CDR_TRACK_EN (1 << 0)
78896e217aSJessica Clarke #define FUDW_MGMT_PHY_LANE_LOS_THRESH (1 << 5)
79896e217aSJessica Clarke #define FUDW_MGMT_PHY_LANE_TERM_EN (1 << 9)
80896e217aSJessica Clarke #define FUDW_MGMT_PHY_LANE_TERM_ACDC (1 << 10)
81896e217aSJessica Clarke #define FUDW_MGMT_PHY_LANE_EN (1 << 11)
82896e217aSJessica Clarke #define FUDW_MGMT_PHY_LANE_INIT \
83896e217aSJessica Clarke (FUDW_MGMT_PHY_LANE_CDR_TRACK_EN | FUDW_MGMT_PHY_LANE_LOS_THRESH | \
84896e217aSJessica Clarke FUDW_MGMT_PHY_LANE_TERM_EN | FUDW_MGMT_PHY_LANE_TERM_ACDC | \
85896e217aSJessica Clarke FUDW_MGMT_PHY_LANE_EN)
86896e217aSJessica Clarke
87896e217aSJessica Clarke #define FUDW_DBI_PORT_DBG1 0x72c
88896e217aSJessica Clarke #define FUDW_DBI_PORT_DBG1_LINK_UP (1 << 4)
89896e217aSJessica Clarke #define FUDW_DBI_PORT_DBG1_LINK_IN_TRAINING (1 << 29)
90896e217aSJessica Clarke
91896e217aSJessica Clarke struct fupci_softc {
92896e217aSJessica Clarke struct pci_dw_softc dw_sc;
93896e217aSJessica Clarke device_t dev;
94896e217aSJessica Clarke struct resource *mgmt_res;
95896e217aSJessica Clarke gpio_pin_t porst_pin;
96896e217aSJessica Clarke gpio_pin_t pwren_pin;
97896e217aSJessica Clarke clk_t pcie_aux_clk;
98896e217aSJessica Clarke hwreset_t pcie_aux_rst;
99896e217aSJessica Clarke };
100896e217aSJessica Clarke
101896e217aSJessica Clarke #define FUDW_MGMT_READ(_sc, _o) bus_read_4((_sc)->mgmt_res, (_o))
102896e217aSJessica Clarke #define FUDW_MGMT_WRITE(_sc, _o, _v) bus_write_4((_sc)->mgmt_res, (_o), (_v))
103896e217aSJessica Clarke
104896e217aSJessica Clarke static struct ofw_compat_data compat_data[] = {
105896e217aSJessica Clarke { "sifive,fu740-pcie", 1 },
106896e217aSJessica Clarke { NULL, 0 },
107896e217aSJessica Clarke };
108896e217aSJessica Clarke
109896e217aSJessica Clarke /* Currently unused; included for completeness */
110896e217aSJessica Clarke static int __unused
fupci_phy_read(struct fupci_softc * sc,int phy,uint32_t reg,uint32_t * val)111896e217aSJessica Clarke fupci_phy_read(struct fupci_softc *sc, int phy, uint32_t reg, uint32_t *val)
112896e217aSJessica Clarke {
113896e217aSJessica Clarke unsigned timeout;
114896e217aSJessica Clarke uint32_t ack;
115896e217aSJessica Clarke
116896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ADDR), reg);
117896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_EN), 1);
118896e217aSJessica Clarke
119896e217aSJessica Clarke timeout = 10;
120896e217aSJessica Clarke do {
121896e217aSJessica Clarke ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK));
122896e217aSJessica Clarke if (ack != 0)
123896e217aSJessica Clarke break;
124896e217aSJessica Clarke DELAY(10);
125896e217aSJessica Clarke } while (--timeout > 0);
126896e217aSJessica Clarke
127896e217aSJessica Clarke if (timeout == 0) {
128896e217aSJessica Clarke device_printf(sc->dev, "Timeout waiting for read ACK\n");
129896e217aSJessica Clarke return (ETIMEDOUT);
130896e217aSJessica Clarke }
131896e217aSJessica Clarke
132896e217aSJessica Clarke *val = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_DATA));
133896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_EN), 0);
134896e217aSJessica Clarke
135896e217aSJessica Clarke timeout = 10;
136896e217aSJessica Clarke do {
137896e217aSJessica Clarke ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK));
138896e217aSJessica Clarke if (ack == 0)
139896e217aSJessica Clarke break;
140896e217aSJessica Clarke DELAY(10);
141896e217aSJessica Clarke } while (--timeout > 0);
142896e217aSJessica Clarke
143896e217aSJessica Clarke if (timeout == 0) {
144896e217aSJessica Clarke device_printf(sc->dev, "Timeout waiting for read un-ACK\n");
145896e217aSJessica Clarke return (ETIMEDOUT);
146896e217aSJessica Clarke }
147896e217aSJessica Clarke
148896e217aSJessica Clarke return (0);
149896e217aSJessica Clarke }
150896e217aSJessica Clarke
151896e217aSJessica Clarke static int
fupci_phy_write(struct fupci_softc * sc,int phy,uint32_t reg,uint32_t val)152896e217aSJessica Clarke fupci_phy_write(struct fupci_softc *sc, int phy, uint32_t reg, uint32_t val)
153896e217aSJessica Clarke {
154896e217aSJessica Clarke unsigned timeout;
155896e217aSJessica Clarke uint32_t ack;
156896e217aSJessica Clarke
157896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ADDR), reg);
158896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_DATA), val);
159896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_EN), 1);
160896e217aSJessica Clarke
161896e217aSJessica Clarke timeout = 10;
162896e217aSJessica Clarke do {
163896e217aSJessica Clarke ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK));
164896e217aSJessica Clarke if (ack != 0)
165896e217aSJessica Clarke break;
166896e217aSJessica Clarke DELAY(10);
167896e217aSJessica Clarke } while (--timeout > 0);
168896e217aSJessica Clarke
169896e217aSJessica Clarke if (timeout == 0) {
170896e217aSJessica Clarke device_printf(sc->dev, "Timeout waiting for write ACK\n");
171896e217aSJessica Clarke return (ETIMEDOUT);
172896e217aSJessica Clarke }
173896e217aSJessica Clarke
174896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_EN), 0);
175896e217aSJessica Clarke
176896e217aSJessica Clarke timeout = 10;
177896e217aSJessica Clarke do {
178896e217aSJessica Clarke ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK));
179896e217aSJessica Clarke if (ack == 0)
180896e217aSJessica Clarke break;
181896e217aSJessica Clarke DELAY(10);
182896e217aSJessica Clarke } while (--timeout > 0);
183896e217aSJessica Clarke
184896e217aSJessica Clarke if (timeout == 0) {
185896e217aSJessica Clarke device_printf(sc->dev, "Timeout waiting for write un-ACK\n");
186896e217aSJessica Clarke return (ETIMEDOUT);
187896e217aSJessica Clarke }
188896e217aSJessica Clarke
189896e217aSJessica Clarke return (0);
190896e217aSJessica Clarke }
191896e217aSJessica Clarke
192896e217aSJessica Clarke static int
fupci_phy_init(struct fupci_softc * sc)193896e217aSJessica Clarke fupci_phy_init(struct fupci_softc *sc)
194896e217aSJessica Clarke {
195896e217aSJessica Clarke device_t dev;
196896e217aSJessica Clarke int error, phy, lane;
197896e217aSJessica Clarke
198896e217aSJessica Clarke dev = sc->dev;
199896e217aSJessica Clarke
200896e217aSJessica Clarke /* Assert core power-on reset (active low) */
201896e217aSJessica Clarke error = gpio_pin_set_active(sc->porst_pin, false);
202896e217aSJessica Clarke if (error != 0) {
203896e217aSJessica Clarke device_printf(dev, "Cannot assert power-on reset: %d\n",
204896e217aSJessica Clarke error);
205896e217aSJessica Clarke return (error);
206896e217aSJessica Clarke }
207896e217aSJessica Clarke
208896e217aSJessica Clarke /* Assert PERST_N */
209896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PERST_N, 0);
210896e217aSJessica Clarke
211896e217aSJessica Clarke /* Enable power */
212896e217aSJessica Clarke error = gpio_pin_set_active(sc->pwren_pin, true);
213896e217aSJessica Clarke if (error != 0) {
214896e217aSJessica Clarke device_printf(dev, "Cannot enable power: %d\n", error);
215896e217aSJessica Clarke return (error);
216896e217aSJessica Clarke }
217896e217aSJessica Clarke
218896e217aSJessica Clarke /* Deassert core power-on reset (active low) */
219896e217aSJessica Clarke error = gpio_pin_set_active(sc->porst_pin, true);
220896e217aSJessica Clarke if (error != 0) {
221896e217aSJessica Clarke device_printf(dev, "Cannot deassert power-on reset: %d\n",
222896e217aSJessica Clarke error);
223896e217aSJessica Clarke return (error);
224896e217aSJessica Clarke }
225896e217aSJessica Clarke
226896e217aSJessica Clarke /* Enable the aux clock */
227896e217aSJessica Clarke error = clk_enable(sc->pcie_aux_clk);
228896e217aSJessica Clarke if (error != 0) {
229896e217aSJessica Clarke device_printf(dev, "Cannot enable aux clock: %d\n", error);
230896e217aSJessica Clarke return (error);
231896e217aSJessica Clarke }
232896e217aSJessica Clarke
233896e217aSJessica Clarke /* Hold LTSSM in reset whilst initialising the PHYs */
234896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_HOLD_PHY_RST, 1);
235896e217aSJessica Clarke
236896e217aSJessica Clarke /* Deassert the aux reset */
237896e217aSJessica Clarke error = hwreset_deassert(sc->pcie_aux_rst);
238896e217aSJessica Clarke if (error != 0) {
239896e217aSJessica Clarke device_printf(dev, "Cannot deassert aux reset: %d\n", error);
240896e217aSJessica Clarke return (error);
241896e217aSJessica Clarke }
242896e217aSJessica Clarke
243896e217aSJessica Clarke /* Enable control register interface */
244896e217aSJessica Clarke for (phy = 0; phy < FUDW_PHYS; ++phy)
245896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, SEL), 1);
246896e217aSJessica Clarke
247896e217aSJessica Clarke /* Wait for enable to take effect */
248896e217aSJessica Clarke DELAY(1);
249896e217aSJessica Clarke
250896e217aSJessica Clarke /* Initialise lane configuration */
251896e217aSJessica Clarke for (phy = 0; phy < FUDW_PHYS; ++phy) {
252896e217aSJessica Clarke for (lane = 0; lane < FUDW_LANES_PER_PHY; ++lane)
253896e217aSJessica Clarke fupci_phy_write(sc, phy, FUDW_MGMT_PHY_LANE(lane),
254896e217aSJessica Clarke FUDW_MGMT_PHY_LANE_INIT);
255896e217aSJessica Clarke }
256896e217aSJessica Clarke
257896e217aSJessica Clarke /* Disable the aux clock whilst taking the LTSSM out of reset */
258896e217aSJessica Clarke error = clk_disable(sc->pcie_aux_clk);
259896e217aSJessica Clarke if (error != 0) {
260896e217aSJessica Clarke device_printf(dev, "Cannot disable aux clock: %d\n", error);
261896e217aSJessica Clarke return (error);
262896e217aSJessica Clarke }
263896e217aSJessica Clarke
264896e217aSJessica Clarke /* Take LTSSM out of reset */
265896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_HOLD_PHY_RST, 0);
266896e217aSJessica Clarke
267896e217aSJessica Clarke /* Enable the aux clock again */
268896e217aSJessica Clarke error = clk_enable(sc->pcie_aux_clk);
269896e217aSJessica Clarke if (error != 0) {
270896e217aSJessica Clarke device_printf(dev, "Cannot re-enable aux clock: %d\n", error);
271896e217aSJessica Clarke return (error);
272896e217aSJessica Clarke }
273896e217aSJessica Clarke
274896e217aSJessica Clarke /* Put the controller in Root Complex mode */
275896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_DEVICE_TYPE, FUDW_MGMT_DEVICE_TYPE_RC);
276896e217aSJessica Clarke
27728aaa58fSJessica Clarke /* Hold PERST for 100ms as per the PCIe spec */
27828aaa58fSJessica Clarke DELAY(100000);
27928aaa58fSJessica Clarke
28028aaa58fSJessica Clarke /* Deassert PERST_N */
28128aaa58fSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_PERST_N, 1);
28228aaa58fSJessica Clarke
283896e217aSJessica Clarke return (0);
284896e217aSJessica Clarke }
285896e217aSJessica Clarke
286896e217aSJessica Clarke static void
fupci_dbi_protect(struct fupci_softc * sc,bool protect)287896e217aSJessica Clarke fupci_dbi_protect(struct fupci_softc *sc, bool protect)
288896e217aSJessica Clarke {
289896e217aSJessica Clarke uint32_t reg;
290896e217aSJessica Clarke
291896e217aSJessica Clarke reg = pci_dw_dbi_rd4(sc->dev, DW_MISC_CONTROL_1);
292896e217aSJessica Clarke if (protect)
293896e217aSJessica Clarke reg &= ~DBI_RO_WR_EN;
294896e217aSJessica Clarke else
295896e217aSJessica Clarke reg |= DBI_RO_WR_EN;
296896e217aSJessica Clarke pci_dw_dbi_wr4(sc->dev, DW_MISC_CONTROL_1, reg);
297896e217aSJessica Clarke }
298896e217aSJessica Clarke
299896e217aSJessica Clarke static int
fupci_init(struct fupci_softc * sc)300896e217aSJessica Clarke fupci_init(struct fupci_softc *sc)
301896e217aSJessica Clarke {
302896e217aSJessica Clarke /* Enable 32-bit I/O window */
303896e217aSJessica Clarke fupci_dbi_protect(sc, false);
304896e217aSJessica Clarke pci_dw_dbi_wr2(sc->dev, PCIR_IOBASEL_1,
305896e217aSJessica Clarke (PCIM_BRIO_32 << 8) | PCIM_BRIO_32);
306896e217aSJessica Clarke fupci_dbi_protect(sc, true);
307896e217aSJessica Clarke
308896e217aSJessica Clarke /* Enable LTSSM */
309896e217aSJessica Clarke FUDW_MGMT_WRITE(sc, FUDW_MGMT_LTSSM_EN, 1);
310896e217aSJessica Clarke
311896e217aSJessica Clarke return (0);
312896e217aSJessica Clarke }
313896e217aSJessica Clarke
314896e217aSJessica Clarke static int
fupci_probe(device_t dev)315896e217aSJessica Clarke fupci_probe(device_t dev)
316896e217aSJessica Clarke {
317896e217aSJessica Clarke if (!ofw_bus_status_okay(dev))
318896e217aSJessica Clarke return (ENXIO);
319896e217aSJessica Clarke
320896e217aSJessica Clarke if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
321896e217aSJessica Clarke return (ENXIO);
322896e217aSJessica Clarke
323896e217aSJessica Clarke device_set_desc(dev, "SiFive FU740 PCIe Controller");
324896e217aSJessica Clarke
325896e217aSJessica Clarke return (BUS_PROBE_DEFAULT);
326896e217aSJessica Clarke }
327896e217aSJessica Clarke
328896e217aSJessica Clarke static int
fupci_attach(device_t dev)329896e217aSJessica Clarke fupci_attach(device_t dev)
330896e217aSJessica Clarke {
331896e217aSJessica Clarke struct fupci_softc *sc;
332896e217aSJessica Clarke phandle_t node;
333896e217aSJessica Clarke int error, rid;
334896e217aSJessica Clarke
335896e217aSJessica Clarke sc = device_get_softc(dev);
336896e217aSJessica Clarke node = ofw_bus_get_node(dev);
337896e217aSJessica Clarke sc->dev = dev;
338896e217aSJessica Clarke
339896e217aSJessica Clarke rid = 0;
340896e217aSJessica Clarke error = ofw_bus_find_string_index(node, "reg-names", "dbi", &rid);
341896e217aSJessica Clarke if (error != 0) {
342896e217aSJessica Clarke device_printf(dev, "Cannot get DBI memory: %d\n", error);
343896e217aSJessica Clarke goto fail;
344896e217aSJessica Clarke }
345896e217aSJessica Clarke sc->dw_sc.dbi_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
346896e217aSJessica Clarke RF_ACTIVE);
347896e217aSJessica Clarke if (sc->dw_sc.dbi_res == NULL) {
348896e217aSJessica Clarke device_printf(dev, "Cannot allocate DBI memory\n");
349896e217aSJessica Clarke error = ENXIO;
350896e217aSJessica Clarke goto fail;
351896e217aSJessica Clarke }
352896e217aSJessica Clarke
353896e217aSJessica Clarke rid = 0;
354896e217aSJessica Clarke error = ofw_bus_find_string_index(node, "reg-names", "mgmt", &rid);
355896e217aSJessica Clarke if (error != 0) {
356896e217aSJessica Clarke device_printf(dev, "Cannot get management space memory: %d\n",
357896e217aSJessica Clarke error);
358896e217aSJessica Clarke goto fail;
359896e217aSJessica Clarke }
360896e217aSJessica Clarke sc->mgmt_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
361896e217aSJessica Clarke RF_ACTIVE);
362896e217aSJessica Clarke if (sc->mgmt_res == NULL) {
363896e217aSJessica Clarke device_printf(dev, "Cannot allocate management space memory\n");
364896e217aSJessica Clarke error = ENXIO;
365896e217aSJessica Clarke goto fail;
366896e217aSJessica Clarke }
367896e217aSJessica Clarke
368896e217aSJessica Clarke error = gpio_pin_get_by_ofw_property(dev, node, "reset-gpios",
369896e217aSJessica Clarke &sc->porst_pin);
370896e217aSJessica Clarke /* Old U-Boot device tree uses perstn-gpios */
371896e217aSJessica Clarke if (error == ENOENT)
372896e217aSJessica Clarke error = gpio_pin_get_by_ofw_property(dev, node, "perstn-gpios",
373896e217aSJessica Clarke &sc->porst_pin);
374896e217aSJessica Clarke if (error != 0) {
375896e217aSJessica Clarke device_printf(dev, "Cannot get power-on reset GPIO: %d\n",
376896e217aSJessica Clarke error);
377896e217aSJessica Clarke goto fail;
378896e217aSJessica Clarke }
379896e217aSJessica Clarke error = gpio_pin_setflags(sc->porst_pin, GPIO_PIN_OUTPUT);
380896e217aSJessica Clarke if (error != 0) {
381896e217aSJessica Clarke device_printf(dev, "Cannot configure power-on reset GPIO: %d\n",
382896e217aSJessica Clarke error);
383896e217aSJessica Clarke goto fail;
384896e217aSJessica Clarke }
385896e217aSJessica Clarke
386896e217aSJessica Clarke error = gpio_pin_get_by_ofw_property(dev, node, "pwren-gpios",
387896e217aSJessica Clarke &sc->pwren_pin);
388896e217aSJessica Clarke if (error != 0) {
389896e217aSJessica Clarke device_printf(dev, "Cannot get power enable GPIO: %d\n",
390896e217aSJessica Clarke error);
391896e217aSJessica Clarke goto fail;
392896e217aSJessica Clarke }
393896e217aSJessica Clarke error = gpio_pin_setflags(sc->pwren_pin, GPIO_PIN_OUTPUT);
394896e217aSJessica Clarke if (error != 0) {
395896e217aSJessica Clarke device_printf(dev, "Cannot configure power enable GPIO: %d\n",
396896e217aSJessica Clarke error);
397896e217aSJessica Clarke goto fail;
398896e217aSJessica Clarke }
399896e217aSJessica Clarke
400896e217aSJessica Clarke error = clk_get_by_ofw_name(dev, node, "pcie_aux", &sc->pcie_aux_clk);
401896e217aSJessica Clarke /* Old U-Boot device tree uses pcieaux */
402896e217aSJessica Clarke if (error == ENOENT)
403896e217aSJessica Clarke error = clk_get_by_ofw_name(dev, node, "pcieaux",
404896e217aSJessica Clarke &sc->pcie_aux_clk);
405896e217aSJessica Clarke if (error != 0) {
406896e217aSJessica Clarke device_printf(dev, "Cannot get aux clock: %d\n", error);
407896e217aSJessica Clarke goto fail;
408896e217aSJessica Clarke }
409896e217aSJessica Clarke
410896e217aSJessica Clarke error = hwreset_get_by_ofw_idx(dev, node, 0, &sc->pcie_aux_rst);
411896e217aSJessica Clarke if (error != 0) {
412896e217aSJessica Clarke device_printf(dev, "Cannot get aux reset: %d\n", error);
413896e217aSJessica Clarke goto fail;
414896e217aSJessica Clarke }
415896e217aSJessica Clarke
416896e217aSJessica Clarke error = fupci_phy_init(sc);
417896e217aSJessica Clarke if (error != 0)
418896e217aSJessica Clarke goto fail;
419896e217aSJessica Clarke
420896e217aSJessica Clarke error = pci_dw_init(dev);
421896e217aSJessica Clarke if (error != 0)
422896e217aSJessica Clarke goto fail;
423896e217aSJessica Clarke
424896e217aSJessica Clarke error = fupci_init(sc);
425896e217aSJessica Clarke if (error != 0)
426896e217aSJessica Clarke goto fail;
427896e217aSJessica Clarke
428*18250ec6SJohn Baldwin bus_attach_children(dev);
429*18250ec6SJohn Baldwin return (0);
430896e217aSJessica Clarke
431896e217aSJessica Clarke fail:
432896e217aSJessica Clarke /* XXX Cleanup */
433896e217aSJessica Clarke return (error);
434896e217aSJessica Clarke }
435896e217aSJessica Clarke
436896e217aSJessica Clarke static int
fupci_get_link(device_t dev,bool * status)437896e217aSJessica Clarke fupci_get_link(device_t dev, bool *status)
438896e217aSJessica Clarke {
439896e217aSJessica Clarke uint32_t reg;
440896e217aSJessica Clarke
441896e217aSJessica Clarke reg = pci_dw_dbi_rd4(dev, FUDW_DBI_PORT_DBG1);
442896e217aSJessica Clarke *status = (reg & FUDW_DBI_PORT_DBG1_LINK_UP) != 0 &&
443896e217aSJessica Clarke (reg & FUDW_DBI_PORT_DBG1_LINK_IN_TRAINING) == 0;
444896e217aSJessica Clarke
445896e217aSJessica Clarke return (0);
446896e217aSJessica Clarke }
447896e217aSJessica Clarke
448896e217aSJessica Clarke static device_method_t fupci_methods[] = {
449896e217aSJessica Clarke /* Device interface */
450896e217aSJessica Clarke DEVMETHOD(device_probe, fupci_probe),
451896e217aSJessica Clarke DEVMETHOD(device_attach, fupci_attach),
452896e217aSJessica Clarke
453896e217aSJessica Clarke /* PCI DW interface */
454896e217aSJessica Clarke DEVMETHOD(pci_dw_get_link, fupci_get_link),
455896e217aSJessica Clarke
456896e217aSJessica Clarke DEVMETHOD_END
457896e217aSJessica Clarke };
458896e217aSJessica Clarke
459896e217aSJessica Clarke DEFINE_CLASS_1(pcib, fupci_driver, fupci_methods,
460896e217aSJessica Clarke sizeof(struct fupci_softc), pci_dw_driver);
461bb32809bSJohn Baldwin DRIVER_MODULE(fu740_pci_dw, simplebus, fupci_driver, NULL, NULL);
462