2bffa1f6960789982dc7eb8643f111925b801628
[15.05/openwrt.git] / target / linux / ramips / patches-3.18 / 0044-mtd-add-chunked-read-io-to-m25p80.patch
1 --- a/drivers/mtd/devices/m25p80.c
2 +++ b/drivers/mtd/devices/m25p80.c
3 @@ -19,6 +19,7 @@
4  #include <linux/errno.h>
5  #include <linux/module.h>
6  #include <linux/device.h>
7 +#include <linux/of.h>
8  
9  #include <linux/mtd/mtd.h>
10  #include <linux/mtd/partitions.h>
11 @@ -32,6 +33,7 @@ struct m25p {
12         struct spi_device       *spi;
13         struct spi_nor          spi_nor;
14         struct mtd_info         mtd;
15 +       u16                     chunk_size;
16         u8                      command[MAX_CMD_SIZE];
17  };
18  
19 @@ -157,6 +159,58 @@ static int m25p80_read(struct spi_nor *n
20         return 0;
21  }
22  
23 +static void m25p80_chunked_write(struct spi_nor *nor, loff_t _from, size_t _len,
24 +                       size_t *_retlen, const u_char *_buf)
25 +{
26 +       struct m25p *flash = nor->priv;
27 +       int chunk_size;
28 +       int retlen = 0;
29 +
30 +       chunk_size = flash->chunk_size;
31 +       if (!chunk_size)
32 +               chunk_size = _len;
33 +
34 +       while (retlen < _len) {
35 +               size_t len = min_t(int, chunk_size, _len - retlen);
36 +               const u_char *buf = _buf + retlen;
37 +               loff_t from = _from + retlen;
38 +
39 +               nor->wait_till_ready(nor);
40 +               nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
41 +
42 +               m25p80_write(nor, from, len, &retlen, buf);
43 +       }
44 +       *_retlen += retlen;
45 +}
46 +
47 +static int m25p80_chunked_read(struct spi_nor *nor, loff_t _from, size_t _len,
48 +                       size_t *_retlen, u_char *_buf)
49 +{
50 +       struct m25p *flash = nor->priv;
51 +       int chunk_size;
52 +
53 +       chunk_size = flash->chunk_size;
54 +       if (!chunk_size)
55 +               chunk_size = _len;
56 +
57 +       *_retlen = 0;
58 +
59 +       while (*_retlen < _len) {
60 +               size_t len = min_t(int, chunk_size, _len - *_retlen);
61 +               u_char *buf = _buf + *_retlen;
62 +               loff_t from = _from + *_retlen;
63 +               int retlen = 0;
64 +               int ret = m25p80_read(nor, from, len, &retlen, buf);
65 +
66 +               if (ret)
67 +                       return ret;
68 +
69 +               *_retlen += retlen;
70 +       }
71 +
72 +       return 0;
73 +}
74 +
75  static int m25p80_erase(struct spi_nor *nor, loff_t offset)
76  {
77         struct m25p *flash = nor->priv;
78 @@ -197,6 +251,7 @@ static int m25p_probe(struct spi_device
79         struct spi_nor *nor;
80         enum read_mode mode = SPI_NOR_NORMAL;
81         char *flash_name = NULL;
82 +       u32 val;
83         int ret;
84  
85         data = dev_get_platdata(&spi->dev);
86 @@ -244,6 +299,14 @@ static int m25p_probe(struct spi_device
87         if (ret)
88                 return ret;
89  
90 +       if (spi->dev.of_node &&
91 +           !of_property_read_u32(spi->dev.of_node, "m25p,chunked-io", &val)) {
92 +               dev_warn(&spi->dev, "using chunked io\n");
93 +               nor->read = m25p80_chunked_read;
94 +               nor->write = m25p80_chunked_write;
95 +               flash->chunk_size = val;
96 +       }
97 +
98         ppdata.of_node = spi->dev.of_node;
99  
100         return mtd_device_parse_register(&flash->mtd, NULL, &ppdata,