lantiq-flash.c 5.8 KB

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