PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/mtd/maps/bfin-async-flash.c

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