gpmc-nand.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * gpmc-nand.c
  3. *
  4. * Copyright (C) 2009 Texas Instruments
  5. * Vimal Singh <vimalsingh@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/io.h>
  14. #include <linux/omap-gpmc.h>
  15. #include <linux/mtd/nand.h>
  16. #include <linux/platform_data/mtd-nand-omap2.h>
  17. #include <asm/mach/flash.h>
  18. #include "soc.h"
  19. /* minimum size for IO mapping */
  20. #define NAND_IO_SIZE 4
  21. static bool gpmc_hwecc_bch_capable(enum omap_ecc ecc_opt)
  22. {
  23. /* platforms which support all ECC schemes */
  24. if (soc_is_am33xx() || soc_is_am43xx() || cpu_is_omap44xx() ||
  25. soc_is_omap54xx() || soc_is_dra7xx())
  26. return 1;
  27. if (ecc_opt == OMAP_ECC_BCH4_CODE_HW_DETECTION_SW ||
  28. ecc_opt == OMAP_ECC_BCH8_CODE_HW_DETECTION_SW) {
  29. if (cpu_is_omap24xx())
  30. return 0;
  31. else if (cpu_is_omap3630() && (GET_OMAP_REVISION() == 0))
  32. return 0;
  33. else
  34. return 1;
  35. }
  36. /* OMAP3xxx do not have ELM engine, so cannot support ECC schemes
  37. * which require H/W based ECC error detection */
  38. if ((cpu_is_omap34xx() || cpu_is_omap3630()) &&
  39. ((ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  40. (ecc_opt == OMAP_ECC_BCH8_CODE_HW)))
  41. return 0;
  42. /* legacy platforms support only HAM1 (1-bit Hamming) ECC scheme */
  43. if (ecc_opt == OMAP_ECC_HAM1_CODE_HW ||
  44. ecc_opt == OMAP_ECC_HAM1_CODE_SW)
  45. return 1;
  46. else
  47. return 0;
  48. }
  49. /* This function will go away once the device-tree convertion is complete */
  50. static void gpmc_set_legacy(struct omap_nand_platform_data *gpmc_nand_data,
  51. struct gpmc_settings *s)
  52. {
  53. /* Enable RD PIN Monitoring Reg */
  54. if (gpmc_nand_data->dev_ready) {
  55. s->wait_on_read = true;
  56. s->wait_on_write = true;
  57. }
  58. if (gpmc_nand_data->devsize == NAND_BUSWIDTH_16)
  59. s->device_width = GPMC_DEVWIDTH_16BIT;
  60. else
  61. s->device_width = GPMC_DEVWIDTH_8BIT;
  62. }
  63. int gpmc_nand_init(struct omap_nand_platform_data *gpmc_nand_data,
  64. struct gpmc_timings *gpmc_t)
  65. {
  66. int err = 0;
  67. struct gpmc_settings s;
  68. struct platform_device *pdev;
  69. struct resource gpmc_nand_res[] = {
  70. { .flags = IORESOURCE_MEM, },
  71. { .flags = IORESOURCE_IRQ, },
  72. { .flags = IORESOURCE_IRQ, },
  73. };
  74. BUG_ON(gpmc_nand_data->cs >= GPMC_CS_NUM);
  75. err = gpmc_cs_request(gpmc_nand_data->cs, NAND_IO_SIZE,
  76. (unsigned long *)&gpmc_nand_res[0].start);
  77. if (err < 0) {
  78. pr_err("omap2-gpmc: Cannot request GPMC CS %d, error %d\n",
  79. gpmc_nand_data->cs, err);
  80. return err;
  81. }
  82. gpmc_nand_res[0].end = gpmc_nand_res[0].start + NAND_IO_SIZE - 1;
  83. gpmc_nand_res[1].start = gpmc_get_client_irq(GPMC_IRQ_FIFOEVENTENABLE);
  84. gpmc_nand_res[2].start = gpmc_get_client_irq(GPMC_IRQ_COUNT_EVENT);
  85. memset(&s, 0, sizeof(struct gpmc_settings));
  86. gpmc_set_legacy(gpmc_nand_data, &s);
  87. s.device_nand = true;
  88. if (gpmc_t) {
  89. err = gpmc_cs_set_timings(gpmc_nand_data->cs, gpmc_t, &s);
  90. if (err < 0) {
  91. pr_err("omap2-gpmc: Unable to set gpmc timings: %d\n",
  92. err);
  93. return err;
  94. }
  95. }
  96. err = gpmc_cs_program_settings(gpmc_nand_data->cs, &s);
  97. if (err < 0)
  98. goto out_free_cs;
  99. err = gpmc_configure(GPMC_CONFIG_WP, 0);
  100. if (err < 0)
  101. goto out_free_cs;
  102. if (!gpmc_hwecc_bch_capable(gpmc_nand_data->ecc_opt)) {
  103. pr_err("omap2-nand: Unsupported NAND ECC scheme selected\n");
  104. err = -EINVAL;
  105. goto out_free_cs;
  106. }
  107. pdev = platform_device_alloc("omap2-nand", gpmc_nand_data->cs);
  108. if (pdev) {
  109. err = platform_device_add_resources(pdev, gpmc_nand_res,
  110. ARRAY_SIZE(gpmc_nand_res));
  111. if (!err)
  112. pdev->dev.platform_data = gpmc_nand_data;
  113. } else {
  114. err = -ENOMEM;
  115. }
  116. if (err)
  117. goto out_free_pdev;
  118. err = platform_device_add(pdev);
  119. if (err) {
  120. dev_err(&pdev->dev, "Unable to register NAND device\n");
  121. goto out_free_pdev;
  122. }
  123. return 0;
  124. out_free_pdev:
  125. platform_device_put(pdev);
  126. out_free_cs:
  127. gpmc_cs_free(gpmc_nand_data->cs);
  128. return err;
  129. }