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