dataflash.c 13 KB

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