gpmc-nand.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_t->cs_on;
  48. t.adv_on = gpmc_t->adv_on;
  49. /* Read */
  50. t.adv_rd_off = gpmc_t->adv_rd_off;
  51. t.oe_on = t.adv_on;
  52. t.access = gpmc_t->access;
  53. t.oe_off = gpmc_t->oe_off;
  54. t.cs_rd_off = gpmc_t->cs_rd_off;
  55. t.rd_cycle = gpmc_t->rd_cycle;
  56. /* Write */
  57. t.adv_wr_off = gpmc_t->adv_wr_off;
  58. t.we_on = t.oe_on;
  59. if (cpu_is_omap34xx()) {
  60. t.wr_data_mux_bus = gpmc_t->wr_data_mux_bus;
  61. t.wr_access = gpmc_t->wr_access;
  62. }
  63. t.we_off = gpmc_t->we_off;
  64. t.cs_wr_off = gpmc_t->cs_wr_off;
  65. t.wr_cycle = 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 gpmc_hwecc_bch_capable(enum omap_ecc ecc_opt)
  80. {
  81. /* support only OMAP3 class */
  82. if (!cpu_is_omap34xx() && !soc_is_am33xx()) {
  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. * and AM33xx derivates. 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. (!soc_is_am33xx())) {
  93. pr_err("BCH 4-bit mode is not supported on this CPU\n");
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. int gpmc_nand_init(struct omap_nand_platform_data *gpmc_nand_data,
  99. struct gpmc_timings *gpmc_t)
  100. {
  101. int err = 0;
  102. struct device *dev = &gpmc_nand_device.dev;
  103. gpmc_nand_device.dev.platform_data = gpmc_nand_data;
  104. err = gpmc_cs_request(gpmc_nand_data->cs, NAND_IO_SIZE,
  105. (unsigned long *)&gpmc_nand_resource[0].start);
  106. if (err < 0) {
  107. dev_err(dev, "Cannot request GPMC CS\n");
  108. return err;
  109. }
  110. gpmc_nand_resource[0].end = gpmc_nand_resource[0].start +
  111. NAND_IO_SIZE - 1;
  112. gpmc_nand_resource[1].start =
  113. gpmc_get_client_irq(GPMC_IRQ_FIFOEVENTENABLE);
  114. gpmc_nand_resource[2].start =
  115. gpmc_get_client_irq(GPMC_IRQ_COUNT_EVENT);
  116. if (gpmc_t) {
  117. err = omap2_nand_gpmc_retime(gpmc_nand_data, gpmc_t);
  118. if (err < 0) {
  119. dev_err(dev, "Unable to set gpmc timings: %d\n", err);
  120. return err;
  121. }
  122. }
  123. /* Enable RD PIN Monitoring Reg */
  124. if (gpmc_nand_data->dev_ready) {
  125. gpmc_cs_configure(gpmc_nand_data->cs, GPMC_CONFIG_RDY_BSY, 1);
  126. }
  127. gpmc_update_nand_reg(&gpmc_nand_data->reg, gpmc_nand_data->cs);
  128. if (!gpmc_hwecc_bch_capable(gpmc_nand_data->ecc_opt))
  129. return -EINVAL;
  130. err = platform_device_register(&gpmc_nand_device);
  131. if (err < 0) {
  132. dev_err(dev, "Unable to register NAND device\n");
  133. goto out_free_cs;
  134. }
  135. return 0;
  136. out_free_cs:
  137. gpmc_cs_free(gpmc_nand_data->cs);
  138. return err;
  139. }