1 // SPDX-License-Identifier: GPL-2.0-only
3 * Socionext SPI flash controller F_OSPI driver
4 * Copyright (C) 2021 Socionext Inc.
10 #include <dm/device_compat.h>
11 #include <linux/bitfield.h>
13 #include <linux/iopoll.h>
18 #define OSPI_PROT_CTL_INDIR 0x00
19 #define OSPI_PROT_MODE_DATA_MASK GENMASK(31, 30)
20 #define OSPI_PROT_MODE_ALT_MASK GENMASK(29, 28)
21 #define OSPI_PROT_MODE_ADDR_MASK GENMASK(27, 26)
22 #define OSPI_PROT_MODE_CODE_MASK GENMASK(25, 24)
23 #define OSPI_PROT_MODE_SINGLE 0
24 #define OSPI_PROT_MODE_DUAL 1
25 #define OSPI_PROT_MODE_QUAD 2
26 #define OSPI_PROT_MODE_OCTAL 3
27 #define OSPI_PROT_DATA_RATE_DATA BIT(23)
28 #define OSPI_PROT_DATA_RATE_ALT BIT(22)
29 #define OSPI_PROT_DATA_RATE_ADDR BIT(21)
30 #define OSPI_PROT_DATA_RATE_CODE BIT(20)
31 #define OSPI_PROT_SDR 0
32 #define OSPI_PROT_DDR 1
33 #define OSPI_PROT_BIT_POS_DATA BIT(19)
34 #define OSPI_PROT_BIT_POS_ALT BIT(18)
35 #define OSPI_PROT_BIT_POS_ADDR BIT(17)
36 #define OSPI_PROT_BIT_POS_CODE BIT(16)
37 #define OSPI_PROT_SAMP_EDGE BIT(12)
38 #define OSPI_PROT_DATA_UNIT_MASK GENMASK(11, 10)
39 #define OSPI_PROT_DATA_UNIT_1B 0
40 #define OSPI_PROT_DATA_UNIT_2B 1
41 #define OSPI_PROT_DATA_UNIT_4B 3
42 #define OSPI_PROT_TRANS_DIR_WRITE BIT(9)
43 #define OSPI_PROT_DATA_EN BIT(8)
44 #define OSPI_PROT_ALT_SIZE_MASK GENMASK(7, 5)
45 #define OSPI_PROT_ADDR_SIZE_MASK GENMASK(4, 2)
46 #define OSPI_PROT_CODE_SIZE_MASK GENMASK(1, 0)
48 #define OSPI_CLK_CTL 0x10
49 #define OSPI_CLK_CTL_BOOT_INT_CLK_EN BIT(16)
50 #define OSPI_CLK_CTL_PHA BIT(12)
51 #define OSPI_CLK_CTL_PHA_180 0
52 #define OSPI_CLK_CTL_PHA_90 1
53 #define OSPI_CLK_CTL_DIV GENMASK(9, 8)
54 #define OSPI_CLK_CTL_DIV_1 0
55 #define OSPI_CLK_CTL_DIV_2 1
56 #define OSPI_CLK_CTL_DIV_4 2
57 #define OSPI_CLK_CTL_DIV_8 3
58 #define OSPI_CLK_CTL_INT_CLK_EN BIT(0)
60 #define OSPI_CS_CTL1 0x14
61 #define OSPI_CS_CTL2 0x18
62 #define OSPI_SSEL 0x20
63 #define OSPI_CMD_IDX_INDIR 0x40
64 #define OSPI_ADDR 0x50
65 #define OSPI_ALT_INDIR 0x60
66 #define OSPI_DMY_INDIR 0x70
68 #define OSPI_DAT_SWP_INDIR 0x90
70 #define OSPI_DAT_SIZE_INDIR 0xA0
71 #define OSPI_DAT_SIZE_EN BIT(15)
72 #define OSPI_DAT_SIZE_MASK GENMASK(10, 0)
73 #define OSPI_DAT_SIZE_MAX (OSPI_DAT_SIZE_MASK + 1)
75 #define OSPI_TRANS_CTL 0xC0
76 #define OSPI_TRANS_CTL_STOP_REQ BIT(1) /* RW1AC */
77 #define OSPI_TRANS_CTL_START_REQ BIT(0) /* RW1AC */
79 #define OSPI_ACC_MODE 0xC4
80 #define OSPI_ACC_MODE_BOOT_DISABLE BIT(0)
82 #define OSPI_SWRST 0xD0
83 #define OSPI_SWRST_INDIR_WRITE_FIFO BIT(9) /* RW1AC */
84 #define OSPI_SWRST_INDIR_READ_FIFO BIT(8) /* RW1AC */
86 #define OSPI_STAT 0xE0
87 #define OSPI_STAT_IS_AXI_WRITING BIT(10)
88 #define OSPI_STAT_IS_AXI_READING BIT(9)
89 #define OSPI_STAT_IS_SPI_INT_CLK_STOP BIT(4)
90 #define OSPI_STAT_IS_SPI_IDLE BIT(3)
93 #define OSPI_IRQ_CS_DEASSERT BIT(8)
94 #define OSPI_IRQ_WRITE_BUF_READY BIT(2)
95 #define OSPI_IRQ_READ_BUF_READY BIT(1)
96 #define OSPI_IRQ_CS_TRANS_COMP BIT(0)
97 #define OSPI_IRQ_ALL \
98 (OSPI_IRQ_CS_DEASSERT | OSPI_IRQ_WRITE_BUF_READY \
99 | OSPI_IRQ_READ_BUF_READY | OSPI_IRQ_CS_TRANS_COMP)
101 #define OSPI_IRQ_STAT_EN 0xF4
102 #define OSPI_IRQ_SIG_EN 0xF8
105 #define OSPI_NUM_CS 4
106 #define OSPI_DUMMY_CYCLE_MAX 255
107 #define OSPI_WAIT_MAX_MSEC 100
120 static u32 f_ospi_get_dummy_cycle(const struct spi_mem_op *op)
122 return (op->dummy.nbytes * 8) / op->dummy.buswidth;
125 static void f_ospi_clear_irq(struct f_ospi *ospi)
127 writel(OSPI_IRQ_CS_DEASSERT | OSPI_IRQ_CS_TRANS_COMP,
128 ospi->base + OSPI_IRQ);
131 static void f_ospi_enable_irq_status(struct f_ospi *ospi, u32 irq_bits)
135 val = readl(ospi->base + OSPI_IRQ_STAT_EN);
137 writel(val, ospi->base + OSPI_IRQ_STAT_EN);
140 static void f_ospi_disable_irq_status(struct f_ospi *ospi, u32 irq_bits)
144 val = readl(ospi->base + OSPI_IRQ_STAT_EN);
146 writel(val, ospi->base + OSPI_IRQ_STAT_EN);
149 static void f_ospi_disable_irq_output(struct f_ospi *ospi, u32 irq_bits)
153 val = readl(ospi->base + OSPI_IRQ_SIG_EN);
155 writel(val, ospi->base + OSPI_IRQ_SIG_EN);
158 static int f_ospi_prepare_config(struct f_ospi *ospi)
160 u32 val, stat0, stat1;
162 /* G4: Disable internal clock */
163 val = readl(ospi->base + OSPI_CLK_CTL);
164 val &= ~(OSPI_CLK_CTL_BOOT_INT_CLK_EN | OSPI_CLK_CTL_INT_CLK_EN);
165 writel(val, ospi->base + OSPI_CLK_CTL);
167 /* G5: Wait for stop */
168 stat0 = OSPI_STAT_IS_AXI_WRITING | OSPI_STAT_IS_AXI_READING;
169 stat1 = OSPI_STAT_IS_SPI_IDLE | OSPI_STAT_IS_SPI_INT_CLK_STOP;
171 return readl_poll_timeout(ospi->base + OSPI_STAT,
172 val, (val & (stat0 | stat1)) == stat1,
176 static int f_ospi_unprepare_config(struct f_ospi *ospi)
180 /* G11: Enable internal clock */
181 val = readl(ospi->base + OSPI_CLK_CTL);
182 val |= OSPI_CLK_CTL_BOOT_INT_CLK_EN | OSPI_CLK_CTL_INT_CLK_EN;
183 writel(val, ospi->base + OSPI_CLK_CTL);
185 /* G12: Wait for clock to start */
186 return readl_poll_timeout(ospi->base + OSPI_STAT,
187 val, !(val & OSPI_STAT_IS_SPI_INT_CLK_STOP),
191 static void f_ospi_config_clk(struct f_ospi *ospi, u32 device_hz)
193 long rate_hz = clk_get_rate(&ospi->clk);
194 u32 div = DIV_ROUND_UP(rate_hz, device_hz);
198 if (rate_hz < device_hz) {
199 dev_warn(ospi->dev, "Device frequency too large: %d\n",
201 div_reg = OSPI_CLK_CTL_DIV_1;
204 div_reg = OSPI_CLK_CTL_DIV_1;
205 } else if (div == 2) {
206 div_reg = OSPI_CLK_CTL_DIV_2;
207 } else if (div <= 4) {
208 div_reg = OSPI_CLK_CTL_DIV_4;
209 } else if (div <= 8) {
210 div_reg = OSPI_CLK_CTL_DIV_8;
212 dev_warn(ospi->dev, "Device frequency too small: %d\n",
214 div_reg = OSPI_CLK_CTL_DIV_8;
220 * clock phase is fixed at 180 degrees and configure edge direction
223 val = readl(ospi->base + OSPI_CLK_CTL);
225 val &= ~(OSPI_CLK_CTL_PHA | OSPI_CLK_CTL_DIV);
226 val |= FIELD_PREP(OSPI_CLK_CTL_PHA, OSPI_CLK_CTL_PHA_180)
227 | FIELD_PREP(OSPI_CLK_CTL_DIV, div_reg);
229 writel(val, ospi->base + OSPI_CLK_CTL);
232 static void f_ospi_config_dll(struct f_ospi *ospi)
234 /* G8: Configure DLL, nothing */
237 static u8 f_ospi_get_mode(struct f_ospi *ospi, int width, int data_size)
239 u8 mode = OSPI_PROT_MODE_SINGLE;
243 mode = OSPI_PROT_MODE_SINGLE;
246 mode = OSPI_PROT_MODE_DUAL;
249 mode = OSPI_PROT_MODE_QUAD;
252 mode = OSPI_PROT_MODE_OCTAL;
256 dev_err(ospi->dev, "Invalid buswidth: %d\n", width);
263 static void f_ospi_config_indir_protocol(struct f_ospi *ospi,
264 const struct spi_mem_op *op)
270 /* Set one chip select */
271 writel(BIT(ospi->chip_select), ospi->base + OSPI_SSEL);
273 mode = f_ospi_get_mode(ospi, op->cmd.buswidth, 1);
274 prot |= FIELD_PREP(OSPI_PROT_MODE_CODE_MASK, mode);
276 mode = f_ospi_get_mode(ospi, op->addr.buswidth, op->addr.nbytes);
277 prot |= FIELD_PREP(OSPI_PROT_MODE_ADDR_MASK, mode);
279 mode = f_ospi_get_mode(ospi, op->data.buswidth, op->data.nbytes);
280 prot |= FIELD_PREP(OSPI_PROT_MODE_DATA_MASK, mode);
282 prot |= FIELD_PREP(OSPI_PROT_DATA_RATE_DATA, OSPI_PROT_SDR);
283 prot |= FIELD_PREP(OSPI_PROT_DATA_RATE_ALT, OSPI_PROT_SDR);
284 prot |= FIELD_PREP(OSPI_PROT_DATA_RATE_ADDR, OSPI_PROT_SDR);
285 prot |= FIELD_PREP(OSPI_PROT_DATA_RATE_CODE, OSPI_PROT_SDR);
287 if (ospi->mode & SPI_LSB_FIRST)
288 prot |= OSPI_PROT_BIT_POS_DATA | OSPI_PROT_BIT_POS_ALT
289 | OSPI_PROT_BIT_POS_ADDR | OSPI_PROT_BIT_POS_CODE;
291 if (ospi->mode & SPI_CPHA)
292 prot |= OSPI_PROT_SAMP_EDGE;
294 /* Examine nbytes % 4 */
295 switch (op->data.nbytes & 0x3) {
297 unit = OSPI_PROT_DATA_UNIT_4B;
301 unit = OSPI_PROT_DATA_UNIT_2B;
302 val = OSPI_DAT_SIZE_EN | (op->data.nbytes - 1);
305 unit = OSPI_PROT_DATA_UNIT_1B;
306 val = OSPI_DAT_SIZE_EN | (op->data.nbytes - 1);
309 prot |= FIELD_PREP(OSPI_PROT_DATA_UNIT_MASK, unit);
311 switch (op->data.dir) {
312 case SPI_MEM_DATA_IN:
313 prot |= OSPI_PROT_DATA_EN;
316 case SPI_MEM_DATA_OUT:
317 prot |= OSPI_PROT_TRANS_DIR_WRITE | OSPI_PROT_DATA_EN;
320 case SPI_MEM_NO_DATA:
321 prot |= OSPI_PROT_TRANS_DIR_WRITE;
325 dev_warn(ospi->dev, "Unsupported direction");
329 prot |= FIELD_PREP(OSPI_PROT_ADDR_SIZE_MASK, op->addr.nbytes);
330 prot |= FIELD_PREP(OSPI_PROT_CODE_SIZE_MASK, 1); /* 1byte */
332 writel(prot, ospi->base + OSPI_PROT_CTL_INDIR);
333 writel(val, ospi->base + OSPI_DAT_SIZE_INDIR);
336 static int f_ospi_indir_prepare_op(struct f_ospi *ospi,
337 const struct spi_mem_op *op)
342 ret = f_ospi_prepare_config(ospi);
346 f_ospi_config_clk(ospi, ospi->max_speed_hz);
348 f_ospi_config_indir_protocol(ospi, op);
350 writel(f_ospi_get_dummy_cycle(op), ospi->base + OSPI_DMY_INDIR);
351 writel(op->addr.val, ospi->base + OSPI_ADDR);
352 writel(op->cmd.opcode, ospi->base + OSPI_CMD_IDX_INDIR);
354 f_ospi_clear_irq(ospi);
356 switch (op->data.dir) {
357 case SPI_MEM_DATA_IN:
358 irq_stat_en = OSPI_IRQ_READ_BUF_READY | OSPI_IRQ_CS_TRANS_COMP;
361 case SPI_MEM_DATA_OUT:
362 irq_stat_en = OSPI_IRQ_WRITE_BUF_READY | OSPI_IRQ_CS_TRANS_COMP;
365 case SPI_MEM_NO_DATA:
366 irq_stat_en = OSPI_IRQ_CS_TRANS_COMP;
370 dev_warn(ospi->dev, "Unsupported direction");
374 f_ospi_disable_irq_status(ospi, ~irq_stat_en);
375 f_ospi_enable_irq_status(ospi, irq_stat_en);
377 return f_ospi_unprepare_config(ospi);
380 static void f_ospi_indir_start_xfer(struct f_ospi *ospi)
382 /* Write only 1, auto cleared */
383 writel(OSPI_TRANS_CTL_START_REQ, ospi->base + OSPI_TRANS_CTL);
386 static void f_ospi_indir_stop_xfer(struct f_ospi *ospi)
388 /* Write only 1, auto cleared */
389 writel(OSPI_TRANS_CTL_STOP_REQ, ospi->base + OSPI_TRANS_CTL);
392 static int f_ospi_indir_wait_xfer_complete(struct f_ospi *ospi)
396 return readl_poll_timeout(ospi->base + OSPI_IRQ, val,
397 val & OSPI_IRQ_CS_TRANS_COMP,
401 static int f_ospi_indir_read(struct f_ospi *ospi,
402 const struct spi_mem_op *op)
404 u8 *buf = op->data.buf.in;
408 /* E1-2: Prepare transfer operation */
409 ret = f_ospi_indir_prepare_op(ospi, op);
413 f_ospi_indir_start_xfer(ospi);
415 /* E3-4: Wait for ready and read data */
416 for (i = 0; i < op->data.nbytes; i++) {
417 ret = readl_poll_timeout(ospi->base + OSPI_IRQ, val,
418 val & OSPI_IRQ_READ_BUF_READY,
423 buf[i] = readl(ospi->base + OSPI_DAT) & 0xFF;
426 /* E5-6: Stop transfer if data size is nothing */
427 if (!(readl(ospi->base + OSPI_DAT_SIZE_INDIR) & OSPI_DAT_SIZE_EN))
428 f_ospi_indir_stop_xfer(ospi);
430 /* E7-8: Wait for completion and clear */
431 ret = f_ospi_indir_wait_xfer_complete(ospi);
435 writel(OSPI_IRQ_CS_TRANS_COMP, ospi->base + OSPI_IRQ);
437 /* E9: Do nothing if data size is valid */
438 if (readl(ospi->base + OSPI_DAT_SIZE_INDIR) & OSPI_DAT_SIZE_EN)
441 /* E10-11: Reset and check read fifo */
442 writel(OSPI_SWRST_INDIR_READ_FIFO, ospi->base + OSPI_SWRST);
444 ret = readl_poll_timeout(ospi->base + OSPI_SWRST, val,
445 !(val & OSPI_SWRST_INDIR_READ_FIFO),
451 static int f_ospi_indir_write(struct f_ospi *ospi,
452 const struct spi_mem_op *op)
454 u8 *buf = (u8 *)op->data.buf.out;
458 /* F1-3: Prepare transfer operation */
459 ret = f_ospi_indir_prepare_op(ospi, op);
463 f_ospi_indir_start_xfer(ospi);
465 if (!(readl(ospi->base + OSPI_PROT_CTL_INDIR) & OSPI_PROT_DATA_EN))
468 /* F4-5: Wait for buffer ready and write data */
469 for (i = 0; i < op->data.nbytes; i++) {
470 ret = readl_poll_timeout(ospi->base + OSPI_IRQ, val,
471 val & OSPI_IRQ_WRITE_BUF_READY,
476 writel(buf[i], ospi->base + OSPI_DAT);
479 /* F6-7: Stop transfer if data size is nothing */
480 if (!(readl(ospi->base + OSPI_DAT_SIZE_INDIR) & OSPI_DAT_SIZE_EN))
481 f_ospi_indir_stop_xfer(ospi);
484 /* F8-9: Wait for completion and clear */
485 ret = f_ospi_indir_wait_xfer_complete(ospi);
489 writel(OSPI_IRQ_CS_TRANS_COMP, ospi->base + OSPI_IRQ);
494 static int f_ospi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
496 struct f_ospi *ospi = dev_get_priv(slave->dev->parent);
497 struct dm_spi_slave_plat *slave_plat;
500 slave_plat = dev_get_parent_plat(slave->dev);
501 ospi->chip_select = slave_plat->cs;
503 switch (op->data.dir) {
504 case SPI_MEM_DATA_IN:
505 err = f_ospi_indir_read(ospi, op);
508 case SPI_MEM_DATA_OUT:
510 case SPI_MEM_NO_DATA:
511 err = f_ospi_indir_write(ospi, op);
515 dev_warn(ospi->dev, "Unsupported direction");
522 static bool f_ospi_supports_op_width(const struct spi_mem_op *op)
524 u8 width_available[] = { 0, 1, 2, 4, 8 };
525 u8 width_op[] = { op->cmd.buswidth, op->addr.buswidth,
526 op->dummy.buswidth, op->data.buswidth };
530 for (i = 0; i < ARRAY_SIZE(width_op); i++) {
531 is_match_found = false;
533 for (j = 0; j < ARRAY_SIZE(width_available); j++) {
534 if (width_op[i] == width_available[j]) {
535 is_match_found = true;
547 static bool f_ospi_supports_op(struct spi_slave *slave,
548 const struct spi_mem_op *op)
550 if (f_ospi_get_dummy_cycle(op) > OSPI_DUMMY_CYCLE_MAX)
553 if (op->addr.nbytes > 4)
556 if (!f_ospi_supports_op_width(op))
562 static int f_ospi_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op)
564 op->data.nbytes = min((int)op->data.nbytes, (int)(OSPI_DAT_SIZE_MAX));
569 static const struct spi_controller_mem_ops f_ospi_mem_ops = {
570 .adjust_op_size = f_ospi_adjust_op_size,
571 .supports_op = f_ospi_supports_op,
572 .exec_op = f_ospi_exec_op,
575 static int f_ospi_set_speed(struct udevice *bus, u32 speed)
577 struct f_ospi *ospi = dev_get_priv(bus);
579 ospi->max_speed_hz = speed;
584 static int f_ospi_set_mode(struct udevice *bus, u32 mode)
586 struct f_ospi *ospi = dev_get_priv(bus);
593 static int f_ospi_init(struct f_ospi *ospi)
597 ret = f_ospi_prepare_config(ospi);
601 /* Disable boot signal */
602 writel(OSPI_ACC_MODE_BOOT_DISABLE, ospi->base + OSPI_ACC_MODE);
604 f_ospi_config_dll(ospi);
607 f_ospi_clear_irq(ospi);
608 f_ospi_disable_irq_status(ospi, OSPI_IRQ_ALL);
609 f_ospi_disable_irq_output(ospi, OSPI_IRQ_ALL);
611 return f_ospi_unprepare_config(ospi);
614 static int f_ospi_of_to_plat(struct udevice *dev)
616 struct f_ospi *ospi = dev_get_priv(dev);
618 ospi->base = dev_read_addr_ptr(dev);
619 ospi->num_cs = dev_read_u32_default(dev, "num-cs", OSPI_NUM_CS);
624 static int f_ospi_probe(struct udevice *dev)
626 struct f_ospi *ospi = dev_get_priv(dev);
631 ret = clk_get_by_index(dev, 0, &ospi->clk);
633 dev_err(dev, "Failed to get clock\n");
637 ret = clk_enable(&ospi->clk);
639 dev_err(dev, "Failed to enable the clock\n");
643 ret = f_ospi_init(ospi);
645 goto err_disable_clk;
650 clk_disable(&ospi->clk);
653 dev_err(dev, "Socionext F_OSPI probe failed\n");
657 static int f_ospi_remove(struct udevice *dev)
659 struct f_ospi *ospi = dev_get_priv(dev);
661 clk_disable(&ospi->clk);
666 static const struct dm_spi_ops f_ospi_ops = {
667 .set_speed = f_ospi_set_speed,
668 .set_mode = f_ospi_set_mode,
669 .mem_ops = &f_ospi_mem_ops,
672 static const struct udevice_id f_ospi_dt_ids[] = {
673 { .compatible = "socionext,f-ospi" },
677 U_BOOT_DRIVER(f_ospi) = {
680 .of_match = f_ospi_dt_ids,
681 .of_to_plat = f_ospi_of_to_plat,
683 .probe = f_ospi_probe,
684 .remove = f_ospi_remove,
685 .priv_auto = sizeof(struct f_ospi),