bfin-async-flash.c 5.8 KB

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