Pratik Patel | 620cf78 | 2015-05-19 10:55:21 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/amba/bus.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/coresight.h> |
| 17 | #include <linux/device.h> |
Paul Gortmaker | 5a6a7cd | 2015-06-03 16:52:57 -0400 | [diff] [blame^] | 18 | #include <linux/module.h> |
Pratik Patel | 620cf78 | 2015-05-19 10:55:21 -0600 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/pm_runtime.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | #include "coresight-priv.h" |
| 28 | |
| 29 | #define REPLICATOR_IDFILTER0 0x000 |
| 30 | #define REPLICATOR_IDFILTER1 0x004 |
| 31 | |
| 32 | /** |
| 33 | * struct replicator_state - specifics associated to a replicator component |
| 34 | * @base: memory mapped base address for this component. |
| 35 | * @dev: the device entity associated with this component |
| 36 | * @atclk: optional clock for the core parts of the replicator. |
| 37 | * @csdev: component vitals needed by the framework |
| 38 | */ |
| 39 | struct replicator_state { |
| 40 | void __iomem *base; |
| 41 | struct device *dev; |
| 42 | struct clk *atclk; |
| 43 | struct coresight_device *csdev; |
| 44 | }; |
| 45 | |
| 46 | static int replicator_enable(struct coresight_device *csdev, int inport, |
| 47 | int outport) |
| 48 | { |
| 49 | struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 50 | |
| 51 | pm_runtime_get_sync(drvdata->dev); |
| 52 | |
| 53 | CS_UNLOCK(drvdata->base); |
| 54 | |
| 55 | /* |
| 56 | * Ensure that the other port is disabled |
| 57 | * 0x00 - passing through the replicator unimpeded |
| 58 | * 0xff - disable (or impede) the flow of ATB data |
| 59 | */ |
| 60 | if (outport == 0) { |
| 61 | writel_relaxed(0x00, drvdata->base + REPLICATOR_IDFILTER0); |
| 62 | writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1); |
| 63 | } else { |
| 64 | writel_relaxed(0x00, drvdata->base + REPLICATOR_IDFILTER1); |
| 65 | writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0); |
| 66 | } |
| 67 | |
| 68 | CS_LOCK(drvdata->base); |
| 69 | |
| 70 | dev_info(drvdata->dev, "REPLICATOR enabled\n"); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static void replicator_disable(struct coresight_device *csdev, int inport, |
| 75 | int outport) |
| 76 | { |
| 77 | struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 78 | |
| 79 | CS_UNLOCK(drvdata->base); |
| 80 | |
| 81 | /* disable the flow of ATB data through port */ |
| 82 | if (outport == 0) |
| 83 | writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0); |
| 84 | else |
| 85 | writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1); |
| 86 | |
| 87 | CS_LOCK(drvdata->base); |
| 88 | |
| 89 | pm_runtime_put(drvdata->dev); |
| 90 | |
| 91 | dev_info(drvdata->dev, "REPLICATOR disabled\n"); |
| 92 | } |
| 93 | |
| 94 | static const struct coresight_ops_link replicator_link_ops = { |
| 95 | .enable = replicator_enable, |
| 96 | .disable = replicator_disable, |
| 97 | }; |
| 98 | |
| 99 | static const struct coresight_ops replicator_cs_ops = { |
| 100 | .link_ops = &replicator_link_ops, |
| 101 | }; |
| 102 | |
| 103 | static int replicator_probe(struct amba_device *adev, const struct amba_id *id) |
| 104 | { |
| 105 | int ret; |
| 106 | struct device *dev = &adev->dev; |
| 107 | struct resource *res = &adev->res; |
| 108 | struct coresight_platform_data *pdata = NULL; |
| 109 | struct replicator_state *drvdata; |
| 110 | struct coresight_desc *desc; |
| 111 | struct device_node *np = adev->dev.of_node; |
| 112 | void __iomem *base; |
| 113 | |
| 114 | if (np) { |
| 115 | pdata = of_get_coresight_platform_data(dev, np); |
| 116 | if (IS_ERR(pdata)) |
| 117 | return PTR_ERR(pdata); |
| 118 | adev->dev.platform_data = pdata; |
| 119 | } |
| 120 | |
| 121 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
| 122 | if (!drvdata) |
| 123 | return -ENOMEM; |
| 124 | |
| 125 | drvdata->dev = &adev->dev; |
| 126 | drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */ |
| 127 | if (!IS_ERR(drvdata->atclk)) { |
| 128 | ret = clk_prepare_enable(drvdata->atclk); |
| 129 | if (ret) |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | /* Validity for the resource is already checked by the AMBA core */ |
| 134 | base = devm_ioremap_resource(dev, res); |
| 135 | if (IS_ERR(base)) |
| 136 | return PTR_ERR(base); |
| 137 | |
| 138 | drvdata->base = base; |
| 139 | dev_set_drvdata(dev, drvdata); |
| 140 | pm_runtime_put(&adev->dev); |
| 141 | |
| 142 | desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); |
| 143 | if (!desc) |
| 144 | return -ENOMEM; |
| 145 | |
| 146 | desc->type = CORESIGHT_DEV_TYPE_LINK; |
| 147 | desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT; |
| 148 | desc->ops = &replicator_cs_ops; |
| 149 | desc->pdata = adev->dev.platform_data; |
| 150 | desc->dev = &adev->dev; |
| 151 | drvdata->csdev = coresight_register(desc); |
| 152 | if (IS_ERR(drvdata->csdev)) |
| 153 | return PTR_ERR(drvdata->csdev); |
| 154 | |
| 155 | dev_info(dev, "%s initialized\n", (char *)id->data); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int replicator_remove(struct amba_device *adev) |
| 160 | { |
| 161 | struct replicator_state *drvdata = amba_get_drvdata(adev); |
| 162 | |
| 163 | pm_runtime_disable(&adev->dev); |
| 164 | coresight_unregister(drvdata->csdev); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | #ifdef CONFIG_PM |
| 169 | static int replicator_runtime_suspend(struct device *dev) |
| 170 | { |
| 171 | struct replicator_state *drvdata = dev_get_drvdata(dev); |
| 172 | |
| 173 | if (drvdata && !IS_ERR(drvdata->atclk)) |
| 174 | clk_disable_unprepare(drvdata->atclk); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int replicator_runtime_resume(struct device *dev) |
| 180 | { |
| 181 | struct replicator_state *drvdata = dev_get_drvdata(dev); |
| 182 | |
| 183 | if (drvdata && !IS_ERR(drvdata->atclk)) |
| 184 | clk_prepare_enable(drvdata->atclk); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | static const struct dev_pm_ops replicator_dev_pm_ops = { |
| 191 | SET_RUNTIME_PM_OPS(replicator_runtime_suspend, |
| 192 | replicator_runtime_resume, |
| 193 | NULL) |
| 194 | }; |
| 195 | |
| 196 | static struct amba_id replicator_ids[] = { |
| 197 | { |
| 198 | .id = 0x0003b909, |
| 199 | .mask = 0x0003ffff, |
| 200 | .data = "REPLICATOR 1.0", |
| 201 | }, |
| 202 | { 0, 0 }, |
| 203 | }; |
| 204 | |
| 205 | static struct amba_driver replicator_driver = { |
| 206 | .drv = { |
| 207 | .name = "coresight-replicator-qcom", |
| 208 | .pm = &replicator_dev_pm_ops, |
| 209 | }, |
| 210 | .probe = replicator_probe, |
| 211 | .remove = replicator_remove, |
| 212 | .id_table = replicator_ids, |
| 213 | }; |
| 214 | |
| 215 | module_amba_driver(replicator_driver); |