Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* drivers/mtd/maps/plat-ram.c
0003  *
0004  * (c) 2004-2005 Simtec Electronics
0005  *  http://www.simtec.co.uk/products/SWLINUX/
0006  *  Ben Dooks <ben@simtec.co.uk>
0007  *
0008  * Generic platform device based RAM map
0009 */
0010 
0011 #include <linux/module.h>
0012 #include <linux/types.h>
0013 #include <linux/kernel.h>
0014 #include <linux/string.h>
0015 #include <linux/ioport.h>
0016 #include <linux/device.h>
0017 #include <linux/slab.h>
0018 #include <linux/platform_device.h>
0019 
0020 #include <linux/mtd/mtd.h>
0021 #include <linux/mtd/map.h>
0022 #include <linux/mtd/partitions.h>
0023 #include <linux/mtd/plat-ram.h>
0024 
0025 #include <asm/io.h>
0026 
0027 /* private structure for each mtd platform ram device created */
0028 
0029 struct platram_info {
0030     struct device       *dev;
0031     struct mtd_info     *mtd;
0032     struct map_info      map;
0033     struct platdata_mtd_ram *pdata;
0034 };
0035 
0036 /* to_platram_info()
0037  *
0038  * device private data to struct platram_info conversion
0039 */
0040 
0041 static inline struct platram_info *to_platram_info(struct platform_device *dev)
0042 {
0043     return platform_get_drvdata(dev);
0044 }
0045 
0046 /* platram_setrw
0047  *
0048  * call the platform device's set rw/ro control
0049  *
0050  * to = 0 => read-only
0051  *    = 1 => read-write
0052 */
0053 
0054 static inline void platram_setrw(struct platram_info *info, int to)
0055 {
0056     if (info->pdata == NULL)
0057         return;
0058 
0059     if (info->pdata->set_rw != NULL)
0060         (info->pdata->set_rw)(info->dev, to);
0061 }
0062 
0063 /* platram_remove
0064  *
0065  * called to remove the device from the driver's control
0066 */
0067 
0068 static int platram_remove(struct platform_device *pdev)
0069 {
0070     struct platram_info *info = to_platram_info(pdev);
0071 
0072     dev_dbg(&pdev->dev, "removing device\n");
0073 
0074     if (info == NULL)
0075         return 0;
0076 
0077     if (info->mtd) {
0078         mtd_device_unregister(info->mtd);
0079         map_destroy(info->mtd);
0080     }
0081 
0082     /* ensure ram is left read-only */
0083 
0084     platram_setrw(info, PLATRAM_RO);
0085 
0086     kfree(info);
0087 
0088     return 0;
0089 }
0090 
0091 /* platram_probe
0092  *
0093  * called from device drive system when a device matching our
0094  * driver is found.
0095 */
0096 
0097 static int platram_probe(struct platform_device *pdev)
0098 {
0099     struct platdata_mtd_ram *pdata;
0100     struct platram_info *info;
0101     struct resource *res;
0102     int err = 0;
0103 
0104     dev_dbg(&pdev->dev, "probe entered\n");
0105 
0106     if (dev_get_platdata(&pdev->dev) == NULL) {
0107         dev_err(&pdev->dev, "no platform data supplied\n");
0108         err = -ENOENT;
0109         goto exit_error;
0110     }
0111 
0112     pdata = dev_get_platdata(&pdev->dev);
0113 
0114     info = kzalloc(sizeof(*info), GFP_KERNEL);
0115     if (info == NULL) {
0116         err = -ENOMEM;
0117         goto exit_error;
0118     }
0119 
0120     platform_set_drvdata(pdev, info);
0121 
0122     info->dev = &pdev->dev;
0123     info->pdata = pdata;
0124 
0125     /* get the resource for the memory mapping */
0126     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0127     info->map.virt = devm_ioremap_resource(&pdev->dev, res);
0128     if (IS_ERR(info->map.virt)) {
0129         err = PTR_ERR(info->map.virt);
0130         goto exit_free;
0131     }
0132 
0133     dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
0134         (unsigned long long)res->start);
0135 
0136     /* setup map parameters */
0137 
0138     info->map.phys = res->start;
0139     info->map.size = resource_size(res);
0140     info->map.name = pdata->mapname != NULL ?
0141             (char *)pdata->mapname : (char *)pdev->name;
0142     info->map.bankwidth = pdata->bankwidth;
0143 
0144     dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
0145 
0146     simple_map_init(&info->map);
0147 
0148     dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
0149 
0150     /* probe for the right mtd map driver
0151      * supplied by the platform_data struct */
0152 
0153     if (pdata->map_probes) {
0154         const char * const *map_probes = pdata->map_probes;
0155 
0156         for ( ; !info->mtd && *map_probes; map_probes++)
0157             info->mtd = do_map_probe(*map_probes , &info->map);
0158     }
0159     /* fallback to map_ram */
0160     else
0161         info->mtd = do_map_probe("map_ram", &info->map);
0162 
0163     if (info->mtd == NULL) {
0164         dev_err(&pdev->dev, "failed to probe for map_ram\n");
0165         err = -ENOMEM;
0166         goto exit_free;
0167     }
0168 
0169     info->mtd->dev.parent = &pdev->dev;
0170 
0171     platram_setrw(info, PLATRAM_RW);
0172 
0173     /* check to see if there are any available partitions, or whether
0174      * to add this device whole */
0175 
0176     err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
0177                     pdata->partitions,
0178                     pdata->nr_partitions);
0179     if (err) {
0180         dev_err(&pdev->dev, "failed to register mtd device\n");
0181         goto exit_free;
0182     }
0183 
0184     dev_info(&pdev->dev, "registered mtd device\n");
0185 
0186     if (pdata->nr_partitions) {
0187         /* add the whole device. */
0188         err = mtd_device_register(info->mtd, NULL, 0);
0189         if (err) {
0190             dev_err(&pdev->dev,
0191                 "failed to register the entire device\n");
0192             goto exit_free;
0193         }
0194     }
0195 
0196     return 0;
0197 
0198  exit_free:
0199     platram_remove(pdev);
0200  exit_error:
0201     return err;
0202 }
0203 
0204 /* device driver info */
0205 
0206 /* work with hotplug and coldplug */
0207 MODULE_ALIAS("platform:mtd-ram");
0208 
0209 static struct platform_driver platram_driver = {
0210     .probe      = platram_probe,
0211     .remove     = platram_remove,
0212     .driver     = {
0213         .name   = "mtd-ram",
0214     },
0215 };
0216 
0217 module_platform_driver(platram_driver);
0218 
0219 MODULE_LICENSE("GPL");
0220 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
0221 MODULE_DESCRIPTION("MTD platform RAM map driver");