mt7620.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Parts of this file are based on Ralink's 2.6.21 BSP
  7. *
  8. * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
  9. * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
  10. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <asm/mipsregs.h>
  16. #include <asm/mach-ralink/ralink_regs.h>
  17. #include <asm/mach-ralink/mt7620.h>
  18. #include <asm/mach-ralink/pinmux.h>
  19. #include "common.h"
  20. /* analog */
  21. #define PMU0_CFG 0x88
  22. #define PMU_SW_SET BIT(28)
  23. #define A_DCDC_EN BIT(24)
  24. #define A_SSC_PERI BIT(19)
  25. #define A_SSC_GEN BIT(18)
  26. #define A_SSC_M 0x3
  27. #define A_SSC_S 16
  28. #define A_DLY_M 0x7
  29. #define A_DLY_S 8
  30. #define A_VTUNE_M 0xff
  31. /* digital */
  32. #define PMU1_CFG 0x8C
  33. #define DIG_SW_SEL BIT(25)
  34. /* clock scaling */
  35. #define CLKCFG_FDIV_MASK 0x1f00
  36. #define CLKCFG_FDIV_USB_VAL 0x0300
  37. #define CLKCFG_FFRAC_MASK 0x001f
  38. #define CLKCFG_FFRAC_USB_VAL 0x0003
  39. /* EFUSE bits */
  40. #define EFUSE_MT7688 0x100000
  41. /* DRAM type bit */
  42. #define DRAM_TYPE_MT7628_MASK 0x1
  43. /* does the board have sdram or ddram */
  44. static int dram_type;
  45. static struct rt2880_pmx_func i2c_grp[] = { FUNC("i2c", 0, 1, 2) };
  46. static struct rt2880_pmx_func spi_grp[] = { FUNC("spi", 0, 3, 4) };
  47. static struct rt2880_pmx_func uartlite_grp[] = { FUNC("uartlite", 0, 15, 2) };
  48. static struct rt2880_pmx_func mdio_grp[] = { FUNC("mdio", 0, 22, 2) };
  49. static struct rt2880_pmx_func rgmii1_grp[] = { FUNC("rgmii1", 0, 24, 12) };
  50. static struct rt2880_pmx_func refclk_grp[] = { FUNC("spi refclk", 0, 37, 3) };
  51. static struct rt2880_pmx_func ephy_grp[] = { FUNC("ephy", 0, 40, 5) };
  52. static struct rt2880_pmx_func rgmii2_grp[] = { FUNC("rgmii2", 0, 60, 12) };
  53. static struct rt2880_pmx_func wled_grp[] = { FUNC("wled", 0, 72, 1) };
  54. static struct rt2880_pmx_func pa_grp[] = { FUNC("pa", 0, 18, 4) };
  55. static struct rt2880_pmx_func uartf_grp[] = {
  56. FUNC("uartf", MT7620_GPIO_MODE_UARTF, 7, 8),
  57. FUNC("pcm uartf", MT7620_GPIO_MODE_PCM_UARTF, 7, 8),
  58. FUNC("pcm i2s", MT7620_GPIO_MODE_PCM_I2S, 7, 8),
  59. FUNC("i2s uartf", MT7620_GPIO_MODE_I2S_UARTF, 7, 8),
  60. FUNC("pcm gpio", MT7620_GPIO_MODE_PCM_GPIO, 11, 4),
  61. FUNC("gpio uartf", MT7620_GPIO_MODE_GPIO_UARTF, 7, 4),
  62. FUNC("gpio i2s", MT7620_GPIO_MODE_GPIO_I2S, 7, 4),
  63. };
  64. static struct rt2880_pmx_func wdt_grp[] = {
  65. FUNC("wdt rst", 0, 17, 1),
  66. FUNC("wdt refclk", 0, 17, 1),
  67. };
  68. static struct rt2880_pmx_func pcie_rst_grp[] = {
  69. FUNC("pcie rst", MT7620_GPIO_MODE_PCIE_RST, 36, 1),
  70. FUNC("pcie refclk", MT7620_GPIO_MODE_PCIE_REF, 36, 1)
  71. };
  72. static struct rt2880_pmx_func nd_sd_grp[] = {
  73. FUNC("nand", MT7620_GPIO_MODE_NAND, 45, 15),
  74. FUNC("sd", MT7620_GPIO_MODE_SD, 47, 13)
  75. };
  76. static struct rt2880_pmx_group mt7620a_pinmux_data[] = {
  77. GRP("i2c", i2c_grp, 1, MT7620_GPIO_MODE_I2C),
  78. GRP("uartf", uartf_grp, MT7620_GPIO_MODE_UART0_MASK,
  79. MT7620_GPIO_MODE_UART0_SHIFT),
  80. GRP("spi", spi_grp, 1, MT7620_GPIO_MODE_SPI),
  81. GRP("uartlite", uartlite_grp, 1, MT7620_GPIO_MODE_UART1),
  82. GRP_G("wdt", wdt_grp, MT7620_GPIO_MODE_WDT_MASK,
  83. MT7620_GPIO_MODE_WDT_GPIO, MT7620_GPIO_MODE_WDT_SHIFT),
  84. GRP("mdio", mdio_grp, 1, MT7620_GPIO_MODE_MDIO),
  85. GRP("rgmii1", rgmii1_grp, 1, MT7620_GPIO_MODE_RGMII1),
  86. GRP("spi refclk", refclk_grp, 1, MT7620_GPIO_MODE_SPI_REF_CLK),
  87. GRP_G("pcie", pcie_rst_grp, MT7620_GPIO_MODE_PCIE_MASK,
  88. MT7620_GPIO_MODE_PCIE_GPIO, MT7620_GPIO_MODE_PCIE_SHIFT),
  89. GRP_G("nd_sd", nd_sd_grp, MT7620_GPIO_MODE_ND_SD_MASK,
  90. MT7620_GPIO_MODE_ND_SD_GPIO, MT7620_GPIO_MODE_ND_SD_SHIFT),
  91. GRP("rgmii2", rgmii2_grp, 1, MT7620_GPIO_MODE_RGMII2),
  92. GRP("wled", wled_grp, 1, MT7620_GPIO_MODE_WLED),
  93. GRP("ephy", ephy_grp, 1, MT7620_GPIO_MODE_EPHY),
  94. GRP("pa", pa_grp, 1, MT7620_GPIO_MODE_PA),
  95. { 0 }
  96. };
  97. static struct rt2880_pmx_func pwm1_grp_mt7628[] = {
  98. FUNC("sdxc d6", 3, 19, 1),
  99. FUNC("utif", 2, 19, 1),
  100. FUNC("gpio", 1, 19, 1),
  101. FUNC("pwm1", 0, 19, 1),
  102. };
  103. static struct rt2880_pmx_func pwm0_grp_mt7628[] = {
  104. FUNC("sdxc d7", 3, 18, 1),
  105. FUNC("utif", 2, 18, 1),
  106. FUNC("gpio", 1, 18, 1),
  107. FUNC("pwm0", 0, 18, 1),
  108. };
  109. static struct rt2880_pmx_func uart2_grp_mt7628[] = {
  110. FUNC("sdxc d5 d4", 3, 20, 2),
  111. FUNC("pwm", 2, 20, 2),
  112. FUNC("gpio", 1, 20, 2),
  113. FUNC("uart2", 0, 20, 2),
  114. };
  115. static struct rt2880_pmx_func uart1_grp_mt7628[] = {
  116. FUNC("sw_r", 3, 45, 2),
  117. FUNC("pwm", 2, 45, 2),
  118. FUNC("gpio", 1, 45, 2),
  119. FUNC("uart1", 0, 45, 2),
  120. };
  121. static struct rt2880_pmx_func i2c_grp_mt7628[] = {
  122. FUNC("-", 3, 4, 2),
  123. FUNC("debug", 2, 4, 2),
  124. FUNC("gpio", 1, 4, 2),
  125. FUNC("i2c", 0, 4, 2),
  126. };
  127. static struct rt2880_pmx_func refclk_grp_mt7628[] = { FUNC("refclk", 0, 37, 1) };
  128. static struct rt2880_pmx_func perst_grp_mt7628[] = { FUNC("perst", 0, 36, 1) };
  129. static struct rt2880_pmx_func wdt_grp_mt7628[] = { FUNC("wdt", 0, 38, 1) };
  130. static struct rt2880_pmx_func spi_grp_mt7628[] = { FUNC("spi", 0, 7, 4) };
  131. static struct rt2880_pmx_func sd_mode_grp_mt7628[] = {
  132. FUNC("jtag", 3, 22, 8),
  133. FUNC("utif", 2, 22, 8),
  134. FUNC("gpio", 1, 22, 8),
  135. FUNC("sdxc", 0, 22, 8),
  136. };
  137. static struct rt2880_pmx_func uart0_grp_mt7628[] = {
  138. FUNC("-", 3, 12, 2),
  139. FUNC("-", 2, 12, 2),
  140. FUNC("gpio", 1, 12, 2),
  141. FUNC("uart0", 0, 12, 2),
  142. };
  143. static struct rt2880_pmx_func i2s_grp_mt7628[] = {
  144. FUNC("antenna", 3, 0, 4),
  145. FUNC("pcm", 2, 0, 4),
  146. FUNC("gpio", 1, 0, 4),
  147. FUNC("i2s", 0, 0, 4),
  148. };
  149. static struct rt2880_pmx_func spi_cs1_grp_mt7628[] = {
  150. FUNC("-", 3, 6, 1),
  151. FUNC("refclk", 2, 6, 1),
  152. FUNC("gpio", 1, 6, 1),
  153. FUNC("spi cs1", 0, 6, 1),
  154. };
  155. static struct rt2880_pmx_func spis_grp_mt7628[] = {
  156. FUNC("pwm", 3, 14, 4),
  157. FUNC("util", 2, 14, 4),
  158. FUNC("gpio", 1, 14, 4),
  159. FUNC("spis", 0, 14, 4),
  160. };
  161. static struct rt2880_pmx_func gpio_grp_mt7628[] = {
  162. FUNC("pcie", 3, 11, 1),
  163. FUNC("refclk", 2, 11, 1),
  164. FUNC("gpio", 1, 11, 1),
  165. FUNC("gpio", 0, 11, 1),
  166. };
  167. static struct rt2880_pmx_func wled_kn_grp_mt7628[] = {
  168. FUNC("rsvd", 3, 35, 1),
  169. FUNC("rsvd", 2, 35, 1),
  170. FUNC("gpio", 1, 35, 1),
  171. FUNC("wled_kn", 0, 35, 1),
  172. };
  173. static struct rt2880_pmx_func wled_an_grp_mt7628[] = {
  174. FUNC("rsvd", 3, 44, 1),
  175. FUNC("rsvd", 2, 44, 1),
  176. FUNC("gpio", 1, 44, 1),
  177. FUNC("wled_an", 0, 44, 1),
  178. };
  179. #define MT7628_GPIO_MODE_MASK 0x3
  180. #define MT7628_GPIO_MODE_WLED_KN 48
  181. #define MT7628_GPIO_MODE_WLED_AN 32
  182. #define MT7628_GPIO_MODE_PWM1 30
  183. #define MT7628_GPIO_MODE_PWM0 28
  184. #define MT7628_GPIO_MODE_UART2 26
  185. #define MT7628_GPIO_MODE_UART1 24
  186. #define MT7628_GPIO_MODE_I2C 20
  187. #define MT7628_GPIO_MODE_REFCLK 18
  188. #define MT7628_GPIO_MODE_PERST 16
  189. #define MT7628_GPIO_MODE_WDT 14
  190. #define MT7628_GPIO_MODE_SPI 12
  191. #define MT7628_GPIO_MODE_SDMODE 10
  192. #define MT7628_GPIO_MODE_UART0 8
  193. #define MT7628_GPIO_MODE_I2S 6
  194. #define MT7628_GPIO_MODE_CS1 4
  195. #define MT7628_GPIO_MODE_SPIS 2
  196. #define MT7628_GPIO_MODE_GPIO 0
  197. static struct rt2880_pmx_group mt7628an_pinmux_data[] = {
  198. GRP_G("pwm1", pwm1_grp_mt7628, MT7628_GPIO_MODE_MASK,
  199. 1, MT7628_GPIO_MODE_PWM1),
  200. GRP_G("pwm0", pwm0_grp_mt7628, MT7628_GPIO_MODE_MASK,
  201. 1, MT7628_GPIO_MODE_PWM0),
  202. GRP_G("uart2", uart2_grp_mt7628, MT7628_GPIO_MODE_MASK,
  203. 1, MT7628_GPIO_MODE_UART2),
  204. GRP_G("uart1", uart1_grp_mt7628, MT7628_GPIO_MODE_MASK,
  205. 1, MT7628_GPIO_MODE_UART1),
  206. GRP_G("i2c", i2c_grp_mt7628, MT7628_GPIO_MODE_MASK,
  207. 1, MT7628_GPIO_MODE_I2C),
  208. GRP("refclk", refclk_grp_mt7628, 1, MT7628_GPIO_MODE_REFCLK),
  209. GRP("perst", perst_grp_mt7628, 1, MT7628_GPIO_MODE_PERST),
  210. GRP("wdt", wdt_grp_mt7628, 1, MT7628_GPIO_MODE_WDT),
  211. GRP("spi", spi_grp_mt7628, 1, MT7628_GPIO_MODE_SPI),
  212. GRP_G("sdmode", sd_mode_grp_mt7628, MT7628_GPIO_MODE_MASK,
  213. 1, MT7628_GPIO_MODE_SDMODE),
  214. GRP_G("uart0", uart0_grp_mt7628, MT7628_GPIO_MODE_MASK,
  215. 1, MT7628_GPIO_MODE_UART0),
  216. GRP_G("i2s", i2s_grp_mt7628, MT7628_GPIO_MODE_MASK,
  217. 1, MT7628_GPIO_MODE_I2S),
  218. GRP_G("spi cs1", spi_cs1_grp_mt7628, MT7628_GPIO_MODE_MASK,
  219. 1, MT7628_GPIO_MODE_CS1),
  220. GRP_G("spis", spis_grp_mt7628, MT7628_GPIO_MODE_MASK,
  221. 1, MT7628_GPIO_MODE_SPIS),
  222. GRP_G("gpio", gpio_grp_mt7628, MT7628_GPIO_MODE_MASK,
  223. 1, MT7628_GPIO_MODE_GPIO),
  224. GRP_G("wled_an", wled_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
  225. 1, MT7628_GPIO_MODE_WLED_AN),
  226. GRP_G("wled_kn", wled_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
  227. 1, MT7628_GPIO_MODE_WLED_KN),
  228. { 0 }
  229. };
  230. static inline int is_mt76x8(void)
  231. {
  232. return ralink_soc == MT762X_SOC_MT7628AN ||
  233. ralink_soc == MT762X_SOC_MT7688;
  234. }
  235. static __init u32
  236. mt7620_calc_rate(u32 ref_rate, u32 mul, u32 div)
  237. {
  238. u64 t;
  239. t = ref_rate;
  240. t *= mul;
  241. do_div(t, div);
  242. return t;
  243. }
  244. #define MHZ(x) ((x) * 1000 * 1000)
  245. static __init unsigned long
  246. mt7620_get_xtal_rate(void)
  247. {
  248. u32 reg;
  249. reg = rt_sysc_r32(SYSC_REG_SYSTEM_CONFIG0);
  250. if (reg & SYSCFG0_XTAL_FREQ_SEL)
  251. return MHZ(40);
  252. return MHZ(20);
  253. }
  254. static __init unsigned long
  255. mt7620_get_periph_rate(unsigned long xtal_rate)
  256. {
  257. u32 reg;
  258. reg = rt_sysc_r32(SYSC_REG_CLKCFG0);
  259. if (reg & CLKCFG0_PERI_CLK_SEL)
  260. return xtal_rate;
  261. return MHZ(40);
  262. }
  263. static const u32 mt7620_clk_divider[] __initconst = { 2, 3, 4, 8 };
  264. static __init unsigned long
  265. mt7620_get_cpu_pll_rate(unsigned long xtal_rate)
  266. {
  267. u32 reg;
  268. u32 mul;
  269. u32 div;
  270. reg = rt_sysc_r32(SYSC_REG_CPLL_CONFIG0);
  271. if (reg & CPLL_CFG0_BYPASS_REF_CLK)
  272. return xtal_rate;
  273. if ((reg & CPLL_CFG0_SW_CFG) == 0)
  274. return MHZ(600);
  275. mul = (reg >> CPLL_CFG0_PLL_MULT_RATIO_SHIFT) &
  276. CPLL_CFG0_PLL_MULT_RATIO_MASK;
  277. mul += 24;
  278. if (reg & CPLL_CFG0_LC_CURFCK)
  279. mul *= 2;
  280. div = (reg >> CPLL_CFG0_PLL_DIV_RATIO_SHIFT) &
  281. CPLL_CFG0_PLL_DIV_RATIO_MASK;
  282. WARN_ON(div >= ARRAY_SIZE(mt7620_clk_divider));
  283. return mt7620_calc_rate(xtal_rate, mul, mt7620_clk_divider[div]);
  284. }
  285. static __init unsigned long
  286. mt7620_get_pll_rate(unsigned long xtal_rate, unsigned long cpu_pll_rate)
  287. {
  288. u32 reg;
  289. reg = rt_sysc_r32(SYSC_REG_CPLL_CONFIG1);
  290. if (reg & CPLL_CFG1_CPU_AUX1)
  291. return xtal_rate;
  292. if (reg & CPLL_CFG1_CPU_AUX0)
  293. return MHZ(480);
  294. return cpu_pll_rate;
  295. }
  296. static __init unsigned long
  297. mt7620_get_cpu_rate(unsigned long pll_rate)
  298. {
  299. u32 reg;
  300. u32 mul;
  301. u32 div;
  302. reg = rt_sysc_r32(SYSC_REG_CPU_SYS_CLKCFG);
  303. mul = reg & CPU_SYS_CLKCFG_CPU_FFRAC_MASK;
  304. div = (reg >> CPU_SYS_CLKCFG_CPU_FDIV_SHIFT) &
  305. CPU_SYS_CLKCFG_CPU_FDIV_MASK;
  306. return mt7620_calc_rate(pll_rate, mul, div);
  307. }
  308. static const u32 mt7620_ocp_dividers[16] __initconst = {
  309. [CPU_SYS_CLKCFG_OCP_RATIO_2] = 2,
  310. [CPU_SYS_CLKCFG_OCP_RATIO_3] = 3,
  311. [CPU_SYS_CLKCFG_OCP_RATIO_4] = 4,
  312. [CPU_SYS_CLKCFG_OCP_RATIO_5] = 5,
  313. [CPU_SYS_CLKCFG_OCP_RATIO_10] = 10,
  314. };
  315. static __init unsigned long
  316. mt7620_get_dram_rate(unsigned long pll_rate)
  317. {
  318. if (dram_type == SYSCFG0_DRAM_TYPE_SDRAM)
  319. return pll_rate / 4;
  320. return pll_rate / 3;
  321. }
  322. static __init unsigned long
  323. mt7620_get_sys_rate(unsigned long cpu_rate)
  324. {
  325. u32 reg;
  326. u32 ocp_ratio;
  327. u32 div;
  328. reg = rt_sysc_r32(SYSC_REG_CPU_SYS_CLKCFG);
  329. ocp_ratio = (reg >> CPU_SYS_CLKCFG_OCP_RATIO_SHIFT) &
  330. CPU_SYS_CLKCFG_OCP_RATIO_MASK;
  331. if (WARN_ON(ocp_ratio >= ARRAY_SIZE(mt7620_ocp_dividers)))
  332. return cpu_rate;
  333. div = mt7620_ocp_dividers[ocp_ratio];
  334. if (WARN(!div, "invalid divider for OCP ratio %u", ocp_ratio))
  335. return cpu_rate;
  336. return cpu_rate / div;
  337. }
  338. void __init ralink_clk_init(void)
  339. {
  340. unsigned long xtal_rate;
  341. unsigned long cpu_pll_rate;
  342. unsigned long pll_rate;
  343. unsigned long cpu_rate;
  344. unsigned long sys_rate;
  345. unsigned long dram_rate;
  346. unsigned long periph_rate;
  347. xtal_rate = mt7620_get_xtal_rate();
  348. #define RFMT(label) label ":%lu.%03luMHz "
  349. #define RINT(x) ((x) / 1000000)
  350. #define RFRAC(x) (((x) / 1000) % 1000)
  351. if (is_mt76x8()) {
  352. if (xtal_rate == MHZ(40))
  353. cpu_rate = MHZ(580);
  354. else
  355. cpu_rate = MHZ(575);
  356. dram_rate = sys_rate = cpu_rate / 3;
  357. periph_rate = MHZ(40);
  358. ralink_clk_add("10000d00.uartlite", periph_rate);
  359. ralink_clk_add("10000e00.uartlite", periph_rate);
  360. } else {
  361. cpu_pll_rate = mt7620_get_cpu_pll_rate(xtal_rate);
  362. pll_rate = mt7620_get_pll_rate(xtal_rate, cpu_pll_rate);
  363. cpu_rate = mt7620_get_cpu_rate(pll_rate);
  364. dram_rate = mt7620_get_dram_rate(pll_rate);
  365. sys_rate = mt7620_get_sys_rate(cpu_rate);
  366. periph_rate = mt7620_get_periph_rate(xtal_rate);
  367. pr_debug(RFMT("XTAL") RFMT("CPU_PLL") RFMT("PLL"),
  368. RINT(xtal_rate), RFRAC(xtal_rate),
  369. RINT(cpu_pll_rate), RFRAC(cpu_pll_rate),
  370. RINT(pll_rate), RFRAC(pll_rate));
  371. ralink_clk_add("10000500.uart", periph_rate);
  372. }
  373. pr_debug(RFMT("CPU") RFMT("DRAM") RFMT("SYS") RFMT("PERIPH"),
  374. RINT(cpu_rate), RFRAC(cpu_rate),
  375. RINT(dram_rate), RFRAC(dram_rate),
  376. RINT(sys_rate), RFRAC(sys_rate),
  377. RINT(periph_rate), RFRAC(periph_rate));
  378. #undef RFRAC
  379. #undef RINT
  380. #undef RFMT
  381. ralink_clk_add("cpu", cpu_rate);
  382. ralink_clk_add("10000100.timer", periph_rate);
  383. ralink_clk_add("10000120.watchdog", periph_rate);
  384. ralink_clk_add("10000b00.spi", sys_rate);
  385. ralink_clk_add("10000c00.uartlite", periph_rate);
  386. ralink_clk_add("10180000.wmac", xtal_rate);
  387. if (IS_ENABLED(CONFIG_USB) && !is_mt76x8()) {
  388. /*
  389. * When the CPU goes into sleep mode, the BUS clock will be
  390. * too low for USB to function properly. Adjust the busses
  391. * fractional divider to fix this
  392. */
  393. u32 val = rt_sysc_r32(SYSC_REG_CPU_SYS_CLKCFG);
  394. val &= ~(CLKCFG_FDIV_MASK | CLKCFG_FFRAC_MASK);
  395. val |= CLKCFG_FDIV_USB_VAL | CLKCFG_FFRAC_USB_VAL;
  396. rt_sysc_w32(val, SYSC_REG_CPU_SYS_CLKCFG);
  397. }
  398. }
  399. void __init ralink_of_remap(void)
  400. {
  401. rt_sysc_membase = plat_of_remap_node("ralink,mt7620a-sysc");
  402. rt_memc_membase = plat_of_remap_node("ralink,mt7620a-memc");
  403. if (!rt_sysc_membase || !rt_memc_membase)
  404. panic("Failed to remap core resources");
  405. }
  406. static __init void
  407. mt7620_dram_init(struct ralink_soc_info *soc_info)
  408. {
  409. switch (dram_type) {
  410. case SYSCFG0_DRAM_TYPE_SDRAM:
  411. pr_info("Board has SDRAM\n");
  412. soc_info->mem_size_min = MT7620_SDRAM_SIZE_MIN;
  413. soc_info->mem_size_max = MT7620_SDRAM_SIZE_MAX;
  414. break;
  415. case SYSCFG0_DRAM_TYPE_DDR1:
  416. pr_info("Board has DDR1\n");
  417. soc_info->mem_size_min = MT7620_DDR1_SIZE_MIN;
  418. soc_info->mem_size_max = MT7620_DDR1_SIZE_MAX;
  419. break;
  420. case SYSCFG0_DRAM_TYPE_DDR2:
  421. pr_info("Board has DDR2\n");
  422. soc_info->mem_size_min = MT7620_DDR2_SIZE_MIN;
  423. soc_info->mem_size_max = MT7620_DDR2_SIZE_MAX;
  424. break;
  425. default:
  426. BUG();
  427. }
  428. }
  429. static __init void
  430. mt7628_dram_init(struct ralink_soc_info *soc_info)
  431. {
  432. switch (dram_type) {
  433. case SYSCFG0_DRAM_TYPE_DDR1_MT7628:
  434. pr_info("Board has DDR1\n");
  435. soc_info->mem_size_min = MT7620_DDR1_SIZE_MIN;
  436. soc_info->mem_size_max = MT7620_DDR1_SIZE_MAX;
  437. break;
  438. case SYSCFG0_DRAM_TYPE_DDR2_MT7628:
  439. pr_info("Board has DDR2\n");
  440. soc_info->mem_size_min = MT7620_DDR2_SIZE_MIN;
  441. soc_info->mem_size_max = MT7620_DDR2_SIZE_MAX;
  442. break;
  443. default:
  444. BUG();
  445. }
  446. }
  447. void prom_soc_init(struct ralink_soc_info *soc_info)
  448. {
  449. void __iomem *sysc = (void __iomem *) KSEG1ADDR(MT7620_SYSC_BASE);
  450. unsigned char *name = NULL;
  451. u32 n0;
  452. u32 n1;
  453. u32 rev;
  454. u32 cfg0;
  455. u32 pmu0;
  456. u32 pmu1;
  457. u32 bga;
  458. n0 = __raw_readl(sysc + SYSC_REG_CHIP_NAME0);
  459. n1 = __raw_readl(sysc + SYSC_REG_CHIP_NAME1);
  460. rev = __raw_readl(sysc + SYSC_REG_CHIP_REV);
  461. bga = (rev >> CHIP_REV_PKG_SHIFT) & CHIP_REV_PKG_MASK;
  462. if (n0 == MT7620_CHIP_NAME0 && n1 == MT7620_CHIP_NAME1) {
  463. if (bga) {
  464. ralink_soc = MT762X_SOC_MT7620A;
  465. name = "MT7620A";
  466. soc_info->compatible = "ralink,mt7620a-soc";
  467. } else {
  468. ralink_soc = MT762X_SOC_MT7620N;
  469. name = "MT7620N";
  470. soc_info->compatible = "ralink,mt7620n-soc";
  471. }
  472. } else if (n0 == MT7620_CHIP_NAME0 && n1 == MT7628_CHIP_NAME1) {
  473. u32 efuse = __raw_readl(sysc + SYSC_REG_EFUSE_CFG);
  474. if (efuse & EFUSE_MT7688) {
  475. ralink_soc = MT762X_SOC_MT7688;
  476. name = "MT7688";
  477. } else {
  478. ralink_soc = MT762X_SOC_MT7628AN;
  479. name = "MT7628AN";
  480. }
  481. soc_info->compatible = "ralink,mt7628an-soc";
  482. } else {
  483. panic("mt762x: unknown SoC, n0:%08x n1:%08x\n", n0, n1);
  484. }
  485. snprintf(soc_info->sys_type, RAMIPS_SYS_TYPE_LEN,
  486. "Ralink %s ver:%u eco:%u",
  487. name,
  488. (rev >> CHIP_REV_VER_SHIFT) & CHIP_REV_VER_MASK,
  489. (rev & CHIP_REV_ECO_MASK));
  490. cfg0 = __raw_readl(sysc + SYSC_REG_SYSTEM_CONFIG0);
  491. if (is_mt76x8())
  492. dram_type = cfg0 & DRAM_TYPE_MT7628_MASK;
  493. else
  494. dram_type = (cfg0 >> SYSCFG0_DRAM_TYPE_SHIFT) &
  495. SYSCFG0_DRAM_TYPE_MASK;
  496. soc_info->mem_base = MT7620_DRAM_BASE;
  497. if (is_mt76x8())
  498. mt7628_dram_init(soc_info);
  499. else
  500. mt7620_dram_init(soc_info);
  501. pmu0 = __raw_readl(sysc + PMU0_CFG);
  502. pmu1 = __raw_readl(sysc + PMU1_CFG);
  503. pr_info("Analog PMU set to %s control\n",
  504. (pmu0 & PMU_SW_SET) ? ("sw") : ("hw"));
  505. pr_info("Digital PMU set to %s control\n",
  506. (pmu1 & DIG_SW_SEL) ? ("sw") : ("hw"));
  507. if (is_mt76x8())
  508. rt2880_pinmux_data = mt7628an_pinmux_data;
  509. else
  510. rt2880_pinmux_data = mt7620a_pinmux_data;
  511. }