bfin-async-flash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/map.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/mtd/physmap.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.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...) \
  30. ({ static const 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. };
  40. static void switch_to_flash(struct async_state *state)
  41. {
  42. local_irq_save(state->irq_flags);
  43. gpio_set_value(state->enet_flash_pin, 0);
  44. state->save_ambctl0 = bfin_read_EBIU_AMBCTL0();
  45. state->save_ambctl1 = bfin_read_EBIU_AMBCTL1();
  46. bfin_write_EBIU_AMBCTL0(state->flash_ambctl0);
  47. bfin_write_EBIU_AMBCTL1(state->flash_ambctl1);
  48. SSYNC();
  49. }
  50. static void switch_back(struct async_state *state)
  51. {
  52. bfin_write_EBIU_AMBCTL0(state->save_ambctl0);
  53. bfin_write_EBIU_AMBCTL1(state->save_ambctl1);
  54. SSYNC();
  55. gpio_set_value(state->enet_flash_pin, 1);
  56. local_irq_restore(state->irq_flags);
  57. }
  58. static map_word bfin_flash_read(struct map_info *map, unsigned long ofs)
  59. {
  60. struct async_state *state = (struct async_state *)map->map_priv_1;
  61. uint16_t word;
  62. map_word test;
  63. switch_to_flash(state);
  64. word = readw(map->virt + ofs);
  65. switch_back(state);
  66. test.x[0] = word;
  67. return test;
  68. }
  69. static void bfin_flash_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  70. {
  71. struct async_state *state = (struct async_state *)map->map_priv_1;
  72. switch_to_flash(state);
  73. memcpy(to, map->virt + from, len);
  74. switch_back(state);
  75. }
  76. static void bfin_flash_write(struct map_info *map, map_word d1, unsigned long ofs)
  77. {
  78. struct async_state *state = (struct async_state *)map->map_priv_1;
  79. uint16_t d;
  80. d = d1.x[0];
  81. switch_to_flash(state);
  82. writew(d, map->virt + ofs);
  83. SSYNC();
  84. switch_back(state);
  85. }
  86. static void bfin_flash_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  87. {
  88. struct async_state *state = (struct async_state *)map->map_priv_1;
  89. switch_to_flash(state);
  90. memcpy(map->virt + to, from, len);
  91. SSYNC();
  92. switch_back(state);
  93. }
  94. static const char * const part_probe_types[] = {
  95. "cmdlinepart", "RedBoot", NULL };
  96. static int bfin_flash_probe(struct platform_device *pdev)
  97. {
  98. struct physmap_flash_data *pdata = dev_get_platdata(&pdev->dev);
  99. struct resource *memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  100. struct resource *flash_ambctl = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  101. struct async_state *state;
  102. state = kzalloc(sizeof(*state), GFP_KERNEL);
  103. if (!state)
  104. return -ENOMEM;
  105. state->map.name = DRIVER_NAME;
  106. state->map.read = bfin_flash_read;
  107. state->map.copy_from = bfin_flash_copy_from;
  108. state->map.write = bfin_flash_write;
  109. state->map.copy_to = bfin_flash_copy_to;
  110. state->map.bankwidth = pdata->width;
  111. state->map.size = resource_size(memory);
  112. state->map.virt = (void __iomem *)memory->start;
  113. state->map.phys = memory->start;
  114. state->map.map_priv_1 = (unsigned long)state;
  115. state->enet_flash_pin = platform_get_irq(pdev, 0);
  116. state->flash_ambctl0 = flash_ambctl->start;
  117. state->flash_ambctl1 = flash_ambctl->end;
  118. if (gpio_request(state->enet_flash_pin, DRIVER_NAME)) {
  119. pr_devinit(KERN_ERR DRIVER_NAME ": Failed to request gpio %d\n", state->enet_flash_pin);
  120. kfree(state);
  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. gpio_free(state->enet_flash_pin);
  128. kfree(state);
  129. return -ENXIO;
  130. }
  131. mtd_device_parse_register(state->mtd, part_probe_types, NULL,
  132. pdata->parts, pdata->nr_parts);
  133. platform_set_drvdata(pdev, state);
  134. return 0;
  135. }
  136. static int bfin_flash_remove(struct platform_device *pdev)
  137. {
  138. struct async_state *state = platform_get_drvdata(pdev);
  139. gpio_free(state->enet_flash_pin);
  140. mtd_device_unregister(state->mtd);
  141. map_destroy(state->mtd);
  142. kfree(state);
  143. return 0;
  144. }
  145. static struct platform_driver bfin_flash_driver = {
  146. .probe = bfin_flash_probe,
  147. .remove = bfin_flash_remove,
  148. .driver = {
  149. .name = DRIVER_NAME,
  150. },
  151. };
  152. module_platform_driver(bfin_flash_driver);
  153. MODULE_LICENSE("GPL");
  154. MODULE_DESCRIPTION("MTD map driver for Blackfins with flash/ethernet on same async bank");