gpmc-nand.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/mtd/nand.h>
  15. #include <linux/platform_data/mtd-nand-omap2.h>
  16. #include <asm/mach/flash.h>
  17. #include "gpmc.h"
  18. #include "soc.h"
  19. #include "gpmc-nand.h"
  20. /* minimum size for IO mapping */
  21. #define NAND_IO_SIZE 4
  22. static struct resource gpmc_nand_resource[] = {
  23. {
  24. .flags = IORESOURCE_MEM,
  25. },
  26. {
  27. .flags = IORESOURCE_IRQ,
  28. },
  29. {
  30. .flags = IORESOURCE_IRQ,
  31. },
  32. };
  33. static struct platform_device gpmc_nand_device = {
  34. .name = "omap2-nand",
  35. .id = 0,
  36. .num_resources = ARRAY_SIZE(gpmc_nand_resource),
  37. .resource = gpmc_nand_resource,
  38. };
  39. static bool gpmc_hwecc_bch_capable(enum omap_ecc ecc_opt)
  40. {
  41. /* platforms which support all ECC schemes */
  42. if (soc_is_am33xx() || cpu_is_omap44xx() ||
  43. soc_is_omap54xx() || soc_is_dra7xx())
  44. return 1;
  45. /* OMAP3xxx do not have ELM engine, so cannot support ECC schemes
  46. * which require H/W based ECC error detection */
  47. if ((cpu_is_omap34xx() || cpu_is_omap3630()) &&
  48. ((ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  49. (ecc_opt == OMAP_ECC_BCH8_CODE_HW)))
  50. return 0;
  51. /*
  52. * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1
  53. * and AM33xx derivates. Other chips may be added if confirmed to work.
  54. */
  55. if ((ecc_opt == OMAP_ECC_BCH4_CODE_HW_DETECTION_SW) &&
  56. (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0)))
  57. return 0;
  58. /* legacy platforms support only HAM1 (1-bit Hamming) ECC scheme */
  59. if (ecc_opt == OMAP_ECC_HAM1_CODE_HW)
  60. return 1;
  61. else
  62. return 0;
  63. }
  64. /* This function will go away once the device-tree convertion is complete */
  65. static void gpmc_set_legacy(struct omap_nand_platform_data *gpmc_nand_data,
  66. struct gpmc_settings *s)
  67. {
  68. /* Enable RD PIN Monitoring Reg */
  69. if (gpmc_nand_data->dev_ready) {
  70. s->wait_on_read = true;
  71. s->wait_on_write = true;
  72. }
  73. if (gpmc_nand_data->devsize == NAND_BUSWIDTH_16)
  74. s->device_width = GPMC_DEVWIDTH_16BIT;
  75. else
  76. s->device_width = GPMC_DEVWIDTH_8BIT;
  77. }
  78. int gpmc_nand_init(struct omap_nand_platform_data *gpmc_nand_data,
  79. struct gpmc_timings *gpmc_t)
  80. {
  81. int err = 0;
  82. struct gpmc_settings s;
  83. struct device *dev = &gpmc_nand_device.dev;
  84. memset(&s, 0, sizeof(struct gpmc_settings));
  85. gpmc_nand_device.dev.platform_data = gpmc_nand_data;
  86. err = gpmc_cs_request(gpmc_nand_data->cs, NAND_IO_SIZE,
  87. (unsigned long *)&gpmc_nand_resource[0].start);
  88. if (err < 0) {
  89. dev_err(dev, "Cannot request GPMC CS %d, error %d\n",
  90. gpmc_nand_data->cs, err);
  91. return err;
  92. }
  93. gpmc_nand_resource[0].end = gpmc_nand_resource[0].start +
  94. NAND_IO_SIZE - 1;
  95. gpmc_nand_resource[1].start =
  96. gpmc_get_client_irq(GPMC_IRQ_FIFOEVENTENABLE);
  97. gpmc_nand_resource[2].start =
  98. gpmc_get_client_irq(GPMC_IRQ_COUNT_EVENT);
  99. if (gpmc_t) {
  100. err = gpmc_cs_set_timings(gpmc_nand_data->cs, gpmc_t);
  101. if (err < 0) {
  102. dev_err(dev, "Unable to set gpmc timings: %d\n", err);
  103. return err;
  104. }
  105. }
  106. if (gpmc_nand_data->of_node)
  107. gpmc_read_settings_dt(gpmc_nand_data->of_node, &s);
  108. else
  109. gpmc_set_legacy(gpmc_nand_data, &s);
  110. s.device_nand = true;
  111. err = gpmc_cs_program_settings(gpmc_nand_data->cs, &s);
  112. if (err < 0)
  113. goto out_free_cs;
  114. err = gpmc_configure(GPMC_CONFIG_WP, 0);
  115. if (err < 0)
  116. goto out_free_cs;
  117. gpmc_update_nand_reg(&gpmc_nand_data->reg, gpmc_nand_data->cs);
  118. if (!gpmc_hwecc_bch_capable(gpmc_nand_data->ecc_opt)) {
  119. dev_err(dev, "Unsupported NAND ECC scheme selected\n");
  120. return -EINVAL;
  121. }
  122. err = platform_device_register(&gpmc_nand_device);
  123. if (err < 0) {
  124. dev_err(dev, "Unable to register NAND device\n");
  125. goto out_free_cs;
  126. }
  127. return 0;
  128. out_free_cs:
  129. gpmc_cs_free(gpmc_nand_data->cs);
  130. return err;
  131. }