dataflash.c 13 KB

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