Jim Liu | 464b7cd | 2022-04-26 16:52:45 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2022 Nuvoton Technology Corp. |
| 4 | * NPCM Flash Interface Unit(FIU) SPI master controller driver. |
| 5 | */ |
| 6 | |
| 7 | #include <clk.h> |
| 8 | #include <dm.h> |
| 9 | #include <spi.h> |
| 10 | #include <spi-mem.h> |
| 11 | #include <linux/bitfield.h> |
| 12 | #include <linux/log2.h> |
| 13 | #include <linux/iopoll.h> |
| 14 | |
| 15 | #define DW_SIZE 4 |
| 16 | #define CHUNK_SIZE 16 |
| 17 | #define XFER_TIMEOUT 1000000 |
| 18 | |
| 19 | /* FIU UMA Configuration Register (UMA_CFG) */ |
| 20 | #define UMA_CFG_RDATSIZ_MASK GENMASK(28, 24) |
| 21 | #define UMA_CFG_DBSIZ_MASK GENMASK(23, 21) |
| 22 | #define UMA_CFG_WDATSIZ_MASK GENMASK(20, 16) |
| 23 | #define UMA_CFG_ADDSIZ_MASK GENMASK(13, 11) |
| 24 | #define UMA_CFG_RDBPCK_MASK GENMASK(9, 8) |
| 25 | #define UMA_CFG_DBPCK_MASK GENMASK(7, 6) |
| 26 | #define UMA_CFG_WDBPCK_MASK GENMASK(5, 4) |
| 27 | #define UMA_CFG_ADBPCK_MASK GENMASK(3, 2) |
| 28 | #define UMA_CFG_CMBPCK_MASK GENMASK(1, 0) |
| 29 | #define UMA_CFG_CMDSIZ_SHIFT 10 |
| 30 | |
| 31 | /* FIU UMA Control and Status Register (UMA_CTS) */ |
| 32 | #define UMA_CTS_SW_CS BIT(16) |
| 33 | #define UMA_CTS_EXEC_DONE BIT(0) |
| 34 | #define UMA_CTS_RDYST BIT(24) |
| 35 | #define UMA_CTS_DEV_NUM_MASK GENMASK(9, 8) |
| 36 | |
| 37 | struct npcm_fiu_regs { |
| 38 | unsigned int drd_cfg; |
| 39 | unsigned int dwr_cfg; |
| 40 | unsigned int uma_cfg; |
| 41 | unsigned int uma_cts; |
| 42 | unsigned int uma_cmd; |
| 43 | unsigned int uma_addr; |
| 44 | unsigned int prt_cfg; |
| 45 | unsigned char res1[4]; |
| 46 | unsigned int uma_dw0; |
| 47 | unsigned int uma_dw1; |
| 48 | unsigned int uma_dw2; |
| 49 | unsigned int uma_dw3; |
| 50 | unsigned int uma_dr0; |
| 51 | unsigned int uma_dr1; |
| 52 | unsigned int uma_dr2; |
| 53 | unsigned int uma_dr3; |
| 54 | unsigned int prt_cmd0; |
| 55 | unsigned int prt_cmd1; |
| 56 | unsigned int prt_cmd2; |
| 57 | unsigned int prt_cmd3; |
| 58 | unsigned int prt_cmd4; |
| 59 | unsigned int prt_cmd5; |
| 60 | unsigned int prt_cmd6; |
| 61 | unsigned int prt_cmd7; |
| 62 | unsigned int prt_cmd8; |
| 63 | unsigned int prt_cmd9; |
| 64 | unsigned int stuff[4]; |
| 65 | unsigned int fiu_cfg; |
| 66 | }; |
| 67 | |
| 68 | struct npcm_fiu_priv { |
| 69 | struct npcm_fiu_regs *regs; |
| 70 | struct clk clk; |
| 71 | }; |
| 72 | |
| 73 | static int npcm_fiu_spi_set_speed(struct udevice *bus, uint speed) |
| 74 | { |
| 75 | struct npcm_fiu_priv *priv = dev_get_priv(bus); |
| 76 | int ret; |
| 77 | |
| 78 | debug("%s: set speed %u\n", bus->name, speed); |
| 79 | ret = clk_set_rate(&priv->clk, speed); |
| 80 | if (ret < 0) |
| 81 | return ret; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int npcm_fiu_spi_set_mode(struct udevice *bus, uint mode) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static inline void activate_cs(struct npcm_fiu_regs *regs, int cs) |
| 92 | { |
| 93 | writel(FIELD_PREP(UMA_CTS_DEV_NUM_MASK, cs), ®s->uma_cts); |
| 94 | } |
| 95 | |
| 96 | static inline void deactivate_cs(struct npcm_fiu_regs *regs, int cs) |
| 97 | { |
| 98 | writel(FIELD_PREP(UMA_CTS_DEV_NUM_MASK, cs) | UMA_CTS_SW_CS, ®s->uma_cts); |
| 99 | } |
| 100 | |
| 101 | static int fiu_uma_read(struct udevice *bus, u8 *buf, u32 size) |
| 102 | { |
| 103 | struct npcm_fiu_priv *priv = dev_get_priv(bus); |
| 104 | struct npcm_fiu_regs *regs = priv->regs; |
| 105 | u32 data_reg[4]; |
| 106 | u32 val; |
| 107 | int ret; |
| 108 | |
| 109 | /* Set data size */ |
| 110 | writel(FIELD_PREP(UMA_CFG_RDATSIZ_MASK, size), ®s->uma_cfg); |
| 111 | |
| 112 | /* Initiate the read */ |
| 113 | writel(readl(®s->uma_cts) | UMA_CTS_EXEC_DONE, ®s->uma_cts); |
| 114 | |
| 115 | /* Wait for completion */ |
| 116 | ret = readl_poll_timeout(®s->uma_cts, val, |
| 117 | !(val & UMA_CTS_EXEC_DONE), XFER_TIMEOUT); |
| 118 | if (ret) { |
| 119 | printf("npcm_fiu: read timeout\n"); |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | /* Copy data from data registers */ |
| 124 | if (size) |
| 125 | data_reg[0] = readl(®s->uma_dr0); |
| 126 | if (size > DW_SIZE) |
| 127 | data_reg[1] = readl(®s->uma_dr1); |
| 128 | if (size > DW_SIZE * 2) |
| 129 | data_reg[2] = readl(®s->uma_dr2); |
| 130 | if (size > DW_SIZE * 3) |
| 131 | data_reg[3] = readl(®s->uma_dr3); |
| 132 | memcpy(buf, data_reg, size); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int fiu_uma_write(struct udevice *bus, const u8 *buf, u32 size) |
| 138 | { |
| 139 | struct npcm_fiu_priv *priv = dev_get_priv(bus); |
| 140 | struct npcm_fiu_regs *regs = priv->regs; |
| 141 | u32 data_reg[4]; |
| 142 | u32 val; |
| 143 | int ret; |
| 144 | |
| 145 | /* Set data size */ |
| 146 | writel(FIELD_PREP(UMA_CFG_WDATSIZ_MASK, size), ®s->uma_cfg); |
| 147 | |
| 148 | /* Write data to data registers */ |
| 149 | memcpy(data_reg, buf, size); |
| 150 | if (size) |
| 151 | writel(data_reg[0], ®s->uma_dw0); |
| 152 | if (size > DW_SIZE) |
| 153 | writel(data_reg[1], ®s->uma_dw1); |
| 154 | if (size > DW_SIZE * 2) |
| 155 | writel(data_reg[2], ®s->uma_dw2); |
| 156 | if (size > DW_SIZE * 3) |
| 157 | writel(data_reg[3], ®s->uma_dw3); |
| 158 | |
| 159 | /* Initiate the transaction */ |
| 160 | writel(readl(®s->uma_cts) | UMA_CTS_EXEC_DONE, ®s->uma_cts); |
| 161 | |
| 162 | /* Wait for completion */ |
| 163 | ret = readl_poll_timeout(®s->uma_cts, val, |
| 164 | !(val & UMA_CTS_EXEC_DONE), XFER_TIMEOUT); |
| 165 | if (ret) |
| 166 | printf("npcm_fiu: write timeout\n"); |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static int npcm_fiu_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 172 | const void *dout, void *din, unsigned long flags) |
| 173 | { |
| 174 | struct udevice *bus = dev->parent; |
| 175 | struct npcm_fiu_priv *priv = dev_get_priv(bus); |
| 176 | struct npcm_fiu_regs *regs = priv->regs; |
| 177 | struct dm_spi_slave_plat *slave_plat = |
| 178 | dev_get_parent_plat(dev); |
| 179 | const u8 *tx = dout; |
| 180 | u8 *rx = din; |
| 181 | int bytes = bitlen / 8; |
| 182 | int ret = 0; |
| 183 | int len; |
| 184 | |
| 185 | if (flags & SPI_XFER_BEGIN) |
| 186 | activate_cs(regs, slave_plat->cs); |
| 187 | |
| 188 | while (bytes) { |
| 189 | len = (bytes > CHUNK_SIZE) ? CHUNK_SIZE : bytes; |
| 190 | if (tx) { |
| 191 | ret = fiu_uma_write(bus, tx, len); |
| 192 | if (ret) |
| 193 | break; |
| 194 | tx += len; |
| 195 | } else { |
| 196 | ret = fiu_uma_read(bus, rx, len); |
| 197 | if (ret) |
| 198 | break; |
| 199 | rx += len; |
| 200 | } |
| 201 | bytes -= len; |
| 202 | } |
| 203 | |
| 204 | if (flags & SPI_XFER_END) |
| 205 | deactivate_cs(regs, slave_plat->cs); |
| 206 | |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static int npcm_fiu_uma_operation(struct npcm_fiu_priv *priv, const struct spi_mem_op *op, |
| 211 | u32 addr, const u8 *tx, u8 *rx, u32 nbytes, bool started) |
| 212 | { |
| 213 | struct npcm_fiu_regs *regs = priv->regs; |
| 214 | u32 uma_cfg = 0, val; |
| 215 | u32 data_reg[4]; |
| 216 | int ret; |
| 217 | |
| 218 | debug("fiu_uma: opcode 0x%x, dir %d, addr 0x%x, %d bytes\n", |
| 219 | op->cmd.opcode, op->data.dir, addr, nbytes); |
| 220 | debug(" buswidth cmd:%d, addr:%d, dummy:%d, data:%d\n", |
| 221 | op->cmd.buswidth, op->addr.buswidth, op->dummy.buswidth, |
| 222 | op->data.buswidth); |
| 223 | debug(" size cmd:%d, addr:%d, dummy:%d, data:%d\n", |
| 224 | 1, op->addr.nbytes, op->dummy.nbytes, op->data.nbytes); |
| 225 | debug(" tx %p, rx %p\n", tx, rx); |
| 226 | |
| 227 | if (!started) { |
| 228 | /* Send cmd/addr in the begin of an transaction */ |
| 229 | writel(op->cmd.opcode, ®s->uma_cmd); |
| 230 | |
| 231 | uma_cfg |= FIELD_PREP(UMA_CFG_CMBPCK_MASK, ilog2(op->cmd.buswidth)) | |
| 232 | (1 << UMA_CFG_CMDSIZ_SHIFT); |
| 233 | /* Configure addr bytes */ |
| 234 | if (op->addr.nbytes) { |
| 235 | uma_cfg |= FIELD_PREP(UMA_CFG_ADBPCK_MASK, ilog2(op->addr.buswidth)) | |
| 236 | FIELD_PREP(UMA_CFG_ADDSIZ_MASK, op->addr.nbytes); |
| 237 | writel(addr, ®s->uma_addr); |
| 238 | } |
| 239 | /* Configure dummy bytes */ |
| 240 | if (op->dummy.nbytes) |
| 241 | uma_cfg |= FIELD_PREP(UMA_CFG_DBPCK_MASK, ilog2(op->dummy.buswidth)) | |
| 242 | FIELD_PREP(UMA_CFG_DBSIZ_MASK, op->dummy.nbytes); |
| 243 | } |
| 244 | /* Set data bus width and data size */ |
| 245 | if (op->data.dir == SPI_MEM_DATA_IN && nbytes) |
| 246 | uma_cfg |= FIELD_PREP(UMA_CFG_RDBPCK_MASK, ilog2(op->data.buswidth)) | |
| 247 | FIELD_PREP(UMA_CFG_RDATSIZ_MASK, nbytes); |
| 248 | else if (op->data.dir == SPI_MEM_DATA_OUT && nbytes) |
| 249 | uma_cfg |= FIELD_PREP(UMA_CFG_WDBPCK_MASK, ilog2(op->data.buswidth)) | |
| 250 | FIELD_PREP(UMA_CFG_WDATSIZ_MASK, nbytes); |
| 251 | writel(uma_cfg, ®s->uma_cfg); |
| 252 | |
| 253 | if (op->data.dir == SPI_MEM_DATA_OUT && nbytes) { |
| 254 | memcpy(data_reg, tx, nbytes); |
| 255 | |
| 256 | if (nbytes) |
| 257 | writel(data_reg[0], ®s->uma_dw0); |
| 258 | if (nbytes > DW_SIZE) |
| 259 | writel(data_reg[1], ®s->uma_dw1); |
| 260 | if (nbytes > DW_SIZE * 2) |
| 261 | writel(data_reg[2], ®s->uma_dw2); |
| 262 | if (nbytes > DW_SIZE * 3) |
| 263 | writel(data_reg[3], ®s->uma_dw3); |
| 264 | } |
| 265 | /* Initiate the transaction */ |
| 266 | writel(readl(®s->uma_cts) | UMA_CTS_EXEC_DONE, ®s->uma_cts); |
| 267 | |
| 268 | /* Wait for completion */ |
| 269 | ret = readl_poll_timeout(®s->uma_cts, val, |
| 270 | !(val & UMA_CTS_EXEC_DONE), XFER_TIMEOUT); |
| 271 | if (ret) { |
| 272 | printf("npcm_fiu: UMA op timeout\n"); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | if (op->data.dir == SPI_MEM_DATA_IN && nbytes) { |
| 277 | if (nbytes) |
| 278 | data_reg[0] = readl(®s->uma_dr0); |
| 279 | if (nbytes > DW_SIZE) |
| 280 | data_reg[1] = readl(®s->uma_dr1); |
| 281 | if (nbytes > DW_SIZE * 2) |
| 282 | data_reg[2] = readl(®s->uma_dr2); |
| 283 | if (nbytes > DW_SIZE * 3) |
| 284 | data_reg[3] = readl(®s->uma_dr3); |
| 285 | |
| 286 | memcpy(rx, data_reg, nbytes); |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int npcm_fiu_exec_op(struct spi_slave *slave, |
| 293 | const struct spi_mem_op *op) |
| 294 | { |
| 295 | struct udevice *bus = slave->dev->parent; |
| 296 | struct npcm_fiu_priv *priv = dev_get_priv(bus); |
| 297 | struct npcm_fiu_regs *regs = priv->regs; |
| 298 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(slave->dev); |
| 299 | u32 bytes, len, addr; |
| 300 | const u8 *tx; |
| 301 | u8 *rx; |
| 302 | bool started = false; |
| 303 | int ret; |
| 304 | |
| 305 | bytes = op->data.nbytes; |
| 306 | addr = (u32)op->addr.val; |
| 307 | if (!bytes) { |
| 308 | activate_cs(regs, slave_plat->cs); |
| 309 | ret = npcm_fiu_uma_operation(priv, op, addr, NULL, NULL, 0, false); |
| 310 | deactivate_cs(regs, slave_plat->cs); |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | tx = op->data.buf.out; |
| 315 | rx = op->data.buf.in; |
| 316 | /* |
| 317 | * Use SW-control CS for write to extend the transaction and |
| 318 | * keep the Write Enable state. |
| 319 | * Use HW-control CS for read to avoid clock and timing issues. |
| 320 | */ |
| 321 | if (op->data.dir == SPI_MEM_DATA_OUT) |
| 322 | activate_cs(regs, slave_plat->cs); |
| 323 | else |
| 324 | writel(FIELD_PREP(UMA_CTS_DEV_NUM_MASK, slave_plat->cs) | UMA_CTS_SW_CS, |
| 325 | ®s->uma_cts); |
| 326 | while (bytes) { |
| 327 | len = (bytes > CHUNK_SIZE) ? CHUNK_SIZE : bytes; |
| 328 | ret = npcm_fiu_uma_operation(priv, op, addr, tx, rx, len, started); |
| 329 | if (ret) |
| 330 | return ret; |
| 331 | |
| 332 | /* CS is kept low for uma write, extend the transaction */ |
| 333 | if (op->data.dir == SPI_MEM_DATA_OUT) |
| 334 | started = true; |
| 335 | |
| 336 | bytes -= len; |
| 337 | addr += len; |
| 338 | if (tx) |
| 339 | tx += len; |
| 340 | if (rx) |
| 341 | rx += len; |
| 342 | } |
| 343 | if (op->data.dir == SPI_MEM_DATA_OUT) |
| 344 | deactivate_cs(regs, slave_plat->cs); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int npcm_fiu_spi_probe(struct udevice *bus) |
| 350 | { |
| 351 | struct npcm_fiu_priv *priv = dev_get_priv(bus); |
| 352 | int ret; |
| 353 | |
| 354 | priv->regs = (struct npcm_fiu_regs *)dev_read_addr_ptr(bus); |
| 355 | |
| 356 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 357 | if (ret < 0) |
| 358 | return ret; |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static const struct spi_controller_mem_ops npcm_fiu_mem_ops = { |
| 364 | .exec_op = npcm_fiu_exec_op, |
| 365 | }; |
| 366 | |
| 367 | static const struct dm_spi_ops npcm_fiu_spi_ops = { |
| 368 | .xfer = npcm_fiu_spi_xfer, |
| 369 | .set_speed = npcm_fiu_spi_set_speed, |
| 370 | .set_mode = npcm_fiu_spi_set_mode, |
| 371 | .mem_ops = &npcm_fiu_mem_ops, |
| 372 | }; |
| 373 | |
| 374 | static const struct udevice_id npcm_fiu_spi_ids[] = { |
| 375 | { .compatible = "nuvoton,npcm845-fiu" }, |
| 376 | { .compatible = "nuvoton,npcm750-fiu" }, |
| 377 | { } |
| 378 | }; |
| 379 | |
| 380 | U_BOOT_DRIVER(npcm_fiu_spi) = { |
| 381 | .name = "npcm_fiu_spi", |
| 382 | .id = UCLASS_SPI, |
| 383 | .of_match = npcm_fiu_spi_ids, |
| 384 | .ops = &npcm_fiu_spi_ops, |
| 385 | .priv_auto = sizeof(struct npcm_fiu_priv), |
| 386 | .probe = npcm_fiu_spi_probe, |
| 387 | }; |