You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

167 lines
4.6 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007, 2008 by Ben Dooks *
  3. * ben@fluff.org *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. /*
  19. * S3C2440 OpenOCD NAND Flash controller support.
  20. *
  21. * Many thanks to Simtec Electronics for sponsoring this work.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "s3c24xx.h"
  27. NAND_DEVICE_COMMAND_HANDLER(s3c2440_nand_device_command)
  28. {
  29. struct s3c24xx_nand_controller *info;
  30. CALL_S3C24XX_DEVICE_COMMAND(nand, &info);
  31. /* fill in the address fields for the core device */
  32. info->cmd = S3C2440_NFCMD;
  33. info->addr = S3C2440_NFADDR;
  34. info->data = S3C2440_NFDATA;
  35. info->nfstat = S3C2440_NFSTAT;
  36. return ERROR_OK;
  37. }
  38. static int s3c2440_init(struct nand_device *nand)
  39. {
  40. struct target *target = nand->target;
  41. target_write_u32(target, S3C2410_NFCONF,
  42. S3C2440_NFCONF_TACLS(3) |
  43. S3C2440_NFCONF_TWRPH0(7) |
  44. S3C2440_NFCONF_TWRPH1(7));
  45. target_write_u32(target, S3C2440_NFCONT,
  46. S3C2440_NFCONT_INITECC | S3C2440_NFCONT_ENABLE);
  47. return ERROR_OK;
  48. }
  49. int s3c2440_nand_ready(struct nand_device *nand, int timeout)
  50. {
  51. struct s3c24xx_nand_controller *s3c24xx_info = nand->controller_priv;
  52. struct target *target = nand->target;
  53. uint8_t status;
  54. if (target->state != TARGET_HALTED) {
  55. LOG_ERROR("target must be halted to use S3C24XX NAND flash controller");
  56. return ERROR_NAND_OPERATION_FAILED;
  57. }
  58. do {
  59. target_read_u8(target, s3c24xx_info->nfstat, &status);
  60. if (status & S3C2440_NFSTAT_READY)
  61. return 1;
  62. alive_sleep(1);
  63. } while (timeout-- > 0);
  64. return 0;
  65. }
  66. /* use the fact we can read/write 4 bytes in one go via a single 32bit op */
  67. int s3c2440_read_block_data(struct nand_device *nand, uint8_t *data, int data_size)
  68. {
  69. struct s3c24xx_nand_controller *s3c24xx_info = nand->controller_priv;
  70. struct target *target = nand->target;
  71. uint32_t nfdata = s3c24xx_info->data;
  72. uint32_t tmp;
  73. LOG_INFO("%s: reading data: %p, %p, %d", __func__, nand, data, data_size);
  74. if (target->state != TARGET_HALTED) {
  75. LOG_ERROR("target must be halted to use S3C24XX NAND flash controller");
  76. return ERROR_NAND_OPERATION_FAILED;
  77. }
  78. while (data_size >= 4) {
  79. target_read_u32(target, nfdata, &tmp);
  80. data[0] = tmp;
  81. data[1] = tmp >> 8;
  82. data[2] = tmp >> 16;
  83. data[3] = tmp >> 24;
  84. data_size -= 4;
  85. data += 4;
  86. }
  87. while (data_size > 0) {
  88. target_read_u8(target, nfdata, data);
  89. data_size -= 1;
  90. data += 1;
  91. }
  92. return ERROR_OK;
  93. }
  94. int s3c2440_write_block_data(struct nand_device *nand, uint8_t *data, int data_size)
  95. {
  96. struct s3c24xx_nand_controller *s3c24xx_info = nand->controller_priv;
  97. struct target *target = nand->target;
  98. uint32_t nfdata = s3c24xx_info->data;
  99. uint32_t tmp;
  100. if (target->state != TARGET_HALTED) {
  101. LOG_ERROR("target must be halted to use S3C24XX NAND flash controller");
  102. return ERROR_NAND_OPERATION_FAILED;
  103. }
  104. while (data_size >= 4) {
  105. tmp = le_to_h_u32(data);
  106. target_write_u32(target, nfdata, tmp);
  107. data_size -= 4;
  108. data += 4;
  109. }
  110. while (data_size > 0) {
  111. target_write_u8(target, nfdata, *data);
  112. data_size -= 1;
  113. data += 1;
  114. }
  115. return ERROR_OK;
  116. }
  117. struct nand_flash_controller s3c2440_nand_controller = {
  118. .name = "s3c2440",
  119. .nand_device_command = &s3c2440_nand_device_command,
  120. .init = &s3c2440_init,
  121. .reset = &s3c24xx_reset,
  122. .command = &s3c24xx_command,
  123. .address = &s3c24xx_address,
  124. .write_data = &s3c24xx_write_data,
  125. .read_data = &s3c24xx_read_data,
  126. .write_page = s3c24xx_write_page,
  127. .read_page = s3c24xx_read_page,
  128. .write_block_data = &s3c2440_write_block_data,
  129. .read_block_data = &s3c2440_read_block_data,
  130. .nand_ready = &s3c2440_nand_ready,
  131. };