Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Arcx Anybus-S Controller driver
0004  *
0005  * Copyright (C) 2018 Arcx Inc
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/init.h>
0011 #include <linux/slab.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/gpio/consumer.h>
0014 #include <linux/io.h>
0015 #include <linux/of.h>
0016 #include <linux/delay.h>
0017 #include <linux/idr.h>
0018 #include <linux/mutex.h>
0019 #include <linux/regulator/driver.h>
0020 #include <linux/regulator/machine.h>
0021 #include <linux/regmap.h>
0022 
0023 /* move to <linux/anybuss-controller.h> when taking this out of staging */
0024 #include "anybuss-controller.h"
0025 
0026 #define CPLD_STATUS1        0x80
0027 #define CPLD_CONTROL        0x80
0028 #define CPLD_CONTROL_CRST   0x40
0029 #define CPLD_CONTROL_RST1   0x04
0030 #define CPLD_CONTROL_RST2   0x80
0031 #define CPLD_STATUS1_AB     0x02
0032 #define CPLD_STATUS1_CAN_POWER  0x01
0033 #define CPLD_DESIGN_LO      0x81
0034 #define CPLD_DESIGN_HI      0x82
0035 #define CPLD_CAP        0x83
0036 #define CPLD_CAP_COMPAT     0x01
0037 #define CPLD_CAP_SEP_RESETS 0x02
0038 
0039 struct controller_priv {
0040     struct device *class_dev;
0041     bool common_reset;
0042     struct gpio_desc *reset_gpiod;
0043     void __iomem *cpld_base;
0044     struct mutex ctrl_lock; /* protects CONTROL register */
0045     u8 control_reg;
0046     char version[3];
0047     u16 design_no;
0048 };
0049 
0050 static void do_reset(struct controller_priv *cd, u8 rst_bit, bool reset)
0051 {
0052     mutex_lock(&cd->ctrl_lock);
0053     /*
0054      * CPLD_CONTROL is write-only, so cache its value in
0055      * cd->control_reg
0056      */
0057     if (reset)
0058         cd->control_reg &= ~rst_bit;
0059     else
0060         cd->control_reg |= rst_bit;
0061     writeb(cd->control_reg, cd->cpld_base + CPLD_CONTROL);
0062     /*
0063      * h/w work-around:
0064      * the hardware is 'too fast', so a reset followed by an immediate
0065      * not-reset will _not_ change the anybus reset line in any way,
0066      * losing the reset. to prevent this from happening, introduce
0067      * a minimum reset duration.
0068      * Verified minimum safe duration required using a scope
0069      * on 14-June-2018: 100 us.
0070      */
0071     if (reset)
0072         usleep_range(100, 200);
0073     mutex_unlock(&cd->ctrl_lock);
0074 }
0075 
0076 static int anybuss_reset(struct controller_priv *cd,
0077              unsigned long id, bool reset)
0078 {
0079     if (id >= 2)
0080         return -EINVAL;
0081     if (cd->common_reset)
0082         do_reset(cd, CPLD_CONTROL_CRST, reset);
0083     else
0084         do_reset(cd, id ? CPLD_CONTROL_RST2 : CPLD_CONTROL_RST1, reset);
0085     return 0;
0086 }
0087 
0088 static void export_reset_0(struct device *dev, bool assert)
0089 {
0090     struct controller_priv *cd = dev_get_drvdata(dev);
0091 
0092     anybuss_reset(cd, 0, assert);
0093 }
0094 
0095 static void export_reset_1(struct device *dev, bool assert)
0096 {
0097     struct controller_priv *cd = dev_get_drvdata(dev);
0098 
0099     anybuss_reset(cd, 1, assert);
0100 }
0101 
0102 /*
0103  * parallel bus limitation:
0104  *
0105  * the anybus is 8-bit wide. we can't assume that the hardware will translate
0106  * word accesses on the parallel bus to multiple byte-accesses on the anybus.
0107  *
0108  * the imx WEIM bus does not provide this type of translation.
0109  *
0110  * to be safe, we will limit parallel bus accesses to a single byte
0111  * at a time for now.
0112  */
0113 
0114 static const struct regmap_config arcx_regmap_cfg = {
0115     .reg_bits = 16,
0116     .val_bits = 8,
0117     .max_register = 0x7ff,
0118     .use_single_read = true,
0119     .use_single_write = true,
0120     /*
0121      * single-byte parallel bus accesses are atomic, so don't
0122      * require any synchronization.
0123      */
0124     .disable_locking = true,
0125 };
0126 
0127 static struct regmap *create_parallel_regmap(struct platform_device *pdev,
0128                          int idx)
0129 {
0130     void __iomem *base;
0131     struct device *dev = &pdev->dev;
0132 
0133     base = devm_platform_ioremap_resource(pdev, idx + 1);
0134     if (IS_ERR(base))
0135         return ERR_CAST(base);
0136     return devm_regmap_init_mmio(dev, base, &arcx_regmap_cfg);
0137 }
0138 
0139 static struct anybuss_host *
0140 create_anybus_host(struct platform_device *pdev, int idx)
0141 {
0142     struct anybuss_ops ops = {};
0143 
0144     switch (idx) {
0145     case 0:
0146         ops.reset = export_reset_0;
0147         break;
0148     case 1:
0149         ops.reset = export_reset_1;
0150         break;
0151     default:
0152         return ERR_PTR(-EINVAL);
0153     }
0154     ops.host_idx = idx;
0155     ops.regmap = create_parallel_regmap(pdev, idx);
0156     if (IS_ERR(ops.regmap))
0157         return ERR_CAST(ops.regmap);
0158     ops.irq = platform_get_irq(pdev, idx);
0159     if (ops.irq <= 0)
0160         return ERR_PTR(-EINVAL);
0161     return devm_anybuss_host_common_probe(&pdev->dev, &ops);
0162 }
0163 
0164 static ssize_t version_show(struct device *dev,
0165                 struct device_attribute *attr, char *buf)
0166 {
0167     struct controller_priv *cd = dev_get_drvdata(dev);
0168 
0169     return sprintf(buf, "%s\n", cd->version);
0170 }
0171 static DEVICE_ATTR_RO(version);
0172 
0173 static ssize_t design_number_show(struct device *dev,
0174                   struct device_attribute *attr, char *buf)
0175 {
0176     struct controller_priv *cd = dev_get_drvdata(dev);
0177 
0178     return sprintf(buf, "%d\n", cd->design_no);
0179 }
0180 static DEVICE_ATTR_RO(design_number);
0181 
0182 static struct attribute *controller_attributes[] = {
0183     &dev_attr_version.attr,
0184     &dev_attr_design_number.attr,
0185     NULL,
0186 };
0187 
0188 static const struct attribute_group controller_attribute_group = {
0189     .attrs = controller_attributes,
0190 };
0191 
0192 static const struct attribute_group *controller_attribute_groups[] = {
0193     &controller_attribute_group,
0194     NULL,
0195 };
0196 
0197 static void controller_device_release(struct device *dev)
0198 {
0199     kfree(dev);
0200 }
0201 
0202 static int can_power_is_enabled(struct regulator_dev *rdev)
0203 {
0204     struct controller_priv *cd = rdev_get_drvdata(rdev);
0205 
0206     return !(readb(cd->cpld_base + CPLD_STATUS1) & CPLD_STATUS1_CAN_POWER);
0207 }
0208 
0209 static const struct regulator_ops can_power_ops = {
0210     .is_enabled = can_power_is_enabled,
0211 };
0212 
0213 static const struct regulator_desc can_power_desc = {
0214     .name = "regulator-can-power",
0215     .id = -1,
0216     .type = REGULATOR_VOLTAGE,
0217     .owner = THIS_MODULE,
0218     .ops = &can_power_ops,
0219 };
0220 
0221 static struct class *controller_class;
0222 static DEFINE_IDA(controller_index_ida);
0223 
0224 static int controller_probe(struct platform_device *pdev)
0225 {
0226     struct controller_priv *cd;
0227     struct device *dev = &pdev->dev;
0228     struct regulator_config config = { };
0229     struct regulator_dev *regulator;
0230     int err, id;
0231     struct anybuss_host *host;
0232     u8 status1, cap;
0233 
0234     cd = devm_kzalloc(dev, sizeof(*cd), GFP_KERNEL);
0235     if (!cd)
0236         return -ENOMEM;
0237     dev_set_drvdata(dev, cd);
0238     mutex_init(&cd->ctrl_lock);
0239     cd->reset_gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
0240     if (IS_ERR(cd->reset_gpiod))
0241         return PTR_ERR(cd->reset_gpiod);
0242 
0243     /* CPLD control memory, sits at index 0 */
0244     cd->cpld_base = devm_platform_ioremap_resource(pdev, 0);
0245     if (IS_ERR(cd->cpld_base)) {
0246         dev_err(dev,
0247             "failed to map cpld base address\n");
0248         err = PTR_ERR(cd->cpld_base);
0249         goto out_reset;
0250     }
0251 
0252     /* identify cpld */
0253     status1 = readb(cd->cpld_base + CPLD_STATUS1);
0254     cd->design_no = (readb(cd->cpld_base + CPLD_DESIGN_HI) << 8) |
0255                 readb(cd->cpld_base + CPLD_DESIGN_LO);
0256     snprintf(cd->version, sizeof(cd->version), "%c%d",
0257          'A' + ((status1 >> 5) & 0x7),
0258          (status1 >> 2) & 0x7);
0259     dev_info(dev, "design number %d, revision %s\n",
0260          cd->design_no,
0261         cd->version);
0262     cap = readb(cd->cpld_base + CPLD_CAP);
0263     if (!(cap & CPLD_CAP_COMPAT)) {
0264         dev_err(dev, "unsupported controller [cap=0x%02X]", cap);
0265         err = -ENODEV;
0266         goto out_reset;
0267     }
0268 
0269     if (status1 & CPLD_STATUS1_AB) {
0270         dev_info(dev, "has anybus-S slot(s)");
0271         cd->common_reset = !(cap & CPLD_CAP_SEP_RESETS);
0272         dev_info(dev, "supports %s", cd->common_reset ?
0273             "a common reset" : "separate resets");
0274         for (id = 0; id < 2; id++) {
0275             host = create_anybus_host(pdev, id);
0276             if (!IS_ERR(host))
0277                 continue;
0278             err = PTR_ERR(host);
0279             /* -ENODEV is fine, it just means no card detected */
0280             if (err != -ENODEV)
0281                 goto out_reset;
0282         }
0283     }
0284 
0285     id = ida_simple_get(&controller_index_ida, 0, 0, GFP_KERNEL);
0286     if (id < 0) {
0287         err = id;
0288         goto out_reset;
0289     }
0290     /* export can power readout as a regulator */
0291     config.dev = dev;
0292     config.driver_data = cd;
0293     regulator = devm_regulator_register(dev, &can_power_desc, &config);
0294     if (IS_ERR(regulator)) {
0295         err = PTR_ERR(regulator);
0296         goto out_ida;
0297     }
0298     /* make controller info visible to userspace */
0299     cd->class_dev = kzalloc(sizeof(*cd->class_dev), GFP_KERNEL);
0300     if (!cd->class_dev) {
0301         err = -ENOMEM;
0302         goto out_ida;
0303     }
0304     cd->class_dev->class = controller_class;
0305     cd->class_dev->groups = controller_attribute_groups;
0306     cd->class_dev->parent = dev;
0307     cd->class_dev->id = id;
0308     cd->class_dev->release = controller_device_release;
0309     dev_set_name(cd->class_dev, "%d", cd->class_dev->id);
0310     dev_set_drvdata(cd->class_dev, cd);
0311     err = device_register(cd->class_dev);
0312     if (err)
0313         goto out_dev;
0314     return 0;
0315 out_dev:
0316     put_device(cd->class_dev);
0317 out_ida:
0318     ida_simple_remove(&controller_index_ida, id);
0319 out_reset:
0320     gpiod_set_value_cansleep(cd->reset_gpiod, 1);
0321     return err;
0322 }
0323 
0324 static int controller_remove(struct platform_device *pdev)
0325 {
0326     struct controller_priv *cd = platform_get_drvdata(pdev);
0327     int id = cd->class_dev->id;
0328 
0329     device_unregister(cd->class_dev);
0330     ida_simple_remove(&controller_index_ida, id);
0331     gpiod_set_value_cansleep(cd->reset_gpiod, 1);
0332     return 0;
0333 }
0334 
0335 static const struct of_device_id controller_of_match[] = {
0336     { .compatible = "arcx,anybus-controller" },
0337     { }
0338 };
0339 
0340 MODULE_DEVICE_TABLE(of, controller_of_match);
0341 
0342 static struct platform_driver controller_driver = {
0343     .probe = controller_probe,
0344     .remove = controller_remove,
0345     .driver     = {
0346         .name   = "arcx-anybus-controller",
0347         .of_match_table = of_match_ptr(controller_of_match),
0348     },
0349 };
0350 
0351 static int __init controller_init(void)
0352 {
0353     int err;
0354 
0355     controller_class = class_create(THIS_MODULE, "arcx_anybus_controller");
0356     if (IS_ERR(controller_class))
0357         return PTR_ERR(controller_class);
0358     err = platform_driver_register(&controller_driver);
0359     if (err)
0360         class_destroy(controller_class);
0361 
0362     return err;
0363 }
0364 
0365 static void __exit controller_exit(void)
0366 {
0367     platform_driver_unregister(&controller_driver);
0368     class_destroy(controller_class);
0369     ida_destroy(&controller_index_ida);
0370 }
0371 
0372 module_init(controller_init);
0373 module_exit(controller_exit);
0374 
0375 MODULE_DESCRIPTION("Arcx Anybus-S Controller driver");
0376 MODULE_AUTHOR("Sven Van Asbroeck <TheSven73@gmail.com>");
0377 MODULE_LICENSE("GPL v2");