blob: dc6e65273b798c5be03f8b2878df8fcb8a8e38c1 [file] [log] [blame]
Sekhar Nori18db23d2019-08-01 19:12:57 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Texas Instruments, Inc
4 */
5
Sekhar Nori18db23d2019-08-01 19:12:57 +05306#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Sekhar Nori18db23d2019-08-01 19:12:57 +05308#include <pci.h>
9#include <generic-phy.h>
10#include <power-domain.h>
11#include <regmap.h>
12#include <syscon.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Sekhar Nori18db23d2019-08-01 19:12:57 +053014#include <asm/io.h>
15#include <asm-generic/gpio.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Sekhar Nori18db23d2019-08-01 19:12:57 +053020
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010021#include "pcie_dw_common.h"
22
Sekhar Nori18db23d2019-08-01 19:12:57 +053023DECLARE_GLOBAL_DATA_PTR;
24
25#define PCIE_VENDORID_MASK GENMASK(15, 0)
26#define PCIE_DEVICEID_SHIFT 16
27
Sekhar Nori18db23d2019-08-01 19:12:57 +053028#define PCIE_LINK_CAPABILITY 0x7c
29#define PCIE_LINK_CTL_2 0xa0
30#define TARGET_LINK_SPEED_MASK 0xf
31#define LINK_SPEED_GEN_1 0x1
32#define LINK_SPEED_GEN_2 0x2
33#define LINK_SPEED_GEN_3 0x3
34
35#define PCIE_MISC_CONTROL_1_OFF 0x8bc
36#define PCIE_DBI_RO_WR_EN BIT(0)
37
38#define PLR_OFFSET 0x700
39#define PCIE_PORT_DEBUG0 (PLR_OFFSET + 0x28)
40#define PORT_LOGIC_LTSSM_STATE_MASK 0x1f
41#define PORT_LOGIC_LTSSM_STATE_L0 0x11
42
Sekhar Nori18db23d2019-08-01 19:12:57 +053043#define PCIE_LINK_UP_TIMEOUT_MS 100
44
Sekhar Nori18db23d2019-08-01 19:12:57 +053045/* Offsets from App base */
46#define PCIE_CMD_STATUS 0x04
47#define LTSSM_EN_VAL BIT(0)
48
Sekhar Nori18db23d2019-08-01 19:12:57 +053049#define AM654_PCIE_DEV_TYPE_MASK 0x3
50#define EP 0x0
51#define LEG_EP 0x1
52#define RC 0x2
53
54/**
55 * struct pcie_dw_ti - TI DW PCIe controller state
56 *
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010057 * @pci: The common PCIe DW structure
Sekhar Nori18db23d2019-08-01 19:12:57 +053058 * @app_base: The base address of application register space
Sekhar Nori18db23d2019-08-01 19:12:57 +053059 */
60struct pcie_dw_ti {
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010061 /* Must be first member of the struct */
62 struct pcie_dw dw;
Sekhar Nori18db23d2019-08-01 19:12:57 +053063 void *app_base;
Sekhar Nori18db23d2019-08-01 19:12:57 +053064};
65
66enum dw_pcie_device_mode {
67 DW_PCIE_UNKNOWN_TYPE,
68 DW_PCIE_EP_TYPE,
69 DW_PCIE_LEG_EP_TYPE,
70 DW_PCIE_RC_TYPE,
71};
72
Sekhar Nori18db23d2019-08-01 19:12:57 +053073/**
74 * pcie_dw_configure() - Configure link capabilities and speed
75 *
76 * @regs_base: A pointer to the PCIe controller registers
77 * @cap_speed: The capabilities and speed to configure
78 *
79 * Configure the link capabilities and speed in the PCIe root complex.
80 */
81static void pcie_dw_configure(struct pcie_dw_ti *pci, u32 cap_speed)
82{
83 u32 val;
84
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010085 dw_pcie_dbi_write_enable(&pci->dw, true);
Sekhar Nori18db23d2019-08-01 19:12:57 +053086
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010087 val = readl(pci->dw.dbi_base + PCIE_LINK_CAPABILITY);
Sekhar Nori18db23d2019-08-01 19:12:57 +053088 val &= ~TARGET_LINK_SPEED_MASK;
89 val |= cap_speed;
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010090 writel(val, pci->dw.dbi_base + PCIE_LINK_CAPABILITY);
Sekhar Nori18db23d2019-08-01 19:12:57 +053091
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010092 val = readl(pci->dw.dbi_base + PCIE_LINK_CTL_2);
Sekhar Nori18db23d2019-08-01 19:12:57 +053093 val &= ~TARGET_LINK_SPEED_MASK;
94 val |= cap_speed;
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010095 writel(val, pci->dw.dbi_base + PCIE_LINK_CTL_2);
Sekhar Nori18db23d2019-08-01 19:12:57 +053096
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010097 dw_pcie_dbi_write_enable(&pci->dw, false);
Sekhar Nori18db23d2019-08-01 19:12:57 +053098}
99
100/**
101 * is_link_up() - Return the link state
102 *
103 * @regs_base: A pointer to the PCIe DBICS registers
104 *
105 * Return: 1 (true) for active line and 0 (false) for no link
106 */
107static int is_link_up(struct pcie_dw_ti *pci)
108{
109 u32 val;
110
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100111 val = readl(pci->dw.dbi_base + PCIE_PORT_DEBUG0);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530112 val &= PORT_LOGIC_LTSSM_STATE_MASK;
113
114 return (val == PORT_LOGIC_LTSSM_STATE_L0);
115}
116
117/**
118 * wait_link_up() - Wait for the link to come up
119 *
120 * @regs_base: A pointer to the PCIe controller registers
121 *
122 * Return: 1 (true) for active line and 0 (false) for no link (timeout)
123 */
124static int wait_link_up(struct pcie_dw_ti *pci)
125{
126 unsigned long timeout;
127
128 timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS;
129 while (!is_link_up(pci)) {
130 if (get_timer(0) > timeout)
131 return 0;
132 };
133
134 return 1;
135}
136
137static int pcie_dw_ti_pcie_link_up(struct pcie_dw_ti *pci, u32 cap_speed)
138{
139 u32 val;
140
141 if (is_link_up(pci)) {
142 printf("PCI Link already up before configuration!\n");
143 return 1;
144 }
145
146 /* DW pre link configurations */
147 pcie_dw_configure(pci, cap_speed);
148
149 /* Initiate link training */
150 val = readl(pci->app_base + PCIE_CMD_STATUS);
151 val |= LTSSM_EN_VAL;
152 writel(val, pci->app_base + PCIE_CMD_STATUS);
153
154 /* Check that link was established */
155 if (!wait_link_up(pci))
156 return 0;
157
158 /*
159 * Link can be established in Gen 1. still need to wait
160 * till MAC nagaotiation is completed
161 */
162 udelay(100);
163
164 return 1;
165}
166
Sekhar Nori18db23d2019-08-01 19:12:57 +0530167static int pcie_am654_set_mode(struct pcie_dw_ti *pci,
168 enum dw_pcie_device_mode mode)
169{
170 struct regmap *syscon;
171 u32 val;
172 u32 mask;
173 int ret;
174
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100175 syscon = syscon_regmap_lookup_by_phandle(pci->dw.dev,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530176 "ti,syscon-pcie-mode");
177 if (IS_ERR(syscon))
178 return 0;
179
180 mask = AM654_PCIE_DEV_TYPE_MASK;
181
182 switch (mode) {
183 case DW_PCIE_RC_TYPE:
184 val = RC;
185 break;
186 case DW_PCIE_EP_TYPE:
187 val = EP;
188 break;
189 default:
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100190 dev_err(pci->dw.dev, "INVALID device type %d\n", mode);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530191 return -EINVAL;
192 }
193
194 ret = regmap_update_bits(syscon, 0, mask, val);
195 if (ret) {
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100196 dev_err(pci->dw.dev, "failed to set pcie mode\n");
Sekhar Nori18db23d2019-08-01 19:12:57 +0530197 return ret;
198 }
199
200 return 0;
201}
202
203static int pcie_dw_init_id(struct pcie_dw_ti *pci)
204{
205 struct regmap *devctrl_regs;
206 unsigned int id;
207 int ret;
208
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100209 devctrl_regs = syscon_regmap_lookup_by_phandle(pci->dw.dev,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530210 "ti,syscon-pcie-id");
211 if (IS_ERR(devctrl_regs))
212 return PTR_ERR(devctrl_regs);
213
214 ret = regmap_read(devctrl_regs, 0, &id);
215 if (ret)
216 return ret;
217
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100218 dw_pcie_dbi_write_enable(&pci->dw, true);
219 writew(id & PCIE_VENDORID_MASK, pci->dw.dbi_base + PCI_VENDOR_ID);
220 writew(id >> PCIE_DEVICEID_SHIFT, pci->dw.dbi_base + PCI_DEVICE_ID);
221 dw_pcie_dbi_write_enable(&pci->dw, false);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530222
223 return 0;
224}
225
226/**
227 * pcie_dw_ti_probe() - Probe the PCIe bus for active link
228 *
229 * @dev: A pointer to the device being operated on
230 *
231 * Probe for an active link on the PCIe bus and configure the controller
232 * to enable this port.
233 *
234 * Return: 0 on success, else -ENODEV
235 */
236static int pcie_dw_ti_probe(struct udevice *dev)
237{
238 struct pcie_dw_ti *pci = dev_get_priv(dev);
239 struct udevice *ctlr = pci_get_controller(dev);
240 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
241 struct power_domain pci_pwrdmn;
242 struct phy phy0, phy1;
243 int ret;
244
245 ret = power_domain_get_by_index(dev, &pci_pwrdmn, 0);
246 if (ret) {
247 dev_err(dev, "failed to get power domain\n");
248 return ret;
249 }
250
251 ret = power_domain_on(&pci_pwrdmn);
252 if (ret) {
253 dev_err(dev, "Power domain on failed\n");
254 return ret;
255 }
256
257 ret = generic_phy_get_by_name(dev, "pcie-phy0", &phy0);
258 if (ret) {
259 dev_err(dev, "Unable to get phy0");
260 return ret;
261 }
262 generic_phy_reset(&phy0);
263 generic_phy_init(&phy0);
264 generic_phy_power_on(&phy0);
265
266 ret = generic_phy_get_by_name(dev, "pcie-phy1", &phy1);
267 if (ret) {
268 dev_err(dev, "Unable to get phy1");
269 return ret;
270 }
271 generic_phy_reset(&phy1);
272 generic_phy_init(&phy1);
273 generic_phy_power_on(&phy1);
274
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100275 pci->dw.first_busno = dev_seq(dev);
276 pci->dw.dev = dev;
Sekhar Nori18db23d2019-08-01 19:12:57 +0530277
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100278 pcie_dw_setup_host(&pci->dw);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530279 pcie_dw_init_id(pci);
280
281 if (device_is_compatible(dev, "ti,am654-pcie-rc"))
282 pcie_am654_set_mode(pci, DW_PCIE_RC_TYPE);
283
284 if (!pcie_dw_ti_pcie_link_up(pci, LINK_SPEED_GEN_2)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700285 printf("PCIE-%d: Link down\n", dev_seq(dev));
Sekhar Nori18db23d2019-08-01 19:12:57 +0530286 return -ENODEV;
287 }
288
Simon Glass75e534b2020-12-16 21:20:07 -0700289 printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev_seq(dev),
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100290 pcie_dw_get_link_speed(&pci->dw),
291 pcie_dw_get_link_width(&pci->dw),
Sekhar Nori18db23d2019-08-01 19:12:57 +0530292 hose->first_busno);
293
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100294 pcie_dw_prog_outbound_atu_unroll(&pci->dw, PCIE_ATU_REGION_INDEX0,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530295 PCIE_ATU_TYPE_MEM,
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100296 pci->dw.mem.phys_start,
297 pci->dw.mem.bus_start, pci->dw.mem.size);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530298
299 return 0;
300}
301
302/**
Simon Glassaad29ae2020-12-03 16:55:21 -0700303 * pcie_dw_ti_of_to_plat() - Translate from DT to device state
Sekhar Nori18db23d2019-08-01 19:12:57 +0530304 *
305 * @dev: A pointer to the device being operated on
306 *
307 * Translate relevant data from the device tree pertaining to device @dev into
308 * state that the driver will later make use of. This state is stored in the
309 * device's private data structure.
310 *
311 * Return: 0 on success, else -EINVAL
312 */
Simon Glassaad29ae2020-12-03 16:55:21 -0700313static int pcie_dw_ti_of_to_plat(struct udevice *dev)
Sekhar Nori18db23d2019-08-01 19:12:57 +0530314{
315 struct pcie_dw_ti *pcie = dev_get_priv(dev);
316
317 /* Get the controller base address */
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100318 pcie->dw.dbi_base = (void *)dev_read_addr_name(dev, "dbics");
319 if ((fdt_addr_t)pcie->dw.dbi_base == FDT_ADDR_T_NONE)
Sekhar Nori18db23d2019-08-01 19:12:57 +0530320 return -EINVAL;
321
322 /* Get the config space base address and size */
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100323 pcie->dw.cfg_base = (void *)dev_read_addr_size_name(dev, "config",
324 &pcie->dw.cfg_size);
325 if ((fdt_addr_t)pcie->dw.cfg_base == FDT_ADDR_T_NONE)
Sekhar Nori18db23d2019-08-01 19:12:57 +0530326 return -EINVAL;
327
328 /* Get the iATU base address and size */
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100329 pcie->dw.atu_base = (void *)dev_read_addr_name(dev, "atu");
330 if ((fdt_addr_t)pcie->dw.atu_base == FDT_ADDR_T_NONE)
Sekhar Nori18db23d2019-08-01 19:12:57 +0530331 return -EINVAL;
332
333 /* Get the app base address and size */
334 pcie->app_base = (void *)dev_read_addr_name(dev, "app");
335 if ((fdt_addr_t)pcie->app_base == FDT_ADDR_T_NONE)
336 return -EINVAL;
337
338 return 0;
339}
340
341static const struct dm_pci_ops pcie_dw_ti_ops = {
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100342 .read_config = pcie_dw_read_config,
343 .write_config = pcie_dw_write_config,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530344};
345
346static const struct udevice_id pcie_dw_ti_ids[] = {
347 { .compatible = "ti,am654-pcie-rc" },
348 { }
349};
350
351U_BOOT_DRIVER(pcie_dw_ti) = {
352 .name = "pcie_dw_ti",
353 .id = UCLASS_PCI,
354 .of_match = pcie_dw_ti_ids,
355 .ops = &pcie_dw_ti_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700356 .of_to_plat = pcie_dw_ti_of_to_plat,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530357 .probe = pcie_dw_ti_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700358 .priv_auto = sizeof(struct pcie_dw_ti),
Sekhar Nori18db23d2019-08-01 19:12:57 +0530359};