physmap_of.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Normal mappings of chips in physical memory for OF devices
  3. *
  4. * Copyright (C) 2006 MontaVista Software Inc.
  5. * Author: Vitaly Wool <vwool@ru.mvista.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/map.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/mtd/physmap.h>
  22. #include <asm/io.h>
  23. #include <asm/prom.h>
  24. #include <asm/of_device.h>
  25. #include <asm/of_platform.h>
  26. struct physmap_flash_info {
  27. struct mtd_info *mtd;
  28. struct map_info map;
  29. struct resource *res;
  30. #ifdef CONFIG_MTD_PARTITIONS
  31. int nr_parts;
  32. struct mtd_partition *parts;
  33. #endif
  34. };
  35. static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
  36. #ifdef CONFIG_MTD_PARTITIONS
  37. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
  38. #endif
  39. #ifdef CONFIG_MTD_PARTITIONS
  40. static int parse_flash_partitions(struct device_node *node,
  41. struct mtd_partition **parts)
  42. {
  43. int i, plen, retval = -ENOMEM;
  44. const u32 *part;
  45. const char *name;
  46. part = of_get_property(node, "partitions", &plen);
  47. if (part == NULL)
  48. goto err;
  49. retval = plen / (2 * sizeof(u32));
  50. *parts = kzalloc(retval * sizeof(struct mtd_partition), GFP_KERNEL);
  51. if (*parts == NULL) {
  52. printk(KERN_ERR "Can't allocate the flash partition data!\n");
  53. goto err;
  54. }
  55. name = of_get_property(node, "partition-names", &plen);
  56. for (i = 0; i < retval; i++) {
  57. (*parts)[i].offset = *part++;
  58. (*parts)[i].size = *part & ~1;
  59. if (*part++ & 1) /* bit 0 set signifies read only partition */
  60. (*parts)[i].mask_flags = MTD_WRITEABLE;
  61. if (name != NULL && plen > 0) {
  62. int len = strlen(name) + 1;
  63. (*parts)[i].name = (char *)name;
  64. plen -= len;
  65. name += len;
  66. } else
  67. (*parts)[i].name = "unnamed";
  68. }
  69. err:
  70. return retval;
  71. }
  72. #endif
  73. static int of_physmap_remove(struct of_device *dev)
  74. {
  75. struct physmap_flash_info *info;
  76. info = dev_get_drvdata(&dev->dev);
  77. if (info == NULL)
  78. return 0;
  79. dev_set_drvdata(&dev->dev, NULL);
  80. if (info->mtd != NULL) {
  81. #ifdef CONFIG_MTD_PARTITIONS
  82. if (info->nr_parts) {
  83. del_mtd_partitions(info->mtd);
  84. kfree(info->parts);
  85. } else {
  86. del_mtd_device(info->mtd);
  87. }
  88. #else
  89. del_mtd_device(info->mtd);
  90. #endif
  91. map_destroy(info->mtd);
  92. }
  93. if (info->map.virt != NULL)
  94. iounmap(info->map.virt);
  95. if (info->res != NULL) {
  96. release_resource(info->res);
  97. kfree(info->res);
  98. }
  99. return 0;
  100. }
  101. static int __devinit of_physmap_probe(struct of_device *dev, const struct of_device_id *match)
  102. {
  103. struct device_node *dp = dev->node;
  104. struct resource res;
  105. struct physmap_flash_info *info;
  106. const char **probe_type;
  107. const char *of_probe;
  108. const u32 *width;
  109. int err;
  110. if (of_address_to_resource(dp, 0, &res)) {
  111. dev_err(&dev->dev, "Can't get the flash mapping!\n");
  112. err = -EINVAL;
  113. goto err_out;
  114. }
  115. dev_dbg(&dev->dev, "physmap flash device: %.8llx at %.8llx\n",
  116. (unsigned long long)res.end - res.start + 1,
  117. (unsigned long long)res.start);
  118. info = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
  119. if (info == NULL) {
  120. err = -ENOMEM;
  121. goto err_out;
  122. }
  123. memset(info, 0, sizeof(*info));
  124. dev_set_drvdata(&dev->dev, info);
  125. info->res = request_mem_region(res.start, res.end - res.start + 1,
  126. dev->dev.bus_id);
  127. if (info->res == NULL) {
  128. dev_err(&dev->dev, "Could not reserve memory region\n");
  129. err = -ENOMEM;
  130. goto err_out;
  131. }
  132. width = of_get_property(dp, "bank-width", NULL);
  133. if (width == NULL) {
  134. dev_err(&dev->dev, "Can't get the flash bank width!\n");
  135. err = -EINVAL;
  136. goto err_out;
  137. }
  138. info->map.name = dev->dev.bus_id;
  139. info->map.phys = res.start;
  140. info->map.size = res.end - res.start + 1;
  141. info->map.bankwidth = *width;
  142. info->map.virt = ioremap(info->map.phys, info->map.size);
  143. if (info->map.virt == NULL) {
  144. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  145. err = EIO;
  146. goto err_out;
  147. }
  148. simple_map_init(&info->map);
  149. of_probe = of_get_property(dp, "probe-type", NULL);
  150. if (of_probe == NULL) {
  151. probe_type = rom_probe_types;
  152. for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
  153. info->mtd = do_map_probe(*probe_type, &info->map);
  154. } else if (!strcmp(of_probe, "CFI"))
  155. info->mtd = do_map_probe("cfi_probe", &info->map);
  156. else if (!strcmp(of_probe, "JEDEC"))
  157. info->mtd = do_map_probe("jedec_probe", &info->map);
  158. else {
  159. if (strcmp(of_probe, "ROM"))
  160. dev_dbg(&dev->dev, "map_probe: don't know probe type "
  161. "'%s', mapping as rom\n", of_probe);
  162. info->mtd = do_map_probe("mtd_rom", &info->map);
  163. }
  164. if (info->mtd == NULL) {
  165. dev_err(&dev->dev, "map_probe failed\n");
  166. err = -ENXIO;
  167. goto err_out;
  168. }
  169. info->mtd->owner = THIS_MODULE;
  170. #ifdef CONFIG_MTD_PARTITIONS
  171. err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
  172. if (err > 0) {
  173. add_mtd_partitions(info->mtd, info->parts, err);
  174. } else if ((err = parse_flash_partitions(dp, &info->parts)) > 0) {
  175. dev_info(&dev->dev, "Using OF partition information\n");
  176. add_mtd_partitions(info->mtd, info->parts, err);
  177. info->nr_parts = err;
  178. } else
  179. #endif
  180. add_mtd_device(info->mtd);
  181. return 0;
  182. err_out:
  183. of_physmap_remove(dev);
  184. return err;
  185. return 0;
  186. }
  187. static struct of_device_id of_physmap_match[] = {
  188. {
  189. .type = "rom",
  190. .compatible = "direct-mapped"
  191. },
  192. { },
  193. };
  194. MODULE_DEVICE_TABLE(of, of_physmap_match);
  195. static struct of_platform_driver of_physmap_flash_driver = {
  196. .name = "physmap-flash",
  197. .match_table = of_physmap_match,
  198. .probe = of_physmap_probe,
  199. .remove = of_physmap_remove,
  200. };
  201. static int __init of_physmap_init(void)
  202. {
  203. return of_register_platform_driver(&of_physmap_flash_driver);
  204. }
  205. static void __exit of_physmap_exit(void)
  206. {
  207. of_unregister_platform_driver(&of_physmap_flash_driver);
  208. }
  209. module_init(of_physmap_init);
  210. module_exit(of_physmap_exit);
  211. MODULE_LICENSE("GPL");
  212. MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
  213. MODULE_DESCRIPTION("Configurable MTD map driver for OF");