hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/drivers/devfreq/event/rockchip-dfi.c
....@@ -1,15 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
34 * Author: Lin Huang <hl@rock-chips.com>
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms and conditions of the GNU General Public License,
7
- * version 2, as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope it will be useful, but WITHOUT
10
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
- * more details.
135 */
146
157 #include <linux/clk.h>
....@@ -26,8 +18,12 @@
2618 #include <linux/list.h>
2719 #include <linux/of.h>
2820
21
+#include <soc/rockchip/rk3399_grf.h>
22
+
2923 #define PX30_PMUGRF_OS_REG2 0x208
3024 #define PX30_PMUGRF_OS_REG3 0x20c
25
+
26
+#define RK3588_PMUGRF_OS_REG(n) (0x200 + (n) * 4)
3127
3228 #define RK3128_GRF_SOC_CON0 0x140
3329 #define RK3128_GRF_OS_REG1 0x1cc
....@@ -60,14 +56,18 @@
6056 #define RK3528_PMUGRF_OS_REG18 0x248
6157 #define RK3528_PMUGRF_OS_REG19 0x24c
6258
63
-#define MAX_DMC_NUM_CH 2
59
+#define MAX_DMC_NUM_CH 4
6460 #define READ_DRAMTYPE_INFO(n) (((n) >> 13) & 0x7)
6561 #define READ_CH_INFO(n) (((n) >> 28) & 0x3)
6662 #define READ_DRAMTYPE_INFO_V3(n, m) ((((n) >> 13) & 0x7) | ((((m) >> 12) & 0x3) << 3))
6763 #define READ_SYSREG_VERSION(m) (((m) >> 28) & 0xf)
64
+#define READ_LP5_BANK_MODE(m) (((m) >> 1) & 0x3)
65
+#define READ_LP5_CKR(m) (((m) >> 0) & 0x1)
6866 /* DDRMON_CTRL */
6967 #define DDRMON_CTRL 0x04
70
-#define CLR_DDRMON_CTRL (0x3f0000 << 0)
68
+#define CLR_DDRMON_CTRL (0xffff0000 << 0)
69
+#define LPDDR5_BANK_MODE(m) ((0x30000 | ((m) & 0x3)) << 7)
70
+#define LPDDR5_EN (0x10001 << 6)
7171 #define DDR4_EN (0x10001 << 5)
7272 #define LPDDR4_EN (0x10001 << 4)
7373 #define HARDWARE_EN (0x10001 << 3)
....@@ -91,12 +91,14 @@
9191 LPDDR3 = 6,
9292 LPDDR4 = 7,
9393 LPDDR4X = 8,
94
+ LPDDR5 = 9,
95
+ DDR5 = 10,
9496 UNUSED = 0xFF
9597 };
9698
9799 struct dmc_usage {
98
- u32 access;
99
- u32 total;
100
+ u64 access;
101
+ u64 total;
100102 };
101103
102104 /*
....@@ -115,7 +117,13 @@
115117 struct regmap *regmap_pmugrf;
116118 struct clk *clk;
117119 u32 dram_type;
120
+ u32 mon_idx;
118121 u32 count_rate;
122
+ u32 dram_dynamic_info_reg;
123
+ /* 0: BG mode, 1: 16 Bank mode, 2: 8 bank mode */
124
+ u32 lp5_bank_mode;
125
+ /* 0: clk:dqs = 1:2, 1: 1:4 */
126
+ u32 lp5_ckr;
119127 /*
120128 * available mask, 1: available, 0: not available
121129 * each bit represent a channel
....@@ -352,28 +360,56 @@
352360 {
353361 struct rockchip_dfi *info = devfreq_event_get_drvdata(edev);
354362 void __iomem *dfi_regs = info->regs;
363
+ u32 mon_idx = 0, val_6 = 0;
364
+ u32 i;
355365
356
- /* clear DDRMON_CTRL setting */
357
- writel_relaxed(CLR_DDRMON_CTRL, dfi_regs + DDRMON_CTRL);
366
+ if (info->mon_idx)
367
+ mon_idx = info->mon_idx;
358368
359
- /* set ddr type to dfi */
360
- if (info->dram_type == LPDDR3 || info->dram_type == LPDDR2)
361
- writel_relaxed(LPDDR2_3_EN, dfi_regs + DDRMON_CTRL);
362
- else if (info->dram_type == LPDDR4 || info->dram_type == LPDDR4X)
363
- writel_relaxed(LPDDR4_EN, dfi_regs + DDRMON_CTRL);
364
- else if (info->dram_type == DDR4)
365
- writel_relaxed(DDR4_EN, dfi_regs + DDRMON_CTRL);
369
+ if (info->dram_dynamic_info_reg)
370
+ regmap_read(info->regmap_pmugrf, info->dram_dynamic_info_reg, &val_6);
366371
367
- /* enable count, use software mode */
368
- writel_relaxed(SOFTWARE_EN, dfi_regs + DDRMON_CTRL);
372
+ if (info->dram_type == LPDDR5) {
373
+ info->lp5_bank_mode = READ_LP5_BANK_MODE(val_6);
374
+ info->lp5_ckr = READ_LP5_CKR(val_6);
375
+ }
376
+
377
+ for (i = 0; i < MAX_DMC_NUM_CH; i++) {
378
+ if (!(info->ch_msk & BIT(i)))
379
+ continue;
380
+ /* clear DDRMON_CTRL setting */
381
+ writel_relaxed(CLR_DDRMON_CTRL, dfi_regs + i * mon_idx + DDRMON_CTRL);
382
+
383
+ /* set ddr type to dfi */
384
+ if (info->dram_type == LPDDR3 || info->dram_type == LPDDR2)
385
+ writel_relaxed(LPDDR2_3_EN, dfi_regs + i * mon_idx + DDRMON_CTRL);
386
+ else if (info->dram_type == LPDDR4 || info->dram_type == LPDDR4X)
387
+ writel_relaxed(LPDDR4_EN, dfi_regs + i * mon_idx + DDRMON_CTRL);
388
+ else if (info->dram_type == DDR4)
389
+ writel_relaxed(DDR4_EN, dfi_regs + i * mon_idx + DDRMON_CTRL);
390
+ else if (info->dram_type == LPDDR5)
391
+ writel_relaxed(LPDDR5_EN | LPDDR5_BANK_MODE(info->lp5_bank_mode),
392
+ dfi_regs + i * mon_idx + DDRMON_CTRL);
393
+
394
+ /* enable count, use software mode */
395
+ writel_relaxed(SOFTWARE_EN, dfi_regs + i * mon_idx + DDRMON_CTRL);
396
+ }
369397 }
370398
371399 static void rockchip_dfi_stop_hardware_counter(struct devfreq_event_dev *edev)
372400 {
373401 struct rockchip_dfi *info = devfreq_event_get_drvdata(edev);
374402 void __iomem *dfi_regs = info->regs;
403
+ u32 mon_idx = 0, i;
375404
376
- writel_relaxed(SOFTWARE_DIS, dfi_regs + DDRMON_CTRL);
405
+ if (info->mon_idx)
406
+ mon_idx = info->mon_idx;
407
+
408
+ for (i = 0; i < MAX_DMC_NUM_CH; i++) {
409
+ if (!(info->ch_msk & BIT(i)))
410
+ continue;
411
+ writel_relaxed(SOFTWARE_DIS, dfi_regs + i * mon_idx + DDRMON_CTRL);
412
+ }
377413 }
378414
379415 static int rockchip_dfi_get_busier_ch(struct devfreq_event_dev *edev)
....@@ -382,10 +418,12 @@
382418 u32 tmp, max = 0;
383419 u32 i, busier_ch = 0;
384420 void __iomem *dfi_regs = info->regs;
385
- u32 count_rate = 1;
421
+ u32 mon_idx = 0x20, count_rate = 1;
386422
387423 rockchip_dfi_stop_hardware_counter(edev);
388424
425
+ if (info->mon_idx)
426
+ mon_idx = info->mon_idx;
389427 if (info->count_rate)
390428 count_rate = info->count_rate;
391429
....@@ -394,14 +432,17 @@
394432 if (!(info->ch_msk & BIT(i)))
395433 continue;
396434
435
+ /* rk3588 counter is dfi clk rate */
397436 info->ch_usage[i].total = readl_relaxed(dfi_regs +
398
- DDRMON_CH0_COUNT_NUM + i * 20) * count_rate;
437
+ DDRMON_CH0_COUNT_NUM + i * mon_idx) * count_rate;
399438
400
- /* LPDDR4 and LPDDR4X BL = 16,other DDR type BL = 8 */
439
+ /* LPDDR5 LPDDR4 and LPDDR4X BL = 16,other DDR type BL = 8 */
401440 tmp = readl_relaxed(dfi_regs +
402
- DDRMON_CH0_DFI_ACCESS_NUM + i * 20);
441
+ DDRMON_CH0_DFI_ACCESS_NUM + i * mon_idx);
403442 if (info->dram_type == LPDDR4 || info->dram_type == LPDDR4X)
404443 tmp *= 8;
444
+ else if (info->dram_type == LPDDR5)
445
+ tmp *= 16 / (4 << info->lp5_ckr);
405446 else
406447 tmp *= 4;
407448 info->ch_usage[i].access = tmp;
....@@ -473,6 +514,45 @@
473514 .get_event = rockchip_dfi_get_event,
474515 .set_event = rockchip_dfi_set_event,
475516 };
517
+
518
+static __maybe_unused __init int rk3588_dfi_init(struct platform_device *pdev,
519
+ struct rockchip_dfi *data,
520
+ struct devfreq_event_desc *desc)
521
+{
522
+ struct device_node *np = pdev->dev.of_node;
523
+ struct resource *res;
524
+ u32 val_2, val_3, val_4;
525
+
526
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
527
+ data->regs = devm_ioremap_resource(&pdev->dev, res);
528
+ if (IS_ERR(data->regs))
529
+ return PTR_ERR(data->regs);
530
+
531
+ data->regmap_pmugrf = syscon_regmap_lookup_by_phandle(np, "rockchip,pmu_grf");
532
+ if (IS_ERR(data->regmap_pmugrf))
533
+ return PTR_ERR(data->regmap_pmugrf);
534
+
535
+ regmap_read(data->regmap_pmugrf, RK3588_PMUGRF_OS_REG(2), &val_2);
536
+ regmap_read(data->regmap_pmugrf, RK3588_PMUGRF_OS_REG(3), &val_3);
537
+ regmap_read(data->regmap_pmugrf, RK3588_PMUGRF_OS_REG(4), &val_4);
538
+ if (READ_SYSREG_VERSION(val_3) >= 0x3)
539
+ data->dram_type = READ_DRAMTYPE_INFO_V3(val_2, val_3);
540
+ else
541
+ data->dram_type = READ_DRAMTYPE_INFO(val_2);
542
+
543
+ data->mon_idx = 0x4000;
544
+ if (data->dram_type == LPDDR5)
545
+ data->count_rate = 1;
546
+ else
547
+ data->count_rate = 2;
548
+ data->dram_dynamic_info_reg = RK3588_PMUGRF_OS_REG(6);
549
+ data->ch_msk = READ_CH_INFO(val_2) | READ_CH_INFO(val_4) << 2;
550
+ data->clk = NULL;
551
+
552
+ desc->ops = &rockchip_dfi_ops;
553
+
554
+ return 0;
555
+}
476556
477557 static __maybe_unused __init int px30_dfi_init(struct platform_device *pdev,
478558 struct rockchip_dfi *data,
....@@ -586,12 +666,10 @@
586666 struct devfreq_event_desc *desc)
587667 {
588668 struct device *dev = &pdev->dev;
589
- struct resource *res;
590669 struct device_node *np = pdev->dev.of_node, *node;
591670 u32 val;
592671
593
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
594
- data->regs = devm_ioremap_resource(&pdev->dev, res);
672
+ data->regs = devm_platform_ioremap_resource(pdev, 0);
595673 if (IS_ERR(data->regs))
596674 return PTR_ERR(data->regs);
597675
....@@ -605,6 +683,7 @@
605683 node = of_parse_phandle(np, "rockchip,pmu", 0);
606684 if (node) {
607685 data->regmap_pmu = syscon_node_to_regmap(node);
686
+ of_node_put(node);
608687 if (IS_ERR(data->regmap_pmu))
609688 return PTR_ERR(data->regmap_pmu);
610689 }
....@@ -708,9 +787,15 @@
708787 #ifdef CONFIG_CPU_RK3528
709788 { .compatible = "rockchip,rk3528-dfi", .data = rk3528_dfi_init },
710789 #endif
790
+#ifdef CONFIG_CPU_RK3562
791
+ { .compatible = "rockchip,rk3562-dfi", .data = px30_dfi_init },
792
+#endif
711793 #ifdef CONFIG_CPU_RK3568
712794 { .compatible = "rockchip,rk3568-dfi", .data = px30_dfi_init },
713795 #endif
796
+#ifdef CONFIG_CPU_RK3588
797
+ { .compatible = "rockchip,rk3588-dfi", .data = rk3588_dfi_init },
798
+#endif
714799 #ifdef CONFIG_CPU_RV1126
715800 { .compatible = "rockchip,rv1126-dfi", .data = px30_dfi_init },
716801 #endif