Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi: intel: Add support for second flash chip

Intel SPI flash controller has been supporting two chip selects long
time already even if the most common configuration is to have a single
flash chip for the BIOS and related data. This adds support for the
second chip select if we find out that there are two flash components
(this information is available in the mandatory flash descriptor on the
first chip). The second chip is exposed as is without any partition
information.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Link: https://lore.kernel.org/r/20220816130818.89600-1-mika.westerberg@linux.intel.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Mika Westerberg and committed by
Mark Brown
3f03c618 51e99de5

+147 -16
+147 -16
drivers/spi/spi-intel.c
··· 116 116 #define ERASE_64K_OPCODE_SHIFT 16 117 117 #define ERASE_64K_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT) 118 118 119 + /* Flash descriptor fields */ 120 + #define FLVALSIG_MAGIC 0x0ff0a55a 121 + #define FLMAP0_NC_MASK GENMASK(9, 8) 122 + #define FLMAP0_NC_SHIFT 8 123 + #define FLMAP0_FCBA_MASK GENMASK(7, 0) 124 + 125 + #define FLCOMP_C0DEN_MASK GENMASK(3, 0) 126 + #define FLCOMP_C0DEN_512K 0x00 127 + #define FLCOMP_C0DEN_1M 0x01 128 + #define FLCOMP_C0DEN_2M 0x02 129 + #define FLCOMP_C0DEN_4M 0x03 130 + #define FLCOMP_C0DEN_8M 0x04 131 + #define FLCOMP_C0DEN_16M 0x05 132 + #define FLCOMP_C0DEN_32M 0x06 133 + #define FLCOMP_C0DEN_64M 0x07 134 + 119 135 #define INTEL_SPI_TIMEOUT 5000 /* ms */ 120 136 #define INTEL_SPI_FIFO_SZ 64 121 137 ··· 145 129 * @master: Pointer to the SPI controller structure 146 130 * @nregions: Maximum number of regions 147 131 * @pr_num: Maximum number of protected range registers 132 + * @chip0_size: Size of the first flash chip in bytes 148 133 * @locked: Is SPI setting locked 149 134 * @swseq_reg: Use SW sequencer in register reads/writes 150 135 * @swseq_erase: Use SW sequencer in erase operation ··· 163 146 struct spi_controller *master; 164 147 size_t nregions; 165 148 size_t pr_num; 149 + size_t chip0_size; 166 150 bool locked; 167 151 bool swseq_reg; 168 152 bool swseq_erase; ··· 176 158 struct spi_mem_op mem_op; 177 159 u32 replacement_op; 178 160 int (*exec_op)(struct intel_spi *ispi, 161 + const struct spi_mem *mem, 179 162 const struct intel_spi_mem_op *iop, 180 163 const struct spi_mem_op *op); 181 164 }; ··· 460 441 return 0; 461 442 } 462 443 463 - static int intel_spi_read_reg(struct intel_spi *ispi, 444 + static u32 intel_spi_chip_addr(const struct intel_spi *ispi, 445 + const struct spi_mem *mem) 446 + { 447 + /* Pick up the correct start address */ 448 + if (!mem) 449 + return 0; 450 + return mem->spi->chip_select == 1 ? ispi->chip0_size : 0; 451 + } 452 + 453 + static int intel_spi_read_reg(struct intel_spi *ispi, const struct spi_mem *mem, 464 454 const struct intel_spi_mem_op *iop, 465 455 const struct spi_mem_op *op) 466 456 { ··· 477 449 u8 opcode = op->cmd.opcode; 478 450 int ret; 479 451 480 - /* Address of the first chip */ 481 - writel(0, ispi->base + FADDR); 452 + writel(intel_spi_chip_addr(ispi, mem), ispi->base + FADDR); 482 453 483 454 if (ispi->swseq_reg) 484 455 ret = intel_spi_sw_cycle(ispi, opcode, nbytes, ··· 491 464 return intel_spi_read_block(ispi, op->data.buf.in, nbytes); 492 465 } 493 466 494 - static int intel_spi_write_reg(struct intel_spi *ispi, 467 + static int intel_spi_write_reg(struct intel_spi *ispi, const struct spi_mem *mem, 495 468 const struct intel_spi_mem_op *iop, 496 469 const struct spi_mem_op *op) 497 470 { ··· 538 511 if (opcode == SPINOR_OP_WRDI) 539 512 return 0; 540 513 541 - writel(0, ispi->base + FADDR); 514 + writel(intel_spi_chip_addr(ispi, mem), ispi->base + FADDR); 542 515 543 516 /* Write the value beforehand */ 544 517 ret = intel_spi_write_block(ispi, op->data.buf.out, nbytes); ··· 551 524 return intel_spi_hw_cycle(ispi, opcode, nbytes); 552 525 } 553 526 554 - static int intel_spi_read(struct intel_spi *ispi, 527 + static int intel_spi_read(struct intel_spi *ispi, const struct spi_mem *mem, 555 528 const struct intel_spi_mem_op *iop, 556 529 const struct spi_mem_op *op) 557 530 { 558 - void *read_buf = op->data.buf.in; 531 + u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val; 559 532 size_t block_size, nbytes = op->data.nbytes; 560 - u32 addr = op->addr.val; 533 + void *read_buf = op->data.buf.in; 561 534 u32 val, status; 562 535 int ret; 563 536 ··· 612 585 return 0; 613 586 } 614 587 615 - static int intel_spi_write(struct intel_spi *ispi, 588 + static int intel_spi_write(struct intel_spi *ispi, const struct spi_mem *mem, 616 589 const struct intel_spi_mem_op *iop, 617 590 const struct spi_mem_op *op) 618 591 { 592 + u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val; 619 593 size_t block_size, nbytes = op->data.nbytes; 620 594 const void *write_buf = op->data.buf.out; 621 - u32 addr = op->addr.val; 622 595 u32 val, status; 623 596 int ret; 624 597 ··· 675 648 return 0; 676 649 } 677 650 678 - static int intel_spi_erase(struct intel_spi *ispi, 651 + static int intel_spi_erase(struct intel_spi *ispi, const struct spi_mem *mem, 679 652 const struct intel_spi_mem_op *iop, 680 653 const struct spi_mem_op *op) 681 654 { 655 + u32 addr = intel_spi_chip_addr(ispi, mem) + op->addr.val; 682 656 u8 opcode = op->cmd.opcode; 683 - u32 addr = op->addr.val; 684 657 u32 val, status; 685 658 int ret; 686 659 ··· 792 765 if (!iop) 793 766 return -EOPNOTSUPP; 794 767 795 - return iop->exec_op(ispi, iop, op); 768 + return iop->exec_op(ispi, mem, iop, op); 796 769 } 797 770 798 771 static const char *intel_spi_get_name(struct spi_mem *mem) ··· 832 805 op.data.nbytes = len; 833 806 op.data.buf.in = buf; 834 807 835 - ret = iop->exec_op(ispi, iop, &op); 808 + ret = iop->exec_op(ispi, desc->mem, iop, &op); 836 809 return ret ? ret : len; 837 810 } 838 811 ··· 848 821 op.data.nbytes = len; 849 822 op.data.buf.out = buf; 850 823 851 - ret = iop->exec_op(ispi, iop, &op); 824 + ret = iop->exec_op(ispi, desc->mem, iop, &op); 852 825 return ret ? ret : len; 853 826 } 854 827 ··· 1253 1226 } 1254 1227 } 1255 1228 1229 + static int intel_spi_read_desc(struct intel_spi *ispi) 1230 + { 1231 + struct spi_mem_op op = 1232 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_READ, 0), 1233 + SPI_MEM_OP_ADDR(3, 0, 0), 1234 + SPI_MEM_OP_NO_DUMMY, 1235 + SPI_MEM_OP_DATA_IN(0, NULL, 0)); 1236 + u32 buf[2], nc, fcba, flcomp; 1237 + ssize_t ret; 1238 + 1239 + op.addr.val = 0x10; 1240 + op.data.buf.in = buf; 1241 + op.data.nbytes = sizeof(buf); 1242 + 1243 + ret = intel_spi_read(ispi, NULL, NULL, &op); 1244 + if (ret) { 1245 + dev_warn(ispi->dev, "failed to read descriptor\n"); 1246 + return ret; 1247 + } 1248 + 1249 + dev_dbg(ispi->dev, "FLVALSIG=0x%08x\n", buf[0]); 1250 + dev_dbg(ispi->dev, "FLMAP0=0x%08x\n", buf[1]); 1251 + 1252 + if (buf[0] != FLVALSIG_MAGIC) { 1253 + dev_warn(ispi->dev, "descriptor signature not valid\n"); 1254 + return -ENODEV; 1255 + } 1256 + 1257 + fcba = (buf[1] & FLMAP0_FCBA_MASK) << 4; 1258 + dev_dbg(ispi->dev, "FCBA=%#x\n", fcba); 1259 + 1260 + op.addr.val = fcba; 1261 + op.data.buf.in = &flcomp; 1262 + op.data.nbytes = sizeof(flcomp); 1263 + 1264 + ret = intel_spi_read(ispi, NULL, NULL, &op); 1265 + if (ret) { 1266 + dev_warn(ispi->dev, "failed to read FLCOMP\n"); 1267 + return -ENODEV; 1268 + } 1269 + 1270 + dev_dbg(ispi->dev, "FLCOMP=0x%08x\n", flcomp); 1271 + 1272 + switch (flcomp & FLCOMP_C0DEN_MASK) { 1273 + case FLCOMP_C0DEN_512K: 1274 + ispi->chip0_size = SZ_512K; 1275 + break; 1276 + case FLCOMP_C0DEN_1M: 1277 + ispi->chip0_size = SZ_1M; 1278 + break; 1279 + case FLCOMP_C0DEN_2M: 1280 + ispi->chip0_size = SZ_2M; 1281 + break; 1282 + case FLCOMP_C0DEN_4M: 1283 + ispi->chip0_size = SZ_4M; 1284 + break; 1285 + case FLCOMP_C0DEN_8M: 1286 + ispi->chip0_size = SZ_8M; 1287 + break; 1288 + case FLCOMP_C0DEN_16M: 1289 + ispi->chip0_size = SZ_16M; 1290 + break; 1291 + case FLCOMP_C0DEN_32M: 1292 + ispi->chip0_size = SZ_32M; 1293 + break; 1294 + case FLCOMP_C0DEN_64M: 1295 + ispi->chip0_size = SZ_64M; 1296 + break; 1297 + default: 1298 + return -EINVAL; 1299 + } 1300 + 1301 + dev_dbg(ispi->dev, "chip0 size %zd KB\n", ispi->chip0_size / SZ_1K); 1302 + 1303 + nc = (buf[1] & FLMAP0_NC_MASK) >> FLMAP0_NC_SHIFT; 1304 + if (!nc) 1305 + ispi->master->num_chipselect = 1; 1306 + else if (nc == 1) 1307 + ispi->master->num_chipselect = 2; 1308 + else 1309 + return -EINVAL; 1310 + 1311 + dev_dbg(ispi->dev, "%u flash components found\n", 1312 + ispi->master->num_chipselect); 1313 + return 0; 1314 + } 1315 + 1256 1316 static int intel_spi_populate_chip(struct intel_spi *ispi) 1257 1317 { 1258 1318 struct flash_platform_data *pdata; 1259 1319 struct spi_board_info chip; 1320 + int ret; 1260 1321 1261 1322 pdata = devm_kzalloc(ispi->dev, sizeof(*pdata), GFP_KERNEL); 1262 1323 if (!pdata) ··· 1362 1247 snprintf(chip.modalias, 8, "spi-nor"); 1363 1248 chip.platform_data = pdata; 1364 1249 1365 - return spi_new_device(ispi->master, &chip) ? 0 : -ENODEV; 1250 + if (!spi_new_device(ispi->master, &chip)) 1251 + return -ENODEV; 1252 + 1253 + /* Add the second chip if present */ 1254 + if (ispi->master->num_chipselect < 2) 1255 + return 0; 1256 + 1257 + ret = intel_spi_read_desc(ispi); 1258 + if (ret) 1259 + return ret; 1260 + 1261 + chip.platform_data = NULL; 1262 + chip.chip_select = 1; 1263 + 1264 + if (!spi_new_device(ispi->master, &chip)) 1265 + return -ENODEV; 1266 + return 0; 1366 1267 } 1367 1268 1368 1269 /**