/u-boot-2011.09/arch/arm/cpu/armv7/sun8iw9/nand/nand_bsp/nand_for_boot0/osal/nand_osal_boot0.c

https://gitlab.com/pine64-android/brandy · C · 454 lines · 277 code · 64 blank · 113 comment · 25 complexity · f9eaf0e698f03c2b8d117ece121a4b5d MD5 · raw file

  1. /*
  2. **********************************************************************************************************************
  3. * eGon
  4. * the Embedded GO-ON Bootloader System
  5. * eGON arm boot sub-system
  6. *
  7. * Copyright(C), 2006-2010, SoftWinners Microelectronic Co., Ltd.
  8. * All Rights Reserved
  9. *
  10. * File : nand_osal_boot0.c
  11. *
  12. * By : Jerry
  13. *
  14. * Version : V2.00
  15. *
  16. * Date :
  17. *
  18. * Descript:
  19. **********************************************************************************************************************
  20. */
  21. #include "../../nand_common.h"
  22. int NAND_Print(const char * str, ...);
  23. #define get_wvalue(addr) (*((volatile unsigned long *)(addr)))
  24. #define put_wvalue(addr, v) (*((volatile unsigned long *)(addr)) = (unsigned long)(v))
  25. u32 _get_pll4_periph1_clk(void)
  26. {
  27. #if 1
  28. return 24000000;
  29. #else
  30. u32 n,div1,div2;
  31. u32 rval;
  32. rval = get_wvalue((0x06000000 + 0xC)); //read pll4-periph1 register
  33. n = (0xff & (rval >> 8));
  34. div1 = (0x1 & (rval >> 16));
  35. div2 = (0x1 & (rval >> 18));
  36. rval = 24000000 * n / (div1+1) / (div2+1);;
  37. return rval; //24000000 * n / (div1+1) / (div2+1);
  38. #endif
  39. }
  40. __u32 _Getpll6Clk(void)
  41. {
  42. return 600;
  43. }
  44. int NAND_ClkRequest(__u32 nand_index)
  45. {
  46. __u32 cfg;
  47. __u32 m, n;
  48. __u32 clk_src;
  49. clk_src = 0;
  50. if(nand_index == 0)
  51. {
  52. //10M
  53. if(clk_src == 0)
  54. {
  55. m = 0;
  56. n = 0;
  57. }
  58. else
  59. {
  60. m = 14;
  61. n = 1;
  62. }
  63. /*set nand clock gate on*/
  64. cfg = 0;
  65. /*gate on nand clock*/
  66. cfg |= (1U << 31);
  67. /*take pll6 as nand src block*/
  68. cfg |= ((clk_src&0x3) << 24);
  69. //set divn = 0
  70. cfg |= (n&0x3)<<16;
  71. cfg |= (m&0xf)<<0;
  72. *(volatile __u32 *)(0x01c20000 + 0x80) = cfg;
  73. //open ahb
  74. cfg = *(volatile __u32 *)(0x01c20000 + 0x60);
  75. cfg |= (0x1<<13);
  76. *(volatile __u32 *)(0x01c20000 + 0x60) = cfg;
  77. //reset
  78. cfg = *(volatile __u32 *)(0x01c20000 + 0x2c0);
  79. cfg &= (~(0x1<<13));
  80. *(volatile __u32 *)(0x01c20000 + 0x2c0) = cfg;
  81. cfg = *(volatile __u32 *)(0x01c20000 + 0x2c0);
  82. cfg |= (0x1<<13);
  83. *(volatile __u32 *)(0x01c20000 + 0x2c0) = cfg;
  84. cfg = *(volatile __u32 *)(0x01c20000 + 0x2c0);
  85. cfg &= (~(0x1<<13));
  86. *(volatile __u32 *)(0x01c20000 + 0x2c0) = cfg;
  87. cfg = *(volatile __u32 *)(0x01c20000 + 0x2c0);
  88. cfg |= (0x1<<13);
  89. *(volatile __u32 *)(0x01c20000 + 0x2c0) = cfg;
  90. NAND_Print("NAND_ClkRequest, nand_index: 0x%x\n", nand_index);
  91. NAND_Print("Reg 0x01c20080: 0x%x\n", *(volatile __u32 *)(0x01c20080));
  92. NAND_Print("Reg 0x01c20060: 0x%x\n", *(volatile __u32 *)(0x01c20060));
  93. NAND_Print("Reg 0x01c202c0: 0x%x\n", *(volatile __u32 *)(0x01c202c0));
  94. }
  95. else
  96. {
  97. //10M
  98. if(clk_src == 0)
  99. {
  100. m = 0;
  101. n = 0;
  102. }
  103. else
  104. {
  105. m = 14;
  106. n = 1;
  107. }
  108. /*set nand clock gate on*/
  109. cfg = 0;
  110. /*gate on nand clock*/
  111. cfg |= (1U << 31);
  112. /*take pll6 as nand src block*/
  113. cfg |= ((clk_src&0x3) << 24);
  114. //set divn = 0
  115. cfg |= (n&0x3)<<16;
  116. cfg |= (m&0xf)<<0;
  117. *(volatile __u32 *)(0x01c20000 + 0x84) = cfg;
  118. //open ahb
  119. cfg = *(volatile __u32 *)(0x01c20000 + 0x60);
  120. cfg |= (0x1<<12);
  121. *(volatile __u32 *)(0x01c20000 + 0x60) = cfg;
  122. //open reset
  123. cfg = *(volatile __u32 *)(0x01c20000 + 0x2c0);
  124. cfg &= (~(0x1<<12));
  125. *(volatile __u32 *)(0x01c20000 + 0x2c0) = cfg;
  126. cfg = *(volatile __u32 *)(0x01c20000 + 0x2c0);
  127. cfg |= (0x1<<12);
  128. *(volatile __u32 *)(0x01c20000 + 0x2c0) = cfg;
  129. cfg = *(volatile __u32 *)(0x01c20000 + 0x2c0);
  130. cfg &= (~(0x1<<12));
  131. *(volatile __u32 *)(0x01c20000 + 0x2c0) = cfg;
  132. cfg = *(volatile __u32 *)(0x01c20000 + 0x2c0);
  133. cfg |= (0x1<<12);
  134. *(volatile __u32 *)(0x01c20000 + 0x2c0) = cfg;
  135. NAND_Print("NAND_ClkRequest, nand_index: 0x%x\n", nand_index);
  136. NAND_Print("Reg 0x01c20084: 0x%x\n", *(volatile __u32 *)(0x01c20084));
  137. NAND_Print("Reg 0x01c20060: 0x%x\n", *(volatile __u32 *)(0x01c20060));
  138. NAND_Print("Reg 0x01c202c0: 0x%x\n", *(volatile __u32 *)(0x01c202c0));
  139. }
  140. return 0;
  141. }
  142. void NAND_ClkRelease(__u32 nand_index)
  143. {
  144. return ;
  145. }
  146. /*
  147. **********************************************************************************************************************
  148. *
  149. * NAND_GetCmuClk
  150. *
  151. * Description:
  152. *
  153. *
  154. * Parameters:
  155. *
  156. *
  157. * Return value:
  158. *
  159. *
  160. **********************************************************************************************************************
  161. */
  162. int NAND_SetClk(__u32 nand_index, __u32 nand_clock)
  163. {
  164. __u32 edo_clk;
  165. __u32 cfg;
  166. __u32 m = 0, n = 0;
  167. __u32 clk_src;
  168. clk_src = 0;
  169. if(clk_src == 0)
  170. {
  171. }
  172. else
  173. {
  174. edo_clk = nand_clock * 2;
  175. if(edo_clk <= 20) //10M
  176. {
  177. n = 1;
  178. m = 14;
  179. }
  180. else if((edo_clk >20)&&(edo_clk <= 40)) //20M
  181. {
  182. n = 0;
  183. m = 14;
  184. }
  185. else if((edo_clk >40)&&(edo_clk <= 50)) //25M
  186. {
  187. n = 0;
  188. m = 11;
  189. }
  190. else if((edo_clk >50)&&(edo_clk <= 60)) //30M
  191. {
  192. n = 0;
  193. m = 9;
  194. }
  195. else //40M
  196. {
  197. n = 0;
  198. m = 7;
  199. }
  200. }
  201. if(nand_index == 0)
  202. {
  203. /*set nand clock*/
  204. /*set nand clock gate on*/
  205. cfg = *(volatile __u32 *)(0x01c20000 + 0x80);
  206. cfg &= (~(0x03 << 16));
  207. cfg &= (~(0xf));
  208. cfg |= ((n&0x3)<<16);
  209. cfg |= ((m&0xf));
  210. *(volatile __u32 *)(0x01c20000 + 0x80) = cfg;
  211. NAND_Print("NAND_SetClk, nand_index: 0x%x\n", nand_index);
  212. NAND_Print("Reg 0x01c20080: 0x%x\n", *(volatile __u32 *)(0x01c20080));
  213. }
  214. else
  215. {
  216. /*set nand clock*/
  217. /*set nand clock gate on*/
  218. cfg = *(volatile __u32 *)(0x01c20000 + 0x84);
  219. cfg &= (~(0x03 << 16));
  220. cfg &= (~(0xf));
  221. cfg |= ((n&0x3)<<16);
  222. cfg |= ((m&0xf));
  223. *(volatile __u32 *)(0x01c20000 + 0x84) = cfg;
  224. NAND_Print("NAND_SetClk, nand_index: 0x%x\n", nand_index);
  225. NAND_Print("Reg 0x01c20084: 0x%x\n", *(volatile __u32 *)(0x01c20084));
  226. }
  227. return 0;
  228. }
  229. int NAND_GetClk(__u32 nand_index)
  230. {
  231. __u32 pll6_clk;
  232. __u32 cfg;
  233. __u32 nand_max_clock;
  234. __u32 m, n;
  235. if(nand_index == 0)
  236. {
  237. /*set nand clock*/
  238. pll6_clk = _Getpll6Clk();
  239. /*set nand clock gate on*/
  240. cfg = *(volatile __u32 *)(0x01c20000 + 0x80);
  241. m = ((cfg)&0xf) +1;
  242. n = ((cfg>>16)&0x3);
  243. nand_max_clock = pll6_clk/(2*(1<<n)*(m+1));
  244. NAND_Print("NAND_GetClk, nand_index: 0x%x, nand_clk: %dM\n", nand_index, nand_max_clock);
  245. NAND_Print("Reg 0x01c20080: 0x%x\n", *(volatile __u32 *)(0x01c20080));
  246. }
  247. else
  248. {
  249. /*set nand clock*/
  250. pll6_clk = _Getpll6Clk();
  251. /*set nand clock gate on*/
  252. cfg = *(volatile __u32 *)(0x01c20000 + 0x84);
  253. m = ((cfg)&0xf) +1;
  254. n = ((cfg>>16)&0x3);
  255. nand_max_clock = pll6_clk/(2*(1<<n)*(m+1));
  256. NAND_Print("NAND_GetClk, nand_index: 0x%x, nand_clk: %dM\n", nand_index, nand_max_clock);
  257. NAND_Print("Reg 0x01c20084: 0x%x\n", *(volatile __u32 *)(0x01c20084));
  258. }
  259. return nand_max_clock;
  260. }
  261. void NAND_PIORequest(__u32 nand_index)
  262. {
  263. __u32 cfg;
  264. if(nand_index == 0)
  265. {
  266. *(volatile __u32 *)(0x01c20800 + 0x48) = 0x22222222;
  267. *(volatile __u32 *)(0x01c20800 + 0x4c) = 0x22222222;
  268. *(volatile __u32 *)(0x01c20800 + 0x50) = 0x00000222;
  269. //cfg = *(volatile __u32 *)(0x01c20800 + 0x54);
  270. //cfg &= (~0xfff);
  271. //cfg |= 0x222;
  272. //*(volatile __u32 *)(0x01c20800 + 0x54) = cfg;
  273. NAND_Print("NAND_PIORequest, nand_index: 0x%x\n", nand_index);
  274. NAND_Print("Reg 0x01c20848: 0x%x\n", *(volatile __u32 *)(0x01c20848));
  275. NAND_Print("Reg 0x01c2084c: 0x%x\n", *(volatile __u32 *)(0x01c2084c));
  276. NAND_Print("Reg 0x01c20850: 0x%x\n", *(volatile __u32 *)(0x01c20850));
  277. //NAND_Print("Reg 0x01c20854: 0x%x\n", *(volatile __u32 *)(0x01c20854));
  278. }
  279. else if(nand_index == 1)
  280. {
  281. *(volatile __u32 *)(0x01c20800 + 0x50) = 0x33333333;
  282. *(volatile __u32 *)(0x01c20800 + 0xfc) = 0x22222222;
  283. cfg = *(volatile __u32 *)(0x01c20800 + 0x100);
  284. cfg &= (~0xf);
  285. cfg |= (0x2);
  286. *(volatile __u32 *)(0x01c20800 + 0x100) = cfg;
  287. cfg = *(volatile __u32 *)(0x01c20800 + 0x108);
  288. cfg &= (~0xfU<<20);
  289. cfg |= (0x22<<20);
  290. *(volatile __u32 *)(0x01c20800 + 0x108) = cfg;
  291. NAND_Print("NAND_PIORequest, nand_index: 0x%x\n", nand_index);
  292. NAND_Print("Reg 0x01c20850: 0x%x\n", *(volatile __u32 *)(0x01c20850));
  293. NAND_Print("Reg 0x01c208fc: 0x%x\n", *(volatile __u32 *)(0x01c208fc));
  294. NAND_Print("Reg 0x01c20900: 0x%x\n", *(volatile __u32 *)(0x01c20900));
  295. NAND_Print("Reg 0x01c20908: 0x%x\n", *(volatile __u32 *)(0x01c20908));
  296. }
  297. else
  298. {
  299. }
  300. }
  301. void NAND_PIORelease(__u32 nand_index)
  302. {
  303. return;
  304. }
  305. void NAND_EnRbInt(void)
  306. {
  307. return ;
  308. }
  309. void NAND_ClearRbInt(void)
  310. {
  311. return ;
  312. }
  313. int NAND_WaitRbReady(void)
  314. {
  315. return 0;
  316. }
  317. int NAND_WaitDmaFinish(void)
  318. {
  319. return 0;
  320. }
  321. void NAND_RbInterrupt(void)
  322. {
  323. return ;
  324. }
  325. /*
  326. ************************************************************************************************************
  327. *
  328. * OSAL_malloc
  329. *
  330. * º¯ÊýÃû³Æ£º
  331. *
  332. * ²ÎÊýÁÐ±í£º
  333. *
  334. * ·µ»ØÖµ £º
  335. *
  336. * ˵Ã÷ £º ÕâÊÇÒ»¸öÐé¼ÙµÄmallocº¯Êý£¬Ä¿µÄÖ»ÊÇÌṩÕâÑùÒ»¸öº¯Êý£¬±ÜÃâ±àÒ벻ͨ¹ý
  337. * ±¾Éí²»ÌṩÈκεĺ¯Êý¹¦ÄÜ
  338. *
  339. ************************************************************************************************************
  340. */
  341. void* NAND_Malloc(unsigned int Size)
  342. {
  343. // return (void *)malloc(Size);
  344. return (void *)CONFIG_SYS_SDRAM_BASE;
  345. }
  346. /*
  347. ************************************************************************************************************
  348. *
  349. * OSAL_free
  350. *
  351. * º¯ÊýÃû³Æ£º
  352. *
  353. * ²ÎÊýÁÐ±í£º
  354. *
  355. * ·µ»ØÖµ £º
  356. *
  357. * ˵Ã÷ £º ÕâÊÇÒ»¸öÐé¼ÙµÄfreeº¯Êý£¬Ä¿µÄÖ»ÊÇÌṩÕâÑùÒ»¸öº¯Êý£¬±ÜÃâ±àÒ벻ͨ¹ý
  358. * ±¾Éí²»ÌṩÈκεĺ¯Êý¹¦ÄÜ
  359. *
  360. ************************************************************************************************************
  361. */
  362. void NAND_Free(void *pAddr, unsigned int Size)
  363. {
  364. //return free(pAddr);
  365. }
  366. void *NAND_IORemap(unsigned int base_addr, unsigned int size)
  367. {
  368. return (void *)base_addr;
  369. }
  370. /*
  371. **********************************************************************************************************************
  372. *
  373. * OSAL_printf
  374. *
  375. * Description: Óû§¿ÉÒÔ×ÔÐÐÉ趨ÊÇ·ñÐèÒª´òÓ¡
  376. *
  377. *
  378. * Parameters:
  379. *
  380. *
  381. * Return value:
  382. *
  383. *
  384. **********************************************************************************************************************
  385. */
  386. __s32 NAND_Print(const char * str, ...)
  387. {
  388. printf(str);
  389. return 0;
  390. }