1 From b6d5d4c3d595b4cfb6a052ac7151fdb1d9a776ea Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 27 Jul 2014 09:58:09 +0100
4 Subject: [PATCH 44/57] mtd: add chunked read io to m25p80
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
9 drivers/mtd/devices/m25p80.c | 128 ++++++++++++++++++++++++++++++++++++++++++
10 1 file changed, 128 insertions(+)
12 --- a/drivers/mtd/devices/m25p80.c
13 +++ b/drivers/mtd/devices/m25p80.c
14 @@ -110,6 +110,7 @@ struct m25p {
22 @@ -562,6 +563,89 @@ static int m25p80_read(struct mtd_info *
26 +static int m25p80_read_chunked(struct mtd_info *mtd, loff_t from, size_t len,
27 + size_t *retlen, u_char *buf)
29 + struct m25p *flash = mtd_to_m25p(mtd);
30 + struct spi_transfer t[2];
31 + struct spi_message m;
35 + pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
36 + __func__, (u32)from, len);
38 + mutex_lock(&flash->lock);
39 + /* Wait till previous write/erase is done. */
40 + if (wait_till_ready(flash)) {
41 + /* REVISIT status return?? */
42 + mutex_unlock(&flash->lock);
46 + spi_message_init(&m);
47 + memset(t, 0, (sizeof t));
49 + t[0].tx_buf = flash->command;
50 + t[0].len = m25p_cmdsz(flash);
51 + spi_message_add_tail(&t[0], &m);
52 + spi_message_add_tail(&t[1], &m);
56 + for (idx = 0; idx < len; idx += rlen) {
57 + rlen = min_t(int, flash->chunk_size, len - idx);
60 + wait_till_ready(flash);
62 + t[1].rx_buf = &buf[idx];
65 + /* Set up the write data buffer. */
66 + opcode = OPCODE_NORM_READ;
67 + flash->command[0] = opcode;
68 + m25p_addr2cmd(flash, from + idx, flash->command);
70 + spi_sync(flash->spi, &m);
72 + *retlen += m.actual_length - m25p_cmdsz(flash);
75 + mutex_unlock(&flash->lock);
80 +static int m25p80_write_data(struct m25p *flash, struct spi_message *m,
81 + struct spi_transfer *t, int to)
83 + const void *buf = t->tx_buf;
88 + chunk_size = flash->chunk_size;
92 + while (retlen < len) {
93 + t->tx_buf = buf + retlen;
94 + t->len = min_t(int, chunk_size, len - retlen);
97 + wait_till_ready(flash);
99 + write_enable(flash);
100 + m25p_addr2cmd(flash, to + retlen, flash->command);
101 + spi_sync(flash->spi, m);
103 + retlen += m->actual_length - m25p_cmdsz(flash);
110 * Write an address range to the flash chip. Data must be written in
111 * FLASH_PAGESIZE chunks. The address range may be any size provided
112 @@ -596,11 +680,8 @@ static int m25p80_write(struct mtd_info
116 - write_enable(flash);
118 /* Set up the opcode in the write buffer. */
119 flash->command[0] = flash->program_opcode;
120 - m25p_addr2cmd(flash, to, flash->command);
122 page_offset = to & (flash->page_size - 1);
124 @@ -608,9 +689,7 @@ static int m25p80_write(struct mtd_info
125 if (page_offset + len <= flash->page_size) {
128 - spi_sync(flash->spi, &m);
130 - *retlen = m.actual_length - m25p_cmdsz(flash);
131 + *retlen = m25p80_write_data(flash, &m, &t[1], to);
135 @@ -618,9 +697,7 @@ static int m25p80_write(struct mtd_info
136 page_size = flash->page_size - page_offset;
138 t[1].len = page_size;
139 - spi_sync(flash->spi, &m);
141 - *retlen = m.actual_length - m25p_cmdsz(flash);
142 + *retlen = m25p80_write_data(flash, &m, &t[1], to);
144 /* write everything in flash->page_size chunks */
145 for (i = page_size; i < len; i += page_size) {
146 @@ -628,19 +705,12 @@ static int m25p80_write(struct mtd_info
147 if (page_size > flash->page_size)
148 page_size = flash->page_size;
150 - /* write the next page to flash */
151 - m25p_addr2cmd(flash, to + i, flash->command);
153 t[1].tx_buf = buf + i;
154 t[1].len = page_size;
156 wait_till_ready(flash);
158 - write_enable(flash);
160 - spi_sync(flash->spi, &m);
162 - *retlen += m.actual_length - m25p_cmdsz(flash);
163 + *retlen += m25p80_write_data(flash, &m, &t[1], to + i);
167 @@ -1105,6 +1175,7 @@ static int m25p_probe(struct spi_device
168 struct mtd_part_parser_data ppdata;
169 struct device_node *np = spi->dev.of_node;
173 /* Platform data helps sort out which chip type we have, as
174 * well as how this board partitions it. If we don't have
175 @@ -1187,6 +1258,12 @@ static int m25p_probe(struct spi_device
176 flash->mtd._erase = m25p80_erase;
177 flash->mtd._read = m25p80_read;
179 + if (np && !of_property_read_u32(np, "m25p,chunked-io", &val)) {
180 + dev_warn(&spi->dev, "using chunked io\n");
181 + flash->mtd._read = m25p80_read_chunked;
182 + flash->chunk_size = val;
185 /* flash protection support for STmicro chips */
186 if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ST) {
187 flash->mtd._lock = m25p80_lock;