1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2293840b9SJohn Crispin /*
3293840b9SJohn Crispin *
4293840b9SJohn Crispin * Parts of this file are based on Ralink's 2.6.21 BSP
5293840b9SJohn Crispin *
6293840b9SJohn Crispin * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7293840b9SJohn Crispin * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
897b92108SJohn Crispin * Copyright (C) 2013 John Crispin <john@phrozen.org>
9293840b9SJohn Crispin */
10293840b9SJohn Crispin
11293840b9SJohn Crispin #include <linux/kernel.h>
12293840b9SJohn Crispin #include <linux/init.h>
13*2165248fSSergio Paracuellos #include <linux/slab.h>
14*2165248fSSergio Paracuellos #include <linux/sys_soc.h>
15293840b9SJohn Crispin
16293840b9SJohn Crispin #include <asm/mipsregs.h>
17293840b9SJohn Crispin #include <asm/mach-ralink/ralink_regs.h>
18293840b9SJohn Crispin #include <asm/mach-ralink/rt3883.h>
19293840b9SJohn Crispin
20293840b9SJohn Crispin #include "common.h"
21293840b9SJohn Crispin
22*2165248fSSergio Paracuellos static struct ralink_soc_info *soc_info_ptr;
23*2165248fSSergio Paracuellos
rt3883_get_soc_name0(void)24293840b9SJohn Crispin static unsigned int __init rt3883_get_soc_name0(void)
25293840b9SJohn Crispin {
26293840b9SJohn Crispin return __raw_readl(RT3883_SYSC_BASE + RT3883_SYSC_REG_CHIPID0_3);
27293840b9SJohn Crispin }
28293840b9SJohn Crispin
rt3883_get_soc_name1(void)29293840b9SJohn Crispin static unsigned int __init rt3883_get_soc_name1(void)
30293840b9SJohn Crispin {
31293840b9SJohn Crispin return __raw_readl(RT3883_SYSC_BASE + RT3883_SYSC_REG_CHIPID4_7);
32293840b9SJohn Crispin }
33293840b9SJohn Crispin
rt3883_soc_valid(void)34293840b9SJohn Crispin static bool __init rt3883_soc_valid(void)
35293840b9SJohn Crispin {
36293840b9SJohn Crispin if (rt3883_get_soc_name0() == RT3883_CHIP_NAME0 &&
37293840b9SJohn Crispin rt3883_get_soc_name1() == RT3883_CHIP_NAME1)
38293840b9SJohn Crispin return true;
39293840b9SJohn Crispin else
40293840b9SJohn Crispin return false;
41293840b9SJohn Crispin }
42293840b9SJohn Crispin
rt3883_get_soc_name(void)43293840b9SJohn Crispin static const char __init *rt3883_get_soc_name(void)
44293840b9SJohn Crispin {
45293840b9SJohn Crispin if (rt3883_soc_valid())
46293840b9SJohn Crispin return "RT3883";
47293840b9SJohn Crispin else
48293840b9SJohn Crispin return "invalid";
49293840b9SJohn Crispin }
50293840b9SJohn Crispin
rt3883_get_soc_id(void)51293840b9SJohn Crispin static unsigned int __init rt3883_get_soc_id(void)
52293840b9SJohn Crispin {
53293840b9SJohn Crispin return __raw_readl(RT3883_SYSC_BASE + RT3883_SYSC_REG_REVID);
54293840b9SJohn Crispin }
55293840b9SJohn Crispin
rt3883_get_soc_ver(void)56293840b9SJohn Crispin static unsigned int __init rt3883_get_soc_ver(void)
57293840b9SJohn Crispin {
58293840b9SJohn Crispin return (rt3883_get_soc_id() >> RT3883_REVID_VER_ID_SHIFT) & RT3883_REVID_VER_ID_MASK;
592517caf1SJohn Crispin }
602517caf1SJohn Crispin
rt3883_get_soc_rev(void)61293840b9SJohn Crispin static unsigned int __init rt3883_get_soc_rev(void)
623b2e7c7cSJohn Crispin {
63293840b9SJohn Crispin return (rt3883_get_soc_id() & RT3883_REVID_ECO_ID_MASK);
64293840b9SJohn Crispin }
651e209c96SJohn Crispin
rt3883_soc_dev_init(void)66293840b9SJohn Crispin static int __init rt3883_soc_dev_init(void)
67293840b9SJohn Crispin {
68293840b9SJohn Crispin struct soc_device *soc_dev;
69293840b9SJohn Crispin struct soc_device_attribute *soc_dev_attr;
70293840b9SJohn Crispin
71293840b9SJohn Crispin soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
72293840b9SJohn Crispin if (!soc_dev_attr)
73293840b9SJohn Crispin return -ENOMEM;
74293840b9SJohn Crispin
75293840b9SJohn Crispin soc_dev_attr->family = "Ralink";
76293840b9SJohn Crispin soc_dev_attr->soc_id = rt3883_get_soc_name();
7789f9b304SSergio Paracuellos
7889f9b304SSergio Paracuellos soc_dev_attr->data = soc_info_ptr;
7989f9b304SSergio Paracuellos
8089f9b304SSergio Paracuellos soc_dev = soc_device_register(soc_dev_attr);
8189f9b304SSergio Paracuellos if (IS_ERR(soc_dev)) {
8289f9b304SSergio Paracuellos kfree(soc_dev_attr);
8389f9b304SSergio Paracuellos return PTR_ERR(soc_dev);
8489f9b304SSergio Paracuellos }
8589f9b304SSergio Paracuellos
8689f9b304SSergio Paracuellos return 0;
8789f9b304SSergio Paracuellos }
8889f9b304SSergio Paracuellos device_initcall(rt3883_soc_dev_init);
8989f9b304SSergio Paracuellos
prom_soc_init(struct ralink_soc_info * soc_info)9089f9b304SSergio Paracuellos void __init prom_soc_init(struct ralink_soc_info *soc_info)
9189f9b304SSergio Paracuellos {
9289f9b304SSergio Paracuellos if (rt3883_soc_valid())
9389f9b304SSergio Paracuellos soc_info->compatible = "ralink,rt3883-soc";
9489f9b304SSergio Paracuellos else
9589f9b304SSergio Paracuellos panic("rt3883: unknown SoC, n0:%08x n1:%08x",
9689f9b304SSergio Paracuellos rt3883_get_soc_name0(), rt3883_get_soc_name1());
9789f9b304SSergio Paracuellos
9889f9b304SSergio Paracuellos snprintf(soc_info->sys_type, RAMIPS_SYS_TYPE_LEN,
9989f9b304SSergio Paracuellos "Ralink %s ver:%u eco:%u",
10089f9b304SSergio Paracuellos rt3883_get_soc_name(),
10189f9b304SSergio Paracuellos rt3883_get_soc_ver(),
10289f9b304SSergio Paracuellos rt3883_get_soc_rev());
10389f9b304SSergio Paracuellos
10489f9b304SSergio Paracuellos soc_info->mem_base = RT3883_SDRAM_BASE;
10589f9b304SSergio Paracuellos soc_info->mem_size_min = RT3883_MEM_SIZE_MIN;
10689f9b304SSergio Paracuellos soc_info->mem_size_max = RT3883_MEM_SIZE_MAX;
10789f9b304SSergio Paracuellos
10889f9b304SSergio Paracuellos ralink_soc = RT3883_SOC;
10989f9b304SSergio Paracuellos soc_info_ptr = soc_info;
11089f9b304SSergio Paracuellos }
11189f9b304SSergio Paracuellos