lantiq-flash.c 5.6 KB

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