blob: 625f4814df0bd03308956a5c20a72e9e43deb6c8 [file] [log] [blame]
Aparna Dasc9907512013-03-08 10:20:52 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Pratik Patel7831c082011-06-08 21:44:37 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
Pratik Patelcf418622011-09-22 11:15:11 -070015#include <linux/init.h>
16#include <linux/types.h>
17#include <linux/device.h>
Pratik Patel7831c082011-06-08 21:44:37 -070018#include <linux/platform_device.h>
19#include <linux/io.h>
20#include <linux/err.h>
Pratik Patel16aefdb2012-05-30 10:41:23 -070021#include <linux/slab.h>
Pratik Patelf17b1472012-05-25 22:23:52 -070022#include <linux/clk.h>
Pratik Patel5f6d1af2012-06-13 15:48:13 -070023#include <linux/of_coresight.h>
Pratik Patel1746b8f2012-06-02 21:11:41 -070024#include <linux/coresight.h>
Pratik Patel7831c082011-06-08 21:44:37 -070025
Pratik Patel1746b8f2012-06-02 21:11:41 -070026#include "coresight-priv.h"
Pratik Patel7831c082011-06-08 21:44:37 -070027
Pratik Patel3b0ca882012-06-01 16:54:14 -070028#define funnel_writel(drvdata, val, off) \
29 __raw_writel((val), drvdata->base + off)
30#define funnel_readl(drvdata, off) \
31 __raw_readl(drvdata->base + off)
Pratik Patel7831c082011-06-08 21:44:37 -070032
Pratik Patel3b0ca882012-06-01 16:54:14 -070033#define FUNNEL_LOCK(drvdata) \
Pratik Patel7831c082011-06-08 21:44:37 -070034do { \
35 mb(); \
Pratik Patel3b0ca882012-06-01 16:54:14 -070036 funnel_writel(drvdata, 0x0, CORESIGHT_LAR); \
Pratik Patel7831c082011-06-08 21:44:37 -070037} while (0)
Pratik Patel3b0ca882012-06-01 16:54:14 -070038#define FUNNEL_UNLOCK(drvdata) \
Pratik Patel7831c082011-06-08 21:44:37 -070039do { \
Pratik Patel3b0ca882012-06-01 16:54:14 -070040 funnel_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \
Pratik Patel7831c082011-06-08 21:44:37 -070041 mb(); \
42} while (0)
43
Pratik Patel3b0ca882012-06-01 16:54:14 -070044#define FUNNEL_FUNCTL (0x000)
45#define FUNNEL_PRICTL (0x004)
46#define FUNNEL_ITATBDATA0 (0xEEC)
47#define FUNNEL_ITATBCTR2 (0xEF0)
48#define FUNNEL_ITATBCTR1 (0xEF4)
49#define FUNNEL_ITATBCTR0 (0xEF8)
50
Pratik Patel3b0ca882012-06-01 16:54:14 -070051#define FUNNEL_HOLDTIME_MASK (0xF00)
52#define FUNNEL_HOLDTIME_SHFT (0x8)
53#define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT)
54
Pratik Patel16aefdb2012-05-30 10:41:23 -070055struct funnel_drvdata {
Pratik Patel3b0ca882012-06-01 16:54:14 -070056 void __iomem *base;
57 struct device *dev;
58 struct coresight_device *csdev;
59 struct clk *clk;
60 uint32_t priority;
Pratik Patel7831c082011-06-08 21:44:37 -070061};
62
Pratik Patel3b0ca882012-06-01 16:54:14 -070063static void __funnel_enable(struct funnel_drvdata *drvdata, int port)
Pratik Patel7831c082011-06-08 21:44:37 -070064{
65 uint32_t functl;
66
Pratik Patel3b0ca882012-06-01 16:54:14 -070067 FUNNEL_UNLOCK(drvdata);
Pratik Patel7831c082011-06-08 21:44:37 -070068
Pratik Patel3b0ca882012-06-01 16:54:14 -070069 functl = funnel_readl(drvdata, FUNNEL_FUNCTL);
Pratik Patel61de7302012-03-07 12:06:10 -080070 functl &= ~FUNNEL_HOLDTIME_MASK;
71 functl |= FUNNEL_HOLDTIME;
Pratik Patel3b0ca882012-06-01 16:54:14 -070072 functl |= (1 << port);
73 funnel_writel(drvdata, functl, FUNNEL_FUNCTL);
74 funnel_writel(drvdata, drvdata->priority, FUNNEL_PRICTL);
Pratik Patel7831c082011-06-08 21:44:37 -070075
Pratik Patel3b0ca882012-06-01 16:54:14 -070076 FUNNEL_LOCK(drvdata);
Pratik Patel7831c082011-06-08 21:44:37 -070077}
78
Pratik Patel3b0ca882012-06-01 16:54:14 -070079static int funnel_enable(struct coresight_device *csdev, int inport,
80 int outport)
Pratik Patel7831c082011-06-08 21:44:37 -070081{
Pratik Patel3b0ca882012-06-01 16:54:14 -070082 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
Pratik Patelf17b1472012-05-25 22:23:52 -070083 int ret;
84
Pratik Patel16aefdb2012-05-30 10:41:23 -070085 ret = clk_prepare_enable(drvdata->clk);
Pratik Patelf17b1472012-05-25 22:23:52 -070086 if (ret)
87 return ret;
88
Pratik Patel3b0ca882012-06-01 16:54:14 -070089 __funnel_enable(drvdata, inport);
Pratik Patelf17b1472012-05-25 22:23:52 -070090
Pratik Patel3b0ca882012-06-01 16:54:14 -070091 dev_info(drvdata->dev, "FUNNEL inport %d enabled\n", inport);
Pratik Patelf17b1472012-05-25 22:23:52 -070092 return 0;
Pratik Patel7831c082011-06-08 21:44:37 -070093}
94
Pratik Patel3b0ca882012-06-01 16:54:14 -070095static void __funnel_disable(struct funnel_drvdata *drvdata, int inport)
Pratik Patel7831c082011-06-08 21:44:37 -070096{
97 uint32_t functl;
98
Pratik Patel3b0ca882012-06-01 16:54:14 -070099 FUNNEL_UNLOCK(drvdata);
Pratik Patel7831c082011-06-08 21:44:37 -0700100
Pratik Patel3b0ca882012-06-01 16:54:14 -0700101 functl = funnel_readl(drvdata, FUNNEL_FUNCTL);
102 functl &= ~(1 << inport);
103 funnel_writel(drvdata, functl, FUNNEL_FUNCTL);
Pratik Patel7831c082011-06-08 21:44:37 -0700104
Pratik Patel3b0ca882012-06-01 16:54:14 -0700105 FUNNEL_LOCK(drvdata);
Pratik Patel7831c082011-06-08 21:44:37 -0700106}
107
Pratik Patel3b0ca882012-06-01 16:54:14 -0700108static void funnel_disable(struct coresight_device *csdev, int inport,
109 int outport)
Pratik Patel7831c082011-06-08 21:44:37 -0700110{
Pratik Patel3b0ca882012-06-01 16:54:14 -0700111 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
112
113 __funnel_disable(drvdata, inport);
Pratik Patelf17b1472012-05-25 22:23:52 -0700114
Pratik Patel16aefdb2012-05-30 10:41:23 -0700115 clk_disable_unprepare(drvdata->clk);
Pratik Patel3b0ca882012-06-01 16:54:14 -0700116
117 dev_info(drvdata->dev, "FUNNEL inport %d disabled\n", inport);
Pratik Patel7831c082011-06-08 21:44:37 -0700118}
119
Pratik Patel3b0ca882012-06-01 16:54:14 -0700120static const struct coresight_ops_link funnel_link_ops = {
121 .enable = funnel_enable,
122 .disable = funnel_disable,
123};
124
125static const struct coresight_ops funnel_cs_ops = {
126 .link_ops = &funnel_link_ops,
127};
128
Pratik Patela9c0e062012-05-28 13:45:35 -0700129static ssize_t funnel_show_priority(struct device *dev,
130 struct device_attribute *attr, char *buf)
131{
Pratik Patel3b0ca882012-06-01 16:54:14 -0700132 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
Pratik Patel16aefdb2012-05-30 10:41:23 -0700133 unsigned long val = drvdata->priority;
Pratik Patel3b0ca882012-06-01 16:54:14 -0700134
Pratik Patela9c0e062012-05-28 13:45:35 -0700135 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
136}
Pratik Patel6630ebe2012-03-06 16:44:22 -0800137
Pratik Patela9c0e062012-05-28 13:45:35 -0700138static ssize_t funnel_store_priority(struct device *dev,
139 struct device_attribute *attr,
140 const char *buf, size_t size)
Pratik Patel6630ebe2012-03-06 16:44:22 -0800141{
Pratik Patel3b0ca882012-06-01 16:54:14 -0700142 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
Pratik Patel6630ebe2012-03-06 16:44:22 -0800143 unsigned long val;
144
145 if (sscanf(buf, "%lx", &val) != 1)
146 return -EINVAL;
147
Pratik Patel16aefdb2012-05-30 10:41:23 -0700148 drvdata->priority = val;
Pratik Patela9c0e062012-05-28 13:45:35 -0700149 return size;
Pratik Patel6630ebe2012-03-06 16:44:22 -0800150}
Pratik Patela9c0e062012-05-28 13:45:35 -0700151static DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, funnel_show_priority,
152 funnel_store_priority);
Pratik Patel6630ebe2012-03-06 16:44:22 -0800153
Pratik Patel3b0ca882012-06-01 16:54:14 -0700154static struct attribute *funnel_attrs[] = {
155 &dev_attr_priority.attr,
156 NULL,
157};
Pratik Patel6630ebe2012-03-06 16:44:22 -0800158
Pratik Patel3b0ca882012-06-01 16:54:14 -0700159static struct attribute_group funnel_attr_grp = {
160 .attrs = funnel_attrs,
161};
Pratik Patel6630ebe2012-03-06 16:44:22 -0800162
Pratik Patel3b0ca882012-06-01 16:54:14 -0700163static const struct attribute_group *funnel_attr_grps[] = {
164 &funnel_attr_grp,
165 NULL,
166};
Pratik Patel6630ebe2012-03-06 16:44:22 -0800167
Pratik Patel7831c082011-06-08 21:44:37 -0700168static int __devinit funnel_probe(struct platform_device *pdev)
169{
170 int ret;
Pratik Patel4a1b2522012-06-17 15:31:15 -0700171 struct device *dev = &pdev->dev;
Pratik Patel5f6d1af2012-06-13 15:48:13 -0700172 struct coresight_platform_data *pdata;
Pratik Patel3b0ca882012-06-01 16:54:14 -0700173 struct funnel_drvdata *drvdata;
Pratik Patel7831c082011-06-08 21:44:37 -0700174 struct resource *res;
Pratik Patel3b0ca882012-06-01 16:54:14 -0700175 struct coresight_desc *desc;
Pratik Patel7831c082011-06-08 21:44:37 -0700176
Pratik Patel5f6d1af2012-06-13 15:48:13 -0700177 if (pdev->dev.of_node) {
178 pdata = of_get_coresight_platform_data(dev, pdev->dev.of_node);
179 if (IS_ERR(pdata))
180 return PTR_ERR(pdata);
181 pdev->dev.platform_data = pdata;
182 }
183
Pratik Patel4a1b2522012-06-17 15:31:15 -0700184 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
185 if (!drvdata)
186 return -ENOMEM;
Pratik Patel16aefdb2012-05-30 10:41:23 -0700187 drvdata->dev = &pdev->dev;
Pratik Patel3b0ca882012-06-01 16:54:14 -0700188 platform_set_drvdata(pdev, drvdata);
Pratik Patel783408a2012-03-21 10:28:12 -0700189
Aparna Dasc9907512013-03-08 10:20:52 -0800190 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "funnel-base");
Pratik Patel4a1b2522012-06-17 15:31:15 -0700191 if (!res)
192 return -ENODEV;
Pratik Patel2c09b762012-07-21 15:54:54 -0700193
Pratik Patel4a1b2522012-06-17 15:31:15 -0700194 drvdata->base = devm_ioremap(dev, res->start, resource_size(res));
195 if (!drvdata->base)
196 return -ENOMEM;
Pratik Patelf17b1472012-05-25 22:23:52 -0700197
Pratik Patel4a1b2522012-06-17 15:31:15 -0700198 drvdata->clk = devm_clk_get(dev, "core_clk");
199 if (IS_ERR(drvdata->clk))
200 return PTR_ERR(drvdata->clk);
Pratik Patel2c09b762012-07-21 15:54:54 -0700201
Pratik Patel6fb38342012-06-03 14:51:38 -0700202 ret = clk_set_rate(drvdata->clk, CORESIGHT_CLK_RATE_TRACE);
Pratik Patelf17b1472012-05-25 22:23:52 -0700203 if (ret)
Pratik Patel4a1b2522012-06-17 15:31:15 -0700204 return ret;
Pratik Patelf17b1472012-05-25 22:23:52 -0700205
Pratik Patel4a1b2522012-06-17 15:31:15 -0700206 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
207 if (!desc)
208 return -ENOMEM;
Pratik Patel3b0ca882012-06-01 16:54:14 -0700209 desc->type = CORESIGHT_DEV_TYPE_LINK;
210 desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
211 desc->ops = &funnel_cs_ops;
212 desc->pdata = pdev->dev.platform_data;
213 desc->dev = &pdev->dev;
214 desc->groups = funnel_attr_grps;
215 desc->owner = THIS_MODULE;
216 drvdata->csdev = coresight_register(desc);
Pratik Patel4a1b2522012-06-17 15:31:15 -0700217 if (IS_ERR(drvdata->csdev))
218 return PTR_ERR(drvdata->csdev);
Pratik Patel6630ebe2012-03-06 16:44:22 -0800219
Pratik Patel4a1b2522012-06-17 15:31:15 -0700220 dev_info(dev, "FUNNEL initialized\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700221 return 0;
Pratik Patel7831c082011-06-08 21:44:37 -0700222}
223
Pratik Patelf6fe9182012-03-20 14:04:18 -0700224static int __devexit funnel_remove(struct platform_device *pdev)
Pratik Patel7831c082011-06-08 21:44:37 -0700225{
Pratik Patel3b0ca882012-06-01 16:54:14 -0700226 struct funnel_drvdata *drvdata = platform_get_drvdata(pdev);
227
228 coresight_unregister(drvdata->csdev);
Pratik Patel7831c082011-06-08 21:44:37 -0700229 return 0;
230}
231
Pratik Patel9eae4822012-05-14 17:34:53 -0700232static struct of_device_id funnel_match[] = {
Pratik Patel5f6d1af2012-06-13 15:48:13 -0700233 {.compatible = "arm,coresight-funnel"},
Pratik Patel9eae4822012-05-14 17:34:53 -0700234 {}
235};
236
Pratik Patel7831c082011-06-08 21:44:37 -0700237static struct platform_driver funnel_driver = {
238 .probe = funnel_probe,
Pratik Patelf6fe9182012-03-20 14:04:18 -0700239 .remove = __devexit_p(funnel_remove),
Pratik Patel7831c082011-06-08 21:44:37 -0700240 .driver = {
Pratik Patel3b0ca882012-06-01 16:54:14 -0700241 .name = "coresight-funnel",
Pratik Patel9eae4822012-05-14 17:34:53 -0700242 .owner = THIS_MODULE,
243 .of_match_table = funnel_match,
Pratik Patel7831c082011-06-08 21:44:37 -0700244 },
245};
246
Pratik Patelf6fe9182012-03-20 14:04:18 -0700247static int __init funnel_init(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700248{
249 return platform_driver_register(&funnel_driver);
250}
Pratik Patelf6fe9182012-03-20 14:04:18 -0700251module_init(funnel_init);
Pratik Patel7831c082011-06-08 21:44:37 -0700252
Pratik Patelf6fe9182012-03-20 14:04:18 -0700253static void __exit funnel_exit(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700254{
255 platform_driver_unregister(&funnel_driver);
256}
Pratik Patelf6fe9182012-03-20 14:04:18 -0700257module_exit(funnel_exit);
258
259MODULE_LICENSE("GPL v2");
260MODULE_DESCRIPTION("CoreSight Funnel driver");