1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 * GNU General Public License for more details.
10 *
11 * Copyright (C) 2012 ARM Limited
12 */
13
14#include <linux/basic_mmio_gpio.h>
15#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/mfd/core.h>
18#include <linux/of_address.h>
19#include <linux/of_platform.h>
20#include <linux/platform_data/syscon.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23#include <linux/stat.h>
24#include <linux/vexpress.h>
25
26#define SYS_ID			0x000
27#define SYS_SW			0x004
28#define SYS_LED			0x008
29#define SYS_100HZ		0x024
30#define SYS_FLAGSSET		0x030
31#define SYS_FLAGSCLR		0x034
32#define SYS_NVFLAGS		0x038
33#define SYS_NVFLAGSSET		0x038
34#define SYS_NVFLAGSCLR		0x03c
35#define SYS_MCI			0x048
36#define SYS_FLASH		0x04c
37#define SYS_CFGSW		0x058
38#define SYS_24MHZ		0x05c
39#define SYS_MISC		0x060
40#define SYS_DMA			0x064
41#define SYS_PROCID0		0x084
42#define SYS_PROCID1		0x088
43#define SYS_CFGDATA		0x0a0
44#define SYS_CFGCTRL		0x0a4
45#define SYS_CFGSTAT		0x0a8
46
47#define SYS_HBI_MASK		0xfff
48#define SYS_PROCIDx_HBI_SHIFT	0
49
50#define SYS_MISC_MASTERSITE	(1 << 14)
51
52void vexpress_flags_set(u32 data)
53{
54	static void __iomem *base;
55
56	if (!base) {
57		struct device_node *node = of_find_compatible_node(NULL, NULL,
58				"arm,vexpress-sysreg");
59
60		base = of_iomap(node, 0);
61	}
62
63	if (WARN_ON(!base))
64		return;
65
66	writel(~0, base + SYS_FLAGSCLR);
67	writel(data, base + SYS_FLAGSSET);
68}
69
70/* The sysreg block is just a random collection of various functions... */
71
72static struct syscon_platform_data vexpress_sysreg_sys_id_pdata = {
73	.label = "sys_id",
74};
75
76static struct bgpio_pdata vexpress_sysreg_sys_led_pdata = {
77	.label = "sys_led",
78	.base = -1,
79	.ngpio = 8,
80};
81
82static struct bgpio_pdata vexpress_sysreg_sys_mci_pdata = {
83	.label = "sys_mci",
84	.base = -1,
85	.ngpio = 2,
86};
87
88static struct bgpio_pdata vexpress_sysreg_sys_flash_pdata = {
89	.label = "sys_flash",
90	.base = -1,
91	.ngpio = 1,
92};
93
94static struct syscon_platform_data vexpress_sysreg_sys_misc_pdata = {
95	.label = "sys_misc",
96};
97
98static struct syscon_platform_data vexpress_sysreg_sys_procid_pdata = {
99	.label = "sys_procid",
100};
101
102static struct mfd_cell vexpress_sysreg_cells[] = {
103	{
104		.name = "syscon",
105		.num_resources = 1,
106		.resources = (struct resource []) {
107			DEFINE_RES_MEM(SYS_ID, 0x4),
108		},
109		.platform_data = &vexpress_sysreg_sys_id_pdata,
110		.pdata_size = sizeof(vexpress_sysreg_sys_id_pdata),
111	}, {
112		.name = "basic-mmio-gpio",
113		.of_compatible = "arm,vexpress-sysreg,sys_led",
114		.num_resources = 1,
115		.resources = (struct resource []) {
116			DEFINE_RES_MEM_NAMED(SYS_LED, 0x4, "dat"),
117		},
118		.platform_data = &vexpress_sysreg_sys_led_pdata,
119		.pdata_size = sizeof(vexpress_sysreg_sys_led_pdata),
120	}, {
121		.name = "basic-mmio-gpio",
122		.of_compatible = "arm,vexpress-sysreg,sys_mci",
123		.num_resources = 1,
124		.resources = (struct resource []) {
125			DEFINE_RES_MEM_NAMED(SYS_MCI, 0x4, "dat"),
126		},
127		.platform_data = &vexpress_sysreg_sys_mci_pdata,
128		.pdata_size = sizeof(vexpress_sysreg_sys_mci_pdata),
129	}, {
130		.name = "basic-mmio-gpio",
131		.of_compatible = "arm,vexpress-sysreg,sys_flash",
132		.num_resources = 1,
133		.resources = (struct resource []) {
134			DEFINE_RES_MEM_NAMED(SYS_FLASH, 0x4, "dat"),
135		},
136		.platform_data = &vexpress_sysreg_sys_flash_pdata,
137		.pdata_size = sizeof(vexpress_sysreg_sys_flash_pdata),
138	}, {
139		.name = "syscon",
140		.num_resources = 1,
141		.resources = (struct resource []) {
142			DEFINE_RES_MEM(SYS_MISC, 0x4),
143		},
144		.platform_data = &vexpress_sysreg_sys_misc_pdata,
145		.pdata_size = sizeof(vexpress_sysreg_sys_misc_pdata),
146	}, {
147		.name = "syscon",
148		.num_resources = 1,
149		.resources = (struct resource []) {
150			DEFINE_RES_MEM(SYS_PROCID0, 0x8),
151		},
152		.platform_data = &vexpress_sysreg_sys_procid_pdata,
153		.pdata_size = sizeof(vexpress_sysreg_sys_procid_pdata),
154	}, {
155		.name = "vexpress-syscfg",
156		.num_resources = 1,
157		.resources = (struct resource []) {
158			DEFINE_RES_MEM(SYS_CFGDATA, 0xc),
159		},
160	}
161};
162
163static int vexpress_sysreg_probe(struct platform_device *pdev)
164{
165	struct resource *mem;
166	void __iomem *base;
167	struct bgpio_chip *mmc_gpio_chip;
168	int master;
169	u32 dt_hbi;
170
171	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
172	if (!mem)
173		return -EINVAL;
174
175	base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
176	if (!base)
177		return -ENOMEM;
178
179	master = readl(base + SYS_MISC) & SYS_MISC_MASTERSITE ?
180			VEXPRESS_SITE_DB2 : VEXPRESS_SITE_DB1;
181	vexpress_config_set_master(master);
182
183	/* Confirm board type against DT property, if available */
184	if (of_property_read_u32(of_root, "arm,hbi", &dt_hbi) == 0) {
185		u32 id = readl(base + (master == VEXPRESS_SITE_DB1 ?
186				 SYS_PROCID0 : SYS_PROCID1));
187		u32 hbi = (id >> SYS_PROCIDx_HBI_SHIFT) & SYS_HBI_MASK;
188
189		if (WARN_ON(dt_hbi != hbi))
190			dev_warn(&pdev->dev, "DT HBI (%x) is not matching hardware (%x)!\n",
191					dt_hbi, hbi);
192	}
193
194	/*
195	 * Duplicated SYS_MCI pseudo-GPIO controller for compatibility with
196	 * older trees using sysreg node for MMC control lines.
197	 */
198	mmc_gpio_chip = devm_kzalloc(&pdev->dev, sizeof(*mmc_gpio_chip),
199			GFP_KERNEL);
200	if (!mmc_gpio_chip)
201		return -ENOMEM;
202	bgpio_init(mmc_gpio_chip, &pdev->dev, 0x4, base + SYS_MCI,
203			NULL, NULL, NULL, NULL, 0);
204	mmc_gpio_chip->gc.ngpio = 2;
205	gpiochip_add(&mmc_gpio_chip->gc);
206
207	return mfd_add_devices(&pdev->dev, PLATFORM_DEVID_AUTO,
208			vexpress_sysreg_cells,
209			ARRAY_SIZE(vexpress_sysreg_cells), mem, 0, NULL);
210}
211
212static const struct of_device_id vexpress_sysreg_match[] = {
213	{ .compatible = "arm,vexpress-sysreg", },
214	{},
215};
216
217static struct platform_driver vexpress_sysreg_driver = {
218	.driver = {
219		.name = "vexpress-sysreg",
220		.of_match_table = vexpress_sysreg_match,
221	},
222	.probe = vexpress_sysreg_probe,
223};
224
225static int __init vexpress_sysreg_init(void)
226{
227	struct device_node *node;
228
229	/* Need the sysreg early, before any other device... */
230	for_each_matching_node(node, vexpress_sysreg_match)
231		of_platform_device_create(node, NULL, NULL);
232
233	return platform_driver_register(&vexpress_sysreg_driver);
234}
235core_initcall(vexpress_sysreg_init);
236