mtd: sf: Make sure we don't register the same device twice
[oweals/u-boot.git] / drivers / mtd / spi / sf_mtd.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
4  */
5
6 #include <common.h>
7 #include <malloc.h>
8 #include <linux/errno.h>
9 #include <linux/mtd/mtd.h>
10 #include <spi_flash.h>
11
12 static struct mtd_info sf_mtd_info;
13 static bool sf_mtd_registered;
14 static char sf_mtd_name[8];
15
16 static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
17 {
18         struct spi_flash *flash = mtd->priv;
19         int err;
20
21         instr->state = MTD_ERASING;
22
23         err = spi_flash_erase(flash, instr->addr, instr->len);
24         if (err) {
25                 instr->state = MTD_ERASE_FAILED;
26                 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
27                 return -EIO;
28         }
29
30         instr->state = MTD_ERASE_DONE;
31         mtd_erase_callback(instr);
32
33         return 0;
34 }
35
36 static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
37         size_t *retlen, u_char *buf)
38 {
39         struct spi_flash *flash = mtd->priv;
40         int err;
41
42         err = spi_flash_read(flash, from, len, buf);
43         if (!err)
44                 *retlen = len;
45
46         return err;
47 }
48
49 static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
50         size_t *retlen, const u_char *buf)
51 {
52         struct spi_flash *flash = mtd->priv;
53         int err;
54
55         err = spi_flash_write(flash, to, len, buf);
56         if (!err)
57                 *retlen = len;
58
59         return err;
60 }
61
62 static void spi_flash_mtd_sync(struct mtd_info *mtd)
63 {
64 }
65
66 static int spi_flash_mtd_number(void)
67 {
68 #ifdef CONFIG_SYS_MAX_FLASH_BANKS
69         return CONFIG_SYS_MAX_FLASH_BANKS;
70 #else
71         return 0;
72 #endif
73 }
74
75 int spi_flash_mtd_register(struct spi_flash *flash)
76 {
77         int ret;
78
79         if (sf_mtd_registered)
80                 del_mtd_device(&sf_mtd_info);
81
82         sf_mtd_registered = false;
83         memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
84         sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
85
86         sf_mtd_info.name = sf_mtd_name;
87         sf_mtd_info.type = MTD_NORFLASH;
88         sf_mtd_info.flags = MTD_CAP_NORFLASH;
89         sf_mtd_info.writesize = 1;
90         sf_mtd_info.writebufsize = flash->page_size;
91
92         sf_mtd_info._erase = spi_flash_mtd_erase;
93         sf_mtd_info._read = spi_flash_mtd_read;
94         sf_mtd_info._write = spi_flash_mtd_write;
95         sf_mtd_info._sync = spi_flash_mtd_sync;
96
97         sf_mtd_info.size = flash->size;
98         sf_mtd_info.priv = flash;
99
100         /* Only uniform flash devices for now */
101         sf_mtd_info.numeraseregions = 0;
102         sf_mtd_info.erasesize = flash->sector_size;
103
104         ret = add_mtd_device(&sf_mtd_info);
105         if (!ret)
106                 sf_mtd_registered = true;
107
108         return ret;
109 }
110
111 void spi_flash_mtd_unregister(void)
112 {
113         del_mtd_device(&sf_mtd_info);
114 }