rt305x.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/io.h>
  16. #include <asm/mipsregs.h>
  17. #include <asm/mach-ralink/ralink_regs.h>
  18. #include <asm/mach-ralink/rt305x.h>
  19. #include "common.h"
  20. static struct ralink_soc_info *soc_info_ptr;
  21. static unsigned long rt5350_get_mem_size(void)
  22. {
  23. unsigned long ret;
  24. u32 t;
  25. t = __raw_readl(RT305X_SYSC_BASE + SYSC_REG_SYSTEM_CONFIG);
  26. t = (t >> RT5350_SYSCFG0_DRAM_SIZE_SHIFT) &
  27. RT5350_SYSCFG0_DRAM_SIZE_MASK;
  28. switch (t) {
  29. case RT5350_SYSCFG0_DRAM_SIZE_2M:
  30. ret = 2;
  31. break;
  32. case RT5350_SYSCFG0_DRAM_SIZE_8M:
  33. ret = 8;
  34. break;
  35. case RT5350_SYSCFG0_DRAM_SIZE_16M:
  36. ret = 16;
  37. break;
  38. case RT5350_SYSCFG0_DRAM_SIZE_32M:
  39. ret = 32;
  40. break;
  41. case RT5350_SYSCFG0_DRAM_SIZE_64M:
  42. ret = 64;
  43. break;
  44. default:
  45. panic("rt5350: invalid DRAM size: %u", t);
  46. break;
  47. }
  48. return ret;
  49. }
  50. static unsigned int __init rt305x_get_soc_name0(void)
  51. {
  52. return __raw_readl(RT305X_SYSC_BASE + SYSC_REG_CHIP_NAME0);
  53. }
  54. static unsigned int __init rt305x_get_soc_name1(void)
  55. {
  56. return __raw_readl(RT305X_SYSC_BASE + SYSC_REG_CHIP_NAME1);
  57. }
  58. static bool __init rt3052_soc_valid(void)
  59. {
  60. if (rt305x_get_soc_name0() == RT3052_CHIP_NAME0 &&
  61. rt305x_get_soc_name1() == RT3052_CHIP_NAME1)
  62. return true;
  63. else
  64. return false;
  65. }
  66. static bool __init rt3350_soc_valid(void)
  67. {
  68. if (rt305x_get_soc_name0() == RT3350_CHIP_NAME0 &&
  69. rt305x_get_soc_name1() == RT3350_CHIP_NAME1)
  70. return true;
  71. else
  72. return false;
  73. }
  74. static bool __init rt3352_soc_valid(void)
  75. {
  76. if (rt305x_get_soc_name0() == RT3352_CHIP_NAME0 &&
  77. rt305x_get_soc_name1() == RT3352_CHIP_NAME1)
  78. return true;
  79. else
  80. return false;
  81. }
  82. static bool __init rt5350_soc_valid(void)
  83. {
  84. if (rt305x_get_soc_name0() == RT5350_CHIP_NAME0 &&
  85. rt305x_get_soc_name1() == RT5350_CHIP_NAME1)
  86. return true;
  87. else
  88. return false;
  89. }
  90. static const char __init *rt305x_get_soc_name(struct ralink_soc_info *soc_info)
  91. {
  92. if (rt3052_soc_valid()) {
  93. unsigned long icache_sets;
  94. icache_sets = (read_c0_config1() >> 22) & 7;
  95. if (icache_sets == 1) {
  96. ralink_soc = RT305X_SOC_RT3050;
  97. soc_info->compatible = "ralink,rt3050-soc";
  98. return "RT3050";
  99. } else {
  100. ralink_soc = RT305X_SOC_RT3052;
  101. soc_info->compatible = "ralink,rt3052-soc";
  102. return "RT3052";
  103. }
  104. } else if (rt3350_soc_valid()) {
  105. ralink_soc = RT305X_SOC_RT3350;
  106. soc_info->compatible = "ralink,rt3350-soc";
  107. return "RT3350";
  108. } else if (rt3352_soc_valid()) {
  109. ralink_soc = RT305X_SOC_RT3352;
  110. soc_info->compatible = "ralink,rt3352-soc";
  111. return "RT3352";
  112. } else if (rt5350_soc_valid()) {
  113. ralink_soc = RT305X_SOC_RT5350;
  114. soc_info->compatible = "ralink,rt5350-soc";
  115. return "RT5350";
  116. } else {
  117. panic("rt305x: unknown SoC, n0:%08x n1:%08x",
  118. rt305x_get_soc_name0(), rt305x_get_soc_name1());
  119. }
  120. }
  121. static unsigned int __init rt305x_get_soc_id(void)
  122. {
  123. return __raw_readl(RT305X_SYSC_BASE + SYSC_REG_CHIP_ID);
  124. }
  125. static unsigned int __init rt305x_get_soc_ver(void)
  126. {
  127. return (rt305x_get_soc_id() >> CHIP_ID_ID_SHIFT) & CHIP_ID_ID_MASK;
  128. }
  129. static unsigned int __init rt305x_get_soc_rev(void)
  130. {
  131. return (rt305x_get_soc_id() & CHIP_ID_REV_MASK);
  132. }
  133. static const char __init *rt305x_get_soc_id_name(void)
  134. {
  135. if (soc_is_rt3050())
  136. return "rt3050";
  137. else if (soc_is_rt3052())
  138. return "rt3052";
  139. else if (soc_is_rt3350())
  140. return "rt3350";
  141. else if (soc_is_rt3352())
  142. return "rt3352";
  143. else if (soc_is_rt5350())
  144. return "rt5350";
  145. else
  146. return "invalid";
  147. }
  148. static int __init rt305x_soc_dev_init(void)
  149. {
  150. struct soc_device *soc_dev;
  151. struct soc_device_attribute *soc_dev_attr;
  152. soc_dev_attr = kzalloc_obj(*soc_dev_attr);
  153. if (!soc_dev_attr)
  154. return -ENOMEM;
  155. soc_dev_attr->family = "Ralink";
  156. soc_dev_attr->soc_id = rt305x_get_soc_id_name();
  157. soc_dev_attr->data = soc_info_ptr;
  158. soc_dev = soc_device_register(soc_dev_attr);
  159. if (IS_ERR(soc_dev)) {
  160. kfree(soc_dev_attr);
  161. return PTR_ERR(soc_dev);
  162. }
  163. return 0;
  164. }
  165. device_initcall(rt305x_soc_dev_init);
  166. void __init prom_soc_init(struct ralink_soc_info *soc_info)
  167. {
  168. const char *name = rt305x_get_soc_name(soc_info);
  169. snprintf(soc_info->sys_type, RAMIPS_SYS_TYPE_LEN,
  170. "Ralink %s id:%u rev:%u",
  171. name,
  172. rt305x_get_soc_ver(),
  173. rt305x_get_soc_rev());
  174. soc_info->mem_base = RT305X_SDRAM_BASE;
  175. if (soc_is_rt5350()) {
  176. soc_info->mem_size = rt5350_get_mem_size();
  177. } else if (soc_is_rt305x() || soc_is_rt3350()) {
  178. soc_info->mem_size_min = RT305X_MEM_SIZE_MIN;
  179. soc_info->mem_size_max = RT305X_MEM_SIZE_MAX;
  180. } else if (soc_is_rt3352()) {
  181. soc_info->mem_size_min = RT3352_MEM_SIZE_MIN;
  182. soc_info->mem_size_max = RT3352_MEM_SIZE_MAX;
  183. }
  184. soc_info_ptr = soc_info;
  185. }