bfin-async-flash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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...) \
  31. ({ static const char __fmt[] = fmt; printk(__fmt, ## args); })
  32. #define DRIVER_NAME "bfin-async-flash"
  33. struct async_state {
  34. struct mtd_info *mtd;
  35. struct map_info map;
  36. int enet_flash_pin;
  37. uint32_t flash_ambctl0, flash_ambctl1;
  38. uint32_t save_ambctl0, save_ambctl1;
  39. unsigned long irq_flags;
  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 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 = resource_size(memory);
  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. mtd_device_parse_register(state->mtd, part_probe_types, NULL,
  133. pdata->parts, pdata->nr_parts);
  134. platform_set_drvdata(pdev, state);
  135. return 0;
  136. }
  137. static int bfin_flash_remove(struct platform_device *pdev)
  138. {
  139. struct async_state *state = platform_get_drvdata(pdev);
  140. gpio_free(state->enet_flash_pin);
  141. mtd_device_unregister(state->mtd);
  142. map_destroy(state->mtd);
  143. kfree(state);
  144. return 0;
  145. }
  146. static struct platform_driver bfin_flash_driver = {
  147. .probe = bfin_flash_probe,
  148. .remove = bfin_flash_remove,
  149. .driver = {
  150. .name = DRIVER_NAME,
  151. },
  152. };
  153. module_platform_driver(bfin_flash_driver);
  154. MODULE_LICENSE("GPL");
  155. MODULE_DESCRIPTION("MTD map driver for Blackfins with flash/ethernet on same async bank");