| 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 | * | 
|  | 9 | * Copyright 2000 Nicolas Pitre <nico@cam.org> | 
|  | 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> | 
|  | 25 | #include <linux/types.h> | 
|  | 26 |  | 
|  | 27 | #include <asm/blackfin.h> | 
|  | 28 | #include <linux/gpio.h> | 
|  | 29 | #include <linux/io.h> | 
|  | 30 | #include <asm/unaligned.h> | 
|  | 31 |  | 
|  | 32 | #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); }) | 
|  | 33 |  | 
|  | 34 | #define DRIVER_NAME "bfin-async-flash" | 
|  | 35 |  | 
|  | 36 | struct async_state { | 
|  | 37 | struct mtd_info *mtd; | 
|  | 38 | struct map_info map; | 
|  | 39 | int enet_flash_pin; | 
|  | 40 | uint32_t flash_ambctl0, flash_ambctl1; | 
|  | 41 | uint32_t save_ambctl0, save_ambctl1; | 
|  | 42 | unsigned long irq_flags; | 
| Mike Frysinger | 4938c88 | 2009-06-02 00:06:23 -0400 | [diff] [blame] | 43 | #ifdef CONFIG_MTD_PARTITIONS | 
|  | 44 | struct mtd_partition *parts; | 
|  | 45 | #endif | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 46 | }; | 
|  | 47 |  | 
|  | 48 | static void switch_to_flash(struct async_state *state) | 
|  | 49 | { | 
|  | 50 | local_irq_save(state->irq_flags); | 
|  | 51 |  | 
|  | 52 | gpio_set_value(state->enet_flash_pin, 0); | 
|  | 53 |  | 
|  | 54 | state->save_ambctl0 = bfin_read_EBIU_AMBCTL0(); | 
|  | 55 | state->save_ambctl1 = bfin_read_EBIU_AMBCTL1(); | 
|  | 56 | bfin_write_EBIU_AMBCTL0(state->flash_ambctl0); | 
|  | 57 | bfin_write_EBIU_AMBCTL1(state->flash_ambctl1); | 
|  | 58 | SSYNC(); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | static void switch_back(struct async_state *state) | 
|  | 62 | { | 
|  | 63 | bfin_write_EBIU_AMBCTL0(state->save_ambctl0); | 
|  | 64 | bfin_write_EBIU_AMBCTL1(state->save_ambctl1); | 
|  | 65 | SSYNC(); | 
|  | 66 |  | 
|  | 67 | gpio_set_value(state->enet_flash_pin, 1); | 
|  | 68 |  | 
|  | 69 | local_irq_restore(state->irq_flags); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | static map_word bfin_read(struct map_info *map, unsigned long ofs) | 
|  | 73 | { | 
|  | 74 | struct async_state *state = (struct async_state *)map->map_priv_1; | 
|  | 75 | uint16_t word; | 
|  | 76 | map_word test; | 
|  | 77 |  | 
|  | 78 | switch_to_flash(state); | 
|  | 79 |  | 
|  | 80 | word = readw(map->virt + ofs); | 
|  | 81 |  | 
|  | 82 | switch_back(state); | 
|  | 83 |  | 
|  | 84 | test.x[0] = word; | 
|  | 85 | return test; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | static void bfin_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len) | 
|  | 89 | { | 
|  | 90 | struct async_state *state = (struct async_state *)map->map_priv_1; | 
|  | 91 |  | 
|  | 92 | switch_to_flash(state); | 
|  | 93 |  | 
|  | 94 | memcpy(to, map->virt + from, len); | 
|  | 95 |  | 
|  | 96 | switch_back(state); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | static void bfin_write(struct map_info *map, map_word d1, unsigned long ofs) | 
|  | 100 | { | 
|  | 101 | struct async_state *state = (struct async_state *)map->map_priv_1; | 
|  | 102 | uint16_t d; | 
|  | 103 |  | 
|  | 104 | d = d1.x[0]; | 
|  | 105 |  | 
|  | 106 | switch_to_flash(state); | 
|  | 107 |  | 
|  | 108 | writew(d, map->virt + ofs); | 
|  | 109 | SSYNC(); | 
|  | 110 |  | 
|  | 111 | switch_back(state); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | static void bfin_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len) | 
|  | 115 | { | 
|  | 116 | struct async_state *state = (struct async_state *)map->map_priv_1; | 
|  | 117 |  | 
|  | 118 | switch_to_flash(state); | 
|  | 119 |  | 
|  | 120 | memcpy(map->virt + to, from, len); | 
|  | 121 | SSYNC(); | 
|  | 122 |  | 
|  | 123 | switch_back(state); | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | #ifdef CONFIG_MTD_PARTITIONS | 
|  | 127 | static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL }; | 
|  | 128 | #endif | 
|  | 129 |  | 
|  | 130 | static int __devinit bfin_flash_probe(struct platform_device *pdev) | 
|  | 131 | { | 
|  | 132 | int ret; | 
|  | 133 | struct physmap_flash_data *pdata = pdev->dev.platform_data; | 
|  | 134 | struct resource *memory = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
|  | 135 | struct resource *flash_ambctl = platform_get_resource(pdev, IORESOURCE_MEM, 1); | 
|  | 136 | struct async_state *state; | 
|  | 137 |  | 
|  | 138 | state = kzalloc(sizeof(*state), GFP_KERNEL); | 
|  | 139 | if (!state) | 
|  | 140 | return -ENOMEM; | 
|  | 141 |  | 
|  | 142 | state->map.name       = DRIVER_NAME; | 
|  | 143 | state->map.read       = bfin_read; | 
|  | 144 | state->map.copy_from  = bfin_copy_from; | 
|  | 145 | state->map.write      = bfin_write; | 
|  | 146 | state->map.copy_to    = bfin_copy_to; | 
|  | 147 | state->map.bankwidth  = pdata->width; | 
|  | 148 | state->map.size       = memory->end - memory->start + 1; | 
|  | 149 | state->map.virt       = (void __iomem *)memory->start; | 
|  | 150 | state->map.phys       = memory->start; | 
|  | 151 | state->map.map_priv_1 = (unsigned long)state; | 
|  | 152 | state->enet_flash_pin = platform_get_irq(pdev, 0); | 
|  | 153 | state->flash_ambctl0  = flash_ambctl->start; | 
|  | 154 | state->flash_ambctl1  = flash_ambctl->end; | 
|  | 155 |  | 
|  | 156 | if (gpio_request(state->enet_flash_pin, DRIVER_NAME)) { | 
|  | 157 | 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] | 158 | kfree(state); | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 159 | return -EBUSY; | 
|  | 160 | } | 
|  | 161 | gpio_direction_output(state->enet_flash_pin, 1); | 
|  | 162 |  | 
|  | 163 | pr_devinit(KERN_NOTICE DRIVER_NAME ": probing %d-bit flash bus\n", state->map.bankwidth * 8); | 
|  | 164 | state->mtd = do_map_probe(memory->name, &state->map); | 
| Mike Frysinger | 10715b8 | 2009-02-11 13:12:18 -0800 | [diff] [blame] | 165 | if (!state->mtd) { | 
|  | 166 | gpio_free(state->enet_flash_pin); | 
|  | 167 | kfree(state); | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 168 | return -ENXIO; | 
| Mike Frysinger | 10715b8 | 2009-02-11 13:12:18 -0800 | [diff] [blame] | 169 | } | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 170 |  | 
|  | 171 | #ifdef CONFIG_MTD_PARTITIONS | 
|  | 172 | ret = parse_mtd_partitions(state->mtd, part_probe_types, &pdata->parts, 0); | 
|  | 173 | if (ret > 0) { | 
|  | 174 | pr_devinit(KERN_NOTICE DRIVER_NAME ": Using commandline partition definition\n"); | 
|  | 175 | add_mtd_partitions(state->mtd, pdata->parts, ret); | 
| Mike Frysinger | 4938c88 | 2009-06-02 00:06:23 -0400 | [diff] [blame] | 176 | state->parts = pdata->parts; | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 177 |  | 
|  | 178 | } else if (pdata->nr_parts) { | 
|  | 179 | pr_devinit(KERN_NOTICE DRIVER_NAME ": Using board partition definition\n"); | 
|  | 180 | add_mtd_partitions(state->mtd, pdata->parts, pdata->nr_parts); | 
|  | 181 |  | 
|  | 182 | } else | 
|  | 183 | #endif | 
|  | 184 | { | 
|  | 185 | pr_devinit(KERN_NOTICE DRIVER_NAME ": no partition info available, registering whole flash at once\n"); | 
|  | 186 | add_mtd_device(state->mtd); | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | platform_set_drvdata(pdev, state); | 
|  | 190 |  | 
|  | 191 | return 0; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | static int __devexit bfin_flash_remove(struct platform_device *pdev) | 
|  | 195 | { | 
|  | 196 | struct async_state *state = platform_get_drvdata(pdev); | 
|  | 197 | gpio_free(state->enet_flash_pin); | 
|  | 198 | #ifdef CONFIG_MTD_PARTITIONS | 
|  | 199 | del_mtd_partitions(state->mtd); | 
| Mike Frysinger | 4938c88 | 2009-06-02 00:06:23 -0400 | [diff] [blame] | 200 | kfree(state->parts); | 
| Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 201 | #endif | 
|  | 202 | map_destroy(state->mtd); | 
|  | 203 | kfree(state); | 
|  | 204 | return 0; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | static struct platform_driver bfin_flash_driver = { | 
|  | 208 | .probe		= bfin_flash_probe, | 
|  | 209 | .remove		= __devexit_p(bfin_flash_remove), | 
|  | 210 | .driver		= { | 
|  | 211 | .name	= DRIVER_NAME, | 
|  | 212 | }, | 
|  | 213 | }; | 
|  | 214 |  | 
|  | 215 | static int __init bfin_flash_init(void) | 
|  | 216 | { | 
|  | 217 | return platform_driver_register(&bfin_flash_driver); | 
|  | 218 | } | 
|  | 219 | module_init(bfin_flash_init); | 
|  | 220 |  | 
|  | 221 | static void __exit bfin_flash_exit(void) | 
|  | 222 | { | 
|  | 223 | platform_driver_unregister(&bfin_flash_driver); | 
|  | 224 | } | 
|  | 225 | module_exit(bfin_flash_exit); | 
|  | 226 |  | 
|  | 227 | MODULE_LICENSE("GPL"); | 
|  | 228 | MODULE_DESCRIPTION("MTD map driver for Blackfins with flash/ethernet on same async bank"); |