1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2015 Nikolay Martynov <mar.kolya@gmail.com>
5 * Copyright (C) 2015 John Crispin <john@phrozen.org>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/sys_soc.h>
12 #include <linux/memblock.h>
13 #include <linux/pci.h>
14 #include <linux/bug.h>
16 #include <asm/bootinfo.h>
17 #include <asm/mipsregs.h>
18 #include <asm/smp-ops.h>
19 #include <asm/mips-cps.h>
20 #include <asm/mach-ralink/ralink_regs.h>
21 #include <asm/mach-ralink/mt7621.h>
25 #define MT7621_MEM_TEST_PATTERN 0xaa5555aa
27 static u32 detect_magic __initdata;
29 int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
31 struct resource_entry *entry;
34 entry = resource_list_first_type(&bridge->windows, IORESOURCE_MEM);
36 pr_err("Cannot get memory resource\n");
40 if (mips_cps_numiocu(0)) {
42 * Hardware doesn't accept mask values with 1s after
43 * 0s (e.g. 0xffef), so warn if that's happen
45 mask = ~(entry->res->end - entry->res->start) & CM_GCR_REGn_MASK_ADDRMASK;
46 WARN_ON(mask && BIT(ffz(~mask)) - 1 != ~mask);
48 write_gcr_reg1_base(entry->res->start);
49 write_gcr_reg1_mask(mask | CM_GCR_REGn_MASK_CMTGT_IOCU0);
50 pr_info("PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
51 (unsigned long long)read_gcr_reg1_base(),
52 (unsigned long long)read_gcr_reg1_mask());
58 phys_addr_t mips_cpc_default_phys_base(void)
60 panic("Cannot detect cpc address");
63 static bool __init mt7621_addr_wraparound_test(phys_addr_t size)
65 void *dm = (void *)KSEG1ADDR(&detect_magic);
67 if (CPHYSADDR(dm + size) >= MT7621_LOWMEM_MAX_SIZE)
69 __raw_writel(MT7621_MEM_TEST_PATTERN, dm);
70 if (__raw_readl(dm) != __raw_readl(dm + size))
72 __raw_writel(~MT7621_MEM_TEST_PATTERN, dm);
73 return __raw_readl(dm) == __raw_readl(dm + size);
76 static void __init mt7621_memory_detect(void)
80 for (size = 32 * SZ_1M; size <= 256 * SZ_1M; size <<= 1) {
81 if (mt7621_addr_wraparound_test(size)) {
82 memblock_add(MT7621_LOWMEM_BASE, size);
87 memblock_add(MT7621_LOWMEM_BASE, MT7621_LOWMEM_MAX_SIZE);
88 memblock_add(MT7621_HIGHMEM_BASE, MT7621_HIGHMEM_SIZE);
91 void __init ralink_of_remap(void)
93 rt_sysc_membase = plat_of_remap_node("mediatek,mt7621-sysc");
94 rt_memc_membase = plat_of_remap_node("mediatek,mt7621-memc");
96 if (!rt_sysc_membase || !rt_memc_membase)
97 panic("Failed to remap core resources");
100 static unsigned int __init mt7621_get_soc_name0(void)
102 return __raw_readl(MT7621_SYSC_BASE + SYSC_REG_CHIP_NAME0);
105 static unsigned int __init mt7621_get_soc_name1(void)
107 return __raw_readl(MT7621_SYSC_BASE + SYSC_REG_CHIP_NAME1);
110 static bool __init mt7621_soc_valid(void)
112 if (mt7621_get_soc_name0() == MT7621_CHIP_NAME0 &&
113 mt7621_get_soc_name1() == MT7621_CHIP_NAME1)
119 static const char __init *mt7621_get_soc_id(void)
121 if (mt7621_soc_valid())
127 static unsigned int __init mt7621_get_soc_rev(void)
129 return __raw_readl(MT7621_SYSC_BASE + SYSC_REG_CHIP_REV);
132 static unsigned int __init mt7621_get_soc_ver(void)
134 return (mt7621_get_soc_rev() >> CHIP_REV_VER_SHIFT) & CHIP_REV_VER_MASK;
137 static unsigned int __init mt7621_get_soc_eco(void)
139 return (mt7621_get_soc_rev() & CHIP_REV_ECO_MASK);
142 static const char __init *mt7621_get_soc_revision(void)
144 if (mt7621_get_soc_rev() == 1 && mt7621_get_soc_eco() == 1)
150 static void soc_dev_init(struct ralink_soc_info *soc_info)
152 struct soc_device *soc_dev;
153 struct soc_device_attribute *soc_dev_attr;
155 soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
159 soc_dev_attr->soc_id = "mt7621";
160 soc_dev_attr->family = "Ralink";
161 soc_dev_attr->revision = mt7621_get_soc_revision();
163 soc_dev_attr->data = soc_info;
165 soc_dev = soc_device_register(soc_dev_attr);
166 if (IS_ERR(soc_dev)) {
172 void __init prom_soc_init(struct ralink_soc_info *soc_info)
174 /* Early detection of CMP support */
178 if (mips_cps_numiocu(0)) {
180 * mips_cm_probe() wipes out bootloader
181 * config for CM regions and we have to configure them
182 * again. This SoC cannot talk to pamlbus devices
183 * witout proper iocu region set up.
185 * FIXME: it would be better to do this with values
186 * from DT, but we need this very early because
187 * without this we cannot talk to pretty much anything
190 write_gcr_reg0_base(MT7621_PALMBUS_BASE);
191 write_gcr_reg0_mask(~MT7621_PALMBUS_SIZE |
192 CM_GCR_REGn_MASK_CMTGT_IOCU0);
196 if (mt7621_soc_valid())
197 soc_info->compatible = "mediatek,mt7621-soc";
199 panic("mt7621: unknown SoC, n0:%08x n1:%08x\n",
200 mt7621_get_soc_name0(),
201 mt7621_get_soc_name1());
202 ralink_soc = MT762X_SOC_MT7621AT;
204 snprintf(soc_info->sys_type, RAMIPS_SYS_TYPE_LEN,
205 "MediaTek %s ver:%u eco:%u",
207 mt7621_get_soc_ver(),
208 mt7621_get_soc_eco());
210 soc_info->mem_detect = mt7621_memory_detect;
212 soc_dev_init(soc_info);
214 if (!register_cps_smp_ops())
216 if (!register_cmp_smp_ops())
218 if (!register_vsmp_smp_ops())