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