1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117#include <linux/module.h>
118#include <linux/device.h>
119#include <linux/platform_device.h>
120#include <linux/spinlock.h>
121#include <linux/netdevice.h>
122#include <linux/etherdevice.h>
123#include <linux/io.h>
124#include <linux/of.h>
125#include <linux/of_net.h>
126#include <linux/of_address.h>
127#include <linux/of_platform.h>
128#include <linux/of_device.h>
129#include <linux/clk.h>
130#include <linux/property.h>
131#include <linux/acpi.h>
132#include <linux/mdio.h>
133
134#include "xgbe.h"
135#include "xgbe-common.h"
136
137#ifdef CONFIG_ACPI
138static const struct acpi_device_id xgbe_acpi_match[];
139
140static struct xgbe_version_data *xgbe_acpi_vdata(struct xgbe_prv_data *pdata)
141{
142 const struct acpi_device_id *id;
143
144 id = acpi_match_device(xgbe_acpi_match, pdata->dev);
145
146 return id ? (struct xgbe_version_data *)id->driver_data : NULL;
147}
148
149static int xgbe_acpi_support(struct xgbe_prv_data *pdata)
150{
151 struct device *dev = pdata->dev;
152 u32 property;
153 int ret;
154
155
156 ret = device_property_read_u32(dev, XGBE_ACPI_DMA_FREQ, &property);
157 if (ret) {
158 dev_err(dev, "unable to obtain %s property\n",
159 XGBE_ACPI_DMA_FREQ);
160 return ret;
161 }
162 pdata->sysclk_rate = property;
163
164
165 ret = device_property_read_u32(dev, XGBE_ACPI_PTP_FREQ, &property);
166 if (ret) {
167 dev_err(dev, "unable to obtain %s property\n",
168 XGBE_ACPI_PTP_FREQ);
169 return ret;
170 }
171 pdata->ptpclk_rate = property;
172
173 return 0;
174}
175#else
176static struct xgbe_version_data *xgbe_acpi_vdata(struct xgbe_prv_data *pdata)
177{
178 return NULL;
179}
180
181static int xgbe_acpi_support(struct xgbe_prv_data *pdata)
182{
183 return -EINVAL;
184}
185#endif
186
187#ifdef CONFIG_OF
188static const struct of_device_id xgbe_of_match[];
189
190static struct xgbe_version_data *xgbe_of_vdata(struct xgbe_prv_data *pdata)
191{
192 const struct of_device_id *id;
193
194 id = of_match_device(xgbe_of_match, pdata->dev);
195
196 return id ? (struct xgbe_version_data *)id->data : NULL;
197}
198
199static int xgbe_of_support(struct xgbe_prv_data *pdata)
200{
201 struct device *dev = pdata->dev;
202
203
204 pdata->sysclk = devm_clk_get(dev, XGBE_DMA_CLOCK);
205 if (IS_ERR(pdata->sysclk)) {
206 dev_err(dev, "dma devm_clk_get failed\n");
207 return PTR_ERR(pdata->sysclk);
208 }
209 pdata->sysclk_rate = clk_get_rate(pdata->sysclk);
210
211
212 pdata->ptpclk = devm_clk_get(dev, XGBE_PTP_CLOCK);
213 if (IS_ERR(pdata->ptpclk)) {
214 dev_err(dev, "ptp devm_clk_get failed\n");
215 return PTR_ERR(pdata->ptpclk);
216 }
217 pdata->ptpclk_rate = clk_get_rate(pdata->ptpclk);
218
219 return 0;
220}
221
222static struct platform_device *xgbe_of_get_phy_pdev(struct xgbe_prv_data *pdata)
223{
224 struct device *dev = pdata->dev;
225 struct device_node *phy_node;
226 struct platform_device *phy_pdev;
227
228 phy_node = of_parse_phandle(dev->of_node, "phy-handle", 0);
229 if (phy_node) {
230
231
232
233 phy_pdev = of_find_device_by_node(phy_node);
234 of_node_put(phy_node);
235 } else {
236
237
238
239
240 get_device(dev);
241 phy_pdev = pdata->platdev;
242 }
243
244 return phy_pdev;
245}
246#else
247static struct xgbe_version_data *xgbe_of_vdata(struct xgbe_prv_data *pdata)
248{
249 return NULL;
250}
251
252static int xgbe_of_support(struct xgbe_prv_data *pdata)
253{
254 return -EINVAL;
255}
256
257static struct platform_device *xgbe_of_get_phy_pdev(struct xgbe_prv_data *pdata)
258{
259 return NULL;
260}
261#endif
262
263static unsigned int xgbe_resource_count(struct platform_device *pdev,
264 unsigned int type)
265{
266 unsigned int count;
267 int i;
268
269 for (i = 0, count = 0; i < pdev->num_resources; i++) {
270 struct resource *res = &pdev->resource[i];
271
272 if (type == resource_type(res))
273 count++;
274 }
275
276 return count;
277}
278
279static struct platform_device *xgbe_get_phy_pdev(struct xgbe_prv_data *pdata)
280{
281 struct platform_device *phy_pdev;
282
283 if (pdata->use_acpi) {
284 get_device(pdata->dev);
285 phy_pdev = pdata->platdev;
286 } else {
287 phy_pdev = xgbe_of_get_phy_pdev(pdata);
288 }
289
290 return phy_pdev;
291}
292
293static struct xgbe_version_data *xgbe_get_vdata(struct xgbe_prv_data *pdata)
294{
295 return pdata->use_acpi ? xgbe_acpi_vdata(pdata)
296 : xgbe_of_vdata(pdata);
297}
298
299static int xgbe_platform_probe(struct platform_device *pdev)
300{
301 struct xgbe_prv_data *pdata;
302 struct device *dev = &pdev->dev;
303 struct platform_device *phy_pdev;
304 struct resource *res;
305 const char *phy_mode;
306 unsigned int phy_memnum, phy_irqnum;
307 unsigned int dma_irqnum, dma_irqend;
308 enum dev_dma_attr attr;
309 int ret;
310
311 pdata = xgbe_alloc_pdata(dev);
312 if (IS_ERR(pdata)) {
313 ret = PTR_ERR(pdata);
314 goto err_alloc;
315 }
316
317 pdata->platdev = pdev;
318 pdata->adev = ACPI_COMPANION(dev);
319 platform_set_drvdata(pdev, pdata);
320
321
322 pdata->use_acpi = dev->of_node ? 0 : 1;
323
324
325 pdata->vdata = xgbe_get_vdata(pdata);
326
327 phy_pdev = xgbe_get_phy_pdev(pdata);
328 if (!phy_pdev) {
329 dev_err(dev, "unable to obtain phy device\n");
330 ret = -EINVAL;
331 goto err_phydev;
332 }
333 pdata->phy_platdev = phy_pdev;
334 pdata->phy_dev = &phy_pdev->dev;
335
336 if (pdev == phy_pdev) {
337
338
339
340
341 phy_memnum = xgbe_resource_count(pdev, IORESOURCE_MEM) - 3;
342 phy_irqnum = xgbe_resource_count(pdev, IORESOURCE_IRQ) - 1;
343 dma_irqnum = 1;
344 dma_irqend = phy_irqnum;
345 } else {
346
347
348
349 phy_memnum = 0;
350 phy_irqnum = 0;
351 dma_irqnum = 1;
352 dma_irqend = xgbe_resource_count(pdev, IORESOURCE_IRQ);
353 }
354
355
356 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
357 pdata->xgmac_regs = devm_ioremap_resource(dev, res);
358 if (IS_ERR(pdata->xgmac_regs)) {
359 dev_err(dev, "xgmac ioremap failed\n");
360 ret = PTR_ERR(pdata->xgmac_regs);
361 goto err_io;
362 }
363 if (netif_msg_probe(pdata))
364 dev_dbg(dev, "xgmac_regs = %p\n", pdata->xgmac_regs);
365
366 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
367 pdata->xpcs_regs = devm_ioremap_resource(dev, res);
368 if (IS_ERR(pdata->xpcs_regs)) {
369 dev_err(dev, "xpcs ioremap failed\n");
370 ret = PTR_ERR(pdata->xpcs_regs);
371 goto err_io;
372 }
373 if (netif_msg_probe(pdata))
374 dev_dbg(dev, "xpcs_regs = %p\n", pdata->xpcs_regs);
375
376 res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
377 pdata->rxtx_regs = devm_ioremap_resource(dev, res);
378 if (IS_ERR(pdata->rxtx_regs)) {
379 dev_err(dev, "rxtx ioremap failed\n");
380 ret = PTR_ERR(pdata->rxtx_regs);
381 goto err_io;
382 }
383 if (netif_msg_probe(pdata))
384 dev_dbg(dev, "rxtx_regs = %p\n", pdata->rxtx_regs);
385
386 res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
387 pdata->sir0_regs = devm_ioremap_resource(dev, res);
388 if (IS_ERR(pdata->sir0_regs)) {
389 dev_err(dev, "sir0 ioremap failed\n");
390 ret = PTR_ERR(pdata->sir0_regs);
391 goto err_io;
392 }
393 if (netif_msg_probe(pdata))
394 dev_dbg(dev, "sir0_regs = %p\n", pdata->sir0_regs);
395
396 res = platform_get_resource(phy_pdev, IORESOURCE_MEM, phy_memnum++);
397 pdata->sir1_regs = devm_ioremap_resource(dev, res);
398 if (IS_ERR(pdata->sir1_regs)) {
399 dev_err(dev, "sir1 ioremap failed\n");
400 ret = PTR_ERR(pdata->sir1_regs);
401 goto err_io;
402 }
403 if (netif_msg_probe(pdata))
404 dev_dbg(dev, "sir1_regs = %p\n", pdata->sir1_regs);
405
406
407 ret = device_property_read_u8_array(dev, XGBE_MAC_ADDR_PROPERTY,
408 pdata->mac_addr,
409 sizeof(pdata->mac_addr));
410 if (ret || !is_valid_ether_addr(pdata->mac_addr)) {
411 dev_err(dev, "invalid %s property\n", XGBE_MAC_ADDR_PROPERTY);
412 if (!ret)
413 ret = -EINVAL;
414 goto err_io;
415 }
416
417
418 ret = device_property_read_string(dev, XGBE_PHY_MODE_PROPERTY,
419 &phy_mode);
420 if (ret || strcmp(phy_mode, phy_modes(PHY_INTERFACE_MODE_XGMII))) {
421 dev_err(dev, "invalid %s property\n", XGBE_PHY_MODE_PROPERTY);
422 if (!ret)
423 ret = -EINVAL;
424 goto err_io;
425 }
426 pdata->phy_mode = PHY_INTERFACE_MODE_XGMII;
427
428
429 if (device_property_present(dev, XGBE_DMA_IRQS_PROPERTY)) {
430 pdata->per_channel_irq = 1;
431 pdata->channel_irq_mode = XGBE_IRQ_MODE_EDGE;
432 }
433
434
435 if (pdata->use_acpi)
436 ret = xgbe_acpi_support(pdata);
437 else
438 ret = xgbe_of_support(pdata);
439 if (ret)
440 goto err_io;
441
442
443 attr = device_get_dma_attr(dev);
444 if (attr == DEV_DMA_NOT_SUPPORTED) {
445 dev_err(dev, "DMA is not supported");
446 ret = -ENODEV;
447 goto err_io;
448 }
449 pdata->coherent = (attr == DEV_DMA_COHERENT);
450 if (pdata->coherent) {
451 pdata->arcr = XGBE_DMA_OS_ARCR;
452 pdata->awcr = XGBE_DMA_OS_AWCR;
453 } else {
454 pdata->arcr = XGBE_DMA_SYS_ARCR;
455 pdata->awcr = XGBE_DMA_SYS_AWCR;
456 }
457
458
459 pdata->tx_max_fifo_size = pdata->vdata->tx_max_fifo_size;
460 pdata->rx_max_fifo_size = pdata->vdata->rx_max_fifo_size;
461
462
463 xgbe_set_counts(pdata);
464
465
466 pdata->irq_count = 2;
467
468
469 ret = platform_get_irq(pdev, 0);
470 if (ret < 0) {
471 dev_err(dev, "platform_get_irq 0 failed\n");
472 goto err_io;
473 }
474 pdata->dev_irq = ret;
475
476
477 if (pdata->per_channel_irq) {
478 unsigned int i, max = ARRAY_SIZE(pdata->channel_irq);
479
480 for (i = 0; (i < max) && (dma_irqnum < dma_irqend); i++) {
481 ret = platform_get_irq(pdata->platdev, dma_irqnum++);
482 if (ret < 0) {
483 netdev_err(pdata->netdev,
484 "platform_get_irq %u failed\n",
485 dma_irqnum - 1);
486 goto err_io;
487 }
488
489 pdata->channel_irq[i] = ret;
490 }
491
492 pdata->channel_irq_count = max;
493
494 pdata->irq_count += max;
495 }
496
497
498 ret = platform_get_irq(phy_pdev, phy_irqnum++);
499 if (ret < 0) {
500 dev_err(dev, "platform_get_irq phy 0 failed\n");
501 goto err_io;
502 }
503 pdata->an_irq = ret;
504
505
506 ret = xgbe_config_netdev(pdata);
507 if (ret)
508 goto err_io;
509
510 netdev_notice(pdata->netdev, "net device enabled\n");
511
512 return 0;
513
514err_io:
515 platform_device_put(phy_pdev);
516
517err_phydev:
518 xgbe_free_pdata(pdata);
519
520err_alloc:
521 dev_notice(dev, "net device not enabled\n");
522
523 return ret;
524}
525
526static int xgbe_platform_remove(struct platform_device *pdev)
527{
528 struct xgbe_prv_data *pdata = platform_get_drvdata(pdev);
529
530 xgbe_deconfig_netdev(pdata);
531
532 platform_device_put(pdata->phy_platdev);
533
534 xgbe_free_pdata(pdata);
535
536 return 0;
537}
538
539#ifdef CONFIG_PM_SLEEP
540static int xgbe_platform_suspend(struct device *dev)
541{
542 struct xgbe_prv_data *pdata = dev_get_drvdata(dev);
543 struct net_device *netdev = pdata->netdev;
544 int ret = 0;
545
546 DBGPR("-->xgbe_suspend\n");
547
548 if (netif_running(netdev))
549 ret = xgbe_powerdown(netdev, XGMAC_DRIVER_CONTEXT);
550
551 pdata->lpm_ctrl = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
552 pdata->lpm_ctrl |= MDIO_CTRL1_LPOWER;
553 XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
554
555 DBGPR("<--xgbe_suspend\n");
556
557 return ret;
558}
559
560static int xgbe_platform_resume(struct device *dev)
561{
562 struct xgbe_prv_data *pdata = dev_get_drvdata(dev);
563 struct net_device *netdev = pdata->netdev;
564 int ret = 0;
565
566 DBGPR("-->xgbe_resume\n");
567
568 pdata->lpm_ctrl &= ~MDIO_CTRL1_LPOWER;
569 XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
570
571 if (netif_running(netdev)) {
572 ret = xgbe_powerup(netdev, XGMAC_DRIVER_CONTEXT);
573
574
575
576
577 schedule_work(&pdata->restart_work);
578 }
579
580 DBGPR("<--xgbe_resume\n");
581
582 return ret;
583}
584#endif
585
586static const struct xgbe_version_data xgbe_v1 = {
587 .init_function_ptrs_phy_impl = xgbe_init_function_ptrs_phy_v1,
588 .xpcs_access = XGBE_XPCS_ACCESS_V1,
589 .tx_max_fifo_size = 81920,
590 .rx_max_fifo_size = 81920,
591 .tx_tstamp_workaround = 1,
592};
593
594#ifdef CONFIG_ACPI
595static const struct acpi_device_id xgbe_acpi_match[] = {
596 { .id = "AMDI8001",
597 .driver_data = (kernel_ulong_t)&xgbe_v1 },
598 {},
599};
600
601MODULE_DEVICE_TABLE(acpi, xgbe_acpi_match);
602#endif
603
604#ifdef CONFIG_OF
605static const struct of_device_id xgbe_of_match[] = {
606 { .compatible = "amd,xgbe-seattle-v1a",
607 .data = &xgbe_v1 },
608 {},
609};
610
611MODULE_DEVICE_TABLE(of, xgbe_of_match);
612#endif
613
614static SIMPLE_DEV_PM_OPS(xgbe_platform_pm_ops,
615 xgbe_platform_suspend, xgbe_platform_resume);
616
617static struct platform_driver xgbe_driver = {
618 .driver = {
619 .name = XGBE_DRV_NAME,
620#ifdef CONFIG_ACPI
621 .acpi_match_table = xgbe_acpi_match,
622#endif
623#ifdef CONFIG_OF
624 .of_match_table = xgbe_of_match,
625#endif
626 .pm = &xgbe_platform_pm_ops,
627 },
628 .probe = xgbe_platform_probe,
629 .remove = xgbe_platform_remove,
630};
631
632int xgbe_platform_init(void)
633{
634 return platform_driver_register(&xgbe_driver);
635}
636
637void xgbe_platform_exit(void)
638{
639 platform_driver_unregister(&xgbe_driver);
640}
641