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@cam.org>
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/types.h>
27 #include <asm/blackfin.h>
28 #include <linux/gpio.h>
30 #include <asm/unaligned.h>
32 #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); })
34 #define DRIVER_NAME "bfin-async-flash"
40 uint32_t flash_ambctl0
, flash_ambctl1
;
41 uint32_t save_ambctl0
, save_ambctl1
;
42 unsigned long irq_flags
;
45 static void switch_to_flash(struct async_state
*state
)
47 local_irq_save(state
->irq_flags
);
49 gpio_set_value(state
->enet_flash_pin
, 0);
51 state
->save_ambctl0
= bfin_read_EBIU_AMBCTL0();
52 state
->save_ambctl1
= bfin_read_EBIU_AMBCTL1();
53 bfin_write_EBIU_AMBCTL0(state
->flash_ambctl0
);
54 bfin_write_EBIU_AMBCTL1(state
->flash_ambctl1
);
58 static void switch_back(struct async_state
*state
)
60 bfin_write_EBIU_AMBCTL0(state
->save_ambctl0
);
61 bfin_write_EBIU_AMBCTL1(state
->save_ambctl1
);
64 gpio_set_value(state
->enet_flash_pin
, 1);
66 local_irq_restore(state
->irq_flags
);
69 static map_word
bfin_read(struct map_info
*map
, unsigned long ofs
)
71 struct async_state
*state
= (struct async_state
*)map
->map_priv_1
;
75 switch_to_flash(state
);
77 word
= readw(map
->virt
+ ofs
);
85 static void bfin_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
87 struct async_state
*state
= (struct async_state
*)map
->map_priv_1
;
89 switch_to_flash(state
);
91 memcpy(to
, map
->virt
+ from
, len
);
96 static void bfin_write(struct map_info
*map
, map_word d1
, unsigned long ofs
)
98 struct async_state
*state
= (struct async_state
*)map
->map_priv_1
;
103 switch_to_flash(state
);
105 writew(d
, map
->virt
+ ofs
);
111 static void bfin_copy_to(struct map_info
*map
, unsigned long to
, const void *from
, ssize_t len
)
113 struct async_state
*state
= (struct async_state
*)map
->map_priv_1
;
115 switch_to_flash(state
);
117 memcpy(map
->virt
+ to
, from
, len
);
123 #ifdef CONFIG_MTD_PARTITIONS
124 static const char *part_probe_types
[] = { "cmdlinepart", "RedBoot", NULL
};
127 static int __devinit
bfin_flash_probe(struct platform_device
*pdev
)
130 struct physmap_flash_data
*pdata
= pdev
->dev
.platform_data
;
131 struct resource
*memory
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
132 struct resource
*flash_ambctl
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
133 struct async_state
*state
;
135 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
139 state
->map
.name
= DRIVER_NAME
;
140 state
->map
.read
= bfin_read
;
141 state
->map
.copy_from
= bfin_copy_from
;
142 state
->map
.write
= bfin_write
;
143 state
->map
.copy_to
= bfin_copy_to
;
144 state
->map
.bankwidth
= pdata
->width
;
145 state
->map
.size
= memory
->end
- memory
->start
+ 1;
146 state
->map
.virt
= (void __iomem
*)memory
->start
;
147 state
->map
.phys
= memory
->start
;
148 state
->map
.map_priv_1
= (unsigned long)state
;
149 state
->enet_flash_pin
= platform_get_irq(pdev
, 0);
150 state
->flash_ambctl0
= flash_ambctl
->start
;
151 state
->flash_ambctl1
= flash_ambctl
->end
;
153 if (gpio_request(state
->enet_flash_pin
, DRIVER_NAME
)) {
154 pr_devinit(KERN_ERR DRIVER_NAME
": Failed to request gpio %d\n", state
->enet_flash_pin
);
157 gpio_direction_output(state
->enet_flash_pin
, 1);
159 pr_devinit(KERN_NOTICE DRIVER_NAME
": probing %d-bit flash bus\n", state
->map
.bankwidth
* 8);
160 state
->mtd
= do_map_probe(memory
->name
, &state
->map
);
164 #ifdef CONFIG_MTD_PARTITIONS
165 ret
= parse_mtd_partitions(state
->mtd
, part_probe_types
, &pdata
->parts
, 0);
167 pr_devinit(KERN_NOTICE DRIVER_NAME
": Using commandline partition definition\n");
168 add_mtd_partitions(state
->mtd
, pdata
->parts
, ret
);
170 } else if (pdata
->nr_parts
) {
171 pr_devinit(KERN_NOTICE DRIVER_NAME
": Using board partition definition\n");
172 add_mtd_partitions(state
->mtd
, pdata
->parts
, pdata
->nr_parts
);
177 pr_devinit(KERN_NOTICE DRIVER_NAME
": no partition info available, registering whole flash at once\n");
178 add_mtd_device(state
->mtd
);
181 platform_set_drvdata(pdev
, state
);
186 static int __devexit
bfin_flash_remove(struct platform_device
*pdev
)
188 struct async_state
*state
= platform_get_drvdata(pdev
);
189 gpio_free(state
->enet_flash_pin
);
190 #ifdef CONFIG_MTD_PARTITIONS
191 del_mtd_partitions(state
->mtd
);
193 map_destroy(state
->mtd
);
198 static struct platform_driver bfin_flash_driver
= {
199 .probe
= bfin_flash_probe
,
200 .remove
= __devexit_p(bfin_flash_remove
),
206 static int __init
bfin_flash_init(void)
208 return platform_driver_register(&bfin_flash_driver
);
210 module_init(bfin_flash_init
);
212 static void __exit
bfin_flash_exit(void)
214 platform_driver_unregister(&bfin_flash_driver
);
216 module_exit(bfin_flash_exit
);
218 MODULE_LICENSE("GPL");
219 MODULE_DESCRIPTION("MTD map driver for Blackfins with flash/ethernet on same async bank");