lantiq-flash.c 6.2 KB

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