bfin-async-flash.c 5.6 KB

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