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_probe(struct udevice * dev)82 static int comphy_probe(struct udevice *dev)
83 {
84 	int node = dev_of_offset(dev);
85 	struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
86 	int last_idx = 0;
87 	static int current_idx;
88 	int res;
89 
90 	/* Save base addresses for later use */
91 	chip_cfg->comphy_base_addr = devfdt_get_addr_index_ptr(dev, 0);
92 	if (!chip_cfg->comphy_base_addr)
93 		return -EINVAL;
94 
95 	chip_cfg->hpipe3_base_addr = devfdt_get_addr_index_ptr(dev, 1);
96 	if (!chip_cfg->hpipe3_base_addr)
97 		return -EINVAL;
98 
99 	if (device_is_compatible(dev, "marvell,comphy-a3700")) {
100 		chip_cfg->comphy_init_map = comphy_a3700_init_serdes_map;
101 		chip_cfg->ptr_comphy_chip_init = comphy_a3700_init;
102 		chip_cfg->rx_training = NULL;
103 	}
104 
105 	if (device_is_compatible(dev, "marvell,comphy-cp110")) {
106 		chip_cfg->comphy_init_map = comphy_cp110_init_serdes_map;
107 		chip_cfg->ptr_comphy_chip_init = comphy_cp110_init;
108 		chip_cfg->rx_training = comphy_cp110_sfi_rx_training;
109 	}
110 
111 	/*
112 	 * Bail out if no chip_init function is defined, e.g. no
113 	 * compatible node is found
114 	 */
115 	if (!chip_cfg->ptr_comphy_chip_init) {
116 		dev_err(dev, "comphy: No compatible DT node found\n");
117 		return -ENODEV;
118 	}
119 
120 	res = chip_cfg->comphy_init_map(node, chip_cfg);
121 	if (res < 0)
122 		return res;
123 
124 	/* Save CP index for MultiCP devices (A8K) */
125 	chip_cfg->cp_index = current_idx++;
126 	/* PHY power UP sequence */
127 	chip_cfg->ptr_comphy_chip_init(chip_cfg, chip_cfg->comphy_map_data);
128 	/* PHY print SerDes status */
129 	printf("Comphy chip #%d:\n", chip_cfg->cp_index);
130 	comphy_print(chip_cfg, chip_cfg->comphy_map_data);
131 
132 	/*
133 	 * Only run the dedicated PHY init code once, in the last PHY init call
134 	 */
135 	if (of_machine_is_compatible("marvell,armada8040"))
136 		last_idx = 1;
137 
138 	if (chip_cfg->cp_index == last_idx) {
139 		/* Initialize dedicated PHYs (not muxed SerDes lanes) */
140 		comphy_dedicated_phys_init();
141 	}
142 
143 	return 0;
144 }
145 
146 static const struct udevice_id comphy_ids[] = {
147 	{ .compatible = "marvell,mvebu-comphy" },
148 	{ .compatible = "marvell,comphy-a3700" },
149 	{ }
150 };
151 
152 U_BOOT_DRIVER(mvebu_comphy) = {
153 	.name	= "mvebu_comphy",
154 	.id	= UCLASS_MISC,
155 	.of_match = comphy_ids,
156 	.probe	= comphy_probe,
157 	.priv_auto	= sizeof(struct chip_serdes_phy_config),
158 };
159