dataflash.c 16 KB

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