xref: /linux/drivers/spi/spi-aspeed-smc.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1e3228ed9SCédric Le Goater // SPDX-License-Identifier: GPL-2.0-or-later
2e3228ed9SCédric Le Goater /*
3e3228ed9SCédric Le Goater  * ASPEED FMC/SPI Memory Controller Driver
4e3228ed9SCédric Le Goater  *
5e3228ed9SCédric Le Goater  * Copyright (c) 2015-2022, IBM Corporation.
6e3228ed9SCédric Le Goater  * Copyright (c) 2020, ASPEED Corporation.
7e3228ed9SCédric Le Goater  */
8e3228ed9SCédric Le Goater 
9e3228ed9SCédric Le Goater #include <linux/clk.h>
10e3228ed9SCédric Le Goater #include <linux/module.h>
11e3228ed9SCédric Le Goater #include <linux/of.h>
12e3228ed9SCédric Le Goater #include <linux/of_platform.h>
13e3228ed9SCédric Le Goater #include <linux/platform_device.h>
14e3228ed9SCédric Le Goater #include <linux/spi/spi.h>
15e3228ed9SCédric Le Goater #include <linux/spi/spi-mem.h>
16e3228ed9SCédric Le Goater 
17e3228ed9SCédric Le Goater #define DEVICE_NAME "spi-aspeed-smc"
18e3228ed9SCédric Le Goater 
19e3228ed9SCédric Le Goater /* Type setting Register */
20e3228ed9SCédric Le Goater #define CONFIG_REG			0x0
21e3228ed9SCédric Le Goater #define   CONFIG_TYPE_SPI		0x2
22e3228ed9SCédric Le Goater 
23e3228ed9SCédric Le Goater /* CE Control Register */
24e3228ed9SCédric Le Goater #define CE_CTRL_REG			0x4
25e3228ed9SCédric Le Goater 
26e3228ed9SCédric Le Goater /* CEx Control Register */
27e3228ed9SCédric Le Goater #define CE0_CTRL_REG			0x10
28e3228ed9SCédric Le Goater #define   CTRL_IO_MODE_MASK		GENMASK(30, 28)
29e3228ed9SCédric Le Goater #define   CTRL_IO_SINGLE_DATA	        0x0
30e3228ed9SCédric Le Goater #define   CTRL_IO_DUAL_DATA		BIT(29)
31e3228ed9SCédric Le Goater #define   CTRL_IO_QUAD_DATA		BIT(30)
32e3228ed9SCédric Le Goater #define   CTRL_COMMAND_SHIFT		16
3353526ab2SCédric Le Goater #define   CTRL_IO_ADDRESS_4B		BIT(13)	/* AST2400 SPI only */
34e3228ed9SCédric Le Goater #define   CTRL_IO_DUMMY_SET(dummy)					\
35e3228ed9SCédric Le Goater 	(((((dummy) >> 2) & 0x1) << 14) | (((dummy) & 0x3) << 6))
36eeaec1eaSCédric Le Goater #define   CTRL_FREQ_SEL_SHIFT		8
37eeaec1eaSCédric Le Goater #define   CTRL_FREQ_SEL_MASK		GENMASK(11, CTRL_FREQ_SEL_SHIFT)
38e3228ed9SCédric Le Goater #define   CTRL_CE_STOP_ACTIVE		BIT(2)
39e3228ed9SCédric Le Goater #define   CTRL_IO_MODE_CMD_MASK		GENMASK(1, 0)
40e3228ed9SCédric Le Goater #define   CTRL_IO_MODE_NORMAL		0x0
41e3228ed9SCédric Le Goater #define   CTRL_IO_MODE_READ		0x1
42e3228ed9SCédric Le Goater #define   CTRL_IO_MODE_WRITE		0x2
43e3228ed9SCédric Le Goater #define   CTRL_IO_MODE_USER		0x3
44e3228ed9SCédric Le Goater 
45e3228ed9SCédric Le Goater #define   CTRL_IO_CMD_MASK		0xf0ff40c3
46e3228ed9SCédric Le Goater 
47e3228ed9SCédric Le Goater /* CEx Address Decoding Range Register */
48e3228ed9SCédric Le Goater #define CE0_SEGMENT_ADDR_REG		0x30
49e3228ed9SCédric Le Goater 
50eeaec1eaSCédric Le Goater /* CEx Read timing compensation register */
51eeaec1eaSCédric Le Goater #define CE0_TIMING_COMPENSATION_REG	0x94
52eeaec1eaSCédric Le Goater 
53e3228ed9SCédric Le Goater enum aspeed_spi_ctl_reg_value {
54e3228ed9SCédric Le Goater 	ASPEED_SPI_BASE,
55e3228ed9SCédric Le Goater 	ASPEED_SPI_READ,
56e3228ed9SCédric Le Goater 	ASPEED_SPI_WRITE,
57e3228ed9SCédric Le Goater 	ASPEED_SPI_MAX,
58e3228ed9SCédric Le Goater };
59e3228ed9SCédric Le Goater 
60e3228ed9SCédric Le Goater struct aspeed_spi;
61e3228ed9SCédric Le Goater 
62e3228ed9SCédric Le Goater struct aspeed_spi_chip {
63e3228ed9SCédric Le Goater 	struct aspeed_spi	*aspi;
64e3228ed9SCédric Le Goater 	u32			 cs;
65e3228ed9SCédric Le Goater 	void __iomem		*ctl;
66e3228ed9SCédric Le Goater 	void __iomem		*ahb_base;
67e3228ed9SCédric Le Goater 	u32			 ahb_window_size;
68e3228ed9SCédric Le Goater 	u32			 ctl_val[ASPEED_SPI_MAX];
69e3228ed9SCédric Le Goater 	u32			 clk_freq;
70e3228ed9SCédric Le Goater };
71e3228ed9SCédric Le Goater 
72e3228ed9SCédric Le Goater struct aspeed_spi_data {
73e3228ed9SCédric Le Goater 	u32	ctl0;
74e3228ed9SCédric Le Goater 	u32	max_cs;
75e3228ed9SCédric Le Goater 	bool	hastype;
76e3228ed9SCédric Le Goater 	u32	mode_bits;
77e3228ed9SCédric Le Goater 	u32	we0;
78eeaec1eaSCédric Le Goater 	u32	timing;
79eeaec1eaSCédric Le Goater 	u32	hclk_mask;
80eeaec1eaSCédric Le Goater 	u32	hdiv_max;
81e3228ed9SCédric Le Goater 
82e3228ed9SCédric Le Goater 	u32 (*segment_start)(struct aspeed_spi *aspi, u32 reg);
83e3228ed9SCédric Le Goater 	u32 (*segment_end)(struct aspeed_spi *aspi, u32 reg);
84e3228ed9SCédric Le Goater 	u32 (*segment_reg)(struct aspeed_spi *aspi, u32 start, u32 end);
85eeaec1eaSCédric Le Goater 	int (*calibrate)(struct aspeed_spi_chip *chip, u32 hdiv,
86eeaec1eaSCédric Le Goater 			 const u8 *golden_buf, u8 *test_buf);
87e3228ed9SCédric Le Goater };
88e3228ed9SCédric Le Goater 
89e3228ed9SCédric Le Goater #define ASPEED_SPI_MAX_NUM_CS	5
90e3228ed9SCédric Le Goater 
91e3228ed9SCédric Le Goater struct aspeed_spi {
92e3228ed9SCédric Le Goater 	const struct aspeed_spi_data	*data;
93e3228ed9SCédric Le Goater 
94e3228ed9SCédric Le Goater 	void __iomem		*regs;
95e3228ed9SCédric Le Goater 	void __iomem		*ahb_base;
96e3228ed9SCédric Le Goater 	u32			 ahb_base_phy;
97e3228ed9SCédric Le Goater 	u32			 ahb_window_size;
98e3228ed9SCédric Le Goater 	struct device		*dev;
99e3228ed9SCédric Le Goater 
100e3228ed9SCédric Le Goater 	struct clk		*clk;
101e3228ed9SCédric Le Goater 	u32			 clk_freq;
102e3228ed9SCédric Le Goater 
103e3228ed9SCédric Le Goater 	struct aspeed_spi_chip	 chips[ASPEED_SPI_MAX_NUM_CS];
104e3228ed9SCédric Le Goater };
105e3228ed9SCédric Le Goater 
aspeed_spi_get_io_mode(const struct spi_mem_op * op)106e3228ed9SCédric Le Goater static u32 aspeed_spi_get_io_mode(const struct spi_mem_op *op)
107e3228ed9SCédric Le Goater {
108e3228ed9SCédric Le Goater 	switch (op->data.buswidth) {
109e3228ed9SCédric Le Goater 	case 1:
110e3228ed9SCédric Le Goater 		return CTRL_IO_SINGLE_DATA;
111e3228ed9SCédric Le Goater 	case 2:
112e3228ed9SCédric Le Goater 		return CTRL_IO_DUAL_DATA;
113e3228ed9SCédric Le Goater 	case 4:
114e3228ed9SCédric Le Goater 		return CTRL_IO_QUAD_DATA;
115e3228ed9SCédric Le Goater 	default:
116e3228ed9SCédric Le Goater 		return CTRL_IO_SINGLE_DATA;
117e3228ed9SCédric Le Goater 	}
118e3228ed9SCédric Le Goater }
119e3228ed9SCédric Le Goater 
aspeed_spi_set_io_mode(struct aspeed_spi_chip * chip,u32 io_mode)120e3228ed9SCédric Le Goater static void aspeed_spi_set_io_mode(struct aspeed_spi_chip *chip, u32 io_mode)
121e3228ed9SCédric Le Goater {
122e3228ed9SCédric Le Goater 	u32 ctl;
123e3228ed9SCédric Le Goater 
124e3228ed9SCédric Le Goater 	if (io_mode > 0) {
125e3228ed9SCédric Le Goater 		ctl = readl(chip->ctl) & ~CTRL_IO_MODE_MASK;
126e3228ed9SCédric Le Goater 		ctl |= io_mode;
127e3228ed9SCédric Le Goater 		writel(ctl, chip->ctl);
128e3228ed9SCédric Le Goater 	}
129e3228ed9SCédric Le Goater }
130e3228ed9SCédric Le Goater 
aspeed_spi_start_user(struct aspeed_spi_chip * chip)131e3228ed9SCédric Le Goater static void aspeed_spi_start_user(struct aspeed_spi_chip *chip)
132e3228ed9SCédric Le Goater {
133e3228ed9SCédric Le Goater 	u32 ctl = chip->ctl_val[ASPEED_SPI_BASE];
134e3228ed9SCédric Le Goater 
135e3228ed9SCédric Le Goater 	ctl |= CTRL_IO_MODE_USER | CTRL_CE_STOP_ACTIVE;
136e3228ed9SCédric Le Goater 	writel(ctl, chip->ctl);
137e3228ed9SCédric Le Goater 
138e3228ed9SCédric Le Goater 	ctl &= ~CTRL_CE_STOP_ACTIVE;
139e3228ed9SCédric Le Goater 	writel(ctl, chip->ctl);
140e3228ed9SCédric Le Goater }
141e3228ed9SCédric Le Goater 
aspeed_spi_stop_user(struct aspeed_spi_chip * chip)142e3228ed9SCédric Le Goater static void aspeed_spi_stop_user(struct aspeed_spi_chip *chip)
143e3228ed9SCédric Le Goater {
144e3228ed9SCédric Le Goater 	u32 ctl = chip->ctl_val[ASPEED_SPI_READ] |
145e3228ed9SCédric Le Goater 		CTRL_IO_MODE_USER | CTRL_CE_STOP_ACTIVE;
146e3228ed9SCédric Le Goater 
147e3228ed9SCédric Le Goater 	writel(ctl, chip->ctl);
148e3228ed9SCédric Le Goater 
149e3228ed9SCédric Le Goater 	/* Restore defaults */
150e3228ed9SCédric Le Goater 	writel(chip->ctl_val[ASPEED_SPI_READ], chip->ctl);
151e3228ed9SCédric Le Goater }
152e3228ed9SCédric Le Goater 
aspeed_spi_read_from_ahb(void * buf,void __iomem * src,size_t len)153e3228ed9SCédric Le Goater static int aspeed_spi_read_from_ahb(void *buf, void __iomem *src, size_t len)
154e3228ed9SCédric Le Goater {
155e3228ed9SCédric Le Goater 	size_t offset = 0;
156e3228ed9SCédric Le Goater 
157e3228ed9SCédric Le Goater 	if (IS_ALIGNED((uintptr_t)src, sizeof(uintptr_t)) &&
158e3228ed9SCédric Le Goater 	    IS_ALIGNED((uintptr_t)buf, sizeof(uintptr_t))) {
159e3228ed9SCédric Le Goater 		ioread32_rep(src, buf, len >> 2);
160e3228ed9SCédric Le Goater 		offset = len & ~0x3;
161e3228ed9SCédric Le Goater 		len -= offset;
162e3228ed9SCédric Le Goater 	}
163e3228ed9SCédric Le Goater 	ioread8_rep(src, (u8 *)buf + offset, len);
164e3228ed9SCédric Le Goater 	return 0;
165e3228ed9SCédric Le Goater }
166e3228ed9SCédric Le Goater 
aspeed_spi_write_to_ahb(void __iomem * dst,const void * buf,size_t len)167e3228ed9SCédric Le Goater static int aspeed_spi_write_to_ahb(void __iomem *dst, const void *buf, size_t len)
168e3228ed9SCédric Le Goater {
169e3228ed9SCédric Le Goater 	size_t offset = 0;
170e3228ed9SCédric Le Goater 
171e3228ed9SCédric Le Goater 	if (IS_ALIGNED((uintptr_t)dst, sizeof(uintptr_t)) &&
172e3228ed9SCédric Le Goater 	    IS_ALIGNED((uintptr_t)buf, sizeof(uintptr_t))) {
173e3228ed9SCédric Le Goater 		iowrite32_rep(dst, buf, len >> 2);
174e3228ed9SCédric Le Goater 		offset = len & ~0x3;
175e3228ed9SCédric Le Goater 		len -= offset;
176e3228ed9SCédric Le Goater 	}
177e3228ed9SCédric Le Goater 	iowrite8_rep(dst, (const u8 *)buf + offset, len);
178e3228ed9SCédric Le Goater 	return 0;
179e3228ed9SCédric Le Goater }
180e3228ed9SCédric Le Goater 
aspeed_spi_send_cmd_addr(struct aspeed_spi_chip * chip,u8 addr_nbytes,u64 offset,u32 opcode)181e3228ed9SCédric Le Goater static int aspeed_spi_send_cmd_addr(struct aspeed_spi_chip *chip, u8 addr_nbytes,
182e3228ed9SCédric Le Goater 				    u64 offset, u32 opcode)
183e3228ed9SCédric Le Goater {
184e3228ed9SCédric Le Goater 	__be32 temp;
185e3228ed9SCédric Le Goater 	u32 cmdaddr;
186e3228ed9SCédric Le Goater 
187e3228ed9SCédric Le Goater 	switch (addr_nbytes) {
188e3228ed9SCédric Le Goater 	case 3:
189e3228ed9SCédric Le Goater 		cmdaddr = offset & 0xFFFFFF;
190e3228ed9SCédric Le Goater 		cmdaddr |= opcode << 24;
191e3228ed9SCédric Le Goater 
192e3228ed9SCédric Le Goater 		temp = cpu_to_be32(cmdaddr);
193e3228ed9SCédric Le Goater 		aspeed_spi_write_to_ahb(chip->ahb_base, &temp, 4);
194e3228ed9SCédric Le Goater 		break;
195e3228ed9SCédric Le Goater 	case 4:
196e3228ed9SCédric Le Goater 		temp = cpu_to_be32(offset);
197e3228ed9SCédric Le Goater 		aspeed_spi_write_to_ahb(chip->ahb_base, &opcode, 1);
198e3228ed9SCédric Le Goater 		aspeed_spi_write_to_ahb(chip->ahb_base, &temp, 4);
199e3228ed9SCédric Le Goater 		break;
200e3228ed9SCédric Le Goater 	default:
201e3228ed9SCédric Le Goater 		WARN_ONCE(1, "Unexpected address width %u", addr_nbytes);
202e3228ed9SCédric Le Goater 		return -EOPNOTSUPP;
203e3228ed9SCédric Le Goater 	}
204e3228ed9SCédric Le Goater 	return 0;
205e3228ed9SCédric Le Goater }
206e3228ed9SCédric Le Goater 
aspeed_spi_read_reg(struct aspeed_spi_chip * chip,const struct spi_mem_op * op)207e3228ed9SCédric Le Goater static int aspeed_spi_read_reg(struct aspeed_spi_chip *chip,
208e3228ed9SCédric Le Goater 			       const struct spi_mem_op *op)
209e3228ed9SCédric Le Goater {
210e3228ed9SCédric Le Goater 	aspeed_spi_start_user(chip);
211e3228ed9SCédric Le Goater 	aspeed_spi_write_to_ahb(chip->ahb_base, &op->cmd.opcode, 1);
212e3228ed9SCédric Le Goater 	aspeed_spi_read_from_ahb(op->data.buf.in,
213e3228ed9SCédric Le Goater 				 chip->ahb_base, op->data.nbytes);
214e3228ed9SCédric Le Goater 	aspeed_spi_stop_user(chip);
215e3228ed9SCédric Le Goater 	return 0;
216e3228ed9SCédric Le Goater }
217e3228ed9SCédric Le Goater 
aspeed_spi_write_reg(struct aspeed_spi_chip * chip,const struct spi_mem_op * op)218e3228ed9SCédric Le Goater static int aspeed_spi_write_reg(struct aspeed_spi_chip *chip,
219e3228ed9SCédric Le Goater 				const struct spi_mem_op *op)
220e3228ed9SCédric Le Goater {
221e3228ed9SCédric Le Goater 	aspeed_spi_start_user(chip);
222e3228ed9SCédric Le Goater 	aspeed_spi_write_to_ahb(chip->ahb_base, &op->cmd.opcode, 1);
223e3228ed9SCédric Le Goater 	aspeed_spi_write_to_ahb(chip->ahb_base, op->data.buf.out,
224e3228ed9SCédric Le Goater 				op->data.nbytes);
225e3228ed9SCédric Le Goater 	aspeed_spi_stop_user(chip);
226e3228ed9SCédric Le Goater 	return 0;
227e3228ed9SCédric Le Goater }
228e3228ed9SCédric Le Goater 
aspeed_spi_read_user(struct aspeed_spi_chip * chip,const struct spi_mem_op * op,u64 offset,size_t len,void * buf)229e3228ed9SCédric Le Goater static ssize_t aspeed_spi_read_user(struct aspeed_spi_chip *chip,
230e3228ed9SCédric Le Goater 				    const struct spi_mem_op *op,
231e3228ed9SCédric Le Goater 				    u64 offset, size_t len, void *buf)
232e3228ed9SCédric Le Goater {
233e3228ed9SCédric Le Goater 	int io_mode = aspeed_spi_get_io_mode(op);
234e3228ed9SCédric Le Goater 	u8 dummy = 0xFF;
235e3228ed9SCédric Le Goater 	int i;
236e3228ed9SCédric Le Goater 	int ret;
237e3228ed9SCédric Le Goater 
238e3228ed9SCédric Le Goater 	aspeed_spi_start_user(chip);
239e3228ed9SCédric Le Goater 
240e3228ed9SCédric Le Goater 	ret = aspeed_spi_send_cmd_addr(chip, op->addr.nbytes, offset, op->cmd.opcode);
241e3228ed9SCédric Le Goater 	if (ret < 0)
242e3228ed9SCédric Le Goater 		return ret;
243e3228ed9SCédric Le Goater 
244e3228ed9SCédric Le Goater 	if (op->dummy.buswidth && op->dummy.nbytes) {
245e3228ed9SCédric Le Goater 		for (i = 0; i < op->dummy.nbytes / op->dummy.buswidth; i++)
246e3228ed9SCédric Le Goater 			aspeed_spi_write_to_ahb(chip->ahb_base, &dummy,	sizeof(dummy));
247e3228ed9SCédric Le Goater 	}
248e3228ed9SCédric Le Goater 
249e3228ed9SCédric Le Goater 	aspeed_spi_set_io_mode(chip, io_mode);
250e3228ed9SCédric Le Goater 
251e3228ed9SCédric Le Goater 	aspeed_spi_read_from_ahb(buf, chip->ahb_base, len);
252e3228ed9SCédric Le Goater 	aspeed_spi_stop_user(chip);
253e3228ed9SCédric Le Goater 	return 0;
254e3228ed9SCédric Le Goater }
255e3228ed9SCédric Le Goater 
aspeed_spi_write_user(struct aspeed_spi_chip * chip,const struct spi_mem_op * op)256e3228ed9SCédric Le Goater static ssize_t aspeed_spi_write_user(struct aspeed_spi_chip *chip,
257e3228ed9SCédric Le Goater 				     const struct spi_mem_op *op)
258e3228ed9SCédric Le Goater {
259e3228ed9SCédric Le Goater 	int ret;
260e3228ed9SCédric Le Goater 
261e3228ed9SCédric Le Goater 	aspeed_spi_start_user(chip);
262e3228ed9SCédric Le Goater 	ret = aspeed_spi_send_cmd_addr(chip, op->addr.nbytes, op->addr.val, op->cmd.opcode);
263e3228ed9SCédric Le Goater 	if (ret < 0)
264e3228ed9SCédric Le Goater 		return ret;
265e3228ed9SCédric Le Goater 	aspeed_spi_write_to_ahb(chip->ahb_base, op->data.buf.out, op->data.nbytes);
266e3228ed9SCédric Le Goater 	aspeed_spi_stop_user(chip);
267e3228ed9SCédric Le Goater 	return 0;
268e3228ed9SCédric Le Goater }
269e3228ed9SCédric Le Goater 
270e3228ed9SCédric Le Goater /* support for 1-1-1, 1-1-2 or 1-1-4 */
aspeed_spi_supports_op(struct spi_mem * mem,const struct spi_mem_op * op)271e3228ed9SCédric Le Goater static bool aspeed_spi_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
272e3228ed9SCédric Le Goater {
273e3228ed9SCédric Le Goater 	if (op->cmd.buswidth > 1)
274e3228ed9SCédric Le Goater 		return false;
275e3228ed9SCédric Le Goater 
276e3228ed9SCédric Le Goater 	if (op->addr.nbytes != 0) {
277e3228ed9SCédric Le Goater 		if (op->addr.buswidth > 1)
278e3228ed9SCédric Le Goater 			return false;
279e3228ed9SCédric Le Goater 		if (op->addr.nbytes < 3 || op->addr.nbytes > 4)
280e3228ed9SCédric Le Goater 			return false;
281e3228ed9SCédric Le Goater 	}
282e3228ed9SCédric Le Goater 
283e3228ed9SCédric Le Goater 	if (op->dummy.nbytes != 0) {
284e3228ed9SCédric Le Goater 		if (op->dummy.buswidth > 1 || op->dummy.nbytes > 7)
285e3228ed9SCédric Le Goater 			return false;
286e3228ed9SCédric Le Goater 	}
287e3228ed9SCédric Le Goater 
288e3228ed9SCédric Le Goater 	if (op->data.nbytes != 0 && op->data.buswidth > 4)
289e3228ed9SCédric Le Goater 		return false;
290e3228ed9SCédric Le Goater 
291e3228ed9SCédric Le Goater 	return spi_mem_default_supports_op(mem, op);
292e3228ed9SCédric Le Goater }
293e3228ed9SCédric Le Goater 
29453526ab2SCédric Le Goater static const struct aspeed_spi_data ast2400_spi_data;
29553526ab2SCédric Le Goater 
do_aspeed_spi_exec_op(struct spi_mem * mem,const struct spi_mem_op * op)296e3228ed9SCédric Le Goater static int do_aspeed_spi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
297e3228ed9SCédric Le Goater {
29821ac58f5SYang Yingliang 	struct aspeed_spi *aspi = spi_controller_get_devdata(mem->spi->controller);
2999e264f3fSAmit Kumar Mahapatra via Alsa-devel 	struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(mem->spi, 0)];
300e3228ed9SCédric Le Goater 	u32 addr_mode, addr_mode_backup;
301e3228ed9SCédric Le Goater 	u32 ctl_val;
302e3228ed9SCédric Le Goater 	int ret = 0;
303e3228ed9SCédric Le Goater 
304e3228ed9SCédric Le Goater 	dev_dbg(aspi->dev,
305e3228ed9SCédric Le Goater 		"CE%d %s OP %#x mode:%d.%d.%d.%d naddr:%#x ndummies:%#x len:%#x",
306e3228ed9SCédric Le Goater 		chip->cs, op->data.dir == SPI_MEM_DATA_IN ? "read" : "write",
307e3228ed9SCédric Le Goater 		op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
308e3228ed9SCédric Le Goater 		op->dummy.buswidth, op->data.buswidth,
309e3228ed9SCédric Le Goater 		op->addr.nbytes, op->dummy.nbytes, op->data.nbytes);
310e3228ed9SCédric Le Goater 
311e3228ed9SCédric Le Goater 	addr_mode = readl(aspi->regs + CE_CTRL_REG);
312e3228ed9SCédric Le Goater 	addr_mode_backup = addr_mode;
313e3228ed9SCédric Le Goater 
314e3228ed9SCédric Le Goater 	ctl_val = chip->ctl_val[ASPEED_SPI_BASE];
315e3228ed9SCédric Le Goater 	ctl_val &= ~CTRL_IO_CMD_MASK;
316e3228ed9SCédric Le Goater 
317e3228ed9SCédric Le Goater 	ctl_val |= op->cmd.opcode << CTRL_COMMAND_SHIFT;
318e3228ed9SCédric Le Goater 
319e3228ed9SCédric Le Goater 	/* 4BYTE address mode */
320e3228ed9SCédric Le Goater 	if (op->addr.nbytes) {
321e3228ed9SCédric Le Goater 		if (op->addr.nbytes == 4)
322e3228ed9SCédric Le Goater 			addr_mode |= (0x11 << chip->cs);
323e3228ed9SCédric Le Goater 		else
324e3228ed9SCédric Le Goater 			addr_mode &= ~(0x11 << chip->cs);
32553526ab2SCédric Le Goater 
32653526ab2SCédric Le Goater 		if (op->addr.nbytes == 4 && chip->aspi->data == &ast2400_spi_data)
32753526ab2SCédric Le Goater 			ctl_val |= CTRL_IO_ADDRESS_4B;
328e3228ed9SCédric Le Goater 	}
329e3228ed9SCédric Le Goater 
330e3228ed9SCédric Le Goater 	if (op->dummy.nbytes)
331e3228ed9SCédric Le Goater 		ctl_val |= CTRL_IO_DUMMY_SET(op->dummy.nbytes / op->dummy.buswidth);
332e3228ed9SCédric Le Goater 
333e3228ed9SCédric Le Goater 	if (op->data.nbytes)
334e3228ed9SCédric Le Goater 		ctl_val |= aspeed_spi_get_io_mode(op);
335e3228ed9SCédric Le Goater 
336e3228ed9SCédric Le Goater 	if (op->data.dir == SPI_MEM_DATA_OUT)
337e3228ed9SCédric Le Goater 		ctl_val |= CTRL_IO_MODE_WRITE;
338e3228ed9SCédric Le Goater 	else
339e3228ed9SCédric Le Goater 		ctl_val |= CTRL_IO_MODE_READ;
340e3228ed9SCédric Le Goater 
341e3228ed9SCédric Le Goater 	if (addr_mode != addr_mode_backup)
342e3228ed9SCédric Le Goater 		writel(addr_mode, aspi->regs + CE_CTRL_REG);
343e3228ed9SCédric Le Goater 	writel(ctl_val, chip->ctl);
344e3228ed9SCédric Le Goater 
345e3228ed9SCédric Le Goater 	if (op->data.dir == SPI_MEM_DATA_IN) {
346e3228ed9SCédric Le Goater 		if (!op->addr.nbytes)
347e3228ed9SCédric Le Goater 			ret = aspeed_spi_read_reg(chip, op);
348e3228ed9SCédric Le Goater 		else
349e3228ed9SCédric Le Goater 			ret = aspeed_spi_read_user(chip, op, op->addr.val,
350e3228ed9SCédric Le Goater 						   op->data.nbytes, op->data.buf.in);
351e3228ed9SCédric Le Goater 	} else {
352e3228ed9SCédric Le Goater 		if (!op->addr.nbytes)
353e3228ed9SCédric Le Goater 			ret = aspeed_spi_write_reg(chip, op);
354e3228ed9SCédric Le Goater 		else
355e3228ed9SCédric Le Goater 			ret = aspeed_spi_write_user(chip, op);
356e3228ed9SCédric Le Goater 	}
357e3228ed9SCédric Le Goater 
358e3228ed9SCédric Le Goater 	/* Restore defaults */
359e3228ed9SCédric Le Goater 	if (addr_mode != addr_mode_backup)
360e3228ed9SCédric Le Goater 		writel(addr_mode_backup, aspi->regs + CE_CTRL_REG);
361e3228ed9SCédric Le Goater 	writel(chip->ctl_val[ASPEED_SPI_READ], chip->ctl);
362e3228ed9SCédric Le Goater 	return ret;
363e3228ed9SCédric Le Goater }
364e3228ed9SCédric Le Goater 
aspeed_spi_exec_op(struct spi_mem * mem,const struct spi_mem_op * op)365e3228ed9SCédric Le Goater static int aspeed_spi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
366e3228ed9SCédric Le Goater {
367e3228ed9SCédric Le Goater 	int ret;
368e3228ed9SCédric Le Goater 
369e3228ed9SCédric Le Goater 	ret = do_aspeed_spi_exec_op(mem, op);
370e3228ed9SCédric Le Goater 	if (ret)
371e3228ed9SCédric Le Goater 		dev_err(&mem->spi->dev, "operation failed: %d\n", ret);
372e3228ed9SCédric Le Goater 	return ret;
373e3228ed9SCédric Le Goater }
374e3228ed9SCédric Le Goater 
aspeed_spi_get_name(struct spi_mem * mem)375e3228ed9SCédric Le Goater static const char *aspeed_spi_get_name(struct spi_mem *mem)
376e3228ed9SCédric Le Goater {
37721ac58f5SYang Yingliang 	struct aspeed_spi *aspi = spi_controller_get_devdata(mem->spi->controller);
378e3228ed9SCédric Le Goater 	struct device *dev = aspi->dev;
379e3228ed9SCédric Le Goater 
3809e264f3fSAmit Kumar Mahapatra via Alsa-devel 	return devm_kasprintf(dev, GFP_KERNEL, "%s.%d", dev_name(dev),
3819e264f3fSAmit Kumar Mahapatra via Alsa-devel 			      spi_get_chipselect(mem->spi, 0));
382e3228ed9SCédric Le Goater }
383e3228ed9SCédric Le Goater 
384e3228ed9SCédric Le Goater struct aspeed_spi_window {
385e3228ed9SCédric Le Goater 	u32 cs;
386e3228ed9SCédric Le Goater 	u32 offset;
387e3228ed9SCédric Le Goater 	u32 size;
388e3228ed9SCédric Le Goater };
389e3228ed9SCédric Le Goater 
aspeed_spi_get_windows(struct aspeed_spi * aspi,struct aspeed_spi_window windows[ASPEED_SPI_MAX_NUM_CS])390e3228ed9SCédric Le Goater static void aspeed_spi_get_windows(struct aspeed_spi *aspi,
391e3228ed9SCédric Le Goater 				   struct aspeed_spi_window windows[ASPEED_SPI_MAX_NUM_CS])
392e3228ed9SCédric Le Goater {
393e3228ed9SCédric Le Goater 	const struct aspeed_spi_data *data = aspi->data;
394e3228ed9SCédric Le Goater 	u32 reg_val;
395e3228ed9SCédric Le Goater 	u32 cs;
396e3228ed9SCédric Le Goater 
397e3228ed9SCédric Le Goater 	for (cs = 0; cs < aspi->data->max_cs; cs++) {
398e3228ed9SCédric Le Goater 		reg_val = readl(aspi->regs + CE0_SEGMENT_ADDR_REG + cs * 4);
399e3228ed9SCédric Le Goater 		windows[cs].cs = cs;
400e3228ed9SCédric Le Goater 		windows[cs].size = data->segment_end(aspi, reg_val) -
401e3228ed9SCédric Le Goater 			data->segment_start(aspi, reg_val);
402f8aa6c89SCédric Le Goater 		windows[cs].offset = data->segment_start(aspi, reg_val) - aspi->ahb_base_phy;
403e3228ed9SCédric Le Goater 		dev_vdbg(aspi->dev, "CE%d offset=0x%.8x size=0x%x\n", cs,
404e3228ed9SCédric Le Goater 			 windows[cs].offset, windows[cs].size);
405e3228ed9SCédric Le Goater 	}
406e3228ed9SCédric Le Goater }
407e3228ed9SCédric Le Goater 
408e3228ed9SCédric Le Goater /*
409e3228ed9SCédric Le Goater  * On the AST2600, some CE windows are closed by default at reset but
410e3228ed9SCédric Le Goater  * U-Boot should open all.
411e3228ed9SCédric Le Goater  */
aspeed_spi_chip_set_default_window(struct aspeed_spi_chip * chip)412e3228ed9SCédric Le Goater static int aspeed_spi_chip_set_default_window(struct aspeed_spi_chip *chip)
413e3228ed9SCédric Le Goater {
414e3228ed9SCédric Le Goater 	struct aspeed_spi *aspi = chip->aspi;
415e3228ed9SCédric Le Goater 	struct aspeed_spi_window windows[ASPEED_SPI_MAX_NUM_CS] = { 0 };
416e3228ed9SCédric Le Goater 	struct aspeed_spi_window *win = &windows[chip->cs];
417e3228ed9SCédric Le Goater 
41853526ab2SCédric Le Goater 	/* No segment registers for the AST2400 SPI controller */
41953526ab2SCédric Le Goater 	if (aspi->data == &ast2400_spi_data) {
42053526ab2SCédric Le Goater 		win->offset = 0;
42153526ab2SCédric Le Goater 		win->size = aspi->ahb_window_size;
42253526ab2SCédric Le Goater 	} else {
423e3228ed9SCédric Le Goater 		aspeed_spi_get_windows(aspi, windows);
42453526ab2SCédric Le Goater 	}
425e3228ed9SCédric Le Goater 
426e3228ed9SCédric Le Goater 	chip->ahb_base = aspi->ahb_base + win->offset;
427e3228ed9SCédric Le Goater 	chip->ahb_window_size = win->size;
428e3228ed9SCédric Le Goater 
429e3228ed9SCédric Le Goater 	dev_dbg(aspi->dev, "CE%d default window [ 0x%.8x - 0x%.8x ] %dMB",
430e3228ed9SCédric Le Goater 		chip->cs, aspi->ahb_base_phy + win->offset,
431e3228ed9SCédric Le Goater 		aspi->ahb_base_phy + win->offset + win->size - 1,
432e3228ed9SCédric Le Goater 		win->size >> 20);
433e3228ed9SCédric Le Goater 
434e3228ed9SCédric Le Goater 	return chip->ahb_window_size ? 0 : -1;
435e3228ed9SCédric Le Goater }
436e3228ed9SCédric Le Goater 
aspeed_spi_set_window(struct aspeed_spi * aspi,const struct aspeed_spi_window * win)437bb084f94SCédric Le Goater static int aspeed_spi_set_window(struct aspeed_spi *aspi,
438bb084f94SCédric Le Goater 				 const struct aspeed_spi_window *win)
439bb084f94SCédric Le Goater {
440bb084f94SCédric Le Goater 	u32 start = aspi->ahb_base_phy + win->offset;
441bb084f94SCédric Le Goater 	u32 end = start + win->size;
442bb084f94SCédric Le Goater 	void __iomem *seg_reg = aspi->regs + CE0_SEGMENT_ADDR_REG + win->cs * 4;
443bb084f94SCédric Le Goater 	u32 seg_val_backup = readl(seg_reg);
444bb084f94SCédric Le Goater 	u32 seg_val = aspi->data->segment_reg(aspi, start, end);
445bb084f94SCédric Le Goater 
446bb084f94SCédric Le Goater 	if (seg_val == seg_val_backup)
447bb084f94SCédric Le Goater 		return 0;
448bb084f94SCédric Le Goater 
449bb084f94SCédric Le Goater 	writel(seg_val, seg_reg);
450bb084f94SCédric Le Goater 
451bb084f94SCédric Le Goater 	/*
452bb084f94SCédric Le Goater 	 * Restore initial value if something goes wrong else we could
453bb084f94SCédric Le Goater 	 * loose access to the chip.
454bb084f94SCédric Le Goater 	 */
455bb084f94SCédric Le Goater 	if (seg_val != readl(seg_reg)) {
456bb084f94SCédric Le Goater 		dev_err(aspi->dev, "CE%d invalid window [ 0x%.8x - 0x%.8x ] %dMB",
457bb084f94SCédric Le Goater 			win->cs, start, end - 1, win->size >> 20);
458bb084f94SCédric Le Goater 		writel(seg_val_backup, seg_reg);
459bb084f94SCédric Le Goater 		return -EIO;
460bb084f94SCédric Le Goater 	}
461bb084f94SCédric Le Goater 
462bb084f94SCédric Le Goater 	if (win->size)
463bb084f94SCédric Le Goater 		dev_dbg(aspi->dev, "CE%d new window [ 0x%.8x - 0x%.8x ] %dMB",
464bb084f94SCédric Le Goater 			win->cs, start, end - 1,  win->size >> 20);
465bb084f94SCédric Le Goater 	else
466bb084f94SCédric Le Goater 		dev_dbg(aspi->dev, "CE%d window closed", win->cs);
467bb084f94SCédric Le Goater 
468bb084f94SCédric Le Goater 	return 0;
469bb084f94SCédric Le Goater }
470bb084f94SCédric Le Goater 
471bb084f94SCédric Le Goater /*
472bb084f94SCédric Le Goater  * Yet to be done when possible :
473bb084f94SCédric Le Goater  * - Align mappings on flash size (we don't have the info)
474bb084f94SCédric Le Goater  * - ioremap each window, not strictly necessary since the overall window
475bb084f94SCédric Le Goater  *   is correct.
476bb084f94SCédric Le Goater  */
4775785eedeSCédric Le Goater static const struct aspeed_spi_data ast2500_spi_data;
47873ae97e3SPotin Lai static const struct aspeed_spi_data ast2600_spi_data;
47973ae97e3SPotin Lai static const struct aspeed_spi_data ast2600_fmc_data;
4805785eedeSCédric Le Goater 
aspeed_spi_chip_adjust_window(struct aspeed_spi_chip * chip,u32 local_offset,u32 size)481bb084f94SCédric Le Goater static int aspeed_spi_chip_adjust_window(struct aspeed_spi_chip *chip,
482bb084f94SCédric Le Goater 					 u32 local_offset, u32 size)
483bb084f94SCédric Le Goater {
484bb084f94SCédric Le Goater 	struct aspeed_spi *aspi = chip->aspi;
485bb084f94SCédric Le Goater 	struct aspeed_spi_window windows[ASPEED_SPI_MAX_NUM_CS] = { 0 };
486bb084f94SCédric Le Goater 	struct aspeed_spi_window *win = &windows[chip->cs];
487bb084f94SCédric Le Goater 	int ret;
488bb084f94SCédric Le Goater 
48953526ab2SCédric Le Goater 	/* No segment registers for the AST2400 SPI controller */
49053526ab2SCédric Le Goater 	if (aspi->data == &ast2400_spi_data)
49153526ab2SCédric Le Goater 		return 0;
49253526ab2SCédric Le Goater 
4935785eedeSCédric Le Goater 	/*
4945785eedeSCédric Le Goater 	 * Due to an HW issue on the AST2500 SPI controller, the CE0
4955785eedeSCédric Le Goater 	 * window size should be smaller than the maximum 128MB.
4965785eedeSCédric Le Goater 	 */
4975785eedeSCédric Le Goater 	if (aspi->data == &ast2500_spi_data && chip->cs == 0 && size == SZ_128M) {
4985785eedeSCédric Le Goater 		size = 120 << 20;
4995785eedeSCédric Le Goater 		dev_info(aspi->dev, "CE%d window resized to %dMB (AST2500 HW quirk)",
5005785eedeSCédric Le Goater 			 chip->cs, size >> 20);
5015785eedeSCédric Le Goater 	}
5025785eedeSCédric Le Goater 
50373ae97e3SPotin Lai 	/*
50473ae97e3SPotin Lai 	 * The decoding size of AST2600 SPI controller should set at
50573ae97e3SPotin Lai 	 * least 2MB.
50673ae97e3SPotin Lai 	 */
50773ae97e3SPotin Lai 	if ((aspi->data == &ast2600_spi_data || aspi->data == &ast2600_fmc_data) &&
50873ae97e3SPotin Lai 	    size < SZ_2M) {
50973ae97e3SPotin Lai 		size = SZ_2M;
51073ae97e3SPotin Lai 		dev_info(aspi->dev, "CE%d window resized to %dMB (AST2600 Decoding)",
51173ae97e3SPotin Lai 			 chip->cs, size >> 20);
51273ae97e3SPotin Lai 	}
51373ae97e3SPotin Lai 
514bb084f94SCédric Le Goater 	aspeed_spi_get_windows(aspi, windows);
515bb084f94SCédric Le Goater 
516bb084f94SCédric Le Goater 	/* Adjust this chip window */
517bb084f94SCédric Le Goater 	win->offset += local_offset;
518bb084f94SCédric Le Goater 	win->size = size;
519bb084f94SCédric Le Goater 
520bb084f94SCédric Le Goater 	if (win->offset + win->size > aspi->ahb_window_size) {
521bb084f94SCédric Le Goater 		win->size = aspi->ahb_window_size - win->offset;
522bb084f94SCédric Le Goater 		dev_warn(aspi->dev, "CE%d window resized to %dMB", chip->cs, win->size >> 20);
523bb084f94SCédric Le Goater 	}
524bb084f94SCédric Le Goater 
525bb084f94SCédric Le Goater 	ret = aspeed_spi_set_window(aspi, win);
526bb084f94SCédric Le Goater 	if (ret)
527bb084f94SCédric Le Goater 		return ret;
528bb084f94SCédric Le Goater 
529bb084f94SCédric Le Goater 	/* Update chip mapping info */
530bb084f94SCédric Le Goater 	chip->ahb_base = aspi->ahb_base + win->offset;
531bb084f94SCédric Le Goater 	chip->ahb_window_size = win->size;
532bb084f94SCédric Le Goater 
533bb084f94SCédric Le Goater 	/*
534bb084f94SCédric Le Goater 	 * Also adjust next chip window to make sure that it does not
535bb084f94SCédric Le Goater 	 * overlap with the current window.
536bb084f94SCédric Le Goater 	 */
537bb084f94SCédric Le Goater 	if (chip->cs < aspi->data->max_cs - 1) {
538bb084f94SCédric Le Goater 		struct aspeed_spi_window *next = &windows[chip->cs + 1];
539bb084f94SCédric Le Goater 
540bb084f94SCédric Le Goater 		/* Change offset and size to keep the same end address */
541bb084f94SCédric Le Goater 		if ((next->offset + next->size) > (win->offset + win->size))
542bb084f94SCédric Le Goater 			next->size = (next->offset + next->size) - (win->offset + win->size);
543bb084f94SCédric Le Goater 		else
544bb084f94SCédric Le Goater 			next->size = 0;
545bb084f94SCédric Le Goater 		next->offset = win->offset + win->size;
546bb084f94SCédric Le Goater 
547bb084f94SCédric Le Goater 		aspeed_spi_set_window(aspi, next);
548bb084f94SCédric Le Goater 	}
549bb084f94SCédric Le Goater 	return 0;
550bb084f94SCédric Le Goater }
551bb084f94SCédric Le Goater 
552eeaec1eaSCédric Le Goater static int aspeed_spi_do_calibration(struct aspeed_spi_chip *chip);
553eeaec1eaSCédric Le Goater 
aspeed_spi_dirmap_create(struct spi_mem_dirmap_desc * desc)5549da06d7bSCédric Le Goater static int aspeed_spi_dirmap_create(struct spi_mem_dirmap_desc *desc)
5559da06d7bSCédric Le Goater {
55621ac58f5SYang Yingliang 	struct aspeed_spi *aspi = spi_controller_get_devdata(desc->mem->spi->controller);
5579e264f3fSAmit Kumar Mahapatra via Alsa-devel 	struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(desc->mem->spi, 0)];
5589da06d7bSCédric Le Goater 	struct spi_mem_op *op = &desc->info.op_tmpl;
5599da06d7bSCédric Le Goater 	u32 ctl_val;
5609da06d7bSCédric Le Goater 	int ret = 0;
5619da06d7bSCédric Le Goater 
5628988ba7dSCédric Le Goater 	dev_dbg(aspi->dev,
5638988ba7dSCédric Le Goater 		"CE%d %s dirmap [ 0x%.8llx - 0x%.8llx ] OP %#x mode:%d.%d.%d.%d naddr:%#x ndummies:%#x\n",
5648988ba7dSCédric Le Goater 		chip->cs, op->data.dir == SPI_MEM_DATA_IN ? "read" : "write",
5658988ba7dSCédric Le Goater 		desc->info.offset, desc->info.offset + desc->info.length,
5668988ba7dSCédric Le Goater 		op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
5678988ba7dSCédric Le Goater 		op->dummy.buswidth, op->data.buswidth,
5688988ba7dSCédric Le Goater 		op->addr.nbytes, op->dummy.nbytes);
5698988ba7dSCédric Le Goater 
5709da06d7bSCédric Le Goater 	chip->clk_freq = desc->mem->spi->max_speed_hz;
5719da06d7bSCédric Le Goater 
5729da06d7bSCédric Le Goater 	/* Only for reads */
5739da06d7bSCédric Le Goater 	if (op->data.dir != SPI_MEM_DATA_IN)
5749da06d7bSCédric Le Goater 		return -EOPNOTSUPP;
5759da06d7bSCédric Le Goater 
576bb084f94SCédric Le Goater 	aspeed_spi_chip_adjust_window(chip, desc->info.offset, desc->info.length);
577bb084f94SCédric Le Goater 
5789da06d7bSCédric Le Goater 	if (desc->info.length > chip->ahb_window_size)
5799da06d7bSCédric Le Goater 		dev_warn(aspi->dev, "CE%d window (%dMB) too small for mapping",
5809da06d7bSCédric Le Goater 			 chip->cs, chip->ahb_window_size >> 20);
5819da06d7bSCédric Le Goater 
5829da06d7bSCédric Le Goater 	/* Define the default IO read settings */
5839da06d7bSCédric Le Goater 	ctl_val = readl(chip->ctl) & ~CTRL_IO_CMD_MASK;
5849da06d7bSCédric Le Goater 	ctl_val |= aspeed_spi_get_io_mode(op) |
5859da06d7bSCédric Le Goater 		op->cmd.opcode << CTRL_COMMAND_SHIFT |
5869da06d7bSCédric Le Goater 		CTRL_IO_MODE_READ;
5879da06d7bSCédric Le Goater 
58830554a1fSCédric Le Goater 	if (op->dummy.nbytes)
58930554a1fSCédric Le Goater 		ctl_val |= CTRL_IO_DUMMY_SET(op->dummy.nbytes / op->dummy.buswidth);
59030554a1fSCédric Le Goater 
5919da06d7bSCédric Le Goater 	/* Tune 4BYTE address mode */
5929da06d7bSCédric Le Goater 	if (op->addr.nbytes) {
5939da06d7bSCédric Le Goater 		u32 addr_mode = readl(aspi->regs + CE_CTRL_REG);
5949da06d7bSCédric Le Goater 
5959da06d7bSCédric Le Goater 		if (op->addr.nbytes == 4)
5969da06d7bSCédric Le Goater 			addr_mode |= (0x11 << chip->cs);
5979da06d7bSCédric Le Goater 		else
5989da06d7bSCédric Le Goater 			addr_mode &= ~(0x11 << chip->cs);
5999da06d7bSCédric Le Goater 		writel(addr_mode, aspi->regs + CE_CTRL_REG);
60053526ab2SCédric Le Goater 
60153526ab2SCédric Le Goater 		/* AST2400 SPI controller sets 4BYTE address mode in
60253526ab2SCédric Le Goater 		 * CE0 Control Register
60353526ab2SCédric Le Goater 		 */
60453526ab2SCédric Le Goater 		if (op->addr.nbytes == 4 && chip->aspi->data == &ast2400_spi_data)
60553526ab2SCédric Le Goater 			ctl_val |= CTRL_IO_ADDRESS_4B;
6069da06d7bSCédric Le Goater 	}
6079da06d7bSCédric Le Goater 
6089da06d7bSCédric Le Goater 	/* READ mode is the controller default setting */
6099da06d7bSCédric Le Goater 	chip->ctl_val[ASPEED_SPI_READ] = ctl_val;
6109da06d7bSCédric Le Goater 	writel(chip->ctl_val[ASPEED_SPI_READ], chip->ctl);
6119da06d7bSCédric Le Goater 
612eeaec1eaSCédric Le Goater 	ret = aspeed_spi_do_calibration(chip);
613eeaec1eaSCédric Le Goater 
6149da06d7bSCédric Le Goater 	dev_info(aspi->dev, "CE%d read buswidth:%d [0x%08x]\n",
6159da06d7bSCédric Le Goater 		 chip->cs, op->data.buswidth, chip->ctl_val[ASPEED_SPI_READ]);
6169da06d7bSCédric Le Goater 
6179da06d7bSCédric Le Goater 	return ret;
6189da06d7bSCédric Le Goater }
6199da06d7bSCédric Le Goater 
aspeed_spi_dirmap_read(struct spi_mem_dirmap_desc * desc,u64 offset,size_t len,void * buf)6209da06d7bSCédric Le Goater static ssize_t aspeed_spi_dirmap_read(struct spi_mem_dirmap_desc *desc,
6219da06d7bSCédric Le Goater 				      u64 offset, size_t len, void *buf)
6229da06d7bSCédric Le Goater {
62321ac58f5SYang Yingliang 	struct aspeed_spi *aspi = spi_controller_get_devdata(desc->mem->spi->controller);
6249e264f3fSAmit Kumar Mahapatra via Alsa-devel 	struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(desc->mem->spi, 0)];
6259da06d7bSCédric Le Goater 
6269da06d7bSCédric Le Goater 	/* Switch to USER command mode if mapping window is too small */
6279da06d7bSCédric Le Goater 	if (chip->ahb_window_size < offset + len) {
6289da06d7bSCédric Le Goater 		int ret;
6299da06d7bSCédric Le Goater 
6309da06d7bSCédric Le Goater 		ret = aspeed_spi_read_user(chip, &desc->info.op_tmpl, offset, len, buf);
6319da06d7bSCédric Le Goater 		if (ret < 0)
6329da06d7bSCédric Le Goater 			return ret;
6339da06d7bSCédric Le Goater 	} else {
6349da06d7bSCédric Le Goater 		memcpy_fromio(buf, chip->ahb_base + offset, len);
6359da06d7bSCédric Le Goater 	}
6369da06d7bSCédric Le Goater 
6379da06d7bSCédric Le Goater 	return len;
6389da06d7bSCédric Le Goater }
6399da06d7bSCédric Le Goater 
640e3228ed9SCédric Le Goater static const struct spi_controller_mem_ops aspeed_spi_mem_ops = {
641e3228ed9SCédric Le Goater 	.supports_op = aspeed_spi_supports_op,
642e3228ed9SCédric Le Goater 	.exec_op = aspeed_spi_exec_op,
643e3228ed9SCédric Le Goater 	.get_name = aspeed_spi_get_name,
6449da06d7bSCédric Le Goater 	.dirmap_create = aspeed_spi_dirmap_create,
6459da06d7bSCédric Le Goater 	.dirmap_read = aspeed_spi_dirmap_read,
646e3228ed9SCédric Le Goater };
647e3228ed9SCédric Le Goater 
aspeed_spi_chip_set_type(struct aspeed_spi * aspi,unsigned int cs,int type)648e3228ed9SCédric Le Goater static void aspeed_spi_chip_set_type(struct aspeed_spi *aspi, unsigned int cs, int type)
649e3228ed9SCédric Le Goater {
650e3228ed9SCédric Le Goater 	u32 reg;
651e3228ed9SCédric Le Goater 
652e3228ed9SCédric Le Goater 	reg = readl(aspi->regs + CONFIG_REG);
653e3228ed9SCédric Le Goater 	reg &= ~(0x3 << (cs * 2));
654e3228ed9SCédric Le Goater 	reg |= type << (cs * 2);
655e3228ed9SCédric Le Goater 	writel(reg, aspi->regs + CONFIG_REG);
656e3228ed9SCédric Le Goater }
657e3228ed9SCédric Le Goater 
aspeed_spi_chip_enable(struct aspeed_spi * aspi,unsigned int cs,bool enable)658e3228ed9SCédric Le Goater static void aspeed_spi_chip_enable(struct aspeed_spi *aspi, unsigned int cs, bool enable)
659e3228ed9SCédric Le Goater {
660e3228ed9SCédric Le Goater 	u32 we_bit = BIT(aspi->data->we0 + cs);
661e3228ed9SCédric Le Goater 	u32 reg = readl(aspi->regs + CONFIG_REG);
662e3228ed9SCédric Le Goater 
663e3228ed9SCédric Le Goater 	if (enable)
664e3228ed9SCédric Le Goater 		reg |= we_bit;
665e3228ed9SCédric Le Goater 	else
666e3228ed9SCédric Le Goater 		reg &= ~we_bit;
667e3228ed9SCédric Le Goater 	writel(reg, aspi->regs + CONFIG_REG);
668e3228ed9SCédric Le Goater }
669e3228ed9SCédric Le Goater 
aspeed_spi_setup(struct spi_device * spi)670e3228ed9SCédric Le Goater static int aspeed_spi_setup(struct spi_device *spi)
671e3228ed9SCédric Le Goater {
67221ac58f5SYang Yingliang 	struct aspeed_spi *aspi = spi_controller_get_devdata(spi->controller);
673e3228ed9SCédric Le Goater 	const struct aspeed_spi_data *data = aspi->data;
6749e264f3fSAmit Kumar Mahapatra via Alsa-devel 	unsigned int cs = spi_get_chipselect(spi, 0);
675e3228ed9SCédric Le Goater 	struct aspeed_spi_chip *chip = &aspi->chips[cs];
676e3228ed9SCédric Le Goater 
677e3228ed9SCédric Le Goater 	chip->aspi = aspi;
678e3228ed9SCédric Le Goater 	chip->cs = cs;
679e3228ed9SCédric Le Goater 	chip->ctl = aspi->regs + data->ctl0 + cs * 4;
680e3228ed9SCédric Le Goater 
681e3228ed9SCédric Le Goater 	/* The driver only supports SPI type flash */
682e3228ed9SCédric Le Goater 	if (data->hastype)
683e3228ed9SCédric Le Goater 		aspeed_spi_chip_set_type(aspi, cs, CONFIG_TYPE_SPI);
684e3228ed9SCédric Le Goater 
685e3228ed9SCédric Le Goater 	if (aspeed_spi_chip_set_default_window(chip) < 0) {
686e3228ed9SCédric Le Goater 		dev_warn(aspi->dev, "CE%d window invalid", cs);
687e3228ed9SCédric Le Goater 		return -EINVAL;
688e3228ed9SCédric Le Goater 	}
689e3228ed9SCédric Le Goater 
690e3228ed9SCédric Le Goater 	aspeed_spi_chip_enable(aspi, cs, true);
691e3228ed9SCédric Le Goater 
692e3228ed9SCédric Le Goater 	chip->ctl_val[ASPEED_SPI_BASE] = CTRL_CE_STOP_ACTIVE | CTRL_IO_MODE_USER;
693e3228ed9SCédric Le Goater 
694e3228ed9SCédric Le Goater 	dev_dbg(aspi->dev, "CE%d setup done\n", cs);
695e3228ed9SCédric Le Goater 	return 0;
696e3228ed9SCédric Le Goater }
697e3228ed9SCédric Le Goater 
aspeed_spi_cleanup(struct spi_device * spi)698e3228ed9SCédric Le Goater static void aspeed_spi_cleanup(struct spi_device *spi)
699e3228ed9SCédric Le Goater {
70021ac58f5SYang Yingliang 	struct aspeed_spi *aspi = spi_controller_get_devdata(spi->controller);
7019e264f3fSAmit Kumar Mahapatra via Alsa-devel 	unsigned int cs = spi_get_chipselect(spi, 0);
702e3228ed9SCédric Le Goater 
703e3228ed9SCédric Le Goater 	aspeed_spi_chip_enable(aspi, cs, false);
704e3228ed9SCédric Le Goater 
705e3228ed9SCédric Le Goater 	dev_dbg(aspi->dev, "CE%d cleanup done\n", cs);
706e3228ed9SCédric Le Goater }
707e3228ed9SCédric Le Goater 
aspeed_spi_enable(struct aspeed_spi * aspi,bool enable)708e3228ed9SCédric Le Goater static void aspeed_spi_enable(struct aspeed_spi *aspi, bool enable)
709e3228ed9SCédric Le Goater {
710e3228ed9SCédric Le Goater 	int cs;
711e3228ed9SCédric Le Goater 
712e3228ed9SCédric Le Goater 	for (cs = 0; cs < aspi->data->max_cs; cs++)
713e3228ed9SCédric Le Goater 		aspeed_spi_chip_enable(aspi, cs, enable);
714e3228ed9SCédric Le Goater }
715e3228ed9SCédric Le Goater 
aspeed_spi_probe(struct platform_device * pdev)716e3228ed9SCédric Le Goater static int aspeed_spi_probe(struct platform_device *pdev)
717e3228ed9SCédric Le Goater {
718e3228ed9SCédric Le Goater 	struct device *dev = &pdev->dev;
719e3228ed9SCédric Le Goater 	const struct aspeed_spi_data *data;
720e3228ed9SCédric Le Goater 	struct spi_controller *ctlr;
721e3228ed9SCédric Le Goater 	struct aspeed_spi *aspi;
722e3228ed9SCédric Le Goater 	struct resource *res;
723e3228ed9SCédric Le Goater 	int ret;
724e3228ed9SCédric Le Goater 
725e3228ed9SCédric Le Goater 	data = of_device_get_match_data(&pdev->dev);
726e3228ed9SCédric Le Goater 	if (!data)
727e3228ed9SCédric Le Goater 		return -ENODEV;
728e3228ed9SCédric Le Goater 
72921ac58f5SYang Yingliang 	ctlr = devm_spi_alloc_host(dev, sizeof(*aspi));
730e3228ed9SCédric Le Goater 	if (!ctlr)
731e3228ed9SCédric Le Goater 		return -ENOMEM;
732e3228ed9SCédric Le Goater 
733e3228ed9SCédric Le Goater 	aspi = spi_controller_get_devdata(ctlr);
734e3228ed9SCédric Le Goater 	platform_set_drvdata(pdev, aspi);
735e3228ed9SCédric Le Goater 	aspi->data = data;
736e3228ed9SCédric Le Goater 	aspi->dev = dev;
737e3228ed9SCédric Le Goater 
7386d0cebbdSYang Yingliang 	aspi->regs = devm_platform_ioremap_resource(pdev, 0);
73904e0456fSShang XiaoJing 	if (IS_ERR(aspi->regs))
740e3228ed9SCédric Le Goater 		return PTR_ERR(aspi->regs);
741e3228ed9SCédric Le Goater 
7426d0cebbdSYang Yingliang 	aspi->ahb_base = devm_platform_get_and_ioremap_resource(pdev, 1, &res);
743e3228ed9SCédric Le Goater 	if (IS_ERR(aspi->ahb_base)) {
744e3228ed9SCédric Le Goater 		dev_err(dev, "missing AHB mapping window\n");
745e3228ed9SCédric Le Goater 		return PTR_ERR(aspi->ahb_base);
746e3228ed9SCédric Le Goater 	}
747e3228ed9SCédric Le Goater 
748e3228ed9SCédric Le Goater 	aspi->ahb_window_size = resource_size(res);
749e3228ed9SCédric Le Goater 	aspi->ahb_base_phy = res->start;
750e3228ed9SCédric Le Goater 
751*9ee8fbc0SLi Zetao 	aspi->clk = devm_clk_get_enabled(&pdev->dev, NULL);
752e3228ed9SCédric Le Goater 	if (IS_ERR(aspi->clk)) {
753e3228ed9SCédric Le Goater 		dev_err(dev, "missing clock\n");
754e3228ed9SCédric Le Goater 		return PTR_ERR(aspi->clk);
755e3228ed9SCédric Le Goater 	}
756e3228ed9SCédric Le Goater 
757e3228ed9SCédric Le Goater 	aspi->clk_freq = clk_get_rate(aspi->clk);
758e3228ed9SCédric Le Goater 	if (!aspi->clk_freq) {
759e3228ed9SCédric Le Goater 		dev_err(dev, "invalid clock\n");
760e3228ed9SCédric Le Goater 		return -EINVAL;
761e3228ed9SCédric Le Goater 	}
762e3228ed9SCédric Le Goater 
763e3228ed9SCédric Le Goater 	/* IRQ is for DMA, which the driver doesn't support yet */
764e3228ed9SCédric Le Goater 
765e3228ed9SCédric Le Goater 	ctlr->mode_bits = SPI_RX_DUAL | SPI_TX_DUAL | data->mode_bits;
766e3228ed9SCédric Le Goater 	ctlr->bus_num = pdev->id;
767e3228ed9SCédric Le Goater 	ctlr->mem_ops = &aspeed_spi_mem_ops;
768e3228ed9SCédric Le Goater 	ctlr->setup = aspeed_spi_setup;
769e3228ed9SCédric Le Goater 	ctlr->cleanup = aspeed_spi_cleanup;
770e3228ed9SCédric Le Goater 	ctlr->num_chipselect = data->max_cs;
771e3228ed9SCédric Le Goater 	ctlr->dev.of_node = dev->of_node;
772e3228ed9SCédric Le Goater 
773e3228ed9SCédric Le Goater 	ret = devm_spi_register_controller(dev, ctlr);
774*9ee8fbc0SLi Zetao 	if (ret)
775e3228ed9SCédric Le Goater 		dev_err(&pdev->dev, "spi_register_controller failed\n");
776e3228ed9SCédric Le Goater 
777e3228ed9SCédric Le Goater 	return ret;
778e3228ed9SCédric Le Goater }
779e3228ed9SCédric Le Goater 
aspeed_spi_remove(struct platform_device * pdev)780ebf9a50dSUwe Kleine-König static void aspeed_spi_remove(struct platform_device *pdev)
781e3228ed9SCédric Le Goater {
782e3228ed9SCédric Le Goater 	struct aspeed_spi *aspi = platform_get_drvdata(pdev);
783e3228ed9SCédric Le Goater 
784e3228ed9SCédric Le Goater 	aspeed_spi_enable(aspi, false);
785e3228ed9SCédric Le Goater }
786e3228ed9SCédric Le Goater 
787e3228ed9SCédric Le Goater /*
788e3228ed9SCédric Le Goater  * AHB mappings
789e3228ed9SCédric Le Goater  */
790e3228ed9SCédric Le Goater 
791e3228ed9SCédric Le Goater /*
792e3228ed9SCédric Le Goater  * The Segment Registers of the AST2400 and AST2500 use a 8MB unit.
793e3228ed9SCédric Le Goater  * The address range is encoded with absolute addresses in the overall
794e3228ed9SCédric Le Goater  * mapping window.
795e3228ed9SCédric Le Goater  */
aspeed_spi_segment_start(struct aspeed_spi * aspi,u32 reg)796e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_start(struct aspeed_spi *aspi, u32 reg)
797e3228ed9SCédric Le Goater {
798e3228ed9SCédric Le Goater 	return ((reg >> 16) & 0xFF) << 23;
799e3228ed9SCédric Le Goater }
800e3228ed9SCédric Le Goater 
aspeed_spi_segment_end(struct aspeed_spi * aspi,u32 reg)801e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_end(struct aspeed_spi *aspi, u32 reg)
802e3228ed9SCédric Le Goater {
803e3228ed9SCédric Le Goater 	return ((reg >> 24) & 0xFF) << 23;
804e3228ed9SCédric Le Goater }
805e3228ed9SCédric Le Goater 
aspeed_spi_segment_reg(struct aspeed_spi * aspi,u32 start,u32 end)806e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_reg(struct aspeed_spi *aspi, u32 start, u32 end)
807e3228ed9SCédric Le Goater {
808e3228ed9SCédric Le Goater 	return (((start >> 23) & 0xFF) << 16) | (((end >> 23) & 0xFF) << 24);
809e3228ed9SCédric Le Goater }
810e3228ed9SCédric Le Goater 
811e3228ed9SCédric Le Goater /*
812e3228ed9SCédric Le Goater  * The Segment Registers of the AST2600 use a 1MB unit. The address
813e3228ed9SCédric Le Goater  * range is encoded with offsets in the overall mapping window.
814e3228ed9SCédric Le Goater  */
815e3228ed9SCédric Le Goater 
816e3228ed9SCédric Le Goater #define AST2600_SEG_ADDR_MASK 0x0ff00000
817e3228ed9SCédric Le Goater 
aspeed_spi_segment_ast2600_start(struct aspeed_spi * aspi,u32 reg)818e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_ast2600_start(struct aspeed_spi *aspi,
819e3228ed9SCédric Le Goater 					    u32 reg)
820e3228ed9SCédric Le Goater {
821e3228ed9SCédric Le Goater 	u32 start_offset = (reg << 16) & AST2600_SEG_ADDR_MASK;
822e3228ed9SCédric Le Goater 
823e3228ed9SCédric Le Goater 	return aspi->ahb_base_phy + start_offset;
824e3228ed9SCédric Le Goater }
825e3228ed9SCédric Le Goater 
aspeed_spi_segment_ast2600_end(struct aspeed_spi * aspi,u32 reg)826e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_ast2600_end(struct aspeed_spi *aspi,
827e3228ed9SCédric Le Goater 					  u32 reg)
828e3228ed9SCédric Le Goater {
829e3228ed9SCédric Le Goater 	u32 end_offset = reg & AST2600_SEG_ADDR_MASK;
830e3228ed9SCédric Le Goater 
831e3228ed9SCédric Le Goater 	/* segment is disabled */
832e3228ed9SCédric Le Goater 	if (!end_offset)
833e3228ed9SCédric Le Goater 		return aspi->ahb_base_phy;
834e3228ed9SCédric Le Goater 
835e3228ed9SCédric Le Goater 	return aspi->ahb_base_phy + end_offset + 0x100000;
836e3228ed9SCédric Le Goater }
837e3228ed9SCédric Le Goater 
aspeed_spi_segment_ast2600_reg(struct aspeed_spi * aspi,u32 start,u32 end)838e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_ast2600_reg(struct aspeed_spi *aspi,
839e3228ed9SCédric Le Goater 					  u32 start, u32 end)
840e3228ed9SCédric Le Goater {
841e3228ed9SCédric Le Goater 	/* disable zero size segments */
842e3228ed9SCédric Le Goater 	if (start == end)
843e3228ed9SCédric Le Goater 		return 0;
844e3228ed9SCédric Le Goater 
845e3228ed9SCédric Le Goater 	return ((start & AST2600_SEG_ADDR_MASK) >> 16) |
846e3228ed9SCédric Le Goater 		((end - 1) & AST2600_SEG_ADDR_MASK);
847e3228ed9SCédric Le Goater }
848e3228ed9SCédric Le Goater 
849e3228ed9SCédric Le Goater /*
850eeaec1eaSCédric Le Goater  * Read timing compensation sequences
851eeaec1eaSCédric Le Goater  */
852eeaec1eaSCédric Le Goater 
853eeaec1eaSCédric Le Goater #define CALIBRATE_BUF_SIZE SZ_16K
854eeaec1eaSCédric Le Goater 
aspeed_spi_check_reads(struct aspeed_spi_chip * chip,const u8 * golden_buf,u8 * test_buf)855eeaec1eaSCédric Le Goater static bool aspeed_spi_check_reads(struct aspeed_spi_chip *chip,
856eeaec1eaSCédric Le Goater 				   const u8 *golden_buf, u8 *test_buf)
857eeaec1eaSCédric Le Goater {
858eeaec1eaSCédric Le Goater 	int i;
859eeaec1eaSCédric Le Goater 
860eeaec1eaSCédric Le Goater 	for (i = 0; i < 10; i++) {
861eeaec1eaSCédric Le Goater 		memcpy_fromio(test_buf, chip->ahb_base, CALIBRATE_BUF_SIZE);
862eeaec1eaSCédric Le Goater 		if (memcmp(test_buf, golden_buf, CALIBRATE_BUF_SIZE) != 0) {
863eeaec1eaSCédric Le Goater #if defined(VERBOSE_DEBUG)
864eeaec1eaSCédric Le Goater 			print_hex_dump_bytes(DEVICE_NAME "  fail: ", DUMP_PREFIX_NONE,
865eeaec1eaSCédric Le Goater 					     test_buf, 0x100);
866eeaec1eaSCédric Le Goater #endif
867eeaec1eaSCédric Le Goater 			return false;
868eeaec1eaSCédric Le Goater 		}
869eeaec1eaSCédric Le Goater 	}
870eeaec1eaSCédric Le Goater 	return true;
871eeaec1eaSCédric Le Goater }
872eeaec1eaSCédric Le Goater 
873eeaec1eaSCédric Le Goater #define FREAD_TPASS(i)	(((i) / 2) | (((i) & 1) ? 0 : 8))
874eeaec1eaSCédric Le Goater 
875eeaec1eaSCédric Le Goater /*
876eeaec1eaSCédric Le Goater  * The timing register is shared by all devices. Only update for CE0.
877eeaec1eaSCédric Le Goater  */
aspeed_spi_calibrate(struct aspeed_spi_chip * chip,u32 hdiv,const u8 * golden_buf,u8 * test_buf)878eeaec1eaSCédric Le Goater static int aspeed_spi_calibrate(struct aspeed_spi_chip *chip, u32 hdiv,
879eeaec1eaSCédric Le Goater 				const u8 *golden_buf, u8 *test_buf)
880eeaec1eaSCédric Le Goater {
881eeaec1eaSCédric Le Goater 	struct aspeed_spi *aspi = chip->aspi;
882eeaec1eaSCédric Le Goater 	const struct aspeed_spi_data *data = aspi->data;
883eeaec1eaSCédric Le Goater 	int i;
884eeaec1eaSCédric Le Goater 	int good_pass = -1, pass_count = 0;
885eeaec1eaSCédric Le Goater 	u32 shift = (hdiv - 1) << 2;
886eeaec1eaSCédric Le Goater 	u32 mask = ~(0xfu << shift);
887eeaec1eaSCédric Le Goater 	u32 fread_timing_val = 0;
888eeaec1eaSCédric Le Goater 
889eeaec1eaSCédric Le Goater 	/* Try HCLK delay 0..5, each one with/without delay and look for a
890eeaec1eaSCédric Le Goater 	 * good pair.
891eeaec1eaSCédric Le Goater 	 */
892eeaec1eaSCédric Le Goater 	for (i = 0; i < 12; i++) {
893eeaec1eaSCédric Le Goater 		bool pass;
894eeaec1eaSCédric Le Goater 
895eeaec1eaSCédric Le Goater 		if (chip->cs == 0) {
896eeaec1eaSCédric Le Goater 			fread_timing_val &= mask;
897eeaec1eaSCédric Le Goater 			fread_timing_val |= FREAD_TPASS(i) << shift;
898eeaec1eaSCédric Le Goater 			writel(fread_timing_val, aspi->regs + data->timing);
899eeaec1eaSCédric Le Goater 		}
900eeaec1eaSCédric Le Goater 		pass = aspeed_spi_check_reads(chip, golden_buf, test_buf);
901eeaec1eaSCédric Le Goater 		dev_dbg(aspi->dev,
902eeaec1eaSCédric Le Goater 			"  * [%08x] %d HCLK delay, %dns DI delay : %s",
903eeaec1eaSCédric Le Goater 			fread_timing_val, i / 2, (i & 1) ? 0 : 4,
904eeaec1eaSCédric Le Goater 			pass ? "PASS" : "FAIL");
905eeaec1eaSCédric Le Goater 		if (pass) {
906eeaec1eaSCédric Le Goater 			pass_count++;
907eeaec1eaSCédric Le Goater 			if (pass_count == 3) {
908eeaec1eaSCédric Le Goater 				good_pass = i - 1;
909eeaec1eaSCédric Le Goater 				break;
910eeaec1eaSCédric Le Goater 			}
911eeaec1eaSCédric Le Goater 		} else {
912eeaec1eaSCédric Le Goater 			pass_count = 0;
913eeaec1eaSCédric Le Goater 		}
914eeaec1eaSCédric Le Goater 	}
915eeaec1eaSCédric Le Goater 
916eeaec1eaSCédric Le Goater 	/* No good setting for this frequency */
917eeaec1eaSCédric Le Goater 	if (good_pass < 0)
918eeaec1eaSCédric Le Goater 		return -1;
919eeaec1eaSCédric Le Goater 
920eeaec1eaSCédric Le Goater 	/* We have at least one pass of margin, let's use first pass */
921eeaec1eaSCédric Le Goater 	if (chip->cs == 0) {
922eeaec1eaSCédric Le Goater 		fread_timing_val &= mask;
923eeaec1eaSCédric Le Goater 		fread_timing_val |= FREAD_TPASS(good_pass) << shift;
924eeaec1eaSCédric Le Goater 		writel(fread_timing_val, aspi->regs + data->timing);
925eeaec1eaSCédric Le Goater 	}
926eeaec1eaSCédric Le Goater 	dev_dbg(aspi->dev, " * -> good is pass %d [0x%08x]",
927eeaec1eaSCédric Le Goater 		good_pass, fread_timing_val);
928eeaec1eaSCédric Le Goater 	return 0;
929eeaec1eaSCédric Le Goater }
930eeaec1eaSCédric Le Goater 
aspeed_spi_check_calib_data(const u8 * test_buf,u32 size)931eeaec1eaSCédric Le Goater static bool aspeed_spi_check_calib_data(const u8 *test_buf, u32 size)
932eeaec1eaSCédric Le Goater {
933eeaec1eaSCédric Le Goater 	const u32 *tb32 = (const u32 *)test_buf;
934eeaec1eaSCédric Le Goater 	u32 i, cnt = 0;
935eeaec1eaSCédric Le Goater 
936eeaec1eaSCédric Le Goater 	/* We check if we have enough words that are neither all 0
937eeaec1eaSCédric Le Goater 	 * nor all 1's so the calibration can be considered valid.
938eeaec1eaSCédric Le Goater 	 *
939eeaec1eaSCédric Le Goater 	 * I use an arbitrary threshold for now of 64
940eeaec1eaSCédric Le Goater 	 */
941eeaec1eaSCédric Le Goater 	size >>= 2;
942eeaec1eaSCédric Le Goater 	for (i = 0; i < size; i++) {
943eeaec1eaSCédric Le Goater 		if (tb32[i] != 0 && tb32[i] != 0xffffffff)
944eeaec1eaSCédric Le Goater 			cnt++;
945eeaec1eaSCédric Le Goater 	}
946eeaec1eaSCédric Le Goater 	return cnt >= 64;
947eeaec1eaSCédric Le Goater }
948eeaec1eaSCédric Le Goater 
949eeaec1eaSCédric Le Goater static const u32 aspeed_spi_hclk_divs[] = {
950eeaec1eaSCédric Le Goater 	0xf, /* HCLK */
951eeaec1eaSCédric Le Goater 	0x7, /* HCLK/2 */
952eeaec1eaSCédric Le Goater 	0xe, /* HCLK/3 */
953eeaec1eaSCédric Le Goater 	0x6, /* HCLK/4 */
954eeaec1eaSCédric Le Goater 	0xd, /* HCLK/5 */
955eeaec1eaSCédric Le Goater };
956eeaec1eaSCédric Le Goater 
957eeaec1eaSCédric Le Goater #define ASPEED_SPI_HCLK_DIV(i) \
958eeaec1eaSCédric Le Goater 	(aspeed_spi_hclk_divs[(i) - 1] << CTRL_FREQ_SEL_SHIFT)
959eeaec1eaSCédric Le Goater 
aspeed_spi_do_calibration(struct aspeed_spi_chip * chip)960eeaec1eaSCédric Le Goater static int aspeed_spi_do_calibration(struct aspeed_spi_chip *chip)
961eeaec1eaSCédric Le Goater {
962eeaec1eaSCédric Le Goater 	struct aspeed_spi *aspi = chip->aspi;
963eeaec1eaSCédric Le Goater 	const struct aspeed_spi_data *data = aspi->data;
964eeaec1eaSCédric Le Goater 	u32 ahb_freq = aspi->clk_freq;
965eeaec1eaSCédric Le Goater 	u32 max_freq = chip->clk_freq;
966eeaec1eaSCédric Le Goater 	u32 ctl_val;
967eeaec1eaSCédric Le Goater 	u8 *golden_buf = NULL;
968eeaec1eaSCédric Le Goater 	u8 *test_buf = NULL;
969eeaec1eaSCédric Le Goater 	int i, rc, best_div = -1;
970eeaec1eaSCédric Le Goater 
971eeaec1eaSCédric Le Goater 	dev_dbg(aspi->dev, "calculate timing compensation - AHB freq: %d MHz",
972eeaec1eaSCédric Le Goater 		ahb_freq / 1000000);
973eeaec1eaSCédric Le Goater 
974eeaec1eaSCédric Le Goater 	/*
975eeaec1eaSCédric Le Goater 	 * use the related low frequency to get check calibration data
976eeaec1eaSCédric Le Goater 	 * and get golden data.
977eeaec1eaSCédric Le Goater 	 */
978eeaec1eaSCédric Le Goater 	ctl_val = chip->ctl_val[ASPEED_SPI_READ] & data->hclk_mask;
979eeaec1eaSCédric Le Goater 	writel(ctl_val, chip->ctl);
980eeaec1eaSCédric Le Goater 
981eeaec1eaSCédric Le Goater 	test_buf = kzalloc(CALIBRATE_BUF_SIZE * 2, GFP_KERNEL);
982eeaec1eaSCédric Le Goater 	if (!test_buf)
983eeaec1eaSCédric Le Goater 		return -ENOMEM;
984eeaec1eaSCédric Le Goater 
985eeaec1eaSCédric Le Goater 	golden_buf = test_buf + CALIBRATE_BUF_SIZE;
986eeaec1eaSCédric Le Goater 
987eeaec1eaSCédric Le Goater 	memcpy_fromio(golden_buf, chip->ahb_base, CALIBRATE_BUF_SIZE);
988eeaec1eaSCédric Le Goater 	if (!aspeed_spi_check_calib_data(golden_buf, CALIBRATE_BUF_SIZE)) {
989eeaec1eaSCédric Le Goater 		dev_info(aspi->dev, "Calibration area too uniform, using low speed");
990eeaec1eaSCédric Le Goater 		goto no_calib;
991eeaec1eaSCédric Le Goater 	}
992eeaec1eaSCédric Le Goater 
993eeaec1eaSCédric Le Goater #if defined(VERBOSE_DEBUG)
994eeaec1eaSCédric Le Goater 	print_hex_dump_bytes(DEVICE_NAME "  good: ", DUMP_PREFIX_NONE,
995eeaec1eaSCédric Le Goater 			     golden_buf, 0x100);
996eeaec1eaSCédric Le Goater #endif
997eeaec1eaSCédric Le Goater 
998eeaec1eaSCédric Le Goater 	/* Now we iterate the HCLK dividers until we find our breaking point */
999eeaec1eaSCédric Le Goater 	for (i = ARRAY_SIZE(aspeed_spi_hclk_divs); i > data->hdiv_max - 1; i--) {
1000eeaec1eaSCédric Le Goater 		u32 tv, freq;
1001eeaec1eaSCédric Le Goater 
1002eeaec1eaSCédric Le Goater 		freq = ahb_freq / i;
1003eeaec1eaSCédric Le Goater 		if (freq > max_freq)
1004eeaec1eaSCédric Le Goater 			continue;
1005eeaec1eaSCédric Le Goater 
1006eeaec1eaSCédric Le Goater 		/* Set the timing */
1007eeaec1eaSCédric Le Goater 		tv = chip->ctl_val[ASPEED_SPI_READ] | ASPEED_SPI_HCLK_DIV(i);
1008eeaec1eaSCédric Le Goater 		writel(tv, chip->ctl);
1009eeaec1eaSCédric Le Goater 		dev_dbg(aspi->dev, "Trying HCLK/%d [%08x] ...", i, tv);
1010eeaec1eaSCédric Le Goater 		rc = data->calibrate(chip, i, golden_buf, test_buf);
1011eeaec1eaSCédric Le Goater 		if (rc == 0)
1012eeaec1eaSCédric Le Goater 			best_div = i;
1013eeaec1eaSCédric Le Goater 	}
1014eeaec1eaSCédric Le Goater 
1015eeaec1eaSCédric Le Goater 	/* Nothing found ? */
1016eeaec1eaSCédric Le Goater 	if (best_div < 0) {
1017eeaec1eaSCédric Le Goater 		dev_warn(aspi->dev, "No good frequency, using dumb slow");
1018eeaec1eaSCédric Le Goater 	} else {
1019eeaec1eaSCédric Le Goater 		dev_dbg(aspi->dev, "Found good read timings at HCLK/%d", best_div);
1020eeaec1eaSCédric Le Goater 
1021eeaec1eaSCédric Le Goater 		/* Record the freq */
1022eeaec1eaSCédric Le Goater 		for (i = 0; i < ASPEED_SPI_MAX; i++)
1023eeaec1eaSCédric Le Goater 			chip->ctl_val[i] = (chip->ctl_val[i] & data->hclk_mask) |
1024eeaec1eaSCédric Le Goater 				ASPEED_SPI_HCLK_DIV(best_div);
1025eeaec1eaSCédric Le Goater 	}
1026eeaec1eaSCédric Le Goater 
1027eeaec1eaSCédric Le Goater no_calib:
1028eeaec1eaSCédric Le Goater 	writel(chip->ctl_val[ASPEED_SPI_READ], chip->ctl);
1029eeaec1eaSCédric Le Goater 	kfree(test_buf);
1030eeaec1eaSCédric Le Goater 	return 0;
1031eeaec1eaSCédric Le Goater }
1032eeaec1eaSCédric Le Goater 
1033eeaec1eaSCédric Le Goater #define TIMING_DELAY_DI		BIT(3)
1034eeaec1eaSCédric Le Goater #define TIMING_DELAY_HCYCLE_MAX	5
1035eeaec1eaSCédric Le Goater #define TIMING_REG_AST2600(chip)				\
1036eeaec1eaSCédric Le Goater 	((chip)->aspi->regs + (chip)->aspi->data->timing +	\
1037eeaec1eaSCédric Le Goater 	 (chip)->cs * 4)
1038eeaec1eaSCédric Le Goater 
aspeed_spi_ast2600_calibrate(struct aspeed_spi_chip * chip,u32 hdiv,const u8 * golden_buf,u8 * test_buf)1039eeaec1eaSCédric Le Goater static int aspeed_spi_ast2600_calibrate(struct aspeed_spi_chip *chip, u32 hdiv,
1040eeaec1eaSCédric Le Goater 					const u8 *golden_buf, u8 *test_buf)
1041eeaec1eaSCédric Le Goater {
1042eeaec1eaSCédric Le Goater 	struct aspeed_spi *aspi = chip->aspi;
1043eeaec1eaSCédric Le Goater 	int hcycle;
1044eeaec1eaSCédric Le Goater 	u32 shift = (hdiv - 2) << 3;
1045eeaec1eaSCédric Le Goater 	u32 mask = ~(0xfu << shift);
1046eeaec1eaSCédric Le Goater 	u32 fread_timing_val = 0;
1047eeaec1eaSCédric Le Goater 
1048eeaec1eaSCédric Le Goater 	for (hcycle = 0; hcycle <= TIMING_DELAY_HCYCLE_MAX; hcycle++) {
1049eeaec1eaSCédric Le Goater 		int delay_ns;
1050eeaec1eaSCédric Le Goater 		bool pass = false;
1051eeaec1eaSCédric Le Goater 
1052eeaec1eaSCédric Le Goater 		fread_timing_val &= mask;
1053eeaec1eaSCédric Le Goater 		fread_timing_val |= hcycle << shift;
1054eeaec1eaSCédric Le Goater 
1055eeaec1eaSCédric Le Goater 		/* no DI input delay first  */
1056eeaec1eaSCédric Le Goater 		writel(fread_timing_val, TIMING_REG_AST2600(chip));
1057eeaec1eaSCédric Le Goater 		pass = aspeed_spi_check_reads(chip, golden_buf, test_buf);
1058eeaec1eaSCédric Le Goater 		dev_dbg(aspi->dev,
1059eeaec1eaSCédric Le Goater 			"  * [%08x] %d HCLK delay, DI delay none : %s",
1060eeaec1eaSCédric Le Goater 			fread_timing_val, hcycle, pass ? "PASS" : "FAIL");
1061eeaec1eaSCédric Le Goater 		if (pass)
1062eeaec1eaSCédric Le Goater 			return 0;
1063eeaec1eaSCédric Le Goater 
1064eeaec1eaSCédric Le Goater 		/* Add DI input delays  */
1065eeaec1eaSCédric Le Goater 		fread_timing_val &= mask;
1066eeaec1eaSCédric Le Goater 		fread_timing_val |= (TIMING_DELAY_DI | hcycle) << shift;
1067eeaec1eaSCédric Le Goater 
1068eeaec1eaSCédric Le Goater 		for (delay_ns = 0; delay_ns < 0x10; delay_ns++) {
1069eeaec1eaSCédric Le Goater 			fread_timing_val &= ~(0xf << (4 + shift));
1070eeaec1eaSCédric Le Goater 			fread_timing_val |= delay_ns << (4 + shift);
1071eeaec1eaSCédric Le Goater 
1072eeaec1eaSCédric Le Goater 			writel(fread_timing_val, TIMING_REG_AST2600(chip));
1073eeaec1eaSCédric Le Goater 			pass = aspeed_spi_check_reads(chip, golden_buf, test_buf);
1074eeaec1eaSCédric Le Goater 			dev_dbg(aspi->dev,
1075eeaec1eaSCédric Le Goater 				"  * [%08x] %d HCLK delay, DI delay %d.%dns : %s",
1076eeaec1eaSCédric Le Goater 				fread_timing_val, hcycle, (delay_ns + 1) / 2,
1077eeaec1eaSCédric Le Goater 				(delay_ns + 1) & 1 ? 5 : 5, pass ? "PASS" : "FAIL");
1078eeaec1eaSCédric Le Goater 			/*
1079eeaec1eaSCédric Le Goater 			 * TODO: This is optimistic. We should look
1080eeaec1eaSCédric Le Goater 			 * for a working interval and save the middle
1081eeaec1eaSCédric Le Goater 			 * value in the read timing register.
1082eeaec1eaSCédric Le Goater 			 */
1083eeaec1eaSCédric Le Goater 			if (pass)
1084eeaec1eaSCédric Le Goater 				return 0;
1085eeaec1eaSCédric Le Goater 		}
1086eeaec1eaSCédric Le Goater 	}
1087eeaec1eaSCédric Le Goater 
1088eeaec1eaSCédric Le Goater 	/* No good setting for this frequency */
1089eeaec1eaSCédric Le Goater 	return -1;
1090eeaec1eaSCédric Le Goater }
1091eeaec1eaSCédric Le Goater 
1092eeaec1eaSCédric Le Goater /*
1093e3228ed9SCédric Le Goater  * Platform definitions
1094e3228ed9SCédric Le Goater  */
1095e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2400_fmc_data = {
1096e3228ed9SCédric Le Goater 	.max_cs	       = 5,
1097e3228ed9SCédric Le Goater 	.hastype       = true,
1098e3228ed9SCédric Le Goater 	.we0	       = 16,
1099e3228ed9SCédric Le Goater 	.ctl0	       = CE0_CTRL_REG,
1100eeaec1eaSCédric Le Goater 	.timing	       = CE0_TIMING_COMPENSATION_REG,
1101eeaec1eaSCédric Le Goater 	.hclk_mask     = 0xfffff0ff,
1102eeaec1eaSCédric Le Goater 	.hdiv_max      = 1,
1103eeaec1eaSCédric Le Goater 	.calibrate     = aspeed_spi_calibrate,
1104e3228ed9SCédric Le Goater 	.segment_start = aspeed_spi_segment_start,
1105e3228ed9SCédric Le Goater 	.segment_end   = aspeed_spi_segment_end,
1106e3228ed9SCédric Le Goater 	.segment_reg   = aspeed_spi_segment_reg,
1107e3228ed9SCédric Le Goater };
1108e3228ed9SCédric Le Goater 
110953526ab2SCédric Le Goater static const struct aspeed_spi_data ast2400_spi_data = {
111053526ab2SCédric Le Goater 	.max_cs	       = 1,
111153526ab2SCédric Le Goater 	.hastype       = false,
111253526ab2SCédric Le Goater 	.we0	       = 0,
111353526ab2SCédric Le Goater 	.ctl0	       = 0x04,
1114eeaec1eaSCédric Le Goater 	.timing	       = 0x14,
1115eeaec1eaSCédric Le Goater 	.hclk_mask     = 0xfffff0ff,
1116eeaec1eaSCédric Le Goater 	.hdiv_max      = 1,
1117eeaec1eaSCédric Le Goater 	.calibrate     = aspeed_spi_calibrate,
111853526ab2SCédric Le Goater 	/* No segment registers */
111953526ab2SCédric Le Goater };
112053526ab2SCédric Le Goater 
1121e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2500_fmc_data = {
1122e3228ed9SCédric Le Goater 	.max_cs	       = 3,
1123e3228ed9SCédric Le Goater 	.hastype       = true,
1124e3228ed9SCédric Le Goater 	.we0	       = 16,
1125e3228ed9SCédric Le Goater 	.ctl0	       = CE0_CTRL_REG,
1126eeaec1eaSCédric Le Goater 	.timing	       = CE0_TIMING_COMPENSATION_REG,
1127eeaec1eaSCédric Le Goater 	.hclk_mask     = 0xffffd0ff,
1128eeaec1eaSCédric Le Goater 	.hdiv_max      = 1,
1129eeaec1eaSCédric Le Goater 	.calibrate     = aspeed_spi_calibrate,
1130e3228ed9SCédric Le Goater 	.segment_start = aspeed_spi_segment_start,
1131e3228ed9SCédric Le Goater 	.segment_end   = aspeed_spi_segment_end,
1132e3228ed9SCédric Le Goater 	.segment_reg   = aspeed_spi_segment_reg,
1133e3228ed9SCédric Le Goater };
1134e3228ed9SCédric Le Goater 
1135e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2500_spi_data = {
1136e3228ed9SCédric Le Goater 	.max_cs	       = 2,
1137e3228ed9SCédric Le Goater 	.hastype       = false,
1138e3228ed9SCédric Le Goater 	.we0	       = 16,
1139e3228ed9SCédric Le Goater 	.ctl0	       = CE0_CTRL_REG,
1140eeaec1eaSCédric Le Goater 	.timing	       = CE0_TIMING_COMPENSATION_REG,
1141eeaec1eaSCédric Le Goater 	.hclk_mask     = 0xffffd0ff,
1142eeaec1eaSCédric Le Goater 	.hdiv_max      = 1,
1143eeaec1eaSCédric Le Goater 	.calibrate     = aspeed_spi_calibrate,
1144e3228ed9SCédric Le Goater 	.segment_start = aspeed_spi_segment_start,
1145e3228ed9SCédric Le Goater 	.segment_end   = aspeed_spi_segment_end,
1146e3228ed9SCédric Le Goater 	.segment_reg   = aspeed_spi_segment_reg,
1147e3228ed9SCédric Le Goater };
1148e3228ed9SCédric Le Goater 
1149e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2600_fmc_data = {
1150e3228ed9SCédric Le Goater 	.max_cs	       = 3,
1151e3228ed9SCédric Le Goater 	.hastype       = false,
11525302e1ffSChin-Ting Kuo 	.mode_bits     = SPI_RX_QUAD | SPI_TX_QUAD,
1153e3228ed9SCédric Le Goater 	.we0	       = 16,
1154e3228ed9SCédric Le Goater 	.ctl0	       = CE0_CTRL_REG,
1155eeaec1eaSCédric Le Goater 	.timing	       = CE0_TIMING_COMPENSATION_REG,
1156eeaec1eaSCédric Le Goater 	.hclk_mask     = 0xf0fff0ff,
1157eeaec1eaSCédric Le Goater 	.hdiv_max      = 2,
1158eeaec1eaSCédric Le Goater 	.calibrate     = aspeed_spi_ast2600_calibrate,
1159e3228ed9SCédric Le Goater 	.segment_start = aspeed_spi_segment_ast2600_start,
1160e3228ed9SCédric Le Goater 	.segment_end   = aspeed_spi_segment_ast2600_end,
1161e3228ed9SCédric Le Goater 	.segment_reg   = aspeed_spi_segment_ast2600_reg,
1162e3228ed9SCédric Le Goater };
1163e3228ed9SCédric Le Goater 
1164e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2600_spi_data = {
1165e3228ed9SCédric Le Goater 	.max_cs	       = 2,
1166e3228ed9SCédric Le Goater 	.hastype       = false,
11675302e1ffSChin-Ting Kuo 	.mode_bits     = SPI_RX_QUAD | SPI_TX_QUAD,
1168e3228ed9SCédric Le Goater 	.we0	       = 16,
1169e3228ed9SCédric Le Goater 	.ctl0	       = CE0_CTRL_REG,
1170eeaec1eaSCédric Le Goater 	.timing	       = CE0_TIMING_COMPENSATION_REG,
1171eeaec1eaSCédric Le Goater 	.hclk_mask     = 0xf0fff0ff,
1172eeaec1eaSCédric Le Goater 	.hdiv_max      = 2,
1173eeaec1eaSCédric Le Goater 	.calibrate     = aspeed_spi_ast2600_calibrate,
1174e3228ed9SCédric Le Goater 	.segment_start = aspeed_spi_segment_ast2600_start,
1175e3228ed9SCédric Le Goater 	.segment_end   = aspeed_spi_segment_ast2600_end,
1176e3228ed9SCédric Le Goater 	.segment_reg   = aspeed_spi_segment_ast2600_reg,
1177e3228ed9SCédric Le Goater };
1178e3228ed9SCédric Le Goater 
1179e3228ed9SCédric Le Goater static const struct of_device_id aspeed_spi_matches[] = {
1180e3228ed9SCédric Le Goater 	{ .compatible = "aspeed,ast2400-fmc", .data = &ast2400_fmc_data },
118153526ab2SCédric Le Goater 	{ .compatible = "aspeed,ast2400-spi", .data = &ast2400_spi_data },
1182e3228ed9SCédric Le Goater 	{ .compatible = "aspeed,ast2500-fmc", .data = &ast2500_fmc_data },
1183e3228ed9SCédric Le Goater 	{ .compatible = "aspeed,ast2500-spi", .data = &ast2500_spi_data },
1184e3228ed9SCédric Le Goater 	{ .compatible = "aspeed,ast2600-fmc", .data = &ast2600_fmc_data },
1185e3228ed9SCédric Le Goater 	{ .compatible = "aspeed,ast2600-spi", .data = &ast2600_spi_data },
1186e3228ed9SCédric Le Goater 	{ }
1187e3228ed9SCédric Le Goater };
1188e3228ed9SCédric Le Goater MODULE_DEVICE_TABLE(of, aspeed_spi_matches);
1189e3228ed9SCédric Le Goater 
1190e3228ed9SCédric Le Goater static struct platform_driver aspeed_spi_driver = {
1191e3228ed9SCédric Le Goater 	.probe			= aspeed_spi_probe,
1192ebf9a50dSUwe Kleine-König 	.remove_new		= aspeed_spi_remove,
1193e3228ed9SCédric Le Goater 	.driver	= {
1194e3228ed9SCédric Le Goater 		.name		= DEVICE_NAME,
1195e3228ed9SCédric Le Goater 		.of_match_table = aspeed_spi_matches,
1196e3228ed9SCédric Le Goater 	}
1197e3228ed9SCédric Le Goater };
1198e3228ed9SCédric Le Goater 
1199e3228ed9SCédric Le Goater module_platform_driver(aspeed_spi_driver);
1200e3228ed9SCédric Le Goater 
1201e3228ed9SCédric Le Goater MODULE_DESCRIPTION("ASPEED Static Memory Controller Driver");
1202e3228ed9SCédric Le Goater MODULE_AUTHOR("Chin-Ting Kuo <chin-ting_kuo@aspeedtech.com>");
1203e3228ed9SCédric Le Goater MODULE_AUTHOR("Cedric Le Goater <clg@kaod.org>");
1204e3228ed9SCédric Le Goater MODULE_LICENSE("GPL v2");
1205