ramips: rework and fix m25p80 chunked-io support
[openwrt.git] / target / linux / ramips / patches-3.14 / 0044-mtd-add-chunked-read-io-to-m25p80.patch
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
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
8 ---
9  drivers/mtd/devices/m25p80.c |  128 ++++++++++++++++++++++++++++++++++++++++++
10  1 file changed, 128 insertions(+)
11
12 --- a/drivers/mtd/devices/m25p80.c
13 +++ b/drivers/mtd/devices/m25p80.c
14 @@ -110,6 +110,7 @@ struct m25p {
15         struct mtd_info         mtd;
16         u16                     page_size;
17         u16                     addr_width;
18 +       u16                     chunk_size;
19         u8                      erase_opcode;
20         u8                      read_opcode;
21         u8                      program_opcode;
22 @@ -562,6 +563,89 @@ static int m25p80_read(struct mtd_info *
23         return 0;
24  }
25  
26 +static int m25p80_read_chunked(struct mtd_info *mtd, loff_t from, size_t len,
27 +       size_t *retlen, u_char *buf)
28 +{
29 +       struct m25p *flash = mtd_to_m25p(mtd);
30 +       struct spi_transfer t[2];
31 +       struct spi_message m;
32 +       uint8_t opcode;
33 +       int idx, rlen;
34 +
35 +       pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
36 +                       __func__, (u32)from, len);
37 +
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);
43 +               return 1;
44 +       }
45 +
46 +       spi_message_init(&m);
47 +       memset(t, 0, (sizeof t));
48 +
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);
53 +
54 +       *retlen = 0;
55 +
56 +       for (idx = 0; idx < len; idx += rlen) {
57 +               rlen = min_t(int, flash->chunk_size, len - idx);
58 +
59 +               if (idx)
60 +                       wait_till_ready(flash);
61 +
62 +               t[1].rx_buf = &buf[idx];
63 +               t[1].len = rlen;
64 +
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);
69 +
70 +               spi_sync(flash->spi, &m);
71 +
72 +               *retlen += m.actual_length - m25p_cmdsz(flash);
73 +       }
74 +
75 +       mutex_unlock(&flash->lock);
76 +
77 +       return 0;
78 +}
79 +
80 +static int m25p80_write_data(struct m25p *flash, struct spi_message *m,
81 +                            struct spi_transfer *t, int to)
82 +{
83 +       const void *buf = t->tx_buf;
84 +       int len = t->len;
85 +       int retlen = 0;
86 +       int chunk_size;
87 +
88 +       chunk_size = flash->chunk_size;
89 +       if (!chunk_size)
90 +               chunk_size = len;
91 +
92 +       while (retlen < len) {
93 +               t->tx_buf = buf + retlen;
94 +               t->len = min_t(int, chunk_size, len - retlen);
95 +
96 +               if (retlen)
97 +                       wait_till_ready(flash);
98 +
99 +               write_enable(flash);
100 +               m25p_addr2cmd(flash, to + retlen, flash->command);
101 +               spi_sync(flash->spi, m);
102 +
103 +               retlen += m->actual_length - m25p_cmdsz(flash);
104 +       }
105 +
106 +       return retlen;
107 +}
108 +
109  /*
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 
113                 return 1;
114         }
115  
116 -       write_enable(flash);
117 -
118         /* Set up the opcode in the write buffer. */
119         flash->command[0] = flash->program_opcode;
120 -       m25p_addr2cmd(flash, to, flash->command);
121  
122         page_offset = to & (flash->page_size - 1);
123  
124 @@ -608,9 +689,7 @@ static int m25p80_write(struct mtd_info 
125         if (page_offset + len <= flash->page_size) {
126                 t[1].len = len;
127  
128 -               spi_sync(flash->spi, &m);
129 -
130 -               *retlen = m.actual_length - m25p_cmdsz(flash);
131 +               *retlen = m25p80_write_data(flash, &m, &t[1], to);
132         } else {
133                 u32 i;
134  
135 @@ -618,9 +697,7 @@ static int m25p80_write(struct mtd_info 
136                 page_size = flash->page_size - page_offset;
137  
138                 t[1].len = page_size;
139 -               spi_sync(flash->spi, &m);
140 -
141 -               *retlen = m.actual_length - m25p_cmdsz(flash);
142 +               *retlen = m25p80_write_data(flash, &m, &t[1], to);
143  
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;
149  
150 -                       /* write the next page to flash */
151 -                       m25p_addr2cmd(flash, to + i, flash->command);
152 -
153                         t[1].tx_buf = buf + i;
154                         t[1].len = page_size;
155  
156                         wait_till_ready(flash);
157  
158 -                       write_enable(flash);
159 -
160 -                       spi_sync(flash->spi, &m);
161 -
162 -                       *retlen += m.actual_length - m25p_cmdsz(flash);
163 +                       *retlen += m25p80_write_data(flash, &m, &t[1], to + i);
164                 }
165         }
166  
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;
170         int ret;
171 +       u32 val;
172  
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;
178  
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;
183 +       }
184 +
185         /* flash protection support for STmicro chips */
186         if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ST) {
187                 flash->mtd._lock = m25p80_lock;