60185341355046d279dab787bb5b6e3359c1ebb3
[15.05/openwrt.git] / target / linux / ramips / patches-3.10 / 0206-MTD-add-chunked-read-io-to-m25p80.patch
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
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.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
15 @@ -392,6 +392,57 @@
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 +       while (idx < len) {
40 +               int rlen = (len - idx > 4) ? (4) : (len - idx);
41 +
42 +               t[1].rx_buf = &buf[idx];
43 +               t[1].len = rlen;
44 +
45 +               mutex_lock(&flash->lock);
46 +
47 +               /* Wait till previous write/erase is done. */
48 +               if (wait_till_ready(flash)) {
49 +                       /* REVISIT status return?? */
50 +                       mutex_unlock(&flash->lock);
51 +                       return 1;
52 +               }
53 +
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);
58 +
59 +               spi_sync(flash->spi, &m);
60 +
61 +               *retlen = m.actual_length - m25p_cmdsz(flash) -
62 +                       (flash->fast_read ? 1 : 0);
63 +
64 +               mutex_unlock(&flash->lock);
65 +               idx += rlen;
66 +       }
67 +       return 0;
68 +}
69 +
70  /*
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
73 @@ -479,6 +530,76 @@
74         return 0;
75  }
76  
77 +static int m25p80_write_chunked(struct mtd_info *mtd, loff_t to, size_t len,
78 +       size_t *retlen, const u_char *buf)
79 +{
80 +       struct m25p *flash = mtd_to_m25p(mtd);
81 +       struct spi_transfer t;
82 +       struct spi_message m;
83 +       u32 i, page_size;
84 +       u8 tmp[8];
85 +
86 +       pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
87 +                       __func__, (u32)to, len);
88 +
89 +       spi_message_init(&m);
90 +       memset(&t, 0, (sizeof t));
91 +
92 +       t.tx_buf = tmp;
93 +       t.len = 8;
94 +       spi_message_add_tail(&t, &m);
95 +
96 +       mutex_lock(&flash->lock);
97 +
98 +       /* Wait until finished previous write command. */
99 +       if (wait_till_ready(flash)) {
100 +               mutex_unlock(&flash->lock);
101 +               return 1;
102 +       }
103 +
104 +       write_enable(flash);
105 +
106 +       /* Set up the opcode in the write buffer. */
107 +       flash->command[0] = OPCODE_PP;
108 +       m25p_addr2cmd(flash, to, flash->command);
109 +
110 +       t.len = 4 + (to & 0x3);
111 +       if (t.len == 4)
112 +               t.len = 8;
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;
117 +
118 +       *retlen = m.actual_length - m25p_cmdsz(flash);
119 +
120 +       /* write everything in flash->page_size chunks */
121 +       for (i = page_size; i < len; i += page_size) {
122 +               page_size = len - i;
123 +               if (page_size > 4)
124 +                       page_size = 4;
125 +
126 +               /* write the next page to flash */
127 +               m25p_addr2cmd(flash, to + i, flash->command);
128 +
129 +               memcpy(tmp, flash->command, 4);
130 +               memcpy(&tmp[4], buf + i, page_size);
131 +               t.len = 4 + page_size;
132 +
133 +               wait_till_ready(flash);
134 +
135 +               write_enable(flash);
136 +
137 +               spi_sync(flash->spi, &m);
138 +
139 +               *retlen += m.actual_length - m25p_cmdsz(flash);
140 +       }
141 +
142 +       mutex_unlock(&flash->lock);
143 +
144 +       return 0;
145 +}
146 +
147  static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
148                 size_t *retlen, const u_char *buf)
149  {
150 @@ -1057,6 +1178,12 @@
151                 flash->fast_read = true;
152  #endif
153  
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;
158 +       }
159 +
160  #ifdef CONFIG_M25PXX_USE_FAST_READ
161         flash->fast_read = true;
162  #endif