kernel: refresh patches
[openwrt.git] / target / linux / lantiq / patches-3.14 / 0160-owrt-lantiq-multiple-flash.patch
1 --- a/drivers/mtd/maps/lantiq-flash.c
2 +++ b/drivers/mtd/maps/lantiq-flash.c
3 @@ -20,6 +20,7 @@
4  #include <linux/mtd/cfi.h>
5  #include <linux/platform_device.h>
6  #include <linux/mtd/physmap.h>
7 +#include <linux/mtd/concat.h>
8  #include <linux/of.h>
9  
10  #include <lantiq_soc.h>
11 @@ -39,10 +40,12 @@ enum {
12         LTQ_NOR_NORMAL
13  };
14  
15 +#define MAX_RESOURCES          4
16 +
17  struct ltq_mtd {
18 -       struct resource *res;
19 -       struct mtd_info *mtd;
20 -       struct map_info *map;
21 +       struct mtd_info *mtd[MAX_RESOURCES];
22 +       struct mtd_info *cmtd;
23 +       struct map_info map[MAX_RESOURCES];
24  };
25  
26  static const char ltq_map_name[] = "ltq_nor";
27 @@ -110,12 +113,39 @@ ltq_copy_to(struct map_info *map, unsign
28  }
29  
30  static int
31 +ltq_mtd_remove(struct platform_device *pdev)
32 +{
33 +       struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
34 +       int i;
35 +
36 +       if (ltq_mtd == NULL)
37 +               return 0;
38 +
39 +       if (ltq_mtd->cmtd) {
40 +               mtd_device_unregister(ltq_mtd->cmtd);
41 +               if (ltq_mtd->cmtd != ltq_mtd->mtd[0])
42 +                       mtd_concat_destroy(ltq_mtd->cmtd);
43 +       }
44 +
45 +       for (i = 0; i < MAX_RESOURCES; i++) {
46 +               if (ltq_mtd->mtd[i] != NULL)
47 +                       map_destroy(ltq_mtd->mtd[i]);
48 +       }
49 +
50 +       kfree(ltq_mtd);
51 +
52 +       return 0;
53 +}
54 +
55 +static int
56  ltq_mtd_probe(struct platform_device *pdev)
57  {
58         struct mtd_part_parser_data ppdata;
59         struct ltq_mtd *ltq_mtd;
60         struct cfi_private *cfi;
61 -       int err;
62 +       int err = 0;
63 +       int i;
64 +       int devices_found = 0;
65  
66         static const char *rom_probe_types[] = {
67                 "cfi_probe", "jedec_probe", NULL
68 @@ -134,79 +164,88 @@ ltq_mtd_probe(struct platform_device *pd
69  
70         platform_set_drvdata(pdev, ltq_mtd);
71  
72 -       ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
73 -       if (!ltq_mtd->res) {
74 -               dev_err(&pdev->dev, "failed to get memory resource\n");
75 -               return -ENOENT;
76 +       for (i = 0; i < pdev->num_resources; i++) {
77 +               printk(KERN_NOTICE "lantiq nor flash device: %.8llx at %.8llx\n",
78 +                      (unsigned long long)resource_size(&pdev->resource[i]),
79 +                      (unsigned long long)pdev->resource[i].start);
80 +       
81 +               if (!devm_request_mem_region(&pdev->dev,
82 +                       pdev->resource[i].start,
83 +                       resource_size(&pdev->resource[i]),
84 +                       dev_name(&pdev->dev))) {
85 +                       dev_err(&pdev->dev, "Could not reserve memory region\n");
86 +                       return -ENOMEM;
87 +               }
88 +
89 +               ltq_mtd->map[i].name = ltq_map_name;
90 +               ltq_mtd->map[i].bankwidth = 2;
91 +               ltq_mtd->map[i].read = ltq_read16;
92 +               ltq_mtd->map[i].write = ltq_write16;
93 +               ltq_mtd->map[i].copy_from = ltq_copy_from;
94 +               ltq_mtd->map[i].copy_to = ltq_copy_to;
95 +
96 +               if (of_find_property(pdev->dev.of_node, "lantiq,noxip", NULL))
97 +                       ltq_mtd->map[i].phys = NO_XIP;
98 +               else
99 +                       ltq_mtd->map[i].phys = pdev->resource[i].start;
100 +               ltq_mtd->map[i].size = resource_size(&pdev->resource[i]);
101 +               ltq_mtd->map[i].virt = devm_ioremap(&pdev->dev, ltq_mtd->map[i].phys,
102 +                                                ltq_mtd->map[i].size);
103 +               if (IS_ERR(ltq_mtd->map[i].virt))
104 +                       return PTR_ERR(ltq_mtd->map[i].virt);
105 +
106 +               if (ltq_mtd->map[i].virt == NULL) {
107 +                       dev_err(&pdev->dev, "Failed to ioremap flash region\n");
108 +                       err = PTR_ERR(ltq_mtd->map[i].virt);
109 +                       goto err_out;
110 +               }
111 +
112 +               ltq_mtd->map[i].map_priv_1 = LTQ_NOR_PROBING;
113 +               for (type = rom_probe_types; !ltq_mtd->mtd[i] && *type; type++)
114 +                       ltq_mtd->mtd[i] = do_map_probe(*type, &ltq_mtd->map[i]);
115 +               ltq_mtd->map[i].map_priv_1 = LTQ_NOR_NORMAL;
116 +
117 +               if (!ltq_mtd->mtd[i]) {
118 +                       dev_err(&pdev->dev, "probing failed\n");
119 +                       return -ENXIO;
120 +               } else {
121 +                       devices_found++;
122 +               }
123 +
124 +               ltq_mtd->mtd[i]->owner = THIS_MODULE;
125 +               ltq_mtd->mtd[i]->dev.parent = &pdev->dev;
126 +
127 +               cfi = ltq_mtd->map[i].fldrv_priv;
128 +               cfi->addr_unlock1 ^= 1;
129 +               cfi->addr_unlock2 ^= 1;
130         }
131  
132 -       ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
133 -                                   GFP_KERNEL);
134 -       if (!ltq_mtd->map)
135 -               return -ENOMEM;
136 -
137 -       if (of_find_property(pdev->dev.of_node, "lantiq,noxip", NULL))
138 -               ltq_mtd->map->phys = NO_XIP;
139 -       else
140 -               ltq_mtd->map->phys = ltq_mtd->res->start;
141 -       ltq_mtd->res->start;
142 -       ltq_mtd->map->size = resource_size(ltq_mtd->res);
143 -       ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
144 -       if (IS_ERR(ltq_mtd->map->virt))
145 -               return PTR_ERR(ltq_mtd->map->virt);
146 -
147 -       ltq_mtd->map->name = ltq_map_name;
148 -       ltq_mtd->map->bankwidth = 2;
149 -       ltq_mtd->map->read = ltq_read16;
150 -       ltq_mtd->map->write = ltq_write16;
151 -       ltq_mtd->map->copy_from = ltq_copy_from;
152 -       ltq_mtd->map->copy_to = ltq_copy_to;
153 -
154 -       ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
155 -
156 -       for (type = rom_probe_types; !ltq_mtd->mtd && *type; type++)
157 -               ltq_mtd->mtd = do_map_probe(*type, ltq_mtd->map);
158 -
159 -       ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
160 -
161 -       if (!ltq_mtd->mtd) {
162 -               dev_err(&pdev->dev, "probing failed\n");
163 -               return -ENXIO;
164 -       }
165 -
166 -       ltq_mtd->mtd->owner = THIS_MODULE;
167 -
168 -       cfi = ltq_mtd->map->fldrv_priv;
169 -       cfi->addr_unlock1 ^= 1;
170 -       cfi->addr_unlock2 ^= 1;
171 +       if (devices_found == 1) {
172 +               ltq_mtd->cmtd = ltq_mtd->mtd[0];
173 +       } else if (devices_found > 1) {
174 +               /*
175 +                * We detected multiple devices. Concatenate them together.
176 +                */
177 +               ltq_mtd->cmtd = mtd_concat_create(ltq_mtd->mtd, devices_found, dev_name(&pdev->dev));
178 +               if (ltq_mtd->cmtd == NULL)
179 +                       err = -ENXIO;
180 +       }
181  
182         ppdata.of_node = pdev->dev.of_node;
183 -       err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types,
184 +       err = mtd_device_parse_register(ltq_mtd->cmtd, ltq_probe_types,
185                                         &ppdata, NULL, 0);
186         if (err) {
187                 dev_err(&pdev->dev, "failed to add partitions\n");
188 -               goto err_destroy;
189 +               goto err_out;
190         }
191  
192         return 0;
193  
194 -err_destroy:
195 -       map_destroy(ltq_mtd->mtd);
196 +err_out:
197 +       ltq_mtd_remove(pdev);
198         return err;
199  }
200  
201 -static int
202 -ltq_mtd_remove(struct platform_device *pdev)
203 -{
204 -       struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
205 -
206 -       if (ltq_mtd && ltq_mtd->mtd) {
207 -               mtd_device_unregister(ltq_mtd->mtd);
208 -               map_destroy(ltq_mtd->mtd);
209 -       }
210 -       return 0;
211 -}
212 -
213  static const struct of_device_id ltq_mtd_match[] = {
214         { .compatible = "lantiq,nor" },
215         {},