dataflash.c 14 KB

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