lantiq-flash.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 * const ltq_probe_types[] = { "cmdlinepart", "ofpart", NULL };
  44. static map_word
  45. ltq_read16(struct map_info *map, unsigned long adr)
  46. {
  47. unsigned long flags;
  48. map_word temp;
  49. if (map->map_priv_1 == LTQ_NOR_PROBING)
  50. adr ^= 2;
  51. spin_lock_irqsave(&ebu_lock, flags);
  52. temp.x[0] = *(u16 *)(map->virt + adr);
  53. spin_unlock_irqrestore(&ebu_lock, flags);
  54. return temp;
  55. }
  56. static void
  57. ltq_write16(struct map_info *map, map_word d, unsigned long adr)
  58. {
  59. unsigned long flags;
  60. if (map->map_priv_1 == LTQ_NOR_PROBING)
  61. adr ^= 2;
  62. spin_lock_irqsave(&ebu_lock, flags);
  63. *(u16 *)(map->virt + adr) = d.x[0];
  64. spin_unlock_irqrestore(&ebu_lock, flags);
  65. }
  66. /*
  67. * The following 2 functions copy data between iomem and a cached memory
  68. * section. As memcpy() makes use of pre-fetching we cannot use it here.
  69. * The normal alternative of using memcpy_{to,from}io also makes use of
  70. * memcpy() on MIPS so it is not applicable either. We are therefore stuck
  71. * with having to use our own loop.
  72. */
  73. static void
  74. ltq_copy_from(struct map_info *map, void *to,
  75. unsigned long from, ssize_t len)
  76. {
  77. unsigned char *f = (unsigned char *)map->virt + from;
  78. unsigned char *t = (unsigned char *)to;
  79. unsigned long flags;
  80. spin_lock_irqsave(&ebu_lock, flags);
  81. while (len--)
  82. *t++ = *f++;
  83. spin_unlock_irqrestore(&ebu_lock, flags);
  84. }
  85. static void
  86. ltq_copy_to(struct map_info *map, unsigned long to,
  87. const void *from, ssize_t len)
  88. {
  89. unsigned char *f = (unsigned char *)from;
  90. unsigned char *t = (unsigned char *)map->virt + to;
  91. unsigned long flags;
  92. spin_lock_irqsave(&ebu_lock, flags);
  93. while (len--)
  94. *t++ = *f++;
  95. spin_unlock_irqrestore(&ebu_lock, flags);
  96. }
  97. static int
  98. ltq_mtd_probe(struct platform_device *pdev)
  99. {
  100. struct mtd_part_parser_data ppdata;
  101. struct ltq_mtd *ltq_mtd;
  102. struct cfi_private *cfi;
  103. int err;
  104. if (of_machine_is_compatible("lantiq,falcon") &&
  105. (ltq_boot_select() != BS_FLASH)) {
  106. dev_err(&pdev->dev, "invalid bootstrap options\n");
  107. return -ENODEV;
  108. }
  109. ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL);
  110. platform_set_drvdata(pdev, ltq_mtd);
  111. ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. if (!ltq_mtd->res) {
  113. dev_err(&pdev->dev, "failed to get memory resource\n");
  114. err = -ENOENT;
  115. goto err_out;
  116. }
  117. ltq_mtd->map = kzalloc(sizeof(struct map_info), GFP_KERNEL);
  118. ltq_mtd->map->phys = ltq_mtd->res->start;
  119. ltq_mtd->map->size = resource_size(ltq_mtd->res);
  120. ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
  121. if (IS_ERR(ltq_mtd->map->virt)) {
  122. err = PTR_ERR(ltq_mtd->map->virt);
  123. goto err_out;
  124. }
  125. ltq_mtd->map->name = ltq_map_name;
  126. ltq_mtd->map->bankwidth = 2;
  127. ltq_mtd->map->read = ltq_read16;
  128. ltq_mtd->map->write = ltq_write16;
  129. ltq_mtd->map->copy_from = ltq_copy_from;
  130. ltq_mtd->map->copy_to = ltq_copy_to;
  131. ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
  132. ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
  133. ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
  134. if (!ltq_mtd->mtd) {
  135. dev_err(&pdev->dev, "probing failed\n");
  136. err = -ENXIO;
  137. goto err_free;
  138. }
  139. ltq_mtd->mtd->owner = THIS_MODULE;
  140. cfi = ltq_mtd->map->fldrv_priv;
  141. cfi->addr_unlock1 ^= 1;
  142. cfi->addr_unlock2 ^= 1;
  143. ppdata.of_node = pdev->dev.of_node;
  144. err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types,
  145. &ppdata, NULL, 0);
  146. if (err) {
  147. dev_err(&pdev->dev, "failed to add partitions\n");
  148. goto err_destroy;
  149. }
  150. return 0;
  151. err_destroy:
  152. map_destroy(ltq_mtd->mtd);
  153. err_free:
  154. kfree(ltq_mtd->map);
  155. err_out:
  156. kfree(ltq_mtd);
  157. return err;
  158. }
  159. static int
  160. ltq_mtd_remove(struct platform_device *pdev)
  161. {
  162. struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
  163. if (ltq_mtd) {
  164. if (ltq_mtd->mtd) {
  165. mtd_device_unregister(ltq_mtd->mtd);
  166. map_destroy(ltq_mtd->mtd);
  167. }
  168. kfree(ltq_mtd->map);
  169. kfree(ltq_mtd);
  170. }
  171. return 0;
  172. }
  173. static const struct of_device_id ltq_mtd_match[] = {
  174. { .compatible = "lantiq,nor" },
  175. {},
  176. };
  177. MODULE_DEVICE_TABLE(of, ltq_mtd_match);
  178. static struct platform_driver ltq_mtd_driver = {
  179. .probe = ltq_mtd_probe,
  180. .remove = ltq_mtd_remove,
  181. .driver = {
  182. .name = "ltq-nor",
  183. .owner = THIS_MODULE,
  184. .of_match_table = ltq_mtd_match,
  185. },
  186. };
  187. module_platform_driver(ltq_mtd_driver);
  188. MODULE_LICENSE("GPL");
  189. MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  190. MODULE_DESCRIPTION("Lantiq SoC NOR");