common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / pci / pcie_dw_ti.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Texas Instruments, Inc
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <log.h>
9 #include <pci.h>
10 #include <generic-phy.h>
11 #include <power-domain.h>
12 #include <regmap.h>
13 #include <syscon.h>
14 #include <asm/io.h>
15 #include <asm-generic/gpio.h>
16 #include <dm/device_compat.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 #define PCIE_VENDORID_MASK      GENMASK(15, 0)
23 #define PCIE_DEVICEID_SHIFT     16
24
25 /* PCI DBICS registers */
26 #define PCIE_CONFIG_BAR0                0x10
27 #define PCIE_LINK_STATUS_REG            0x80
28 #define PCIE_LINK_STATUS_SPEED_OFF      16
29 #define PCIE_LINK_STATUS_SPEED_MASK     (0xf << PCIE_LINK_STATUS_SPEED_OFF)
30 #define PCIE_LINK_STATUS_WIDTH_OFF      20
31 #define PCIE_LINK_STATUS_WIDTH_MASK     (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
32
33 #define PCIE_LINK_CAPABILITY            0x7c
34 #define PCIE_LINK_CTL_2                 0xa0
35 #define TARGET_LINK_SPEED_MASK          0xf
36 #define LINK_SPEED_GEN_1                0x1
37 #define LINK_SPEED_GEN_2                0x2
38 #define LINK_SPEED_GEN_3                0x3
39
40 #define PCIE_MISC_CONTROL_1_OFF         0x8bc
41 #define PCIE_DBI_RO_WR_EN               BIT(0)
42
43 #define PLR_OFFSET                      0x700
44 #define PCIE_PORT_DEBUG0                (PLR_OFFSET + 0x28)
45 #define PORT_LOGIC_LTSSM_STATE_MASK     0x1f
46 #define PORT_LOGIC_LTSSM_STATE_L0       0x11
47
48 #define PCIE_LINK_WIDTH_SPEED_CONTROL   0x80c
49 #define PORT_LOGIC_SPEED_CHANGE         (0x1 << 17)
50
51 #define PCIE_LINK_UP_TIMEOUT_MS         100
52
53 /*
54  * iATU Unroll-specific register definitions
55  * From 4.80 core version the address translation will be made by unroll.
56  * The registers are offset from atu_base
57  */
58 #define PCIE_ATU_UNR_REGION_CTRL1       0x00
59 #define PCIE_ATU_UNR_REGION_CTRL2       0x04
60 #define PCIE_ATU_UNR_LOWER_BASE         0x08
61 #define PCIE_ATU_UNR_UPPER_BASE         0x0c
62 #define PCIE_ATU_UNR_LIMIT              0x10
63 #define PCIE_ATU_UNR_LOWER_TARGET       0x14
64 #define PCIE_ATU_UNR_UPPER_TARGET       0x18
65
66 #define PCIE_ATU_REGION_INDEX1          (0x1 << 0)
67 #define PCIE_ATU_REGION_INDEX0          (0x0 << 0)
68 #define PCIE_ATU_TYPE_MEM               (0x0 << 0)
69 #define PCIE_ATU_TYPE_IO                (0x2 << 0)
70 #define PCIE_ATU_TYPE_CFG0              (0x4 << 0)
71 #define PCIE_ATU_TYPE_CFG1              (0x5 << 0)
72 #define PCIE_ATU_ENABLE                 (0x1 << 31)
73 #define PCIE_ATU_BAR_MODE_ENABLE        (0x1 << 30)
74 #define PCIE_ATU_BUS(x)                 (((x) & 0xff) << 24)
75 #define PCIE_ATU_DEV(x)                 (((x) & 0x1f) << 19)
76 #define PCIE_ATU_FUNC(x)                (((x) & 0x7) << 16)
77
78 /* Register address builder */
79 #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region)        ((region) << 9)
80
81 /* Offsets from App base */
82 #define PCIE_CMD_STATUS                 0x04
83 #define LTSSM_EN_VAL                    BIT(0)
84
85 /* Parameters for the waiting for iATU enabled routine */
86 #define LINK_WAIT_MAX_IATU_RETRIES      5
87 #define LINK_WAIT_IATU                  10000
88
89 #define AM654_PCIE_DEV_TYPE_MASK        0x3
90 #define EP                              0x0
91 #define LEG_EP                          0x1
92 #define RC                              0x2
93
94 /**
95  * struct pcie_dw_ti - TI DW PCIe controller state
96  *
97  * @app_base: The base address of application register space
98  * @dbics_base: The base address of dbics register space
99  * @cfg_base: The base address of configuration space
100  * @atu_base: The base address of ATU space
101  * @cfg_size: The size of the configuration space which is needed
102  *            as it gets written into the PCIE_ATU_LIMIT register
103  * @first_busno: This driver supports multiple PCIe controllers.
104  *               first_busno stores the bus number of the PCIe root-port
105  *               number which may vary depending on the PCIe setup
106  *               (PEX switches etc).
107  */
108 struct pcie_dw_ti {
109         void *app_base;
110         void *dbi_base;
111         void *cfg_base;
112         void *atu_base;
113         fdt_size_t cfg_size;
114         int first_busno;
115         struct udevice *dev;
116
117         /* IO and MEM PCI regions */
118         struct pci_region io;
119         struct pci_region mem;
120 };
121
122 enum dw_pcie_device_mode {
123         DW_PCIE_UNKNOWN_TYPE,
124         DW_PCIE_EP_TYPE,
125         DW_PCIE_LEG_EP_TYPE,
126         DW_PCIE_RC_TYPE,
127 };
128
129 static int pcie_dw_get_link_speed(struct pcie_dw_ti *pci)
130 {
131         return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
132                 PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
133 }
134
135 static int pcie_dw_get_link_width(struct pcie_dw_ti *pci)
136 {
137         return (readl(pci->dbi_base + PCIE_LINK_STATUS_REG) &
138                 PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
139 }
140
141 static void dw_pcie_writel_ob_unroll(struct pcie_dw_ti *pci, u32 index, u32 reg,
142                                      u32 val)
143 {
144         u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
145         void __iomem *base = pci->atu_base;
146
147         writel(val, base + offset + reg);
148 }
149
150 static u32 dw_pcie_readl_ob_unroll(struct pcie_dw_ti *pci, u32 index, u32 reg)
151 {
152         u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
153         void __iomem *base = pci->atu_base;
154
155         return readl(base + offset + reg);
156 }
157
158 /**
159  * pcie_dw_prog_outbound_atu_unroll() - Configure ATU for outbound accesses
160  *
161  * @pcie: Pointer to the PCI controller state
162  * @index: ATU region index
163  * @type: ATU accsess type
164  * @cpu_addr: the physical address for the translation entry
165  * @pci_addr: the pcie bus address for the translation entry
166  * @size: the size of the translation entry
167  */
168 static void pcie_dw_prog_outbound_atu_unroll(struct pcie_dw_ti *pci, int index,
169                                              int type, u64 cpu_addr,
170                                              u64 pci_addr, u32 size)
171 {
172         u32 retries, val;
173
174         debug("ATU programmed with: index: %d, type: %d, cpu addr: %8llx, pci addr: %8llx, size: %8x\n",
175               index, type, cpu_addr, pci_addr, size);
176
177         dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
178                                  lower_32_bits(cpu_addr));
179         dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
180                                  upper_32_bits(cpu_addr));
181         dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT,
182                                  lower_32_bits(cpu_addr + size - 1));
183         dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
184                                  lower_32_bits(pci_addr));
185         dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
186                                  upper_32_bits(pci_addr));
187         dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
188                                  type);
189         dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
190                                  PCIE_ATU_ENABLE);
191
192         /*
193          * Make sure ATU enable takes effect before any subsequent config
194          * and I/O accesses.
195          */
196         for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
197                 val = dw_pcie_readl_ob_unroll(pci, index,
198                                               PCIE_ATU_UNR_REGION_CTRL2);
199                 if (val & PCIE_ATU_ENABLE)
200                         return;
201
202                 udelay(LINK_WAIT_IATU);
203         }
204         dev_err(pci->dev, "outbound iATU is not being enabled\n");
205 }
206
207 /**
208  * set_cfg_address() - Configure the PCIe controller config space access
209  *
210  * @pcie: Pointer to the PCI controller state
211  * @d: PCI device to access
212  * @where: Offset in the configuration space
213  *
214  * Configures the PCIe controller to access the configuration space of
215  * a specific PCIe device and returns the address to use for this
216  * access.
217  *
218  * Return: Address that can be used to access the configation space
219  *         of the requested device / offset
220  */
221 static uintptr_t set_cfg_address(struct pcie_dw_ti *pcie,
222                                  pci_dev_t d, uint where)
223 {
224         int bus = PCI_BUS(d) - pcie->first_busno;
225         uintptr_t va_address;
226         u32 atu_type;
227
228         /* Use dbi_base for own configuration read and write */
229         if (!bus) {
230                 va_address = (uintptr_t)pcie->dbi_base;
231                 goto out;
232         }
233
234         if (bus == 1)
235                 /* For local bus, change TLP Type field to 4. */
236                 atu_type = PCIE_ATU_TYPE_CFG0;
237         else
238                 /* Otherwise, change TLP Type field to 5. */
239                 atu_type = PCIE_ATU_TYPE_CFG1;
240
241         /*
242          * Not accessing root port configuration space?
243          * Region #0 is used for Outbound CFG space access.
244          * Direction = Outbound
245          * Region Index = 0
246          */
247         d = PCI_MASK_BUS(d);
248         d = PCI_ADD_BUS(bus, d);
249         pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
250                                          atu_type, (u64)pcie->cfg_base,
251                                          d << 8, pcie->cfg_size);
252
253         va_address = (uintptr_t)pcie->cfg_base;
254
255 out:
256         va_address += where & ~0x3;
257
258         return va_address;
259 }
260
261 /**
262  * pcie_dw_addr_valid() - Check for valid bus address
263  *
264  * @d: The PCI device to access
265  * @first_busno: Bus number of the PCIe controller root complex
266  *
267  * Return 1 (true) if the PCI device can be accessed by this controller.
268  *
269  * Return: 1 on valid, 0 on invalid
270  */
271 static int pcie_dw_addr_valid(pci_dev_t d, int first_busno)
272 {
273         if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
274                 return 0;
275         if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
276                 return 0;
277
278         return 1;
279 }
280
281 /**
282  * pcie_dw_ti_read_config() - Read from configuration space
283  *
284  * @bus: Pointer to the PCI bus
285  * @bdf: Identifies the PCIe device to access
286  * @offset: The offset into the device's configuration space
287  * @valuep: A pointer at which to store the read value
288  * @size: Indicates the size of access to perform
289  *
290  * Read a value of size @size from offset @offset within the configuration
291  * space of the device identified by the bus, device & function numbers in @bdf
292  * on the PCI bus @bus.
293  *
294  * Return: 0 on success
295  */
296 static int pcie_dw_ti_read_config(const struct udevice *bus, pci_dev_t bdf,
297                                   uint offset, ulong *valuep,
298                                   enum pci_size_t size)
299 {
300         struct pcie_dw_ti *pcie = dev_get_priv(bus);
301         uintptr_t va_address;
302         ulong value;
303
304         debug("PCIE CFG read: bdf=%2x:%2x:%2x ",
305               PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
306
307         if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
308                 debug("- out of range\n");
309                 *valuep = pci_get_ff(size);
310                 return 0;
311         }
312
313         va_address = set_cfg_address(pcie, bdf, offset);
314
315         value = readl(va_address);
316
317         debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
318         *valuep = pci_conv_32_to_size(value, offset, size);
319
320         pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
321                                          PCIE_ATU_TYPE_IO, pcie->io.phys_start,
322                                          pcie->io.bus_start, pcie->io.size);
323
324         return 0;
325 }
326
327 /**
328  * pcie_dw_ti_write_config() - Write to configuration space
329  *
330  * @bus: Pointer to the PCI bus
331  * @bdf: Identifies the PCIe device to access
332  * @offset: The offset into the device's configuration space
333  * @value: The value to write
334  * @size: Indicates the size of access to perform
335  *
336  * Write the value @value of size @size from offset @offset within the
337  * configuration space of the device identified by the bus, device & function
338  * numbers in @bdf on the PCI bus @bus.
339  *
340  * Return: 0 on success
341  */
342 static int pcie_dw_ti_write_config(struct udevice *bus, pci_dev_t bdf,
343                                    uint offset, ulong value,
344                                    enum pci_size_t size)
345 {
346         struct pcie_dw_ti *pcie = dev_get_priv(bus);
347         uintptr_t va_address;
348         ulong old;
349
350         debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
351               PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
352         debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
353
354         if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
355                 debug("- out of range\n");
356                 return 0;
357         }
358
359         va_address = set_cfg_address(pcie, bdf, offset);
360
361         old = readl(va_address);
362         value = pci_conv_size_to_32(old, value, offset, size);
363         writel(value, va_address);
364
365         pcie_dw_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
366                                          PCIE_ATU_TYPE_IO, pcie->io.phys_start,
367                                          pcie->io.bus_start, pcie->io.size);
368
369         return 0;
370 }
371
372 static inline void dw_pcie_dbi_write_enable(struct pcie_dw_ti *pci, bool en)
373 {
374         u32 val;
375
376         val = readl(pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
377         if (en)
378                 val |= PCIE_DBI_RO_WR_EN;
379         else
380                 val &= ~PCIE_DBI_RO_WR_EN;
381         writel(val, pci->dbi_base + PCIE_MISC_CONTROL_1_OFF);
382 }
383
384 /**
385  * pcie_dw_configure() - Configure link capabilities and speed
386  *
387  * @regs_base: A pointer to the PCIe controller registers
388  * @cap_speed: The capabilities and speed to configure
389  *
390  * Configure the link capabilities and speed in the PCIe root complex.
391  */
392 static void pcie_dw_configure(struct pcie_dw_ti *pci, u32 cap_speed)
393 {
394         u32 val;
395
396         dw_pcie_dbi_write_enable(pci, true);
397
398         val = readl(pci->dbi_base + PCIE_LINK_CAPABILITY);
399         val &= ~TARGET_LINK_SPEED_MASK;
400         val |= cap_speed;
401         writel(val, pci->dbi_base + PCIE_LINK_CAPABILITY);
402
403         val = readl(pci->dbi_base + PCIE_LINK_CTL_2);
404         val &= ~TARGET_LINK_SPEED_MASK;
405         val |= cap_speed;
406         writel(val, pci->dbi_base + PCIE_LINK_CTL_2);
407
408         dw_pcie_dbi_write_enable(pci, false);
409 }
410
411 /**
412  * is_link_up() - Return the link state
413  *
414  * @regs_base: A pointer to the PCIe DBICS registers
415  *
416  * Return: 1 (true) for active line and 0 (false) for no link
417  */
418 static int is_link_up(struct pcie_dw_ti *pci)
419 {
420         u32 val;
421
422         val = readl(pci->dbi_base + PCIE_PORT_DEBUG0);
423         val &= PORT_LOGIC_LTSSM_STATE_MASK;
424
425         return (val == PORT_LOGIC_LTSSM_STATE_L0);
426 }
427
428 /**
429  * wait_link_up() - Wait for the link to come up
430  *
431  * @regs_base: A pointer to the PCIe controller registers
432  *
433  * Return: 1 (true) for active line and 0 (false) for no link (timeout)
434  */
435 static int wait_link_up(struct pcie_dw_ti *pci)
436 {
437         unsigned long timeout;
438
439         timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS;
440         while (!is_link_up(pci)) {
441                 if (get_timer(0) > timeout)
442                         return 0;
443         };
444
445         return 1;
446 }
447
448 static int pcie_dw_ti_pcie_link_up(struct pcie_dw_ti *pci, u32 cap_speed)
449 {
450         u32 val;
451
452         if (is_link_up(pci)) {
453                 printf("PCI Link already up before configuration!\n");
454                 return 1;
455         }
456
457         /* DW pre link configurations */
458         pcie_dw_configure(pci, cap_speed);
459
460         /* Initiate link training */
461         val = readl(pci->app_base + PCIE_CMD_STATUS);
462         val |= LTSSM_EN_VAL;
463         writel(val, pci->app_base + PCIE_CMD_STATUS);
464
465         /* Check that link was established */
466         if (!wait_link_up(pci))
467                 return 0;
468
469         /*
470          * Link can be established in Gen 1. still need to wait
471          * till MAC nagaotiation is completed
472          */
473         udelay(100);
474
475         return 1;
476 }
477
478 /**
479  * pcie_dw_setup_host() - Setup the PCIe controller for RC opertaion
480  *
481  * @pcie: Pointer to the PCI controller state
482  *
483  * Configure the host BARs of the PCIe controller root port so that
484  * PCI(e) devices may access the system memory.
485  */
486 static void pcie_dw_setup_host(struct pcie_dw_ti *pci)
487 {
488         u32 val;
489
490         /* setup RC BARs */
491         writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
492                pci->dbi_base + PCI_BASE_ADDRESS_0);
493         writel(0x0, pci->dbi_base + PCI_BASE_ADDRESS_1);
494
495         /* setup interrupt pins */
496         dw_pcie_dbi_write_enable(pci, true);
497         val = readl(pci->dbi_base + PCI_INTERRUPT_LINE);
498         val &= 0xffff00ff;
499         val |= 0x00000100;
500         writel(val, pci->dbi_base + PCI_INTERRUPT_LINE);
501         dw_pcie_dbi_write_enable(pci, false);
502
503         /* setup bus numbers */
504         val = readl(pci->dbi_base + PCI_PRIMARY_BUS);
505         val &= 0xff000000;
506         val |= 0x00ff0100;
507         writel(val, pci->dbi_base + PCI_PRIMARY_BUS);
508
509         /* setup command register */
510         val = readl(pci->dbi_base + PCI_COMMAND);
511         val &= 0xffff0000;
512         val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
513                 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
514         writel(val, pci->dbi_base + PCI_COMMAND);
515
516         /* Enable write permission for the DBI read-only register */
517         dw_pcie_dbi_write_enable(pci, true);
518         /* program correct class for RC */
519         writew(PCI_CLASS_BRIDGE_PCI, pci->dbi_base + PCI_CLASS_DEVICE);
520         /* Better disable write permission right after the update */
521         dw_pcie_dbi_write_enable(pci, false);
522
523         val = readl(pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
524         val |= PORT_LOGIC_SPEED_CHANGE;
525         writel(val, pci->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
526 }
527
528 static int pcie_am654_set_mode(struct pcie_dw_ti *pci,
529                                enum dw_pcie_device_mode mode)
530 {
531         struct regmap *syscon;
532         u32 val;
533         u32 mask;
534         int ret;
535
536         syscon = syscon_regmap_lookup_by_phandle(pci->dev,
537                                                  "ti,syscon-pcie-mode");
538         if (IS_ERR(syscon))
539                 return 0;
540
541         mask = AM654_PCIE_DEV_TYPE_MASK;
542
543         switch (mode) {
544         case DW_PCIE_RC_TYPE:
545                 val = RC;
546                 break;
547         case DW_PCIE_EP_TYPE:
548                 val = EP;
549                 break;
550         default:
551                 dev_err(pci->dev, "INVALID device type %d\n", mode);
552                 return -EINVAL;
553         }
554
555         ret = regmap_update_bits(syscon, 0, mask, val);
556         if (ret) {
557                 dev_err(pci->dev, "failed to set pcie mode\n");
558                 return ret;
559         }
560
561         return 0;
562 }
563
564 static int pcie_dw_init_id(struct pcie_dw_ti *pci)
565 {
566         struct regmap *devctrl_regs;
567         unsigned int id;
568         int ret;
569
570         devctrl_regs = syscon_regmap_lookup_by_phandle(pci->dev,
571                                                        "ti,syscon-pcie-id");
572         if (IS_ERR(devctrl_regs))
573                 return PTR_ERR(devctrl_regs);
574
575         ret = regmap_read(devctrl_regs, 0, &id);
576         if (ret)
577                 return ret;
578
579         dw_pcie_dbi_write_enable(pci, true);
580         writew(id & PCIE_VENDORID_MASK, pci->dbi_base + PCI_VENDOR_ID);
581         writew(id >> PCIE_DEVICEID_SHIFT, pci->dbi_base + PCI_DEVICE_ID);
582         dw_pcie_dbi_write_enable(pci, false);
583
584         return 0;
585 }
586
587 /**
588  * pcie_dw_ti_probe() - Probe the PCIe bus for active link
589  *
590  * @dev: A pointer to the device being operated on
591  *
592  * Probe for an active link on the PCIe bus and configure the controller
593  * to enable this port.
594  *
595  * Return: 0 on success, else -ENODEV
596  */
597 static int pcie_dw_ti_probe(struct udevice *dev)
598 {
599         struct pcie_dw_ti *pci = dev_get_priv(dev);
600         struct udevice *ctlr = pci_get_controller(dev);
601         struct pci_controller *hose = dev_get_uclass_priv(ctlr);
602         struct power_domain pci_pwrdmn;
603         struct phy phy0, phy1;
604         int ret;
605
606         ret = power_domain_get_by_index(dev, &pci_pwrdmn, 0);
607         if (ret) {
608                 dev_err(dev, "failed to get power domain\n");
609                 return ret;
610         }
611
612         ret = power_domain_on(&pci_pwrdmn);
613         if (ret) {
614                 dev_err(dev, "Power domain on failed\n");
615                 return ret;
616         }
617
618         ret = generic_phy_get_by_name(dev,  "pcie-phy0", &phy0);
619         if (ret) {
620                 dev_err(dev, "Unable to get phy0");
621                 return ret;
622         }
623         generic_phy_reset(&phy0);
624         generic_phy_init(&phy0);
625         generic_phy_power_on(&phy0);
626
627         ret = generic_phy_get_by_name(dev,  "pcie-phy1", &phy1);
628         if (ret) {
629                 dev_err(dev, "Unable to get phy1");
630                 return ret;
631         }
632         generic_phy_reset(&phy1);
633         generic_phy_init(&phy1);
634         generic_phy_power_on(&phy1);
635
636         pci->first_busno = dev->seq;
637         pci->dev = dev;
638
639         pcie_dw_setup_host(pci);
640         pcie_dw_init_id(pci);
641
642         if (device_is_compatible(dev, "ti,am654-pcie-rc"))
643                 pcie_am654_set_mode(pci, DW_PCIE_RC_TYPE);
644
645         if (!pcie_dw_ti_pcie_link_up(pci, LINK_SPEED_GEN_2)) {
646                 printf("PCIE-%d: Link down\n", dev->seq);
647                 return -ENODEV;
648         }
649
650         printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev->seq,
651                pcie_dw_get_link_speed(pci),
652                pcie_dw_get_link_width(pci),
653                hose->first_busno);
654
655         /* Store the IO and MEM windows settings for future use by the ATU */
656         pci->io.phys_start = hose->regions[0].phys_start; /* IO base */
657         pci->io.bus_start  = hose->regions[0].bus_start;  /* IO_bus_addr */
658         pci->io.size        = hose->regions[0].size;       /* IO size */
659
660         pci->mem.phys_start = hose->regions[1].phys_start; /* MEM base */
661         pci->mem.bus_start  = hose->regions[1].bus_start;  /* MEM_bus_addr */
662         pci->mem.size        = hose->regions[1].size;       /* MEM size */
663
664         pcie_dw_prog_outbound_atu_unroll(pci, PCIE_ATU_REGION_INDEX0,
665                                          PCIE_ATU_TYPE_MEM,
666                                          pci->mem.phys_start,
667                                          pci->mem.bus_start, pci->mem.size);
668
669         return 0;
670 }
671
672 /**
673  * pcie_dw_ti_ofdata_to_platdata() - Translate from DT to device state
674  *
675  * @dev: A pointer to the device being operated on
676  *
677  * Translate relevant data from the device tree pertaining to device @dev into
678  * state that the driver will later make use of. This state is stored in the
679  * device's private data structure.
680  *
681  * Return: 0 on success, else -EINVAL
682  */
683 static int pcie_dw_ti_ofdata_to_platdata(struct udevice *dev)
684 {
685         struct pcie_dw_ti *pcie = dev_get_priv(dev);
686
687         /* Get the controller base address */
688         pcie->dbi_base = (void *)dev_read_addr_name(dev, "dbics");
689         if ((fdt_addr_t)pcie->dbi_base == FDT_ADDR_T_NONE)
690                 return -EINVAL;
691
692         /* Get the config space base address and size */
693         pcie->cfg_base = (void *)dev_read_addr_size_name(dev, "config",
694                                                          &pcie->cfg_size);
695         if ((fdt_addr_t)pcie->cfg_base == FDT_ADDR_T_NONE)
696                 return -EINVAL;
697
698         /* Get the iATU base address and size */
699         pcie->atu_base = (void *)dev_read_addr_name(dev, "atu");
700         if ((fdt_addr_t)pcie->atu_base == FDT_ADDR_T_NONE)
701                 return -EINVAL;
702
703         /* Get the app base address and size */
704         pcie->app_base = (void *)dev_read_addr_name(dev, "app");
705         if ((fdt_addr_t)pcie->app_base == FDT_ADDR_T_NONE)
706                 return -EINVAL;
707
708         return 0;
709 }
710
711 static const struct dm_pci_ops pcie_dw_ti_ops = {
712         .read_config    = pcie_dw_ti_read_config,
713         .write_config   = pcie_dw_ti_write_config,
714 };
715
716 static const struct udevice_id pcie_dw_ti_ids[] = {
717         { .compatible = "ti,am654-pcie-rc" },
718         { }
719 };
720
721 U_BOOT_DRIVER(pcie_dw_ti) = {
722         .name                   = "pcie_dw_ti",
723         .id                     = UCLASS_PCI,
724         .of_match               = pcie_dw_ti_ids,
725         .ops                    = &pcie_dw_ti_ops,
726         .ofdata_to_platdata     = pcie_dw_ti_ofdata_to_platdata,
727         .probe                  = pcie_dw_ti_probe,
728         .priv_auto_alloc_size   = sizeof(struct pcie_dw_ti),
729 };