Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
| 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 | #include <linux/module.h> |
| 13 | #include <linux/bitops.h> |
| 14 | #include <linux/cdev.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/iopoll.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/uaccess.h> |
| 23 | #include <soc/qcom/memory_dump.h> |
| 24 | #include <soc/qcom/scm.h> |
| 25 | |
| 26 | #define TIMEOUT_US (100) |
| 27 | |
| 28 | #define BM(lsb, msb) ((BIT(msb) - BIT(lsb)) + BIT(msb)) |
| 29 | #define BMVAL(val, lsb, msb) ((val & BM(lsb, msb)) >> lsb) |
| 30 | #define BVAL(val, n) ((val & BIT(n)) >> n) |
| 31 | |
| 32 | #define dcc_writel(drvdata, val, off) \ |
| 33 | __raw_writel((val), drvdata->base + off) |
| 34 | #define dcc_readl(drvdata, off) \ |
| 35 | __raw_readl(drvdata->base + off) |
| 36 | |
| 37 | #define dcc_sram_writel(drvdata, val, off) \ |
| 38 | __raw_writel((val), drvdata->ram_base + off) |
| 39 | #define dcc_sram_readl(drvdata, off) \ |
| 40 | __raw_readl(drvdata->ram_base + off) |
| 41 | |
| 42 | #define HLOS_LIST_START 1 |
| 43 | |
| 44 | /* DCC registers */ |
| 45 | #define DCC_HW_VERSION (0x00) |
| 46 | #define DCC_HW_INFO (0x04) |
| 47 | #define DCC_EXEC_CTRL (0x08) |
| 48 | #define DCC_STATUS (0x0C) |
| 49 | #define DCC_CFG (0x10) |
| 50 | #define DCC_FDA_CURR (0x14) |
| 51 | #define DCC_LLA_CURR (0x18) |
| 52 | #define DCC_LL_LOCK(m) (0x1C + 0x80 * (m + HLOS_LIST_START)) |
| 53 | #define DCC_LL_CFG(m) (0x20 + 0x80 * (m + HLOS_LIST_START)) |
| 54 | #define DCC_LL_BASE(m) (0x24 + 0x80 * (m + HLOS_LIST_START)) |
| 55 | #define DCC_FD_BASE(m) (0x28 + 0x80 * (m + HLOS_LIST_START)) |
| 56 | #define DCC_LL_TIMEOUT(m) (0x2c + 0x80 * (m + HLOS_LIST_START)) |
| 57 | #define DCC_LL_INT_ENABLE(m) (0x30 + 0x80 * (m + HLOS_LIST_START)) |
| 58 | #define DCC_LL_INT_STATUS(m) (0x34 + 0x80 * (m + HLOS_LIST_START)) |
| 59 | #define DCC_FDA_CAPTURED(m) (0x38 + 0x80 * (m + HLOS_LIST_START)) |
| 60 | #define DCC_LLA_CAPTURED(m) (0x3C + 0x80 * (m + HLOS_LIST_START)) |
| 61 | #define DCC_LL_CRC_CAPTURED(m) (0x40 + 0x80 * (m + HLOS_LIST_START)) |
| 62 | #define DCC_LL_SW_TRIGGER(m) (0x44 + 0x80 * (m + HLOS_LIST_START)) |
| 63 | #define DCC_LL_BUS_ACCESS_STATUS(m) (0x48 + 0x80 * (m + HLOS_LIST_START)) |
| 64 | |
| 65 | #define DCC_REG_DUMP_MAGIC_V2 (0x42445953) |
| 66 | #define DCC_REG_DUMP_VER (1) |
| 67 | |
| 68 | #define MAX_DCC_OFFSET (0xFF * 4) |
| 69 | #define MAX_DCC_LEN 0x7F |
| 70 | #define MAX_LOOP_CNT 0xFF |
| 71 | |
| 72 | #define DCC_ADDR_DESCRIPTOR 0x00 |
| 73 | #define DCC_LOOP_DESCRIPTOR (BIT(30)) |
| 74 | #define DCC_RD_MOD_WR_DESCRIPTOR (BIT(31)) |
| 75 | #define DCC_LINK_DESCRIPTOR (BIT(31) | BIT(30)) |
| 76 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 77 | #define DCC_READ_IND 0x00 |
| 78 | #define DCC_WRITE_IND (BIT(28)) |
| 79 | |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 80 | #define DCC_AHB_IND 0x00 |
| 81 | #define DCC_APB_IND BIT(29) |
| 82 | |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 83 | #define DCC_MAX_LINK_LIST 5 |
| 84 | #define DCC_INVALID_LINK_LIST 0xFF |
| 85 | |
| 86 | enum dcc_func_type { |
| 87 | DCC_FUNC_TYPE_CAPTURE, |
| 88 | DCC_FUNC_TYPE_CRC, |
| 89 | }; |
| 90 | |
| 91 | static const char * const str_dcc_func_type[] = { |
| 92 | [DCC_FUNC_TYPE_CAPTURE] = "cap", |
| 93 | [DCC_FUNC_TYPE_CRC] = "crc", |
| 94 | }; |
| 95 | |
| 96 | enum dcc_data_sink { |
| 97 | DCC_DATA_SINK_SRAM, |
| 98 | DCC_DATA_SINK_ATB |
| 99 | }; |
| 100 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 101 | enum dcc_descriptor_type { |
| 102 | DCC_ADDR_TYPE, |
| 103 | DCC_LOOP_TYPE, |
| 104 | DCC_READ_WRITE_TYPE, |
| 105 | DCC_WRITE_TYPE |
| 106 | }; |
| 107 | |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 108 | static const char * const str_dcc_data_sink[] = { |
| 109 | [DCC_DATA_SINK_SRAM] = "sram", |
| 110 | [DCC_DATA_SINK_ATB] = "atb", |
| 111 | }; |
| 112 | |
| 113 | struct rpm_trig_req { |
| 114 | uint32_t enable; |
| 115 | uint32_t reserved; |
| 116 | }; |
| 117 | |
| 118 | struct dcc_config_entry { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 119 | uint32_t base; |
| 120 | uint32_t offset; |
| 121 | uint32_t len; |
| 122 | uint32_t index; |
| 123 | uint32_t loop_cnt; |
| 124 | uint32_t write_val; |
| 125 | uint32_t mask; |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 126 | bool apb_bus; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 127 | enum dcc_descriptor_type desc_type; |
| 128 | struct list_head list; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | struct dcc_drvdata { |
| 132 | void __iomem *base; |
| 133 | uint32_t reg_size; |
| 134 | struct device *dev; |
| 135 | struct mutex mutex; |
| 136 | void __iomem *ram_base; |
| 137 | uint32_t ram_size; |
Satyajit Desai | 3d86e1b | 2017-04-13 17:11:09 -0700 | [diff] [blame] | 138 | uint32_t ram_offset; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 139 | enum dcc_data_sink data_sink; |
| 140 | enum dcc_func_type func_type[DCC_MAX_LINK_LIST]; |
| 141 | uint32_t ram_cfg; |
| 142 | uint32_t ram_start; |
| 143 | bool enable[DCC_MAX_LINK_LIST]; |
| 144 | bool configured[DCC_MAX_LINK_LIST]; |
| 145 | bool interrupt_disable; |
| 146 | char *sram_node; |
| 147 | struct cdev sram_dev; |
| 148 | struct class *sram_class; |
| 149 | struct list_head cfg_head[DCC_MAX_LINK_LIST]; |
| 150 | uint32_t nr_config[DCC_MAX_LINK_LIST]; |
| 151 | void *reg_buf; |
| 152 | struct msm_dump_data reg_data; |
| 153 | bool save_reg; |
| 154 | void *sram_buf; |
| 155 | struct msm_dump_data sram_data; |
| 156 | uint8_t curr_list; |
Satyajit Desai | fe1311b | 2017-05-19 16:02:35 -0700 | [diff] [blame] | 157 | uint8_t cti_trig; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static bool dcc_ready(struct dcc_drvdata *drvdata) |
| 161 | { |
| 162 | uint32_t val; |
| 163 | |
| 164 | /* poll until DCC ready */ |
| 165 | if (!readl_poll_timeout((drvdata->base + DCC_STATUS), val, |
| 166 | (BMVAL(val, 0, 1) == 0), 1, TIMEOUT_US)) |
| 167 | return true; |
| 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | static int dcc_read_status(struct dcc_drvdata *drvdata) |
| 173 | { |
| 174 | int curr_list; |
| 175 | uint32_t bus_status; |
| 176 | |
| 177 | for (curr_list = 0; curr_list < DCC_MAX_LINK_LIST; curr_list++) { |
| 178 | if (!drvdata->enable[curr_list]) |
| 179 | continue; |
| 180 | |
| 181 | bus_status = dcc_readl(drvdata, |
| 182 | DCC_LL_BUS_ACCESS_STATUS(curr_list)); |
| 183 | |
| 184 | if (bus_status) { |
| 185 | |
| 186 | dev_err(drvdata->dev, |
| 187 | "Read access error for list %d err: 0x%x", |
| 188 | curr_list, bus_status); |
| 189 | |
| 190 | dcc_writel(drvdata, 0x3, |
| 191 | DCC_LL_BUS_ACCESS_STATUS(curr_list)); |
| 192 | return -ENODATA; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int dcc_sw_trigger(struct dcc_drvdata *drvdata) |
| 200 | { |
| 201 | int ret = 0; |
| 202 | int curr_list; |
| 203 | |
| 204 | mutex_lock(&drvdata->mutex); |
| 205 | |
| 206 | if (!dcc_ready(drvdata)) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 207 | dev_err(drvdata->dev, "DCC is not ready\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 208 | ret = -EBUSY; |
| 209 | goto err; |
| 210 | } |
| 211 | |
| 212 | for (curr_list = 0; curr_list < DCC_MAX_LINK_LIST; curr_list++) { |
| 213 | if (!drvdata->enable[curr_list]) |
| 214 | continue; |
| 215 | |
| 216 | dcc_writel(drvdata, 1, DCC_LL_SW_TRIGGER(curr_list)); |
| 217 | } |
| 218 | |
| 219 | if (!dcc_ready(drvdata)) { |
| 220 | dev_err(drvdata->dev, |
| 221 | "DCC is busy after receiving sw tigger.\n"); |
| 222 | ret = -EBUSY; |
| 223 | goto err; |
| 224 | } |
| 225 | |
| 226 | ret = dcc_read_status(drvdata); |
| 227 | |
| 228 | err: |
| 229 | mutex_unlock(&drvdata->mutex); |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static int __dcc_ll_cfg(struct dcc_drvdata *drvdata, int curr_list) |
| 234 | { |
| 235 | int ret = 0; |
| 236 | uint32_t sram_offset = drvdata->ram_cfg * 4; |
| 237 | uint32_t prev_addr, addr; |
| 238 | uint32_t prev_off = 0, off; |
| 239 | uint32_t loop_off = 0; |
| 240 | uint32_t link; |
| 241 | uint32_t pos, total_len = 0, loop_len = 0; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 242 | uint32_t loop, loop_cnt = 0; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 243 | bool loop_start = false; |
| 244 | struct dcc_config_entry *entry; |
| 245 | |
| 246 | prev_addr = 0; |
| 247 | addr = 0; |
| 248 | link = 0; |
| 249 | |
| 250 | list_for_each_entry(entry, &drvdata->cfg_head[curr_list], list) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 251 | switch (entry->desc_type) { |
| 252 | case DCC_READ_WRITE_TYPE: |
| 253 | { |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 254 | if (link) { |
| 255 | /* write new offset = 1 to continue |
| 256 | * processing the list |
| 257 | */ |
| 258 | link |= ((0x1 << 8) & BM(8, 14)); |
| 259 | dcc_sram_writel(drvdata, link, sram_offset); |
| 260 | sram_offset += 4; |
| 261 | /* Reset link and prev_off */ |
| 262 | addr = 0x00; |
| 263 | link = 0; |
| 264 | prev_off = 0; |
| 265 | prev_addr = addr; |
| 266 | } |
| 267 | |
| 268 | addr = DCC_RD_MOD_WR_DESCRIPTOR; |
| 269 | dcc_sram_writel(drvdata, addr, sram_offset); |
| 270 | sram_offset += 4; |
| 271 | |
| 272 | dcc_sram_writel(drvdata, entry->mask, sram_offset); |
| 273 | sram_offset += 4; |
| 274 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 275 | dcc_sram_writel(drvdata, entry->write_val, sram_offset); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 276 | sram_offset += 4; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 277 | addr = 0; |
| 278 | break; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 281 | case DCC_LOOP_TYPE: |
| 282 | { |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 283 | /* Check if we need to write link of prev entry */ |
| 284 | if (link) { |
| 285 | dcc_sram_writel(drvdata, link, sram_offset); |
| 286 | sram_offset += 4; |
| 287 | } |
| 288 | |
| 289 | if (loop_start) { |
| 290 | loop = (sram_offset - loop_off) / 4; |
| 291 | loop |= (loop_cnt << 13) & BM(13, 27); |
| 292 | loop |= DCC_LOOP_DESCRIPTOR; |
| 293 | total_len += (total_len - loop_len) * loop_cnt; |
| 294 | |
| 295 | dcc_sram_writel(drvdata, loop, sram_offset); |
| 296 | sram_offset += 4; |
| 297 | |
| 298 | loop_start = false; |
| 299 | loop_len = 0; |
| 300 | loop_off = 0; |
| 301 | } else { |
| 302 | loop_start = true; |
| 303 | loop_cnt = entry->loop_cnt - 1; |
| 304 | loop_len = total_len; |
| 305 | loop_off = sram_offset; |
| 306 | } |
| 307 | |
| 308 | /* Reset link and prev_off */ |
| 309 | addr = 0x00; |
| 310 | link = 0; |
| 311 | prev_off = 0; |
| 312 | prev_addr = addr; |
| 313 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 314 | break; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 317 | case DCC_WRITE_TYPE: |
| 318 | { |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 319 | if (link) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 320 | /* write new offset = 1 to continue |
| 321 | * processing the list |
| 322 | */ |
| 323 | link |= ((0x1 << 8) & BM(8, 14)); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 324 | dcc_sram_writel(drvdata, link, sram_offset); |
| 325 | sram_offset += 4; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 326 | /* Reset link and prev_off */ |
| 327 | addr = 0x00; |
| 328 | prev_off = 0; |
| 329 | prev_addr = addr; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 330 | } |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 331 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 332 | off = entry->offset/4; |
| 333 | /* write new offset-length pair to correct position */ |
| 334 | link |= ((off & BM(0, 7)) | BIT(15) | |
| 335 | ((entry->len << 8) & BM(8, 14))); |
| 336 | link |= DCC_LINK_DESCRIPTOR; |
| 337 | |
| 338 | /* Address type */ |
| 339 | addr = (entry->base >> 4) & BM(0, 27); |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 340 | if (entry->apb_bus) |
| 341 | addr |= DCC_ADDR_DESCRIPTOR | DCC_WRITE_IND |
| 342 | | DCC_APB_IND; |
| 343 | else |
| 344 | addr |= DCC_ADDR_DESCRIPTOR | DCC_WRITE_IND |
| 345 | | DCC_AHB_IND; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 346 | |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 347 | dcc_sram_writel(drvdata, addr, sram_offset); |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 348 | sram_offset += 4; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 349 | |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 350 | dcc_sram_writel(drvdata, link, sram_offset); |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 351 | sram_offset += 4; |
| 352 | |
| 353 | dcc_sram_writel(drvdata, entry->write_val, sram_offset); |
| 354 | sram_offset += 4; |
| 355 | addr = 0x00; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 356 | link = 0; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 357 | break; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 360 | default: |
| 361 | { |
| 362 | /* Address type */ |
| 363 | addr = (entry->base >> 4) & BM(0, 27); |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 364 | if (entry->apb_bus) |
| 365 | addr |= DCC_ADDR_DESCRIPTOR | DCC_READ_IND |
| 366 | | DCC_APB_IND; |
| 367 | else |
| 368 | addr |= DCC_ADDR_DESCRIPTOR | DCC_READ_IND |
| 369 | | DCC_AHB_IND; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 370 | |
| 371 | off = entry->offset/4; |
| 372 | total_len += entry->len * 4; |
| 373 | |
| 374 | if (!prev_addr || prev_addr != addr || prev_off > off) { |
| 375 | /* Check if we need to write prev link entry */ |
| 376 | if (link) { |
| 377 | dcc_sram_writel(drvdata, |
| 378 | link, sram_offset); |
| 379 | sram_offset += 4; |
| 380 | } |
| 381 | dev_dbg(drvdata->dev, |
| 382 | "DCC: sram address 0x%x\n", |
| 383 | sram_offset); |
| 384 | |
| 385 | /* Write address */ |
| 386 | dcc_sram_writel(drvdata, addr, sram_offset); |
| 387 | sram_offset += 4; |
| 388 | |
| 389 | /* Reset link and prev_off */ |
| 390 | link = 0; |
| 391 | prev_off = 0; |
| 392 | } |
| 393 | |
| 394 | if ((off - prev_off) > 0xFF || |
| 395 | entry->len > MAX_DCC_LEN) { |
| 396 | dev_err(drvdata->dev, |
| 397 | "DCC: Progamming error Base: 0x%x, offset 0x%x\n", |
| 398 | entry->base, entry->offset); |
| 399 | ret = -EINVAL; |
| 400 | goto err; |
| 401 | } |
| 402 | |
| 403 | if (link) { |
| 404 | /* |
| 405 | * link already has one offset-length so new |
| 406 | * offset-length needs to be placed at |
| 407 | * bits [29:15] |
| 408 | */ |
| 409 | pos = 15; |
| 410 | |
| 411 | /* Clear bits [31:16] */ |
| 412 | link &= BM(0, 14); |
| 413 | } else { |
| 414 | /* |
| 415 | * link is empty, so new offset-length needs |
| 416 | * to be placed at bits [15:0] |
| 417 | */ |
| 418 | pos = 0; |
| 419 | link = 1 << 15; |
| 420 | } |
| 421 | |
| 422 | /* write new offset-length pair to correct position */ |
| 423 | link |= (((off-prev_off) & BM(0, 7)) | |
| 424 | ((entry->len << 8) & BM(8, 14))) << pos; |
| 425 | |
| 426 | link |= DCC_LINK_DESCRIPTOR; |
| 427 | |
| 428 | if (pos) { |
| 429 | dcc_sram_writel(drvdata, link, sram_offset); |
| 430 | sram_offset += 4; |
| 431 | link = 0; |
| 432 | } |
| 433 | |
Satyajit Desai | 6d001d5 | 2017-06-16 15:16:16 -0700 | [diff] [blame] | 434 | prev_off = off + entry->len - 1; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 435 | prev_addr = addr; |
| 436 | } |
| 437 | } |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (link) { |
| 441 | dcc_sram_writel(drvdata, link, sram_offset); |
| 442 | sram_offset += 4; |
| 443 | } |
| 444 | |
| 445 | if (loop_start) { |
| 446 | dev_err(drvdata->dev, |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 447 | "DCC: Progamming error: Loop unterminated\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 448 | ret = -EINVAL; |
| 449 | goto err; |
| 450 | } |
| 451 | |
| 452 | /* Handling special case of list ending with a rd_mod_wr */ |
| 453 | if (addr == DCC_RD_MOD_WR_DESCRIPTOR) { |
| 454 | addr = (0xC105E) & BM(0, 27); |
| 455 | addr |= DCC_ADDR_DESCRIPTOR; |
| 456 | |
| 457 | dcc_sram_writel(drvdata, addr, sram_offset); |
| 458 | sram_offset += 4; |
| 459 | } |
| 460 | |
| 461 | /* Setting zero to indicate end of the list */ |
| 462 | link = DCC_LINK_DESCRIPTOR; |
| 463 | dcc_sram_writel(drvdata, link, sram_offset); |
| 464 | sram_offset += 4; |
| 465 | |
| 466 | /* Update ram_cfg and check if the data will overstep */ |
| 467 | if (drvdata->data_sink == DCC_DATA_SINK_SRAM && |
| 468 | drvdata->func_type[curr_list] == DCC_FUNC_TYPE_CAPTURE) { |
| 469 | drvdata->ram_cfg = (sram_offset + total_len) / 4; |
| 470 | |
| 471 | if (sram_offset + total_len > drvdata->ram_size) { |
| 472 | sram_offset += total_len; |
| 473 | goto overstep; |
| 474 | } |
| 475 | } else { |
| 476 | drvdata->ram_cfg = sram_offset / 4; |
| 477 | |
| 478 | if (sram_offset > drvdata->ram_size) |
| 479 | goto overstep; |
| 480 | } |
| 481 | |
| 482 | drvdata->ram_start = sram_offset/4; |
| 483 | return 0; |
| 484 | overstep: |
| 485 | ret = -EINVAL; |
| 486 | memset_io(drvdata->ram_base, 0, drvdata->ram_size); |
| 487 | dev_err(drvdata->dev, "DCC SRAM oversteps, 0x%x (0x%x)\n", |
| 488 | sram_offset, drvdata->ram_size); |
| 489 | err: |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | static void __dcc_reg_dump(struct dcc_drvdata *drvdata) |
| 494 | { |
| 495 | uint32_t *reg_buf; |
| 496 | uint8_t i = 0; |
| 497 | uint8_t j; |
| 498 | |
| 499 | if (!drvdata->reg_buf) |
| 500 | return; |
| 501 | |
| 502 | drvdata->reg_data.version = DCC_REG_DUMP_VER; |
| 503 | |
| 504 | reg_buf = drvdata->reg_buf; |
| 505 | |
| 506 | reg_buf[i++] = dcc_readl(drvdata, DCC_HW_VERSION); |
| 507 | reg_buf[i++] = dcc_readl(drvdata, DCC_HW_INFO); |
| 508 | reg_buf[i++] = dcc_readl(drvdata, DCC_EXEC_CTRL); |
| 509 | reg_buf[i++] = dcc_readl(drvdata, DCC_STATUS); |
| 510 | reg_buf[i++] = dcc_readl(drvdata, DCC_CFG); |
| 511 | reg_buf[i++] = dcc_readl(drvdata, DCC_FDA_CURR); |
| 512 | reg_buf[i++] = dcc_readl(drvdata, DCC_LLA_CURR); |
| 513 | |
| 514 | for (j = 0; j < DCC_MAX_LINK_LIST; j++) |
| 515 | reg_buf[i++] = dcc_readl(drvdata, DCC_LL_LOCK(j)); |
| 516 | for (j = 0; j < DCC_MAX_LINK_LIST; j++) |
| 517 | reg_buf[i++] = dcc_readl(drvdata, DCC_LL_CFG(j)); |
| 518 | for (j = 0; j < DCC_MAX_LINK_LIST; j++) |
| 519 | reg_buf[i++] = dcc_readl(drvdata, DCC_LL_BASE(j)); |
| 520 | for (j = 0; j < DCC_MAX_LINK_LIST; j++) |
| 521 | reg_buf[i++] = dcc_readl(drvdata, DCC_FD_BASE(j)); |
| 522 | |
| 523 | drvdata->reg_data.magic = DCC_REG_DUMP_MAGIC_V2; |
| 524 | } |
| 525 | |
| 526 | static void __dcc_first_crc(struct dcc_drvdata *drvdata) |
| 527 | { |
| 528 | int i; |
| 529 | |
| 530 | /* |
| 531 | * Need to send 2 triggers to DCC. First trigger sets CRC error status |
| 532 | * bit. So need second trigger to reset this bit. |
| 533 | */ |
| 534 | for (i = 0; i < 2; i++) { |
| 535 | if (!dcc_ready(drvdata)) |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 536 | dev_err(drvdata->dev, "DCC is not ready\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 537 | |
| 538 | dcc_writel(drvdata, 1, |
| 539 | DCC_LL_SW_TRIGGER(drvdata->curr_list)); |
| 540 | } |
| 541 | |
| 542 | /* Clear CRC error interrupt */ |
| 543 | dcc_writel(drvdata, BIT(1), |
| 544 | DCC_LL_INT_STATUS(drvdata->curr_list)); |
| 545 | } |
| 546 | |
| 547 | static int dcc_valid_list(struct dcc_drvdata *drvdata, int curr_list) |
| 548 | { |
| 549 | uint32_t lock_reg; |
| 550 | |
| 551 | if (list_empty(&drvdata->cfg_head[curr_list])) |
| 552 | return -EINVAL; |
| 553 | |
| 554 | if (drvdata->enable[curr_list]) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 555 | dev_err(drvdata->dev, "DCC is already enabled\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 556 | return -EINVAL; |
| 557 | } |
| 558 | |
| 559 | lock_reg = dcc_readl(drvdata, DCC_LL_LOCK(curr_list)); |
| 560 | if (lock_reg & 0x1) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 561 | dev_err(drvdata->dev, "DCC is already enabled\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 562 | return -EINVAL; |
| 563 | } |
| 564 | |
| 565 | dev_err(drvdata->dev, "DCC list passed %d\n", curr_list); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int dcc_enable(struct dcc_drvdata *drvdata) |
| 570 | { |
| 571 | int ret = 0; |
| 572 | int list; |
| 573 | uint32_t ram_cfg_base; |
| 574 | |
| 575 | mutex_lock(&drvdata->mutex); |
| 576 | |
| 577 | memset_io(drvdata->ram_base, 0, drvdata->ram_size); |
| 578 | |
| 579 | for (list = 0; list < DCC_MAX_LINK_LIST; list++) { |
| 580 | |
| 581 | if (dcc_valid_list(drvdata, list)) |
| 582 | continue; |
| 583 | |
| 584 | /* 1. Take ownership of the list */ |
| 585 | dcc_writel(drvdata, BIT(0), DCC_LL_LOCK(list)); |
| 586 | |
| 587 | /* 2. Program linked-list in the SRAM */ |
| 588 | ram_cfg_base = drvdata->ram_cfg; |
| 589 | ret = __dcc_ll_cfg(drvdata, list); |
| 590 | if (ret) { |
| 591 | dev_info(drvdata->dev, "DCC ram programming failed\n"); |
| 592 | goto err; |
| 593 | } |
| 594 | |
| 595 | /* 3. If in capture mode program DCC_RAM_CFG reg */ |
| 596 | if (drvdata->func_type[list] == DCC_FUNC_TYPE_CAPTURE) { |
Satyajit Desai | 3d86e1b | 2017-04-13 17:11:09 -0700 | [diff] [blame] | 597 | dcc_writel(drvdata, ram_cfg_base + |
| 598 | drvdata->ram_offset/4, DCC_LL_BASE(list)); |
| 599 | dcc_writel(drvdata, drvdata->ram_start + |
| 600 | drvdata->ram_offset/4, DCC_FD_BASE(list)); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 601 | dcc_writel(drvdata, 0, DCC_LL_TIMEOUT(list)); |
| 602 | } |
| 603 | |
Satyajit Desai | dc56a3f | 2017-05-19 12:19:15 -0700 | [diff] [blame] | 604 | /* 4. Configure trigger, data sink and function type */ |
Satyajit Desai | fe1311b | 2017-05-19 16:02:35 -0700 | [diff] [blame] | 605 | dcc_writel(drvdata, BIT(9) | ((drvdata->cti_trig << 8) | |
| 606 | (drvdata->data_sink << 4) | |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 607 | (drvdata->func_type[list])), DCC_LL_CFG(list)); |
| 608 | |
| 609 | /* 5. Clears interrupt status register */ |
| 610 | dcc_writel(drvdata, 0, DCC_LL_INT_ENABLE(list)); |
| 611 | dcc_writel(drvdata, (BIT(0) | BIT(1) | BIT(2)), |
| 612 | DCC_LL_INT_STATUS(list)); |
| 613 | |
| 614 | dev_info(drvdata->dev, "All values written to enable"); |
| 615 | /* Make sure all config is written in sram */ |
| 616 | mb(); |
| 617 | |
| 618 | drvdata->enable[list] = 1; |
| 619 | |
| 620 | if (drvdata->func_type[list] == DCC_FUNC_TYPE_CRC) { |
| 621 | __dcc_first_crc(drvdata); |
| 622 | |
| 623 | /* Enable CRC error interrupt */ |
| 624 | if (!drvdata->interrupt_disable) |
| 625 | dcc_writel(drvdata, BIT(1), |
| 626 | DCC_LL_INT_ENABLE(list)); |
| 627 | } |
| 628 | } |
| 629 | /* Save DCC registers */ |
| 630 | if (drvdata->save_reg) |
| 631 | __dcc_reg_dump(drvdata); |
| 632 | |
| 633 | err: |
| 634 | mutex_unlock(&drvdata->mutex); |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static void dcc_disable(struct dcc_drvdata *drvdata) |
| 639 | { |
| 640 | int curr_list; |
| 641 | |
| 642 | mutex_lock(&drvdata->mutex); |
| 643 | |
| 644 | if (!dcc_ready(drvdata)) |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 645 | dev_err(drvdata->dev, "DCC is not ready Disabling DCC...\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 646 | |
| 647 | for (curr_list = 0; curr_list < DCC_MAX_LINK_LIST; curr_list++) { |
| 648 | if (!drvdata->enable[curr_list]) |
| 649 | continue; |
| 650 | |
| 651 | dcc_writel(drvdata, 0, DCC_LL_LOCK(curr_list)); |
| 652 | drvdata->enable[curr_list] = 0; |
| 653 | } |
| 654 | drvdata->ram_cfg = 0; |
| 655 | drvdata->ram_start = 0; |
| 656 | /* Save DCC registers */ |
| 657 | if (drvdata->save_reg) |
| 658 | __dcc_reg_dump(drvdata); |
| 659 | |
| 660 | mutex_unlock(&drvdata->mutex); |
| 661 | } |
| 662 | |
| 663 | static ssize_t dcc_curr_list(struct device *dev, |
| 664 | struct device_attribute *attr, |
| 665 | const char *buf, size_t size) |
| 666 | { |
| 667 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 668 | unsigned long val; |
| 669 | uint32_t lock_reg; |
| 670 | |
| 671 | if (kstrtoul(buf, 16, &val)) |
| 672 | return -EINVAL; |
| 673 | |
| 674 | if (val >= DCC_MAX_LINK_LIST) |
| 675 | return -EINVAL; |
| 676 | |
| 677 | mutex_lock(&drvdata->mutex); |
| 678 | lock_reg = dcc_readl(drvdata, DCC_LL_LOCK(val)); |
| 679 | if (lock_reg & 0x1) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 680 | dev_err(drvdata->dev, "DCC linked list is already configured\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 681 | mutex_unlock(&drvdata->mutex); |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | drvdata->curr_list = val; |
| 685 | mutex_unlock(&drvdata->mutex); |
| 686 | |
| 687 | return size; |
| 688 | } |
| 689 | static DEVICE_ATTR(curr_list, 0200, |
| 690 | NULL, dcc_curr_list); |
| 691 | |
| 692 | static ssize_t dcc_show_func_type(struct device *dev, |
| 693 | struct device_attribute *attr, char *buf) |
| 694 | { |
| 695 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 696 | ssize_t len = 0; |
| 697 | unsigned int i; |
| 698 | |
| 699 | for (i = 0; i < DCC_MAX_LINK_LIST; i++) |
| 700 | len += scnprintf(buf + len, PAGE_SIZE - len, "%u :%s\n", |
| 701 | i, str_dcc_func_type[drvdata->func_type[i]]); |
| 702 | |
| 703 | return len; |
| 704 | } |
| 705 | |
| 706 | static ssize_t dcc_store_func_type(struct device *dev, |
| 707 | struct device_attribute *attr, |
| 708 | const char *buf, size_t size) |
| 709 | { |
| 710 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 711 | char str[10] = ""; |
| 712 | int ret; |
| 713 | |
| 714 | if (strlen(buf) >= 10) |
| 715 | return -EINVAL; |
| 716 | if (sscanf(buf, "%s", str) != 1) |
| 717 | return -EINVAL; |
| 718 | |
| 719 | if (drvdata->curr_list >= DCC_MAX_LINK_LIST) { |
| 720 | dev_err(dev, |
| 721 | "Select link list to program using curr_list\n"); |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | mutex_lock(&drvdata->mutex); |
| 726 | if (drvdata->enable[drvdata->curr_list]) { |
| 727 | ret = -EBUSY; |
| 728 | goto out; |
| 729 | } |
| 730 | |
| 731 | if (!strcmp(str, str_dcc_func_type[DCC_FUNC_TYPE_CAPTURE])) |
| 732 | drvdata->func_type[drvdata->curr_list] = |
| 733 | DCC_FUNC_TYPE_CAPTURE; |
| 734 | else if (!strcmp(str, str_dcc_func_type[DCC_FUNC_TYPE_CRC])) |
| 735 | drvdata->func_type[drvdata->curr_list] = |
| 736 | DCC_FUNC_TYPE_CRC; |
| 737 | else { |
| 738 | ret = -EINVAL; |
| 739 | goto out; |
| 740 | } |
| 741 | |
| 742 | ret = size; |
| 743 | out: |
| 744 | mutex_unlock(&drvdata->mutex); |
| 745 | return ret; |
| 746 | } |
| 747 | static DEVICE_ATTR(func_type, 0644, |
| 748 | dcc_show_func_type, dcc_store_func_type); |
| 749 | |
| 750 | static ssize_t dcc_show_data_sink(struct device *dev, |
| 751 | struct device_attribute *attr, char *buf) |
| 752 | { |
| 753 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 754 | |
| 755 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 756 | str_dcc_data_sink[drvdata->data_sink]); |
| 757 | } |
| 758 | |
| 759 | static ssize_t dcc_store_data_sink(struct device *dev, |
| 760 | struct device_attribute *attr, |
| 761 | const char *buf, size_t size) |
| 762 | { |
| 763 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 764 | char str[10] = ""; |
| 765 | int ret; |
| 766 | |
| 767 | if (strlen(buf) >= 10) |
| 768 | return -EINVAL; |
| 769 | if (sscanf(buf, "%s", str) != 1) |
| 770 | return -EINVAL; |
| 771 | |
| 772 | mutex_lock(&drvdata->mutex); |
| 773 | if (drvdata->enable[drvdata->curr_list]) { |
| 774 | ret = -EBUSY; |
| 775 | goto out; |
| 776 | } |
| 777 | |
| 778 | if (!strcmp(str, str_dcc_data_sink[DCC_DATA_SINK_SRAM])) |
| 779 | drvdata->data_sink = DCC_DATA_SINK_SRAM; |
| 780 | else if (!strcmp(str, str_dcc_data_sink[DCC_DATA_SINK_ATB])) |
| 781 | drvdata->data_sink = DCC_DATA_SINK_ATB; |
| 782 | else { |
| 783 | ret = -EINVAL; |
| 784 | goto out; |
| 785 | } |
| 786 | |
| 787 | ret = size; |
| 788 | out: |
| 789 | mutex_unlock(&drvdata->mutex); |
| 790 | return ret; |
| 791 | } |
| 792 | static DEVICE_ATTR(data_sink, 0644, |
| 793 | dcc_show_data_sink, dcc_store_data_sink); |
| 794 | |
| 795 | static ssize_t dcc_store_trigger(struct device *dev, |
| 796 | struct device_attribute *attr, |
| 797 | const char *buf, size_t size) |
| 798 | { |
| 799 | int ret = 0; |
| 800 | unsigned long val; |
| 801 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 802 | |
| 803 | if (kstrtoul(buf, 16, &val)) |
| 804 | return -EINVAL; |
| 805 | if (val != 1) |
| 806 | return -EINVAL; |
| 807 | |
| 808 | ret = dcc_sw_trigger(drvdata); |
| 809 | if (!ret) |
| 810 | ret = size; |
| 811 | |
| 812 | return ret; |
| 813 | } |
| 814 | static DEVICE_ATTR(trigger, 0200, NULL, dcc_store_trigger); |
| 815 | |
| 816 | static ssize_t dcc_show_enable(struct device *dev, |
| 817 | struct device_attribute *attr, char *buf) |
| 818 | { |
| 819 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 820 | |
| 821 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
| 822 | (unsigned int)drvdata->enable[drvdata->curr_list]); |
| 823 | } |
| 824 | |
| 825 | static ssize_t dcc_store_enable(struct device *dev, |
| 826 | struct device_attribute *attr, |
| 827 | const char *buf, size_t size) |
| 828 | { |
| 829 | int ret = 0; |
| 830 | unsigned long val; |
| 831 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 832 | |
| 833 | if (kstrtoul(buf, 16, &val)) |
| 834 | return -EINVAL; |
| 835 | |
| 836 | if (val) |
| 837 | ret = dcc_enable(drvdata); |
| 838 | else |
| 839 | dcc_disable(drvdata); |
| 840 | |
| 841 | if (!ret) |
| 842 | ret = size; |
| 843 | |
| 844 | return ret; |
| 845 | |
| 846 | } |
| 847 | static DEVICE_ATTR(enable, 0644, dcc_show_enable, |
| 848 | dcc_store_enable); |
| 849 | |
| 850 | static ssize_t dcc_show_config(struct device *dev, |
| 851 | struct device_attribute *attr, char *buf) |
| 852 | { |
| 853 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 854 | struct dcc_config_entry *entry; |
| 855 | char local_buf[64]; |
| 856 | int len = 0, count = 0; |
| 857 | |
| 858 | buf[0] = '\0'; |
| 859 | |
| 860 | mutex_lock(&drvdata->mutex); |
| 861 | list_for_each_entry(entry, |
| 862 | &drvdata->cfg_head[drvdata->curr_list], list) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 863 | switch (entry->desc_type) { |
| 864 | case DCC_READ_WRITE_TYPE: |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 865 | len = snprintf(local_buf, 64, |
| 866 | "Index: 0x%x, mask: 0x%x, val: 0x%x\n", |
| 867 | entry->index, entry->mask, |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 868 | entry->write_val); |
| 869 | break; |
| 870 | case DCC_LOOP_TYPE: |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 871 | len = snprintf(local_buf, 64, "Index: 0x%x, Loop: %d\n", |
| 872 | entry->index, entry->loop_cnt); |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 873 | break; |
| 874 | case DCC_WRITE_TYPE: |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 875 | len = snprintf(local_buf, 64, |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 876 | "Write Index: 0x%x, Base: 0x%x, Offset: 0x%x, len: 0x%x APB: %d\n", |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 877 | entry->index, entry->base, |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 878 | entry->offset, entry->len, |
| 879 | entry->apb_bus); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 880 | break; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 881 | default: |
| 882 | len = snprintf(local_buf, 64, |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 883 | "Read Index: 0x%x, Base: 0x%x, Offset: 0x%x, len: 0x%x APB: %d\n", |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 884 | entry->index, entry->base, |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 885 | entry->offset, entry->len, |
| 886 | entry->apb_bus); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 887 | } |
| 888 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 889 | if ((count + len) > PAGE_SIZE) { |
| 890 | dev_err(dev, "DCC: Couldn't write complete config\n"); |
| 891 | break; |
| 892 | } |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 893 | strlcat(buf, local_buf, PAGE_SIZE); |
| 894 | count += len; |
| 895 | } |
| 896 | |
| 897 | mutex_unlock(&drvdata->mutex); |
| 898 | |
| 899 | return count; |
| 900 | } |
| 901 | |
| 902 | static int dcc_config_add(struct dcc_drvdata *drvdata, unsigned int addr, |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 903 | unsigned int len, int apb_bus) |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 904 | { |
| 905 | int ret; |
| 906 | struct dcc_config_entry *entry, *pentry; |
| 907 | unsigned int base, offset; |
| 908 | |
| 909 | mutex_lock(&drvdata->mutex); |
| 910 | |
| 911 | if (!len) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 912 | dev_err(drvdata->dev, "DCC: Invalid length\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 913 | ret = -EINVAL; |
| 914 | goto err; |
| 915 | } |
| 916 | |
| 917 | base = addr & BM(4, 31); |
| 918 | |
| 919 | if (!list_empty(&drvdata->cfg_head[drvdata->curr_list])) { |
| 920 | pentry = list_last_entry(&drvdata->cfg_head[drvdata->curr_list], |
| 921 | struct dcc_config_entry, list); |
| 922 | |
| 923 | if (addr >= (pentry->base + pentry->offset) && |
| 924 | addr <= (pentry->base + pentry->offset + MAX_DCC_OFFSET)) { |
| 925 | |
| 926 | /* Re-use base address from last entry */ |
| 927 | base = pentry->base; |
| 928 | |
| 929 | /* |
| 930 | * Check if new address is contiguous to last entry's |
| 931 | * addresses. If yes then we can re-use last entry and |
| 932 | * just need to update its length. |
| 933 | */ |
| 934 | if ((pentry->len * 4 + pentry->base + pentry->offset) |
| 935 | == addr) { |
| 936 | len += pentry->len; |
| 937 | |
| 938 | /* |
| 939 | * Check if last entry can hold additional new |
| 940 | * length. If yes then we don't need to create |
| 941 | * a new entry else we need to add a new entry |
| 942 | * with same base but updated offset. |
| 943 | */ |
| 944 | if (len > MAX_DCC_LEN) |
| 945 | pentry->len = MAX_DCC_LEN; |
| 946 | else |
| 947 | pentry->len = len; |
| 948 | |
| 949 | /* |
| 950 | * Update start addr and len for remaining |
| 951 | * addresses, which will be part of new |
| 952 | * entry. |
| 953 | */ |
| 954 | addr = pentry->base + pentry->offset + |
| 955 | pentry->len * 4; |
| 956 | len -= pentry->len; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | offset = addr - base; |
| 962 | |
| 963 | while (len) { |
| 964 | entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL); |
| 965 | if (!entry) { |
| 966 | ret = -ENOMEM; |
| 967 | goto err; |
| 968 | } |
| 969 | |
| 970 | entry->base = base; |
| 971 | entry->offset = offset; |
| 972 | entry->len = min_t(uint32_t, len, MAX_DCC_LEN); |
| 973 | entry->index = drvdata->nr_config[drvdata->curr_list]++; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 974 | entry->desc_type = DCC_ADDR_TYPE; |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 975 | entry->apb_bus = apb_bus; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 976 | INIT_LIST_HEAD(&entry->list); |
| 977 | list_add_tail(&entry->list, |
| 978 | &drvdata->cfg_head[drvdata->curr_list]); |
| 979 | |
| 980 | len -= entry->len; |
| 981 | offset += MAX_DCC_LEN * 4; |
| 982 | } |
| 983 | |
| 984 | mutex_unlock(&drvdata->mutex); |
| 985 | return 0; |
| 986 | err: |
| 987 | mutex_unlock(&drvdata->mutex); |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | static ssize_t dcc_store_config(struct device *dev, |
| 992 | struct device_attribute *attr, |
| 993 | const char *buf, size_t size) |
| 994 | { |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 995 | int ret, len, apb_bus; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 996 | unsigned int base; |
| 997 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 998 | int nval; |
| 999 | |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 1000 | nval = sscanf(buf, "%x %i %d", &base, &len, &apb_bus); |
| 1001 | if (nval <= 0 || nval > 3) |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1002 | return -EINVAL; |
| 1003 | |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1004 | if (drvdata->curr_list >= DCC_MAX_LINK_LIST) { |
| 1005 | dev_err(dev, "Select link list to program using curr_list\n"); |
| 1006 | return -EINVAL; |
| 1007 | } |
| 1008 | |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 1009 | if (nval == 1) { |
| 1010 | len = 1; |
| 1011 | apb_bus = 0; |
| 1012 | } else if (nval == 2) { |
| 1013 | apb_bus = 0; |
| 1014 | } else { |
| 1015 | apb_bus = 1; |
| 1016 | } |
| 1017 | |
| 1018 | ret = dcc_config_add(drvdata, base, len, apb_bus); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1019 | if (ret) |
| 1020 | return ret; |
| 1021 | |
| 1022 | return size; |
| 1023 | |
| 1024 | } |
| 1025 | static DEVICE_ATTR(config, 0644, dcc_show_config, |
| 1026 | dcc_store_config); |
| 1027 | |
| 1028 | static void dcc_config_reset(struct dcc_drvdata *drvdata) |
| 1029 | { |
| 1030 | struct dcc_config_entry *entry, *temp; |
| 1031 | int curr_list; |
| 1032 | |
| 1033 | mutex_lock(&drvdata->mutex); |
| 1034 | |
| 1035 | for (curr_list = 0; curr_list < DCC_MAX_LINK_LIST; curr_list++) { |
| 1036 | |
| 1037 | list_for_each_entry_safe(entry, temp, |
| 1038 | &drvdata->cfg_head[curr_list], list) { |
| 1039 | list_del(&entry->list); |
| 1040 | devm_kfree(drvdata->dev, entry); |
| 1041 | drvdata->nr_config[curr_list]--; |
| 1042 | } |
| 1043 | } |
| 1044 | drvdata->ram_start = 0; |
| 1045 | drvdata->ram_cfg = 0; |
| 1046 | mutex_unlock(&drvdata->mutex); |
| 1047 | } |
| 1048 | |
| 1049 | static ssize_t dcc_store_config_reset(struct device *dev, |
| 1050 | struct device_attribute *attr, |
| 1051 | const char *buf, size_t size) |
| 1052 | { |
| 1053 | unsigned long val; |
| 1054 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1055 | |
| 1056 | if (kstrtoul(buf, 16, &val)) |
| 1057 | return -EINVAL; |
| 1058 | |
| 1059 | if (val) |
| 1060 | dcc_config_reset(drvdata); |
| 1061 | |
| 1062 | return size; |
| 1063 | } |
| 1064 | static DEVICE_ATTR(config_reset, 0200, NULL, dcc_store_config_reset); |
| 1065 | |
| 1066 | static ssize_t dcc_show_crc_error(struct device *dev, |
| 1067 | struct device_attribute *attr, char *buf) |
| 1068 | { |
| 1069 | int ret; |
| 1070 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1071 | |
| 1072 | mutex_lock(&drvdata->mutex); |
| 1073 | if (!drvdata->enable[drvdata->curr_list]) { |
| 1074 | ret = -EINVAL; |
| 1075 | goto err; |
| 1076 | } |
| 1077 | |
| 1078 | ret = scnprintf(buf, PAGE_SIZE, "%u\n", |
| 1079 | (unsigned int)BVAL(dcc_readl( |
| 1080 | drvdata, DCC_LL_INT_STATUS(drvdata->curr_list)), 1)); |
| 1081 | err: |
| 1082 | mutex_unlock(&drvdata->mutex); |
| 1083 | return ret; |
| 1084 | } |
| 1085 | static DEVICE_ATTR(crc_error, 0444, dcc_show_crc_error, NULL); |
| 1086 | |
| 1087 | static ssize_t dcc_show_ready(struct device *dev, |
| 1088 | struct device_attribute *attr, char *buf) |
| 1089 | { |
| 1090 | int ret; |
| 1091 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1092 | |
| 1093 | mutex_lock(&drvdata->mutex); |
| 1094 | if (!drvdata->enable[drvdata->curr_list]) { |
| 1095 | ret = -EINVAL; |
| 1096 | goto err; |
| 1097 | } |
| 1098 | |
| 1099 | ret = scnprintf(buf, PAGE_SIZE, "%u\n", |
| 1100 | (unsigned int)BVAL(dcc_readl(drvdata, DCC_STATUS), 1)); |
| 1101 | err: |
| 1102 | mutex_unlock(&drvdata->mutex); |
| 1103 | return ret; |
| 1104 | } |
| 1105 | static DEVICE_ATTR(ready, 0444, dcc_show_ready, NULL); |
| 1106 | |
| 1107 | static ssize_t dcc_show_interrupt_disable(struct device *dev, |
| 1108 | struct device_attribute *attr, |
| 1109 | char *buf) |
| 1110 | { |
| 1111 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1112 | |
| 1113 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
| 1114 | (unsigned int)drvdata->interrupt_disable); |
| 1115 | } |
| 1116 | |
| 1117 | static ssize_t dcc_store_interrupt_disable(struct device *dev, |
| 1118 | struct device_attribute *attr, |
| 1119 | const char *buf, size_t size) |
| 1120 | { |
| 1121 | unsigned long val; |
| 1122 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1123 | |
| 1124 | if (kstrtoul(buf, 16, &val)) |
| 1125 | return -EINVAL; |
| 1126 | |
| 1127 | mutex_lock(&drvdata->mutex); |
| 1128 | drvdata->interrupt_disable = (val ? 1:0); |
| 1129 | mutex_unlock(&drvdata->mutex); |
| 1130 | return size; |
| 1131 | } |
| 1132 | static DEVICE_ATTR(interrupt_disable, 0644, |
| 1133 | dcc_show_interrupt_disable, dcc_store_interrupt_disable); |
| 1134 | |
| 1135 | static ssize_t dcc_store_loop(struct device *dev, |
| 1136 | struct device_attribute *attr, |
| 1137 | const char *buf, size_t size) |
| 1138 | { |
| 1139 | int ret = size; |
| 1140 | unsigned long loop_cnt; |
| 1141 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1142 | struct dcc_config_entry *entry; |
| 1143 | |
| 1144 | mutex_lock(&drvdata->mutex); |
| 1145 | |
Satyajit Desai | 9e1ffcb | 2017-05-23 12:40:08 -0700 | [diff] [blame] | 1146 | if (kstrtoul(buf, 16, &loop_cnt)) { |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1147 | ret = -EINVAL; |
Satyajit Desai | 9e1ffcb | 2017-05-23 12:40:08 -0700 | [diff] [blame] | 1148 | goto err; |
| 1149 | } |
| 1150 | |
| 1151 | if (drvdata->curr_list >= DCC_MAX_LINK_LIST) { |
| 1152 | dev_err(dev, "Select link list to program using curr_list\n"); |
| 1153 | ret = -EINVAL; |
| 1154 | goto err; |
| 1155 | } |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1156 | |
| 1157 | entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL); |
| 1158 | if (!entry) { |
| 1159 | ret = -ENOMEM; |
| 1160 | goto err; |
| 1161 | } |
| 1162 | |
| 1163 | entry->loop_cnt = min_t(uint32_t, loop_cnt, MAX_LOOP_CNT); |
| 1164 | entry->index = drvdata->nr_config[drvdata->curr_list]++; |
Satyajit Desai | 9e1ffcb | 2017-05-23 12:40:08 -0700 | [diff] [blame] | 1165 | entry->desc_type = DCC_LOOP_TYPE; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1166 | INIT_LIST_HEAD(&entry->list); |
| 1167 | list_add_tail(&entry->list, &drvdata->cfg_head[drvdata->curr_list]); |
| 1168 | |
| 1169 | err: |
| 1170 | mutex_unlock(&drvdata->mutex); |
| 1171 | return ret; |
| 1172 | } |
| 1173 | static DEVICE_ATTR(loop, 0200, NULL, dcc_store_loop); |
| 1174 | |
| 1175 | static ssize_t dcc_rd_mod_wr(struct device *dev, |
| 1176 | struct device_attribute *attr, |
| 1177 | const char *buf, size_t size) |
| 1178 | { |
| 1179 | int ret = size; |
| 1180 | int nval; |
| 1181 | unsigned int mask, val; |
| 1182 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1183 | struct dcc_config_entry *entry; |
| 1184 | |
| 1185 | mutex_lock(&drvdata->mutex); |
| 1186 | |
| 1187 | nval = sscanf(buf, "%x %x", &mask, &val); |
| 1188 | |
| 1189 | if (nval <= 1 || nval > 2) { |
| 1190 | ret = -EINVAL; |
| 1191 | goto err; |
| 1192 | } |
| 1193 | |
| 1194 | if (list_empty(&drvdata->cfg_head[drvdata->curr_list])) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1195 | dev_err(drvdata->dev, "DCC: No read address programmed\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1196 | ret = -EPERM; |
| 1197 | goto err; |
| 1198 | } |
| 1199 | |
| 1200 | entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL); |
| 1201 | if (!entry) { |
| 1202 | ret = -ENOMEM; |
| 1203 | goto err; |
| 1204 | } |
| 1205 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1206 | entry->desc_type = DCC_READ_WRITE_TYPE; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1207 | entry->mask = mask; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1208 | entry->write_val = val; |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1209 | entry->index = drvdata->nr_config[drvdata->curr_list]++; |
| 1210 | INIT_LIST_HEAD(&entry->list); |
| 1211 | list_add_tail(&entry->list, &drvdata->cfg_head[drvdata->curr_list]); |
| 1212 | err: |
| 1213 | mutex_unlock(&drvdata->mutex); |
| 1214 | return ret; |
| 1215 | } |
| 1216 | static DEVICE_ATTR(rd_mod_wr, 0200, NULL, dcc_rd_mod_wr); |
| 1217 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1218 | static ssize_t dcc_write(struct device *dev, |
| 1219 | struct device_attribute *attr, |
| 1220 | const char *buf, size_t size) |
| 1221 | { |
| 1222 | int ret = size; |
| 1223 | int nval; |
| 1224 | unsigned int addr, write_val; |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 1225 | int apb_bus; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1226 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1227 | struct dcc_config_entry *entry; |
| 1228 | |
| 1229 | mutex_lock(&drvdata->mutex); |
| 1230 | |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 1231 | nval = sscanf(buf, "%x %x %d", &addr, &write_val, &apb_bus); |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1232 | |
Satyajit Desai | 9e1ffcb | 2017-05-23 12:40:08 -0700 | [diff] [blame] | 1233 | if (nval <= 1 || nval > 3) { |
| 1234 | ret = -EINVAL; |
| 1235 | goto err; |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Satyajit Desai | 9e1ffcb | 2017-05-23 12:40:08 -0700 | [diff] [blame] | 1238 | if (drvdata->curr_list >= DCC_MAX_LINK_LIST) { |
| 1239 | dev_err(dev, "Select link list to program using curr_list\n"); |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1240 | ret = -EINVAL; |
| 1241 | goto err; |
| 1242 | } |
| 1243 | |
| 1244 | entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL); |
| 1245 | if (!entry) { |
| 1246 | ret = -ENOMEM; |
| 1247 | goto err; |
| 1248 | } |
| 1249 | |
Satyajit Desai | e95613c | 2017-05-01 17:04:47 -0700 | [diff] [blame] | 1250 | if (nval == 3) |
| 1251 | entry->apb_bus = true; |
| 1252 | |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1253 | entry->desc_type = DCC_WRITE_TYPE; |
| 1254 | entry->base = addr & BM(4, 31); |
| 1255 | entry->offset = addr - entry->base; |
| 1256 | entry->write_val = write_val; |
| 1257 | entry->index = drvdata->nr_config[drvdata->curr_list]++; |
| 1258 | entry->len = 1; |
| 1259 | INIT_LIST_HEAD(&entry->list); |
| 1260 | list_add_tail(&entry->list, &drvdata->cfg_head[drvdata->curr_list]); |
| 1261 | err: |
| 1262 | mutex_unlock(&drvdata->mutex); |
| 1263 | return ret; |
| 1264 | } |
| 1265 | static DEVICE_ATTR(config_write, 0200, NULL, dcc_write); |
| 1266 | |
Satyajit Desai | fe1311b | 2017-05-19 16:02:35 -0700 | [diff] [blame] | 1267 | static ssize_t dcc_show_cti_trig(struct device *dev, |
| 1268 | struct device_attribute *attr, char *buf) |
| 1269 | { |
| 1270 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1271 | |
| 1272 | return scnprintf(buf, PAGE_SIZE, "%d\n", drvdata->cti_trig); |
| 1273 | } |
| 1274 | |
| 1275 | static ssize_t dcc_store_cti_trig(struct device *dev, |
| 1276 | struct device_attribute *attr, |
| 1277 | const char *buf, size_t size) |
| 1278 | { |
| 1279 | unsigned long val; |
| 1280 | int ret = 0; |
| 1281 | struct dcc_drvdata *drvdata = dev_get_drvdata(dev); |
| 1282 | |
| 1283 | if (kstrtoul(buf, 16, &val)) |
| 1284 | return -EINVAL; |
| 1285 | |
| 1286 | mutex_lock(&drvdata->mutex); |
| 1287 | |
| 1288 | if (drvdata->enable[drvdata->curr_list]) { |
| 1289 | ret = -EBUSY; |
| 1290 | goto out; |
| 1291 | } |
| 1292 | |
| 1293 | if (val) |
| 1294 | drvdata->cti_trig = 1; |
| 1295 | else |
| 1296 | drvdata->cti_trig = 0; |
| 1297 | out: |
| 1298 | mutex_unlock(&drvdata->mutex); |
| 1299 | return ret; |
| 1300 | } |
| 1301 | static DEVICE_ATTR(cti_trig, 0644, |
| 1302 | dcc_show_cti_trig, dcc_store_cti_trig); |
| 1303 | |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1304 | static const struct device_attribute *dcc_attrs[] = { |
| 1305 | &dev_attr_func_type, |
| 1306 | &dev_attr_data_sink, |
| 1307 | &dev_attr_trigger, |
| 1308 | &dev_attr_enable, |
| 1309 | &dev_attr_config, |
| 1310 | &dev_attr_config_reset, |
| 1311 | &dev_attr_ready, |
| 1312 | &dev_attr_crc_error, |
| 1313 | &dev_attr_interrupt_disable, |
| 1314 | &dev_attr_loop, |
| 1315 | &dev_attr_rd_mod_wr, |
| 1316 | &dev_attr_curr_list, |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1317 | &dev_attr_config_write, |
Satyajit Desai | fe1311b | 2017-05-19 16:02:35 -0700 | [diff] [blame] | 1318 | &dev_attr_cti_trig, |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1319 | NULL, |
| 1320 | }; |
| 1321 | |
| 1322 | static int dcc_create_files(struct device *dev, |
| 1323 | const struct device_attribute **attrs) |
| 1324 | { |
| 1325 | int ret = 0, i; |
| 1326 | |
| 1327 | for (i = 0; attrs[i] != NULL; i++) { |
| 1328 | ret = device_create_file(dev, attrs[i]); |
| 1329 | if (ret) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1330 | dev_err(dev, "DCC: Couldn't create sysfs attribute: %s\n", |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1331 | attrs[i]->attr.name); |
| 1332 | break; |
| 1333 | } |
| 1334 | } |
| 1335 | return ret; |
| 1336 | } |
| 1337 | |
| 1338 | static int dcc_sram_open(struct inode *inode, struct file *file) |
| 1339 | { |
| 1340 | struct dcc_drvdata *drvdata = container_of(inode->i_cdev, |
| 1341 | struct dcc_drvdata, |
| 1342 | sram_dev); |
| 1343 | file->private_data = drvdata; |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
| 1348 | static ssize_t dcc_sram_read(struct file *file, char __user *data, |
| 1349 | size_t len, loff_t *ppos) |
| 1350 | { |
| 1351 | unsigned char *buf; |
| 1352 | struct dcc_drvdata *drvdata = file->private_data; |
| 1353 | |
| 1354 | /* EOF check */ |
| 1355 | if (drvdata->ram_size <= *ppos) |
| 1356 | return 0; |
| 1357 | |
| 1358 | if ((*ppos + len) > drvdata->ram_size) |
| 1359 | len = (drvdata->ram_size - *ppos); |
| 1360 | |
| 1361 | buf = kzalloc(len, GFP_KERNEL); |
| 1362 | if (!buf) |
| 1363 | return -ENOMEM; |
| 1364 | |
| 1365 | memcpy_fromio(buf, (drvdata->ram_base + *ppos), len); |
| 1366 | |
| 1367 | if (copy_to_user(data, buf, len)) { |
| 1368 | dev_err(drvdata->dev, |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1369 | "DCC: Couldn't copy all data to user\n"); |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1370 | kfree(buf); |
| 1371 | return -EFAULT; |
| 1372 | } |
| 1373 | |
| 1374 | *ppos += len; |
| 1375 | |
| 1376 | kfree(buf); |
| 1377 | |
| 1378 | return len; |
| 1379 | } |
| 1380 | |
| 1381 | static const struct file_operations dcc_sram_fops = { |
| 1382 | .owner = THIS_MODULE, |
| 1383 | .open = dcc_sram_open, |
| 1384 | .read = dcc_sram_read, |
| 1385 | .llseek = no_llseek, |
| 1386 | }; |
| 1387 | |
| 1388 | static int dcc_sram_dev_register(struct dcc_drvdata *drvdata) |
| 1389 | { |
| 1390 | int ret; |
| 1391 | struct device *device; |
| 1392 | dev_t dev; |
| 1393 | |
| 1394 | ret = alloc_chrdev_region(&dev, 0, 1, drvdata->sram_node); |
| 1395 | if (ret) |
| 1396 | goto err_alloc; |
| 1397 | |
| 1398 | cdev_init(&drvdata->sram_dev, &dcc_sram_fops); |
| 1399 | |
| 1400 | drvdata->sram_dev.owner = THIS_MODULE; |
| 1401 | ret = cdev_add(&drvdata->sram_dev, dev, 1); |
| 1402 | if (ret) |
| 1403 | goto err_cdev_add; |
| 1404 | |
| 1405 | drvdata->sram_class = class_create(THIS_MODULE, |
| 1406 | drvdata->sram_node); |
| 1407 | if (IS_ERR(drvdata->sram_class)) { |
| 1408 | ret = PTR_ERR(drvdata->sram_class); |
| 1409 | goto err_class_create; |
| 1410 | } |
| 1411 | |
| 1412 | device = device_create(drvdata->sram_class, NULL, |
| 1413 | drvdata->sram_dev.dev, drvdata, |
| 1414 | drvdata->sram_node); |
| 1415 | if (IS_ERR(device)) { |
| 1416 | ret = PTR_ERR(device); |
| 1417 | goto err_dev_create; |
| 1418 | } |
| 1419 | |
| 1420 | return 0; |
| 1421 | err_dev_create: |
| 1422 | class_destroy(drvdata->sram_class); |
| 1423 | err_class_create: |
| 1424 | cdev_del(&drvdata->sram_dev); |
| 1425 | err_cdev_add: |
| 1426 | unregister_chrdev_region(drvdata->sram_dev.dev, 1); |
| 1427 | err_alloc: |
| 1428 | return ret; |
| 1429 | } |
| 1430 | |
| 1431 | static void dcc_sram_dev_deregister(struct dcc_drvdata *drvdata) |
| 1432 | { |
| 1433 | device_destroy(drvdata->sram_class, drvdata->sram_dev.dev); |
| 1434 | class_destroy(drvdata->sram_class); |
| 1435 | cdev_del(&drvdata->sram_dev); |
| 1436 | unregister_chrdev_region(drvdata->sram_dev.dev, 1); |
| 1437 | } |
| 1438 | |
| 1439 | static int dcc_sram_dev_init(struct dcc_drvdata *drvdata) |
| 1440 | { |
| 1441 | int ret = 0; |
| 1442 | size_t node_size; |
| 1443 | char *node_name = "dcc_sram"; |
| 1444 | struct device *dev = drvdata->dev; |
| 1445 | |
| 1446 | node_size = strlen(node_name) + 1; |
| 1447 | |
| 1448 | drvdata->sram_node = devm_kzalloc(dev, node_size, GFP_KERNEL); |
| 1449 | if (!drvdata->sram_node) |
| 1450 | return -ENOMEM; |
| 1451 | |
| 1452 | strlcpy(drvdata->sram_node, node_name, node_size); |
| 1453 | ret = dcc_sram_dev_register(drvdata); |
| 1454 | if (ret) |
| 1455 | dev_err(drvdata->dev, "DCC: sram node not registered.\n"); |
| 1456 | |
| 1457 | return ret; |
| 1458 | } |
| 1459 | |
| 1460 | static void dcc_sram_dev_exit(struct dcc_drvdata *drvdata) |
| 1461 | { |
| 1462 | dcc_sram_dev_deregister(drvdata); |
| 1463 | } |
| 1464 | |
| 1465 | static void dcc_allocate_dump_mem(struct dcc_drvdata *drvdata) |
| 1466 | { |
| 1467 | int ret; |
| 1468 | struct device *dev = drvdata->dev; |
| 1469 | struct msm_dump_entry reg_dump_entry, sram_dump_entry; |
| 1470 | |
| 1471 | /* Allocate memory for dcc reg dump */ |
| 1472 | drvdata->reg_buf = devm_kzalloc(dev, drvdata->reg_size, GFP_KERNEL); |
| 1473 | if (drvdata->reg_buf) { |
| 1474 | drvdata->reg_data.addr = virt_to_phys(drvdata->reg_buf); |
| 1475 | drvdata->reg_data.len = drvdata->reg_size; |
| 1476 | reg_dump_entry.id = MSM_DUMP_DATA_DCC_REG; |
| 1477 | reg_dump_entry.addr = virt_to_phys(&drvdata->reg_data); |
| 1478 | ret = msm_dump_data_register(MSM_DUMP_TABLE_APPS, |
| 1479 | ®_dump_entry); |
| 1480 | if (ret) { |
| 1481 | dev_err(dev, "DCC REG dump setup failed\n"); |
| 1482 | devm_kfree(dev, drvdata->reg_buf); |
| 1483 | } |
| 1484 | } else { |
| 1485 | dev_err(dev, "DCC REG dump allocation failed\n"); |
| 1486 | } |
| 1487 | |
| 1488 | /* Allocate memory for dcc sram dump */ |
| 1489 | drvdata->sram_buf = devm_kzalloc(dev, drvdata->ram_size, GFP_KERNEL); |
| 1490 | if (drvdata->sram_buf) { |
| 1491 | drvdata->sram_data.addr = virt_to_phys(drvdata->sram_buf); |
| 1492 | drvdata->sram_data.len = drvdata->ram_size; |
| 1493 | sram_dump_entry.id = MSM_DUMP_DATA_DCC_SRAM; |
| 1494 | sram_dump_entry.addr = virt_to_phys(&drvdata->sram_data); |
| 1495 | ret = msm_dump_data_register(MSM_DUMP_TABLE_APPS, |
| 1496 | &sram_dump_entry); |
| 1497 | if (ret) { |
| 1498 | dev_err(dev, "DCC SRAM dump setup failed\n"); |
| 1499 | devm_kfree(dev, drvdata->sram_buf); |
| 1500 | } |
| 1501 | } else { |
| 1502 | dev_err(dev, "DCC SRAM dump allocation failed\n"); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | static int dcc_probe(struct platform_device *pdev) |
| 1507 | { |
| 1508 | int ret, i; |
| 1509 | struct device *dev = &pdev->dev; |
| 1510 | struct dcc_drvdata *drvdata; |
| 1511 | struct resource *res; |
| 1512 | const char *data_sink; |
| 1513 | |
| 1514 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
| 1515 | if (!drvdata) |
| 1516 | return -ENOMEM; |
| 1517 | |
| 1518 | drvdata->dev = &pdev->dev; |
| 1519 | platform_set_drvdata(pdev, drvdata); |
| 1520 | |
| 1521 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dcc-base"); |
| 1522 | if (!res) |
| 1523 | return -EINVAL; |
| 1524 | |
| 1525 | drvdata->reg_size = resource_size(res); |
| 1526 | drvdata->base = devm_ioremap(dev, res->start, resource_size(res)); |
| 1527 | if (!drvdata->base) |
| 1528 | return -ENOMEM; |
| 1529 | |
| 1530 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 1531 | "dcc-ram-base"); |
| 1532 | if (!res) |
| 1533 | return -EINVAL; |
| 1534 | |
| 1535 | drvdata->ram_size = resource_size(res); |
| 1536 | drvdata->ram_base = devm_ioremap(dev, res->start, resource_size(res)); |
| 1537 | if (!drvdata->ram_base) |
| 1538 | return -ENOMEM; |
| 1539 | |
Satyajit Desai | 3d86e1b | 2017-04-13 17:11:09 -0700 | [diff] [blame] | 1540 | ret = of_property_read_u32(pdev->dev.of_node, "dcc-ram-offset", |
| 1541 | &drvdata->ram_offset); |
| 1542 | if (ret) |
| 1543 | return -EINVAL; |
| 1544 | |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1545 | drvdata->save_reg = of_property_read_bool(pdev->dev.of_node, |
| 1546 | "qcom,save-reg"); |
| 1547 | |
| 1548 | mutex_init(&drvdata->mutex); |
| 1549 | |
| 1550 | for (i = 0; i < DCC_MAX_LINK_LIST; i++) { |
| 1551 | INIT_LIST_HEAD(&drvdata->cfg_head[i]); |
| 1552 | drvdata->nr_config[i] = 0; |
| 1553 | } |
| 1554 | |
| 1555 | memset_io(drvdata->ram_base, 0, drvdata->ram_size); |
| 1556 | |
| 1557 | drvdata->data_sink = DCC_DATA_SINK_SRAM; |
| 1558 | ret = of_property_read_string(pdev->dev.of_node, "qcom,data-sink", |
| 1559 | &data_sink); |
| 1560 | if (!ret) { |
| 1561 | for (i = 0; i < ARRAY_SIZE(str_dcc_data_sink); i++) |
| 1562 | if (!strcmp(data_sink, str_dcc_data_sink[i])) { |
| 1563 | drvdata->data_sink = i; |
| 1564 | break; |
| 1565 | } |
| 1566 | |
| 1567 | if (i == ARRAY_SIZE(str_dcc_data_sink)) { |
Satyajit Desai | 3307362 | 2017-04-17 16:45:06 -0700 | [diff] [blame] | 1568 | dev_err(dev, "Unknown sink type for DCC Using '%s' as data sink\n", |
Satyajit Desai | 765e7ef | 2016-11-09 14:27:45 -0800 | [diff] [blame] | 1569 | str_dcc_data_sink[drvdata->data_sink]); |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | drvdata->curr_list = DCC_INVALID_LINK_LIST; |
| 1574 | |
| 1575 | ret = dcc_sram_dev_init(drvdata); |
| 1576 | if (ret) |
| 1577 | goto err; |
| 1578 | |
| 1579 | ret = dcc_create_files(dev, dcc_attrs); |
| 1580 | if (ret) |
| 1581 | goto err; |
| 1582 | |
| 1583 | dcc_allocate_dump_mem(drvdata); |
| 1584 | return 0; |
| 1585 | err: |
| 1586 | return ret; |
| 1587 | } |
| 1588 | |
| 1589 | static int dcc_remove(struct platform_device *pdev) |
| 1590 | { |
| 1591 | struct dcc_drvdata *drvdata = platform_get_drvdata(pdev); |
| 1592 | |
| 1593 | dcc_sram_dev_exit(drvdata); |
| 1594 | |
| 1595 | dcc_config_reset(drvdata); |
| 1596 | |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | static const struct of_device_id msm_dcc_match[] = { |
| 1601 | { .compatible = "qcom,dcc_v2"}, |
| 1602 | {} |
| 1603 | }; |
| 1604 | |
| 1605 | static struct platform_driver dcc_driver = { |
| 1606 | .probe = dcc_probe, |
| 1607 | .remove = dcc_remove, |
| 1608 | .driver = { |
| 1609 | .name = "msm-dcc", |
| 1610 | .owner = THIS_MODULE, |
| 1611 | .of_match_table = msm_dcc_match, |
| 1612 | }, |
| 1613 | }; |
| 1614 | |
| 1615 | static int __init dcc_init(void) |
| 1616 | { |
| 1617 | return platform_driver_register(&dcc_driver); |
| 1618 | } |
| 1619 | module_init(dcc_init); |
| 1620 | |
| 1621 | static void __exit dcc_exit(void) |
| 1622 | { |
| 1623 | platform_driver_unregister(&dcc_driver); |
| 1624 | } |
| 1625 | module_exit(dcc_exit); |
| 1626 | |
| 1627 | MODULE_LICENSE("GPL v2"); |
| 1628 | MODULE_DESCRIPTION("MSM data capture and compare engine"); |