Skip to content

Commit 7090d2f

Browse files
vwaxmiquelraynal
authored andcommitted
mtd: phram: Allow probing via reserved-memory
Allow phram to be probed from the devicetree. It expects to be in a reserved-memory node as documented by the bindings. This allows things like partitioning to be specified via the devicetree. Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com> Acked-by: Rob Herring <robh@kernel.org> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Link: https://lore.kernel.org/linux-mtd/20220412135302.1682890-4-vincent.whitchurch@axis.com
1 parent 4e96945 commit 7090d2f

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

drivers/mtd/devices/phram.c

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include <linux/slab.h>
2828
#include <linux/mtd/mtd.h>
2929
#include <asm/div64.h>
30+
#include <linux/platform_device.h>
31+
#include <linux/of_address.h>
32+
#include <linux/of.h>
3033

3134
struct phram_mtd_list {
3235
struct mtd_info mtd;
@@ -89,8 +92,10 @@ static void unregister_devices(void)
8992
}
9093
}
9194

92-
static int register_device(char *name, phys_addr_t start, size_t len, uint32_t erasesize)
95+
static int register_device(struct platform_device *pdev, const char *name,
96+
phys_addr_t start, size_t len, uint32_t erasesize)
9397
{
98+
struct device_node *np = pdev ? pdev->dev.of_node : NULL;
9499
struct phram_mtd_list *new;
95100
int ret = -ENOMEM;
96101

@@ -119,13 +124,19 @@ static int register_device(char *name, phys_addr_t start, size_t len, uint32_t e
119124
new->mtd.erasesize = erasesize;
120125
new->mtd.writesize = 1;
121126

127+
mtd_set_of_node(&new->mtd, np);
128+
122129
ret = -EAGAIN;
123130
if (mtd_device_register(&new->mtd, NULL, 0)) {
124131
pr_err("Failed to register new device\n");
125132
goto out2;
126133
}
127134

128-
list_add_tail(&new->list, &phram_list);
135+
if (pdev)
136+
platform_set_drvdata(pdev, new);
137+
else
138+
list_add_tail(&new->list, &phram_list);
139+
129140
return 0;
130141

131142
out2:
@@ -278,7 +289,7 @@ static int phram_setup(const char *val)
278289
goto error;
279290
}
280291

281-
ret = register_device(name, start, len, (uint32_t)erasesize);
292+
ret = register_device(NULL, name, start, len, (uint32_t)erasesize);
282293
if (ret)
283294
goto error;
284295

@@ -325,23 +336,71 @@ static int phram_param_call(const char *val, const struct kernel_param *kp)
325336
module_param_call(phram, phram_param_call, NULL, NULL, 0200);
326337
MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>[,<erasesize>]\"");
327338

339+
#ifdef CONFIG_OF
340+
static const struct of_device_id phram_of_match[] = {
341+
{ .compatible = "phram" },
342+
{}
343+
};
344+
MODULE_DEVICE_TABLE(of, phram_of_match);
345+
#endif
346+
347+
static int phram_probe(struct platform_device *pdev)
348+
{
349+
struct resource *res;
350+
351+
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
352+
if (!res)
353+
return -ENOMEM;
354+
355+
/* mtd_set_of_node() reads name from "label" */
356+
return register_device(pdev, NULL, res->start, resource_size(res),
357+
PAGE_SIZE);
358+
}
359+
360+
static int phram_remove(struct platform_device *pdev)
361+
{
362+
struct phram_mtd_list *phram = platform_get_drvdata(pdev);
363+
364+
mtd_device_unregister(&phram->mtd);
365+
iounmap(phram->mtd.priv);
366+
kfree(phram);
367+
368+
return 0;
369+
}
370+
371+
static struct platform_driver phram_driver = {
372+
.probe = phram_probe,
373+
.remove = phram_remove,
374+
.driver = {
375+
.name = "phram",
376+
.of_match_table = of_match_ptr(phram_of_match),
377+
},
378+
};
328379

329380
static int __init init_phram(void)
330381
{
331-
int ret = 0;
382+
int ret;
383+
384+
ret = platform_driver_register(&phram_driver);
385+
if (ret)
386+
return ret;
332387

333388
#ifndef MODULE
334389
if (phram_paramline[0])
335390
ret = phram_setup(phram_paramline);
336391
phram_init_called = 1;
337392
#endif
338393

394+
if (ret)
395+
platform_driver_unregister(&phram_driver);
396+
339397
return ret;
340398
}
341399

342400
static void __exit cleanup_phram(void)
343401
{
344402
unregister_devices();
403+
platform_driver_unregister(&phram_driver);
345404
}
346405

347406
module_init(init_phram);

drivers/of/platform.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ EXPORT_SYMBOL_GPL(of_platform_default_populate);
509509

510510
#ifndef CONFIG_PPC
511511
static const struct of_device_id reserved_mem_matches[] = {
512+
{ .compatible = "phram" },
512513
{ .compatible = "qcom,rmtfs-mem" },
513514
{ .compatible = "qcom,cmd-db" },
514515
{ .compatible = "qcom,smem" },

0 commit comments

Comments
 (0)