mt7621.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2015 Nikolay Martynov <mar.kolya@gmail.com>
  5. * Copyright (C) 2015 John Crispin <john@phrozen.org>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/sys_soc.h>
  11. #include <linux/memblock.h>
  12. #include <linux/pci.h>
  13. #include <linux/bug.h>
  14. #include <asm/bootinfo.h>
  15. #include <asm/mipsregs.h>
  16. #include <asm/smp-ops.h>
  17. #include <asm/mips-cps.h>
  18. #include <asm/mach-ralink/ralink_regs.h>
  19. #include <asm/mach-ralink/mt7621.h>
  20. #include "common.h"
  21. #define MT7621_MEM_TEST_PATTERN 0xaa5555aa
  22. static u32 detect_magic __initdata;
  23. static struct ralink_soc_info *soc_info_ptr;
  24. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  25. {
  26. struct resource_entry *entry;
  27. resource_size_t mask;
  28. entry = resource_list_first_type(&bridge->windows, IORESOURCE_MEM);
  29. if (!entry) {
  30. pr_err("Cannot get memory resource\n");
  31. return -EINVAL;
  32. }
  33. if (mips_cps_numiocu(0)) {
  34. /*
  35. * Hardware doesn't accept mask values with 1s after
  36. * 0s (e.g. 0xffef), so warn if that's happen
  37. */
  38. mask = ~(entry->res->end - entry->res->start) & CM_GCR_REGn_MASK_ADDRMASK;
  39. WARN_ON(mask && BIT(ffz(~mask)) - 1 != ~mask);
  40. write_gcr_reg1_base(entry->res->start);
  41. write_gcr_reg1_mask(mask | CM_GCR_REGn_MASK_CMTGT_IOCU0);
  42. pr_info("PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
  43. (unsigned long long)read_gcr_reg1_base(),
  44. (unsigned long long)read_gcr_reg1_mask());
  45. }
  46. return 0;
  47. }
  48. phys_addr_t mips_cpc_default_phys_base(void)
  49. {
  50. panic("Cannot detect cpc address");
  51. }
  52. static bool __init mt7621_addr_wraparound_test(phys_addr_t size)
  53. {
  54. void *dm = (void *)KSEG1ADDR(&detect_magic);
  55. if (CPHYSADDR(dm + size) >= MT7621_LOWMEM_MAX_SIZE)
  56. return true;
  57. __raw_writel(MT7621_MEM_TEST_PATTERN, dm);
  58. if (__raw_readl(dm) != __raw_readl(dm + size))
  59. return false;
  60. __raw_writel(~MT7621_MEM_TEST_PATTERN, dm);
  61. return __raw_readl(dm) == __raw_readl(dm + size);
  62. }
  63. static void __init mt7621_memory_detect(void)
  64. {
  65. phys_addr_t size;
  66. for (size = 32 * SZ_1M; size <= 256 * SZ_1M; size <<= 1) {
  67. if (mt7621_addr_wraparound_test(size)) {
  68. memblock_add(MT7621_LOWMEM_BASE, size);
  69. return;
  70. }
  71. }
  72. memblock_add(MT7621_LOWMEM_BASE, MT7621_LOWMEM_MAX_SIZE);
  73. memblock_add(MT7621_HIGHMEM_BASE, MT7621_HIGHMEM_SIZE);
  74. }
  75. static unsigned int __init mt7621_get_soc_name0(void)
  76. {
  77. return __raw_readl(MT7621_SYSC_BASE + SYSC_REG_CHIP_NAME0);
  78. }
  79. static unsigned int __init mt7621_get_soc_name1(void)
  80. {
  81. return __raw_readl(MT7621_SYSC_BASE + SYSC_REG_CHIP_NAME1);
  82. }
  83. static bool __init mt7621_soc_valid(void)
  84. {
  85. if (mt7621_get_soc_name0() == MT7621_CHIP_NAME0 &&
  86. mt7621_get_soc_name1() == MT7621_CHIP_NAME1)
  87. return true;
  88. else
  89. return false;
  90. }
  91. static const char __init *mt7621_get_soc_id(void)
  92. {
  93. if (mt7621_soc_valid())
  94. return "MT7621";
  95. else
  96. return "invalid";
  97. }
  98. static unsigned int __init mt7621_get_soc_rev(void)
  99. {
  100. return __raw_readl(MT7621_SYSC_BASE + SYSC_REG_CHIP_REV);
  101. }
  102. static unsigned int __init mt7621_get_soc_ver(void)
  103. {
  104. return (mt7621_get_soc_rev() >> CHIP_REV_VER_SHIFT) & CHIP_REV_VER_MASK;
  105. }
  106. static unsigned int __init mt7621_get_soc_eco(void)
  107. {
  108. return (mt7621_get_soc_rev() & CHIP_REV_ECO_MASK);
  109. }
  110. static const char __init *mt7621_get_soc_revision(void)
  111. {
  112. if (mt7621_get_soc_rev() == 1 && mt7621_get_soc_eco() == 1)
  113. return "E2";
  114. else
  115. return "E1";
  116. }
  117. static int __init mt7621_soc_dev_init(void)
  118. {
  119. struct soc_device *soc_dev;
  120. struct soc_device_attribute *soc_dev_attr;
  121. soc_dev_attr = kzalloc_obj(*soc_dev_attr);
  122. if (!soc_dev_attr)
  123. return -ENOMEM;
  124. soc_dev_attr->soc_id = "mt7621";
  125. soc_dev_attr->family = "Ralink";
  126. soc_dev_attr->revision = mt7621_get_soc_revision();
  127. soc_dev_attr->data = soc_info_ptr;
  128. soc_dev = soc_device_register(soc_dev_attr);
  129. if (IS_ERR(soc_dev)) {
  130. kfree(soc_dev_attr);
  131. return PTR_ERR(soc_dev);
  132. }
  133. return 0;
  134. }
  135. device_initcall(mt7621_soc_dev_init);
  136. void __init prom_soc_init(struct ralink_soc_info *soc_info)
  137. {
  138. /* Early detection of CMP support */
  139. mips_cm_probe();
  140. mips_cpc_probe();
  141. if (mips_cps_numiocu(0)) {
  142. /*
  143. * mips_cm_probe() wipes out bootloader
  144. * config for CM regions and we have to configure them
  145. * again. This SoC cannot talk to pamlbus devices
  146. * without proper iocu region set up.
  147. *
  148. * FIXME: it would be better to do this with values
  149. * from DT, but we need this very early because
  150. * without this we cannot talk to pretty much anything
  151. * including serial.
  152. */
  153. write_gcr_reg0_base(MT7621_PALMBUS_BASE);
  154. write_gcr_reg0_mask(~MT7621_PALMBUS_SIZE |
  155. CM_GCR_REGn_MASK_CMTGT_IOCU0);
  156. __sync();
  157. }
  158. if (mt7621_soc_valid())
  159. soc_info->compatible = "mediatek,mt7621-soc";
  160. else
  161. panic("mt7621: unknown SoC, n0:%08x n1:%08x\n",
  162. mt7621_get_soc_name0(),
  163. mt7621_get_soc_name1());
  164. ralink_soc = MT762X_SOC_MT7621AT;
  165. snprintf(soc_info->sys_type, RAMIPS_SYS_TYPE_LEN,
  166. "MediaTek %s ver:%u eco:%u",
  167. mt7621_get_soc_id(),
  168. mt7621_get_soc_ver(),
  169. mt7621_get_soc_eco());
  170. soc_info->mem_detect = mt7621_memory_detect;
  171. soc_info_ptr = soc_info;
  172. if (!register_cps_smp_ops())
  173. return;
  174. if (!register_vsmp_smp_ops())
  175. return;
  176. }