lantiq-flash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <[email protected]>
  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->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  104. if (!ltq_mtd->res) {
  105. dev_err(&pdev->dev, "failed to get memory resource\n");
  106. return -ENOENT;
  107. }
  108. ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
  109. GFP_KERNEL);
  110. if (!ltq_mtd->map)
  111. return -ENOMEM;
  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_ioremap_resource(&pdev->dev, ltq_mtd->res);
  115. if (IS_ERR(ltq_mtd->map->virt))
  116. return PTR_ERR(ltq_mtd->map->virt);
  117. ltq_mtd->map->name = ltq_map_name;
  118. ltq_mtd->map->bankwidth = 2;
  119. ltq_mtd->map->read = ltq_read16;
  120. ltq_mtd->map->write = ltq_write16;
  121. ltq_mtd->map->copy_from = ltq_copy_from;
  122. ltq_mtd->map->copy_to = ltq_copy_to;
  123. ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
  124. ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
  125. ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
  126. if (!ltq_mtd->mtd) {
  127. dev_err(&pdev->dev, "probing failed\n");
  128. return -ENXIO;
  129. }
  130. ltq_mtd->mtd->dev.parent = &pdev->dev;
  131. mtd_set_of_node(ltq_mtd->mtd, pdev->dev.of_node);
  132. cfi = ltq_mtd->map->fldrv_priv;
  133. cfi->addr_unlock1 ^= 1;
  134. cfi->addr_unlock2 ^= 1;
  135. err = mtd_device_register(ltq_mtd->mtd, NULL, 0);
  136. if (err) {
  137. dev_err(&pdev->dev, "failed to add partitions\n");
  138. goto err_destroy;
  139. }
  140. return 0;
  141. err_destroy:
  142. map_destroy(ltq_mtd->mtd);
  143. return err;
  144. }
  145. static int
  146. ltq_mtd_remove(struct platform_device *pdev)
  147. {
  148. struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
  149. if (ltq_mtd && ltq_mtd->mtd) {
  150. mtd_device_unregister(ltq_mtd->mtd);
  151. map_destroy(ltq_mtd->mtd);
  152. }
  153. return 0;
  154. }
  155. static const struct of_device_id ltq_mtd_match[] = {
  156. { .compatible = "lantiq,nor" },
  157. {},
  158. };
  159. MODULE_DEVICE_TABLE(of, ltq_mtd_match);
  160. static struct platform_driver ltq_mtd_driver = {
  161. .probe = ltq_mtd_probe,
  162. .remove = ltq_mtd_remove,
  163. .driver = {
  164. .name = "ltq-nor",
  165. .of_match_table = ltq_mtd_match,
  166. },
  167. };
  168. module_platform_driver(ltq_mtd_driver);
  169. MODULE_LICENSE("GPL");
  170. MODULE_AUTHOR("John Crispin <[email protected]>");
  171. MODULE_DESCRIPTION("Lantiq SoC NOR");