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 <dm.h>
9 #include <fdtdec.h>
10 #include <asm/global_data.h>
11 #include <asm/io.h>
12 #include <dm/device_compat.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/libfdt.h>
16 
17 #include "comphy_core.h"
18 
19 #define COMPHY_MAX_CHIP 4
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
get_speed_string(u32 speed)23 static const char *get_speed_string(u32 speed)
24 {
25 	static const char * const speed_strings[] = {
26 		"1.25 Gbps", "2.5 Gbps", "3.125 Gbps",
27 		"5 Gbps", "5.125 Gpbs", "6 Gbps",
28 		"10.3125 Gbps"
29 	};
30 
31 	if (speed < 0 || speed >= COMPHY_SPEED_MAX)
32 		return "invalid";
33 
34 	return speed_strings[speed];
35 }
36 
get_type_string(u32 type)37 static const char *get_type_string(u32 type)
38 {
39 	static const char * const type_strings[] = {
40 		"UNCONNECTED", "PEX0", "PEX1", "PEX2", "PEX3",
41 		"SATA0", "SATA1", "SGMII0", "SGMII1", "SGMII2",
42 		"USB3", "USB3_HOST0", "USB3_HOST1",
43 		"USB3_DEVICE", "RXAUI0", "RXAUI1", "SFI0", "SFI1", "AP",
44 		"IGNORE"
45 	};
46 
47 	if (type < 0 || type >= COMPHY_TYPE_MAX)
48 		return "invalid";
49 
50 	return type_strings[type];
51 }
52 
comphy_print(struct chip_serdes_phy_config * chip_cfg,struct comphy_map * comphy_map_data)53 void comphy_print(struct chip_serdes_phy_config *chip_cfg,
54 		  struct comphy_map *comphy_map_data)
55 {
56 	u32 lane;
57 
58 	for (lane = 0; lane < chip_cfg->comphy_lanes_count;
59 	     lane++, comphy_map_data++) {
60 		if (comphy_map_data->speed == COMPHY_SPEED_INVALID) {
61 			printf("Comphy-%d: %-13s\n", lane,
62 			       get_type_string(comphy_map_data->type));
63 		} else {
64 			printf("Comphy-%d: %-13s %-10s\n", lane,
65 			       get_type_string(comphy_map_data->type),
66 			       get_speed_string(comphy_map_data->speed));
67 		}
68 	}
69 }
70 
comphy_rx_training(struct udevice * dev,u32 lane)71 int comphy_rx_training(struct udevice *dev, u32 lane)
72 {
73 	struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
74 
75 	if (chip_cfg->rx_training)
76 		return chip_cfg->rx_training(chip_cfg, lane);
77 
78 	return 0;
79 }
80 
comphy_probe(struct udevice * dev)81 static int comphy_probe(struct udevice *dev)
82 {
83 	int node = dev_of_offset(dev);
84 	struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
85 	int last_idx = 0;
86 	static int current_idx;
87 	int res;
88 
89 	/* Save base addresses for later use */
90 	chip_cfg->comphy_base_addr = devfdt_get_addr_index_ptr(dev, 0);
91 	if (!chip_cfg->comphy_base_addr)
92 		return -EINVAL;
93 
94 	chip_cfg->hpipe3_base_addr = devfdt_get_addr_index_ptr(dev, 1);
95 	if (!chip_cfg->hpipe3_base_addr)
96 		return -EINVAL;
97 
98 	if (device_is_compatible(dev, "marvell,comphy-a3700")) {
99 		chip_cfg->comphy_init_map = comphy_a3700_init_serdes_map;
100 		chip_cfg->ptr_comphy_chip_init = comphy_a3700_init;
101 		chip_cfg->rx_training = NULL;
102 	}
103 
104 	if (device_is_compatible(dev, "marvell,comphy-cp110")) {
105 		chip_cfg->comphy_init_map = comphy_cp110_init_serdes_map;
106 		chip_cfg->ptr_comphy_chip_init = comphy_cp110_init;
107 		chip_cfg->rx_training = comphy_cp110_sfi_rx_training;
108 	}
109 
110 	/*
111 	 * Bail out if no chip_init function is defined, e.g. no
112 	 * compatible node is found
113 	 */
114 	if (!chip_cfg->ptr_comphy_chip_init) {
115 		dev_err(dev, "comphy: No compatible DT node found\n");
116 		return -ENODEV;
117 	}
118 
119 	res = chip_cfg->comphy_init_map(node, chip_cfg);
120 	if (res < 0)
121 		return res;
122 
123 	/* Save CP index for MultiCP devices (A8K) */
124 	chip_cfg->cp_index = current_idx++;
125 	/* PHY power UP sequence */
126 	chip_cfg->ptr_comphy_chip_init(chip_cfg, chip_cfg->comphy_map_data);
127 	/* PHY print SerDes status */
128 	printf("Comphy chip #%d:\n", chip_cfg->cp_index);
129 	comphy_print(chip_cfg, chip_cfg->comphy_map_data);
130 
131 	/*
132 	 * Only run the dedicated PHY init code once, in the last PHY init call
133 	 */
134 	if (of_machine_is_compatible("marvell,armada8040"))
135 		last_idx = 1;
136 
137 	if (chip_cfg->cp_index == last_idx) {
138 		/* Initialize dedicated PHYs (not muxed SerDes lanes) */
139 		comphy_dedicated_phys_init();
140 	}
141 
142 	return 0;
143 }
144 
145 static const struct udevice_id comphy_ids[] = {
146 	{ .compatible = "marvell,mvebu-comphy" },
147 	{ .compatible = "marvell,comphy-a3700" },
148 	{ }
149 };
150 
151 U_BOOT_DRIVER(mvebu_comphy) = {
152 	.name	= "mvebu_comphy",
153 	.id	= UCLASS_MISC,
154 	.of_match = comphy_ids,
155 	.probe	= comphy_probe,
156 	.priv_auto	= sizeof(struct chip_serdes_phy_config),
157 };
158