dataflash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * LowLevel function for ATMEL DataFlash support
  3. * Author : Hamid Ikdoumi (Atmel)
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <config.h>
  9. #include <asm/hardware.h>
  10. #include <dataflash.h>
  11. static AT91S_DataFlash DataFlashInst;
  12. extern void AT91F_SpiInit (void);
  13. extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
  14. extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
  15. unsigned long addr,
  16. unsigned long size, char *buffer);
  17. extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
  18. unsigned char *src,
  19. int dest,
  20. int size );
  21. int AT91F_DataflashInit (void)
  22. {
  23. int i, j;
  24. int dfcode;
  25. int part;
  26. int found[CONFIG_SYS_MAX_DATAFLASH_BANKS];
  27. unsigned char protected;
  28. AT91F_SpiInit ();
  29. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  30. found[i] = 0;
  31. dataflash_info[i].Desc.state = IDLE;
  32. dataflash_info[i].id = 0;
  33. dataflash_info[i].Device.pages_number = 0;
  34. dfcode = AT91F_DataflashProbe (cs[i].cs,
  35. &dataflash_info[i].Desc);
  36. switch (dfcode) {
  37. case AT45DB021:
  38. dataflash_info[i].Device.pages_number = 1024;
  39. dataflash_info[i].Device.pages_size = 264;
  40. dataflash_info[i].Device.page_offset = 9;
  41. dataflash_info[i].Device.byte_mask = 0x300;
  42. dataflash_info[i].Device.cs = cs[i].cs;
  43. dataflash_info[i].Desc.DataFlash_state = IDLE;
  44. dataflash_info[i].logical_address = cs[i].addr;
  45. dataflash_info[i].id = dfcode;
  46. found[i] += dfcode;
  47. break;
  48. case AT45DB081:
  49. dataflash_info[i].Device.pages_number = 4096;
  50. dataflash_info[i].Device.pages_size = 264;
  51. dataflash_info[i].Device.page_offset = 9;
  52. dataflash_info[i].Device.byte_mask = 0x300;
  53. dataflash_info[i].Device.cs = cs[i].cs;
  54. dataflash_info[i].Desc.DataFlash_state = IDLE;
  55. dataflash_info[i].logical_address = cs[i].addr;
  56. dataflash_info[i].id = dfcode;
  57. found[i] += dfcode;
  58. break;
  59. case AT45DB161:
  60. dataflash_info[i].Device.pages_number = 4096;
  61. dataflash_info[i].Device.pages_size = 528;
  62. dataflash_info[i].Device.page_offset = 10;
  63. dataflash_info[i].Device.byte_mask = 0x300;
  64. dataflash_info[i].Device.cs = cs[i].cs;
  65. dataflash_info[i].Desc.DataFlash_state = IDLE;
  66. dataflash_info[i].logical_address = cs[i].addr;
  67. dataflash_info[i].id = dfcode;
  68. found[i] += dfcode;
  69. break;
  70. case AT45DB321:
  71. dataflash_info[i].Device.pages_number = 8192;
  72. dataflash_info[i].Device.pages_size = 528;
  73. dataflash_info[i].Device.page_offset = 10;
  74. dataflash_info[i].Device.byte_mask = 0x300;
  75. dataflash_info[i].Device.cs = cs[i].cs;
  76. dataflash_info[i].Desc.DataFlash_state = IDLE;
  77. dataflash_info[i].logical_address = cs[i].addr;
  78. dataflash_info[i].id = dfcode;
  79. found[i] += dfcode;
  80. break;
  81. case AT45DB642:
  82. dataflash_info[i].Device.pages_number = 8192;
  83. dataflash_info[i].Device.pages_size = 1056;
  84. dataflash_info[i].Device.page_offset = 11;
  85. dataflash_info[i].Device.byte_mask = 0x700;
  86. dataflash_info[i].Device.cs = cs[i].cs;
  87. dataflash_info[i].Desc.DataFlash_state = IDLE;
  88. dataflash_info[i].logical_address = cs[i].addr;
  89. dataflash_info[i].id = dfcode;
  90. found[i] += dfcode;
  91. break;
  92. case AT45DB128:
  93. dataflash_info[i].Device.pages_number = 16384;
  94. dataflash_info[i].Device.pages_size = 1056;
  95. dataflash_info[i].Device.page_offset = 11;
  96. dataflash_info[i].Device.byte_mask = 0x700;
  97. dataflash_info[i].Device.cs = cs[i].cs;
  98. dataflash_info[i].Desc.DataFlash_state = IDLE;
  99. dataflash_info[i].logical_address = cs[i].addr;
  100. dataflash_info[i].id = dfcode;
  101. found[i] += dfcode;
  102. break;
  103. default:
  104. dfcode = 0;
  105. break;
  106. }
  107. /* set the last area end to the dataflash size*/
  108. dataflash_info[i].end_address =
  109. (dataflash_info[i].Device.pages_number *
  110. dataflash_info[i].Device.pages_size) - 1;
  111. part = 0;
  112. /* set the area addresses */
  113. for(j = 0; j < NB_DATAFLASH_AREA; j++) {
  114. if(found[i]!=0) {
  115. dataflash_info[i].Device.area_list[j].start =
  116. area_list[part].start +
  117. dataflash_info[i].logical_address;
  118. if(area_list[part].end == 0xffffffff) {
  119. dataflash_info[i].Device.area_list[j].end =
  120. dataflash_info[i].end_address +
  121. dataflash_info[i].logical_address;
  122. } else {
  123. dataflash_info[i].Device.area_list[j].end =
  124. area_list[part].end +
  125. dataflash_info[i].logical_address;
  126. }
  127. protected = area_list[part].protected;
  128. /* Set the environment according to the label...*/
  129. if(protected == FLAG_PROTECT_INVALID) {
  130. dataflash_info[i].Device.area_list[j].protected =
  131. FLAG_PROTECT_INVALID;
  132. } else {
  133. dataflash_info[i].Device.area_list[j].protected =
  134. protected;
  135. }
  136. strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
  137. (const char *)area_list[part].label);
  138. }
  139. part++;
  140. }
  141. }
  142. return found[0];
  143. }
  144. void AT91F_Dataflashenv_set(void)
  145. {
  146. int i, j;
  147. int part;
  148. unsigned char env;
  149. unsigned char s[32]; /* Will fit a long int in hex */
  150. unsigned long start;
  151. for (i = 0, part= 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  152. for(j = 0; j < NB_DATAFLASH_AREA; j++) {
  153. env = area_list[part].setenv;
  154. /* Set the environment according to the label...*/
  155. if((env & FLAG_SETENV) == FLAG_SETENV) {
  156. start = dataflash_info[i].Device.area_list[j].start;
  157. sprintf((char *)s, "%lX", start);
  158. env_set((char *)area_list[part].label,
  159. (char *)s);
  160. }
  161. part++;
  162. }
  163. }
  164. }
  165. void dataflash_print_info (void)
  166. {
  167. int i, j;
  168. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  169. if (dataflash_info[i].id != 0) {
  170. printf("DataFlash:");
  171. switch (dataflash_info[i].id) {
  172. case AT45DB021:
  173. printf("AT45DB021\n");
  174. break;
  175. case AT45DB161:
  176. printf("AT45DB161\n");
  177. break;
  178. case AT45DB321:
  179. printf("AT45DB321\n");
  180. break;
  181. case AT45DB642:
  182. printf("AT45DB642\n");
  183. break;
  184. case AT45DB128:
  185. printf("AT45DB128\n");
  186. break;
  187. }
  188. printf("Nb pages: %6d\n"
  189. "Page Size: %6d\n"
  190. "Size=%8d bytes\n"
  191. "Logical address: 0x%08X\n",
  192. (unsigned int) dataflash_info[i].Device.pages_number,
  193. (unsigned int) dataflash_info[i].Device.pages_size,
  194. (unsigned int) dataflash_info[i].Device.pages_number *
  195. dataflash_info[i].Device.pages_size,
  196. (unsigned int) dataflash_info[i].logical_address);
  197. for (j = 0; j < NB_DATAFLASH_AREA; j++) {
  198. switch(dataflash_info[i].Device.area_list[j].protected) {
  199. case FLAG_PROTECT_SET:
  200. case FLAG_PROTECT_CLEAR:
  201. printf("Area %i:\t%08lX to %08lX %s", j,
  202. dataflash_info[i].Device.area_list[j].start,
  203. dataflash_info[i].Device.area_list[j].end,
  204. (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
  205. printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
  206. break;
  207. case FLAG_PROTECT_INVALID:
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. /*---------------------------------------------------------------------------*/
  215. /* Function Name : AT91F_DataflashSelect */
  216. /* Object : Select the correct device */
  217. /*---------------------------------------------------------------------------*/
  218. AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
  219. unsigned long *addr)
  220. {
  221. char addr_valid = 0;
  222. int i;
  223. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
  224. if ( dataflash_info[i].id
  225. && ((((int) *addr) & 0xFF000000) ==
  226. dataflash_info[i].logical_address)) {
  227. addr_valid = 1;
  228. break;
  229. }
  230. if (!addr_valid) {
  231. pFlash = (AT91PS_DataFlash) 0;
  232. return pFlash;
  233. }
  234. pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
  235. pFlash->pDevice = &(dataflash_info[i].Device);
  236. *addr -= dataflash_info[i].logical_address;
  237. return (pFlash);
  238. }
  239. /*---------------------------------------------------------------------------*/
  240. /* Function Name : addr_dataflash */
  241. /* Object : Test if address is valid */
  242. /*---------------------------------------------------------------------------*/
  243. int addr_dataflash (unsigned long addr)
  244. {
  245. int addr_valid = 0;
  246. int i;
  247. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  248. if ((((int) addr) & 0xFF000000) ==
  249. dataflash_info[i].logical_address) {
  250. addr_valid = 1;
  251. break;
  252. }
  253. }
  254. return addr_valid;
  255. }
  256. /*---------------------------------------------------------------------------*/
  257. /* Function Name : size_dataflash */
  258. /* Object : Test if address is valid regarding the size */
  259. /*---------------------------------------------------------------------------*/
  260. int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
  261. unsigned long size)
  262. {
  263. /* is outside the dataflash */
  264. if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
  265. pdataFlash->pDevice->pages_number)) return 0;
  266. /* is too large for the dataflash */
  267. if (size > ((pdataFlash->pDevice->pages_size *
  268. pdataFlash->pDevice->pages_number) -
  269. ((int)addr & 0x0FFFFFFF))) return 0;
  270. return 1;
  271. }
  272. /*---------------------------------------------------------------------------*/
  273. /* Function Name : prot_dataflash */
  274. /* Object : Test if destination area is protected */
  275. /*---------------------------------------------------------------------------*/
  276. int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
  277. {
  278. int area;
  279. /* find area */
  280. for (area = 0; area < NB_DATAFLASH_AREA; area++) {
  281. if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
  282. (addr < pdataFlash->pDevice->area_list[area].end))
  283. break;
  284. }
  285. if (area == NB_DATAFLASH_AREA)
  286. return -1;
  287. /*test protection value*/
  288. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
  289. return 0;
  290. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
  291. return 0;
  292. return 1;
  293. }
  294. /*--------------------------------------------------------------------------*/
  295. /* Function Name : dataflash_real_protect */
  296. /* Object : protect/unprotect area */
  297. /*--------------------------------------------------------------------------*/
  298. int dataflash_real_protect (int flag, unsigned long start_addr,
  299. unsigned long end_addr)
  300. {
  301. int i,j, area1, area2, addr_valid = 0;
  302. /* find dataflash */
  303. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  304. if ((((int) start_addr) & 0xF0000000) ==
  305. dataflash_info[i].logical_address) {
  306. addr_valid = 1;
  307. break;
  308. }
  309. }
  310. if (!addr_valid) {
  311. return -1;
  312. }
  313. /* find start area */
  314. for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
  315. if (start_addr == dataflash_info[i].Device.area_list[area1].start)
  316. break;
  317. }
  318. if (area1 == NB_DATAFLASH_AREA) return -1;
  319. /* find end area */
  320. for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
  321. if (end_addr == dataflash_info[i].Device.area_list[area2].end)
  322. break;
  323. }
  324. if (area2 == NB_DATAFLASH_AREA)
  325. return -1;
  326. /*set protection value*/
  327. for(j = area1; j < area2 + 1 ; j++)
  328. if(dataflash_info[i].Device.area_list[j].protected
  329. != FLAG_PROTECT_INVALID) {
  330. if (flag == 0) {
  331. dataflash_info[i].Device.area_list[j].protected
  332. = FLAG_PROTECT_CLEAR;
  333. } else {
  334. dataflash_info[i].Device.area_list[j].protected
  335. = FLAG_PROTECT_SET;
  336. }
  337. }
  338. return (area2 - area1 + 1);
  339. }
  340. /*---------------------------------------------------------------------------*/
  341. /* Function Name : read_dataflash */
  342. /* Object : dataflash memory read */
  343. /*---------------------------------------------------------------------------*/
  344. int read_dataflash (unsigned long addr, unsigned long size, char *result)
  345. {
  346. unsigned long AddrToRead = addr;
  347. AT91PS_DataFlash pFlash = &DataFlashInst;
  348. pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
  349. if (pFlash == 0)
  350. return ERR_UNKNOWN_FLASH_TYPE;
  351. if (size_dataflash(pFlash,addr,size) == 0)
  352. return ERR_INVAL;
  353. return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
  354. }
  355. /*---------------------------------------------------------------------------*/
  356. /* Function Name : write_dataflash */
  357. /* Object : write a block in dataflash */
  358. /*---------------------------------------------------------------------------*/
  359. int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
  360. unsigned long size)
  361. {
  362. unsigned long AddrToWrite = addr_dest;
  363. AT91PS_DataFlash pFlash = &DataFlashInst;
  364. pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
  365. if (pFlash == 0)
  366. return ERR_UNKNOWN_FLASH_TYPE;
  367. if (size_dataflash(pFlash,addr_dest,size) == 0)
  368. return ERR_INVAL;
  369. if (prot_dataflash(pFlash,addr_dest) == 0)
  370. return ERR_PROTECTED;
  371. if (AddrToWrite == -1)
  372. return -1;
  373. return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
  374. AddrToWrite, size);
  375. }
  376. void dataflash_perror (int err)
  377. {
  378. switch (err) {
  379. case ERR_OK:
  380. break;
  381. case ERR_TIMOUT:
  382. printf("Timeout writing to DataFlash\n");
  383. break;
  384. case ERR_PROTECTED:
  385. printf("Can't write to protected/invalid DataFlash sectors\n");
  386. break;
  387. case ERR_INVAL:
  388. printf("Outside available DataFlash\n");
  389. break;
  390. case ERR_UNKNOWN_FLASH_TYPE:
  391. printf("Unknown Type of DataFlash\n");
  392. break;
  393. case ERR_PROG_ERROR:
  394. printf("General DataFlash Programming Error\n");
  395. break;
  396. default:
  397. printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
  398. break;
  399. }
  400. }