ixp4xx.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/err.h>
  16. #include <linux/module.h>
  17. #include <linux/types.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 resource *res;
  128. };
  129. static const char * const probes[] = { "RedBoot", "cmdlinepart", NULL };
  130. static int ixp4xx_flash_remove(struct platform_device *dev)
  131. {
  132. struct flash_platform_data *plat = dev_get_platdata(&dev->dev);
  133. struct ixp4xx_flash_info *info = platform_get_drvdata(dev);
  134. if(!info)
  135. return 0;
  136. if (info->mtd) {
  137. mtd_device_unregister(info->mtd);
  138. map_destroy(info->mtd);
  139. }
  140. if (plat->exit)
  141. plat->exit();
  142. return 0;
  143. }
  144. static int ixp4xx_flash_probe(struct platform_device *dev)
  145. {
  146. struct flash_platform_data *plat = dev_get_platdata(&dev->dev);
  147. struct ixp4xx_flash_info *info;
  148. struct mtd_part_parser_data ppdata = {
  149. .origin = dev->resource->start,
  150. };
  151. int err = -1;
  152. if (!plat)
  153. return -ENODEV;
  154. if (plat->init) {
  155. err = plat->init();
  156. if (err)
  157. return err;
  158. }
  159. info = devm_kzalloc(&dev->dev, sizeof(struct ixp4xx_flash_info),
  160. GFP_KERNEL);
  161. if(!info) {
  162. err = -ENOMEM;
  163. goto Error;
  164. }
  165. platform_set_drvdata(dev, info);
  166. /*
  167. * Tell the MTD layer we're not 1:1 mapped so that it does
  168. * not attempt to do a direct access on us.
  169. */
  170. info->map.phys = NO_XIP;
  171. info->map.size = resource_size(dev->resource);
  172. /*
  173. * We only support 16-bit accesses for now. If and when
  174. * any board use 8-bit access, we'll fixup the driver to
  175. * handle that.
  176. */
  177. info->map.bankwidth = 2;
  178. info->map.name = dev_name(&dev->dev);
  179. info->map.read = ixp4xx_read16;
  180. info->map.write = ixp4xx_probe_write16;
  181. info->map.copy_from = ixp4xx_copy_from;
  182. info->map.virt = devm_ioremap_resource(&dev->dev, dev->resource);
  183. if (IS_ERR(info->map.virt)) {
  184. err = PTR_ERR(info->map.virt);
  185. goto Error;
  186. }
  187. info->mtd = do_map_probe(plat->map_name, &info->map);
  188. if (!info->mtd) {
  189. printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
  190. err = -ENXIO;
  191. goto Error;
  192. }
  193. info->mtd->dev.parent = &dev->dev;
  194. /* Use the fast version */
  195. info->map.write = ixp4xx_write16;
  196. err = mtd_device_parse_register(info->mtd, probes, &ppdata,
  197. plat->parts, plat->nr_parts);
  198. if (err) {
  199. printk(KERN_ERR "Could not parse partitions\n");
  200. goto Error;
  201. }
  202. return 0;
  203. Error:
  204. ixp4xx_flash_remove(dev);
  205. return err;
  206. }
  207. static struct platform_driver ixp4xx_flash_driver = {
  208. .probe = ixp4xx_flash_probe,
  209. .remove = ixp4xx_flash_remove,
  210. .driver = {
  211. .name = "IXP4XX-Flash",
  212. },
  213. };
  214. module_platform_driver(ixp4xx_flash_driver);
  215. MODULE_LICENSE("GPL");
  216. MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
  217. MODULE_AUTHOR("Deepak Saxena");
  218. MODULE_ALIAS("platform:IXP4XX-Flash");