1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
9 #include <linux/errno.h>
10 #include <linux/mtd/mtd.h>
11 #include <spi_flash.h>
13 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
15 int spi_flash_mtd_register(struct spi_flash *flash)
17 return add_mtd_device(&flash->mtd);
20 void spi_flash_mtd_unregister(struct spi_flash *flash)
22 del_mtd_device(&flash->mtd);
25 #else /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */
27 static struct mtd_info sf_mtd_info;
28 static bool sf_mtd_registered;
29 static char sf_mtd_name[8];
31 static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
33 struct spi_flash *flash = mtd->priv;
39 instr->state = MTD_ERASING;
41 err = spi_flash_erase(flash, instr->addr, instr->len);
43 instr->state = MTD_ERASE_FAILED;
44 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
48 instr->state = MTD_ERASE_DONE;
53 static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
54 size_t *retlen, u_char *buf)
56 struct spi_flash *flash = mtd->priv;
62 err = spi_flash_read(flash, from, len, buf);
69 static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
70 size_t *retlen, const u_char *buf)
72 struct spi_flash *flash = mtd->priv;
78 err = spi_flash_write(flash, to, len, buf);
85 static void spi_flash_mtd_sync(struct mtd_info *mtd)
89 static int spi_flash_mtd_number(void)
91 #ifdef CONFIG_SYS_MAX_FLASH_BANKS
92 return CONFIG_SYS_MAX_FLASH_BANKS;
98 int spi_flash_mtd_register(struct spi_flash *flash)
102 if (sf_mtd_registered) {
103 ret = del_mtd_device(&sf_mtd_info);
107 sf_mtd_registered = false;
110 sf_mtd_registered = false;
111 memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
112 sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
114 sf_mtd_info.name = sf_mtd_name;
115 sf_mtd_info.type = MTD_NORFLASH;
116 sf_mtd_info.flags = MTD_CAP_NORFLASH;
117 sf_mtd_info.writesize = 1;
118 sf_mtd_info.writebufsize = flash->page_size;
120 sf_mtd_info._erase = spi_flash_mtd_erase;
121 sf_mtd_info._read = spi_flash_mtd_read;
122 sf_mtd_info._write = spi_flash_mtd_write;
123 sf_mtd_info._sync = spi_flash_mtd_sync;
125 sf_mtd_info.size = flash->size;
126 sf_mtd_info.priv = flash;
127 sf_mtd_info.dev = flash->dev;
129 /* Only uniform flash devices for now */
130 sf_mtd_info.numeraseregions = 0;
131 sf_mtd_info.erasesize = flash->sector_size;
133 ret = add_mtd_device(&sf_mtd_info);
135 sf_mtd_registered = true;
140 void spi_flash_mtd_unregister(struct spi_flash *flash)
144 if (!sf_mtd_registered)
147 ret = del_mtd_device(&sf_mtd_info);
149 sf_mtd_registered = false;
154 * Setting mtd->priv to NULL is the best we can do. Thanks to that,
155 * the MTD layer can still call mtd hooks without risking a
156 * use-after-free bug. Still, things should be fixed to prevent the
157 * spi_flash object from being destroyed when del_mtd_device() fails.
159 sf_mtd_info.priv = NULL;
160 printf("Failed to unregister MTD %s and the spi_flash object is going away: you're in deep trouble!",
164 #endif /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */