1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015-2016 Marvell International Ltd.
4  *
5  * Copyright (C) 2016 Stefan Roese <sr@denx.de>
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <fdtdec.h>
11 #include <asm/global_data.h>
12 #include <asm/io.h>
13 #include <dm/device_compat.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/libfdt.h>
17 
18 #include "comphy_core.h"
19 
20 #define COMPHY_MAX_CHIP 4
21 
22 DECLARE_GLOBAL_DATA_PTR;
23 
get_speed_string(u32 speed)24 static const char *get_speed_string(u32 speed)
25 {
26 	static const char * const speed_strings[] = {
27 		"1.25 Gbps", "2.5 Gbps", "3.125 Gbps",
28 		"5 Gbps", "5.125 Gpbs", "6 Gbps",
29 		"10.3125 Gbps"
30 	};
31 
32 	if (speed < 0 || speed > COMPHY_SPEED_MAX)
33 		return "invalid";
34 
35 	return speed_strings[speed];
36 }
37 
get_type_string(u32 type)38 static const char *get_type_string(u32 type)
39 {
40 	static const char * const type_strings[] = {
41 		"UNCONNECTED", "PEX0", "PEX1", "PEX2", "PEX3",
42 		"SATA0", "SATA1", "SGMII0", "SGMII1", "SGMII2",
43 		"USB3", "USB3_HOST0", "USB3_HOST1",
44 		"USB3_DEVICE", "RXAUI0", "RXAUI1", "SFI0", "SFI1", "AP",
45 		"IGNORE"
46 	};
47 
48 	if (type < 0 || type > COMPHY_TYPE_MAX)
49 		return "invalid";
50 
51 	return type_strings[type];
52 }
53 
comphy_print(struct chip_serdes_phy_config * chip_cfg,struct comphy_map * comphy_map_data)54 void comphy_print(struct chip_serdes_phy_config *chip_cfg,
55 		  struct comphy_map *comphy_map_data)
56 {
57 	u32 lane;
58 
59 	for (lane = 0; lane < chip_cfg->comphy_lanes_count;
60 	     lane++, comphy_map_data++) {
61 		if (comphy_map_data->speed == COMPHY_SPEED_INVALID) {
62 			printf("Comphy-%d: %-13s\n", lane,
63 			       get_type_string(comphy_map_data->type));
64 		} else {
65 			printf("Comphy-%d: %-13s %-10s\n", lane,
66 			       get_type_string(comphy_map_data->type),
67 			       get_speed_string(comphy_map_data->speed));
68 		}
69 	}
70 }
71 
comphy_rx_training(struct udevice * dev,u32 lane)72 int comphy_rx_training(struct udevice *dev, u32 lane)
73 {
74 	struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
75 
76 	if (chip_cfg->rx_training)
77 		return chip_cfg->rx_training(chip_cfg, lane);
78 
79 	return 0;
80 }
81 
comphy_update_map(struct comphy_map * serdes_map,int count)82 __weak int comphy_update_map(struct comphy_map *serdes_map, int count)
83 {
84 	return 0;
85 }
86 
comphy_probe(struct udevice * dev)87 static int comphy_probe(struct udevice *dev)
88 {
89 	const void *blob = gd->fdt_blob;
90 	int node = dev_of_offset(dev);
91 	struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
92 	int subnode;
93 	int lane;
94 	int last_idx = 0;
95 	static int current_idx;
96 	int res;
97 
98 	/* Save base addresses for later use */
99 	chip_cfg->comphy_base_addr = (void *)devfdt_get_addr_index(dev, 0);
100 	if (IS_ERR(chip_cfg->comphy_base_addr))
101 		return PTR_ERR(chip_cfg->comphy_base_addr);
102 
103 	chip_cfg->hpipe3_base_addr = (void *)devfdt_get_addr_index(dev, 1);
104 	if (IS_ERR(chip_cfg->hpipe3_base_addr))
105 		return PTR_ERR(chip_cfg->hpipe3_base_addr);
106 
107 	chip_cfg->comphy_lanes_count = fdtdec_get_int(blob, node,
108 						      "max-lanes", 0);
109 	if (chip_cfg->comphy_lanes_count <= 0) {
110 		dev_err(dev, "comphy max lanes is wrong\n");
111 		return -EINVAL;
112 	}
113 
114 	chip_cfg->comphy_mux_bitcount = fdtdec_get_int(blob, node,
115 						       "mux-bitcount", 0);
116 	if (chip_cfg->comphy_mux_bitcount <= 0) {
117 		dev_err(dev, "comphy mux bit count is wrong\n");
118 		return -EINVAL;
119 	}
120 
121 	chip_cfg->comphy_mux_lane_order =
122 		fdtdec_locate_array(blob, node, "mux-lane-order",
123 				    chip_cfg->comphy_lanes_count);
124 
125 	if (device_is_compatible(dev, "marvell,comphy-armada-3700")) {
126 		chip_cfg->ptr_comphy_chip_init = comphy_a3700_init;
127 		chip_cfg->rx_training = NULL;
128 	}
129 
130 	if (device_is_compatible(dev, "marvell,comphy-cp110")) {
131 		chip_cfg->ptr_comphy_chip_init = comphy_cp110_init;
132 		chip_cfg->rx_training = comphy_cp110_sfi_rx_training;
133 	}
134 
135 	/*
136 	 * Bail out if no chip_init function is defined, e.g. no
137 	 * compatible node is found
138 	 */
139 	if (!chip_cfg->ptr_comphy_chip_init) {
140 		dev_err(dev, "comphy: No compatible DT node found\n");
141 		return -ENODEV;
142 	}
143 
144 	lane = 0;
145 	fdt_for_each_subnode(subnode, blob, node) {
146 		/* Skip disabled ports */
147 		if (!fdtdec_get_is_enabled(blob, subnode))
148 			continue;
149 
150 		chip_cfg->comphy_map_data[lane].type =
151 			fdtdec_get_int(blob, subnode, "phy-type",
152 				       COMPHY_TYPE_INVALID);
153 
154 		if (chip_cfg->comphy_map_data[lane].type ==
155 		    COMPHY_TYPE_INVALID) {
156 			printf("no phy type for lane %d, setting lane as unconnected\n",
157 			       lane + 1);
158 			continue;
159 		}
160 
161 		chip_cfg->comphy_map_data[lane].speed =
162 			fdtdec_get_int(blob, subnode, "phy-speed",
163 				       COMPHY_SPEED_INVALID);
164 
165 		chip_cfg->comphy_map_data[lane].invert =
166 			fdtdec_get_int(blob, subnode, "phy-invert",
167 				       COMPHY_POLARITY_NO_INVERT);
168 
169 		chip_cfg->comphy_map_data[lane].clk_src =
170 			fdtdec_get_bool(blob, subnode, "clk-src");
171 
172 		chip_cfg->comphy_map_data[lane].end_point =
173 			fdtdec_get_bool(blob, subnode, "end_point");
174 
175 		lane++;
176 	}
177 
178 	res = comphy_update_map(chip_cfg->comphy_map_data, chip_cfg->comphy_lanes_count);
179 	if (res < 0)
180 		return res;
181 
182 	/* Save CP index for MultiCP devices (A8K) */
183 	chip_cfg->cp_index = current_idx++;
184 	/* PHY power UP sequence */
185 	chip_cfg->ptr_comphy_chip_init(chip_cfg, chip_cfg->comphy_map_data);
186 	/* PHY print SerDes status */
187 	printf("Comphy chip #%d:\n", chip_cfg->cp_index);
188 	comphy_print(chip_cfg, chip_cfg->comphy_map_data);
189 
190 	/*
191 	 * Only run the dedicated PHY init code once, in the last PHY init call
192 	 */
193 	if (of_machine_is_compatible("marvell,armada8040"))
194 		last_idx = 1;
195 
196 	if (chip_cfg->cp_index == last_idx) {
197 		/* Initialize dedicated PHYs (not muxed SerDes lanes) */
198 		comphy_dedicated_phys_init();
199 	}
200 
201 	return 0;
202 }
203 
204 static const struct udevice_id comphy_ids[] = {
205 	{ .compatible = "marvell,mvebu-comphy" },
206 	{ }
207 };
208 
209 U_BOOT_DRIVER(mvebu_comphy) = {
210 	.name	= "mvebu_comphy",
211 	.id	= UCLASS_MISC,
212 	.of_match = comphy_ids,
213 	.probe	= comphy_probe,
214 	.priv_auto	= sizeof(struct chip_serdes_phy_config),
215 };
216