blob: a68728626f0a19238d0096503d4ee331baa4cded [file] [log] [blame]
Mukesh Ojha7085e052018-02-21 19:17:54 +05301/* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
Mukesh Ojhacdbf01c2017-12-04 17:46:09 +05302 *
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/clk.h>
16#include <linux/delay.h>
17#include <linux/io.h>
18#include <linux/iopoll.h>
19#include <linux/fs.h>
20#include <linux/of.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23#include <linux/uaccess.h>
24#include <soc/qcom/memory_dump.h>
25#include <soc/qcom/rpm-smd.h>
26#include <soc/qcom/scm.h>
27
28#define RPM_MISC_REQ_TYPE 0x6373696d
29#define RPM_MISC_DDR_DCC_ENABLE 0x32726464
30
31#define TIMEOUT_US (100)
32
33#define BM(lsb, msb) ((BIT(msb) - BIT(lsb)) + BIT(msb))
34#define BMVAL(val, lsb, msb) ((val & BM(lsb, msb)) >> lsb)
35#define BVAL(val, n) ((val & BIT(n)) >> n)
36
37#define dcc_writel(drvdata, val, off) \
38 __raw_writel((val), drvdata->base + off)
39#define dcc_readl(drvdata, off) \
40 __raw_readl(drvdata->base + off)
41
42#define dcc_sram_writel(drvdata, val, off) \
43 __raw_writel((val), drvdata->ram_base + off)
44#define dcc_sram_readl(drvdata, off) \
45 __raw_readl(drvdata->ram_base + off)
46
47/* DCC registers */
48#define DCC_HW_VERSION (0x00)
49#define DCC_HW_INFO (0x04)
50#define DCC_CGC_CFG (0x10)
51#define DCC_LL (0x14)
52#define DCC_RAM_CFG (0x18)
53#define DCC_CFG (0x1C)
54#define DCC_SW_CTL (0x20)
55#define DCC_STATUS (0x24)
56#define DCC_FETCH_ADDR (0x28)
57#define DCC_SRAM_ADDR (0x2C)
58#define DCC_INT_ENABLE (0x30)
59#define DCC_INT_STATUS (0x34)
60#define DCC_QSB_CFG (0x38)
61
62#define DCC_REG_DUMP_MAGIC_V2 (0x42445953)
63#define DCC_REG_DUMP_VER (1)
64
65#define MAX_DCC_OFFSET (0xFF * 4)
66#define MAX_DCC_LEN 0x7F
67
68#define SCM_SVC_DISABLE_XPU 0x23
69
70enum dcc_func_type {
71 DCC_FUNC_TYPE_CAPTURE,
72 DCC_FUNC_TYPE_CRC,
73};
74
75static const char * const str_dcc_func_type[] = {
76 [DCC_FUNC_TYPE_CAPTURE] = "cap",
77 [DCC_FUNC_TYPE_CRC] = "crc",
78};
79
80enum dcc_data_sink {
81 DCC_DATA_SINK_ATB,
82 DCC_DATA_SINK_SRAM
83};
84
85static const char * const str_dcc_data_sink[] = {
86 [DCC_DATA_SINK_ATB] = "atb",
87 [DCC_DATA_SINK_SRAM] = "sram",
88};
89
90struct rpm_trig_req {
91 uint32_t enable;
92 uint32_t reserved;
93};
94
95struct dcc_config_entry {
96 uint32_t base;
97 uint32_t offset;
98 uint32_t len;
99 uint32_t index;
100 struct list_head list;
101};
102
103struct dcc_drvdata {
104 void __iomem *base;
105 uint32_t reg_size;
106 struct device *dev;
107 struct mutex mutex;
108 void __iomem *ram_base;
109 uint32_t ram_size;
110 struct clk *clk;
111 enum dcc_data_sink data_sink;
112 enum dcc_func_type func_type;
113 uint32_t ram_cfg;
114 bool enable;
115 bool interrupt_disable;
116 char *sram_node;
117 struct cdev sram_dev;
118 struct class *sram_class;
119 struct list_head config_head;
120 uint32_t nr_config;
121 void *reg_buf;
122 struct msm_dump_data reg_data;
123 bool save_reg;
124 void *sram_buf;
125 struct msm_dump_data sram_data;
126 struct rpm_trig_req rpm_trig_req;
127 struct msm_rpm_kvp rpm_kvp;
128 bool xpu_scm_avail;
129 uint64_t xpu_addr;
130 uint32_t xpu_unlock_count;
131};
132
133static int dcc_cfg_xpu(struct dcc_drvdata *drvdata, bool enable)
134{
135 struct scm_desc desc = {0};
136
137 desc.args[0] = drvdata->xpu_addr;
138 desc.args[1] = enable;
139 desc.arginfo = SCM_ARGS(2, SCM_VAL, SCM_VAL);
140
141 return scm_call2(SCM_SIP_FNID(SCM_SVC_MP, SCM_SVC_DISABLE_XPU), &desc);
142}
143
144static int dcc_xpu_lock(struct dcc_drvdata *drvdata)
145{
146 int ret = 0;
147
148 mutex_lock(&drvdata->mutex);
149 if (!drvdata->xpu_scm_avail)
150 goto err;
151
152 if (drvdata->xpu_unlock_count == 0)
153 goto err;
154
155 if (drvdata->xpu_unlock_count == 1) {
156 ret = clk_prepare_enable(drvdata->clk);
157 if (ret)
158 goto err;
159
160 /* make sure all access to DCC are completed */
161 mb();
162
163 ret = dcc_cfg_xpu(drvdata, 1);
164 if (ret)
165 dev_err(drvdata->dev, "Falied to lock DCC XPU.\n");
166
167 clk_disable_unprepare(drvdata->clk);
168 }
169
170 if (!ret)
171 drvdata->xpu_unlock_count--;
172err:
173 mutex_unlock(&drvdata->mutex);
174 return ret;
175}
176
177static int dcc_xpu_unlock(struct dcc_drvdata *drvdata)
178{
179 int ret = 0;
180
181 mutex_lock(&drvdata->mutex);
182 if (!drvdata->xpu_scm_avail)
183 goto err;
184
185 if (drvdata->xpu_unlock_count == 0) {
186 ret = clk_prepare_enable(drvdata->clk);
187 if (ret)
188 goto err;
189
190 ret = dcc_cfg_xpu(drvdata, 0);
191 if (ret)
192 dev_err(drvdata->dev, "Falied to unlock DCC XPU.\n");
193
194 clk_disable_unprepare(drvdata->clk);
195 }
196
197 if (!ret)
198 drvdata->xpu_unlock_count++;
199err:
200 mutex_unlock(&drvdata->mutex);
201 return ret;
202}
203
204static bool dcc_ready(struct dcc_drvdata *drvdata)
205{
206 uint32_t val;
207
208 /* poll until DCC ready */
209 if (!readl_poll_timeout((drvdata->base + DCC_STATUS), val,
210 (BVAL(val, 4) == 1), 1, TIMEOUT_US))
211 return true;
212
213 return false;
214}
215
216static int dcc_sw_trigger(struct dcc_drvdata *drvdata)
217{
218 int ret;
219
220 ret = 0;
221 mutex_lock(&drvdata->mutex);
222
223 if (!drvdata->enable) {
224 dev_err(drvdata->dev,
225 "DCC is disabled. Can't send sw trigger.\n");
226 ret = -EINVAL;
227 goto err;
228 }
229
230 if (!dcc_ready(drvdata)) {
231 dev_err(drvdata->dev, "DCC is not ready!\n");
232 ret = -EBUSY;
233 goto err;
234 }
235
236 dcc_writel(drvdata, 1, DCC_SW_CTL);
237
238 if (!dcc_ready(drvdata)) {
239 dev_err(drvdata->dev,
240 "DCC is busy after receiving sw tigger.\n");
241 ret = -EBUSY;
242 goto err;
243 }
244err:
245 mutex_unlock(&drvdata->mutex);
246 return ret;
247}
248
249static int __dcc_ll_cfg(struct dcc_drvdata *drvdata)
250{
251 int ret = 0;
252 uint32_t sram_offset = 0;
253 uint32_t prev_addr, addr;
254 uint32_t prev_off = 0, off;
255 uint32_t link;
256 uint32_t pos, total_len = 0;
257 struct dcc_config_entry *entry;
258
259 if (list_empty(&drvdata->config_head)) {
260 dev_err(drvdata->dev,
261 "No configuration is available to program in DCC SRAM!\n");
262 return -EINVAL;
263 }
264
265 memset_io(drvdata->ram_base, 0, drvdata->ram_size);
266
267 prev_addr = 0;
268 link = 0;
269
270 list_for_each_entry(entry, &drvdata->config_head, list) {
271 /* Address type */
272 addr = (entry->base >> 4) & BM(0, 27);
273 addr |= BIT(31);
274 off = entry->offset/4;
275 total_len += entry->len * 4;
276
277 if (!prev_addr || prev_addr != addr || prev_off > off) {
278 /* Check if we need to write link of prev entry */
279 if (link) {
280 dcc_sram_writel(drvdata, link, sram_offset);
281 sram_offset += 4;
282 }
283
284 /* Write address */
285 dcc_sram_writel(drvdata, addr, sram_offset);
286 sram_offset += 4;
287
288 /* Reset link and prev_off */
289 link = 0;
290 prev_off = 0;
291 }
292
293 if ((off - prev_off) > 0xFF || entry->len > MAX_DCC_LEN) {
294 dev_err(drvdata->dev,
295 "DCC: Progamming error! Base: 0x%x, offset 0x%x.\n",
296 entry->base, entry->offset);
297 ret = -EINVAL;
298 goto err;
299 }
300
301 if (link) {
302 /*
303 * link already has one offset-length so new
304 * offset-length needs to be placed at bits [31:16]
305 */
306 pos = 16;
307
308 /* Clear bits [31:16] */
309 link &= BM(0, 15);
310
311 } else {
312 /*
313 * link is empty, so new offset-length needs to be
314 * placed at bits [15:0]
315 */
316 pos = 0;
317 link = 1 << 16;
318 }
319
320 /* write new offset-length pair to correct position */
321 link |= (((off-prev_off) & BM(0, 7)) |
322 ((entry->len << 8) & BM(8, 14))) << pos;
323
324 if (pos) {
325 dcc_sram_writel(drvdata, link, sram_offset);
326 sram_offset += 4;
327 link = 0;
328 }
329
330 prev_off = off;
331 prev_addr = addr;
332 }
333
334 if (link) {
335 dcc_sram_writel(drvdata, link, sram_offset);
336 sram_offset += 4;
337 }
338
339 /* Setting zero to indicate end of the list */
340 dcc_sram_writel(drvdata, 0, sram_offset);
341 sram_offset += 4;
342
343 /* check if the data will overstep */
344 if (drvdata->data_sink == DCC_DATA_SINK_SRAM
345 && drvdata->func_type == DCC_FUNC_TYPE_CAPTURE) {
346 if (sram_offset + total_len > drvdata->ram_size) {
347 sram_offset += total_len;
348 goto overstep;
349 }
350 } else {
351 if (sram_offset > drvdata->ram_size)
352 goto overstep;
353 }
354
355 drvdata->ram_cfg = (sram_offset / 4);
356 return 0;
357overstep:
358 ret = -EINVAL;
359 memset_io(drvdata->ram_base, 0, drvdata->ram_size);
360 dev_err(drvdata->dev, "DCC SRAM oversteps, 0x%x (0x%x)\n",
361 sram_offset, drvdata->ram_size);
362err:
363 return ret;
364}
365
366static void __dcc_reg_dump(struct dcc_drvdata *drvdata)
367{
368 uint32_t *reg_buf;
369
370 if (!drvdata->reg_buf)
371 return;
372
373 drvdata->reg_data.version = DCC_REG_DUMP_VER;
374
375 reg_buf = drvdata->reg_buf;
376
377 reg_buf[0] = dcc_readl(drvdata, DCC_HW_VERSION);
378 reg_buf[1] = dcc_readl(drvdata, DCC_HW_INFO);
379 reg_buf[2] = dcc_readl(drvdata, DCC_CGC_CFG);
380 reg_buf[3] = dcc_readl(drvdata, DCC_LL);
381 reg_buf[4] = dcc_readl(drvdata, DCC_RAM_CFG);
382 reg_buf[5] = dcc_readl(drvdata, DCC_CFG);
383 reg_buf[6] = dcc_readl(drvdata, DCC_SW_CTL);
384 reg_buf[7] = dcc_readl(drvdata, DCC_STATUS);
385 reg_buf[8] = dcc_readl(drvdata, DCC_FETCH_ADDR);
386 reg_buf[9] = dcc_readl(drvdata, DCC_SRAM_ADDR);
387 reg_buf[10] = dcc_readl(drvdata, DCC_INT_ENABLE);
388 reg_buf[11] = dcc_readl(drvdata, DCC_INT_STATUS);
389 reg_buf[12] = dcc_readl(drvdata, DCC_QSB_CFG);
390
391 drvdata->reg_data.magic = DCC_REG_DUMP_MAGIC_V2;
392}
393
394static void __dcc_first_crc(struct dcc_drvdata *drvdata)
395{
396 int i;
397
398 /*
399 * Need to send 2 triggers to DCC. First trigger sets CRC error status
400 * bit. So need second trigger to reset this bit.
401 */
402 for (i = 0; i < 2; i++) {
403 if (!dcc_ready(drvdata))
404 dev_err(drvdata->dev, "DCC is not ready!\n");
405
406 dcc_writel(drvdata, 1, DCC_SW_CTL);
407 }
408
409 /* Clear CRC error interrupt */
410 dcc_writel(drvdata, BIT(0), DCC_INT_STATUS);
411}
412
413static int dcc_enable(struct dcc_drvdata *drvdata)
414{
415 int ret = 0;
416
417 mutex_lock(&drvdata->mutex);
418
419 if (drvdata->enable) {
420 dev_err(drvdata->dev, "DCC is already enabled!\n");
421 mutex_unlock(&drvdata->mutex);
422 return 0;
423 }
424
425 /* 1. Prepare and enable DCC clock */
426 ret = clk_prepare_enable(drvdata->clk);
427 if (ret)
428 goto err;
429
430 dcc_writel(drvdata, 0, DCC_LL);
431
432 /* 2. Program linked-list in the SRAM */
433 ret = __dcc_ll_cfg(drvdata);
434 if (ret)
435 goto err_prog_ll;
436
437 /* 3. If in capture mode program DCC_RAM_CFG reg */
438 if (drvdata->func_type == DCC_FUNC_TYPE_CAPTURE)
439 dcc_writel(drvdata, drvdata->ram_cfg, DCC_RAM_CFG);
440
441 /* 4. Configure data sink and function type */
442 dcc_writel(drvdata, ((drvdata->data_sink << 4) | (drvdata->func_type)),
443 DCC_CFG);
444
445 /* 5. Clears interrupt status register */
446 dcc_writel(drvdata, 0, DCC_INT_ENABLE);
447 dcc_writel(drvdata, (BIT(4) | BIT(0)), DCC_INT_STATUS);
448
449 /* Make sure all config is written in sram */
450 mb();
451
452 /* 6. Set LL bit */
453 dcc_writel(drvdata, 1, DCC_LL);
454 drvdata->enable = 1;
455
456 if (drvdata->func_type == DCC_FUNC_TYPE_CRC) {
457 __dcc_first_crc(drvdata);
458
459 /* Enable CRC error interrupt */
460 if (!drvdata->interrupt_disable)
461 dcc_writel(drvdata, BIT(0), DCC_INT_ENABLE);
462 }
463
464 /* Save DCC registers */
465 if (drvdata->save_reg)
466 __dcc_reg_dump(drvdata);
467
468err_prog_ll:
469 if (!drvdata->enable)
470 clk_disable_unprepare(drvdata->clk);
471err:
472 mutex_unlock(&drvdata->mutex);
473 return ret;
474}
475
476static int __dcc_rpm_sw_trigger(struct dcc_drvdata *drvdata, bool enable)
477{
478 int ret = 0;
479 struct msm_rpm_kvp *rpm_kvp = &drvdata->rpm_kvp;
480
481 if (enable == drvdata->rpm_trig_req.enable)
482 return 0;
483
484 if (enable && (!drvdata->enable || drvdata->func_type !=
485 DCC_FUNC_TYPE_CRC)) {
486 dev_err(drvdata->dev,
487 "DCC: invalid state! Can't send sw trigger req to rpm\n");
488 return -EINVAL;
489 }
490
491 drvdata->rpm_trig_req.enable = enable;
492 rpm_kvp->key = RPM_MISC_DDR_DCC_ENABLE;
493 rpm_kvp->length = sizeof(struct rpm_trig_req);
494 rpm_kvp->data = (void *)(&drvdata->rpm_trig_req);
495
496 ret = msm_rpm_send_message(MSM_RPM_CTX_ACTIVE_SET,
497 RPM_MISC_REQ_TYPE, 0, rpm_kvp, 1);
498 if (ret) {
499 dev_err(drvdata->dev,
500 "DCC: SW trigger %s req to rpm failed %d\n",
501 (enable ? "enable" : "disable"), ret);
502 drvdata->rpm_trig_req.enable = !enable;
503 }
504
505 return ret;
506}
507
508static void dcc_disable(struct dcc_drvdata *drvdata)
509{
510 mutex_lock(&drvdata->mutex);
511 if (!drvdata->enable) {
512 mutex_unlock(&drvdata->mutex);
513 return;
514 }
515
516 /* Send request to RPM to disable DCC SW trigger */
517
518 if (__dcc_rpm_sw_trigger(drvdata, 0))
519 dev_err(drvdata->dev,
520 "DCC: Request to RPM to disable SW trigger failed.\n");
521
522 if (!dcc_ready(drvdata))
523 dev_err(drvdata->dev, "DCC is not ready! Disabling DCC...\n");
524
525 dcc_writel(drvdata, 0, DCC_LL);
526 drvdata->enable = 0;
527
528 /* Save DCC registers */
529 if (drvdata->save_reg)
530 __dcc_reg_dump(drvdata);
531
532 clk_disable_unprepare(drvdata->clk);
533
534 mutex_unlock(&drvdata->mutex);
535}
536
537static ssize_t dcc_show_func_type(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
540 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
541
542 return scnprintf(buf, PAGE_SIZE, "%s\n",
543 str_dcc_func_type[drvdata->func_type]);
544}
545
546static ssize_t dcc_store_func_type(struct device *dev,
547 struct device_attribute *attr,
548 const char *buf, size_t size)
549{
550 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
551 char str[10] = "";
552 int ret;
553
554 if (strlen(buf) >= 10)
555 return -EINVAL;
556
Mukesh Ojha7085e052018-02-21 19:17:54 +0530557 if (sscanf(buf, "%9s", str) != 1)
558 return -EINVAL;
Mukesh Ojhacdbf01c2017-12-04 17:46:09 +0530559
560 mutex_lock(&drvdata->mutex);
561 if (drvdata->enable) {
562 ret = -EBUSY;
563 goto out;
564 }
565
566 if (!strcmp(str, str_dcc_func_type[DCC_FUNC_TYPE_CAPTURE]))
567 drvdata->func_type = DCC_FUNC_TYPE_CAPTURE;
568 else if (!strcmp(str, str_dcc_func_type[DCC_FUNC_TYPE_CRC]))
569 drvdata->func_type = DCC_FUNC_TYPE_CRC;
570 else {
571 ret = -EINVAL;
572 goto out;
573 }
574
575 ret = size;
576out:
577 mutex_unlock(&drvdata->mutex);
578 return ret;
579}
580static DEVICE_ATTR(func_type, 0644,
581 dcc_show_func_type, dcc_store_func_type);
582
583static ssize_t dcc_show_data_sink(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
586 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
587
588 return scnprintf(buf, PAGE_SIZE, "%s\n",
589 str_dcc_data_sink[drvdata->data_sink]);
590}
591
592static ssize_t dcc_store_data_sink(struct device *dev,
593 struct device_attribute *attr,
594 const char *buf, size_t size)
595{
596 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
597 char str[10] = "";
598 int ret;
599
600 if (strlen(buf) >= 10)
601 return -EINVAL;
602
Mukesh Ojha7085e052018-02-21 19:17:54 +0530603 if (sscanf(buf, "%9s", str) != 1)
604 return -EINVAL;
Mukesh Ojhacdbf01c2017-12-04 17:46:09 +0530605
606 mutex_lock(&drvdata->mutex);
607 if (drvdata->enable) {
608 ret = -EBUSY;
609 goto out;
610 }
611
612 if (!strcmp(str, str_dcc_data_sink[DCC_DATA_SINK_SRAM]))
613 drvdata->data_sink = DCC_DATA_SINK_SRAM;
614 else if (!strcmp(str, str_dcc_data_sink[DCC_DATA_SINK_ATB]))
615 drvdata->data_sink = DCC_DATA_SINK_ATB;
616 else {
617 ret = -EINVAL;
618 goto out;
619 }
620
621 ret = size;
622out:
623 mutex_unlock(&drvdata->mutex);
624 return ret;
625}
626static DEVICE_ATTR(data_sink, 0644,
627 dcc_show_data_sink, dcc_store_data_sink);
628
629static ssize_t dcc_store_trigger(struct device *dev,
630 struct device_attribute *attr,
631 const char *buf, size_t size)
632{
633 int ret = 0;
634 unsigned long val;
635 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
636
637 if (kstrtoul(buf, 16, &val))
638 return -EINVAL;
639 if (val != 1)
640 return -EINVAL;
641
642 ret = dcc_xpu_unlock(drvdata);
643 if (ret)
644 return ret;
645
646 ret = dcc_sw_trigger(drvdata);
647 if (!ret)
648 ret = size;
649
650 dcc_xpu_lock(drvdata);
651 return ret;
652}
653static DEVICE_ATTR(trigger, 0200, NULL, dcc_store_trigger);
654
655static ssize_t dcc_show_enable(struct device *dev,
656 struct device_attribute *attr, char *buf)
657{
658 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
659
660 return scnprintf(buf, PAGE_SIZE, "%u\n",
661 (unsigned int)drvdata->enable);
662}
663
664static ssize_t dcc_store_enable(struct device *dev,
665 struct device_attribute *attr,
666 const char *buf, size_t size)
667{
668 int ret = 0;
669 unsigned long val;
670 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
671
672 if (kstrtoul(buf, 16, &val))
673 return -EINVAL;
674
675 ret = dcc_xpu_unlock(drvdata);
676 if (ret)
677 return ret;
678
679 if (val)
680 ret = dcc_enable(drvdata);
681 else
682 dcc_disable(drvdata);
683
684 if (!ret)
685 ret = size;
686
687 dcc_xpu_lock(drvdata);
688 return ret;
689
690}
691static DEVICE_ATTR(enable, 0644, dcc_show_enable,
692 dcc_store_enable);
693
694static ssize_t dcc_show_config(struct device *dev,
695 struct device_attribute *attr, char *buf)
696{
697 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
698 struct dcc_config_entry *entry;
699 char local_buf[64];
700 int len = 0, count = 0;
701
702 buf[0] = '\0';
703
704 mutex_lock(&drvdata->mutex);
705 list_for_each_entry(entry, &drvdata->config_head, list) {
706 len = snprintf(local_buf, 64,
707 "Index: 0x%x, Base: 0x%x, Offset: 0x%x, len: 0x%x\n",
708 entry->index, entry->base,
709 entry->offset, entry->len);
710
711 if ((count + len) > PAGE_SIZE) {
712 dev_err(dev, "DCC: Couldn't write complete config!\n");
713 break;
714 }
715
716 strlcat(buf, local_buf, PAGE_SIZE);
717 count += len;
718 }
719
720 mutex_unlock(&drvdata->mutex);
721
722 return count;
723}
724
725static int dcc_config_add(struct dcc_drvdata *drvdata, unsigned int addr,
726 unsigned int len)
727{
728 int ret;
729 struct dcc_config_entry *entry, *pentry;
730 unsigned int base, offset;
731
732 mutex_lock(&drvdata->mutex);
733
734 if (!len) {
735 dev_err(drvdata->dev, "DCC: Invalid length!\n");
736 ret = -EINVAL;
737 goto err;
738 }
739
740 base = addr & BM(4, 31);
741
742 if (!list_empty(&drvdata->config_head)) {
743 pentry = list_last_entry(&drvdata->config_head,
744 struct dcc_config_entry, list);
745
746 if (addr >= (pentry->base + pentry->offset) &&
747 addr <= (pentry->base + pentry->offset + MAX_DCC_OFFSET)) {
748
749 /* Re-use base address from last entry */
750 base = pentry->base;
751
752 /*
753 * Check if new address is contiguous to last entry's
754 * addresses. If yes then we can re-use last entry and
755 * just need to update its length.
756 */
757 if ((pentry->len * 4 + pentry->base + pentry->offset)
758 == addr) {
759 len += pentry->len;
760
761 /*
762 * Check if last entry can hold additional new
763 * length. If yes then we don't need to create
764 * a new entry else we need to add a new entry
765 * with same base but updated offset.
766 */
767 if (len > MAX_DCC_LEN)
768 pentry->len = MAX_DCC_LEN;
769 else
770 pentry->len = len;
771
772 /*
773 * Update start addr and len for remaining
774 * addresses, which will be part of new
775 * entry.
776 */
777 addr = pentry->base + pentry->offset +
778 pentry->len * 4;
779 len -= pentry->len;
780 }
781 }
782 }
783
784 offset = addr - base;
785
786 while (len) {
787 entry = devm_kzalloc(drvdata->dev, sizeof(*entry), GFP_KERNEL);
788 if (!entry) {
789 ret = -ENOMEM;
790 goto err;
791 }
792
793 entry->base = base;
794 entry->offset = offset;
795 entry->len = min_t(uint32_t, len, MAX_DCC_LEN);
796 entry->index = drvdata->nr_config++;
797 INIT_LIST_HEAD(&entry->list);
798 list_add_tail(&entry->list, &drvdata->config_head);
799
800 len -= entry->len;
801 offset += MAX_DCC_LEN * 4;
802 }
803
804 mutex_unlock(&drvdata->mutex);
805 return 0;
806err:
807 mutex_unlock(&drvdata->mutex);
808 return ret;
809}
810
811static ssize_t dcc_store_config(struct device *dev,
812 struct device_attribute *attr,
813 const char *buf, size_t size)
814{
815 int ret;
816 unsigned int base, len;
817 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
818 int nval;
819
820 nval = sscanf(buf, "%x %u", &base, &len);
821 if (nval <= 0 || nval > 2)
822 return -EINVAL;
823
824 if (nval == 1)
825 len = 1;
826
827 ret = dcc_config_add(drvdata, base, len);
828 if (ret)
829 return ret;
830
831 return size;
832
833}
834static DEVICE_ATTR(config, 0644, dcc_show_config,
835 dcc_store_config);
836
837static void dcc_config_reset(struct dcc_drvdata *drvdata)
838{
839 struct dcc_config_entry *entry, *temp;
840
841 mutex_lock(&drvdata->mutex);
842
843 list_for_each_entry_safe(entry, temp, &drvdata->config_head, list) {
844 list_del(&entry->list);
845 devm_kfree(drvdata->dev, entry);
846 drvdata->nr_config--;
847 }
848
849 mutex_unlock(&drvdata->mutex);
850}
851
852static ssize_t dcc_store_config_reset(struct device *dev,
853 struct device_attribute *attr,
854 const char *buf, size_t size)
855{
856 unsigned long val;
857 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
858
859 if (kstrtoul(buf, 16, &val))
860 return -EINVAL;
861
862 if (val)
863 dcc_config_reset(drvdata);
864
865 return size;
866}
867static DEVICE_ATTR(config_reset, 0200, NULL, dcc_store_config_reset);
868
869static ssize_t dcc_show_crc_error(struct device *dev,
870 struct device_attribute *attr, char *buf)
871{
872 int ret;
873 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
874
875 ret = dcc_xpu_unlock(drvdata);
876 if (ret)
877 return ret;
878
879 mutex_lock(&drvdata->mutex);
880 if (!drvdata->enable) {
881 ret = -EINVAL;
882 goto err;
883 }
884
885 ret = scnprintf(buf, PAGE_SIZE, "%u\n",
886 (unsigned int)BVAL(dcc_readl(drvdata, DCC_STATUS), 0));
887err:
888 mutex_unlock(&drvdata->mutex);
889 dcc_xpu_lock(drvdata);
890 return ret;
891}
892static DEVICE_ATTR(crc_error, 0444, dcc_show_crc_error, NULL);
893
894static ssize_t dcc_show_ready(struct device *dev,
895 struct device_attribute *attr, char *buf)
896{
897 int ret;
898 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
899
900 ret = dcc_xpu_unlock(drvdata);
901 if (ret)
902 return ret;
903
904 mutex_lock(&drvdata->mutex);
905 if (!drvdata->enable) {
906 ret = -EINVAL;
907 goto err;
908 }
909
910 ret = scnprintf(buf, PAGE_SIZE, "%u\n",
911 (unsigned int)BVAL(dcc_readl(drvdata, DCC_STATUS), 4));
912err:
913 mutex_unlock(&drvdata->mutex);
914 dcc_xpu_lock(drvdata);
915 return ret;
916}
917static DEVICE_ATTR(ready, 0444, dcc_show_ready, NULL);
918
919static ssize_t dcc_show_interrupt_disable(struct device *dev,
920 struct device_attribute *attr,
921 char *buf)
922{
923 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
924
925 return scnprintf(buf, PAGE_SIZE, "%u\n",
926 (unsigned int)drvdata->interrupt_disable);
927}
928
929static ssize_t dcc_store_interrupt_disable(struct device *dev,
930 struct device_attribute *attr,
931 const char *buf, size_t size)
932{
933 unsigned long val;
934 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
935
936 if (kstrtoul(buf, 16, &val))
937 return -EINVAL;
938
939 mutex_lock(&drvdata->mutex);
940 drvdata->interrupt_disable = (val ? 1:0);
941 mutex_unlock(&drvdata->mutex);
942 return size;
943}
944static DEVICE_ATTR(interrupt_disable, 0644,
945 dcc_show_interrupt_disable, dcc_store_interrupt_disable);
946
947static ssize_t dcc_show_rpm_sw_trigger_on(struct device *dev,
948 struct device_attribute *attr,
949 char *buf)
950{
951 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
952
953 return scnprintf(buf, PAGE_SIZE, "%u\n",
954 (unsigned int)drvdata->rpm_trig_req.enable);
955}
956
957static ssize_t dcc_store_rpm_sw_trigger_on(struct device *dev,
958 struct device_attribute *attr,
959 const char *buf, size_t size)
960{
961 unsigned long val;
962 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
963
964 if (kstrtoul(buf, 16, &val))
965 return -EINVAL;
966
967 mutex_lock(&drvdata->mutex);
968 __dcc_rpm_sw_trigger(drvdata, !!val);
969 mutex_unlock(&drvdata->mutex);
970 return size;
971}
972static DEVICE_ATTR(rpm_sw_trigger_on, 0644,
973 dcc_show_rpm_sw_trigger_on, dcc_store_rpm_sw_trigger_on);
974
975static ssize_t dcc_store_xpu_unlock(struct device *dev,
976 struct device_attribute *attr,
977 const char *buf, size_t size)
978{
979 int ret;
980 unsigned long val;
981 struct dcc_drvdata *drvdata = dev_get_drvdata(dev);
982
983 if (kstrtoul(buf, 10, &val))
984 return -EINVAL;
985
986 ret = val ? dcc_xpu_unlock(drvdata) : dcc_xpu_lock(drvdata);
987 if (!ret)
988 ret = size;
989
990 return ret;
991}
992static DEVICE_ATTR(xpu_unlock, 0200, NULL, dcc_store_xpu_unlock);
993
994static const struct device_attribute *dcc_attrs[] = {
995 &dev_attr_func_type,
996 &dev_attr_data_sink,
997 &dev_attr_trigger,
998 &dev_attr_enable,
999 &dev_attr_config,
1000 &dev_attr_config_reset,
1001 &dev_attr_ready,
1002 &dev_attr_crc_error,
1003 &dev_attr_interrupt_disable,
1004 &dev_attr_rpm_sw_trigger_on,
1005 &dev_attr_xpu_unlock,
1006 NULL,
1007};
1008
1009static int dcc_create_files(struct device *dev,
1010 const struct device_attribute **attrs)
1011{
1012 int ret = 0, i;
1013
1014 for (i = 0; attrs[i] != NULL; i++) {
1015 ret = device_create_file(dev, attrs[i]);
1016 if (ret) {
1017 dev_err(dev, "DCC: Couldn't create sysfs attribute: %s!\n",
1018 attrs[i]->attr.name);
1019 break;
1020 }
1021 }
1022 return ret;
1023}
1024
1025static int dcc_sram_open(struct inode *inode, struct file *file)
1026{
1027 struct dcc_drvdata *drvdata = container_of(inode->i_cdev,
1028 struct dcc_drvdata,
1029 sram_dev);
1030 file->private_data = drvdata;
1031
1032 return dcc_xpu_unlock(drvdata);
1033}
1034
1035static ssize_t dcc_sram_read(struct file *file, char __user *data,
1036 size_t len, loff_t *ppos)
1037{
1038 int ret;
1039 unsigned char *buf;
1040 struct dcc_drvdata *drvdata = file->private_data;
1041
1042 /* EOF check */
1043 if (drvdata->ram_size <= *ppos)
1044 return 0;
1045
1046 if ((*ppos + len) > drvdata->ram_size)
1047 len = (drvdata->ram_size - *ppos);
1048
1049 buf = kzalloc(len, GFP_KERNEL);
1050 if (!buf)
1051 return -ENOMEM;
1052
1053 ret = clk_prepare_enable(drvdata->clk);
1054 if (ret) {
1055 kfree(buf);
1056 return ret;
1057 }
1058
1059 memcpy_fromio(buf, (drvdata->ram_base + *ppos), len);
1060
1061 clk_disable_unprepare(drvdata->clk);
1062
1063 if (copy_to_user(data, buf, len)) {
1064 dev_err(drvdata->dev,
1065 "DCC: Couldn't copy all data to user!\n");
1066 kfree(buf);
1067 return -EFAULT;
1068 }
1069
1070 *ppos += len;
1071
1072 kfree(buf);
1073
1074 return len;
1075}
1076
1077static int dcc_sram_release(struct inode *inode, struct file *file)
1078{
1079 struct dcc_drvdata *drvdata = file->private_data;
1080
1081 return dcc_xpu_lock(drvdata);
1082}
1083
1084static const struct file_operations dcc_sram_fops = {
1085 .owner = THIS_MODULE,
1086 .open = dcc_sram_open,
1087 .read = dcc_sram_read,
1088 .release = dcc_sram_release,
1089 .llseek = no_llseek,
1090};
1091
1092static int dcc_sram_dev_register(struct dcc_drvdata *drvdata)
1093{
1094 int ret;
1095 struct device *device;
1096 dev_t dev;
1097
1098 ret = alloc_chrdev_region(&dev, 0, 1, drvdata->sram_node);
1099 if (ret)
1100 goto err_alloc;
1101
1102 cdev_init(&drvdata->sram_dev, &dcc_sram_fops);
1103
1104 drvdata->sram_dev.owner = THIS_MODULE;
1105 ret = cdev_add(&drvdata->sram_dev, dev, 1);
1106 if (ret)
1107 goto err_cdev_add;
1108
1109 drvdata->sram_class = class_create(THIS_MODULE,
1110 drvdata->sram_node);
1111 if (IS_ERR(drvdata->sram_class)) {
1112 ret = PTR_ERR(drvdata->sram_class);
1113 goto err_class_create;
1114 }
1115
1116 device = device_create(drvdata->sram_class, NULL,
1117 drvdata->sram_dev.dev, drvdata,
1118 drvdata->sram_node);
1119 if (IS_ERR(device)) {
1120 ret = PTR_ERR(device);
1121 goto err_dev_create;
1122 }
1123
1124 return 0;
1125err_dev_create:
1126 class_destroy(drvdata->sram_class);
1127err_class_create:
1128 cdev_del(&drvdata->sram_dev);
1129err_cdev_add:
1130 unregister_chrdev_region(drvdata->sram_dev.dev, 1);
1131err_alloc:
1132 return ret;
1133}
1134
1135static void dcc_sram_dev_deregister(struct dcc_drvdata *drvdata)
1136{
1137 device_destroy(drvdata->sram_class, drvdata->sram_dev.dev);
1138 class_destroy(drvdata->sram_class);
1139 cdev_del(&drvdata->sram_dev);
1140 unregister_chrdev_region(drvdata->sram_dev.dev, 1);
1141}
1142
1143static int dcc_sram_dev_init(struct dcc_drvdata *drvdata)
1144{
1145 int ret = 0;
1146 size_t node_size;
1147 char *node_name = "dcc_sram";
1148 struct device *dev = drvdata->dev;
1149
1150 node_size = strlen(node_name) + 1;
1151
1152 drvdata->sram_node = devm_kzalloc(dev, node_size, GFP_KERNEL);
1153 if (!drvdata->sram_node)
1154 return -ENOMEM;
1155
1156 strlcpy(drvdata->sram_node, node_name, node_size);
1157 ret = dcc_sram_dev_register(drvdata);
1158 if (ret)
1159 dev_err(drvdata->dev, "DCC: sram node not registered.\n");
1160
1161 return ret;
1162}
1163
1164static void dcc_sram_dev_exit(struct dcc_drvdata *drvdata)
1165{
1166 dcc_sram_dev_deregister(drvdata);
1167}
1168
1169static void dcc_allocate_dump_mem(struct dcc_drvdata *drvdata)
1170{
1171 int ret;
1172 struct device *dev = drvdata->dev;
1173 struct msm_dump_entry reg_dump_entry, sram_dump_entry;
1174
1175 /* Allocate memory for dcc reg dump */
1176 drvdata->reg_buf = devm_kzalloc(dev, drvdata->reg_size, GFP_KERNEL);
1177 if (drvdata->reg_buf) {
1178 strlcpy(drvdata->reg_data.name, "KDCC_REG",
1179 sizeof(drvdata->reg_data.name));
1180 drvdata->reg_data.addr = virt_to_phys(drvdata->reg_buf);
1181 drvdata->reg_data.len = drvdata->reg_size;
1182 reg_dump_entry.id = MSM_DUMP_DATA_DCC_REG;
1183 reg_dump_entry.addr = virt_to_phys(&drvdata->reg_data);
1184 ret = msm_dump_data_register(MSM_DUMP_TABLE_APPS,
1185 &reg_dump_entry);
1186 if (ret) {
1187 dev_err(dev, "DCC REG dump setup failed\n");
1188 devm_kfree(dev, drvdata->reg_buf);
1189 }
1190 } else {
1191 dev_err(dev, "DCC REG dump allocation failed\n");
1192 }
1193
1194 /* Allocate memory for dcc sram dump */
1195 drvdata->sram_buf = devm_kzalloc(dev, drvdata->ram_size, GFP_KERNEL);
1196 if (drvdata->sram_buf) {
1197 strlcpy(drvdata->sram_data.name, "KDCC_SRAM",
1198 sizeof(drvdata->sram_data.name));
1199 drvdata->sram_data.addr = virt_to_phys(drvdata->sram_buf);
1200 drvdata->sram_data.len = drvdata->ram_size;
1201 sram_dump_entry.id = MSM_DUMP_DATA_DCC_SRAM;
1202 sram_dump_entry.addr = virt_to_phys(&drvdata->sram_data);
1203 ret = msm_dump_data_register(MSM_DUMP_TABLE_APPS,
1204 &sram_dump_entry);
1205 if (ret) {
1206 dev_err(dev, "DCC SRAM dump setup failed\n");
1207 devm_kfree(dev, drvdata->sram_buf);
1208 }
1209 } else {
1210 dev_err(dev, "DCC SRAM dump allocation failed\n");
1211 }
1212}
1213
1214static int dcc_probe(struct platform_device *pdev)
1215{
1216 int ret, i;
1217 struct device *dev = &pdev->dev;
1218 struct dcc_drvdata *drvdata;
1219 struct resource *res;
1220 const char *data_sink;
1221
1222 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1223 if (!drvdata)
1224 return -ENOMEM;
1225
1226 drvdata->dev = &pdev->dev;
1227 platform_set_drvdata(pdev, drvdata);
1228
1229 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dcc-base");
1230 if (!res)
1231 return -EINVAL;
1232
1233 drvdata->reg_size = resource_size(res);
1234 drvdata->base = devm_ioremap(dev, res->start, resource_size(res));
1235 if (!drvdata->base)
1236 return -ENOMEM;
1237
1238 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1239 "dcc-ram-base");
1240 if (!res)
1241 return -EINVAL;
1242
1243 drvdata->ram_size = resource_size(res);
1244 drvdata->ram_base = devm_ioremap(dev, res->start, resource_size(res));
1245 if (!drvdata->ram_base)
1246 return -ENOMEM;
1247
1248 drvdata->clk = devm_clk_get(dev, "apb_pclk");
1249 if (IS_ERR(drvdata->clk)) {
1250 ret = PTR_ERR(drvdata->clk);
1251 goto err;
1252 }
1253
1254 drvdata->save_reg = of_property_read_bool(pdev->dev.of_node,
1255 "qcom,save-reg");
1256
1257 mutex_init(&drvdata->mutex);
1258
1259 INIT_LIST_HEAD(&drvdata->config_head);
1260 drvdata->nr_config = 0;
1261 drvdata->xpu_scm_avail = 0;
1262
1263 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1264 "dcc-xpu-base");
1265 if (res) {
1266 if (scm_is_call_available(SCM_SVC_MP,
1267 SCM_SVC_DISABLE_XPU) > 0) {
1268 drvdata->xpu_scm_avail = 1;
1269 drvdata->xpu_addr = res->start;
1270 } else {
1271 dev_err(dev, "scm call is not available\n");
1272 return -EINVAL;
1273 }
1274 } else {
1275 dev_info(dev, "DCC XPU is not specified\n");
1276 }
1277
1278 ret = dcc_xpu_unlock(drvdata);
1279 if (ret)
1280 goto err;
1281
1282 ret = clk_prepare_enable(drvdata->clk);
1283 if (ret) {
1284 dcc_xpu_lock(drvdata);
1285 goto err;
1286 }
1287
1288 memset_io(drvdata->ram_base, 0, drvdata->ram_size);
1289
1290 dcc_xpu_lock(drvdata);
1291
1292 clk_disable_unprepare(drvdata->clk);
1293
1294 drvdata->data_sink = DCC_DATA_SINK_SRAM;
1295 ret = of_property_read_string(pdev->dev.of_node, "qcom,data-sink",
1296 &data_sink);
1297 if (!ret) {
1298 for (i = 0; i < ARRAY_SIZE(str_dcc_data_sink); i++)
1299 if (!strcmp(data_sink, str_dcc_data_sink[i])) {
1300 drvdata->data_sink = i;
1301 break;
1302 }
1303
1304 if (i == ARRAY_SIZE(str_dcc_data_sink)) {
1305 dev_err(dev, "Unknown sink type for DCC! Using '%s' as data sink\n",
1306 str_dcc_data_sink[drvdata->data_sink]);
1307 }
1308 }
1309
1310 ret = dcc_sram_dev_init(drvdata);
1311 if (ret)
1312 goto err;
1313
1314 ret = dcc_create_files(dev, dcc_attrs);
1315 if (ret)
1316 goto err;
1317
1318 dcc_allocate_dump_mem(drvdata);
1319 return 0;
1320err:
1321 return ret;
1322}
1323
1324static int dcc_remove(struct platform_device *pdev)
1325{
1326 struct dcc_drvdata *drvdata = platform_get_drvdata(pdev);
1327
1328 dcc_sram_dev_exit(drvdata);
1329
1330 dcc_config_reset(drvdata);
1331
1332 return 0;
1333}
1334
1335static const struct of_device_id msm_dcc_match[] = {
1336 { .compatible = "qcom,dcc"},
1337 {}
1338};
1339
1340static struct platform_driver dcc_driver = {
1341 .probe = dcc_probe,
1342 .remove = dcc_remove,
1343 .driver = {
1344 .name = "msm-dcc",
1345 .owner = THIS_MODULE,
1346 .of_match_table = msm_dcc_match,
1347 },
1348};
1349
1350static int __init dcc_init(void)
1351{
1352 return platform_driver_register(&dcc_driver);
1353}
1354module_init(dcc_init);
1355
1356static void __exit dcc_exit(void)
1357{
1358 platform_driver_unregister(&dcc_driver);
1359}
1360module_exit(dcc_exit);
1361
1362MODULE_LICENSE("GPL v2");
1363MODULE_DESCRIPTION("MSM data capture and compare engine");