PageRenderTime 65ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/mtd/maps/ixp2000.c

http://github.com/CyanogenMod/cm-kernel
C | 272 lines | 187 code | 49 blank | 36 comment | 17 complexity | 8971f7af33c3b5e01029d0fd1e99d0e9 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * drivers/mtd/maps/ixp2000.c
  3. *
  4. * Mapping for the Intel XScale IXP2000 based systems
  5. *
  6. * Copyright (C) 2002 Intel Corp.
  7. * Copyright (C) 2003-2004 MontaVista Software, Inc.
  8. *
  9. * Original Author: Naeem M Afzal <naeem.m.afzal@intel.com>
  10. * Maintainer: Deepak Saxena <dsaxena@plexity.net>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  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 <mach/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. };
  39. static inline unsigned long flash_bank_setup(struct map_info *map, unsigned long ofs)
  40. {
  41. unsigned long (*set_bank)(unsigned long) =
  42. (unsigned long(*)(unsigned long))map->map_priv_2;
  43. return (set_bank ? set_bank(ofs) : ofs);
  44. }
  45. #ifdef __ARMEB__
  46. /*
  47. * Rev A0 and A1 of IXP2400 silicon have a broken addressing unit which
  48. * causes the lower address bits to be XORed with 0x11 on 8 bit accesses
  49. * and XORed with 0x10 on 16 bit accesses. See the spec update, erratum 44.
  50. */
  51. static int erratum44_workaround = 0;
  52. static inline unsigned long address_fix8_write(unsigned long addr)
  53. {
  54. if (erratum44_workaround) {
  55. return (addr ^ 3);
  56. }
  57. return addr;
  58. }
  59. #else
  60. #define address_fix8_write(x) (x)
  61. #endif
  62. static map_word ixp2000_flash_read8(struct map_info *map, unsigned long ofs)
  63. {
  64. map_word val;
  65. val.x[0] = *((u8 *)(map->map_priv_1 + flash_bank_setup(map, ofs)));
  66. return val;
  67. }
  68. /*
  69. * We can't use the standard memcpy due to the broken SlowPort
  70. * address translation on rev A0 and A1 silicon and the fact that
  71. * we have banked flash.
  72. */
  73. static void ixp2000_flash_copy_from(struct map_info *map, void *to,
  74. unsigned long from, ssize_t len)
  75. {
  76. from = flash_bank_setup(map, from);
  77. while(len--)
  78. *(__u8 *) to++ = *(__u8 *)(map->map_priv_1 + from++);
  79. }
  80. static void ixp2000_flash_write8(struct map_info *map, map_word d, unsigned long ofs)
  81. {
  82. *(__u8 *) (address_fix8_write(map->map_priv_1 +
  83. flash_bank_setup(map, ofs))) = d.x[0];
  84. }
  85. static void ixp2000_flash_copy_to(struct map_info *map, unsigned long to,
  86. const void *from, ssize_t len)
  87. {
  88. to = flash_bank_setup(map, to);
  89. while(len--) {
  90. unsigned long tmp = address_fix8_write(map->map_priv_1 + to++);
  91. *(__u8 *)(tmp) = *(__u8 *)(from++);
  92. }
  93. }
  94. static int ixp2000_flash_remove(struct platform_device *dev)
  95. {
  96. struct flash_platform_data *plat = dev->dev.platform_data;
  97. struct ixp2000_flash_info *info = platform_get_drvdata(dev);
  98. platform_set_drvdata(dev, NULL);
  99. if(!info)
  100. return 0;
  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. 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. return 0;
  115. }
  116. static int ixp2000_flash_probe(struct platform_device *dev)
  117. {
  118. static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };
  119. struct ixp2000_flash_data *ixp_data = dev->dev.platform_data;
  120. struct flash_platform_data *plat;
  121. struct ixp2000_flash_info *info;
  122. unsigned long window_size;
  123. int err = -1;
  124. if (!ixp_data)
  125. return -ENODEV;
  126. plat = ixp_data->platform_data;
  127. if (!plat)
  128. return -ENODEV;
  129. window_size = dev->resource->end - dev->resource->start + 1;
  130. dev_info(&dev->dev, "Probe of IXP2000 flash(%d banks x %dMiB)\n",
  131. ixp_data->nr_banks, ((u32)window_size >> 20));
  132. if (plat->width != 1) {
  133. dev_err(&dev->dev, "IXP2000 MTD map only supports 8-bit mode, asking for %d\n",
  134. plat->width * 8);
  135. return -EIO;
  136. }
  137. info = kmalloc(sizeof(struct ixp2000_flash_info), GFP_KERNEL);
  138. if(!info) {
  139. err = -ENOMEM;
  140. goto Error;
  141. }
  142. memset(info, 0, sizeof(struct ixp2000_flash_info));
  143. platform_set_drvdata(dev, info);
  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 = ixp_data->nr_banks * window_size;
  150. info->map.bankwidth = 1;
  151. /*
  152. * map_priv_2 is used to store a ptr to to the bank_setup routine
  153. */
  154. info->map.map_priv_2 = (unsigned long) ixp_data->bank_setup;
  155. info->map.name = dev_name(&dev->dev);
  156. info->map.read = ixp2000_flash_read8;
  157. info->map.write = ixp2000_flash_write8;
  158. info->map.copy_from = ixp2000_flash_copy_from;
  159. info->map.copy_to = ixp2000_flash_copy_to;
  160. info->res = request_mem_region(dev->resource->start,
  161. dev->resource->end - dev->resource->start + 1,
  162. dev_name(&dev->dev));
  163. if (!info->res) {
  164. dev_err(&dev->dev, "Could not reserve memory region\n");
  165. err = -ENOMEM;
  166. goto Error;
  167. }
  168. info->map.map_priv_1 = (unsigned long) ioremap(dev->resource->start,
  169. dev->resource->end - dev->resource->start + 1);
  170. if (!info->map.map_priv_1) {
  171. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  172. err = -EIO;
  173. goto Error;
  174. }
  175. #if defined(__ARMEB__)
  176. /*
  177. * Enable erratum 44 workaround for NPUs with broken slowport
  178. */
  179. erratum44_workaround = ixp2000_has_broken_slowport();
  180. dev_info(&dev->dev, "Erratum 44 workaround %s\n",
  181. erratum44_workaround ? "enabled" : "disabled");
  182. #endif
  183. info->mtd = do_map_probe(plat->map_name, &info->map);
  184. if (!info->mtd) {
  185. dev_err(&dev->dev, "map_probe failed\n");
  186. err = -ENXIO;
  187. goto Error;
  188. }
  189. info->mtd->owner = THIS_MODULE;
  190. err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0);
  191. if (err > 0) {
  192. err = add_mtd_partitions(info->mtd, info->partitions, err);
  193. if(err)
  194. dev_err(&dev->dev, "Could not parse partitions\n");
  195. }
  196. if (err)
  197. goto Error;
  198. return 0;
  199. Error:
  200. ixp2000_flash_remove(dev);
  201. return err;
  202. }
  203. static struct platform_driver ixp2000_flash_driver = {
  204. .probe = ixp2000_flash_probe,
  205. .remove = ixp2000_flash_remove,
  206. .driver = {
  207. .name = "IXP2000-Flash",
  208. .owner = THIS_MODULE,
  209. },
  210. };
  211. static int __init ixp2000_flash_init(void)
  212. {
  213. return platform_driver_register(&ixp2000_flash_driver);
  214. }
  215. static void __exit ixp2000_flash_exit(void)
  216. {
  217. platform_driver_unregister(&ixp2000_flash_driver);
  218. }
  219. module_init(ixp2000_flash_init);
  220. module_exit(ixp2000_flash_exit);
  221. MODULE_LICENSE("GPL");
  222. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
  223. MODULE_ALIAS("platform:IXP2000-Flash");