1*d2912cb1SThomas 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 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 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 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 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 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 1213c544739SJohn Crispin ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1223c544739SJohn Crispin if (!ltq_mtd->res) { 12302fa961fSJohn Crispin dev_err(&pdev->dev, "failed to get memory resource\n"); 124436bf63bSJingoo Han return -ENOENT; 1253c544739SJohn Crispin } 1263c544739SJohn Crispin 127436bf63bSJingoo Han ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info), 128436bf63bSJingoo Han GFP_KERNEL); 129436bf63bSJingoo Han if (!ltq_mtd->map) 130436bf63bSJingoo Han return -ENOMEM; 131436bf63bSJingoo Han 13202fa961fSJohn Crispin ltq_mtd->map->phys = ltq_mtd->res->start; 13302fa961fSJohn Crispin ltq_mtd->map->size = resource_size(ltq_mtd->res); 134b0de774cSThierry Reding ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res); 135436bf63bSJingoo Han if (IS_ERR(ltq_mtd->map->virt)) 136436bf63bSJingoo Han return PTR_ERR(ltq_mtd->map->virt); 1373c544739SJohn Crispin 1383c544739SJohn Crispin ltq_mtd->map->name = ltq_map_name; 1393c544739SJohn Crispin ltq_mtd->map->bankwidth = 2; 1403c544739SJohn Crispin ltq_mtd->map->read = ltq_read16; 1413c544739SJohn Crispin ltq_mtd->map->write = ltq_write16; 1423c544739SJohn Crispin ltq_mtd->map->copy_from = ltq_copy_from; 1433c544739SJohn Crispin ltq_mtd->map->copy_to = ltq_copy_to; 1443c544739SJohn Crispin 1453c544739SJohn Crispin ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING; 1463c544739SJohn Crispin ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map); 1473c544739SJohn Crispin ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL; 1483c544739SJohn Crispin 1493c544739SJohn Crispin if (!ltq_mtd->mtd) { 1503c544739SJohn Crispin dev_err(&pdev->dev, "probing failed\n"); 151436bf63bSJingoo Han return -ENXIO; 1523c544739SJohn Crispin } 1533c544739SJohn Crispin 154c54c2fb7SFrans Klaver ltq_mtd->mtd->dev.parent = &pdev->dev; 155004b5e60SBrian Norris mtd_set_of_node(ltq_mtd->mtd, pdev->dev.of_node); 1563c544739SJohn Crispin 1573c544739SJohn Crispin cfi = ltq_mtd->map->fldrv_priv; 1583c544739SJohn Crispin cfi->addr_unlock1 ^= 1; 1593c544739SJohn Crispin cfi->addr_unlock2 ^= 1; 1603c544739SJohn Crispin 161004b5e60SBrian Norris err = mtd_device_register(ltq_mtd->mtd, NULL, 0); 1623c544739SJohn Crispin if (err) { 1633c544739SJohn Crispin dev_err(&pdev->dev, "failed to add partitions\n"); 1643c544739SJohn Crispin goto err_destroy; 1653c544739SJohn Crispin } 1663c544739SJohn Crispin 1673c544739SJohn Crispin return 0; 1683c544739SJohn Crispin 1693c544739SJohn Crispin err_destroy: 1703c544739SJohn Crispin map_destroy(ltq_mtd->mtd); 1713c544739SJohn Crispin return err; 1723c544739SJohn Crispin } 1733c544739SJohn Crispin 174810b7e06SBill Pemberton static int 1753c544739SJohn Crispin ltq_mtd_remove(struct platform_device *pdev) 1763c544739SJohn Crispin { 1773c544739SJohn Crispin struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev); 1783c544739SJohn Crispin 179436bf63bSJingoo Han if (ltq_mtd && ltq_mtd->mtd) { 1802e5db86dSJohn Crispin mtd_device_unregister(ltq_mtd->mtd); 1813c544739SJohn Crispin map_destroy(ltq_mtd->mtd); 1823c544739SJohn Crispin } 1833c544739SJohn Crispin return 0; 1843c544739SJohn Crispin } 1853c544739SJohn Crispin 18602fa961fSJohn Crispin static const struct of_device_id ltq_mtd_match[] = { 18702fa961fSJohn Crispin { .compatible = "lantiq,nor" }, 18802fa961fSJohn Crispin {}, 18902fa961fSJohn Crispin }; 19002fa961fSJohn Crispin MODULE_DEVICE_TABLE(of, ltq_mtd_match); 19102fa961fSJohn Crispin 1923c544739SJohn Crispin static struct platform_driver ltq_mtd_driver = { 19302fa961fSJohn Crispin .probe = ltq_mtd_probe, 1945153b88cSBill Pemberton .remove = ltq_mtd_remove, 1953c544739SJohn Crispin .driver = { 19602fa961fSJohn Crispin .name = "ltq-nor", 19702fa961fSJohn Crispin .of_match_table = ltq_mtd_match, 1983c544739SJohn Crispin }, 1993c544739SJohn Crispin }; 2003c544739SJohn Crispin 20102fa961fSJohn Crispin module_platform_driver(ltq_mtd_driver); 2023c544739SJohn Crispin 2033c544739SJohn Crispin MODULE_LICENSE("GPL"); 20440bc941dSJohn Crispin MODULE_AUTHOR("John Crispin <john@phrozen.org>"); 2053c544739SJohn Crispin MODULE_DESCRIPTION("Lantiq SoC NOR"); 206