blob: 0aed28a52b78acff7ad31f430285759fd0305bb3 [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
6#include <common.h>
Simon Glass8e201882020-05-10 11:39:54 -06007#include <flash.h>
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +02008#include <malloc.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +09009#include <linux/errno.h>
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020010#include <linux/mtd/mtd.h>
11#include <spi_flash.h>
12
Marek Behúnc1fcc142021-05-26 14:08:20 +020013#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
14
15int spi_flash_mtd_register(struct spi_flash *flash)
16{
17 return add_mtd_device(&flash->mtd);
18}
19
20void spi_flash_mtd_unregister(struct spi_flash *flash)
21{
22 del_mtd_device(&flash->mtd);
23}
24
25#else /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */
26
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020027static struct mtd_info sf_mtd_info;
Boris Brezillon9080e282018-12-02 10:54:25 +010028static bool sf_mtd_registered;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020029static char sf_mtd_name[8];
30
31static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
32{
33 struct spi_flash *flash = mtd->priv;
34 int err;
35
Boris Brezillon925a51d2018-12-02 10:54:32 +010036 if (!flash)
37 return -ENODEV;
38
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020039 instr->state = MTD_ERASING;
40
41 err = spi_flash_erase(flash, instr->addr, instr->len);
42 if (err) {
43 instr->state = MTD_ERASE_FAILED;
44 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
45 return -EIO;
46 }
47
48 instr->state = MTD_ERASE_DONE;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020049
50 return 0;
51}
52
53static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
54 size_t *retlen, u_char *buf)
55{
56 struct spi_flash *flash = mtd->priv;
57 int err;
58
Boris Brezillon925a51d2018-12-02 10:54:32 +010059 if (!flash)
60 return -ENODEV;
61
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020062 err = spi_flash_read(flash, from, len, buf);
63 if (!err)
64 *retlen = len;
65
66 return err;
67}
68
69static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
70 size_t *retlen, const u_char *buf)
71{
72 struct spi_flash *flash = mtd->priv;
73 int err;
74
Boris Brezillon925a51d2018-12-02 10:54:32 +010075 if (!flash)
76 return -ENODEV;
77
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020078 err = spi_flash_write(flash, to, len, buf);
79 if (!err)
80 *retlen = len;
81
82 return err;
83}
84
85static void spi_flash_mtd_sync(struct mtd_info *mtd)
86{
87}
88
89static int spi_flash_mtd_number(void)
90{
91#ifdef CONFIG_SYS_MAX_FLASH_BANKS
92 return CONFIG_SYS_MAX_FLASH_BANKS;
93#else
94 return 0;
95#endif
96}
97
98int spi_flash_mtd_register(struct spi_flash *flash)
99{
Boris Brezillon9080e282018-12-02 10:54:25 +0100100 int ret;
101
Boris Brezillon925a51d2018-12-02 10:54:32 +0100102 if (sf_mtd_registered) {
103 ret = del_mtd_device(&sf_mtd_info);
104 if (ret)
105 return ret;
106
107 sf_mtd_registered = false;
108 }
Boris Brezillon9080e282018-12-02 10:54:25 +0100109
110 sf_mtd_registered = false;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200111 memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
112 sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
113
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;
119
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;
124
125 sf_mtd_info.size = flash->size;
126 sf_mtd_info.priv = flash;
Marek Behúnd37eb232021-05-26 14:08:21 +0200127 sf_mtd_info.dev = flash->dev;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200128
129 /* Only uniform flash devices for now */
130 sf_mtd_info.numeraseregions = 0;
131 sf_mtd_info.erasesize = flash->sector_size;
132
Boris Brezillon9080e282018-12-02 10:54:25 +0100133 ret = add_mtd_device(&sf_mtd_info);
134 if (!ret)
135 sf_mtd_registered = true;
136
137 return ret;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200138}
139
Marek Behúnc1fcc142021-05-26 14:08:20 +0200140void spi_flash_mtd_unregister(struct spi_flash *flash)
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200141{
Boris Brezillon925a51d2018-12-02 10:54:32 +0100142 int ret;
143
144 if (!sf_mtd_registered)
145 return;
146
147 ret = del_mtd_device(&sf_mtd_info);
148 if (!ret) {
149 sf_mtd_registered = false;
150 return;
151 }
152
153 /*
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.
158 */
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!",
161 sf_mtd_info.name);
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200162}
Marek Behúnc1fcc142021-05-26 14:08:20 +0200163
164#endif /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */