1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 Semihalf
5 * Written by: Piotr Ziecik <kosmo@semihalf.com>
13 #include <linux/errno.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/concat.h>
16 #include <mtd/cfi_flash.h>
18 static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS];
19 static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16];
20 #ifdef CONFIG_MTD_CONCAT
21 static char c_mtd_name[16];
24 static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
26 flash_info_t *fi = mtd->priv;
27 size_t a_start = fi->start[0] + instr->addr;
28 size_t a_end = a_start + instr->len;
33 for (sect = 0; sect < fi->sector_count; sect++) {
34 if (a_start == fi->start[sect])
37 if (sect < fi->sector_count - 1) {
38 if (a_end == fi->start[sect + 1]) {
48 if (s_first >= 0 && s_first <= s_last) {
49 instr->state = MTD_ERASING;
52 error = flash_erase(fi, s_first, s_last);
56 instr->state = MTD_ERASE_FAILED;
60 instr->state = MTD_ERASE_DONE;
61 mtd_erase_callback(instr);
68 static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
69 size_t *retlen, u_char *buf)
71 flash_info_t *fi = mtd->priv;
72 u_char *f = (u_char*)(fi->start[0]) + from;
74 if (dma_memcpy(buf, f, len) < 0)
81 static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
82 size_t *retlen, const u_char *buf)
84 flash_info_t *fi = mtd->priv;
85 u_long t = fi->start[0] + to;
89 error = write_buff(fi, (u_char*)buf, t, len);
100 static void cfi_mtd_sync(struct mtd_info *mtd)
103 * This function should wait until all pending operations
104 * finish. However this driver is fully synchronous, so
105 * this function returns immediately
109 static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
111 flash_info_t *fi = mtd->priv;
113 flash_set_verbose(0);
114 flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
115 fi->start[0] + ofs + len - 1, fi);
116 flash_set_verbose(1);
121 static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
123 flash_info_t *fi = mtd->priv;
125 flash_set_verbose(0);
126 flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
127 fi->start[0] + ofs + len - 1, fi);
128 flash_set_verbose(1);
133 static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
136 int sect_size_old = 0;
144 * First detect the number of eraseregions so that we can allocate
145 * the array of eraseregions correctly
147 for (sect = 0; sect < fi->sector_count; sect++) {
148 if (sect_size_old != flash_sector_size(fi, sect))
150 sect_size_old = flash_sector_size(fi, sect);
156 case 1: /* flash has uniform erase size */
157 mtd->numeraseregions = 0;
158 mtd->erasesize = sect_size_old;
162 mtd->numeraseregions = regions;
163 mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);
166 * Now detect the largest sector and fill the eraseregions
169 base_addr = offset = fi->start[0];
170 sect_size_old = flash_sector_size(fi, 0);
171 for (sect = 0; sect < fi->sector_count; sect++) {
172 if (sect_size_old != flash_sector_size(fi, sect)) {
173 mtd->eraseregions[regions].offset = offset - base_addr;
174 mtd->eraseregions[regions].erasesize = sect_size_old;
175 mtd->eraseregions[regions].numblocks = numblocks;
176 /* Now start counting the next eraseregions */
179 offset = fi->start[sect];
184 * Select the largest sector size as erasesize (e.g. for UBI)
186 if (flash_sector_size(fi, sect) > sect_size)
187 sect_size = flash_sector_size(fi, sect);
189 sect_size_old = flash_sector_size(fi, sect);
193 * Set the last region
195 mtd->eraseregions[regions].offset = offset - base_addr;
196 mtd->eraseregions[regions].erasesize = sect_size_old;
197 mtd->eraseregions[regions].numblocks = numblocks;
199 mtd->erasesize = sect_size;
204 int cfi_mtd_init(void)
206 struct mtd_info *mtd;
209 #ifdef CONFIG_MTD_CONCAT
210 int devices_found = 0;
211 struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS];
214 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
216 mtd = &cfi_mtd_info[i];
218 memset(mtd, 0, sizeof(struct mtd_info));
220 error = cfi_mtd_set_erasesize(mtd, fi);
224 sprintf(cfi_mtd_names[i], "nor%d", i);
225 mtd->name = cfi_mtd_names[i];
226 mtd->type = MTD_NORFLASH;
227 mtd->flags = MTD_CAP_NORFLASH;
228 mtd->size = fi->size;
230 mtd->writebufsize = mtd->writesize;
232 mtd->_erase = cfi_mtd_erase;
233 mtd->_read = cfi_mtd_read;
234 mtd->_write = cfi_mtd_write;
235 mtd->_sync = cfi_mtd_sync;
236 mtd->_lock = cfi_mtd_lock;
237 mtd->_unlock = cfi_mtd_unlock;
240 if (add_mtd_device(mtd))
243 #ifdef CONFIG_MTD_CONCAT
244 mtd_list[devices_found++] = mtd;
248 #ifdef CONFIG_MTD_CONCAT
249 if (devices_found > 1) {
251 * We detected multiple devices. Concatenate them together.
253 sprintf(c_mtd_name, "nor%d", devices_found);
254 mtd = mtd_concat_create(mtd_list, devices_found, c_mtd_name);
259 if (add_mtd_device(mtd))
262 #endif /* CONFIG_MTD_CONCAT */