ixp4xx.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 device *_dev)
  89. {
  90. struct platform_device *dev = to_platform_device(_dev);
  91. struct flash_platform_data *plat = dev->dev.platform_data;
  92. struct ixp4xx_flash_info *info = dev_get_drvdata(&dev->dev);
  93. dev_set_drvdata(&dev->dev, NULL);
  94. if(!info)
  95. return 0;
  96. if (info->mtd) {
  97. del_mtd_partitions(info->mtd);
  98. map_destroy(info->mtd);
  99. }
  100. if (info->map.virt)
  101. iounmap(info->map.virt);
  102. if (info->partitions)
  103. kfree(info->partitions);
  104. if (info->res) {
  105. release_resource(info->res);
  106. kfree(info->res);
  107. }
  108. if (plat->exit)
  109. plat->exit();
  110. return 0;
  111. }
  112. static int ixp4xx_flash_probe(struct device *_dev)
  113. {
  114. struct platform_device *dev = to_platform_device(_dev);
  115. struct flash_platform_data *plat = dev->dev.platform_data;
  116. struct ixp4xx_flash_info *info;
  117. int err = -1;
  118. if (!plat)
  119. return -ENODEV;
  120. if (plat->init) {
  121. err = plat->init();
  122. if (err)
  123. return err;
  124. }
  125. info = kmalloc(sizeof(struct ixp4xx_flash_info), GFP_KERNEL);
  126. if(!info) {
  127. err = -ENOMEM;
  128. goto Error;
  129. }
  130. memzero(info, sizeof(struct ixp4xx_flash_info));
  131. dev_set_drvdata(&dev->dev, info);
  132. /*
  133. * Tell the MTD layer we're not 1:1 mapped so that it does
  134. * not attempt to do a direct access on us.
  135. */
  136. info->map.phys = NO_XIP;
  137. info->map.size = dev->resource->end - dev->resource->start + 1;
  138. /*
  139. * We only support 16-bit accesses for now. If and when
  140. * any board use 8-bit access, we'll fixup the driver to
  141. * handle that.
  142. */
  143. info->map.bankwidth = 2;
  144. info->map.name = dev->dev.bus_id;
  145. info->map.read = ixp4xx_read16,
  146. info->map.write = ixp4xx_probe_write16,
  147. info->map.copy_from = ixp4xx_copy_from,
  148. info->res = request_mem_region(dev->resource->start,
  149. dev->resource->end - dev->resource->start + 1,
  150. "IXP4XXFlash");
  151. if (!info->res) {
  152. printk(KERN_ERR "IXP4XXFlash: Could not reserve memory region\n");
  153. err = -ENOMEM;
  154. goto Error;
  155. }
  156. info->map.virt = ioremap(dev->resource->start,
  157. dev->resource->end - dev->resource->start + 1);
  158. if (!info->map.virt) {
  159. printk(KERN_ERR "IXP4XXFlash: Failed to ioremap region\n");
  160. err = -EIO;
  161. goto Error;
  162. }
  163. info->mtd = do_map_probe(plat->map_name, &info->map);
  164. if (!info->mtd) {
  165. printk(KERN_ERR "IXP4XXFlash: map_probe failed\n");
  166. err = -ENXIO;
  167. goto Error;
  168. }
  169. info->mtd->owner = THIS_MODULE;
  170. /* Use the fast version */
  171. info->map.write = ixp4xx_write16,
  172. err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
  173. if (err > 0) {
  174. err = add_mtd_partitions(info->mtd, info->partitions, err);
  175. if(err)
  176. printk(KERN_ERR "Could not parse partitions\n");
  177. }
  178. if (err)
  179. goto Error;
  180. return 0;
  181. Error:
  182. ixp4xx_flash_remove(_dev);
  183. return err;
  184. }
  185. static struct device_driver ixp4xx_flash_driver = {
  186. .name = "IXP4XX-Flash",
  187. .bus = &platform_bus_type,
  188. .probe = ixp4xx_flash_probe,
  189. .remove = ixp4xx_flash_remove,
  190. };
  191. static int __init ixp4xx_flash_init(void)
  192. {
  193. return driver_register(&ixp4xx_flash_driver);
  194. }
  195. static void __exit ixp4xx_flash_exit(void)
  196. {
  197. driver_unregister(&ixp4xx_flash_driver);
  198. }
  199. module_init(ixp4xx_flash_init);
  200. module_exit(ixp4xx_flash_exit);
  201. MODULE_LICENSE("GPL");
  202. MODULE_DESCRIPTION("MTD map driver for Intel IXP4xx systems");
  203. MODULE_AUTHOR("Deepak Saxena");