ixp2000.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * $Id: ixp2000.c,v 1.6 2005/03/18 14:07:46 gleixner Exp $
  3. *
  4. * drivers/mtd/maps/ixp2000.c
  5. *
  6. * Mapping for the Intel XScale IXP2000 based systems
  7. *
  8. * Copyright (C) 2002 Intel Corp.
  9. * Copyright (C) 2003-2004 MontaVista Software, Inc.
  10. *
  11. * Original Author: Naeem M Afzal <naeem.m.afzal@intel.com>
  12. * Maintainer: Deepak Saxena <dsaxena@plexity.net>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/string.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/map.h>
  26. #include <linux/mtd/partitions.h>
  27. #include <linux/ioport.h>
  28. #include <linux/device.h>
  29. #include <asm/io.h>
  30. #include <asm/hardware.h>
  31. #include <asm/mach/flash.h>
  32. #include <linux/reboot.h>
  33. struct ixp2000_flash_info {
  34. struct mtd_info *mtd;
  35. struct map_info map;
  36. struct mtd_partition *partitions;
  37. struct resource *res;
  38. int nr_banks;
  39. };
  40. static inline unsigned long flash_bank_setup(struct map_info *map, unsigned long ofs)
  41. {
  42. unsigned long (*set_bank)(unsigned long) =
  43. (unsigned long(*)(unsigned long))map->map_priv_2;
  44. return (set_bank ? set_bank(ofs) : ofs);
  45. }
  46. #ifdef __ARMEB__
  47. /*
  48. * Rev A0 and A1 of IXP2400 silicon have a broken addressing unit which
  49. * causes the lower address bits to be XORed with 0x11 on 8 bit accesses
  50. * and XORed with 0x10 on 16 bit accesses. See the spec update, erratum 44.
  51. */
  52. static int erratum44_workaround = 0;
  53. static inline unsigned long address_fix8_write(unsigned long addr)
  54. {
  55. if (erratum44_workaround) {
  56. return (addr ^ 3);
  57. }
  58. return addr;
  59. }
  60. #else
  61. #define address_fix8_write(x) (x)
  62. #endif
  63. static map_word ixp2000_flash_read8(struct map_info *map, unsigned long ofs)
  64. {
  65. map_word val;
  66. val.x[0] = *((u8 *)(map->map_priv_1 + flash_bank_setup(map, ofs)));
  67. return val;
  68. }
  69. /*
  70. * We can't use the standard memcpy due to the broken SlowPort
  71. * address translation on rev A0 and A1 silicon and the fact that
  72. * we have banked flash.
  73. */
  74. static void ixp2000_flash_copy_from(struct map_info *map, void *to,
  75. unsigned long from, ssize_t len)
  76. {
  77. from = flash_bank_setup(map, from);
  78. while(len--)
  79. *(__u8 *) to++ = *(__u8 *)(map->map_priv_1 + from++);
  80. }
  81. static void ixp2000_flash_write8(struct map_info *map, map_word d, unsigned long ofs)
  82. {
  83. *(__u8 *) (address_fix8_write(map->map_priv_1 +
  84. flash_bank_setup(map, ofs))) = d.x[0];
  85. }
  86. static void ixp2000_flash_copy_to(struct map_info *map, unsigned long to,
  87. const void *from, ssize_t len)
  88. {
  89. to = flash_bank_setup(map, to);
  90. while(len--) {
  91. unsigned long tmp = address_fix8_write(map->map_priv_1 + to++);
  92. *(__u8 *)(tmp) = *(__u8 *)(from++);
  93. }
  94. }
  95. static int ixp2000_flash_remove(struct device *_dev)
  96. {
  97. struct platform_device *dev = to_platform_device(_dev);
  98. struct flash_platform_data *plat = dev->dev.platform_data;
  99. struct ixp2000_flash_info *info = dev_get_drvdata(&dev->dev);
  100. dev_set_drvdata(&dev->dev, NULL);
  101. if(!info)
  102. return 0;
  103. if (info->mtd) {
  104. del_mtd_partitions(info->mtd);
  105. map_destroy(info->mtd);
  106. }
  107. if (info->map.map_priv_1)
  108. iounmap((void *) info->map.map_priv_1);
  109. if (info->partitions) {
  110. kfree(info->partitions); }
  111. if (info->res) {
  112. release_resource(info->res);
  113. kfree(info->res);
  114. }
  115. if (plat->exit)
  116. plat->exit();
  117. return 0;
  118. }
  119. static int ixp2000_flash_probe(struct device *_dev)
  120. {
  121. static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
  122. struct platform_device *dev = to_platform_device(_dev);
  123. struct ixp2000_flash_data *ixp_data = dev->dev.platform_data;
  124. struct flash_platform_data *plat;
  125. struct ixp2000_flash_info *info;
  126. unsigned long window_size;
  127. int err = -1;
  128. if (!ixp_data)
  129. return -ENODEV;
  130. plat = ixp_data->platform_data;
  131. if (!plat)
  132. return -ENODEV;
  133. window_size = dev->resource->end - dev->resource->start + 1;
  134. dev_info(_dev, "Probe of IXP2000 flash(%d banks x %dMiB)\n",
  135. ixp_data->nr_banks, ((u32)window_size >> 20));
  136. if (plat->width != 1) {
  137. dev_err(_dev, "IXP2000 MTD map only supports 8-bit mode, asking for %d\n",
  138. plat->width * 8);
  139. return -EIO;
  140. }
  141. info = kmalloc(sizeof(struct ixp2000_flash_info), GFP_KERNEL);
  142. if(!info) {
  143. err = -ENOMEM;
  144. goto Error;
  145. }
  146. memzero(info, sizeof(struct ixp2000_flash_info));
  147. dev_set_drvdata(&dev->dev, info);
  148. /*
  149. * Tell the MTD layer we're not 1:1 mapped so that it does
  150. * not attempt to do a direct access on us.
  151. */
  152. info->map.phys = NO_XIP;
  153. info->nr_banks = ixp_data->nr_banks;
  154. info->map.size = ixp_data->nr_banks * window_size;
  155. info->map.bankwidth = 1;
  156. /*
  157. * map_priv_2 is used to store a ptr to to the bank_setup routine
  158. */
  159. info->map.map_priv_2 = (void __iomem *) ixp_data->bank_setup;
  160. info->map.name = dev->dev.bus_id;
  161. info->map.read = ixp2000_flash_read8;
  162. info->map.write = ixp2000_flash_write8;
  163. info->map.copy_from = ixp2000_flash_copy_from;
  164. info->map.copy_to = ixp2000_flash_copy_to;
  165. info->res = request_mem_region(dev->resource->start,
  166. dev->resource->end - dev->resource->start + 1,
  167. dev->dev.bus_id);
  168. if (!info->res) {
  169. dev_err(_dev, "Could not reserve memory region\n");
  170. err = -ENOMEM;
  171. goto Error;
  172. }
  173. info->map.map_priv_1 = ioremap(dev->resource->start,
  174. dev->resource->end - dev->resource->start + 1);
  175. if (!info->map.map_priv_1) {
  176. dev_err(_dev, "Failed to ioremap flash region\n");
  177. err = -EIO;
  178. goto Error;
  179. }
  180. #if defined(__ARMEB__)
  181. /*
  182. * Enable erratum 44 workaround for NPUs with broken slowport
  183. */
  184. erratum44_workaround = ixp2000_has_broken_slowport();
  185. dev_info(_dev, "Erratum 44 workaround %s\n",
  186. erratum44_workaround ? "enabled" : "disabled");
  187. #endif
  188. info->mtd = do_map_probe(plat->map_name, &info->map);
  189. if (!info->mtd) {
  190. dev_err(_dev, "map_probe failed\n");
  191. err = -ENXIO;
  192. goto Error;
  193. }
  194. info->mtd->owner = THIS_MODULE;
  195. err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
  196. if (err > 0) {
  197. err = add_mtd_partitions(info->mtd, info->partitions, err);
  198. if(err)
  199. dev_err(_dev, "Could not parse partitions\n");
  200. }
  201. if (err)
  202. goto Error;
  203. return 0;
  204. Error:
  205. ixp2000_flash_remove(_dev);
  206. return err;
  207. }
  208. static struct device_driver ixp2000_flash_driver = {
  209. .name = "IXP2000-Flash",
  210. .bus = &platform_bus_type,
  211. .probe = &ixp2000_flash_probe,
  212. .remove = &ixp2000_flash_remove
  213. };
  214. static int __init ixp2000_flash_init(void)
  215. {
  216. return driver_register(&ixp2000_flash_driver);
  217. }
  218. static void __exit ixp2000_flash_exit(void)
  219. {
  220. driver_unregister(&ixp2000_flash_driver);
  221. }
  222. module_init(ixp2000_flash_init);
  223. module_exit(ixp2000_flash_exit);
  224. MODULE_LICENSE("GPL");
  225. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");