GNU Linux-libre 4.9.304-gnu1
[releases.git] / drivers / mtd / maps / physmap_of.c
1 /*
2  * Flash mappings described by the OF (or flattened) device tree
3  *
4  * Copyright (C) 2006 MontaVista Software Inc.
5  * Author: Vitaly Wool <vwool@ru.mvista.com>
6  *
7  * Revised to handle newer style flash binding by:
8  *   Copyright (C) 2007 David Gibson, IBM Corporation.
9  *
10  * This program is free software; you can redistribute  it and/or modify it
11  * under  the terms of  the GNU General  Public License as published by the
12  * Free Software Foundation;  either version 2 of the  License, or (at your
13  * option) any later version.
14  */
15
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/device.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/map.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/mtd/concat.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_platform.h>
26 #include <linux/slab.h>
27 #include "physmap_of_versatile.h"
28
29 struct of_flash_list {
30         struct mtd_info *mtd;
31         struct map_info map;
32 };
33
34 struct of_flash {
35         struct mtd_info         *cmtd;
36         int list_size; /* number of elements in of_flash_list */
37         struct of_flash_list    list[0];
38 };
39
40 static int of_flash_remove(struct platform_device *dev)
41 {
42         struct of_flash *info;
43         int i;
44
45         info = dev_get_drvdata(&dev->dev);
46         if (!info)
47                 return 0;
48         dev_set_drvdata(&dev->dev, NULL);
49
50         if (info->cmtd) {
51                 mtd_device_unregister(info->cmtd);
52                 if (info->cmtd != info->list[0].mtd)
53                         mtd_concat_destroy(info->cmtd);
54         }
55
56         for (i = 0; i < info->list_size; i++)
57                 if (info->list[i].mtd)
58                         map_destroy(info->list[i].mtd);
59
60         return 0;
61 }
62
63 static const char * const rom_probe_types[] = {
64         "cfi_probe", "jedec_probe", "map_rom" };
65
66 /* Helper function to handle probing of the obsolete "direct-mapped"
67  * compatible binding, which has an extra "probe-type" property
68  * describing the type of flash probe necessary. */
69 static struct mtd_info *obsolete_probe(struct platform_device *dev,
70                                        struct map_info *map)
71 {
72         struct device_node *dp = dev->dev.of_node;
73         const char *of_probe;
74         struct mtd_info *mtd;
75         int i;
76
77         dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
78                  "flash binding\n");
79
80         of_probe = of_get_property(dp, "probe-type", NULL);
81         if (!of_probe) {
82                 for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
83                         mtd = do_map_probe(rom_probe_types[i], map);
84                         if (mtd)
85                                 return mtd;
86                 }
87                 return NULL;
88         } else if (strcmp(of_probe, "CFI") == 0) {
89                 return do_map_probe("cfi_probe", map);
90         } else if (strcmp(of_probe, "JEDEC") == 0) {
91                 return do_map_probe("jedec_probe", map);
92         } else {
93                 if (strcmp(of_probe, "ROM") != 0)
94                         dev_warn(&dev->dev, "obsolete_probe: don't know probe "
95                                  "type '%s', mapping as rom\n", of_probe);
96                 return do_map_probe("map_rom", map);
97         }
98 }
99
100 /* When partitions are set we look for a linux,part-probe property which
101    specifies the list of partition probers to use. If none is given then the
102    default is use. These take precedence over other device tree
103    information. */
104 static const char * const part_probe_types_def[] = {
105         "cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL };
106
107 static const char * const *of_get_probes(struct device_node *dp)
108 {
109         const char *cp;
110         int cplen;
111         unsigned int l;
112         unsigned int count;
113         const char **res;
114
115         cp = of_get_property(dp, "linux,part-probe", &cplen);
116         if (cp == NULL)
117                 return part_probe_types_def;
118
119         count = 0;
120         for (l = 0; l != cplen; l++)
121                 if (cp[l] == 0)
122                         count++;
123
124         res = kzalloc((count + 1)*sizeof(*res), GFP_KERNEL);
125         if (!res)
126                 return NULL;
127         count = 0;
128         while (cplen > 0) {
129                 res[count] = cp;
130                 l = strlen(cp) + 1;
131                 cp += l;
132                 cplen -= l;
133                 count++;
134         }
135         return res;
136 }
137
138 static void of_free_probes(const char * const *probes)
139 {
140         if (probes != part_probe_types_def)
141                 kfree(probes);
142 }
143
144 static const struct of_device_id of_flash_match[];
145 static int of_flash_probe(struct platform_device *dev)
146 {
147         const char * const *part_probe_types;
148         const struct of_device_id *match;
149         struct device_node *dp = dev->dev.of_node;
150         struct resource res;
151         struct of_flash *info;
152         const char *probe_type;
153         const __be32 *width;
154         int err;
155         int i;
156         int count;
157         const __be32 *p;
158         int reg_tuple_size;
159         struct mtd_info **mtd_list = NULL;
160         resource_size_t res_size;
161         bool map_indirect;
162         const char *mtd_name = NULL;
163
164         match = of_match_device(of_flash_match, &dev->dev);
165         if (!match)
166                 return -EINVAL;
167         probe_type = match->data;
168
169         reg_tuple_size = (of_n_addr_cells(dp) + of_n_size_cells(dp)) * sizeof(u32);
170
171         of_property_read_string(dp, "linux,mtd-name", &mtd_name);
172
173         /*
174          * Get number of "reg" tuples. Scan for MTD devices on area's
175          * described by each "reg" region. This makes it possible (including
176          * the concat support) to support the Intel P30 48F4400 chips which
177          * consists internally of 2 non-identical NOR chips on one die.
178          */
179         p = of_get_property(dp, "reg", &count);
180         if (!p || count % reg_tuple_size != 0) {
181                 dev_err(&dev->dev, "Malformed reg property on %s\n",
182                                 dev->dev.of_node->full_name);
183                 err = -EINVAL;
184                 goto err_flash_remove;
185         }
186         count /= reg_tuple_size;
187
188         map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access");
189
190         err = -ENOMEM;
191         info = devm_kzalloc(&dev->dev,
192                             sizeof(struct of_flash) +
193                             sizeof(struct of_flash_list) * count, GFP_KERNEL);
194         if (!info)
195                 goto err_flash_remove;
196
197         dev_set_drvdata(&dev->dev, info);
198
199         mtd_list = kzalloc(sizeof(*mtd_list) * count, GFP_KERNEL);
200         if (!mtd_list)
201                 goto err_flash_remove;
202
203         for (i = 0; i < count; i++) {
204                 err = -ENXIO;
205                 if (of_address_to_resource(dp, i, &res)) {
206                         /*
207                          * Continue with next register tuple if this
208                          * one is not mappable
209                          */
210                         continue;
211                 }
212
213                 dev_dbg(&dev->dev, "of_flash device: %pR\n", &res);
214
215                 err = -EBUSY;
216                 res_size = resource_size(&res);
217                 info->list[i].map.virt = devm_ioremap_resource(&dev->dev, &res);
218                 if (IS_ERR(info->list[i].map.virt)) {
219                         err = PTR_ERR(info->list[i].map.virt);
220                         goto err_out;
221                 }
222
223                 err = -ENXIO;
224                 width = of_get_property(dp, "bank-width", NULL);
225                 if (!width) {
226                         dev_err(&dev->dev, "Can't get bank width from device"
227                                 " tree\n");
228                         goto err_out;
229                 }
230
231                 info->list[i].map.name = mtd_name ?: dev_name(&dev->dev);
232                 info->list[i].map.phys = res.start;
233                 info->list[i].map.size = res_size;
234                 info->list[i].map.bankwidth = be32_to_cpup(width);
235                 info->list[i].map.device_node = dp;
236                 err = of_flash_probe_versatile(dev, dp, &info->list[i].map);
237                 if (err) {
238                         dev_err(&dev->dev, "Can't probe Versatile VPP\n");
239                         return err;
240                 }
241
242                 simple_map_init(&info->list[i].map);
243
244                 /*
245                  * On some platforms (e.g. MPC5200) a direct 1:1 mapping
246                  * may cause problems with JFFS2 usage, as the local bus (LPB)
247                  * doesn't support unaligned accesses as implemented in the
248                  * JFFS2 code via memcpy(). By setting NO_XIP, the
249                  * flash will not be exposed directly to the MTD users
250                  * (e.g. JFFS2) any more.
251                  */
252                 if (map_indirect)
253                         info->list[i].map.phys = NO_XIP;
254
255                 if (probe_type) {
256                         info->list[i].mtd = do_map_probe(probe_type,
257                                                          &info->list[i].map);
258                 } else {
259                         info->list[i].mtd = obsolete_probe(dev,
260                                                            &info->list[i].map);
261                 }
262
263                 /* Fall back to mapping region as ROM */
264                 if (!info->list[i].mtd) {
265                         dev_warn(&dev->dev,
266                                 "do_map_probe() failed for type %s\n",
267                                  probe_type);
268
269                         info->list[i].mtd = do_map_probe("map_rom",
270                                                          &info->list[i].map);
271                 }
272                 mtd_list[i] = info->list[i].mtd;
273
274                 err = -ENXIO;
275                 if (!info->list[i].mtd) {
276                         dev_err(&dev->dev, "do_map_probe() failed\n");
277                         goto err_out;
278                 } else {
279                         info->list_size++;
280                 }
281                 info->list[i].mtd->dev.parent = &dev->dev;
282         }
283
284         err = 0;
285         info->cmtd = NULL;
286         if (info->list_size == 1) {
287                 info->cmtd = info->list[0].mtd;
288         } else if (info->list_size > 1) {
289                 /*
290                  * We detected multiple devices. Concatenate them together.
291                  */
292                 info->cmtd = mtd_concat_create(mtd_list, info->list_size,
293                                                dev_name(&dev->dev));
294         }
295         if (info->cmtd == NULL)
296                 err = -ENXIO;
297
298         if (err)
299                 goto err_out;
300
301         info->cmtd->dev.parent = &dev->dev;
302         mtd_set_of_node(info->cmtd, dp);
303         part_probe_types = of_get_probes(dp);
304         if (!part_probe_types) {
305                 err = -ENOMEM;
306                 goto err_out;
307         }
308         mtd_device_parse_register(info->cmtd, part_probe_types, NULL,
309                         NULL, 0);
310         of_free_probes(part_probe_types);
311
312         kfree(mtd_list);
313
314         return 0;
315
316 err_out:
317         kfree(mtd_list);
318 err_flash_remove:
319         of_flash_remove(dev);
320
321         return err;
322 }
323
324 static const struct of_device_id of_flash_match[] = {
325         {
326                 .compatible     = "cfi-flash",
327                 .data           = (void *)"cfi_probe",
328         },
329         {
330                 /* FIXME: JEDEC chips can't be safely and reliably
331                  * probed, although the mtd code gets it right in
332                  * practice most of the time.  We should use the
333                  * vendor and device ids specified by the binding to
334                  * bypass the heuristic probe code, but the mtd layer
335                  * provides, at present, no interface for doing so
336                  * :(. */
337                 .compatible     = "jedec-flash",
338                 .data           = (void *)"jedec_probe",
339         },
340         {
341                 .compatible     = "mtd-ram",
342                 .data           = (void *)"map_ram",
343         },
344         {
345                 .compatible     = "mtd-rom",
346                 .data           = (void *)"map_rom",
347         },
348         {
349                 .type           = "rom",
350                 .compatible     = "direct-mapped"
351         },
352         { },
353 };
354 MODULE_DEVICE_TABLE(of, of_flash_match);
355
356 static struct platform_driver of_flash_driver = {
357         .driver = {
358                 .name = "of-flash",
359                 .of_match_table = of_flash_match,
360         },
361         .probe          = of_flash_probe,
362         .remove         = of_flash_remove,
363 };
364
365 module_platform_driver(of_flash_driver);
366
367 MODULE_LICENSE("GPL");
368 MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
369 MODULE_DESCRIPTION("Device tree based MTD map driver");