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/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 <linux/of.h>
  22. #include <lantiq_soc.h>
  23. /*
  24. * The NOR flash is connected to the same external bus unit (EBU) as PCI.
  25. * To make PCI work we need to enable the endianness swapping for the address
  26. * written to the EBU. This endianness swapping works for PCI correctly but
  27. * fails for attached NOR devices. To workaround this we need to use a complex
  28. * map. The workaround involves swapping all addresses whilst probing the chip.
  29. * Once probing is complete we stop swapping the addresses but swizzle the
  30. * unlock addresses to ensure that access to the NOR device works correctly.
  31. */
  32. enum {
  33. LTQ_NOR_PROBING,
  34. LTQ_NOR_NORMAL
  35. };
  36. struct ltq_mtd {
  37. struct resource *res;
  38. struct mtd_info *mtd;
  39. struct map_info *map;
  40. };
  41. static const char ltq_map_name[] = "ltq_nor";
  42. static const char *ltq_probe_types[] __devinitconst = {
  43. "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 __devinit
  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_request_and_ioremap(&pdev->dev, ltq_mtd->res);
  121. if (!ltq_mtd->map->virt) {
  122. dev_err(&pdev->dev, "failed to remap mem resource\n");
  123. err = -EBUSY;
  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 __devexit
  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 = __devexit_p(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");