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