Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 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 | |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 14 | #include <linux/gpio/driver.h> |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 15 | #include <linux/err.h> |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 16 | #include <linux/io.h> |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 17 | #include <linux/mfd/core.h> |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 18 | #include <linux/of_address.h> |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 19 | #include <linux/of_platform.h> |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 20 | #include <linux/platform_data/syscon.h> |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/stat.h> |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 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 |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 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 |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 48 | #define SYS_PROCIDx_HBI_SHIFT 0 |
| 49 | |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 50 | #define SYS_MISC_MASTERSITE (1 << 14) |
| 51 | |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 52 | void vexpress_flags_set(u32 data) |
| 53 | { |
Pawel Moll | f0bd7cc | 2015-01-20 17:25:15 +0000 | [diff] [blame] | 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); |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 70 | /* The sysreg block is just a random collection of various functions... */ |
Pawel Moll | 8eb12b9 | 2013-06-11 11:56:02 +0100 | [diff] [blame] | 71 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 72 | static struct syscon_platform_data vexpress_sysreg_sys_id_pdata = { |
| 73 | .label = "sys_id", |
| 74 | }; |
| 75 | |
| 76 | static struct bgpio_pdata vexpress_sysreg_sys_led_pdata = { |
| 77 | .label = "sys_led", |
| 78 | .base = -1, |
| 79 | .ngpio = 8, |
| 80 | }; |
| 81 | |
| 82 | static struct bgpio_pdata vexpress_sysreg_sys_mci_pdata = { |
| 83 | .label = "sys_mci", |
| 84 | .base = -1, |
| 85 | .ngpio = 2, |
| 86 | }; |
| 87 | |
| 88 | static struct bgpio_pdata vexpress_sysreg_sys_flash_pdata = { |
| 89 | .label = "sys_flash", |
| 90 | .base = -1, |
| 91 | .ngpio = 1, |
| 92 | }; |
| 93 | |
| 94 | static struct syscon_platform_data vexpress_sysreg_sys_misc_pdata = { |
| 95 | .label = "sys_misc", |
| 96 | }; |
| 97 | |
| 98 | static struct syscon_platform_data vexpress_sysreg_sys_procid_pdata = { |
| 99 | .label = "sys_procid", |
| 100 | }; |
| 101 | |
| 102 | static 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 Moll | 8ea402f | 2013-01-30 10:33:16 +0000 | [diff] [blame] | 160 | } |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
Greg Kroah-Hartman | 612b95c | 2012-12-21 15:03:15 -0800 | [diff] [blame] | 163 | static int vexpress_sysreg_probe(struct platform_device *pdev) |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 164 | { |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 165 | struct resource *mem; |
| 166 | void __iomem *base; |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 167 | struct gpio_chip *mmc_gpio_chip; |
Pawel Moll | f0bd7cc | 2015-01-20 17:25:15 +0000 | [diff] [blame] | 168 | int master; |
Pawel Moll | 6b2c31c | 2014-02-06 14:33:44 +0000 | [diff] [blame] | 169 | u32 dt_hbi; |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 170 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 171 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 172 | if (!mem) |
| 173 | return -EINVAL; |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 174 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 175 | base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem)); |
| 176 | if (!base) |
| 177 | return -ENOMEM; |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 178 | |
Pawel Moll | f0bd7cc | 2015-01-20 17:25:15 +0000 | [diff] [blame] | 179 | master = readl(base + SYS_MISC) & SYS_MISC_MASTERSITE ? |
| 180 | VEXPRESS_SITE_DB2 : VEXPRESS_SITE_DB1; |
| 181 | vexpress_config_set_master(master); |
Pawel Moll | 8eb12b9 | 2013-06-11 11:56:02 +0100 | [diff] [blame] | 182 | |
Pawel Moll | 6b2c31c | 2014-02-06 14:33:44 +0000 | [diff] [blame] | 183 | /* Confirm board type against DT property, if available */ |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 184 | if (of_property_read_u32(of_root, "arm,hbi", &dt_hbi) == 0) { |
Pawel Moll | f0bd7cc | 2015-01-20 17:25:15 +0000 | [diff] [blame] | 185 | u32 id = readl(base + (master == VEXPRESS_SITE_DB1 ? |
| 186 | SYS_PROCID0 : SYS_PROCID1)); |
Pawel Moll | 6b2c31c | 2014-02-06 14:33:44 +0000 | [diff] [blame] | 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 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 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); |
Linus Walleij | 0f4630f | 2015-12-04 14:02:58 +0100 | [diff] [blame] | 204 | mmc_gpio_chip->ngpio = 2; |
| 205 | gpiochip_add(mmc_gpio_chip); |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 206 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame] | 207 | return mfd_add_devices(&pdev->dev, PLATFORM_DEVID_AUTO, |
| 208 | vexpress_sysreg_cells, |
| 209 | ARRAY_SIZE(vexpress_sysreg_cells), mem, 0, NULL); |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static const struct of_device_id vexpress_sysreg_match[] = { |
| 213 | { .compatible = "arm,vexpress-sysreg", }, |
| 214 | {}, |
| 215 | }; |
| 216 | |
| 217 | static 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 | |
| 225 | static int __init vexpress_sysreg_init(void) |
| 226 | { |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 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 | |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 233 | return platform_driver_register(&vexpress_sysreg_driver); |
| 234 | } |
| 235 | core_initcall(vexpress_sysreg_init); |