blob: 576538feb70613f5e5ef6be8345e2610ec1a73ef [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roesec0132f62016-08-30 16:48:20 +02002/*
3 * Copyright (C) 2015-2016 Marvell International Ltd.
Stefan Roesec0132f62016-08-30 16:48:20 +02004 */
5
6#include <common.h>
7#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Stefan Roesec0132f62016-08-30 16:48:20 +020010#include <asm/io.h>
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +030011#include <asm/ptrace.h>
Stefan Roesec0132f62016-08-30 16:48:20 +020012#include <asm/arch/cpu.h>
13#include <asm/arch/soc.h>
Simon Glassc05ed002020-05-10 11:40:11 -060014#include <linux/delay.h>
Stefan Roesec0132f62016-08-30 16:48:20 +020015
Marek BehĂșn4b8cb842018-08-17 12:58:51 +020016#include "comphy_core.h"
Stefan Roesec0132f62016-08-30 16:48:20 +020017#include "sata.h"
18#include "utmi_phy.h"
19
20DECLARE_GLOBAL_DATA_PTR;
21
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +030022/* Firmware related definitions used for SMC calls */
23#define MV_SIP_COMPHY_POWER_ON 0x82000001
24#define MV_SIP_COMPHY_POWER_OFF 0x82000002
25#define MV_SIP_COMPHY_PLL_LOCK 0x82000003
Grzegorz Jaszczyk2e28b592018-04-03 16:59:12 +020026#define MV_SIP_COMPHY_XFI_TRAIN 0x82000004
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +030027
Igal Liberman3261f6d2020-10-18 17:11:13 +030028/* Used to distinguish between different possible callers (U-boot/Linux) */
29#define COMPHY_CALLER_UBOOT (0x1 << 21)
30
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +030031#define COMPHY_FW_MODE_FORMAT(mode) ((mode) << 12)
32#define COMPHY_FW_FORMAT(mode, idx, speeds) \
33 (((mode) << 12) | ((idx) << 8) | ((speeds) << 2))
Grzegorz Jaszczyk0a1a1642020-10-18 17:11:12 +030034
35#define COMPHY_FW_PCIE_FORMAT(pcie_width, clk_src, mode, speeds) \
Igal Liberman3261f6d2020-10-18 17:11:13 +030036 (COMPHY_CALLER_UBOOT | ((pcie_width) << 18) | \
37 ((clk_src) << 17) | COMPHY_FW_FORMAT(mode, 0, speeds))
Grzegorz Jaszczyk0a1a1642020-10-18 17:11:12 +030038
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +030039#define COMPHY_SATA_MODE 0x1
40#define COMPHY_SGMII_MODE 0x2 /* SGMII 1G */
41#define COMPHY_HS_SGMII_MODE 0x3 /* SGMII 2.5G */
42#define COMPHY_USB3H_MODE 0x4
43#define COMPHY_USB3D_MODE 0x5
44#define COMPHY_PCIE_MODE 0x6
45#define COMPHY_RXAUI_MODE 0x7
46#define COMPHY_XFI_MODE 0x8
47#define COMPHY_SFI_MODE 0x9
48#define COMPHY_USB3_MODE 0xa
49#define COMPHY_AP_MODE 0xb
50
51/* Comphy unit index macro */
52#define COMPHY_UNIT_ID0 0
53#define COMPHY_UNIT_ID1 1
54#define COMPHY_UNIT_ID2 2
55#define COMPHY_UNIT_ID3 3
56
Stefan Roesec0132f62016-08-30 16:48:20 +020057struct utmi_phy_data {
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +010058 void __iomem *utmi_pll_addr;
Stefan Roesec0132f62016-08-30 16:48:20 +020059 void __iomem *utmi_base_addr;
60 void __iomem *usb_cfg_addr;
61 void __iomem *utmi_cfg_addr;
62 u32 utmi_phy_port;
63};
64
Stefan Roesec0132f62016-08-30 16:48:20 +020065static u32 polling_with_timeout(void __iomem *addr, u32 val,
66 u32 mask, unsigned long usec_timout)
67{
68 u32 data;
69
70 do {
71 udelay(1);
72 data = readl(addr) & mask;
73 } while (data != val && --usec_timout > 0);
74
75 if (usec_timout == 0)
76 return data;
77
78 return 0;
79}
80
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +030081static int comphy_smc(u32 function_id, void __iomem *comphy_base_addr,
82 u32 lane, u32 mode)
83{
84 struct pt_regs pregs = {0};
85
86 pregs.regs[0] = function_id;
87 pregs.regs[1] = (unsigned long)comphy_base_addr;
88 pregs.regs[2] = lane;
89 pregs.regs[3] = mode;
90
91 smc_call(&pregs);
92
93 /*
94 * TODO: Firmware return 0 on success, temporary map it to u-boot
95 * convention, but after all comphy will be reworked the convention in
96 * u-boot should be change and this conversion removed
97 */
98 return pregs.regs[0] ? 0 : 1;
99}
100
Grzegorz Jaszczyk2e28b592018-04-03 16:59:12 +0200101/* This function performs RX training for all FFE possible values.
102 * We get the result for each FFE and eventually the best FFE will
103 * be used and set to the HW.
104 *
105 * Return '1' on succsess.
106 * Return '0' on failure.
107 */
108int comphy_cp110_sfi_rx_training(struct chip_serdes_phy_config *ptr_chip_cfg,
109 u32 lane)
110{
111 int ret;
Igal Liberman341e5482018-05-14 11:20:54 +0300112 u32 type = ptr_chip_cfg->comphy_map_data[lane].type;
Grzegorz Jaszczyk2e28b592018-04-03 16:59:12 +0200113
114 debug_enter();
115
Igal Liberman341e5482018-05-14 11:20:54 +0300116 if (type != COMPHY_TYPE_SFI0 && type != COMPHY_TYPE_SFI1) {
Grzegorz Jaszczyk2e28b592018-04-03 16:59:12 +0200117 pr_err("Comphy %d isn't configured to SFI\n", lane);
118 return 0;
119 }
120
121 /* Mode is not relevant for xfi training */
122 ret = comphy_smc(MV_SIP_COMPHY_XFI_TRAIN,
123 ptr_chip_cfg->comphy_base_addr, lane, 0);
124
125 debug_exit();
126
127 return ret;
128}
129
Stefan Roesec0132f62016-08-30 16:48:20 +0200130static int comphy_sata_power_up(u32 lane, void __iomem *hpipe_base,
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +0300131 void __iomem *comphy_base_addr, int cp_index,
132 u32 type)
Stefan Roesec0132f62016-08-30 16:48:20 +0200133{
134 u32 mask, data, i, ret = 1;
Stefan Roesec0132f62016-08-30 16:48:20 +0200135 void __iomem *sata_base = NULL;
136 int sata_node = -1; /* Set to -1 in order to read the first sata node */
137
138 debug_enter();
139
140 /*
141 * Assumption - each CP has only one SATA controller
142 * Calling fdt_node_offset_by_compatible first time (with sata_node = -1
143 * will return the first node always.
144 * In order to parse each CPs SATA node, fdt_node_offset_by_compatible
145 * must be called again (according to the CP id)
146 */
Igal Liberman528213d2017-04-24 18:45:32 +0300147 for (i = 0; i < (cp_index + 1); i++)
Stefan Roesec0132f62016-08-30 16:48:20 +0200148 sata_node = fdt_node_offset_by_compatible(
149 gd->fdt_blob, sata_node, "marvell,armada-8k-ahci");
150
151 if (sata_node == 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900152 pr_err("SATA node not found in FDT\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200153 return 0;
154 }
155
156 sata_base = (void __iomem *)fdtdec_get_addr_size_auto_noparent(
157 gd->fdt_blob, sata_node, "reg", 0, NULL, true);
158 if (sata_base == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900159 pr_err("SATA address not found in FDT\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200160 return 0;
161 }
162
163 debug("SATA address found in FDT %p\n", sata_base);
164
165 debug("stage: MAC configuration - power down comphy\n");
166 /*
167 * MAC configuration powe down comphy use indirect address for
168 * vendor spesific SATA control register
169 */
170 reg_set(sata_base + SATA3_VENDOR_ADDRESS,
171 SATA_CONTROL_REG << SATA3_VENDOR_ADDR_OFSSET,
172 SATA3_VENDOR_ADDR_MASK);
173 /* SATA 0 power down */
174 mask = SATA3_CTRL_SATA0_PD_MASK;
175 data = 0x1 << SATA3_CTRL_SATA0_PD_OFFSET;
176 /* SATA 1 power down */
177 mask |= SATA3_CTRL_SATA1_PD_MASK;
178 data |= 0x1 << SATA3_CTRL_SATA1_PD_OFFSET;
179 /* SATA SSU disable */
180 mask |= SATA3_CTRL_SATA1_ENABLE_MASK;
181 data |= 0x0 << SATA3_CTRL_SATA1_ENABLE_OFFSET;
182 /* SATA port 1 disable */
183 mask |= SATA3_CTRL_SATA_SSU_MASK;
184 data |= 0x0 << SATA3_CTRL_SATA_SSU_OFFSET;
185 reg_set(sata_base + SATA3_VENDOR_DATA, data, mask);
186
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +0300187 ret = comphy_smc(MV_SIP_COMPHY_POWER_ON, comphy_base_addr, lane, type);
Stefan Roesec0132f62016-08-30 16:48:20 +0200188
Stefan Roesec0132f62016-08-30 16:48:20 +0200189 /*
190 * MAC configuration power up comphy - power up PLL/TX/RX
191 * use indirect address for vendor spesific SATA control register
192 */
193 reg_set(sata_base + SATA3_VENDOR_ADDRESS,
194 SATA_CONTROL_REG << SATA3_VENDOR_ADDR_OFSSET,
195 SATA3_VENDOR_ADDR_MASK);
196 /* SATA 0 power up */
197 mask = SATA3_CTRL_SATA0_PD_MASK;
198 data = 0x0 << SATA3_CTRL_SATA0_PD_OFFSET;
199 /* SATA 1 power up */
200 mask |= SATA3_CTRL_SATA1_PD_MASK;
201 data |= 0x0 << SATA3_CTRL_SATA1_PD_OFFSET;
202 /* SATA SSU enable */
203 mask |= SATA3_CTRL_SATA1_ENABLE_MASK;
204 data |= 0x1 << SATA3_CTRL_SATA1_ENABLE_OFFSET;
205 /* SATA port 1 enable */
206 mask |= SATA3_CTRL_SATA_SSU_MASK;
207 data |= 0x1 << SATA3_CTRL_SATA_SSU_OFFSET;
208 reg_set(sata_base + SATA3_VENDOR_DATA, data, mask);
209
210 /* MBUS request size and interface select register */
211 reg_set(sata_base + SATA3_VENDOR_ADDRESS,
212 SATA_MBUS_SIZE_SELECT_REG << SATA3_VENDOR_ADDR_OFSSET,
213 SATA3_VENDOR_ADDR_MASK);
214 /* Mbus regret enable */
215 reg_set(sata_base + SATA3_VENDOR_DATA,
216 0x1 << SATA_MBUS_REGRET_EN_OFFSET, SATA_MBUS_REGRET_EN_MASK);
217
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +0300218 ret = comphy_smc(MV_SIP_COMPHY_PLL_LOCK, comphy_base_addr, lane, type);
Stefan Roesec0132f62016-08-30 16:48:20 +0200219
220 debug_exit();
221 return ret;
222}
223
Stefan Roesec0132f62016-08-30 16:48:20 +0200224static void comphy_utmi_power_down(u32 utmi_index, void __iomem *utmi_base_addr,
225 void __iomem *usb_cfg_addr,
226 void __iomem *utmi_cfg_addr,
227 u32 utmi_phy_port)
228{
229 u32 mask, data;
230
231 debug_enter();
232 debug("stage: UTMI %d - Power down transceiver (power down Phy), Power down PLL, and SuspendDM\n",
233 utmi_index);
234 /* Power down UTMI PHY */
235 reg_set(utmi_cfg_addr, 0x0 << UTMI_PHY_CFG_PU_OFFSET,
236 UTMI_PHY_CFG_PU_MASK);
237
238 /*
239 * If UTMI connected to USB Device, configure mux prior to PHY init
240 * (Device can be connected to UTMI0 or to UTMI1)
241 */
Stefan Roesee89acc42017-04-24 18:45:23 +0300242 if (utmi_phy_port == UTMI_PHY_TO_USB3_DEVICE0) {
Stefan Roesec0132f62016-08-30 16:48:20 +0200243 debug("stage: UTMI %d - Enable Device mode and configure UTMI mux\n",
244 utmi_index);
245 /* USB3 Device UTMI enable */
246 mask = UTMI_USB_CFG_DEVICE_EN_MASK;
247 data = 0x1 << UTMI_USB_CFG_DEVICE_EN_OFFSET;
248 /* USB3 Device UTMI MUX */
249 mask |= UTMI_USB_CFG_DEVICE_MUX_MASK;
250 data |= utmi_index << UTMI_USB_CFG_DEVICE_MUX_OFFSET;
251 reg_set(usb_cfg_addr, data, mask);
252 }
253
254 /* Set Test suspendm mode */
255 mask = UTMI_CTRL_STATUS0_SUSPENDM_MASK;
256 data = 0x1 << UTMI_CTRL_STATUS0_SUSPENDM_OFFSET;
257 /* Enable Test UTMI select */
258 mask |= UTMI_CTRL_STATUS0_TEST_SEL_MASK;
259 data |= 0x1 << UTMI_CTRL_STATUS0_TEST_SEL_OFFSET;
260 reg_set(utmi_base_addr + UTMI_CTRL_STATUS0_REG, data, mask);
261
262 /* Wait for UTMI power down */
263 mdelay(1);
264
265 debug_exit();
266 return;
267}
268
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100269static void comphy_utmi_phy_config(u32 utmi_index, void __iomem *utmi_pll_addr,
270 void __iomem *utmi_base_addr,
Stefan Roesec0132f62016-08-30 16:48:20 +0200271 void __iomem *usb_cfg_addr,
272 void __iomem *utmi_cfg_addr,
273 u32 utmi_phy_port)
274{
275 u32 mask, data;
276
277 debug_exit();
278 debug("stage: Configure UTMI PHY %d registers\n", utmi_index);
279 /* Reference Clock Divider Select */
280 mask = UTMI_PLL_CTRL_REFDIV_MASK;
281 data = 0x5 << UTMI_PLL_CTRL_REFDIV_OFFSET;
282 /* Feedback Clock Divider Select - 90 for 25Mhz*/
283 mask |= UTMI_PLL_CTRL_FBDIV_MASK;
284 data |= 0x60 << UTMI_PLL_CTRL_FBDIV_OFFSET;
285 /* Select LPFR - 0x0 for 25Mhz/5=5Mhz*/
286 mask |= UTMI_PLL_CTRL_SEL_LPFR_MASK;
287 data |= 0x0 << UTMI_PLL_CTRL_SEL_LPFR_OFFSET;
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100288 reg_set(utmi_pll_addr + UTMI_PLL_CTRL_REG, data, mask);
Stefan Roesec0132f62016-08-30 16:48:20 +0200289
290 /* Impedance Calibration Threshold Setting */
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100291 reg_set(utmi_pll_addr + UTMI_CALIB_CTRL_REG,
Igal Liberman3e69b4a2017-04-30 20:16:55 +0300292 0x7 << UTMI_CALIB_CTRL_IMPCAL_VTH_OFFSET,
Stefan Roesec0132f62016-08-30 16:48:20 +0200293 UTMI_CALIB_CTRL_IMPCAL_VTH_MASK);
294
295 /* Set LS TX driver strength coarse control */
Igal Liberman3e69b4a2017-04-30 20:16:55 +0300296 mask = UTMI_TX_CH_CTRL_AMP_MASK;
297 data = 0x4 << UTMI_TX_CH_CTRL_AMP_OFFSET;
Stefan Roesec0132f62016-08-30 16:48:20 +0200298 reg_set(utmi_base_addr + UTMI_TX_CH_CTRL_REG, data, mask);
299
300 /* Enable SQ */
301 mask = UTMI_RX_CH_CTRL0_SQ_DET_MASK;
302 data = 0x0 << UTMI_RX_CH_CTRL0_SQ_DET_OFFSET;
303 /* Enable analog squelch detect */
304 mask |= UTMI_RX_CH_CTRL0_SQ_ANA_DTC_MASK;
305 data |= 0x1 << UTMI_RX_CH_CTRL0_SQ_ANA_DTC_OFFSET;
306 reg_set(utmi_base_addr + UTMI_RX_CH_CTRL0_REG, data, mask);
307
308 /* Set External squelch calibration number */
309 mask = UTMI_RX_CH_CTRL1_SQ_AMP_CAL_MASK;
310 data = 0x1 << UTMI_RX_CH_CTRL1_SQ_AMP_CAL_OFFSET;
311 /* Enable the External squelch calibration */
312 mask |= UTMI_RX_CH_CTRL1_SQ_AMP_CAL_EN_MASK;
313 data |= 0x1 << UTMI_RX_CH_CTRL1_SQ_AMP_CAL_EN_OFFSET;
314 reg_set(utmi_base_addr + UTMI_RX_CH_CTRL1_REG, data, mask);
315
316 /* Set Control VDAT Reference Voltage - 0.325V */
317 mask = UTMI_CHGDTC_CTRL_VDAT_MASK;
318 data = 0x1 << UTMI_CHGDTC_CTRL_VDAT_OFFSET;
319 /* Set Control VSRC Reference Voltage - 0.6V */
320 mask |= UTMI_CHGDTC_CTRL_VSRC_MASK;
321 data |= 0x1 << UTMI_CHGDTC_CTRL_VSRC_OFFSET;
322 reg_set(utmi_base_addr + UTMI_CHGDTC_CTRL_REG, data, mask);
323
324 debug_exit();
325 return;
326}
327
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100328static int comphy_utmi_power_up(u32 utmi_index, void __iomem *utmi_pll_addr,
329 void __iomem *utmi_base_addr,
Stefan Roesec0132f62016-08-30 16:48:20 +0200330 void __iomem *usb_cfg_addr,
331 void __iomem *utmi_cfg_addr, u32 utmi_phy_port)
332{
333 u32 data, mask, ret = 1;
334 void __iomem *addr;
335
336 debug_enter();
337 debug("stage: UTMI %d - Power up transceiver(Power up Phy), and exit SuspendDM\n",
338 utmi_index);
339 /* Power UP UTMI PHY */
340 reg_set(utmi_cfg_addr, 0x1 << UTMI_PHY_CFG_PU_OFFSET,
341 UTMI_PHY_CFG_PU_MASK);
342 /* Disable Test UTMI select */
343 reg_set(utmi_base_addr + UTMI_CTRL_STATUS0_REG,
344 0x0 << UTMI_CTRL_STATUS0_TEST_SEL_OFFSET,
345 UTMI_CTRL_STATUS0_TEST_SEL_MASK);
346
347 debug("stage: Polling for PLL and impedance calibration done, and PLL ready done\n");
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100348 addr = utmi_pll_addr + UTMI_CALIB_CTRL_REG;
Stefan Roesec0132f62016-08-30 16:48:20 +0200349 data = UTMI_CALIB_CTRL_IMPCAL_DONE_MASK;
350 mask = data;
351 data = polling_with_timeout(addr, data, mask, 100);
352 if (data != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900353 pr_err("Impedance calibration is not done\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200354 debug("Read from reg = %p - value = 0x%x\n", addr, data);
355 ret = 0;
356 }
357
358 data = UTMI_CALIB_CTRL_PLLCAL_DONE_MASK;
359 mask = data;
360 data = polling_with_timeout(addr, data, mask, 100);
361 if (data != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900362 pr_err("PLL calibration is not done\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200363 debug("Read from reg = %p - value = 0x%x\n", addr, data);
364 ret = 0;
365 }
366
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100367 addr = utmi_pll_addr + UTMI_PLL_CTRL_REG;
Stefan Roesec0132f62016-08-30 16:48:20 +0200368 data = UTMI_PLL_CTRL_PLL_RDY_MASK;
369 mask = data;
370 data = polling_with_timeout(addr, data, mask, 100);
371 if (data != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900372 pr_err("PLL is not ready\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200373 debug("Read from reg = %p - value = 0x%x\n", addr, data);
374 ret = 0;
375 }
376
377 if (ret)
378 debug("Passed\n");
379 else
380 debug("\n");
381
382 debug_exit();
383 return ret;
384}
385
386/*
387 * comphy_utmi_phy_init initialize the UTMI PHY
388 * the init split in 3 parts:
389 * 1. Power down transceiver and PLL
390 * 2. UTMI PHY configure
Omri Itach22bc8682017-04-06 12:54:16 +0300391 * 3. Power up transceiver and PLL
Stefan Roesec0132f62016-08-30 16:48:20 +0200392 * Note: - Power down/up should be once for both UTMI PHYs
393 * - comphy_dedicated_phys_init call this function if at least there is
394 * one UTMI PHY exists in FDT blob. access to cp110_utmi_data[0] is
395 * legal
396 */
397static void comphy_utmi_phy_init(u32 utmi_phy_count,
398 struct utmi_phy_data *cp110_utmi_data)
399{
400 u32 i;
401
402 debug_enter();
403 /* UTMI Power down */
404 for (i = 0; i < utmi_phy_count; i++) {
405 comphy_utmi_power_down(i, cp110_utmi_data[i].utmi_base_addr,
406 cp110_utmi_data[i].usb_cfg_addr,
407 cp110_utmi_data[i].utmi_cfg_addr,
408 cp110_utmi_data[i].utmi_phy_port);
409 }
410 /* PLL Power down */
411 debug("stage: UTMI PHY power down PLL\n");
412 for (i = 0; i < utmi_phy_count; i++) {
413 reg_set(cp110_utmi_data[i].usb_cfg_addr,
414 0x0 << UTMI_USB_CFG_PLL_OFFSET, UTMI_USB_CFG_PLL_MASK);
415 }
416 /* UTMI configure */
417 for (i = 0; i < utmi_phy_count; i++) {
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100418 comphy_utmi_phy_config(i, cp110_utmi_data[i].utmi_pll_addr,
419 cp110_utmi_data[i].utmi_base_addr,
Stefan Roesec0132f62016-08-30 16:48:20 +0200420 cp110_utmi_data[i].usb_cfg_addr,
421 cp110_utmi_data[i].utmi_cfg_addr,
422 cp110_utmi_data[i].utmi_phy_port);
423 }
424 /* UTMI Power up */
425 for (i = 0; i < utmi_phy_count; i++) {
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100426 if (!comphy_utmi_power_up(i, cp110_utmi_data[i].utmi_pll_addr,
427 cp110_utmi_data[i].utmi_base_addr,
Stefan Roesec0132f62016-08-30 16:48:20 +0200428 cp110_utmi_data[i].usb_cfg_addr,
429 cp110_utmi_data[i].utmi_cfg_addr,
430 cp110_utmi_data[i].utmi_phy_port)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900431 pr_err("Failed to initialize UTMI PHY %d\n", i);
Stefan Roesec0132f62016-08-30 16:48:20 +0200432 continue;
433 }
434 printf("UTMI PHY %d initialized to ", i);
Stefan Roesee89acc42017-04-24 18:45:23 +0300435 if (cp110_utmi_data[i].utmi_phy_port ==
436 UTMI_PHY_TO_USB3_DEVICE0)
Stefan Roesec0132f62016-08-30 16:48:20 +0200437 printf("USB Device\n");
438 else
439 printf("USB Host%d\n",
440 cp110_utmi_data[i].utmi_phy_port);
441 }
442 /* PLL Power up */
443 debug("stage: UTMI PHY power up PLL\n");
444 for (i = 0; i < utmi_phy_count; i++) {
445 reg_set(cp110_utmi_data[i].usb_cfg_addr,
446 0x1 << UTMI_USB_CFG_PLL_OFFSET, UTMI_USB_CFG_PLL_MASK);
447 }
448
449 debug_exit();
450 return;
451}
452
453/*
454 * comphy_dedicated_phys_init initialize the dedicated PHYs
455 * - not muxed SerDes lanes e.g. UTMI PHY
456 */
457void comphy_dedicated_phys_init(void)
458{
459 struct utmi_phy_data cp110_utmi_data[MAX_UTMI_PHY_COUNT];
Omri Itach22bc8682017-04-06 12:54:16 +0300460 int node = -1;
461 int node_idx;
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100462 int parent = -1;
Stefan Roesec0132f62016-08-30 16:48:20 +0200463
464 debug_enter();
465 debug("Initialize USB UTMI PHYs\n");
466
Omri Itach22bc8682017-04-06 12:54:16 +0300467 for (node_idx = 0; node_idx < MAX_UTMI_PHY_COUNT;) {
468 /* Find the UTMI phy node in device tree */
469 node = fdt_node_offset_by_compatible(gd->fdt_blob, node,
470 "marvell,mvebu-utmi-2.6.0");
471 if (node <= 0)
472 break;
Stefan Roesec0132f62016-08-30 16:48:20 +0200473
Omri Itach22bc8682017-04-06 12:54:16 +0300474 /* check if node is enabled */
475 if (!fdtdec_get_is_enabled(gd->fdt_blob, node))
476 continue;
477
Grzegorz Jaszczyka007f232019-02-27 15:35:58 +0100478 parent = fdt_parent_offset(gd->fdt_blob, node);
479 if (parent <= 0)
480 break;
481
482 /* get base address of UTMI PLL */
483 cp110_utmi_data[node_idx].utmi_pll_addr =
484 (void __iomem *)fdtdec_get_addr_size_auto_noparent(
485 gd->fdt_blob, parent, "reg", 0, NULL, true);
486 if (!cp110_utmi_data[node_idx].utmi_pll_addr) {
487 pr_err("UTMI PHY PLL address is invalid\n");
488 continue;
489 }
490
Stefan Roesec0132f62016-08-30 16:48:20 +0200491 /* get base address of UTMI phy */
Omri Itach22bc8682017-04-06 12:54:16 +0300492 cp110_utmi_data[node_idx].utmi_base_addr =
Stefan Roesec0132f62016-08-30 16:48:20 +0200493 (void __iomem *)fdtdec_get_addr_size_auto_noparent(
494 gd->fdt_blob, node, "reg", 0, NULL, true);
Omri Itach22bc8682017-04-06 12:54:16 +0300495 if (!cp110_utmi_data[node_idx].utmi_base_addr) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900496 pr_err("UTMI PHY base address is invalid\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200497 continue;
498 }
499
500 /* get usb config address */
Omri Itach22bc8682017-04-06 12:54:16 +0300501 cp110_utmi_data[node_idx].usb_cfg_addr =
Stefan Roesec0132f62016-08-30 16:48:20 +0200502 (void __iomem *)fdtdec_get_addr_size_auto_noparent(
503 gd->fdt_blob, node, "reg", 1, NULL, true);
Omri Itach22bc8682017-04-06 12:54:16 +0300504 if (!cp110_utmi_data[node_idx].usb_cfg_addr) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900505 pr_err("UTMI PHY base address is invalid\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200506 continue;
507 }
508
509 /* get UTMI config address */
Omri Itach22bc8682017-04-06 12:54:16 +0300510 cp110_utmi_data[node_idx].utmi_cfg_addr =
Stefan Roesec0132f62016-08-30 16:48:20 +0200511 (void __iomem *)fdtdec_get_addr_size_auto_noparent(
512 gd->fdt_blob, node, "reg", 2, NULL, true);
Omri Itach22bc8682017-04-06 12:54:16 +0300513 if (!cp110_utmi_data[node_idx].utmi_cfg_addr) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900514 pr_err("UTMI PHY base address is invalid\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200515 continue;
516 }
517
518 /*
519 * get the port number (to check if the utmi connected to
520 * host/device)
521 */
Omri Itach22bc8682017-04-06 12:54:16 +0300522 cp110_utmi_data[node_idx].utmi_phy_port = fdtdec_get_int(
Stefan Roesec0132f62016-08-30 16:48:20 +0200523 gd->fdt_blob, node, "utmi-port", UTMI_PHY_INVALID);
Omri Itach22bc8682017-04-06 12:54:16 +0300524 if (cp110_utmi_data[node_idx].utmi_phy_port ==
525 UTMI_PHY_INVALID) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900526 pr_err("UTMI PHY port type is invalid\n");
Stefan Roesec0132f62016-08-30 16:48:20 +0200527 continue;
528 }
529
Omri Itach22bc8682017-04-06 12:54:16 +0300530 /* count valid UTMI unit */
531 node_idx++;
Stefan Roesec0132f62016-08-30 16:48:20 +0200532 }
533
Omri Itach22bc8682017-04-06 12:54:16 +0300534 if (node_idx > 0)
535 comphy_utmi_phy_init(node_idx, cp110_utmi_data);
Stefan Roesec0132f62016-08-30 16:48:20 +0200536
537 debug_exit();
538}
539
Stefan Roesec0132f62016-08-30 16:48:20 +0200540int comphy_cp110_init(struct chip_serdes_phy_config *ptr_chip_cfg,
541 struct comphy_map *serdes_map)
542{
543 struct comphy_map *ptr_comphy_map;
544 void __iomem *comphy_base_addr, *hpipe_base_addr;
Igal Liberman5f41aaf2018-05-09 18:50:29 +0300545 u32 comphy_max_count, lane, id, ret = 0;
Stefan Roesec0132f62016-08-30 16:48:20 +0200546 u32 pcie_width = 0;
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +0300547 u32 mode;
Stefan Roesec0132f62016-08-30 16:48:20 +0200548
549 debug_enter();
550
551 comphy_max_count = ptr_chip_cfg->comphy_lanes_count;
552 comphy_base_addr = ptr_chip_cfg->comphy_base_addr;
553 hpipe_base_addr = ptr_chip_cfg->hpipe3_base_addr;
554
Stefan Roesec0132f62016-08-30 16:48:20 +0200555 /* Check if the first 4 lanes configured as By-4 */
556 for (lane = 0, ptr_comphy_map = serdes_map; lane < 4;
557 lane++, ptr_comphy_map++) {
Igal Liberman2dbba242017-04-26 15:40:00 +0300558 if (ptr_comphy_map->type != COMPHY_TYPE_PEX0)
Stefan Roesec0132f62016-08-30 16:48:20 +0200559 break;
560 pcie_width++;
561 }
562
563 for (lane = 0, ptr_comphy_map = serdes_map; lane < comphy_max_count;
564 lane++, ptr_comphy_map++) {
565 debug("Initialize serdes number %d\n", lane);
566 debug("Serdes type = 0x%x\n", ptr_comphy_map->type);
567 if (lane == 4) {
568 /*
569 * PCIe lanes above the first 4 lanes, can be only
570 * by1
571 */
572 pcie_width = 1;
573 }
574 switch (ptr_comphy_map->type) {
Igal Liberman2dbba242017-04-26 15:40:00 +0300575 case COMPHY_TYPE_UNCONNECTED:
Igal Liberman80ebc632018-11-19 09:58:32 +0200576 mode = COMPHY_TYPE_UNCONNECTED | COMPHY_CALLER_UBOOT;
Christine Gharzuzif596b012018-05-23 12:10:36 +0300577 ret = comphy_smc(MV_SIP_COMPHY_POWER_OFF,
578 ptr_chip_cfg->comphy_base_addr,
Igal Liberman80ebc632018-11-19 09:58:32 +0200579 lane, mode);
Igal Liberman2dbba242017-04-26 15:40:00 +0300580 case COMPHY_TYPE_IGNORE:
Stefan Roesec0132f62016-08-30 16:48:20 +0200581 continue;
582 break;
Igal Liberman2dbba242017-04-26 15:40:00 +0300583 case COMPHY_TYPE_PEX0:
584 case COMPHY_TYPE_PEX1:
585 case COMPHY_TYPE_PEX2:
586 case COMPHY_TYPE_PEX3:
Grzegorz Jaszczyk0a1a1642020-10-18 17:11:12 +0300587 mode = COMPHY_FW_PCIE_FORMAT(pcie_width,
588 ptr_comphy_map->clk_src,
589 COMPHY_PCIE_MODE,
590 ptr_comphy_map->speed);
591 ret = comphy_smc(MV_SIP_COMPHY_POWER_ON,
592 ptr_chip_cfg->comphy_base_addr, lane,
593 mode);
Stefan Roesec0132f62016-08-30 16:48:20 +0200594 break;
Igal Liberman2dbba242017-04-26 15:40:00 +0300595 case COMPHY_TYPE_SATA0:
596 case COMPHY_TYPE_SATA1:
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +0300597 mode = COMPHY_FW_MODE_FORMAT(COMPHY_SATA_MODE);
598 ret = comphy_sata_power_up(lane, hpipe_base_addr,
599 comphy_base_addr,
600 ptr_chip_cfg->cp_index,
601 mode);
Stefan Roesec0132f62016-08-30 16:48:20 +0200602 break;
Igal Liberman2dbba242017-04-26 15:40:00 +0300603 case COMPHY_TYPE_USB3_HOST0:
604 case COMPHY_TYPE_USB3_HOST1:
Grzegorz Jaszczykf635c3b2018-03-29 12:30:20 +0200605 mode = COMPHY_FW_MODE_FORMAT(COMPHY_USB3H_MODE);
606 ret = comphy_smc(MV_SIP_COMPHY_POWER_ON,
607 ptr_chip_cfg->comphy_base_addr, lane,
608 mode);
609 break;
Igal Liberman2dbba242017-04-26 15:40:00 +0300610 case COMPHY_TYPE_USB3_DEVICE:
Grzegorz Jaszczykf635c3b2018-03-29 12:30:20 +0200611 mode = COMPHY_FW_MODE_FORMAT(COMPHY_USB3D_MODE);
612 ret = comphy_smc(MV_SIP_COMPHY_POWER_ON,
613 ptr_chip_cfg->comphy_base_addr, lane,
614 mode);
Stefan Roesec0132f62016-08-30 16:48:20 +0200615 break;
Igal Liberman2dbba242017-04-26 15:40:00 +0300616 case COMPHY_TYPE_SGMII0:
617 case COMPHY_TYPE_SGMII1:
Igal Liberman2dbba242017-04-26 15:40:00 +0300618 case COMPHY_TYPE_SGMII2:
Igal Liberman5f41aaf2018-05-09 18:50:29 +0300619 /* Calculate SGMII ID */
620 id = ptr_comphy_map->type - COMPHY_TYPE_SGMII0;
621
Igal Liberman2dbba242017-04-26 15:40:00 +0300622 if (ptr_comphy_map->speed == COMPHY_SPEED_INVALID) {
Stefan Roesec0132f62016-08-30 16:48:20 +0200623 debug("Warning: SGMII PHY speed in lane %d is invalid, set PHY speed to 1.25G\n",
624 lane);
Igal Liberman2dbba242017-04-26 15:40:00 +0300625 ptr_comphy_map->speed = COMPHY_SPEED_1_25G;
Stefan Roesec0132f62016-08-30 16:48:20 +0200626 }
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +0300627
Igal Liberman5f41aaf2018-05-09 18:50:29 +0300628 mode = COMPHY_FW_FORMAT(COMPHY_SGMII_MODE, id,
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +0300629 ptr_comphy_map->speed);
630 ret = comphy_smc(MV_SIP_COMPHY_POWER_ON,
631 ptr_chip_cfg->comphy_base_addr, lane,
632 mode);
Stefan Roesec0132f62016-08-30 16:48:20 +0200633 break;
Igal Liberman341e5482018-05-14 11:20:54 +0300634 case COMPHY_TYPE_SFI0:
635 case COMPHY_TYPE_SFI1:
636 /* Calculate SFI id */
637 id = ptr_comphy_map->type - COMPHY_TYPE_SFI0;
638 mode = COMPHY_FW_FORMAT(COMPHY_SFI_MODE, id,
Grzegorz Jaszczykb24bb992020-10-18 17:11:11 +0300639 ptr_comphy_map->speed);
640 ret = comphy_smc(MV_SIP_COMPHY_POWER_ON,
Igal Liberman341e5482018-05-14 11:20:54 +0300641 ptr_chip_cfg->comphy_base_addr, lane, mode);
Stefan Roesec0132f62016-08-30 16:48:20 +0200642 break;
Igal Liberman2dbba242017-04-26 15:40:00 +0300643 case COMPHY_TYPE_RXAUI0:
644 case COMPHY_TYPE_RXAUI1:
Grzegorz Jaszczyk26d97632018-03-27 12:52:24 +0200645 mode = COMPHY_FW_MODE_FORMAT(COMPHY_RXAUI_MODE);
646 ret = comphy_smc(MV_SIP_COMPHY_POWER_ON,
647 ptr_chip_cfg->comphy_base_addr, lane,
648 mode);
Stefan Roesec0132f62016-08-30 16:48:20 +0200649 break;
650 default:
651 debug("Unknown SerDes type, skip initialize SerDes %d\n",
652 lane);
653 break;
654 }
655 if (ret == 0) {
656 /*
Stefan Roesed37f0202017-04-24 18:45:25 +0300657 * If interface wans't initialized, set the lane to
Igal Liberman2dbba242017-04-26 15:40:00 +0300658 * COMPHY_TYPE_UNCONNECTED state.
Stefan Roesec0132f62016-08-30 16:48:20 +0200659 */
Igal Liberman2dbba242017-04-26 15:40:00 +0300660 ptr_comphy_map->type = COMPHY_TYPE_UNCONNECTED;
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900661 pr_err("PLL is not locked - Failed to initialize lane %d\n",
Stefan Roesec0132f62016-08-30 16:48:20 +0200662 lane);
663 }
664 }
665
666 debug_exit();
667 return 0;
668}