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.
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.
11 * Copyright (C) 2012 ARM Limited
14 #include <linux/err.h>
15 #include <linux/gpio.h>
17 #include <linux/leds.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/slab.h>
22 #include <linux/stat.h>
23 #include <linux/timer.h>
24 #include <linux/vexpress.h>
29 #define SYS_100HZ 0x024
30 #define SYS_FLAGS 0x030
31 #define SYS_FLAGSSET 0x030
32 #define SYS_FLAGSCLR 0x034
33 #define SYS_NVFLAGS 0x038
34 #define SYS_NVFLAGSSET 0x038
35 #define SYS_NVFLAGSCLR 0x03c
37 #define SYS_FLASH 0x04c
38 #define SYS_CFGSW 0x058
39 #define SYS_24MHZ 0x05c
40 #define SYS_MISC 0x060
42 #define SYS_PROCID0 0x084
43 #define SYS_PROCID1 0x088
44 #define SYS_CFGDATA 0x0a0
45 #define SYS_CFGCTRL 0x0a4
46 #define SYS_CFGSTAT 0x0a8
48 #define SYS_HBI_MASK 0xfff
49 #define SYS_ID_HBI_SHIFT 16
50 #define SYS_PROCIDx_HBI_SHIFT 0
52 #define SYS_LED_LED(n) (1 << (n))
54 #define SYS_MCI_CARDIN (1 << 0)
55 #define SYS_MCI_WPROT (1 << 1)
57 #define SYS_FLASH_WPn (1 << 0)
59 #define SYS_MISC_MASTERSITE (1 << 14)
61 #define SYS_CFGCTRL_START (1 << 31)
62 #define SYS_CFGCTRL_WRITE (1 << 30)
63 #define SYS_CFGCTRL_DCC(n) (((n) & 0xf) << 26)
64 #define SYS_CFGCTRL_FUNC(n) (((n) & 0x3f) << 20)
65 #define SYS_CFGCTRL_SITE(n) (((n) & 0x3) << 16)
66 #define SYS_CFGCTRL_POSITION(n) (((n) & 0xf) << 12)
67 #define SYS_CFGCTRL_DEVICE(n) (((n) & 0xfff) << 0)
69 #define SYS_CFGSTAT_ERR (1 << 1)
70 #define SYS_CFGSTAT_COMPLETE (1 << 0)
73 static void __iomem *vexpress_sysreg_base;
74 static struct device *vexpress_sysreg_dev;
75 static int vexpress_master_site;
78 void vexpress_flags_set(u32 data)
80 writel(~0, vexpress_sysreg_base + SYS_FLAGSCLR);
81 writel(data, vexpress_sysreg_base + SYS_FLAGSSET);
84 u32 vexpress_get_procid(int site)
86 if (site == VEXPRESS_SITE_MASTER)
87 site = vexpress_master_site;
89 return readl(vexpress_sysreg_base + (site == VEXPRESS_SITE_DB1 ?
90 SYS_PROCID0 : SYS_PROCID1));
93 u32 vexpress_get_hbi(int site)
98 case VEXPRESS_SITE_MB:
99 id = readl(vexpress_sysreg_base + SYS_ID);
100 return (id >> SYS_ID_HBI_SHIFT) & SYS_HBI_MASK;
101 case VEXPRESS_SITE_MASTER:
102 case VEXPRESS_SITE_DB1:
103 case VEXPRESS_SITE_DB2:
104 id = vexpress_get_procid(site);
105 return (id >> SYS_PROCIDx_HBI_SHIFT) & SYS_HBI_MASK;
111 void __iomem *vexpress_get_24mhz_clock_base(void)
113 return vexpress_sysreg_base + SYS_24MHZ;
117 static void vexpress_sysreg_find_prop(struct device_node *node,
118 const char *name, u32 *val)
122 if (of_property_read_u32(node, name, val) == 0) {
126 node = of_get_next_parent(node);
130 unsigned __vexpress_get_site(struct device *dev, struct device_node *node)
134 WARN_ON(dev && node && dev->of_node != node);
139 vexpress_sysreg_find_prop(node, "arm,vexpress,site", &site);
140 } else if (dev && dev->bus == &platform_bus_type) {
141 struct platform_device *pdev = to_platform_device(dev);
143 if (pdev->num_resources == 1 &&
144 pdev->resource[0].flags == IORESOURCE_BUS)
145 site = pdev->resource[0].start;
146 } else if (dev && strncmp(dev_name(dev), "ct:", 3) == 0) {
147 site = VEXPRESS_SITE_MASTER;
150 if (site == VEXPRESS_SITE_MASTER)
151 site = vexpress_master_site;
157 struct vexpress_sysreg_config_func {
162 static struct vexpress_config_bridge *vexpress_sysreg_config_bridge;
163 static struct timer_list vexpress_sysreg_config_timer;
164 static u32 *vexpress_sysreg_config_data;
165 static int vexpress_sysreg_config_tries;
167 static void *vexpress_sysreg_config_func_get(struct device *dev,
168 struct device_node *node)
170 struct vexpress_sysreg_config_func *config_func;
179 vexpress_sysreg_find_prop(node, "arm,vexpress,site", &site);
180 vexpress_sysreg_find_prop(node, "arm,vexpress,position",
182 vexpress_sysreg_find_prop(node, "arm,vexpress,dcc", &dcc);
183 err = of_property_read_u32_array(node,
184 "arm,vexpress-sysreg,func", func_device,
185 ARRAY_SIZE(func_device));
187 } else if (dev && dev->bus == &platform_bus_type) {
188 struct platform_device *pdev = to_platform_device(dev);
190 if (pdev->num_resources == 1 &&
191 pdev->resource[0].flags == IORESOURCE_BUS) {
192 site = pdev->resource[0].start;
193 func_device[0] = pdev->resource[0].end;
194 func_device[1] = pdev->id;
201 config_func = kzalloc(sizeof(*config_func), GFP_KERNEL);
205 config_func->template = SYS_CFGCTRL_DCC(dcc);
206 config_func->template |= SYS_CFGCTRL_FUNC(func_device[0]);
207 config_func->template |= SYS_CFGCTRL_SITE(site == VEXPRESS_SITE_MASTER ?
208 vexpress_master_site : site);
209 config_func->template |= SYS_CFGCTRL_POSITION(position);
210 config_func->device |= func_device[1];
212 dev_dbg(vexpress_sysreg_dev, "func 0x%p = 0x%x, %d\n", config_func,
213 config_func->template, config_func->device);
218 static void vexpress_sysreg_config_func_put(void *func)
223 static int vexpress_sysreg_config_func_exec(void *func, int offset,
224 bool write, u32 *data)
227 struct vexpress_sysreg_config_func *config_func = func;
230 if (WARN_ON(!vexpress_sysreg_base))
233 command = readl(vexpress_sysreg_base + SYS_CFGCTRL);
234 if (WARN_ON(command & SYS_CFGCTRL_START))
237 command = SYS_CFGCTRL_START;
238 command |= write ? SYS_CFGCTRL_WRITE : 0;
239 command |= config_func->template;
240 command |= SYS_CFGCTRL_DEVICE(config_func->device + offset);
242 /* Use a canary for reads */
246 dev_dbg(vexpress_sysreg_dev, "command %x, data %x\n",
248 writel(*data, vexpress_sysreg_base + SYS_CFGDATA);
249 writel(0, vexpress_sysreg_base + SYS_CFGSTAT);
250 writel(command, vexpress_sysreg_base + SYS_CFGCTRL);
253 if (vexpress_sysreg_dev) {
254 /* Schedule completion check */
256 vexpress_sysreg_config_data = data;
257 vexpress_sysreg_config_tries = 100;
258 mod_timer(&vexpress_sysreg_config_timer,
259 jiffies + usecs_to_jiffies(100));
260 status = VEXPRESS_CONFIG_STATUS_WAIT;
262 /* Early execution, no timer available, have to spin */
267 cfgstat = readl(vexpress_sysreg_base + SYS_CFGSTAT);
270 if (!write && (cfgstat & SYS_CFGSTAT_COMPLETE))
271 *data = readl(vexpress_sysreg_base + SYS_CFGDATA);
272 status = VEXPRESS_CONFIG_STATUS_DONE;
274 if (cfgstat & SYS_CFGSTAT_ERR)
281 struct vexpress_config_bridge_info vexpress_sysreg_config_bridge_info = {
282 .name = "vexpress-sysreg",
283 .func_get = vexpress_sysreg_config_func_get,
284 .func_put = vexpress_sysreg_config_func_put,
285 .func_exec = vexpress_sysreg_config_func_exec,
288 static void vexpress_sysreg_config_complete(unsigned long data)
290 int status = VEXPRESS_CONFIG_STATUS_DONE;
291 u32 cfgstat = readl(vexpress_sysreg_base + SYS_CFGSTAT);
293 if (cfgstat & SYS_CFGSTAT_ERR)
295 if (!vexpress_sysreg_config_tries--)
299 dev_err(vexpress_sysreg_dev, "error %d\n", status);
300 } else if (!(cfgstat & SYS_CFGSTAT_COMPLETE)) {
301 mod_timer(&vexpress_sysreg_config_timer,
302 jiffies + usecs_to_jiffies(50));
306 if (vexpress_sysreg_config_data) {
307 *vexpress_sysreg_config_data = readl(vexpress_sysreg_base +
309 dev_dbg(vexpress_sysreg_dev, "read data %x\n",
310 *vexpress_sysreg_config_data);
311 vexpress_sysreg_config_data = NULL;
314 vexpress_config_complete(vexpress_sysreg_config_bridge, status);
318 void vexpress_sysreg_setup(struct device_node *node)
320 if (WARN_ON(!vexpress_sysreg_base))
323 if (readl(vexpress_sysreg_base + SYS_MISC) & SYS_MISC_MASTERSITE)
324 vexpress_master_site = VEXPRESS_SITE_DB2;
326 vexpress_master_site = VEXPRESS_SITE_DB1;
328 vexpress_sysreg_config_bridge = vexpress_config_bridge_register(
329 node, &vexpress_sysreg_config_bridge_info);
330 WARN_ON(!vexpress_sysreg_config_bridge);
333 void __init vexpress_sysreg_early_init(void __iomem *base)
335 vexpress_sysreg_base = base;
336 vexpress_sysreg_setup(NULL);
339 void __init vexpress_sysreg_of_early_init(void)
341 struct device_node *node;
343 if (vexpress_sysreg_base)
346 node = of_find_compatible_node(NULL, NULL, "arm,vexpress-sysreg");
348 vexpress_sysreg_base = of_iomap(node, 0);
349 vexpress_sysreg_setup(node);
354 #define VEXPRESS_SYSREG_GPIO(_name, _reg, _value) \
355 [VEXPRESS_GPIO_##_name] = { \
357 .value = _reg##_##_value, \
360 static struct vexpress_sysreg_gpio {
363 } vexpress_sysreg_gpios[] = {
364 VEXPRESS_SYSREG_GPIO(MMC_CARDIN, SYS_MCI, CARDIN),
365 VEXPRESS_SYSREG_GPIO(MMC_WPROT, SYS_MCI, WPROT),
366 VEXPRESS_SYSREG_GPIO(FLASH_WPn, SYS_FLASH, WPn),
367 VEXPRESS_SYSREG_GPIO(LED0, SYS_LED, LED(0)),
368 VEXPRESS_SYSREG_GPIO(LED1, SYS_LED, LED(1)),
369 VEXPRESS_SYSREG_GPIO(LED2, SYS_LED, LED(2)),
370 VEXPRESS_SYSREG_GPIO(LED3, SYS_LED, LED(3)),
371 VEXPRESS_SYSREG_GPIO(LED4, SYS_LED, LED(4)),
372 VEXPRESS_SYSREG_GPIO(LED5, SYS_LED, LED(5)),
373 VEXPRESS_SYSREG_GPIO(LED6, SYS_LED, LED(6)),
374 VEXPRESS_SYSREG_GPIO(LED7, SYS_LED, LED(7)),
377 static int vexpress_sysreg_gpio_direction_input(struct gpio_chip *chip,
383 static int vexpress_sysreg_gpio_get(struct gpio_chip *chip,
386 struct vexpress_sysreg_gpio *gpio = &vexpress_sysreg_gpios[offset];
387 u32 reg_value = readl(vexpress_sysreg_base + gpio->reg);
389 return !!(reg_value & gpio->value);
392 static void vexpress_sysreg_gpio_set(struct gpio_chip *chip,
393 unsigned offset, int value)
395 struct vexpress_sysreg_gpio *gpio = &vexpress_sysreg_gpios[offset];
396 u32 reg_value = readl(vexpress_sysreg_base + gpio->reg);
399 reg_value |= gpio->value;
401 reg_value &= ~gpio->value;
403 writel(reg_value, vexpress_sysreg_base + gpio->reg);
406 static int vexpress_sysreg_gpio_direction_output(struct gpio_chip *chip,
407 unsigned offset, int value)
409 vexpress_sysreg_gpio_set(chip, offset, value);
414 static struct gpio_chip vexpress_sysreg_gpio_chip = {
415 .label = "vexpress-sysreg",
416 .direction_input = vexpress_sysreg_gpio_direction_input,
417 .direction_output = vexpress_sysreg_gpio_direction_output,
418 .get = vexpress_sysreg_gpio_get,
419 .set = vexpress_sysreg_gpio_set,
420 .ngpio = ARRAY_SIZE(vexpress_sysreg_gpios),
425 #define VEXPRESS_SYSREG_GREEN_LED(_name, _default_trigger, _gpio) \
427 .name = "v2m:green:"_name, \
428 .default_trigger = _default_trigger, \
429 .gpio = VEXPRESS_GPIO_##_gpio, \
432 struct gpio_led vexpress_sysreg_leds[] = {
433 VEXPRESS_SYSREG_GREEN_LED("user1", "heartbeat", LED0),
434 VEXPRESS_SYSREG_GREEN_LED("user2", "mmc0", LED1),
435 VEXPRESS_SYSREG_GREEN_LED("user3", "cpu0", LED2),
436 VEXPRESS_SYSREG_GREEN_LED("user4", "cpu1", LED3),
437 VEXPRESS_SYSREG_GREEN_LED("user5", "cpu2", LED4),
438 VEXPRESS_SYSREG_GREEN_LED("user6", "cpu3", LED5),
439 VEXPRESS_SYSREG_GREEN_LED("user7", "cpu4", LED6),
440 VEXPRESS_SYSREG_GREEN_LED("user8", "cpu5", LED7),
443 struct gpio_led_platform_data vexpress_sysreg_leds_pdata = {
444 .num_leds = ARRAY_SIZE(vexpress_sysreg_leds),
445 .leds = vexpress_sysreg_leds,
449 static ssize_t vexpress_sysreg_sys_id_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 return sprintf(buf, "0x%08x\n", readl(vexpress_sysreg_base + SYS_ID));
455 DEVICE_ATTR(sys_id, S_IRUGO, vexpress_sysreg_sys_id_show, NULL);
457 static int vexpress_sysreg_probe(struct platform_device *pdev)
460 struct resource *res = platform_get_resource(pdev,
463 if (!devm_request_mem_region(&pdev->dev, res->start,
464 resource_size(res), pdev->name)) {
465 dev_err(&pdev->dev, "Failed to request memory region!\n");
469 if (!vexpress_sysreg_base) {
470 vexpress_sysreg_base = devm_ioremap(&pdev->dev, res->start,
472 vexpress_sysreg_setup(pdev->dev.of_node);
475 if (!vexpress_sysreg_base) {
476 dev_err(&pdev->dev, "Failed to obtain base address!\n");
480 setup_timer(&vexpress_sysreg_config_timer,
481 vexpress_sysreg_config_complete, 0);
483 vexpress_sysreg_gpio_chip.dev = &pdev->dev;
484 err = gpiochip_add(&vexpress_sysreg_gpio_chip);
486 vexpress_config_bridge_unregister(
487 vexpress_sysreg_config_bridge);
488 dev_err(&pdev->dev, "Failed to register GPIO chip! (%d)\n",
493 vexpress_sysreg_dev = &pdev->dev;
495 platform_device_register_data(vexpress_sysreg_dev, "leds-gpio",
496 PLATFORM_DEVID_AUTO, &vexpress_sysreg_leds_pdata,
497 sizeof(vexpress_sysreg_leds_pdata));
499 device_create_file(vexpress_sysreg_dev, &dev_attr_sys_id);
504 static const struct of_device_id vexpress_sysreg_match[] = {
505 { .compatible = "arm,vexpress-sysreg", },
509 static struct platform_driver vexpress_sysreg_driver = {
511 .name = "vexpress-sysreg",
512 .of_match_table = vexpress_sysreg_match,
514 .probe = vexpress_sysreg_probe,
517 static int __init vexpress_sysreg_init(void)
519 vexpress_sysreg_of_early_init();
520 return platform_driver_register(&vexpress_sysreg_driver);
522 core_initcall(vexpress_sysreg_init);