1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * drivers/net/ethernet/ibm/emac/tah.c
4 *
5 * Driver for PowerPC 4xx on-chip ethernet controller, TAH support.
6 *
7 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
8 * <benh@kernel.crashing.org>
9 *
10 * Based on the arch/ppc version of the driver:
11 *
12 * Copyright 2004 MontaVista Software, Inc.
13 * Matt Porter <mporter@kernel.crashing.org>
14 *
15 * Copyright (c) 2005 Eugene Surovegin <ebs@ebshome.net>
16 */
17 #include <linux/of_address.h>
18 #include <linux/platform_device.h>
19 #include <asm/io.h>
20
21 #include "emac.h"
22 #include "core.h"
23
tah_attach(struct platform_device * ofdev,int channel)24 int tah_attach(struct platform_device *ofdev, int channel)
25 {
26 struct tah_instance *dev = platform_get_drvdata(ofdev);
27
28 mutex_lock(&dev->lock);
29 /* Reset has been done at probe() time... nothing else to do for now */
30 ++dev->users;
31 mutex_unlock(&dev->lock);
32
33 return 0;
34 }
35
tah_detach(struct platform_device * ofdev,int channel)36 void tah_detach(struct platform_device *ofdev, int channel)
37 {
38 struct tah_instance *dev = platform_get_drvdata(ofdev);
39
40 mutex_lock(&dev->lock);
41 --dev->users;
42 mutex_unlock(&dev->lock);
43 }
44
tah_reset(struct platform_device * ofdev)45 void tah_reset(struct platform_device *ofdev)
46 {
47 struct tah_instance *dev = platform_get_drvdata(ofdev);
48 struct tah_regs __iomem *p = dev->base;
49 int n;
50
51 /* Reset TAH */
52 out_be32(&p->mr, TAH_MR_SR);
53 n = 100;
54 while ((in_be32(&p->mr) & TAH_MR_SR) && n)
55 --n;
56
57 if (unlikely(!n))
58 printk(KERN_ERR "%pOF: reset timeout\n", ofdev->dev.of_node);
59
60 /* 10KB TAH TX FIFO accommodates the max MTU of 9000 */
61 out_be32(&p->mr,
62 TAH_MR_CVR | TAH_MR_ST_768 | TAH_MR_TFS_10KB | TAH_MR_DTFP |
63 TAH_MR_DIG);
64 }
65
tah_get_regs_len(struct platform_device * ofdev)66 int tah_get_regs_len(struct platform_device *ofdev)
67 {
68 return sizeof(struct emac_ethtool_regs_subhdr) +
69 sizeof(struct tah_regs);
70 }
71
tah_dump_regs(struct platform_device * ofdev,void * buf)72 void *tah_dump_regs(struct platform_device *ofdev, void *buf)
73 {
74 struct tah_instance *dev = platform_get_drvdata(ofdev);
75 struct emac_ethtool_regs_subhdr *hdr = buf;
76 struct tah_regs *regs = (struct tah_regs *)(hdr + 1);
77
78 hdr->version = 0;
79 hdr->index = 0; /* for now, are there chips with more than one
80 * zmii ? if yes, then we'll add a cell_index
81 * like we do for emac
82 */
83 memcpy_fromio(regs, dev->base, sizeof(struct tah_regs));
84 return regs + 1;
85 }
86
tah_probe(struct platform_device * ofdev)87 static int tah_probe(struct platform_device *ofdev)
88 {
89 struct tah_instance *dev;
90 int err;
91
92 dev = devm_kzalloc(&ofdev->dev, sizeof(struct tah_instance),
93 GFP_KERNEL);
94 if (!dev)
95 return -ENOMEM;
96
97 err = devm_mutex_init(&ofdev->dev, &dev->lock);
98 if (err)
99 return err;
100
101 dev->ofdev = ofdev;
102
103 dev->base = devm_platform_ioremap_resource(ofdev, 0);
104 if (IS_ERR(dev->base)) {
105 dev_err(&ofdev->dev, "can't map device registers");
106 return PTR_ERR(dev->base);
107 }
108
109 platform_set_drvdata(ofdev, dev);
110
111 /* Initialize TAH and enable IPv4 checksum verification, no TSO yet */
112 tah_reset(ofdev);
113
114 printk(KERN_INFO "TAH %pOF initialized\n", ofdev->dev.of_node);
115 wmb();
116
117 return 0;
118 }
119
120 static const struct of_device_id tah_match[] =
121 {
122 {
123 .compatible = "ibm,tah",
124 },
125 /* For backward compat with old DT */
126 {
127 .type = "tah",
128 },
129 {},
130 };
131
132 static struct platform_driver tah_driver = {
133 .driver = {
134 .name = "emac-tah",
135 .of_match_table = tah_match,
136 },
137 .probe = tah_probe,
138 };
139
tah_init(void)140 int __init tah_init(void)
141 {
142 return platform_driver_register(&tah_driver);
143 }
144
tah_exit(void)145 void tah_exit(void)
146 {
147 platform_driver_unregister(&tah_driver);
148 }
149