dataflash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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[CONFIG_SYS_MAX_DATAFLASH_BANKS];
  41. unsigned char protected;
  42. AT91F_SpiInit ();
  43. for (i = 0; i < CONFIG_SYS_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 = 264;
  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 AT45DB081:
  63. dataflash_info[i].Device.pages_number = 4096;
  64. dataflash_info[i].Device.pages_size = 264;
  65. dataflash_info[i].Device.page_offset = 9;
  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 AT45DB161:
  74. dataflash_info[i].Device.pages_number = 4096;
  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 AT45DB321:
  85. dataflash_info[i].Device.pages_number = 8192;
  86. dataflash_info[i].Device.pages_size = 528;
  87. dataflash_info[i].Device.page_offset = 10;
  88. dataflash_info[i].Device.byte_mask = 0x300;
  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 AT45DB642:
  96. dataflash_info[i].Device.pages_number = 8192;
  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. case AT45DB128:
  107. dataflash_info[i].Device.pages_number = 16384;
  108. dataflash_info[i].Device.pages_size = 1056;
  109. dataflash_info[i].Device.page_offset = 11;
  110. dataflash_info[i].Device.byte_mask = 0x700;
  111. dataflash_info[i].Device.cs = cs[i].cs;
  112. dataflash_info[i].Desc.DataFlash_state = IDLE;
  113. dataflash_info[i].logical_address = cs[i].addr;
  114. dataflash_info[i].id = dfcode;
  115. found[i] += dfcode;;
  116. break;
  117. default:
  118. dfcode = 0;
  119. break;
  120. }
  121. /* set the last area end to the dataflash size*/
  122. area_list[NB_DATAFLASH_AREA -1].end =
  123. (dataflash_info[i].Device.pages_number *
  124. dataflash_info[i].Device.pages_size)-1;
  125. part = 0;
  126. last_part = 0;
  127. /* set the area addresses */
  128. for(j = 0; j<NB_DATAFLASH_AREA; j++) {
  129. if(found[i]!=0) {
  130. dataflash_info[i].Device.area_list[j].start =
  131. area_list[part].start +
  132. dataflash_info[i].logical_address;
  133. if(area_list[part].end == 0xffffffff) {
  134. dataflash_info[i].Device.area_list[j].end =
  135. dataflash_info[i].end_address +
  136. dataflash_info [i].logical_address;
  137. last_part = 1;
  138. } else {
  139. dataflash_info[i].Device.area_list[j].end =
  140. area_list[part].end +
  141. dataflash_info[i].logical_address;
  142. }
  143. protected = area_list[part].protected;
  144. /* Set the environment according to the label...*/
  145. if(protected == FLAG_PROTECT_INVALID) {
  146. dataflash_info[i].Device.area_list[j].protected =
  147. FLAG_PROTECT_INVALID;
  148. } else {
  149. dataflash_info[i].Device.area_list[j].protected =
  150. protected;
  151. }
  152. strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
  153. (const char *)area_list[part].label);
  154. }
  155. part++;
  156. }
  157. }
  158. return found[0];
  159. }
  160. void AT91F_DataflashSetEnv (void)
  161. {
  162. int i, j;
  163. int part;
  164. unsigned char env;
  165. unsigned char s[32]; /* Will fit a long int in hex */
  166. unsigned long start;
  167. for (i = 0, part= 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  168. for(j = 0; j<NB_DATAFLASH_AREA; j++) {
  169. env = area_list[part].setenv;
  170. /* Set the environment according to the label...*/
  171. if((env & FLAG_SETENV) == FLAG_SETENV) {
  172. start =
  173. dataflash_info[i].Device.area_list[j].start;
  174. sprintf((char*) s,"%lX",start);
  175. setenv((char*) area_list[part].label,(char*) s);
  176. }
  177. part++;
  178. }
  179. }
  180. }
  181. void dataflash_print_info (void)
  182. {
  183. int i, j;
  184. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  185. if (dataflash_info[i].id != 0) {
  186. printf("DataFlash:");
  187. switch (dataflash_info[i].id) {
  188. case AT45DB021:
  189. printf("AT45DB021\n");
  190. break;
  191. case AT45DB161:
  192. printf("AT45DB161\n");
  193. break;
  194. case AT45DB321:
  195. printf("AT45DB321\n");
  196. break;
  197. case AT45DB642:
  198. printf("AT45DB642\n");
  199. break;
  200. case AT45DB128:
  201. printf("AT45DB128\n");
  202. break;
  203. }
  204. printf("Nb pages: %6d\n"
  205. "Page Size: %6d\n"
  206. "Size=%8d bytes\n"
  207. "Logical address: 0x%08X\n",
  208. (unsigned int) dataflash_info[i].Device.pages_number,
  209. (unsigned int) dataflash_info[i].Device.pages_size,
  210. (unsigned int) dataflash_info[i].Device.pages_number *
  211. dataflash_info[i].Device.pages_size,
  212. (unsigned int) dataflash_info[i].logical_address);
  213. for (j=0; j< NB_DATAFLASH_AREA; j++) {
  214. switch(dataflash_info[i].Device.area_list[j].protected) {
  215. case FLAG_PROTECT_SET:
  216. case FLAG_PROTECT_CLEAR:
  217. printf("Area %i:\t%08lX to %08lX %s", j,
  218. dataflash_info[i].Device.area_list[j].start,
  219. dataflash_info[i].Device.area_list[j].end,
  220. (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
  221. printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
  222. break;
  223. case FLAG_PROTECT_INVALID:
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. }
  230. /*---------------------------------------------------------------------------*/
  231. /* Function Name : AT91F_DataflashSelect */
  232. /* Object : Select the correct device */
  233. /*---------------------------------------------------------------------------*/
  234. AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
  235. unsigned long *addr)
  236. {
  237. char addr_valid = 0;
  238. int i;
  239. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
  240. if ( dataflash_info[i].id
  241. && ((((int) *addr) & 0xFF000000) ==
  242. dataflash_info[i].logical_address)) {
  243. addr_valid = 1;
  244. break;
  245. }
  246. if (!addr_valid) {
  247. pFlash = (AT91PS_DataFlash) 0;
  248. return pFlash;
  249. }
  250. pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
  251. pFlash->pDevice = &(dataflash_info[i].Device);
  252. *addr -= dataflash_info[i].logical_address;
  253. return (pFlash);
  254. }
  255. /*---------------------------------------------------------------------------*/
  256. /* Function Name : addr_dataflash */
  257. /* Object : Test if address is valid */
  258. /*---------------------------------------------------------------------------*/
  259. int addr_dataflash (unsigned long addr)
  260. {
  261. int addr_valid = 0;
  262. int i;
  263. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  264. if ((((int) addr) & 0xFF000000) ==
  265. dataflash_info[i].logical_address) {
  266. addr_valid = 1;
  267. break;
  268. }
  269. }
  270. return addr_valid;
  271. }
  272. /*---------------------------------------------------------------------------*/
  273. /* Function Name : size_dataflash */
  274. /* Object : Test if address is valid regarding the size */
  275. /*---------------------------------------------------------------------------*/
  276. int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
  277. unsigned long size)
  278. {
  279. /* is outside the dataflash */
  280. if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
  281. pdataFlash->pDevice->pages_number)) return 0;
  282. /* is too large for the dataflash */
  283. if (size > ((pdataFlash->pDevice->pages_size *
  284. pdataFlash->pDevice->pages_number) -
  285. ((int)addr & 0x0FFFFFFF))) return 0;
  286. return 1;
  287. }
  288. /*---------------------------------------------------------------------------*/
  289. /* Function Name : prot_dataflash */
  290. /* Object : Test if destination area is protected */
  291. /*---------------------------------------------------------------------------*/
  292. int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
  293. {
  294. int area;
  295. /* find area */
  296. for (area=0; area < NB_DATAFLASH_AREA; area++) {
  297. if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
  298. (addr < pdataFlash->pDevice->area_list[area].end))
  299. break;
  300. }
  301. if (area == NB_DATAFLASH_AREA)
  302. return -1;
  303. /*test protection value*/
  304. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
  305. return 0;
  306. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
  307. return 0;
  308. return 1;
  309. }
  310. /*--------------------------------------------------------------------------*/
  311. /* Function Name : dataflash_real_protect */
  312. /* Object : protect/unprotect area */
  313. /*--------------------------------------------------------------------------*/
  314. int dataflash_real_protect (int flag, unsigned long start_addr,
  315. unsigned long end_addr)
  316. {
  317. int i,j, area1, area2, addr_valid = 0;
  318. /* find dataflash */
  319. for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
  320. if ((((int) start_addr) & 0xF0000000) ==
  321. dataflash_info[i].logical_address) {
  322. addr_valid = 1;
  323. break;
  324. }
  325. }
  326. if (!addr_valid) {
  327. return -1;
  328. }
  329. /* find start area */
  330. for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
  331. if (start_addr == dataflash_info[i].Device.area_list[area1].start)
  332. break;
  333. }
  334. if (area1 == NB_DATAFLASH_AREA) return -1;
  335. /* find end area */
  336. for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
  337. if (end_addr == dataflash_info[i].Device.area_list[area2].end)
  338. break;
  339. }
  340. if (area2 == NB_DATAFLASH_AREA)
  341. return -1;
  342. /*set protection value*/
  343. for(j = area1; j < area2+1 ; j++)
  344. if(dataflash_info[i].Device.area_list[j].protected
  345. != FLAG_PROTECT_INVALID) {
  346. if (flag == 0) {
  347. dataflash_info[i].Device.area_list[j].protected
  348. = FLAG_PROTECT_CLEAR;
  349. } else {
  350. dataflash_info[i].Device.area_list[j].protected
  351. = FLAG_PROTECT_SET;
  352. }
  353. }
  354. return (area2-area1+1);
  355. }
  356. /*---------------------------------------------------------------------------*/
  357. /* Function Name : read_dataflash */
  358. /* Object : dataflash memory read */
  359. /*---------------------------------------------------------------------------*/
  360. int read_dataflash (unsigned long addr, unsigned long size, char *result)
  361. {
  362. unsigned long AddrToRead = addr;
  363. AT91PS_DataFlash pFlash = &DataFlashInst;
  364. pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
  365. if (pFlash == 0)
  366. return ERR_UNKNOWN_FLASH_TYPE;
  367. if (size_dataflash(pFlash,addr,size) == 0)
  368. return ERR_INVAL;
  369. return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
  370. }
  371. /*---------------------------------------------------------------------------*/
  372. /* Function Name : write_dataflash */
  373. /* Object : write a block in dataflash */
  374. /*---------------------------------------------------------------------------*/
  375. int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
  376. unsigned long size)
  377. {
  378. unsigned long AddrToWrite = addr_dest;
  379. AT91PS_DataFlash pFlash = &DataFlashInst;
  380. pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
  381. if (pFlash == 0)
  382. return ERR_UNKNOWN_FLASH_TYPE;
  383. if (size_dataflash(pFlash,addr_dest,size) == 0)
  384. return ERR_INVAL;
  385. if (prot_dataflash(pFlash,addr_dest) == 0)
  386. return ERR_PROTECTED;
  387. if (AddrToWrite == -1)
  388. return -1;
  389. return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
  390. AddrToWrite, size);
  391. }
  392. void dataflash_perror (int err)
  393. {
  394. switch (err) {
  395. case ERR_OK:
  396. break;
  397. case ERR_TIMOUT:
  398. printf("Timeout writing to DataFlash\n");
  399. break;
  400. case ERR_PROTECTED:
  401. printf("Can't write to protected/invalid DataFlash sectors\n");
  402. break;
  403. case ERR_INVAL:
  404. printf("Outside available DataFlash\n");
  405. break;
  406. case ERR_UNKNOWN_FLASH_TYPE:
  407. printf("Unknown Type of DataFlash\n");
  408. break;
  409. case ERR_PROG_ERROR:
  410. printf("General DataFlash Programming Error\n");
  411. break;
  412. default:
  413. printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
  414. break;
  415. }
  416. }