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 | |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/mtd/map.h> |
| 21 | #include <linux/mtd/partitions.h> |
| 22 | #include <linux/mtd/physmap.h> |
| 23 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 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 | |
Artem Bityutskiy | 7bf350b7 | 2012-11-22 12:16:28 +0200 | [diff] [blame] | 32 | #define pr_devinit(fmt, args...) \ |
| 33 | ({ static const char __fmt[] = fmt; printk(__fmt, ## args); }) |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 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; |
| 44 | }; |
| 45 | |
| 46 | static void switch_to_flash(struct async_state *state) |
| 47 | { |
| 48 | local_irq_save(state->irq_flags); |
| 49 | |
| 50 | gpio_set_value(state->enet_flash_pin, 0); |
| 51 | |
| 52 | state->save_ambctl0 = bfin_read_EBIU_AMBCTL0(); |
| 53 | state->save_ambctl1 = bfin_read_EBIU_AMBCTL1(); |
| 54 | bfin_write_EBIU_AMBCTL0(state->flash_ambctl0); |
| 55 | bfin_write_EBIU_AMBCTL1(state->flash_ambctl1); |
| 56 | SSYNC(); |
| 57 | } |
| 58 | |
| 59 | static void switch_back(struct async_state *state) |
| 60 | { |
| 61 | bfin_write_EBIU_AMBCTL0(state->save_ambctl0); |
| 62 | bfin_write_EBIU_AMBCTL1(state->save_ambctl1); |
| 63 | SSYNC(); |
| 64 | |
| 65 | gpio_set_value(state->enet_flash_pin, 1); |
| 66 | |
| 67 | local_irq_restore(state->irq_flags); |
| 68 | } |
| 69 | |
Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | struct async_state *state = (struct async_state *)map->map_priv_1; |
| 73 | uint16_t word; |
| 74 | map_word test; |
| 75 | |
| 76 | switch_to_flash(state); |
| 77 | |
| 78 | word = readw(map->virt + ofs); |
| 79 | |
| 80 | switch_back(state); |
| 81 | |
| 82 | test.x[0] = word; |
| 83 | return test; |
| 84 | } |
| 85 | |
Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 86 | 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] | 87 | { |
| 88 | struct async_state *state = (struct async_state *)map->map_priv_1; |
| 89 | |
| 90 | switch_to_flash(state); |
| 91 | |
| 92 | memcpy(to, map->virt + from, len); |
| 93 | |
| 94 | switch_back(state); |
| 95 | } |
| 96 | |
Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 97 | 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] | 98 | { |
| 99 | struct async_state *state = (struct async_state *)map->map_priv_1; |
| 100 | uint16_t d; |
| 101 | |
| 102 | d = d1.x[0]; |
| 103 | |
| 104 | switch_to_flash(state); |
| 105 | |
| 106 | writew(d, map->virt + ofs); |
| 107 | SSYNC(); |
| 108 | |
| 109 | switch_back(state); |
| 110 | } |
| 111 | |
Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 112 | 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] | 113 | { |
| 114 | struct async_state *state = (struct async_state *)map->map_priv_1; |
| 115 | |
| 116 | switch_to_flash(state); |
| 117 | |
| 118 | memcpy(map->virt + to, from, len); |
| 119 | SSYNC(); |
| 120 | |
| 121 | switch_back(state); |
| 122 | } |
| 123 | |
Artem Bityutskiy | 0984c89 | 2013-03-12 10:46:37 +0200 | [diff] [blame] | 124 | static const char * const part_probe_types[] = { |
| 125 | "cmdlinepart", "RedBoot", NULL }; |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 126 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 127 | static int bfin_flash_probe(struct platform_device *pdev) |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 128 | { |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 129 | struct physmap_flash_data *pdata = dev_get_platdata(&pdev->dev); |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 130 | struct resource *memory = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 131 | struct resource *flash_ambctl = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 132 | struct async_state *state; |
| 133 | |
| 134 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 135 | if (!state) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | state->map.name = DRIVER_NAME; |
Mike Frysinger | 4335c10 | 2010-01-17 10:52:46 -0500 | [diff] [blame] | 139 | state->map.read = bfin_flash_read; |
| 140 | state->map.copy_from = bfin_flash_copy_from; |
| 141 | state->map.write = bfin_flash_write; |
| 142 | state->map.copy_to = bfin_flash_copy_to; |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 143 | state->map.bankwidth = pdata->width; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 144 | state->map.size = resource_size(memory); |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 145 | state->map.virt = (void __iomem *)memory->start; |
| 146 | state->map.phys = memory->start; |
| 147 | state->map.map_priv_1 = (unsigned long)state; |
| 148 | state->enet_flash_pin = platform_get_irq(pdev, 0); |
| 149 | state->flash_ambctl0 = flash_ambctl->start; |
| 150 | state->flash_ambctl1 = flash_ambctl->end; |
| 151 | |
| 152 | if (gpio_request(state->enet_flash_pin, DRIVER_NAME)) { |
| 153 | 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] | 154 | kfree(state); |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 155 | return -EBUSY; |
| 156 | } |
| 157 | gpio_direction_output(state->enet_flash_pin, 1); |
| 158 | |
| 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); |
Mike Frysinger | 10715b8 | 2009-02-11 13:12:18 -0800 | [diff] [blame] | 161 | if (!state->mtd) { |
| 162 | gpio_free(state->enet_flash_pin); |
| 163 | kfree(state); |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 164 | return -ENXIO; |
Mike Frysinger | 10715b8 | 2009-02-11 13:12:18 -0800 | [diff] [blame] | 165 | } |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 166 | |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 167 | mtd_device_parse_register(state->mtd, part_probe_types, NULL, |
| 168 | pdata->parts, pdata->nr_parts); |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 169 | |
| 170 | platform_set_drvdata(pdev, state); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 175 | static int bfin_flash_remove(struct platform_device *pdev) |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 176 | { |
| 177 | struct async_state *state = platform_get_drvdata(pdev); |
| 178 | gpio_free(state->enet_flash_pin); |
Jamie Iles | fee88c5 | 2011-05-23 10:22:52 +0100 | [diff] [blame] | 179 | mtd_device_unregister(state->mtd); |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 180 | map_destroy(state->mtd); |
| 181 | kfree(state); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static struct platform_driver bfin_flash_driver = { |
| 186 | .probe = bfin_flash_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 187 | .remove = bfin_flash_remove, |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 188 | .driver = { |
| 189 | .name = DRIVER_NAME, |
| 190 | }, |
| 191 | }; |
| 192 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 193 | module_platform_driver(bfin_flash_driver); |
Mike Frysinger | 2e3c22f | 2008-05-19 18:32:24 +0800 | [diff] [blame] | 194 | |
| 195 | MODULE_LICENSE("GPL"); |
| 196 | MODULE_DESCRIPTION("MTD map driver for Blackfins with flash/ethernet on same async bank"); |