gpmc-nand.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 int omap2_nand_gpmc_retime(
  40. struct omap_nand_platform_data *gpmc_nand_data,
  41. struct gpmc_timings *gpmc_t)
  42. {
  43. struct gpmc_timings t;
  44. int err;
  45. memset(&t, 0, sizeof(t));
  46. t.sync_clk = gpmc_t->sync_clk;
  47. t.cs_on = gpmc_round_ns_to_ticks(gpmc_t->cs_on);
  48. t.adv_on = gpmc_round_ns_to_ticks(gpmc_t->adv_on);
  49. /* Read */
  50. t.adv_rd_off = gpmc_round_ns_to_ticks(gpmc_t->adv_rd_off);
  51. t.oe_on = t.adv_on;
  52. t.access = gpmc_round_ns_to_ticks(gpmc_t->access);
  53. t.oe_off = gpmc_round_ns_to_ticks(gpmc_t->oe_off);
  54. t.cs_rd_off = gpmc_round_ns_to_ticks(gpmc_t->cs_rd_off);
  55. t.rd_cycle = gpmc_round_ns_to_ticks(gpmc_t->rd_cycle);
  56. /* Write */
  57. t.adv_wr_off = gpmc_round_ns_to_ticks(gpmc_t->adv_wr_off);
  58. t.we_on = t.oe_on;
  59. if (cpu_is_omap34xx()) {
  60. t.wr_data_mux_bus = gpmc_round_ns_to_ticks(gpmc_t->wr_data_mux_bus);
  61. t.wr_access = gpmc_round_ns_to_ticks(gpmc_t->wr_access);
  62. }
  63. t.we_off = gpmc_round_ns_to_ticks(gpmc_t->we_off);
  64. t.cs_wr_off = gpmc_round_ns_to_ticks(gpmc_t->cs_wr_off);
  65. t.wr_cycle = gpmc_round_ns_to_ticks(gpmc_t->wr_cycle);
  66. /* Configure GPMC */
  67. if (gpmc_nand_data->devsize == NAND_BUSWIDTH_16)
  68. gpmc_cs_configure(gpmc_nand_data->cs, GPMC_CONFIG_DEV_SIZE, 1);
  69. else
  70. gpmc_cs_configure(gpmc_nand_data->cs, GPMC_CONFIG_DEV_SIZE, 0);
  71. gpmc_cs_configure(gpmc_nand_data->cs,
  72. GPMC_CONFIG_DEV_TYPE, GPMC_DEVICETYPE_NAND);
  73. gpmc_cs_configure(gpmc_nand_data->cs, GPMC_CONFIG_WP, 0);
  74. err = gpmc_cs_set_timings(gpmc_nand_data->cs, &t);
  75. if (err)
  76. return err;
  77. return 0;
  78. }
  79. static bool __init gpmc_hwecc_bch_capable(enum omap_ecc ecc_opt)
  80. {
  81. /* support only OMAP3 class */
  82. if (!cpu_is_omap34xx()) {
  83. pr_err("BCH ecc is not supported on this CPU\n");
  84. return 0;
  85. }
  86. /*
  87. * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1.
  88. * Other chips may be added if confirmed to work.
  89. */
  90. if ((ecc_opt == OMAP_ECC_BCH4_CODE_HW) &&
  91. (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0))) {
  92. pr_err("BCH 4-bit mode is not supported on this CPU\n");
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. int __init gpmc_nand_init(struct omap_nand_platform_data *gpmc_nand_data,
  98. struct gpmc_timings *gpmc_t)
  99. {
  100. int err = 0;
  101. struct device *dev = &gpmc_nand_device.dev;
  102. gpmc_nand_device.dev.platform_data = gpmc_nand_data;
  103. err = gpmc_cs_request(gpmc_nand_data->cs, NAND_IO_SIZE,
  104. (unsigned long *)&gpmc_nand_resource[0].start);
  105. if (err < 0) {
  106. dev_err(dev, "Cannot request GPMC CS\n");
  107. return err;
  108. }
  109. gpmc_nand_resource[0].end = gpmc_nand_resource[0].start +
  110. NAND_IO_SIZE - 1;
  111. gpmc_nand_resource[1].start =
  112. gpmc_get_client_irq(GPMC_IRQ_FIFOEVENTENABLE);
  113. gpmc_nand_resource[2].start =
  114. gpmc_get_client_irq(GPMC_IRQ_COUNT_EVENT);
  115. if (gpmc_t) {
  116. err = omap2_nand_gpmc_retime(gpmc_nand_data, gpmc_t);
  117. if (err < 0) {
  118. dev_err(dev, "Unable to set gpmc timings: %d\n", err);
  119. return err;
  120. }
  121. }
  122. /* Enable RD PIN Monitoring Reg */
  123. if (gpmc_nand_data->dev_ready) {
  124. gpmc_cs_configure(gpmc_nand_data->cs, GPMC_CONFIG_RDY_BSY, 1);
  125. }
  126. gpmc_update_nand_reg(&gpmc_nand_data->reg, gpmc_nand_data->cs);
  127. if (!gpmc_hwecc_bch_capable(gpmc_nand_data->ecc_opt))
  128. return -EINVAL;
  129. err = platform_device_register(&gpmc_nand_device);
  130. if (err < 0) {
  131. dev_err(dev, "Unable to register NAND device\n");
  132. goto out_free_cs;
  133. }
  134. return 0;
  135. out_free_cs:
  136. gpmc_cs_free(gpmc_nand_data->cs);
  137. return err;
  138. }