Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/
0004 // Author: Vignesh Raghavendra <vigneshr@ti.com>
0005 
0006 #include <linux/err.h>
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/mtd/hyperbus.h>
0010 #include <linux/mtd/map.h>
0011 #include <linux/mtd/mtd.h>
0012 #include <linux/of.h>
0013 #include <linux/types.h>
0014 
0015 static struct hyperbus_device *map_to_hbdev(struct map_info *map)
0016 {
0017     return container_of(map, struct hyperbus_device, map);
0018 }
0019 
0020 static map_word hyperbus_read16(struct map_info *map, unsigned long addr)
0021 {
0022     struct hyperbus_device *hbdev = map_to_hbdev(map);
0023     struct hyperbus_ctlr *ctlr = hbdev->ctlr;
0024     map_word read_data;
0025 
0026     read_data.x[0] = ctlr->ops->read16(hbdev, addr);
0027 
0028     return read_data;
0029 }
0030 
0031 static void hyperbus_write16(struct map_info *map, map_word d,
0032                  unsigned long addr)
0033 {
0034     struct hyperbus_device *hbdev = map_to_hbdev(map);
0035     struct hyperbus_ctlr *ctlr = hbdev->ctlr;
0036 
0037     ctlr->ops->write16(hbdev, addr, d.x[0]);
0038 }
0039 
0040 static void hyperbus_copy_from(struct map_info *map, void *to,
0041                    unsigned long from, ssize_t len)
0042 {
0043     struct hyperbus_device *hbdev = map_to_hbdev(map);
0044     struct hyperbus_ctlr *ctlr = hbdev->ctlr;
0045 
0046     ctlr->ops->copy_from(hbdev, to, from, len);
0047 }
0048 
0049 static void hyperbus_copy_to(struct map_info *map, unsigned long to,
0050                  const void *from, ssize_t len)
0051 {
0052     struct hyperbus_device *hbdev = map_to_hbdev(map);
0053     struct hyperbus_ctlr *ctlr = hbdev->ctlr;
0054 
0055     ctlr->ops->copy_to(hbdev, to, from, len);
0056 }
0057 
0058 int hyperbus_register_device(struct hyperbus_device *hbdev)
0059 {
0060     const struct hyperbus_ops *ops;
0061     struct hyperbus_ctlr *ctlr;
0062     struct device_node *np;
0063     struct map_info *map;
0064     struct device *dev;
0065     int ret;
0066 
0067     if (!hbdev || !hbdev->np || !hbdev->ctlr || !hbdev->ctlr->dev) {
0068         pr_err("hyperbus: please fill all the necessary fields!\n");
0069         return -EINVAL;
0070     }
0071 
0072     np = hbdev->np;
0073     ctlr = hbdev->ctlr;
0074     if (!of_device_is_compatible(np, "cypress,hyperflash")) {
0075         dev_err(ctlr->dev, "\"cypress,hyperflash\" compatible missing\n");
0076         return -ENODEV;
0077     }
0078 
0079     hbdev->memtype = HYPERFLASH;
0080 
0081     dev = ctlr->dev;
0082     map = &hbdev->map;
0083     map->name = dev_name(dev);
0084     map->bankwidth = 2;
0085     map->device_node = np;
0086 
0087     simple_map_init(map);
0088     ops = ctlr->ops;
0089     if (ops) {
0090         if (ops->read16)
0091             map->read = hyperbus_read16;
0092         if (ops->write16)
0093             map->write = hyperbus_write16;
0094         if (ops->copy_to)
0095             map->copy_to = hyperbus_copy_to;
0096         if (ops->copy_from)
0097             map->copy_from = hyperbus_copy_from;
0098 
0099         if (ops->calibrate && !ctlr->calibrated) {
0100             ret = ops->calibrate(hbdev);
0101             if (!ret) {
0102                 dev_err(dev, "Calibration failed\n");
0103                 return -ENODEV;
0104             }
0105             ctlr->calibrated = true;
0106         }
0107     }
0108 
0109     hbdev->mtd = do_map_probe("cfi_probe", map);
0110     if (!hbdev->mtd) {
0111         dev_err(dev, "probing of hyperbus device failed\n");
0112         return -ENODEV;
0113     }
0114 
0115     hbdev->mtd->dev.parent = dev;
0116     mtd_set_of_node(hbdev->mtd, np);
0117 
0118     ret = mtd_device_register(hbdev->mtd, NULL, 0);
0119     if (ret) {
0120         dev_err(dev, "failed to register mtd device\n");
0121         map_destroy(hbdev->mtd);
0122         return ret;
0123     }
0124 
0125     return 0;
0126 }
0127 EXPORT_SYMBOL_GPL(hyperbus_register_device);
0128 
0129 void hyperbus_unregister_device(struct hyperbus_device *hbdev)
0130 {
0131     if (hbdev && hbdev->mtd) {
0132         WARN_ON(mtd_device_unregister(hbdev->mtd));
0133         map_destroy(hbdev->mtd);
0134     }
0135 }
0136 EXPORT_SYMBOL_GPL(hyperbus_unregister_device);
0137 
0138 MODULE_DESCRIPTION("HyperBus Framework");
0139 MODULE_LICENSE("GPL v2");
0140 MODULE_AUTHOR("Vignesh Raghavendra <vigneshr@ti.com>");