ixp2000.c 6.6 KB

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