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