ixp4xx.c 6.7 KB

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