lantiq-flash.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
  7. * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/io.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/map.h>
  17. #include <linux/mtd/partitions.h>
  18. #include <linux/mtd/cfi.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mtd/physmap.h>
  21. #include <lantiq_soc.h>
  22. /*
  23. * The NOR flash is connected to the same external bus unit (EBU) as PCI.
  24. * To make PCI work we need to enable the endianness swapping for the address
  25. * written to the EBU. This endianness swapping works for PCI correctly but
  26. * fails for attached NOR devices. To workaround this we need to use a complex
  27. * map. The workaround involves swapping all addresses whilst probing the chip.
  28. * Once probing is complete we stop swapping the addresses but swizzle the
  29. * unlock addresses to ensure that access to the NOR device works correctly.
  30. */
  31. enum {
  32. LTQ_NOR_PROBING,
  33. LTQ_NOR_NORMAL
  34. };
  35. struct ltq_mtd {
  36. struct resource *res;
  37. struct mtd_info *mtd;
  38. struct map_info *map;
  39. };
  40. static const char ltq_map_name[] = "ltq_nor";
  41. static const char *ltq_probe_types[] __devinitconst = {
  42. "cmdlinepart", "ofpart", NULL };
  43. static map_word
  44. ltq_read16(struct map_info *map, unsigned long adr)
  45. {
  46. unsigned long flags;
  47. map_word temp;
  48. if (map->map_priv_1 == LTQ_NOR_PROBING)
  49. adr ^= 2;
  50. spin_lock_irqsave(&ebu_lock, flags);
  51. temp.x[0] = *(u16 *)(map->virt + adr);
  52. spin_unlock_irqrestore(&ebu_lock, flags);
  53. return temp;
  54. }
  55. static void
  56. ltq_write16(struct map_info *map, map_word d, unsigned long adr)
  57. {
  58. unsigned long flags;
  59. if (map->map_priv_1 == LTQ_NOR_PROBING)
  60. adr ^= 2;
  61. spin_lock_irqsave(&ebu_lock, flags);
  62. *(u16 *)(map->virt + adr) = d.x[0];
  63. spin_unlock_irqrestore(&ebu_lock, flags);
  64. }
  65. /*
  66. * The following 2 functions copy data between iomem and a cached memory
  67. * section. As memcpy() makes use of pre-fetching we cannot use it here.
  68. * The normal alternative of using memcpy_{to,from}io also makes use of
  69. * memcpy() on MIPS so it is not applicable either. We are therefore stuck
  70. * with having to use our own loop.
  71. */
  72. static void
  73. ltq_copy_from(struct map_info *map, void *to,
  74. unsigned long from, ssize_t len)
  75. {
  76. unsigned char *f = (unsigned char *)map->virt + from;
  77. unsigned char *t = (unsigned char *)to;
  78. unsigned long flags;
  79. spin_lock_irqsave(&ebu_lock, flags);
  80. while (len--)
  81. *t++ = *f++;
  82. spin_unlock_irqrestore(&ebu_lock, flags);
  83. }
  84. static void
  85. ltq_copy_to(struct map_info *map, unsigned long to,
  86. const void *from, ssize_t len)
  87. {
  88. unsigned char *f = (unsigned char *)from;
  89. unsigned char *t = (unsigned char *)map->virt + to;
  90. unsigned long flags;
  91. spin_lock_irqsave(&ebu_lock, flags);
  92. while (len--)
  93. *t++ = *f++;
  94. spin_unlock_irqrestore(&ebu_lock, flags);
  95. }
  96. static int __devinit
  97. ltq_mtd_probe(struct platform_device *pdev)
  98. {
  99. struct mtd_part_parser_data ppdata;
  100. struct ltq_mtd *ltq_mtd;
  101. struct cfi_private *cfi;
  102. int err;
  103. ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL);
  104. platform_set_drvdata(pdev, ltq_mtd);
  105. ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  106. if (!ltq_mtd->res) {
  107. dev_err(&pdev->dev, "failed to get memory resource\n");
  108. err = -ENOENT;
  109. goto err_out;
  110. }
  111. ltq_mtd->map = kzalloc(sizeof(struct map_info), GFP_KERNEL);
  112. ltq_mtd->map->phys = ltq_mtd->res->start;
  113. ltq_mtd->map->size = resource_size(ltq_mtd->res);
  114. ltq_mtd->map->virt = devm_request_and_ioremap(&pdev->dev, ltq_mtd->res);
  115. if (!ltq_mtd->map->virt) {
  116. dev_err(&pdev->dev, "failed to remap mem resource\n");
  117. err = -EBUSY;
  118. goto err_out;
  119. }
  120. ltq_mtd->map->name = ltq_map_name;
  121. ltq_mtd->map->bankwidth = 2;
  122. ltq_mtd->map->read = ltq_read16;
  123. ltq_mtd->map->write = ltq_write16;
  124. ltq_mtd->map->copy_from = ltq_copy_from;
  125. ltq_mtd->map->copy_to = ltq_copy_to;
  126. ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
  127. ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
  128. ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
  129. if (!ltq_mtd->mtd) {
  130. dev_err(&pdev->dev, "probing failed\n");
  131. err = -ENXIO;
  132. goto err_free;
  133. }
  134. ltq_mtd->mtd->owner = THIS_MODULE;
  135. cfi = ltq_mtd->map->fldrv_priv;
  136. cfi->addr_unlock1 ^= 1;
  137. cfi->addr_unlock2 ^= 1;
  138. ppdata.of_node = pdev->dev.of_node;
  139. err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types,
  140. &ppdata, NULL, 0);
  141. if (err) {
  142. dev_err(&pdev->dev, "failed to add partitions\n");
  143. goto err_destroy;
  144. }
  145. return 0;
  146. err_destroy:
  147. map_destroy(ltq_mtd->mtd);
  148. err_free:
  149. kfree(ltq_mtd->map);
  150. err_out:
  151. kfree(ltq_mtd);
  152. return err;
  153. }
  154. static int __devexit
  155. ltq_mtd_remove(struct platform_device *pdev)
  156. {
  157. struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
  158. if (ltq_mtd) {
  159. if (ltq_mtd->mtd) {
  160. mtd_device_unregister(ltq_mtd->mtd);
  161. map_destroy(ltq_mtd->mtd);
  162. }
  163. kfree(ltq_mtd->map);
  164. kfree(ltq_mtd);
  165. }
  166. return 0;
  167. }
  168. static const struct of_device_id ltq_mtd_match[] = {
  169. { .compatible = "lantiq,nor" },
  170. {},
  171. };
  172. MODULE_DEVICE_TABLE(of, ltq_mtd_match);
  173. static struct platform_driver ltq_mtd_driver = {
  174. .probe = ltq_mtd_probe,
  175. .remove = __devexit_p(ltq_mtd_remove),
  176. .driver = {
  177. .name = "ltq-nor",
  178. .owner = THIS_MODULE,
  179. .of_match_table = ltq_mtd_match,
  180. },
  181. };
  182. module_platform_driver(ltq_mtd_driver);
  183. MODULE_LICENSE("GPL");
  184. MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  185. MODULE_DESCRIPTION("Lantiq SoC NOR");