summaryrefslogtreecommitdiff
path: root/drivers/mtd/renesas_rpc_hf.c
blob: 03545822b07731989e176eee22e88525877afde2 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Renesas RCar Gen3 RPC HyperFlash driver
 *
 * Copyright (C) 2016 Renesas Electronics Corporation
 * Copyright (C) 2016 Cogent Embedded, Inc.
 * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
 */

#include <malloc.h>
#include <asm/io.h>
#include <clk.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <dm/of_access.h>
#include <errno.h>
#include <fdt_support.h>
#include <flash.h>
#include <mtd.h>
#include <wait_bit.h>
#include <linux/bitops.h>
#include <mtd/cfi_flash.h>
#include <asm/global_data.h>

#define RPC_CMNCR		0x0000	/* R/W */
#define RPC_CMNCR_MD		BIT(31)
#define RPC_CMNCR_MOIIO0(val)	(((val) & 0x3) << 16)
#define RPC_CMNCR_MOIIO1(val)	(((val) & 0x3) << 18)
#define RPC_CMNCR_MOIIO2(val)	(((val) & 0x3) << 20)
#define RPC_CMNCR_MOIIO3(val)	(((val) & 0x3) << 22)
#define RPC_CMNCR_MOIIO_HIZ	(RPC_CMNCR_MOIIO0(3) | RPC_CMNCR_MOIIO1(3) | \
				 RPC_CMNCR_MOIIO2(3) | RPC_CMNCR_MOIIO3(3))
#define RPC_CMNCR_IO0FV(val)	(((val) & 0x3) << 8)
#define RPC_CMNCR_IO2FV(val)	(((val) & 0x3) << 12)
#define RPC_CMNCR_IO3FV(val)	(((val) & 0x3) << 14)
#define RPC_CMNCR_IOFV_HIZ	(RPC_CMNCR_IO0FV(3) | RPC_CMNCR_IO2FV(3) | \
				 RPC_CMNCR_IO3FV(3))
#define RPC_CMNCR_BSZ(val)	(((val) & 0x3) << 0)

#define RPC_SSLDR		0x0004	/* R/W */
#define RPC_SSLDR_SPNDL(d)	(((d) & 0x7) << 16)
#define RPC_SSLDR_SLNDL(d)	(((d) & 0x7) << 8)
#define RPC_SSLDR_SCKDL(d)	(((d) & 0x7) << 0)

#define RPC_DRCR		0x000C	/* R/W */
#define RPC_DRCR_SSLN		BIT(24)
#define RPC_DRCR_RBURST(v)	(((v) & 0x1F) << 16)
#define RPC_DRCR_RCF		BIT(9)
#define RPC_DRCR_RBE		BIT(8)
#define RPC_DRCR_SSLE		BIT(0)

#define RPC_DRCMR		0x0010	/* R/W */
#define RPC_DRCMR_CMD(c)	(((c) & 0xFF) << 16)
#define RPC_DRCMR_OCMD(c)	(((c) & 0xFF) << 0)

#define RPC_DREAR		0x0014	/* R/W */
#define RPC_DREAR_EAV(v)	(((v) & 0xFF) << 16)
#define RPC_DREAR_EAC(v)	(((v) & 0x7) << 0)

#define RPC_DROPR		0x0018	/* R/W */
#define RPC_DROPR_OPD3(o)	(((o) & 0xFF) << 24)
#define RPC_DROPR_OPD2(o)	(((o) & 0xFF) << 16)
#define RPC_DROPR_OPD1(o)	(((o) & 0xFF) << 8)
#define RPC_DROPR_OPD0(o)	(((o) & 0xFF) << 0)

#define RPC_DRENR		0x001C	/* R/W */
#define RPC_DRENR_CDB(o)	(u32)((((o) & 0x3) << 30))
#define RPC_DRENR_OCDB(o)	(((o) & 0x3) << 28)
#define RPC_DRENR_ADB(o)	(((o) & 0x3) << 24)
#define RPC_DRENR_OPDB(o)	(((o) & 0x3) << 20)
#define RPC_DRENR_SPIDB(o)	(((o) & 0x3) << 16)
#define RPC_DRENR_DME		BIT(15)
#define RPC_DRENR_CDE		BIT(14)
#define RPC_DRENR_OCDE		BIT(12)
#define RPC_DRENR_ADE(v)	(((v) & 0xF) << 8)
#define RPC_DRENR_OPDE(v)	(((v) & 0xF) << 4)

#define RPC_SMCR		0x0020	/* R/W */
#define RPC_SMCR_SSLKP		BIT(8)
#define RPC_SMCR_SPIRE		BIT(2)
#define RPC_SMCR_SPIWE		BIT(1)
#define RPC_SMCR_SPIE		BIT(0)

#define RPC_SMCMR		0x0024	/* R/W */
#define RPC_SMCMR_CMD(c)	(((c) & 0xFF) << 16)
#define RPC_SMCMR_OCMD(c)	(((c) & 0xFF) << 0)

#define RPC_SMADR		0x0028	/* R/W */
#define RPC_SMOPR		0x002C	/* R/W */
#define RPC_SMOPR_OPD0(o)	(((o) & 0xFF) << 0)
#define RPC_SMOPR_OPD1(o)	(((o) & 0xFF) << 8)
#define RPC_SMOPR_OPD2(o)	(((o) & 0xFF) << 16)
#define RPC_SMOPR_OPD3(o)	(((o) & 0xFF) << 24)

#define RPC_SMENR		0x0030	/* R/W */
#define RPC_SMENR_CDB(o)	(((o) & 0x3) << 30)
#define RPC_SMENR_OCDB(o)	(((o) & 0x3) << 28)
#define RPC_SMENR_ADB(o)	(((o) & 0x3) << 24)
#define RPC_SMENR_OPDB(o)	(((o) & 0x3) << 20)
#define RPC_SMENR_SPIDB(o)	(((o) & 0x3) << 16)
#define RPC_SMENR_DME		BIT(15)
#define RPC_SMENR_CDE		BIT(14)
#define RPC_SMENR_OCDE		BIT(12)
#define RPC_SMENR_ADE(v)	(((v) & 0xF) << 8)
#define RPC_SMENR_OPDE(v)	(((v) & 0xF) << 4)
#define RPC_SMENR_SPIDE(v)	(((v) & 0xF) << 0)

#define RPC_SMRDR0		0x0038	/* R */
#define RPC_SMRDR1		0x003C	/* R */
#define RPC_SMWDR0		0x0040	/* R/W */
#define RPC_SMWDR1		0x0044	/* R/W */
#define RPC_CMNSR		0x0048	/* R */
#define RPC_CMNSR_SSLF		BIT(1)
#define	RPC_CMNSR_TEND		BIT(0)

#define RPC_DRDMCR		0x0058	/* R/W */
#define RPC_DRDMCR_DMCYC(v)	(((v) & 0xF) << 0)

#define RPC_DRDRENR		0x005C	/* R/W */
#define RPC_DRDRENR_HYPE	(0x5 << 12)
#define RPC_DRDRENR_ADDRE	BIT(8)
#define RPC_DRDRENR_OPDRE	BIT(4)
#define RPC_DRDRENR_DRDRE	BIT(0)

#define RPC_SMDMCR		0x0060	/* R/W */
#define RPC_SMDMCR_DMCYC(v)	(((v) & 0xF) << 0)

#define RPC_SMDRENR		0x0064	/* R/W */
#define RPC_SMDRENR_HYPE	(0x5 << 12)
#define RPC_SMDRENR_ADDRE	BIT(8)
#define RPC_SMDRENR_OPDRE	BIT(4)
#define RPC_SMDRENR_SPIDRE	BIT(0)

#define RPC_PHYCNT		0x007C	/* R/W */
#define RPC_PHYCNT_CAL		BIT(31)
#define PRC_PHYCNT_OCTA_AA	BIT(22)
#define PRC_PHYCNT_OCTA_SA	BIT(23)
#define PRC_PHYCNT_EXDS		BIT(21)
#define RPC_PHYCNT_OCT		BIT(20)
#define RPC_PHYCNT_WBUF2	BIT(4)
#define RPC_PHYCNT_WBUF		BIT(2)
#define RPC_PHYCNT_MEM(v)	(((v) & 0x3) << 0)

#define RPC_PHYINT		0x0088	/* R/W */
#define RPC_PHYINT_RSTEN	BIT(18)
#define RPC_PHYINT_WPEN		BIT(17)
#define RPC_PHYINT_INTEN	BIT(16)
#define RPC_PHYINT_RST		BIT(2)
#define RPC_PHYINT_WP		BIT(1)
#define RPC_PHYINT_INT		BIT(0)

#define RPC_WBUF		0x8000	/* R/W size=4/8/16/32/64Bytes */
#define RPC_WBUF_SIZE		0x100

static phys_addr_t rpc_base;

enum rpc_hf_size {
	RPC_HF_SIZE_16BIT = RPC_SMENR_SPIDE(0x8),
	RPC_HF_SIZE_32BIT = RPC_SMENR_SPIDE(0xC),
	RPC_HF_SIZE_64BIT = RPC_SMENR_SPIDE(0xF),
};

static int rpc_hf_wait_tend(void)
{
	void __iomem *reg = (void __iomem *)rpc_base + RPC_CMNSR;
	return wait_for_bit_le32(reg, RPC_CMNSR_TEND, true, 1000, 0);
}

static int rpc_hf_mode(bool man)
{
	int ret;

	ret = rpc_hf_wait_tend();
	if (ret)
		return ret;

	clrsetbits_le32(rpc_base + RPC_PHYCNT,
		 RPC_PHYCNT_WBUF | RPC_PHYCNT_WBUF2 |
		 RPC_PHYCNT_CAL | RPC_PHYCNT_MEM(3),
		 RPC_PHYCNT_CAL | RPC_PHYCNT_MEM(3));

	clrsetbits_le32(rpc_base + RPC_CMNCR,
		 RPC_CMNCR_MD | RPC_CMNCR_BSZ(3),
		 RPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ |
		 (man ? RPC_CMNCR_MD : 0) | RPC_CMNCR_BSZ(1));

	if (man)
		return 0;

	writel(RPC_DRCR_RBURST(0x1F) | RPC_DRCR_RCF | RPC_DRCR_RBE,
	       rpc_base + RPC_DRCR);

	writel(RPC_DRCMR_CMD(0xA0), rpc_base + RPC_DRCMR);
	writel(RPC_DRENR_CDB(2) | RPC_DRENR_OCDB(2) | RPC_DRENR_ADB(2) |
	       RPC_DRENR_SPIDB(2) | RPC_DRENR_CDE | RPC_DRENR_OCDE |
	       RPC_DRENR_ADE(4), rpc_base + RPC_DRENR);
	writel(RPC_DRDMCR_DMCYC(0xE), rpc_base + RPC_DRDMCR);
	writel(RPC_DRDRENR_HYPE | RPC_DRDRENR_ADDRE | RPC_DRDRENR_DRDRE,
	       rpc_base + RPC_DRDRENR);

	/* Dummy read */
	readl(rpc_base + RPC_DRCR);

	return 0;
}

static int rpc_hf_xfer(void *addr, u64 wdata, u64 *rdata,
		       enum rpc_hf_size size, bool write)
{
	int ret;
	u32 val;

	ret = rpc_hf_mode(1);
	if (ret)
		return ret;

	/* Submit HF address, SMCMR CMD[7] ~= CA Bit# 47 (R/nW) */
	writel(write ? 0 : RPC_SMCMR_CMD(0x80), rpc_base + RPC_SMCMR);
	writel((uintptr_t)addr >> 1, rpc_base + RPC_SMADR);
	writel(0x0, rpc_base + RPC_SMOPR);

	writel(RPC_SMDRENR_HYPE | RPC_SMDRENR_ADDRE | RPC_SMDRENR_SPIDRE,
	       rpc_base + RPC_SMDRENR);

	val = RPC_SMENR_CDB(2) | RPC_SMENR_OCDB(2) |
	      RPC_SMENR_ADB(2) | RPC_SMENR_SPIDB(2) |
	      RPC_SMENR_CDE | RPC_SMENR_OCDE | RPC_SMENR_ADE(4) | size;

	if (write) {
		writel(val, rpc_base + RPC_SMENR);

		if (size == RPC_HF_SIZE_64BIT)
			writeq(cpu_to_be64(wdata), rpc_base + RPC_SMWDR0);
		else
			writel(cpu_to_be32(wdata), rpc_base + RPC_SMWDR0);

		writel(RPC_SMCR_SPIWE | RPC_SMCR_SPIE, rpc_base + RPC_SMCR);
	} else {
		val |= RPC_SMENR_DME;

		writel(RPC_SMDMCR_DMCYC(0xE), rpc_base + RPC_SMDMCR);

		writel(val, rpc_base + RPC_SMENR);

		writel(RPC_SMCR_SPIRE | RPC_SMCR_SPIE, rpc_base + RPC_SMCR);

		ret = rpc_hf_wait_tend();
		if (ret)
			return ret;

		if (size == RPC_HF_SIZE_64BIT)
			*rdata = be64_to_cpu(readq(rpc_base + RPC_SMRDR0));
		else
			*rdata = be32_to_cpu(readl(rpc_base + RPC_SMRDR0));
	}

	return rpc_hf_mode(0);
}

static void rpc_hf_write_cmd(void *addr, u64 wdata, enum rpc_hf_size size)
{
	int ret;

	ret = rpc_hf_xfer(addr, wdata, NULL, size, 1);
	if (ret)
		printf("RPC: Write failed, ret=%i\n", ret);
}

static u64 rpc_hf_read_reg(void *addr, enum rpc_hf_size size)
{
	u64 rdata = 0;
	int ret;

	ret = rpc_hf_xfer(addr, 0, &rdata, size, 0);
	if (ret)
		printf("RPC: Read failed, ret=%i\n", ret);

	return rdata;
}

void flash_write8(u8 value, void *addr)
{
	rpc_hf_write_cmd(addr, value, RPC_HF_SIZE_16BIT);
}

void flash_write16(u16 value, void *addr)
{
	rpc_hf_write_cmd(addr, value, RPC_HF_SIZE_16BIT);
}

void flash_write32(u32 value, void *addr)
{
	rpc_hf_write_cmd(addr, value, RPC_HF_SIZE_32BIT);
}

void flash_write64(u64 value, void *addr)
{
	rpc_hf_write_cmd(addr, value, RPC_HF_SIZE_64BIT);
}

u8 flash_read8(void *addr)
{
	return rpc_hf_read_reg(addr, RPC_HF_SIZE_16BIT);
}

u16 flash_read16(void *addr)
{
	return rpc_hf_read_reg(addr, RPC_HF_SIZE_16BIT);
}

u32 flash_read32(void *addr)
{
	return rpc_hf_read_reg(addr, RPC_HF_SIZE_32BIT);
}

u64 flash_read64(void *addr)
{
	return rpc_hf_read_reg(addr, RPC_HF_SIZE_64BIT);
}

static int rpc_hf_bind(struct udevice *parent)
{
	const void *fdt = gd->fdt_blob;
	ofnode node;
	int ret, off;

	/*
	 * Check if there are any SPI NOR child nodes, if so, do NOT bind
	 * as this controller will be operated by the QSPI driver instead.
	 */
	dev_for_each_subnode(node, parent) {
		off = ofnode_to_offset(node);

		ret = fdt_node_check_compatible(fdt, off, "spi-flash");
		if (!ret)
			return -ENODEV;

		ret = fdt_node_check_compatible(fdt, off, "jedec,spi-nor");
		if (!ret)
			return -ENODEV;
	}

	return 0;
}

static int rpc_hf_probe(struct udevice *dev)
{
	void *blob = (void *)gd->fdt_blob;
	const fdt32_t *cell;
	int node = dev_of_offset(dev);
	int parent, addrc, sizec, len, ret;
	struct clk clk;
	phys_addr_t flash_base;

	parent = fdt_parent_offset(blob, node);
	fdt_support_default_count_cells(blob, parent, &addrc, &sizec);
	cell = fdt_getprop(blob, node, "reg", &len);
	if (!cell)
		return -ENOENT;

	if (addrc != 2 || sizec != 2)
		return -EINVAL;

	ret = clk_get_by_index(dev, 0, &clk);
	if (ret < 0) {
		dev_err(dev, "Failed to get RPC clock\n");
		return ret;
	}

	ret = clk_enable(&clk);
	if (ret) {
		dev_err(dev, "Failed to enable RPC clock\n");
		return ret;
	}

	rpc_base = fdt_translate_address(blob, node, cell);
	flash_base = fdt_translate_address(blob, node, cell + addrc + sizec);

	flash_info[0].dev = dev;
	flash_info[0].base = flash_base;
	cfi_flash_num_flash_banks = 1;
	gd->bd->bi_flashstart = flash_base;

	return 0;
}

static const struct udevice_id rpc_hf_ids[] = {
	{ .compatible = "renesas,r7s72100-rpc-if" },
	{ .compatible = "renesas,rcar-gen3-rpc-if" },
	{ .compatible = "renesas,rcar-gen4-rpc-if" },
	{}
};

U_BOOT_DRIVER(rpc_hf) = {
	.name		= "rpc_hf",
	.id		= UCLASS_MTD,
	.of_match	= rpc_hf_ids,
	.bind		= rpc_hf_bind,
	.probe		= rpc_hf_probe,
};