2 * drivers/mtd/maps/bfin-async-flash.c
4 * Handle the case where flash memory and ethernet mac/phy are
5 * mapped onto the same async bank. The BF533-STAMP does this
6 * for example. All board-specific configuration goes in your
7 * board resources file.
9 * Copyright 2000 Nicolas Pitre <nico@fluxnic.net>
10 * Copyright 2005-2008 Analog Devices Inc.
12 * Enter bugs at http://blackfin.uclinux.org/
14 * Licensed under the GPL-2 or later.
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/map.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/physmap.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/types.h>
28 #include <asm/blackfin.h>
29 #include <linux/gpio.h>
31 #include <asm/unaligned.h>
33 #define pr_devinit(fmt, args...) \
34 ({ static const char __fmt[] = fmt; printk(__fmt, ## args); })
36 #define DRIVER_NAME "bfin-async-flash"
42 uint32_t flash_ambctl0
, flash_ambctl1
;
43 uint32_t save_ambctl0
, save_ambctl1
;
44 unsigned long irq_flags
;
47 static void switch_to_flash(struct async_state
*state
)
49 local_irq_save(state
->irq_flags
);
51 gpio_set_value(state
->enet_flash_pin
, 0);
53 state
->save_ambctl0
= bfin_read_EBIU_AMBCTL0();
54 state
->save_ambctl1
= bfin_read_EBIU_AMBCTL1();
55 bfin_write_EBIU_AMBCTL0(state
->flash_ambctl0
);
56 bfin_write_EBIU_AMBCTL1(state
->flash_ambctl1
);
60 static void switch_back(struct async_state
*state
)
62 bfin_write_EBIU_AMBCTL0(state
->save_ambctl0
);
63 bfin_write_EBIU_AMBCTL1(state
->save_ambctl1
);
66 gpio_set_value(state
->enet_flash_pin
, 1);
68 local_irq_restore(state
->irq_flags
);
71 static map_word
bfin_flash_read(struct map_info
*map
, unsigned long ofs
)
73 struct async_state
*state
= (struct async_state
*)map
->map_priv_1
;
77 switch_to_flash(state
);
79 word
= readw(map
->virt
+ ofs
);
87 static void bfin_flash_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
89 struct async_state
*state
= (struct async_state
*)map
->map_priv_1
;
91 switch_to_flash(state
);
93 memcpy(to
, map
->virt
+ from
, len
);
98 static void bfin_flash_write(struct map_info
*map
, map_word d1
, unsigned long ofs
)
100 struct async_state
*state
= (struct async_state
*)map
->map_priv_1
;
105 switch_to_flash(state
);
107 writew(d
, map
->virt
+ ofs
);
113 static void bfin_flash_copy_to(struct map_info
*map
, unsigned long to
, const void *from
, ssize_t len
)
115 struct async_state
*state
= (struct async_state
*)map
->map_priv_1
;
117 switch_to_flash(state
);
119 memcpy(map
->virt
+ to
, from
, len
);
125 static const char * const part_probe_types
[] = {
126 "cmdlinepart", "RedBoot", NULL
};
128 static int bfin_flash_probe(struct platform_device
*pdev
)
131 struct physmap_flash_data
*pdata
= dev_get_platdata(&pdev
->dev
);
132 struct resource
*memory
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
133 struct resource
*flash_ambctl
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
134 struct async_state
*state
;
136 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
140 state
->map
.name
= DRIVER_NAME
;
141 state
->map
.read
= bfin_flash_read
;
142 state
->map
.copy_from
= bfin_flash_copy_from
;
143 state
->map
.write
= bfin_flash_write
;
144 state
->map
.copy_to
= bfin_flash_copy_to
;
145 state
->map
.bankwidth
= pdata
->width
;
146 state
->map
.size
= resource_size(memory
);
147 state
->map
.virt
= (void __iomem
*)memory
->start
;
148 state
->map
.phys
= memory
->start
;
149 state
->map
.map_priv_1
= (unsigned long)state
;
150 state
->enet_flash_pin
= platform_get_irq(pdev
, 0);
151 state
->flash_ambctl0
= flash_ambctl
->start
;
152 state
->flash_ambctl1
= flash_ambctl
->end
;
154 if (gpio_request(state
->enet_flash_pin
, DRIVER_NAME
)) {
155 pr_devinit(KERN_ERR DRIVER_NAME
": Failed to request gpio %d\n", state
->enet_flash_pin
);
159 gpio_direction_output(state
->enet_flash_pin
, 1);
161 pr_devinit(KERN_NOTICE DRIVER_NAME
": probing %d-bit flash bus\n", state
->map
.bankwidth
* 8);
162 state
->mtd
= do_map_probe(memory
->name
, &state
->map
);
164 gpio_free(state
->enet_flash_pin
);
169 mtd_device_parse_register(state
->mtd
, part_probe_types
, NULL
,
170 pdata
->parts
, pdata
->nr_parts
);
172 platform_set_drvdata(pdev
, state
);
177 static int bfin_flash_remove(struct platform_device
*pdev
)
179 struct async_state
*state
= platform_get_drvdata(pdev
);
180 gpio_free(state
->enet_flash_pin
);
181 mtd_device_unregister(state
->mtd
);
182 map_destroy(state
->mtd
);
187 static struct platform_driver bfin_flash_driver
= {
188 .probe
= bfin_flash_probe
,
189 .remove
= bfin_flash_remove
,
195 module_platform_driver(bfin_flash_driver
);
197 MODULE_LICENSE("GPL");
198 MODULE_DESCRIPTION("MTD map driver for Blackfins with flash/ethernet on same async bank");