blob: 78a5d035865191c1d5b8967dcb11fb1a07b03ec9 [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
50#define AM654_PCIE_DEV_TYPE_MASK 0x3
51#define EP 0x0
52#define LEG_EP 0x1
53#define RC 0x2
54
55/**
56 * struct pcie_dw_ti - TI DW PCIe controller state
57 *
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010058 * @pci: The common PCIe DW structure
Sekhar Nori18db23d2019-08-01 19:12:57 +053059 * @app_base: The base address of application register space
Sekhar Nori18db23d2019-08-01 19:12:57 +053060 */
61struct pcie_dw_ti {
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010062 /* Must be first member of the struct */
63 struct pcie_dw dw;
Sekhar Nori18db23d2019-08-01 19:12:57 +053064 void *app_base;
Sekhar Nori18db23d2019-08-01 19:12:57 +053065};
66
67enum dw_pcie_device_mode {
68 DW_PCIE_UNKNOWN_TYPE,
69 DW_PCIE_EP_TYPE,
70 DW_PCIE_LEG_EP_TYPE,
71 DW_PCIE_RC_TYPE,
72};
73
Sekhar Nori18db23d2019-08-01 19:12:57 +053074/**
75 * pcie_dw_configure() - Configure link capabilities and speed
76 *
77 * @regs_base: A pointer to the PCIe controller registers
78 * @cap_speed: The capabilities and speed to configure
79 *
80 * Configure the link capabilities and speed in the PCIe root complex.
81 */
82static void pcie_dw_configure(struct pcie_dw_ti *pci, u32 cap_speed)
83{
84 u32 val;
85
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010086 dw_pcie_dbi_write_enable(&pci->dw, true);
Sekhar Nori18db23d2019-08-01 19:12:57 +053087
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010088 val = readl(pci->dw.dbi_base + PCIE_LINK_CAPABILITY);
Sekhar Nori18db23d2019-08-01 19:12:57 +053089 val &= ~TARGET_LINK_SPEED_MASK;
90 val |= cap_speed;
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010091 writel(val, pci->dw.dbi_base + PCIE_LINK_CAPABILITY);
Sekhar Nori18db23d2019-08-01 19:12:57 +053092
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010093 val = readl(pci->dw.dbi_base + PCIE_LINK_CTL_2);
Sekhar Nori18db23d2019-08-01 19:12:57 +053094 val &= ~TARGET_LINK_SPEED_MASK;
95 val |= cap_speed;
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010096 writel(val, pci->dw.dbi_base + PCIE_LINK_CTL_2);
Sekhar Nori18db23d2019-08-01 19:12:57 +053097
Neil Armstrongc0c39ce2021-03-25 15:49:19 +010098 dw_pcie_dbi_write_enable(&pci->dw, false);
Sekhar Nori18db23d2019-08-01 19:12:57 +053099}
100
101/**
102 * is_link_up() - Return the link state
103 *
104 * @regs_base: A pointer to the PCIe DBICS registers
105 *
106 * Return: 1 (true) for active line and 0 (false) for no link
107 */
108static int is_link_up(struct pcie_dw_ti *pci)
109{
110 u32 val;
111
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100112 val = readl(pci->dw.dbi_base + PCIE_PORT_DEBUG0);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530113 val &= PORT_LOGIC_LTSSM_STATE_MASK;
114
115 return (val == PORT_LOGIC_LTSSM_STATE_L0);
116}
117
118/**
119 * wait_link_up() - Wait for the link to come up
120 *
121 * @regs_base: A pointer to the PCIe controller registers
122 *
123 * Return: 1 (true) for active line and 0 (false) for no link (timeout)
124 */
125static int wait_link_up(struct pcie_dw_ti *pci)
126{
127 unsigned long timeout;
128
129 timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS;
130 while (!is_link_up(pci)) {
131 if (get_timer(0) > timeout)
132 return 0;
133 };
134
135 return 1;
136}
137
138static int pcie_dw_ti_pcie_link_up(struct pcie_dw_ti *pci, u32 cap_speed)
139{
140 u32 val;
141
142 if (is_link_up(pci)) {
143 printf("PCI Link already up before configuration!\n");
144 return 1;
145 }
146
147 /* DW pre link configurations */
148 pcie_dw_configure(pci, cap_speed);
149
150 /* Initiate link training */
151 val = readl(pci->app_base + PCIE_CMD_STATUS);
152 val |= LTSSM_EN_VAL;
153 writel(val, pci->app_base + PCIE_CMD_STATUS);
154
155 /* Check that link was established */
156 if (!wait_link_up(pci))
157 return 0;
158
159 /*
160 * Link can be established in Gen 1. still need to wait
161 * till MAC nagaotiation is completed
162 */
163 udelay(100);
164
165 return 1;
166}
167
Sekhar Nori18db23d2019-08-01 19:12:57 +0530168static int pcie_am654_set_mode(struct pcie_dw_ti *pci,
169 enum dw_pcie_device_mode mode)
170{
171 struct regmap *syscon;
172 u32 val;
173 u32 mask;
174 int ret;
175
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100176 syscon = syscon_regmap_lookup_by_phandle(pci->dw.dev,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530177 "ti,syscon-pcie-mode");
178 if (IS_ERR(syscon))
179 return 0;
180
181 mask = AM654_PCIE_DEV_TYPE_MASK;
182
183 switch (mode) {
184 case DW_PCIE_RC_TYPE:
185 val = RC;
186 break;
187 case DW_PCIE_EP_TYPE:
188 val = EP;
189 break;
190 default:
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100191 dev_err(pci->dw.dev, "INVALID device type %d\n", mode);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530192 return -EINVAL;
193 }
194
195 ret = regmap_update_bits(syscon, 0, mask, val);
196 if (ret) {
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100197 dev_err(pci->dw.dev, "failed to set pcie mode\n");
Sekhar Nori18db23d2019-08-01 19:12:57 +0530198 return ret;
199 }
200
201 return 0;
202}
203
204static int pcie_dw_init_id(struct pcie_dw_ti *pci)
205{
206 struct regmap *devctrl_regs;
207 unsigned int id;
208 int ret;
209
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100210 devctrl_regs = syscon_regmap_lookup_by_phandle(pci->dw.dev,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530211 "ti,syscon-pcie-id");
212 if (IS_ERR(devctrl_regs))
213 return PTR_ERR(devctrl_regs);
214
215 ret = regmap_read(devctrl_regs, 0, &id);
216 if (ret)
217 return ret;
218
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100219 dw_pcie_dbi_write_enable(&pci->dw, true);
220 writew(id & PCIE_VENDORID_MASK, pci->dw.dbi_base + PCI_VENDOR_ID);
221 writew(id >> PCIE_DEVICEID_SHIFT, pci->dw.dbi_base + PCI_DEVICE_ID);
222 dw_pcie_dbi_write_enable(&pci->dw, false);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530223
224 return 0;
225}
226
227/**
228 * pcie_dw_ti_probe() - Probe the PCIe bus for active link
229 *
230 * @dev: A pointer to the device being operated on
231 *
232 * Probe for an active link on the PCIe bus and configure the controller
233 * to enable this port.
234 *
235 * Return: 0 on success, else -ENODEV
236 */
237static int pcie_dw_ti_probe(struct udevice *dev)
238{
239 struct pcie_dw_ti *pci = dev_get_priv(dev);
240 struct udevice *ctlr = pci_get_controller(dev);
241 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
242 struct power_domain pci_pwrdmn;
243 struct phy phy0, phy1;
244 int ret;
245
246 ret = power_domain_get_by_index(dev, &pci_pwrdmn, 0);
247 if (ret) {
248 dev_err(dev, "failed to get power domain\n");
249 return ret;
250 }
251
252 ret = power_domain_on(&pci_pwrdmn);
253 if (ret) {
254 dev_err(dev, "Power domain on failed\n");
255 return ret;
256 }
257
258 ret = generic_phy_get_by_name(dev, "pcie-phy0", &phy0);
259 if (ret) {
260 dev_err(dev, "Unable to get phy0");
261 return ret;
262 }
263 generic_phy_reset(&phy0);
264 generic_phy_init(&phy0);
265 generic_phy_power_on(&phy0);
266
267 ret = generic_phy_get_by_name(dev, "pcie-phy1", &phy1);
268 if (ret) {
269 dev_err(dev, "Unable to get phy1");
270 return ret;
271 }
272 generic_phy_reset(&phy1);
273 generic_phy_init(&phy1);
274 generic_phy_power_on(&phy1);
275
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100276 pci->dw.first_busno = dev_seq(dev);
277 pci->dw.dev = dev;
Sekhar Nori18db23d2019-08-01 19:12:57 +0530278
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100279 pcie_dw_setup_host(&pci->dw);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530280 pcie_dw_init_id(pci);
281
282 if (device_is_compatible(dev, "ti,am654-pcie-rc"))
283 pcie_am654_set_mode(pci, DW_PCIE_RC_TYPE);
284
285 if (!pcie_dw_ti_pcie_link_up(pci, LINK_SPEED_GEN_2)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700286 printf("PCIE-%d: Link down\n", dev_seq(dev));
Sekhar Nori18db23d2019-08-01 19:12:57 +0530287 return -ENODEV;
288 }
289
Simon Glass75e534b2020-12-16 21:20:07 -0700290 printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev_seq(dev),
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100291 pcie_dw_get_link_speed(&pci->dw),
292 pcie_dw_get_link_width(&pci->dw),
Sekhar Nori18db23d2019-08-01 19:12:57 +0530293 hose->first_busno);
294
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100295 pcie_dw_prog_outbound_atu_unroll(&pci->dw, PCIE_ATU_REGION_INDEX0,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530296 PCIE_ATU_TYPE_MEM,
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100297 pci->dw.mem.phys_start,
298 pci->dw.mem.bus_start, pci->dw.mem.size);
Sekhar Nori18db23d2019-08-01 19:12:57 +0530299
300 return 0;
301}
302
303/**
Simon Glassaad29ae2020-12-03 16:55:21 -0700304 * pcie_dw_ti_of_to_plat() - Translate from DT to device state
Sekhar Nori18db23d2019-08-01 19:12:57 +0530305 *
306 * @dev: A pointer to the device being operated on
307 *
308 * Translate relevant data from the device tree pertaining to device @dev into
309 * state that the driver will later make use of. This state is stored in the
310 * device's private data structure.
311 *
312 * Return: 0 on success, else -EINVAL
313 */
Simon Glassaad29ae2020-12-03 16:55:21 -0700314static int pcie_dw_ti_of_to_plat(struct udevice *dev)
Sekhar Nori18db23d2019-08-01 19:12:57 +0530315{
316 struct pcie_dw_ti *pcie = dev_get_priv(dev);
317
318 /* Get the controller base address */
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100319 pcie->dw.dbi_base = (void *)dev_read_addr_name(dev, "dbics");
320 if ((fdt_addr_t)pcie->dw.dbi_base == FDT_ADDR_T_NONE)
Sekhar Nori18db23d2019-08-01 19:12:57 +0530321 return -EINVAL;
322
323 /* Get the config space base address and size */
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100324 pcie->dw.cfg_base = (void *)dev_read_addr_size_name(dev, "config",
325 &pcie->dw.cfg_size);
326 if ((fdt_addr_t)pcie->dw.cfg_base == FDT_ADDR_T_NONE)
Sekhar Nori18db23d2019-08-01 19:12:57 +0530327 return -EINVAL;
328
329 /* Get the iATU base address and size */
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100330 pcie->dw.atu_base = (void *)dev_read_addr_name(dev, "atu");
331 if ((fdt_addr_t)pcie->dw.atu_base == FDT_ADDR_T_NONE)
Sekhar Nori18db23d2019-08-01 19:12:57 +0530332 return -EINVAL;
333
334 /* Get the app base address and size */
335 pcie->app_base = (void *)dev_read_addr_name(dev, "app");
336 if ((fdt_addr_t)pcie->app_base == FDT_ADDR_T_NONE)
337 return -EINVAL;
338
339 return 0;
340}
341
342static const struct dm_pci_ops pcie_dw_ti_ops = {
Neil Armstrongc0c39ce2021-03-25 15:49:19 +0100343 .read_config = pcie_dw_read_config,
344 .write_config = pcie_dw_write_config,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530345};
346
347static const struct udevice_id pcie_dw_ti_ids[] = {
348 { .compatible = "ti,am654-pcie-rc" },
349 { }
350};
351
352U_BOOT_DRIVER(pcie_dw_ti) = {
353 .name = "pcie_dw_ti",
354 .id = UCLASS_PCI,
355 .of_match = pcie_dw_ti_ids,
356 .ops = &pcie_dw_ti_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700357 .of_to_plat = pcie_dw_ti_of_to_plat,
Sekhar Nori18db23d2019-08-01 19:12:57 +0530358 .probe = pcie_dw_ti_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700359 .priv_auto = sizeof(struct pcie_dw_ti),
Sekhar Nori18db23d2019-08-01 19:12:57 +0530360};