2 * Intel(R) Trace Hub PTI output driver
4 * Copyright (C) 2014-2015 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/sizes.h>
22 #include <linux/printk.h>
23 #include <linux/slab.h>
32 struct intel_th_device
*thdev
;
39 /* map PTI widths to MODE settings of PTI_CTL register */
40 static const unsigned int pti_mode
[] = {
41 0, 4, 8, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0,
44 static int pti_width_mode(unsigned int width
)
48 for (i
= 0; i
< ARRAY_SIZE(pti_mode
); i
++)
49 if (pti_mode
[i
] == width
)
55 static ssize_t
mode_show(struct device
*dev
, struct device_attribute
*attr
,
58 struct pti_device
*pti
= dev_get_drvdata(dev
);
60 return scnprintf(buf
, PAGE_SIZE
, "%d\n", pti_mode
[pti
->mode
]);
63 static ssize_t
mode_store(struct device
*dev
, struct device_attribute
*attr
,
64 const char *buf
, size_t size
)
66 struct pti_device
*pti
= dev_get_drvdata(dev
);
70 ret
= kstrtoul(buf
, 10, &val
);
74 ret
= pti_width_mode(val
);
83 static DEVICE_ATTR_RW(mode
);
86 freerunning_clock_show(struct device
*dev
, struct device_attribute
*attr
,
89 struct pti_device
*pti
= dev_get_drvdata(dev
);
91 return scnprintf(buf
, PAGE_SIZE
, "%d\n", pti
->freeclk
);
95 freerunning_clock_store(struct device
*dev
, struct device_attribute
*attr
,
96 const char *buf
, size_t size
)
98 struct pti_device
*pti
= dev_get_drvdata(dev
);
102 ret
= kstrtoul(buf
, 10, &val
);
106 pti
->freeclk
= !!val
;
111 static DEVICE_ATTR_RW(freerunning_clock
);
114 clock_divider_show(struct device
*dev
, struct device_attribute
*attr
,
117 struct pti_device
*pti
= dev_get_drvdata(dev
);
119 return scnprintf(buf
, PAGE_SIZE
, "%d\n", 1u << pti
->clkdiv
);
123 clock_divider_store(struct device
*dev
, struct device_attribute
*attr
,
124 const char *buf
, size_t size
)
126 struct pti_device
*pti
= dev_get_drvdata(dev
);
130 ret
= kstrtoul(buf
, 10, &val
);
134 if (!is_power_of_2(val
) || val
> 8 || !val
)
142 static DEVICE_ATTR_RW(clock_divider
);
144 static struct attribute
*pti_output_attrs
[] = {
146 &dev_attr_freerunning_clock
.attr
,
147 &dev_attr_clock_divider
.attr
,
151 static struct attribute_group pti_output_group
= {
152 .attrs
= pti_output_attrs
,
155 static int intel_th_pti_activate(struct intel_th_device
*thdev
)
157 struct pti_device
*pti
= dev_get_drvdata(&thdev
->dev
);
161 ctl
|= pti
->patgen
<< __ffs(PTI_PATGENMODE
);
164 ctl
|= pti
->mode
<< __ffs(PTI_MODE
);
165 ctl
|= pti
->clkdiv
<< __ffs(PTI_CLKDIV
);
167 iowrite32(ctl
, pti
->base
+ REG_PTI_CTL
);
169 intel_th_trace_enable(thdev
);
174 static void intel_th_pti_deactivate(struct intel_th_device
*thdev
)
176 struct pti_device
*pti
= dev_get_drvdata(&thdev
->dev
);
178 intel_th_trace_disable(thdev
);
180 iowrite32(0, pti
->base
+ REG_PTI_CTL
);
183 static void read_hw_config(struct pti_device
*pti
)
185 u32 ctl
= ioread32(pti
->base
+ REG_PTI_CTL
);
187 pti
->mode
= (ctl
& PTI_MODE
) >> __ffs(PTI_MODE
);
188 pti
->clkdiv
= (ctl
& PTI_CLKDIV
) >> __ffs(PTI_CLKDIV
);
189 pti
->freeclk
= !!(ctl
& PTI_FCEN
);
191 if (!pti_mode
[pti
->mode
])
192 pti
->mode
= pti_width_mode(4);
197 static int intel_th_pti_probe(struct intel_th_device
*thdev
)
199 struct device
*dev
= &thdev
->dev
;
200 struct resource
*res
;
201 struct pti_device
*pti
;
204 res
= intel_th_device_get_resource(thdev
, IORESOURCE_MEM
, 0);
208 base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
212 pti
= devm_kzalloc(dev
, sizeof(*pti
), GFP_KERNEL
);
221 dev_set_drvdata(dev
, pti
);
226 static void intel_th_pti_remove(struct intel_th_device
*thdev
)
230 static struct intel_th_driver intel_th_pti_driver
= {
231 .probe
= intel_th_pti_probe
,
232 .remove
= intel_th_pti_remove
,
233 .activate
= intel_th_pti_activate
,
234 .deactivate
= intel_th_pti_deactivate
,
235 .attr_group
= &pti_output_group
,
238 .owner
= THIS_MODULE
,
242 module_driver(intel_th_pti_driver
,
243 intel_th_driver_register
,
244 intel_th_driver_unregister
);
246 MODULE_LICENSE("GPL v2");
247 MODULE_DESCRIPTION("Intel(R) Trace Hub PTI output driver");
248 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");