dataflash.c 14 KB

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