dataflash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #ifdef CONFIG_HAS_DATAFLASH
  23. #include <asm/hardware.h>
  24. #include <dataflash.h>
  25. AT91S_DATAFLASH_INFO dataflash_info[CFG_MAX_DATAFLASH_BANKS];
  26. static AT91S_DataFlash DataFlashInst;
  27. #ifdef CONFIG_AT91SAM9260EK
  28. int cs[][CFG_MAX_DATAFLASH_BANKS] = {
  29. {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
  30. {CFG_DATAFLASH_LOGIC_ADDR_CS1, 1}
  31. };
  32. #elif defined(CONFIG_AT91SAM9263EK)
  33. int cs[][CFG_MAX_DATAFLASH_BANKS] = {
  34. {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0} /* Logical adress, CS */
  35. };
  36. #else
  37. int cs[][CFG_MAX_DATAFLASH_BANKS] = {
  38. {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
  39. {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
  40. };
  41. #endif
  42. /*define the area offsets*/
  43. #if defined(CONFIG_AT91SAM9261EK) || defined(CONFIG_AT91SAM9260EK) || defined(CONFIG_AT91SAM9263EK)
  44. #if defined(CONFIG_NEW_PARTITION)
  45. dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
  46. {0x00000000, 0x00003FFF, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
  47. {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment */
  48. {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
  49. {0x0003DE00, 0x00041FFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "MON"}, /* Room for alternative boot monitor */
  50. {0x00042000, 0x0018BFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
  51. {0x0018C000, 0xFFFFFFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
  52. };
  53. #else
  54. dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
  55. {0, 0x3fff, FLAG_PROTECT_SET}, /* ROM code */
  56. {0x4000, 0x7fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
  57. {0x8000, 0x37fff, FLAG_PROTECT_SET}, /* u-boot code */
  58. {0x38000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
  59. };
  60. #endif
  61. #elif defined(CONFIG_NEW_PARTITION)
  62. /*define the area offsets*/
  63. /* Invalid partitions should be defined with start > end */
  64. dataflash_protect_t area_list[NB_DATAFLASH_AREA*CFG_MAX_DATAFLASH_BANKS] = {
  65. {0x00000000, 0x000083ff, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
  66. {0x00008400, 0x00020fff, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
  67. {0x00021000, 0x000293ff, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment 8Kb */
  68. {0x00029400, 0x00041fff, FLAG_PROTECT_INVALID, 0, "<Unused>"}, /* Rest of Sector 1 */
  69. {0x00042000, 0x0018Bfff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
  70. {0x0018C000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
  71. {0x00000000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "Data"}, /* data area */
  72. {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
  73. {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
  74. {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
  75. {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
  76. {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
  77. };
  78. #else
  79. dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
  80. {0, 0x7fff, FLAG_PROTECT_SET}, /* ROM code */
  81. {0x8000, 0x1ffff, FLAG_PROTECT_SET}, /* u-boot code */
  82. {0x20000, 0x27fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
  83. {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
  84. };
  85. #endif
  86. extern void AT91F_SpiInit (void);
  87. extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
  88. extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
  89. unsigned long addr,
  90. unsigned long size, char *buffer);
  91. extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
  92. unsigned char *src,
  93. int dest,
  94. int size );
  95. int AT91F_DataflashInit (void)
  96. {
  97. int i, j;
  98. int dfcode;
  99. int part = 0;
  100. int last_part;
  101. int found[CFG_MAX_DATAFLASH_BANKS];
  102. unsigned char protected;
  103. AT91F_SpiInit ();
  104. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  105. found[i] = 0;
  106. dataflash_info[i].Desc.state = IDLE;
  107. dataflash_info[i].id = 0;
  108. dataflash_info[i].Device.pages_number = 0;
  109. dfcode = AT91F_DataflashProbe (cs[i][1],
  110. &dataflash_info[i].Desc);
  111. switch (dfcode) {
  112. case AT45DB161:
  113. dataflash_info[i].Device.pages_number = 4096;
  114. dataflash_info[i].Device.pages_size = 528;
  115. dataflash_info[i].Device.page_offset = 10;
  116. dataflash_info[i].Device.byte_mask = 0x300;
  117. dataflash_info[i].Device.cs = cs[i][1];
  118. dataflash_info[i].Desc.DataFlash_state = IDLE;
  119. dataflash_info[i].logical_address = cs[i][0];
  120. dataflash_info[i].id = dfcode;
  121. found[i] += dfcode;;
  122. break;
  123. case AT45DB321:
  124. dataflash_info[i].Device.pages_number = 8192;
  125. dataflash_info[i].Device.pages_size = 528;
  126. dataflash_info[i].Device.page_offset = 10;
  127. dataflash_info[i].Device.byte_mask = 0x300;
  128. dataflash_info[i].Device.cs = cs[i][1];
  129. dataflash_info[i].Desc.DataFlash_state = IDLE;
  130. dataflash_info[i].logical_address = cs[i][0];
  131. dataflash_info[i].id = dfcode;
  132. found[i] += dfcode;;
  133. break;
  134. case AT45DB642:
  135. dataflash_info[i].Device.pages_number = 8192;
  136. dataflash_info[i].Device.pages_size = 1056;
  137. dataflash_info[i].Device.page_offset = 11;
  138. dataflash_info[i].Device.byte_mask = 0x700;
  139. dataflash_info[i].Device.cs = cs[i][1];
  140. dataflash_info[i].Desc.DataFlash_state = IDLE;
  141. dataflash_info[i].logical_address = cs[i][0];
  142. dataflash_info[i].id = dfcode;
  143. found[i] += dfcode;;
  144. break;
  145. case AT45DB128:
  146. dataflash_info[i].Device.pages_number = 16384;
  147. dataflash_info[i].Device.pages_size = 1056;
  148. dataflash_info[i].Device.page_offset = 11;
  149. dataflash_info[i].Device.byte_mask = 0x700;
  150. dataflash_info[i].Device.cs = cs[i][1];
  151. dataflash_info[i].Desc.DataFlash_state = IDLE;
  152. dataflash_info[i].logical_address = cs[i][0];
  153. dataflash_info[i].id = dfcode;
  154. found[i] += dfcode;;
  155. break;
  156. default:
  157. dfcode = 0;
  158. break;
  159. }
  160. /* set the last area end to the dataflash size*/
  161. area_list[NB_DATAFLASH_AREA -1].end =
  162. (dataflash_info[i].Device.pages_number *
  163. dataflash_info[i].Device.pages_size)-1;
  164. last_part=0;
  165. /* set the area addresses */
  166. for(j = 0; j<NB_DATAFLASH_AREA; j++) {
  167. if(found[i]!=0) {
  168. dataflash_info[i].Device.area_list[j].start =
  169. area_list[part].start +
  170. dataflash_info[i].logical_address;
  171. if(area_list[part].end == 0xffffffff) {
  172. dataflash_info[i].Device.area_list[j].end =
  173. dataflash_info[i].end_address +
  174. dataflash_info [i].logical_address;
  175. last_part = 1;
  176. } else {
  177. dataflash_info[i].Device.area_list[j].end =
  178. area_list[part].end +
  179. dataflash_info[i].logical_address;
  180. }
  181. protected = area_list[part].protected;
  182. /* Set the environment according to the label...*/
  183. if(protected == FLAG_PROTECT_INVALID) {
  184. dataflash_info[i].Device.area_list[j].protected =
  185. FLAG_PROTECT_INVALID;
  186. } else {
  187. dataflash_info[i].Device.area_list[j].protected =
  188. protected;
  189. }
  190. strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
  191. (const char *)area_list[part].label);
  192. }
  193. part++;
  194. }
  195. }
  196. return found[0];
  197. }
  198. #ifdef CONFIG_NEW_DF_PARTITION
  199. int AT91F_DataflashSetEnv (void)
  200. {
  201. int i, j;
  202. int part;
  203. unsigned char env;
  204. unsigned char s[32]; /* Will fit a long int in hex */
  205. unsigned long start;
  206. for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  207. for(j = 0; j<NB_DATAFLASH_AREA; j++) {
  208. env = area_list[part].setenv;
  209. /* Set the environment according to the label...*/
  210. if((env & FLAG_SETENV) == FLAG_SETENV) {
  211. start =
  212. dataflash_info[i].Device.area_list[j].start;
  213. sprintf(s,"%X",start);
  214. setenv(area_list[part].label,s);
  215. }
  216. part++;
  217. }
  218. }
  219. }
  220. #endif
  221. void dataflash_print_info (void)
  222. {
  223. int i, j;
  224. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  225. if (dataflash_info[i].id != 0) {
  226. printf("DataFlash:");
  227. switch (dataflash_info[i].id) {
  228. case AT45DB161:
  229. printf("AT45DB161\n");
  230. break;
  231. case AT45DB321:
  232. printf("AT45DB321\n");
  233. break;
  234. case AT45DB642:
  235. printf("AT45DB642\n");
  236. break;
  237. case AT45DB128:
  238. printf("AT45DB128\n");
  239. break;
  240. }
  241. printf("Nb pages: %6d\n"
  242. "Page Size: %6d\n"
  243. "Size=%8d bytes\n"
  244. "Logical address: 0x%08X\n",
  245. (unsigned int) dataflash_info[i].Device.pages_number,
  246. (unsigned int) dataflash_info[i].Device.pages_size,
  247. (unsigned int) dataflash_info[i].Device.pages_number *
  248. dataflash_info[i].Device.pages_size,
  249. (unsigned int) dataflash_info[i].logical_address);
  250. for (j=0; j< NB_DATAFLASH_AREA; j++) {
  251. switch(dataflash_info[i].Device.area_list[j].protected) {
  252. case FLAG_PROTECT_SET:
  253. case FLAG_PROTECT_CLEAR:
  254. printf("Area %i:\t%08lX to %08lX %s", j,
  255. dataflash_info[i].Device.area_list[j].start,
  256. dataflash_info[i].Device.area_list[j].end,
  257. (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
  258. #ifdef CONFIG_NEW_DF_PARTITION
  259. printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
  260. #else
  261. printf("\n");
  262. #endif
  263. break;
  264. #ifdef CONFIG_NEW_DF_PARTITION
  265. case FLAG_PROTECT_INVALID:
  266. break;
  267. #endif
  268. }
  269. }
  270. }
  271. }
  272. }
  273. /*---------------------------------------------------------------------------*/
  274. /* Function Name : AT91F_DataflashSelect */
  275. /* Object : Select the correct device */
  276. /*---------------------------------------------------------------------------*/
  277. AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
  278. unsigned long *addr)
  279. {
  280. char addr_valid = 0;
  281. int i;
  282. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
  283. if ( dataflash_info[i].id
  284. && ((((int) addr) & 0xFF000000) ==
  285. dataflash_info[i].logical_address)) {
  286. addr_valid = 1;
  287. break;
  288. }
  289. if (!addr_valid) {
  290. pFlash = (AT91PS_DataFlash) 0;
  291. return pFlash;
  292. }
  293. pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
  294. pFlash->pDevice = &(dataflash_info[i].Device);
  295. *addr -= dataflash_info[i].logical_address;
  296. return (pFlash);
  297. }
  298. /*---------------------------------------------------------------------------*/
  299. /* Function Name : addr_dataflash */
  300. /* Object : Test if address is valid */
  301. /*---------------------------------------------------------------------------*/
  302. int addr_dataflash (unsigned long addr)
  303. {
  304. int addr_valid = 0;
  305. int i;
  306. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  307. if ((((int) addr) & 0xFF000000) ==
  308. dataflash_info[i].logical_address) {
  309. addr_valid = 1;
  310. break;
  311. }
  312. }
  313. return addr_valid;
  314. }
  315. /*---------------------------------------------------------------------------*/
  316. /* Function Name : size_dataflash */
  317. /* Object : Test if address is valid regarding the size */
  318. /*---------------------------------------------------------------------------*/
  319. int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
  320. unsigned long size)
  321. {
  322. /* is outside the dataflash */
  323. if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
  324. pdataFlash->pDevice->pages_number)) return 0;
  325. /* is too large for the dataflash */
  326. if (size > ((pdataFlash->pDevice->pages_size *
  327. pdataFlash->pDevice->pages_number) -
  328. ((int)addr & 0x0FFFFFFF))) return 0;
  329. return 1;
  330. }
  331. /*---------------------------------------------------------------------------*/
  332. /* Function Name : prot_dataflash */
  333. /* Object : Test if destination area is protected */
  334. /*---------------------------------------------------------------------------*/
  335. int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
  336. {
  337. int area;
  338. /* find area */
  339. for (area=0; area < NB_DATAFLASH_AREA; area++) {
  340. if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
  341. (addr < pdataFlash->pDevice->area_list[area].end))
  342. break;
  343. }
  344. if (area == NB_DATAFLASH_AREA)
  345. return -1;
  346. /*test protection value*/
  347. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
  348. return 0;
  349. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
  350. return 0;
  351. return 1;
  352. }
  353. /*--------------------------------------------------------------------------*/
  354. /* Function Name : dataflash_real_protect */
  355. /* Object : protect/unprotect area */
  356. /*--------------------------------------------------------------------------*/
  357. int dataflash_real_protect (int flag, unsigned long start_addr,
  358. unsigned long end_addr)
  359. {
  360. int i,j, area1, area2, addr_valid = 0;
  361. /* find dataflash */
  362. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  363. if ((((int) start_addr) & 0xF0000000) ==
  364. dataflash_info[i].logical_address) {
  365. addr_valid = 1;
  366. break;
  367. }
  368. }
  369. if (!addr_valid) {
  370. return -1;
  371. }
  372. /* find start area */
  373. for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
  374. if (start_addr == dataflash_info[i].Device.area_list[area1].start)
  375. break;
  376. }
  377. if (area1 == NB_DATAFLASH_AREA) return -1;
  378. /* find end area */
  379. for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
  380. if (end_addr == dataflash_info[i].Device.area_list[area2].end)
  381. break;
  382. }
  383. if (area2 == NB_DATAFLASH_AREA)
  384. return -1;
  385. /*set protection value*/
  386. for(j = area1; j < area2+1 ; j++)
  387. if(dataflash_info[i].Device.area_list[j].protected
  388. != FLAG_PROTECT_INVALID) {
  389. if (flag == 0) {
  390. dataflash_info[i].Device.area_list[j].protected
  391. = FLAG_PROTECT_CLEAR;
  392. } else {
  393. dataflash_info[i].Device.area_list[j].protected
  394. = FLAG_PROTECT_SET;
  395. }
  396. }
  397. return (area2-area1+1);
  398. }
  399. /*---------------------------------------------------------------------------*/
  400. /* Function Name : read_dataflash */
  401. /* Object : dataflash memory read */
  402. /*---------------------------------------------------------------------------*/
  403. int read_dataflash (unsigned long addr, unsigned long size, char *result)
  404. {
  405. unsigned long AddrToRead = addr;
  406. AT91PS_DataFlash pFlash = &DataFlashInst;
  407. pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
  408. if (pFlash == 0)
  409. return ERR_UNKNOWN_FLASH_TYPE;
  410. if (size_dataflash(pFlash,addr,size) == 0)
  411. return ERR_INVAL;
  412. return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
  413. }
  414. /*---------------------------------------------------------------------------*/
  415. /* Function Name : write_dataflash */
  416. /* Object : write a block in dataflash */
  417. /*---------------------------------------------------------------------------*/
  418. int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
  419. unsigned long size)
  420. {
  421. unsigned long AddrToWrite = addr_dest;
  422. AT91PS_DataFlash pFlash = &DataFlashInst;
  423. pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
  424. if (pFlash == 0)
  425. return ERR_UNKNOWN_FLASH_TYPE;
  426. if (size_dataflash(pFlash,addr_dest,size) == 0)
  427. return ERR_INVAL;
  428. if (prot_dataflash(pFlash,addr_dest) == 0)
  429. return ERR_PROTECTED;
  430. if (AddrToWrite == -1)
  431. return -1;
  432. return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
  433. AddrToWrite, size);
  434. }
  435. void dataflash_perror (int err)
  436. {
  437. switch (err) {
  438. case ERR_OK:
  439. break;
  440. case ERR_TIMOUT:
  441. printf("Timeout writing to DataFlash\n");
  442. break;
  443. case ERR_PROTECTED:
  444. printf("Can't write to protected/invalid DataFlash sectors\n");
  445. break;
  446. case ERR_INVAL:
  447. printf("Outside available DataFlash\n");
  448. break;
  449. case ERR_UNKNOWN_FLASH_TYPE:
  450. printf("Unknown Type of DataFlash\n");
  451. break;
  452. case ERR_PROG_ERROR:
  453. printf("General DataFlash Programming Error\n");
  454. break;
  455. default:
  456. printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
  457. break;
  458. }
  459. }
  460. #endif