Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 2 | * |
| 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 Patel | cf41862 | 2011-09-22 11:15:11 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/err.h> |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 21 | #include <linux/slab.h> |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 22 | #include <linux/clk.h> |
Pratik Patel | 1746b8f | 2012-06-02 21:11:41 -0700 | [diff] [blame] | 23 | #include <linux/coresight.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 24 | |
Pratik Patel | 1746b8f | 2012-06-02 21:11:41 -0700 | [diff] [blame] | 25 | #include "coresight-priv.h" |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 26 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 27 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 28 | #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 Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 32 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 33 | #define FUNNEL_LOCK(drvdata) \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 34 | do { \ |
| 35 | mb(); \ |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 36 | funnel_writel(drvdata, 0x0, CORESIGHT_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 37 | } while (0) |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 38 | #define FUNNEL_UNLOCK(drvdata) \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 39 | do { \ |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 40 | funnel_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 41 | mb(); \ |
| 42 | } while (0) |
| 43 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 44 | |
| 45 | #define FUNNEL_FUNCTL (0x000) |
| 46 | #define FUNNEL_PRICTL (0x004) |
| 47 | #define FUNNEL_ITATBDATA0 (0xEEC) |
| 48 | #define FUNNEL_ITATBCTR2 (0xEF0) |
| 49 | #define FUNNEL_ITATBCTR1 (0xEF4) |
| 50 | #define FUNNEL_ITATBCTR0 (0xEF8) |
| 51 | |
| 52 | |
| 53 | #define FUNNEL_HOLDTIME_MASK (0xF00) |
| 54 | #define FUNNEL_HOLDTIME_SHFT (0x8) |
| 55 | #define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT) |
| 56 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 57 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 58 | struct funnel_drvdata { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 59 | void __iomem *base; |
| 60 | struct device *dev; |
| 61 | struct coresight_device *csdev; |
| 62 | struct clk *clk; |
| 63 | uint32_t priority; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 66 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 67 | static void __funnel_enable(struct funnel_drvdata *drvdata, int port) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 68 | { |
| 69 | uint32_t functl; |
| 70 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 71 | FUNNEL_UNLOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 72 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 73 | functl = funnel_readl(drvdata, FUNNEL_FUNCTL); |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame] | 74 | functl &= ~FUNNEL_HOLDTIME_MASK; |
| 75 | functl |= FUNNEL_HOLDTIME; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 76 | functl |= (1 << port); |
| 77 | funnel_writel(drvdata, functl, FUNNEL_FUNCTL); |
| 78 | funnel_writel(drvdata, drvdata->priority, FUNNEL_PRICTL); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 79 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 80 | FUNNEL_LOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 83 | static int funnel_enable(struct coresight_device *csdev, int inport, |
| 84 | int outport) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 85 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 86 | struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 87 | int ret; |
| 88 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 89 | ret = clk_prepare_enable(drvdata->clk); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 90 | if (ret) |
| 91 | return ret; |
| 92 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 93 | __funnel_enable(drvdata, inport); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 94 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 95 | dev_info(drvdata->dev, "FUNNEL inport %d enabled\n", inport); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 96 | return 0; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 99 | static void __funnel_disable(struct funnel_drvdata *drvdata, int inport) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 100 | { |
| 101 | uint32_t functl; |
| 102 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 103 | FUNNEL_UNLOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 104 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 105 | functl = funnel_readl(drvdata, FUNNEL_FUNCTL); |
| 106 | functl &= ~(1 << inport); |
| 107 | funnel_writel(drvdata, functl, FUNNEL_FUNCTL); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 108 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 109 | FUNNEL_LOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 112 | static void funnel_disable(struct coresight_device *csdev, int inport, |
| 113 | int outport) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 114 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 115 | struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 116 | |
| 117 | __funnel_disable(drvdata, inport); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 118 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 119 | clk_disable_unprepare(drvdata->clk); |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 120 | |
| 121 | dev_info(drvdata->dev, "FUNNEL inport %d disabled\n", inport); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 124 | static const struct coresight_ops_link funnel_link_ops = { |
| 125 | .enable = funnel_enable, |
| 126 | .disable = funnel_disable, |
| 127 | }; |
| 128 | |
| 129 | static const struct coresight_ops funnel_cs_ops = { |
| 130 | .link_ops = &funnel_link_ops, |
| 131 | }; |
| 132 | |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 133 | static ssize_t funnel_show_priority(struct device *dev, |
| 134 | struct device_attribute *attr, char *buf) |
| 135 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 136 | struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 137 | unsigned long val = drvdata->priority; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 138 | |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 139 | return scnprintf(buf, PAGE_SIZE, "%#lx\n", val); |
| 140 | } |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 141 | |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 142 | static ssize_t funnel_store_priority(struct device *dev, |
| 143 | struct device_attribute *attr, |
| 144 | const char *buf, size_t size) |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 145 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 146 | struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 147 | unsigned long val; |
| 148 | |
| 149 | if (sscanf(buf, "%lx", &val) != 1) |
| 150 | return -EINVAL; |
| 151 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 152 | drvdata->priority = val; |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 153 | return size; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 154 | } |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 155 | static DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, funnel_show_priority, |
| 156 | funnel_store_priority); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 157 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 158 | static struct attribute *funnel_attrs[] = { |
| 159 | &dev_attr_priority.attr, |
| 160 | NULL, |
| 161 | }; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 162 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 163 | static struct attribute_group funnel_attr_grp = { |
| 164 | .attrs = funnel_attrs, |
| 165 | }; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 166 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 167 | static const struct attribute_group *funnel_attr_grps[] = { |
| 168 | &funnel_attr_grp, |
| 169 | NULL, |
| 170 | }; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 171 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 172 | static int __devinit funnel_probe(struct platform_device *pdev) |
| 173 | { |
| 174 | int ret; |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame] | 175 | struct device *dev = &pdev->dev; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 176 | struct funnel_drvdata *drvdata; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 177 | struct resource *res; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 178 | struct coresight_desc *desc; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 179 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame] | 180 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
| 181 | if (!drvdata) |
| 182 | return -ENOMEM; |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 183 | drvdata->dev = &pdev->dev; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 184 | platform_set_drvdata(pdev, drvdata); |
Pratik Patel | 783408a | 2012-03-21 10:28:12 -0700 | [diff] [blame] | 185 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame] | 186 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 187 | if (!res) |
| 188 | return -ENODEV; |
| 189 | drvdata->base = devm_ioremap(dev, res->start, resource_size(res)); |
| 190 | if (!drvdata->base) |
| 191 | return -ENOMEM; |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 192 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame] | 193 | drvdata->clk = devm_clk_get(dev, "core_clk"); |
| 194 | if (IS_ERR(drvdata->clk)) |
| 195 | return PTR_ERR(drvdata->clk); |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 196 | ret = clk_set_rate(drvdata->clk, CORESIGHT_CLK_RATE_TRACE); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 197 | if (ret) |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame] | 198 | return ret; |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 199 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame] | 200 | desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); |
| 201 | if (!desc) |
| 202 | return -ENOMEM; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 203 | desc->type = CORESIGHT_DEV_TYPE_LINK; |
| 204 | desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG; |
| 205 | desc->ops = &funnel_cs_ops; |
| 206 | desc->pdata = pdev->dev.platform_data; |
| 207 | desc->dev = &pdev->dev; |
| 208 | desc->groups = funnel_attr_grps; |
| 209 | desc->owner = THIS_MODULE; |
| 210 | drvdata->csdev = coresight_register(desc); |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame] | 211 | if (IS_ERR(drvdata->csdev)) |
| 212 | return PTR_ERR(drvdata->csdev); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 213 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame] | 214 | dev_info(dev, "FUNNEL initialized\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 215 | return 0; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 218 | static int __devexit funnel_remove(struct platform_device *pdev) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 219 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 220 | struct funnel_drvdata *drvdata = platform_get_drvdata(pdev); |
| 221 | |
| 222 | coresight_unregister(drvdata->csdev); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Pratik Patel | 9eae482 | 2012-05-14 17:34:53 -0700 | [diff] [blame] | 226 | static struct of_device_id funnel_match[] = { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 227 | {.compatible = "coresight-funnel"}, |
Pratik Patel | 9eae482 | 2012-05-14 17:34:53 -0700 | [diff] [blame] | 228 | {} |
| 229 | }; |
| 230 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 231 | static struct platform_driver funnel_driver = { |
| 232 | .probe = funnel_probe, |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 233 | .remove = __devexit_p(funnel_remove), |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 234 | .driver = { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 235 | .name = "coresight-funnel", |
Pratik Patel | 9eae482 | 2012-05-14 17:34:53 -0700 | [diff] [blame] | 236 | .owner = THIS_MODULE, |
| 237 | .of_match_table = funnel_match, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 238 | }, |
| 239 | }; |
| 240 | |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 241 | static int __init funnel_init(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 242 | { |
| 243 | return platform_driver_register(&funnel_driver); |
| 244 | } |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 245 | module_init(funnel_init); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 246 | |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 247 | static void __exit funnel_exit(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 248 | { |
| 249 | platform_driver_unregister(&funnel_driver); |
| 250 | } |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 251 | module_exit(funnel_exit); |
| 252 | |
| 253 | MODULE_LICENSE("GPL v2"); |
| 254 | MODULE_DESCRIPTION("CoreSight Funnel driver"); |