Kyungmin Park | 68ee4b1 | 2005-09-09 07:39:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mtd/onenand/generic.c |
| 3 | * |
| 4 | * Copyright (c) 2005 Samsung Electronics |
| 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Overview: |
| 12 | * This is a device driver for the OneNAND flash for generic boards. |
| 13 | */ |
| 14 | |
Kyungmin Park | 68ee4b1 | 2005-09-09 07:39:50 +0100 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
Tim Schmielau | de25968 | 2006-01-08 01:02:05 -0800 | [diff] [blame^] | 17 | #include <linux/slab.h> |
Kyungmin Park | 27f4e08 | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
Kyungmin Park | 68ee4b1 | 2005-09-09 07:39:50 +0100 | [diff] [blame] | 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/mtd/onenand.h> |
| 21 | #include <linux/mtd/partitions.h> |
| 22 | |
| 23 | #include <asm/io.h> |
| 24 | #include <asm/mach/flash.h> |
| 25 | |
| 26 | #define DRIVER_NAME "onenand" |
| 27 | |
| 28 | |
| 29 | #ifdef CONFIG_MTD_PARTITIONS |
| 30 | static const char *part_probes[] = { "cmdlinepart", NULL, }; |
| 31 | #endif |
| 32 | |
| 33 | struct onenand_info { |
| 34 | struct mtd_info mtd; |
| 35 | struct mtd_partition *parts; |
| 36 | struct onenand_chip onenand; |
| 37 | }; |
| 38 | |
| 39 | static int __devinit generic_onenand_probe(struct device *dev) |
| 40 | { |
| 41 | struct onenand_info *info; |
| 42 | struct platform_device *pdev = to_platform_device(dev); |
Kyungmin Park | 27f4e08 | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 43 | struct flash_platform_data *pdata = pdev->dev.platform_data; |
Kyungmin Park | 68ee4b1 | 2005-09-09 07:39:50 +0100 | [diff] [blame] | 44 | struct resource *res = pdev->resource; |
| 45 | unsigned long size = res->end - res->start + 1; |
| 46 | int err; |
| 47 | |
| 48 | info = kmalloc(sizeof(struct onenand_info), GFP_KERNEL); |
| 49 | if (!info) |
| 50 | return -ENOMEM; |
| 51 | |
| 52 | memset(info, 0, sizeof(struct onenand_info)); |
| 53 | |
| 54 | if (!request_mem_region(res->start, size, dev->driver->name)) { |
| 55 | err = -EBUSY; |
| 56 | goto out_free_info; |
| 57 | } |
| 58 | |
| 59 | info->onenand.base = ioremap(res->start, size); |
| 60 | if (!info->onenand.base) { |
| 61 | err = -ENOMEM; |
| 62 | goto out_release_mem_region; |
| 63 | } |
| 64 | |
| 65 | info->onenand.mmcontrol = pdata->mmcontrol; |
| 66 | |
| 67 | info->mtd.name = pdev->dev.bus_id; |
| 68 | info->mtd.priv = &info->onenand; |
| 69 | info->mtd.owner = THIS_MODULE; |
| 70 | |
| 71 | if (onenand_scan(&info->mtd, 1)) { |
| 72 | err = -ENXIO; |
| 73 | goto out_iounmap; |
| 74 | } |
| 75 | |
| 76 | #ifdef CONFIG_MTD_PARTITIONS |
| 77 | err = parse_mtd_partitions(&info->mtd, part_probes, &info->parts, 0); |
| 78 | if (err > 0) |
| 79 | add_mtd_partitions(&info->mtd, info->parts, err); |
| 80 | else if (err < 0 && pdata->parts) |
| 81 | add_mtd_partitions(&info->mtd, pdata->parts, pdata->nr_parts); |
| 82 | else |
| 83 | #endif |
| 84 | err = add_mtd_device(&info->mtd); |
| 85 | |
| 86 | dev_set_drvdata(&pdev->dev, info); |
| 87 | |
| 88 | return 0; |
| 89 | |
| 90 | out_iounmap: |
| 91 | iounmap(info->onenand.base); |
| 92 | out_release_mem_region: |
| 93 | release_mem_region(res->start, size); |
| 94 | out_free_info: |
| 95 | kfree(info); |
| 96 | |
| 97 | return err; |
| 98 | } |
| 99 | |
| 100 | static int __devexit generic_onenand_remove(struct device *dev) |
| 101 | { |
| 102 | struct platform_device *pdev = to_platform_device(dev); |
| 103 | struct onenand_info *info = dev_get_drvdata(&pdev->dev); |
| 104 | struct resource *res = pdev->resource; |
| 105 | unsigned long size = res->end - res->start + 1; |
| 106 | |
| 107 | dev_set_drvdata(&pdev->dev, NULL); |
| 108 | |
| 109 | if (info) { |
| 110 | if (info->parts) |
| 111 | del_mtd_partitions(&info->mtd); |
| 112 | else |
| 113 | del_mtd_device(&info->mtd); |
| 114 | |
| 115 | onenand_release(&info->mtd); |
| 116 | release_mem_region(res->start, size); |
| 117 | iounmap(info->onenand.base); |
| 118 | kfree(info); |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static struct device_driver generic_onenand_driver = { |
| 125 | .name = DRIVER_NAME, |
| 126 | .bus = &platform_bus_type, |
| 127 | .probe = generic_onenand_probe, |
| 128 | .remove = __devexit_p(generic_onenand_remove), |
| 129 | }; |
| 130 | |
| 131 | MODULE_ALIAS(DRIVER_NAME); |
| 132 | |
| 133 | static int __init generic_onenand_init(void) |
| 134 | { |
| 135 | return driver_register(&generic_onenand_driver); |
| 136 | } |
| 137 | |
| 138 | static void __exit generic_onenand_exit(void) |
| 139 | { |
| 140 | driver_unregister(&generic_onenand_driver); |
| 141 | } |
| 142 | |
| 143 | module_init(generic_onenand_init); |
| 144 | module_exit(generic_onenand_exit); |
| 145 | |
| 146 | MODULE_LICENSE("GPL"); |
| 147 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 148 | MODULE_DESCRIPTION("Glue layer for OneNAND flash on generic boards"); |