cfi_mtd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * Written by: Piotr Ziecik <kosmo@semihalf.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. #include <common.h>
  26. #include <flash.h>
  27. #include <asm/errno.h>
  28. #include <linux/mtd/mtd.h>
  29. extern flash_info_t flash_info[];
  30. static struct mtd_info cfi_mtd_info[CONFIG_SYS_MAX_FLASH_BANKS];
  31. static char cfi_mtd_names[CONFIG_SYS_MAX_FLASH_BANKS][16];
  32. static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  33. {
  34. flash_info_t *fi = mtd->priv;
  35. size_t a_start = fi->start[0] + instr->addr;
  36. size_t a_end = a_start + instr->len;
  37. int s_first = -1;
  38. int s_last = -1;
  39. int error, sect;
  40. for (sect = 0; sect < fi->sector_count - 1; sect++) {
  41. if (a_start == fi->start[sect])
  42. s_first = sect;
  43. if (a_end == fi->start[sect + 1]) {
  44. s_last = sect;
  45. break;
  46. }
  47. }
  48. if (s_first >= 0 && s_first <= s_last) {
  49. instr->state = MTD_ERASING;
  50. flash_set_verbose(0);
  51. error = flash_erase(fi, s_first, s_last);
  52. flash_set_verbose(1);
  53. if (error) {
  54. instr->state = MTD_ERASE_FAILED;
  55. return -EIO;
  56. }
  57. instr->state = MTD_ERASE_DONE;
  58. mtd_erase_callback(instr);
  59. return 0;
  60. }
  61. return -EINVAL;
  62. }
  63. static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  64. size_t *retlen, u_char *buf)
  65. {
  66. flash_info_t *fi = mtd->priv;
  67. u_char *f = (u_char*)(fi->start[0]) + from;
  68. memcpy(buf, f, len);
  69. *retlen = len;
  70. return 0;
  71. }
  72. static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  73. size_t *retlen, const u_char *buf)
  74. {
  75. flash_info_t *fi = mtd->priv;
  76. u_long t = fi->start[0] + to;
  77. int error;
  78. flash_set_verbose(0);
  79. error = write_buff(fi, (u_char*)buf, t, len);
  80. flash_set_verbose(1);
  81. if (!error) {
  82. *retlen = len;
  83. return 0;
  84. }
  85. return -EIO;
  86. }
  87. static void cfi_mtd_sync(struct mtd_info *mtd)
  88. {
  89. /*
  90. * This function should wait until all pending operations
  91. * finish. However this driver is fully synchronous, so
  92. * this function returns immediately
  93. */
  94. }
  95. static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  96. {
  97. flash_info_t *fi = mtd->priv;
  98. flash_set_verbose(0);
  99. flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
  100. fi->start[0] + ofs + len - 1, fi);
  101. flash_set_verbose(1);
  102. return 0;
  103. }
  104. static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  105. {
  106. flash_info_t *fi = mtd->priv;
  107. flash_set_verbose(0);
  108. flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
  109. fi->start[0] + ofs + len - 1, fi);
  110. flash_set_verbose(1);
  111. return 0;
  112. }
  113. static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
  114. {
  115. int sect_size = 0;
  116. int sect;
  117. for (sect = 0; sect < fi->sector_count; sect++) {
  118. if (!sect_size) {
  119. sect_size = flash_sector_size(fi, sect);
  120. continue;
  121. }
  122. if (sect_size != flash_sector_size(fi, sect)) {
  123. sect_size = 0;
  124. break;
  125. }
  126. }
  127. if (!sect_size) {
  128. puts("cfi-mtd: devices with multiple sector sizes are"
  129. "not supported\n");
  130. return -EINVAL;
  131. }
  132. mtd->erasesize = sect_size;
  133. return 0;
  134. }
  135. int cfi_mtd_init(void)
  136. {
  137. struct mtd_info *mtd;
  138. flash_info_t *fi;
  139. int error, i;
  140. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  141. fi = &flash_info[i];
  142. mtd = &cfi_mtd_info[i];
  143. memset(mtd, 0, sizeof(struct mtd_info));
  144. error = cfi_mtd_set_erasesize(mtd, fi);
  145. if (error)
  146. continue;
  147. sprintf(cfi_mtd_names[i], "nor%d", i);
  148. mtd->name = cfi_mtd_names[i];
  149. mtd->type = MTD_NORFLASH;
  150. mtd->flags = MTD_CAP_NORFLASH;
  151. mtd->size = fi->size;
  152. mtd->writesize = 1;
  153. mtd->erase = cfi_mtd_erase;
  154. mtd->read = cfi_mtd_read;
  155. mtd->write = cfi_mtd_write;
  156. mtd->sync = cfi_mtd_sync;
  157. mtd->lock = cfi_mtd_lock;
  158. mtd->unlock = cfi_mtd_unlock;
  159. mtd->priv = fi;
  160. if (add_mtd_device(mtd))
  161. return -ENOMEM;
  162. }
  163. return 0;
  164. }