xref: /openbmc/u-boot/drivers/phy/marvell/comphy_mux.c (revision 333279af23ac08ebc8d8056c677c98964dd013b6)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
23335786aSStefan Roese /*
33335786aSStefan Roese  * Copyright (C) 2015-2016 Marvell International Ltd.
43335786aSStefan Roese  */
53335786aSStefan Roese 
63335786aSStefan Roese #include <common.h>
73335786aSStefan Roese #include <asm/io.h>
83335786aSStefan Roese 
9*4b8cb843SMarek Behún #include "comphy_core.h"
103335786aSStefan Roese #include "comphy_hpipe.h"
113335786aSStefan Roese 
123335786aSStefan Roese /*
133335786aSStefan Roese  * comphy_mux_check_config()
143335786aSStefan Roese  * description: this function passes over the COMPHY lanes and check if the type
153335786aSStefan Roese  *              is valid for specific lane. If the type is not valid,
163335786aSStefan Roese  *              the function update the struct and set the type of the lane as
173335786aSStefan Roese  *              PHY_TYPE_UNCONNECTED
183335786aSStefan Roese  */
comphy_mux_check_config(struct comphy_mux_data * mux_data,struct comphy_map * comphy_map_data,int comphy_max_lanes)193335786aSStefan Roese static void comphy_mux_check_config(struct comphy_mux_data *mux_data,
203335786aSStefan Roese 		struct comphy_map *comphy_map_data, int comphy_max_lanes)
213335786aSStefan Roese {
223335786aSStefan Roese 	struct comphy_mux_options *mux_opt;
233335786aSStefan Roese 	int lane, opt, valid;
243335786aSStefan Roese 
253335786aSStefan Roese 	debug_enter();
263335786aSStefan Roese 
273335786aSStefan Roese 	for (lane = 0; lane < comphy_max_lanes;
283335786aSStefan Roese 	     lane++, comphy_map_data++, mux_data++) {
296ecc0b1cSStefan Roese 		/* Don't check ignored COMPHYs */
306ecc0b1cSStefan Roese 		if (comphy_map_data->type == PHY_TYPE_IGNORE)
316ecc0b1cSStefan Roese 			continue;
326ecc0b1cSStefan Roese 
333335786aSStefan Roese 		mux_opt = mux_data->mux_values;
343335786aSStefan Roese 		for (opt = 0, valid = 0; opt < mux_data->max_lane_values;
353335786aSStefan Roese 		     opt++, mux_opt++) {
363335786aSStefan Roese 			if (mux_opt->type == comphy_map_data->type) {
373335786aSStefan Roese 				valid = 1;
383335786aSStefan Roese 				break;
393335786aSStefan Roese 			}
403335786aSStefan Roese 		}
413335786aSStefan Roese 		if (valid == 0) {
423335786aSStefan Roese 			debug("lane number %d, had invalid type %d\n",
433335786aSStefan Roese 			      lane, comphy_map_data->type);
443335786aSStefan Roese 			debug("set lane %d as type %d\n", lane,
453335786aSStefan Roese 			      PHY_TYPE_UNCONNECTED);
463335786aSStefan Roese 			comphy_map_data->type = PHY_TYPE_UNCONNECTED;
473335786aSStefan Roese 		} else {
483335786aSStefan Roese 			debug("lane number %d, has type %d\n",
493335786aSStefan Roese 			      lane, comphy_map_data->type);
503335786aSStefan Roese 		}
513335786aSStefan Roese 	}
523335786aSStefan Roese 
533335786aSStefan Roese 	debug_exit();
543335786aSStefan Roese }
553335786aSStefan Roese 
comphy_mux_get_mux_value(struct comphy_mux_data * mux_data,u32 type,int lane)563335786aSStefan Roese static u32 comphy_mux_get_mux_value(struct comphy_mux_data *mux_data,
573335786aSStefan Roese 				    u32 type, int lane)
583335786aSStefan Roese {
593335786aSStefan Roese 	struct comphy_mux_options *mux_opt;
603335786aSStefan Roese 	int opt;
613335786aSStefan Roese 	u32 value = 0;
623335786aSStefan Roese 
633335786aSStefan Roese 	debug_enter();
643335786aSStefan Roese 
653335786aSStefan Roese 	mux_opt = mux_data->mux_values;
663335786aSStefan Roese 	for (opt = 0 ; opt < mux_data->max_lane_values; opt++, mux_opt++) {
673335786aSStefan Roese 		if (mux_opt->type == type) {
683335786aSStefan Roese 			value = mux_opt->mux_value;
693335786aSStefan Roese 			break;
703335786aSStefan Roese 		}
713335786aSStefan Roese 	}
723335786aSStefan Roese 
733335786aSStefan Roese 	debug_exit();
743335786aSStefan Roese 
753335786aSStefan Roese 	return value;
763335786aSStefan Roese }
773335786aSStefan Roese 
comphy_mux_reg_write(struct comphy_mux_data * mux_data,struct comphy_map * comphy_map_data,int comphy_max_lanes,void __iomem * selector_base,const fdt32_t * mux_lane_order,u32 bitcount)783335786aSStefan Roese static void comphy_mux_reg_write(struct comphy_mux_data *mux_data,
793335786aSStefan Roese 				 struct comphy_map *comphy_map_data,
803335786aSStefan Roese 				 int comphy_max_lanes,
817586ac2bSMarek Behún 				 void __iomem *selector_base,
827586ac2bSMarek Behún 				 const fdt32_t *mux_lane_order, u32 bitcount)
833335786aSStefan Roese {
843335786aSStefan Roese 	u32 lane, value, offset, mask;
853335786aSStefan Roese 
863335786aSStefan Roese 	debug_enter();
873335786aSStefan Roese 
883335786aSStefan Roese 	for (lane = 0; lane < comphy_max_lanes;
893335786aSStefan Roese 	     lane++, comphy_map_data++, mux_data++) {
906ecc0b1cSStefan Roese 		if (comphy_map_data->type == PHY_TYPE_IGNORE)
916ecc0b1cSStefan Roese 			continue;
926ecc0b1cSStefan Roese 
937586ac2bSMarek Behún 		/*
947586ac2bSMarek Behún 		 * if the order of nodes in selector base register is
957586ac2bSMarek Behún 		 * nontrivial, use mapping from mux_lane_order
967586ac2bSMarek Behún 		 */
977586ac2bSMarek Behún 		if (mux_lane_order)
987586ac2bSMarek Behún 			offset = fdt32_to_cpu(mux_lane_order[lane]) * bitcount;
997586ac2bSMarek Behún 		else
1003335786aSStefan Roese 			offset = lane * bitcount;
1017586ac2bSMarek Behún 
1023335786aSStefan Roese 		mask = (((1 << bitcount) - 1) << offset);
1033335786aSStefan Roese 		value = (comphy_mux_get_mux_value(mux_data,
1043335786aSStefan Roese 						  comphy_map_data->type,
1053335786aSStefan Roese 						  lane) << offset);
1063335786aSStefan Roese 		reg_set(selector_base, value, mask);
1073335786aSStefan Roese 	}
1083335786aSStefan Roese 
1093335786aSStefan Roese 	debug_exit();
1103335786aSStefan Roese }
1113335786aSStefan Roese 
comphy_mux_init(struct chip_serdes_phy_config * chip_cfg,struct comphy_map * comphy_map_data,void __iomem * selector_base)1123335786aSStefan Roese void comphy_mux_init(struct chip_serdes_phy_config *chip_cfg,
1133335786aSStefan Roese 		     struct comphy_map *comphy_map_data,
1143335786aSStefan Roese 		     void __iomem *selector_base)
1153335786aSStefan Roese {
1163335786aSStefan Roese 	struct comphy_mux_data *mux_data;
1177586ac2bSMarek Behún 	const fdt32_t *mux_lane_order;
1183335786aSStefan Roese 	u32 mux_bitcount;
1193335786aSStefan Roese 	u32 comphy_max_lanes;
1203335786aSStefan Roese 
1213335786aSStefan Roese 	debug_enter();
1223335786aSStefan Roese 
1233335786aSStefan Roese 	comphy_max_lanes = chip_cfg->comphy_lanes_count;
1243335786aSStefan Roese 	mux_data = chip_cfg->mux_data;
1257586ac2bSMarek Behún 	mux_lane_order = chip_cfg->comphy_mux_lane_order;
1263335786aSStefan Roese 	mux_bitcount = chip_cfg->comphy_mux_bitcount;
1273335786aSStefan Roese 
1283335786aSStefan Roese 	/* check if the configuration is valid */
1293335786aSStefan Roese 	comphy_mux_check_config(mux_data, comphy_map_data, comphy_max_lanes);
1303335786aSStefan Roese 	/* Init COMPHY selectors */
1313335786aSStefan Roese 	comphy_mux_reg_write(mux_data, comphy_map_data, comphy_max_lanes,
1327586ac2bSMarek Behún 			     selector_base, mux_lane_order, mux_bitcount);
1333335786aSStefan Roese 
1343335786aSStefan Roese 	debug_exit();
1353335786aSStefan Roese }
136