ramips: fix 16 bit IO on newer cores
[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,61 @@ 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 +       if (nor->addr_width > 3)
35 +               chunk_size -= nor->addr_width - 3;
36 +
37 +       while (retlen < _len) {
38 +               size_t len = min_t(int, chunk_size, _len - retlen);
39 +               const u_char *buf = _buf + retlen;
40 +               loff_t from = _from + retlen;
41 +
42 +               nor->wait_till_ready(nor);
43 +               nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
44 +
45 +               m25p80_write(nor, from, len, &retlen, buf);
46 +       }
47 +       *_retlen += retlen;
48 +}
49 +
50 +static int m25p80_chunked_read(struct spi_nor *nor, loff_t _from, size_t _len,
51 +                       size_t *_retlen, u_char *_buf)
52 +{
53 +       struct m25p *flash = nor->priv;
54 +       int chunk_size;
55 +
56 +       chunk_size = flash->chunk_size;
57 +       if (!chunk_size)
58 +               chunk_size = _len;
59 +
60 +       *_retlen = 0;
61 +
62 +       while (*_retlen < _len) {
63 +               size_t len = min_t(int, chunk_size, _len - *_retlen);
64 +               u_char *buf = _buf + *_retlen;
65 +               loff_t from = _from + *_retlen;
66 +               int retlen = 0;
67 +               int ret = m25p80_read(nor, from, len, &retlen, buf);
68 +
69 +               if (ret)
70 +                       return ret;
71 +
72 +               *_retlen += retlen;
73 +       }
74 +
75 +       return 0;
76 +}
77 +
78  static int m25p80_erase(struct spi_nor *nor, loff_t offset)
79  {
80         struct m25p *flash = nor->priv;
81 @@ -197,6 +254,7 @@ static int m25p_probe(struct spi_device
82         struct spi_nor *nor;
83         enum read_mode mode = SPI_NOR_NORMAL;
84         char *flash_name = NULL;
85 +       u32 val;
86         int ret;
87  
88         data = dev_get_platdata(&spi->dev);
89 @@ -244,6 +302,14 @@ static int m25p_probe(struct spi_device
90         if (ret)
91                 return ret;
92  
93 +       if (spi->dev.of_node &&
94 +           !of_property_read_u32(spi->dev.of_node, "m25p,chunked-io", &val)) {
95 +               dev_warn(&spi->dev, "using chunked io\n");
96 +               nor->read = m25p80_chunked_read;
97 +               nor->write = m25p80_chunked_write;
98 +               flash->chunk_size = val;
99 +       }
100 +
101         ppdata.of_node = spi->dev.of_node;
102  
103         return mtd_device_parse_register(&flash->mtd, NULL, &ppdata,