dataflash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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_DataflashSetEnv (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. setenv((char*) area_list[part].label,(char*) s);
  159. }
  160. part++;
  161. }
  162. }
  163. }
  164. void dataflash_print_info (void)
  165. {
  166. int i, j;
  167. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  168. if (dataflash_info[i].id != 0) {
  169. printf("DataFlash:");
  170. switch (dataflash_info[i].id) {
  171. case AT45DB021:
  172. printf("AT45DB021\n");
  173. break;
  174. case AT45DB161:
  175. printf("AT45DB161\n");
  176. break;
  177. case AT45DB321:
  178. printf("AT45DB321\n");
  179. break;
  180. case AT45DB642:
  181. printf("AT45DB642\n");
  182. break;
  183. case AT45DB128:
  184. printf("AT45DB128\n");
  185. break;
  186. }
  187. printf("Nb pages: %6d\n"
  188. "Page Size: %6d\n"
  189. "Size=%8d bytes\n"
  190. "Logical address: 0x%08X\n",
  191. (unsigned int) dataflash_info[i].Device.pages_number,
  192. (unsigned int) dataflash_info[i].Device.pages_size,
  193. (unsigned int) dataflash_info[i].Device.pages_number *
  194. dataflash_info[i].Device.pages_size,
  195. (unsigned int) dataflash_info[i].logical_address);
  196. for (j = 0; j < NB_DATAFLASH_AREA; j++) {
  197. switch(dataflash_info[i].Device.area_list[j].protected) {
  198. case FLAG_PROTECT_SET:
  199. case FLAG_PROTECT_CLEAR:
  200. printf("Area %i:\t%08lX to %08lX %s", j,
  201. dataflash_info[i].Device.area_list[j].start,
  202. dataflash_info[i].Device.area_list[j].end,
  203. (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
  204. printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
  205. break;
  206. case FLAG_PROTECT_INVALID:
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. /*---------------------------------------------------------------------------*/
  214. /* Function Name : AT91F_DataflashSelect */
  215. /* Object : Select the correct device */
  216. /*---------------------------------------------------------------------------*/
  217. AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
  218. unsigned long *addr)
  219. {
  220. char addr_valid = 0;
  221. int i;
  222. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
  223. if ( dataflash_info[i].id
  224. && ((((int) *addr) & 0xFF000000) ==
  225. dataflash_info[i].logical_address)) {
  226. addr_valid = 1;
  227. break;
  228. }
  229. if (!addr_valid) {
  230. pFlash = (AT91PS_DataFlash) 0;
  231. return pFlash;
  232. }
  233. pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
  234. pFlash->pDevice = &(dataflash_info[i].Device);
  235. *addr -= dataflash_info[i].logical_address;
  236. return (pFlash);
  237. }
  238. /*---------------------------------------------------------------------------*/
  239. /* Function Name : addr_dataflash */
  240. /* Object : Test if address is valid */
  241. /*---------------------------------------------------------------------------*/
  242. int addr_dataflash (unsigned long addr)
  243. {
  244. int addr_valid = 0;
  245. int i;
  246. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  247. if ((((int) addr) & 0xFF000000) ==
  248. dataflash_info[i].logical_address) {
  249. addr_valid = 1;
  250. break;
  251. }
  252. }
  253. return addr_valid;
  254. }
  255. /*---------------------------------------------------------------------------*/
  256. /* Function Name : size_dataflash */
  257. /* Object : Test if address is valid regarding the size */
  258. /*---------------------------------------------------------------------------*/
  259. int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
  260. unsigned long size)
  261. {
  262. /* is outside the dataflash */
  263. if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
  264. pdataFlash->pDevice->pages_number)) return 0;
  265. /* is too large for the dataflash */
  266. if (size > ((pdataFlash->pDevice->pages_size *
  267. pdataFlash->pDevice->pages_number) -
  268. ((int)addr & 0x0FFFFFFF))) return 0;
  269. return 1;
  270. }
  271. /*---------------------------------------------------------------------------*/
  272. /* Function Name : prot_dataflash */
  273. /* Object : Test if destination area is protected */
  274. /*---------------------------------------------------------------------------*/
  275. int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
  276. {
  277. int area;
  278. /* find area */
  279. for (area = 0; area < NB_DATAFLASH_AREA; area++) {
  280. if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
  281. (addr < pdataFlash->pDevice->area_list[area].end))
  282. break;
  283. }
  284. if (area == NB_DATAFLASH_AREA)
  285. return -1;
  286. /*test protection value*/
  287. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
  288. return 0;
  289. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
  290. return 0;
  291. return 1;
  292. }
  293. /*--------------------------------------------------------------------------*/
  294. /* Function Name : dataflash_real_protect */
  295. /* Object : protect/unprotect area */
  296. /*--------------------------------------------------------------------------*/
  297. int dataflash_real_protect (int flag, unsigned long start_addr,
  298. unsigned long end_addr)
  299. {
  300. int i,j, area1, area2, addr_valid = 0;
  301. /* find dataflash */
  302. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  303. if ((((int) start_addr) & 0xF0000000) ==
  304. dataflash_info[i].logical_address) {
  305. addr_valid = 1;
  306. break;
  307. }
  308. }
  309. if (!addr_valid) {
  310. return -1;
  311. }
  312. /* find start area */
  313. for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
  314. if (start_addr == dataflash_info[i].Device.area_list[area1].start)
  315. break;
  316. }
  317. if (area1 == NB_DATAFLASH_AREA) return -1;
  318. /* find end area */
  319. for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
  320. if (end_addr == dataflash_info[i].Device.area_list[area2].end)
  321. break;
  322. }
  323. if (area2 == NB_DATAFLASH_AREA)
  324. return -1;
  325. /*set protection value*/
  326. for(j = area1; j < area2 + 1 ; j++)
  327. if(dataflash_info[i].Device.area_list[j].protected
  328. != FLAG_PROTECT_INVALID) {
  329. if (flag == 0) {
  330. dataflash_info[i].Device.area_list[j].protected
  331. = FLAG_PROTECT_CLEAR;
  332. } else {
  333. dataflash_info[i].Device.area_list[j].protected
  334. = FLAG_PROTECT_SET;
  335. }
  336. }
  337. return (area2 - area1 + 1);
  338. }
  339. /*---------------------------------------------------------------------------*/
  340. /* Function Name : read_dataflash */
  341. /* Object : dataflash memory read */
  342. /*---------------------------------------------------------------------------*/
  343. int read_dataflash (unsigned long addr, unsigned long size, char *result)
  344. {
  345. unsigned long AddrToRead = addr;
  346. AT91PS_DataFlash pFlash = &DataFlashInst;
  347. pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
  348. if (pFlash == 0)
  349. return ERR_UNKNOWN_FLASH_TYPE;
  350. if (size_dataflash(pFlash,addr,size) == 0)
  351. return ERR_INVAL;
  352. return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
  353. }
  354. /*---------------------------------------------------------------------------*/
  355. /* Function Name : write_dataflash */
  356. /* Object : write a block in dataflash */
  357. /*---------------------------------------------------------------------------*/
  358. int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
  359. unsigned long size)
  360. {
  361. unsigned long AddrToWrite = addr_dest;
  362. AT91PS_DataFlash pFlash = &DataFlashInst;
  363. pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
  364. if (pFlash == 0)
  365. return ERR_UNKNOWN_FLASH_TYPE;
  366. if (size_dataflash(pFlash,addr_dest,size) == 0)
  367. return ERR_INVAL;
  368. if (prot_dataflash(pFlash,addr_dest) == 0)
  369. return ERR_PROTECTED;
  370. if (AddrToWrite == -1)
  371. return -1;
  372. return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
  373. AddrToWrite, size);
  374. }
  375. void dataflash_perror (int err)
  376. {
  377. switch (err) {
  378. case ERR_OK:
  379. break;
  380. case ERR_TIMOUT:
  381. printf("Timeout writing to DataFlash\n");
  382. break;
  383. case ERR_PROTECTED:
  384. printf("Can't write to protected/invalid DataFlash sectors\n");
  385. break;
  386. case ERR_INVAL:
  387. printf("Outside available DataFlash\n");
  388. break;
  389. case ERR_UNKNOWN_FLASH_TYPE:
  390. printf("Unknown Type of DataFlash\n");
  391. break;
  392. case ERR_PROG_ERROR:
  393. printf("General DataFlash Programming Error\n");
  394. break;
  395. default:
  396. printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
  397. break;
  398. }
  399. }