cfi_mtd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  32. {
  33. flash_info_t *fi = mtd->priv;
  34. size_t a_start = fi->start[0] + instr->addr;
  35. size_t a_end = a_start + instr->len;
  36. int s_first = -1;
  37. int s_last = -1;
  38. int error, sect;
  39. for (sect = 0; sect < fi->sector_count - 1; sect++) {
  40. if (a_start == fi->start[sect])
  41. s_first = sect;
  42. if (a_end == fi->start[sect + 1]) {
  43. s_last = sect;
  44. break;
  45. }
  46. }
  47. if (s_first >= 0 && s_first <= s_last) {
  48. instr->state = MTD_ERASING;
  49. flash_set_verbose(0);
  50. error = flash_erase(fi, s_first, s_last);
  51. flash_set_verbose(1);
  52. if (error) {
  53. instr->state = MTD_ERASE_FAILED;
  54. return -EIO;
  55. }
  56. instr->state = MTD_ERASE_DONE;
  57. mtd_erase_callback(instr);
  58. return 0;
  59. }
  60. return -EINVAL;
  61. }
  62. static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  63. size_t *retlen, u_char *buf)
  64. {
  65. flash_info_t *fi = mtd->priv;
  66. u_char *f = (u_char*)(fi->start[0]) + from;
  67. memcpy(buf, f, len);
  68. *retlen = len;
  69. return 0;
  70. }
  71. static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  72. size_t *retlen, const u_char *buf)
  73. {
  74. flash_info_t *fi = mtd->priv;
  75. u_long t = fi->start[0] + to;
  76. int error;
  77. flash_set_verbose(0);
  78. error = write_buff(fi, (u_char*)buf, t, len);
  79. flash_set_verbose(1);
  80. if (!error) {
  81. *retlen = len;
  82. return 0;
  83. }
  84. return -EIO;
  85. }
  86. static void cfi_mtd_sync(struct mtd_info *mtd)
  87. {
  88. /*
  89. * This function should wait until all pending operations
  90. * finish. However this driver is fully synchronous, so
  91. * this function returns immediately
  92. */
  93. }
  94. static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  95. {
  96. flash_info_t *fi = mtd->priv;
  97. flash_set_verbose(0);
  98. flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
  99. fi->start[0] + ofs + len - 1, fi);
  100. flash_set_verbose(1);
  101. return 0;
  102. }
  103. static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  104. {
  105. flash_info_t *fi = mtd->priv;
  106. flash_set_verbose(0);
  107. flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
  108. fi->start[0] + ofs + len - 1, fi);
  109. flash_set_verbose(1);
  110. return 0;
  111. }
  112. static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
  113. {
  114. int sect_size = 0;
  115. int sect;
  116. for (sect = 0; sect < fi->sector_count; sect++) {
  117. if (!sect_size) {
  118. sect_size = flash_sector_size(fi, sect);
  119. continue;
  120. }
  121. if (sect_size != flash_sector_size(fi, sect)) {
  122. sect_size = 0;
  123. break;
  124. }
  125. }
  126. if (!sect_size) {
  127. puts("cfi-mtd: devices with multiple sector sizes are"
  128. "not supported\n");
  129. return -EINVAL;
  130. }
  131. mtd->erasesize = sect_size;
  132. return 0;
  133. }
  134. int cfi_mtd_init(void)
  135. {
  136. struct mtd_info *mtd;
  137. flash_info_t *fi;
  138. int error, i;
  139. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  140. fi = &flash_info[i];
  141. mtd = &cfi_mtd_info[i];
  142. memset(mtd, 0, sizeof(struct mtd_info));
  143. error = cfi_mtd_set_erasesize(mtd, fi);
  144. if (error)
  145. continue;
  146. mtd->name = CFI_MTD_DEV_NAME;
  147. mtd->type = MTD_NORFLASH;
  148. mtd->flags = MTD_CAP_NORFLASH;
  149. mtd->size = fi->size;
  150. mtd->writesize = 1;
  151. mtd->erase = cfi_mtd_erase;
  152. mtd->read = cfi_mtd_read;
  153. mtd->write = cfi_mtd_write;
  154. mtd->sync = cfi_mtd_sync;
  155. mtd->lock = cfi_mtd_lock;
  156. mtd->unlock = cfi_mtd_unlock;
  157. mtd->priv = fi;
  158. if (add_mtd_device(mtd))
  159. return -ENOMEM;
  160. }
  161. return 0;
  162. }