1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * Copyright 2023 ROCKCHIP
4 */
5
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/of_address.h>
9 #include <linux/of_platform.h>
10 #include <linux/of_net.h>
11 #include <linux/uio_driver.h>
12 #include <linux/list.h>
13
14 #include <linux/clk.h>
15 #include <linux/kernel.h>
16 #include <linux/interrupt.h>
17 #include <linux/ip.h>
18 #include <linux/tcp.h>
19 #include <linux/ethtool.h>
20 #include <linux/if_ether.h>
21 #include <linux/crc32.h>
22 #include <linux/mii.h>
23 #include <linux/if.h>
24 #include <linux/if_vlan.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/prefetch.h>
28 #include <linux/pinctrl/consumer.h>
29 #ifdef CONFIG_DEBUG_FS
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32 #endif /* CONFIG_DEBUG_FS */
33 #include <linux/net_tstamp.h>
34 #include <linux/udp.h>
35 #include <net/pkt_cls.h>
36 #include "stmmac_ptp.h"
37 #include "stmmac.h"
38 #include <linux/reset.h>
39 #include <linux/of_mdio.h>
40 #include "dwmac1000.h"
41 #include "dwxgmac2.h"
42 #include "hwif.h"
43 #include "mmc.h"
44
45 #define DRIVER_NAME "rockchip_gmac_uio_drv"
46 #define DRIVER_VERSION "0.1"
47
48 #define TC_DEFAULT 64
49 static int tc = TC_DEFAULT;
50
51 #define DEFAULT_BUFSIZE 1536
52 static int buf_sz = DEFAULT_BUFSIZE;
53
54 #define STMMAC_RX_COPYBREAK 256
55
56 /**
57 * rockchip_gmac_uio_pdev_info
58 * local information for uio module driver
59 *
60 * @dev: device pointer
61 * @ndev: network device pointer
62 * @name: uio name
63 * @uio: uio information
64 * @map_num: number of uio memory regions
65 */
66 struct rockchip_gmac_uio_pdev_info {
67 struct device *dev;
68 struct net_device *ndev;
69 char name[16];
70 struct uio_info uio;
71 int map_num;
72 };
73
rockchip_gmac_uio_open(struct uio_info * info,struct inode * inode)74 static int rockchip_gmac_uio_open(struct uio_info *info, struct inode *inode)
75 {
76 return 0;
77 }
78
rockchip_gmac_uio_release(struct uio_info * info,struct inode * inode)79 static int rockchip_gmac_uio_release(struct uio_info *info,
80 struct inode *inode)
81 {
82 return 0;
83 }
84
rockchip_gmac_uio_mmap(struct uio_info * info,struct vm_area_struct * vma)85 static int rockchip_gmac_uio_mmap(struct uio_info *info,
86 struct vm_area_struct *vma)
87 {
88 u32 ret;
89 u32 pfn;
90
91 pfn = (info->mem[vma->vm_pgoff].addr) >> PAGE_SHIFT;
92
93 if (vma->vm_pgoff)
94 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
95 else
96 vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
97
98 ret = remap_pfn_range(vma, vma->vm_start, pfn,
99 vma->vm_end - vma->vm_start, vma->vm_page_prot);
100 if (ret) {
101 /* Error Handle */
102 pr_err("remap_pfn_range failed");
103 }
104 return ret;
105 }
106
107 /**
108 * uio_free_dma_rx_desc_resources - free RX dma desc resources
109 * @priv: private structure
110 */
uio_free_dma_rx_desc_resources(struct stmmac_priv * priv)111 static void uio_free_dma_rx_desc_resources(struct stmmac_priv *priv)
112 {
113 u32 rx_count = priv->plat->rx_queues_to_use;
114 u32 queue;
115
116 /* Free RX queue resources */
117 for (queue = 0; queue < rx_count; queue++) {
118 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
119
120 /* Free DMA regions of consistent memory previously allocated */
121 if (!priv->extend_desc)
122 dma_free_coherent(priv->device, priv->dma_rx_size *
123 sizeof(struct dma_desc),
124 rx_q->dma_rx, rx_q->dma_rx_phy);
125 else
126 dma_free_coherent(priv->device, priv->dma_rx_size *
127 sizeof(struct dma_extended_desc),
128 rx_q->dma_erx, rx_q->dma_rx_phy);
129 }
130 }
131
132 /**
133 * uio_free_dma_tx_desc_resources - free TX dma desc resources
134 * @priv: private structure
135 */
uio_free_dma_tx_desc_resources(struct stmmac_priv * priv)136 static void uio_free_dma_tx_desc_resources(struct stmmac_priv *priv)
137 {
138 u32 tx_count = priv->plat->tx_queues_to_use;
139 u32 queue;
140
141 /* Free TX queue resources */
142 for (queue = 0; queue < tx_count; queue++) {
143 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
144 size_t size;
145 void *addr;
146
147 if (priv->extend_desc) {
148 size = sizeof(struct dma_extended_desc);
149 addr = tx_q->dma_etx;
150 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
151 size = sizeof(struct dma_edesc);
152 addr = tx_q->dma_entx;
153 } else {
154 size = sizeof(struct dma_desc);
155 addr = tx_q->dma_tx;
156 }
157
158 size *= priv->dma_tx_size;
159
160 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
161 }
162 }
163
164 /**
165 * uio_alloc_dma_rx_desc_resources - alloc RX resources.
166 * @priv: private structure
167 * Description: according to which descriptor can be used (extend or basic)
168 * this function allocates the resources for TX and RX paths. In case of
169 * reception, for example, it pre-allocated the RX socket buffer in order to
170 * allow zero-copy mechanism.
171 */
uio_alloc_dma_rx_desc_resources(struct stmmac_priv * priv)172 static int uio_alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
173 {
174 u32 rx_count = priv->plat->rx_queues_to_use;
175 int ret = -ENOMEM;
176 u32 queue;
177
178 /* RX queues buffers and DMA */
179 for (queue = 0; queue < rx_count; queue++) {
180 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
181
182 if (priv->extend_desc) {
183 rx_q->dma_erx = dma_alloc_coherent(priv->device,
184 priv->dma_rx_size *
185 sizeof(struct dma_extended_desc),
186 &rx_q->dma_rx_phy,
187 GFP_KERNEL);
188 if (!rx_q->dma_erx)
189 goto err_dma;
190 } else {
191 rx_q->dma_rx = dma_alloc_coherent(priv->device,
192 priv->dma_rx_size *
193 sizeof(struct dma_desc),
194 &rx_q->dma_rx_phy,
195 GFP_KERNEL);
196 if (!rx_q->dma_rx)
197 goto err_dma;
198 }
199 }
200
201 return 0;
202
203 err_dma:
204 uio_free_dma_rx_desc_resources(priv);
205
206 return ret;
207 }
208
209 /**
210 * uio_alloc_dma_tx_desc_resources - alloc TX resources.
211 * @priv: private structure
212 * Description: according to which descriptor can be used (extend or basic)
213 * this function allocates the resources for TX and RX paths. In case of
214 * reception, for example, it pre-allocated the RX socket buffer in order to
215 * allow zero-copy mechanism.
216 */
uio_alloc_dma_tx_desc_resources(struct stmmac_priv * priv)217 static int uio_alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
218 {
219 u32 tx_count = priv->plat->tx_queues_to_use;
220 int ret = -ENOMEM;
221 u32 queue;
222
223 /* TX queues buffers and DMA */
224 for (queue = 0; queue < tx_count; queue++) {
225 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
226 size_t size;
227 void *addr;
228
229 tx_q->queue_index = queue;
230 tx_q->priv_data = priv;
231
232 if (priv->extend_desc)
233 size = sizeof(struct dma_extended_desc);
234 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
235 size = sizeof(struct dma_edesc);
236 else
237 size = sizeof(struct dma_desc);
238
239 size *= priv->dma_tx_size;
240
241 addr = dma_alloc_coherent(priv->device, size,
242 &tx_q->dma_tx_phy, GFP_KERNEL);
243 if (!addr)
244 goto err_dma;
245
246 if (priv->extend_desc)
247 tx_q->dma_etx = addr;
248 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
249 tx_q->dma_entx = addr;
250 else
251 tx_q->dma_tx = addr;
252 }
253
254 return 0;
255
256 err_dma:
257 uio_free_dma_tx_desc_resources(priv);
258 return ret;
259 }
260
261 /**
262 * uio_alloc_dma_desc_resources - alloc TX/RX resources.
263 * @priv: private structure
264 * Description: according to which descriptor can be used (extend or basic)
265 * this function allocates the resources for TX and RX paths. In case of
266 * reception, for example, it pre-allocated the RX socket buffer in order to
267 * allow zero-copy mechanism.
268 */
uio_alloc_dma_desc_resources(struct stmmac_priv * priv)269 static int uio_alloc_dma_desc_resources(struct stmmac_priv *priv)
270 {
271 /* RX Allocation */
272 int ret = uio_alloc_dma_rx_desc_resources(priv);
273
274 if (ret)
275 return ret;
276
277 ret = uio_alloc_dma_tx_desc_resources(priv);
278
279 return ret;
280 }
281
282 /**
283 * uio_free_dma_desc_resources - free dma desc resources
284 * @priv: private structure
285 */
uio_free_dma_desc_resources(struct stmmac_priv * priv)286 static void uio_free_dma_desc_resources(struct stmmac_priv *priv)
287 {
288 /* Release the DMA RX socket buffers */
289 uio_free_dma_rx_desc_resources(priv);
290
291 /* Release the DMA TX socket buffers */
292 uio_free_dma_tx_desc_resources(priv);
293 }
294
295 /**
296 * rockchip_gmac_uio_init_phy - PHY initialization
297 * @dev: net device structure
298 * Description: it initializes the driver's PHY state, and attaches the PHY
299 * to the mac driver.
300 * Return value:
301 * 0 on success
302 */
rockchip_gmac_uio_init_phy(struct net_device * dev)303 static int rockchip_gmac_uio_init_phy(struct net_device *dev)
304 {
305 struct stmmac_priv *priv = netdev_priv(dev);
306 struct device_node *node;
307 int ret;
308
309 node = priv->plat->phylink_node;
310
311 if (node)
312 ret = phylink_of_phy_connect(priv->phylink, node, 0);
313
314 /* Some DT bindings do not set-up the PHY handle. Let's try to
315 * manually parse it
316 */
317 if (!node || ret) {
318 int addr = priv->plat->phy_addr;
319 struct phy_device *phydev;
320
321 phydev = mdiobus_get_phy(priv->mii, addr);
322 if (!phydev) {
323 netdev_err(priv->dev, "no phy at addr %d\n", addr);
324 return -ENODEV;
325 }
326
327 ret = phylink_connect_phy(priv->phylink, phydev);
328 }
329
330 if (!priv->plat->pmt) {
331 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
332
333 phylink_ethtool_get_wol(priv->phylink, &wol);
334 device_set_wakeup_capable(priv->device, !!wol.supported);
335 }
336
337 return ret;
338 }
339
340 /**
341 * rockchip_gmac_uio_init_dma_engine - DMA init.
342 * @priv: driver private structure
343 * Description:
344 * It inits the DMA invoking the specific MAC/GMAC callback.
345 * Some DMA parameters can be passed from the platform;
346 * in case of these are not passed a default is kept for the MAC or GMAC.
347 */
rockchip_gmac_uio_init_dma_engine(struct stmmac_priv * priv)348 static int rockchip_gmac_uio_init_dma_engine(struct stmmac_priv *priv)
349 {
350 u32 rx_channels_count = priv->plat->rx_queues_to_use;
351 u32 tx_channels_count = priv->plat->tx_queues_to_use;
352 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
353 struct stmmac_rx_queue *rx_q;
354 struct stmmac_tx_queue *tx_q;
355 u32 chan = 0;
356 int atds = 0;
357 int ret = 0;
358
359 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
360 dev_err(priv->device, "Invalid DMA configuration\n");
361 return -EINVAL;
362 }
363
364 if (priv->extend_desc && priv->mode == STMMAC_RING_MODE)
365 atds = 1;
366
367 ret = stmmac_reset(priv, priv->ioaddr);
368 if (ret) {
369 dev_err(priv->device, "Failed to reset the dma\n");
370 return ret;
371 }
372
373 /* DMA Configuration */
374 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
375
376 if (priv->plat->axi)
377 stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
378
379 /* DMA CSR Channel configuration */
380 for (chan = 0; chan < dma_csr_ch; chan++)
381 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
382
383 /* DMA RX Channel Configuration */
384 for (chan = 0; chan < rx_channels_count; chan++) {
385 rx_q = &priv->rx_queue[chan];
386
387 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
388 rx_q->dma_rx_phy, chan);
389
390 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
391 (priv->dma_rx_size *
392 sizeof(struct dma_desc));
393 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
394 rx_q->rx_tail_addr, chan);
395 }
396
397 /* DMA TX Channel Configuration */
398 for (chan = 0; chan < tx_channels_count; chan++) {
399 tx_q = &priv->tx_queue[chan];
400
401 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
402 tx_q->dma_tx_phy, chan);
403
404 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
405 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
406 tx_q->tx_tail_addr, chan);
407 }
408
409 return ret;
410 }
411
uio_set_rings_length(struct stmmac_priv * priv)412 static void uio_set_rings_length(struct stmmac_priv *priv)
413 {
414 u32 rx_channels_count = priv->plat->rx_queues_to_use;
415 u32 tx_channels_count = priv->plat->tx_queues_to_use;
416 u32 chan;
417
418 /* set TX ring length */
419 for (chan = 0; chan < tx_channels_count; chan++)
420 stmmac_set_tx_ring_len(priv, priv->ioaddr,
421 (priv->dma_tx_size - 1), chan);
422
423 /* set RX ring length */
424 for (chan = 0; chan < rx_channels_count; chan++)
425 stmmac_set_rx_ring_len(priv, priv->ioaddr,
426 (priv->dma_rx_size - 1), chan);
427 }
428
429 /**
430 * uio_set_tx_queue_weight - Set TX queue weight
431 * @priv: driver private structure
432 * Description: It is used for setting TX queues weight
433 */
uio_set_tx_queue_weight(struct stmmac_priv * priv)434 static void uio_set_tx_queue_weight(struct stmmac_priv *priv)
435 {
436 u32 tx_queues_count = priv->plat->tx_queues_to_use;
437 u32 weight;
438 u32 queue;
439
440 for (queue = 0; queue < tx_queues_count; queue++) {
441 weight = priv->plat->tx_queues_cfg[queue].weight;
442 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
443 }
444 }
445
446 /**
447 * uio_configure_cbs - Configure CBS in TX queue
448 * @priv: driver private structure
449 * Description: It is used for configuring CBS in AVB TX queues
450 */
uio_configure_cbs(struct stmmac_priv * priv)451 static void uio_configure_cbs(struct stmmac_priv *priv)
452 {
453 u32 tx_queues_count = priv->plat->tx_queues_to_use;
454 u32 mode_to_use;
455 u32 queue;
456
457 /* queue 0 is reserved for legacy traffic */
458 for (queue = 1; queue < tx_queues_count; queue++) {
459 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
460 if (mode_to_use == MTL_QUEUE_DCB)
461 continue;
462
463 stmmac_config_cbs(priv, priv->hw,
464 priv->plat->tx_queues_cfg[queue].send_slope,
465 priv->plat->tx_queues_cfg[queue].idle_slope,
466 priv->plat->tx_queues_cfg[queue].high_credit,
467 priv->plat->tx_queues_cfg[queue].low_credit,
468 queue);
469 }
470 }
471
472 /**
473 * uio_rx_queue_dma_chan_map - Map RX queue to RX dma channel
474 * @priv: driver private structure
475 * Description: It is used for mapping RX queues to RX dma channels
476 */
uio_rx_queue_dma_chan_map(struct stmmac_priv * priv)477 static void uio_rx_queue_dma_chan_map(struct stmmac_priv *priv)
478 {
479 u32 rx_queues_count = priv->plat->rx_queues_to_use;
480 u32 queue;
481 u32 chan;
482
483 for (queue = 0; queue < rx_queues_count; queue++) {
484 chan = priv->plat->rx_queues_cfg[queue].chan;
485 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
486 }
487 }
488
489 /**
490 * uio_mac_config_rx_queues_prio - Configure RX Queue priority
491 * @priv: driver private structure
492 * Description: It is used for configuring the RX Queue Priority
493 */
uio_mac_config_rx_queues_prio(struct stmmac_priv * priv)494 static void uio_mac_config_rx_queues_prio(struct stmmac_priv *priv)
495 {
496 u32 rx_queues_count = priv->plat->rx_queues_to_use;
497 u32 queue;
498 u32 prio;
499
500 for (queue = 0; queue < rx_queues_count; queue++) {
501 if (!priv->plat->rx_queues_cfg[queue].use_prio)
502 continue;
503
504 prio = priv->plat->rx_queues_cfg[queue].prio;
505 stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
506 }
507 }
508
509 /**
510 * uio_mac_config_tx_queues_prio - Configure TX Queue priority
511 * @priv: driver private structure
512 * Description: It is used for configuring the TX Queue Priority
513 */
uio_mac_config_tx_queues_prio(struct stmmac_priv * priv)514 static void uio_mac_config_tx_queues_prio(struct stmmac_priv *priv)
515 {
516 u32 tx_queues_count = priv->plat->tx_queues_to_use;
517 u32 queue;
518 u32 prio;
519
520 for (queue = 0; queue < tx_queues_count; queue++) {
521 if (!priv->plat->tx_queues_cfg[queue].use_prio)
522 continue;
523
524 prio = priv->plat->tx_queues_cfg[queue].prio;
525 stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
526 }
527 }
528
529 /**
530 * uio_mac_config_rx_queues_routing - Configure RX Queue Routing
531 * @priv: driver private structure
532 * Description: It is used for configuring the RX queue routing
533 */
uio_mac_config_rx_queues_routing(struct stmmac_priv * priv)534 static void uio_mac_config_rx_queues_routing(struct stmmac_priv *priv)
535 {
536 u32 rx_queues_count = priv->plat->rx_queues_to_use;
537 u32 queue;
538 u8 packet;
539
540 for (queue = 0; queue < rx_queues_count; queue++) {
541 /* no specific packet type routing specified for the queue */
542 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
543 continue;
544
545 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
546 stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
547 }
548 }
549
uio_mac_config_rss(struct stmmac_priv * priv)550 static void uio_mac_config_rss(struct stmmac_priv *priv)
551 {
552 if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
553 priv->rss.enable = false;
554 return;
555 }
556
557 if (priv->dev->features & NETIF_F_RXHASH)
558 priv->rss.enable = true;
559 else
560 priv->rss.enable = false;
561
562 stmmac_rss_configure(priv, priv->hw, &priv->rss,
563 priv->plat->rx_queues_to_use);
564 }
565
566 /**
567 * uio_mac_enable_rx_queues - Enable MAC rx queues
568 * @priv: driver private structure
569 * Description: It is used for enabling the rx queues in the MAC
570 */
uio_mac_enable_rx_queues(struct stmmac_priv * priv)571 static void uio_mac_enable_rx_queues(struct stmmac_priv *priv)
572 {
573 u32 rx_queues_count = priv->plat->rx_queues_to_use;
574 int queue;
575 u8 mode;
576
577 for (queue = 0; queue < rx_queues_count; queue++) {
578 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
579 stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
580 }
581 }
582
583 /**
584 * rockchip_gmac_uio_mtl_configuration - Configure MTL
585 * @priv: driver private structure
586 * Description: It is used for configuring MTL
587 */
rockchip_gmac_uio_mtl_configuration(struct stmmac_priv * priv)588 static void rockchip_gmac_uio_mtl_configuration(struct stmmac_priv *priv)
589 {
590 u32 rx_queues_count = priv->plat->rx_queues_to_use;
591 u32 tx_queues_count = priv->plat->tx_queues_to_use;
592
593 if (tx_queues_count > 1)
594 uio_set_tx_queue_weight(priv);
595
596 /* Configure MTL RX algorithms */
597 if (rx_queues_count > 1)
598 stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
599 priv->plat->rx_sched_algorithm);
600
601 /* Configure MTL TX algorithms */
602 if (tx_queues_count > 1)
603 stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
604 priv->plat->tx_sched_algorithm);
605
606 /* Configure CBS in AVB TX queues */
607 if (tx_queues_count > 1)
608 uio_configure_cbs(priv);
609
610 /* Map RX MTL to DMA channels */
611 uio_rx_queue_dma_chan_map(priv);
612
613 /* Enable MAC RX Queues */
614 uio_mac_enable_rx_queues(priv);
615
616 /* Set RX priorities */
617 if (rx_queues_count > 1)
618 uio_mac_config_rx_queues_prio(priv);
619
620 /* Set TX priorities */
621 if (tx_queues_count > 1)
622 uio_mac_config_tx_queues_prio(priv);
623
624 /* Set RX routing */
625 if (rx_queues_count > 1)
626 uio_mac_config_rx_queues_routing(priv);
627
628 /* Receive Side Scaling */
629 if (rx_queues_count > 1)
630 uio_mac_config_rss(priv);
631 }
632
uio_safety_feat_configuration(struct stmmac_priv * priv)633 static void uio_safety_feat_configuration(struct stmmac_priv *priv)
634 {
635 if (priv->dma_cap.asp) {
636 netdev_info(priv->dev, "Enabling Safety Features\n");
637 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp);
638 } else {
639 netdev_info(priv->dev, "No Safety Features support found\n");
640 }
641 }
642
643 /**
644 * uio_dma_operation_mode - HW DMA operation mode
645 * @priv: driver private structure
646 * Description: it is used for configuring the DMA operation mode register in
647 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
648 */
uio_dma_operation_mode(struct stmmac_priv * priv)649 static void uio_dma_operation_mode(struct stmmac_priv *priv)
650 {
651 u32 rx_channels_count = priv->plat->rx_queues_to_use;
652 u32 tx_channels_count = priv->plat->tx_queues_to_use;
653 int rxfifosz = priv->plat->rx_fifo_size;
654 int txfifosz = priv->plat->tx_fifo_size;
655 u32 txmode = 0;
656 u32 rxmode = 0;
657 u32 chan = 0;
658 u8 qmode = 0;
659
660 if (rxfifosz == 0)
661 rxfifosz = priv->dma_cap.rx_fifo_size;
662 if (txfifosz == 0)
663 txfifosz = priv->dma_cap.tx_fifo_size;
664
665 /* Adjust for real per queue fifo size */
666 rxfifosz /= rx_channels_count;
667 txfifosz /= tx_channels_count;
668
669 if (priv->plat->force_thresh_dma_mode) {
670 txmode = tc;
671 rxmode = tc;
672 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
673 /* In case of GMAC, SF mode can be enabled
674 * to perform the TX COE in HW. This depends on:
675 * 1) TX COE if actually supported
676 * 2) There is no bugged Jumbo frame support
677 * that needs to not insert csum in the TDES.
678 */
679 txmode = SF_DMA_MODE;
680 rxmode = SF_DMA_MODE;
681 priv->xstats.threshold = SF_DMA_MODE;
682 } else {
683 txmode = tc;
684 rxmode = SF_DMA_MODE;
685 }
686
687 /* configure all channels */
688 for (chan = 0; chan < rx_channels_count; chan++) {
689 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
690
691 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
692 rxfifosz, qmode);
693 stmmac_set_dma_bfsize(priv, priv->ioaddr, priv->dma_buf_sz,
694 chan);
695 }
696
697 for (chan = 0; chan < tx_channels_count; chan++) {
698 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
699
700 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
701 txfifosz, qmode);
702 }
703 }
704
705 /**
706 * rockchip_gmac_uio_hw_setup - setup mac in a usable state.
707 * @dev : pointer to the device structure.
708 * @init_ptp: initialize PTP if set
709 * Description:
710 * this is the main function to setup the HW in a usable state because the
711 * dma engine is reset, the core registers are configured (e.g. AXI,
712 * Checksum features, timers). The DMA is ready to start receiving and
713 * transmitting.
714 * Return value:
715 * 0 on success and an appropriate (-)ve integer as defined in errno.h
716 * file on failure.
717 */
rockchip_gmac_uio_hw_setup(struct net_device * dev,bool init_ptp)718 static int rockchip_gmac_uio_hw_setup(struct net_device *dev, bool init_ptp)
719 {
720 struct stmmac_priv *priv = netdev_priv(dev);
721 int ret;
722
723 /* DMA initialization and SW reset */
724 ret = rockchip_gmac_uio_init_dma_engine(priv);
725 if (ret < 0) {
726 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
727 __func__);
728 return ret;
729 }
730
731 /* Copy the MAC addr into the HW */
732 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
733
734 /* PS and related bits will be programmed according to the speed */
735 if (priv->hw->pcs) {
736 int speed = priv->plat->mac_port_sel_speed;
737
738 if (speed == SPEED_10 || speed == SPEED_100 ||
739 speed == SPEED_1000) {
740 priv->hw->ps = speed;
741 } else {
742 dev_warn(priv->device, "invalid port speed\n");
743 priv->hw->ps = 0;
744 }
745 }
746
747 /* Initialize the MAC Core */
748 stmmac_core_init(priv, priv->hw, dev);
749
750 /* Initialize MTL*/
751 rockchip_gmac_uio_mtl_configuration(priv);
752
753 /* Initialize Safety Features */
754 uio_safety_feat_configuration(priv);
755
756 ret = stmmac_rx_ipc(priv, priv->hw);
757 if (!ret) {
758 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
759 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
760 priv->hw->rx_csum = 0;
761 }
762
763 /* Enable the MAC Rx/Tx */
764 stmmac_mac_set(priv, priv->ioaddr, true);
765
766 /* Set the HW DMA mode and the COE */
767 uio_dma_operation_mode(priv);
768
769 if (priv->hw->pcs)
770 stmmac_pcs_ctrl_ane(priv, priv->hw, 1, priv->hw->ps, 0);
771
772 /* set TX and RX rings length */
773 uio_set_rings_length(priv);
774
775 return 0;
776 }
777
uio_set_bfsize(int mtu,int bufsize)778 static int uio_set_bfsize(int mtu, int bufsize)
779 {
780 int ret = bufsize;
781
782 if (mtu >= BUF_SIZE_8KiB)
783 ret = BUF_SIZE_16KiB;
784 else if (mtu >= BUF_SIZE_4KiB)
785 ret = BUF_SIZE_8KiB;
786 else if (mtu >= BUF_SIZE_2KiB)
787 ret = BUF_SIZE_4KiB;
788 else if (mtu > DEFAULT_BUFSIZE)
789 ret = BUF_SIZE_2KiB;
790 else
791 ret = DEFAULT_BUFSIZE;
792
793 return ret;
794 }
795
796 /**
797 * uio_open - open entry point of the driver
798 * @dev : pointer to the device structure.
799 * Description:
800 * This function is the open entry point of the driver.
801 * Return value:
802 * 0 on success and an appropriate (-)ve integer as defined in errno.h
803 * file on failure.
804 */
uio_open(struct net_device * dev)805 static int uio_open(struct net_device *dev)
806 {
807 struct stmmac_priv *priv = netdev_priv(dev);
808 int bfsize = 0;
809 int ret;
810
811 if (priv->hw->pcs != STMMAC_PCS_TBI &&
812 priv->hw->pcs != STMMAC_PCS_RTBI &&
813 !priv->hw->xpcs) {
814 ret = rockchip_gmac_uio_init_phy(dev);
815 if (ret) {
816 netdev_err(priv->dev,
817 "%s: Cannot attach to PHY (error: %d)\n",
818 __func__, ret);
819 return ret;
820 }
821 }
822
823 /* Extra statistics */
824 priv->xstats.threshold = tc;
825
826 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
827 if (bfsize < 0)
828 bfsize = 0;
829
830 if (bfsize < BUF_SIZE_16KiB)
831 bfsize = uio_set_bfsize(dev->mtu, priv->dma_buf_sz);
832
833 priv->dma_buf_sz = bfsize;
834 buf_sz = bfsize;
835
836 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
837
838 if (!priv->dma_tx_size)
839 priv->dma_tx_size = DMA_DEFAULT_TX_SIZE;
840 if (!priv->dma_rx_size)
841 priv->dma_rx_size = DMA_DEFAULT_RX_SIZE;
842
843 ret = uio_alloc_dma_desc_resources(priv);
844 if (ret < 0) {
845 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
846 __func__);
847 goto dma_desc_error;
848 }
849
850 ret = rockchip_gmac_uio_hw_setup(dev, true);
851 if (ret < 0) {
852 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
853 goto init_error;
854 }
855
856 phylink_start(priv->phylink);
857 /* We may have called phylink_speed_down before */
858 phylink_speed_up(priv->phylink);
859
860 return 0;
861
862 init_error:
863 uio_free_dma_desc_resources(priv);
864 dma_desc_error:
865 phylink_disconnect_phy(priv->phylink);
866 return ret;
867 }
868
869 /**
870 * uio_release - close entry point of the driver
871 * @dev : device pointer.
872 * Description:
873 * This is the stop entry point of the driver.
874 */
uio_release(struct net_device * dev)875 static int uio_release(struct net_device *dev)
876 {
877 struct stmmac_priv *priv = netdev_priv(dev);
878
879 /* Stop and disconnect the PHY */
880 if (dev->phydev) {
881 phy_stop(dev->phydev);
882 phy_disconnect(dev->phydev);
883 if (priv->plat->integrated_phy_power)
884 priv->plat->integrated_phy_power(priv->plat->bsp_priv,
885 false);
886 }
887
888 /* Release and free the Rx/Tx resources */
889 uio_free_dma_desc_resources(priv);
890
891 /* Disable the MAC Rx/Tx */
892 stmmac_mac_set(priv, priv->ioaddr, false);
893
894 netif_carrier_off(dev);
895
896 return 0;
897 }
898
899 /**
900 * rockchip_gmac_uio_probe() platform driver probe routine
901 * - register uio devices filled with memory maps retrieved
902 * from device tree
903 */
rockchip_gmac_uio_probe(struct platform_device * pdev)904 static int rockchip_gmac_uio_probe(struct platform_device *pdev)
905 {
906 struct device *dev = &pdev->dev;
907 struct device_node *np = dev->of_node, *mac_node;
908 struct rockchip_gmac_uio_pdev_info *pdev_info;
909 struct net_device *netdev;
910 struct stmmac_priv *priv;
911 struct uio_info *uio;
912 struct resource *res;
913 int err = 0;
914
915 pdev_info = devm_kzalloc(dev, sizeof(struct rockchip_gmac_uio_pdev_info),
916 GFP_KERNEL);
917 if (!pdev_info)
918 return -ENOMEM;
919
920 uio = &pdev_info->uio;
921 pdev_info->dev = dev;
922 mac_node = of_parse_phandle(np, "rockchip,ethernet", 0);
923 if (!mac_node)
924 return -ENODEV;
925
926 if (of_device_is_available(mac_node)) {
927 netdev = of_find_net_device_by_node(mac_node);
928 of_node_put(mac_node);
929 if (!netdev)
930 return -ENODEV;
931 } else {
932 of_node_put(mac_node);
933 return -EINVAL;
934 }
935
936 pdev_info->ndev = netdev;
937 rtnl_lock();
938 dev_close(netdev);
939 rtnl_unlock();
940
941 rtnl_lock();
942 err = uio_open(netdev);
943 if (err) {
944 rtnl_unlock();
945 dev_err(dev, "Failed to open stmmac resource: %d\n", err);
946 return err;
947 }
948 rtnl_unlock();
949
950 priv = netdev_priv(netdev);
951 snprintf(pdev_info->name, sizeof(pdev_info->name), "uio_%s",
952 netdev->name);
953 uio->name = pdev_info->name;
954 uio->version = DRIVER_VERSION;
955
956 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
957 if (!res)
958 return -ENODEV;
959
960 uio->mem[0].name = "eth_regs";
961 uio->mem[0].addr = res->start & PAGE_MASK;
962 uio->mem[0].size = PAGE_ALIGN(resource_size(res));
963 uio->mem[0].memtype = UIO_MEM_PHYS;
964
965 uio->mem[1].name = "eth_rx_bd";
966 uio->mem[1].addr = priv->rx_queue[0].dma_rx_phy;
967 uio->mem[1].size = priv->dma_rx_size * sizeof(struct dma_desc);
968 uio->mem[1].memtype = UIO_MEM_PHYS;
969
970 uio->mem[2].name = "eth_tx_bd";
971 uio->mem[2].addr = priv->tx_queue[0].dma_tx_phy;
972 uio->mem[2].size = priv->dma_tx_size * sizeof(struct dma_desc);
973 uio->mem[2].memtype = UIO_MEM_PHYS;
974
975 uio->open = rockchip_gmac_uio_open;
976 uio->release = rockchip_gmac_uio_release;
977 /* Custom mmap function. */
978 uio->mmap = rockchip_gmac_uio_mmap;
979 uio->priv = pdev_info;
980
981 err = uio_register_device(dev, uio);
982 if (err) {
983 dev_err(dev, "Failed to register uio device: %d\n", err);
984 return err;
985 }
986
987 pdev_info->map_num = 3;
988
989 dev_info(dev, "Registered %s uio devices, %d register maps attached\n",
990 pdev_info->name, pdev_info->map_num);
991
992 platform_set_drvdata(pdev, pdev_info);
993
994 return 0;
995 }
996
997 /**
998 * rockchip_gmac_uio_remove() - ROCKCHIP ETH UIO platform driver release
999 * routine - unregister uio devices
1000 */
rockchip_gmac_uio_remove(struct platform_device * pdev)1001 static int rockchip_gmac_uio_remove(struct platform_device *pdev)
1002 {
1003 struct rockchip_gmac_uio_pdev_info *pdev_info =
1004 platform_get_drvdata(pdev);
1005 struct net_device *netdev;
1006
1007 if (!pdev_info)
1008 return -EINVAL;
1009
1010 netdev = pdev_info->ndev;
1011
1012 uio_unregister_device(&pdev_info->uio);
1013
1014 if (netdev) {
1015 rtnl_lock();
1016 uio_release(netdev);
1017 rtnl_unlock();
1018 }
1019
1020 platform_set_drvdata(pdev, NULL);
1021
1022 if (netdev) {
1023 rtnl_lock();
1024 dev_open(netdev, NULL);
1025 rtnl_unlock();
1026 }
1027
1028 return 0;
1029 }
1030
1031 static const struct of_device_id rockchip_gmac_uio_of_match[] = {
1032 { .compatible = "rockchip,uio-gmac", },
1033 { }
1034 };
1035
1036 static struct platform_driver rockchip_gmac_uio_driver = {
1037 .driver = {
1038 .owner = THIS_MODULE,
1039 .name = DRIVER_NAME,
1040 .of_match_table = rockchip_gmac_uio_of_match,
1041 },
1042 .probe = rockchip_gmac_uio_probe,
1043 .remove = rockchip_gmac_uio_remove,
1044 };
1045
1046 module_platform_driver(rockchip_gmac_uio_driver);
1047
1048 MODULE_LICENSE("GPL");
1049 MODULE_AUTHOR("ROCKCHIP");
1050 MODULE_DESCRIPTION("ROCKCHIP GMAC UIO Driver");
1051