blob: 234b168827261547642112bad334cdcc565fe94a [file] [log] [blame]
Padmarao Begari32678b02022-10-27 11:32:01 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2022 Microchip Technology Inc.
4 * Padmarao Begari <padmarao.begari@microchip.com>
5 * Naga Sureshkumar Relli <nagasuresh.relli@microchip.com>
6 */
7
Padmarao Begari32678b02022-10-27 11:32:01 +05308#include <clk.h>
9#include <dm.h>
10#include <log.h>
11#include <malloc.h>
12#include <spi.h>
13#include <spi-mem.h>
14#include <asm/io.h>
15#include <linux/bitops.h>
16#include <linux/delay.h>
17#include <linux/types.h>
18#include <linux/sizes.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22/*
23 * QSPI Control register mask defines
24 */
25#define CONTROL_ENABLE BIT(0)
26#define CONTROL_MASTER BIT(1)
27#define CONTROL_XIP BIT(2)
28#define CONTROL_XIPADDR BIT(3)
29#define CONTROL_CLKIDLE BIT(10)
30#define CONTROL_SAMPLE_MASK GENMASK(12, 11)
31#define CONTROL_MODE0 BIT(13)
32#define CONTROL_MODE12_MASK GENMASK(15, 14)
33#define CONTROL_MODE12_EX_RO BIT(14)
34#define CONTROL_MODE12_EX_RW BIT(15)
35#define CONTROL_MODE12_FULL GENMASK(15, 14)
36#define CONTROL_FLAGSX4 BIT(16)
37#define CONTROL_CLKRATE_MASK GENMASK(27, 24)
38#define CONTROL_CLKRATE_SHIFT 24
39
40/*
41 * QSPI Frames register mask defines
42 */
43#define FRAMES_TOTALBYTES_MASK GENMASK(15, 0)
44#define FRAMES_CMDBYTES_MASK GENMASK(24, 16)
45#define FRAMES_CMDBYTES_SHIFT 16
46#define FRAMES_SHIFT 25
47#define FRAMES_IDLE_MASK GENMASK(29, 26)
48#define FRAMES_IDLE_SHIFT 26
49#define FRAMES_FLAGBYTE BIT(30)
50#define FRAMES_FLAGWORD BIT(31)
51
52/*
53 * QSPI Interrupt Enable register mask defines
54 */
55#define IEN_TXDONE BIT(0)
56#define IEN_RXDONE BIT(1)
57#define IEN_RXAVAILABLE BIT(2)
58#define IEN_TXAVAILABLE BIT(3)
59#define IEN_RXFIFOEMPTY BIT(4)
60#define IEN_TXFIFOFULL BIT(5)
61
62/*
63 * QSPI Status register mask defines
64 */
65#define STATUS_TXDONE BIT(0)
66#define STATUS_RXDONE BIT(1)
67#define STATUS_RXAVAILABLE BIT(2)
68#define STATUS_TXAVAILABLE BIT(3)
69#define STATUS_RXFIFOEMPTY BIT(4)
70#define STATUS_TXFIFOFULL BIT(5)
71#define STATUS_READY BIT(7)
72#define STATUS_FLAGSX4 BIT(8)
73#define STATUS_MASK GENMASK(8, 0)
74
75#define BYTESUPPER_MASK GENMASK(31, 16)
76#define BYTESLOWER_MASK GENMASK(15, 0)
77
78#define MAX_DIVIDER 16
79#define MIN_DIVIDER 0
80#define MAX_DATA_CMD_LEN 256
81
82/* QSPI ready time out value */
83#define TIMEOUT_MS (1000 * 500)
84
85/*
86 * QSPI Register offsets.
87 */
88#define REG_CONTROL (0x00)
89#define REG_FRAMES (0x04)
90#define REG_IEN (0x0c)
91#define REG_STATUS (0x10)
92#define REG_DIRECT_ACCESS (0x14)
93#define REG_UPPER_ACCESS (0x18)
94#define REG_RX_DATA (0x40)
95#define REG_TX_DATA (0x44)
96#define REG_X4_RX_DATA (0x48)
97#define REG_X4_TX_DATA (0x4c)
98#define REG_FRAMESUP (0x50)
99
100/**
101 * struct mchp_coreqspi - Defines qspi driver instance
102 * @regs: Address of the QSPI controller registers
103 * @freq: QSPI Input frequency
104 * @txbuf: TX buffer
105 * @rxbuf: RX buffer
106 * @tx_len: Number of bytes left to transfer
107 * @rx_len: Number of bytes left to receive
108 */
109struct mchp_coreqspi {
110 void __iomem *regs;
111 u32 freq;
112 u8 *txbuf;
113 u8 *rxbuf;
114 int tx_len;
115 int rx_len;
116};
117
118static void mchp_coreqspi_init_hw(struct mchp_coreqspi *qspi)
119{
120 u32 control;
121
122 control = CONTROL_CLKIDLE | CONTROL_ENABLE;
123
124 writel(control, qspi->regs + REG_CONTROL);
125 writel(0, qspi->regs + REG_IEN);
126}
127
128static inline void mchp_coreqspi_read_op(struct mchp_coreqspi *qspi)
129{
130 u32 control, data;
131
132 if (!qspi->rx_len)
133 return;
134
135 control = readl(qspi->regs + REG_CONTROL);
136
137 /*
138 * Read 4-bytes from the SPI FIFO in single transaction and then read
139 * the reamaining data byte wise.
140 */
141 control |= CONTROL_FLAGSX4;
142 writel(control, qspi->regs + REG_CONTROL);
143
144 while (qspi->rx_len >= 4) {
145 while (readl(qspi->regs + REG_STATUS) & STATUS_RXFIFOEMPTY)
146 ;
147 data = readl(qspi->regs + REG_X4_RX_DATA);
148 *(u32 *)qspi->rxbuf = data;
149 qspi->rxbuf += 4;
150 qspi->rx_len -= 4;
151 }
152
153 control &= ~CONTROL_FLAGSX4;
154 writel(control, qspi->regs + REG_CONTROL);
155
156 while (qspi->rx_len--) {
157 while (readl(qspi->regs + REG_STATUS) & STATUS_RXFIFOEMPTY)
158 ;
159 data = readl(qspi->regs + REG_RX_DATA);
160 *qspi->rxbuf++ = (data & 0xFF);
161 }
162}
163
164static inline void mchp_coreqspi_write_op(struct mchp_coreqspi *qspi, bool word)
165{
166 u32 control, data;
167
168 control = readl(qspi->regs + REG_CONTROL);
169 control |= CONTROL_FLAGSX4;
170 writel(control, qspi->regs + REG_CONTROL);
171
172 while (qspi->tx_len >= 4) {
173 while (readl(qspi->regs + REG_STATUS) & STATUS_TXFIFOFULL)
174 ;
175 data = *(u32 *)qspi->txbuf;
176 qspi->txbuf += 4;
177 qspi->tx_len -= 4;
178 writel(data, qspi->regs + REG_X4_TX_DATA);
179 }
180
181 control &= ~CONTROL_FLAGSX4;
182 writel(control, qspi->regs + REG_CONTROL);
183
184 while (qspi->tx_len--) {
185 while (readl(qspi->regs + REG_STATUS) & STATUS_TXFIFOFULL)
186 ;
187 data = *qspi->txbuf++;
188 writel(data, qspi->regs + REG_TX_DATA);
189 }
190}
191
192static inline void mchp_coreqspi_config_op(struct mchp_coreqspi *qspi,
193 const struct spi_mem_op *op)
194{
195 u32 idle_cycles = 0;
196 int total_bytes, cmd_bytes, frames, ctrl;
197
198 cmd_bytes = op->cmd.nbytes + op->addr.nbytes;
199 total_bytes = cmd_bytes + op->data.nbytes;
200
201 /*
202 * As per the coreQSPI IP spec,the number of command and data bytes are
203 * controlled by the frames register for each SPI sequence. This supports
204 * the SPI flash memory read and writes sequences as below. so configure
205 * the cmd and total bytes accordingly.
206 * ---------------------------------------------------------------------
207 * TOTAL BYTES | CMD BYTES | What happens |
208 * ______________________________________________________________________
209 * | | |
210 * 1 | 1 | The SPI core will transmit a single byte |
211 * | | and receive data is discarded |
212 * | | |
213 * 1 | 0 | The SPI core will transmit a single byte |
214 * | | and return a single byte |
215 * | | |
216 * 10 | 4 | The SPI core will transmit 4 command |
217 * | | bytes discarding the receive data and |
218 * | | transmits 6 dummy bytes returning the 6 |
219 * | | received bytes and return a single byte |
220 * | | |
221 * 10 | 10 | The SPI core will transmit 10 command |
222 * | | |
223 * 10 | 0 | The SPI core will transmit 10 command |
224 * | | bytes and returning 10 received bytes |
225 * ______________________________________________________________________
226 */
227
228 if (!(op->data.dir == SPI_MEM_DATA_IN))
229 cmd_bytes = total_bytes;
230
231 frames = total_bytes & BYTESUPPER_MASK;
232 writel(frames, qspi->regs + REG_FRAMESUP);
233 frames = total_bytes & BYTESLOWER_MASK;
234 frames |= cmd_bytes << FRAMES_CMDBYTES_SHIFT;
235
236 if (op->dummy.buswidth)
237 idle_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
238
239 frames |= idle_cycles << FRAMES_IDLE_SHIFT;
240 ctrl = readl(qspi->regs + REG_CONTROL);
241
242 if (ctrl & CONTROL_MODE12_MASK)
243 frames |= (1 << FRAMES_SHIFT);
244
245 frames |= FRAMES_FLAGWORD;
246 writel(frames, qspi->regs + REG_FRAMES);
247}
248
249static int mchp_coreqspi_wait_for_ready(struct spi_slave *slave)
250{
251 struct mchp_coreqspi *qspi = dev_get_priv(slave->dev->parent);
252 unsigned long count = 0;
253
254 while (1) {
255 if (readl(qspi->regs + REG_STATUS) & STATUS_READY)
256 return 0;
257
258 udelay(1);
259 count += 1;
260 if (count == TIMEOUT_MS)
261 return -ETIMEDOUT;
262 }
263}
264
265static int mchp_coreqspi_set_operate_mode(struct mchp_coreqspi *qspi,
266 const struct spi_mem_op *op)
267{
268 u32 control = readl(qspi->regs + REG_CONTROL);
269
270 /*
271 * The operating mode can be configured based on the command that needs
272 * to be send.
273 * bits[15:14]: Sets whether multiple bit SPI operates in normal,
274 * extended or full modes.
275 * 00: Normal (single DQ0 TX and single DQ1 RX lines)
276 * 01: Extended RO (command and address bytes on DQ0 only)
277 * 10: Extended RW (command byte on DQ0 only)
278 * 11: Full. (command and address are on all DQ lines)
279 * bit[13]: Sets whether multiple bit SPI uses 2 or 4 bits of data
280 * 0: 2-bits (BSPI)
281 * 1: 4-bits (QSPI)
282 */
283 if (op->data.buswidth == 4 || op->data.buswidth == 2) {
284 control &= ~CONTROL_MODE12_MASK;
285 if (op->cmd.buswidth == 1 && (op->addr.buswidth == 1 ||
286 op->addr.buswidth == 0))
287 control |= CONTROL_MODE12_EX_RO;
288 else if (op->cmd.buswidth == 1)
289 control |= CONTROL_MODE12_EX_RW;
290 else
291 control |= CONTROL_MODE12_FULL;
292
293 control |= CONTROL_MODE0;
294 } else {
295 control &= ~(CONTROL_MODE12_MASK | CONTROL_MODE0);
296 }
297
298 writel(control, qspi->regs + REG_CONTROL);
299
300 return 0;
301}
302
303static int mchp_coreqspi_exec_op(struct spi_slave *slave,
304 const struct spi_mem_op *op)
305{
306 struct mchp_coreqspi *qspi = dev_get_priv(slave->dev->parent);
307
308 u32 address = op->addr.val;
309 u8 opcode = op->cmd.opcode;
310 u8 opaddr[5];
311 int err = 0, i;
312
313 err = mchp_coreqspi_wait_for_ready(slave);
314 if (err)
315 return err;
316
317 err = mchp_coreqspi_set_operate_mode(qspi, op);
318 if (err)
319 return err;
320
321 mchp_coreqspi_config_op(qspi, op);
322 if (op->cmd.opcode) {
323 qspi->txbuf = &opcode;
324 qspi->rxbuf = NULL;
325 qspi->tx_len = op->cmd.nbytes;
326 qspi->rx_len = 0;
327 mchp_coreqspi_write_op(qspi, false);
328 }
329
330 qspi->txbuf = &opaddr[0];
331 if (op->addr.nbytes) {
332 for (i = 0; i < op->addr.nbytes; i++)
333 qspi->txbuf[i] = address >> (8 * (op->addr.nbytes - i - 1));
334
335 qspi->rxbuf = NULL;
336 qspi->tx_len = op->addr.nbytes;
337 qspi->rx_len = 0;
338 mchp_coreqspi_write_op(qspi, false);
339 }
340
341 if (op->data.nbytes) {
342 if (op->data.dir == SPI_MEM_DATA_OUT) {
343 qspi->txbuf = (u8 *)op->data.buf.out;
344 qspi->rxbuf = NULL;
345 qspi->rx_len = 0;
346 qspi->tx_len = op->data.nbytes;
347 mchp_coreqspi_write_op(qspi, true);
348 } else {
349 qspi->txbuf = NULL;
350 qspi->rxbuf = (u8 *)op->data.buf.in;
351 qspi->rx_len = op->data.nbytes;
352 qspi->tx_len = 0;
353 mchp_coreqspi_read_op(qspi);
354 }
355 }
356
357 return 0;
358}
359
360static bool mchp_coreqspi_supports_op(struct spi_slave *slave,
361 const struct spi_mem_op *op)
362{
363 if (!spi_mem_default_supports_op(slave, op))
364 return false;
365
366 if ((op->data.buswidth == 4 || op->data.buswidth == 2) &&
367 (op->cmd.buswidth == 1 && (op->addr.buswidth == 1 ||
368 op->addr.buswidth == 0))) {
369 /*
370 * If the command and address are on DQ0 only, then this
371 * controller doesn't support sending data on dual and
372 * quad lines. but it supports reading data on dual and
373 * quad lines with same configuration as command and
374 * address on DQ0.
375 * i.e. The control register[15:13] :EX_RO(read only) is
376 * meant only for the command and address are on DQ0 but
377 * not to write data, it is just to read.
378 * Ex: 0x34h is Quad Load Program Data which is not
379 * supported. Then the spi-mem layer will iterate over
380 * each command and it will chose the supported one.
381 */
382 if (op->data.dir == SPI_MEM_DATA_OUT)
383 return false;
384 }
385
386 return true;
387}
388
389static int mchp_coreqspi_adjust_op_size(struct spi_slave *slave,
390 struct spi_mem_op *op)
391{
392 if (op->data.dir == SPI_MEM_DATA_OUT) {
393 if (op->data.nbytes > MAX_DATA_CMD_LEN)
394 op->data.nbytes = MAX_DATA_CMD_LEN;
395 }
396
397 return 0;
398}
399
400static int mchp_coreqspi_set_speed(struct udevice *dev, uint speed)
401{
402 struct mchp_coreqspi *qspi = dev_get_priv(dev);
403 u32 control, baud_rate_val = 0;
404
405 if (speed > (qspi->freq / 2))
406 speed = qspi->freq / 2;
407
408 baud_rate_val = DIV_ROUND_UP(qspi->freq, 2 * speed);
409 if (baud_rate_val >= MAX_DIVIDER || baud_rate_val <= MIN_DIVIDER)
410 return -EINVAL;
411
412 control = readl(qspi->regs + REG_CONTROL);
413 control &= ~CONTROL_CLKRATE_MASK;
414 control |= baud_rate_val << CONTROL_CLKRATE_SHIFT;
415 writel(control, qspi->regs + REG_CONTROL);
416
417 return 0;
418}
419
420static int mchp_coreqspi_set_mode(struct udevice *dev, uint mode)
421{
422 struct mchp_coreqspi *qspi = dev_get_priv(dev);
423 u32 control;
424
425 control = readl(qspi->regs + REG_CONTROL);
426
427 if ((mode & SPI_CPOL) && (mode & SPI_CPHA))
428 control |= CONTROL_CLKIDLE;
429 else
430 control &= ~CONTROL_CLKIDLE;
431
432 writel(control, qspi->regs + REG_CONTROL);
433
434 return 0;
435}
436
437static int mchp_coreqspi_claim_bus(struct udevice *dev)
438{
439 return 0;
440}
441
442static int mchp_coreqspi_release_bus(struct udevice *dev)
443{
444 return 0;
445}
446
447static int mchp_coreqspi_probe(struct udevice *dev)
448{
449 struct mchp_coreqspi *qspi = dev_get_priv(dev);
450 struct clk clk;
451 ulong clk_rate;
452 int ret;
453
454 ret = clk_get_by_index(dev, 0, &clk);
455 if (ret)
456 return -EINVAL;
457
458 ret = clk_enable(&clk);
459 if (ret)
460 return ret;
461
462 clk_rate = clk_get_rate(&clk);
463 if (!clk_rate)
464 return -EINVAL;
465 qspi->freq = clk_rate;
466
467 qspi->regs = dev_read_addr_ptr(dev);
468 if (!qspi->regs)
469 return -EINVAL;
470
471 /* Init the mpfs qspi hw */
472 mchp_coreqspi_init_hw(qspi);
473
474 return 0;
475}
476
477static const struct spi_controller_mem_ops mchp_coreqspi_mem_ops = {
478 .adjust_op_size = mchp_coreqspi_adjust_op_size,
479 .supports_op = mchp_coreqspi_supports_op,
480 .exec_op = mchp_coreqspi_exec_op,
481};
482
483static const struct dm_spi_ops mchp_coreqspi_ops = {
484 .claim_bus = mchp_coreqspi_claim_bus,
485 .release_bus = mchp_coreqspi_release_bus,
486 .set_speed = mchp_coreqspi_set_speed,
487 .set_mode = mchp_coreqspi_set_mode,
488 .mem_ops = &mchp_coreqspi_mem_ops,
489};
490
491static const struct udevice_id mchp_coreqspi_ids[] = {
492 { .compatible = "microchip,mpfs-coreqspi-rtl-v2" },
493 { .compatible = "microchip,mpfs-qspi" },
494 { }
495};
496
497U_BOOT_DRIVER(mchp_coreqspi) = {
498 .name = "mchp_coreqspi",
499 .id = UCLASS_SPI,
500 .of_match = mchp_coreqspi_ids,
501 .ops = &mchp_coreqspi_ops,
502 .priv_auto = sizeof(struct mchp_coreqspi),
503 .probe = mchp_coreqspi_probe,
504};