dev-nand.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * S3C series device definition for nand device
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/partitions.h>
  12. #include <mach/map.h>
  13. #include <plat/devs.h>
  14. #include <plat/nand.h>
  15. static struct resource s3c_nand_resource[] = {
  16. [0] = {
  17. .start = S3C_PA_NAND,
  18. .end = S3C_PA_NAND + SZ_1M,
  19. .flags = IORESOURCE_MEM,
  20. }
  21. };
  22. struct platform_device s3c_device_nand = {
  23. .name = "s3c2410-nand",
  24. .id = -1,
  25. .num_resources = ARRAY_SIZE(s3c_nand_resource),
  26. .resource = s3c_nand_resource,
  27. };
  28. EXPORT_SYMBOL(s3c_device_nand);
  29. /**
  30. * s3c_nand_copy_set() - copy nand set data
  31. * @set: The new structure, directly copied from the old.
  32. *
  33. * Copy all the fields from the NAND set field from what is probably __initdata
  34. * to new kernel memory. The code returns 0 if the copy happened correctly or
  35. * an error code for the calling function to display.
  36. *
  37. * Note, we currently do not try and look to see if we've already copied the
  38. * data in a previous set.
  39. */
  40. static int __init s3c_nand_copy_set(struct s3c2410_nand_set *set)
  41. {
  42. void *ptr;
  43. int size;
  44. size = sizeof(struct mtd_partition) * set->nr_partitions;
  45. if (size) {
  46. ptr = kmemdup(set->partitions, size, GFP_KERNEL);
  47. set->partitions = ptr;
  48. if (!ptr)
  49. return -ENOMEM;
  50. }
  51. size = sizeof(int) * set->nr_chips;
  52. if (size) {
  53. ptr = kmemdup(set->nr_map, size, GFP_KERNEL);
  54. set->nr_map = ptr;
  55. if (!ptr)
  56. return -ENOMEM;
  57. }
  58. if (set->ecc_layout) {
  59. ptr = kmemdup(set->ecc_layout,
  60. sizeof(struct nand_ecclayout), GFP_KERNEL);
  61. set->ecc_layout = ptr;
  62. if (!ptr)
  63. return -ENOMEM;
  64. }
  65. return 0;
  66. }
  67. void __init s3c_nand_set_platdata(struct s3c2410_platform_nand *nand)
  68. {
  69. struct s3c2410_platform_nand *npd;
  70. int size;
  71. int ret;
  72. /* note, if we get a failure in allocation, we simply drop out of the
  73. * function. If there is so little memory available at initialisation
  74. * time then there is little chance the system is going to run.
  75. */
  76. npd = kmemdup(nand, sizeof(struct s3c2410_platform_nand), GFP_KERNEL);
  77. if (!npd) {
  78. printk(KERN_ERR "%s: failed copying platform data\n", __func__);
  79. return;
  80. }
  81. /* now see if we need to copy any of the nand set data */
  82. size = sizeof(struct s3c2410_nand_set) * npd->nr_sets;
  83. if (size) {
  84. struct s3c2410_nand_set *from = npd->sets;
  85. struct s3c2410_nand_set *to;
  86. int i;
  87. to = kmemdup(from, size, GFP_KERNEL);
  88. npd->sets = to; /* set, even if we failed */
  89. if (!to) {
  90. printk(KERN_ERR "%s: no memory for sets\n", __func__);
  91. return;
  92. }
  93. for (i = 0; i < npd->nr_sets; i++) {
  94. ret = s3c_nand_copy_set(to);
  95. if (ret) {
  96. printk(KERN_ERR "%s: failed to copy set %d\n",
  97. __func__, i);
  98. return;
  99. }
  100. to++;
  101. }
  102. }
  103. s3c_device_nand.dev.platform_data = npd;
  104. }
  105. EXPORT_SYMBOL_GPL(s3c_nand_set_platdata);