Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Intel(R) Trace Hub PTI output driver
0004  *
0005  * Copyright (C) 2014-2016 Intel Corporation.
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/types.h>
0011 #include <linux/module.h>
0012 #include <linux/device.h>
0013 #include <linux/sizes.h>
0014 #include <linux/printk.h>
0015 #include <linux/slab.h>
0016 #include <linux/mm.h>
0017 #include <linux/io.h>
0018 
0019 #include "intel_th.h"
0020 #include "pti.h"
0021 
0022 struct pti_device {
0023     void __iomem        *base;
0024     struct intel_th_device  *thdev;
0025     unsigned int        mode;
0026     unsigned int        freeclk;
0027     unsigned int        clkdiv;
0028     unsigned int        patgen;
0029     unsigned int        lpp_dest_mask;
0030     unsigned int        lpp_dest;
0031 };
0032 
0033 /* map PTI widths to MODE settings of PTI_CTL register */
0034 static const unsigned int pti_mode[] = {
0035     0, 4, 8, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0,
0036 };
0037 
0038 static int pti_width_mode(unsigned int width)
0039 {
0040     int i;
0041 
0042     for (i = 0; i < ARRAY_SIZE(pti_mode); i++)
0043         if (pti_mode[i] == width)
0044             return i;
0045 
0046     return -EINVAL;
0047 }
0048 
0049 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
0050              char *buf)
0051 {
0052     struct pti_device *pti = dev_get_drvdata(dev);
0053 
0054     return scnprintf(buf, PAGE_SIZE, "%d\n", pti_mode[pti->mode]);
0055 }
0056 
0057 static ssize_t mode_store(struct device *dev, struct device_attribute *attr,
0058               const char *buf, size_t size)
0059 {
0060     struct pti_device *pti = dev_get_drvdata(dev);
0061     unsigned long val;
0062     int ret;
0063 
0064     ret = kstrtoul(buf, 10, &val);
0065     if (ret)
0066         return ret;
0067 
0068     ret = pti_width_mode(val);
0069     if (ret < 0)
0070         return ret;
0071 
0072     pti->mode = ret;
0073 
0074     return size;
0075 }
0076 
0077 static DEVICE_ATTR_RW(mode);
0078 
0079 static ssize_t
0080 freerunning_clock_show(struct device *dev, struct device_attribute *attr,
0081                char *buf)
0082 {
0083     struct pti_device *pti = dev_get_drvdata(dev);
0084 
0085     return scnprintf(buf, PAGE_SIZE, "%d\n", pti->freeclk);
0086 }
0087 
0088 static ssize_t
0089 freerunning_clock_store(struct device *dev, struct device_attribute *attr,
0090             const char *buf, size_t size)
0091 {
0092     struct pti_device *pti = dev_get_drvdata(dev);
0093     unsigned long val;
0094     int ret;
0095 
0096     ret = kstrtoul(buf, 10, &val);
0097     if (ret)
0098         return ret;
0099 
0100     pti->freeclk = !!val;
0101 
0102     return size;
0103 }
0104 
0105 static DEVICE_ATTR_RW(freerunning_clock);
0106 
0107 static ssize_t
0108 clock_divider_show(struct device *dev, struct device_attribute *attr,
0109            char *buf)
0110 {
0111     struct pti_device *pti = dev_get_drvdata(dev);
0112 
0113     return scnprintf(buf, PAGE_SIZE, "%d\n", 1u << pti->clkdiv);
0114 }
0115 
0116 static ssize_t
0117 clock_divider_store(struct device *dev, struct device_attribute *attr,
0118             const char *buf, size_t size)
0119 {
0120     struct pti_device *pti = dev_get_drvdata(dev);
0121     unsigned long val;
0122     int ret;
0123 
0124     ret = kstrtoul(buf, 10, &val);
0125     if (ret)
0126         return ret;
0127 
0128     if (!is_power_of_2(val) || val > 8 || !val)
0129         return -EINVAL;
0130 
0131     pti->clkdiv = val;
0132 
0133     return size;
0134 }
0135 
0136 static DEVICE_ATTR_RW(clock_divider);
0137 
0138 static struct attribute *pti_output_attrs[] = {
0139     &dev_attr_mode.attr,
0140     &dev_attr_freerunning_clock.attr,
0141     &dev_attr_clock_divider.attr,
0142     NULL,
0143 };
0144 
0145 static const struct attribute_group pti_output_group = {
0146     .attrs  = pti_output_attrs,
0147 };
0148 
0149 static int intel_th_pti_activate(struct intel_th_device *thdev)
0150 {
0151     struct pti_device *pti = dev_get_drvdata(&thdev->dev);
0152     u32 ctl = PTI_EN;
0153 
0154     if (pti->patgen)
0155         ctl |= pti->patgen << __ffs(PTI_PATGENMODE);
0156     if (pti->freeclk)
0157         ctl |= PTI_FCEN;
0158     ctl |= pti->mode << __ffs(PTI_MODE);
0159     ctl |= pti->clkdiv << __ffs(PTI_CLKDIV);
0160     ctl |= pti->lpp_dest << __ffs(LPP_DEST);
0161 
0162     iowrite32(ctl, pti->base + REG_PTI_CTL);
0163 
0164     intel_th_trace_enable(thdev);
0165 
0166     return 0;
0167 }
0168 
0169 static void intel_th_pti_deactivate(struct intel_th_device *thdev)
0170 {
0171     struct pti_device *pti = dev_get_drvdata(&thdev->dev);
0172 
0173     intel_th_trace_disable(thdev);
0174 
0175     iowrite32(0, pti->base + REG_PTI_CTL);
0176 }
0177 
0178 static void read_hw_config(struct pti_device *pti)
0179 {
0180     u32 ctl = ioread32(pti->base + REG_PTI_CTL);
0181 
0182     pti->mode   = (ctl & PTI_MODE) >> __ffs(PTI_MODE);
0183     pti->clkdiv = (ctl & PTI_CLKDIV) >> __ffs(PTI_CLKDIV);
0184     pti->freeclk    = !!(ctl & PTI_FCEN);
0185 
0186     if (!pti_mode[pti->mode])
0187         pti->mode = pti_width_mode(4);
0188     if (!pti->clkdiv)
0189         pti->clkdiv = 1;
0190 
0191     if (pti->thdev->output.type == GTH_LPP) {
0192         if (ctl & LPP_PTIPRESENT)
0193             pti->lpp_dest_mask |= LPP_DEST_PTI;
0194         if (ctl & LPP_BSSBPRESENT)
0195             pti->lpp_dest_mask |= LPP_DEST_EXI;
0196         if (ctl & LPP_DEST)
0197             pti->lpp_dest = 1;
0198     }
0199 }
0200 
0201 static int intel_th_pti_probe(struct intel_th_device *thdev)
0202 {
0203     struct device *dev = &thdev->dev;
0204     struct resource *res;
0205     struct pti_device *pti;
0206     void __iomem *base;
0207 
0208     res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
0209     if (!res)
0210         return -ENODEV;
0211 
0212     base = devm_ioremap(dev, res->start, resource_size(res));
0213     if (!base)
0214         return -ENOMEM;
0215 
0216     pti = devm_kzalloc(dev, sizeof(*pti), GFP_KERNEL);
0217     if (!pti)
0218         return -ENOMEM;
0219 
0220     pti->thdev = thdev;
0221     pti->base = base;
0222 
0223     read_hw_config(pti);
0224 
0225     dev_set_drvdata(dev, pti);
0226 
0227     return 0;
0228 }
0229 
0230 static void intel_th_pti_remove(struct intel_th_device *thdev)
0231 {
0232 }
0233 
0234 static struct intel_th_driver intel_th_pti_driver = {
0235     .probe  = intel_th_pti_probe,
0236     .remove = intel_th_pti_remove,
0237     .activate   = intel_th_pti_activate,
0238     .deactivate = intel_th_pti_deactivate,
0239     .attr_group = &pti_output_group,
0240     .driver = {
0241         .name   = "pti",
0242         .owner  = THIS_MODULE,
0243     },
0244 };
0245 
0246 static const char * const lpp_dest_str[] = { "pti", "exi" };
0247 
0248 static ssize_t lpp_dest_show(struct device *dev, struct device_attribute *attr,
0249                  char *buf)
0250 {
0251     struct pti_device *pti = dev_get_drvdata(dev);
0252     ssize_t ret = 0;
0253     int i;
0254 
0255     for (i = ARRAY_SIZE(lpp_dest_str) - 1; i >= 0; i--) {
0256         const char *fmt = pti->lpp_dest == i ? "[%s] " : "%s ";
0257 
0258         if (!(pti->lpp_dest_mask & BIT(i)))
0259             continue;
0260 
0261         ret += scnprintf(buf + ret, PAGE_SIZE - ret,
0262                  fmt, lpp_dest_str[i]);
0263     }
0264 
0265     if (ret)
0266         buf[ret - 1] = '\n';
0267 
0268     return ret;
0269 }
0270 
0271 static ssize_t lpp_dest_store(struct device *dev, struct device_attribute *attr,
0272                   const char *buf, size_t size)
0273 {
0274     struct pti_device *pti = dev_get_drvdata(dev);
0275     int i;
0276 
0277     i = sysfs_match_string(lpp_dest_str, buf);
0278     if (i < 0)
0279         return i;
0280 
0281     if (!(pti->lpp_dest_mask & BIT(i)))
0282         return -EINVAL;
0283 
0284     pti->lpp_dest = i;
0285     return size;
0286 }
0287 
0288 static DEVICE_ATTR_RW(lpp_dest);
0289 
0290 static struct attribute *lpp_output_attrs[] = {
0291     &dev_attr_mode.attr,
0292     &dev_attr_freerunning_clock.attr,
0293     &dev_attr_clock_divider.attr,
0294     &dev_attr_lpp_dest.attr,
0295     NULL,
0296 };
0297 
0298 static const struct attribute_group lpp_output_group = {
0299     .attrs  = lpp_output_attrs,
0300 };
0301 
0302 static struct intel_th_driver intel_th_lpp_driver = {
0303     .probe      = intel_th_pti_probe,
0304     .remove     = intel_th_pti_remove,
0305     .activate   = intel_th_pti_activate,
0306     .deactivate = intel_th_pti_deactivate,
0307     .attr_group = &lpp_output_group,
0308     .driver = {
0309         .name   = "lpp",
0310         .owner  = THIS_MODULE,
0311     },
0312 };
0313 
0314 static int __init intel_th_pti_lpp_init(void)
0315 {
0316     int err;
0317 
0318     err = intel_th_driver_register(&intel_th_pti_driver);
0319     if (err)
0320         return err;
0321 
0322     err = intel_th_driver_register(&intel_th_lpp_driver);
0323     if (err) {
0324         intel_th_driver_unregister(&intel_th_pti_driver);
0325         return err;
0326     }
0327 
0328     return 0;
0329 }
0330 
0331 module_init(intel_th_pti_lpp_init);
0332 
0333 static void __exit intel_th_pti_lpp_exit(void)
0334 {
0335     intel_th_driver_unregister(&intel_th_pti_driver);
0336     intel_th_driver_unregister(&intel_th_lpp_driver);
0337 }
0338 
0339 module_exit(intel_th_pti_lpp_exit);
0340 
0341 MODULE_LICENSE("GPL v2");
0342 MODULE_DESCRIPTION("Intel(R) Trace Hub PTI/LPP output driver");
0343 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");