1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * ID and revision information for mvebu SoCs
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2014 Marvell
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Gregory CLEMENT <gregory.clement@free-electrons.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * This file is licensed under the terms of the GNU General Public
9*4882a593Smuzhiyun * License version 2. This program is licensed "as is" without any
10*4882a593Smuzhiyun * warranty of any kind, whether express or implied.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * All the mvebu SoCs have information related to their variant and
13*4882a593Smuzhiyun * revision that can be read from the PCI control register. This is
14*4882a593Smuzhiyun * done before the PCI initialization to avoid any conflict. Once the
15*4882a593Smuzhiyun * ID and revision are retrieved, the mapping is freed.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define pr_fmt(fmt) "mvebu-soc-id: " fmt
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <linux/clk.h>
21*4882a593Smuzhiyun #include <linux/init.h>
22*4882a593Smuzhiyun #include <linux/io.h>
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/of.h>
25*4882a593Smuzhiyun #include <linux/of_address.h>
26*4882a593Smuzhiyun #include <linux/slab.h>
27*4882a593Smuzhiyun #include <linux/sys_soc.h>
28*4882a593Smuzhiyun #include "common.h"
29*4882a593Smuzhiyun #include "mvebu-soc-id.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define PCIE_DEV_ID_OFF 0x0
32*4882a593Smuzhiyun #define PCIE_DEV_REV_OFF 0x8
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define SOC_ID_MASK 0xFFFF0000
35*4882a593Smuzhiyun #define SOC_REV_MASK 0xFF
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun static u32 soc_dev_id;
38*4882a593Smuzhiyun static u32 soc_rev;
39*4882a593Smuzhiyun static bool is_id_valid;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun static const struct of_device_id mvebu_pcie_of_match_table[] = {
42*4882a593Smuzhiyun { .compatible = "marvell,armada-xp-pcie", },
43*4882a593Smuzhiyun { .compatible = "marvell,armada-370-pcie", },
44*4882a593Smuzhiyun { .compatible = "marvell,kirkwood-pcie" },
45*4882a593Smuzhiyun {},
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun
mvebu_get_soc_id(u32 * dev,u32 * rev)48*4882a593Smuzhiyun int mvebu_get_soc_id(u32 *dev, u32 *rev)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun if (is_id_valid) {
51*4882a593Smuzhiyun *dev = soc_dev_id;
52*4882a593Smuzhiyun *rev = soc_rev;
53*4882a593Smuzhiyun return 0;
54*4882a593Smuzhiyun } else
55*4882a593Smuzhiyun return -ENODEV;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
get_soc_id_by_pci(void)58*4882a593Smuzhiyun static int __init get_soc_id_by_pci(void)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun struct device_node *np;
61*4882a593Smuzhiyun int ret = 0;
62*4882a593Smuzhiyun void __iomem *pci_base;
63*4882a593Smuzhiyun struct clk *clk;
64*4882a593Smuzhiyun struct device_node *child;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun np = of_find_matching_node(NULL, mvebu_pcie_of_match_table);
67*4882a593Smuzhiyun if (!np)
68*4882a593Smuzhiyun return ret;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * ID and revision are available from any port, so we
72*4882a593Smuzhiyun * just pick the first one
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun child = of_get_next_child(np, NULL);
75*4882a593Smuzhiyun if (child == NULL) {
76*4882a593Smuzhiyun pr_err("cannot get pci node\n");
77*4882a593Smuzhiyun ret = -ENOMEM;
78*4882a593Smuzhiyun goto clk_err;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun clk = of_clk_get_by_name(child, NULL);
82*4882a593Smuzhiyun if (IS_ERR(clk)) {
83*4882a593Smuzhiyun pr_err("cannot get clock\n");
84*4882a593Smuzhiyun ret = -ENOMEM;
85*4882a593Smuzhiyun goto clk_err;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun ret = clk_prepare_enable(clk);
89*4882a593Smuzhiyun if (ret) {
90*4882a593Smuzhiyun pr_err("cannot enable clock\n");
91*4882a593Smuzhiyun goto clk_err;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun pci_base = of_iomap(child, 0);
95*4882a593Smuzhiyun if (pci_base == NULL) {
96*4882a593Smuzhiyun pr_err("cannot map registers\n");
97*4882a593Smuzhiyun ret = -ENOMEM;
98*4882a593Smuzhiyun goto res_ioremap;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* SoC ID */
102*4882a593Smuzhiyun soc_dev_id = readl(pci_base + PCIE_DEV_ID_OFF) >> 16;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* SoC revision */
105*4882a593Smuzhiyun soc_rev = readl(pci_base + PCIE_DEV_REV_OFF) & SOC_REV_MASK;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun is_id_valid = true;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun pr_info("MVEBU SoC ID=0x%X, Rev=0x%X\n", soc_dev_id, soc_rev);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun iounmap(pci_base);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun res_ioremap:
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * If the PCIe unit is actually enabled and we have PCI
116*4882a593Smuzhiyun * support in the kernel, we intentionally do not release the
117*4882a593Smuzhiyun * reference to the clock. We want to keep it running since
118*4882a593Smuzhiyun * the bootloader does some PCIe link configuration that the
119*4882a593Smuzhiyun * kernel is for now unable to do, and gating the clock would
120*4882a593Smuzhiyun * make us loose this precious configuration.
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun if (!of_device_is_available(child) || !IS_ENABLED(CONFIG_PCI_MVEBU)) {
123*4882a593Smuzhiyun clk_disable_unprepare(clk);
124*4882a593Smuzhiyun clk_put(clk);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun clk_err:
128*4882a593Smuzhiyun of_node_put(child);
129*4882a593Smuzhiyun of_node_put(np);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun return ret;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
mvebu_soc_id_init(void)134*4882a593Smuzhiyun static int __init mvebu_soc_id_init(void)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * First try to get the ID and the revision by the system
139*4882a593Smuzhiyun * register and use PCI registers only if it is not possible
140*4882a593Smuzhiyun */
141*4882a593Smuzhiyun if (!mvebu_system_controller_get_soc_id(&soc_dev_id, &soc_rev)) {
142*4882a593Smuzhiyun is_id_valid = true;
143*4882a593Smuzhiyun pr_info("MVEBU SoC ID=0x%X, Rev=0x%X\n", soc_dev_id, soc_rev);
144*4882a593Smuzhiyun return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun return get_soc_id_by_pci();
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun early_initcall(mvebu_soc_id_init);
150*4882a593Smuzhiyun
mvebu_soc_device(void)151*4882a593Smuzhiyun static int __init mvebu_soc_device(void)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun struct soc_device_attribute *soc_dev_attr;
154*4882a593Smuzhiyun struct soc_device *soc_dev;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* Also protects against running on non-mvebu systems */
157*4882a593Smuzhiyun if (!is_id_valid)
158*4882a593Smuzhiyun return 0;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
161*4882a593Smuzhiyun if (!soc_dev_attr)
162*4882a593Smuzhiyun return -ENOMEM;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun soc_dev_attr->family = kasprintf(GFP_KERNEL, "Marvell");
165*4882a593Smuzhiyun soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%X", soc_rev);
166*4882a593Smuzhiyun soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%X", soc_dev_id);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun soc_dev = soc_device_register(soc_dev_attr);
169*4882a593Smuzhiyun if (IS_ERR(soc_dev)) {
170*4882a593Smuzhiyun kfree(soc_dev_attr->family);
171*4882a593Smuzhiyun kfree(soc_dev_attr->revision);
172*4882a593Smuzhiyun kfree(soc_dev_attr->soc_id);
173*4882a593Smuzhiyun kfree(soc_dev_attr);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun return 0;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun postcore_initcall(mvebu_soc_device);
179