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