ixp4xx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * $Id: ixp4xx.c,v 1.12 2005/11/07 11:14:27 gleixner 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. #ifndef __ARMEB__
  33. #define BYTE0(h) ((h) & 0xFF)
  34. #define BYTE1(h) (((h) >> 8) & 0xFF)
  35. #else
  36. #define BYTE0(h) (((h) >> 8) & 0xFF)
  37. #define BYTE1(h) ((h) & 0xFF)
  38. #endif
  39. static map_word ixp4xx_read16(struct map_info *map, unsigned long ofs)
  40. {
  41. map_word val;
  42. val.x[0] = le16_to_cpu(readw(map->virt + ofs));
  43. return val;
  44. }
  45. /*
  46. * The IXP4xx expansion bus only allows 16-bit wide acceses
  47. * when attached to a 16-bit wide device (such as the 28F128J3A),
  48. * so we can't just memcpy_fromio().
  49. */
  50. static void ixp4xx_copy_from(struct map_info *map, void *to,
  51. unsigned long from, ssize_t len)
  52. {
  53. int i;
  54. u8 *dest = (u8 *) to;
  55. void __iomem *src = map->virt + from;
  56. u16 data;
  57. for (i = 0; i < (len / 2); i++) {
  58. data = le16_to_cpu(readw(src + 2*i));
  59. dest[i * 2] = BYTE0(data);
  60. dest[i * 2 + 1] = BYTE1(data);
  61. }
  62. if (len & 1)
  63. dest[len - 1] = BYTE0(le16_to_cpu(readw(src + 2*i)));
  64. }
  65. /*
  66. * Unaligned writes are ignored, causing the 8-bit
  67. * probe to fail and proceed to the 16-bit probe (which succeeds).
  68. */
  69. static void ixp4xx_probe_write16(struct map_info *map, map_word d, unsigned long adr)
  70. {
  71. if (!(adr & 1))
  72. writew(cpu_to_le16(d.x[0]), map->virt + adr);
  73. }
  74. /*
  75. * Fast write16 function without the probing check above
  76. */
  77. static void ixp4xx_write16(struct map_info *map, map_word d, unsigned long adr)
  78. {
  79. writew(cpu_to_le16(d.x[0]), map->virt + adr);
  80. }
  81. struct ixp4xx_flash_info {
  82. struct mtd_info *mtd;
  83. struct map_info map;
  84. struct mtd_partition *partitions;
  85. struct resource *res;
  86. };
  87. static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
  88. static int ixp4xx_flash_remove(struct platform_device *dev)
  89. {
  90. struct flash_platform_data *plat = dev->dev.platform_data;
  91. struct ixp4xx_flash_info *info = platform_get_drvdata(dev);
  92. platform_set_drvdata(dev, NULL);
  93. if(!info)
  94. return 0;
  95. if (info->mtd) {
  96. del_mtd_partitions(info->mtd);
  97. map_destroy(info->mtd);
  98. }
  99. if (info->map.virt)
  100. iounmap(info->map.virt);
  101. kfree(info->partitions);
  102. if (info->res) {
  103. release_resource(info->res);
  104. kfree(info->res);
  105. }
  106. if (plat->exit)
  107. plat->exit();
  108. return 0;
  109. }
  110. static int ixp4xx_flash_probe(struct platform_device *dev)
  111. {
  112. struct flash_platform_data *plat = dev->dev.platform_data;
  113. struct ixp4xx_flash_info *info;
  114. int err = -1;
  115. if (!plat)
  116. return -ENODEV;
  117. if (plat->init) {
  118. err = plat->init();
  119. if (err)
  120. return err;
  121. }
  122. info = kmalloc(sizeof(struct ixp4xx_flash_info), GFP_KERNEL);
  123. if(!info) {
  124. err = -ENOMEM;
  125. goto Error;
  126. }
  127. memzero(info, sizeof(struct ixp4xx_flash_info));
  128. platform_set_drvdata(dev, info);
  129. /*
  130. * Tell the MTD layer we're not 1:1 mapped so that it does
  131. * not attempt to do a direct access on us.
  132. */
  133. info->map.phys = NO_XIP;
  134. info->map.size = dev->resource->end - dev->resource->start + 1;
  135. /*
  136. * We only support 16-bit accesses for now. If and when
  137. * any board use 8-bit access, we'll fixup the driver to
  138. * handle that.
  139. */
  140. info->map.bankwidth = 2;
  141. info->map.name = dev->dev.bus_id;
  142. info->map.read = ixp4xx_read16,
  143. info->map.write = ixp4xx_probe_write16,
  144. info->map.copy_from = ixp4xx_copy_from,
  145. info->res = request_mem_region(dev->resource->start,
  146. dev->resource->end - dev->resource->start + 1,
  147. "IXP4XXFlash");
  148. if (!info->res) {
  149. printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n");
  150. err = -ENOMEM;
  151. goto Error;
  152. }
  153. info->map.virt = ioremap(dev->resource->start,
  154. dev->resource->end - dev->resource->start + 1);
  155. if (!info->map.virt) {
  156. printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n");
  157. err = -EIO;
  158. goto Error;
  159. }
  160. info->mtd = do_map_probe(plat->map_name, &info->map);
  161. if (!info->mtd) {
  162. printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
  163. err = -ENXIO;
  164. goto Error;
  165. }
  166. info->mtd->owner = THIS_MODULE;
  167. /* Use the fast version */
  168. info->map.write = ixp4xx_write16,
  169. err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
  170. if (err > 0) {
  171. err = add_mtd_partitions(info->mtd, info->partitions, err);
  172. if(err)
  173. printk(KERN_ERR "Could not parse partitions\n");
  174. }
  175. if (err)
  176. goto Error;
  177. return 0;
  178. Error:
  179. ixp4xx_flash_remove(dev);
  180. return err;
  181. }
  182. static struct platform_driver ixp4xx_flash_driver = {
  183. .probe = ixp4xx_flash_probe,
  184. .remove = ixp4xx_flash_remove,
  185. .driver = {
  186. .name = "IXP4XX-Flash",
  187. },
  188. };
  189. static int __init ixp4xx_flash_init(void)
  190. {
  191. return platform_driver_register(&ixp4xx_flash_driver);
  192. }
  193. static void __exit ixp4xx_flash_exit(void)
  194. {
  195. platform_driver_unregister(&ixp4xx_flash_driver);
  196. }
  197. module_init(ixp4xx_flash_init);
  198. module_exit(ixp4xx_flash_exit);
  199. MODULE_LICENSE("GPL");
  200. MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
  201. MODULE_AUTHOR("Deepak Saxena");