1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
4 */
5
6 #include <malloc.h>
7 #include <linux/errno.h>
8 #include <linux/mtd/mtd.h>
9 #include <spi_flash.h>
10
11 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
12
spi_flash_mtd_register(struct spi_flash * flash)13 int spi_flash_mtd_register(struct spi_flash *flash)
14 {
15 return add_mtd_device(&flash->mtd);
16 }
17
spi_flash_mtd_unregister(struct spi_flash * flash)18 void 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
25 static struct mtd_info sf_mtd_info;
26 static bool sf_mtd_registered;
27 static char sf_mtd_name[8];
28
spi_flash_mtd_erase(struct mtd_info * mtd,struct erase_info * instr)29 static 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
34 if (!flash)
35 return -ENODEV;
36
37 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;
47
48 return 0;
49 }
50
spi_flash_mtd_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)51 static 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
57 if (!flash)
58 return -ENODEV;
59
60 err = spi_flash_read(flash, from, len, buf);
61 if (!err)
62 *retlen = len;
63
64 return err;
65 }
66
spi_flash_mtd_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)67 static 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
73 if (!flash)
74 return -ENODEV;
75
76 err = spi_flash_write(flash, to, len, buf);
77 if (!err)
78 *retlen = len;
79
80 return err;
81 }
82
spi_flash_mtd_sync(struct mtd_info * mtd)83 static void spi_flash_mtd_sync(struct mtd_info *mtd)
84 {
85 }
86
spi_flash_mtd_number(void)87 static 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
spi_flash_mtd_register(struct spi_flash * flash)96 int spi_flash_mtd_register(struct spi_flash *flash)
97 {
98 int ret;
99
100 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 }
107
108 sf_mtd_registered = false;
109 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;
125 sf_mtd_info.dev = flash->dev;
126
127 /* Only uniform flash devices for now */
128 sf_mtd_info.numeraseregions = 0;
129 sf_mtd_info.erasesize = flash->sector_size;
130
131 ret = add_mtd_device(&sf_mtd_info);
132 if (!ret)
133 sf_mtd_registered = true;
134
135 return ret;
136 }
137
spi_flash_mtd_unregister(struct spi_flash * flash)138 void spi_flash_mtd_unregister(struct spi_flash *flash)
139 {
140 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);
160 }
161
162 #endif /* !CONFIG_IS_ENABLED(DM_SPI_FLASH) */
163