ixp4xx.c 5.8 KB

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