mt7620.c 20 KB

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