ralink: add 3.14 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 ---
8  drivers/mtd/devices/m25p80.c |  128 ++++++++++++++++++++++++++++++++++++++++++
9  1 file changed, 128 insertions(+)
10
11 diff --git a/drivers/mtd/devices/m25p80.c b/drivers/mtd/devices/m25p80.c
12 index ad19139..6f4290e 100644
13 --- a/drivers/mtd/devices/m25p80.c
14 +++ b/drivers/mtd/devices/m25p80.c
15 @@ -556,6 +556,58 @@ static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
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 @@ -643,6 +695,76 @@ static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
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 @@ -1252,6 +1374,12 @@ static int m25p_probe(struct spi_device *spi)
152                 return -EINVAL;
153         }
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         flash->program_opcode = OPCODE_PP;
162  
163         if (info->addr_width)
164 -- 
165 1.7.10.4
166