dataflash.c 13 KB

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