cfi_mtd.c 4.3 KB

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