xref: /openbmc/linux/drivers/mtd/nand/onenand/generic.c (revision d9fd5a71)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (c) 2005 Samsung Electronics
4  *  Kyungmin Park <kyungmin.park@samsung.com>
5  *
6  *  Overview:
7  *   This is a device driver for the OneNAND flash for generic boards.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/platform_device.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/onenand.h>
15 #include <linux/mtd/partitions.h>
16 #include <linux/io.h>
17 
18 /*
19  * Note: Driver name and platform data format have been updated!
20  *
21  * This version of the driver is named "onenand-flash" and takes struct
22  * onenand_platform_data as platform data. The old ARM-specific version
23  * with the name "onenand" used to take struct flash_platform_data.
24  */
25 #define DRIVER_NAME	"onenand-flash"
26 
27 struct onenand_info {
28 	struct mtd_info		mtd;
29 	struct onenand_chip	onenand;
30 };
31 
32 static int generic_onenand_probe(struct platform_device *pdev)
33 {
34 	struct onenand_info *info;
35 	struct onenand_platform_data *pdata = dev_get_platdata(&pdev->dev);
36 	struct resource *res = pdev->resource;
37 	unsigned long size = resource_size(res);
38 	int err;
39 
40 	info = kzalloc(sizeof(struct onenand_info), GFP_KERNEL);
41 	if (!info)
42 		return -ENOMEM;
43 
44 	if (!request_mem_region(res->start, size, dev_name(&pdev->dev))) {
45 		err = -EBUSY;
46 		goto out_free_info;
47 	}
48 
49 	info->onenand.base = ioremap(res->start, size);
50 	if (!info->onenand.base) {
51 		err = -ENOMEM;
52 		goto out_release_mem_region;
53 	}
54 
55 	info->onenand.mmcontrol = pdata ? pdata->mmcontrol : NULL;
56 	info->onenand.irq = platform_get_irq(pdev, 0);
57 
58 	info->mtd.dev.parent = &pdev->dev;
59 	info->mtd.priv = &info->onenand;
60 
61 	if (onenand_scan(&info->mtd, 1)) {
62 		err = -ENXIO;
63 		goto out_iounmap;
64 	}
65 
66 	err = mtd_device_register(&info->mtd, pdata ? pdata->parts : NULL,
67 				  pdata ? pdata->nr_parts : 0);
68 
69 	platform_set_drvdata(pdev, info);
70 
71 	return 0;
72 
73 out_iounmap:
74 	iounmap(info->onenand.base);
75 out_release_mem_region:
76 	release_mem_region(res->start, size);
77 out_free_info:
78 	kfree(info);
79 
80 	return err;
81 }
82 
83 static int generic_onenand_remove(struct platform_device *pdev)
84 {
85 	struct onenand_info *info = platform_get_drvdata(pdev);
86 	struct resource *res = pdev->resource;
87 	unsigned long size = resource_size(res);
88 
89 	if (info) {
90 		onenand_release(&info->mtd);
91 		release_mem_region(res->start, size);
92 		iounmap(info->onenand.base);
93 		kfree(info);
94 	}
95 
96 	return 0;
97 }
98 
99 static struct platform_driver generic_onenand_driver = {
100 	.driver = {
101 		.name		= DRIVER_NAME,
102 	},
103 	.probe		= generic_onenand_probe,
104 	.remove		= generic_onenand_remove,
105 };
106 
107 module_platform_driver(generic_onenand_driver);
108 
109 MODULE_LICENSE("GPL");
110 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
111 MODULE_DESCRIPTION("Glue layer for OneNAND flash on generic boards");
112 MODULE_ALIAS("platform:" DRIVER_NAME);
113