1 // SPDX-License-Identifier: GPL-2.0
2 /* Renesas Ethernet Switch device driver
3 *
4 * Copyright (C) 2022 Renesas Electronics Corporation
5 */
6
7 #include <linux/clk.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/err.h>
10 #include <linux/etherdevice.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/net_tstamp.h>
15 #include <linux/of.h>
16 #include <linux/of_mdio.h>
17 #include <linux/of_net.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/sys_soc.h>
25
26 #include "rswitch.h"
27
rswitch_reg_wait(void __iomem * addr,u32 offs,u32 mask,u32 expected)28 static int rswitch_reg_wait(void __iomem *addr, u32 offs, u32 mask, u32 expected)
29 {
30 u32 val;
31
32 return readl_poll_timeout_atomic(addr + offs, val, (val & mask) == expected,
33 1, RSWITCH_TIMEOUT_US);
34 }
35
rswitch_modify(void __iomem * addr,enum rswitch_reg reg,u32 clear,u32 set)36 static void rswitch_modify(void __iomem *addr, enum rswitch_reg reg, u32 clear, u32 set)
37 {
38 iowrite32((ioread32(addr + reg) & ~clear) | set, addr + reg);
39 }
40
41 /* Common Agent block (COMA) */
rswitch_reset(struct rswitch_private * priv)42 static void rswitch_reset(struct rswitch_private *priv)
43 {
44 iowrite32(RRC_RR, priv->addr + RRC);
45 iowrite32(RRC_RR_CLR, priv->addr + RRC);
46 }
47
rswitch_clock_enable(struct rswitch_private * priv)48 static void rswitch_clock_enable(struct rswitch_private *priv)
49 {
50 iowrite32(RCEC_ACE_DEFAULT | RCEC_RCE, priv->addr + RCEC);
51 }
52
rswitch_clock_disable(struct rswitch_private * priv)53 static void rswitch_clock_disable(struct rswitch_private *priv)
54 {
55 iowrite32(RCDC_RCD, priv->addr + RCDC);
56 }
57
rswitch_agent_clock_is_enabled(void __iomem * coma_addr,unsigned int port)58 static bool rswitch_agent_clock_is_enabled(void __iomem *coma_addr,
59 unsigned int port)
60 {
61 u32 val = ioread32(coma_addr + RCEC);
62
63 if (val & RCEC_RCE)
64 return (val & BIT(port)) ? true : false;
65 else
66 return false;
67 }
68
rswitch_agent_clock_ctrl(void __iomem * coma_addr,unsigned int port,int enable)69 static void rswitch_agent_clock_ctrl(void __iomem *coma_addr, unsigned int port,
70 int enable)
71 {
72 u32 val;
73
74 if (enable) {
75 val = ioread32(coma_addr + RCEC);
76 iowrite32(val | RCEC_RCE | BIT(port), coma_addr + RCEC);
77 } else {
78 val = ioread32(coma_addr + RCDC);
79 iowrite32(val | BIT(port), coma_addr + RCDC);
80 }
81 }
82
rswitch_bpool_config(struct rswitch_private * priv)83 static int rswitch_bpool_config(struct rswitch_private *priv)
84 {
85 u32 val;
86
87 val = ioread32(priv->addr + CABPIRM);
88 if (val & CABPIRM_BPR)
89 return 0;
90
91 iowrite32(CABPIRM_BPIOG, priv->addr + CABPIRM);
92
93 return rswitch_reg_wait(priv->addr, CABPIRM, CABPIRM_BPR, CABPIRM_BPR);
94 }
95
rswitch_coma_init(struct rswitch_private * priv)96 static void rswitch_coma_init(struct rswitch_private *priv)
97 {
98 iowrite32(CABPPFLC_INIT_VALUE, priv->addr + CABPPFLC0);
99 }
100
101 /* R-Switch-2 block (TOP) */
rswitch_top_init(struct rswitch_private * priv)102 static void rswitch_top_init(struct rswitch_private *priv)
103 {
104 unsigned int i;
105
106 for (i = 0; i < RSWITCH_MAX_NUM_QUEUES; i++)
107 iowrite32((i / 16) << (GWCA_INDEX * 8), priv->addr + TPEMIMC7(i));
108 }
109
110 /* Forwarding engine block (MFWD) */
rswitch_fwd_init(struct rswitch_private * priv)111 static void rswitch_fwd_init(struct rswitch_private *priv)
112 {
113 unsigned int i;
114
115 /* For ETHA */
116 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
117 iowrite32(FWPC0_DEFAULT, priv->addr + FWPC0(i));
118 iowrite32(0, priv->addr + FWPBFC(i));
119 }
120
121 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
122 iowrite32(priv->rdev[i]->rx_queue->index,
123 priv->addr + FWPBFCSDC(GWCA_INDEX, i));
124 iowrite32(BIT(priv->gwca.index), priv->addr + FWPBFC(i));
125 }
126
127 /* For GWCA */
128 iowrite32(FWPC0_DEFAULT, priv->addr + FWPC0(priv->gwca.index));
129 iowrite32(FWPC1_DDE, priv->addr + FWPC1(priv->gwca.index));
130 iowrite32(0, priv->addr + FWPBFC(priv->gwca.index));
131 iowrite32(GENMASK(RSWITCH_NUM_PORTS - 1, 0), priv->addr + FWPBFC(priv->gwca.index));
132 }
133
134 /* Gateway CPU agent block (GWCA) */
rswitch_gwca_change_mode(struct rswitch_private * priv,enum rswitch_gwca_mode mode)135 static int rswitch_gwca_change_mode(struct rswitch_private *priv,
136 enum rswitch_gwca_mode mode)
137 {
138 int ret;
139
140 if (!rswitch_agent_clock_is_enabled(priv->addr, priv->gwca.index))
141 rswitch_agent_clock_ctrl(priv->addr, priv->gwca.index, 1);
142
143 iowrite32(mode, priv->addr + GWMC);
144
145 ret = rswitch_reg_wait(priv->addr, GWMS, GWMS_OPS_MASK, mode);
146
147 if (mode == GWMC_OPC_DISABLE)
148 rswitch_agent_clock_ctrl(priv->addr, priv->gwca.index, 0);
149
150 return ret;
151 }
152
rswitch_gwca_mcast_table_reset(struct rswitch_private * priv)153 static int rswitch_gwca_mcast_table_reset(struct rswitch_private *priv)
154 {
155 iowrite32(GWMTIRM_MTIOG, priv->addr + GWMTIRM);
156
157 return rswitch_reg_wait(priv->addr, GWMTIRM, GWMTIRM_MTR, GWMTIRM_MTR);
158 }
159
rswitch_gwca_axi_ram_reset(struct rswitch_private * priv)160 static int rswitch_gwca_axi_ram_reset(struct rswitch_private *priv)
161 {
162 iowrite32(GWARIRM_ARIOG, priv->addr + GWARIRM);
163
164 return rswitch_reg_wait(priv->addr, GWARIRM, GWARIRM_ARR, GWARIRM_ARR);
165 }
166
rswitch_is_any_data_irq(struct rswitch_private * priv,u32 * dis,bool tx)167 static bool rswitch_is_any_data_irq(struct rswitch_private *priv, u32 *dis, bool tx)
168 {
169 u32 *mask = tx ? priv->gwca.tx_irq_bits : priv->gwca.rx_irq_bits;
170 unsigned int i;
171
172 for (i = 0; i < RSWITCH_NUM_IRQ_REGS; i++) {
173 if (dis[i] & mask[i])
174 return true;
175 }
176
177 return false;
178 }
179
rswitch_get_data_irq_status(struct rswitch_private * priv,u32 * dis)180 static void rswitch_get_data_irq_status(struct rswitch_private *priv, u32 *dis)
181 {
182 unsigned int i;
183
184 for (i = 0; i < RSWITCH_NUM_IRQ_REGS; i++) {
185 dis[i] = ioread32(priv->addr + GWDIS(i));
186 dis[i] &= ioread32(priv->addr + GWDIE(i));
187 }
188 }
189
rswitch_enadis_data_irq(struct rswitch_private * priv,unsigned int index,bool enable)190 static void rswitch_enadis_data_irq(struct rswitch_private *priv,
191 unsigned int index, bool enable)
192 {
193 u32 offs = enable ? GWDIE(index / 32) : GWDID(index / 32);
194
195 iowrite32(BIT(index % 32), priv->addr + offs);
196 }
197
rswitch_ack_data_irq(struct rswitch_private * priv,unsigned int index)198 static void rswitch_ack_data_irq(struct rswitch_private *priv,
199 unsigned int index)
200 {
201 u32 offs = GWDIS(index / 32);
202
203 iowrite32(BIT(index % 32), priv->addr + offs);
204 }
205
rswitch_next_queue_index(struct rswitch_gwca_queue * gq,bool cur,unsigned int num)206 static unsigned int rswitch_next_queue_index(struct rswitch_gwca_queue *gq,
207 bool cur, unsigned int num)
208 {
209 unsigned int index = cur ? gq->cur : gq->dirty;
210
211 if (index + num >= gq->ring_size)
212 index = (index + num) % gq->ring_size;
213 else
214 index += num;
215
216 return index;
217 }
218
rswitch_get_num_cur_queues(struct rswitch_gwca_queue * gq)219 static unsigned int rswitch_get_num_cur_queues(struct rswitch_gwca_queue *gq)
220 {
221 if (gq->cur >= gq->dirty)
222 return gq->cur - gq->dirty;
223 else
224 return gq->ring_size - gq->dirty + gq->cur;
225 }
226
rswitch_is_queue_rxed(struct rswitch_gwca_queue * gq)227 static bool rswitch_is_queue_rxed(struct rswitch_gwca_queue *gq)
228 {
229 struct rswitch_ext_ts_desc *desc = &gq->rx_ring[gq->dirty];
230
231 if ((desc->desc.die_dt & DT_MASK) != DT_FEMPTY)
232 return true;
233
234 return false;
235 }
236
rswitch_gwca_queue_alloc_rx_buf(struct rswitch_gwca_queue * gq,unsigned int start_index,unsigned int num)237 static int rswitch_gwca_queue_alloc_rx_buf(struct rswitch_gwca_queue *gq,
238 unsigned int start_index,
239 unsigned int num)
240 {
241 unsigned int i, index;
242
243 for (i = 0; i < num; i++) {
244 index = (i + start_index) % gq->ring_size;
245 if (gq->rx_bufs[index])
246 continue;
247 gq->rx_bufs[index] = netdev_alloc_frag(RSWITCH_BUF_SIZE);
248 if (!gq->rx_bufs[index])
249 goto err;
250 }
251
252 return 0;
253
254 err:
255 for (; i-- > 0; ) {
256 index = (i + start_index) % gq->ring_size;
257 skb_free_frag(gq->rx_bufs[index]);
258 gq->rx_bufs[index] = NULL;
259 }
260
261 return -ENOMEM;
262 }
263
rswitch_gwca_queue_free(struct net_device * ndev,struct rswitch_gwca_queue * gq)264 static void rswitch_gwca_queue_free(struct net_device *ndev,
265 struct rswitch_gwca_queue *gq)
266 {
267 unsigned int i;
268
269 if (!gq->dir_tx) {
270 dma_free_coherent(ndev->dev.parent,
271 sizeof(struct rswitch_ext_ts_desc) *
272 (gq->ring_size + 1), gq->rx_ring, gq->ring_dma);
273 gq->rx_ring = NULL;
274
275 for (i = 0; i < gq->ring_size; i++)
276 skb_free_frag(gq->rx_bufs[i]);
277 kfree(gq->rx_bufs);
278 gq->rx_bufs = NULL;
279 } else {
280 dma_free_coherent(ndev->dev.parent,
281 sizeof(struct rswitch_ext_desc) *
282 (gq->ring_size + 1), gq->tx_ring, gq->ring_dma);
283 gq->tx_ring = NULL;
284 kfree(gq->skbs);
285 gq->skbs = NULL;
286 kfree(gq->unmap_addrs);
287 gq->unmap_addrs = NULL;
288 }
289 }
290
rswitch_gwca_ts_queue_free(struct rswitch_private * priv)291 static void rswitch_gwca_ts_queue_free(struct rswitch_private *priv)
292 {
293 struct rswitch_gwca_queue *gq = &priv->gwca.ts_queue;
294
295 dma_free_coherent(&priv->pdev->dev,
296 sizeof(struct rswitch_ts_desc) * (gq->ring_size + 1),
297 gq->ts_ring, gq->ring_dma);
298 gq->ts_ring = NULL;
299 }
300
rswitch_gwca_queue_alloc(struct net_device * ndev,struct rswitch_private * priv,struct rswitch_gwca_queue * gq,bool dir_tx,unsigned int ring_size)301 static int rswitch_gwca_queue_alloc(struct net_device *ndev,
302 struct rswitch_private *priv,
303 struct rswitch_gwca_queue *gq,
304 bool dir_tx, unsigned int ring_size)
305 {
306 unsigned int i, bit;
307
308 gq->dir_tx = dir_tx;
309 gq->ring_size = ring_size;
310 gq->ndev = ndev;
311
312 if (!dir_tx) {
313 gq->rx_bufs = kcalloc(gq->ring_size, sizeof(*gq->rx_bufs), GFP_KERNEL);
314 if (!gq->rx_bufs)
315 return -ENOMEM;
316 if (rswitch_gwca_queue_alloc_rx_buf(gq, 0, gq->ring_size) < 0)
317 goto out;
318
319 gq->rx_ring = dma_alloc_coherent(ndev->dev.parent,
320 sizeof(struct rswitch_ext_ts_desc) *
321 (gq->ring_size + 1), &gq->ring_dma, GFP_KERNEL);
322 } else {
323 gq->skbs = kcalloc(gq->ring_size, sizeof(*gq->skbs), GFP_KERNEL);
324 if (!gq->skbs)
325 return -ENOMEM;
326 gq->unmap_addrs = kcalloc(gq->ring_size, sizeof(*gq->unmap_addrs), GFP_KERNEL);
327 if (!gq->unmap_addrs)
328 goto out;
329 gq->tx_ring = dma_alloc_coherent(ndev->dev.parent,
330 sizeof(struct rswitch_ext_desc) *
331 (gq->ring_size + 1), &gq->ring_dma, GFP_KERNEL);
332 }
333
334 if (!gq->rx_ring && !gq->tx_ring)
335 goto out;
336
337 i = gq->index / 32;
338 bit = BIT(gq->index % 32);
339 if (dir_tx)
340 priv->gwca.tx_irq_bits[i] |= bit;
341 else
342 priv->gwca.rx_irq_bits[i] |= bit;
343
344 return 0;
345
346 out:
347 rswitch_gwca_queue_free(ndev, gq);
348
349 return -ENOMEM;
350 }
351
rswitch_desc_set_dptr(struct rswitch_desc * desc,dma_addr_t addr)352 static void rswitch_desc_set_dptr(struct rswitch_desc *desc, dma_addr_t addr)
353 {
354 desc->dptrl = cpu_to_le32(lower_32_bits(addr));
355 desc->dptrh = upper_32_bits(addr) & 0xff;
356 }
357
rswitch_desc_get_dptr(const struct rswitch_desc * desc)358 static dma_addr_t rswitch_desc_get_dptr(const struct rswitch_desc *desc)
359 {
360 return __le32_to_cpu(desc->dptrl) | (u64)(desc->dptrh) << 32;
361 }
362
rswitch_gwca_queue_format(struct net_device * ndev,struct rswitch_private * priv,struct rswitch_gwca_queue * gq)363 static int rswitch_gwca_queue_format(struct net_device *ndev,
364 struct rswitch_private *priv,
365 struct rswitch_gwca_queue *gq)
366 {
367 unsigned int ring_size = sizeof(struct rswitch_ext_desc) * gq->ring_size;
368 struct rswitch_ext_desc *desc;
369 struct rswitch_desc *linkfix;
370 dma_addr_t dma_addr;
371 unsigned int i;
372
373 memset(gq->tx_ring, 0, ring_size);
374 for (i = 0, desc = gq->tx_ring; i < gq->ring_size; i++, desc++) {
375 if (!gq->dir_tx) {
376 dma_addr = dma_map_single(ndev->dev.parent,
377 gq->rx_bufs[i] + RSWITCH_HEADROOM,
378 RSWITCH_MAP_BUF_SIZE,
379 DMA_FROM_DEVICE);
380 if (dma_mapping_error(ndev->dev.parent, dma_addr))
381 goto err;
382
383 desc->desc.info_ds = cpu_to_le16(RSWITCH_DESC_BUF_SIZE);
384 rswitch_desc_set_dptr(&desc->desc, dma_addr);
385 desc->desc.die_dt = DT_FEMPTY | DIE;
386 } else {
387 desc->desc.die_dt = DT_EEMPTY | DIE;
388 }
389 }
390 rswitch_desc_set_dptr(&desc->desc, gq->ring_dma);
391 desc->desc.die_dt = DT_LINKFIX;
392
393 linkfix = &priv->gwca.linkfix_table[gq->index];
394 linkfix->die_dt = DT_LINKFIX;
395 rswitch_desc_set_dptr(linkfix, gq->ring_dma);
396
397 iowrite32(GWDCC_BALR | (gq->dir_tx ? GWDCC_DCP(GWCA_IPV_NUM) | GWDCC_DQT : 0) | GWDCC_EDE,
398 priv->addr + GWDCC_OFFS(gq->index));
399
400 return 0;
401
402 err:
403 if (!gq->dir_tx) {
404 for (desc = gq->tx_ring; i-- > 0; desc++) {
405 dma_addr = rswitch_desc_get_dptr(&desc->desc);
406 dma_unmap_single(ndev->dev.parent, dma_addr,
407 RSWITCH_MAP_BUF_SIZE, DMA_FROM_DEVICE);
408 }
409 }
410
411 return -ENOMEM;
412 }
413
rswitch_gwca_ts_queue_fill(struct rswitch_private * priv,unsigned int start_index,unsigned int num)414 static void rswitch_gwca_ts_queue_fill(struct rswitch_private *priv,
415 unsigned int start_index,
416 unsigned int num)
417 {
418 struct rswitch_gwca_queue *gq = &priv->gwca.ts_queue;
419 struct rswitch_ts_desc *desc;
420 unsigned int i, index;
421
422 for (i = 0; i < num; i++) {
423 index = (i + start_index) % gq->ring_size;
424 desc = &gq->ts_ring[index];
425 desc->desc.die_dt = DT_FEMPTY_ND | DIE;
426 }
427 }
428
rswitch_gwca_queue_ext_ts_fill(struct net_device * ndev,struct rswitch_gwca_queue * gq,unsigned int start_index,unsigned int num)429 static int rswitch_gwca_queue_ext_ts_fill(struct net_device *ndev,
430 struct rswitch_gwca_queue *gq,
431 unsigned int start_index,
432 unsigned int num)
433 {
434 struct rswitch_device *rdev = netdev_priv(ndev);
435 struct rswitch_ext_ts_desc *desc;
436 unsigned int i, index;
437 dma_addr_t dma_addr;
438
439 for (i = 0; i < num; i++) {
440 index = (i + start_index) % gq->ring_size;
441 desc = &gq->rx_ring[index];
442 if (!gq->dir_tx) {
443 dma_addr = dma_map_single(ndev->dev.parent,
444 gq->rx_bufs[index] + RSWITCH_HEADROOM,
445 RSWITCH_MAP_BUF_SIZE,
446 DMA_FROM_DEVICE);
447 if (dma_mapping_error(ndev->dev.parent, dma_addr))
448 goto err;
449
450 desc->desc.info_ds = cpu_to_le16(RSWITCH_DESC_BUF_SIZE);
451 rswitch_desc_set_dptr(&desc->desc, dma_addr);
452 dma_wmb();
453 desc->desc.die_dt = DT_FEMPTY | DIE;
454 desc->info1 = cpu_to_le64(INFO1_SPN(rdev->etha->index));
455 } else {
456 desc->desc.die_dt = DT_EEMPTY | DIE;
457 }
458 }
459
460 return 0;
461
462 err:
463 if (!gq->dir_tx) {
464 for (; i-- > 0; ) {
465 index = (i + start_index) % gq->ring_size;
466 desc = &gq->rx_ring[index];
467 dma_addr = rswitch_desc_get_dptr(&desc->desc);
468 dma_unmap_single(ndev->dev.parent, dma_addr,
469 RSWITCH_MAP_BUF_SIZE, DMA_FROM_DEVICE);
470 }
471 }
472
473 return -ENOMEM;
474 }
475
rswitch_gwca_queue_ext_ts_format(struct net_device * ndev,struct rswitch_private * priv,struct rswitch_gwca_queue * gq)476 static int rswitch_gwca_queue_ext_ts_format(struct net_device *ndev,
477 struct rswitch_private *priv,
478 struct rswitch_gwca_queue *gq)
479 {
480 unsigned int ring_size = sizeof(struct rswitch_ext_ts_desc) * gq->ring_size;
481 struct rswitch_ext_ts_desc *desc;
482 struct rswitch_desc *linkfix;
483 int err;
484
485 memset(gq->rx_ring, 0, ring_size);
486 err = rswitch_gwca_queue_ext_ts_fill(ndev, gq, 0, gq->ring_size);
487 if (err < 0)
488 return err;
489
490 desc = &gq->rx_ring[gq->ring_size]; /* Last */
491 rswitch_desc_set_dptr(&desc->desc, gq->ring_dma);
492 desc->desc.die_dt = DT_LINKFIX;
493
494 linkfix = &priv->gwca.linkfix_table[gq->index];
495 linkfix->die_dt = DT_LINKFIX;
496 rswitch_desc_set_dptr(linkfix, gq->ring_dma);
497
498 iowrite32(GWDCC_BALR | (gq->dir_tx ? GWDCC_DCP(GWCA_IPV_NUM) | GWDCC_DQT : 0) |
499 GWDCC_ETS | GWDCC_EDE,
500 priv->addr + GWDCC_OFFS(gq->index));
501
502 return 0;
503 }
504
rswitch_gwca_linkfix_alloc(struct rswitch_private * priv)505 static int rswitch_gwca_linkfix_alloc(struct rswitch_private *priv)
506 {
507 unsigned int i, num_queues = priv->gwca.num_queues;
508 struct rswitch_gwca *gwca = &priv->gwca;
509 struct device *dev = &priv->pdev->dev;
510
511 gwca->linkfix_table_size = sizeof(struct rswitch_desc) * num_queues;
512 gwca->linkfix_table = dma_alloc_coherent(dev, gwca->linkfix_table_size,
513 &gwca->linkfix_table_dma, GFP_KERNEL);
514 if (!gwca->linkfix_table)
515 return -ENOMEM;
516 for (i = 0; i < num_queues; i++)
517 gwca->linkfix_table[i].die_dt = DT_EOS;
518
519 return 0;
520 }
521
rswitch_gwca_linkfix_free(struct rswitch_private * priv)522 static void rswitch_gwca_linkfix_free(struct rswitch_private *priv)
523 {
524 struct rswitch_gwca *gwca = &priv->gwca;
525
526 if (gwca->linkfix_table)
527 dma_free_coherent(&priv->pdev->dev, gwca->linkfix_table_size,
528 gwca->linkfix_table, gwca->linkfix_table_dma);
529 gwca->linkfix_table = NULL;
530 }
531
rswitch_gwca_ts_queue_alloc(struct rswitch_private * priv)532 static int rswitch_gwca_ts_queue_alloc(struct rswitch_private *priv)
533 {
534 struct rswitch_gwca_queue *gq = &priv->gwca.ts_queue;
535 struct rswitch_ts_desc *desc;
536
537 gq->ring_size = TS_RING_SIZE;
538 gq->ts_ring = dma_alloc_coherent(&priv->pdev->dev,
539 sizeof(struct rswitch_ts_desc) *
540 (gq->ring_size + 1), &gq->ring_dma, GFP_KERNEL);
541
542 if (!gq->ts_ring)
543 return -ENOMEM;
544
545 rswitch_gwca_ts_queue_fill(priv, 0, TS_RING_SIZE);
546 desc = &gq->ts_ring[gq->ring_size];
547 desc->desc.die_dt = DT_LINKFIX;
548 rswitch_desc_set_dptr(&desc->desc, gq->ring_dma);
549
550 return 0;
551 }
552
rswitch_gwca_get(struct rswitch_private * priv)553 static struct rswitch_gwca_queue *rswitch_gwca_get(struct rswitch_private *priv)
554 {
555 struct rswitch_gwca_queue *gq;
556 unsigned int index;
557
558 index = find_first_zero_bit(priv->gwca.used, priv->gwca.num_queues);
559 if (index >= priv->gwca.num_queues)
560 return NULL;
561 set_bit(index, priv->gwca.used);
562 gq = &priv->gwca.queues[index];
563 memset(gq, 0, sizeof(*gq));
564 gq->index = index;
565
566 return gq;
567 }
568
rswitch_gwca_put(struct rswitch_private * priv,struct rswitch_gwca_queue * gq)569 static void rswitch_gwca_put(struct rswitch_private *priv,
570 struct rswitch_gwca_queue *gq)
571 {
572 clear_bit(gq->index, priv->gwca.used);
573 }
574
rswitch_txdmac_alloc(struct net_device * ndev)575 static int rswitch_txdmac_alloc(struct net_device *ndev)
576 {
577 struct rswitch_device *rdev = netdev_priv(ndev);
578 struct rswitch_private *priv = rdev->priv;
579 int err;
580
581 rdev->tx_queue = rswitch_gwca_get(priv);
582 if (!rdev->tx_queue)
583 return -EBUSY;
584
585 err = rswitch_gwca_queue_alloc(ndev, priv, rdev->tx_queue, true, TX_RING_SIZE);
586 if (err < 0) {
587 rswitch_gwca_put(priv, rdev->tx_queue);
588 return err;
589 }
590
591 return 0;
592 }
593
rswitch_txdmac_free(struct net_device * ndev)594 static void rswitch_txdmac_free(struct net_device *ndev)
595 {
596 struct rswitch_device *rdev = netdev_priv(ndev);
597
598 rswitch_gwca_queue_free(ndev, rdev->tx_queue);
599 rswitch_gwca_put(rdev->priv, rdev->tx_queue);
600 }
601
rswitch_txdmac_init(struct rswitch_private * priv,unsigned int index)602 static int rswitch_txdmac_init(struct rswitch_private *priv, unsigned int index)
603 {
604 struct rswitch_device *rdev = priv->rdev[index];
605
606 return rswitch_gwca_queue_format(rdev->ndev, priv, rdev->tx_queue);
607 }
608
rswitch_rxdmac_alloc(struct net_device * ndev)609 static int rswitch_rxdmac_alloc(struct net_device *ndev)
610 {
611 struct rswitch_device *rdev = netdev_priv(ndev);
612 struct rswitch_private *priv = rdev->priv;
613 int err;
614
615 rdev->rx_queue = rswitch_gwca_get(priv);
616 if (!rdev->rx_queue)
617 return -EBUSY;
618
619 err = rswitch_gwca_queue_alloc(ndev, priv, rdev->rx_queue, false, RX_RING_SIZE);
620 if (err < 0) {
621 rswitch_gwca_put(priv, rdev->rx_queue);
622 return err;
623 }
624
625 return 0;
626 }
627
rswitch_rxdmac_free(struct net_device * ndev)628 static void rswitch_rxdmac_free(struct net_device *ndev)
629 {
630 struct rswitch_device *rdev = netdev_priv(ndev);
631
632 rswitch_gwca_queue_free(ndev, rdev->rx_queue);
633 rswitch_gwca_put(rdev->priv, rdev->rx_queue);
634 }
635
rswitch_rxdmac_init(struct rswitch_private * priv,unsigned int index)636 static int rswitch_rxdmac_init(struct rswitch_private *priv, unsigned int index)
637 {
638 struct rswitch_device *rdev = priv->rdev[index];
639 struct net_device *ndev = rdev->ndev;
640
641 return rswitch_gwca_queue_ext_ts_format(ndev, priv, rdev->rx_queue);
642 }
643
rswitch_gwca_hw_init(struct rswitch_private * priv)644 static int rswitch_gwca_hw_init(struct rswitch_private *priv)
645 {
646 unsigned int i;
647 int err;
648
649 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE);
650 if (err < 0)
651 return err;
652 err = rswitch_gwca_change_mode(priv, GWMC_OPC_CONFIG);
653 if (err < 0)
654 return err;
655
656 err = rswitch_gwca_mcast_table_reset(priv);
657 if (err < 0)
658 return err;
659 err = rswitch_gwca_axi_ram_reset(priv);
660 if (err < 0)
661 return err;
662
663 iowrite32(GWVCC_VEM_SC_TAG, priv->addr + GWVCC);
664 iowrite32(0, priv->addr + GWTTFC);
665 iowrite32(lower_32_bits(priv->gwca.linkfix_table_dma), priv->addr + GWDCBAC1);
666 iowrite32(upper_32_bits(priv->gwca.linkfix_table_dma), priv->addr + GWDCBAC0);
667 iowrite32(lower_32_bits(priv->gwca.ts_queue.ring_dma), priv->addr + GWTDCAC10);
668 iowrite32(upper_32_bits(priv->gwca.ts_queue.ring_dma), priv->addr + GWTDCAC00);
669 iowrite32(GWCA_TS_IRQ_BIT, priv->addr + GWTSDCC0);
670
671 iowrite32(GWTPC_PPPL(GWCA_IPV_NUM), priv->addr + GWTPC0);
672
673 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
674 err = rswitch_rxdmac_init(priv, i);
675 if (err < 0)
676 return err;
677 err = rswitch_txdmac_init(priv, i);
678 if (err < 0)
679 return err;
680 }
681
682 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE);
683 if (err < 0)
684 return err;
685 return rswitch_gwca_change_mode(priv, GWMC_OPC_OPERATION);
686 }
687
rswitch_gwca_hw_deinit(struct rswitch_private * priv)688 static int rswitch_gwca_hw_deinit(struct rswitch_private *priv)
689 {
690 int err;
691
692 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE);
693 if (err < 0)
694 return err;
695 err = rswitch_gwca_change_mode(priv, GWMC_OPC_RESET);
696 if (err < 0)
697 return err;
698
699 return rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE);
700 }
701
rswitch_gwca_halt(struct rswitch_private * priv)702 static int rswitch_gwca_halt(struct rswitch_private *priv)
703 {
704 int err;
705
706 priv->gwca_halt = true;
707 err = rswitch_gwca_hw_deinit(priv);
708 dev_err(&priv->pdev->dev, "halted (%d)\n", err);
709
710 return err;
711 }
712
rswitch_rx(struct net_device * ndev,int * quota)713 static bool rswitch_rx(struct net_device *ndev, int *quota)
714 {
715 struct rswitch_device *rdev = netdev_priv(ndev);
716 struct rswitch_gwca_queue *gq = rdev->rx_queue;
717 struct rswitch_ext_ts_desc *desc;
718 int limit, boguscnt, ret;
719 struct sk_buff *skb;
720 dma_addr_t dma_addr;
721 unsigned int num;
722 u16 pkt_len;
723 u32 get_ts;
724
725 if (*quota <= 0)
726 return true;
727
728 boguscnt = min_t(int, gq->ring_size, *quota);
729 limit = boguscnt;
730
731 desc = &gq->rx_ring[gq->cur];
732 while ((desc->desc.die_dt & DT_MASK) != DT_FEMPTY) {
733 dma_rmb();
734 pkt_len = le16_to_cpu(desc->desc.info_ds) & RX_DS;
735 dma_addr = rswitch_desc_get_dptr(&desc->desc);
736 dma_unmap_single(ndev->dev.parent, dma_addr,
737 RSWITCH_MAP_BUF_SIZE, DMA_FROM_DEVICE);
738 skb = build_skb(gq->rx_bufs[gq->cur], RSWITCH_BUF_SIZE);
739 if (!skb)
740 goto out;
741 skb_reserve(skb, RSWITCH_HEADROOM);
742 skb_put(skb, pkt_len);
743
744 get_ts = rdev->priv->ptp_priv->tstamp_rx_ctrl & RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT;
745 if (get_ts) {
746 struct skb_shared_hwtstamps *shhwtstamps;
747 struct timespec64 ts;
748
749 shhwtstamps = skb_hwtstamps(skb);
750 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
751 ts.tv_sec = __le32_to_cpu(desc->ts_sec);
752 ts.tv_nsec = __le32_to_cpu(desc->ts_nsec & cpu_to_le32(0x3fffffff));
753 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
754 }
755 skb->protocol = eth_type_trans(skb, ndev);
756 napi_gro_receive(&rdev->napi, skb);
757 rdev->ndev->stats.rx_packets++;
758 rdev->ndev->stats.rx_bytes += pkt_len;
759
760 out:
761 gq->rx_bufs[gq->cur] = NULL;
762 gq->cur = rswitch_next_queue_index(gq, true, 1);
763 desc = &gq->rx_ring[gq->cur];
764
765 if (--boguscnt <= 0)
766 break;
767 }
768
769 num = rswitch_get_num_cur_queues(gq);
770 ret = rswitch_gwca_queue_alloc_rx_buf(gq, gq->dirty, num);
771 if (ret < 0)
772 goto err;
773 ret = rswitch_gwca_queue_ext_ts_fill(ndev, gq, gq->dirty, num);
774 if (ret < 0)
775 goto err;
776 gq->dirty = rswitch_next_queue_index(gq, false, num);
777
778 *quota -= limit - boguscnt;
779
780 return boguscnt <= 0;
781
782 err:
783 rswitch_gwca_halt(rdev->priv);
784
785 return 0;
786 }
787
rswitch_tx_free(struct net_device * ndev)788 static void rswitch_tx_free(struct net_device *ndev)
789 {
790 struct rswitch_device *rdev = netdev_priv(ndev);
791 struct rswitch_gwca_queue *gq = rdev->tx_queue;
792 struct rswitch_ext_desc *desc;
793 struct sk_buff *skb;
794
795 desc = &gq->tx_ring[gq->dirty];
796 while ((desc->desc.die_dt & DT_MASK) == DT_FEMPTY) {
797 dma_rmb();
798
799 skb = gq->skbs[gq->dirty];
800 if (skb) {
801 rdev->ndev->stats.tx_packets++;
802 rdev->ndev->stats.tx_bytes += skb->len;
803 dma_unmap_single(ndev->dev.parent,
804 gq->unmap_addrs[gq->dirty],
805 skb->len, DMA_TO_DEVICE);
806 dev_kfree_skb_any(gq->skbs[gq->dirty]);
807 gq->skbs[gq->dirty] = NULL;
808 }
809
810 desc->desc.die_dt = DT_EEMPTY;
811 gq->dirty = rswitch_next_queue_index(gq, false, 1);
812 desc = &gq->tx_ring[gq->dirty];
813 }
814 }
815
rswitch_poll(struct napi_struct * napi,int budget)816 static int rswitch_poll(struct napi_struct *napi, int budget)
817 {
818 struct net_device *ndev = napi->dev;
819 struct rswitch_private *priv;
820 struct rswitch_device *rdev;
821 unsigned long flags;
822 int quota = budget;
823
824 rdev = netdev_priv(ndev);
825 priv = rdev->priv;
826
827 retry:
828 rswitch_tx_free(ndev);
829
830 if (rswitch_rx(ndev, "a))
831 goto out;
832 else if (rdev->priv->gwca_halt)
833 goto err;
834 else if (rswitch_is_queue_rxed(rdev->rx_queue))
835 goto retry;
836
837 netif_wake_subqueue(ndev, 0);
838
839 if (napi_complete_done(napi, budget - quota)) {
840 spin_lock_irqsave(&priv->lock, flags);
841 if (test_bit(rdev->port, priv->opened_ports)) {
842 rswitch_enadis_data_irq(priv, rdev->tx_queue->index, true);
843 rswitch_enadis_data_irq(priv, rdev->rx_queue->index, true);
844 }
845 spin_unlock_irqrestore(&priv->lock, flags);
846 }
847
848 out:
849 return budget - quota;
850
851 err:
852 napi_complete(napi);
853
854 return 0;
855 }
856
rswitch_queue_interrupt(struct net_device * ndev)857 static void rswitch_queue_interrupt(struct net_device *ndev)
858 {
859 struct rswitch_device *rdev = netdev_priv(ndev);
860
861 if (napi_schedule_prep(&rdev->napi)) {
862 spin_lock(&rdev->priv->lock);
863 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, false);
864 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, false);
865 spin_unlock(&rdev->priv->lock);
866 __napi_schedule(&rdev->napi);
867 }
868 }
869
rswitch_data_irq(struct rswitch_private * priv,u32 * dis)870 static irqreturn_t rswitch_data_irq(struct rswitch_private *priv, u32 *dis)
871 {
872 struct rswitch_gwca_queue *gq;
873 unsigned int i, index, bit;
874
875 for (i = 0; i < priv->gwca.num_queues; i++) {
876 gq = &priv->gwca.queues[i];
877 index = gq->index / 32;
878 bit = BIT(gq->index % 32);
879 if (!(dis[index] & bit))
880 continue;
881
882 rswitch_ack_data_irq(priv, gq->index);
883 rswitch_queue_interrupt(gq->ndev);
884 }
885
886 return IRQ_HANDLED;
887 }
888
rswitch_gwca_irq(int irq,void * dev_id)889 static irqreturn_t rswitch_gwca_irq(int irq, void *dev_id)
890 {
891 struct rswitch_private *priv = dev_id;
892 u32 dis[RSWITCH_NUM_IRQ_REGS];
893 irqreturn_t ret = IRQ_NONE;
894
895 rswitch_get_data_irq_status(priv, dis);
896
897 if (rswitch_is_any_data_irq(priv, dis, true) ||
898 rswitch_is_any_data_irq(priv, dis, false))
899 ret = rswitch_data_irq(priv, dis);
900
901 return ret;
902 }
903
rswitch_gwca_request_irqs(struct rswitch_private * priv)904 static int rswitch_gwca_request_irqs(struct rswitch_private *priv)
905 {
906 char *resource_name, *irq_name;
907 int i, ret, irq;
908
909 for (i = 0; i < GWCA_NUM_IRQS; i++) {
910 resource_name = kasprintf(GFP_KERNEL, GWCA_IRQ_RESOURCE_NAME, i);
911 if (!resource_name)
912 return -ENOMEM;
913
914 irq = platform_get_irq_byname(priv->pdev, resource_name);
915 kfree(resource_name);
916 if (irq < 0)
917 return irq;
918
919 irq_name = devm_kasprintf(&priv->pdev->dev, GFP_KERNEL,
920 GWCA_IRQ_NAME, i);
921 if (!irq_name)
922 return -ENOMEM;
923
924 ret = devm_request_irq(&priv->pdev->dev, irq, rswitch_gwca_irq,
925 0, irq_name, priv);
926 if (ret < 0)
927 return ret;
928 }
929
930 return 0;
931 }
932
rswitch_ts(struct rswitch_private * priv)933 static void rswitch_ts(struct rswitch_private *priv)
934 {
935 struct rswitch_gwca_queue *gq = &priv->gwca.ts_queue;
936 struct skb_shared_hwtstamps shhwtstamps;
937 struct rswitch_ts_desc *desc;
938 struct rswitch_device *rdev;
939 struct sk_buff *ts_skb;
940 struct timespec64 ts;
941 unsigned int num;
942 u32 tag, port;
943
944 desc = &gq->ts_ring[gq->cur];
945 while ((desc->desc.die_dt & DT_MASK) != DT_FEMPTY_ND) {
946 dma_rmb();
947
948 port = TS_DESC_DPN(__le32_to_cpu(desc->desc.dptrl));
949 if (unlikely(port >= RSWITCH_NUM_PORTS))
950 goto next;
951 rdev = priv->rdev[port];
952
953 tag = TS_DESC_TSUN(__le32_to_cpu(desc->desc.dptrl));
954 if (unlikely(tag >= TS_TAGS_PER_PORT))
955 goto next;
956 ts_skb = xchg(&rdev->ts_skb[tag], NULL);
957 smp_mb(); /* order rdev->ts_skb[] read before bitmap update */
958 clear_bit(tag, rdev->ts_skb_used);
959
960 if (unlikely(!ts_skb))
961 goto next;
962
963 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
964 ts.tv_sec = __le32_to_cpu(desc->ts_sec);
965 ts.tv_nsec = __le32_to_cpu(desc->ts_nsec & cpu_to_le32(0x3fffffff));
966 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
967 skb_tstamp_tx(ts_skb, &shhwtstamps);
968 dev_consume_skb_irq(ts_skb);
969
970 next:
971 gq->cur = rswitch_next_queue_index(gq, true, 1);
972 desc = &gq->ts_ring[gq->cur];
973 }
974
975 num = rswitch_get_num_cur_queues(gq);
976 rswitch_gwca_ts_queue_fill(priv, gq->dirty, num);
977 gq->dirty = rswitch_next_queue_index(gq, false, num);
978 }
979
rswitch_gwca_ts_irq(int irq,void * dev_id)980 static irqreturn_t rswitch_gwca_ts_irq(int irq, void *dev_id)
981 {
982 struct rswitch_private *priv = dev_id;
983
984 if (ioread32(priv->addr + GWTSDIS) & GWCA_TS_IRQ_BIT) {
985 iowrite32(GWCA_TS_IRQ_BIT, priv->addr + GWTSDIS);
986 rswitch_ts(priv);
987
988 return IRQ_HANDLED;
989 }
990
991 return IRQ_NONE;
992 }
993
rswitch_gwca_ts_request_irqs(struct rswitch_private * priv)994 static int rswitch_gwca_ts_request_irqs(struct rswitch_private *priv)
995 {
996 int irq;
997
998 irq = platform_get_irq_byname(priv->pdev, GWCA_TS_IRQ_RESOURCE_NAME);
999 if (irq < 0)
1000 return irq;
1001
1002 return devm_request_irq(&priv->pdev->dev, irq, rswitch_gwca_ts_irq,
1003 0, GWCA_TS_IRQ_NAME, priv);
1004 }
1005
1006 /* Ethernet TSN Agent block (ETHA) and Ethernet MAC IP block (RMAC) */
rswitch_etha_change_mode(struct rswitch_etha * etha,enum rswitch_etha_mode mode)1007 static int rswitch_etha_change_mode(struct rswitch_etha *etha,
1008 enum rswitch_etha_mode mode)
1009 {
1010 int ret;
1011
1012 if (!rswitch_agent_clock_is_enabled(etha->coma_addr, etha->index))
1013 rswitch_agent_clock_ctrl(etha->coma_addr, etha->index, 1);
1014
1015 iowrite32(mode, etha->addr + EAMC);
1016
1017 ret = rswitch_reg_wait(etha->addr, EAMS, EAMS_OPS_MASK, mode);
1018
1019 if (mode == EAMC_OPC_DISABLE)
1020 rswitch_agent_clock_ctrl(etha->coma_addr, etha->index, 0);
1021
1022 return ret;
1023 }
1024
rswitch_etha_read_mac_address(struct rswitch_etha * etha)1025 static void rswitch_etha_read_mac_address(struct rswitch_etha *etha)
1026 {
1027 u32 mrmac0 = ioread32(etha->addr + MRMAC0);
1028 u32 mrmac1 = ioread32(etha->addr + MRMAC1);
1029 u8 *mac = ða->mac_addr[0];
1030
1031 mac[0] = (mrmac0 >> 8) & 0xFF;
1032 mac[1] = (mrmac0 >> 0) & 0xFF;
1033 mac[2] = (mrmac1 >> 24) & 0xFF;
1034 mac[3] = (mrmac1 >> 16) & 0xFF;
1035 mac[4] = (mrmac1 >> 8) & 0xFF;
1036 mac[5] = (mrmac1 >> 0) & 0xFF;
1037 }
1038
rswitch_etha_write_mac_address(struct rswitch_etha * etha,const u8 * mac)1039 static void rswitch_etha_write_mac_address(struct rswitch_etha *etha, const u8 *mac)
1040 {
1041 iowrite32((mac[0] << 8) | mac[1], etha->addr + MRMAC0);
1042 iowrite32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
1043 etha->addr + MRMAC1);
1044 }
1045
rswitch_etha_wait_link_verification(struct rswitch_etha * etha)1046 static int rswitch_etha_wait_link_verification(struct rswitch_etha *etha)
1047 {
1048 iowrite32(MLVC_PLV, etha->addr + MLVC);
1049
1050 return rswitch_reg_wait(etha->addr, MLVC, MLVC_PLV, 0);
1051 }
1052
rswitch_rmac_setting(struct rswitch_etha * etha,const u8 * mac)1053 static void rswitch_rmac_setting(struct rswitch_etha *etha, const u8 *mac)
1054 {
1055 u32 pis, lsc;
1056
1057 rswitch_etha_write_mac_address(etha, mac);
1058
1059 switch (etha->phy_interface) {
1060 case PHY_INTERFACE_MODE_SGMII:
1061 pis = MPIC_PIS_GMII;
1062 break;
1063 case PHY_INTERFACE_MODE_USXGMII:
1064 case PHY_INTERFACE_MODE_5GBASER:
1065 pis = MPIC_PIS_XGMII;
1066 break;
1067 default:
1068 pis = FIELD_GET(MPIC_PIS, ioread32(etha->addr + MPIC));
1069 break;
1070 }
1071
1072 switch (etha->speed) {
1073 case 100:
1074 lsc = MPIC_LSC_100M;
1075 break;
1076 case 1000:
1077 lsc = MPIC_LSC_1G;
1078 break;
1079 case 2500:
1080 lsc = MPIC_LSC_2_5G;
1081 break;
1082 default:
1083 lsc = FIELD_GET(MPIC_LSC, ioread32(etha->addr + MPIC));
1084 break;
1085 }
1086
1087 rswitch_modify(etha->addr, MPIC, MPIC_PIS | MPIC_LSC,
1088 FIELD_PREP(MPIC_PIS, pis) | FIELD_PREP(MPIC_LSC, lsc));
1089 }
1090
rswitch_etha_enable_mii(struct rswitch_etha * etha)1091 static void rswitch_etha_enable_mii(struct rswitch_etha *etha)
1092 {
1093 rswitch_modify(etha->addr, MPIC, MPIC_PSMCS_MASK | MPIC_PSMHT_MASK,
1094 MPIC_PSMCS(etha->psmcs) | MPIC_PSMHT(0x06));
1095 rswitch_modify(etha->addr, MPSM, 0, MPSM_MFF_C45);
1096 }
1097
rswitch_etha_hw_init(struct rswitch_etha * etha,const u8 * mac)1098 static int rswitch_etha_hw_init(struct rswitch_etha *etha, const u8 *mac)
1099 {
1100 int err;
1101
1102 err = rswitch_etha_change_mode(etha, EAMC_OPC_DISABLE);
1103 if (err < 0)
1104 return err;
1105 err = rswitch_etha_change_mode(etha, EAMC_OPC_CONFIG);
1106 if (err < 0)
1107 return err;
1108
1109 iowrite32(EAVCC_VEM_SC_TAG, etha->addr + EAVCC);
1110 rswitch_rmac_setting(etha, mac);
1111 rswitch_etha_enable_mii(etha);
1112
1113 err = rswitch_etha_wait_link_verification(etha);
1114 if (err < 0)
1115 return err;
1116
1117 err = rswitch_etha_change_mode(etha, EAMC_OPC_DISABLE);
1118 if (err < 0)
1119 return err;
1120
1121 return rswitch_etha_change_mode(etha, EAMC_OPC_OPERATION);
1122 }
1123
rswitch_etha_set_access(struct rswitch_etha * etha,bool read,int phyad,int devad,int regad,int data)1124 static int rswitch_etha_set_access(struct rswitch_etha *etha, bool read,
1125 int phyad, int devad, int regad, int data)
1126 {
1127 int pop = read ? MDIO_READ_C45 : MDIO_WRITE_C45;
1128 u32 val;
1129 int ret;
1130
1131 if (devad == 0xffffffff)
1132 return -ENODEV;
1133
1134 writel(MMIS1_CLEAR_FLAGS, etha->addr + MMIS1);
1135
1136 val = MPSM_PSME | MPSM_MFF_C45;
1137 iowrite32((regad << 16) | (devad << 8) | (phyad << 3) | val, etha->addr + MPSM);
1138
1139 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PAACS, MMIS1_PAACS);
1140 if (ret)
1141 return ret;
1142
1143 rswitch_modify(etha->addr, MMIS1, MMIS1_PAACS, MMIS1_PAACS);
1144
1145 if (read) {
1146 writel((pop << 13) | (devad << 8) | (phyad << 3) | val, etha->addr + MPSM);
1147
1148 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PRACS, MMIS1_PRACS);
1149 if (ret)
1150 return ret;
1151
1152 ret = (ioread32(etha->addr + MPSM) & MPSM_PRD_MASK) >> 16;
1153
1154 rswitch_modify(etha->addr, MMIS1, MMIS1_PRACS, MMIS1_PRACS);
1155 } else {
1156 iowrite32((data << 16) | (pop << 13) | (devad << 8) | (phyad << 3) | val,
1157 etha->addr + MPSM);
1158
1159 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PWACS, MMIS1_PWACS);
1160 }
1161
1162 return ret;
1163 }
1164
rswitch_etha_mii_read_c45(struct mii_bus * bus,int addr,int devad,int regad)1165 static int rswitch_etha_mii_read_c45(struct mii_bus *bus, int addr, int devad,
1166 int regad)
1167 {
1168 struct rswitch_etha *etha = bus->priv;
1169
1170 return rswitch_etha_set_access(etha, true, addr, devad, regad, 0);
1171 }
1172
rswitch_etha_mii_write_c45(struct mii_bus * bus,int addr,int devad,int regad,u16 val)1173 static int rswitch_etha_mii_write_c45(struct mii_bus *bus, int addr, int devad,
1174 int regad, u16 val)
1175 {
1176 struct rswitch_etha *etha = bus->priv;
1177
1178 return rswitch_etha_set_access(etha, false, addr, devad, regad, val);
1179 }
1180
1181 /* Call of_node_put(port) after done */
rswitch_get_port_node(struct rswitch_device * rdev)1182 static struct device_node *rswitch_get_port_node(struct rswitch_device *rdev)
1183 {
1184 struct device_node *ports, *port;
1185 int err = 0;
1186 u32 index;
1187
1188 ports = of_get_child_by_name(rdev->ndev->dev.parent->of_node,
1189 "ethernet-ports");
1190 if (!ports)
1191 return NULL;
1192
1193 for_each_child_of_node(ports, port) {
1194 err = of_property_read_u32(port, "reg", &index);
1195 if (err < 0) {
1196 port = NULL;
1197 goto out;
1198 }
1199 if (index == rdev->etha->index) {
1200 if (!of_device_is_available(port))
1201 port = NULL;
1202 break;
1203 }
1204 }
1205
1206 out:
1207 of_node_put(ports);
1208
1209 return port;
1210 }
1211
rswitch_etha_get_params(struct rswitch_device * rdev)1212 static int rswitch_etha_get_params(struct rswitch_device *rdev)
1213 {
1214 u32 max_speed;
1215 int err;
1216
1217 if (!rdev->np_port)
1218 return 0; /* ignored */
1219
1220 err = of_get_phy_mode(rdev->np_port, &rdev->etha->phy_interface);
1221 if (err)
1222 return err;
1223
1224 err = of_property_read_u32(rdev->np_port, "max-speed", &max_speed);
1225 if (!err) {
1226 rdev->etha->speed = max_speed;
1227 return 0;
1228 }
1229
1230 /* if no "max-speed" property, let's use default speed */
1231 switch (rdev->etha->phy_interface) {
1232 case PHY_INTERFACE_MODE_MII:
1233 rdev->etha->speed = SPEED_100;
1234 break;
1235 case PHY_INTERFACE_MODE_SGMII:
1236 rdev->etha->speed = SPEED_1000;
1237 break;
1238 case PHY_INTERFACE_MODE_USXGMII:
1239 rdev->etha->speed = SPEED_2500;
1240 break;
1241 default:
1242 return -EINVAL;
1243 }
1244
1245 return 0;
1246 }
1247
rswitch_mii_register(struct rswitch_device * rdev)1248 static int rswitch_mii_register(struct rswitch_device *rdev)
1249 {
1250 struct device_node *mdio_np;
1251 struct mii_bus *mii_bus;
1252 int err;
1253
1254 mii_bus = mdiobus_alloc();
1255 if (!mii_bus)
1256 return -ENOMEM;
1257
1258 mii_bus->name = "rswitch_mii";
1259 sprintf(mii_bus->id, "etha%d", rdev->etha->index);
1260 mii_bus->priv = rdev->etha;
1261 mii_bus->read_c45 = rswitch_etha_mii_read_c45;
1262 mii_bus->write_c45 = rswitch_etha_mii_write_c45;
1263 mii_bus->parent = &rdev->priv->pdev->dev;
1264
1265 mdio_np = of_get_child_by_name(rdev->np_port, "mdio");
1266 err = of_mdiobus_register(mii_bus, mdio_np);
1267 if (err < 0) {
1268 mdiobus_free(mii_bus);
1269 goto out;
1270 }
1271
1272 rdev->etha->mii = mii_bus;
1273
1274 out:
1275 of_node_put(mdio_np);
1276
1277 return err;
1278 }
1279
rswitch_mii_unregister(struct rswitch_device * rdev)1280 static void rswitch_mii_unregister(struct rswitch_device *rdev)
1281 {
1282 if (rdev->etha->mii) {
1283 mdiobus_unregister(rdev->etha->mii);
1284 mdiobus_free(rdev->etha->mii);
1285 rdev->etha->mii = NULL;
1286 }
1287 }
1288
rswitch_adjust_link(struct net_device * ndev)1289 static void rswitch_adjust_link(struct net_device *ndev)
1290 {
1291 struct rswitch_device *rdev = netdev_priv(ndev);
1292 struct phy_device *phydev = ndev->phydev;
1293
1294 if (phydev->link != rdev->etha->link) {
1295 phy_print_status(phydev);
1296 if (phydev->link)
1297 phy_power_on(rdev->serdes);
1298 else if (rdev->serdes->power_count)
1299 phy_power_off(rdev->serdes);
1300
1301 rdev->etha->link = phydev->link;
1302
1303 if (!rdev->priv->etha_no_runtime_change &&
1304 phydev->speed != rdev->etha->speed) {
1305 rdev->etha->speed = phydev->speed;
1306
1307 rswitch_etha_hw_init(rdev->etha, rdev->ndev->dev_addr);
1308 phy_set_speed(rdev->serdes, rdev->etha->speed);
1309 }
1310 }
1311 }
1312
rswitch_phy_remove_link_mode(struct rswitch_device * rdev,struct phy_device * phydev)1313 static void rswitch_phy_remove_link_mode(struct rswitch_device *rdev,
1314 struct phy_device *phydev)
1315 {
1316 if (!rdev->priv->etha_no_runtime_change)
1317 return;
1318
1319 switch (rdev->etha->speed) {
1320 case SPEED_2500:
1321 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Full_BIT);
1322 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Full_BIT);
1323 break;
1324 case SPEED_1000:
1325 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_2500baseX_Full_BIT);
1326 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Full_BIT);
1327 break;
1328 case SPEED_100:
1329 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_2500baseX_Full_BIT);
1330 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Full_BIT);
1331 break;
1332 default:
1333 break;
1334 }
1335
1336 phy_set_max_speed(phydev, rdev->etha->speed);
1337 }
1338
rswitch_phy_device_init(struct rswitch_device * rdev)1339 static int rswitch_phy_device_init(struct rswitch_device *rdev)
1340 {
1341 struct phy_device *phydev;
1342 struct device_node *phy;
1343 int err = -ENOENT;
1344
1345 if (!rdev->np_port)
1346 return -ENODEV;
1347
1348 phy = of_parse_phandle(rdev->np_port, "phy-handle", 0);
1349 if (!phy)
1350 return -ENODEV;
1351
1352 /* Set phydev->host_interfaces before calling of_phy_connect() to
1353 * configure the PHY with the information of host_interfaces.
1354 */
1355 phydev = of_phy_find_device(phy);
1356 if (!phydev)
1357 goto out;
1358 __set_bit(rdev->etha->phy_interface, phydev->host_interfaces);
1359
1360 phydev = of_phy_connect(rdev->ndev, phy, rswitch_adjust_link, 0,
1361 rdev->etha->phy_interface);
1362 if (!phydev)
1363 goto out;
1364
1365 phy_set_max_speed(phydev, SPEED_2500);
1366 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT);
1367 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT);
1368 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT);
1369 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1370 rswitch_phy_remove_link_mode(rdev, phydev);
1371
1372 phy_attached_info(phydev);
1373
1374 err = 0;
1375 out:
1376 of_node_put(phy);
1377
1378 return err;
1379 }
1380
rswitch_phy_device_deinit(struct rswitch_device * rdev)1381 static void rswitch_phy_device_deinit(struct rswitch_device *rdev)
1382 {
1383 if (rdev->ndev->phydev)
1384 phy_disconnect(rdev->ndev->phydev);
1385 }
1386
rswitch_serdes_set_params(struct rswitch_device * rdev)1387 static int rswitch_serdes_set_params(struct rswitch_device *rdev)
1388 {
1389 int err;
1390
1391 err = phy_set_mode_ext(rdev->serdes, PHY_MODE_ETHERNET,
1392 rdev->etha->phy_interface);
1393 if (err < 0)
1394 return err;
1395
1396 return phy_set_speed(rdev->serdes, rdev->etha->speed);
1397 }
1398
rswitch_ether_port_init_one(struct rswitch_device * rdev)1399 static int rswitch_ether_port_init_one(struct rswitch_device *rdev)
1400 {
1401 int err;
1402
1403 if (!rdev->etha->operated) {
1404 err = rswitch_etha_hw_init(rdev->etha, rdev->ndev->dev_addr);
1405 if (err < 0)
1406 return err;
1407 if (rdev->priv->etha_no_runtime_change)
1408 rdev->etha->operated = true;
1409 }
1410
1411 err = rswitch_mii_register(rdev);
1412 if (err < 0)
1413 return err;
1414
1415 err = rswitch_phy_device_init(rdev);
1416 if (err < 0)
1417 goto err_phy_device_init;
1418
1419 rdev->serdes = devm_of_phy_get(&rdev->priv->pdev->dev, rdev->np_port, NULL);
1420 if (IS_ERR(rdev->serdes)) {
1421 err = PTR_ERR(rdev->serdes);
1422 goto err_serdes_phy_get;
1423 }
1424
1425 err = rswitch_serdes_set_params(rdev);
1426 if (err < 0)
1427 goto err_serdes_set_params;
1428
1429 return 0;
1430
1431 err_serdes_set_params:
1432 err_serdes_phy_get:
1433 rswitch_phy_device_deinit(rdev);
1434
1435 err_phy_device_init:
1436 rswitch_mii_unregister(rdev);
1437
1438 return err;
1439 }
1440
rswitch_ether_port_deinit_one(struct rswitch_device * rdev)1441 static void rswitch_ether_port_deinit_one(struct rswitch_device *rdev)
1442 {
1443 rswitch_phy_device_deinit(rdev);
1444 rswitch_mii_unregister(rdev);
1445 }
1446
rswitch_ether_port_init_all(struct rswitch_private * priv)1447 static int rswitch_ether_port_init_all(struct rswitch_private *priv)
1448 {
1449 int i, err;
1450
1451 rswitch_for_each_enabled_port(priv, i) {
1452 err = rswitch_ether_port_init_one(priv->rdev[i]);
1453 if (err)
1454 goto err_init_one;
1455 }
1456
1457 rswitch_for_each_enabled_port(priv, i) {
1458 err = phy_init(priv->rdev[i]->serdes);
1459 if (err)
1460 goto err_serdes;
1461 }
1462
1463 return 0;
1464
1465 err_serdes:
1466 rswitch_for_each_enabled_port_continue_reverse(priv, i)
1467 phy_exit(priv->rdev[i]->serdes);
1468 i = RSWITCH_NUM_PORTS;
1469
1470 err_init_one:
1471 rswitch_for_each_enabled_port_continue_reverse(priv, i)
1472 rswitch_ether_port_deinit_one(priv->rdev[i]);
1473
1474 return err;
1475 }
1476
rswitch_ether_port_deinit_all(struct rswitch_private * priv)1477 static void rswitch_ether_port_deinit_all(struct rswitch_private *priv)
1478 {
1479 unsigned int i;
1480
1481 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
1482 phy_exit(priv->rdev[i]->serdes);
1483 rswitch_ether_port_deinit_one(priv->rdev[i]);
1484 }
1485 }
1486
rswitch_open(struct net_device * ndev)1487 static int rswitch_open(struct net_device *ndev)
1488 {
1489 struct rswitch_device *rdev = netdev_priv(ndev);
1490 unsigned long flags;
1491
1492 if (bitmap_empty(rdev->priv->opened_ports, RSWITCH_NUM_PORTS))
1493 iowrite32(GWCA_TS_IRQ_BIT, rdev->priv->addr + GWTSDIE);
1494
1495 napi_enable(&rdev->napi);
1496
1497 spin_lock_irqsave(&rdev->priv->lock, flags);
1498 bitmap_set(rdev->priv->opened_ports, rdev->port, 1);
1499 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, true);
1500 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, true);
1501 spin_unlock_irqrestore(&rdev->priv->lock, flags);
1502
1503 phy_start(ndev->phydev);
1504
1505 netif_start_queue(ndev);
1506
1507 return 0;
1508 };
1509
rswitch_stop(struct net_device * ndev)1510 static int rswitch_stop(struct net_device *ndev)
1511 {
1512 struct rswitch_device *rdev = netdev_priv(ndev);
1513 struct sk_buff *ts_skb;
1514 unsigned long flags;
1515 unsigned int tag;
1516
1517 netif_tx_stop_all_queues(ndev);
1518
1519 phy_stop(ndev->phydev);
1520
1521 spin_lock_irqsave(&rdev->priv->lock, flags);
1522 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, false);
1523 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, false);
1524 bitmap_clear(rdev->priv->opened_ports, rdev->port, 1);
1525 spin_unlock_irqrestore(&rdev->priv->lock, flags);
1526
1527 napi_disable(&rdev->napi);
1528
1529 if (bitmap_empty(rdev->priv->opened_ports, RSWITCH_NUM_PORTS))
1530 iowrite32(GWCA_TS_IRQ_BIT, rdev->priv->addr + GWTSDID);
1531
1532 for (tag = find_first_bit(rdev->ts_skb_used, TS_TAGS_PER_PORT);
1533 tag < TS_TAGS_PER_PORT;
1534 tag = find_next_bit(rdev->ts_skb_used, TS_TAGS_PER_PORT, tag + 1)) {
1535 ts_skb = xchg(&rdev->ts_skb[tag], NULL);
1536 clear_bit(tag, rdev->ts_skb_used);
1537 if (ts_skb)
1538 dev_kfree_skb(ts_skb);
1539 }
1540
1541 return 0;
1542 };
1543
rswitch_ext_desc_set_info1(struct rswitch_device * rdev,struct sk_buff * skb,struct rswitch_ext_desc * desc)1544 static bool rswitch_ext_desc_set_info1(struct rswitch_device *rdev,
1545 struct sk_buff *skb,
1546 struct rswitch_ext_desc *desc)
1547 {
1548 desc->info1 = cpu_to_le64(INFO1_DV(BIT(rdev->etha->index)) |
1549 INFO1_IPV(GWCA_IPV_NUM) | INFO1_FMT);
1550 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
1551 unsigned int tag;
1552
1553 tag = find_first_zero_bit(rdev->ts_skb_used, TS_TAGS_PER_PORT);
1554 if (tag == TS_TAGS_PER_PORT)
1555 return false;
1556 smp_mb(); /* order bitmap read before rdev->ts_skb[] write */
1557 rdev->ts_skb[tag] = skb_get(skb);
1558 set_bit(tag, rdev->ts_skb_used);
1559
1560 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1561 desc->info1 |= cpu_to_le64(INFO1_TSUN(tag) | INFO1_TXC);
1562
1563 skb_tx_timestamp(skb);
1564 }
1565
1566 return true;
1567 }
1568
rswitch_ext_desc_set(struct rswitch_device * rdev,struct sk_buff * skb,struct rswitch_ext_desc * desc,dma_addr_t dma_addr,u16 len,u8 die_dt)1569 static bool rswitch_ext_desc_set(struct rswitch_device *rdev,
1570 struct sk_buff *skb,
1571 struct rswitch_ext_desc *desc,
1572 dma_addr_t dma_addr, u16 len, u8 die_dt)
1573 {
1574 rswitch_desc_set_dptr(&desc->desc, dma_addr);
1575 desc->desc.info_ds = cpu_to_le16(len);
1576 if (!rswitch_ext_desc_set_info1(rdev, skb, desc))
1577 return false;
1578
1579 dma_wmb();
1580
1581 desc->desc.die_dt = die_dt;
1582
1583 return true;
1584 }
1585
rswitch_ext_desc_get_die_dt(unsigned int nr_desc,unsigned int index)1586 static u8 rswitch_ext_desc_get_die_dt(unsigned int nr_desc, unsigned int index)
1587 {
1588 if (nr_desc == 1)
1589 return DT_FSINGLE | DIE;
1590 if (index == 0)
1591 return DT_FSTART;
1592 if (nr_desc - 1 == index)
1593 return DT_FEND | DIE;
1594 return DT_FMID;
1595 }
1596
rswitch_ext_desc_get_len(u8 die_dt,unsigned int orig_len)1597 static u16 rswitch_ext_desc_get_len(u8 die_dt, unsigned int orig_len)
1598 {
1599 switch (die_dt & DT_MASK) {
1600 case DT_FSINGLE:
1601 case DT_FEND:
1602 return (orig_len % RSWITCH_DESC_BUF_SIZE) ?: RSWITCH_DESC_BUF_SIZE;
1603 case DT_FSTART:
1604 case DT_FMID:
1605 return RSWITCH_DESC_BUF_SIZE;
1606 default:
1607 return 0;
1608 }
1609 }
1610
rswitch_start_xmit(struct sk_buff * skb,struct net_device * ndev)1611 static netdev_tx_t rswitch_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1612 {
1613 struct rswitch_device *rdev = netdev_priv(ndev);
1614 struct rswitch_gwca_queue *gq = rdev->tx_queue;
1615 dma_addr_t dma_addr, dma_addr_orig;
1616 netdev_tx_t ret = NETDEV_TX_OK;
1617 struct rswitch_ext_desc *desc;
1618 unsigned int i, nr_desc;
1619 u8 die_dt;
1620 u16 len;
1621
1622 nr_desc = (skb->len - 1) / RSWITCH_DESC_BUF_SIZE + 1;
1623 if (rswitch_get_num_cur_queues(gq) >= gq->ring_size - nr_desc) {
1624 netif_stop_subqueue(ndev, 0);
1625 return NETDEV_TX_BUSY;
1626 }
1627
1628 if (skb_put_padto(skb, ETH_ZLEN))
1629 return ret;
1630
1631 dma_addr_orig = dma_map_single(ndev->dev.parent, skb->data, skb->len, DMA_TO_DEVICE);
1632 if (dma_mapping_error(ndev->dev.parent, dma_addr_orig))
1633 goto err_kfree;
1634
1635 /* Stored the skb at the last descriptor to avoid skb free before hardware completes send */
1636 gq->skbs[(gq->cur + nr_desc - 1) % gq->ring_size] = skb;
1637 gq->unmap_addrs[(gq->cur + nr_desc - 1) % gq->ring_size] = dma_addr_orig;
1638
1639 dma_wmb();
1640
1641 /* DT_FSTART should be set at last. So, this is reverse order. */
1642 for (i = nr_desc; i-- > 0; ) {
1643 desc = &gq->tx_ring[rswitch_next_queue_index(gq, true, i)];
1644 die_dt = rswitch_ext_desc_get_die_dt(nr_desc, i);
1645 dma_addr = dma_addr_orig + i * RSWITCH_DESC_BUF_SIZE;
1646 len = rswitch_ext_desc_get_len(die_dt, skb->len);
1647 if (!rswitch_ext_desc_set(rdev, skb, desc, dma_addr, len, die_dt))
1648 goto err_unmap;
1649 }
1650
1651 gq->cur = rswitch_next_queue_index(gq, true, nr_desc);
1652 rswitch_modify(rdev->addr, GWTRC(gq->index), 0, BIT(gq->index % 32));
1653
1654 return ret;
1655
1656 err_unmap:
1657 gq->skbs[(gq->cur + nr_desc - 1) % gq->ring_size] = NULL;
1658 dma_unmap_single(ndev->dev.parent, dma_addr_orig, skb->len, DMA_TO_DEVICE);
1659
1660 err_kfree:
1661 dev_kfree_skb_any(skb);
1662
1663 return ret;
1664 }
1665
rswitch_get_stats(struct net_device * ndev)1666 static struct net_device_stats *rswitch_get_stats(struct net_device *ndev)
1667 {
1668 return &ndev->stats;
1669 }
1670
rswitch_hwstamp_get(struct net_device * ndev,struct ifreq * req)1671 static int rswitch_hwstamp_get(struct net_device *ndev, struct ifreq *req)
1672 {
1673 struct rswitch_device *rdev = netdev_priv(ndev);
1674 struct rcar_gen4_ptp_private *ptp_priv;
1675 struct hwtstamp_config config;
1676
1677 ptp_priv = rdev->priv->ptp_priv;
1678
1679 config.flags = 0;
1680 config.tx_type = ptp_priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1681 HWTSTAMP_TX_OFF;
1682 switch (ptp_priv->tstamp_rx_ctrl & RCAR_GEN4_RXTSTAMP_TYPE) {
1683 case RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT:
1684 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1685 break;
1686 case RCAR_GEN4_RXTSTAMP_TYPE_ALL:
1687 config.rx_filter = HWTSTAMP_FILTER_ALL;
1688 break;
1689 default:
1690 config.rx_filter = HWTSTAMP_FILTER_NONE;
1691 break;
1692 }
1693
1694 return copy_to_user(req->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
1695 }
1696
rswitch_hwstamp_set(struct net_device * ndev,struct ifreq * req)1697 static int rswitch_hwstamp_set(struct net_device *ndev, struct ifreq *req)
1698 {
1699 struct rswitch_device *rdev = netdev_priv(ndev);
1700 u32 tstamp_rx_ctrl = RCAR_GEN4_RXTSTAMP_ENABLED;
1701 struct hwtstamp_config config;
1702 u32 tstamp_tx_ctrl;
1703
1704 if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1705 return -EFAULT;
1706
1707 if (config.flags)
1708 return -EINVAL;
1709
1710 switch (config.tx_type) {
1711 case HWTSTAMP_TX_OFF:
1712 tstamp_tx_ctrl = 0;
1713 break;
1714 case HWTSTAMP_TX_ON:
1715 tstamp_tx_ctrl = RCAR_GEN4_TXTSTAMP_ENABLED;
1716 break;
1717 default:
1718 return -ERANGE;
1719 }
1720
1721 switch (config.rx_filter) {
1722 case HWTSTAMP_FILTER_NONE:
1723 tstamp_rx_ctrl = 0;
1724 break;
1725 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1726 tstamp_rx_ctrl |= RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT;
1727 break;
1728 default:
1729 config.rx_filter = HWTSTAMP_FILTER_ALL;
1730 tstamp_rx_ctrl |= RCAR_GEN4_RXTSTAMP_TYPE_ALL;
1731 break;
1732 }
1733
1734 rdev->priv->ptp_priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1735 rdev->priv->ptp_priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1736
1737 return copy_to_user(req->ifr_data, &config, sizeof(config)) ? -EFAULT : 0;
1738 }
1739
rswitch_eth_ioctl(struct net_device * ndev,struct ifreq * req,int cmd)1740 static int rswitch_eth_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1741 {
1742 if (!netif_running(ndev))
1743 return -EINVAL;
1744
1745 switch (cmd) {
1746 case SIOCGHWTSTAMP:
1747 return rswitch_hwstamp_get(ndev, req);
1748 case SIOCSHWTSTAMP:
1749 return rswitch_hwstamp_set(ndev, req);
1750 default:
1751 return phy_mii_ioctl(ndev->phydev, req, cmd);
1752 }
1753 }
1754
1755 static const struct net_device_ops rswitch_netdev_ops = {
1756 .ndo_open = rswitch_open,
1757 .ndo_stop = rswitch_stop,
1758 .ndo_start_xmit = rswitch_start_xmit,
1759 .ndo_get_stats = rswitch_get_stats,
1760 .ndo_eth_ioctl = rswitch_eth_ioctl,
1761 .ndo_validate_addr = eth_validate_addr,
1762 .ndo_set_mac_address = eth_mac_addr,
1763 };
1764
rswitch_get_ts_info(struct net_device * ndev,struct ethtool_ts_info * info)1765 static int rswitch_get_ts_info(struct net_device *ndev, struct ethtool_ts_info *info)
1766 {
1767 struct rswitch_device *rdev = netdev_priv(ndev);
1768
1769 info->phc_index = ptp_clock_index(rdev->priv->ptp_priv->clock);
1770 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1771 SOF_TIMESTAMPING_RX_SOFTWARE |
1772 SOF_TIMESTAMPING_SOFTWARE |
1773 SOF_TIMESTAMPING_TX_HARDWARE |
1774 SOF_TIMESTAMPING_RX_HARDWARE |
1775 SOF_TIMESTAMPING_RAW_HARDWARE;
1776 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
1777 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1778
1779 return 0;
1780 }
1781
1782 static const struct ethtool_ops rswitch_ethtool_ops = {
1783 .get_ts_info = rswitch_get_ts_info,
1784 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1785 .set_link_ksettings = phy_ethtool_set_link_ksettings,
1786 };
1787
1788 static const struct of_device_id renesas_eth_sw_of_table[] = {
1789 { .compatible = "renesas,r8a779f0-ether-switch", },
1790 { }
1791 };
1792 MODULE_DEVICE_TABLE(of, renesas_eth_sw_of_table);
1793
rswitch_etha_init(struct rswitch_private * priv,unsigned int index)1794 static void rswitch_etha_init(struct rswitch_private *priv, unsigned int index)
1795 {
1796 struct rswitch_etha *etha = &priv->etha[index];
1797
1798 memset(etha, 0, sizeof(*etha));
1799 etha->index = index;
1800 etha->addr = priv->addr + RSWITCH_ETHA_OFFSET + index * RSWITCH_ETHA_SIZE;
1801 etha->coma_addr = priv->addr;
1802
1803 /* MPIC.PSMCS = (clk [MHz] / (MDC frequency [MHz] * 2) - 1.
1804 * Calculating PSMCS value as MDC frequency = 2.5MHz. So, multiply
1805 * both the numerator and the denominator by 10.
1806 */
1807 etha->psmcs = clk_get_rate(priv->clk) / 100000 / (25 * 2) - 1;
1808 }
1809
rswitch_device_alloc(struct rswitch_private * priv,unsigned int index)1810 static int rswitch_device_alloc(struct rswitch_private *priv, unsigned int index)
1811 {
1812 struct platform_device *pdev = priv->pdev;
1813 struct rswitch_device *rdev;
1814 struct net_device *ndev;
1815 int err;
1816
1817 if (index >= RSWITCH_NUM_PORTS)
1818 return -EINVAL;
1819
1820 ndev = alloc_etherdev_mqs(sizeof(struct rswitch_device), 1, 1);
1821 if (!ndev)
1822 return -ENOMEM;
1823
1824 SET_NETDEV_DEV(ndev, &pdev->dev);
1825 ether_setup(ndev);
1826
1827 rdev = netdev_priv(ndev);
1828 rdev->ndev = ndev;
1829 rdev->priv = priv;
1830 priv->rdev[index] = rdev;
1831 rdev->port = index;
1832 rdev->etha = &priv->etha[index];
1833 rdev->addr = priv->addr;
1834
1835 ndev->base_addr = (unsigned long)rdev->addr;
1836 snprintf(ndev->name, IFNAMSIZ, "tsn%d", index);
1837 ndev->netdev_ops = &rswitch_netdev_ops;
1838 ndev->ethtool_ops = &rswitch_ethtool_ops;
1839
1840 netif_napi_add(ndev, &rdev->napi, rswitch_poll);
1841
1842 rdev->np_port = rswitch_get_port_node(rdev);
1843 rdev->disabled = !rdev->np_port;
1844 err = of_get_ethdev_address(rdev->np_port, ndev);
1845 if (err) {
1846 if (is_valid_ether_addr(rdev->etha->mac_addr))
1847 eth_hw_addr_set(ndev, rdev->etha->mac_addr);
1848 else
1849 eth_hw_addr_random(ndev);
1850 }
1851
1852 err = rswitch_etha_get_params(rdev);
1853 if (err < 0)
1854 goto out_get_params;
1855
1856 if (rdev->priv->gwca.speed < rdev->etha->speed)
1857 rdev->priv->gwca.speed = rdev->etha->speed;
1858
1859 err = rswitch_rxdmac_alloc(ndev);
1860 if (err < 0)
1861 goto out_rxdmac;
1862
1863 err = rswitch_txdmac_alloc(ndev);
1864 if (err < 0)
1865 goto out_txdmac;
1866
1867 return 0;
1868
1869 out_txdmac:
1870 rswitch_rxdmac_free(ndev);
1871
1872 out_rxdmac:
1873 out_get_params:
1874 of_node_put(rdev->np_port);
1875 netif_napi_del(&rdev->napi);
1876 free_netdev(ndev);
1877
1878 return err;
1879 }
1880
rswitch_device_free(struct rswitch_private * priv,unsigned int index)1881 static void rswitch_device_free(struct rswitch_private *priv, unsigned int index)
1882 {
1883 struct rswitch_device *rdev = priv->rdev[index];
1884 struct net_device *ndev = rdev->ndev;
1885
1886 rswitch_txdmac_free(ndev);
1887 rswitch_rxdmac_free(ndev);
1888 of_node_put(rdev->np_port);
1889 netif_napi_del(&rdev->napi);
1890 free_netdev(ndev);
1891 }
1892
rswitch_init(struct rswitch_private * priv)1893 static int rswitch_init(struct rswitch_private *priv)
1894 {
1895 int i, err;
1896
1897 for (i = 0; i < RSWITCH_NUM_PORTS; i++)
1898 rswitch_etha_init(priv, i);
1899
1900 rswitch_clock_enable(priv);
1901 for (i = 0; i < RSWITCH_NUM_PORTS; i++)
1902 rswitch_etha_read_mac_address(&priv->etha[i]);
1903
1904 rswitch_reset(priv);
1905
1906 rswitch_clock_enable(priv);
1907 rswitch_top_init(priv);
1908 err = rswitch_bpool_config(priv);
1909 if (err < 0)
1910 return err;
1911
1912 rswitch_coma_init(priv);
1913
1914 err = rswitch_gwca_linkfix_alloc(priv);
1915 if (err < 0)
1916 return -ENOMEM;
1917
1918 err = rswitch_gwca_ts_queue_alloc(priv);
1919 if (err < 0)
1920 goto err_ts_queue_alloc;
1921
1922 for (i = 0; i < RSWITCH_NUM_PORTS; i++) {
1923 err = rswitch_device_alloc(priv, i);
1924 if (err < 0) {
1925 for (i--; i >= 0; i--)
1926 rswitch_device_free(priv, i);
1927 goto err_device_alloc;
1928 }
1929 }
1930
1931 rswitch_fwd_init(priv);
1932
1933 err = rcar_gen4_ptp_register(priv->ptp_priv, RCAR_GEN4_PTP_REG_LAYOUT_S4,
1934 RCAR_GEN4_PTP_CLOCK_S4);
1935 if (err < 0)
1936 goto err_ptp_register;
1937
1938 err = rswitch_gwca_request_irqs(priv);
1939 if (err < 0)
1940 goto err_gwca_request_irq;
1941
1942 err = rswitch_gwca_ts_request_irqs(priv);
1943 if (err < 0)
1944 goto err_gwca_ts_request_irq;
1945
1946 err = rswitch_gwca_hw_init(priv);
1947 if (err < 0)
1948 goto err_gwca_hw_init;
1949
1950 err = rswitch_ether_port_init_all(priv);
1951 if (err)
1952 goto err_ether_port_init_all;
1953
1954 rswitch_for_each_enabled_port(priv, i) {
1955 err = register_netdev(priv->rdev[i]->ndev);
1956 if (err) {
1957 rswitch_for_each_enabled_port_continue_reverse(priv, i)
1958 unregister_netdev(priv->rdev[i]->ndev);
1959 goto err_register_netdev;
1960 }
1961 }
1962
1963 rswitch_for_each_enabled_port(priv, i)
1964 netdev_info(priv->rdev[i]->ndev, "MAC address %pM\n",
1965 priv->rdev[i]->ndev->dev_addr);
1966
1967 return 0;
1968
1969 err_register_netdev:
1970 rswitch_ether_port_deinit_all(priv);
1971
1972 err_ether_port_init_all:
1973 rswitch_gwca_hw_deinit(priv);
1974
1975 err_gwca_hw_init:
1976 err_gwca_ts_request_irq:
1977 err_gwca_request_irq:
1978 rcar_gen4_ptp_unregister(priv->ptp_priv);
1979
1980 err_ptp_register:
1981 for (i = 0; i < RSWITCH_NUM_PORTS; i++)
1982 rswitch_device_free(priv, i);
1983
1984 err_device_alloc:
1985 rswitch_gwca_ts_queue_free(priv);
1986
1987 err_ts_queue_alloc:
1988 rswitch_gwca_linkfix_free(priv);
1989
1990 return err;
1991 }
1992
1993 static const struct soc_device_attribute rswitch_soc_no_speed_change[] = {
1994 { .soc_id = "r8a779f0", .revision = "ES1.0" },
1995 { /* Sentinel */ }
1996 };
1997
renesas_eth_sw_probe(struct platform_device * pdev)1998 static int renesas_eth_sw_probe(struct platform_device *pdev)
1999 {
2000 const struct soc_device_attribute *attr;
2001 struct rswitch_private *priv;
2002 struct resource *res;
2003 int ret;
2004
2005 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "secure_base");
2006 if (!res) {
2007 dev_err(&pdev->dev, "invalid resource\n");
2008 return -EINVAL;
2009 }
2010
2011 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
2012 if (!priv)
2013 return -ENOMEM;
2014 spin_lock_init(&priv->lock);
2015
2016 priv->clk = devm_clk_get(&pdev->dev, NULL);
2017 if (IS_ERR(priv->clk))
2018 return PTR_ERR(priv->clk);
2019
2020 attr = soc_device_match(rswitch_soc_no_speed_change);
2021 if (attr)
2022 priv->etha_no_runtime_change = true;
2023
2024 priv->ptp_priv = rcar_gen4_ptp_alloc(pdev);
2025 if (!priv->ptp_priv)
2026 return -ENOMEM;
2027
2028 platform_set_drvdata(pdev, priv);
2029 priv->pdev = pdev;
2030 priv->addr = devm_ioremap_resource(&pdev->dev, res);
2031 if (IS_ERR(priv->addr))
2032 return PTR_ERR(priv->addr);
2033
2034 priv->ptp_priv->addr = priv->addr + RCAR_GEN4_GPTP_OFFSET_S4;
2035
2036 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
2037 if (ret < 0) {
2038 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2039 if (ret < 0)
2040 return ret;
2041 }
2042
2043 priv->gwca.index = AGENT_INDEX_GWCA;
2044 priv->gwca.num_queues = min(RSWITCH_NUM_PORTS * NUM_QUEUES_PER_NDEV,
2045 RSWITCH_MAX_NUM_QUEUES);
2046 priv->gwca.queues = devm_kcalloc(&pdev->dev, priv->gwca.num_queues,
2047 sizeof(*priv->gwca.queues), GFP_KERNEL);
2048 if (!priv->gwca.queues)
2049 return -ENOMEM;
2050
2051 pm_runtime_enable(&pdev->dev);
2052 pm_runtime_get_sync(&pdev->dev);
2053
2054 ret = rswitch_init(priv);
2055 if (ret < 0) {
2056 pm_runtime_put(&pdev->dev);
2057 pm_runtime_disable(&pdev->dev);
2058 return ret;
2059 }
2060
2061 device_set_wakeup_capable(&pdev->dev, 1);
2062
2063 return ret;
2064 }
2065
rswitch_deinit(struct rswitch_private * priv)2066 static void rswitch_deinit(struct rswitch_private *priv)
2067 {
2068 int i;
2069
2070 rswitch_gwca_hw_deinit(priv);
2071 rcar_gen4_ptp_unregister(priv->ptp_priv);
2072
2073 rswitch_for_each_enabled_port(priv, i) {
2074 struct rswitch_device *rdev = priv->rdev[i];
2075
2076 unregister_netdev(rdev->ndev);
2077 rswitch_ether_port_deinit_one(rdev);
2078 phy_exit(priv->rdev[i]->serdes);
2079 }
2080
2081 for (i = 0; i < RSWITCH_NUM_PORTS; i++)
2082 rswitch_device_free(priv, i);
2083
2084 rswitch_gwca_ts_queue_free(priv);
2085 rswitch_gwca_linkfix_free(priv);
2086
2087 rswitch_clock_disable(priv);
2088 }
2089
renesas_eth_sw_remove(struct platform_device * pdev)2090 static int renesas_eth_sw_remove(struct platform_device *pdev)
2091 {
2092 struct rswitch_private *priv = platform_get_drvdata(pdev);
2093
2094 rswitch_deinit(priv);
2095
2096 pm_runtime_put(&pdev->dev);
2097 pm_runtime_disable(&pdev->dev);
2098
2099 platform_set_drvdata(pdev, NULL);
2100
2101 return 0;
2102 }
2103
2104 static struct platform_driver renesas_eth_sw_driver_platform = {
2105 .probe = renesas_eth_sw_probe,
2106 .remove = renesas_eth_sw_remove,
2107 .driver = {
2108 .name = "renesas_eth_sw",
2109 .of_match_table = renesas_eth_sw_of_table,
2110 }
2111 };
2112 module_platform_driver(renesas_eth_sw_driver_platform);
2113 MODULE_AUTHOR("Yoshihiro Shimoda");
2114 MODULE_DESCRIPTION("Renesas Ethernet Switch device driver");
2115 MODULE_LICENSE("GPL");
2116