rbtx4939-flash.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * rbtx4939-flash (based on physmap.c)
  3. *
  4. * This is a simplified physmap driver with map_init callback function.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  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/platform_device.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/map.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <asm/txx9/rbtx4939.h>
  23. struct rbtx4939_flash_info {
  24. struct mtd_info *mtd;
  25. struct map_info map;
  26. int nr_parts;
  27. struct mtd_partition *parts;
  28. };
  29. static int rbtx4939_flash_remove(struct platform_device *dev)
  30. {
  31. struct rbtx4939_flash_info *info;
  32. info = platform_get_drvdata(dev);
  33. if (!info)
  34. return 0;
  35. platform_set_drvdata(dev, NULL);
  36. if (info->mtd) {
  37. struct rbtx4939_flash_data *pdata = dev->dev.platform_data;
  38. if (info->nr_parts)
  39. kfree(info->parts);
  40. mtd_device_unregister(info->mtd);
  41. map_destroy(info->mtd);
  42. }
  43. return 0;
  44. }
  45. static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
  46. static const char *part_probe_types[] = { "cmdlinepart", NULL };
  47. static int rbtx4939_flash_probe(struct platform_device *dev)
  48. {
  49. struct rbtx4939_flash_data *pdata;
  50. struct rbtx4939_flash_info *info;
  51. struct resource *res;
  52. const char **probe_type;
  53. int err = 0;
  54. unsigned long size;
  55. pdata = dev->dev.platform_data;
  56. if (!pdata)
  57. return -ENODEV;
  58. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  59. if (!res)
  60. return -ENODEV;
  61. info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info),
  62. GFP_KERNEL);
  63. if (!info)
  64. return -ENOMEM;
  65. platform_set_drvdata(dev, info);
  66. size = resource_size(res);
  67. pr_notice("rbtx4939 platform flash device: %pR\n", res);
  68. if (!devm_request_mem_region(&dev->dev, res->start, size,
  69. dev_name(&dev->dev)))
  70. return -EBUSY;
  71. info->map.name = dev_name(&dev->dev);
  72. info->map.phys = res->start;
  73. info->map.size = size;
  74. info->map.bankwidth = pdata->width;
  75. info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size);
  76. if (!info->map.virt)
  77. return -EBUSY;
  78. if (pdata->map_init)
  79. (*pdata->map_init)(&info->map);
  80. else
  81. simple_map_init(&info->map);
  82. probe_type = rom_probe_types;
  83. for (; !info->mtd && *probe_type; probe_type++)
  84. info->mtd = do_map_probe(*probe_type, &info->map);
  85. if (!info->mtd) {
  86. dev_err(&dev->dev, "map_probe failed\n");
  87. err = -ENXIO;
  88. goto err_out;
  89. }
  90. info->mtd->owner = THIS_MODULE;
  91. if (err)
  92. goto err_out;
  93. err = parse_mtd_partitions(info->mtd, part_probe_types,
  94. &info->parts, 0);
  95. if (err > 0) {
  96. mtd_device_register(info->mtd, info->parts, err);
  97. info->nr_parts = err;
  98. return 0;
  99. }
  100. if (pdata->nr_parts) {
  101. pr_notice("Using rbtx4939 partition information\n");
  102. mtd_device_register(info->mtd, pdata->parts, pdata->nr_parts);
  103. return 0;
  104. }
  105. mtd_device_register(info->mtd, NULL, 0);
  106. return 0;
  107. err_out:
  108. rbtx4939_flash_remove(dev);
  109. return err;
  110. }
  111. #ifdef CONFIG_PM
  112. static void rbtx4939_flash_shutdown(struct platform_device *dev)
  113. {
  114. struct rbtx4939_flash_info *info = platform_get_drvdata(dev);
  115. if (info->mtd->suspend && info->mtd->resume)
  116. if (info->mtd->suspend(info->mtd) == 0)
  117. info->mtd->resume(info->mtd);
  118. }
  119. #else
  120. #define rbtx4939_flash_shutdown NULL
  121. #endif
  122. static struct platform_driver rbtx4939_flash_driver = {
  123. .probe = rbtx4939_flash_probe,
  124. .remove = rbtx4939_flash_remove,
  125. .shutdown = rbtx4939_flash_shutdown,
  126. .driver = {
  127. .name = "rbtx4939-flash",
  128. .owner = THIS_MODULE,
  129. },
  130. };
  131. static int __init rbtx4939_flash_init(void)
  132. {
  133. return platform_driver_register(&rbtx4939_flash_driver);
  134. }
  135. static void __exit rbtx4939_flash_exit(void)
  136. {
  137. platform_driver_unregister(&rbtx4939_flash_driver);
  138. }
  139. module_init(rbtx4939_flash_init);
  140. module_exit(rbtx4939_flash_exit);
  141. MODULE_LICENSE("GPL");
  142. MODULE_DESCRIPTION("RBTX4939 MTD map driver");
  143. MODULE_ALIAS("platform:rbtx4939-flash");