cfi_mtd.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <malloc.h>
  28. #include <asm/errno.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/concat.h>
  31. /* use CONFIG_SYS_MAX_FLASH_BANKS_DETECT if defined */
  32. #ifdef CONFIG_SYS_MAX_FLASH_BANKS_DETECT
  33. # define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS_DETECT
  34. #else
  35. # define CFI_MAX_FLASH_BANKS CONFIG_SYS_MAX_FLASH_BANKS
  36. #endif
  37. extern flash_info_t flash_info[];
  38. static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS];
  39. static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16];
  40. #ifdef CONFIG_MTD_CONCAT
  41. static char c_mtd_name[16];
  42. #endif
  43. static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  44. {
  45. flash_info_t *fi = mtd->priv;
  46. size_t a_start = fi->start[0] + instr->addr;
  47. size_t a_end = a_start + instr->len;
  48. int s_first = -1;
  49. int s_last = -1;
  50. int error, sect;
  51. for (sect = 0; sect < fi->sector_count; sect++) {
  52. if (a_start == fi->start[sect])
  53. s_first = sect;
  54. if (sect < fi->sector_count - 1) {
  55. if (a_end == fi->start[sect + 1]) {
  56. s_last = sect;
  57. break;
  58. }
  59. } else {
  60. s_last = sect;
  61. break;
  62. }
  63. }
  64. if (s_first >= 0 && s_first <= s_last) {
  65. instr->state = MTD_ERASING;
  66. flash_set_verbose(0);
  67. error = flash_erase(fi, s_first, s_last);
  68. flash_set_verbose(1);
  69. if (error) {
  70. instr->state = MTD_ERASE_FAILED;
  71. return -EIO;
  72. }
  73. instr->state = MTD_ERASE_DONE;
  74. mtd_erase_callback(instr);
  75. return 0;
  76. }
  77. return -EINVAL;
  78. }
  79. static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  80. size_t *retlen, u_char *buf)
  81. {
  82. flash_info_t *fi = mtd->priv;
  83. u_char *f = (u_char*)(fi->start[0]) + from;
  84. memcpy(buf, f, len);
  85. *retlen = len;
  86. return 0;
  87. }
  88. static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  89. size_t *retlen, const u_char *buf)
  90. {
  91. flash_info_t *fi = mtd->priv;
  92. u_long t = fi->start[0] + to;
  93. int error;
  94. flash_set_verbose(0);
  95. error = write_buff(fi, (u_char*)buf, t, len);
  96. flash_set_verbose(1);
  97. if (!error) {
  98. *retlen = len;
  99. return 0;
  100. }
  101. return -EIO;
  102. }
  103. static void cfi_mtd_sync(struct mtd_info *mtd)
  104. {
  105. /*
  106. * This function should wait until all pending operations
  107. * finish. However this driver is fully synchronous, so
  108. * this function returns immediately
  109. */
  110. }
  111. static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  112. {
  113. flash_info_t *fi = mtd->priv;
  114. flash_set_verbose(0);
  115. flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
  116. fi->start[0] + ofs + len - 1, fi);
  117. flash_set_verbose(1);
  118. return 0;
  119. }
  120. static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  121. {
  122. flash_info_t *fi = mtd->priv;
  123. flash_set_verbose(0);
  124. flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
  125. fi->start[0] + ofs + len - 1, fi);
  126. flash_set_verbose(1);
  127. return 0;
  128. }
  129. static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
  130. {
  131. int sect_size = 0;
  132. int sect_size_old = 0;
  133. int sect;
  134. int regions = 0;
  135. int numblocks = 0;
  136. ulong offset = 0;
  137. ulong base_addr = fi->start[0];
  138. /*
  139. * First detect the number of eraseregions so that we can allocate
  140. * the array of eraseregions correctly
  141. */
  142. for (sect = 0; sect < fi->sector_count; sect++) {
  143. if (sect_size_old != flash_sector_size(fi, sect))
  144. regions++;
  145. sect_size_old = flash_sector_size(fi, sect);
  146. }
  147. mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);
  148. /*
  149. * Now detect the largest sector and fill the eraseregions
  150. */
  151. sect_size_old = 0;
  152. regions = 0;
  153. for (sect = 0; sect < fi->sector_count; sect++) {
  154. if ((sect_size_old != flash_sector_size(fi, sect)) &&
  155. (sect_size_old != 0)) {
  156. mtd->eraseregions[regions].offset = offset - base_addr;
  157. mtd->eraseregions[regions].erasesize = sect_size_old;
  158. mtd->eraseregions[regions].numblocks = numblocks;
  159. /* Now start counting the next eraseregions */
  160. numblocks = 0;
  161. regions++;
  162. } else {
  163. numblocks++;
  164. }
  165. if (sect_size_old != flash_sector_size(fi, sect))
  166. offset = fi->start[sect];
  167. /*
  168. * Select the largest sector size as erasesize (e.g. for UBI)
  169. */
  170. if (flash_sector_size(fi, sect) > sect_size)
  171. sect_size = flash_sector_size(fi, sect);
  172. sect_size_old = flash_sector_size(fi, sect);
  173. }
  174. /*
  175. * Set the last region
  176. */
  177. mtd->eraseregions[regions].offset = offset - base_addr;
  178. mtd->eraseregions[regions].erasesize = sect_size_old;
  179. mtd->eraseregions[regions].numblocks = numblocks + 1;
  180. if (regions)
  181. mtd->numeraseregions = regions + 1;
  182. else
  183. mtd->numeraseregions = 0;
  184. mtd->erasesize = sect_size;
  185. return 0;
  186. }
  187. int cfi_mtd_init(void)
  188. {
  189. struct mtd_info *mtd;
  190. flash_info_t *fi;
  191. int error, i;
  192. int devices_found = 0;
  193. struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS];
  194. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  195. fi = &flash_info[i];
  196. mtd = &cfi_mtd_info[i];
  197. memset(mtd, 0, sizeof(struct mtd_info));
  198. error = cfi_mtd_set_erasesize(mtd, fi);
  199. if (error)
  200. continue;
  201. sprintf(cfi_mtd_names[i], "nor%d", i);
  202. mtd->name = cfi_mtd_names[i];
  203. mtd->type = MTD_NORFLASH;
  204. mtd->flags = MTD_CAP_NORFLASH;
  205. mtd->size = fi->size;
  206. mtd->writesize = 1;
  207. mtd->erase = cfi_mtd_erase;
  208. mtd->read = cfi_mtd_read;
  209. mtd->write = cfi_mtd_write;
  210. mtd->sync = cfi_mtd_sync;
  211. mtd->lock = cfi_mtd_lock;
  212. mtd->unlock = cfi_mtd_unlock;
  213. mtd->priv = fi;
  214. if (add_mtd_device(mtd))
  215. return -ENOMEM;
  216. mtd_list[devices_found++] = mtd;
  217. }
  218. #ifdef CONFIG_MTD_CONCAT
  219. if (devices_found > 1) {
  220. /*
  221. * We detected multiple devices. Concatenate them together.
  222. */
  223. sprintf(c_mtd_name, "nor%d", devices_found);
  224. mtd = mtd_concat_create(mtd_list, devices_found, c_mtd_name);
  225. if (mtd == NULL)
  226. return -ENXIO;
  227. if (add_mtd_device(mtd))
  228. return -ENOMEM;
  229. }
  230. #endif /* CONFIG_MTD_CONCAT */
  231. return 0;
  232. }