Kunihiko Hayashi | 7509ea1 | 2022-11-29 11:17:09 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Socionext SPI flash controller F_OSPI driver |
| 4 | * Copyright (C) 2021 Socionext Inc. |
| 5 | */ |
| 6 | |
| 7 | #include <clk.h> |
Kunihiko Hayashi | 7509ea1 | 2022-11-29 11:17:09 +0900 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <dm/device_compat.h> |
| 10 | #include <linux/bitfield.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/iopoll.h> |
| 13 | #include <spi.h> |
| 14 | #include <spi-mem.h> |
| 15 | |
| 16 | /* Registers */ |
| 17 | #define OSPI_PROT_CTL_INDIR 0x00 |
| 18 | #define OSPI_PROT_MODE_DATA_MASK GENMASK(31, 30) |
| 19 | #define OSPI_PROT_MODE_ALT_MASK GENMASK(29, 28) |
| 20 | #define OSPI_PROT_MODE_ADDR_MASK GENMASK(27, 26) |
| 21 | #define OSPI_PROT_MODE_CODE_MASK GENMASK(25, 24) |
| 22 | #define OSPI_PROT_MODE_SINGLE 0 |
| 23 | #define OSPI_PROT_MODE_DUAL 1 |
| 24 | #define OSPI_PROT_MODE_QUAD 2 |
| 25 | #define OSPI_PROT_MODE_OCTAL 3 |
| 26 | #define OSPI_PROT_DATA_RATE_DATA BIT(23) |
| 27 | #define OSPI_PROT_DATA_RATE_ALT BIT(22) |
| 28 | #define OSPI_PROT_DATA_RATE_ADDR BIT(21) |
| 29 | #define OSPI_PROT_DATA_RATE_CODE BIT(20) |
| 30 | #define OSPI_PROT_SDR 0 |
| 31 | #define OSPI_PROT_DDR 1 |
| 32 | #define OSPI_PROT_BIT_POS_DATA BIT(19) |
| 33 | #define OSPI_PROT_BIT_POS_ALT BIT(18) |
| 34 | #define OSPI_PROT_BIT_POS_ADDR BIT(17) |
| 35 | #define OSPI_PROT_BIT_POS_CODE BIT(16) |
| 36 | #define OSPI_PROT_SAMP_EDGE BIT(12) |
| 37 | #define OSPI_PROT_DATA_UNIT_MASK GENMASK(11, 10) |
| 38 | #define OSPI_PROT_DATA_UNIT_1B 0 |
| 39 | #define OSPI_PROT_DATA_UNIT_2B 1 |
| 40 | #define OSPI_PROT_DATA_UNIT_4B 3 |
| 41 | #define OSPI_PROT_TRANS_DIR_WRITE BIT(9) |
| 42 | #define OSPI_PROT_DATA_EN BIT(8) |
| 43 | #define OSPI_PROT_ALT_SIZE_MASK GENMASK(7, 5) |
| 44 | #define OSPI_PROT_ADDR_SIZE_MASK GENMASK(4, 2) |
| 45 | #define OSPI_PROT_CODE_SIZE_MASK GENMASK(1, 0) |
| 46 | |
| 47 | #define OSPI_CLK_CTL 0x10 |
| 48 | #define OSPI_CLK_CTL_BOOT_INT_CLK_EN BIT(16) |
| 49 | #define OSPI_CLK_CTL_PHA BIT(12) |
| 50 | #define OSPI_CLK_CTL_PHA_180 0 |
| 51 | #define OSPI_CLK_CTL_PHA_90 1 |
| 52 | #define OSPI_CLK_CTL_DIV GENMASK(9, 8) |
| 53 | #define OSPI_CLK_CTL_DIV_1 0 |
| 54 | #define OSPI_CLK_CTL_DIV_2 1 |
| 55 | #define OSPI_CLK_CTL_DIV_4 2 |
| 56 | #define OSPI_CLK_CTL_DIV_8 3 |
| 57 | #define OSPI_CLK_CTL_INT_CLK_EN BIT(0) |
| 58 | |
| 59 | #define OSPI_CS_CTL1 0x14 |
| 60 | #define OSPI_CS_CTL2 0x18 |
| 61 | #define OSPI_SSEL 0x20 |
| 62 | #define OSPI_CMD_IDX_INDIR 0x40 |
| 63 | #define OSPI_ADDR 0x50 |
| 64 | #define OSPI_ALT_INDIR 0x60 |
| 65 | #define OSPI_DMY_INDIR 0x70 |
| 66 | #define OSPI_DAT 0x80 |
| 67 | #define OSPI_DAT_SWP_INDIR 0x90 |
| 68 | |
| 69 | #define OSPI_DAT_SIZE_INDIR 0xA0 |
| 70 | #define OSPI_DAT_SIZE_EN BIT(15) |
| 71 | #define OSPI_DAT_SIZE_MASK GENMASK(10, 0) |
| 72 | #define OSPI_DAT_SIZE_MAX (OSPI_DAT_SIZE_MASK + 1) |
| 73 | |
| 74 | #define OSPI_TRANS_CTL 0xC0 |
| 75 | #define OSPI_TRANS_CTL_STOP_REQ BIT(1) /* RW1AC */ |
| 76 | #define OSPI_TRANS_CTL_START_REQ BIT(0) /* RW1AC */ |
| 77 | |
| 78 | #define OSPI_ACC_MODE 0xC4 |
| 79 | #define OSPI_ACC_MODE_BOOT_DISABLE BIT(0) |
| 80 | |
| 81 | #define OSPI_SWRST 0xD0 |
| 82 | #define OSPI_SWRST_INDIR_WRITE_FIFO BIT(9) /* RW1AC */ |
| 83 | #define OSPI_SWRST_INDIR_READ_FIFO BIT(8) /* RW1AC */ |
| 84 | |
| 85 | #define OSPI_STAT 0xE0 |
| 86 | #define OSPI_STAT_IS_AXI_WRITING BIT(10) |
| 87 | #define OSPI_STAT_IS_AXI_READING BIT(9) |
| 88 | #define OSPI_STAT_IS_SPI_INT_CLK_STOP BIT(4) |
| 89 | #define OSPI_STAT_IS_SPI_IDLE BIT(3) |
| 90 | |
| 91 | #define OSPI_IRQ 0xF0 |
| 92 | #define OSPI_IRQ_CS_DEASSERT BIT(8) |
| 93 | #define OSPI_IRQ_WRITE_BUF_READY BIT(2) |
| 94 | #define OSPI_IRQ_READ_BUF_READY BIT(1) |
| 95 | #define OSPI_IRQ_CS_TRANS_COMP BIT(0) |
| 96 | #define OSPI_IRQ_ALL \ |
| 97 | (OSPI_IRQ_CS_DEASSERT | OSPI_IRQ_WRITE_BUF_READY \ |
| 98 | | OSPI_IRQ_READ_BUF_READY | OSPI_IRQ_CS_TRANS_COMP) |
| 99 | |
| 100 | #define OSPI_IRQ_STAT_EN 0xF4 |
| 101 | #define OSPI_IRQ_SIG_EN 0xF8 |
| 102 | |
| 103 | /* Parameters */ |
| 104 | #define OSPI_NUM_CS 4 |
| 105 | #define OSPI_DUMMY_CYCLE_MAX 255 |
| 106 | #define OSPI_WAIT_MAX_MSEC 100 |
| 107 | |
| 108 | struct f_ospi { |
| 109 | void __iomem *base; |
| 110 | struct udevice *dev; |
| 111 | struct clk clk; |
| 112 | |
| 113 | u32 mode; |
| 114 | u32 max_speed_hz; |
| 115 | u32 num_cs; |
| 116 | u32 chip_select; |
| 117 | }; |
| 118 | |
| 119 | static u32 f_ospi_get_dummy_cycle(const struct spi_mem_op *op) |
| 120 | { |
| 121 | return (op->dummy.nbytes * 8) / op->dummy.buswidth; |
| 122 | } |
| 123 | |
| 124 | static void f_ospi_clear_irq(struct f_ospi *ospi) |
| 125 | { |
| 126 | writel(OSPI_IRQ_CS_DEASSERT | OSPI_IRQ_CS_TRANS_COMP, |
| 127 | ospi->base + OSPI_IRQ); |
| 128 | } |
| 129 | |
| 130 | static void f_ospi_enable_irq_status(struct f_ospi *ospi, u32 irq_bits) |
| 131 | { |
| 132 | u32 val; |
| 133 | |
| 134 | val = readl(ospi->base + OSPI_IRQ_STAT_EN); |
| 135 | val |= irq_bits; |
| 136 | writel(val, ospi->base + OSPI_IRQ_STAT_EN); |
| 137 | } |
| 138 | |
| 139 | static void f_ospi_disable_irq_status(struct f_ospi *ospi, u32 irq_bits) |
| 140 | { |
| 141 | u32 val; |
| 142 | |
| 143 | val = readl(ospi->base + OSPI_IRQ_STAT_EN); |
| 144 | val &= ~irq_bits; |
| 145 | writel(val, ospi->base + OSPI_IRQ_STAT_EN); |
| 146 | } |
| 147 | |
| 148 | static void f_ospi_disable_irq_output(struct f_ospi *ospi, u32 irq_bits) |
| 149 | { |
| 150 | u32 val; |
| 151 | |
| 152 | val = readl(ospi->base + OSPI_IRQ_SIG_EN); |
| 153 | val &= ~irq_bits; |
| 154 | writel(val, ospi->base + OSPI_IRQ_SIG_EN); |
| 155 | } |
| 156 | |
| 157 | static int f_ospi_prepare_config(struct f_ospi *ospi) |
| 158 | { |
| 159 | u32 val, stat0, stat1; |
| 160 | |
| 161 | /* G4: Disable internal clock */ |
| 162 | val = readl(ospi->base + OSPI_CLK_CTL); |
| 163 | val &= ~(OSPI_CLK_CTL_BOOT_INT_CLK_EN | OSPI_CLK_CTL_INT_CLK_EN); |
| 164 | writel(val, ospi->base + OSPI_CLK_CTL); |
| 165 | |
| 166 | /* G5: Wait for stop */ |
| 167 | stat0 = OSPI_STAT_IS_AXI_WRITING | OSPI_STAT_IS_AXI_READING; |
| 168 | stat1 = OSPI_STAT_IS_SPI_IDLE | OSPI_STAT_IS_SPI_INT_CLK_STOP; |
| 169 | |
| 170 | return readl_poll_timeout(ospi->base + OSPI_STAT, |
| 171 | val, (val & (stat0 | stat1)) == stat1, |
| 172 | OSPI_WAIT_MAX_MSEC); |
| 173 | } |
| 174 | |
| 175 | static int f_ospi_unprepare_config(struct f_ospi *ospi) |
| 176 | { |
| 177 | u32 val; |
| 178 | |
| 179 | /* G11: Enable internal clock */ |
| 180 | val = readl(ospi->base + OSPI_CLK_CTL); |
| 181 | val |= OSPI_CLK_CTL_BOOT_INT_CLK_EN | OSPI_CLK_CTL_INT_CLK_EN; |
| 182 | writel(val, ospi->base + OSPI_CLK_CTL); |
| 183 | |
| 184 | /* G12: Wait for clock to start */ |
| 185 | return readl_poll_timeout(ospi->base + OSPI_STAT, |
| 186 | val, !(val & OSPI_STAT_IS_SPI_INT_CLK_STOP), |
| 187 | OSPI_WAIT_MAX_MSEC); |
| 188 | } |
| 189 | |
| 190 | static void f_ospi_config_clk(struct f_ospi *ospi, u32 device_hz) |
| 191 | { |
| 192 | long rate_hz = clk_get_rate(&ospi->clk); |
| 193 | u32 div = DIV_ROUND_UP(rate_hz, device_hz); |
| 194 | u32 div_reg; |
| 195 | u32 val; |
| 196 | |
| 197 | if (rate_hz < device_hz) { |
| 198 | dev_warn(ospi->dev, "Device frequency too large: %d\n", |
| 199 | device_hz); |
| 200 | div_reg = OSPI_CLK_CTL_DIV_1; |
| 201 | } else { |
| 202 | if (div == 1) { |
| 203 | div_reg = OSPI_CLK_CTL_DIV_1; |
| 204 | } else if (div == 2) { |
| 205 | div_reg = OSPI_CLK_CTL_DIV_2; |
| 206 | } else if (div <= 4) { |
| 207 | div_reg = OSPI_CLK_CTL_DIV_4; |
| 208 | } else if (div <= 8) { |
| 209 | div_reg = OSPI_CLK_CTL_DIV_8; |
| 210 | } else { |
| 211 | dev_warn(ospi->dev, "Device frequency too small: %d\n", |
| 212 | device_hz); |
| 213 | div_reg = OSPI_CLK_CTL_DIV_8; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * G7: Set clock mode |
| 219 | * clock phase is fixed at 180 degrees and configure edge direction |
| 220 | * instead. |
| 221 | */ |
| 222 | val = readl(ospi->base + OSPI_CLK_CTL); |
| 223 | |
| 224 | val &= ~(OSPI_CLK_CTL_PHA | OSPI_CLK_CTL_DIV); |
| 225 | val |= FIELD_PREP(OSPI_CLK_CTL_PHA, OSPI_CLK_CTL_PHA_180) |
| 226 | | FIELD_PREP(OSPI_CLK_CTL_DIV, div_reg); |
| 227 | |
| 228 | writel(val, ospi->base + OSPI_CLK_CTL); |
| 229 | } |
| 230 | |
| 231 | static void f_ospi_config_dll(struct f_ospi *ospi) |
| 232 | { |
| 233 | /* G8: Configure DLL, nothing */ |
| 234 | } |
| 235 | |
| 236 | static u8 f_ospi_get_mode(struct f_ospi *ospi, int width, int data_size) |
| 237 | { |
| 238 | u8 mode = OSPI_PROT_MODE_SINGLE; |
| 239 | |
| 240 | switch (width) { |
| 241 | case 1: |
| 242 | mode = OSPI_PROT_MODE_SINGLE; |
| 243 | break; |
| 244 | case 2: |
| 245 | mode = OSPI_PROT_MODE_DUAL; |
| 246 | break; |
| 247 | case 4: |
| 248 | mode = OSPI_PROT_MODE_QUAD; |
| 249 | break; |
| 250 | case 8: |
| 251 | mode = OSPI_PROT_MODE_OCTAL; |
| 252 | break; |
| 253 | default: |
| 254 | if (data_size) |
| 255 | dev_err(ospi->dev, "Invalid buswidth: %d\n", width); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | return mode; |
| 260 | } |
| 261 | |
| 262 | static void f_ospi_config_indir_protocol(struct f_ospi *ospi, |
| 263 | const struct spi_mem_op *op) |
| 264 | { |
| 265 | u8 mode; |
| 266 | u32 prot = 0, val; |
| 267 | int unit; |
| 268 | |
| 269 | /* Set one chip select */ |
| 270 | writel(BIT(ospi->chip_select), ospi->base + OSPI_SSEL); |
| 271 | |
| 272 | mode = f_ospi_get_mode(ospi, op->cmd.buswidth, 1); |
| 273 | prot |= FIELD_PREP(OSPI_PROT_MODE_CODE_MASK, mode); |
| 274 | |
| 275 | mode = f_ospi_get_mode(ospi, op->addr.buswidth, op->addr.nbytes); |
| 276 | prot |= FIELD_PREP(OSPI_PROT_MODE_ADDR_MASK, mode); |
| 277 | |
| 278 | mode = f_ospi_get_mode(ospi, op->data.buswidth, op->data.nbytes); |
| 279 | prot |= FIELD_PREP(OSPI_PROT_MODE_DATA_MASK, mode); |
| 280 | |
| 281 | prot |= FIELD_PREP(OSPI_PROT_DATA_RATE_DATA, OSPI_PROT_SDR); |
| 282 | prot |= FIELD_PREP(OSPI_PROT_DATA_RATE_ALT, OSPI_PROT_SDR); |
| 283 | prot |= FIELD_PREP(OSPI_PROT_DATA_RATE_ADDR, OSPI_PROT_SDR); |
| 284 | prot |= FIELD_PREP(OSPI_PROT_DATA_RATE_CODE, OSPI_PROT_SDR); |
| 285 | |
| 286 | if (ospi->mode & SPI_LSB_FIRST) |
| 287 | prot |= OSPI_PROT_BIT_POS_DATA | OSPI_PROT_BIT_POS_ALT |
| 288 | | OSPI_PROT_BIT_POS_ADDR | OSPI_PROT_BIT_POS_CODE; |
| 289 | |
| 290 | if (ospi->mode & SPI_CPHA) |
| 291 | prot |= OSPI_PROT_SAMP_EDGE; |
| 292 | |
| 293 | /* Examine nbytes % 4 */ |
| 294 | switch (op->data.nbytes & 0x3) { |
| 295 | case 0: |
| 296 | unit = OSPI_PROT_DATA_UNIT_4B; |
| 297 | val = 0; |
| 298 | break; |
| 299 | case 2: |
| 300 | unit = OSPI_PROT_DATA_UNIT_2B; |
| 301 | val = OSPI_DAT_SIZE_EN | (op->data.nbytes - 1); |
| 302 | break; |
| 303 | default: |
| 304 | unit = OSPI_PROT_DATA_UNIT_1B; |
| 305 | val = OSPI_DAT_SIZE_EN | (op->data.nbytes - 1); |
| 306 | break; |
| 307 | } |
| 308 | prot |= FIELD_PREP(OSPI_PROT_DATA_UNIT_MASK, unit); |
| 309 | |
| 310 | switch (op->data.dir) { |
| 311 | case SPI_MEM_DATA_IN: |
| 312 | prot |= OSPI_PROT_DATA_EN; |
| 313 | break; |
| 314 | |
| 315 | case SPI_MEM_DATA_OUT: |
| 316 | prot |= OSPI_PROT_TRANS_DIR_WRITE | OSPI_PROT_DATA_EN; |
| 317 | break; |
| 318 | |
| 319 | case SPI_MEM_NO_DATA: |
| 320 | prot |= OSPI_PROT_TRANS_DIR_WRITE; |
| 321 | break; |
| 322 | |
| 323 | default: |
| 324 | dev_warn(ospi->dev, "Unsupported direction"); |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | prot |= FIELD_PREP(OSPI_PROT_ADDR_SIZE_MASK, op->addr.nbytes); |
| 329 | prot |= FIELD_PREP(OSPI_PROT_CODE_SIZE_MASK, 1); /* 1byte */ |
| 330 | |
| 331 | writel(prot, ospi->base + OSPI_PROT_CTL_INDIR); |
| 332 | writel(val, ospi->base + OSPI_DAT_SIZE_INDIR); |
| 333 | } |
| 334 | |
| 335 | static int f_ospi_indir_prepare_op(struct f_ospi *ospi, |
| 336 | const struct spi_mem_op *op) |
| 337 | { |
| 338 | u32 irq_stat_en; |
| 339 | int ret; |
| 340 | |
| 341 | ret = f_ospi_prepare_config(ospi); |
| 342 | if (ret) |
| 343 | return ret; |
| 344 | |
| 345 | f_ospi_config_clk(ospi, ospi->max_speed_hz); |
| 346 | |
| 347 | f_ospi_config_indir_protocol(ospi, op); |
| 348 | |
| 349 | writel(f_ospi_get_dummy_cycle(op), ospi->base + OSPI_DMY_INDIR); |
| 350 | writel(op->addr.val, ospi->base + OSPI_ADDR); |
| 351 | writel(op->cmd.opcode, ospi->base + OSPI_CMD_IDX_INDIR); |
| 352 | |
| 353 | f_ospi_clear_irq(ospi); |
| 354 | |
| 355 | switch (op->data.dir) { |
| 356 | case SPI_MEM_DATA_IN: |
| 357 | irq_stat_en = OSPI_IRQ_READ_BUF_READY | OSPI_IRQ_CS_TRANS_COMP; |
| 358 | break; |
| 359 | |
| 360 | case SPI_MEM_DATA_OUT: |
| 361 | irq_stat_en = OSPI_IRQ_WRITE_BUF_READY | OSPI_IRQ_CS_TRANS_COMP; |
| 362 | break; |
| 363 | |
| 364 | case SPI_MEM_NO_DATA: |
| 365 | irq_stat_en = OSPI_IRQ_CS_TRANS_COMP; |
| 366 | break; |
| 367 | |
| 368 | default: |
| 369 | dev_warn(ospi->dev, "Unsupported direction"); |
| 370 | irq_stat_en = 0; |
| 371 | } |
| 372 | |
| 373 | f_ospi_disable_irq_status(ospi, ~irq_stat_en); |
| 374 | f_ospi_enable_irq_status(ospi, irq_stat_en); |
| 375 | |
| 376 | return f_ospi_unprepare_config(ospi); |
| 377 | } |
| 378 | |
| 379 | static void f_ospi_indir_start_xfer(struct f_ospi *ospi) |
| 380 | { |
| 381 | /* Write only 1, auto cleared */ |
| 382 | writel(OSPI_TRANS_CTL_START_REQ, ospi->base + OSPI_TRANS_CTL); |
| 383 | } |
| 384 | |
| 385 | static void f_ospi_indir_stop_xfer(struct f_ospi *ospi) |
| 386 | { |
| 387 | /* Write only 1, auto cleared */ |
| 388 | writel(OSPI_TRANS_CTL_STOP_REQ, ospi->base + OSPI_TRANS_CTL); |
| 389 | } |
| 390 | |
| 391 | static int f_ospi_indir_wait_xfer_complete(struct f_ospi *ospi) |
| 392 | { |
| 393 | u32 val; |
| 394 | |
| 395 | return readl_poll_timeout(ospi->base + OSPI_IRQ, val, |
| 396 | val & OSPI_IRQ_CS_TRANS_COMP, |
| 397 | OSPI_WAIT_MAX_MSEC); |
| 398 | } |
| 399 | |
| 400 | static int f_ospi_indir_read(struct f_ospi *ospi, |
| 401 | const struct spi_mem_op *op) |
| 402 | { |
| 403 | u8 *buf = op->data.buf.in; |
| 404 | u32 val; |
| 405 | int i, ret; |
| 406 | |
| 407 | /* E1-2: Prepare transfer operation */ |
| 408 | ret = f_ospi_indir_prepare_op(ospi, op); |
| 409 | if (ret) |
| 410 | goto out; |
| 411 | |
| 412 | f_ospi_indir_start_xfer(ospi); |
| 413 | |
| 414 | /* E3-4: Wait for ready and read data */ |
| 415 | for (i = 0; i < op->data.nbytes; i++) { |
| 416 | ret = readl_poll_timeout(ospi->base + OSPI_IRQ, val, |
| 417 | val & OSPI_IRQ_READ_BUF_READY, |
| 418 | OSPI_WAIT_MAX_MSEC); |
| 419 | if (ret) |
| 420 | goto out; |
| 421 | |
| 422 | buf[i] = readl(ospi->base + OSPI_DAT) & 0xFF; |
| 423 | } |
| 424 | |
| 425 | /* E5-6: Stop transfer if data size is nothing */ |
| 426 | if (!(readl(ospi->base + OSPI_DAT_SIZE_INDIR) & OSPI_DAT_SIZE_EN)) |
| 427 | f_ospi_indir_stop_xfer(ospi); |
| 428 | |
| 429 | /* E7-8: Wait for completion and clear */ |
| 430 | ret = f_ospi_indir_wait_xfer_complete(ospi); |
| 431 | if (ret) |
| 432 | goto out; |
| 433 | |
| 434 | writel(OSPI_IRQ_CS_TRANS_COMP, ospi->base + OSPI_IRQ); |
| 435 | |
| 436 | /* E9: Do nothing if data size is valid */ |
| 437 | if (readl(ospi->base + OSPI_DAT_SIZE_INDIR) & OSPI_DAT_SIZE_EN) |
| 438 | goto out; |
| 439 | |
| 440 | /* E10-11: Reset and check read fifo */ |
| 441 | writel(OSPI_SWRST_INDIR_READ_FIFO, ospi->base + OSPI_SWRST); |
| 442 | |
| 443 | ret = readl_poll_timeout(ospi->base + OSPI_SWRST, val, |
| 444 | !(val & OSPI_SWRST_INDIR_READ_FIFO), |
| 445 | OSPI_WAIT_MAX_MSEC); |
| 446 | out: |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | static int f_ospi_indir_write(struct f_ospi *ospi, |
| 451 | const struct spi_mem_op *op) |
| 452 | { |
| 453 | u8 *buf = (u8 *)op->data.buf.out; |
| 454 | u32 val; |
| 455 | int i, ret; |
| 456 | |
| 457 | /* F1-3: Prepare transfer operation */ |
| 458 | ret = f_ospi_indir_prepare_op(ospi, op); |
| 459 | if (ret) |
| 460 | goto out; |
| 461 | |
| 462 | f_ospi_indir_start_xfer(ospi); |
| 463 | |
| 464 | if (!(readl(ospi->base + OSPI_PROT_CTL_INDIR) & OSPI_PROT_DATA_EN)) |
| 465 | goto nodata; |
| 466 | |
| 467 | /* F4-5: Wait for buffer ready and write data */ |
| 468 | for (i = 0; i < op->data.nbytes; i++) { |
| 469 | ret = readl_poll_timeout(ospi->base + OSPI_IRQ, val, |
| 470 | val & OSPI_IRQ_WRITE_BUF_READY, |
| 471 | OSPI_WAIT_MAX_MSEC); |
| 472 | if (ret) |
| 473 | goto out; |
| 474 | |
| 475 | writel(buf[i], ospi->base + OSPI_DAT); |
| 476 | } |
| 477 | |
| 478 | /* F6-7: Stop transfer if data size is nothing */ |
| 479 | if (!(readl(ospi->base + OSPI_DAT_SIZE_INDIR) & OSPI_DAT_SIZE_EN)) |
| 480 | f_ospi_indir_stop_xfer(ospi); |
| 481 | |
| 482 | nodata: |
| 483 | /* F8-9: Wait for completion and clear */ |
| 484 | ret = f_ospi_indir_wait_xfer_complete(ospi); |
| 485 | if (ret) |
| 486 | goto out; |
| 487 | |
| 488 | writel(OSPI_IRQ_CS_TRANS_COMP, ospi->base + OSPI_IRQ); |
| 489 | out: |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | static int f_ospi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op) |
| 494 | { |
| 495 | struct f_ospi *ospi = dev_get_priv(slave->dev->parent); |
| 496 | struct dm_spi_slave_plat *slave_plat; |
| 497 | int err = 0; |
| 498 | |
| 499 | slave_plat = dev_get_parent_plat(slave->dev); |
| 500 | ospi->chip_select = slave_plat->cs; |
| 501 | |
| 502 | switch (op->data.dir) { |
| 503 | case SPI_MEM_DATA_IN: |
| 504 | err = f_ospi_indir_read(ospi, op); |
| 505 | break; |
| 506 | |
| 507 | case SPI_MEM_DATA_OUT: |
| 508 | fallthrough; |
| 509 | case SPI_MEM_NO_DATA: |
| 510 | err = f_ospi_indir_write(ospi, op); |
| 511 | break; |
| 512 | |
| 513 | default: |
| 514 | dev_warn(ospi->dev, "Unsupported direction"); |
| 515 | err = -EOPNOTSUPP; |
| 516 | } |
| 517 | |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | static bool f_ospi_supports_op_width(const struct spi_mem_op *op) |
| 522 | { |
| 523 | u8 width_available[] = { 0, 1, 2, 4, 8 }; |
| 524 | u8 width_op[] = { op->cmd.buswidth, op->addr.buswidth, |
| 525 | op->dummy.buswidth, op->data.buswidth }; |
| 526 | bool is_match_found; |
| 527 | int i, j; |
| 528 | |
| 529 | for (i = 0; i < ARRAY_SIZE(width_op); i++) { |
| 530 | is_match_found = false; |
| 531 | |
| 532 | for (j = 0; j < ARRAY_SIZE(width_available); j++) { |
| 533 | if (width_op[i] == width_available[j]) { |
| 534 | is_match_found = true; |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (!is_match_found) |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | static bool f_ospi_supports_op(struct spi_slave *slave, |
| 547 | const struct spi_mem_op *op) |
| 548 | { |
| 549 | if (f_ospi_get_dummy_cycle(op) > OSPI_DUMMY_CYCLE_MAX) |
| 550 | return false; |
| 551 | |
| 552 | if (op->addr.nbytes > 4) |
| 553 | return false; |
| 554 | |
| 555 | if (!f_ospi_supports_op_width(op)) |
| 556 | return false; |
| 557 | |
Kunihiko Hayashi | 468212a | 2023-03-27 14:34:51 +0900 | [diff] [blame] | 558 | return spi_mem_default_supports_op(slave, op); |
Kunihiko Hayashi | 7509ea1 | 2022-11-29 11:17:09 +0900 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static int f_ospi_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op) |
| 562 | { |
| 563 | op->data.nbytes = min((int)op->data.nbytes, (int)(OSPI_DAT_SIZE_MAX)); |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static const struct spi_controller_mem_ops f_ospi_mem_ops = { |
| 569 | .adjust_op_size = f_ospi_adjust_op_size, |
| 570 | .supports_op = f_ospi_supports_op, |
| 571 | .exec_op = f_ospi_exec_op, |
| 572 | }; |
| 573 | |
| 574 | static int f_ospi_set_speed(struct udevice *bus, u32 speed) |
| 575 | { |
| 576 | struct f_ospi *ospi = dev_get_priv(bus); |
| 577 | |
| 578 | ospi->max_speed_hz = speed; |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int f_ospi_set_mode(struct udevice *bus, u32 mode) |
| 584 | { |
| 585 | struct f_ospi *ospi = dev_get_priv(bus); |
| 586 | |
| 587 | ospi->mode = mode; |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int f_ospi_init(struct f_ospi *ospi) |
| 593 | { |
| 594 | int ret; |
| 595 | |
| 596 | ret = f_ospi_prepare_config(ospi); |
| 597 | if (ret) |
| 598 | return ret; |
| 599 | |
| 600 | /* Disable boot signal */ |
| 601 | writel(OSPI_ACC_MODE_BOOT_DISABLE, ospi->base + OSPI_ACC_MODE); |
| 602 | |
| 603 | f_ospi_config_dll(ospi); |
| 604 | |
| 605 | /* Disable IRQ */ |
| 606 | f_ospi_clear_irq(ospi); |
| 607 | f_ospi_disable_irq_status(ospi, OSPI_IRQ_ALL); |
| 608 | f_ospi_disable_irq_output(ospi, OSPI_IRQ_ALL); |
| 609 | |
| 610 | return f_ospi_unprepare_config(ospi); |
| 611 | } |
| 612 | |
| 613 | static int f_ospi_of_to_plat(struct udevice *dev) |
| 614 | { |
| 615 | struct f_ospi *ospi = dev_get_priv(dev); |
| 616 | |
| 617 | ospi->base = dev_read_addr_ptr(dev); |
| 618 | ospi->num_cs = dev_read_u32_default(dev, "num-cs", OSPI_NUM_CS); |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static int f_ospi_probe(struct udevice *dev) |
| 624 | { |
| 625 | struct f_ospi *ospi = dev_get_priv(dev); |
| 626 | int ret; |
| 627 | |
| 628 | ospi->dev = dev; |
| 629 | |
| 630 | ret = clk_get_by_index(dev, 0, &ospi->clk); |
| 631 | if (ret < 0) { |
| 632 | dev_err(dev, "Failed to get clock\n"); |
| 633 | goto err_put_ctlr; |
| 634 | } |
| 635 | |
| 636 | ret = clk_enable(&ospi->clk); |
| 637 | if (ret) { |
| 638 | dev_err(dev, "Failed to enable the clock\n"); |
| 639 | goto err_put_ctlr; |
| 640 | } |
| 641 | |
| 642 | ret = f_ospi_init(ospi); |
| 643 | if (ret) |
| 644 | goto err_disable_clk; |
| 645 | |
| 646 | return 0; |
| 647 | |
| 648 | err_disable_clk: |
| 649 | clk_disable(&ospi->clk); |
| 650 | |
| 651 | err_put_ctlr: |
| 652 | dev_err(dev, "Socionext F_OSPI probe failed\n"); |
| 653 | return ret; |
| 654 | } |
| 655 | |
| 656 | static int f_ospi_remove(struct udevice *dev) |
| 657 | { |
| 658 | struct f_ospi *ospi = dev_get_priv(dev); |
| 659 | |
| 660 | clk_disable(&ospi->clk); |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static const struct dm_spi_ops f_ospi_ops = { |
| 666 | .set_speed = f_ospi_set_speed, |
| 667 | .set_mode = f_ospi_set_mode, |
| 668 | .mem_ops = &f_ospi_mem_ops, |
| 669 | }; |
| 670 | |
| 671 | static const struct udevice_id f_ospi_dt_ids[] = { |
| 672 | { .compatible = "socionext,f-ospi" }, |
| 673 | {} |
| 674 | }; |
| 675 | |
| 676 | U_BOOT_DRIVER(f_ospi) = { |
| 677 | .name = "sn-f-ospi", |
| 678 | .id = UCLASS_SPI, |
| 679 | .of_match = f_ospi_dt_ids, |
| 680 | .of_to_plat = f_ospi_of_to_plat, |
| 681 | .ops = &f_ospi_ops, |
| 682 | .probe = f_ospi_probe, |
| 683 | .remove = f_ospi_remove, |
| 684 | .priv_auto = sizeof(struct f_ospi), |
| 685 | }; |