ixp4xx.c 5.8 KB

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