Linux Audio

Check our new training course

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