blob: 987fac2501f41d76affcb8a3ea0c6eddfd711162 [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>
Simon Glassb79fdc72020-05-10 11:39:54 -06007#include <flash.h>
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +02008#include <malloc.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +09009#include <linux/errno.h>
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020010#include <linux/mtd/mtd.h>
11#include <spi_flash.h>
12
13static struct mtd_info sf_mtd_info;
Boris Brezillon492151b2018-12-02 10:54:25 +010014static bool sf_mtd_registered;
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020015static char sf_mtd_name[8];
16
17static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
18{
19 struct spi_flash *flash = mtd->priv;
20 int err;
21
Boris Brezillon08898e82018-12-02 10:54:32 +010022 if (!flash)
23 return -ENODEV;
24
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020025 instr->state = MTD_ERASING;
26
27 err = spi_flash_erase(flash, instr->addr, instr->len);
28 if (err) {
29 instr->state = MTD_ERASE_FAILED;
30 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
31 return -EIO;
32 }
33
34 instr->state = MTD_ERASE_DONE;
35 mtd_erase_callback(instr);
36
37 return 0;
38}
39
40static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
41 size_t *retlen, u_char *buf)
42{
43 struct spi_flash *flash = mtd->priv;
44 int err;
45
Boris Brezillon08898e82018-12-02 10:54:32 +010046 if (!flash)
47 return -ENODEV;
48
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020049 err = spi_flash_read(flash, from, len, buf);
50 if (!err)
51 *retlen = len;
52
53 return err;
54}
55
56static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
57 size_t *retlen, const u_char *buf)
58{
59 struct spi_flash *flash = mtd->priv;
60 int err;
61
Boris Brezillon08898e82018-12-02 10:54:32 +010062 if (!flash)
63 return -ENODEV;
64
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020065 err = spi_flash_write(flash, to, len, buf);
66 if (!err)
67 *retlen = len;
68
69 return err;
70}
71
72static void spi_flash_mtd_sync(struct mtd_info *mtd)
73{
74}
75
76static int spi_flash_mtd_number(void)
77{
78#ifdef CONFIG_SYS_MAX_FLASH_BANKS
79 return CONFIG_SYS_MAX_FLASH_BANKS;
80#else
81 return 0;
82#endif
83}
84
85int spi_flash_mtd_register(struct spi_flash *flash)
86{
Boris Brezillon492151b2018-12-02 10:54:25 +010087 int ret;
88
Boris Brezillon08898e82018-12-02 10:54:32 +010089 if (sf_mtd_registered) {
90 ret = del_mtd_device(&sf_mtd_info);
91 if (ret)
92 return ret;
93
94 sf_mtd_registered = false;
95 }
Boris Brezillon492151b2018-12-02 10:54:25 +010096
97 sf_mtd_registered = false;
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +020098 memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
99 sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
100
101 sf_mtd_info.name = sf_mtd_name;
102 sf_mtd_info.type = MTD_NORFLASH;
103 sf_mtd_info.flags = MTD_CAP_NORFLASH;
104 sf_mtd_info.writesize = 1;
105 sf_mtd_info.writebufsize = flash->page_size;
106
107 sf_mtd_info._erase = spi_flash_mtd_erase;
108 sf_mtd_info._read = spi_flash_mtd_read;
109 sf_mtd_info._write = spi_flash_mtd_write;
110 sf_mtd_info._sync = spi_flash_mtd_sync;
111
112 sf_mtd_info.size = flash->size;
113 sf_mtd_info.priv = flash;
114
115 /* Only uniform flash devices for now */
116 sf_mtd_info.numeraseregions = 0;
117 sf_mtd_info.erasesize = flash->sector_size;
118
Boris Brezillon492151b2018-12-02 10:54:25 +0100119 ret = add_mtd_device(&sf_mtd_info);
120 if (!ret)
121 sf_mtd_registered = true;
122
123 return ret;
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +0200124}
125
126void spi_flash_mtd_unregister(void)
127{
Boris Brezillon08898e82018-12-02 10:54:32 +0100128 int ret;
129
130 if (!sf_mtd_registered)
131 return;
132
133 ret = del_mtd_device(&sf_mtd_info);
134 if (!ret) {
135 sf_mtd_registered = false;
136 return;
137 }
138
139 /*
140 * Setting mtd->priv to NULL is the best we can do. Thanks to that,
141 * the MTD layer can still call mtd hooks without risking a
142 * use-after-free bug. Still, things should be fixed to prevent the
143 * spi_flash object from being destroyed when del_mtd_device() fails.
144 */
145 sf_mtd_info.priv = NULL;
146 printf("Failed to unregister MTD %s and the spi_flash object is going away: you're in deep trouble!",
147 sf_mtd_info.name);
Daniel Schwierzeck9fe6d872015-04-27 07:42:04 +0200148}