rbtx4939-flash.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifdef CONFIG_MTD_PARTITIONS
  27. int nr_parts;
  28. struct mtd_partition *parts;
  29. #endif
  30. };
  31. static int rbtx4939_flash_remove(struct platform_device *dev)
  32. {
  33. struct rbtx4939_flash_info *info;
  34. info = platform_get_drvdata(dev);
  35. if (!info)
  36. return 0;
  37. platform_set_drvdata(dev, NULL);
  38. if (info->mtd) {
  39. #ifdef CONFIG_MTD_PARTITIONS
  40. struct rbtx4939_flash_data *pdata = dev->dev.platform_data;
  41. if (info->nr_parts) {
  42. del_mtd_partitions(info->mtd);
  43. kfree(info->parts);
  44. } else if (pdata->nr_parts)
  45. del_mtd_partitions(info->mtd);
  46. else
  47. del_mtd_device(info->mtd);
  48. #else
  49. del_mtd_device(info->mtd);
  50. #endif
  51. map_destroy(info->mtd);
  52. }
  53. return 0;
  54. }
  55. static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
  56. #ifdef CONFIG_MTD_PARTITIONS
  57. static const char *part_probe_types[] = { "cmdlinepart", NULL };
  58. #endif
  59. static int rbtx4939_flash_probe(struct platform_device *dev)
  60. {
  61. struct rbtx4939_flash_data *pdata;
  62. struct rbtx4939_flash_info *info;
  63. struct resource *res;
  64. const char **probe_type;
  65. int err = 0;
  66. unsigned long size;
  67. pdata = dev->dev.platform_data;
  68. if (!pdata)
  69. return -ENODEV;
  70. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  71. if (!res)
  72. return -ENODEV;
  73. info = devm_kzalloc(&dev->dev, sizeof(struct rbtx4939_flash_info),
  74. GFP_KERNEL);
  75. if (!info)
  76. return -ENOMEM;
  77. platform_set_drvdata(dev, info);
  78. size = resource_size(res);
  79. pr_notice("rbtx4939 platform flash device: %pR\n", res);
  80. if (!devm_request_mem_region(&dev->dev, res->start, size,
  81. dev_name(&dev->dev)))
  82. return -EBUSY;
  83. info->map.name = dev_name(&dev->dev);
  84. info->map.phys = res->start;
  85. info->map.size = size;
  86. info->map.bankwidth = pdata->width;
  87. info->map.virt = devm_ioremap(&dev->dev, info->map.phys, size);
  88. if (!info->map.virt)
  89. return -EBUSY;
  90. if (pdata->map_init)
  91. (*pdata->map_init)(&info->map);
  92. else
  93. simple_map_init(&info->map);
  94. probe_type = rom_probe_types;
  95. for (; !info->mtd && *probe_type; probe_type++)
  96. info->mtd = do_map_probe(*probe_type, &info->map);
  97. if (!info->mtd) {
  98. dev_err(&dev->dev, "map_probe failed\n");
  99. err = -ENXIO;
  100. goto err_out;
  101. }
  102. info->mtd->owner = THIS_MODULE;
  103. if (err)
  104. goto err_out;
  105. #ifdef CONFIG_MTD_PARTITIONS
  106. err = parse_mtd_partitions(info->mtd, part_probe_types,
  107. &info->parts, 0);
  108. if (err > 0) {
  109. add_mtd_partitions(info->mtd, info->parts, err);
  110. info->nr_parts = err;
  111. return 0;
  112. }
  113. if (pdata->nr_parts) {
  114. pr_notice("Using rbtx4939 partition information\n");
  115. add_mtd_partitions(info->mtd, pdata->parts, pdata->nr_parts);
  116. return 0;
  117. }
  118. #endif
  119. add_mtd_device(info->mtd);
  120. return 0;
  121. err_out:
  122. rbtx4939_flash_remove(dev);
  123. return err;
  124. }
  125. #ifdef CONFIG_PM
  126. static int rbtx4939_flash_suspend(struct platform_device *dev,
  127. pm_message_t state)
  128. {
  129. struct rbtx4939_flash_info *info = platform_get_drvdata(dev);
  130. if (info->mtd->suspend)
  131. return info->mtd->suspend(info->mtd);
  132. return 0;
  133. }
  134. static int rbtx4939_flash_resume(struct platform_device *dev)
  135. {
  136. struct rbtx4939_flash_info *info = platform_get_drvdata(dev);
  137. if (info->mtd->resume)
  138. info->mtd->resume(info->mtd);
  139. return 0;
  140. }
  141. static void rbtx4939_flash_shutdown(struct platform_device *dev)
  142. {
  143. struct rbtx4939_flash_info *info = platform_get_drvdata(dev);
  144. if (info->mtd->suspend && info->mtd->resume)
  145. if (info->mtd->suspend(info->mtd) == 0)
  146. info->mtd->resume(info->mtd);
  147. }
  148. #else
  149. #define rbtx4939_flash_suspend NULL
  150. #define rbtx4939_flash_resume NULL
  151. #define rbtx4939_flash_shutdown NULL
  152. #endif
  153. static struct platform_driver rbtx4939_flash_driver = {
  154. .probe = rbtx4939_flash_probe,
  155. .remove = rbtx4939_flash_remove,
  156. .suspend = rbtx4939_flash_suspend,
  157. .resume = rbtx4939_flash_resume,
  158. .shutdown = rbtx4939_flash_shutdown,
  159. .driver = {
  160. .name = "rbtx4939-flash",
  161. .owner = THIS_MODULE,
  162. },
  163. };
  164. static int __init rbtx4939_flash_init(void)
  165. {
  166. return platform_driver_register(&rbtx4939_flash_driver);
  167. }
  168. static void __exit rbtx4939_flash_exit(void)
  169. {
  170. platform_driver_unregister(&rbtx4939_flash_driver);
  171. }
  172. module_init(rbtx4939_flash_init);
  173. module_exit(rbtx4939_flash_exit);
  174. MODULE_LICENSE("GPL");
  175. MODULE_DESCRIPTION("RBTX4939 MTD map driver");
  176. MODULE_ALIAS("platform:rbtx4939-flash");