Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  *  Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
0005  *  Copyright (C) 2010 John Crispin <john@phrozen.org>
0006  */
0007 
0008 #include <linux/err.h>
0009 #include <linux/module.h>
0010 #include <linux/types.h>
0011 #include <linux/kernel.h>
0012 #include <linux/io.h>
0013 #include <linux/slab.h>
0014 #include <linux/mtd/mtd.h>
0015 #include <linux/mtd/map.h>
0016 #include <linux/mtd/partitions.h>
0017 #include <linux/mtd/cfi.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/mtd/physmap.h>
0020 #include <linux/of.h>
0021 
0022 #include <lantiq_soc.h>
0023 
0024 /*
0025  * The NOR flash is connected to the same external bus unit (EBU) as PCI.
0026  * To make PCI work we need to enable the endianness swapping for the address
0027  * written to the EBU. This endianness swapping works for PCI correctly but
0028  * fails for attached NOR devices. To workaround this we need to use a complex
0029  * map. The workaround involves swapping all addresses whilst probing the chip.
0030  * Once probing is complete we stop swapping the addresses but swizzle the
0031  * unlock addresses to ensure that access to the NOR device works correctly.
0032  */
0033 
0034 enum {
0035     LTQ_NOR_PROBING,
0036     LTQ_NOR_NORMAL
0037 };
0038 
0039 struct ltq_mtd {
0040     struct resource *res;
0041     struct mtd_info *mtd;
0042     struct map_info *map;
0043 };
0044 
0045 static const char ltq_map_name[] = "ltq_nor";
0046 
0047 static map_word
0048 ltq_read16(struct map_info *map, unsigned long adr)
0049 {
0050     unsigned long flags;
0051     map_word temp;
0052 
0053     if (map->map_priv_1 == LTQ_NOR_PROBING)
0054         adr ^= 2;
0055     spin_lock_irqsave(&ebu_lock, flags);
0056     temp.x[0] = *(u16 *)(map->virt + adr);
0057     spin_unlock_irqrestore(&ebu_lock, flags);
0058     return temp;
0059 }
0060 
0061 static void
0062 ltq_write16(struct map_info *map, map_word d, unsigned long adr)
0063 {
0064     unsigned long flags;
0065 
0066     if (map->map_priv_1 == LTQ_NOR_PROBING)
0067         adr ^= 2;
0068     spin_lock_irqsave(&ebu_lock, flags);
0069     *(u16 *)(map->virt + adr) = d.x[0];
0070     spin_unlock_irqrestore(&ebu_lock, flags);
0071 }
0072 
0073 /*
0074  * The following 2 functions copy data between iomem and a cached memory
0075  * section. As memcpy() makes use of pre-fetching we cannot use it here.
0076  * The normal alternative of using memcpy_{to,from}io also makes use of
0077  * memcpy() on MIPS so it is not applicable either. We are therefore stuck
0078  * with having to use our own loop.
0079  */
0080 static void
0081 ltq_copy_from(struct map_info *map, void *to,
0082     unsigned long from, ssize_t len)
0083 {
0084     unsigned char *f = (unsigned char *)map->virt + from;
0085     unsigned char *t = (unsigned char *)to;
0086     unsigned long flags;
0087 
0088     spin_lock_irqsave(&ebu_lock, flags);
0089     while (len--)
0090         *t++ = *f++;
0091     spin_unlock_irqrestore(&ebu_lock, flags);
0092 }
0093 
0094 static void
0095 ltq_copy_to(struct map_info *map, unsigned long to,
0096     const void *from, ssize_t len)
0097 {
0098     unsigned char *f = (unsigned char *)from;
0099     unsigned char *t = (unsigned char *)map->virt + to;
0100     unsigned long flags;
0101 
0102     spin_lock_irqsave(&ebu_lock, flags);
0103     while (len--)
0104         *t++ = *f++;
0105     spin_unlock_irqrestore(&ebu_lock, flags);
0106 }
0107 
0108 static int
0109 ltq_mtd_probe(struct platform_device *pdev)
0110 {
0111     struct ltq_mtd *ltq_mtd;
0112     struct cfi_private *cfi;
0113     int err;
0114 
0115     ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL);
0116     if (!ltq_mtd)
0117         return -ENOMEM;
0118 
0119     platform_set_drvdata(pdev, ltq_mtd);
0120 
0121     ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0122     if (!ltq_mtd->res) {
0123         dev_err(&pdev->dev, "failed to get memory resource\n");
0124         return -ENOENT;
0125     }
0126 
0127     ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
0128                     GFP_KERNEL);
0129     if (!ltq_mtd->map)
0130         return -ENOMEM;
0131 
0132     ltq_mtd->map->phys = ltq_mtd->res->start;
0133     ltq_mtd->map->size = resource_size(ltq_mtd->res);
0134     ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
0135     if (IS_ERR(ltq_mtd->map->virt))
0136         return PTR_ERR(ltq_mtd->map->virt);
0137 
0138     ltq_mtd->map->name = ltq_map_name;
0139     ltq_mtd->map->bankwidth = 2;
0140     ltq_mtd->map->read = ltq_read16;
0141     ltq_mtd->map->write = ltq_write16;
0142     ltq_mtd->map->copy_from = ltq_copy_from;
0143     ltq_mtd->map->copy_to = ltq_copy_to;
0144 
0145     ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
0146     ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
0147     ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
0148 
0149     if (!ltq_mtd->mtd) {
0150         dev_err(&pdev->dev, "probing failed\n");
0151         return -ENXIO;
0152     }
0153 
0154     ltq_mtd->mtd->dev.parent = &pdev->dev;
0155     mtd_set_of_node(ltq_mtd->mtd, pdev->dev.of_node);
0156 
0157     cfi = ltq_mtd->map->fldrv_priv;
0158     cfi->addr_unlock1 ^= 1;
0159     cfi->addr_unlock2 ^= 1;
0160 
0161     err = mtd_device_register(ltq_mtd->mtd, NULL, 0);
0162     if (err) {
0163         dev_err(&pdev->dev, "failed to add partitions\n");
0164         goto err_destroy;
0165     }
0166 
0167     return 0;
0168 
0169 err_destroy:
0170     map_destroy(ltq_mtd->mtd);
0171     return err;
0172 }
0173 
0174 static int
0175 ltq_mtd_remove(struct platform_device *pdev)
0176 {
0177     struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
0178 
0179     if (ltq_mtd && ltq_mtd->mtd) {
0180         mtd_device_unregister(ltq_mtd->mtd);
0181         map_destroy(ltq_mtd->mtd);
0182     }
0183     return 0;
0184 }
0185 
0186 static const struct of_device_id ltq_mtd_match[] = {
0187     { .compatible = "lantiq,nor" },
0188     {},
0189 };
0190 MODULE_DEVICE_TABLE(of, ltq_mtd_match);
0191 
0192 static struct platform_driver ltq_mtd_driver = {
0193     .probe = ltq_mtd_probe,
0194     .remove = ltq_mtd_remove,
0195     .driver = {
0196         .name = "ltq-nor",
0197         .of_match_table = ltq_mtd_match,
0198     },
0199 };
0200 
0201 module_platform_driver(ltq_mtd_driver);
0202 
0203 MODULE_LICENSE("GPL");
0204 MODULE_AUTHOR("John Crispin <john@phrozen.org>");
0205 MODULE_DESCRIPTION("Lantiq SoC NOR");