6123ec110591a5aad3b0bf8a1520be212256a5c5
[openwrt/openwrt.git] / target / linux / ramips / patches-3.10 / 0131-mtd-add-chunked-read-io-to-m25p80.patch
1 From f85e6dacdb7d7a9bc37f33cf8770006ab64286f7 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 131/133] mtd: add chunked read io to m25p80
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8 drivers/mtd/devices/m25p80.c | 127 ++++++++++++++++++++++++++++++++++++++++++
9 1 file changed, 127 insertions(+)
10
11 Index: linux-3.10.34/drivers/mtd/devices/m25p80.c
12 ===================================================================
13 --- linux-3.10.34.orig/drivers/mtd/devices/m25p80.c 2014-03-30 19:40:10.135292951 +0100
14 +++ linux-3.10.34/drivers/mtd/devices/m25p80.c 2014-03-30 19:44:40.151295482 +0100
15 @@ -392,6 +392,58 @@
16 return 0;
17 }
18
19 +static int m25p80_read_chunked(struct mtd_info *mtd, loff_t from, size_t len,
20 + size_t *retlen, u_char *buf)
21 +{
22 + struct m25p *flash = mtd_to_m25p(mtd);
23 + struct spi_transfer t[2];
24 + struct spi_message m;
25 + uint8_t opcode;
26 + int idx = 0;
27 +
28 + pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
29 + __func__, (u32)from, len);
30 +
31 + spi_message_init(&m);
32 + memset(t, 0, (sizeof t));
33 +
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);
38 +
39 + *retlen = 0;
40 +
41 + while (idx < len) {
42 + int rlen = (len - idx > 4) ? (4) : (len - idx);
43 +
44 + t[1].rx_buf = &buf[idx];
45 + t[1].len = rlen;
46 +
47 + mutex_lock(&flash->lock);
48 +
49 + /* Wait till previous write/erase is done. */
50 + if (wait_till_ready(flash)) {
51 + /* REVISIT status return?? */
52 + mutex_unlock(&flash->lock);
53 + return 1;
54 + }
55 +
56 + /* Set up the write data buffer. */
57 + opcode = OPCODE_NORM_READ;
58 + flash->command[0] = opcode;
59 + m25p_addr2cmd(flash, from + idx, flash->command);
60 +
61 + spi_sync(flash->spi, &m);
62 +
63 + *retlen += m.actual_length - m25p_cmdsz(flash);
64 +
65 + mutex_unlock(&flash->lock);
66 + idx += rlen;
67 + }
68 + return 0;
69 +}
70 +
71 /*
72 * Write an address range to the flash chip. Data must be written in
73 * FLASH_PAGESIZE chunks. The address range may be any size provided
74 @@ -479,6 +531,76 @@
75 return 0;
76 }
77
78 +static int m25p80_write_chunked(struct mtd_info *mtd, loff_t to, size_t len,
79 + size_t *retlen, const u_char *buf)
80 +{
81 + struct m25p *flash = mtd_to_m25p(mtd);
82 + struct spi_transfer t;
83 + struct spi_message m;
84 + u32 i, page_size;
85 + u8 tmp[8];
86 +
87 + pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
88 + __func__, (u32)to, len);
89 +
90 + spi_message_init(&m);
91 + memset(&t, 0, (sizeof t));
92 +
93 + t.tx_buf = tmp;
94 + t.len = 8;
95 + spi_message_add_tail(&t, &m);
96 +
97 + mutex_lock(&flash->lock);
98 +
99 + /* Wait until finished previous write command. */
100 + if (wait_till_ready(flash)) {
101 + mutex_unlock(&flash->lock);
102 + return 1;
103 + }
104 +
105 + write_enable(flash);
106 +
107 + /* Set up the opcode in the write buffer. */
108 + flash->command[0] = OPCODE_PP;
109 + m25p_addr2cmd(flash, to, flash->command);
110 +
111 + t.len = 4 + (to & 0x3);
112 + if (t.len == 4)
113 + t.len = 8;
114 + memcpy(tmp, flash->command, 4);
115 + memcpy(&tmp[4], buf, t.len - 4);
116 + spi_sync(flash->spi, &m);
117 + page_size = t.len - 4;
118 +
119 + *retlen = m.actual_length - m25p_cmdsz(flash);
120 +
121 + /* write everything in flash->page_size chunks */
122 + for (i = page_size; i < len; i += page_size) {
123 + page_size = len - i;
124 + if (page_size > 4)
125 + page_size = 4;
126 +
127 + /* write the next page to flash */
128 + m25p_addr2cmd(flash, to + i, flash->command);
129 +
130 + memcpy(tmp, flash->command, 4);
131 + memcpy(&tmp[4], buf + i, page_size);
132 + t.len = 4 + page_size;
133 +
134 + wait_till_ready(flash);
135 +
136 + write_enable(flash);
137 +
138 + spi_sync(flash->spi, &m);
139 +
140 + *retlen += m.actual_length - m25p_cmdsz(flash);
141 + }
142 +
143 + mutex_unlock(&flash->lock);
144 +
145 + return 0;
146 +}
147 +
148 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
149 size_t *retlen, const u_char *buf)
150 {
151 @@ -1058,6 +1180,12 @@
152 flash->fast_read = true;
153 #endif
154
155 + if (np && of_property_read_bool(np, "m25p,chunked-io")) {
156 + dev_warn(&spi->dev, "using chunked io\n");
157 + flash->mtd._read = m25p80_read_chunked;
158 + flash->mtd._write = m25p80_write_chunked;
159 + }
160 +
161 #ifdef CONFIG_M25PXX_USE_FAST_READ
162 flash->fast_read = true;
163 #endif