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)
242*f1d6afbeSChristophe JAILLET goto stop_user;
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);
252*f1d6afbeSChristophe JAILLET stop_user:
253e3228ed9SCédric Le Goater aspeed_spi_stop_user(chip);
254*f1d6afbeSChristophe JAILLET return ret;
255e3228ed9SCédric Le Goater }
256e3228ed9SCédric Le Goater
aspeed_spi_write_user(struct aspeed_spi_chip * chip,const struct spi_mem_op * op)257e3228ed9SCédric Le Goater static ssize_t aspeed_spi_write_user(struct aspeed_spi_chip *chip,
258e3228ed9SCédric Le Goater const struct spi_mem_op *op)
259e3228ed9SCédric Le Goater {
260e3228ed9SCédric Le Goater int ret;
261e3228ed9SCédric Le Goater
262e3228ed9SCédric Le Goater aspeed_spi_start_user(chip);
263e3228ed9SCédric Le Goater ret = aspeed_spi_send_cmd_addr(chip, op->addr.nbytes, op->addr.val, op->cmd.opcode);
264e3228ed9SCédric Le Goater if (ret < 0)
265*f1d6afbeSChristophe JAILLET goto stop_user;
266e3228ed9SCédric Le Goater aspeed_spi_write_to_ahb(chip->ahb_base, op->data.buf.out, op->data.nbytes);
267*f1d6afbeSChristophe JAILLET stop_user:
268e3228ed9SCédric Le Goater aspeed_spi_stop_user(chip);
269*f1d6afbeSChristophe JAILLET return ret;
270e3228ed9SCédric Le Goater }
271e3228ed9SCédric Le Goater
272e3228ed9SCé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)273e3228ed9SCédric Le Goater static bool aspeed_spi_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
274e3228ed9SCédric Le Goater {
275e3228ed9SCédric Le Goater if (op->cmd.buswidth > 1)
276e3228ed9SCédric Le Goater return false;
277e3228ed9SCédric Le Goater
278e3228ed9SCédric Le Goater if (op->addr.nbytes != 0) {
279e3228ed9SCédric Le Goater if (op->addr.buswidth > 1)
280e3228ed9SCédric Le Goater return false;
281e3228ed9SCédric Le Goater if (op->addr.nbytes < 3 || op->addr.nbytes > 4)
282e3228ed9SCédric Le Goater return false;
283e3228ed9SCédric Le Goater }
284e3228ed9SCédric Le Goater
285e3228ed9SCédric Le Goater if (op->dummy.nbytes != 0) {
286e3228ed9SCédric Le Goater if (op->dummy.buswidth > 1 || op->dummy.nbytes > 7)
287e3228ed9SCédric Le Goater return false;
288e3228ed9SCédric Le Goater }
289e3228ed9SCédric Le Goater
290e3228ed9SCédric Le Goater if (op->data.nbytes != 0 && op->data.buswidth > 4)
291e3228ed9SCédric Le Goater return false;
292e3228ed9SCédric Le Goater
293e3228ed9SCédric Le Goater return spi_mem_default_supports_op(mem, op);
294e3228ed9SCédric Le Goater }
295e3228ed9SCédric Le Goater
29653526ab2SCédric Le Goater static const struct aspeed_spi_data ast2400_spi_data;
29753526ab2SCédric Le Goater
do_aspeed_spi_exec_op(struct spi_mem * mem,const struct spi_mem_op * op)298e3228ed9SCédric Le Goater static int do_aspeed_spi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
299e3228ed9SCédric Le Goater {
30021ac58f5SYang Yingliang struct aspeed_spi *aspi = spi_controller_get_devdata(mem->spi->controller);
3019e264f3fSAmit Kumar Mahapatra via Alsa-devel struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(mem->spi, 0)];
302e3228ed9SCédric Le Goater u32 addr_mode, addr_mode_backup;
303e3228ed9SCédric Le Goater u32 ctl_val;
304e3228ed9SCédric Le Goater int ret = 0;
305e3228ed9SCédric Le Goater
306e3228ed9SCédric Le Goater dev_dbg(aspi->dev,
307e3228ed9SCédric Le Goater "CE%d %s OP %#x mode:%d.%d.%d.%d naddr:%#x ndummies:%#x len:%#x",
308e3228ed9SCédric Le Goater chip->cs, op->data.dir == SPI_MEM_DATA_IN ? "read" : "write",
309e3228ed9SCédric Le Goater op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
310e3228ed9SCédric Le Goater op->dummy.buswidth, op->data.buswidth,
311e3228ed9SCédric Le Goater op->addr.nbytes, op->dummy.nbytes, op->data.nbytes);
312e3228ed9SCédric Le Goater
313e3228ed9SCédric Le Goater addr_mode = readl(aspi->regs + CE_CTRL_REG);
314e3228ed9SCédric Le Goater addr_mode_backup = addr_mode;
315e3228ed9SCédric Le Goater
316e3228ed9SCédric Le Goater ctl_val = chip->ctl_val[ASPEED_SPI_BASE];
317e3228ed9SCédric Le Goater ctl_val &= ~CTRL_IO_CMD_MASK;
318e3228ed9SCédric Le Goater
319e3228ed9SCédric Le Goater ctl_val |= op->cmd.opcode << CTRL_COMMAND_SHIFT;
320e3228ed9SCédric Le Goater
321e3228ed9SCédric Le Goater /* 4BYTE address mode */
322e3228ed9SCédric Le Goater if (op->addr.nbytes) {
323e3228ed9SCédric Le Goater if (op->addr.nbytes == 4)
324e3228ed9SCédric Le Goater addr_mode |= (0x11 << chip->cs);
325e3228ed9SCédric Le Goater else
326e3228ed9SCédric Le Goater addr_mode &= ~(0x11 << chip->cs);
32753526ab2SCédric Le Goater
32853526ab2SCédric Le Goater if (op->addr.nbytes == 4 && chip->aspi->data == &ast2400_spi_data)
32953526ab2SCédric Le Goater ctl_val |= CTRL_IO_ADDRESS_4B;
330e3228ed9SCédric Le Goater }
331e3228ed9SCédric Le Goater
332e3228ed9SCédric Le Goater if (op->dummy.nbytes)
333e3228ed9SCédric Le Goater ctl_val |= CTRL_IO_DUMMY_SET(op->dummy.nbytes / op->dummy.buswidth);
334e3228ed9SCédric Le Goater
335e3228ed9SCédric Le Goater if (op->data.nbytes)
336e3228ed9SCédric Le Goater ctl_val |= aspeed_spi_get_io_mode(op);
337e3228ed9SCédric Le Goater
338e3228ed9SCédric Le Goater if (op->data.dir == SPI_MEM_DATA_OUT)
339e3228ed9SCédric Le Goater ctl_val |= CTRL_IO_MODE_WRITE;
340e3228ed9SCédric Le Goater else
341e3228ed9SCédric Le Goater ctl_val |= CTRL_IO_MODE_READ;
342e3228ed9SCédric Le Goater
343e3228ed9SCédric Le Goater if (addr_mode != addr_mode_backup)
344e3228ed9SCédric Le Goater writel(addr_mode, aspi->regs + CE_CTRL_REG);
345e3228ed9SCédric Le Goater writel(ctl_val, chip->ctl);
346e3228ed9SCédric Le Goater
347e3228ed9SCédric Le Goater if (op->data.dir == SPI_MEM_DATA_IN) {
348e3228ed9SCédric Le Goater if (!op->addr.nbytes)
349e3228ed9SCédric Le Goater ret = aspeed_spi_read_reg(chip, op);
350e3228ed9SCédric Le Goater else
351e3228ed9SCédric Le Goater ret = aspeed_spi_read_user(chip, op, op->addr.val,
352e3228ed9SCédric Le Goater op->data.nbytes, op->data.buf.in);
353e3228ed9SCédric Le Goater } else {
354e3228ed9SCédric Le Goater if (!op->addr.nbytes)
355e3228ed9SCédric Le Goater ret = aspeed_spi_write_reg(chip, op);
356e3228ed9SCédric Le Goater else
357e3228ed9SCédric Le Goater ret = aspeed_spi_write_user(chip, op);
358e3228ed9SCédric Le Goater }
359e3228ed9SCédric Le Goater
360e3228ed9SCédric Le Goater /* Restore defaults */
361e3228ed9SCédric Le Goater if (addr_mode != addr_mode_backup)
362e3228ed9SCédric Le Goater writel(addr_mode_backup, aspi->regs + CE_CTRL_REG);
363e3228ed9SCédric Le Goater writel(chip->ctl_val[ASPEED_SPI_READ], chip->ctl);
364e3228ed9SCédric Le Goater return ret;
365e3228ed9SCédric Le Goater }
366e3228ed9SCédric Le Goater
aspeed_spi_exec_op(struct spi_mem * mem,const struct spi_mem_op * op)367e3228ed9SCédric Le Goater static int aspeed_spi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
368e3228ed9SCédric Le Goater {
369e3228ed9SCédric Le Goater int ret;
370e3228ed9SCédric Le Goater
371e3228ed9SCédric Le Goater ret = do_aspeed_spi_exec_op(mem, op);
372e3228ed9SCédric Le Goater if (ret)
373e3228ed9SCédric Le Goater dev_err(&mem->spi->dev, "operation failed: %d\n", ret);
374e3228ed9SCédric Le Goater return ret;
375e3228ed9SCédric Le Goater }
376e3228ed9SCédric Le Goater
aspeed_spi_get_name(struct spi_mem * mem)377e3228ed9SCédric Le Goater static const char *aspeed_spi_get_name(struct spi_mem *mem)
378e3228ed9SCédric Le Goater {
37921ac58f5SYang Yingliang struct aspeed_spi *aspi = spi_controller_get_devdata(mem->spi->controller);
380e3228ed9SCédric Le Goater struct device *dev = aspi->dev;
381e3228ed9SCédric Le Goater
3829e264f3fSAmit Kumar Mahapatra via Alsa-devel return devm_kasprintf(dev, GFP_KERNEL, "%s.%d", dev_name(dev),
3839e264f3fSAmit Kumar Mahapatra via Alsa-devel spi_get_chipselect(mem->spi, 0));
384e3228ed9SCédric Le Goater }
385e3228ed9SCédric Le Goater
386e3228ed9SCédric Le Goater struct aspeed_spi_window {
387e3228ed9SCédric Le Goater u32 cs;
388e3228ed9SCédric Le Goater u32 offset;
389e3228ed9SCédric Le Goater u32 size;
390e3228ed9SCédric Le Goater };
391e3228ed9SCédric Le Goater
aspeed_spi_get_windows(struct aspeed_spi * aspi,struct aspeed_spi_window windows[ASPEED_SPI_MAX_NUM_CS])392e3228ed9SCédric Le Goater static void aspeed_spi_get_windows(struct aspeed_spi *aspi,
393e3228ed9SCédric Le Goater struct aspeed_spi_window windows[ASPEED_SPI_MAX_NUM_CS])
394e3228ed9SCédric Le Goater {
395e3228ed9SCédric Le Goater const struct aspeed_spi_data *data = aspi->data;
396e3228ed9SCédric Le Goater u32 reg_val;
397e3228ed9SCédric Le Goater u32 cs;
398e3228ed9SCédric Le Goater
399e3228ed9SCédric Le Goater for (cs = 0; cs < aspi->data->max_cs; cs++) {
400e3228ed9SCédric Le Goater reg_val = readl(aspi->regs + CE0_SEGMENT_ADDR_REG + cs * 4);
401e3228ed9SCédric Le Goater windows[cs].cs = cs;
402e3228ed9SCédric Le Goater windows[cs].size = data->segment_end(aspi, reg_val) -
403e3228ed9SCédric Le Goater data->segment_start(aspi, reg_val);
404f8aa6c89SCédric Le Goater windows[cs].offset = data->segment_start(aspi, reg_val) - aspi->ahb_base_phy;
405e3228ed9SCédric Le Goater dev_vdbg(aspi->dev, "CE%d offset=0x%.8x size=0x%x\n", cs,
406e3228ed9SCédric Le Goater windows[cs].offset, windows[cs].size);
407e3228ed9SCédric Le Goater }
408e3228ed9SCédric Le Goater }
409e3228ed9SCédric Le Goater
410e3228ed9SCédric Le Goater /*
411e3228ed9SCédric Le Goater * On the AST2600, some CE windows are closed by default at reset but
412e3228ed9SCédric Le Goater * U-Boot should open all.
413e3228ed9SCédric Le Goater */
aspeed_spi_chip_set_default_window(struct aspeed_spi_chip * chip)414e3228ed9SCédric Le Goater static int aspeed_spi_chip_set_default_window(struct aspeed_spi_chip *chip)
415e3228ed9SCédric Le Goater {
416e3228ed9SCédric Le Goater struct aspeed_spi *aspi = chip->aspi;
417e3228ed9SCédric Le Goater struct aspeed_spi_window windows[ASPEED_SPI_MAX_NUM_CS] = { 0 };
418e3228ed9SCédric Le Goater struct aspeed_spi_window *win = &windows[chip->cs];
419e3228ed9SCédric Le Goater
42053526ab2SCédric Le Goater /* No segment registers for the AST2400 SPI controller */
42153526ab2SCédric Le Goater if (aspi->data == &ast2400_spi_data) {
42253526ab2SCédric Le Goater win->offset = 0;
42353526ab2SCédric Le Goater win->size = aspi->ahb_window_size;
42453526ab2SCédric Le Goater } else {
425e3228ed9SCédric Le Goater aspeed_spi_get_windows(aspi, windows);
42653526ab2SCédric Le Goater }
427e3228ed9SCédric Le Goater
428e3228ed9SCédric Le Goater chip->ahb_base = aspi->ahb_base + win->offset;
429e3228ed9SCédric Le Goater chip->ahb_window_size = win->size;
430e3228ed9SCédric Le Goater
431e3228ed9SCédric Le Goater dev_dbg(aspi->dev, "CE%d default window [ 0x%.8x - 0x%.8x ] %dMB",
432e3228ed9SCédric Le Goater chip->cs, aspi->ahb_base_phy + win->offset,
433e3228ed9SCédric Le Goater aspi->ahb_base_phy + win->offset + win->size - 1,
434e3228ed9SCédric Le Goater win->size >> 20);
435e3228ed9SCédric Le Goater
436e3228ed9SCédric Le Goater return chip->ahb_window_size ? 0 : -1;
437e3228ed9SCédric Le Goater }
438e3228ed9SCédric Le Goater
aspeed_spi_set_window(struct aspeed_spi * aspi,const struct aspeed_spi_window * win)439bb084f94SCédric Le Goater static int aspeed_spi_set_window(struct aspeed_spi *aspi,
440bb084f94SCédric Le Goater const struct aspeed_spi_window *win)
441bb084f94SCédric Le Goater {
442bb084f94SCédric Le Goater u32 start = aspi->ahb_base_phy + win->offset;
443bb084f94SCédric Le Goater u32 end = start + win->size;
444bb084f94SCédric Le Goater void __iomem *seg_reg = aspi->regs + CE0_SEGMENT_ADDR_REG + win->cs * 4;
445bb084f94SCédric Le Goater u32 seg_val_backup = readl(seg_reg);
446bb084f94SCédric Le Goater u32 seg_val = aspi->data->segment_reg(aspi, start, end);
447bb084f94SCédric Le Goater
448bb084f94SCédric Le Goater if (seg_val == seg_val_backup)
449bb084f94SCédric Le Goater return 0;
450bb084f94SCédric Le Goater
451bb084f94SCédric Le Goater writel(seg_val, seg_reg);
452bb084f94SCédric Le Goater
453bb084f94SCédric Le Goater /*
454bb084f94SCédric Le Goater * Restore initial value if something goes wrong else we could
455bb084f94SCédric Le Goater * loose access to the chip.
456bb084f94SCédric Le Goater */
457bb084f94SCédric Le Goater if (seg_val != readl(seg_reg)) {
458bb084f94SCédric Le Goater dev_err(aspi->dev, "CE%d invalid window [ 0x%.8x - 0x%.8x ] %dMB",
459bb084f94SCédric Le Goater win->cs, start, end - 1, win->size >> 20);
460bb084f94SCédric Le Goater writel(seg_val_backup, seg_reg);
461bb084f94SCédric Le Goater return -EIO;
462bb084f94SCédric Le Goater }
463bb084f94SCédric Le Goater
464bb084f94SCédric Le Goater if (win->size)
465bb084f94SCédric Le Goater dev_dbg(aspi->dev, "CE%d new window [ 0x%.8x - 0x%.8x ] %dMB",
466bb084f94SCédric Le Goater win->cs, start, end - 1, win->size >> 20);
467bb084f94SCédric Le Goater else
468bb084f94SCédric Le Goater dev_dbg(aspi->dev, "CE%d window closed", win->cs);
469bb084f94SCédric Le Goater
470bb084f94SCédric Le Goater return 0;
471bb084f94SCédric Le Goater }
472bb084f94SCédric Le Goater
473bb084f94SCédric Le Goater /*
474bb084f94SCédric Le Goater * Yet to be done when possible :
475bb084f94SCédric Le Goater * - Align mappings on flash size (we don't have the info)
476bb084f94SCédric Le Goater * - ioremap each window, not strictly necessary since the overall window
477bb084f94SCédric Le Goater * is correct.
478bb084f94SCédric Le Goater */
4795785eedeSCédric Le Goater static const struct aspeed_spi_data ast2500_spi_data;
48073ae97e3SPotin Lai static const struct aspeed_spi_data ast2600_spi_data;
48173ae97e3SPotin Lai static const struct aspeed_spi_data ast2600_fmc_data;
4825785eedeSCédric Le Goater
aspeed_spi_chip_adjust_window(struct aspeed_spi_chip * chip,u32 local_offset,u32 size)483bb084f94SCédric Le Goater static int aspeed_spi_chip_adjust_window(struct aspeed_spi_chip *chip,
484bb084f94SCédric Le Goater u32 local_offset, u32 size)
485bb084f94SCédric Le Goater {
486bb084f94SCédric Le Goater struct aspeed_spi *aspi = chip->aspi;
487bb084f94SCédric Le Goater struct aspeed_spi_window windows[ASPEED_SPI_MAX_NUM_CS] = { 0 };
488bb084f94SCédric Le Goater struct aspeed_spi_window *win = &windows[chip->cs];
489bb084f94SCédric Le Goater int ret;
490bb084f94SCédric Le Goater
49153526ab2SCédric Le Goater /* No segment registers for the AST2400 SPI controller */
49253526ab2SCédric Le Goater if (aspi->data == &ast2400_spi_data)
49353526ab2SCédric Le Goater return 0;
49453526ab2SCédric Le Goater
4955785eedeSCédric Le Goater /*
4965785eedeSCédric Le Goater * Due to an HW issue on the AST2500 SPI controller, the CE0
4975785eedeSCédric Le Goater * window size should be smaller than the maximum 128MB.
4985785eedeSCédric Le Goater */
4995785eedeSCédric Le Goater if (aspi->data == &ast2500_spi_data && chip->cs == 0 && size == SZ_128M) {
5005785eedeSCédric Le Goater size = 120 << 20;
5015785eedeSCédric Le Goater dev_info(aspi->dev, "CE%d window resized to %dMB (AST2500 HW quirk)",
5025785eedeSCédric Le Goater chip->cs, size >> 20);
5035785eedeSCédric Le Goater }
5045785eedeSCédric Le Goater
50573ae97e3SPotin Lai /*
50673ae97e3SPotin Lai * The decoding size of AST2600 SPI controller should set at
50773ae97e3SPotin Lai * least 2MB.
50873ae97e3SPotin Lai */
50973ae97e3SPotin Lai if ((aspi->data == &ast2600_spi_data || aspi->data == &ast2600_fmc_data) &&
51073ae97e3SPotin Lai size < SZ_2M) {
51173ae97e3SPotin Lai size = SZ_2M;
51273ae97e3SPotin Lai dev_info(aspi->dev, "CE%d window resized to %dMB (AST2600 Decoding)",
51373ae97e3SPotin Lai chip->cs, size >> 20);
51473ae97e3SPotin Lai }
51573ae97e3SPotin Lai
516bb084f94SCédric Le Goater aspeed_spi_get_windows(aspi, windows);
517bb084f94SCédric Le Goater
518bb084f94SCédric Le Goater /* Adjust this chip window */
519bb084f94SCédric Le Goater win->offset += local_offset;
520bb084f94SCédric Le Goater win->size = size;
521bb084f94SCédric Le Goater
522bb084f94SCédric Le Goater if (win->offset + win->size > aspi->ahb_window_size) {
523bb084f94SCédric Le Goater win->size = aspi->ahb_window_size - win->offset;
524bb084f94SCédric Le Goater dev_warn(aspi->dev, "CE%d window resized to %dMB", chip->cs, win->size >> 20);
525bb084f94SCédric Le Goater }
526bb084f94SCédric Le Goater
527bb084f94SCédric Le Goater ret = aspeed_spi_set_window(aspi, win);
528bb084f94SCédric Le Goater if (ret)
529bb084f94SCédric Le Goater return ret;
530bb084f94SCédric Le Goater
531bb084f94SCédric Le Goater /* Update chip mapping info */
532bb084f94SCédric Le Goater chip->ahb_base = aspi->ahb_base + win->offset;
533bb084f94SCédric Le Goater chip->ahb_window_size = win->size;
534bb084f94SCédric Le Goater
535bb084f94SCédric Le Goater /*
536bb084f94SCédric Le Goater * Also adjust next chip window to make sure that it does not
537bb084f94SCédric Le Goater * overlap with the current window.
538bb084f94SCédric Le Goater */
539bb084f94SCédric Le Goater if (chip->cs < aspi->data->max_cs - 1) {
540bb084f94SCédric Le Goater struct aspeed_spi_window *next = &windows[chip->cs + 1];
541bb084f94SCédric Le Goater
542bb084f94SCédric Le Goater /* Change offset and size to keep the same end address */
543bb084f94SCédric Le Goater if ((next->offset + next->size) > (win->offset + win->size))
544bb084f94SCédric Le Goater next->size = (next->offset + next->size) - (win->offset + win->size);
545bb084f94SCédric Le Goater else
546bb084f94SCédric Le Goater next->size = 0;
547bb084f94SCédric Le Goater next->offset = win->offset + win->size;
548bb084f94SCédric Le Goater
549bb084f94SCédric Le Goater aspeed_spi_set_window(aspi, next);
550bb084f94SCédric Le Goater }
551bb084f94SCédric Le Goater return 0;
552bb084f94SCédric Le Goater }
553bb084f94SCédric Le Goater
554eeaec1eaSCédric Le Goater static int aspeed_spi_do_calibration(struct aspeed_spi_chip *chip);
555eeaec1eaSCédric Le Goater
aspeed_spi_dirmap_create(struct spi_mem_dirmap_desc * desc)5569da06d7bSCédric Le Goater static int aspeed_spi_dirmap_create(struct spi_mem_dirmap_desc *desc)
5579da06d7bSCédric Le Goater {
55821ac58f5SYang Yingliang struct aspeed_spi *aspi = spi_controller_get_devdata(desc->mem->spi->controller);
5599e264f3fSAmit Kumar Mahapatra via Alsa-devel struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(desc->mem->spi, 0)];
5609da06d7bSCédric Le Goater struct spi_mem_op *op = &desc->info.op_tmpl;
5619da06d7bSCédric Le Goater u32 ctl_val;
5629da06d7bSCédric Le Goater int ret = 0;
5639da06d7bSCédric Le Goater
5648988ba7dSCédric Le Goater dev_dbg(aspi->dev,
5658988ba7dSCédric Le Goater "CE%d %s dirmap [ 0x%.8llx - 0x%.8llx ] OP %#x mode:%d.%d.%d.%d naddr:%#x ndummies:%#x\n",
5668988ba7dSCédric Le Goater chip->cs, op->data.dir == SPI_MEM_DATA_IN ? "read" : "write",
5678988ba7dSCédric Le Goater desc->info.offset, desc->info.offset + desc->info.length,
5688988ba7dSCédric Le Goater op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
5698988ba7dSCédric Le Goater op->dummy.buswidth, op->data.buswidth,
5708988ba7dSCédric Le Goater op->addr.nbytes, op->dummy.nbytes);
5718988ba7dSCédric Le Goater
5729da06d7bSCédric Le Goater chip->clk_freq = desc->mem->spi->max_speed_hz;
5739da06d7bSCédric Le Goater
5749da06d7bSCédric Le Goater /* Only for reads */
5759da06d7bSCédric Le Goater if (op->data.dir != SPI_MEM_DATA_IN)
5769da06d7bSCédric Le Goater return -EOPNOTSUPP;
5779da06d7bSCédric Le Goater
578bb084f94SCédric Le Goater aspeed_spi_chip_adjust_window(chip, desc->info.offset, desc->info.length);
579bb084f94SCédric Le Goater
5809da06d7bSCédric Le Goater if (desc->info.length > chip->ahb_window_size)
5819da06d7bSCédric Le Goater dev_warn(aspi->dev, "CE%d window (%dMB) too small for mapping",
5829da06d7bSCédric Le Goater chip->cs, chip->ahb_window_size >> 20);
5839da06d7bSCédric Le Goater
5849da06d7bSCédric Le Goater /* Define the default IO read settings */
5859da06d7bSCédric Le Goater ctl_val = readl(chip->ctl) & ~CTRL_IO_CMD_MASK;
5869da06d7bSCédric Le Goater ctl_val |= aspeed_spi_get_io_mode(op) |
5879da06d7bSCédric Le Goater op->cmd.opcode << CTRL_COMMAND_SHIFT |
5889da06d7bSCédric Le Goater CTRL_IO_MODE_READ;
5899da06d7bSCédric Le Goater
59030554a1fSCédric Le Goater if (op->dummy.nbytes)
59130554a1fSCédric Le Goater ctl_val |= CTRL_IO_DUMMY_SET(op->dummy.nbytes / op->dummy.buswidth);
59230554a1fSCédric Le Goater
5939da06d7bSCédric Le Goater /* Tune 4BYTE address mode */
5949da06d7bSCédric Le Goater if (op->addr.nbytes) {
5959da06d7bSCédric Le Goater u32 addr_mode = readl(aspi->regs + CE_CTRL_REG);
5969da06d7bSCédric Le Goater
5979da06d7bSCédric Le Goater if (op->addr.nbytes == 4)
5989da06d7bSCédric Le Goater addr_mode |= (0x11 << chip->cs);
5999da06d7bSCédric Le Goater else
6009da06d7bSCédric Le Goater addr_mode &= ~(0x11 << chip->cs);
6019da06d7bSCédric Le Goater writel(addr_mode, aspi->regs + CE_CTRL_REG);
60253526ab2SCédric Le Goater
60353526ab2SCédric Le Goater /* AST2400 SPI controller sets 4BYTE address mode in
60453526ab2SCédric Le Goater * CE0 Control Register
60553526ab2SCédric Le Goater */
60653526ab2SCédric Le Goater if (op->addr.nbytes == 4 && chip->aspi->data == &ast2400_spi_data)
60753526ab2SCédric Le Goater ctl_val |= CTRL_IO_ADDRESS_4B;
6089da06d7bSCédric Le Goater }
6099da06d7bSCédric Le Goater
6109da06d7bSCédric Le Goater /* READ mode is the controller default setting */
6119da06d7bSCédric Le Goater chip->ctl_val[ASPEED_SPI_READ] = ctl_val;
6129da06d7bSCédric Le Goater writel(chip->ctl_val[ASPEED_SPI_READ], chip->ctl);
6139da06d7bSCédric Le Goater
614eeaec1eaSCédric Le Goater ret = aspeed_spi_do_calibration(chip);
615eeaec1eaSCédric Le Goater
6169da06d7bSCédric Le Goater dev_info(aspi->dev, "CE%d read buswidth:%d [0x%08x]\n",
6179da06d7bSCédric Le Goater chip->cs, op->data.buswidth, chip->ctl_val[ASPEED_SPI_READ]);
6189da06d7bSCédric Le Goater
6199da06d7bSCédric Le Goater return ret;
6209da06d7bSCédric Le Goater }
6219da06d7bSCédric Le Goater
aspeed_spi_dirmap_read(struct spi_mem_dirmap_desc * desc,u64 offset,size_t len,void * buf)6229da06d7bSCédric Le Goater static ssize_t aspeed_spi_dirmap_read(struct spi_mem_dirmap_desc *desc,
6239da06d7bSCédric Le Goater u64 offset, size_t len, void *buf)
6249da06d7bSCédric Le Goater {
62521ac58f5SYang Yingliang struct aspeed_spi *aspi = spi_controller_get_devdata(desc->mem->spi->controller);
6269e264f3fSAmit Kumar Mahapatra via Alsa-devel struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(desc->mem->spi, 0)];
6279da06d7bSCédric Le Goater
6289da06d7bSCédric Le Goater /* Switch to USER command mode if mapping window is too small */
6299da06d7bSCédric Le Goater if (chip->ahb_window_size < offset + len) {
6309da06d7bSCédric Le Goater int ret;
6319da06d7bSCédric Le Goater
6329da06d7bSCédric Le Goater ret = aspeed_spi_read_user(chip, &desc->info.op_tmpl, offset, len, buf);
6339da06d7bSCédric Le Goater if (ret < 0)
6349da06d7bSCédric Le Goater return ret;
6359da06d7bSCédric Le Goater } else {
6369da06d7bSCédric Le Goater memcpy_fromio(buf, chip->ahb_base + offset, len);
6379da06d7bSCédric Le Goater }
6389da06d7bSCédric Le Goater
6399da06d7bSCédric Le Goater return len;
6409da06d7bSCédric Le Goater }
6419da06d7bSCédric Le Goater
642e3228ed9SCédric Le Goater static const struct spi_controller_mem_ops aspeed_spi_mem_ops = {
643e3228ed9SCédric Le Goater .supports_op = aspeed_spi_supports_op,
644e3228ed9SCédric Le Goater .exec_op = aspeed_spi_exec_op,
645e3228ed9SCédric Le Goater .get_name = aspeed_spi_get_name,
6469da06d7bSCédric Le Goater .dirmap_create = aspeed_spi_dirmap_create,
6479da06d7bSCédric Le Goater .dirmap_read = aspeed_spi_dirmap_read,
648e3228ed9SCédric Le Goater };
649e3228ed9SCédric Le Goater
aspeed_spi_chip_set_type(struct aspeed_spi * aspi,unsigned int cs,int type)650e3228ed9SCédric Le Goater static void aspeed_spi_chip_set_type(struct aspeed_spi *aspi, unsigned int cs, int type)
651e3228ed9SCédric Le Goater {
652e3228ed9SCédric Le Goater u32 reg;
653e3228ed9SCédric Le Goater
654e3228ed9SCédric Le Goater reg = readl(aspi->regs + CONFIG_REG);
655e3228ed9SCédric Le Goater reg &= ~(0x3 << (cs * 2));
656e3228ed9SCédric Le Goater reg |= type << (cs * 2);
657e3228ed9SCédric Le Goater writel(reg, aspi->regs + CONFIG_REG);
658e3228ed9SCédric Le Goater }
659e3228ed9SCédric Le Goater
aspeed_spi_chip_enable(struct aspeed_spi * aspi,unsigned int cs,bool enable)660e3228ed9SCédric Le Goater static void aspeed_spi_chip_enable(struct aspeed_spi *aspi, unsigned int cs, bool enable)
661e3228ed9SCédric Le Goater {
662e3228ed9SCédric Le Goater u32 we_bit = BIT(aspi->data->we0 + cs);
663e3228ed9SCédric Le Goater u32 reg = readl(aspi->regs + CONFIG_REG);
664e3228ed9SCédric Le Goater
665e3228ed9SCédric Le Goater if (enable)
666e3228ed9SCédric Le Goater reg |= we_bit;
667e3228ed9SCédric Le Goater else
668e3228ed9SCédric Le Goater reg &= ~we_bit;
669e3228ed9SCédric Le Goater writel(reg, aspi->regs + CONFIG_REG);
670e3228ed9SCédric Le Goater }
671e3228ed9SCédric Le Goater
aspeed_spi_setup(struct spi_device * spi)672e3228ed9SCédric Le Goater static int aspeed_spi_setup(struct spi_device *spi)
673e3228ed9SCédric Le Goater {
67421ac58f5SYang Yingliang struct aspeed_spi *aspi = spi_controller_get_devdata(spi->controller);
675e3228ed9SCédric Le Goater const struct aspeed_spi_data *data = aspi->data;
6769e264f3fSAmit Kumar Mahapatra via Alsa-devel unsigned int cs = spi_get_chipselect(spi, 0);
677e3228ed9SCédric Le Goater struct aspeed_spi_chip *chip = &aspi->chips[cs];
678e3228ed9SCédric Le Goater
679e3228ed9SCédric Le Goater chip->aspi = aspi;
680e3228ed9SCédric Le Goater chip->cs = cs;
681e3228ed9SCédric Le Goater chip->ctl = aspi->regs + data->ctl0 + cs * 4;
682e3228ed9SCédric Le Goater
683e3228ed9SCédric Le Goater /* The driver only supports SPI type flash */
684e3228ed9SCédric Le Goater if (data->hastype)
685e3228ed9SCédric Le Goater aspeed_spi_chip_set_type(aspi, cs, CONFIG_TYPE_SPI);
686e3228ed9SCédric Le Goater
687e3228ed9SCédric Le Goater if (aspeed_spi_chip_set_default_window(chip) < 0) {
688e3228ed9SCédric Le Goater dev_warn(aspi->dev, "CE%d window invalid", cs);
689e3228ed9SCédric Le Goater return -EINVAL;
690e3228ed9SCédric Le Goater }
691e3228ed9SCédric Le Goater
692e3228ed9SCédric Le Goater aspeed_spi_chip_enable(aspi, cs, true);
693e3228ed9SCédric Le Goater
694e3228ed9SCédric Le Goater chip->ctl_val[ASPEED_SPI_BASE] = CTRL_CE_STOP_ACTIVE | CTRL_IO_MODE_USER;
695e3228ed9SCédric Le Goater
696e3228ed9SCédric Le Goater dev_dbg(aspi->dev, "CE%d setup done\n", cs);
697e3228ed9SCédric Le Goater return 0;
698e3228ed9SCédric Le Goater }
699e3228ed9SCédric Le Goater
aspeed_spi_cleanup(struct spi_device * spi)700e3228ed9SCédric Le Goater static void aspeed_spi_cleanup(struct spi_device *spi)
701e3228ed9SCédric Le Goater {
70221ac58f5SYang Yingliang struct aspeed_spi *aspi = spi_controller_get_devdata(spi->controller);
7039e264f3fSAmit Kumar Mahapatra via Alsa-devel unsigned int cs = spi_get_chipselect(spi, 0);
704e3228ed9SCédric Le Goater
705e3228ed9SCédric Le Goater aspeed_spi_chip_enable(aspi, cs, false);
706e3228ed9SCédric Le Goater
707e3228ed9SCédric Le Goater dev_dbg(aspi->dev, "CE%d cleanup done\n", cs);
708e3228ed9SCédric Le Goater }
709e3228ed9SCédric Le Goater
aspeed_spi_enable(struct aspeed_spi * aspi,bool enable)710e3228ed9SCédric Le Goater static void aspeed_spi_enable(struct aspeed_spi *aspi, bool enable)
711e3228ed9SCédric Le Goater {
712e3228ed9SCédric Le Goater int cs;
713e3228ed9SCédric Le Goater
714e3228ed9SCédric Le Goater for (cs = 0; cs < aspi->data->max_cs; cs++)
715e3228ed9SCédric Le Goater aspeed_spi_chip_enable(aspi, cs, enable);
716e3228ed9SCédric Le Goater }
717e3228ed9SCédric Le Goater
aspeed_spi_probe(struct platform_device * pdev)718e3228ed9SCédric Le Goater static int aspeed_spi_probe(struct platform_device *pdev)
719e3228ed9SCédric Le Goater {
720e3228ed9SCédric Le Goater struct device *dev = &pdev->dev;
721e3228ed9SCédric Le Goater const struct aspeed_spi_data *data;
722e3228ed9SCédric Le Goater struct spi_controller *ctlr;
723e3228ed9SCédric Le Goater struct aspeed_spi *aspi;
724e3228ed9SCédric Le Goater struct resource *res;
725e3228ed9SCédric Le Goater int ret;
726e3228ed9SCédric Le Goater
727e3228ed9SCédric Le Goater data = of_device_get_match_data(&pdev->dev);
728e3228ed9SCédric Le Goater if (!data)
729e3228ed9SCédric Le Goater return -ENODEV;
730e3228ed9SCédric Le Goater
73121ac58f5SYang Yingliang ctlr = devm_spi_alloc_host(dev, sizeof(*aspi));
732e3228ed9SCédric Le Goater if (!ctlr)
733e3228ed9SCédric Le Goater return -ENOMEM;
734e3228ed9SCédric Le Goater
735e3228ed9SCédric Le Goater aspi = spi_controller_get_devdata(ctlr);
736e3228ed9SCédric Le Goater platform_set_drvdata(pdev, aspi);
737e3228ed9SCédric Le Goater aspi->data = data;
738e3228ed9SCédric Le Goater aspi->dev = dev;
739e3228ed9SCédric Le Goater
7406d0cebbdSYang Yingliang aspi->regs = devm_platform_ioremap_resource(pdev, 0);
74104e0456fSShang XiaoJing if (IS_ERR(aspi->regs))
742e3228ed9SCédric Le Goater return PTR_ERR(aspi->regs);
743e3228ed9SCédric Le Goater
7446d0cebbdSYang Yingliang aspi->ahb_base = devm_platform_get_and_ioremap_resource(pdev, 1, &res);
745e3228ed9SCédric Le Goater if (IS_ERR(aspi->ahb_base)) {
746e3228ed9SCédric Le Goater dev_err(dev, "missing AHB mapping window\n");
747e3228ed9SCédric Le Goater return PTR_ERR(aspi->ahb_base);
748e3228ed9SCédric Le Goater }
749e3228ed9SCédric Le Goater
750e3228ed9SCédric Le Goater aspi->ahb_window_size = resource_size(res);
751e3228ed9SCédric Le Goater aspi->ahb_base_phy = res->start;
752e3228ed9SCédric Le Goater
753e3228ed9SCédric Le Goater aspi->clk = devm_clk_get(&pdev->dev, NULL);
754e3228ed9SCédric Le Goater if (IS_ERR(aspi->clk)) {
755e3228ed9SCédric Le Goater dev_err(dev, "missing clock\n");
756e3228ed9SCédric Le Goater return PTR_ERR(aspi->clk);
757e3228ed9SCédric Le Goater }
758e3228ed9SCédric Le Goater
759e3228ed9SCédric Le Goater aspi->clk_freq = clk_get_rate(aspi->clk);
760e3228ed9SCédric Le Goater if (!aspi->clk_freq) {
761e3228ed9SCédric Le Goater dev_err(dev, "invalid clock\n");
762e3228ed9SCédric Le Goater return -EINVAL;
763e3228ed9SCédric Le Goater }
764e3228ed9SCédric Le Goater
765e3228ed9SCédric Le Goater ret = clk_prepare_enable(aspi->clk);
766e3228ed9SCédric Le Goater if (ret) {
767e3228ed9SCédric Le Goater dev_err(dev, "can not enable the clock\n");
768e3228ed9SCédric Le Goater return ret;
769e3228ed9SCédric Le Goater }
770e3228ed9SCédric Le Goater
771e3228ed9SCédric Le Goater /* IRQ is for DMA, which the driver doesn't support yet */
772e3228ed9SCédric Le Goater
773e3228ed9SCédric Le Goater ctlr->mode_bits = SPI_RX_DUAL | SPI_TX_DUAL | data->mode_bits;
774e3228ed9SCédric Le Goater ctlr->bus_num = pdev->id;
775e3228ed9SCédric Le Goater ctlr->mem_ops = &aspeed_spi_mem_ops;
776e3228ed9SCédric Le Goater ctlr->setup = aspeed_spi_setup;
777e3228ed9SCédric Le Goater ctlr->cleanup = aspeed_spi_cleanup;
778e3228ed9SCédric Le Goater ctlr->num_chipselect = data->max_cs;
779e3228ed9SCédric Le Goater ctlr->dev.of_node = dev->of_node;
780e3228ed9SCédric Le Goater
781e3228ed9SCédric Le Goater ret = devm_spi_register_controller(dev, ctlr);
782e3228ed9SCédric Le Goater if (ret) {
783e3228ed9SCédric Le Goater dev_err(&pdev->dev, "spi_register_controller failed\n");
784e3228ed9SCédric Le Goater goto disable_clk;
785e3228ed9SCédric Le Goater }
786e3228ed9SCédric Le Goater return 0;
787e3228ed9SCédric Le Goater
788e3228ed9SCédric Le Goater disable_clk:
789e3228ed9SCédric Le Goater clk_disable_unprepare(aspi->clk);
790e3228ed9SCédric Le Goater return ret;
791e3228ed9SCédric Le Goater }
792e3228ed9SCédric Le Goater
aspeed_spi_remove(struct platform_device * pdev)793ebf9a50dSUwe Kleine-König static void aspeed_spi_remove(struct platform_device *pdev)
794e3228ed9SCédric Le Goater {
795e3228ed9SCédric Le Goater struct aspeed_spi *aspi = platform_get_drvdata(pdev);
796e3228ed9SCédric Le Goater
797e3228ed9SCédric Le Goater aspeed_spi_enable(aspi, false);
798e3228ed9SCédric Le Goater clk_disable_unprepare(aspi->clk);
799e3228ed9SCédric Le Goater }
800e3228ed9SCédric Le Goater
801e3228ed9SCédric Le Goater /*
802e3228ed9SCédric Le Goater * AHB mappings
803e3228ed9SCédric Le Goater */
804e3228ed9SCédric Le Goater
805e3228ed9SCédric Le Goater /*
806e3228ed9SCédric Le Goater * The Segment Registers of the AST2400 and AST2500 use a 8MB unit.
807e3228ed9SCédric Le Goater * The address range is encoded with absolute addresses in the overall
808e3228ed9SCédric Le Goater * mapping window.
809e3228ed9SCédric Le Goater */
aspeed_spi_segment_start(struct aspeed_spi * aspi,u32 reg)810e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_start(struct aspeed_spi *aspi, u32 reg)
811e3228ed9SCédric Le Goater {
812e3228ed9SCédric Le Goater return ((reg >> 16) & 0xFF) << 23;
813e3228ed9SCédric Le Goater }
814e3228ed9SCédric Le Goater
aspeed_spi_segment_end(struct aspeed_spi * aspi,u32 reg)815e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_end(struct aspeed_spi *aspi, u32 reg)
816e3228ed9SCédric Le Goater {
817e3228ed9SCédric Le Goater return ((reg >> 24) & 0xFF) << 23;
818e3228ed9SCédric Le Goater }
819e3228ed9SCédric Le Goater
aspeed_spi_segment_reg(struct aspeed_spi * aspi,u32 start,u32 end)820e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_reg(struct aspeed_spi *aspi, u32 start, u32 end)
821e3228ed9SCédric Le Goater {
822e3228ed9SCédric Le Goater return (((start >> 23) & 0xFF) << 16) | (((end >> 23) & 0xFF) << 24);
823e3228ed9SCédric Le Goater }
824e3228ed9SCédric Le Goater
825e3228ed9SCédric Le Goater /*
826e3228ed9SCédric Le Goater * The Segment Registers of the AST2600 use a 1MB unit. The address
827e3228ed9SCédric Le Goater * range is encoded with offsets in the overall mapping window.
828e3228ed9SCédric Le Goater */
829e3228ed9SCédric Le Goater
830e3228ed9SCédric Le Goater #define AST2600_SEG_ADDR_MASK 0x0ff00000
831e3228ed9SCédric Le Goater
aspeed_spi_segment_ast2600_start(struct aspeed_spi * aspi,u32 reg)832e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_ast2600_start(struct aspeed_spi *aspi,
833e3228ed9SCédric Le Goater u32 reg)
834e3228ed9SCédric Le Goater {
835e3228ed9SCédric Le Goater u32 start_offset = (reg << 16) & AST2600_SEG_ADDR_MASK;
836e3228ed9SCédric Le Goater
837e3228ed9SCédric Le Goater return aspi->ahb_base_phy + start_offset;
838e3228ed9SCédric Le Goater }
839e3228ed9SCédric Le Goater
aspeed_spi_segment_ast2600_end(struct aspeed_spi * aspi,u32 reg)840e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_ast2600_end(struct aspeed_spi *aspi,
841e3228ed9SCédric Le Goater u32 reg)
842e3228ed9SCédric Le Goater {
843e3228ed9SCédric Le Goater u32 end_offset = reg & AST2600_SEG_ADDR_MASK;
844e3228ed9SCédric Le Goater
845e3228ed9SCédric Le Goater /* segment is disabled */
846e3228ed9SCédric Le Goater if (!end_offset)
847e3228ed9SCédric Le Goater return aspi->ahb_base_phy;
848e3228ed9SCédric Le Goater
849e3228ed9SCédric Le Goater return aspi->ahb_base_phy + end_offset + 0x100000;
850e3228ed9SCédric Le Goater }
851e3228ed9SCédric Le Goater
aspeed_spi_segment_ast2600_reg(struct aspeed_spi * aspi,u32 start,u32 end)852e3228ed9SCédric Le Goater static u32 aspeed_spi_segment_ast2600_reg(struct aspeed_spi *aspi,
853e3228ed9SCédric Le Goater u32 start, u32 end)
854e3228ed9SCédric Le Goater {
855e3228ed9SCédric Le Goater /* disable zero size segments */
856e3228ed9SCédric Le Goater if (start == end)
857e3228ed9SCédric Le Goater return 0;
858e3228ed9SCédric Le Goater
859e3228ed9SCédric Le Goater return ((start & AST2600_SEG_ADDR_MASK) >> 16) |
860e3228ed9SCédric Le Goater ((end - 1) & AST2600_SEG_ADDR_MASK);
861e3228ed9SCédric Le Goater }
862e3228ed9SCédric Le Goater
863e3228ed9SCédric Le Goater /*
864eeaec1eaSCédric Le Goater * Read timing compensation sequences
865eeaec1eaSCédric Le Goater */
866eeaec1eaSCédric Le Goater
867eeaec1eaSCédric Le Goater #define CALIBRATE_BUF_SIZE SZ_16K
868eeaec1eaSCédric Le Goater
aspeed_spi_check_reads(struct aspeed_spi_chip * chip,const u8 * golden_buf,u8 * test_buf)869eeaec1eaSCédric Le Goater static bool aspeed_spi_check_reads(struct aspeed_spi_chip *chip,
870eeaec1eaSCédric Le Goater const u8 *golden_buf, u8 *test_buf)
871eeaec1eaSCédric Le Goater {
872eeaec1eaSCédric Le Goater int i;
873eeaec1eaSCédric Le Goater
874eeaec1eaSCédric Le Goater for (i = 0; i < 10; i++) {
875eeaec1eaSCédric Le Goater memcpy_fromio(test_buf, chip->ahb_base, CALIBRATE_BUF_SIZE);
876eeaec1eaSCédric Le Goater if (memcmp(test_buf, golden_buf, CALIBRATE_BUF_SIZE) != 0) {
877eeaec1eaSCédric Le Goater #if defined(VERBOSE_DEBUG)
878eeaec1eaSCédric Le Goater print_hex_dump_bytes(DEVICE_NAME " fail: ", DUMP_PREFIX_NONE,
879eeaec1eaSCédric Le Goater test_buf, 0x100);
880eeaec1eaSCédric Le Goater #endif
881eeaec1eaSCédric Le Goater return false;
882eeaec1eaSCédric Le Goater }
883eeaec1eaSCédric Le Goater }
884eeaec1eaSCédric Le Goater return true;
885eeaec1eaSCédric Le Goater }
886eeaec1eaSCédric Le Goater
887eeaec1eaSCédric Le Goater #define FREAD_TPASS(i) (((i) / 2) | (((i) & 1) ? 0 : 8))
888eeaec1eaSCédric Le Goater
889eeaec1eaSCédric Le Goater /*
890eeaec1eaSCédric Le Goater * The timing register is shared by all devices. Only update for CE0.
891eeaec1eaSCédric Le Goater */
aspeed_spi_calibrate(struct aspeed_spi_chip * chip,u32 hdiv,const u8 * golden_buf,u8 * test_buf)892eeaec1eaSCédric Le Goater static int aspeed_spi_calibrate(struct aspeed_spi_chip *chip, u32 hdiv,
893eeaec1eaSCédric Le Goater const u8 *golden_buf, u8 *test_buf)
894eeaec1eaSCédric Le Goater {
895eeaec1eaSCédric Le Goater struct aspeed_spi *aspi = chip->aspi;
896eeaec1eaSCédric Le Goater const struct aspeed_spi_data *data = aspi->data;
897eeaec1eaSCédric Le Goater int i;
898eeaec1eaSCédric Le Goater int good_pass = -1, pass_count = 0;
899eeaec1eaSCédric Le Goater u32 shift = (hdiv - 1) << 2;
900eeaec1eaSCédric Le Goater u32 mask = ~(0xfu << shift);
901eeaec1eaSCédric Le Goater u32 fread_timing_val = 0;
902eeaec1eaSCédric Le Goater
903eeaec1eaSCédric Le Goater /* Try HCLK delay 0..5, each one with/without delay and look for a
904eeaec1eaSCédric Le Goater * good pair.
905eeaec1eaSCédric Le Goater */
906eeaec1eaSCédric Le Goater for (i = 0; i < 12; i++) {
907eeaec1eaSCédric Le Goater bool pass;
908eeaec1eaSCédric Le Goater
909eeaec1eaSCédric Le Goater if (chip->cs == 0) {
910eeaec1eaSCédric Le Goater fread_timing_val &= mask;
911eeaec1eaSCédric Le Goater fread_timing_val |= FREAD_TPASS(i) << shift;
912eeaec1eaSCédric Le Goater writel(fread_timing_val, aspi->regs + data->timing);
913eeaec1eaSCédric Le Goater }
914eeaec1eaSCédric Le Goater pass = aspeed_spi_check_reads(chip, golden_buf, test_buf);
915eeaec1eaSCédric Le Goater dev_dbg(aspi->dev,
916eeaec1eaSCédric Le Goater " * [%08x] %d HCLK delay, %dns DI delay : %s",
917eeaec1eaSCédric Le Goater fread_timing_val, i / 2, (i & 1) ? 0 : 4,
918eeaec1eaSCédric Le Goater pass ? "PASS" : "FAIL");
919eeaec1eaSCédric Le Goater if (pass) {
920eeaec1eaSCédric Le Goater pass_count++;
921eeaec1eaSCédric Le Goater if (pass_count == 3) {
922eeaec1eaSCédric Le Goater good_pass = i - 1;
923eeaec1eaSCédric Le Goater break;
924eeaec1eaSCédric Le Goater }
925eeaec1eaSCédric Le Goater } else {
926eeaec1eaSCédric Le Goater pass_count = 0;
927eeaec1eaSCédric Le Goater }
928eeaec1eaSCédric Le Goater }
929eeaec1eaSCédric Le Goater
930eeaec1eaSCédric Le Goater /* No good setting for this frequency */
931eeaec1eaSCédric Le Goater if (good_pass < 0)
932eeaec1eaSCédric Le Goater return -1;
933eeaec1eaSCédric Le Goater
934eeaec1eaSCédric Le Goater /* We have at least one pass of margin, let's use first pass */
935eeaec1eaSCédric Le Goater if (chip->cs == 0) {
936eeaec1eaSCédric Le Goater fread_timing_val &= mask;
937eeaec1eaSCédric Le Goater fread_timing_val |= FREAD_TPASS(good_pass) << shift;
938eeaec1eaSCédric Le Goater writel(fread_timing_val, aspi->regs + data->timing);
939eeaec1eaSCédric Le Goater }
940eeaec1eaSCédric Le Goater dev_dbg(aspi->dev, " * -> good is pass %d [0x%08x]",
941eeaec1eaSCédric Le Goater good_pass, fread_timing_val);
942eeaec1eaSCédric Le Goater return 0;
943eeaec1eaSCédric Le Goater }
944eeaec1eaSCédric Le Goater
aspeed_spi_check_calib_data(const u8 * test_buf,u32 size)945eeaec1eaSCédric Le Goater static bool aspeed_spi_check_calib_data(const u8 *test_buf, u32 size)
946eeaec1eaSCédric Le Goater {
947eeaec1eaSCédric Le Goater const u32 *tb32 = (const u32 *)test_buf;
948eeaec1eaSCédric Le Goater u32 i, cnt = 0;
949eeaec1eaSCédric Le Goater
950eeaec1eaSCédric Le Goater /* We check if we have enough words that are neither all 0
951eeaec1eaSCédric Le Goater * nor all 1's so the calibration can be considered valid.
952eeaec1eaSCédric Le Goater *
953eeaec1eaSCédric Le Goater * I use an arbitrary threshold for now of 64
954eeaec1eaSCédric Le Goater */
955eeaec1eaSCédric Le Goater size >>= 2;
956eeaec1eaSCédric Le Goater for (i = 0; i < size; i++) {
957eeaec1eaSCédric Le Goater if (tb32[i] != 0 && tb32[i] != 0xffffffff)
958eeaec1eaSCédric Le Goater cnt++;
959eeaec1eaSCédric Le Goater }
960eeaec1eaSCédric Le Goater return cnt >= 64;
961eeaec1eaSCédric Le Goater }
962eeaec1eaSCédric Le Goater
963eeaec1eaSCédric Le Goater static const u32 aspeed_spi_hclk_divs[] = {
964eeaec1eaSCédric Le Goater 0xf, /* HCLK */
965eeaec1eaSCédric Le Goater 0x7, /* HCLK/2 */
966eeaec1eaSCédric Le Goater 0xe, /* HCLK/3 */
967eeaec1eaSCédric Le Goater 0x6, /* HCLK/4 */
968eeaec1eaSCédric Le Goater 0xd, /* HCLK/5 */
969eeaec1eaSCédric Le Goater };
970eeaec1eaSCédric Le Goater
971eeaec1eaSCédric Le Goater #define ASPEED_SPI_HCLK_DIV(i) \
972eeaec1eaSCédric Le Goater (aspeed_spi_hclk_divs[(i) - 1] << CTRL_FREQ_SEL_SHIFT)
973eeaec1eaSCédric Le Goater
aspeed_spi_do_calibration(struct aspeed_spi_chip * chip)974eeaec1eaSCédric Le Goater static int aspeed_spi_do_calibration(struct aspeed_spi_chip *chip)
975eeaec1eaSCédric Le Goater {
976eeaec1eaSCédric Le Goater struct aspeed_spi *aspi = chip->aspi;
977eeaec1eaSCédric Le Goater const struct aspeed_spi_data *data = aspi->data;
978eeaec1eaSCédric Le Goater u32 ahb_freq = aspi->clk_freq;
979eeaec1eaSCédric Le Goater u32 max_freq = chip->clk_freq;
980eeaec1eaSCédric Le Goater u32 ctl_val;
981eeaec1eaSCédric Le Goater u8 *golden_buf = NULL;
982eeaec1eaSCédric Le Goater u8 *test_buf = NULL;
983eeaec1eaSCédric Le Goater int i, rc, best_div = -1;
984eeaec1eaSCédric Le Goater
985eeaec1eaSCédric Le Goater dev_dbg(aspi->dev, "calculate timing compensation - AHB freq: %d MHz",
986eeaec1eaSCédric Le Goater ahb_freq / 1000000);
987eeaec1eaSCédric Le Goater
988eeaec1eaSCédric Le Goater /*
989eeaec1eaSCédric Le Goater * use the related low frequency to get check calibration data
990eeaec1eaSCédric Le Goater * and get golden data.
991eeaec1eaSCédric Le Goater */
992eeaec1eaSCédric Le Goater ctl_val = chip->ctl_val[ASPEED_SPI_READ] & data->hclk_mask;
993eeaec1eaSCédric Le Goater writel(ctl_val, chip->ctl);
994eeaec1eaSCédric Le Goater
995eeaec1eaSCédric Le Goater test_buf = kzalloc(CALIBRATE_BUF_SIZE * 2, GFP_KERNEL);
996eeaec1eaSCédric Le Goater if (!test_buf)
997eeaec1eaSCédric Le Goater return -ENOMEM;
998eeaec1eaSCédric Le Goater
999eeaec1eaSCédric Le Goater golden_buf = test_buf + CALIBRATE_BUF_SIZE;
1000eeaec1eaSCédric Le Goater
1001eeaec1eaSCédric Le Goater memcpy_fromio(golden_buf, chip->ahb_base, CALIBRATE_BUF_SIZE);
1002eeaec1eaSCédric Le Goater if (!aspeed_spi_check_calib_data(golden_buf, CALIBRATE_BUF_SIZE)) {
1003eeaec1eaSCédric Le Goater dev_info(aspi->dev, "Calibration area too uniform, using low speed");
1004eeaec1eaSCédric Le Goater goto no_calib;
1005eeaec1eaSCédric Le Goater }
1006eeaec1eaSCédric Le Goater
1007eeaec1eaSCédric Le Goater #if defined(VERBOSE_DEBUG)
1008eeaec1eaSCédric Le Goater print_hex_dump_bytes(DEVICE_NAME " good: ", DUMP_PREFIX_NONE,
1009eeaec1eaSCédric Le Goater golden_buf, 0x100);
1010eeaec1eaSCédric Le Goater #endif
1011eeaec1eaSCédric Le Goater
1012eeaec1eaSCédric Le Goater /* Now we iterate the HCLK dividers until we find our breaking point */
1013eeaec1eaSCédric Le Goater for (i = ARRAY_SIZE(aspeed_spi_hclk_divs); i > data->hdiv_max - 1; i--) {
1014eeaec1eaSCédric Le Goater u32 tv, freq;
1015eeaec1eaSCédric Le Goater
1016eeaec1eaSCédric Le Goater freq = ahb_freq / i;
1017eeaec1eaSCédric Le Goater if (freq > max_freq)
1018eeaec1eaSCédric Le Goater continue;
1019eeaec1eaSCédric Le Goater
1020eeaec1eaSCédric Le Goater /* Set the timing */
1021eeaec1eaSCédric Le Goater tv = chip->ctl_val[ASPEED_SPI_READ] | ASPEED_SPI_HCLK_DIV(i);
1022eeaec1eaSCédric Le Goater writel(tv, chip->ctl);
1023eeaec1eaSCédric Le Goater dev_dbg(aspi->dev, "Trying HCLK/%d [%08x] ...", i, tv);
1024eeaec1eaSCédric Le Goater rc = data->calibrate(chip, i, golden_buf, test_buf);
1025eeaec1eaSCédric Le Goater if (rc == 0)
1026eeaec1eaSCédric Le Goater best_div = i;
1027eeaec1eaSCédric Le Goater }
1028eeaec1eaSCédric Le Goater
1029eeaec1eaSCédric Le Goater /* Nothing found ? */
1030eeaec1eaSCédric Le Goater if (best_div < 0) {
1031eeaec1eaSCédric Le Goater dev_warn(aspi->dev, "No good frequency, using dumb slow");
1032eeaec1eaSCédric Le Goater } else {
1033eeaec1eaSCédric Le Goater dev_dbg(aspi->dev, "Found good read timings at HCLK/%d", best_div);
1034eeaec1eaSCédric Le Goater
1035eeaec1eaSCédric Le Goater /* Record the freq */
1036eeaec1eaSCédric Le Goater for (i = 0; i < ASPEED_SPI_MAX; i++)
1037eeaec1eaSCédric Le Goater chip->ctl_val[i] = (chip->ctl_val[i] & data->hclk_mask) |
1038eeaec1eaSCédric Le Goater ASPEED_SPI_HCLK_DIV(best_div);
1039eeaec1eaSCédric Le Goater }
1040eeaec1eaSCédric Le Goater
1041eeaec1eaSCédric Le Goater no_calib:
1042eeaec1eaSCédric Le Goater writel(chip->ctl_val[ASPEED_SPI_READ], chip->ctl);
1043eeaec1eaSCédric Le Goater kfree(test_buf);
1044eeaec1eaSCédric Le Goater return 0;
1045eeaec1eaSCédric Le Goater }
1046eeaec1eaSCédric Le Goater
1047eeaec1eaSCédric Le Goater #define TIMING_DELAY_DI BIT(3)
1048eeaec1eaSCédric Le Goater #define TIMING_DELAY_HCYCLE_MAX 5
1049eeaec1eaSCédric Le Goater #define TIMING_REG_AST2600(chip) \
1050eeaec1eaSCédric Le Goater ((chip)->aspi->regs + (chip)->aspi->data->timing + \
1051eeaec1eaSCédric Le Goater (chip)->cs * 4)
1052eeaec1eaSCédric Le Goater
aspeed_spi_ast2600_calibrate(struct aspeed_spi_chip * chip,u32 hdiv,const u8 * golden_buf,u8 * test_buf)1053eeaec1eaSCédric Le Goater static int aspeed_spi_ast2600_calibrate(struct aspeed_spi_chip *chip, u32 hdiv,
1054eeaec1eaSCédric Le Goater const u8 *golden_buf, u8 *test_buf)
1055eeaec1eaSCédric Le Goater {
1056eeaec1eaSCédric Le Goater struct aspeed_spi *aspi = chip->aspi;
1057eeaec1eaSCédric Le Goater int hcycle;
1058eeaec1eaSCédric Le Goater u32 shift = (hdiv - 2) << 3;
1059eeaec1eaSCédric Le Goater u32 mask = ~(0xfu << shift);
1060eeaec1eaSCédric Le Goater u32 fread_timing_val = 0;
1061eeaec1eaSCédric Le Goater
1062eeaec1eaSCédric Le Goater for (hcycle = 0; hcycle <= TIMING_DELAY_HCYCLE_MAX; hcycle++) {
1063eeaec1eaSCédric Le Goater int delay_ns;
1064eeaec1eaSCédric Le Goater bool pass = false;
1065eeaec1eaSCédric Le Goater
1066eeaec1eaSCédric Le Goater fread_timing_val &= mask;
1067eeaec1eaSCédric Le Goater fread_timing_val |= hcycle << shift;
1068eeaec1eaSCédric Le Goater
1069eeaec1eaSCédric Le Goater /* no DI input delay first */
1070eeaec1eaSCédric Le Goater writel(fread_timing_val, TIMING_REG_AST2600(chip));
1071eeaec1eaSCédric Le Goater pass = aspeed_spi_check_reads(chip, golden_buf, test_buf);
1072eeaec1eaSCédric Le Goater dev_dbg(aspi->dev,
1073eeaec1eaSCédric Le Goater " * [%08x] %d HCLK delay, DI delay none : %s",
1074eeaec1eaSCédric Le Goater fread_timing_val, hcycle, pass ? "PASS" : "FAIL");
1075eeaec1eaSCédric Le Goater if (pass)
1076eeaec1eaSCédric Le Goater return 0;
1077eeaec1eaSCédric Le Goater
1078eeaec1eaSCédric Le Goater /* Add DI input delays */
1079eeaec1eaSCédric Le Goater fread_timing_val &= mask;
1080eeaec1eaSCédric Le Goater fread_timing_val |= (TIMING_DELAY_DI | hcycle) << shift;
1081eeaec1eaSCédric Le Goater
1082eeaec1eaSCédric Le Goater for (delay_ns = 0; delay_ns < 0x10; delay_ns++) {
1083eeaec1eaSCédric Le Goater fread_timing_val &= ~(0xf << (4 + shift));
1084eeaec1eaSCédric Le Goater fread_timing_val |= delay_ns << (4 + shift);
1085eeaec1eaSCédric Le Goater
1086eeaec1eaSCédric Le Goater writel(fread_timing_val, TIMING_REG_AST2600(chip));
1087eeaec1eaSCédric Le Goater pass = aspeed_spi_check_reads(chip, golden_buf, test_buf);
1088eeaec1eaSCédric Le Goater dev_dbg(aspi->dev,
1089eeaec1eaSCédric Le Goater " * [%08x] %d HCLK delay, DI delay %d.%dns : %s",
1090eeaec1eaSCédric Le Goater fread_timing_val, hcycle, (delay_ns + 1) / 2,
1091eeaec1eaSCédric Le Goater (delay_ns + 1) & 1 ? 5 : 5, pass ? "PASS" : "FAIL");
1092eeaec1eaSCédric Le Goater /*
1093eeaec1eaSCédric Le Goater * TODO: This is optimistic. We should look
1094eeaec1eaSCédric Le Goater * for a working interval and save the middle
1095eeaec1eaSCédric Le Goater * value in the read timing register.
1096eeaec1eaSCédric Le Goater */
1097eeaec1eaSCédric Le Goater if (pass)
1098eeaec1eaSCédric Le Goater return 0;
1099eeaec1eaSCédric Le Goater }
1100eeaec1eaSCédric Le Goater }
1101eeaec1eaSCédric Le Goater
1102eeaec1eaSCédric Le Goater /* No good setting for this frequency */
1103eeaec1eaSCédric Le Goater return -1;
1104eeaec1eaSCédric Le Goater }
1105eeaec1eaSCédric Le Goater
1106eeaec1eaSCédric Le Goater /*
1107e3228ed9SCédric Le Goater * Platform definitions
1108e3228ed9SCédric Le Goater */
1109e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2400_fmc_data = {
1110e3228ed9SCédric Le Goater .max_cs = 5,
1111e3228ed9SCédric Le Goater .hastype = true,
1112e3228ed9SCédric Le Goater .we0 = 16,
1113e3228ed9SCédric Le Goater .ctl0 = CE0_CTRL_REG,
1114eeaec1eaSCédric Le Goater .timing = CE0_TIMING_COMPENSATION_REG,
1115eeaec1eaSCédric Le Goater .hclk_mask = 0xfffff0ff,
1116eeaec1eaSCédric Le Goater .hdiv_max = 1,
1117eeaec1eaSCédric Le Goater .calibrate = aspeed_spi_calibrate,
1118e3228ed9SCédric Le Goater .segment_start = aspeed_spi_segment_start,
1119e3228ed9SCédric Le Goater .segment_end = aspeed_spi_segment_end,
1120e3228ed9SCédric Le Goater .segment_reg = aspeed_spi_segment_reg,
1121e3228ed9SCédric Le Goater };
1122e3228ed9SCédric Le Goater
112353526ab2SCédric Le Goater static const struct aspeed_spi_data ast2400_spi_data = {
112453526ab2SCédric Le Goater .max_cs = 1,
112553526ab2SCédric Le Goater .hastype = false,
112653526ab2SCédric Le Goater .we0 = 0,
112753526ab2SCédric Le Goater .ctl0 = 0x04,
1128eeaec1eaSCédric Le Goater .timing = 0x14,
1129eeaec1eaSCédric Le Goater .hclk_mask = 0xfffff0ff,
1130eeaec1eaSCédric Le Goater .hdiv_max = 1,
1131eeaec1eaSCédric Le Goater .calibrate = aspeed_spi_calibrate,
113253526ab2SCédric Le Goater /* No segment registers */
113353526ab2SCédric Le Goater };
113453526ab2SCédric Le Goater
1135e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2500_fmc_data = {
1136e3228ed9SCédric Le Goater .max_cs = 3,
1137e3228ed9SCédric Le Goater .hastype = true,
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 ast2500_spi_data = {
1150e3228ed9SCédric Le Goater .max_cs = 2,
1151e3228ed9SCédric Le Goater .hastype = false,
1152e3228ed9SCédric Le Goater .we0 = 16,
1153e3228ed9SCédric Le Goater .ctl0 = CE0_CTRL_REG,
1154eeaec1eaSCédric Le Goater .timing = CE0_TIMING_COMPENSATION_REG,
1155eeaec1eaSCédric Le Goater .hclk_mask = 0xffffd0ff,
1156eeaec1eaSCédric Le Goater .hdiv_max = 1,
1157eeaec1eaSCédric Le Goater .calibrate = aspeed_spi_calibrate,
1158e3228ed9SCédric Le Goater .segment_start = aspeed_spi_segment_start,
1159e3228ed9SCédric Le Goater .segment_end = aspeed_spi_segment_end,
1160e3228ed9SCédric Le Goater .segment_reg = aspeed_spi_segment_reg,
1161e3228ed9SCédric Le Goater };
1162e3228ed9SCédric Le Goater
1163e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2600_fmc_data = {
1164e3228ed9SCédric Le Goater .max_cs = 3,
1165e3228ed9SCédric Le Goater .hastype = false,
11665302e1ffSChin-Ting Kuo .mode_bits = SPI_RX_QUAD | SPI_TX_QUAD,
1167e3228ed9SCédric Le Goater .we0 = 16,
1168e3228ed9SCédric Le Goater .ctl0 = CE0_CTRL_REG,
1169eeaec1eaSCédric Le Goater .timing = CE0_TIMING_COMPENSATION_REG,
1170eeaec1eaSCédric Le Goater .hclk_mask = 0xf0fff0ff,
1171eeaec1eaSCédric Le Goater .hdiv_max = 2,
1172eeaec1eaSCédric Le Goater .calibrate = aspeed_spi_ast2600_calibrate,
1173e3228ed9SCédric Le Goater .segment_start = aspeed_spi_segment_ast2600_start,
1174e3228ed9SCédric Le Goater .segment_end = aspeed_spi_segment_ast2600_end,
1175e3228ed9SCédric Le Goater .segment_reg = aspeed_spi_segment_ast2600_reg,
1176e3228ed9SCédric Le Goater };
1177e3228ed9SCédric Le Goater
1178e3228ed9SCédric Le Goater static const struct aspeed_spi_data ast2600_spi_data = {
1179e3228ed9SCédric Le Goater .max_cs = 2,
1180e3228ed9SCédric Le Goater .hastype = false,
11815302e1ffSChin-Ting Kuo .mode_bits = SPI_RX_QUAD | SPI_TX_QUAD,
1182e3228ed9SCédric Le Goater .we0 = 16,
1183e3228ed9SCédric Le Goater .ctl0 = CE0_CTRL_REG,
1184eeaec1eaSCédric Le Goater .timing = CE0_TIMING_COMPENSATION_REG,
1185eeaec1eaSCédric Le Goater .hclk_mask = 0xf0fff0ff,
1186eeaec1eaSCédric Le Goater .hdiv_max = 2,
1187eeaec1eaSCédric Le Goater .calibrate = aspeed_spi_ast2600_calibrate,
1188e3228ed9SCédric Le Goater .segment_start = aspeed_spi_segment_ast2600_start,
1189e3228ed9SCédric Le Goater .segment_end = aspeed_spi_segment_ast2600_end,
1190e3228ed9SCédric Le Goater .segment_reg = aspeed_spi_segment_ast2600_reg,
1191e3228ed9SCédric Le Goater };
1192e3228ed9SCédric Le Goater
1193e3228ed9SCédric Le Goater static const struct of_device_id aspeed_spi_matches[] = {
1194e3228ed9SCédric Le Goater { .compatible = "aspeed,ast2400-fmc", .data = &ast2400_fmc_data },
119553526ab2SCédric Le Goater { .compatible = "aspeed,ast2400-spi", .data = &ast2400_spi_data },
1196e3228ed9SCédric Le Goater { .compatible = "aspeed,ast2500-fmc", .data = &ast2500_fmc_data },
1197e3228ed9SCédric Le Goater { .compatible = "aspeed,ast2500-spi", .data = &ast2500_spi_data },
1198e3228ed9SCédric Le Goater { .compatible = "aspeed,ast2600-fmc", .data = &ast2600_fmc_data },
1199e3228ed9SCédric Le Goater { .compatible = "aspeed,ast2600-spi", .data = &ast2600_spi_data },
1200e3228ed9SCédric Le Goater { }
1201e3228ed9SCédric Le Goater };
1202e3228ed9SCédric Le Goater MODULE_DEVICE_TABLE(of, aspeed_spi_matches);
1203e3228ed9SCédric Le Goater
1204e3228ed9SCédric Le Goater static struct platform_driver aspeed_spi_driver = {
1205e3228ed9SCédric Le Goater .probe = aspeed_spi_probe,
1206ebf9a50dSUwe Kleine-König .remove_new = aspeed_spi_remove,
1207e3228ed9SCédric Le Goater .driver = {
1208e3228ed9SCédric Le Goater .name = DEVICE_NAME,
1209e3228ed9SCédric Le Goater .of_match_table = aspeed_spi_matches,
1210e3228ed9SCédric Le Goater }
1211e3228ed9SCédric Le Goater };
1212e3228ed9SCédric Le Goater
1213e3228ed9SCédric Le Goater module_platform_driver(aspeed_spi_driver);
1214e3228ed9SCédric Le Goater
1215e3228ed9SCédric Le Goater MODULE_DESCRIPTION("ASPEED Static Memory Controller Driver");
1216e3228ed9SCédric Le Goater MODULE_AUTHOR("Chin-Ting Kuo <chin-ting_kuo@aspeedtech.com>");
1217e3228ed9SCédric Le Goater MODULE_AUTHOR("Cedric Le Goater <clg@kaod.org>");
1218e3228ed9SCédric Le Goater MODULE_LICENSE("GPL v2");
1219