| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * drivers/mtd/maps/bfin-async-flash.c | 
 | 3 |  * | 
 | 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. | 
 | 8 |  * | 
| Nicolas Pitre | 2f82af0 | 2009-09-14 03:25:28 -0400 | [diff] [blame] | 9 |  * Copyright 2000 Nicolas Pitre <nico@fluxnic.net> | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 10 |  * Copyright 2005-2008 Analog Devices Inc. | 
 | 11 |  * | 
 | 12 |  * Enter bugs at http://blackfin.uclinux.org/ | 
 | 13 |  * | 
 | 14 |  * Licensed under the GPL-2 or later. | 
 | 15 |  */ | 
 | 16 |  | 
 | 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> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 26 | #include <linux/types.h> | 
 | 27 |  | 
 | 28 | #include <asm/blackfin.h> | 
 | 29 | #include <linux/gpio.h> | 
 | 30 | #include <linux/io.h> | 
 | 31 | #include <asm/unaligned.h> | 
 | 32 |  | 
 | 33 | #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); }) | 
 | 34 |  | 
 | 35 | #define DRIVER_NAME "bfin-async-flash" | 
 | 36 |  | 
 | 37 | struct async_state { | 
 | 38 | 	struct mtd_info *mtd; | 
 | 39 | 	struct map_info map; | 
 | 40 | 	int enet_flash_pin; | 
 | 41 | 	uint32_t flash_ambctl0, flash_ambctl1; | 
 | 42 | 	uint32_t save_ambctl0, save_ambctl1; | 
 | 43 | 	unsigned long irq_flags; | 
| Mike Frysinger | 4938c88 | 2009-06-02 00:06:23 -0400 | [diff] [blame] | 44 | 	struct mtd_partition *parts; | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 45 | }; | 
 | 46 |  | 
 | 47 | static void switch_to_flash(struct async_state *state) | 
 | 48 | { | 
 | 49 | 	local_irq_save(state->irq_flags); | 
 | 50 |  | 
 | 51 | 	gpio_set_value(state->enet_flash_pin, 0); | 
 | 52 |  | 
 | 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); | 
 | 57 | 	SSYNC(); | 
 | 58 | } | 
 | 59 |  | 
 | 60 | static void switch_back(struct async_state *state) | 
 | 61 | { | 
 | 62 | 	bfin_write_EBIU_AMBCTL0(state->save_ambctl0); | 
 | 63 | 	bfin_write_EBIU_AMBCTL1(state->save_ambctl1); | 
 | 64 | 	SSYNC(); | 
 | 65 |  | 
 | 66 | 	gpio_set_value(state->enet_flash_pin, 1); | 
 | 67 |  | 
 | 68 | 	local_irq_restore(state->irq_flags); | 
 | 69 | } | 
 | 70 |  | 
| Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 71 | static map_word bfin_flash_read(struct map_info *map, unsigned long ofs) | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 72 | { | 
 | 73 | 	struct async_state *state = (struct async_state *)map->map_priv_1; | 
 | 74 | 	uint16_t word; | 
 | 75 | 	map_word test; | 
 | 76 |  | 
 | 77 | 	switch_to_flash(state); | 
 | 78 |  | 
 | 79 | 	word = readw(map->virt + ofs); | 
 | 80 |  | 
 | 81 | 	switch_back(state); | 
 | 82 |  | 
 | 83 | 	test.x[0] = word; | 
 | 84 | 	return test; | 
 | 85 | } | 
 | 86 |  | 
| Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 87 | static void bfin_flash_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len) | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 88 | { | 
 | 89 | 	struct async_state *state = (struct async_state *)map->map_priv_1; | 
 | 90 |  | 
 | 91 | 	switch_to_flash(state); | 
 | 92 |  | 
 | 93 | 	memcpy(to, map->virt + from, len); | 
 | 94 |  | 
 | 95 | 	switch_back(state); | 
 | 96 | } | 
 | 97 |  | 
| Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 98 | static void bfin_flash_write(struct map_info *map, map_word d1, unsigned long ofs) | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 99 | { | 
 | 100 | 	struct async_state *state = (struct async_state *)map->map_priv_1; | 
 | 101 | 	uint16_t d; | 
 | 102 |  | 
 | 103 | 	d = d1.x[0]; | 
 | 104 |  | 
 | 105 | 	switch_to_flash(state); | 
 | 106 |  | 
 | 107 | 	writew(d, map->virt + ofs); | 
 | 108 | 	SSYNC(); | 
 | 109 |  | 
 | 110 | 	switch_back(state); | 
 | 111 | } | 
 | 112 |  | 
| Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 113 | static void bfin_flash_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len) | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 114 | { | 
 | 115 | 	struct async_state *state = (struct async_state *)map->map_priv_1; | 
 | 116 |  | 
 | 117 | 	switch_to_flash(state); | 
 | 118 |  | 
 | 119 | 	memcpy(map->virt + to, from, len); | 
 | 120 | 	SSYNC(); | 
 | 121 |  | 
 | 122 | 	switch_back(state); | 
 | 123 | } | 
 | 124 |  | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 125 | static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL }; | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 126 |  | 
 | 127 | static int __devinit bfin_flash_probe(struct platform_device *pdev) | 
 | 128 | { | 
 | 129 | 	int ret; | 
 | 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; | 
 | 134 |  | 
 | 135 | 	state = kzalloc(sizeof(*state), GFP_KERNEL); | 
 | 136 | 	if (!state) | 
 | 137 | 		return -ENOMEM; | 
 | 138 |  | 
 | 139 | 	state->map.name       = DRIVER_NAME; | 
| Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 140 | 	state->map.read       = bfin_flash_read; | 
 | 141 | 	state->map.copy_from  = bfin_flash_copy_from; | 
 | 142 | 	state->map.write      = bfin_flash_write; | 
 | 143 | 	state->map.copy_to    = bfin_flash_copy_to; | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 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; | 
 | 152 |  | 
 | 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); | 
| Mike Frysinger | 10715b8 | 2009-02-11 13:12:18 -0800 | [diff] [blame] | 155 | 		kfree(state); | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 156 | 		return -EBUSY; | 
 | 157 | 	} | 
 | 158 | 	gpio_direction_output(state->enet_flash_pin, 1); | 
 | 159 |  | 
 | 160 | 	pr_devinit(KERN_NOTICE DRIVER_NAME ": probing %d-bit flash bus\n", state->map.bankwidth * 8); | 
 | 161 | 	state->mtd = do_map_probe(memory->name, &state->map); | 
| Mike Frysinger | 10715b8 | 2009-02-11 13:12:18 -0800 | [diff] [blame] | 162 | 	if (!state->mtd) { | 
 | 163 | 		gpio_free(state->enet_flash_pin); | 
 | 164 | 		kfree(state); | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 165 | 		return -ENXIO; | 
| Mike Frysinger | 10715b8 | 2009-02-11 13:12:18 -0800 | [diff] [blame] | 166 | 	} | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 167 |  | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 168 | 	ret = parse_mtd_partitions(state->mtd, part_probe_types, &pdata->parts, 0); | 
 | 169 | 	if (ret > 0) { | 
 | 170 | 		pr_devinit(KERN_NOTICE DRIVER_NAME ": Using commandline partition definition\n"); | 
| Jamie Iles | fee88c5 | 2011-05-23 10:22:52 +0100 | [diff] [blame] | 171 | 		mtd_device_register(state->mtd, pdata->parts, ret); | 
| Mike Frysinger | 4938c88 | 2009-06-02 00:06:23 -0400 | [diff] [blame] | 172 | 		state->parts = pdata->parts; | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 173 | 	} else if (pdata->nr_parts) { | 
 | 174 | 		pr_devinit(KERN_NOTICE DRIVER_NAME ": Using board partition definition\n"); | 
| Jamie Iles | fee88c5 | 2011-05-23 10:22:52 +0100 | [diff] [blame] | 175 | 		mtd_device_register(state->mtd, pdata->parts, pdata->nr_parts); | 
 | 176 | 	} else { | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 177 | 		pr_devinit(KERN_NOTICE DRIVER_NAME ": no partition info available, registering whole flash at once\n"); | 
| Jamie Iles | fee88c5 | 2011-05-23 10:22:52 +0100 | [diff] [blame] | 178 | 		mtd_device_register(state->mtd, NULL, 0); | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 179 | 	} | 
 | 180 |  | 
 | 181 | 	platform_set_drvdata(pdev, state); | 
 | 182 |  | 
 | 183 | 	return 0; | 
 | 184 | } | 
 | 185 |  | 
 | 186 | static int __devexit bfin_flash_remove(struct platform_device *pdev) | 
 | 187 | { | 
 | 188 | 	struct async_state *state = platform_get_drvdata(pdev); | 
 | 189 | 	gpio_free(state->enet_flash_pin); | 
| Jamie Iles | fee88c5 | 2011-05-23 10:22:52 +0100 | [diff] [blame] | 190 | 	mtd_device_unregister(state->mtd); | 
| Mike Frysinger | 4938c88 | 2009-06-02 00:06:23 -0400 | [diff] [blame] | 191 | 	kfree(state->parts); | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 192 | 	map_destroy(state->mtd); | 
 | 193 | 	kfree(state); | 
 | 194 | 	return 0; | 
 | 195 | } | 
 | 196 |  | 
 | 197 | static struct platform_driver bfin_flash_driver = { | 
 | 198 | 	.probe		= bfin_flash_probe, | 
 | 199 | 	.remove		= __devexit_p(bfin_flash_remove), | 
 | 200 | 	.driver		= { | 
 | 201 | 		.name	= DRIVER_NAME, | 
 | 202 | 	}, | 
 | 203 | }; | 
 | 204 |  | 
 | 205 | static int __init bfin_flash_init(void) | 
 | 206 | { | 
 | 207 | 	return platform_driver_register(&bfin_flash_driver); | 
 | 208 | } | 
 | 209 | module_init(bfin_flash_init); | 
 | 210 |  | 
 | 211 | static void __exit bfin_flash_exit(void) | 
 | 212 | { | 
 | 213 | 	platform_driver_unregister(&bfin_flash_driver); | 
 | 214 | } | 
 | 215 | module_exit(bfin_flash_exit); | 
 | 216 |  | 
 | 217 | MODULE_LICENSE("GPL"); | 
 | 218 | MODULE_DESCRIPTION("MTD map driver for Blackfins with flash/ethernet on same async bank"); |