13335786aSStefan Roese /*
23335786aSStefan Roese * Copyright (C) 2015-2016 Marvell International Ltd.
33335786aSStefan Roese *
43335786aSStefan Roese * SPDX-License-Identifier: GPL-2.0+
53335786aSStefan Roese */
63335786aSStefan Roese
73335786aSStefan Roese #include <common.h>
83335786aSStefan Roese #include <asm/io.h>
93335786aSStefan Roese
103335786aSStefan Roese #include "comphy.h"
113335786aSStefan Roese #include "comphy_hpipe.h"
123335786aSStefan Roese
133335786aSStefan Roese /*
143335786aSStefan Roese * comphy_mux_check_config()
153335786aSStefan Roese * description: this function passes over the COMPHY lanes and check if the type
163335786aSStefan Roese * is valid for specific lane. If the type is not valid,
173335786aSStefan Roese * the function update the struct and set the type of the lane as
183335786aSStefan Roese * PHY_TYPE_UNCONNECTED
193335786aSStefan Roese */
comphy_mux_check_config(struct comphy_mux_data * mux_data,struct comphy_map * comphy_map_data,int comphy_max_lanes)203335786aSStefan Roese static void comphy_mux_check_config(struct comphy_mux_data *mux_data,
213335786aSStefan Roese struct comphy_map *comphy_map_data, int comphy_max_lanes)
223335786aSStefan Roese {
233335786aSStefan Roese struct comphy_mux_options *mux_opt;
243335786aSStefan Roese int lane, opt, valid;
253335786aSStefan Roese
263335786aSStefan Roese debug_enter();
273335786aSStefan Roese
283335786aSStefan Roese for (lane = 0; lane < comphy_max_lanes;
293335786aSStefan Roese lane++, comphy_map_data++, mux_data++) {
30*6ecc0b1cSStefan Roese /* Don't check ignored COMPHYs */
31*6ecc0b1cSStefan Roese if (comphy_map_data->type == PHY_TYPE_IGNORE)
32*6ecc0b1cSStefan Roese continue;
33*6ecc0b1cSStefan Roese
343335786aSStefan Roese mux_opt = mux_data->mux_values;
353335786aSStefan Roese for (opt = 0, valid = 0; opt < mux_data->max_lane_values;
363335786aSStefan Roese opt++, mux_opt++) {
373335786aSStefan Roese if (mux_opt->type == comphy_map_data->type) {
383335786aSStefan Roese valid = 1;
393335786aSStefan Roese break;
403335786aSStefan Roese }
413335786aSStefan Roese }
423335786aSStefan Roese if (valid == 0) {
433335786aSStefan Roese debug("lane number %d, had invalid type %d\n",
443335786aSStefan Roese lane, comphy_map_data->type);
453335786aSStefan Roese debug("set lane %d as type %d\n", lane,
463335786aSStefan Roese PHY_TYPE_UNCONNECTED);
473335786aSStefan Roese comphy_map_data->type = PHY_TYPE_UNCONNECTED;
483335786aSStefan Roese } else {
493335786aSStefan Roese debug("lane number %d, has type %d\n",
503335786aSStefan Roese lane, comphy_map_data->type);
513335786aSStefan Roese }
523335786aSStefan Roese }
533335786aSStefan Roese
543335786aSStefan Roese debug_exit();
553335786aSStefan Roese }
563335786aSStefan Roese
comphy_mux_get_mux_value(struct comphy_mux_data * mux_data,u32 type,int lane)573335786aSStefan Roese static u32 comphy_mux_get_mux_value(struct comphy_mux_data *mux_data,
583335786aSStefan Roese u32 type, int lane)
593335786aSStefan Roese {
603335786aSStefan Roese struct comphy_mux_options *mux_opt;
613335786aSStefan Roese int opt;
623335786aSStefan Roese u32 value = 0;
633335786aSStefan Roese
643335786aSStefan Roese debug_enter();
653335786aSStefan Roese
663335786aSStefan Roese mux_opt = mux_data->mux_values;
673335786aSStefan Roese for (opt = 0 ; opt < mux_data->max_lane_values; opt++, mux_opt++) {
683335786aSStefan Roese if (mux_opt->type == type) {
693335786aSStefan Roese value = mux_opt->mux_value;
703335786aSStefan Roese break;
713335786aSStefan Roese }
723335786aSStefan Roese }
733335786aSStefan Roese
743335786aSStefan Roese debug_exit();
753335786aSStefan Roese
763335786aSStefan Roese return value;
773335786aSStefan Roese }
783335786aSStefan 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,u32 bitcount)793335786aSStefan Roese static void comphy_mux_reg_write(struct comphy_mux_data *mux_data,
803335786aSStefan Roese struct comphy_map *comphy_map_data,
813335786aSStefan Roese int comphy_max_lanes,
823335786aSStefan Roese void __iomem *selector_base, 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++) {
90*6ecc0b1cSStefan Roese if (comphy_map_data->type == PHY_TYPE_IGNORE)
91*6ecc0b1cSStefan Roese continue;
92*6ecc0b1cSStefan Roese
933335786aSStefan Roese offset = lane * bitcount;
943335786aSStefan Roese mask = (((1 << bitcount) - 1) << offset);
953335786aSStefan Roese value = (comphy_mux_get_mux_value(mux_data,
963335786aSStefan Roese comphy_map_data->type,
973335786aSStefan Roese lane) << offset);
983335786aSStefan Roese reg_set(selector_base, value, mask);
993335786aSStefan Roese }
1003335786aSStefan Roese
1013335786aSStefan Roese debug_exit();
1023335786aSStefan Roese }
1033335786aSStefan Roese
comphy_mux_init(struct chip_serdes_phy_config * chip_cfg,struct comphy_map * comphy_map_data,void __iomem * selector_base)1043335786aSStefan Roese void comphy_mux_init(struct chip_serdes_phy_config *chip_cfg,
1053335786aSStefan Roese struct comphy_map *comphy_map_data,
1063335786aSStefan Roese void __iomem *selector_base)
1073335786aSStefan Roese {
1083335786aSStefan Roese struct comphy_mux_data *mux_data;
1093335786aSStefan Roese u32 mux_bitcount;
1103335786aSStefan Roese u32 comphy_max_lanes;
1113335786aSStefan Roese
1123335786aSStefan Roese debug_enter();
1133335786aSStefan Roese
1143335786aSStefan Roese comphy_max_lanes = chip_cfg->comphy_lanes_count;
1153335786aSStefan Roese mux_data = chip_cfg->mux_data;
1163335786aSStefan Roese mux_bitcount = chip_cfg->comphy_mux_bitcount;
1173335786aSStefan Roese
1183335786aSStefan Roese /* check if the configuration is valid */
1193335786aSStefan Roese comphy_mux_check_config(mux_data, comphy_map_data, comphy_max_lanes);
1203335786aSStefan Roese /* Init COMPHY selectors */
1213335786aSStefan Roese comphy_mux_reg_write(mux_data, comphy_map_data, comphy_max_lanes,
1223335786aSStefan Roese selector_base, mux_bitcount);
1233335786aSStefan Roese
1243335786aSStefan Roese debug_exit();
1253335786aSStefan Roese }
126