ar71xx: build firmware image for the WNDR3700 v4 board
[openwrt/staging/wigyori.git] / package / boot / uboot-lantiq / patches / 0016-sf-add-MTD-layer-driver-for-SPI-flash-devices.patch
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
5
6 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
7
8 --- a/drivers/mtd/spi/Makefile
9 +++ b/drivers/mtd/spi/Makefile
10 @@ -30,6 +30,7 @@ COBJS-$(CONFIG_SPL_SPI_LOAD) += spi_spl_
11 endif
12
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
21 }
22 #endif
23
24 +/* SPI flash MTD adapter init */
25 +int spi_flash_mtd_init(struct spi_flash *flash);
26 +
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);
30 --- /dev/null
31 +++ b/drivers/mtd/spi/spi_flash_mtd.c
32 @@ -0,0 +1,101 @@
33 +/*
34 + * (C) Copyright 2012 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
35 + *
36 + * MTD layer driver for SPI flash devices
37 + *
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.
40 + */
41 +
42 +#include <common.h>
43 +#include <malloc.h>
44 +#include <asm/errno.h>
45 +#include <linux/mtd/mtd.h>
46 +#include <spi_flash.h>
47 +
48 +static struct mtd_info sf_mtd_info;
49 +static char sf_mtd_name[8];
50 +
51 +static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
52 +{
53 + struct spi_flash *flash = mtd->priv;
54 + int err;
55 +
56 + instr->state = MTD_ERASING;
57 +
58 + err = spi_flash_erase(flash, instr->addr, instr->len);
59 + if (err) {
60 + instr->state = MTD_ERASE_FAILED;
61 + instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
62 + return -EIO;
63 + }
64 +
65 + instr->state = MTD_ERASE_DONE;
66 + mtd_erase_callback(instr);
67 +
68 + return 0;
69 +}
70 +
71 +static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
72 + size_t *retlen, u_char *buf)
73 +{
74 + struct spi_flash *flash = mtd->priv;
75 + int err;
76 +
77 + err = spi_flash_read(flash, from, len, buf);
78 + if (!err)
79 + *retlen = len;
80 +
81 + return err;
82 +}
83 +
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)
86 +{
87 + struct spi_flash *flash = mtd->priv;
88 + int err;
89 +
90 + err = spi_flash_write(flash, to, len, buf);
91 + if (!err)
92 + *retlen = len;
93 +
94 + return err;
95 +}
96 +
97 +static void spi_flash_mtd_sync(struct mtd_info *mtd)
98 +{
99 +}
100 +
101 +static int spi_flash_mtd_number(void)
102 +{
103 +#ifdef CONFIG_SYS_MAX_FLASH_BANKS
104 + return CONFIG_SYS_MAX_FLASH_BANKS;
105 +#else
106 + return 0;
107 +#endif
108 +}
109 +
110 +int spi_flash_mtd_init(struct spi_flash *flash)
111 +{
112 + memset(&sf_mtd_info, 0, sizeof(sf_mtd_info));
113 + sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number());
114 +
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;
119 +
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;
124 +
125 + sf_mtd_info.size = flash->size;
126 + sf_mtd_info.priv = flash;
127 +
128 + /* Only uniform flash devices for now */
129 + sf_mtd_info.numeraseregions = 0;
130 + sf_mtd_info.erasesize = flash->sector_size;
131 +
132 + return add_mtd_device(&sf_mtd_info);
133 +}