xref: /rk3399_ARM-atf/drivers/st/ddr/phy/phyinit/src/ddrphy_phyinit_isdbytedisabled.c (revision eaaf26e3e6ac347cbfda00b6ba7d327e715d68f0)
1*79629b1aSNicolas Le Bayon /*
2*79629b1aSNicolas Le Bayon  * Copyright (C) 2021-2024, STMicroelectronics - All Rights Reserved
3*79629b1aSNicolas Le Bayon  *
4*79629b1aSNicolas Le Bayon  * SPDX-License-Identifier: BSD-3-Clause
5*79629b1aSNicolas Le Bayon  */
6*79629b1aSNicolas Le Bayon 
7*79629b1aSNicolas Le Bayon #include <stdlib.h>
8*79629b1aSNicolas Le Bayon 
9*79629b1aSNicolas Le Bayon #include <common/debug.h>
10*79629b1aSNicolas Le Bayon 
11*79629b1aSNicolas Le Bayon #include <ddrphy_phyinit.h>
12*79629b1aSNicolas Le Bayon #include <ddrphy_wrapper.h>
13*79629b1aSNicolas Le Bayon 
14*79629b1aSNicolas Le Bayon /*
15*79629b1aSNicolas Le Bayon  * Helper function to determine if a given DByte is Disabled given PhyInit inputs.
16*79629b1aSNicolas Le Bayon  * @return 1 if disabled, 0 if enabled.
17*79629b1aSNicolas Le Bayon  */
ddrphy_phyinit_isdbytedisabled(struct stm32mp_ddr_config * config,struct pmu_smb_ddr_1d * mb_ddr_1d,uint32_t dbytenumber)18*79629b1aSNicolas Le Bayon int ddrphy_phyinit_isdbytedisabled(struct stm32mp_ddr_config *config,
19*79629b1aSNicolas Le Bayon 				   struct pmu_smb_ddr_1d *mb_ddr_1d, uint32_t dbytenumber)
20*79629b1aSNicolas Le Bayon {
21*79629b1aSNicolas Le Bayon 	int disabledbyte;
22*79629b1aSNicolas Le Bayon 	uint32_t nad0 __maybe_unused;
23*79629b1aSNicolas Le Bayon 	uint32_t nad1 __maybe_unused;
24*79629b1aSNicolas Le Bayon 
25*79629b1aSNicolas Le Bayon 	disabledbyte = 0; /* Default assume Dbyte is Enabled */
26*79629b1aSNicolas Le Bayon 
27*79629b1aSNicolas Le Bayon #if STM32MP_DDR3_TYPE || STM32MP_DDR4_TYPE
28*79629b1aSNicolas Le Bayon 	disabledbyte = (dbytenumber > (config->uib.numactivedbytedfi0 - 1U)) ? 1 : 0;
29*79629b1aSNicolas Le Bayon #else /* STM32MP_LPDDR4_TYPE */
30*79629b1aSNicolas Le Bayon 	nad0 = config->uib.numactivedbytedfi0;
31*79629b1aSNicolas Le Bayon 	nad1 = config->uib.numactivedbytedfi1;
32*79629b1aSNicolas Le Bayon 
33*79629b1aSNicolas Le Bayon 	if ((nad0 + nad1) > config->uib.numdbyte) {
34*79629b1aSNicolas Le Bayon 		ERROR("%s %d\n", __func__, __LINE__);
35*79629b1aSNicolas Le Bayon 		VERBOSE("%s invalid PHY configuration:\n", __func__);
36*79629b1aSNicolas Le Bayon 		VERBOSE("numactivedbytedfi0(%u)+numactivedbytedfi1(%u)>numdbytes(%u).\n",
37*79629b1aSNicolas Le Bayon 			nad0, nad1, config->uib.numdbyte);
38*79629b1aSNicolas Le Bayon 	}
39*79629b1aSNicolas Le Bayon 
40*79629b1aSNicolas Le Bayon 	if (config->uib.dfi1exists != 0U) {
41*79629b1aSNicolas Le Bayon 		if (config->uib.numactivedbytedfi1 == 0U) {
42*79629b1aSNicolas Le Bayon 			/* Only dfi0 (ChA) is enabled, dfi1 (ChB) disabled */
43*79629b1aSNicolas Le Bayon 			disabledbyte = (dbytenumber > (config->uib.numactivedbytedfi0 - 1U)) ?
44*79629b1aSNicolas Le Bayon 				       1 : 0;
45*79629b1aSNicolas Le Bayon 		} else {
46*79629b1aSNicolas Le Bayon 			/* DFI1 enabled */
47*79629b1aSNicolas Le Bayon 			disabledbyte = (((config->uib.numactivedbytedfi0 - 1U) < dbytenumber) &&
48*79629b1aSNicolas Le Bayon 					(dbytenumber < (config->uib.numdbyte / 2U))) ?
49*79629b1aSNicolas Le Bayon 				       1 : (dbytenumber >
50*79629b1aSNicolas Le Bayon 					    ((config->uib.numdbyte / 2U) +
51*79629b1aSNicolas Le Bayon 					     config->uib.numactivedbytedfi1 - 1U)) ? 1 : 0;
52*79629b1aSNicolas Le Bayon 		}
53*79629b1aSNicolas Le Bayon 	} else {
54*79629b1aSNicolas Le Bayon 		disabledbyte = (dbytenumber > (config->uib.numactivedbytedfi0 - 1U)) ? 1 : 0;
55*79629b1aSNicolas Le Bayon 	}
56*79629b1aSNicolas Le Bayon #endif /* STM32MP_DDR3_TYPE || STM32MP_DDR4_TYPE */
57*79629b1aSNicolas Le Bayon 
58*79629b1aSNicolas Le Bayon 	/* Qualify results against MessageBlock */
59*79629b1aSNicolas Le Bayon #if STM32MP_DDR3_TYPE || STM32MP_DDR4_TYPE
60*79629b1aSNicolas Le Bayon 	if ((mb_ddr_1d->enableddqs < 1U) ||
61*79629b1aSNicolas Le Bayon 	    (mb_ddr_1d->enableddqs > (uint8_t)(8U * config->uib.numactivedbytedfi0))) {
62*79629b1aSNicolas Le Bayon 		ERROR("%s %d\n", __func__, __LINE__);
63*79629b1aSNicolas Le Bayon 		VERBOSE("%s enableddqs(%u)\n", __func__, mb_ddr_1d->enableddqs);
64*79629b1aSNicolas Le Bayon 		VERBOSE("Value must be 0 < enableddqs < config->uib.numactivedbytedfi0 * 8.\n");
65*79629b1aSNicolas Le Bayon 	}
66*79629b1aSNicolas Le Bayon 
67*79629b1aSNicolas Le Bayon 	if (dbytenumber < 8) {
68*79629b1aSNicolas Le Bayon 		disabledbyte |= (int)mb_ddr_1d->disableddbyte & (0x1 << dbytenumber);
69*79629b1aSNicolas Le Bayon 	}
70*79629b1aSNicolas Le Bayon #else /* STM32MP_LPDDR4_TYPE */
71*79629b1aSNicolas Le Bayon 	if ((mb_ddr_1d->enableddqscha < 1U) ||
72*79629b1aSNicolas Le Bayon 	    (mb_ddr_1d->enableddqscha > (uint8_t)(8U * config->uib.numactivedbytedfi0))) {
73*79629b1aSNicolas Le Bayon 		ERROR("%s %d\n", __func__, __LINE__);
74*79629b1aSNicolas Le Bayon 		VERBOSE("%s enableddqscha(%u)\n", __func__, mb_ddr_1d->enableddqscha);
75*79629b1aSNicolas Le Bayon 		VERBOSE("Value must be 0 < enableddqscha < config->uib.numactivedbytedfi0*8\n");
76*79629b1aSNicolas Le Bayon 	}
77*79629b1aSNicolas Le Bayon 
78*79629b1aSNicolas Le Bayon 	if ((config->uib.dfi1exists != 0U) && (config->uib.numactivedbytedfi1 > 0U) &&
79*79629b1aSNicolas Le Bayon 	    ((mb_ddr_1d->enableddqschb < 1U) ||
80*79629b1aSNicolas Le Bayon 	     (mb_ddr_1d->enableddqschb > (uint8_t)(8U * config->uib.numactivedbytedfi1)))) {
81*79629b1aSNicolas Le Bayon 		ERROR("%s %d\n", __func__, __LINE__);
82*79629b1aSNicolas Le Bayon 		VERBOSE("%s enableddqschb(%u)\n", __func__, mb_ddr_1d->enableddqschb);
83*79629b1aSNicolas Le Bayon 		VERBOSE("Value must be 0 < enableddqschb < config->uib.numactivedbytedfi1*8\n");
84*79629b1aSNicolas Le Bayon 	}
85*79629b1aSNicolas Le Bayon #endif /* STM32MP_DDR3_TYPE || STM32MP_DDR4_TYPE */
86*79629b1aSNicolas Le Bayon 
87*79629b1aSNicolas Le Bayon 	return disabledbyte;
88*79629b1aSNicolas Le Bayon }
89