mt7620.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Parts of this file are based on Ralink's 2.6.21 BSP
  5. *
  6. * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
  7. * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
  8. * Copyright (C) 2013 John Crispin <john@phrozen.org>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/bug.h>
  13. #include <linux/slab.h>
  14. #include <linux/sys_soc.h>
  15. #include <asm/mipsregs.h>
  16. #include <asm/mach-ralink/ralink_regs.h>
  17. #include <asm/mach-ralink/mt7620.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. /* EFUSE bits */
  34. #define EFUSE_MT7688 0x100000
  35. /* DRAM type bit */
  36. #define DRAM_TYPE_MT7628_MASK 0x1
  37. /* does the board have sdram or ddram */
  38. static int dram_type;
  39. static struct ralink_soc_info *soc_info_ptr;
  40. static __init void
  41. mt7620_dram_init(struct ralink_soc_info *soc_info)
  42. {
  43. switch (dram_type) {
  44. case SYSCFG0_DRAM_TYPE_SDRAM:
  45. pr_info("Board has SDRAM\n");
  46. soc_info->mem_size_min = MT7620_SDRAM_SIZE_MIN;
  47. soc_info->mem_size_max = MT7620_SDRAM_SIZE_MAX;
  48. break;
  49. case SYSCFG0_DRAM_TYPE_DDR1:
  50. pr_info("Board has DDR1\n");
  51. soc_info->mem_size_min = MT7620_DDR1_SIZE_MIN;
  52. soc_info->mem_size_max = MT7620_DDR1_SIZE_MAX;
  53. break;
  54. case SYSCFG0_DRAM_TYPE_DDR2:
  55. pr_info("Board has DDR2\n");
  56. soc_info->mem_size_min = MT7620_DDR2_SIZE_MIN;
  57. soc_info->mem_size_max = MT7620_DDR2_SIZE_MAX;
  58. break;
  59. default:
  60. BUG();
  61. }
  62. }
  63. static __init void
  64. mt7628_dram_init(struct ralink_soc_info *soc_info)
  65. {
  66. switch (dram_type) {
  67. case SYSCFG0_DRAM_TYPE_DDR1_MT7628:
  68. pr_info("Board has DDR1\n");
  69. soc_info->mem_size_min = MT7620_DDR1_SIZE_MIN;
  70. soc_info->mem_size_max = MT7620_DDR1_SIZE_MAX;
  71. break;
  72. case SYSCFG0_DRAM_TYPE_DDR2_MT7628:
  73. pr_info("Board has DDR2\n");
  74. soc_info->mem_size_min = MT7620_DDR2_SIZE_MIN;
  75. soc_info->mem_size_max = MT7620_DDR2_SIZE_MAX;
  76. break;
  77. default:
  78. BUG();
  79. }
  80. }
  81. static unsigned int __init mt7620_get_soc_name0(void)
  82. {
  83. return __raw_readl(MT7620_SYSC_BASE + SYSC_REG_CHIP_NAME0);
  84. }
  85. static unsigned int __init mt7620_get_soc_name1(void)
  86. {
  87. return __raw_readl(MT7620_SYSC_BASE + SYSC_REG_CHIP_NAME1);
  88. }
  89. static bool __init mt7620_soc_valid(void)
  90. {
  91. if (mt7620_get_soc_name0() == MT7620_CHIP_NAME0 &&
  92. mt7620_get_soc_name1() == MT7620_CHIP_NAME1)
  93. return true;
  94. else
  95. return false;
  96. }
  97. static bool __init mt7628_soc_valid(void)
  98. {
  99. if (mt7620_get_soc_name0() == MT7620_CHIP_NAME0 &&
  100. mt7620_get_soc_name1() == MT7628_CHIP_NAME1)
  101. return true;
  102. else
  103. return false;
  104. }
  105. static unsigned int __init mt7620_get_rev(void)
  106. {
  107. return __raw_readl(MT7620_SYSC_BASE + SYSC_REG_CHIP_REV);
  108. }
  109. static unsigned int __init mt7620_get_bga(void)
  110. {
  111. return (mt7620_get_rev() >> CHIP_REV_PKG_SHIFT) & CHIP_REV_PKG_MASK;
  112. }
  113. static unsigned int __init mt7620_get_efuse(void)
  114. {
  115. return __raw_readl(MT7620_SYSC_BASE + SYSC_REG_EFUSE_CFG);
  116. }
  117. static unsigned int __init mt7620_get_soc_ver(void)
  118. {
  119. return (mt7620_get_rev() >> CHIP_REV_VER_SHIFT) & CHIP_REV_VER_MASK;
  120. }
  121. static unsigned int __init mt7620_get_soc_eco(void)
  122. {
  123. return (mt7620_get_rev() & CHIP_REV_ECO_MASK);
  124. }
  125. static const char __init *mt7620_get_soc_name(struct ralink_soc_info *soc_info)
  126. {
  127. if (mt7620_soc_valid()) {
  128. u32 bga = mt7620_get_bga();
  129. if (bga) {
  130. ralink_soc = MT762X_SOC_MT7620A;
  131. soc_info->compatible = "ralink,mt7620a-soc";
  132. return "MT7620A";
  133. } else {
  134. ralink_soc = MT762X_SOC_MT7620N;
  135. soc_info->compatible = "ralink,mt7620n-soc";
  136. return "MT7620N";
  137. }
  138. } else if (mt7628_soc_valid()) {
  139. u32 efuse = mt7620_get_efuse();
  140. unsigned char *name = NULL;
  141. if (efuse & EFUSE_MT7688) {
  142. ralink_soc = MT762X_SOC_MT7688;
  143. name = "MT7688";
  144. } else {
  145. ralink_soc = MT762X_SOC_MT7628AN;
  146. name = "MT7628AN";
  147. }
  148. soc_info->compatible = "ralink,mt7628an-soc";
  149. return name;
  150. } else {
  151. panic("mt762x: unknown SoC, n0:%08x n1:%08x\n",
  152. mt7620_get_soc_name0(), mt7620_get_soc_name1());
  153. }
  154. }
  155. static const char __init *mt7620_get_soc_id_name(void)
  156. {
  157. if (ralink_soc == MT762X_SOC_MT7620A)
  158. return "mt7620a";
  159. else if (ralink_soc == MT762X_SOC_MT7620N)
  160. return "mt7620n";
  161. else if (ralink_soc == MT762X_SOC_MT7688)
  162. return "mt7688";
  163. else if (ralink_soc == MT762X_SOC_MT7628AN)
  164. return "mt7628n";
  165. else
  166. return "invalid";
  167. }
  168. static int __init mt7620_soc_dev_init(void)
  169. {
  170. struct soc_device *soc_dev;
  171. struct soc_device_attribute *soc_dev_attr;
  172. soc_dev_attr = kzalloc_obj(*soc_dev_attr);
  173. if (!soc_dev_attr)
  174. return -ENOMEM;
  175. soc_dev_attr->family = "Ralink";
  176. soc_dev_attr->soc_id = mt7620_get_soc_id_name();
  177. soc_dev_attr->data = soc_info_ptr;
  178. soc_dev = soc_device_register(soc_dev_attr);
  179. if (IS_ERR(soc_dev)) {
  180. kfree(soc_dev_attr);
  181. return PTR_ERR(soc_dev);
  182. }
  183. return 0;
  184. }
  185. device_initcall(mt7620_soc_dev_init);
  186. void __init prom_soc_init(struct ralink_soc_info *soc_info)
  187. {
  188. const char *name = mt7620_get_soc_name(soc_info);
  189. u32 cfg0;
  190. u32 pmu0;
  191. u32 pmu1;
  192. snprintf(soc_info->sys_type, RAMIPS_SYS_TYPE_LEN,
  193. "MediaTek %s ver:%u eco:%u",
  194. name, mt7620_get_soc_ver(), mt7620_get_soc_eco());
  195. cfg0 = __raw_readl(MT7620_SYSC_BASE + SYSC_REG_SYSTEM_CONFIG0);
  196. if (is_mt76x8()) {
  197. dram_type = cfg0 & DRAM_TYPE_MT7628_MASK;
  198. } else {
  199. dram_type = (cfg0 >> SYSCFG0_DRAM_TYPE_SHIFT) &
  200. SYSCFG0_DRAM_TYPE_MASK;
  201. if (dram_type == SYSCFG0_DRAM_TYPE_UNKNOWN)
  202. dram_type = SYSCFG0_DRAM_TYPE_SDRAM;
  203. }
  204. soc_info->mem_base = MT7620_DRAM_BASE;
  205. if (is_mt76x8())
  206. mt7628_dram_init(soc_info);
  207. else
  208. mt7620_dram_init(soc_info);
  209. pmu0 = __raw_readl(MT7620_SYSC_BASE + PMU0_CFG);
  210. pmu1 = __raw_readl(MT7620_SYSC_BASE + PMU1_CFG);
  211. pr_info("Analog PMU set to %s control\n",
  212. (pmu0 & PMU_SW_SET) ? ("sw") : ("hw"));
  213. pr_info("Digital PMU set to %s control\n",
  214. (pmu1 & DIG_SW_SEL) ? ("sw") : ("hw"));
  215. soc_info_ptr = soc_info;
  216. }