1 From 926ae0ca5017a421709ab0478582683c29988b05 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Wed, 27 Nov 2013 20:58:16 +0100
4 Subject: [PATCH 10/20] MTD: add chunked read io to m25p80
6 Signed-off-by: John Crispin <blogic@openwrt.org>
8 drivers/mtd/devices/m25p80.c | 127 ++++++++++++++++++++++++++++++++++++++++++
9 1 file changed, 127 insertions(+)
11 --- a/drivers/mtd/devices/m25p80.c
12 +++ b/drivers/mtd/devices/m25p80.c
13 @@ -392,6 +392,57 @@ static int m25p80_read(struct mtd_info *
17 +static int m25p80_read_chunked(struct mtd_info *mtd, loff_t from, size_t len,
18 + size_t *retlen, u_char *buf)
20 + struct m25p *flash = mtd_to_m25p(mtd);
21 + struct spi_transfer t[2];
22 + struct spi_message m;
26 + pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
27 + __func__, (u32)from, len);
29 + spi_message_init(&m);
30 + memset(t, 0, (sizeof t));
32 + t[0].tx_buf = flash->command;
33 + t[0].len = m25p_cmdsz(flash);
34 + spi_message_add_tail(&t[0], &m);
35 + spi_message_add_tail(&t[1], &m);
38 + int rlen = (len - idx > 4) ? (4) : (len - idx);
40 + t[1].rx_buf = &buf[idx];
43 + mutex_lock(&flash->lock);
45 + /* Wait till previous write/erase is done. */
46 + if (wait_till_ready(flash)) {
47 + /* REVISIT status return?? */
48 + mutex_unlock(&flash->lock);
52 + /* Set up the write data buffer. */
53 + opcode = OPCODE_NORM_READ;
54 + flash->command[0] = opcode;
55 + m25p_addr2cmd(flash, from + idx, flash->command);
57 + spi_sync(flash->spi, &m);
59 + *retlen = m.actual_length - m25p_cmdsz(flash) -
60 + (flash->fast_read ? 1 : 0);
62 + mutex_unlock(&flash->lock);
69 * Write an address range to the flash chip. Data must be written in
70 * FLASH_PAGESIZE chunks. The address range may be any size provided
71 @@ -479,6 +530,76 @@ static int m25p80_write(struct mtd_info
75 +static int m25p80_write_chunked(struct mtd_info *mtd, loff_t to, size_t len,
76 + size_t *retlen, const u_char *buf)
78 + struct m25p *flash = mtd_to_m25p(mtd);
79 + struct spi_transfer t;
80 + struct spi_message m;
84 + pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
85 + __func__, (u32)to, len);
87 + spi_message_init(&m);
88 + memset(&t, 0, (sizeof t));
92 + spi_message_add_tail(&t, &m);
94 + mutex_lock(&flash->lock);
96 + /* Wait until finished previous write command. */
97 + if (wait_till_ready(flash)) {
98 + mutex_unlock(&flash->lock);
102 + write_enable(flash);
104 + /* Set up the opcode in the write buffer. */
105 + flash->command[0] = OPCODE_PP;
106 + m25p_addr2cmd(flash, to, flash->command);
108 + t.len = 4 + (to & 0x3);
111 + memcpy(tmp, flash->command, 4);
112 + memcpy(&tmp[4], buf, t.len - 4);
113 + spi_sync(flash->spi, &m);
114 + page_size = t.len - 4;
116 + *retlen = m.actual_length - m25p_cmdsz(flash);
118 + /* write everything in flash->page_size chunks */
119 + for (i = page_size; i < len; i += page_size) {
120 + page_size = len - i;
124 + /* write the next page to flash */
125 + m25p_addr2cmd(flash, to + i, flash->command);
127 + memcpy(tmp, flash->command, 4);
128 + memcpy(&tmp[4], buf + i, page_size);
129 + t.len = 4 + page_size;
131 + wait_till_ready(flash);
133 + write_enable(flash);
135 + spi_sync(flash->spi, &m);
137 + *retlen += m.actual_length - m25p_cmdsz(flash);
140 + mutex_unlock(&flash->lock);
145 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
146 size_t *retlen, const u_char *buf)
148 @@ -1058,6 +1179,12 @@ static int m25p_probe(struct spi_device
149 flash->fast_read = true;
152 + if (np && of_property_read_bool(np, "m25p,chunked-io")) {
153 + dev_warn(&spi->dev, "using chunked io\n");
154 + flash->mtd._read = m25p80_read_chunked;
155 + flash->mtd._write = m25p80_write_chunked;
158 #ifdef CONFIG_M25PXX_USE_FAST_READ
159 flash->fast_read = true;