blob: 98327557a89980a65ab9555a73434bc04e6db54f [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
6#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Stefan Roese8f64e262016-05-23 11:12:05 +02008#include <asm/io.h>
9
Marek Behún19ce44c2018-08-17 12:58:51 +020010#include "comphy_core.h"
Stefan Roese8f64e262016-05-23 11:12:05 +020011#include "comphy_hpipe.h"
12
13/*
14 * comphy_mux_check_config()
15 * description: this function passes over the COMPHY lanes and check if the type
16 * is valid for specific lane. If the type is not valid,
17 * the function update the struct and set the type of the lane as
18 * PHY_TYPE_UNCONNECTED
19 */
20static void comphy_mux_check_config(struct comphy_mux_data *mux_data,
21 struct comphy_map *comphy_map_data, int comphy_max_lanes)
22{
23 struct comphy_mux_options *mux_opt;
24 int lane, opt, valid;
25
26 debug_enter();
27
28 for (lane = 0; lane < comphy_max_lanes;
29 lane++, comphy_map_data++, mux_data++) {
Stefan Roesef4fed5c2017-04-24 18:45:24 +030030 /* Don't check ignored COMPHYs */
31 if (comphy_map_data->type == PHY_TYPE_IGNORE)
32 continue;
33
Stefan Roese8f64e262016-05-23 11:12:05 +020034 mux_opt = mux_data->mux_values;
35 for (opt = 0, valid = 0; opt < mux_data->max_lane_values;
36 opt++, mux_opt++) {
37 if (mux_opt->type == comphy_map_data->type) {
38 valid = 1;
39 break;
40 }
41 }
42 if (valid == 0) {
43 debug("lane number %d, had invalid type %d\n",
44 lane, comphy_map_data->type);
45 debug("set lane %d as type %d\n", lane,
46 PHY_TYPE_UNCONNECTED);
47 comphy_map_data->type = PHY_TYPE_UNCONNECTED;
48 } else {
49 debug("lane number %d, has type %d\n",
50 lane, comphy_map_data->type);
51 }
52 }
53
54 debug_exit();
55}
56
57static u32 comphy_mux_get_mux_value(struct comphy_mux_data *mux_data,
58 u32 type, int lane)
59{
60 struct comphy_mux_options *mux_opt;
61 int opt;
62 u32 value = 0;
63
64 debug_enter();
65
66 mux_opt = mux_data->mux_values;
67 for (opt = 0 ; opt < mux_data->max_lane_values; opt++, mux_opt++) {
68 if (mux_opt->type == type) {
69 value = mux_opt->mux_value;
70 break;
71 }
72 }
73
74 debug_exit();
75
76 return value;
77}
78
79static void comphy_mux_reg_write(struct comphy_mux_data *mux_data,
80 struct comphy_map *comphy_map_data,
81 int comphy_max_lanes,
Marek Behún13d85392018-04-24 17:21:21 +020082 void __iomem *selector_base,
83 const fdt32_t *mux_lane_order, u32 bitcount)
Stefan Roese8f64e262016-05-23 11:12:05 +020084{
85 u32 lane, value, offset, mask;
86
87 debug_enter();
88
89 for (lane = 0; lane < comphy_max_lanes;
90 lane++, comphy_map_data++, mux_data++) {
Stefan Roesef4fed5c2017-04-24 18:45:24 +030091 if (comphy_map_data->type == PHY_TYPE_IGNORE)
92 continue;
93
Marek Behún13d85392018-04-24 17:21:21 +020094 /*
95 * if the order of nodes in selector base register is
96 * nontrivial, use mapping from mux_lane_order
97 */
98 if (mux_lane_order)
99 offset = fdt32_to_cpu(mux_lane_order[lane]) * bitcount;
100 else
101 offset = lane * bitcount;
102
Stefan Roese8f64e262016-05-23 11:12:05 +0200103 mask = (((1 << bitcount) - 1) << offset);
104 value = (comphy_mux_get_mux_value(mux_data,
105 comphy_map_data->type,
106 lane) << offset);
107 reg_set(selector_base, value, mask);
108 }
109
110 debug_exit();
111}
112
113void comphy_mux_init(struct chip_serdes_phy_config *chip_cfg,
114 struct comphy_map *comphy_map_data,
115 void __iomem *selector_base)
116{
117 struct comphy_mux_data *mux_data;
Marek Behún13d85392018-04-24 17:21:21 +0200118 const fdt32_t *mux_lane_order;
Stefan Roese8f64e262016-05-23 11:12:05 +0200119 u32 mux_bitcount;
120 u32 comphy_max_lanes;
121
122 debug_enter();
123
124 comphy_max_lanes = chip_cfg->comphy_lanes_count;
125 mux_data = chip_cfg->mux_data;
Marek Behún13d85392018-04-24 17:21:21 +0200126 mux_lane_order = chip_cfg->comphy_mux_lane_order;
Stefan Roese8f64e262016-05-23 11:12:05 +0200127 mux_bitcount = chip_cfg->comphy_mux_bitcount;
128
129 /* check if the configuration is valid */
130 comphy_mux_check_config(mux_data, comphy_map_data, comphy_max_lanes);
131 /* Init COMPHY selectors */
132 comphy_mux_reg_write(mux_data, comphy_map_data, comphy_max_lanes,
Marek Behún13d85392018-04-24 17:21:21 +0200133 selector_base, mux_lane_order, mux_bitcount);
Stefan Roese8f64e262016-05-23 11:12:05 +0200134
135 debug_exit();
136}