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