PageRenderTime 1712ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/board/scb9328/flash.c

https://github.com/mike0/uboot-imx
C | 325 lines | 217 code | 52 blank | 56 comment | 53 complexity | 6d1cb41ca26f37cdf082af54927a60e1 MD5 | raw file
  1. /*
  2. * Copyright (C) 2003 ETC s.r.o.
  3. *
  4. * This code was inspired by Marius Groeger and Kyle Harris code
  5. * available in other board ports for U-Boot
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. *
  25. * Written by Peter Figuli <peposh@etc.sk>, 2003.
  26. *
  27. */
  28. #include <common.h>
  29. #include "intel.h"
  30. /*
  31. * This code should handle CFI FLASH memory device. This code is very
  32. * minimalistic approach without many essential error handling code as well.
  33. * Because U-Boot actually is missing smart handling of FLASH device,
  34. * we just set flash_id to anything else to FLASH_UNKNOW, so common code
  35. * can call us without any restrictions.
  36. * TODO: Add CFI Query, to be able to determine FLASH device.
  37. * TODO: Add error handling code
  38. * NOTE: This code was tested with BUS_WIDTH 4 and ITERLEAVE 2 only, but
  39. * hopefully may work with other configurations.
  40. */
  41. #if ( SCB9328_FLASH_BUS_WIDTH == 1 )
  42. # define FLASH_BUS vu_char
  43. # define FLASH_BUS_RET u_char
  44. # if ( SCB9328_FLASH_INTERLEAVE == 1 )
  45. # define FLASH_CMD( x ) x
  46. # else
  47. # error "With 8bit bus only one chip is allowed"
  48. # endif
  49. #elif ( SCB9328_FLASH_BUS_WIDTH == 2 )
  50. # define FLASH_BUS vu_short
  51. # define FLASH_BUS_RET u_short
  52. # if ( SCB9328_FLASH_INTERLEAVE == 1 )
  53. # define FLASH_CMD( x ) x
  54. # elif ( SCB9328_FLASH_INTERLEAVE == 2 )
  55. # define FLASH_CMD( x ) (( x << 8 )| x )
  56. # else
  57. # error "With 16bit bus only 1 or 2 chip(s) are allowed"
  58. # endif
  59. #elif ( SCB9328_FLASH_BUS_WIDTH == 4 )
  60. # define FLASH_BUS vu_long
  61. # define FLASH_BUS_RET u_long
  62. # if ( SCB9328_FLASH_INTERLEAVE == 1 )
  63. # define FLASH_CMD( x ) x
  64. # elif ( SCB9328_FLASH_INTERLEAVE == 2 )
  65. # define FLASH_CMD( x ) (( x << 16 )| x )
  66. # elif ( SCB9328_FLASH_INTERLEAVE == 4 )
  67. # define FLASH_CMD( x ) (( x << 24 )|( x << 16 ) ( x << 8 )| x )
  68. # else
  69. # error "With 32bit bus only 1,2 or 4 chip(s) are allowed"
  70. # endif
  71. #else
  72. # error "Flash bus width might be 1,2,4 for 8,16,32 bit configuration"
  73. #endif
  74. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  75. static FLASH_BUS_RET flash_status_reg (void)
  76. {
  77. FLASH_BUS *addr = (FLASH_BUS *) 0;
  78. *addr = FLASH_CMD (CFI_INTEL_CMD_READ_STATUS_REGISTER);
  79. return *addr;
  80. }
  81. static int flash_ready (ulong timeout)
  82. {
  83. int ok = 1;
  84. ulong start;
  85. start = get_timer(0);
  86. while ((flash_status_reg () & FLASH_CMD (CFI_INTEL_SR_READY)) !=
  87. FLASH_CMD (CFI_INTEL_SR_READY)) {
  88. if (get_timer(start) > timeout && timeout != 0) {
  89. ok = 0;
  90. break;
  91. }
  92. }
  93. return ok;
  94. }
  95. #if ( CONFIG_SYS_MAX_FLASH_BANKS != 1 )
  96. # error "SCB9328 platform has only one flash bank!"
  97. #endif
  98. ulong flash_init (void)
  99. {
  100. int i;
  101. unsigned long address = SCB9328_FLASH_BASE;
  102. flash_info[0].size = SCB9328_FLASH_BANK_SIZE;
  103. flash_info[0].sector_count = CONFIG_SYS_MAX_FLASH_SECT;
  104. flash_info[0].flash_id = INTEL_MANUFACT;
  105. memset (flash_info[0].protect, 0, CONFIG_SYS_MAX_FLASH_SECT);
  106. for (i = 0; i < CONFIG_SYS_MAX_FLASH_SECT; i++) {
  107. flash_info[0].start[i] = address;
  108. #ifdef SCB9328_FLASH_UNLOCK
  109. /* Some devices are hw locked after start. */
  110. *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_LOCK_SETUP);
  111. *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_UNLOCK_BLOCK);
  112. flash_ready (0);
  113. *((FLASH_BUS *) address) = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
  114. #endif
  115. address += SCB9328_FLASH_SECT_SIZE;
  116. }
  117. flash_protect (FLAG_PROTECT_SET,
  118. CONFIG_SYS_FLASH_BASE,
  119. CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
  120. &flash_info[0]);
  121. flash_protect (FLAG_PROTECT_SET,
  122. CONFIG_ENV_ADDR,
  123. CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
  124. return SCB9328_FLASH_BANK_SIZE;
  125. }
  126. void flash_print_info (flash_info_t * info)
  127. {
  128. int i;
  129. printf (" Intel vendor\n");
  130. printf (" Size: %ld MB in %d Sectors\n",
  131. info->size >> 20, info->sector_count);
  132. printf (" Sector Start Addresses:");
  133. for (i = 0; i < info->sector_count; i++) {
  134. if (!(i % 5)) {
  135. printf ("\n");
  136. }
  137. printf (" %08lX%s", info->start[i],
  138. info->protect[i] ? " (RO)" : " ");
  139. }
  140. printf ("\n");
  141. }
  142. int flash_erase (flash_info_t * info, int s_first, int s_last)
  143. {
  144. int flag, non_protected = 0, sector;
  145. int rc = ERR_OK;
  146. FLASH_BUS *address;
  147. for (sector = s_first; sector <= s_last; sector++) {
  148. if (!info->protect[sector]) {
  149. non_protected++;
  150. }
  151. }
  152. if (!non_protected) {
  153. return ERR_PROTECTED;
  154. }
  155. /*
  156. * Disable interrupts which might cause a timeout
  157. * here. Remember that our exception vectors are
  158. * at address 0 in the flash, and we don't want a
  159. * (ticker) exception to happen while the flash
  160. * chip is in programming mode.
  161. */
  162. flag = disable_interrupts ();
  163. /* Start erase on unprotected sectors */
  164. for (sector = s_first; sector <= s_last && !ctrlc (); sector++) {
  165. if (info->protect[sector]) {
  166. printf ("Protected sector %2d skipping...\n", sector);
  167. continue;
  168. } else {
  169. printf ("Erasing sector %2d ... ", sector);
  170. }
  171. address = (FLASH_BUS *) (info->start[sector]);
  172. *address = FLASH_CMD (CFI_INTEL_CMD_BLOCK_ERASE);
  173. *address = FLASH_CMD (CFI_INTEL_CMD_CONFIRM);
  174. if (flash_ready (CONFIG_SYS_FLASH_ERASE_TOUT)) {
  175. *address = FLASH_CMD (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER);
  176. printf ("ok.\n");
  177. } else {
  178. *address = FLASH_CMD (CFI_INTEL_CMD_SUSPEND);
  179. rc = ERR_TIMOUT;
  180. printf ("timeout! Aborting...\n");
  181. break;
  182. }
  183. *address = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
  184. }
  185. if (ctrlc ())
  186. printf ("User Interrupt!\n");
  187. /* allow flash to settle - wait 10 ms */
  188. udelay_masked (10000);
  189. if (flag) {
  190. enable_interrupts ();
  191. }
  192. return rc;
  193. }
  194. static int write_data (flash_info_t * info, ulong dest, FLASH_BUS data)
  195. {
  196. FLASH_BUS *address = (FLASH_BUS *) dest;
  197. int rc = ERR_OK;
  198. int flag;
  199. /* Check if Flash is (sufficiently) erased */
  200. if ((*address & data) != data) {
  201. return ERR_NOT_ERASED;
  202. }
  203. /*
  204. * Disable interrupts which might cause a timeout
  205. * here. Remember that our exception vectors are
  206. * at address 0 in the flash, and we don't want a
  207. * (ticker) exception to happen while the flash
  208. * chip is in programming mode.
  209. */
  210. flag = disable_interrupts ();
  211. *address = FLASH_CMD (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER);
  212. *address = FLASH_CMD (CFI_INTEL_CMD_PROGRAM1);
  213. *address = data;
  214. if (!flash_ready (CONFIG_SYS_FLASH_WRITE_TOUT)) {
  215. *address = FLASH_CMD (CFI_INTEL_CMD_SUSPEND);
  216. rc = ERR_TIMOUT;
  217. printf ("timeout! Aborting...\n");
  218. }
  219. *address = FLASH_CMD (CFI_INTEL_CMD_READ_ARRAY);
  220. if (flag) {
  221. enable_interrupts ();
  222. }
  223. return rc;
  224. }
  225. int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
  226. {
  227. ulong read_addr, write_addr;
  228. FLASH_BUS data;
  229. int i, result = ERR_OK;
  230. read_addr = addr & ~(sizeof (FLASH_BUS) - 1);
  231. write_addr = read_addr;
  232. if (read_addr != addr) {
  233. data = 0;
  234. for (i = 0; i < sizeof (FLASH_BUS); i++) {
  235. if (read_addr < addr || cnt == 0) {
  236. data |= *((uchar *) read_addr) << i * 8;
  237. } else {
  238. data |= (*src++) << i * 8;
  239. cnt--;
  240. }
  241. read_addr++;
  242. }
  243. if ((result = write_data (info, write_addr, data)) != ERR_OK) {
  244. return result;
  245. }
  246. write_addr += sizeof (FLASH_BUS);
  247. }
  248. for (; cnt >= sizeof (FLASH_BUS); cnt -= sizeof (FLASH_BUS)) {
  249. if ((result = write_data (info, write_addr,
  250. *((FLASH_BUS *) src))) != ERR_OK) {
  251. return result;
  252. }
  253. write_addr += sizeof (FLASH_BUS);
  254. src += sizeof (FLASH_BUS);
  255. }
  256. if (cnt > 0) {
  257. read_addr = write_addr;
  258. data = 0;
  259. for (i = 0; i < sizeof (FLASH_BUS); i++) {
  260. if (cnt > 0) {
  261. data |= (*src++) << i * 8;
  262. cnt--;
  263. } else {
  264. data |= *((uchar *) read_addr) << i * 8;
  265. }
  266. read_addr++;
  267. }
  268. if ((result = write_data (info, write_addr, data)) != 0) {
  269. return result;
  270. }
  271. }
  272. return ERR_OK;
  273. }