blob: 201a3ea2a9d3045eaabf961c9db15d44aa340c8e [file] [log] [blame]
Pawel Moll88e0abc2012-09-18 12:24:57 +01001/*
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
Linus Walleij0f4630f2015-12-04 14:02:58 +010014#include <linux/gpio/driver.h>
Pawel Moll88e0abc2012-09-18 12:24:57 +010015#include <linux/err.h>
Pawel Moll88e0abc2012-09-18 12:24:57 +010016#include <linux/io.h>
Pawel Moll974cc7b2014-04-23 10:49:31 +010017#include <linux/mfd/core.h>
Pawel Moll88e0abc2012-09-18 12:24:57 +010018#include <linux/of_address.h>
Pawel Moll3b9334a2014-04-30 16:46:29 +010019#include <linux/of_platform.h>
Pawel Moll974cc7b2014-04-23 10:49:31 +010020#include <linux/platform_data/syscon.h>
Pawel Moll88e0abc2012-09-18 12:24:57 +010021#include <linux/platform_device.h>
Pawel Moll88e0abc2012-09-18 12:24:57 +010022#include <linux/slab.h>
23#include <linux/stat.h>
Pawel Moll88e0abc2012-09-18 12:24:57 +010024#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
Pawel Moll88e0abc2012-09-18 12:24:57 +010030#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
Pawel Moll88e0abc2012-09-18 12:24:57 +010048#define SYS_PROCIDx_HBI_SHIFT 0
49
Pawel Moll88e0abc2012-09-18 12:24:57 +010050#define SYS_MISC_MASTERSITE (1 << 14)
51
Pawel Moll88e0abc2012-09-18 12:24:57 +010052void vexpress_flags_set(u32 data)
53{
Pawel Mollf0bd7cc2015-01-20 17:25:15 +000054 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);
Pawel Moll974cc7b2014-04-23 10:49:31 +010068}
69
Pawel Moll974cc7b2014-04-23 10:49:31 +010070/* The sysreg block is just a random collection of various functions... */
Pawel Moll8eb12b92013-06-11 11:56:02 +010071
Pawel Moll974cc7b2014-04-23 10:49:31 +010072static 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 },
Pawel Moll8ea402f2013-01-30 10:33:16 +0000160 }
Pawel Moll88e0abc2012-09-18 12:24:57 +0100161};
162
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800163static int vexpress_sysreg_probe(struct platform_device *pdev)
Pawel Moll88e0abc2012-09-18 12:24:57 +0100164{
Pawel Moll974cc7b2014-04-23 10:49:31 +0100165 struct resource *mem;
166 void __iomem *base;
Linus Walleij0f4630f2015-12-04 14:02:58 +0100167 struct gpio_chip *mmc_gpio_chip;
Pawel Mollf0bd7cc2015-01-20 17:25:15 +0000168 int master;
Pawel Moll6b2c31c2014-02-06 14:33:44 +0000169 u32 dt_hbi;
Pawel Moll88e0abc2012-09-18 12:24:57 +0100170
Pawel Moll974cc7b2014-04-23 10:49:31 +0100171 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
172 if (!mem)
173 return -EINVAL;
Pawel Moll88e0abc2012-09-18 12:24:57 +0100174
Pawel Moll974cc7b2014-04-23 10:49:31 +0100175 base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
176 if (!base)
177 return -ENOMEM;
Pawel Moll88e0abc2012-09-18 12:24:57 +0100178
Pawel Mollf0bd7cc2015-01-20 17:25:15 +0000179 master = readl(base + SYS_MISC) & SYS_MISC_MASTERSITE ?
180 VEXPRESS_SITE_DB2 : VEXPRESS_SITE_DB1;
181 vexpress_config_set_master(master);
Pawel Moll8eb12b92013-06-11 11:56:02 +0100182
Pawel Moll6b2c31c2014-02-06 14:33:44 +0000183 /* Confirm board type against DT property, if available */
Grant Likely5063e252014-10-03 16:28:27 +0100184 if (of_property_read_u32(of_root, "arm,hbi", &dt_hbi) == 0) {
Pawel Mollf0bd7cc2015-01-20 17:25:15 +0000185 u32 id = readl(base + (master == VEXPRESS_SITE_DB1 ?
186 SYS_PROCID0 : SYS_PROCID1));
Pawel Moll6b2c31c2014-02-06 14:33:44 +0000187 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
Pawel Moll974cc7b2014-04-23 10:49:31 +0100194 /*
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);
Linus Walleij0f4630f2015-12-04 14:02:58 +0100204 mmc_gpio_chip->ngpio = 2;
Linus Walleij7ad07362016-03-30 10:48:09 +0200205 gpiochip_add_data(mmc_gpio_chip, NULL);
Pawel Moll88e0abc2012-09-18 12:24:57 +0100206
Pawel Moll974cc7b2014-04-23 10:49:31 +0100207 return mfd_add_devices(&pdev->dev, PLATFORM_DEVID_AUTO,
208 vexpress_sysreg_cells,
209 ARRAY_SIZE(vexpress_sysreg_cells), mem, 0, NULL);
Pawel Moll88e0abc2012-09-18 12:24:57 +0100210}
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{
Pawel Moll3b9334a2014-04-30 16:46:29 +0100227 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
Pawel Moll88e0abc2012-09-18 12:24:57 +0100233 return platform_driver_register(&vexpress_sysreg_driver);
234}
235core_initcall(vexpress_sysreg_init);