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