ixp2000.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * drivers/mtd/maps/ixp2000.c
  3. *
  4. * Mapping for the Intel XScale IXP2000 based systems
  5. *
  6. * Copyright (C) 2002 Intel Corp.
  7. * Copyright (C) 2003-2004 MontaVista Software, Inc.
  8. *
  9. * Original Author: Naeem M Afzal <naeem.m.afzal@intel.com>
  10. * Maintainer: Deepak Saxena <dsaxena@plexity.net>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/slab.h>
  23. #include <linux/ioport.h>
  24. #include <linux/device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/map.h>
  28. #include <linux/mtd/partitions.h>
  29. #include <asm/io.h>
  30. #include <mach/hardware.h>
  31. #include <asm/mach/flash.h>
  32. #include <linux/reboot.h>
  33. struct ixp2000_flash_info {
  34. struct mtd_info *mtd;
  35. struct map_info map;
  36. struct resource *res;
  37. };
  38. static inline unsigned long flash_bank_setup(struct map_info *map, unsigned long ofs)
  39. {
  40. unsigned long (*set_bank)(unsigned long) =
  41. (unsigned long(*)(unsigned long))map->map_priv_2;
  42. return (set_bank ? set_bank(ofs) : ofs);
  43. }
  44. #ifdef __ARMEB__
  45. /*
  46. * Rev A0 and A1 of IXP2400 silicon have a broken addressing unit which
  47. * causes the lower address bits to be XORed with 0x11 on 8 bit accesses
  48. * and XORed with 0x10 on 16 bit accesses. See the spec update, erratum 44.
  49. */
  50. static int erratum44_workaround = 0;
  51. static inline unsigned long address_fix8_write(unsigned long addr)
  52. {
  53. if (erratum44_workaround) {
  54. return (addr ^ 3);
  55. }
  56. return addr;
  57. }
  58. #else
  59. #define address_fix8_write(x) (x)
  60. #endif
  61. static map_word ixp2000_flash_read8(struct map_info *map, unsigned long ofs)
  62. {
  63. map_word val;
  64. val.x[0] = *((u8 *)(map->map_priv_1 + flash_bank_setup(map, ofs)));
  65. return val;
  66. }
  67. /*
  68. * We can't use the standard memcpy due to the broken SlowPort
  69. * address translation on rev A0 and A1 silicon and the fact that
  70. * we have banked flash.
  71. */
  72. static void ixp2000_flash_copy_from(struct map_info *map, void *to,
  73. unsigned long from, ssize_t len)
  74. {
  75. from = flash_bank_setup(map, from);
  76. while(len--)
  77. *(__u8 *) to++ = *(__u8 *)(map->map_priv_1 + from++);
  78. }
  79. static void ixp2000_flash_write8(struct map_info *map, map_word d, unsigned long ofs)
  80. {
  81. *(__u8 *) (address_fix8_write(map->map_priv_1 +
  82. flash_bank_setup(map, ofs))) = d.x[0];
  83. }
  84. static void ixp2000_flash_copy_to(struct map_info *map, unsigned long to,
  85. const void *from, ssize_t len)
  86. {
  87. to = flash_bank_setup(map, to);
  88. while(len--) {
  89. unsigned long tmp = address_fix8_write(map->map_priv_1 + to++);
  90. *(__u8 *)(tmp) = *(__u8 *)(from++);
  91. }
  92. }
  93. static int ixp2000_flash_remove(struct platform_device *dev)
  94. {
  95. struct flash_platform_data *plat = dev->dev.platform_data;
  96. struct ixp2000_flash_info *info = platform_get_drvdata(dev);
  97. platform_set_drvdata(dev, NULL);
  98. if(!info)
  99. return 0;
  100. if (info->mtd) {
  101. mtd_device_unregister(info->mtd);
  102. map_destroy(info->mtd);
  103. }
  104. if (info->map.map_priv_1)
  105. iounmap((void *) info->map.map_priv_1);
  106. if (info->res) {
  107. release_resource(info->res);
  108. kfree(info->res);
  109. }
  110. if (plat->exit)
  111. plat->exit();
  112. return 0;
  113. }
  114. static int ixp2000_flash_probe(struct platform_device *dev)
  115. {
  116. static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
  117. struct ixp2000_flash_data *ixp_data = dev->dev.platform_data;
  118. struct flash_platform_data *plat;
  119. struct ixp2000_flash_info *info;
  120. unsigned long window_size;
  121. int err = -1;
  122. if (!ixp_data)
  123. return -ENODEV;
  124. plat = ixp_data->platform_data;
  125. if (!plat)
  126. return -ENODEV;
  127. window_size = resource_size(dev->resource);
  128. dev_info(&dev->dev, "Probe of IXP2000 flash(%d banks x %dMiB)\n",
  129. ixp_data->nr_banks, ((u32)window_size >> 20));
  130. if (plat->width != 1) {
  131. dev_err(&dev->dev, "IXP2000 MTD map only supports 8-bit mode, asking for %d\n",
  132. plat->width * 8);
  133. return -EIO;
  134. }
  135. info = kzalloc(sizeof(struct ixp2000_flash_info), GFP_KERNEL);
  136. if(!info) {
  137. err = -ENOMEM;
  138. goto Error;
  139. }
  140. platform_set_drvdata(dev, info);
  141. /*
  142. * Tell the MTD layer we're not 1:1 mapped so that it does
  143. * not attempt to do a direct access on us.
  144. */
  145. info->map.phys = NO_XIP;
  146. info->map.size = ixp_data->nr_banks * window_size;
  147. info->map.bankwidth = 1;
  148. /*
  149. * map_priv_2 is used to store a ptr to the bank_setup routine
  150. */
  151. info->map.map_priv_2 = (unsigned long) ixp_data->bank_setup;
  152. info->map.name = dev_name(&dev->dev);
  153. info->map.read = ixp2000_flash_read8;
  154. info->map.write = ixp2000_flash_write8;
  155. info->map.copy_from = ixp2000_flash_copy_from;
  156. info->map.copy_to = ixp2000_flash_copy_to;
  157. info->res = request_mem_region(dev->resource->start,
  158. resource_size(dev->resource),
  159. dev_name(&dev->dev));
  160. if (!info->res) {
  161. dev_err(&dev->dev, "Could not reserve memory region\n");
  162. err = -ENOMEM;
  163. goto Error;
  164. }
  165. info->map.map_priv_1 =
  166. (unsigned long)ioremap(dev->resource->start,
  167. resource_size(dev->resource));
  168. if (!info->map.map_priv_1) {
  169. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  170. err = -EIO;
  171. goto Error;
  172. }
  173. #if defined(__ARMEB__)
  174. /*
  175. * Enable erratum 44 workaround for NPUs with broken slowport
  176. */
  177. erratum44_workaround = ixp2000_has_broken_slowport();
  178. dev_info(&dev->dev, "Erratum 44 workaround %s\n",
  179. erratum44_workaround ? "enabled" : "disabled");
  180. #endif
  181. info->mtd = do_map_probe(plat->map_name, &info->map);
  182. if (!info->mtd) {
  183. dev_err(&dev->dev, "map_probe failed\n");
  184. err = -ENXIO;
  185. goto Error;
  186. }
  187. info->mtd->owner = THIS_MODULE;
  188. err = mtd_device_parse_register(info->mtd, probes, NULL, NULL, 0);
  189. if (err)
  190. goto Error;
  191. return 0;
  192. Error:
  193. ixp2000_flash_remove(dev);
  194. return err;
  195. }
  196. static struct platform_driver ixp2000_flash_driver = {
  197. .probe = ixp2000_flash_probe,
  198. .remove = ixp2000_flash_remove,
  199. .driver = {
  200. .name = "IXP2000-Flash",
  201. .owner = THIS_MODULE,
  202. },
  203. };
  204. module_platform_driver(ixp2000_flash_driver);
  205. MODULE_LICENSE("GPL");
  206. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
  207. MODULE_ALIAS("platform:IXP2000-Flash");