bfin-async-flash.c 5.8 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@fluxnic.net>
  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/slab.h>
  25. #include <linux/types.h>
  26. #include <asm/blackfin.h>
  27. #include <linux/gpio.h>
  28. #include <linux/io.h>
  29. #include <asm/unaligned.h>
  30. #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); })
  31. #define DRIVER_NAME "bfin-async-flash"
  32. struct async_state {
  33. struct mtd_info *mtd;
  34. struct map_info map;
  35. int enet_flash_pin;
  36. uint32_t flash_ambctl0, flash_ambctl1;
  37. uint32_t save_ambctl0, save_ambctl1;
  38. unsigned long irq_flags;
  39. struct mtd_partition *parts;
  40. };
  41. static void switch_to_flash(struct async_state *state)
  42. {
  43. local_irq_save(state->irq_flags);
  44. gpio_set_value(state->enet_flash_pin, 0);
  45. state->save_ambctl0 = bfin_read_EBIU_AMBCTL0();
  46. state->save_ambctl1 = bfin_read_EBIU_AMBCTL1();
  47. bfin_write_EBIU_AMBCTL0(state->flash_ambctl0);
  48. bfin_write_EBIU_AMBCTL1(state->flash_ambctl1);
  49. SSYNC();
  50. }
  51. static void switch_back(struct async_state *state)
  52. {
  53. bfin_write_EBIU_AMBCTL0(state->save_ambctl0);
  54. bfin_write_EBIU_AMBCTL1(state->save_ambctl1);
  55. SSYNC();
  56. gpio_set_value(state->enet_flash_pin, 1);
  57. local_irq_restore(state->irq_flags);
  58. }
  59. static map_word bfin_flash_read(struct map_info *map, unsigned long ofs)
  60. {
  61. struct async_state *state = (struct async_state *)map->map_priv_1;
  62. uint16_t word;
  63. map_word test;
  64. switch_to_flash(state);
  65. word = readw(map->virt + ofs);
  66. switch_back(state);
  67. test.x[0] = word;
  68. return test;
  69. }
  70. static void bfin_flash_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  71. {
  72. struct async_state *state = (struct async_state *)map->map_priv_1;
  73. switch_to_flash(state);
  74. memcpy(to, map->virt + from, len);
  75. switch_back(state);
  76. }
  77. static void bfin_flash_write(struct map_info *map, map_word d1, unsigned long ofs)
  78. {
  79. struct async_state *state = (struct async_state *)map->map_priv_1;
  80. uint16_t d;
  81. d = d1.x[0];
  82. switch_to_flash(state);
  83. writew(d, map->virt + ofs);
  84. SSYNC();
  85. switch_back(state);
  86. }
  87. static void bfin_flash_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  88. {
  89. struct async_state *state = (struct async_state *)map->map_priv_1;
  90. switch_to_flash(state);
  91. memcpy(map->virt + to, from, len);
  92. SSYNC();
  93. switch_back(state);
  94. }
  95. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
  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_flash_read;
  108. state->map.copy_from = bfin_flash_copy_from;
  109. state->map.write = bfin_flash_write;
  110. state->map.copy_to = bfin_flash_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. kfree(state);
  122. return -EBUSY;
  123. }
  124. gpio_direction_output(state->enet_flash_pin, 1);
  125. pr_devinit(KERN_NOTICE DRIVER_NAME ": probing %d-bit flash bus\n", state->map.bankwidth * 8);
  126. state->mtd = do_map_probe(memory->name, &state->map);
  127. if (!state->mtd) {
  128. gpio_free(state->enet_flash_pin);
  129. kfree(state);
  130. return -ENXIO;
  131. }
  132. ret = parse_mtd_partitions(state->mtd, part_probe_types, &pdata->parts, 0);
  133. if (ret > 0) {
  134. pr_devinit(KERN_NOTICE DRIVER_NAME ": Using commandline partition definition\n");
  135. mtd_device_register(state->mtd, pdata->parts, ret);
  136. state->parts = pdata->parts;
  137. } else if (pdata->nr_parts) {
  138. pr_devinit(KERN_NOTICE DRIVER_NAME ": Using board partition definition\n");
  139. mtd_device_register(state->mtd, pdata->parts, pdata->nr_parts);
  140. } else {
  141. pr_devinit(KERN_NOTICE DRIVER_NAME ": no partition info available, registering whole flash at once\n");
  142. mtd_device_register(state->mtd, NULL, 0);
  143. }
  144. platform_set_drvdata(pdev, state);
  145. return 0;
  146. }
  147. static int __devexit bfin_flash_remove(struct platform_device *pdev)
  148. {
  149. struct async_state *state = platform_get_drvdata(pdev);
  150. gpio_free(state->enet_flash_pin);
  151. mtd_device_unregister(state->mtd);
  152. kfree(state->parts);
  153. map_destroy(state->mtd);
  154. kfree(state);
  155. return 0;
  156. }
  157. static struct platform_driver bfin_flash_driver = {
  158. .probe = bfin_flash_probe,
  159. .remove = __devexit_p(bfin_flash_remove),
  160. .driver = {
  161. .name = DRIVER_NAME,
  162. },
  163. };
  164. static int __init bfin_flash_init(void)
  165. {
  166. return platform_driver_register(&bfin_flash_driver);
  167. }
  168. module_init(bfin_flash_init);
  169. static void __exit bfin_flash_exit(void)
  170. {
  171. platform_driver_unregister(&bfin_flash_driver);
  172. }
  173. module_exit(bfin_flash_exit);
  174. MODULE_LICENSE("GPL");
  175. MODULE_DESCRIPTION("MTD map driver for Blackfins with flash/ethernet on same async bank");