blob: a8aa37fc46f354278c5b52e1f85073f7cdd5d039 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese8f64e262016-05-23 11:12:05 +02002/*
3 * Copyright (C) 2015-2016 Marvell International Ltd.
Stefan Roese8f64e262016-05-23 11:12:05 +02004 */
5
Simon Glass0f2af882020-05-10 11:40:05 -06006#include <log.h>
Stefan Roese8f64e262016-05-23 11:12:05 +02007#include <asm/io.h>
8
Marek Behún19ce44c2018-08-17 12:58:51 +02009#include "comphy_core.h"
Stefan Roese8f64e262016-05-23 11:12:05 +020010
11/*
12 * comphy_mux_check_config()
13 * description: this function passes over the COMPHY lanes and check if the type
14 * is valid for specific lane. If the type is not valid,
15 * the function update the struct and set the type of the lane as
Igal Libermanffd5d2f2017-04-26 15:40:00 +030016 * COMPHY_TYPE_UNCONNECTED
Stefan Roese8f64e262016-05-23 11:12:05 +020017 */
18static void comphy_mux_check_config(struct comphy_mux_data *mux_data,
19 struct comphy_map *comphy_map_data, int comphy_max_lanes)
20{
21 struct comphy_mux_options *mux_opt;
22 int lane, opt, valid;
23
24 debug_enter();
25
26 for (lane = 0; lane < comphy_max_lanes;
27 lane++, comphy_map_data++, mux_data++) {
Stefan Roesef4fed5c2017-04-24 18:45:24 +030028 /* Don't check ignored COMPHYs */
Igal Libermanffd5d2f2017-04-26 15:40:00 +030029 if (comphy_map_data->type == COMPHY_TYPE_IGNORE)
Stefan Roesef4fed5c2017-04-24 18:45:24 +030030 continue;
31
Stefan Roese8f64e262016-05-23 11:12:05 +020032 mux_opt = mux_data->mux_values;
33 for (opt = 0, valid = 0; opt < mux_data->max_lane_values;
34 opt++, mux_opt++) {
35 if (mux_opt->type == comphy_map_data->type) {
36 valid = 1;
37 break;
38 }
39 }
40 if (valid == 0) {
41 debug("lane number %d, had invalid type %d\n",
42 lane, comphy_map_data->type);
43 debug("set lane %d as type %d\n", lane,
Igal Libermanffd5d2f2017-04-26 15:40:00 +030044 COMPHY_TYPE_UNCONNECTED);
45 comphy_map_data->type = COMPHY_TYPE_UNCONNECTED;
Stefan Roese8f64e262016-05-23 11:12:05 +020046 } else {
47 debug("lane number %d, has type %d\n",
48 lane, comphy_map_data->type);
49 }
50 }
51
52 debug_exit();
53}
54
55static u32 comphy_mux_get_mux_value(struct comphy_mux_data *mux_data,
56 u32 type, int lane)
57{
58 struct comphy_mux_options *mux_opt;
59 int opt;
60 u32 value = 0;
61
62 debug_enter();
63
64 mux_opt = mux_data->mux_values;
65 for (opt = 0 ; opt < mux_data->max_lane_values; opt++, mux_opt++) {
66 if (mux_opt->type == type) {
67 value = mux_opt->mux_value;
68 break;
69 }
70 }
71
72 debug_exit();
73
74 return value;
75}
76
77static void comphy_mux_reg_write(struct comphy_mux_data *mux_data,
78 struct comphy_map *comphy_map_data,
79 int comphy_max_lanes,
Marek Behún13d85392018-04-24 17:21:21 +020080 void __iomem *selector_base,
81 const fdt32_t *mux_lane_order, u32 bitcount)
Stefan Roese8f64e262016-05-23 11:12:05 +020082{
83 u32 lane, value, offset, mask;
84
85 debug_enter();
86
87 for (lane = 0; lane < comphy_max_lanes;
88 lane++, comphy_map_data++, mux_data++) {
Igal Libermanffd5d2f2017-04-26 15:40:00 +030089 if (comphy_map_data->type == COMPHY_TYPE_IGNORE)
Stefan Roesef4fed5c2017-04-24 18:45:24 +030090 continue;
91
Marek Behún13d85392018-04-24 17:21:21 +020092 /*
93 * if the order of nodes in selector base register is
94 * nontrivial, use mapping from mux_lane_order
95 */
96 if (mux_lane_order)
97 offset = fdt32_to_cpu(mux_lane_order[lane]) * bitcount;
98 else
99 offset = lane * bitcount;
100
Stefan Roese8f64e262016-05-23 11:12:05 +0200101 mask = (((1 << bitcount) - 1) << offset);
102 value = (comphy_mux_get_mux_value(mux_data,
103 comphy_map_data->type,
104 lane) << offset);
105 reg_set(selector_base, value, mask);
106 }
107
108 debug_exit();
109}
110
111void comphy_mux_init(struct chip_serdes_phy_config *chip_cfg,
112 struct comphy_map *comphy_map_data,
113 void __iomem *selector_base)
114{
115 struct comphy_mux_data *mux_data;
Marek Behún13d85392018-04-24 17:21:21 +0200116 const fdt32_t *mux_lane_order;
Stefan Roese8f64e262016-05-23 11:12:05 +0200117 u32 mux_bitcount;
118 u32 comphy_max_lanes;
119
120 debug_enter();
121
122 comphy_max_lanes = chip_cfg->comphy_lanes_count;
123 mux_data = chip_cfg->mux_data;
Marek Behún13d85392018-04-24 17:21:21 +0200124 mux_lane_order = chip_cfg->comphy_mux_lane_order;
Stefan Roese8f64e262016-05-23 11:12:05 +0200125 mux_bitcount = chip_cfg->comphy_mux_bitcount;
126
127 /* check if the configuration is valid */
128 comphy_mux_check_config(mux_data, comphy_map_data, comphy_max_lanes);
129 /* Init COMPHY selectors */
130 comphy_mux_reg_write(mux_data, comphy_map_data, comphy_max_lanes,
Marek Behún13d85392018-04-24 17:21:21 +0200131 selector_base, mux_lane_order, mux_bitcount);
Stefan Roese8f64e262016-05-23 11:12:05 +0200132
133 debug_exit();
134}