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