blob: 7342f26d88e0b50eb281cc14fff72369c41da5aa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +02002/*
3 * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +02004 */
5
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +02006#include <malloc.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +09007#include <linux/errno.h>
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +02008#include <linux/mtd/mtd.h>
9#include <spi_flash.h>
10
Marek Behúnc1fcc142021-05-26 14:08:20 +020011#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
12
13int spi_flash_mtd_register(struct spi_flash *flash)
14{
15 return add_mtd_device(&flash->mtd);
16}
17
18void spi_flash_mtd_unregister(struct spi_flash *flash)
19{
20 del_mtd_device(&flash->mtd);
21}
22
23#else /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */
24
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020025static struct mtd_info sf_mtd_info;
Boris Brezillon9080e282018-12-02 10:54:25 +010026static bool sf_mtd_registered;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020027static char sf_mtd_name[8];
28
29static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
30{
31 struct spi_flash *flash = mtd->priv;
32 int err;
33
Boris Brezillon925a51d2018-12-02 10:54:32 +010034 if (!flash)
35 return -ENODEV;
36
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020037 instr->state = MTD_ERASING;
38
39 err = spi_flash_erase(flash, instr->addr, instr->len);
40 if (err) {
41 instr->state = MTD_ERASE_FAILED;
42 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
43 return -EIO;
44 }
45
46 instr->state = MTD_ERASE_DONE;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020047
48 return 0;
49}
50
51static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
52 size_t *retlen, u_char *buf)
53{
54 struct spi_flash *flash = mtd->priv;
55 int err;
56
Boris Brezillon925a51d2018-12-02 10:54:32 +010057 if (!flash)
58 return -ENODEV;
59
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020060 err = spi_flash_read(flash, from, len, buf);
61 if (!err)
62 *retlen = len;
63
64 return err;
65}
66
67static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
68 size_t *retlen, const u_char *buf)
69{
70 struct spi_flash *flash = mtd->priv;
71 int err;
72
Boris Brezillon925a51d2018-12-02 10:54:32 +010073 if (!flash)
74 return -ENODEV;
75
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020076 err = spi_flash_write(flash, to, len, buf);
77 if (!err)
78 *retlen = len;
79
80 return err;
81}
82
83static void spi_flash_mtd_sync(struct mtd_info *mtd)
84{
85}
86
87static int spi_flash_mtd_number(void)
88{
89#ifdef CONFIG_SYS_MAX_FLASH_BANKS
90 return CONFIG_SYS_MAX_FLASH_BANKS;
91#else
92 return 0;
93#endif
94}
95
96int spi_flash_mtd_register(struct spi_flash *flash)
97{
Boris Brezillon9080e282018-12-02 10:54:25 +010098 int ret;
99
Boris Brezillon925a51d2018-12-02 10:54:32 +0100100 if (sf_mtd_registered) {
101 ret = del_mtd_device(&sf_mtd_info);
102 if (ret)
103 return ret;
104
105 sf_mtd_registered = false;
106 }
Boris Brezillon9080e282018-12-02 10:54:25 +0100107
108 sf_mtd_registered = false;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200109 memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
110 sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
111
112 sf_mtd_info.name = sf_mtd_name;
113 sf_mtd_info.type = MTD_NORFLASH;
114 sf_mtd_info.flags = MTD_CAP_NORFLASH;
115 sf_mtd_info.writesize = 1;
116 sf_mtd_info.writebufsize = flash->page_size;
117
118 sf_mtd_info._erase = spi_flash_mtd_erase;
119 sf_mtd_info._read = spi_flash_mtd_read;
120 sf_mtd_info._write = spi_flash_mtd_write;
121 sf_mtd_info._sync = spi_flash_mtd_sync;
122
123 sf_mtd_info.size = flash->size;
124 sf_mtd_info.priv = flash;
Marek Behúnd37eb232021-05-26 14:08:21 +0200125 sf_mtd_info.dev = flash->dev;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200126
127 /* Only uniform flash devices for now */
128 sf_mtd_info.numeraseregions = 0;
129 sf_mtd_info.erasesize = flash->sector_size;
130
Boris Brezillon9080e282018-12-02 10:54:25 +0100131 ret = add_mtd_device(&sf_mtd_info);
132 if (!ret)
133 sf_mtd_registered = true;
134
135 return ret;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200136}
137
Marek Behúnc1fcc142021-05-26 14:08:20 +0200138void spi_flash_mtd_unregister(struct spi_flash *flash)
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200139{
Boris Brezillon925a51d2018-12-02 10:54:32 +0100140 int ret;
141
142 if (!sf_mtd_registered)
143 return;
144
145 ret = del_mtd_device(&sf_mtd_info);
146 if (!ret) {
147 sf_mtd_registered = false;
148 return;
149 }
150
151 /*
152 * Setting mtd->priv to NULL is the best we can do. Thanks to that,
153 * the MTD layer can still call mtd hooks without risking a
154 * use-after-free bug. Still, things should be fixed to prevent the
155 * spi_flash object from being destroyed when del_mtd_device() fails.
156 */
157 sf_mtd_info.priv = NULL;
158 printf("Failed to unregister MTD %s and the spi_flash object is going away: you're in deep trouble!",
159 sf_mtd_info.name);
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200160}
Marek Behúnc1fcc142021-05-26 14:08:20 +0200161
162#endif /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */