gpmc-nand.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /* support only OMAP3 class */
  42. if (!cpu_is_omap34xx() && !soc_is_am33xx()) {
  43. pr_err("BCH ecc is not supported on this CPU\n");
  44. return 0;
  45. }
  46. /*
  47. * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1
  48. * and AM33xx derivates. Other chips may be added if confirmed to work.
  49. */
  50. if ((ecc_opt == OMAP_ECC_BCH4_CODE_HW) &&
  51. (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0)) &&
  52. (!soc_is_am33xx())) {
  53. pr_err("BCH 4-bit mode is not supported on this CPU\n");
  54. return 0;
  55. }
  56. return 1;
  57. }
  58. int gpmc_nand_init(struct omap_nand_platform_data *gpmc_nand_data,
  59. struct gpmc_timings *gpmc_t)
  60. {
  61. int err = 0;
  62. struct gpmc_settings s;
  63. struct device *dev = &gpmc_nand_device.dev;
  64. memset(&s, 0, sizeof(struct gpmc_settings));
  65. gpmc_nand_device.dev.platform_data = gpmc_nand_data;
  66. err = gpmc_cs_request(gpmc_nand_data->cs, NAND_IO_SIZE,
  67. (unsigned long *)&gpmc_nand_resource[0].start);
  68. if (err < 0) {
  69. dev_err(dev, "Cannot request GPMC CS %d, error %d\n",
  70. gpmc_nand_data->cs, err);
  71. return err;
  72. }
  73. gpmc_nand_resource[0].end = gpmc_nand_resource[0].start +
  74. NAND_IO_SIZE - 1;
  75. gpmc_nand_resource[1].start =
  76. gpmc_get_client_irq(GPMC_IRQ_FIFOEVENTENABLE);
  77. gpmc_nand_resource[2].start =
  78. gpmc_get_client_irq(GPMC_IRQ_COUNT_EVENT);
  79. if (gpmc_t) {
  80. err = gpmc_cs_set_timings(gpmc_nand_data->cs, gpmc_t);
  81. if (err < 0) {
  82. dev_err(dev, "Unable to set gpmc timings: %d\n", err);
  83. return err;
  84. }
  85. if (gpmc_nand_data->of_node) {
  86. gpmc_read_settings_dt(gpmc_nand_data->of_node, &s);
  87. } else {
  88. /* Enable RD PIN Monitoring Reg */
  89. if (gpmc_nand_data->dev_ready) {
  90. s.wait_on_read = true;
  91. s.wait_on_write = true;
  92. }
  93. }
  94. s.device_nand = true;
  95. if (gpmc_nand_data->devsize == NAND_BUSWIDTH_16)
  96. s.device_width = GPMC_DEVWIDTH_16BIT;
  97. else
  98. s.device_width = GPMC_DEVWIDTH_8BIT;
  99. err = gpmc_cs_program_settings(gpmc_nand_data->cs, &s);
  100. if (err < 0)
  101. goto out_free_cs;
  102. err = gpmc_configure(GPMC_CONFIG_WP, 0);
  103. if (err < 0)
  104. goto out_free_cs;
  105. }
  106. gpmc_update_nand_reg(&gpmc_nand_data->reg, gpmc_nand_data->cs);
  107. if (!gpmc_hwecc_bch_capable(gpmc_nand_data->ecc_opt))
  108. return -EINVAL;
  109. err = platform_device_register(&gpmc_nand_device);
  110. if (err < 0) {
  111. dev_err(dev, "Unable to register NAND device\n");
  112. goto out_free_cs;
  113. }
  114. return 0;
  115. out_free_cs:
  116. gpmc_cs_free(gpmc_nand_data->cs);
  117. return err;
  118. }