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

mtd: spi-nor: Move m25p80 code in spi-nor.c

The m25p80 driver is actually a generic wrapper around the spi-mem
layer. Not only the driver name is misleading, but we'd expect such a
common logic to be directly available in the core. Another reason for
moving this code is that SPI NOR controller drivers should
progressively be replaced by SPI controller drivers implementing the
spi_mem_ops interface, and when the conversion is done, we should have
a single spi-nor driver directly interfacing with the spi-mem layer.

While moving the code we also fix a longstanding issue when
non-DMA-able buffers are passed by the MTD layer.

Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
Signed-off-by: Vignesh Raghavendra <vigneshr@ti.com>
Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>

authored by

Boris Brezillon and committed by
Tudor Ambarus
b35b9a10 f173f26a

+605 -394
-18
drivers/mtd/devices/Kconfig
··· 79 79 other key product data. The second half is programmed with a 80 80 unique-to-each-chip bit pattern at the factory. 81 81 82 - config MTD_M25P80 83 - tristate "Support most SPI Flash chips (AT26DF, M25P, W25X, ...)" 84 - depends on SPI_MASTER && MTD_SPI_NOR 85 - select SPI_MEM 86 - help 87 - This enables access to most modern SPI flash chips, used for 88 - program and data storage. Series supported include Atmel AT26DF, 89 - Spansion S25SL, SST 25VF, ST M25P, and Winbond W25X. Other chips 90 - are supported as well. See the driver source for the current list, 91 - or to add other chips. 92 - 93 - Note that the original DataFlash chips (AT45 series, not AT26DF), 94 - need an entirely different driver. 95 - 96 - Set up your spi devices with the right board-specific platform data, 97 - if you want to specify device partitioning or to use a device which 98 - doesn't support the JEDEC ID instruction. 99 - 100 82 config MTD_MCHP23K256 101 83 tristate "Microchip 23K256 SRAM" 102 84 depends on SPI_MASTER
-1
drivers/mtd/devices/Makefile
··· 12 12 obj-$(CONFIG_MTD_LART) += lart.o 13 13 obj-$(CONFIG_MTD_BLOCK2MTD) += block2mtd.o 14 14 obj-$(CONFIG_MTD_DATAFLASH) += mtd_dataflash.o 15 - obj-$(CONFIG_MTD_M25P80) += m25p80.o 16 15 obj-$(CONFIG_MTD_MCHP23K256) += mchp23k256.o 17 16 obj-$(CONFIG_MTD_SPEAR_SMI) += spear_smi.o 18 17 obj-$(CONFIG_MTD_SST25L) += sst25l.o
-347
drivers/mtd/devices/m25p80.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * MTD SPI driver for ST M25Pxx (and similar) serial flash chips 4 - * 5 - * Author: Mike Lavender, mike@steroidmicros.com 6 - * 7 - * Copyright (c) 2005, Intec Automation Inc. 8 - * 9 - * Some parts are based on lart.c by Abraham Van Der Merwe 10 - * 11 - * Cleaned up and generalized based on mtd_dataflash.c 12 - */ 13 - 14 - #include <linux/err.h> 15 - #include <linux/errno.h> 16 - #include <linux/module.h> 17 - #include <linux/device.h> 18 - 19 - #include <linux/mtd/mtd.h> 20 - #include <linux/mtd/partitions.h> 21 - 22 - #include <linux/spi/spi.h> 23 - #include <linux/spi/spi-mem.h> 24 - #include <linux/spi/flash.h> 25 - #include <linux/mtd/spi-nor.h> 26 - 27 - struct m25p { 28 - struct spi_mem *spimem; 29 - struct spi_nor spi_nor; 30 - }; 31 - 32 - static int m25p80_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len) 33 - { 34 - struct m25p *flash = nor->priv; 35 - struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1), 36 - SPI_MEM_OP_NO_ADDR, 37 - SPI_MEM_OP_NO_DUMMY, 38 - SPI_MEM_OP_DATA_IN(len, NULL, 1)); 39 - void *scratchbuf; 40 - int ret; 41 - 42 - scratchbuf = kmalloc(len, GFP_KERNEL); 43 - if (!scratchbuf) 44 - return -ENOMEM; 45 - 46 - op.data.buf.in = scratchbuf; 47 - ret = spi_mem_exec_op(flash->spimem, &op); 48 - if (ret < 0) 49 - dev_err(&flash->spimem->spi->dev, "error %d reading %x\n", ret, 50 - code); 51 - else 52 - memcpy(val, scratchbuf, len); 53 - 54 - kfree(scratchbuf); 55 - 56 - return ret; 57 - } 58 - 59 - static int m25p80_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len) 60 - { 61 - struct m25p *flash = nor->priv; 62 - struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1), 63 - SPI_MEM_OP_NO_ADDR, 64 - SPI_MEM_OP_NO_DUMMY, 65 - SPI_MEM_OP_DATA_OUT(len, NULL, 1)); 66 - void *scratchbuf; 67 - int ret; 68 - 69 - scratchbuf = kmemdup(buf, len, GFP_KERNEL); 70 - if (!scratchbuf) 71 - return -ENOMEM; 72 - 73 - op.data.buf.out = scratchbuf; 74 - ret = spi_mem_exec_op(flash->spimem, &op); 75 - kfree(scratchbuf); 76 - 77 - return ret; 78 - } 79 - 80 - static ssize_t m25p80_write(struct spi_nor *nor, loff_t to, size_t len, 81 - const u_char *buf) 82 - { 83 - struct m25p *flash = nor->priv; 84 - struct spi_mem_op op = 85 - SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1), 86 - SPI_MEM_OP_ADDR(nor->addr_width, to, 1), 87 - SPI_MEM_OP_NO_DUMMY, 88 - SPI_MEM_OP_DATA_OUT(len, buf, 1)); 89 - int ret; 90 - 91 - /* get transfer protocols. */ 92 - op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto); 93 - op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto); 94 - op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); 95 - 96 - if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) 97 - op.addr.nbytes = 0; 98 - 99 - ret = spi_mem_adjust_op_size(flash->spimem, &op); 100 - if (ret) 101 - return ret; 102 - op.data.nbytes = len < op.data.nbytes ? len : op.data.nbytes; 103 - 104 - ret = spi_mem_exec_op(flash->spimem, &op); 105 - if (ret) 106 - return ret; 107 - 108 - return op.data.nbytes; 109 - } 110 - 111 - /* 112 - * Read an address range from the nor chip. The address range 113 - * may be any size provided it is within the physical boundaries. 114 - */ 115 - static ssize_t m25p80_read(struct spi_nor *nor, loff_t from, size_t len, 116 - u_char *buf) 117 - { 118 - struct m25p *flash = nor->priv; 119 - struct spi_mem_op op = 120 - SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1), 121 - SPI_MEM_OP_ADDR(nor->addr_width, from, 1), 122 - SPI_MEM_OP_DUMMY(nor->read_dummy, 1), 123 - SPI_MEM_OP_DATA_IN(len, buf, 1)); 124 - size_t remaining = len; 125 - int ret; 126 - 127 - /* get transfer protocols. */ 128 - op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto); 129 - op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto); 130 - op.dummy.buswidth = op.addr.buswidth; 131 - op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); 132 - 133 - /* convert the dummy cycles to the number of bytes */ 134 - op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; 135 - 136 - while (remaining) { 137 - op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX; 138 - ret = spi_mem_adjust_op_size(flash->spimem, &op); 139 - if (ret) 140 - return ret; 141 - 142 - ret = spi_mem_exec_op(flash->spimem, &op); 143 - if (ret) 144 - return ret; 145 - 146 - op.addr.val += op.data.nbytes; 147 - remaining -= op.data.nbytes; 148 - op.data.buf.in += op.data.nbytes; 149 - } 150 - 151 - return len; 152 - } 153 - 154 - /* 155 - * board specific setup should have ensured the SPI clock used here 156 - * matches what the READ command supports, at least until this driver 157 - * understands FAST_READ (for clocks over 25 MHz). 158 - */ 159 - static int m25p_probe(struct spi_mem *spimem) 160 - { 161 - struct spi_device *spi = spimem->spi; 162 - struct flash_platform_data *data; 163 - struct m25p *flash; 164 - struct spi_nor *nor; 165 - struct spi_nor_hwcaps hwcaps = { 166 - .mask = SNOR_HWCAPS_READ | 167 - SNOR_HWCAPS_READ_FAST | 168 - SNOR_HWCAPS_PP, 169 - }; 170 - char *flash_name; 171 - int ret; 172 - 173 - data = dev_get_platdata(&spimem->spi->dev); 174 - 175 - flash = devm_kzalloc(&spimem->spi->dev, sizeof(*flash), GFP_KERNEL); 176 - if (!flash) 177 - return -ENOMEM; 178 - 179 - nor = &flash->spi_nor; 180 - 181 - /* install the hooks */ 182 - nor->read = m25p80_read; 183 - nor->write = m25p80_write; 184 - nor->write_reg = m25p80_write_reg; 185 - nor->read_reg = m25p80_read_reg; 186 - 187 - nor->dev = &spimem->spi->dev; 188 - spi_nor_set_flash_node(nor, spi->dev.of_node); 189 - nor->priv = flash; 190 - 191 - spi_mem_set_drvdata(spimem, flash); 192 - flash->spimem = spimem; 193 - 194 - if (spi->mode & SPI_RX_OCTAL) { 195 - hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8; 196 - 197 - if (spi->mode & SPI_TX_OCTAL) 198 - hwcaps.mask |= (SNOR_HWCAPS_READ_1_8_8 | 199 - SNOR_HWCAPS_PP_1_1_8 | 200 - SNOR_HWCAPS_PP_1_8_8); 201 - } else if (spi->mode & SPI_RX_QUAD) { 202 - hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4; 203 - 204 - if (spi->mode & SPI_TX_QUAD) 205 - hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 | 206 - SNOR_HWCAPS_PP_1_1_4 | 207 - SNOR_HWCAPS_PP_1_4_4); 208 - } else if (spi->mode & SPI_RX_DUAL) { 209 - hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2; 210 - 211 - if (spi->mode & SPI_TX_DUAL) 212 - hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2; 213 - } 214 - 215 - if (data && data->name) 216 - nor->mtd.name = data->name; 217 - 218 - if (!nor->mtd.name) 219 - nor->mtd.name = spi_mem_get_name(spimem); 220 - 221 - /* For some (historical?) reason many platforms provide two different 222 - * names in flash_platform_data: "name" and "type". Quite often name is 223 - * set to "m25p80" and then "type" provides a real chip name. 224 - * If that's the case, respect "type" and ignore a "name". 225 - */ 226 - if (data && data->type) 227 - flash_name = data->type; 228 - else if (!strcmp(spi->modalias, "spi-nor")) 229 - flash_name = NULL; /* auto-detect */ 230 - else 231 - flash_name = spi->modalias; 232 - 233 - ret = spi_nor_scan(nor, flash_name, &hwcaps); 234 - if (ret) 235 - return ret; 236 - 237 - return mtd_device_register(&nor->mtd, data ? data->parts : NULL, 238 - data ? data->nr_parts : 0); 239 - } 240 - 241 - 242 - static int m25p_remove(struct spi_mem *spimem) 243 - { 244 - struct m25p *flash = spi_mem_get_drvdata(spimem); 245 - 246 - spi_nor_restore(&flash->spi_nor); 247 - 248 - /* Clean up MTD stuff. */ 249 - return mtd_device_unregister(&flash->spi_nor.mtd); 250 - } 251 - 252 - static void m25p_shutdown(struct spi_mem *spimem) 253 - { 254 - struct m25p *flash = spi_mem_get_drvdata(spimem); 255 - 256 - spi_nor_restore(&flash->spi_nor); 257 - } 258 - /* 259 - * Do NOT add to this array without reading the following: 260 - * 261 - * Historically, many flash devices are bound to this driver by their name. But 262 - * since most of these flash are compatible to some extent, and their 263 - * differences can often be differentiated by the JEDEC read-ID command, we 264 - * encourage new users to add support to the spi-nor library, and simply bind 265 - * against a generic string here (e.g., "jedec,spi-nor"). 266 - * 267 - * Many flash names are kept here in this list (as well as in spi-nor.c) to 268 - * keep them available as module aliases for existing platforms. 269 - */ 270 - static const struct spi_device_id m25p_ids[] = { 271 - /* 272 - * Allow non-DT platform devices to bind to the "spi-nor" modalias, and 273 - * hack around the fact that the SPI core does not provide uevent 274 - * matching for .of_match_table 275 - */ 276 - {"spi-nor"}, 277 - 278 - /* 279 - * Entries not used in DTs that should be safe to drop after replacing 280 - * them with "spi-nor" in platform data. 281 - */ 282 - {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"}, 283 - 284 - /* 285 - * Entries that were used in DTs without "jedec,spi-nor" fallback and 286 - * should be kept for backward compatibility. 287 - */ 288 - {"at25df321a"}, {"at25df641"}, {"at26df081a"}, 289 - {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"}, 290 - {"mx25l25635e"},{"mx66l51235l"}, 291 - {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"}, 292 - {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"}, 293 - {"s25fl064k"}, 294 - {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"}, 295 - {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"}, 296 - {"m25p64"}, {"m25p128"}, 297 - {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"}, 298 - {"w25q80bl"}, {"w25q128"}, {"w25q256"}, 299 - 300 - /* Flashes that can't be detected using JEDEC */ 301 - {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"}, 302 - {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"}, 303 - {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"}, 304 - 305 - /* Everspin MRAMs (non-JEDEC) */ 306 - { "mr25h128" }, /* 128 Kib, 40 MHz */ 307 - { "mr25h256" }, /* 256 Kib, 40 MHz */ 308 - { "mr25h10" }, /* 1 Mib, 40 MHz */ 309 - { "mr25h40" }, /* 4 Mib, 40 MHz */ 310 - 311 - { }, 312 - }; 313 - MODULE_DEVICE_TABLE(spi, m25p_ids); 314 - 315 - static const struct of_device_id m25p_of_table[] = { 316 - /* 317 - * Generic compatibility for SPI NOR that can be identified by the 318 - * JEDEC READ ID opcode (0x9F). Use this, if possible. 319 - */ 320 - { .compatible = "jedec,spi-nor" }, 321 - {} 322 - }; 323 - MODULE_DEVICE_TABLE(of, m25p_of_table); 324 - 325 - static struct spi_mem_driver m25p80_driver = { 326 - .spidrv = { 327 - .driver = { 328 - .name = "m25p80", 329 - .of_match_table = m25p_of_table, 330 - }, 331 - .id_table = m25p_ids, 332 - }, 333 - .probe = m25p_probe, 334 - .remove = m25p_remove, 335 - .shutdown = m25p_shutdown, 336 - 337 - /* REVISIT: many of these chips have deep power-down modes, which 338 - * should clearly be entered on suspend() to minimize power use. 339 - * And also when they're otherwise idle... 340 - */ 341 - }; 342 - 343 - module_spi_mem_driver(m25p80_driver); 344 - 345 - MODULE_LICENSE("GPL"); 346 - MODULE_AUTHOR("Mike Lavender"); 347 - MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");
+2
drivers/mtd/spi-nor/Kconfig
··· 2 2 menuconfig MTD_SPI_NOR 3 3 tristate "SPI-NOR device support" 4 4 depends on MTD 5 + depends on MTD && SPI_MASTER 6 + select SPI_MEM 5 7 help 6 8 This is the framework for the SPI NOR which can be used by the SPI 7 9 device drivers and the SPI-NOR device driver.
+600 -28
drivers/mtd/spi-nor/spi-nor.c
··· 19 19 20 20 #include <linux/mtd/mtd.h> 21 21 #include <linux/of_platform.h> 22 + #include <linux/sched/task_stack.h> 22 23 #include <linux/spi/flash.h> 23 24 #include <linux/mtd/spi-nor.h> 24 25 ··· 289 288 290 289 #define JEDEC_MFR(info) ((info)->id[0]) 291 290 291 + /** 292 + * spi_nor_spimem_xfer_data() - helper function to read/write data to 293 + * flash's memory region 294 + * @nor: pointer to 'struct spi_nor' 295 + * @op: pointer to 'struct spi_mem_op' template for transfer 296 + * 297 + * Return: number of bytes transferred on success, -errno otherwise 298 + */ 299 + static ssize_t spi_nor_spimem_xfer_data(struct spi_nor *nor, 300 + struct spi_mem_op *op) 301 + { 302 + bool usebouncebuf = false; 303 + void *rdbuf = NULL; 304 + const void *buf; 305 + int ret; 306 + 307 + if (op->data.dir == SPI_MEM_DATA_IN) 308 + buf = op->data.buf.in; 309 + else 310 + buf = op->data.buf.out; 311 + 312 + if (object_is_on_stack(buf) || !virt_addr_valid(buf)) 313 + usebouncebuf = true; 314 + 315 + if (usebouncebuf) { 316 + if (op->data.nbytes > nor->bouncebuf_size) 317 + op->data.nbytes = nor->bouncebuf_size; 318 + 319 + if (op->data.dir == SPI_MEM_DATA_IN) { 320 + rdbuf = op->data.buf.in; 321 + op->data.buf.in = nor->bouncebuf; 322 + } else { 323 + op->data.buf.out = nor->bouncebuf; 324 + memcpy(nor->bouncebuf, buf, 325 + op->data.nbytes); 326 + } 327 + } 328 + 329 + ret = spi_mem_adjust_op_size(nor->spimem, op); 330 + if (ret) 331 + return ret; 332 + 333 + ret = spi_mem_exec_op(nor->spimem, op); 334 + if (ret) 335 + return ret; 336 + 337 + if (usebouncebuf && op->data.dir == SPI_MEM_DATA_IN) 338 + memcpy(rdbuf, nor->bouncebuf, op->data.nbytes); 339 + 340 + return op->data.nbytes; 341 + } 342 + 343 + /** 344 + * spi_nor_spimem_read_data() - read data from flash's memory region via 345 + * spi-mem 346 + * @nor: pointer to 'struct spi_nor' 347 + * @from: offset to read from 348 + * @len: number of bytes to read 349 + * @buf: pointer to dst buffer 350 + * 351 + * Return: number of bytes read successfully, -errno otherwise 352 + */ 353 + static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from, 354 + size_t len, u8 *buf) 355 + { 356 + struct spi_mem_op op = 357 + SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1), 358 + SPI_MEM_OP_ADDR(nor->addr_width, from, 1), 359 + SPI_MEM_OP_DUMMY(nor->read_dummy, 1), 360 + SPI_MEM_OP_DATA_IN(len, buf, 1)); 361 + 362 + /* get transfer protocols. */ 363 + op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto); 364 + op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto); 365 + op.dummy.buswidth = op.addr.buswidth; 366 + op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); 367 + 368 + /* convert the dummy cycles to the number of bytes */ 369 + op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; 370 + 371 + return spi_nor_spimem_xfer_data(nor, &op); 372 + } 373 + 374 + /** 375 + * spi_nor_read_data() - read data from flash memory 376 + * @nor: pointer to 'struct spi_nor' 377 + * @from: offset to read from 378 + * @len: number of bytes to read 379 + * @buf: pointer to dst buffer 380 + * 381 + * Return: number of bytes read successfully, -errno otherwise 382 + */ 383 + static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, 384 + u8 *buf) 385 + { 386 + if (nor->spimem) 387 + return spi_nor_spimem_read_data(nor, from, len, buf); 388 + 389 + return nor->read(nor, from, len, buf); 390 + } 391 + 392 + /** 393 + * spi_nor_spimem_write_data() - write data to flash memory via 394 + * spi-mem 395 + * @nor: pointer to 'struct spi_nor' 396 + * @to: offset to write to 397 + * @len: number of bytes to write 398 + * @buf: pointer to src buffer 399 + * 400 + * Return: number of bytes written successfully, -errno otherwise 401 + */ 402 + static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to, 403 + size_t len, const u8 *buf) 404 + { 405 + struct spi_mem_op op = 406 + SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1), 407 + SPI_MEM_OP_ADDR(nor->addr_width, to, 1), 408 + SPI_MEM_OP_NO_DUMMY, 409 + SPI_MEM_OP_DATA_OUT(len, buf, 1)); 410 + 411 + op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto); 412 + op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto); 413 + op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); 414 + 415 + if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) 416 + op.addr.nbytes = 0; 417 + 418 + return spi_nor_spimem_xfer_data(nor, &op); 419 + } 420 + 421 + /** 422 + * spi_nor_write_data() - write data to flash memory 423 + * @nor: pointer to 'struct spi_nor' 424 + * @to: offset to write to 425 + * @len: number of bytes to write 426 + * @buf: pointer to src buffer 427 + * 428 + * Return: number of bytes written successfully, -errno otherwise 429 + */ 430 + static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len, 431 + const u8 *buf) 432 + { 433 + if (nor->spimem) 434 + return spi_nor_spimem_write_data(nor, to, len, buf); 435 + 436 + return nor->write(nor, to, len, buf); 437 + } 438 + 292 439 /* 293 440 * Read the status register, returning its value in the location 294 441 * Return the status register value. ··· 446 297 { 447 298 int ret; 448 299 449 - ret = nor->read_reg(nor, SPINOR_OP_RDSR, nor->bouncebuf, 1); 300 + if (nor->spimem) { 301 + struct spi_mem_op op = 302 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 1), 303 + SPI_MEM_OP_NO_ADDR, 304 + SPI_MEM_OP_NO_DUMMY, 305 + SPI_MEM_OP_DATA_IN(1, nor->bouncebuf, 1)); 306 + 307 + ret = spi_mem_exec_op(nor->spimem, &op); 308 + } else { 309 + ret = nor->read_reg(nor, SPINOR_OP_RDSR, nor->bouncebuf, 1); 310 + } 311 + 450 312 if (ret < 0) { 451 313 pr_err("error %d reading SR\n", (int) ret); 452 314 return ret; ··· 475 315 { 476 316 int ret; 477 317 478 - ret = nor->read_reg(nor, SPINOR_OP_RDFSR, nor->bouncebuf, 1); 318 + if (nor->spimem) { 319 + struct spi_mem_op op = 320 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 1), 321 + SPI_MEM_OP_NO_ADDR, 322 + SPI_MEM_OP_NO_DUMMY, 323 + SPI_MEM_OP_DATA_IN(1, nor->bouncebuf, 1)); 324 + 325 + ret = spi_mem_exec_op(nor->spimem, &op); 326 + } else { 327 + ret = nor->read_reg(nor, SPINOR_OP_RDFSR, nor->bouncebuf, 1); 328 + } 329 + 479 330 if (ret < 0) { 480 331 pr_err("error %d reading FSR\n", ret); 481 332 return ret; ··· 504 333 { 505 334 int ret; 506 335 507 - ret = nor->read_reg(nor, SPINOR_OP_RDCR, nor->bouncebuf, 1); 336 + if (nor->spimem) { 337 + struct spi_mem_op op = 338 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDCR, 1), 339 + SPI_MEM_OP_NO_ADDR, 340 + SPI_MEM_OP_NO_DUMMY, 341 + SPI_MEM_OP_DATA_IN(1, nor->bouncebuf, 1)); 342 + 343 + ret = spi_mem_exec_op(nor->spimem, &op); 344 + } else { 345 + ret = nor->read_reg(nor, SPINOR_OP_RDCR, nor->bouncebuf, 1); 346 + } 347 + 508 348 if (ret < 0) { 509 349 dev_err(nor->dev, "error %d reading CR\n", ret); 510 350 return ret; ··· 531 349 static int write_sr(struct spi_nor *nor, u8 val) 532 350 { 533 351 nor->bouncebuf[0] = val; 352 + if (nor->spimem) { 353 + struct spi_mem_op op = 354 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 1), 355 + SPI_MEM_OP_NO_ADDR, 356 + SPI_MEM_OP_NO_DUMMY, 357 + SPI_MEM_OP_DATA_IN(1, nor->bouncebuf, 1)); 358 + 359 + return spi_mem_exec_op(nor->spimem, &op); 360 + } 361 + 534 362 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->bouncebuf, 1); 535 363 } 536 364 ··· 550 358 */ 551 359 static int write_enable(struct spi_nor *nor) 552 360 { 361 + if (nor->spimem) { 362 + struct spi_mem_op op = 363 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREN, 1), 364 + SPI_MEM_OP_NO_ADDR, 365 + SPI_MEM_OP_NO_DUMMY, 366 + SPI_MEM_OP_NO_DATA); 367 + 368 + return spi_mem_exec_op(nor->spimem, &op); 369 + } 370 + 553 371 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0); 554 372 } 555 373 ··· 568 366 */ 569 367 static int write_disable(struct spi_nor *nor) 570 368 { 369 + if (nor->spimem) { 370 + struct spi_mem_op op = 371 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRDI, 1), 372 + SPI_MEM_OP_NO_ADDR, 373 + SPI_MEM_OP_NO_DUMMY, 374 + SPI_MEM_OP_NO_DATA); 375 + 376 + return spi_mem_exec_op(nor->spimem, &op); 377 + } 378 + 571 379 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0); 572 380 } 573 381 ··· 677 465 } 678 466 } 679 467 468 + static int macronix_set_4byte(struct spi_nor *nor, bool enable) 469 + { 470 + if (nor->spimem) { 471 + struct spi_mem_op op = 472 + SPI_MEM_OP(SPI_MEM_OP_CMD(enable ? 473 + SPINOR_OP_EN4B : 474 + SPINOR_OP_EX4B, 475 + 1), 476 + SPI_MEM_OP_NO_ADDR, 477 + SPI_MEM_OP_NO_DUMMY, 478 + SPI_MEM_OP_NO_DATA); 479 + 480 + return spi_mem_exec_op(nor->spimem, &op); 481 + } 482 + 483 + return nor->write_reg(nor, enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B, 484 + NULL, 0); 485 + } 486 + 487 + static int spansion_set_4byte(struct spi_nor *nor, bool enable) 488 + { 489 + nor->bouncebuf[0] = enable << 7; 490 + 491 + if (nor->spimem) { 492 + struct spi_mem_op op = 493 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_BRWR, 1), 494 + SPI_MEM_OP_NO_ADDR, 495 + SPI_MEM_OP_NO_DUMMY, 496 + SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1)); 497 + 498 + return spi_mem_exec_op(nor->spimem, &op); 499 + } 500 + 501 + return nor->write_reg(nor, SPINOR_OP_BRWR, nor->bouncebuf, 1); 502 + } 503 + 504 + static int spi_nor_write_ear(struct spi_nor *nor, u8 ear) 505 + { 506 + nor->bouncebuf[0] = ear; 507 + 508 + if (nor->spimem) { 509 + struct spi_mem_op op = 510 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREAR, 1), 511 + SPI_MEM_OP_NO_ADDR, 512 + SPI_MEM_OP_NO_DUMMY, 513 + SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1)); 514 + 515 + return spi_mem_exec_op(nor->spimem, &op); 516 + } 517 + 518 + return nor->write_reg(nor, SPINOR_OP_WREAR, nor->bouncebuf, 1); 519 + } 520 + 680 521 /* Enable/disable 4-byte addressing mode. */ 681 522 static int set_4byte(struct spi_nor *nor, bool enable) 682 523 { 683 524 int status; 684 525 bool need_wren = false; 685 - u8 cmd; 686 526 687 527 switch (JEDEC_MFR(nor->info)) { 688 528 case SNOR_MFR_ST: ··· 747 483 if (need_wren) 748 484 write_enable(nor); 749 485 750 - cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B; 751 - status = nor->write_reg(nor, cmd, NULL, 0); 486 + status = macronix_set_4byte(nor, enable); 752 487 if (need_wren) 753 488 write_disable(nor); 754 489 ··· 760 497 * We must clear the register to enable normal behavior. 761 498 */ 762 499 write_enable(nor); 763 - nor->bouncebuf[0] = 0; 764 - nor->write_reg(nor, SPINOR_OP_WREAR, 765 - nor->bouncebuf, 1); 500 + spi_nor_write_ear(nor, 0); 766 501 write_disable(nor); 767 502 } 768 503 769 504 return status; 770 505 default: 771 506 /* Spansion style */ 772 - nor->bouncebuf[0] = enable << 7; 773 - return nor->write_reg(nor, SPINOR_OP_BRWR, nor->bouncebuf, 1); 507 + return spansion_set_4byte(nor, enable); 774 508 } 509 + } 510 + 511 + static int spi_nor_xread_sr(struct spi_nor *nor, u8 *sr) 512 + { 513 + if (nor->spimem) { 514 + struct spi_mem_op op = 515 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_XRDSR, 1), 516 + SPI_MEM_OP_NO_ADDR, 517 + SPI_MEM_OP_NO_DUMMY, 518 + SPI_MEM_OP_DATA_IN(1, sr, 1)); 519 + 520 + return spi_mem_exec_op(nor->spimem, &op); 521 + } 522 + 523 + return nor->read_reg(nor, SPINOR_OP_XRDSR, sr, 1); 775 524 } 776 525 777 526 static int s3an_sr_ready(struct spi_nor *nor) 778 527 { 779 528 int ret; 780 529 781 - ret = nor->read_reg(nor, SPINOR_OP_XRDSR, nor->bouncebuf, 1); 530 + ret = spi_nor_xread_sr(nor, nor->bouncebuf); 782 531 if (ret < 0) { 783 532 dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret); 784 533 return ret; 785 534 } 786 535 787 536 return !!(nor->bouncebuf[0] & XSR_RDY); 537 + } 538 + 539 + static int spi_nor_clear_sr(struct spi_nor *nor) 540 + { 541 + if (nor->spimem) { 542 + struct spi_mem_op op = 543 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLSR, 1), 544 + SPI_MEM_OP_NO_ADDR, 545 + SPI_MEM_OP_NO_DUMMY, 546 + SPI_MEM_OP_NO_DATA); 547 + 548 + return spi_mem_exec_op(nor->spimem, &op); 549 + } 550 + 551 + return nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0); 788 552 } 789 553 790 554 static int spi_nor_sr_ready(struct spi_nor *nor) ··· 826 536 else 827 537 dev_err(nor->dev, "Programming Error occurred\n"); 828 538 829 - nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0); 539 + spi_nor_clear_sr(nor); 830 540 return -EIO; 831 541 } 832 542 833 543 return !(sr & SR_WIP); 544 + } 545 + 546 + static int spi_nor_clear_fsr(struct spi_nor *nor) 547 + { 548 + if (nor->spimem) { 549 + struct spi_mem_op op = 550 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLFSR, 1), 551 + SPI_MEM_OP_NO_ADDR, 552 + SPI_MEM_OP_NO_DUMMY, 553 + SPI_MEM_OP_NO_DATA); 554 + 555 + return spi_mem_exec_op(nor->spimem, &op); 556 + } 557 + 558 + return nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0); 834 559 } 835 560 836 561 static int spi_nor_fsr_ready(struct spi_nor *nor) ··· 864 559 dev_err(nor->dev, 865 560 "Attempted to modify a protected sector.\n"); 866 561 867 - nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0); 562 + spi_nor_clear_fsr(nor); 868 563 return -EIO; 869 564 } 870 565 ··· 932 627 { 933 628 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10)); 934 629 630 + if (nor->spimem) { 631 + struct spi_mem_op op = 632 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CHIP_ERASE, 1), 633 + SPI_MEM_OP_NO_ADDR, 634 + SPI_MEM_OP_NO_DUMMY, 635 + SPI_MEM_OP_NO_DATA); 636 + 637 + return spi_mem_exec_op(nor->spimem, &op); 638 + } 639 + 935 640 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0); 936 641 } 937 642 ··· 1002 687 1003 688 if (nor->erase) 1004 689 return nor->erase(nor, addr); 690 + 691 + if (nor->spimem) { 692 + struct spi_mem_op op = 693 + SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1), 694 + SPI_MEM_OP_ADDR(nor->addr_width, addr, 1), 695 + SPI_MEM_OP_NO_DUMMY, 696 + SPI_MEM_OP_NO_DATA); 697 + 698 + return spi_mem_exec_op(nor->spimem, &op); 699 + } 1005 700 1006 701 /* 1007 702 * Default implementation, if driver doesn't have a specialized HW ··· 1728 1403 1729 1404 write_enable(nor); 1730 1405 1731 - ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2); 1406 + if (nor->spimem) { 1407 + struct spi_mem_op op = 1408 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 1), 1409 + SPI_MEM_OP_NO_ADDR, 1410 + SPI_MEM_OP_NO_DUMMY, 1411 + SPI_MEM_OP_DATA_OUT(2, sr_cr, 1)); 1412 + 1413 + ret = spi_mem_exec_op(nor->spimem, &op); 1414 + } else { 1415 + ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2); 1416 + } 1417 + 1732 1418 if (ret < 0) { 1733 1419 dev_err(nor->dev, 1734 1420 "error while writing configuration register\n"); ··· 1920 1584 return 0; 1921 1585 } 1922 1586 1587 + static int spi_nor_write_sr2(struct spi_nor *nor, u8 *sr2) 1588 + { 1589 + if (nor->spimem) { 1590 + struct spi_mem_op op = 1591 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR2, 1), 1592 + SPI_MEM_OP_NO_ADDR, 1593 + SPI_MEM_OP_NO_DUMMY, 1594 + SPI_MEM_OP_DATA_OUT(1, sr2, 1)); 1595 + 1596 + return spi_mem_exec_op(nor->spimem, &op); 1597 + } 1598 + 1599 + return nor->write_reg(nor, SPINOR_OP_WRSR2, sr2, 1); 1600 + } 1601 + 1602 + static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2) 1603 + { 1604 + if (nor->spimem) { 1605 + struct spi_mem_op op = 1606 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR2, 1), 1607 + SPI_MEM_OP_NO_ADDR, 1608 + SPI_MEM_OP_NO_DUMMY, 1609 + SPI_MEM_OP_DATA_IN(1, sr2, 1)); 1610 + 1611 + return spi_mem_exec_op(nor->spimem, &op); 1612 + } 1613 + 1614 + return nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1); 1615 + } 1616 + 1923 1617 /** 1924 1618 * sr2_bit7_quad_enable() - set QE bit in Status Register 2. 1925 1619 * @nor: pointer to a 'struct spi_nor' ··· 1968 1602 int ret; 1969 1603 1970 1604 /* Check current Quad Enable bit value. */ 1971 - ret = nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1); 1605 + ret = spi_nor_read_sr2(nor, sr2); 1972 1606 if (ret) 1973 1607 return ret; 1974 1608 if (*sr2 & SR2_QUAD_EN_BIT7) ··· 1979 1613 1980 1614 write_enable(nor); 1981 1615 1982 - ret = nor->write_reg(nor, SPINOR_OP_WRSR2, sr2, 1); 1616 + ret = spi_nor_write_sr2(nor, sr2); 1983 1617 if (ret < 0) { 1984 1618 dev_err(nor->dev, "error while writing status register 2\n"); 1985 1619 return -EINVAL; ··· 1992 1626 } 1993 1627 1994 1628 /* Read back and check it. */ 1995 - ret = nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1); 1629 + ret = spi_nor_read_sr2(nor, sr2); 1996 1630 if (!(ret > 0 && (*sr2 & SR2_QUAD_EN_BIT7))) { 1997 1631 dev_err(nor->dev, "SR2 Quad bit not set\n"); 1998 1632 return -EINVAL; ··· 2545 2179 u8 *id = nor->bouncebuf; 2546 2180 const struct flash_info *info; 2547 2181 2548 - tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN); 2182 + if (nor->spimem) { 2183 + struct spi_mem_op op = 2184 + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDID, 1), 2185 + SPI_MEM_OP_NO_ADDR, 2186 + SPI_MEM_OP_NO_DUMMY, 2187 + SPI_MEM_OP_DATA_IN(SPI_NOR_MAX_ID_LEN, id, 1)); 2188 + 2189 + tmp = spi_mem_exec_op(nor->spimem, &op); 2190 + } else { 2191 + tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, 2192 + SPI_NOR_MAX_ID_LEN); 2193 + } 2549 2194 if (tmp < 0) { 2550 2195 dev_err(nor->dev, "error %d reading JEDEC ID\n", tmp); 2551 2196 return ERR_PTR(tmp); ··· 2592 2215 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT) 2593 2216 addr = spi_nor_s3an_addr_convert(nor, addr); 2594 2217 2595 - ret = nor->read(nor, addr, len, buf); 2218 + ret = spi_nor_read_data(nor, addr, len, buf); 2596 2219 if (ret == 0) { 2597 2220 /* We shouldn't see 0-length reads */ 2598 2221 ret = -EIO; ··· 2637 2260 nor->program_opcode = SPINOR_OP_BP; 2638 2261 2639 2262 /* write one byte. */ 2640 - ret = nor->write(nor, to, 1, buf); 2263 + ret = spi_nor_write_data(nor, to, 1, buf); 2641 2264 if (ret < 0) 2642 2265 goto sst_write_err; 2643 2266 WARN(ret != 1, "While writing 1 byte written %i bytes\n", ··· 2653 2276 nor->program_opcode = SPINOR_OP_AAI_WP; 2654 2277 2655 2278 /* write two bytes. */ 2656 - ret = nor->write(nor, to, 2, buf + actual); 2279 + ret = spi_nor_write_data(nor, to, 2, buf + actual); 2657 2280 if (ret < 0) 2658 2281 goto sst_write_err; 2659 2282 WARN(ret != 2, "While writing 2 bytes written %i bytes\n", ··· 2676 2299 write_enable(nor); 2677 2300 2678 2301 nor->program_opcode = SPINOR_OP_BP; 2679 - ret = nor->write(nor, to, 1, buf + actual); 2302 + ret = spi_nor_write_data(nor, to, 1, buf + actual); 2680 2303 if (ret < 0) 2681 2304 goto sst_write_err; 2682 2305 WARN(ret != 1, "While writing 1 byte written %i bytes\n", ··· 2738 2361 addr = spi_nor_s3an_addr_convert(nor, addr); 2739 2362 2740 2363 write_enable(nor); 2741 - ret = nor->write(nor, addr, page_remain, buf + i); 2364 + ret = spi_nor_write_data(nor, addr, page_remain, buf + i); 2742 2365 if (ret < 0) 2743 2366 goto write_err; 2744 2367 written = ret; ··· 2757 2380 2758 2381 static int spi_nor_check(struct spi_nor *nor) 2759 2382 { 2760 - if (!nor->dev || !nor->read || !nor->write || 2761 - !nor->read_reg || !nor->write_reg) { 2383 + if (!nor->dev || 2384 + (!nor->spimem && 2385 + (!nor->read || !nor->write || !nor->read_reg || 2386 + !nor->write_reg))) { 2762 2387 pr_err("spi-nor: please fill all the necessary fields!\n"); 2763 2388 return -EINVAL; 2764 2389 } ··· 2772 2393 { 2773 2394 int ret; 2774 2395 2775 - ret = nor->read_reg(nor, SPINOR_OP_XRDSR, nor->bouncebuf, 1); 2396 + ret = spi_nor_xread_sr(nor, nor->bouncebuf); 2776 2397 if (ret < 0) { 2777 2398 dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret); 2778 2399 return ret; ··· 2902 2523 int ret; 2903 2524 2904 2525 while (len) { 2905 - ret = nor->read(nor, addr, len, buf); 2526 + ret = spi_nor_read_data(nor, addr, len, buf); 2906 2527 if (!ret || ret > len) 2907 2528 return -EIO; 2908 2529 if (ret < 0) ··· 4501 4122 /* 4502 4123 * We need the bounce buffer early to read/write registers when going 4503 4124 * through the spi-mem layer (buffers have to be DMA-able). 4125 + * For spi-mem drivers, we'll reallocate a new buffer if 4126 + * nor->page_size turns out to be greater than PAGE_SIZE (which 4127 + * shouldn't happen before long since NOR pages are usually less 4128 + * than 1KB) after spi_nor_scan() returns. 4504 4129 */ 4505 4130 nor->bouncebuf_size = PAGE_SIZE; 4506 4131 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size, ··· 4706 4323 return 0; 4707 4324 } 4708 4325 EXPORT_SYMBOL_GPL(spi_nor_scan); 4326 + 4327 + static int spi_nor_probe(struct spi_mem *spimem) 4328 + { 4329 + struct spi_device *spi = spimem->spi; 4330 + struct flash_platform_data *data = dev_get_platdata(&spi->dev); 4331 + struct spi_nor *nor; 4332 + struct spi_nor_hwcaps hwcaps = { 4333 + .mask = SNOR_HWCAPS_READ | 4334 + SNOR_HWCAPS_READ_FAST | 4335 + SNOR_HWCAPS_PP, 4336 + }; 4337 + char *flash_name; 4338 + int ret; 4339 + 4340 + nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL); 4341 + if (!nor) 4342 + return -ENOMEM; 4343 + 4344 + nor->spimem = spimem; 4345 + nor->dev = &spi->dev; 4346 + spi_nor_set_flash_node(nor, spi->dev.of_node); 4347 + 4348 + spi_mem_set_drvdata(spimem, nor); 4349 + 4350 + if (spi->mode & SPI_RX_OCTAL) { 4351 + hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8; 4352 + 4353 + if (spi->mode & SPI_TX_OCTAL) 4354 + hwcaps.mask |= (SNOR_HWCAPS_READ_1_8_8 | 4355 + SNOR_HWCAPS_PP_1_1_8 | 4356 + SNOR_HWCAPS_PP_1_8_8); 4357 + } else if (spi->mode & SPI_RX_QUAD) { 4358 + hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4; 4359 + 4360 + if (spi->mode & SPI_TX_QUAD) 4361 + hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 | 4362 + SNOR_HWCAPS_PP_1_1_4 | 4363 + SNOR_HWCAPS_PP_1_4_4); 4364 + } else if (spi->mode & SPI_RX_DUAL) { 4365 + hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2; 4366 + 4367 + if (spi->mode & SPI_TX_DUAL) 4368 + hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2; 4369 + } 4370 + 4371 + if (data && data->name) 4372 + nor->mtd.name = data->name; 4373 + 4374 + if (!nor->mtd.name) 4375 + nor->mtd.name = spi_mem_get_name(spimem); 4376 + 4377 + /* 4378 + * For some (historical?) reason many platforms provide two different 4379 + * names in flash_platform_data: "name" and "type". Quite often name is 4380 + * set to "m25p80" and then "type" provides a real chip name. 4381 + * If that's the case, respect "type" and ignore a "name". 4382 + */ 4383 + if (data && data->type) 4384 + flash_name = data->type; 4385 + else if (!strcmp(spi->modalias, "spi-nor")) 4386 + flash_name = NULL; /* auto-detect */ 4387 + else 4388 + flash_name = spi->modalias; 4389 + 4390 + ret = spi_nor_scan(nor, flash_name, &hwcaps); 4391 + if (ret) 4392 + return ret; 4393 + 4394 + /* 4395 + * None of the existing parts have > 512B pages, but let's play safe 4396 + * and add this logic so that if anyone ever adds support for such 4397 + * a NOR we don't end up with buffer overflows. 4398 + */ 4399 + if (nor->page_size > PAGE_SIZE) { 4400 + nor->bouncebuf_size = nor->page_size; 4401 + devm_kfree(nor->dev, nor->bouncebuf); 4402 + nor->bouncebuf = devm_kmalloc(nor->dev, 4403 + nor->bouncebuf_size, 4404 + GFP_KERNEL); 4405 + if (!nor->bouncebuf) 4406 + return -ENOMEM; 4407 + } 4408 + 4409 + return mtd_device_register(&nor->mtd, data ? data->parts : NULL, 4410 + data ? data->nr_parts : 0); 4411 + } 4412 + 4413 + static int spi_nor_remove(struct spi_mem *spimem) 4414 + { 4415 + struct spi_nor *nor = spi_mem_get_drvdata(spimem); 4416 + 4417 + spi_nor_restore(nor); 4418 + 4419 + /* Clean up MTD stuff. */ 4420 + return mtd_device_unregister(&nor->mtd); 4421 + } 4422 + 4423 + static void spi_nor_shutdown(struct spi_mem *spimem) 4424 + { 4425 + struct spi_nor *nor = spi_mem_get_drvdata(spimem); 4426 + 4427 + spi_nor_restore(nor); 4428 + } 4429 + 4430 + /* 4431 + * Do NOT add to this array without reading the following: 4432 + * 4433 + * Historically, many flash devices are bound to this driver by their name. But 4434 + * since most of these flash are compatible to some extent, and their 4435 + * differences can often be differentiated by the JEDEC read-ID command, we 4436 + * encourage new users to add support to the spi-nor library, and simply bind 4437 + * against a generic string here (e.g., "jedec,spi-nor"). 4438 + * 4439 + * Many flash names are kept here in this list (as well as in spi-nor.c) to 4440 + * keep them available as module aliases for existing platforms. 4441 + */ 4442 + static const struct spi_device_id spi_nor_dev_ids[] = { 4443 + /* 4444 + * Allow non-DT platform devices to bind to the "spi-nor" modalias, and 4445 + * hack around the fact that the SPI core does not provide uevent 4446 + * matching for .of_match_table 4447 + */ 4448 + {"spi-nor"}, 4449 + 4450 + /* 4451 + * Entries not used in DTs that should be safe to drop after replacing 4452 + * them with "spi-nor" in platform data. 4453 + */ 4454 + {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"}, 4455 + 4456 + /* 4457 + * Entries that were used in DTs without "jedec,spi-nor" fallback and 4458 + * should be kept for backward compatibility. 4459 + */ 4460 + {"at25df321a"}, {"at25df641"}, {"at26df081a"}, 4461 + {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"}, 4462 + {"mx25l25635e"},{"mx66l51235l"}, 4463 + {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"}, 4464 + {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"}, 4465 + {"s25fl064k"}, 4466 + {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"}, 4467 + {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"}, 4468 + {"m25p64"}, {"m25p128"}, 4469 + {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"}, 4470 + {"w25q80bl"}, {"w25q128"}, {"w25q256"}, 4471 + 4472 + /* Flashes that can't be detected using JEDEC */ 4473 + {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"}, 4474 + {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"}, 4475 + {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"}, 4476 + 4477 + /* Everspin MRAMs (non-JEDEC) */ 4478 + { "mr25h128" }, /* 128 Kib, 40 MHz */ 4479 + { "mr25h256" }, /* 256 Kib, 40 MHz */ 4480 + { "mr25h10" }, /* 1 Mib, 40 MHz */ 4481 + { "mr25h40" }, /* 4 Mib, 40 MHz */ 4482 + 4483 + { }, 4484 + }; 4485 + MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids); 4486 + 4487 + static const struct of_device_id spi_nor_of_table[] = { 4488 + /* 4489 + * Generic compatibility for SPI NOR that can be identified by the 4490 + * JEDEC READ ID opcode (0x9F). Use this, if possible. 4491 + */ 4492 + { .compatible = "jedec,spi-nor" }, 4493 + { /* sentinel */ }, 4494 + }; 4495 + MODULE_DEVICE_TABLE(of, spi_nor_of_table); 4496 + 4497 + /* 4498 + * REVISIT: many of these chips have deep power-down modes, which 4499 + * should clearly be entered on suspend() to minimize power use. 4500 + * And also when they're otherwise idle... 4501 + */ 4502 + static struct spi_mem_driver spi_nor_driver = { 4503 + .spidrv = { 4504 + .driver = { 4505 + .name = "spi-nor", 4506 + .of_match_table = spi_nor_of_table, 4507 + }, 4508 + .id_table = spi_nor_dev_ids, 4509 + }, 4510 + .probe = spi_nor_probe, 4511 + .remove = spi_nor_remove, 4512 + .shutdown = spi_nor_shutdown, 4513 + }; 4514 + module_spi_mem_driver(spi_nor_driver); 4709 4515 4710 4516 MODULE_LICENSE("GPL v2"); 4711 4517 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
+3
include/linux/mtd/spi-nor.h
··· 9 9 #include <linux/bitops.h> 10 10 #include <linux/mtd/cfi.h> 11 11 #include <linux/mtd/mtd.h> 12 + #include <linux/spi/spi-mem.h> 12 13 13 14 /* 14 15 * Manufacturer IDs ··· 345 344 * @mtd: point to a mtd_info structure 346 345 * @lock: the lock for the read/write/erase/lock/unlock operations 347 346 * @dev: point to a spi device, or a spi nor controller device. 347 + * @spimem: point to the spi mem device 348 348 * @bouncebuf: bounce buffer used when the buffer passed by the MTD 349 349 * layer is not DMA-able 350 350 * @bouncebuf_size: size of the bounce buffer ··· 386 384 struct mtd_info mtd; 387 385 struct mutex lock; 388 386 struct device *dev; 387 + struct spi_mem *spimem; 389 388 u8 *bouncebuf; 390 389 size_t bouncebuf_size; 391 390 const struct flash_info *info;