blob: 68c36002bee26fa475d023b9711bd699dfa2c916 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +02002/*
3 * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +02004 */
5
6#include <common.h>
7#include <malloc.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +09008#include <linux/errno.h>
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +02009#include <linux/mtd/mtd.h>
10#include <spi_flash.h>
11
12static struct mtd_info sf_mtd_info;
Boris Brezillon492151b2018-12-02 10:54:25 +010013static bool sf_mtd_registered;
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020014static char sf_mtd_name[8];
15
16static 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
Boris Brezillon08898e82018-12-02 10:54:32 +010021 if (!flash)
22 return -ENODEV;
23
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020024 instr->state = MTD_ERASING;
25
26 err = spi_flash_erase(flash, instr->addr, instr->len);
27 if (err) {
28 instr->state = MTD_ERASE_FAILED;
29 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
30 return -EIO;
31 }
32
33 instr->state = MTD_ERASE_DONE;
34 mtd_erase_callback(instr);
35
36 return 0;
37}
38
39static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
40 size_t *retlen, u_char *buf)
41{
42 struct spi_flash *flash = mtd->priv;
43 int err;
44
Boris Brezillon08898e82018-12-02 10:54:32 +010045 if (!flash)
46 return -ENODEV;
47
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020048 err = spi_flash_read(flash, from, len, buf);
49 if (!err)
50 *retlen = len;
51
52 return err;
53}
54
55static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
56 size_t *retlen, const u_char *buf)
57{
58 struct spi_flash *flash = mtd->priv;
59 int err;
60
Boris Brezillon08898e82018-12-02 10:54:32 +010061 if (!flash)
62 return -ENODEV;
63
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020064 err = spi_flash_write(flash, to, len, buf);
65 if (!err)
66 *retlen = len;
67
68 return err;
69}
70
71static void spi_flash_mtd_sync(struct mtd_info *mtd)
72{
73}
74
75static int spi_flash_mtd_number(void)
76{
77#ifdef CONFIG_SYS_MAX_FLASH_BANKS
78 return CONFIG_SYS_MAX_FLASH_BANKS;
79#else
80 return 0;
81#endif
82}
83
84int spi_flash_mtd_register(struct spi_flash *flash)
85{
Boris Brezillon492151b2018-12-02 10:54:25 +010086 int ret;
87
Boris Brezillon08898e82018-12-02 10:54:32 +010088 if (sf_mtd_registered) {
89 ret = del_mtd_device(&sf_mtd_info);
90 if (ret)
91 return ret;
92
93 sf_mtd_registered = false;
94 }
Boris Brezillon492151b2018-12-02 10:54:25 +010095
96 sf_mtd_registered = false;
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020097 memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
98 sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
99
100 sf_mtd_info.name = sf_mtd_name;
101 sf_mtd_info.type = MTD_NORFLASH;
102 sf_mtd_info.flags = MTD_CAP_NORFLASH;
103 sf_mtd_info.writesize = 1;
104 sf_mtd_info.writebufsize = flash->page_size;
105
106 sf_mtd_info._erase = spi_flash_mtd_erase;
107 sf_mtd_info._read = spi_flash_mtd_read;
108 sf_mtd_info._write = spi_flash_mtd_write;
109 sf_mtd_info._sync = spi_flash_mtd_sync;
110
111 sf_mtd_info.size = flash->size;
112 sf_mtd_info.priv = flash;
113
114 /* Only uniform flash devices for now */
115 sf_mtd_info.numeraseregions = 0;
116 sf_mtd_info.erasesize = flash->sector_size;
117
Boris Brezillon492151b2018-12-02 10:54:25 +0100118 ret = add_mtd_device(&sf_mtd_info);
119 if (!ret)
120 sf_mtd_registered = true;
121
122 return ret;
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +0200123}
124
125void spi_flash_mtd_unregister(void)
126{
Boris Brezillon08898e82018-12-02 10:54:32 +0100127 int ret;
128
129 if (!sf_mtd_registered)
130 return;
131
132 ret = del_mtd_device(&sf_mtd_info);
133 if (!ret) {
134 sf_mtd_registered = false;
135 return;
136 }
137
138 /*
139 * Setting mtd->priv to NULL is the best we can do. Thanks to that,
140 * the MTD layer can still call mtd hooks without risking a
141 * use-after-free bug. Still, things should be fixed to prevent the
142 * spi_flash object from being destroyed when del_mtd_device() fails.
143 */
144 sf_mtd_info.priv = NULL;
145 printf("Failed to unregister MTD %s and the spi_flash object is going away: you're in deep trouble!",
146 sf_mtd_info.name);
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +0200147}