xref: /linux/drivers/mtd/maps/lantiq-flash.c (revision 004b5e6031f4e9fd90d565fb213b74cd06d03718)
13c544739SJohn Crispin /*
23c544739SJohn Crispin  *  This program is free software; you can redistribute it and/or modify it
33c544739SJohn Crispin  *  under the terms of the GNU General Public License version 2 as published
43c544739SJohn Crispin  *  by the Free Software Foundation.
53c544739SJohn Crispin  *
63c544739SJohn Crispin  *  Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
73c544739SJohn Crispin  *  Copyright (C) 2010 John Crispin <blogic@openwrt.org>
83c544739SJohn Crispin  */
93c544739SJohn Crispin 
10b0de774cSThierry Reding #include <linux/err.h>
113c544739SJohn Crispin #include <linux/module.h>
123c544739SJohn Crispin #include <linux/types.h>
133c544739SJohn Crispin #include <linux/kernel.h>
143c544739SJohn Crispin #include <linux/io.h>
153c544739SJohn Crispin #include <linux/slab.h>
163c544739SJohn Crispin #include <linux/mtd/mtd.h>
173c544739SJohn Crispin #include <linux/mtd/map.h>
183c544739SJohn Crispin #include <linux/mtd/partitions.h>
193c544739SJohn Crispin #include <linux/mtd/cfi.h>
203c544739SJohn Crispin #include <linux/platform_device.h>
213c544739SJohn Crispin #include <linux/mtd/physmap.h>
22fa09ededSJohn Crispin #include <linux/of.h>
233c544739SJohn Crispin 
243c544739SJohn Crispin #include <lantiq_soc.h>
253c544739SJohn Crispin 
263c544739SJohn Crispin /*
273c544739SJohn Crispin  * The NOR flash is connected to the same external bus unit (EBU) as PCI.
283c544739SJohn Crispin  * To make PCI work we need to enable the endianness swapping for the address
293c544739SJohn Crispin  * written to the EBU. This endianness swapping works for PCI correctly but
303c544739SJohn Crispin  * fails for attached NOR devices. To workaround this we need to use a complex
313c544739SJohn Crispin  * map. The workaround involves swapping all addresses whilst probing the chip.
323c544739SJohn Crispin  * Once probing is complete we stop swapping the addresses but swizzle the
333c544739SJohn Crispin  * unlock addresses to ensure that access to the NOR device works correctly.
343c544739SJohn Crispin  */
353c544739SJohn Crispin 
363c544739SJohn Crispin enum {
373c544739SJohn Crispin 	LTQ_NOR_PROBING,
383c544739SJohn Crispin 	LTQ_NOR_NORMAL
393c544739SJohn Crispin };
403c544739SJohn Crispin 
413c544739SJohn Crispin struct ltq_mtd {
423c544739SJohn Crispin 	struct resource *res;
433c544739SJohn Crispin 	struct mtd_info *mtd;
443c544739SJohn Crispin 	struct map_info *map;
453c544739SJohn Crispin };
463c544739SJohn Crispin 
4702fa961fSJohn Crispin static const char ltq_map_name[] = "ltq_nor";
483c544739SJohn Crispin 
493c544739SJohn Crispin static map_word
503c544739SJohn Crispin ltq_read16(struct map_info *map, unsigned long adr)
513c544739SJohn Crispin {
523c544739SJohn Crispin 	unsigned long flags;
533c544739SJohn Crispin 	map_word temp;
543c544739SJohn Crispin 
553c544739SJohn Crispin 	if (map->map_priv_1 == LTQ_NOR_PROBING)
563c544739SJohn Crispin 		adr ^= 2;
573c544739SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
583c544739SJohn Crispin 	temp.x[0] = *(u16 *)(map->virt + adr);
593c544739SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
603c544739SJohn Crispin 	return temp;
613c544739SJohn Crispin }
623c544739SJohn Crispin 
633c544739SJohn Crispin static void
643c544739SJohn Crispin ltq_write16(struct map_info *map, map_word d, unsigned long adr)
653c544739SJohn Crispin {
663c544739SJohn Crispin 	unsigned long flags;
673c544739SJohn Crispin 
683c544739SJohn Crispin 	if (map->map_priv_1 == LTQ_NOR_PROBING)
693c544739SJohn Crispin 		adr ^= 2;
703c544739SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
713c544739SJohn Crispin 	*(u16 *)(map->virt + adr) = d.x[0];
723c544739SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
733c544739SJohn Crispin }
743c544739SJohn Crispin 
753c544739SJohn Crispin /*
763c544739SJohn Crispin  * The following 2 functions copy data between iomem and a cached memory
773c544739SJohn Crispin  * section. As memcpy() makes use of pre-fetching we cannot use it here.
783c544739SJohn Crispin  * The normal alternative of using memcpy_{to,from}io also makes use of
793c544739SJohn Crispin  * memcpy() on MIPS so it is not applicable either. We are therefore stuck
803c544739SJohn Crispin  * with having to use our own loop.
813c544739SJohn Crispin  */
823c544739SJohn Crispin static void
833c544739SJohn Crispin ltq_copy_from(struct map_info *map, void *to,
843c544739SJohn Crispin 	unsigned long from, ssize_t len)
853c544739SJohn Crispin {
863c544739SJohn Crispin 	unsigned char *f = (unsigned char *)map->virt + from;
873c544739SJohn Crispin 	unsigned char *t = (unsigned char *)to;
883c544739SJohn Crispin 	unsigned long flags;
893c544739SJohn Crispin 
903c544739SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
913c544739SJohn Crispin 	while (len--)
923c544739SJohn Crispin 		*t++ = *f++;
933c544739SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
943c544739SJohn Crispin }
953c544739SJohn Crispin 
963c544739SJohn Crispin static void
973c544739SJohn Crispin ltq_copy_to(struct map_info *map, unsigned long to,
983c544739SJohn Crispin 	const void *from, ssize_t len)
993c544739SJohn Crispin {
1003c544739SJohn Crispin 	unsigned char *f = (unsigned char *)from;
1013c544739SJohn Crispin 	unsigned char *t = (unsigned char *)map->virt + to;
1023c544739SJohn Crispin 	unsigned long flags;
1033c544739SJohn Crispin 
1043c544739SJohn Crispin 	spin_lock_irqsave(&ebu_lock, flags);
1053c544739SJohn Crispin 	while (len--)
1063c544739SJohn Crispin 		*t++ = *f++;
1073c544739SJohn Crispin 	spin_unlock_irqrestore(&ebu_lock, flags);
1083c544739SJohn Crispin }
1093c544739SJohn Crispin 
11006f25510SBill Pemberton static int
1113c544739SJohn Crispin ltq_mtd_probe(struct platform_device *pdev)
1123c544739SJohn Crispin {
1133c544739SJohn Crispin 	struct ltq_mtd *ltq_mtd;
1143c544739SJohn Crispin 	struct cfi_private *cfi;
1153c544739SJohn Crispin 	int err;
1163c544739SJohn Crispin 
117fa09ededSJohn Crispin 	if (of_machine_is_compatible("lantiq,falcon") &&
118fa09ededSJohn Crispin 			(ltq_boot_select() != BS_FLASH)) {
119fa09ededSJohn Crispin 		dev_err(&pdev->dev, "invalid bootstrap options\n");
120fa09ededSJohn Crispin 		return -ENODEV;
121fa09ededSJohn Crispin 	}
122fa09ededSJohn Crispin 
123436bf63bSJingoo Han 	ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL);
124436bf63bSJingoo Han 	if (!ltq_mtd)
125436bf63bSJingoo Han 		return -ENOMEM;
126436bf63bSJingoo Han 
1273c544739SJohn Crispin 	platform_set_drvdata(pdev, ltq_mtd);
1283c544739SJohn Crispin 
1293c544739SJohn Crispin 	ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1303c544739SJohn Crispin 	if (!ltq_mtd->res) {
13102fa961fSJohn Crispin 		dev_err(&pdev->dev, "failed to get memory resource\n");
132436bf63bSJingoo Han 		return -ENOENT;
1333c544739SJohn Crispin 	}
1343c544739SJohn Crispin 
135436bf63bSJingoo Han 	ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
136436bf63bSJingoo Han 				    GFP_KERNEL);
137436bf63bSJingoo Han 	if (!ltq_mtd->map)
138436bf63bSJingoo Han 		return -ENOMEM;
139436bf63bSJingoo Han 
14002fa961fSJohn Crispin 	ltq_mtd->map->phys = ltq_mtd->res->start;
14102fa961fSJohn Crispin 	ltq_mtd->map->size = resource_size(ltq_mtd->res);
142b0de774cSThierry Reding 	ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
143436bf63bSJingoo Han 	if (IS_ERR(ltq_mtd->map->virt))
144436bf63bSJingoo Han 		return PTR_ERR(ltq_mtd->map->virt);
1453c544739SJohn Crispin 
1463c544739SJohn Crispin 	ltq_mtd->map->name = ltq_map_name;
1473c544739SJohn Crispin 	ltq_mtd->map->bankwidth = 2;
1483c544739SJohn Crispin 	ltq_mtd->map->read = ltq_read16;
1493c544739SJohn Crispin 	ltq_mtd->map->write = ltq_write16;
1503c544739SJohn Crispin 	ltq_mtd->map->copy_from = ltq_copy_from;
1513c544739SJohn Crispin 	ltq_mtd->map->copy_to = ltq_copy_to;
1523c544739SJohn Crispin 
1533c544739SJohn Crispin 	ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
1543c544739SJohn Crispin 	ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
1553c544739SJohn Crispin 	ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
1563c544739SJohn Crispin 
1573c544739SJohn Crispin 	if (!ltq_mtd->mtd) {
1583c544739SJohn Crispin 		dev_err(&pdev->dev, "probing failed\n");
159436bf63bSJingoo Han 		return -ENXIO;
1603c544739SJohn Crispin 	}
1613c544739SJohn Crispin 
162c54c2fb7SFrans Klaver 	ltq_mtd->mtd->dev.parent = &pdev->dev;
163*004b5e60SBrian Norris 	mtd_set_of_node(ltq_mtd->mtd, pdev->dev.of_node);
1643c544739SJohn Crispin 
1653c544739SJohn Crispin 	cfi = ltq_mtd->map->fldrv_priv;
1663c544739SJohn Crispin 	cfi->addr_unlock1 ^= 1;
1673c544739SJohn Crispin 	cfi->addr_unlock2 ^= 1;
1683c544739SJohn Crispin 
169*004b5e60SBrian Norris 	err = mtd_device_register(ltq_mtd->mtd, NULL, 0);
1703c544739SJohn Crispin 	if (err) {
1713c544739SJohn Crispin 		dev_err(&pdev->dev, "failed to add partitions\n");
1723c544739SJohn Crispin 		goto err_destroy;
1733c544739SJohn Crispin 	}
1743c544739SJohn Crispin 
1753c544739SJohn Crispin 	return 0;
1763c544739SJohn Crispin 
1773c544739SJohn Crispin err_destroy:
1783c544739SJohn Crispin 	map_destroy(ltq_mtd->mtd);
1793c544739SJohn Crispin 	return err;
1803c544739SJohn Crispin }
1813c544739SJohn Crispin 
182810b7e06SBill Pemberton static int
1833c544739SJohn Crispin ltq_mtd_remove(struct platform_device *pdev)
1843c544739SJohn Crispin {
1853c544739SJohn Crispin 	struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
1863c544739SJohn Crispin 
187436bf63bSJingoo Han 	if (ltq_mtd && ltq_mtd->mtd) {
1882e5db86dSJohn Crispin 		mtd_device_unregister(ltq_mtd->mtd);
1893c544739SJohn Crispin 		map_destroy(ltq_mtd->mtd);
1903c544739SJohn Crispin 	}
1913c544739SJohn Crispin 	return 0;
1923c544739SJohn Crispin }
1933c544739SJohn Crispin 
19402fa961fSJohn Crispin static const struct of_device_id ltq_mtd_match[] = {
19502fa961fSJohn Crispin 	{ .compatible = "lantiq,nor" },
19602fa961fSJohn Crispin 	{},
19702fa961fSJohn Crispin };
19802fa961fSJohn Crispin MODULE_DEVICE_TABLE(of, ltq_mtd_match);
19902fa961fSJohn Crispin 
2003c544739SJohn Crispin static struct platform_driver ltq_mtd_driver = {
20102fa961fSJohn Crispin 	.probe = ltq_mtd_probe,
2025153b88cSBill Pemberton 	.remove = ltq_mtd_remove,
2033c544739SJohn Crispin 	.driver = {
20402fa961fSJohn Crispin 		.name = "ltq-nor",
20502fa961fSJohn Crispin 		.of_match_table = ltq_mtd_match,
2063c544739SJohn Crispin 	},
2073c544739SJohn Crispin };
2083c544739SJohn Crispin 
20902fa961fSJohn Crispin module_platform_driver(ltq_mtd_driver);
2103c544739SJohn Crispin 
2113c544739SJohn Crispin MODULE_LICENSE("GPL");
2123c544739SJohn Crispin MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
2133c544739SJohn Crispin MODULE_DESCRIPTION("Lantiq SoC NOR");
214