Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * rbtx4939-flash (based on physmap.c) |
| 3 | * |
| 4 | * This is a simplified physmap driver with map_init callback function. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/mtd/map.h> |
| 21 | #include <linux/mtd/partitions.h> |
| 22 | #include <asm/txx9/rbtx4939.h> |
| 23 | |
| 24 | struct rbtx4939_flash_info { |
| 25 | struct mtd_info *mtd; |
| 26 | struct map_info map; |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | static int rbtx4939_flash_remove(struct platform_device *dev) |
| 30 | { |
| 31 | struct rbtx4939_flash_info *info; |
| 32 | |
| 33 | info = platform_get_drvdata(dev); |
| 34 | if (!info) |
| 35 | return 0; |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 36 | |
| 37 | if (info->mtd) { |
Jamie Iles | 16b0eb1 | 2011-05-23 10:23:08 +0100 | [diff] [blame] | 38 | mtd_device_unregister(info->mtd); |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 39 | map_destroy(info->mtd); |
| 40 | } |
| 41 | return 0; |
| 42 | } |
| 43 | |
Artem Bityutskiy | 0984c89 | 2013-03-12 10:46:37 +0200 | [diff] [blame] | 44 | static const char * const rom_probe_types[] = { |
| 45 | "cfi_probe", "jedec_probe", NULL }; |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 46 | |
| 47 | static int rbtx4939_flash_probe(struct platform_device *dev) |
| 48 | { |
| 49 | struct rbtx4939_flash_data *pdata; |
| 50 | struct rbtx4939_flash_info *info; |
| 51 | struct resource *res; |
Artem Bityutskiy | 0984c89 | 2013-03-12 10:46:37 +0200 | [diff] [blame] | 52 | const char * const *probe_type; |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 53 | int err = 0; |
| 54 | unsigned long size; |
| 55 | |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 56 | pdata = dev_get_platdata(&dev->dev); |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 57 | if (!pdata) |
| 58 | return -ENODEV; |
| 59 | |
| 60 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 61 | if (!res) |
| 62 | return -ENODEV; |
| 63 | info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info), |
| 64 | GFP_KERNEL); |
| 65 | if (!info) |
| 66 | return -ENOMEM; |
| 67 | |
| 68 | platform_set_drvdata(dev, info); |
| 69 | |
| 70 | size = resource_size(res); |
| 71 | pr_notice("rbtx4939 platform flash device: %pR\n", res); |
| 72 | |
| 73 | if (!devm_request_mem_region(&dev->dev, res->start, size, |
| 74 | dev_name(&dev->dev))) |
| 75 | return -EBUSY; |
| 76 | |
| 77 | info->map.name = dev_name(&dev->dev); |
| 78 | info->map.phys = res->start; |
| 79 | info->map.size = size; |
| 80 | info->map.bankwidth = pdata->width; |
| 81 | |
| 82 | info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size); |
| 83 | if (!info->map.virt) |
| 84 | return -EBUSY; |
| 85 | |
| 86 | if (pdata->map_init) |
| 87 | (*pdata->map_init)(&info->map); |
| 88 | else |
| 89 | simple_map_init(&info->map); |
| 90 | |
| 91 | probe_type = rom_probe_types; |
| 92 | for (; !info->mtd && *probe_type; probe_type++) |
| 93 | info->mtd = do_map_probe(*probe_type, &info->map); |
| 94 | if (!info->mtd) { |
| 95 | dev_err(&dev->dev, "map_probe failed\n"); |
| 96 | err = -ENXIO; |
| 97 | goto err_out; |
| 98 | } |
| 99 | info->mtd->owner = THIS_MODULE; |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 100 | err = mtd_device_parse_register(info->mtd, NULL, NULL, pdata->parts, |
| 101 | pdata->nr_parts); |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 102 | |
Dmitry Eremin-Solenikov | c77d809 | 2011-06-02 18:00:04 +0400 | [diff] [blame] | 103 | if (err) |
| 104 | goto err_out; |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 105 | return 0; |
| 106 | |
| 107 | err_out: |
| 108 | rbtx4939_flash_remove(dev); |
| 109 | return err; |
| 110 | } |
| 111 | |
| 112 | #ifdef CONFIG_PM |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 113 | static void rbtx4939_flash_shutdown(struct platform_device *dev) |
| 114 | { |
| 115 | struct rbtx4939_flash_info *info = platform_get_drvdata(dev); |
| 116 | |
Artem Bityutskiy | 079c985 | 2011-12-30 17:15:59 +0200 | [diff] [blame] | 117 | if (mtd_suspend(info->mtd) == 0) |
| 118 | mtd_resume(info->mtd); |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 119 | } |
| 120 | #else |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 121 | #define rbtx4939_flash_shutdown NULL |
| 122 | #endif |
| 123 | |
| 124 | static struct platform_driver rbtx4939_flash_driver = { |
| 125 | .probe = rbtx4939_flash_probe, |
| 126 | .remove = rbtx4939_flash_remove, |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 127 | .shutdown = rbtx4939_flash_shutdown, |
| 128 | .driver = { |
| 129 | .name = "rbtx4939-flash", |
| 130 | .owner = THIS_MODULE, |
| 131 | }, |
| 132 | }; |
| 133 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 134 | module_platform_driver(rbtx4939_flash_driver); |
Atsushi Nemoto | 610f75e | 2009-03-04 12:01:34 -0800 | [diff] [blame] | 135 | |
| 136 | MODULE_LICENSE("GPL"); |
| 137 | MODULE_DESCRIPTION("RBTX4939 MTD map driver"); |
| 138 | MODULE_ALIAS("platform:rbtx4939-flash"); |