1 // SPDX-License-Identifier: GPL-2.0
3 * Tegra20 External Memory Controller driver
5 * Author: Dmitry Osipenko <digetx@gmail.com>
9 #include <linux/clk/tegra.h>
10 #include <linux/completion.h>
11 #include <linux/debugfs.h>
12 #include <linux/err.h>
13 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/sort.h>
20 #include <linux/types.h>
22 #include <soc/tegra/fuse.h>
24 #define EMC_INTSTATUS 0x000
25 #define EMC_INTMASK 0x004
27 #define EMC_TIMING_CONTROL 0x028
36 #define EMC_RD_RCD 0x04c
37 #define EMC_WR_RCD 0x050
39 #define EMC_REXT 0x058
41 #define EMC_QUSE 0x060
42 #define EMC_QRST 0x064
43 #define EMC_QSAFE 0x068
45 #define EMC_REFRESH 0x070
46 #define EMC_BURST_REFRESH_NUM 0x074
47 #define EMC_PDEX2WR 0x078
48 #define EMC_PDEX2RD 0x07c
49 #define EMC_PCHG2PDEN 0x080
50 #define EMC_ACT2PDEN 0x084
51 #define EMC_AR2PDEN 0x088
52 #define EMC_RW2PDEN 0x08c
53 #define EMC_TXSR 0x090
54 #define EMC_TCKE 0x094
55 #define EMC_TFAW 0x098
56 #define EMC_TRPAB 0x09c
57 #define EMC_TCLKSTABLE 0x0a0
58 #define EMC_TCLKSTOP 0x0a4
59 #define EMC_TREFBW 0x0a8
60 #define EMC_QUSE_EXTRA 0x0ac
61 #define EMC_ODT_WRITE 0x0b0
62 #define EMC_ODT_READ 0x0b4
63 #define EMC_FBIO_CFG5 0x104
64 #define EMC_FBIO_CFG6 0x114
65 #define EMC_AUTO_CAL_INTERVAL 0x2a8
66 #define EMC_CFG_2 0x2b8
67 #define EMC_CFG_DIG_DLL 0x2bc
68 #define EMC_DLL_XFORM_DQS 0x2c0
69 #define EMC_DLL_XFORM_QUSE 0x2c4
70 #define EMC_ZCAL_REF_CNT 0x2e0
71 #define EMC_ZCAL_WAIT_CNT 0x2e4
72 #define EMC_CFG_CLKTRIM_0 0x2d0
73 #define EMC_CFG_CLKTRIM_1 0x2d4
74 #define EMC_CFG_CLKTRIM_2 0x2d8
76 #define EMC_CLKCHANGE_REQ_ENABLE BIT(0)
77 #define EMC_CLKCHANGE_PD_ENABLE BIT(1)
78 #define EMC_CLKCHANGE_SR_ENABLE BIT(2)
80 #define EMC_TIMING_UPDATE BIT(0)
82 #define EMC_REFRESH_OVERFLOW_INT BIT(3)
83 #define EMC_CLKCHANGE_COMPLETE_INT BIT(4)
85 #define EMC_DBG_READ_MUX_ASSEMBLY BIT(0)
86 #define EMC_DBG_WRITE_MUX_ACTIVE BIT(1)
87 #define EMC_DBG_FORCE_UPDATE BIT(2)
88 #define EMC_DBG_READ_DQM_CTRL BIT(9)
89 #define EMC_DBG_CFG_PRIORITY BIT(24)
91 static const u16 emc_timing_registers
[] = {
110 EMC_BURST_REFRESH_NUM
,
134 EMC_AUTO_CAL_INTERVAL
,
142 u32 data
[ARRAY_SIZE(emc_timing_registers
)];
147 struct completion clk_handshake_complete
;
148 struct notifier_block clk_nb
;
152 struct emc_timing
*timings
;
153 unsigned int num_timings
;
157 unsigned long min_rate
;
158 unsigned long max_rate
;
162 static irqreturn_t
tegra_emc_isr(int irq
, void *data
)
164 struct tegra_emc
*emc
= data
;
165 u32 intmask
= EMC_REFRESH_OVERFLOW_INT
| EMC_CLKCHANGE_COMPLETE_INT
;
168 status
= readl_relaxed(emc
->regs
+ EMC_INTSTATUS
) & intmask
;
172 /* notify about EMC-CAR handshake completion */
173 if (status
& EMC_CLKCHANGE_COMPLETE_INT
)
174 complete(&emc
->clk_handshake_complete
);
176 /* notify about HW problem */
177 if (status
& EMC_REFRESH_OVERFLOW_INT
)
178 dev_err_ratelimited(emc
->dev
,
179 "refresh request overflow timeout\n");
181 /* clear interrupts */
182 writel_relaxed(status
, emc
->regs
+ EMC_INTSTATUS
);
187 static struct emc_timing
*tegra_emc_find_timing(struct tegra_emc
*emc
,
190 struct emc_timing
*timing
= NULL
;
193 for (i
= 0; i
< emc
->num_timings
; i
++) {
194 if (emc
->timings
[i
].rate
>= rate
) {
195 timing
= &emc
->timings
[i
];
201 dev_err(emc
->dev
, "no timing for rate %lu\n", rate
);
208 static int emc_prepare_timing_change(struct tegra_emc
*emc
, unsigned long rate
)
210 struct emc_timing
*timing
= tegra_emc_find_timing(emc
, rate
);
216 dev_dbg(emc
->dev
, "%s: using timing rate %lu for requested rate %lu\n",
217 __func__
, timing
->rate
, rate
);
219 /* program shadow registers */
220 for (i
= 0; i
< ARRAY_SIZE(timing
->data
); i
++)
221 writel_relaxed(timing
->data
[i
],
222 emc
->regs
+ emc_timing_registers
[i
]);
224 /* wait until programming has settled */
225 readl_relaxed(emc
->regs
+ emc_timing_registers
[i
- 1]);
227 reinit_completion(&emc
->clk_handshake_complete
);
232 static int emc_complete_timing_change(struct tegra_emc
*emc
, bool flush
)
234 unsigned long timeout
;
236 dev_dbg(emc
->dev
, "%s: flush %d\n", __func__
, flush
);
239 /* manually initiate memory timing update */
240 writel_relaxed(EMC_TIMING_UPDATE
,
241 emc
->regs
+ EMC_TIMING_CONTROL
);
245 timeout
= wait_for_completion_timeout(&emc
->clk_handshake_complete
,
246 msecs_to_jiffies(100));
248 dev_err(emc
->dev
, "EMC-CAR handshake failed\n");
255 static int tegra_emc_clk_change_notify(struct notifier_block
*nb
,
256 unsigned long msg
, void *data
)
258 struct tegra_emc
*emc
= container_of(nb
, struct tegra_emc
, clk_nb
);
259 struct clk_notifier_data
*cnd
= data
;
263 case PRE_RATE_CHANGE
:
264 err
= emc_prepare_timing_change(emc
, cnd
->new_rate
);
267 case ABORT_RATE_CHANGE
:
268 err
= emc_prepare_timing_change(emc
, cnd
->old_rate
);
272 err
= emc_complete_timing_change(emc
, true);
275 case POST_RATE_CHANGE
:
276 err
= emc_complete_timing_change(emc
, false);
283 return notifier_from_errno(err
);
286 static int load_one_timing_from_dt(struct tegra_emc
*emc
,
287 struct emc_timing
*timing
,
288 struct device_node
*node
)
293 if (!of_device_is_compatible(node
, "nvidia,tegra20-emc-table")) {
294 dev_err(emc
->dev
, "incompatible DT node: %pOF\n", node
);
298 err
= of_property_read_u32(node
, "clock-frequency", &rate
);
300 dev_err(emc
->dev
, "timing %pOF: failed to read rate: %d\n",
305 err
= of_property_read_u32_array(node
, "nvidia,emc-registers",
307 ARRAY_SIZE(emc_timing_registers
));
310 "timing %pOF: failed to read emc timing data: %d\n",
316 * The EMC clock rate is twice the bus rate, and the bus rate is
319 timing
->rate
= rate
* 2 * 1000;
321 dev_dbg(emc
->dev
, "%s: %pOF: EMC rate %lu\n",
322 __func__
, node
, timing
->rate
);
327 static int cmp_timings(const void *_a
, const void *_b
)
329 const struct emc_timing
*a
= _a
;
330 const struct emc_timing
*b
= _b
;
332 if (a
->rate
< b
->rate
)
335 if (a
->rate
> b
->rate
)
341 static int tegra_emc_load_timings_from_dt(struct tegra_emc
*emc
,
342 struct device_node
*node
)
344 struct device_node
*child
;
345 struct emc_timing
*timing
;
349 child_count
= of_get_child_count(node
);
351 dev_err(emc
->dev
, "no memory timings in DT node: %pOF\n", node
);
355 emc
->timings
= devm_kcalloc(emc
->dev
, child_count
, sizeof(*timing
),
360 emc
->num_timings
= child_count
;
361 timing
= emc
->timings
;
363 for_each_child_of_node(node
, child
) {
364 err
= load_one_timing_from_dt(emc
, timing
++, child
);
371 sort(emc
->timings
, emc
->num_timings
, sizeof(*timing
), cmp_timings
,
375 "got %u timings for RAM code %u (min %luMHz max %luMHz)\n",
377 tegra_read_ram_code(),
378 emc
->timings
[0].rate
/ 1000000,
379 emc
->timings
[emc
->num_timings
- 1].rate
/ 1000000);
384 static struct device_node
*
385 tegra_emc_find_node_by_ram_code(struct device
*dev
)
387 struct device_node
*np
;
391 if (!of_property_read_bool(dev
->of_node
, "nvidia,use-ram-code"))
392 return of_node_get(dev
->of_node
);
394 ram_code
= tegra_read_ram_code();
396 for (np
= of_find_node_by_name(dev
->of_node
, "emc-tables"); np
;
397 np
= of_find_node_by_name(np
, "emc-tables")) {
398 err
= of_property_read_u32(np
, "nvidia,ram-code", &value
);
399 if (err
|| value
!= ram_code
) {
407 dev_err(dev
, "no memory timings for RAM code %u found in device tree\n",
413 static int emc_setup_hw(struct tegra_emc
*emc
)
415 u32 intmask
= EMC_REFRESH_OVERFLOW_INT
| EMC_CLKCHANGE_COMPLETE_INT
;
416 u32 emc_cfg
, emc_dbg
;
418 emc_cfg
= readl_relaxed(emc
->regs
+ EMC_CFG_2
);
421 * Depending on a memory type, DRAM should enter either self-refresh
422 * or power-down state on EMC clock change.
424 if (!(emc_cfg
& EMC_CLKCHANGE_PD_ENABLE
) &&
425 !(emc_cfg
& EMC_CLKCHANGE_SR_ENABLE
)) {
427 "bootloader didn't specify DRAM auto-suspend mode\n");
431 /* enable EMC and CAR to handshake on PLL divider/source changes */
432 emc_cfg
|= EMC_CLKCHANGE_REQ_ENABLE
;
433 writel_relaxed(emc_cfg
, emc
->regs
+ EMC_CFG_2
);
435 /* initialize interrupt */
436 writel_relaxed(intmask
, emc
->regs
+ EMC_INTMASK
);
437 writel_relaxed(intmask
, emc
->regs
+ EMC_INTSTATUS
);
439 /* ensure that unwanted debug features are disabled */
440 emc_dbg
= readl_relaxed(emc
->regs
+ EMC_DBG
);
441 emc_dbg
|= EMC_DBG_CFG_PRIORITY
;
442 emc_dbg
&= ~EMC_DBG_READ_MUX_ASSEMBLY
;
443 emc_dbg
&= ~EMC_DBG_WRITE_MUX_ACTIVE
;
444 emc_dbg
&= ~EMC_DBG_FORCE_UPDATE
;
445 writel_relaxed(emc_dbg
, emc
->regs
+ EMC_DBG
);
450 static long emc_round_rate(unsigned long rate
,
451 unsigned long min_rate
,
452 unsigned long max_rate
,
455 struct emc_timing
*timing
= NULL
;
456 struct tegra_emc
*emc
= arg
;
459 min_rate
= min(min_rate
, emc
->timings
[emc
->num_timings
- 1].rate
);
461 for (i
= 0; i
< emc
->num_timings
; i
++) {
462 if (emc
->timings
[i
].rate
< rate
&& i
!= emc
->num_timings
- 1)
465 if (emc
->timings
[i
].rate
> max_rate
) {
468 if (emc
->timings
[i
].rate
< min_rate
)
472 if (emc
->timings
[i
].rate
< min_rate
)
475 timing
= &emc
->timings
[i
];
480 dev_err(emc
->dev
, "no timing for rate %lu min %lu max %lu\n",
481 rate
, min_rate
, max_rate
);
491 * The memory controller driver exposes some files in debugfs that can be used
492 * to control the EMC frequency. The top-level directory can be found here:
494 * /sys/kernel/debug/emc
496 * It contains the following files:
498 * - available_rates: This file contains a list of valid, space-separated
501 * - min_rate: Writing a value to this file sets the given frequency as the
502 * floor of the permitted range. If this is higher than the currently
503 * configured EMC frequency, this will cause the frequency to be
504 * increased so that it stays within the valid range.
506 * - max_rate: Similarily to the min_rate file, writing a value to this file
507 * sets the given frequency as the ceiling of the permitted range. If
508 * the value is lower than the currently configured EMC frequency, this
509 * will cause the frequency to be decreased so that it stays within the
513 static bool tegra_emc_validate_rate(struct tegra_emc
*emc
, unsigned long rate
)
517 for (i
= 0; i
< emc
->num_timings
; i
++)
518 if (rate
== emc
->timings
[i
].rate
)
524 static int tegra_emc_debug_available_rates_show(struct seq_file
*s
, void *data
)
526 struct tegra_emc
*emc
= s
->private;
527 const char *prefix
= "";
530 for (i
= 0; i
< emc
->num_timings
; i
++) {
531 seq_printf(s
, "%s%lu", prefix
, emc
->timings
[i
].rate
);
540 static int tegra_emc_debug_available_rates_open(struct inode
*inode
,
543 return single_open(file
, tegra_emc_debug_available_rates_show
,
547 static const struct file_operations tegra_emc_debug_available_rates_fops
= {
548 .open
= tegra_emc_debug_available_rates_open
,
551 .release
= single_release
,
554 static int tegra_emc_debug_min_rate_get(void *data
, u64
*rate
)
556 struct tegra_emc
*emc
= data
;
558 *rate
= emc
->debugfs
.min_rate
;
563 static int tegra_emc_debug_min_rate_set(void *data
, u64 rate
)
565 struct tegra_emc
*emc
= data
;
568 if (!tegra_emc_validate_rate(emc
, rate
))
571 err
= clk_set_min_rate(emc
->clk
, rate
);
575 emc
->debugfs
.min_rate
= rate
;
580 DEFINE_SIMPLE_ATTRIBUTE(tegra_emc_debug_min_rate_fops
,
581 tegra_emc_debug_min_rate_get
,
582 tegra_emc_debug_min_rate_set
, "%llu\n");
584 static int tegra_emc_debug_max_rate_get(void *data
, u64
*rate
)
586 struct tegra_emc
*emc
= data
;
588 *rate
= emc
->debugfs
.max_rate
;
593 static int tegra_emc_debug_max_rate_set(void *data
, u64 rate
)
595 struct tegra_emc
*emc
= data
;
598 if (!tegra_emc_validate_rate(emc
, rate
))
601 err
= clk_set_max_rate(emc
->clk
, rate
);
605 emc
->debugfs
.max_rate
= rate
;
610 DEFINE_SIMPLE_ATTRIBUTE(tegra_emc_debug_max_rate_fops
,
611 tegra_emc_debug_max_rate_get
,
612 tegra_emc_debug_max_rate_set
, "%llu\n");
614 static void tegra_emc_debugfs_init(struct tegra_emc
*emc
)
616 struct device
*dev
= emc
->dev
;
620 emc
->debugfs
.min_rate
= ULONG_MAX
;
621 emc
->debugfs
.max_rate
= 0;
623 for (i
= 0; i
< emc
->num_timings
; i
++) {
624 if (emc
->timings
[i
].rate
< emc
->debugfs
.min_rate
)
625 emc
->debugfs
.min_rate
= emc
->timings
[i
].rate
;
627 if (emc
->timings
[i
].rate
> emc
->debugfs
.max_rate
)
628 emc
->debugfs
.max_rate
= emc
->timings
[i
].rate
;
631 if (!emc
->num_timings
) {
632 emc
->debugfs
.min_rate
= clk_get_rate(emc
->clk
);
633 emc
->debugfs
.max_rate
= emc
->debugfs
.min_rate
;
636 err
= clk_set_rate_range(emc
->clk
, emc
->debugfs
.min_rate
,
637 emc
->debugfs
.max_rate
);
639 dev_err(dev
, "failed to set rate range [%lu-%lu] for %pC\n",
640 emc
->debugfs
.min_rate
, emc
->debugfs
.max_rate
,
644 emc
->debugfs
.root
= debugfs_create_dir("emc", NULL
);
645 if (!emc
->debugfs
.root
) {
646 dev_err(emc
->dev
, "failed to create debugfs directory\n");
650 debugfs_create_file("available_rates", S_IRUGO
, emc
->debugfs
.root
,
651 emc
, &tegra_emc_debug_available_rates_fops
);
652 debugfs_create_file("min_rate", S_IRUGO
| S_IWUSR
, emc
->debugfs
.root
,
653 emc
, &tegra_emc_debug_min_rate_fops
);
654 debugfs_create_file("max_rate", S_IRUGO
| S_IWUSR
, emc
->debugfs
.root
,
655 emc
, &tegra_emc_debug_max_rate_fops
);
658 static int tegra_emc_probe(struct platform_device
*pdev
)
660 struct device_node
*np
;
661 struct tegra_emc
*emc
;
662 struct resource
*res
;
665 /* driver has nothing to do in a case of memory timing absence */
666 if (of_get_child_count(pdev
->dev
.of_node
) == 0) {
668 "EMC device tree node doesn't have memory timings\n");
672 irq
= platform_get_irq(pdev
, 0);
674 dev_err(&pdev
->dev
, "interrupt not specified\n");
675 dev_err(&pdev
->dev
, "please update your device tree\n");
679 np
= tegra_emc_find_node_by_ram_code(&pdev
->dev
);
683 emc
= devm_kzalloc(&pdev
->dev
, sizeof(*emc
), GFP_KERNEL
);
689 init_completion(&emc
->clk_handshake_complete
);
690 emc
->clk_nb
.notifier_call
= tegra_emc_clk_change_notify
;
691 emc
->dev
= &pdev
->dev
;
693 err
= tegra_emc_load_timings_from_dt(emc
, np
);
698 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
699 emc
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
700 if (IS_ERR(emc
->regs
))
701 return PTR_ERR(emc
->regs
);
703 err
= emc_setup_hw(emc
);
707 err
= devm_request_irq(&pdev
->dev
, irq
, tegra_emc_isr
, 0,
708 dev_name(&pdev
->dev
), emc
);
710 dev_err(&pdev
->dev
, "failed to request IRQ#%u: %d\n", irq
, err
);
714 tegra20_clk_set_emc_round_callback(emc_round_rate
, emc
);
716 emc
->clk
= devm_clk_get(&pdev
->dev
, "emc");
717 if (IS_ERR(emc
->clk
)) {
718 err
= PTR_ERR(emc
->clk
);
719 dev_err(&pdev
->dev
, "failed to get emc clock: %d\n", err
);
723 err
= clk_notifier_register(emc
->clk
, &emc
->clk_nb
);
725 dev_err(&pdev
->dev
, "failed to register clk notifier: %d\n",
730 platform_set_drvdata(pdev
, emc
);
731 tegra_emc_debugfs_init(emc
);
736 tegra20_clk_set_emc_round_callback(NULL
, NULL
);
741 static const struct of_device_id tegra_emc_of_match
[] = {
742 { .compatible
= "nvidia,tegra20-emc", },
746 static struct platform_driver tegra_emc_driver
= {
747 .probe
= tegra_emc_probe
,
749 .name
= "tegra20-emc",
750 .of_match_table
= tegra_emc_of_match
,
751 .suppress_bind_attrs
= true,
755 static int __init
tegra_emc_init(void)
757 return platform_driver_register(&tegra_emc_driver
);
759 subsys_initcall(tegra_emc_init
);