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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Common code to handle map devices which are simple RAM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * (C) 2000 Red Hat. GPL'd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static int mapram_erase (struct mtd_info *, struct erase_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static void mapram_nop (struct mtd_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static struct mtd_info *map_ram_probe(struct map_info *map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static int mapram_point (struct mtd_info *mtd, loff_t from, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 			 size_t *retlen, void **virt, resource_size_t *phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static int mapram_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static struct mtd_chip_driver mapram_chipdrv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	.probe	= map_ram_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	.name	= "map_ram",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	.module	= THIS_MODULE
^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) static struct mtd_info *map_ram_probe(struct map_info *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	/* Check the first byte is RAM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	map_write8(map, 0x55, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (map_read8(map, 0) != 0x55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	map_write8(map, 0xAA, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (map_read8(map, 0) != 0xAA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	/* Check the last byte is RAM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	map_write8(map, 0x55, map->size-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (map_read8(map, map->size-1) != 0x55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	map_write8(map, 0xAA, map->size-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (map_read8(map, map->size-1) != 0xAA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/* OK. It seems to be RAM. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (!mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	map->fldrv = &mapram_chipdrv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	mtd->priv = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	mtd->name = map->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	mtd->type = MTD_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	mtd->size = map->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	mtd->_erase = mapram_erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	mtd->_read = mapram_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	mtd->_write = mapram_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	mtd->_panic_write = mapram_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	mtd->_point = mapram_point;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	mtd->_sync = mapram_nop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	mtd->_unpoint = mapram_unpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	mtd->flags = MTD_CAP_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	mtd->writesize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	mtd->erasesize = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  	while(mtd->size & (mtd->erasesize - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		mtd->erasesize >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	__module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static int mapram_point(struct mtd_info *mtd, loff_t from, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			size_t *retlen, void **virt, resource_size_t *phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct map_info *map = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (!map->virt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	*virt = map->virt + from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		*phys = map->phys + from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	*retlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int mapram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct map_info *map = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	map_copy_from(map, buf, from, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	*retlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct map_info *map = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	map_copy_to(map, to, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	*retlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	/* Yeah, it's inefficient. Who cares? It's faster than a _real_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	   flash erase. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct map_info *map = mtd->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	map_word allff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	allff = map_word_ff(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	for (i=0; i<instr->len; i += map_bankwidth(map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		map_write(map, allff, instr->addr + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static void mapram_nop(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	/* Nothing to see here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int __init map_ram_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	register_mtd_chip_driver(&mapram_chipdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static void __exit map_ram_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	unregister_mtd_chip_driver(&mapram_chipdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) module_init(map_ram_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) module_exit(map_ram_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODULE_DESCRIPTION("MTD chip driver for RAM chips");