blob: aabbc3589435e83247e13c70d5d55a17a636d777 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +02002/*
3 * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +02004 */
5
6#include <common.h>
7#include <malloc.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +09008#include <linux/errno.h>
Daniel Schwierzeck06cfc032015-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 Brezillon9080e282018-12-02 10:54:25 +010013static bool sf_mtd_registered;
Daniel Schwierzeck06cfc032015-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
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
36static 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
49static 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
62static void spi_flash_mtd_sync(struct mtd_info *mtd)
63{
64}
65
66static 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
75int spi_flash_mtd_register(struct spi_flash *flash)
76{
Boris Brezillon9080e282018-12-02 10:54:25 +010077 int ret;
78
79 if (sf_mtd_registered)
80 del_mtd_device(&sf_mtd_info);
81
82 sf_mtd_registered = false;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +020083 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
Boris Brezillon9080e282018-12-02 10:54:25 +0100104 ret = add_mtd_device(&sf_mtd_info);
105 if (!ret)
106 sf_mtd_registered = true;
107
108 return ret;
Daniel Schwierzeck06cfc032015-04-27 07:42:04 +0200109}
110
111void spi_flash_mtd_unregister(void)
112{
113 del_mtd_device(&sf_mtd_info);
114}