dataflash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. int cs[][CFG_MAX_DATAFLASH_BANKS] = {
  28. {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
  29. {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
  30. };
  31. /*define the area offsets*/
  32. dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
  33. {0, 0x7fff, FLAG_PROTECT_SET}, /* ROM code */
  34. {0x8000, 0x1ffff, FLAG_PROTECT_SET}, /* u-boot code */
  35. {0x20000, 0x27fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
  36. {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
  37. };
  38. extern void AT91F_SpiInit (void);
  39. extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
  40. extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
  41. unsigned long addr,
  42. unsigned long size, char *buffer);
  43. extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
  44. unsigned char *src,
  45. int dest,
  46. int size );
  47. int AT91F_DataflashInit (void)
  48. {
  49. int i, j;
  50. int dfcode;
  51. AT91F_SpiInit ();
  52. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  53. dataflash_info[i].Desc.state = IDLE;
  54. dataflash_info[i].id = 0;
  55. dataflash_info[i].Device.pages_number = 0;
  56. dfcode = AT91F_DataflashProbe (cs[i][1], &dataflash_info[i].Desc);
  57. switch (dfcode) {
  58. case AT45DB161:
  59. dataflash_info[i].Device.pages_number = 4096;
  60. dataflash_info[i].Device.pages_size = 528;
  61. dataflash_info[i].Device.page_offset = 10;
  62. dataflash_info[i].Device.byte_mask = 0x300;
  63. dataflash_info[i].Device.cs = cs[i][1];
  64. dataflash_info[i].Desc.DataFlash_state = IDLE;
  65. dataflash_info[i].logical_address = cs[i][0];
  66. dataflash_info[i].id = dfcode;
  67. break;
  68. case AT45DB321:
  69. dataflash_info[i].Device.pages_number = 8192;
  70. dataflash_info[i].Device.pages_size = 528;
  71. dataflash_info[i].Device.page_offset = 10;
  72. dataflash_info[i].Device.byte_mask = 0x300;
  73. dataflash_info[i].Device.cs = cs[i][1];
  74. dataflash_info[i].Desc.DataFlash_state = IDLE;
  75. dataflash_info[i].logical_address = cs[i][0];
  76. dataflash_info[i].id = dfcode;
  77. break;
  78. case AT45DB642:
  79. dataflash_info[i].Device.pages_number = 8192;
  80. dataflash_info[i].Device.pages_size = 1056;
  81. dataflash_info[i].Device.page_offset = 11;
  82. dataflash_info[i].Device.byte_mask = 0x700;
  83. dataflash_info[i].Device.cs = cs[i][1];
  84. dataflash_info[i].Desc.DataFlash_state = IDLE;
  85. dataflash_info[i].logical_address = cs[i][0];
  86. dataflash_info[i].id = dfcode;
  87. break;
  88. case AT45DB128:
  89. dataflash_info[i].Device.pages_number = 16384;
  90. dataflash_info[i].Device.pages_size = 1056;
  91. dataflash_info[i].Device.page_offset = 11;
  92. dataflash_info[i].Device.byte_mask = 0x700;
  93. dataflash_info[i].Device.cs = cs[i][1];
  94. dataflash_info[i].Desc.DataFlash_state = IDLE;
  95. dataflash_info[i].logical_address = cs[i][0];
  96. dataflash_info[i].id = dfcode;
  97. break;
  98. default:
  99. break;
  100. }
  101. /* set the last area end to the dataflash size*/
  102. area_list[NB_DATAFLASH_AREA -1].end =
  103. (dataflash_info[i].Device.pages_number *
  104. dataflash_info[i].Device.pages_size)-1;
  105. /* set the area addresses */
  106. for(j = 0; j<NB_DATAFLASH_AREA; j++) {
  107. dataflash_info[i].Device.area_list[j].start = area_list[j].start + dataflash_info[i].logical_address;
  108. dataflash_info[i].Device.area_list[j].end = area_list[j].end + dataflash_info[i].logical_address;
  109. dataflash_info[i].Device.area_list[j].protected = area_list[j].protected;
  110. }
  111. }
  112. return (1);
  113. }
  114. void dataflash_print_info (void)
  115. {
  116. int i, j;
  117. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  118. if (dataflash_info[i].id != 0) {
  119. printf ("DataFlash:");
  120. switch (dataflash_info[i].id) {
  121. case AT45DB161:
  122. printf ("AT45DB161\n");
  123. break;
  124. case AT45DB321:
  125. printf ("AT45DB321\n");
  126. break;
  127. case AT45DB642:
  128. printf ("AT45DB642\n");
  129. break;
  130. case AT45DB128:
  131. printf ("AT45DB128\n");
  132. break;
  133. }
  134. printf ("Nb pages: %6d\n"
  135. "Page Size: %6d\n"
  136. "Size=%8d bytes\n"
  137. "Logical address: 0x%08X\n",
  138. (unsigned int) dataflash_info[i].Device.pages_number,
  139. (unsigned int) dataflash_info[i].Device.pages_size,
  140. (unsigned int) dataflash_info[i].Device.pages_number *
  141. dataflash_info[i].Device.pages_size,
  142. (unsigned int) dataflash_info[i].logical_address);
  143. for (j=0; j< NB_DATAFLASH_AREA; j++) {
  144. printf ("Area %i:\t%08lX to %08lX %s\n", j,
  145. dataflash_info[i].Device.area_list[j].start,
  146. dataflash_info[i].Device.area_list[j].end,
  147. (dataflash_info[i].Device.area_list[j].protected ==
  148. FLAG_PROTECT_SET) ? "(RO)" : "");
  149. }
  150. }
  151. }
  152. }
  153. /*------------------------------------------------------------------------------*/
  154. /* Function Name : AT91F_DataflashSelect */
  155. /* Object : Select the correct device */
  156. /*------------------------------------------------------------------------------*/
  157. AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
  158. unsigned int *addr)
  159. {
  160. char addr_valid = 0;
  161. int i;
  162. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
  163. if ((*addr & 0xFF000000) == dataflash_info[i].logical_address) {
  164. addr_valid = 1;
  165. break;
  166. }
  167. if (!addr_valid) {
  168. pFlash = (AT91PS_DataFlash) 0;
  169. return pFlash;
  170. }
  171. pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
  172. pFlash->pDevice = &(dataflash_info[i].Device);
  173. *addr -= dataflash_info[i].logical_address;
  174. return (pFlash);
  175. }
  176. /*------------------------------------------------------------------------------*/
  177. /* Function Name : addr_dataflash */
  178. /* Object : Test if address is valid */
  179. /*------------------------------------------------------------------------------*/
  180. int addr_dataflash (unsigned long addr)
  181. {
  182. int addr_valid = 0;
  183. int i;
  184. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  185. if ((((int) addr) & 0xFF000000) ==
  186. dataflash_info[i].logical_address) {
  187. addr_valid = 1;
  188. break;
  189. }
  190. }
  191. return addr_valid;
  192. }
  193. /*-----------------------------------------------------------------------------*/
  194. /* Function Name : size_dataflash */
  195. /* Object : Test if address is valid regarding the size */
  196. /*-----------------------------------------------------------------------------*/
  197. int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr, unsigned long size)
  198. {
  199. /* is outside the dataflash */
  200. if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
  201. pdataFlash->pDevice->pages_number)) return 0;
  202. /* is too large for the dataflash */
  203. if (size > ((pdataFlash->pDevice->pages_size *
  204. pdataFlash->pDevice->pages_number) - ((int)addr & 0x0FFFFFFF))) return 0;
  205. return 1;
  206. }
  207. /*-----------------------------------------------------------------------------*/
  208. /* Function Name : prot_dataflash */
  209. /* Object : Test if destination area is protected */
  210. /*-----------------------------------------------------------------------------*/
  211. int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
  212. {
  213. int area;
  214. /* find area */
  215. for (area=0; area < NB_DATAFLASH_AREA; area++) {
  216. if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
  217. (addr < pdataFlash->pDevice->area_list[area].end))
  218. break;
  219. }
  220. if (area == NB_DATAFLASH_AREA) return -1;
  221. /*test protection value*/
  222. if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET) return 0;
  223. return 1;
  224. }
  225. /*-----------------------------------------------------------------------------*/
  226. /* Function Name : dataflash_real_protect */
  227. /* Object : protect/unprotect area */
  228. /*-----------------------------------------------------------------------------*/
  229. int dataflash_real_protect (int flag, unsigned long start_addr, unsigned long end_addr)
  230. {
  231. int i,j, area1, area2, addr_valid = 0;
  232. /* find dataflash */
  233. for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
  234. if ((((int) start_addr) & 0xF0000000) ==
  235. dataflash_info[i].logical_address) {
  236. addr_valid = 1;
  237. break;
  238. }
  239. }
  240. if (!addr_valid) {
  241. return -1;
  242. }
  243. /* find start area */
  244. for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
  245. if (start_addr == dataflash_info[i].Device.area_list[area1].start) break;
  246. }
  247. if (area1 == NB_DATAFLASH_AREA) return -1;
  248. /* find end area */
  249. for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
  250. if (end_addr == dataflash_info[i].Device.area_list[area2].end) break;
  251. }
  252. if (area2 == NB_DATAFLASH_AREA) return -1;
  253. /*set protection value*/
  254. for(j = area1; j < area2+1 ; j++)
  255. if (flag == 0) dataflash_info[i].Device.area_list[j].protected = FLAG_PROTECT_CLEAR;
  256. else dataflash_info[i].Device.area_list[j].protected = FLAG_PROTECT_SET;
  257. return (area2-area1+1);
  258. }
  259. /*------------------------------------------------------------------------------*/
  260. /* Function Name : read_dataflash */
  261. /* Object : dataflash memory read */
  262. /*------------------------------------------------------------------------------*/
  263. int read_dataflash (unsigned long addr, unsigned long size, char *result)
  264. {
  265. int AddrToRead = addr;
  266. AT91PS_DataFlash pFlash = &DataFlashInst;
  267. pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
  268. if (pFlash == 0)
  269. return ERR_UNKNOWN_FLASH_TYPE;
  270. if (size_dataflash(pFlash,addr,size) == 0)
  271. return ERR_INVAL;
  272. return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
  273. }
  274. /*-----------------------------------------------------------------------------*/
  275. /* Function Name : write_dataflash */
  276. /* Object : write a block in dataflash */
  277. /*-----------------------------------------------------------------------------*/
  278. int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
  279. unsigned long size)
  280. {
  281. int AddrToWrite = addr_dest;
  282. AT91PS_DataFlash pFlash = &DataFlashInst;
  283. pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
  284. if (pFlash == 0)
  285. return ERR_UNKNOWN_FLASH_TYPE;
  286. if (size_dataflash(pFlash,addr_dest,size) == 0)
  287. return ERR_INVAL;
  288. if (prot_dataflash(pFlash,addr_dest) == 0)
  289. return ERR_PROTECTED;
  290. if (AddrToWrite == -1)
  291. return -1;
  292. return AT91F_DataFlashWrite (pFlash, (char *) addr_src, AddrToWrite, size);
  293. }
  294. void dataflash_perror (int err)
  295. {
  296. switch (err) {
  297. case ERR_OK:
  298. break;
  299. case ERR_TIMOUT:
  300. printf ("Timeout writing to DataFlash\n");
  301. break;
  302. case ERR_PROTECTED:
  303. printf ("Can't write to protected DataFlash sectors\n");
  304. break;
  305. case ERR_INVAL:
  306. printf ("Outside available DataFlash\n");
  307. break;
  308. case ERR_UNKNOWN_FLASH_TYPE:
  309. printf ("Unknown Type of DataFlash\n");
  310. break;
  311. case ERR_PROG_ERROR:
  312. printf ("General DataFlash Programming Error\n");
  313. break;
  314. default:
  315. printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
  316. break;
  317. }
  318. }
  319. #endif