ixp4xx.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * drivers/mtd/maps/ixp4xx.c
  3. *
  4. * MTD Map file for IXP4XX based systems. Please do not make per-board
  5. * changes in here. If your board needs special setup, do it in your
  6. * platform level code in arch/arm/mach-ixp4xx/board-setup.c
  7. *
  8. * Original Author: Intel Corporation
  9. * Maintainer: Deepak Saxena <dsaxena@mvista.com>
  10. *
  11. * Copyright (C) 2002 Intel Corporation
  12. * Copyright (C) 2003-2004 MontaVista Software, Inc.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/ioport.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/map.h>
  26. #include <linux/mtd/partitions.h>
  27. #include <asm/io.h>
  28. #include <asm/mach/flash.h>
  29. #include <linux/reboot.h>
  30. /*
  31. * Read/write a 16 bit word from flash address 'addr'.
  32. *
  33. * When the cpu is in little-endian mode it swizzles the address lines
  34. * ('address coherency') so we need to undo the swizzling to ensure commands
  35. * and the like end up on the correct flash address.
  36. *
  37. * To further complicate matters, due to the way the expansion bus controller
  38. * handles 32 bit reads, the byte stream ABCD is stored on the flash as:
  39. * D15 D0
  40. * +---+---+
  41. * | A | B | 0
  42. * +---+---+
  43. * | C | D | 2
  44. * +---+---+
  45. * This means that on LE systems each 16 bit word must be swapped. Note that
  46. * this requires CONFIG_MTD_CFI_BE_BYTE_SWAP to be enabled to 'unswap' the CFI
  47. * data and other flash commands which are always in D7-D0.
  48. */
  49. #ifndef __ARMEB__
  50. #ifndef CONFIG_MTD_CFI_BE_BYTE_SWAP
  51. # error CONFIG_MTD_CFI_BE_BYTE_SWAP required
  52. #endif
  53. static inline u16 flash_read16(void __iomem *addr)
  54. {
  55. return be16_to_cpu(__raw_readw((void __iomem *)((unsigned long)addr ^ 0x2)));
  56. }
  57. static inline void flash_write16(u16 d, void __iomem *addr)
  58. {
  59. __raw_writew(cpu_to_be16(d), (void __iomem *)((unsigned long)addr ^ 0x2));
  60. }
  61. #define BYTE0(h) ((h) & 0xFF)
  62. #define BYTE1(h) (((h) >> 8) & 0xFF)
  63. #else
  64. static inline u16 flash_read16(const void __iomem *addr)
  65. {
  66. return __raw_readw(addr);
  67. }
  68. static inline void flash_write16(u16 d, void __iomem *addr)
  69. {
  70. __raw_writew(d, addr);
  71. }
  72. #define BYTE0(h) (((h) >> 8) & 0xFF)
  73. #define BYTE1(h) ((h) & 0xFF)
  74. #endif
  75. static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
  76. {
  77. map_word val;
  78. val.x[0] = flash_read16(map->virt + ofs);
  79. return val;
  80. }
  81. /*
  82. * The IXP4xx expansion bus only allows 16-bit wide acceses
  83. * when attached to a 16-bit wide device (such as the 28F128J3A),
  84. * so we can't just memcpy_fromio().
  85. */
  86. static void ixp4xx_copy_from(struct map_info *map, void *to,
  87. unsigned long from, ssize_t len)
  88. {
  89. u8 *dest = (u8 *) to;
  90. void __iomem *src = map->virt + from;
  91. if (len <= 0)
  92. return;
  93. if (from & 1) {
  94. *dest++ = BYTE1(flash_read16(src-1));
  95. src++;
  96. --len;
  97. }
  98. while (len >= 2) {
  99. u16 data = flash_read16(src);
  100. *dest++ = BYTE0(data);
  101. *dest++ = BYTE1(data);
  102. src += 2;
  103. len -= 2;
  104. }
  105. if (len > 0)
  106. *dest++ = BYTE0(flash_read16(src));
  107. }
  108. /*
  109. * Unaligned writes are ignored, causing the 8-bit
  110. * probe to fail and proceed to the 16-bit probe (which succeeds).
  111. */
  112. static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr)
  113. {
  114. if (!(adr & 1))
  115. flash_write16(d.x[0], map->virt + adr);
  116. }
  117. /*
  118. * Fast write16 function without the probing check above
  119. */
  120. static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
  121. {
  122. flash_write16(d.x[0], map->virt + adr);
  123. }
  124. struct ixp4xx_flash_info {
  125. struct mtd_info *mtd;
  126. struct map_info map;
  127. struct mtd_partition *partitions;
  128. struct resource *res;
  129. };
  130. static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
  131. static int ixp4xx_flash_remove(struct platform_device *dev)
  132. {
  133. struct flash_platform_data *plat = dev->dev.platform_data;
  134. struct ixp4xx_flash_info *info = platform_get_drvdata(dev);
  135. platform_set_drvdata(dev, NULL);
  136. if(!info)
  137. return 0;
  138. if (info->mtd) {
  139. mtd_device_unregister(info->mtd);
  140. map_destroy(info->mtd);
  141. }
  142. if (info->map.virt)
  143. iounmap(info->map.virt);
  144. kfree(info->partitions);
  145. if (info->res) {
  146. release_resource(info->res);
  147. kfree(info->res);
  148. }
  149. if (plat->exit)
  150. plat->exit();
  151. return 0;
  152. }
  153. static int ixp4xx_flash_probe(struct platform_device *dev)
  154. {
  155. struct flash_platform_data *plat = dev->dev.platform_data;
  156. struct ixp4xx_flash_info *info;
  157. const char *part_type = NULL;
  158. int nr_parts = 0;
  159. int err = -1;
  160. if (!plat)
  161. return -ENODEV;
  162. if (plat->init) {
  163. err = plat->init();
  164. if (err)
  165. return err;
  166. }
  167. info = kzalloc(sizeof(struct ixp4xx_flash_info), GFP_KERNEL);
  168. if(!info) {
  169. err = -ENOMEM;
  170. goto Error;
  171. }
  172. platform_set_drvdata(dev, info);
  173. /*
  174. * Tell the MTD layer we're not 1:1 mapped so that it does
  175. * not attempt to do a direct access on us.
  176. */
  177. info->map.phys = NO_XIP;
  178. info->map.size = resource_size(dev->resource);
  179. /*
  180. * We only support 16-bit accesses for now. If and when
  181. * any board use 8-bit access, we'll fixup the driver to
  182. * handle that.
  183. */
  184. info->map.bankwidth = 2;
  185. info->map.name = dev_name(&dev->dev);
  186. info->map.read = ixp4xx_read16;
  187. info->map.write = ixp4xx_probe_write16;
  188. info->map.copy_from = ixp4xx_copy_from;
  189. info->res = request_mem_region(dev->resource->start,
  190. resource_size(dev->resource),
  191. "IXP4XXFlash");
  192. if (!info->res) {
  193. printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n");
  194. err = -ENOMEM;
  195. goto Error;
  196. }
  197. info->map.virt = ioremap(dev->resource->start,
  198. resource_size(dev->resource));
  199. if (!info->map.virt) {
  200. printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n");
  201. err = -EIO;
  202. goto Error;
  203. }
  204. info->mtd = do_map_probe(plat->map_name, &info->map);
  205. if (!info->mtd) {
  206. printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
  207. err = -ENXIO;
  208. goto Error;
  209. }
  210. info->mtd->owner = THIS_MODULE;
  211. /* Use the fast version */
  212. info->map.write = ixp4xx_write16;
  213. nr_parts = parse_mtd_partitions(info->mtd, probes, &info->partitions,
  214. dev->resource->start);
  215. if (nr_parts > 0) {
  216. part_type = "dynamic";
  217. } else {
  218. info->partitions = plat->parts;
  219. nr_parts = plat->nr_parts;
  220. part_type = "static";
  221. }
  222. if (nr_parts == 0)
  223. printk(KERN_NOTICE "IXP4xx flash: no partition info "
  224. "available, registering whole flash\n");
  225. else
  226. printk(KERN_NOTICE "IXP4xx flash: using %s partition "
  227. "definition\n", part_type);
  228. err = mtd_device_register(info->mtd, info->partitions, nr_parts);
  229. if (err)
  230. printk(KERN_ERR "Could not parse partitions\n");
  231. if (err)
  232. goto Error;
  233. return 0;
  234. Error:
  235. ixp4xx_flash_remove(dev);
  236. return err;
  237. }
  238. static struct platform_driver ixp4xx_flash_driver = {
  239. .probe = ixp4xx_flash_probe,
  240. .remove = ixp4xx_flash_remove,
  241. .driver = {
  242. .name = "IXP4XX-Flash",
  243. .owner = THIS_MODULE,
  244. },
  245. };
  246. static int __init ixp4xx_flash_init(void)
  247. {
  248. return platform_driver_register(&ixp4xx_flash_driver);
  249. }
  250. static void __exit ixp4xx_flash_exit(void)
  251. {
  252. platform_driver_unregister(&ixp4xx_flash_driver);
  253. }
  254. module_init(ixp4xx_flash_init);
  255. module_exit(ixp4xx_flash_exit);
  256. MODULE_LICENSE("GPL");
  257. MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
  258. MODULE_AUTHOR("Deepak Saxena");
  259. MODULE_ALIAS("platform:IXP4XX-Flash");