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 Index: linux-3.10.21/drivers/mtd/devices/m25p80.c
12 ===================================================================
13 --- linux-3.10.21.orig/drivers/mtd/devices/m25p80.c 2013-12-09 19:34:40.828651303 +0100
14 +++ linux-3.10.21/drivers/mtd/devices/m25p80.c 2013-12-09 19:40:37.636659793 +0100
19 +static int m25p80_read_chunked(struct mtd_info *mtd, loff_t from, size_t len,
20 + size_t *retlen, u_char *buf)
22 + struct m25p *flash = mtd_to_m25p(mtd);
23 + struct spi_transfer t[2];
24 + struct spi_message m;
28 + pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
29 + __func__, (u32)from, len);
31 + spi_message_init(&m);
32 + memset(t, 0, (sizeof t));
34 + t[0].tx_buf = flash->command;
35 + t[0].len = m25p_cmdsz(flash);
36 + spi_message_add_tail(&t[0], &m);
37 + spi_message_add_tail(&t[1], &m);
40 + int rlen = (len - idx > 4) ? (4) : (len - idx);
42 + t[1].rx_buf = &buf[idx];
45 + mutex_lock(&flash->lock);
47 + /* Wait till previous write/erase is done. */
48 + if (wait_till_ready(flash)) {
49 + /* REVISIT status return?? */
50 + mutex_unlock(&flash->lock);
54 + /* Set up the write data buffer. */
55 + opcode = OPCODE_NORM_READ;
56 + flash->command[0] = opcode;
57 + m25p_addr2cmd(flash, from + idx, flash->command);
59 + spi_sync(flash->spi, &m);
61 + *retlen = m.actual_length - m25p_cmdsz(flash) -
62 + (flash->fast_read ? 1 : 0);
64 + mutex_unlock(&flash->lock);
71 * Write an address range to the flash chip. Data must be written in
72 * FLASH_PAGESIZE chunks. The address range may be any size provided
77 +static int m25p80_write_chunked(struct mtd_info *mtd, loff_t to, size_t len,
78 + size_t *retlen, const u_char *buf)
80 + struct m25p *flash = mtd_to_m25p(mtd);
81 + struct spi_transfer t;
82 + struct spi_message m;
86 + pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
87 + __func__, (u32)to, len);
89 + spi_message_init(&m);
90 + memset(&t, 0, (sizeof t));
94 + spi_message_add_tail(&t, &m);
96 + mutex_lock(&flash->lock);
98 + /* Wait until finished previous write command. */
99 + if (wait_till_ready(flash)) {
100 + mutex_unlock(&flash->lock);
104 + write_enable(flash);
106 + /* Set up the opcode in the write buffer. */
107 + flash->command[0] = OPCODE_PP;
108 + m25p_addr2cmd(flash, to, flash->command);
110 + t.len = 4 + (to & 0x3);
113 + memcpy(tmp, flash->command, 4);
114 + memcpy(&tmp[4], buf, t.len - 4);
115 + spi_sync(flash->spi, &m);
116 + page_size = t.len - 4;
118 + *retlen = m.actual_length - m25p_cmdsz(flash);
120 + /* write everything in flash->page_size chunks */
121 + for (i = page_size; i < len; i += page_size) {
122 + page_size = len - i;
126 + /* write the next page to flash */
127 + m25p_addr2cmd(flash, to + i, flash->command);
129 + memcpy(tmp, flash->command, 4);
130 + memcpy(&tmp[4], buf + i, page_size);
131 + t.len = 4 + page_size;
133 + wait_till_ready(flash);
135 + write_enable(flash);
137 + spi_sync(flash->spi, &m);
139 + *retlen += m.actual_length - m25p_cmdsz(flash);
142 + mutex_unlock(&flash->lock);
147 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
148 size_t *retlen, const u_char *buf)
150 @@ -1057,6 +1178,12 @@
151 flash->fast_read = true;
154 + if (np && of_property_read_bool(np, "m25p,chunked-io")) {
155 + dev_warn(&spi->dev, "using chunked io\n");
156 + flash->mtd._read = m25p80_read_chunked;
157 + flash->mtd._write = m25p80_write_chunked;
160 #ifdef CONFIG_M25PXX_USE_FAST_READ
161 flash->fast_read = true;