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> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/miscdevice.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/delay.h> |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 26 | #include <linux/spinlock.h> |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 27 | #include <linux/clk.h> |
Pratik Patel | 1746b8f | 2012-06-02 21:11:41 -0700 | [diff] [blame] | 28 | #include <linux/coresight.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 29 | |
Pratik Patel | 1746b8f | 2012-06-02 21:11:41 -0700 | [diff] [blame] | 30 | #include "coresight-priv.h" |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 31 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 32 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 33 | #define etb_writel(drvdata, val, off) __raw_writel((val), drvdata->base + off) |
| 34 | #define etb_readl(drvdata, off) __raw_readl(drvdata->base + off) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 35 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 36 | #define ETB_LOCK(drvdata) \ |
| 37 | do { \ |
| 38 | mb(); \ |
| 39 | etb_writel(drvdata, 0x0, CORESIGHT_LAR); \ |
| 40 | } while (0) |
| 41 | #define ETB_UNLOCK(drvdata) \ |
| 42 | do { \ |
| 43 | etb_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \ |
| 44 | mb(); \ |
| 45 | } while (0) |
| 46 | |
| 47 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 48 | #define ETB_RAM_DEPTH_REG (0x004) |
| 49 | #define ETB_STATUS_REG (0x00C) |
| 50 | #define ETB_RAM_READ_DATA_REG (0x010) |
| 51 | #define ETB_RAM_READ_POINTER (0x014) |
| 52 | #define ETB_RAM_WRITE_POINTER (0x018) |
| 53 | #define ETB_TRG (0x01C) |
| 54 | #define ETB_CTL_REG (0x020) |
| 55 | #define ETB_RWD_REG (0x024) |
| 56 | #define ETB_FFSR (0x300) |
| 57 | #define ETB_FFCR (0x304) |
| 58 | #define ETB_ITMISCOP0 (0xEE0) |
| 59 | #define ETB_ITTRFLINACK (0xEE4) |
| 60 | #define ETB_ITTRFLIN (0xEE8) |
| 61 | #define ETB_ITATBDATA0 (0xEEC) |
| 62 | #define ETB_ITATBCTR2 (0xEF0) |
| 63 | #define ETB_ITATBCTR1 (0xEF4) |
| 64 | #define ETB_ITATBCTR0 (0xEF8) |
| 65 | |
| 66 | |
| 67 | #define BYTES_PER_WORD 4 |
| 68 | #define ETB_SIZE_WORDS 4096 |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 69 | #define FRAME_SIZE_WORDS 4 |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 70 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 71 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 72 | struct etb_drvdata { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 73 | void __iomem *base; |
| 74 | struct device *dev; |
| 75 | struct coresight_device *csdev; |
| 76 | struct miscdevice miscdev; |
| 77 | struct clk *clk; |
| 78 | spinlock_t spinlock; |
| 79 | bool reading; |
| 80 | atomic_t in_use; |
| 81 | uint8_t *buf; |
| 82 | bool enable; |
| 83 | uint32_t trigger_cntr; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 86 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 87 | static void __etb_enable(struct etb_drvdata *drvdata) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 88 | { |
| 89 | int i; |
| 90 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 91 | ETB_UNLOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 92 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 93 | etb_writel(drvdata, 0x0, ETB_RAM_WRITE_POINTER); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 94 | for (i = 0; i < ETB_SIZE_WORDS; i++) |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 95 | etb_writel(drvdata, 0x0, ETB_RWD_REG); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 96 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 97 | etb_writel(drvdata, 0x0, ETB_RAM_WRITE_POINTER); |
| 98 | etb_writel(drvdata, 0x0, ETB_RAM_READ_POINTER); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 99 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 100 | etb_writel(drvdata, drvdata->trigger_cntr, ETB_TRG); |
| 101 | etb_writel(drvdata, BIT(13) | BIT(0), ETB_FFCR); |
| 102 | etb_writel(drvdata, BIT(0), ETB_CTL_REG); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 103 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 104 | ETB_LOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 107 | static int etb_enable(struct coresight_device *csdev) |
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 | struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 110 | int ret; |
Pratik Patel | aa83fa7 | 2012-05-03 12:46:47 -0700 | [diff] [blame] | 111 | unsigned long flags; |
| 112 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 113 | ret = clk_prepare_enable(drvdata->clk); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 114 | if (ret) |
| 115 | return ret; |
| 116 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 117 | spin_lock_irqsave(&drvdata->spinlock, flags); |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 118 | __etb_enable(drvdata); |
| 119 | drvdata->enable = true; |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 120 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 121 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 122 | dev_info(drvdata->dev, "ETB enabled\n"); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 123 | return 0; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 126 | static void __etb_disable(struct etb_drvdata *drvdata) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 127 | { |
| 128 | int count; |
Pratik Patel | 6a32d26 | 2012-01-20 15:21:31 -0800 | [diff] [blame] | 129 | uint32_t ffcr; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 130 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 131 | ETB_UNLOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 132 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 133 | ffcr = etb_readl(drvdata, ETB_FFCR); |
Pratik Patel | 6a32d26 | 2012-01-20 15:21:31 -0800 | [diff] [blame] | 134 | ffcr |= (BIT(12) | BIT(6)); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 135 | etb_writel(drvdata, ffcr, ETB_FFCR); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 136 | for (count = TIMEOUT_US; BVAL(etb_readl(drvdata, ETB_FFCR), 6) != 0 |
Pratik Patel | 6a32d26 | 2012-01-20 15:21:31 -0800 | [diff] [blame] | 137 | && count > 0; count--) |
| 138 | udelay(1); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 139 | WARN(count == 0, "timeout while flushing DRVDATA, ETB_FFCR: %#x\n", |
| 140 | etb_readl(drvdata, ETB_FFCR)); |
Pratik Patel | 6a32d26 | 2012-01-20 15:21:31 -0800 | [diff] [blame] | 141 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 142 | etb_writel(drvdata, 0x0, ETB_CTL_REG); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 143 | for (count = TIMEOUT_US; BVAL(etb_readl(drvdata, ETB_FFSR), 1) != 1 |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 144 | && count > 0; count--) |
| 145 | udelay(1); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 146 | WARN(count == 0, "timeout while disabling DRVDATA, ETB_FFSR: %#x\n", |
| 147 | etb_readl(drvdata, ETB_FFSR)); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 148 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 149 | ETB_LOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 152 | static void __etb_dump(struct etb_drvdata *drvdata) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 153 | { |
| 154 | int i; |
| 155 | uint8_t *buf_ptr; |
| 156 | uint32_t read_data; |
| 157 | uint32_t read_ptr; |
| 158 | uint32_t write_ptr; |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 159 | uint32_t frame_off; |
| 160 | uint32_t frame_endoff; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 161 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 162 | ETB_UNLOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 163 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 164 | read_ptr = etb_readl(drvdata, ETB_RAM_READ_POINTER); |
| 165 | write_ptr = etb_readl(drvdata, ETB_RAM_WRITE_POINTER); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 166 | |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 167 | frame_off = write_ptr % FRAME_SIZE_WORDS; |
| 168 | frame_endoff = FRAME_SIZE_WORDS - frame_off; |
| 169 | if (frame_off) { |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 170 | dev_err(drvdata->dev, "write_ptr: %lu not aligned to formatter " |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 171 | "frame size\n", (unsigned long)write_ptr); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 172 | dev_err(drvdata->dev, "frameoff: %lu, frame_endoff: %lu\n", |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 173 | (unsigned long)frame_off, (unsigned long)frame_endoff); |
| 174 | write_ptr += frame_endoff; |
| 175 | } |
| 176 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 177 | if ((etb_readl(drvdata, ETB_STATUS_REG) & BIT(0)) == 0) |
| 178 | etb_writel(drvdata, 0x0, ETB_RAM_READ_POINTER); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 179 | else |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 180 | etb_writel(drvdata, write_ptr, ETB_RAM_READ_POINTER); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 181 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 182 | buf_ptr = drvdata->buf; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 183 | for (i = 0; i < ETB_SIZE_WORDS; i++) { |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 184 | read_data = etb_readl(drvdata, ETB_RAM_READ_DATA_REG); |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 185 | *buf_ptr++ = read_data >> 0; |
| 186 | *buf_ptr++ = read_data >> 8; |
| 187 | *buf_ptr++ = read_data >> 16; |
| 188 | *buf_ptr++ = read_data >> 24; |
| 189 | } |
| 190 | |
| 191 | if (frame_off) { |
| 192 | buf_ptr -= (frame_endoff * BYTES_PER_WORD); |
| 193 | for (i = 0; i < frame_endoff; i++) { |
| 194 | *buf_ptr++ = 0x0; |
| 195 | *buf_ptr++ = 0x0; |
| 196 | *buf_ptr++ = 0x0; |
| 197 | *buf_ptr++ = 0x0; |
| 198 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 201 | etb_writel(drvdata, read_ptr, ETB_RAM_READ_POINTER); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 202 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 203 | ETB_LOCK(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 206 | static void etb_disable(struct coresight_device *csdev) |
| 207 | { |
| 208 | struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 209 | unsigned long flags; |
| 210 | |
| 211 | spin_lock_irqsave(&drvdata->spinlock, flags); |
| 212 | __etb_disable(drvdata); |
| 213 | __etb_dump(drvdata); |
| 214 | drvdata->enable = false; |
| 215 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 216 | |
| 217 | clk_disable_unprepare(drvdata->clk); |
| 218 | |
| 219 | dev_info(drvdata->dev, "ETB disabled\n"); |
| 220 | } |
| 221 | |
| 222 | static const struct coresight_ops_sink etb_sink_ops = { |
| 223 | .enable = etb_enable, |
| 224 | .disable = etb_disable, |
| 225 | }; |
| 226 | |
| 227 | static const struct coresight_ops etb_cs_ops = { |
| 228 | .sink_ops = &etb_sink_ops, |
| 229 | }; |
| 230 | |
| 231 | static void etb_dump(struct etb_drvdata *drvdata) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 232 | { |
Pratik Patel | aa83fa7 | 2012-05-03 12:46:47 -0700 | [diff] [blame] | 233 | unsigned long flags; |
| 234 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 235 | spin_lock_irqsave(&drvdata->spinlock, flags); |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 236 | if (drvdata->enable) { |
| 237 | __etb_disable(drvdata); |
| 238 | __etb_dump(drvdata); |
| 239 | __etb_enable(drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 240 | } |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 241 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 242 | |
| 243 | dev_info(drvdata->dev, "ETB dumped\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static int etb_open(struct inode *inode, struct file *file) |
| 247 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 248 | struct etb_drvdata *drvdata = container_of(file->private_data, |
| 249 | struct etb_drvdata, miscdev); |
| 250 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 251 | if (atomic_cmpxchg(&drvdata->in_use, 0, 1)) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 252 | return -EBUSY; |
| 253 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 254 | dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static ssize_t etb_read(struct file *file, char __user *data, |
| 259 | size_t len, loff_t *ppos) |
| 260 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 261 | struct etb_drvdata *drvdata = container_of(file->private_data, |
| 262 | struct etb_drvdata, miscdev); |
| 263 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 264 | if (drvdata->reading == false) { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 265 | etb_dump(drvdata); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 266 | drvdata->reading = true; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if (*ppos + len > ETB_SIZE_WORDS * BYTES_PER_WORD) |
| 270 | len = ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos; |
| 271 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 272 | if (copy_to_user(data, drvdata->buf + *ppos, len)) { |
| 273 | dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 274 | return -EFAULT; |
| 275 | } |
| 276 | |
| 277 | *ppos += len; |
| 278 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 279 | dev_dbg(drvdata->dev, "%s: %d bytes copied, %d bytes left\n", |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 280 | __func__, len, (int) (ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos)); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 281 | return len; |
| 282 | } |
| 283 | |
| 284 | static int etb_release(struct inode *inode, struct file *file) |
| 285 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 286 | struct etb_drvdata *drvdata = container_of(file->private_data, |
| 287 | struct etb_drvdata, miscdev); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 288 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 289 | drvdata->reading = false; |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 290 | atomic_set(&drvdata->in_use, 0); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 291 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 292 | dev_dbg(drvdata->dev, "%s: released\n", __func__); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static const struct file_operations etb_fops = { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 297 | .owner = THIS_MODULE, |
| 298 | .open = etb_open, |
| 299 | .read = etb_read, |
| 300 | .release = etb_release, |
| 301 | .llseek = no_llseek, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 304 | static ssize_t etb_show_trigger_cntr(struct device *dev, |
| 305 | struct device_attribute *attr, char *buf) |
| 306 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 307 | struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent); |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 308 | unsigned long val = drvdata->trigger_cntr; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 309 | |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 310 | return scnprintf(buf, PAGE_SIZE, "%#lx\n", val); |
| 311 | } |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 312 | |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 313 | static ssize_t etb_store_trigger_cntr(struct device *dev, |
| 314 | struct device_attribute *attr, |
| 315 | const char *buf, size_t size) |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 316 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 317 | struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 318 | unsigned long val; |
| 319 | |
| 320 | if (sscanf(buf, "%lx", &val) != 1) |
| 321 | return -EINVAL; |
| 322 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 323 | drvdata->trigger_cntr = val; |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 324 | return size; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 325 | } |
Pratik Patel | a9c0e06 | 2012-05-28 13:45:35 -0700 | [diff] [blame] | 326 | static DEVICE_ATTR(trigger_cntr, S_IRUGO | S_IWUSR, etb_show_trigger_cntr, |
| 327 | etb_store_trigger_cntr); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 328 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 329 | static struct attribute *etb_attrs[] = { |
| 330 | &dev_attr_trigger_cntr.attr, |
| 331 | NULL, |
| 332 | }; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 333 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 334 | static struct attribute_group etb_attr_grp = { |
| 335 | .attrs = etb_attrs, |
| 336 | }; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 337 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 338 | static const struct attribute_group *etb_attr_grps[] = { |
| 339 | &etb_attr_grp, |
| 340 | NULL, |
| 341 | }; |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 342 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 343 | static int __devinit etb_probe(struct platform_device *pdev) |
| 344 | { |
| 345 | int ret; |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 346 | struct device *dev = &pdev->dev; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 347 | struct etb_drvdata *drvdata; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 348 | struct resource *res; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 349 | struct coresight_desc *desc; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 350 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 351 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
| 352 | if (!drvdata) |
| 353 | return -ENOMEM; |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 354 | drvdata->dev = &pdev->dev; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 355 | platform_set_drvdata(pdev, drvdata); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 356 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 357 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 358 | if (!res) |
| 359 | return -ENODEV; |
| 360 | drvdata->base = devm_ioremap(dev, res->start, resource_size(res)); |
| 361 | if (!drvdata->base) |
| 362 | return -ENOMEM; |
| 363 | |
Pratik Patel | 16aefdb | 2012-05-30 10:41:23 -0700 | [diff] [blame] | 364 | spin_lock_init(&drvdata->spinlock); |
Pratik Patel | 9dc7022 | 2012-03-07 12:09:13 -0800 | [diff] [blame] | 365 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 366 | drvdata->clk = devm_clk_get(dev, "core_clk"); |
| 367 | if (IS_ERR(drvdata->clk)) |
| 368 | return PTR_ERR(drvdata->clk); |
Pratik Patel | 6fb3834 | 2012-06-03 14:51:38 -0700 | [diff] [blame] | 369 | ret = clk_set_rate(drvdata->clk, CORESIGHT_CLK_RATE_TRACE); |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 370 | if (ret) |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 371 | return ret; |
Pratik Patel | f17b147 | 2012-05-25 22:23:52 -0700 | [diff] [blame] | 372 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 373 | drvdata->buf = devm_kzalloc(dev, ETB_SIZE_WORDS * BYTES_PER_WORD, |
| 374 | GFP_KERNEL); |
| 375 | if (!drvdata->buf) |
| 376 | return -ENOMEM; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 377 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 378 | desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); |
| 379 | if (!desc) |
| 380 | return -ENOMEM; |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 381 | desc->type = CORESIGHT_DEV_TYPE_SINK; |
| 382 | desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER; |
| 383 | desc->ops = &etb_cs_ops; |
| 384 | desc->pdata = pdev->dev.platform_data; |
| 385 | desc->dev = &pdev->dev; |
| 386 | desc->groups = etb_attr_grps; |
| 387 | desc->owner = THIS_MODULE; |
| 388 | drvdata->csdev = coresight_register(desc); |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 389 | if (IS_ERR(drvdata->csdev)) |
| 390 | return PTR_ERR(drvdata->csdev); |
Pratik Patel | 6630ebe | 2012-03-06 16:44:22 -0800 | [diff] [blame] | 391 | |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 392 | drvdata->miscdev.name = ((struct coresight_platform_data *) |
| 393 | (pdev->dev.platform_data))->name; |
| 394 | drvdata->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 395 | drvdata->miscdev.fops = &etb_fops; |
| 396 | ret = misc_register(&drvdata->miscdev); |
| 397 | if (ret) |
| 398 | goto err; |
| 399 | |
| 400 | dev_info(dev, "ETB initialized\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 401 | return 0; |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 402 | err: |
| 403 | coresight_unregister(drvdata->csdev); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 404 | return ret; |
| 405 | } |
| 406 | |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 407 | static int __devexit etb_remove(struct platform_device *pdev) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 408 | { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 409 | struct etb_drvdata *drvdata = platform_get_drvdata(pdev); |
| 410 | |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 411 | misc_deregister(&drvdata->miscdev); |
Pratik Patel | 4a1b252 | 2012-06-17 15:31:15 -0700 | [diff] [blame^] | 412 | coresight_unregister(drvdata->csdev); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Pratik Patel | 9eae482 | 2012-05-14 17:34:53 -0700 | [diff] [blame] | 416 | static struct of_device_id etb_match[] = { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 417 | {.compatible = "coresight-etb"}, |
Pratik Patel | 9eae482 | 2012-05-14 17:34:53 -0700 | [diff] [blame] | 418 | {} |
| 419 | }; |
| 420 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 421 | static struct platform_driver etb_driver = { |
| 422 | .probe = etb_probe, |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 423 | .remove = __devexit_p(etb_remove), |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 424 | .driver = { |
Pratik Patel | 3b0ca88 | 2012-06-01 16:54:14 -0700 | [diff] [blame] | 425 | .name = "coresight-etb", |
Pratik Patel | 9eae482 | 2012-05-14 17:34:53 -0700 | [diff] [blame] | 426 | .owner = THIS_MODULE, |
| 427 | .of_match_table = etb_match, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 428 | }, |
| 429 | }; |
| 430 | |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 431 | static int __init etb_init(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 432 | { |
| 433 | return platform_driver_register(&etb_driver); |
| 434 | } |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 435 | module_init(etb_init); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 436 | |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 437 | static void __exit etb_exit(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 438 | { |
| 439 | platform_driver_unregister(&etb_driver); |
| 440 | } |
Pratik Patel | f6fe918 | 2012-03-20 14:04:18 -0700 | [diff] [blame] | 441 | module_exit(etb_exit); |
| 442 | |
| 443 | MODULE_LICENSE("GPL v2"); |
| 444 | MODULE_DESCRIPTION("CoreSight Embedded Trace Buffer driver"); |