mt7620.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 <john@phrozen.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, 45, 15)
  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_uart2", 3, 14, 4),
  157. FUNC("utif", 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 p4led_kn_grp_mt7628[] = {
  168. FUNC("jtag", 3, 30, 1),
  169. FUNC("utif", 2, 30, 1),
  170. FUNC("gpio", 1, 30, 1),
  171. FUNC("p4led_kn", 0, 30, 1),
  172. };
  173. static struct rt2880_pmx_func p3led_kn_grp_mt7628[] = {
  174. FUNC("jtag", 3, 31, 1),
  175. FUNC("utif", 2, 31, 1),
  176. FUNC("gpio", 1, 31, 1),
  177. FUNC("p3led_kn", 0, 31, 1),
  178. };
  179. static struct rt2880_pmx_func p2led_kn_grp_mt7628[] = {
  180. FUNC("jtag", 3, 32, 1),
  181. FUNC("utif", 2, 32, 1),
  182. FUNC("gpio", 1, 32, 1),
  183. FUNC("p2led_kn", 0, 32, 1),
  184. };
  185. static struct rt2880_pmx_func p1led_kn_grp_mt7628[] = {
  186. FUNC("jtag", 3, 33, 1),
  187. FUNC("utif", 2, 33, 1),
  188. FUNC("gpio", 1, 33, 1),
  189. FUNC("p1led_kn", 0, 33, 1),
  190. };
  191. static struct rt2880_pmx_func p0led_kn_grp_mt7628[] = {
  192. FUNC("jtag", 3, 34, 1),
  193. FUNC("rsvd", 2, 34, 1),
  194. FUNC("gpio", 1, 34, 1),
  195. FUNC("p0led_kn", 0, 34, 1),
  196. };
  197. static struct rt2880_pmx_func wled_kn_grp_mt7628[] = {
  198. FUNC("rsvd", 3, 35, 1),
  199. FUNC("rsvd", 2, 35, 1),
  200. FUNC("gpio", 1, 35, 1),
  201. FUNC("wled_kn", 0, 35, 1),
  202. };
  203. static struct rt2880_pmx_func p4led_an_grp_mt7628[] = {
  204. FUNC("jtag", 3, 39, 1),
  205. FUNC("utif", 2, 39, 1),
  206. FUNC("gpio", 1, 39, 1),
  207. FUNC("p4led_an", 0, 39, 1),
  208. };
  209. static struct rt2880_pmx_func p3led_an_grp_mt7628[] = {
  210. FUNC("jtag", 3, 40, 1),
  211. FUNC("utif", 2, 40, 1),
  212. FUNC("gpio", 1, 40, 1),
  213. FUNC("p3led_an", 0, 40, 1),
  214. };
  215. static struct rt2880_pmx_func p2led_an_grp_mt7628[] = {
  216. FUNC("jtag", 3, 41, 1),
  217. FUNC("utif", 2, 41, 1),
  218. FUNC("gpio", 1, 41, 1),
  219. FUNC("p2led_an", 0, 41, 1),
  220. };
  221. static struct rt2880_pmx_func p1led_an_grp_mt7628[] = {
  222. FUNC("jtag", 3, 42, 1),
  223. FUNC("utif", 2, 42, 1),
  224. FUNC("gpio", 1, 42, 1),
  225. FUNC("p1led_an", 0, 42, 1),
  226. };
  227. static struct rt2880_pmx_func p0led_an_grp_mt7628[] = {
  228. FUNC("jtag", 3, 43, 1),
  229. FUNC("rsvd", 2, 43, 1),
  230. FUNC("gpio", 1, 43, 1),
  231. FUNC("p0led_an", 0, 43, 1),
  232. };
  233. static struct rt2880_pmx_func wled_an_grp_mt7628[] = {
  234. FUNC("rsvd", 3, 44, 1),
  235. FUNC("rsvd", 2, 44, 1),
  236. FUNC("gpio", 1, 44, 1),
  237. FUNC("wled_an", 0, 44, 1),
  238. };
  239. #define MT7628_GPIO_MODE_MASK 0x3
  240. #define MT7628_GPIO_MODE_P4LED_KN 58
  241. #define MT7628_GPIO_MODE_P3LED_KN 56
  242. #define MT7628_GPIO_MODE_P2LED_KN 54
  243. #define MT7628_GPIO_MODE_P1LED_KN 52
  244. #define MT7628_GPIO_MODE_P0LED_KN 50
  245. #define MT7628_GPIO_MODE_WLED_KN 48
  246. #define MT7628_GPIO_MODE_P4LED_AN 42
  247. #define MT7628_GPIO_MODE_P3LED_AN 40
  248. #define MT7628_GPIO_MODE_P2LED_AN 38
  249. #define MT7628_GPIO_MODE_P1LED_AN 36
  250. #define MT7628_GPIO_MODE_P0LED_AN 34
  251. #define MT7628_GPIO_MODE_WLED_AN 32
  252. #define MT7628_GPIO_MODE_PWM1 30
  253. #define MT7628_GPIO_MODE_PWM0 28
  254. #define MT7628_GPIO_MODE_UART2 26
  255. #define MT7628_GPIO_MODE_UART1 24
  256. #define MT7628_GPIO_MODE_I2C 20
  257. #define MT7628_GPIO_MODE_REFCLK 18
  258. #define MT7628_GPIO_MODE_PERST 16
  259. #define MT7628_GPIO_MODE_WDT 14
  260. #define MT7628_GPIO_MODE_SPI 12
  261. #define MT7628_GPIO_MODE_SDMODE 10
  262. #define MT7628_GPIO_MODE_UART0 8
  263. #define MT7628_GPIO_MODE_I2S 6
  264. #define MT7628_GPIO_MODE_CS1 4
  265. #define MT7628_GPIO_MODE_SPIS 2
  266. #define MT7628_GPIO_MODE_GPIO 0
  267. static struct rt2880_pmx_group mt7628an_pinmux_data[] = {
  268. GRP_G("pwm1", pwm1_grp_mt7628, MT7628_GPIO_MODE_MASK,
  269. 1, MT7628_GPIO_MODE_PWM1),
  270. GRP_G("pwm0", pwm0_grp_mt7628, MT7628_GPIO_MODE_MASK,
  271. 1, MT7628_GPIO_MODE_PWM0),
  272. GRP_G("uart2", uart2_grp_mt7628, MT7628_GPIO_MODE_MASK,
  273. 1, MT7628_GPIO_MODE_UART2),
  274. GRP_G("uart1", uart1_grp_mt7628, MT7628_GPIO_MODE_MASK,
  275. 1, MT7628_GPIO_MODE_UART1),
  276. GRP_G("i2c", i2c_grp_mt7628, MT7628_GPIO_MODE_MASK,
  277. 1, MT7628_GPIO_MODE_I2C),
  278. GRP("refclk", refclk_grp_mt7628, 1, MT7628_GPIO_MODE_REFCLK),
  279. GRP("perst", perst_grp_mt7628, 1, MT7628_GPIO_MODE_PERST),
  280. GRP("wdt", wdt_grp_mt7628, 1, MT7628_GPIO_MODE_WDT),
  281. GRP("spi", spi_grp_mt7628, 1, MT7628_GPIO_MODE_SPI),
  282. GRP_G("sdmode", sd_mode_grp_mt7628, MT7628_GPIO_MODE_MASK,
  283. 1, MT7628_GPIO_MODE_SDMODE),
  284. GRP_G("uart0", uart0_grp_mt7628, MT7628_GPIO_MODE_MASK,
  285. 1, MT7628_GPIO_MODE_UART0),
  286. GRP_G("i2s", i2s_grp_mt7628, MT7628_GPIO_MODE_MASK,
  287. 1, MT7628_GPIO_MODE_I2S),
  288. GRP_G("spi cs1", spi_cs1_grp_mt7628, MT7628_GPIO_MODE_MASK,
  289. 1, MT7628_GPIO_MODE_CS1),
  290. GRP_G("spis", spis_grp_mt7628, MT7628_GPIO_MODE_MASK,
  291. 1, MT7628_GPIO_MODE_SPIS),
  292. GRP_G("gpio", gpio_grp_mt7628, MT7628_GPIO_MODE_MASK,
  293. 1, MT7628_GPIO_MODE_GPIO),
  294. GRP_G("wled_an", wled_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
  295. 1, MT7628_GPIO_MODE_WLED_AN),
  296. GRP_G("p0led_an", p0led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
  297. 1, MT7628_GPIO_MODE_P0LED_AN),
  298. GRP_G("p1led_an", p1led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
  299. 1, MT7628_GPIO_MODE_P1LED_AN),
  300. GRP_G("p2led_an", p2led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
  301. 1, MT7628_GPIO_MODE_P2LED_AN),
  302. GRP_G("p3led_an", p3led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
  303. 1, MT7628_GPIO_MODE_P3LED_AN),
  304. GRP_G("p4led_an", p4led_an_grp_mt7628, MT7628_GPIO_MODE_MASK,
  305. 1, MT7628_GPIO_MODE_P4LED_AN),
  306. GRP_G("wled_kn", wled_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
  307. 1, MT7628_GPIO_MODE_WLED_KN),
  308. GRP_G("p0led_kn", p0led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
  309. 1, MT7628_GPIO_MODE_P0LED_KN),
  310. GRP_G("p1led_kn", p1led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
  311. 1, MT7628_GPIO_MODE_P1LED_KN),
  312. GRP_G("p2led_kn", p2led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
  313. 1, MT7628_GPIO_MODE_P2LED_KN),
  314. GRP_G("p3led_kn", p3led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
  315. 1, MT7628_GPIO_MODE_P3LED_KN),
  316. GRP_G("p4led_kn", p4led_kn_grp_mt7628, MT7628_GPIO_MODE_MASK,
  317. 1, MT7628_GPIO_MODE_P4LED_KN),
  318. { 0 }
  319. };
  320. static inline int is_mt76x8(void)
  321. {
  322. return ralink_soc == MT762X_SOC_MT7628AN ||
  323. ralink_soc == MT762X_SOC_MT7688;
  324. }
  325. static __init u32
  326. mt7620_calc_rate(u32 ref_rate, u32 mul, u32 div)
  327. {
  328. u64 t;
  329. t = ref_rate;
  330. t *= mul;
  331. do_div(t, div);
  332. return t;
  333. }
  334. #define MHZ(x) ((x) * 1000 * 1000)
  335. static __init unsigned long
  336. mt7620_get_xtal_rate(void)
  337. {
  338. u32 reg;
  339. reg = rt_sysc_r32(SYSC_REG_SYSTEM_CONFIG0);
  340. if (reg & SYSCFG0_XTAL_FREQ_SEL)
  341. return MHZ(40);
  342. return MHZ(20);
  343. }
  344. static __init unsigned long
  345. mt7620_get_periph_rate(unsigned long xtal_rate)
  346. {
  347. u32 reg;
  348. reg = rt_sysc_r32(SYSC_REG_CLKCFG0);
  349. if (reg & CLKCFG0_PERI_CLK_SEL)
  350. return xtal_rate;
  351. return MHZ(40);
  352. }
  353. static const u32 mt7620_clk_divider[] __initconst = { 2, 3, 4, 8 };
  354. static __init unsigned long
  355. mt7620_get_cpu_pll_rate(unsigned long xtal_rate)
  356. {
  357. u32 reg;
  358. u32 mul;
  359. u32 div;
  360. reg = rt_sysc_r32(SYSC_REG_CPLL_CONFIG0);
  361. if (reg & CPLL_CFG0_BYPASS_REF_CLK)
  362. return xtal_rate;
  363. if ((reg & CPLL_CFG0_SW_CFG) == 0)
  364. return MHZ(600);
  365. mul = (reg >> CPLL_CFG0_PLL_MULT_RATIO_SHIFT) &
  366. CPLL_CFG0_PLL_MULT_RATIO_MASK;
  367. mul += 24;
  368. if (reg & CPLL_CFG0_LC_CURFCK)
  369. mul *= 2;
  370. div = (reg >> CPLL_CFG0_PLL_DIV_RATIO_SHIFT) &
  371. CPLL_CFG0_PLL_DIV_RATIO_MASK;
  372. WARN_ON(div >= ARRAY_SIZE(mt7620_clk_divider));
  373. return mt7620_calc_rate(xtal_rate, mul, mt7620_clk_divider[div]);
  374. }
  375. static __init unsigned long
  376. mt7620_get_pll_rate(unsigned long xtal_rate, unsigned long cpu_pll_rate)
  377. {
  378. u32 reg;
  379. reg = rt_sysc_r32(SYSC_REG_CPLL_CONFIG1);
  380. if (reg & CPLL_CFG1_CPU_AUX1)
  381. return xtal_rate;
  382. if (reg & CPLL_CFG1_CPU_AUX0)
  383. return MHZ(480);
  384. return cpu_pll_rate;
  385. }
  386. static __init unsigned long
  387. mt7620_get_cpu_rate(unsigned long pll_rate)
  388. {
  389. u32 reg;
  390. u32 mul;
  391. u32 div;
  392. reg = rt_sysc_r32(SYSC_REG_CPU_SYS_CLKCFG);
  393. mul = reg & CPU_SYS_CLKCFG_CPU_FFRAC_MASK;
  394. div = (reg >> CPU_SYS_CLKCFG_CPU_FDIV_SHIFT) &
  395. CPU_SYS_CLKCFG_CPU_FDIV_MASK;
  396. return mt7620_calc_rate(pll_rate, mul, div);
  397. }
  398. static const u32 mt7620_ocp_dividers[16] __initconst = {
  399. [CPU_SYS_CLKCFG_OCP_RATIO_2] = 2,
  400. [CPU_SYS_CLKCFG_OCP_RATIO_3] = 3,
  401. [CPU_SYS_CLKCFG_OCP_RATIO_4] = 4,
  402. [CPU_SYS_CLKCFG_OCP_RATIO_5] = 5,
  403. [CPU_SYS_CLKCFG_OCP_RATIO_10] = 10,
  404. };
  405. static __init unsigned long
  406. mt7620_get_dram_rate(unsigned long pll_rate)
  407. {
  408. if (dram_type == SYSCFG0_DRAM_TYPE_SDRAM)
  409. return pll_rate / 4;
  410. return pll_rate / 3;
  411. }
  412. static __init unsigned long
  413. mt7620_get_sys_rate(unsigned long cpu_rate)
  414. {
  415. u32 reg;
  416. u32 ocp_ratio;
  417. u32 div;
  418. reg = rt_sysc_r32(SYSC_REG_CPU_SYS_CLKCFG);
  419. ocp_ratio = (reg >> CPU_SYS_CLKCFG_OCP_RATIO_SHIFT) &
  420. CPU_SYS_CLKCFG_OCP_RATIO_MASK;
  421. if (WARN_ON(ocp_ratio >= ARRAY_SIZE(mt7620_ocp_dividers)))
  422. return cpu_rate;
  423. div = mt7620_ocp_dividers[ocp_ratio];
  424. if (WARN(!div, "invalid divider for OCP ratio %u", ocp_ratio))
  425. return cpu_rate;
  426. return cpu_rate / div;
  427. }
  428. void __init ralink_clk_init(void)
  429. {
  430. unsigned long xtal_rate;
  431. unsigned long cpu_pll_rate;
  432. unsigned long pll_rate;
  433. unsigned long cpu_rate;
  434. unsigned long sys_rate;
  435. unsigned long dram_rate;
  436. unsigned long periph_rate;
  437. xtal_rate = mt7620_get_xtal_rate();
  438. #define RFMT(label) label ":%lu.%03luMHz "
  439. #define RINT(x) ((x) / 1000000)
  440. #define RFRAC(x) (((x) / 1000) % 1000)
  441. if (is_mt76x8()) {
  442. if (xtal_rate == MHZ(40))
  443. cpu_rate = MHZ(580);
  444. else
  445. cpu_rate = MHZ(575);
  446. dram_rate = sys_rate = cpu_rate / 3;
  447. periph_rate = MHZ(40);
  448. ralink_clk_add("10000d00.uartlite", periph_rate);
  449. ralink_clk_add("10000e00.uartlite", periph_rate);
  450. } else {
  451. cpu_pll_rate = mt7620_get_cpu_pll_rate(xtal_rate);
  452. pll_rate = mt7620_get_pll_rate(xtal_rate, cpu_pll_rate);
  453. cpu_rate = mt7620_get_cpu_rate(pll_rate);
  454. dram_rate = mt7620_get_dram_rate(pll_rate);
  455. sys_rate = mt7620_get_sys_rate(cpu_rate);
  456. periph_rate = mt7620_get_periph_rate(xtal_rate);
  457. pr_debug(RFMT("XTAL") RFMT("CPU_PLL") RFMT("PLL"),
  458. RINT(xtal_rate), RFRAC(xtal_rate),
  459. RINT(cpu_pll_rate), RFRAC(cpu_pll_rate),
  460. RINT(pll_rate), RFRAC(pll_rate));
  461. ralink_clk_add("10000500.uart", periph_rate);
  462. }
  463. pr_debug(RFMT("CPU") RFMT("DRAM") RFMT("SYS") RFMT("PERIPH"),
  464. RINT(cpu_rate), RFRAC(cpu_rate),
  465. RINT(dram_rate), RFRAC(dram_rate),
  466. RINT(sys_rate), RFRAC(sys_rate),
  467. RINT(periph_rate), RFRAC(periph_rate));
  468. #undef RFRAC
  469. #undef RINT
  470. #undef RFMT
  471. ralink_clk_add("cpu", cpu_rate);
  472. ralink_clk_add("10000100.timer", periph_rate);
  473. ralink_clk_add("10000120.watchdog", periph_rate);
  474. ralink_clk_add("10000b00.spi", sys_rate);
  475. ralink_clk_add("10000b40.spi", sys_rate);
  476. ralink_clk_add("10000c00.uartlite", periph_rate);
  477. ralink_clk_add("10000d00.uart1", periph_rate);
  478. ralink_clk_add("10000e00.uart2", periph_rate);
  479. ralink_clk_add("10180000.wmac", xtal_rate);
  480. if (IS_ENABLED(CONFIG_USB) && !is_mt76x8()) {
  481. /*
  482. * When the CPU goes into sleep mode, the BUS clock will be
  483. * too low for USB to function properly. Adjust the busses
  484. * fractional divider to fix this
  485. */
  486. u32 val = rt_sysc_r32(SYSC_REG_CPU_SYS_CLKCFG);
  487. val &= ~(CLKCFG_FDIV_MASK | CLKCFG_FFRAC_MASK);
  488. val |= CLKCFG_FDIV_USB_VAL | CLKCFG_FFRAC_USB_VAL;
  489. rt_sysc_w32(val, SYSC_REG_CPU_SYS_CLKCFG);
  490. }
  491. }
  492. void __init ralink_of_remap(void)
  493. {
  494. rt_sysc_membase = plat_of_remap_node("ralink,mt7620a-sysc");
  495. rt_memc_membase = plat_of_remap_node("ralink,mt7620a-memc");
  496. if (!rt_sysc_membase || !rt_memc_membase)
  497. panic("Failed to remap core resources");
  498. }
  499. static __init void
  500. mt7620_dram_init(struct ralink_soc_info *soc_info)
  501. {
  502. switch (dram_type) {
  503. case SYSCFG0_DRAM_TYPE_SDRAM:
  504. pr_info("Board has SDRAM\n");
  505. soc_info->mem_size_min = MT7620_SDRAM_SIZE_MIN;
  506. soc_info->mem_size_max = MT7620_SDRAM_SIZE_MAX;
  507. break;
  508. case SYSCFG0_DRAM_TYPE_DDR1:
  509. pr_info("Board has DDR1\n");
  510. soc_info->mem_size_min = MT7620_DDR1_SIZE_MIN;
  511. soc_info->mem_size_max = MT7620_DDR1_SIZE_MAX;
  512. break;
  513. case SYSCFG0_DRAM_TYPE_DDR2:
  514. pr_info("Board has DDR2\n");
  515. soc_info->mem_size_min = MT7620_DDR2_SIZE_MIN;
  516. soc_info->mem_size_max = MT7620_DDR2_SIZE_MAX;
  517. break;
  518. default:
  519. BUG();
  520. }
  521. }
  522. static __init void
  523. mt7628_dram_init(struct ralink_soc_info *soc_info)
  524. {
  525. switch (dram_type) {
  526. case SYSCFG0_DRAM_TYPE_DDR1_MT7628:
  527. pr_info("Board has DDR1\n");
  528. soc_info->mem_size_min = MT7620_DDR1_SIZE_MIN;
  529. soc_info->mem_size_max = MT7620_DDR1_SIZE_MAX;
  530. break;
  531. case SYSCFG0_DRAM_TYPE_DDR2_MT7628:
  532. pr_info("Board has DDR2\n");
  533. soc_info->mem_size_min = MT7620_DDR2_SIZE_MIN;
  534. soc_info->mem_size_max = MT7620_DDR2_SIZE_MAX;
  535. break;
  536. default:
  537. BUG();
  538. }
  539. }
  540. void prom_soc_init(struct ralink_soc_info *soc_info)
  541. {
  542. void __iomem *sysc = (void __iomem *) KSEG1ADDR(MT7620_SYSC_BASE);
  543. unsigned char *name = NULL;
  544. u32 n0;
  545. u32 n1;
  546. u32 rev;
  547. u32 cfg0;
  548. u32 pmu0;
  549. u32 pmu1;
  550. u32 bga;
  551. n0 = __raw_readl(sysc + SYSC_REG_CHIP_NAME0);
  552. n1 = __raw_readl(sysc + SYSC_REG_CHIP_NAME1);
  553. rev = __raw_readl(sysc + SYSC_REG_CHIP_REV);
  554. bga = (rev >> CHIP_REV_PKG_SHIFT) & CHIP_REV_PKG_MASK;
  555. if (n0 == MT7620_CHIP_NAME0 && n1 == MT7620_CHIP_NAME1) {
  556. if (bga) {
  557. ralink_soc = MT762X_SOC_MT7620A;
  558. name = "MT7620A";
  559. soc_info->compatible = "ralink,mt7620a-soc";
  560. } else {
  561. ralink_soc = MT762X_SOC_MT7620N;
  562. name = "MT7620N";
  563. soc_info->compatible = "ralink,mt7620n-soc";
  564. }
  565. } else if (n0 == MT7620_CHIP_NAME0 && n1 == MT7628_CHIP_NAME1) {
  566. u32 efuse = __raw_readl(sysc + SYSC_REG_EFUSE_CFG);
  567. if (efuse & EFUSE_MT7688) {
  568. ralink_soc = MT762X_SOC_MT7688;
  569. name = "MT7688";
  570. } else {
  571. ralink_soc = MT762X_SOC_MT7628AN;
  572. name = "MT7628AN";
  573. }
  574. soc_info->compatible = "ralink,mt7628an-soc";
  575. } else {
  576. panic("mt762x: unknown SoC, n0:%08x n1:%08x\n", n0, n1);
  577. }
  578. snprintf(soc_info->sys_type, RAMIPS_SYS_TYPE_LEN,
  579. "MediaTek %s ver:%u eco:%u",
  580. name,
  581. (rev >> CHIP_REV_VER_SHIFT) & CHIP_REV_VER_MASK,
  582. (rev & CHIP_REV_ECO_MASK));
  583. cfg0 = __raw_readl(sysc + SYSC_REG_SYSTEM_CONFIG0);
  584. if (is_mt76x8()) {
  585. dram_type = cfg0 & DRAM_TYPE_MT7628_MASK;
  586. } else {
  587. dram_type = (cfg0 >> SYSCFG0_DRAM_TYPE_SHIFT) &
  588. SYSCFG0_DRAM_TYPE_MASK;
  589. if (dram_type == SYSCFG0_DRAM_TYPE_UNKNOWN)
  590. dram_type = SYSCFG0_DRAM_TYPE_SDRAM;
  591. }
  592. soc_info->mem_base = MT7620_DRAM_BASE;
  593. if (is_mt76x8())
  594. mt7628_dram_init(soc_info);
  595. else
  596. mt7620_dram_init(soc_info);
  597. pmu0 = __raw_readl(sysc + PMU0_CFG);
  598. pmu1 = __raw_readl(sysc + PMU1_CFG);
  599. pr_info("Analog PMU set to %s control\n",
  600. (pmu0 & PMU_SW_SET) ? ("sw") : ("hw"));
  601. pr_info("Digital PMU set to %s control\n",
  602. (pmu1 & DIG_SW_SEL) ? ("sw") : ("hw"));
  603. if (is_mt76x8())
  604. rt2880_pinmux_data = mt7628an_pinmux_data;
  605. else
  606. rt2880_pinmux_data = mt7620a_pinmux_data;
  607. }