1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 Semihalf
5 * Written by: Piotr Ziecik <kosmo@semihalf.com>
12 #include <linux/errno.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/concat.h>
15 #include <mtd/cfi_flash.h>
17 static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS];
18 static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16];
19 #ifdef CONFIG_MTD_CONCAT
20 static char c_mtd_name[16];
23 static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
25 flash_info_t *fi = mtd->priv;
26 size_t a_start = fi->start[0] + instr->addr;
27 size_t a_end = a_start + instr->len;
32 for (sect = 0; sect < fi->sector_count; sect++) {
33 if (a_start == fi->start[sect])
36 if (sect < fi->sector_count - 1) {
37 if (a_end == fi->start[sect + 1]) {
47 if (s_first >= 0 && s_first <= s_last) {
48 instr->state = MTD_ERASING;
51 error = flash_erase(fi, s_first, s_last);
55 instr->state = MTD_ERASE_FAILED;
59 instr->state = MTD_ERASE_DONE;
60 mtd_erase_callback(instr);
67 static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
68 size_t *retlen, u_char *buf)
70 flash_info_t *fi = mtd->priv;
71 u_char *f = (u_char*)(fi->start[0]) + from;
79 static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
80 size_t *retlen, const u_char *buf)
82 flash_info_t *fi = mtd->priv;
83 u_long t = fi->start[0] + to;
87 error = write_buff(fi, (u_char*)buf, t, len);
98 static void cfi_mtd_sync(struct mtd_info *mtd)
101 * This function should wait until all pending operations
102 * finish. However this driver is fully synchronous, so
103 * this function returns immediately
107 static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
109 flash_info_t *fi = mtd->priv;
111 flash_set_verbose(0);
112 flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
113 fi->start[0] + ofs + len - 1, fi);
114 flash_set_verbose(1);
119 static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
121 flash_info_t *fi = mtd->priv;
123 flash_set_verbose(0);
124 flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
125 fi->start[0] + ofs + len - 1, fi);
126 flash_set_verbose(1);
131 static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
134 int sect_size_old = 0;
142 * First detect the number of eraseregions so that we can allocate
143 * the array of eraseregions correctly
145 for (sect = 0; sect < fi->sector_count; sect++) {
146 if (sect_size_old != flash_sector_size(fi, sect))
148 sect_size_old = flash_sector_size(fi, sect);
154 case 1: /* flash has uniform erase size */
155 mtd->numeraseregions = 0;
156 mtd->erasesize = sect_size_old;
160 mtd->numeraseregions = regions;
161 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);
164 * Now detect the largest sector and fill the eraseregions
167 base_addr = offset = fi->start[0];
168 sect_size_old = flash_sector_size(fi, 0);
169 for (sect = 0; sect < fi->sector_count; sect++) {
170 if (sect_size_old != flash_sector_size(fi, sect)) {
171 mtd->eraseregions[regions].offset = offset - base_addr;
172 mtd->eraseregions[regions].erasesize = sect_size_old;
173 mtd->eraseregions[regions].numblocks = numblocks;
174 /* Now start counting the next eraseregions */
177 offset = fi->start[sect];
182 * Select the largest sector size as erasesize (e.g. for UBI)
184 if (flash_sector_size(fi, sect) > sect_size)
185 sect_size = flash_sector_size(fi, sect);
187 sect_size_old = flash_sector_size(fi, sect);
191 * Set the last region
193 mtd->eraseregions[regions].offset = offset - base_addr;
194 mtd->eraseregions[regions].erasesize = sect_size_old;
195 mtd->eraseregions[regions].numblocks = numblocks;
197 mtd->erasesize = sect_size;
202 int cfi_mtd_init(void)
204 struct mtd_info *mtd;
207 #ifdef CONFIG_MTD_CONCAT
208 int devices_found = 0;
209 struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS];
212 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
214 mtd = &cfi_mtd_info[i];
216 memset(mtd, 0, sizeof(struct mtd_info));
218 error = cfi_mtd_set_erasesize(mtd, fi);
222 sprintf(cfi_mtd_names[i], "nor%d", i);
223 mtd->name = cfi_mtd_names[i];
224 mtd->type = MTD_NORFLASH;
225 mtd->flags = MTD_CAP_NORFLASH;
226 mtd->size = fi->size;
228 mtd->writebufsize = mtd->writesize;
230 mtd->_erase = cfi_mtd_erase;
231 mtd->_read = cfi_mtd_read;
232 mtd->_write = cfi_mtd_write;
233 mtd->_sync = cfi_mtd_sync;
234 mtd->_lock = cfi_mtd_lock;
235 mtd->_unlock = cfi_mtd_unlock;
238 if (add_mtd_device(mtd))
241 #ifdef CONFIG_MTD_CONCAT
242 mtd_list[devices_found++] = mtd;
246 #ifdef CONFIG_MTD_CONCAT
247 if (devices_found > 1) {
249 * We detected multiple devices. Concatenate them together.
251 sprintf(c_mtd_name, "nor%d", devices_found);
252 mtd = mtd_concat_create(mtd_list, devices_found, c_mtd_name);
257 if (add_mtd_device(mtd))
260 #endif /* CONFIG_MTD_CONCAT */