mt7620.c 20 KB

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