Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | // SPDX-License-Identifier: GPL-2.0 /* * Arcx Anybus-S Controller driver * * Copyright (C) 2018 Arcx Inc */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/platform_device.h> #include <linux/gpio/consumer.h> #include <linux/io.h> #include <linux/of.h> #include <linux/delay.h> #include <linux/idr.h> #include <linux/mutex.h> #include <linux/regulator/driver.h> #include <linux/regulator/machine.h> #include <linux/regmap.h> /* move to <linux/anybuss-controller.h> when taking this out of staging */ #include "anybuss-controller.h" #define CPLD_STATUS1 0x80 #define CPLD_CONTROL 0x80 #define CPLD_CONTROL_CRST 0x40 #define CPLD_CONTROL_RST1 0x04 #define CPLD_CONTROL_RST2 0x80 #define CPLD_STATUS1_AB 0x02 #define CPLD_STATUS1_CAN_POWER 0x01 #define CPLD_DESIGN_LO 0x81 #define CPLD_DESIGN_HI 0x82 #define CPLD_CAP 0x83 #define CPLD_CAP_COMPAT 0x01 #define CPLD_CAP_SEP_RESETS 0x02 struct controller_priv { struct device *class_dev; bool common_reset; struct gpio_desc *reset_gpiod; void __iomem *cpld_base; struct mutex ctrl_lock; /* protects CONTROL register */ u8 control_reg; char version[3]; u16 design_no; }; static void do_reset(struct controller_priv *cd, u8 rst_bit, bool reset) { mutex_lock(&cd->ctrl_lock); /* * CPLD_CONTROL is write-only, so cache its value in * cd->control_reg */ if (reset) cd->control_reg &= ~rst_bit; else cd->control_reg |= rst_bit; writeb(cd->control_reg, cd->cpld_base + CPLD_CONTROL); /* * h/w work-around: * the hardware is 'too fast', so a reset followed by an immediate * not-reset will _not_ change the anybus reset line in any way, * losing the reset. to prevent this from happening, introduce * a minimum reset duration. * Verified minimum safe duration required using a scope * on 14-June-2018: 100 us. */ if (reset) usleep_range(100, 200); mutex_unlock(&cd->ctrl_lock); } static int anybuss_reset(struct controller_priv *cd, unsigned long id, bool reset) { if (id >= 2) return -EINVAL; if (cd->common_reset) do_reset(cd, CPLD_CONTROL_CRST, reset); else do_reset(cd, id ? CPLD_CONTROL_RST2 : CPLD_CONTROL_RST1, reset); return 0; } static void export_reset_0(struct device *dev, bool assert) { struct controller_priv *cd = dev_get_drvdata(dev); anybuss_reset(cd, 0, assert); } static void export_reset_1(struct device *dev, bool assert) { struct controller_priv *cd = dev_get_drvdata(dev); anybuss_reset(cd, 1, assert); } /* * parallel bus limitation: * * the anybus is 8-bit wide. we can't assume that the hardware will translate * word accesses on the parallel bus to multiple byte-accesses on the anybus. * * the imx WEIM bus does not provide this type of translation. * * to be safe, we will limit parallel bus accesses to a single byte * at a time for now. */ static const struct regmap_config arcx_regmap_cfg = { .reg_bits = 16, .val_bits = 8, .max_register = 0x7ff, .use_single_read = true, .use_single_write = true, /* * single-byte parallel bus accesses are atomic, so don't * require any synchronization. */ .disable_locking = true, }; static struct regmap *create_parallel_regmap(struct platform_device *pdev, int idx) { void __iomem *base; struct device *dev = &pdev->dev; base = devm_platform_ioremap_resource(pdev, idx + 1); if (IS_ERR(base)) return ERR_CAST(base); return devm_regmap_init_mmio(dev, base, &arcx_regmap_cfg); } static struct anybuss_host * create_anybus_host(struct platform_device *pdev, int idx) { struct anybuss_ops ops = {}; switch (idx) { case 0: ops.reset = export_reset_0; break; case 1: ops.reset = export_reset_1; break; default: return ERR_PTR(-EINVAL); } ops.host_idx = idx; ops.regmap = create_parallel_regmap(pdev, idx); if (IS_ERR(ops.regmap)) return ERR_CAST(ops.regmap); ops.irq = platform_get_irq(pdev, idx); if (ops.irq < 0) return ERR_PTR(ops.irq); return devm_anybuss_host_common_probe(&pdev->dev, &ops); } static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf) { struct controller_priv *cd = dev_get_drvdata(dev); return sprintf(buf, "%s\n", cd->version); } static DEVICE_ATTR_RO(version); static ssize_t design_number_show(struct device *dev, struct device_attribute *attr, char *buf) { struct controller_priv *cd = dev_get_drvdata(dev); return sprintf(buf, "%d\n", cd->design_no); } static DEVICE_ATTR_RO(design_number); static struct attribute *controller_attributes[] = { &dev_attr_version.attr, &dev_attr_design_number.attr, NULL, }; static const struct attribute_group controller_attribute_group = { .attrs = controller_attributes, }; static const struct attribute_group *controller_attribute_groups[] = { &controller_attribute_group, NULL, }; static void controller_device_release(struct device *dev) { kfree(dev); } static int can_power_is_enabled(struct regulator_dev *rdev) { struct controller_priv *cd = rdev_get_drvdata(rdev); return !(readb(cd->cpld_base + CPLD_STATUS1) & CPLD_STATUS1_CAN_POWER); } static const struct regulator_ops can_power_ops = { .is_enabled = can_power_is_enabled, }; static const struct regulator_desc can_power_desc = { .name = "regulator-can-power", .id = -1, .type = REGULATOR_VOLTAGE, .owner = THIS_MODULE, .ops = &can_power_ops, }; static const struct class controller_class = { .name = "arcx_anybus_controller", }; static DEFINE_IDA(controller_index_ida); static int controller_probe(struct platform_device *pdev) { struct controller_priv *cd; struct device *dev = &pdev->dev; struct regulator_config config = { }; struct regulator_dev *regulator; int err, id; struct anybuss_host *host; u8 status1, cap; cd = devm_kzalloc(dev, sizeof(*cd), GFP_KERNEL); if (!cd) return -ENOMEM; dev_set_drvdata(dev, cd); mutex_init(&cd->ctrl_lock); cd->reset_gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); if (IS_ERR(cd->reset_gpiod)) return PTR_ERR(cd->reset_gpiod); /* CPLD control memory, sits at index 0 */ cd->cpld_base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(cd->cpld_base)) { dev_err(dev, "failed to map cpld base address\n"); err = PTR_ERR(cd->cpld_base); goto out_reset; } /* identify cpld */ status1 = readb(cd->cpld_base + CPLD_STATUS1); cd->design_no = (readb(cd->cpld_base + CPLD_DESIGN_HI) << 8) | readb(cd->cpld_base + CPLD_DESIGN_LO); snprintf(cd->version, sizeof(cd->version), "%c%d", 'A' + ((status1 >> 5) & 0x7), (status1 >> 2) & 0x7); dev_info(dev, "design number %d, revision %s\n", cd->design_no, cd->version); cap = readb(cd->cpld_base + CPLD_CAP); if (!(cap & CPLD_CAP_COMPAT)) { dev_err(dev, "unsupported controller [cap=0x%02X]", cap); err = -ENODEV; goto out_reset; } if (status1 & CPLD_STATUS1_AB) { dev_info(dev, "has anybus-S slot(s)"); cd->common_reset = !(cap & CPLD_CAP_SEP_RESETS); dev_info(dev, "supports %s", cd->common_reset ? "a common reset" : "separate resets"); for (id = 0; id < 2; id++) { host = create_anybus_host(pdev, id); if (!IS_ERR(host)) continue; err = PTR_ERR(host); /* -ENODEV is fine, it just means no card detected */ if (err != -ENODEV) goto out_reset; } } id = ida_alloc(&controller_index_ida, GFP_KERNEL); if (id < 0) { err = id; goto out_reset; } /* export can power readout as a regulator */ config.dev = dev; config.driver_data = cd; regulator = devm_regulator_register(dev, &can_power_desc, &config); if (IS_ERR(regulator)) { err = PTR_ERR(regulator); goto out_ida; } /* make controller info visible to userspace */ cd->class_dev = kzalloc(sizeof(*cd->class_dev), GFP_KERNEL); if (!cd->class_dev) { err = -ENOMEM; goto out_ida; } cd->class_dev->class = &controller_class; cd->class_dev->groups = controller_attribute_groups; cd->class_dev->parent = dev; cd->class_dev->id = id; cd->class_dev->release = controller_device_release; dev_set_name(cd->class_dev, "%d", cd->class_dev->id); dev_set_drvdata(cd->class_dev, cd); err = device_register(cd->class_dev); if (err) goto out_dev; return 0; out_dev: put_device(cd->class_dev); out_ida: ida_free(&controller_index_ida, id); out_reset: gpiod_set_value_cansleep(cd->reset_gpiod, 1); return err; } static void controller_remove(struct platform_device *pdev) { struct controller_priv *cd = platform_get_drvdata(pdev); int id = cd->class_dev->id; device_unregister(cd->class_dev); ida_free(&controller_index_ida, id); gpiod_set_value_cansleep(cd->reset_gpiod, 1); } static const struct of_device_id controller_of_match[] = { { .compatible = "arcx,anybus-controller" }, { } }; MODULE_DEVICE_TABLE(of, controller_of_match); static struct platform_driver controller_driver = { .probe = controller_probe, .remove_new = controller_remove, .driver = { .name = "arcx-anybus-controller", .of_match_table = controller_of_match, }, }; static int __init controller_init(void) { int err; err = class_register(&controller_class); if (err) return err; err = platform_driver_register(&controller_driver); if (err) class_unregister(&controller_class); return err; } static void __exit controller_exit(void) { platform_driver_unregister(&controller_driver); class_unregister(&controller_class); ida_destroy(&controller_index_ida); } module_init(controller_init); module_exit(controller_exit); MODULE_DESCRIPTION("Arcx Anybus-S Controller driver"); MODULE_AUTHOR("Sven Van Asbroeck <TheSven73@gmail.com>"); MODULE_LICENSE("GPL v2"); |