1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Copyright (c) 2018 Synopsys, Inc. and/or its affiliates.
4 * stmmac HW Interface Handling
9 #include "stmmac_ptp.h"
11 static u32 stmmac_get_id(struct stmmac_priv *priv, u32 id_reg)
13 u32 reg = readl(priv->ioaddr + id_reg);
16 dev_info(priv->device, "Version ID not available\n");
20 dev_info(priv->device, "User ID: 0x%x, Synopsys ID: 0x%x\n",
21 (unsigned int)(reg & GENMASK(15, 8)) >> 8,
22 (unsigned int)(reg & GENMASK(7, 0)));
23 return reg & GENMASK(7, 0);
26 static u32 stmmac_get_dev_id(struct stmmac_priv *priv, u32 id_reg)
28 u32 reg = readl(priv->ioaddr + id_reg);
31 dev_info(priv->device, "Version ID not available\n");
35 return (reg & GENMASK(15, 8)) >> 8;
38 static void stmmac_dwmac_mode_quirk(struct stmmac_priv *priv)
40 struct mac_device_info *mac = priv->hw;
42 if (priv->chain_mode) {
43 dev_info(priv->device, "Chain mode enabled\n");
44 priv->mode = STMMAC_CHAIN_MODE;
45 mac->mode = &chain_mode_ops;
47 dev_info(priv->device, "Ring mode enabled\n");
48 priv->mode = STMMAC_RING_MODE;
49 mac->mode = &ring_mode_ops;
53 static int stmmac_dwmac1_quirks(struct stmmac_priv *priv)
55 struct mac_device_info *mac = priv->hw;
57 if (priv->plat->enh_desc) {
58 dev_info(priv->device, "Enhanced/Alternate descriptors\n");
60 /* GMAC older than 3.50 has no extended descriptors */
61 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
62 dev_info(priv->device, "Enabled extended descriptors\n");
63 priv->extend_desc = 1;
65 dev_warn(priv->device, "Extended descriptors not supported\n");
68 mac->desc = &enh_desc_ops;
70 dev_info(priv->device, "Normal descriptors\n");
71 mac->desc = &ndesc_ops;
74 stmmac_dwmac_mode_quirk(priv);
78 static int stmmac_dwmac4_quirks(struct stmmac_priv *priv)
80 stmmac_dwmac_mode_quirk(priv);
84 static int stmmac_dwxlgmac_quirks(struct stmmac_priv *priv)
86 priv->hw->xlgmac = true;
90 int stmmac_reset(struct stmmac_priv *priv, void __iomem *ioaddr)
92 struct plat_stmmacenet_data *plat = priv ? priv->plat : NULL;
97 if (plat && plat->fix_soc_reset)
98 return plat->fix_soc_reset(plat, ioaddr);
100 return stmmac_do_callback(priv, dma, reset, ioaddr);
103 static const struct stmmac_hwif_entry {
109 const struct stmmac_regs_off regs;
113 const void *hwtimestamp;
117 int (*setup)(struct stmmac_priv *priv);
118 int (*quirks)(struct stmmac_priv *priv);
120 /* NOTE: New HW versions shall go to the end of this table */
127 .ptp_off = PTP_GMAC3_X_OFFSET,
128 .mmc_off = MMC_GMAC3_X_OFFSET,
131 .dma = &dwmac100_dma_ops,
132 .mac = &dwmac100_ops,
133 .hwtimestamp = &stmmac_ptp,
136 .mmc = &dwmac_mmc_ops,
137 .setup = dwmac100_setup,
138 .quirks = stmmac_dwmac1_quirks,
145 .ptp_off = PTP_GMAC3_X_OFFSET,
146 .mmc_off = MMC_GMAC3_X_OFFSET,
149 .dma = &dwmac1000_dma_ops,
150 .mac = &dwmac1000_ops,
151 .hwtimestamp = &stmmac_ptp,
154 .mmc = &dwmac_mmc_ops,
155 .setup = dwmac1000_setup,
156 .quirks = stmmac_dwmac1_quirks,
163 .ptp_off = PTP_GMAC4_OFFSET,
164 .mmc_off = MMC_GMAC4_OFFSET,
166 .desc = &dwmac4_desc_ops,
167 .dma = &dwmac4_dma_ops,
169 .hwtimestamp = &stmmac_ptp,
171 .tc = &dwmac510_tc_ops,
172 .mmc = &dwmac_mmc_ops,
173 .setup = dwmac4_setup,
174 .quirks = stmmac_dwmac4_quirks,
179 .min_id = DWMAC_CORE_4_00,
181 .ptp_off = PTP_GMAC4_OFFSET,
182 .mmc_off = MMC_GMAC4_OFFSET,
184 .desc = &dwmac4_desc_ops,
185 .dma = &dwmac4_dma_ops,
186 .mac = &dwmac410_ops,
187 .hwtimestamp = &stmmac_ptp,
188 .mode = &dwmac4_ring_mode_ops,
189 .tc = &dwmac510_tc_ops,
190 .mmc = &dwmac_mmc_ops,
191 .setup = dwmac4_setup,
197 .min_id = DWMAC_CORE_4_10,
199 .ptp_off = PTP_GMAC4_OFFSET,
200 .mmc_off = MMC_GMAC4_OFFSET,
202 .desc = &dwmac4_desc_ops,
203 .dma = &dwmac410_dma_ops,
204 .mac = &dwmac410_ops,
205 .hwtimestamp = &stmmac_ptp,
206 .mode = &dwmac4_ring_mode_ops,
207 .tc = &dwmac510_tc_ops,
208 .mmc = &dwmac_mmc_ops,
209 .setup = dwmac4_setup,
215 .min_id = DWMAC_CORE_5_10,
217 .ptp_off = PTP_GMAC4_OFFSET,
218 .mmc_off = MMC_GMAC4_OFFSET,
220 .desc = &dwmac4_desc_ops,
221 .dma = &dwmac410_dma_ops,
222 .mac = &dwmac510_ops,
223 .hwtimestamp = &stmmac_ptp,
224 .mode = &dwmac4_ring_mode_ops,
225 .tc = &dwmac510_tc_ops,
226 .mmc = &dwmac_mmc_ops,
227 .setup = dwmac4_setup,
233 .min_id = DWXGMAC_CORE_2_10,
234 .dev_id = DWXGMAC_ID,
236 .ptp_off = PTP_XGMAC_OFFSET,
237 .mmc_off = MMC_XGMAC_OFFSET,
239 .desc = &dwxgmac210_desc_ops,
240 .dma = &dwxgmac210_dma_ops,
241 .mac = &dwxgmac210_ops,
242 .hwtimestamp = &stmmac_ptp,
244 .tc = &dwmac510_tc_ops,
245 .mmc = &dwxgmac_mmc_ops,
246 .setup = dwxgmac2_setup,
252 .min_id = DWXLGMAC_CORE_2_00,
253 .dev_id = DWXLGMAC_ID,
255 .ptp_off = PTP_XGMAC_OFFSET,
256 .mmc_off = MMC_XGMAC_OFFSET,
258 .desc = &dwxgmac210_desc_ops,
259 .dma = &dwxgmac210_dma_ops,
260 .mac = &dwxlgmac2_ops,
261 .hwtimestamp = &stmmac_ptp,
263 .tc = &dwmac510_tc_ops,
264 .mmc = &dwxgmac_mmc_ops,
265 .setup = dwxlgmac2_setup,
266 .quirks = stmmac_dwxlgmac_quirks,
270 int stmmac_hwif_init(struct stmmac_priv *priv)
272 bool needs_xgmac = priv->plat->has_xgmac;
273 bool needs_gmac4 = priv->plat->has_gmac4;
274 bool needs_gmac = priv->plat->has_gmac;
275 const struct stmmac_hwif_entry *entry;
276 struct mac_device_info *mac;
277 bool needs_setup = true;
282 id = stmmac_get_id(priv, GMAC_VERSION);
283 } else if (needs_gmac4 || needs_xgmac) {
284 id = stmmac_get_id(priv, GMAC4_VERSION);
286 dev_id = stmmac_get_dev_id(priv, GMAC4_VERSION);
291 /* Save ID for later use */
292 priv->synopsys_id = id;
294 /* Lets assume some safe values first */
295 priv->ptpaddr = priv->ioaddr +
296 (needs_gmac4 ? PTP_GMAC4_OFFSET : PTP_GMAC3_X_OFFSET);
297 priv->mmcaddr = priv->ioaddr +
298 (needs_gmac4 ? MMC_GMAC4_OFFSET : MMC_GMAC3_X_OFFSET);
300 /* Check for HW specific setup first */
301 if (priv->plat->setup) {
302 mac = priv->plat->setup(priv);
305 mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL);
311 /* Fallback to generic HW */
312 for (i = ARRAY_SIZE(stmmac_hw) - 1; i >= 0; i--) {
313 entry = &stmmac_hw[i];
315 if (needs_gmac ^ entry->gmac)
317 if (needs_gmac4 ^ entry->gmac4)
319 if (needs_xgmac ^ entry->xgmac)
321 /* Use synopsys_id var because some setups can override this */
322 if (priv->synopsys_id < entry->min_id)
324 if (needs_xgmac && (dev_id ^ entry->dev_id))
327 /* Only use generic HW helpers if needed */
328 mac->desc = mac->desc ? : entry->desc;
329 mac->dma = mac->dma ? : entry->dma;
330 mac->mac = mac->mac ? : entry->mac;
331 mac->ptp = mac->ptp ? : entry->hwtimestamp;
332 mac->mode = mac->mode ? : entry->mode;
333 mac->tc = mac->tc ? : entry->tc;
334 mac->mmc = mac->mmc ? : entry->mmc;
337 priv->ptpaddr = priv->ioaddr + entry->regs.ptp_off;
338 priv->mmcaddr = priv->ioaddr + entry->regs.mmc_off;
342 ret = entry->setup(priv);
347 /* Save quirks, if needed for posterior use */
348 priv->hwif_quirks = entry->quirks;
352 dev_err(priv->device, "Failed to find HW IF (id=0x%x, gmac=%d/%d)\n",
353 id, needs_gmac, needs_gmac4);