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 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 14 | #include <linux/basic_mmio_gpio.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 |
| 48 | #define SYS_ID_HBI_SHIFT 16 |
| 49 | #define SYS_PROCIDx_HBI_SHIFT 0 |
| 50 | |
| 51 | #define SYS_MCI_CARDIN (1 << 0) |
| 52 | #define SYS_MCI_WPROT (1 << 1) |
| 53 | |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 54 | #define SYS_MISC_MASTERSITE (1 << 14) |
| 55 | |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 56 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 57 | static void __iomem *__vexpress_sysreg_base; |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 58 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 59 | static void __iomem *vexpress_sysreg_base(void) |
| 60 | { |
| 61 | if (!__vexpress_sysreg_base) { |
| 62 | struct device_node *node = of_find_compatible_node(NULL, NULL, |
| 63 | "arm,vexpress-sysreg"); |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 64 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 65 | __vexpress_sysreg_base = of_iomap(node, 0); |
| 66 | } |
| 67 | |
| 68 | WARN_ON(!__vexpress_sysreg_base); |
| 69 | |
| 70 | return __vexpress_sysreg_base; |
| 71 | } |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 72 | |
| 73 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 74 | static int vexpress_sysreg_get_master(void) |
| 75 | { |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 76 | if (readl(vexpress_sysreg_base() + SYS_MISC) & SYS_MISC_MASTERSITE) |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 77 | return VEXPRESS_SITE_DB2; |
| 78 | |
| 79 | return VEXPRESS_SITE_DB1; |
| 80 | } |
| 81 | |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 82 | void vexpress_flags_set(u32 data) |
| 83 | { |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 84 | writel(~0, vexpress_sysreg_base() + SYS_FLAGSCLR); |
| 85 | writel(data, vexpress_sysreg_base() + SYS_FLAGSSET); |
| 86 | } |
| 87 | |
| 88 | unsigned int vexpress_get_mci_cardin(struct device *dev) |
| 89 | { |
| 90 | return readl(vexpress_sysreg_base() + SYS_MCI) & SYS_MCI_CARDIN; |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | u32 vexpress_get_procid(int site) |
| 94 | { |
| 95 | if (site == VEXPRESS_SITE_MASTER) |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 96 | site = vexpress_sysreg_get_master(); |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 97 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 98 | return readl(vexpress_sysreg_base() + (site == VEXPRESS_SITE_DB1 ? |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 99 | SYS_PROCID0 : SYS_PROCID1)); |
| 100 | } |
| 101 | |
| 102 | u32 vexpress_get_hbi(int site) |
| 103 | { |
| 104 | u32 id; |
| 105 | |
| 106 | switch (site) { |
| 107 | case VEXPRESS_SITE_MB: |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 108 | id = readl(vexpress_sysreg_base() + SYS_ID); |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 109 | return (id >> SYS_ID_HBI_SHIFT) & SYS_HBI_MASK; |
| 110 | case VEXPRESS_SITE_MASTER: |
| 111 | case VEXPRESS_SITE_DB1: |
| 112 | case VEXPRESS_SITE_DB2: |
| 113 | id = vexpress_get_procid(site); |
| 114 | return (id >> SYS_PROCIDx_HBI_SHIFT) & SYS_HBI_MASK; |
| 115 | } |
| 116 | |
| 117 | return ~0; |
| 118 | } |
| 119 | |
| 120 | void __iomem *vexpress_get_24mhz_clock_base(void) |
| 121 | { |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 122 | return vexpress_sysreg_base() + SYS_24MHZ; |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
Pawel Moll | 5266629 | 2012-11-27 16:48:50 +0000 | [diff] [blame] | 126 | void __init vexpress_sysreg_early_init(void __iomem *base) |
| 127 | { |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 128 | __vexpress_sysreg_base = base; |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 129 | |
| 130 | vexpress_config_set_master(vexpress_sysreg_get_master()); |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 134 | /* The sysreg block is just a random collection of various functions... */ |
Pawel Moll | 8eb12b9 | 2013-06-11 11:56:02 +0100 | [diff] [blame] | 135 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 136 | static struct syscon_platform_data vexpress_sysreg_sys_id_pdata = { |
| 137 | .label = "sys_id", |
| 138 | }; |
| 139 | |
| 140 | static struct bgpio_pdata vexpress_sysreg_sys_led_pdata = { |
| 141 | .label = "sys_led", |
| 142 | .base = -1, |
| 143 | .ngpio = 8, |
| 144 | }; |
| 145 | |
| 146 | static struct bgpio_pdata vexpress_sysreg_sys_mci_pdata = { |
| 147 | .label = "sys_mci", |
| 148 | .base = -1, |
| 149 | .ngpio = 2, |
| 150 | }; |
| 151 | |
| 152 | static struct bgpio_pdata vexpress_sysreg_sys_flash_pdata = { |
| 153 | .label = "sys_flash", |
| 154 | .base = -1, |
| 155 | .ngpio = 1, |
| 156 | }; |
| 157 | |
| 158 | static struct syscon_platform_data vexpress_sysreg_sys_misc_pdata = { |
| 159 | .label = "sys_misc", |
| 160 | }; |
| 161 | |
| 162 | static struct syscon_platform_data vexpress_sysreg_sys_procid_pdata = { |
| 163 | .label = "sys_procid", |
| 164 | }; |
| 165 | |
| 166 | static struct mfd_cell vexpress_sysreg_cells[] = { |
| 167 | { |
| 168 | .name = "syscon", |
| 169 | .num_resources = 1, |
| 170 | .resources = (struct resource []) { |
| 171 | DEFINE_RES_MEM(SYS_ID, 0x4), |
| 172 | }, |
| 173 | .platform_data = &vexpress_sysreg_sys_id_pdata, |
| 174 | .pdata_size = sizeof(vexpress_sysreg_sys_id_pdata), |
| 175 | }, { |
| 176 | .name = "basic-mmio-gpio", |
| 177 | .of_compatible = "arm,vexpress-sysreg,sys_led", |
| 178 | .num_resources = 1, |
| 179 | .resources = (struct resource []) { |
| 180 | DEFINE_RES_MEM_NAMED(SYS_LED, 0x4, "dat"), |
| 181 | }, |
| 182 | .platform_data = &vexpress_sysreg_sys_led_pdata, |
| 183 | .pdata_size = sizeof(vexpress_sysreg_sys_led_pdata), |
| 184 | }, { |
| 185 | .name = "basic-mmio-gpio", |
| 186 | .of_compatible = "arm,vexpress-sysreg,sys_mci", |
| 187 | .num_resources = 1, |
| 188 | .resources = (struct resource []) { |
| 189 | DEFINE_RES_MEM_NAMED(SYS_MCI, 0x4, "dat"), |
| 190 | }, |
| 191 | .platform_data = &vexpress_sysreg_sys_mci_pdata, |
| 192 | .pdata_size = sizeof(vexpress_sysreg_sys_mci_pdata), |
| 193 | }, { |
| 194 | .name = "basic-mmio-gpio", |
| 195 | .of_compatible = "arm,vexpress-sysreg,sys_flash", |
| 196 | .num_resources = 1, |
| 197 | .resources = (struct resource []) { |
| 198 | DEFINE_RES_MEM_NAMED(SYS_FLASH, 0x4, "dat"), |
| 199 | }, |
| 200 | .platform_data = &vexpress_sysreg_sys_flash_pdata, |
| 201 | .pdata_size = sizeof(vexpress_sysreg_sys_flash_pdata), |
| 202 | }, { |
| 203 | .name = "syscon", |
| 204 | .num_resources = 1, |
| 205 | .resources = (struct resource []) { |
| 206 | DEFINE_RES_MEM(SYS_MISC, 0x4), |
| 207 | }, |
| 208 | .platform_data = &vexpress_sysreg_sys_misc_pdata, |
| 209 | .pdata_size = sizeof(vexpress_sysreg_sys_misc_pdata), |
| 210 | }, { |
| 211 | .name = "syscon", |
| 212 | .num_resources = 1, |
| 213 | .resources = (struct resource []) { |
| 214 | DEFINE_RES_MEM(SYS_PROCID0, 0x8), |
| 215 | }, |
| 216 | .platform_data = &vexpress_sysreg_sys_procid_pdata, |
| 217 | .pdata_size = sizeof(vexpress_sysreg_sys_procid_pdata), |
| 218 | }, { |
| 219 | .name = "vexpress-syscfg", |
| 220 | .num_resources = 1, |
| 221 | .resources = (struct resource []) { |
| 222 | DEFINE_RES_MEM(SYS_CFGDATA, 0xc), |
| 223 | }, |
Pawel Moll | 8ea402f | 2013-01-30 10:33:16 +0000 | [diff] [blame] | 224 | } |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 225 | }; |
| 226 | |
Greg Kroah-Hartman | 612b95c | 2012-12-21 15:03:15 -0800 | [diff] [blame] | 227 | static int vexpress_sysreg_probe(struct platform_device *pdev) |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 228 | { |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 229 | struct resource *mem; |
| 230 | void __iomem *base; |
| 231 | struct bgpio_chip *mmc_gpio_chip; |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 232 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 233 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 234 | if (!mem) |
| 235 | return -EINVAL; |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 236 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 237 | base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem)); |
| 238 | if (!base) |
| 239 | return -ENOMEM; |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 240 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 241 | vexpress_config_set_master(vexpress_sysreg_get_master()); |
Pawel Moll | 8eb12b9 | 2013-06-11 11:56:02 +0100 | [diff] [blame] | 242 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 243 | /* |
| 244 | * Duplicated SYS_MCI pseudo-GPIO controller for compatibility with |
| 245 | * older trees using sysreg node for MMC control lines. |
| 246 | */ |
| 247 | mmc_gpio_chip = devm_kzalloc(&pdev->dev, sizeof(*mmc_gpio_chip), |
| 248 | GFP_KERNEL); |
| 249 | if (!mmc_gpio_chip) |
| 250 | return -ENOMEM; |
| 251 | bgpio_init(mmc_gpio_chip, &pdev->dev, 0x4, base + SYS_MCI, |
| 252 | NULL, NULL, NULL, NULL, 0); |
| 253 | mmc_gpio_chip->gc.ngpio = 2; |
| 254 | gpiochip_add(&mmc_gpio_chip->gc); |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 255 | |
Pawel Moll | 974cc7b | 2014-04-23 10:49:31 +0100 | [diff] [blame^] | 256 | return mfd_add_devices(&pdev->dev, PLATFORM_DEVID_AUTO, |
| 257 | vexpress_sysreg_cells, |
| 258 | ARRAY_SIZE(vexpress_sysreg_cells), mem, 0, NULL); |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static const struct of_device_id vexpress_sysreg_match[] = { |
| 262 | { .compatible = "arm,vexpress-sysreg", }, |
| 263 | {}, |
| 264 | }; |
| 265 | |
| 266 | static struct platform_driver vexpress_sysreg_driver = { |
| 267 | .driver = { |
| 268 | .name = "vexpress-sysreg", |
| 269 | .of_match_table = vexpress_sysreg_match, |
| 270 | }, |
| 271 | .probe = vexpress_sysreg_probe, |
| 272 | }; |
| 273 | |
| 274 | static int __init vexpress_sysreg_init(void) |
| 275 | { |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 276 | struct device_node *node; |
| 277 | |
| 278 | /* Need the sysreg early, before any other device... */ |
| 279 | for_each_matching_node(node, vexpress_sysreg_match) |
| 280 | of_platform_device_create(node, NULL, NULL); |
| 281 | |
Pawel Moll | 88e0abc | 2012-09-18 12:24:57 +0100 | [diff] [blame] | 282 | return platform_driver_register(&vexpress_sysreg_driver); |
| 283 | } |
| 284 | core_initcall(vexpress_sysreg_init); |