1 From 7f6ded11965b09daf6da44d4fa98da17b9fba36c Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Tue, 6 Nov 2012 19:41:26 +0100
4 Subject: sf: add MTD layer driver for SPI flash devices
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
8 --- a/drivers/mtd/spi/Makefile
9 +++ b/drivers/mtd/spi/Makefile
10 @@ -30,6 +30,7 @@ COBJS-$(CONFIG_SPL_SPI_LOAD) += spi_spl_
13 COBJS-$(CONFIG_SPI_FLASH) += spi_flash.o
14 +COBJS-$(CONFIG_SPI_FLASH_MTD) += spi_flash_mtd.o
15 COBJS-$(CONFIG_SPI_FLASH_ATMEL) += atmel.o
16 COBJS-$(CONFIG_SPI_FLASH_EON) += eon.o
17 COBJS-$(CONFIG_SPI_FLASH_MACRONIX) += macronix.o
18 --- a/drivers/mtd/spi/spi_flash_internal.h
19 +++ b/drivers/mtd/spi/spi_flash_internal.h
20 @@ -122,6 +122,9 @@ static inline int spi_flash_set_4byte_mo
24 +/* SPI flash MTD adapter init */
25 +int spi_flash_mtd_init(struct spi_flash *flash);
27 /* Manufacturer-specific probe functions */
28 int spi_flash_probe_spansion(struct spi_flash *flash, u8 *idcode);
29 int spi_flash_probe_atmel(struct spi_flash *flash, u8 *idcode);
31 +++ b/drivers/mtd/spi/spi_flash_mtd.c
34 + * (C) Copyright 2012 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
36 + * MTD layer driver for SPI flash devices
38 + * This file is released under the terms of GPL v2 and any later version.
39 + * See the file COPYING in the root directory of the source tree for details.
44 +#include <asm/errno.h>
45 +#include <linux/mtd/mtd.h>
46 +#include <spi_flash.h>
48 +static struct mtd_info sf_mtd_info;
49 +static char sf_mtd_name[8];
51 +static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
53 + struct spi_flash *flash = mtd->priv;
56 + instr->state = MTD_ERASING;
58 + err = spi_flash_erase(flash, instr->addr, instr->len);
60 + instr->state = MTD_ERASE_FAILED;
61 + instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
65 + instr->state = MTD_ERASE_DONE;
66 + mtd_erase_callback(instr);
71 +static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
72 + size_t *retlen, u_char *buf)
74 + struct spi_flash *flash = mtd->priv;
77 + err = spi_flash_read(flash, from, len, buf);
84 +static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
85 + size_t *retlen, const u_char *buf)
87 + struct spi_flash *flash = mtd->priv;
90 + err = spi_flash_write(flash, to, len, buf);
97 +static void spi_flash_mtd_sync(struct mtd_info *mtd)
101 +static int spi_flash_mtd_number(void)
103 +#ifdef CONFIG_SYS_MAX_FLASH_BANKS
104 + return CONFIG_SYS_MAX_FLASH_BANKS;
110 +int spi_flash_mtd_init(struct spi_flash *flash)
112 + memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
113 + sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
115 + sf_mtd_info.name = sf_mtd_name;
116 + sf_mtd_info.type = MTD_NORFLASH;
117 + sf_mtd_info.flags = MTD_CAP_NORFLASH;
118 + sf_mtd_info.writesize = 1;
120 + sf_mtd_info.erase = spi_flash_mtd_erase;
121 + sf_mtd_info.read = spi_flash_mtd_read;
122 + sf_mtd_info.write = spi_flash_mtd_write;
123 + sf_mtd_info.sync = spi_flash_mtd_sync;
125 + sf_mtd_info.size = flash->size;
126 + sf_mtd_info.priv = flash;
128 + /* Only uniform flash devices for now */
129 + sf_mtd_info.numeraseregions = 0;
130 + sf_mtd_info.erasesize = flash->sector_size;
132 + return add_mtd_device(&sf_mtd_info);