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