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>
8 drivers/mtd/devices/m25p80.c | 128 ++++++++++++++++++++++++++++++++++++++++++
9 1 file changed, 128 insertions(+)
11 --- a/drivers/mtd/devices/m25p80.c
12 +++ b/drivers/mtd/devices/m25p80.c
13 @@ -562,6 +562,58 @@ 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);
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);
63 + mutex_unlock(&flash->lock);
70 * Write an address range to the flash chip. Data must be written in
71 * FLASH_PAGESIZE chunks. The address range may be any size provided
72 @@ -649,6 +701,76 @@ static int m25p80_write(struct mtd_info
76 +static int m25p80_write_chunked(struct mtd_info *mtd, loff_t to, size_t len,
77 + size_t *retlen, const u_char *buf)
79 + struct m25p *flash = mtd_to_m25p(mtd);
80 + struct spi_transfer t;
81 + struct spi_message m;
85 + pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
86 + __func__, (u32)to, len);
88 + spi_message_init(&m);
89 + memset(&t, 0, (sizeof t));
93 + spi_message_add_tail(&t, &m);
95 + mutex_lock(&flash->lock);
97 + /* Wait until finished previous write command. */
98 + if (wait_till_ready(flash)) {
99 + mutex_unlock(&flash->lock);
103 + write_enable(flash);
105 + /* Set up the opcode in the write buffer. */
106 + flash->command[0] = OPCODE_PP;
107 + m25p_addr2cmd(flash, to, flash->command);
109 + t.len = 4 + (to & 0x3);
112 + memcpy(tmp, flash->command, 4);
113 + memcpy(&tmp[4], buf, t.len - 4);
114 + spi_sync(flash->spi, &m);
115 + page_size = t.len - 4;
117 + *retlen = m.actual_length - m25p_cmdsz(flash);
119 + /* write everything in flash->page_size chunks */
120 + for (i = page_size; i < len; i += page_size) {
121 + page_size = len - i;
125 + /* write the next page to flash */
126 + m25p_addr2cmd(flash, to + i, flash->command);
128 + memcpy(tmp, flash->command, 4);
129 + memcpy(&tmp[4], buf + i, page_size);
130 + t.len = 4 + page_size;
132 + wait_till_ready(flash);
134 + write_enable(flash);
136 + spi_sync(flash->spi, &m);
138 + *retlen += m.actual_length - m25p_cmdsz(flash);
141 + mutex_unlock(&flash->lock);
146 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
147 size_t *retlen, const u_char *buf)
149 @@ -1260,6 +1382,12 @@ static int m25p_probe(struct spi_device
153 + if (np && of_property_read_bool(np, "m25p,chunked-io")) {
154 + dev_warn(&spi->dev, "using chunked io\n");
155 + flash->mtd._read = m25p80_read_chunked;
156 + flash->mtd._write = m25p80_write_chunked;
159 flash->program_opcode = OPCODE_PP;
161 if (info->addr_width)