Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / drivers / phy / marvell / comphy_core.c
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/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
23 static const char *get_speed_string(u32 speed)
24 {
25         static const char * const speed_strings[] = {
26                 "1.25 Gbps", "1.5 Gbps", "2.5 Gbps",
27                 "3.0 Gbps", "3.125 Gbps", "5 Gbps", "6 Gbps",
28                 "6.25 Gbps", "10.31 Gbps"
29         };
30
31         if (speed < 0 || speed > PHY_SPEED_MAX)
32                 return "invalid";
33
34         return speed_strings[speed];
35 }
36
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", "SATA2", "SATA3", "SGMII0",
42                 "SGMII1", "SGMII2", "SGMII3", "QSGMII",
43                 "USB3_HOST0", "USB3_HOST1", "USB3_DEVICE",
44                 "XAUI0", "XAUI1", "XAUI2", "XAUI3",
45                 "RXAUI0", "RXAUI1", "SFI", "IGNORE"
46         };
47
48         if (type < 0 || type > PHY_TYPE_MAX)
49                 return "invalid";
50
51         return type_strings[type];
52 }
53
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 == PHY_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
72 __weak int comphy_update_map(struct comphy_map *serdes_map, int count)
73 {
74         return 0;
75 }
76
77 static int comphy_probe(struct udevice *dev)
78 {
79         const void *blob = gd->fdt_blob;
80         int node = dev_of_offset(dev);
81         struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
82         struct comphy_map comphy_map_data[MAX_LANE_OPTIONS];
83         int subnode;
84         int lane;
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 = (void *)devfdt_get_addr_index(dev, 0);
91         if (IS_ERR(chip_cfg->comphy_base_addr))
92                 return PTR_ERR(chip_cfg->comphy_base_addr);
93
94         chip_cfg->hpipe3_base_addr = (void *)devfdt_get_addr_index(dev, 1);
95         if (IS_ERR(chip_cfg->hpipe3_base_addr))
96                 return PTR_ERR(chip_cfg->hpipe3_base_addr);
97
98         chip_cfg->comphy_lanes_count = fdtdec_get_int(blob, node,
99                                                       "max-lanes", 0);
100         if (chip_cfg->comphy_lanes_count <= 0) {
101                 dev_err(&dev->dev, "comphy max lanes is wrong\n");
102                 return -EINVAL;
103         }
104
105         chip_cfg->comphy_mux_bitcount = fdtdec_get_int(blob, node,
106                                                        "mux-bitcount", 0);
107         if (chip_cfg->comphy_mux_bitcount <= 0) {
108                 dev_err(&dev->dev, "comphy mux bit count is wrong\n");
109                 return -EINVAL;
110         }
111
112         chip_cfg->comphy_mux_lane_order =
113                 fdtdec_locate_array(blob, node, "mux-lane-order",
114                                     chip_cfg->comphy_lanes_count);
115
116         if (device_is_compatible(dev, "marvell,comphy-armada-3700"))
117                 chip_cfg->ptr_comphy_chip_init = comphy_a3700_init;
118
119         if (device_is_compatible(dev, "marvell,comphy-cp110"))
120                 chip_cfg->ptr_comphy_chip_init = comphy_cp110_init;
121
122         /*
123          * Bail out if no chip_init function is defined, e.g. no
124          * compatible node is found
125          */
126         if (!chip_cfg->ptr_comphy_chip_init) {
127                 dev_err(&dev->dev, "comphy: No compatible DT node found\n");
128                 return -ENODEV;
129         }
130
131         lane = 0;
132         fdt_for_each_subnode(subnode, blob, node) {
133                 /* Skip disabled ports */
134                 if (!fdtdec_get_is_enabled(blob, subnode))
135                         continue;
136
137                 comphy_map_data[lane].speed = fdtdec_get_int(
138                         blob, subnode, "phy-speed", PHY_TYPE_INVALID);
139                 comphy_map_data[lane].type = fdtdec_get_int(
140                         blob, subnode, "phy-type", PHY_SPEED_INVALID);
141                 comphy_map_data[lane].invert = fdtdec_get_int(
142                         blob, subnode, "phy-invert", PHY_POLARITY_NO_INVERT);
143                 comphy_map_data[lane].clk_src = fdtdec_get_bool(blob, subnode,
144                                                                 "clk-src");
145                 comphy_map_data[lane].end_point = fdtdec_get_bool(blob, subnode,
146                                                                   "end_point");
147                 if (comphy_map_data[lane].type == PHY_TYPE_INVALID) {
148                         printf("no phy type for lane %d, setting lane as unconnected\n",
149                                lane + 1);
150                 }
151
152                 lane++;
153         }
154
155         res = comphy_update_map(comphy_map_data, chip_cfg->comphy_lanes_count);
156         if (res < 0)
157                 return res;
158
159         /* Save CP index for MultiCP devices (A8K) */
160         chip_cfg->cp_index = current_idx++;
161         /* PHY power UP sequence */
162         chip_cfg->ptr_comphy_chip_init(chip_cfg, comphy_map_data);
163         /* PHY print SerDes status */
164         if (of_machine_is_compatible("marvell,armada8040"))
165                 printf("Comphy chip #%d:\n", chip_cfg->cp_index);
166         comphy_print(chip_cfg, comphy_map_data);
167
168         /*
169          * Only run the dedicated PHY init code once, in the last PHY init call
170          */
171         if (of_machine_is_compatible("marvell,armada8040"))
172                 last_idx = 1;
173
174         if (chip_cfg->cp_index == last_idx) {
175                 /* Initialize dedicated PHYs (not muxed SerDes lanes) */
176                 comphy_dedicated_phys_init();
177         }
178
179         return 0;
180 }
181
182 static const struct udevice_id comphy_ids[] = {
183         { .compatible = "marvell,mvebu-comphy" },
184         { }
185 };
186
187 U_BOOT_DRIVER(mvebu_comphy) = {
188         .name   = "mvebu_comphy",
189         .id     = UCLASS_MISC,
190         .of_match = comphy_ids,
191         .probe  = comphy_probe,
192         .priv_auto_alloc_size = sizeof(struct chip_serdes_phy_config),
193 };