Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2010 John Crispin <john@phrozen.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mtd/map.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mtd/partitions.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mtd/cfi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mtd/physmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <lantiq_soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * The NOR flash is connected to the same external bus unit (EBU) as PCI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * To make PCI work we need to enable the endianness swapping for the address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * written to the EBU. This endianness swapping works for PCI correctly but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * fails for attached NOR devices. To workaround this we need to use a complex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * map. The workaround involves swapping all addresses whilst probing the chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * Once probing is complete we stop swapping the addresses but swizzle the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * unlock addresses to ensure that access to the NOR device works correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	LTQ_NOR_PROBING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	LTQ_NOR_NORMAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct ltq_mtd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct map_info *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static const char ltq_map_name[] = "ltq_nor";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static map_word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) ltq_read16(struct map_info *map, unsigned long adr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	map_word temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (map->map_priv_1 == LTQ_NOR_PROBING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		adr ^= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	spin_lock_irqsave(&ebu_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	temp.x[0] = *(u16 *)(map->virt + adr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	spin_unlock_irqrestore(&ebu_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	return temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) ltq_write16(struct map_info *map, map_word d, unsigned long adr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (map->map_priv_1 == LTQ_NOR_PROBING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		adr ^= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	spin_lock_irqsave(&ebu_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	*(u16 *)(map->virt + adr) = d.x[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	spin_unlock_irqrestore(&ebu_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * The following 2 functions copy data between iomem and a cached memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * section. As memcpy() makes use of pre-fetching we cannot use it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * The normal alternative of using memcpy_{to,from}io also makes use of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * memcpy() on MIPS so it is not applicable either. We are therefore stuck
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * with having to use our own loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) ltq_copy_from(struct map_info *map, void *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	unsigned long from, ssize_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	unsigned char *f = (unsigned char *)map->virt + from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	unsigned char *t = (unsigned char *)to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	spin_lock_irqsave(&ebu_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	while (len--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		*t++ = *f++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	spin_unlock_irqrestore(&ebu_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) ltq_copy_to(struct map_info *map, unsigned long to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	const void *from, ssize_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	unsigned char *f = (unsigned char *)from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	unsigned char *t = (unsigned char *)map->virt + to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	spin_lock_irqsave(&ebu_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	while (len--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		*t++ = *f++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	spin_unlock_irqrestore(&ebu_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ltq_mtd_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct ltq_mtd *ltq_mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct cfi_private *cfi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (!ltq_mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	platform_set_drvdata(pdev, ltq_mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (!ltq_mtd->res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		dev_err(&pdev->dev, "failed to get memory resource\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (!ltq_mtd->map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ltq_mtd->map->phys = ltq_mtd->res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	ltq_mtd->map->size = resource_size(ltq_mtd->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (IS_ERR(ltq_mtd->map->virt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return PTR_ERR(ltq_mtd->map->virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	ltq_mtd->map->name = ltq_map_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	ltq_mtd->map->bankwidth = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	ltq_mtd->map->read = ltq_read16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	ltq_mtd->map->write = ltq_write16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	ltq_mtd->map->copy_from = ltq_copy_from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	ltq_mtd->map->copy_to = ltq_copy_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!ltq_mtd->mtd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		dev_err(&pdev->dev, "probing failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	ltq_mtd->mtd->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	mtd_set_of_node(ltq_mtd->mtd, pdev->dev.of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	cfi = ltq_mtd->map->fldrv_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	cfi->addr_unlock1 ^= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	cfi->addr_unlock2 ^= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	err = mtd_device_register(ltq_mtd->mtd, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		dev_err(&pdev->dev, "failed to add partitions\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		goto err_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) err_destroy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	map_destroy(ltq_mtd->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ltq_mtd_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (ltq_mtd && ltq_mtd->mtd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		mtd_device_unregister(ltq_mtd->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		map_destroy(ltq_mtd->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static const struct of_device_id ltq_mtd_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	{ .compatible = "lantiq,nor" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) MODULE_DEVICE_TABLE(of, ltq_mtd_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static struct platform_driver ltq_mtd_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	.probe = ltq_mtd_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	.remove = ltq_mtd_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		.name = "ltq-nor",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		.of_match_table = ltq_mtd_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) module_platform_driver(ltq_mtd_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) MODULE_AUTHOR("John Crispin <john@phrozen.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) MODULE_DESCRIPTION("Lantiq SoC NOR");