1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
8 #include <linux/errno.h>
9 #include <linux/mtd/mtd.h>
10 #include <spi_flash.h>
12 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
14 int spi_flash_mtd_register(struct spi_flash *flash)
16 return add_mtd_device(&flash->mtd);
19 void spi_flash_mtd_unregister(struct spi_flash *flash)
21 del_mtd_device(&flash->mtd);
24 #else /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */
26 static struct mtd_info sf_mtd_info;
27 static bool sf_mtd_registered;
28 static char sf_mtd_name[8];
30 static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
32 struct spi_flash *flash = mtd->priv;
38 instr->state = MTD_ERASING;
40 err = spi_flash_erase(flash, instr->addr, instr->len);
42 instr->state = MTD_ERASE_FAILED;
43 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
47 instr->state = MTD_ERASE_DONE;
52 static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
53 size_t *retlen, u_char *buf)
55 struct spi_flash *flash = mtd->priv;
61 err = spi_flash_read(flash, from, len, buf);
68 static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
69 size_t *retlen, const u_char *buf)
71 struct spi_flash *flash = mtd->priv;
77 err = spi_flash_write(flash, to, len, buf);
84 static void spi_flash_mtd_sync(struct mtd_info *mtd)
88 static int spi_flash_mtd_number(void)
90 #ifdef CONFIG_SYS_MAX_FLASH_BANKS
91 return CONFIG_SYS_MAX_FLASH_BANKS;
97 int spi_flash_mtd_register(struct spi_flash *flash)
101 if (sf_mtd_registered) {
102 ret = del_mtd_device(&sf_mtd_info);
106 sf_mtd_registered = false;
109 sf_mtd_registered = false;
110 memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
111 sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
113 sf_mtd_info.name = sf_mtd_name;
114 sf_mtd_info.type = MTD_NORFLASH;
115 sf_mtd_info.flags = MTD_CAP_NORFLASH;
116 sf_mtd_info.writesize = 1;
117 sf_mtd_info.writebufsize = flash->page_size;
119 sf_mtd_info._erase = spi_flash_mtd_erase;
120 sf_mtd_info._read = spi_flash_mtd_read;
121 sf_mtd_info._write = spi_flash_mtd_write;
122 sf_mtd_info._sync = spi_flash_mtd_sync;
124 sf_mtd_info.size = flash->size;
125 sf_mtd_info.priv = flash;
126 sf_mtd_info.dev = flash->dev;
128 /* Only uniform flash devices for now */
129 sf_mtd_info.numeraseregions = 0;
130 sf_mtd_info.erasesize = flash->sector_size;
132 ret = add_mtd_device(&sf_mtd_info);
134 sf_mtd_registered = true;
139 void spi_flash_mtd_unregister(struct spi_flash *flash)
143 if (!sf_mtd_registered)
146 ret = del_mtd_device(&sf_mtd_info);
148 sf_mtd_registered = false;
153 * Setting mtd->priv to NULL is the best we can do. Thanks to that,
154 * the MTD layer can still call mtd hooks without risking a
155 * use-after-free bug. Still, things should be fixed to prevent the
156 * spi_flash object from being destroyed when del_mtd_device() fails.
158 sf_mtd_info.priv = NULL;
159 printf("Failed to unregister MTD %s and the spi_flash object is going away: you're in deep trouble!",
163 #endif /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */