xref: /openbmc/linux/drivers/mtd/maps/lantiq-flash.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23c544739SJohn Crispin /*
33c544739SJohn Crispin  *
43c544739SJohn Crispin  *  Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
540bc941dSJohn Crispin  *  Copyright (C) 2010 John Crispin <john@phrozen.org>
63c544739SJohn Crispin  */
73c544739SJohn Crispin 
8b0de774cSThierry Reding #include <linux/err.h>
93c544739SJohn Crispin #include <linux/module.h>
103c544739SJohn Crispin #include <linux/types.h>
113c544739SJohn Crispin #include <linux/kernel.h>
123c544739SJohn Crispin #include <linux/io.h>
133c544739SJohn Crispin #include <linux/slab.h>
143c544739SJohn Crispin #include <linux/mtd/mtd.h>
153c544739SJohn Crispin #include <linux/mtd/map.h>
163c544739SJohn Crispin #include <linux/mtd/partitions.h>
173c544739SJohn Crispin #include <linux/mtd/cfi.h>
183c544739SJohn Crispin #include <linux/platform_device.h>
193c544739SJohn Crispin #include <linux/mtd/physmap.h>
20fa09ededSJohn Crispin #include <linux/of.h>
213c544739SJohn Crispin 
223c544739SJohn Crispin #include <lantiq_soc.h>
233c544739SJohn Crispin 
243c544739SJohn Crispin /*
253c544739SJohn Crispin  * The NOR flash is connected to the same external bus unit (EBU) as PCI.
263c544739SJohn Crispin  * To make PCI work we need to enable the endianness swapping for the address
273c544739SJohn Crispin  * written to the EBU. This endianness swapping works for PCI correctly but
283c544739SJohn Crispin  * fails for attached NOR devices. To workaround this we need to use a complex
293c544739SJohn Crispin  * map. The workaround involves swapping all addresses whilst probing the chip.
303c544739SJohn Crispin  * Once probing is complete we stop swapping the addresses but swizzle the
313c544739SJohn Crispin  * unlock addresses to ensure that access to the NOR device works correctly.
323c544739SJohn Crispin  */
333c544739SJohn Crispin 
343c544739SJohn Crispin enum {
353c544739SJohn Crispin 	LTQ_NOR_PROBING,
363c544739SJohn Crispin 	LTQ_NOR_NORMAL
373c544739SJohn Crispin };
383c544739SJohn Crispin 
393c544739SJohn Crispin struct ltq_mtd {
403c544739SJohn Crispin 	struct resource *res;
413c544739SJohn Crispin 	struct mtd_info *mtd;
423c544739SJohn Crispin 	struct map_info *map;
433c544739SJohn Crispin };
443c544739SJohn Crispin 
4502fa961fSJohn Crispin static const char ltq_map_name[] = "ltq_nor";
463c544739SJohn Crispin 
473c544739SJohn Crispin static map_word
ltq_read16(struct map_info * map,unsigned long adr)483c544739SJohn Crispin ltq_read16(struct map_info *map, unsigned long adr)
493c544739SJohn Crispin {
503c544739SJohn Crispin 	unsigned long flags;
513c544739SJohn Crispin 	map_word temp;
523c544739SJohn Crispin 
533c544739SJohn Crispin 	if (map->map_priv_1 == LTQ_NOR_PROBING)
543c544739SJohn Crispin 		adr ^= 2;
553c544739SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
563c544739SJohn Crispin 	temp.x[0] = *(u16 *)(map->virt + adr);
573c544739SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
583c544739SJohn Crispin 	return temp;
593c544739SJohn Crispin }
603c544739SJohn Crispin 
613c544739SJohn Crispin static void
ltq_write16(struct map_info * map,map_word d,unsigned long adr)623c544739SJohn Crispin ltq_write16(struct map_info *map, map_word d, unsigned long adr)
633c544739SJohn Crispin {
643c544739SJohn Crispin 	unsigned long flags;
653c544739SJohn Crispin 
663c544739SJohn Crispin 	if (map->map_priv_1 == LTQ_NOR_PROBING)
673c544739SJohn Crispin 		adr ^= 2;
683c544739SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
693c544739SJohn Crispin 	*(u16 *)(map->virt + adr) = d.x[0];
703c544739SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
713c544739SJohn Crispin }
723c544739SJohn Crispin 
733c544739SJohn Crispin /*
743c544739SJohn Crispin  * The following 2 functions copy data between iomem and a cached memory
753c544739SJohn Crispin  * section. As memcpy() makes use of pre-fetching we cannot use it here.
763c544739SJohn Crispin  * The normal alternative of using memcpy_{to,from}io also makes use of
773c544739SJohn Crispin  * memcpy() on MIPS so it is not applicable either. We are therefore stuck
783c544739SJohn Crispin  * with having to use our own loop.
793c544739SJohn Crispin  */
803c544739SJohn Crispin static void
ltq_copy_from(struct map_info * map,void * to,unsigned long from,ssize_t len)813c544739SJohn Crispin ltq_copy_from(struct map_info *map, void *to,
823c544739SJohn Crispin 	unsigned long from, ssize_t len)
833c544739SJohn Crispin {
843c544739SJohn Crispin 	unsigned char *f = (unsigned char *)map->virt + from;
853c544739SJohn Crispin 	unsigned char *t = (unsigned char *)to;
863c544739SJohn Crispin 	unsigned long flags;
873c544739SJohn Crispin 
883c544739SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
893c544739SJohn Crispin 	while (len--)
903c544739SJohn Crispin 		*t++ = *f++;
913c544739SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
923c544739SJohn Crispin }
933c544739SJohn Crispin 
943c544739SJohn Crispin static void
ltq_copy_to(struct map_info * map,unsigned long to,const void * from,ssize_t len)953c544739SJohn Crispin ltq_copy_to(struct map_info *map, unsigned long to,
963c544739SJohn Crispin 	const void *from, ssize_t len)
973c544739SJohn Crispin {
983c544739SJohn Crispin 	unsigned char *f = (unsigned char *)from;
993c544739SJohn Crispin 	unsigned char *t = (unsigned char *)map->virt + to;
1003c544739SJohn Crispin 	unsigned long flags;
1013c544739SJohn Crispin 
1023c544739SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
1033c544739SJohn Crispin 	while (len--)
1043c544739SJohn Crispin 		*t++ = *f++;
1053c544739SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
1063c544739SJohn Crispin }
1073c544739SJohn Crispin 
10806f25510SBill Pemberton static int
ltq_mtd_probe(struct platform_device * pdev)1093c544739SJohn Crispin ltq_mtd_probe(struct platform_device *pdev)
1103c544739SJohn Crispin {
1113c544739SJohn Crispin 	struct ltq_mtd *ltq_mtd;
1123c544739SJohn Crispin 	struct cfi_private *cfi;
1133c544739SJohn Crispin 	int err;
1143c544739SJohn Crispin 
115436bf63bSJingoo Han 	ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL);
116436bf63bSJingoo Han 	if (!ltq_mtd)
117436bf63bSJingoo Han 		return -ENOMEM;
118436bf63bSJingoo Han 
1193c544739SJohn Crispin 	platform_set_drvdata(pdev, ltq_mtd);
1203c544739SJohn Crispin 
121*6145e07eSYangtao Li 	ltq_mtd->map->virt = devm_platform_get_and_ioremap_resource(pdev, 0, &ltq_mtd->res);
122*6145e07eSYangtao Li 	if (IS_ERR(ltq_mtd->map->virt))
123*6145e07eSYangtao Li 		return PTR_ERR(ltq_mtd->map->virt);
1243c544739SJohn Crispin 
125436bf63bSJingoo Han 	ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
126436bf63bSJingoo Han 				    GFP_KERNEL);
127436bf63bSJingoo Han 	if (!ltq_mtd->map)
128436bf63bSJingoo Han 		return -ENOMEM;
129436bf63bSJingoo Han 
13002fa961fSJohn Crispin 	ltq_mtd->map->phys = ltq_mtd->res->start;
13102fa961fSJohn Crispin 	ltq_mtd->map->size = resource_size(ltq_mtd->res);
1323c544739SJohn Crispin 
1333c544739SJohn Crispin 	ltq_mtd->map->name = ltq_map_name;
1343c544739SJohn Crispin 	ltq_mtd->map->bankwidth = 2;
1353c544739SJohn Crispin 	ltq_mtd->map->read = ltq_read16;
1363c544739SJohn Crispin 	ltq_mtd->map->write = ltq_write16;
1373c544739SJohn Crispin 	ltq_mtd->map->copy_from = ltq_copy_from;
1383c544739SJohn Crispin 	ltq_mtd->map->copy_to = ltq_copy_to;
1393c544739SJohn Crispin 
1403c544739SJohn Crispin 	ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
1413c544739SJohn Crispin 	ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
1423c544739SJohn Crispin 	ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
1433c544739SJohn Crispin 
1443c544739SJohn Crispin 	if (!ltq_mtd->mtd) {
1453c544739SJohn Crispin 		dev_err(&pdev->dev, "probing failed\n");
146436bf63bSJingoo Han 		return -ENXIO;
1473c544739SJohn Crispin 	}
1483c544739SJohn Crispin 
149c54c2fb7SFrans Klaver 	ltq_mtd->mtd->dev.parent = &pdev->dev;
150004b5e60SBrian Norris 	mtd_set_of_node(ltq_mtd->mtd, pdev->dev.of_node);
1513c544739SJohn Crispin 
1523c544739SJohn Crispin 	cfi = ltq_mtd->map->fldrv_priv;
1533c544739SJohn Crispin 	cfi->addr_unlock1 ^= 1;
1543c544739SJohn Crispin 	cfi->addr_unlock2 ^= 1;
1553c544739SJohn Crispin 
156004b5e60SBrian Norris 	err = mtd_device_register(ltq_mtd->mtd, NULL, 0);
1573c544739SJohn Crispin 	if (err) {
1583c544739SJohn Crispin 		dev_err(&pdev->dev, "failed to add partitions\n");
1593c544739SJohn Crispin 		goto err_destroy;
1603c544739SJohn Crispin 	}
1613c544739SJohn Crispin 
1623c544739SJohn Crispin 	return 0;
1633c544739SJohn Crispin 
1643c544739SJohn Crispin err_destroy:
1653c544739SJohn Crispin 	map_destroy(ltq_mtd->mtd);
1663c544739SJohn Crispin 	return err;
1673c544739SJohn Crispin }
1683c544739SJohn Crispin 
169810b7e06SBill Pemberton static int
ltq_mtd_remove(struct platform_device * pdev)1703c544739SJohn Crispin ltq_mtd_remove(struct platform_device *pdev)
1713c544739SJohn Crispin {
1723c544739SJohn Crispin 	struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
1733c544739SJohn Crispin 
174436bf63bSJingoo Han 	if (ltq_mtd && ltq_mtd->mtd) {
1752e5db86dSJohn Crispin 		mtd_device_unregister(ltq_mtd->mtd);
1763c544739SJohn Crispin 		map_destroy(ltq_mtd->mtd);
1773c544739SJohn Crispin 	}
1783c544739SJohn Crispin 	return 0;
1793c544739SJohn Crispin }
1803c544739SJohn Crispin 
18102fa961fSJohn Crispin static const struct of_device_id ltq_mtd_match[] = {
18202fa961fSJohn Crispin 	{ .compatible = "lantiq,nor" },
18302fa961fSJohn Crispin 	{},
18402fa961fSJohn Crispin };
18502fa961fSJohn Crispin MODULE_DEVICE_TABLE(of, ltq_mtd_match);
18602fa961fSJohn Crispin 
1873c544739SJohn Crispin static struct platform_driver ltq_mtd_driver = {
18802fa961fSJohn Crispin 	.probe = ltq_mtd_probe,
1895153b88cSBill Pemberton 	.remove = ltq_mtd_remove,
1903c544739SJohn Crispin 	.driver = {
19102fa961fSJohn Crispin 		.name = "ltq-nor",
19202fa961fSJohn Crispin 		.of_match_table = ltq_mtd_match,
1933c544739SJohn Crispin 	},
1943c544739SJohn Crispin };
1953c544739SJohn Crispin 
19602fa961fSJohn Crispin module_platform_driver(ltq_mtd_driver);
1973c544739SJohn Crispin 
1983c544739SJohn Crispin MODULE_LICENSE("GPL");
19940bc941dSJohn Crispin MODULE_AUTHOR("John Crispin <john@phrozen.org>");
2003c544739SJohn Crispin MODULE_DESCRIPTION("Lantiq SoC NOR");
201