~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: f2edea9899a4004e9c188a288fa772d64f220a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// SPDX-License-Identifier: GPL-2.0
/*
 * pcie_uniphier.c - Socionext UniPhier PCIe driver
 * Copyright 2019-2021 Socionext, Inc.
 */

#include <clk.h>
#include <common.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <generic-phy.h>
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/compat.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <pci.h>
#include <reset.h>

DECLARE_GLOBAL_DATA_PTR;

/* DBI registers */
#define PCIE_LINK_STATUS_REG		0x0080
#define PCIE_LINK_STATUS_WIDTH_MASK	GENMASK(25, 20)
#define PCIE_LINK_STATUS_SPEED_MASK	GENMASK(19, 16)

#define PCIE_MISC_CONTROL_1_OFF		0x08BC
#define PCIE_DBI_RO_WR_EN		BIT(0)

/* DBI iATU registers */
#define PCIE_ATU_VIEWPORT		0x0900
#define PCIE_ATU_REGION_INBOUND		BIT(31)
#define PCIE_ATU_REGION_OUTBOUND	0
#define PCIE_ATU_REGION_INDEX_MASK	GENMASK(3, 0)

#define PCIE_ATU_CR1			0x0904
#define PCIE_ATU_TYPE_MEM		0
#define PCIE_ATU_TYPE_IO		2
#define PCIE_ATU_TYPE_CFG0		4
#define PCIE_ATU_TYPE_CFG1		5

#define PCIE_ATU_CR2			0x0908
#define PCIE_ATU_ENABLE			BIT(31)
#define PCIE_ATU_MATCH_MODE		BIT(30)
#define PCIE_ATU_BAR_NUM_MASK		GENMASK(10, 8)

#define PCIE_ATU_LOWER_BASE		0x090C
#define PCIE_ATU_UPPER_BASE		0x0910
#define PCIE_ATU_LIMIT			0x0914
#define PCIE_ATU_LOWER_TARGET		0x0918
#define PCIE_ATU_BUS(x)			FIELD_PREP(GENMASK(31, 24), x)
#define PCIE_ATU_DEV(x)			FIELD_PREP(GENMASK(23, 19), x)
#define PCIE_ATU_FUNC(x)		FIELD_PREP(GENMASK(18, 16), x)
#define PCIE_ATU_UPPER_TARGET		0x091C

/* Link Glue registers */
#define PCL_PINCTRL0			0x002c
#define PCL_PERST_PLDN_REGEN		BIT(12)
#define PCL_PERST_NOE_REGEN		BIT(11)
#define PCL_PERST_OUT_REGEN		BIT(8)
#define PCL_PERST_PLDN_REGVAL		BIT(4)
#define PCL_PERST_NOE_REGVAL		BIT(3)
#define PCL_PERST_OUT_REGVAL		BIT(0)

#define PCL_MODE			0x8000
#define PCL_MODE_REGEN			BIT(8)
#define PCL_MODE_REGVAL			BIT(0)

#define PCL_APP_READY_CTRL		0x8008
#define PCL_APP_LTSSM_ENABLE		BIT(0)

#define PCL_APP_PM0			0x8078
#define PCL_SYS_AUX_PWR_DET		BIT(8)

#define PCL_STATUS_LINK			0x8140
#define PCL_RDLH_LINK_UP		BIT(1)
#define PCL_XMLH_LINK_UP		BIT(0)

#define LINK_UP_TIMEOUT_MS		100

struct uniphier_pcie_priv {
	void *base;
	void *dbi_base;
	void *cfg_base;
	fdt_size_t cfg_size;
	struct fdt_resource link_res;
	struct fdt_resource dbi_res;
	struct fdt_resource cfg_res;

	struct clk clk;
	struct reset_ctl rst;
	struct phy phy;

	struct pci_region io;
	struct pci_region mem;
};

static int pcie_dw_get_link_speed(struct uniphier_pcie_priv *priv)
{
	u32 val = readl(priv->dbi_base + PCIE_LINK_STATUS_REG);

	return FIELD_GET(PCIE_LINK_STATUS_SPEED_MASK, val);
}

static int pcie_dw_get_link_width(struct uniphier_pcie_priv *priv)
{
	u32 val = readl(priv->dbi_base + PCIE_LINK_STATUS_REG);

	return FIELD_GET(PCIE_LINK_STATUS_WIDTH_MASK, val);
}

static void pcie_dw_prog_outbound_atu(struct uniphier_pcie_priv *priv,
				      int index, int type, u64 cpu_addr,
				      u64 pci_addr, u32 size)
{
	writel(PCIE_ATU_REGION_OUTBOUND
	       | FIELD_PREP(PCIE_ATU_REGION_INDEX_MASK, index),
	       priv->dbi_base + PCIE_ATU_VIEWPORT);
	writel(lower_32_bits(cpu_addr),
	       priv->dbi_base + PCIE_ATU_LOWER_BASE);
	writel(upper_32_bits(cpu_addr),
	       priv->dbi_base + PCIE_ATU_UPPER_BASE);
	writel(lower_32_bits(cpu_addr + size - 1),
	       priv->dbi_base + PCIE_ATU_LIMIT);
	writel(lower_32_bits(pci_addr),
	       priv->dbi_base + PCIE_ATU_LOWER_TARGET);
	writel(upper_32_bits(pci_addr),
	       priv->dbi_base + PCIE_ATU_UPPER_TARGET);

	writel(type, priv->dbi_base + PCIE_ATU_CR1);
	writel(PCIE_ATU_ENABLE, priv->dbi_base + PCIE_ATU_CR2);
}

static int uniphier_pcie_addr_valid(pci_dev_t bdf, int first_busno)
{
	/* accept only device {0,1} on first bus */
	if ((PCI_BUS(bdf) != first_busno) || (PCI_DEV(bdf) > 1))
		return -EINVAL;

	return 0;
}

static int uniphier_pcie_conf_address(const struct udevice *dev, pci_dev_t bdf,
				      uint offset, void **paddr)
{
	struct uniphier_pcie_priv *priv = dev_get_priv(dev);
	u32 busdev;
	int seq = dev_seq(dev);
	int ret;

	ret = uniphier_pcie_addr_valid(bdf, seq);
	if (ret)
		return ret;

	if ((PCI_BUS(bdf) == seq) && !PCI_DEV(bdf)) {
		*paddr = (void *)(priv->dbi_base + offset);
		return 0;
	}

	busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - seq)
		| PCIE_ATU_DEV(PCI_DEV(bdf))
		| PCIE_ATU_FUNC(PCI_FUNC(bdf));

	pcie_dw_prog_outbound_atu(priv, 0,
				  PCIE_ATU_TYPE_CFG0, (u64)priv->cfg_base,
				  busdev, priv->cfg_size);
	*paddr = (void *)(priv->cfg_base + offset);

	return 0;
}

static int uniphier_pcie_read_config(const struct udevice *dev, pci_dev_t bdf,
				     uint offset, ulong *valp,
				     enum pci_size_t size)
{
	return pci_generic_mmap_read_config(dev, uniphier_pcie_conf_address,
					    bdf, offset, valp, size);
}

static int uniphier_pcie_write_config(struct udevice *dev, pci_dev_t bdf,
				      uint offset, ulong val,
				      enum pci_size_t size)
{
	return pci_generic_mmap_write_config(dev, uniphier_pcie_conf_address,
					     bdf, offset, val, size);
}

static void uniphier_pcie_ltssm_enable(struct uniphier_pcie_priv *priv,
				       bool enable)
{
	u32 val;

	val = readl(priv->base + PCL_APP_READY_CTRL);
	if (enable)
		val |= PCL_APP_LTSSM_ENABLE;
	else
		val &= ~PCL_APP_LTSSM_ENABLE;
	writel(val, priv->base + PCL_APP_READY_CTRL);
}

static int uniphier_pcie_link_up(struct uniphier_pcie_priv *priv)
{
	u32 val, mask;

	val = readl(priv->base + PCL_STATUS_LINK);
	mask = PCL_RDLH_LINK_UP | PCL_XMLH_LINK_UP;

	return (val & mask) == mask;
}

static int uniphier_pcie_wait_link(struct uniphier_pcie_priv *priv)
{
	unsigned long timeout;

	timeout = get_timer(0) + LINK_UP_TIMEOUT_MS;

	while (get_timer(0) < timeout) {
		if (uniphier_pcie_link_up(priv))
			return 0;
	}

	return -ETIMEDOUT;
}

static int uniphier_pcie_establish_link(struct uniphier_pcie_priv *priv)
{
	if (uniphier_pcie_link_up(priv))
		return 0;

	uniphier_pcie_ltssm_enable(priv, true);

	return uniphier_pcie_wait_link(priv);
}

static void uniphier_pcie_init_rc(struct uniphier_pcie_priv *priv)
{
	u32 val;

	/* set RC mode */
	val = readl(priv->base + PCL_MODE);
	val |= PCL_MODE_REGEN;
	val &= ~PCL_MODE_REGVAL;
	writel(val, priv->base + PCL_MODE);

	/* use auxiliary power detection */
	val = readl(priv->base + PCL_APP_PM0);
	val |= PCL_SYS_AUX_PWR_DET;
	writel(val, priv->base + PCL_APP_PM0);

	/* assert PERST# */
	val = readl(priv->base + PCL_PINCTRL0);
	val &= ~(PCL_PERST_NOE_REGVAL | PCL_PERST_OUT_REGVAL
		 | PCL_PERST_PLDN_REGVAL);
	val |= PCL_PERST_NOE_REGEN | PCL_PERST_OUT_REGEN
		| PCL_PERST_PLDN_REGEN;
	writel(val, priv->base + PCL_PINCTRL0);

	uniphier_pcie_ltssm_enable(priv, false);

	mdelay(100);

	/* deassert PERST# */
	val = readl(priv->base + PCL_PINCTRL0);
	val |= PCL_PERST_OUT_REGVAL | PCL_PERST_OUT_REGEN;
	writel(val, priv->base + PCL_PINCTRL0);
}

static void uniphier_pcie_setup_rc(struct uniphier_pcie_priv *priv,
				   struct pci_controller *hose)
{
	/* Store the IO and MEM windows settings for future use by the ATU */
	priv->io.phys_start  = hose->regions[0].phys_start; /* IO base */
	priv->io.bus_start   = hose->regions[0].bus_start;  /* IO_bus_addr */
	priv->io.size	     = hose->regions[0].size;	    /* IO size */
	priv->mem.phys_start = hose->regions[1].phys_start; /* MEM base */
	priv->mem.bus_start  = hose->regions[1].bus_start;  /* MEM_bus_addr */
	priv->mem.size	     = hose->regions[1].size;	    /* MEM size */

	/* outbound: IO */
	pcie_dw_prog_outbound_atu(priv, 0,
				  PCIE_ATU_TYPE_IO, priv->io.phys_start,
				  priv->io.bus_start, priv->io.size);

	/* outbound: MEM */
	pcie_dw_prog_outbound_atu(priv, 1,
				  PCIE_ATU_TYPE_MEM, priv->mem.phys_start,
				  priv->mem.bus_start, priv->mem.size);
}

static int uniphier_pcie_probe(struct udevice *dev)
{
	struct uniphier_pcie_priv *priv = dev_get_priv(dev);
	struct udevice *ctlr = pci_get_controller(dev);
	struct pci_controller *hose = dev_get_uclass_priv(ctlr);
	int ret;

	priv->base = map_physmem(priv->link_res.start,
				 fdt_resource_size(&priv->link_res),
				 MAP_NOCACHE);
	priv->dbi_base = map_physmem(priv->dbi_res.start,
				     fdt_resource_size(&priv->dbi_res),
				     MAP_NOCACHE);
	priv->cfg_size = fdt_resource_size(&priv->cfg_res);
	priv->cfg_base = map_physmem(priv->cfg_res.start,
				     priv->cfg_size, MAP_NOCACHE);

	ret = clk_enable(&priv->clk);
	if (ret) {
		dev_err(dev, "Failed to enable clk: %d\n", ret);
		return ret;
	}
	ret = reset_deassert(&priv->rst);
	if (ret) {
		dev_err(dev, "Failed to deassert reset: %d\n", ret);
		goto out_clk_release;
	}

	ret = generic_phy_init(&priv->phy);
	if (ret) {
		dev_err(dev, "Failed to initialize phy: %d\n", ret);
		goto out_reset_release;
	}

	ret = generic_phy_power_on(&priv->phy);
	if (ret) {
		dev_err(dev, "Failed to power on phy: %d\n", ret);
		goto out_phy_exit;
	}

	uniphier_pcie_init_rc(priv);

	/* set DBI to read only */
	writel(0, priv->dbi_base + PCIE_MISC_CONTROL_1_OFF);

	uniphier_pcie_setup_rc(priv, hose);

	if (uniphier_pcie_establish_link(priv)) {
		printf("PCIE-%d: Link down\n", dev_seq(dev));
	} else {
		printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n",
		       dev_seq(dev), pcie_dw_get_link_speed(priv),
		       pcie_dw_get_link_width(priv), hose->first_busno);
	}

	return 0;

out_phy_exit:
	generic_phy_exit(&priv->phy);
out_reset_release:
	reset_release_all(&priv->rst, 1);
out_clk_release:
	clk_release_all(&priv->clk, 1);

	return ret;
}

static int uniphier_pcie_of_to_plat(struct udevice *dev)
{
	struct uniphier_pcie_priv *priv = dev_get_priv(dev);
	const void *fdt = gd->fdt_blob;
	int node = dev_of_offset(dev);
	int ret;

	ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
				     "link", &priv->link_res);
	if (ret) {
		dev_err(dev, "Failed to get link regs: %d\n", ret);
		return ret;
	}

	ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
				     "dbi", &priv->dbi_res);
	if (ret) {
		dev_err(dev, "Failed to get dbi regs: %d\n", ret);
		return ret;
	}

	ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
				     "config", &priv->cfg_res);
	if (ret) {
		dev_err(dev, "Failed to get config regs: %d\n", ret);
		return ret;
	}

	ret = clk_get_by_index(dev, 0, &priv->clk);
	if (ret) {
		dev_err(dev, "Failed to get clocks property: %d\n", ret);
		return ret;
	}

	ret = reset_get_by_index(dev, 0, &priv->rst);
	if (ret) {
		dev_err(dev, "Failed to get resets property: %d\n", ret);
		return ret;
	}

	ret = generic_phy_get_by_index(dev, 0, &priv->phy);
	if (ret) {
		dev_err(dev, "Failed to get phy property: %d\n", ret);
		return ret;
	}

	return 0;
}

static const struct dm_pci_ops uniphier_pcie_ops = {
	.read_config	= uniphier_pcie_read_config,
	.write_config	= uniphier_pcie_write_config,
};

static const struct udevice_id uniphier_pcie_ids[] = {
	{ .compatible = "socionext,uniphier-pcie", },
	{ /* Sentinel */ }
};

U_BOOT_DRIVER(pcie_uniphier) = {
	.name     = "uniphier-pcie",
	.id       = UCLASS_PCI,
	.of_match = uniphier_pcie_ids,
	.probe    = uniphier_pcie_probe,
	.ops      = &uniphier_pcie_ops,
	.of_to_plat = uniphier_pcie_of_to_plat,
	.priv_auto = sizeof(struct uniphier_pcie_priv),
};