lantiq-flash.c 4.9 KB

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