hikey_bl_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <drivers/arm/pl061_gpio.h>
  8. #include <drivers/arm/sp804_delay_timer.h>
  9. #include <drivers/gpio.h>
  10. #include <lib/mmio.h>
  11. #include <hi6220.h>
  12. #include <hi6553.h>
  13. #include "hikey_private.h"
  14. void hikey_sp804_init(void)
  15. {
  16. uint32_t data;
  17. /* select the clock of dual timer0 */
  18. data = mmio_read_32(AO_SC_TIMER_EN0);
  19. while (data & 3) {
  20. data &= ~3;
  21. data |= 3 << 16;
  22. mmio_write_32(AO_SC_TIMER_EN0, data);
  23. data = mmio_read_32(AO_SC_TIMER_EN0);
  24. }
  25. /* enable the pclk of dual timer0 */
  26. data = mmio_read_32(AO_SC_PERIPH_CLKSTAT4);
  27. while (!(data & PCLK_TIMER1) || !(data & PCLK_TIMER0)) {
  28. mmio_write_32(AO_SC_PERIPH_CLKEN4, PCLK_TIMER1 | PCLK_TIMER0);
  29. data = mmio_read_32(AO_SC_PERIPH_CLKSTAT4);
  30. }
  31. /* reset dual timer0 */
  32. data = mmio_read_32(AO_SC_PERIPH_RSTSTAT4);
  33. mmio_write_32(AO_SC_PERIPH_RSTEN4, PCLK_TIMER1 | PCLK_TIMER0);
  34. do {
  35. data = mmio_read_32(AO_SC_PERIPH_RSTSTAT4);
  36. } while (!(data & PCLK_TIMER1) || !(data & PCLK_TIMER0));
  37. /* unreset dual timer0 */
  38. mmio_write_32(AO_SC_PERIPH_RSTDIS4, PCLK_TIMER1 | PCLK_TIMER0);
  39. do {
  40. data = mmio_read_32(AO_SC_PERIPH_RSTSTAT4);
  41. } while ((data & PCLK_TIMER1) || (data & PCLK_TIMER0));
  42. sp804_timer_init(SP804_TIMER0_BASE, 10, 192);
  43. }
  44. void hikey_gpio_init(void)
  45. {
  46. pl061_gpio_init();
  47. pl061_gpio_register(GPIO0_BASE, 0);
  48. pl061_gpio_register(GPIO1_BASE, 1);
  49. pl061_gpio_register(GPIO2_BASE, 2);
  50. pl061_gpio_register(GPIO3_BASE, 3);
  51. pl061_gpio_register(GPIO4_BASE, 4);
  52. pl061_gpio_register(GPIO5_BASE, 5);
  53. pl061_gpio_register(GPIO6_BASE, 6);
  54. pl061_gpio_register(GPIO7_BASE, 7);
  55. pl061_gpio_register(GPIO8_BASE, 8);
  56. pl061_gpio_register(GPIO9_BASE, 9);
  57. pl061_gpio_register(GPIO10_BASE, 10);
  58. pl061_gpio_register(GPIO11_BASE, 11);
  59. pl061_gpio_register(GPIO12_BASE, 12);
  60. pl061_gpio_register(GPIO13_BASE, 13);
  61. pl061_gpio_register(GPIO14_BASE, 14);
  62. pl061_gpio_register(GPIO15_BASE, 15);
  63. pl061_gpio_register(GPIO16_BASE, 16);
  64. pl061_gpio_register(GPIO17_BASE, 17);
  65. pl061_gpio_register(GPIO18_BASE, 18);
  66. pl061_gpio_register(GPIO19_BASE, 19);
  67. /* Power on indicator LED (USER_LED1). */
  68. gpio_set_direction(32, GPIO_DIR_OUT); /* LED1 */
  69. gpio_set_value(32, GPIO_LEVEL_HIGH);
  70. gpio_set_direction(33, GPIO_DIR_OUT); /* LED2 */
  71. gpio_set_value(33, GPIO_LEVEL_LOW);
  72. gpio_set_direction(34, GPIO_DIR_OUT); /* LED3 */
  73. gpio_set_direction(35, GPIO_DIR_OUT); /* LED4 */
  74. }
  75. void hikey_pmussi_init(void)
  76. {
  77. uint32_t data;
  78. /* Initialize PWR_HOLD GPIO */
  79. gpio_set_direction(0, GPIO_DIR_OUT);
  80. gpio_set_value(0, GPIO_LEVEL_LOW);
  81. /*
  82. * After reset, PMUSSI stays in reset mode.
  83. * Now make it out of reset.
  84. */
  85. mmio_write_32(AO_SC_PERIPH_RSTDIS4,
  86. AO_SC_PERIPH_RSTDIS4_PRESET_PMUSSI_N);
  87. do {
  88. data = mmio_read_32(AO_SC_PERIPH_RSTSTAT4);
  89. } while (data & AO_SC_PERIPH_RSTDIS4_PRESET_PMUSSI_N);
  90. /* Set PMUSSI clock latency for read operation. */
  91. data = mmio_read_32(AO_SC_MCU_SUBSYS_CTRL3);
  92. data &= ~AO_SC_MCU_SUBSYS_CTRL3_RCLK_MASK;
  93. data |= AO_SC_MCU_SUBSYS_CTRL3_RCLK_3;
  94. mmio_write_32(AO_SC_MCU_SUBSYS_CTRL3, data);
  95. /* enable PMUSSI clock */
  96. data = AO_SC_PERIPH_CLKEN5_PCLK_PMUSSI_CCPU |
  97. AO_SC_PERIPH_CLKEN5_PCLK_PMUSSI_MCU;
  98. mmio_write_32(AO_SC_PERIPH_CLKEN5, data);
  99. data = AO_SC_PERIPH_CLKEN4_PCLK_PMUSSI;
  100. mmio_write_32(AO_SC_PERIPH_CLKEN4, data);
  101. gpio_set_value(0, GPIO_LEVEL_HIGH);
  102. }
  103. void hikey_hi6553_init(void)
  104. {
  105. uint8_t data;
  106. mmio_write_8(HI6553_PERI_EN_MARK, 0x1e);
  107. mmio_write_8(HI6553_NP_REG_ADJ1, 0);
  108. data = DISABLE6_XO_CLK_CONN | DISABLE6_XO_CLK_NFC |
  109. DISABLE6_XO_CLK_RF1 | DISABLE6_XO_CLK_RF2;
  110. mmio_write_8(HI6553_DISABLE6_XO_CLK, data);
  111. /* configure BUCK0 & BUCK1 */
  112. mmio_write_8(HI6553_BUCK01_CTRL2, 0x5e);
  113. mmio_write_8(HI6553_BUCK0_CTRL7, 0x10);
  114. mmio_write_8(HI6553_BUCK1_CTRL7, 0x10);
  115. mmio_write_8(HI6553_BUCK0_CTRL5, 0x1e);
  116. mmio_write_8(HI6553_BUCK1_CTRL5, 0x1e);
  117. mmio_write_8(HI6553_BUCK0_CTRL1, 0xfc);
  118. mmio_write_8(HI6553_BUCK1_CTRL1, 0xfc);
  119. /* configure BUCK2 */
  120. mmio_write_8(HI6553_BUCK2_REG1, 0x4f);
  121. mmio_write_8(HI6553_BUCK2_REG5, 0x99);
  122. mmio_write_8(HI6553_BUCK2_REG6, 0x45);
  123. mdelay(1);
  124. mmio_write_8(HI6553_VSET_BUCK2_ADJ, 0x22);
  125. mdelay(1);
  126. /* configure BUCK3 */
  127. mmio_write_8(HI6553_BUCK3_REG3, 0x02);
  128. mmio_write_8(HI6553_BUCK3_REG5, 0x99);
  129. mmio_write_8(HI6553_BUCK3_REG6, 0x41);
  130. mmio_write_8(HI6553_VSET_BUCK3_ADJ, 0x02);
  131. mdelay(1);
  132. /* configure BUCK4 */
  133. mmio_write_8(HI6553_BUCK4_REG2, 0x9a);
  134. mmio_write_8(HI6553_BUCK4_REG5, 0x99);
  135. mmio_write_8(HI6553_BUCK4_REG6, 0x45);
  136. /* configure LDO20 */
  137. mmio_write_8(HI6553_LDO20_REG_ADJ, 0x50);
  138. mmio_write_8(HI6553_NP_REG_CHG, 0x0f);
  139. mmio_write_8(HI6553_CLK_TOP0, 0x06);
  140. mmio_write_8(HI6553_CLK_TOP3, 0xc0);
  141. mmio_write_8(HI6553_CLK_TOP4, 0x00);
  142. /* configure LDO7 & LDO10 for SD slot */
  143. /* enable LDO7 */
  144. data = mmio_read_8(HI6553_LDO7_REG_ADJ);
  145. data = (data & 0xf8) | 0x2;
  146. mmio_write_8(HI6553_LDO7_REG_ADJ, data);
  147. mdelay(5);
  148. mmio_write_8(HI6553_ENABLE2_LDO1_8, 1 << 6);
  149. mdelay(5);
  150. /* enable LDO10 */
  151. data = mmio_read_8(HI6553_LDO10_REG_ADJ);
  152. data = (data & 0xf8) | 0x5;
  153. mmio_write_8(HI6553_LDO10_REG_ADJ, data);
  154. mdelay(5);
  155. mmio_write_8(HI6553_ENABLE3_LDO9_16, 1 << 1);
  156. mdelay(5);
  157. /* enable LDO15 */
  158. data = mmio_read_8(HI6553_LDO15_REG_ADJ);
  159. data = (data & 0xf8) | 0x4;
  160. mmio_write_8(HI6553_LDO15_REG_ADJ, data);
  161. mmio_write_8(HI6553_ENABLE3_LDO9_16, 1 << 6);
  162. mdelay(5);
  163. /* enable LDO19 */
  164. data = mmio_read_8(HI6553_LDO19_REG_ADJ);
  165. data |= 0x7;
  166. mmio_write_8(HI6553_LDO19_REG_ADJ, data);
  167. mmio_write_8(HI6553_ENABLE4_LDO17_22, 1 << 2);
  168. mdelay(5);
  169. /* enable LDO21 */
  170. data = mmio_read_8(HI6553_LDO21_REG_ADJ);
  171. data = (data & 0xf8) | 0x3;
  172. mmio_write_8(HI6553_LDO21_REG_ADJ, data);
  173. mmio_write_8(HI6553_ENABLE4_LDO17_22, 1 << 4);
  174. mdelay(5);
  175. /* enable LDO22 */
  176. data = mmio_read_8(HI6553_LDO22_REG_ADJ);
  177. data = (data & 0xf8) | 0x7;
  178. mmio_write_8(HI6553_LDO22_REG_ADJ, data);
  179. mmio_write_8(HI6553_ENABLE4_LDO17_22, 1 << 5);
  180. mdelay(5);
  181. /* select 32.764KHz */
  182. mmio_write_8(HI6553_CLK19M2_600_586_EN, 0x01);
  183. /* Disable vbus_det interrupts */
  184. data = mmio_read_8(HI6553_IRQ2_MASK);
  185. data = data | 0x3;
  186. mmio_write_8(HI6553_IRQ2_MASK, data);
  187. }
  188. void init_mmc0_pll(void)
  189. {
  190. unsigned int data;
  191. /* select SYSPLL as the source of MMC0 */
  192. /* select SYSPLL as the source of MUX1 (SC_CLK_SEL0) */
  193. mmio_write_32(PERI_SC_CLK_SEL0, 1 << 5 | 1 << 21);
  194. do {
  195. data = mmio_read_32(PERI_SC_CLK_SEL0);
  196. } while (!(data & (1 << 5)));
  197. /* select MUX1 as the source of MUX2 (SC_CLK_SEL0) */
  198. mmio_write_32(PERI_SC_CLK_SEL0, 1 << 29);
  199. do {
  200. data = mmio_read_32(PERI_SC_CLK_SEL0);
  201. } while (data & (1 << 13));
  202. mmio_write_32(PERI_SC_PERIPH_CLKEN0, (1 << 0));
  203. do {
  204. data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
  205. } while (!(data & (1 << 0)));
  206. data = mmio_read_32(PERI_SC_PERIPH_CLKEN12);
  207. data |= 1 << 1;
  208. mmio_write_32(PERI_SC_PERIPH_CLKEN12, data);
  209. do {
  210. mmio_write_32(PERI_SC_CLKCFG8BIT1, (1 << 7) | 0xb);
  211. data = mmio_read_32(PERI_SC_CLKCFG8BIT1);
  212. } while ((data & 0xb) != 0xb);
  213. }
  214. void reset_mmc0_clk(void)
  215. {
  216. unsigned int data;
  217. /* disable mmc0 bus clock */
  218. mmio_write_32(PERI_SC_PERIPH_CLKDIS0, PERI_CLK0_MMC0);
  219. do {
  220. data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
  221. } while (data & PERI_CLK0_MMC0);
  222. /* enable mmc0 bus clock */
  223. mmio_write_32(PERI_SC_PERIPH_CLKEN0, PERI_CLK0_MMC0);
  224. do {
  225. data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
  226. } while (!(data & PERI_CLK0_MMC0));
  227. /* reset mmc0 clock domain */
  228. mmio_write_32(PERI_SC_PERIPH_RSTEN0, PERI_RST0_MMC0);
  229. /* bypass mmc0 clock phase */
  230. data = mmio_read_32(PERI_SC_PERIPH_CTRL2);
  231. data |= 3;
  232. mmio_write_32(PERI_SC_PERIPH_CTRL2, data);
  233. /* disable low power */
  234. data = mmio_read_32(PERI_SC_PERIPH_CTRL13);
  235. data |= 1 << 3;
  236. mmio_write_32(PERI_SC_PERIPH_CTRL13, data);
  237. do {
  238. data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
  239. } while (!(data & PERI_RST0_MMC0));
  240. /* unreset mmc0 clock domain */
  241. mmio_write_32(PERI_SC_PERIPH_RSTDIS0, PERI_RST0_MMC0);
  242. do {
  243. data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
  244. } while (data & PERI_RST0_MMC0);
  245. }
  246. void init_media_clk(void)
  247. {
  248. unsigned int data, value;
  249. data = mmio_read_32(PMCTRL_MEDPLLCTRL);
  250. data |= 1;
  251. mmio_write_32(PMCTRL_MEDPLLCTRL, data);
  252. for (;;) {
  253. data = mmio_read_32(PMCTRL_MEDPLLCTRL);
  254. value = 1 << 28;
  255. if ((data & value) == value)
  256. break;
  257. }
  258. data = mmio_read_32(PERI_SC_PERIPH_CLKEN12);
  259. data = 1 << 10;
  260. mmio_write_32(PERI_SC_PERIPH_CLKEN12, data);
  261. }
  262. void init_mmc1_pll(void)
  263. {
  264. uint32_t data;
  265. /* select SYSPLL as the source of MMC1 */
  266. /* select SYSPLL as the source of MUX1 (SC_CLK_SEL0) */
  267. mmio_write_32(PERI_SC_CLK_SEL0, 1 << 11 | 1 << 27);
  268. do {
  269. data = mmio_read_32(PERI_SC_CLK_SEL0);
  270. } while (!(data & (1 << 11)));
  271. /* select MUX1 as the source of MUX2 (SC_CLK_SEL0) */
  272. mmio_write_32(PERI_SC_CLK_SEL0, 1 << 30);
  273. do {
  274. data = mmio_read_32(PERI_SC_CLK_SEL0);
  275. } while (data & (1 << 14));
  276. mmio_write_32(PERI_SC_PERIPH_CLKEN0, (1 << 1));
  277. do {
  278. data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
  279. } while (!(data & (1 << 1)));
  280. data = mmio_read_32(PERI_SC_PERIPH_CLKEN12);
  281. data |= 1 << 2;
  282. mmio_write_32(PERI_SC_PERIPH_CLKEN12, data);
  283. do {
  284. /* 1.2GHz / 50 = 24MHz */
  285. mmio_write_32(PERI_SC_CLKCFG8BIT2, 0x31 | (1 << 7));
  286. data = mmio_read_32(PERI_SC_CLKCFG8BIT2);
  287. } while ((data & 0x31) != 0x31);
  288. }
  289. void reset_mmc1_clk(void)
  290. {
  291. unsigned int data;
  292. /* disable mmc1 bus clock */
  293. mmio_write_32(PERI_SC_PERIPH_CLKDIS0, PERI_CLK0_MMC1);
  294. do {
  295. data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
  296. } while (data & PERI_CLK0_MMC1);
  297. /* enable mmc1 bus clock */
  298. mmio_write_32(PERI_SC_PERIPH_CLKEN0, PERI_CLK0_MMC1);
  299. do {
  300. data = mmio_read_32(PERI_SC_PERIPH_CLKSTAT0);
  301. } while (!(data & PERI_CLK0_MMC1));
  302. /* reset mmc1 clock domain */
  303. mmio_write_32(PERI_SC_PERIPH_RSTEN0, PERI_RST0_MMC1);
  304. /* bypass mmc1 clock phase */
  305. data = mmio_read_32(PERI_SC_PERIPH_CTRL2);
  306. data |= 3 << 2;
  307. mmio_write_32(PERI_SC_PERIPH_CTRL2, data);
  308. /* disable low power */
  309. data = mmio_read_32(PERI_SC_PERIPH_CTRL13);
  310. data |= 1 << 4;
  311. mmio_write_32(PERI_SC_PERIPH_CTRL13, data);
  312. do {
  313. data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
  314. } while (!(data & PERI_RST0_MMC1));
  315. /* unreset mmc0 clock domain */
  316. mmio_write_32(PERI_SC_PERIPH_RSTDIS0, PERI_RST0_MMC1);
  317. do {
  318. data = mmio_read_32(PERI_SC_PERIPH_RSTSTAT0);
  319. } while (data & PERI_RST0_MMC1);
  320. }
  321. /* Initialize PLL of both eMMC and SD controllers. */
  322. void hikey_mmc_pll_init(void)
  323. {
  324. init_mmc0_pll();
  325. reset_mmc0_clk();
  326. init_media_clk();
  327. dsb();
  328. init_mmc1_pll();
  329. reset_mmc1_clk();
  330. }
  331. void hikey_rtc_init(void)
  332. {
  333. uint32_t data;
  334. data = mmio_read_32(AO_SC_PERIPH_CLKEN4);
  335. data |= AO_SC_PERIPH_RSTDIS4_RESET_RTC0_N;
  336. mmio_write_32(AO_SC_PERIPH_CLKEN4, data);
  337. }