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 <linux/errno.h>
12 #include <asm/io.h>
13 
14 #include "comphy.h"
15 
16 #define COMPHY_MAX_CHIP 4
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
20 static const char *get_speed_string(u32 speed)
21 {
22 	static const char * const speed_strings[] = {
23 		"1.25 Gbps", "1.5 Gbps", "2.5 Gbps",
24 		"3.0 Gbps", "3.125 Gbps", "5 Gbps", "6 Gbps",
25 		"6.25 Gbps", "10.31 Gbps"
26 	};
27 
28 	if (speed < 0 || speed > PHY_SPEED_MAX)
29 		return "invalid";
30 
31 	return speed_strings[speed];
32 }
33 
34 static const char *get_type_string(u32 type)
35 {
36 	static const char * const type_strings[] = {
37 		"UNCONNECTED", "PEX0", "PEX1", "PEX2", "PEX3",
38 		"SATA0", "SATA1", "SATA2", "SATA3", "SGMII0",
39 		"SGMII1", "SGMII2", "SGMII3", "QSGMII",
40 		"USB3_HOST0", "USB3_HOST1", "USB3_DEVICE",
41 		"XAUI0", "XAUI1", "XAUI2", "XAUI3",
42 		"RXAUI0", "RXAUI1", "SFI", "IGNORE"
43 	};
44 
45 	if (type < 0 || type > PHY_TYPE_MAX)
46 		return "invalid";
47 
48 	return type_strings[type];
49 }
50 
51 void comphy_print(struct chip_serdes_phy_config *chip_cfg,
52 		  struct comphy_map *comphy_map_data)
53 {
54 	u32 lane;
55 
56 	for (lane = 0; lane < chip_cfg->comphy_lanes_count;
57 	     lane++, comphy_map_data++) {
58 		if (comphy_map_data->speed == PHY_SPEED_INVALID) {
59 			printf("Comphy-%d: %-13s\n", lane,
60 			       get_type_string(comphy_map_data->type));
61 		} else {
62 			printf("Comphy-%d: %-13s %-10s\n", lane,
63 			       get_type_string(comphy_map_data->type),
64 			       get_speed_string(comphy_map_data->speed));
65 		}
66 	}
67 }
68 
69 static int comphy_probe(struct udevice *dev)
70 {
71 	const void *blob = gd->fdt_blob;
72 	int node = dev_of_offset(dev);
73 	struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
74 	struct comphy_map comphy_map_data[MAX_LANE_OPTIONS];
75 	int subnode;
76 	int lane;
77 	int last_idx = 0;
78 	static int current_idx;
79 
80 	/* Save base addresses for later use */
81 	chip_cfg->comphy_base_addr = (void *)devfdt_get_addr_index(dev, 0);
82 	if (IS_ERR(chip_cfg->comphy_base_addr))
83 		return PTR_ERR(chip_cfg->comphy_base_addr);
84 
85 	chip_cfg->hpipe3_base_addr = (void *)devfdt_get_addr_index(dev, 1);
86 	if (IS_ERR(chip_cfg->hpipe3_base_addr))
87 		return PTR_ERR(chip_cfg->hpipe3_base_addr);
88 
89 	chip_cfg->comphy_lanes_count = fdtdec_get_int(blob, node,
90 						      "max-lanes", 0);
91 	if (chip_cfg->comphy_lanes_count <= 0) {
92 		dev_err(&dev->dev, "comphy max lanes is wrong\n");
93 		return -EINVAL;
94 	}
95 
96 	chip_cfg->comphy_mux_bitcount = fdtdec_get_int(blob, node,
97 						       "mux-bitcount", 0);
98 	if (chip_cfg->comphy_mux_bitcount <= 0) {
99 		dev_err(&dev->dev, "comphy mux bit count is wrong\n");
100 		return -EINVAL;
101 	}
102 
103 	chip_cfg->comphy_mux_lane_order =
104 		fdtdec_locate_array(blob, node, "mux-lane-order",
105 				    chip_cfg->comphy_lanes_count);
106 
107 	if (device_is_compatible(dev, "marvell,comphy-armada-3700"))
108 		chip_cfg->ptr_comphy_chip_init = comphy_a3700_init;
109 
110 	if (device_is_compatible(dev, "marvell,comphy-cp110"))
111 		chip_cfg->ptr_comphy_chip_init = comphy_cp110_init;
112 
113 	/*
114 	 * Bail out if no chip_init function is defined, e.g. no
115 	 * compatible node is found
116 	 */
117 	if (!chip_cfg->ptr_comphy_chip_init) {
118 		dev_err(&dev->dev, "comphy: No compatible DT node found\n");
119 		return -ENODEV;
120 	}
121 
122 	lane = 0;
123 	fdt_for_each_subnode(subnode, blob, node) {
124 		/* Skip disabled ports */
125 		if (!fdtdec_get_is_enabled(blob, subnode))
126 			continue;
127 
128 		comphy_map_data[lane].speed = fdtdec_get_int(
129 			blob, subnode, "phy-speed", PHY_TYPE_INVALID);
130 		comphy_map_data[lane].type = fdtdec_get_int(
131 			blob, subnode, "phy-type", PHY_SPEED_INVALID);
132 		comphy_map_data[lane].invert = fdtdec_get_int(
133 			blob, subnode, "phy-invert", PHY_POLARITY_NO_INVERT);
134 		comphy_map_data[lane].clk_src = fdtdec_get_bool(blob, subnode,
135 								"clk-src");
136 		comphy_map_data[lane].end_point = fdtdec_get_bool(blob, subnode,
137 								  "end_point");
138 		if (comphy_map_data[lane].type == PHY_TYPE_INVALID) {
139 			printf("no phy type for lane %d, setting lane as unconnected\n",
140 			       lane + 1);
141 		}
142 
143 		lane++;
144 	}
145 
146 	/* Save CP index for MultiCP devices (A8K) */
147 	chip_cfg->cp_index = current_idx++;
148 	/* PHY power UP sequence */
149 	chip_cfg->ptr_comphy_chip_init(chip_cfg, comphy_map_data);
150 	/* PHY print SerDes status */
151 	if (of_machine_is_compatible("marvell,armada8040"))
152 		printf("Comphy chip #%d:\n", chip_cfg->cp_index);
153 	comphy_print(chip_cfg, comphy_map_data);
154 
155 	/*
156 	 * Only run the dedicated PHY init code once, in the last PHY init call
157 	 */
158 	if (of_machine_is_compatible("marvell,armada8040"))
159 		last_idx = 1;
160 
161 	if (chip_cfg->cp_index == last_idx) {
162 		/* Initialize dedicated PHYs (not muxed SerDes lanes) */
163 		comphy_dedicated_phys_init();
164 	}
165 
166 	return 0;
167 }
168 
169 static const struct udevice_id comphy_ids[] = {
170 	{ .compatible = "marvell,mvebu-comphy" },
171 	{ }
172 };
173 
174 U_BOOT_DRIVER(mvebu_comphy) = {
175 	.name	= "mvebu_comphy",
176 	.id	= UCLASS_MISC,
177 	.of_match = comphy_ids,
178 	.probe	= comphy_probe,
179 	.priv_auto_alloc_size = sizeof(struct chip_serdes_phy_config),
180 };
181