1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sni_82596.c -- driver for intel 82596 ethernet controller, as
4  *  		  used in older SNI RM machines
5  */
6 
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/string.h>
10 #include <linux/errno.h>
11 #include <linux/ioport.h>
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/skbuff.h>
17 #include <linux/types.h>
18 #include <linux/bitops.h>
19 #include <linux/platform_device.h>
20 #include <linux/io.h>
21 #include <linux/irq.h>
22 
23 #define SNI_82596_DRIVER_VERSION "SNI RM 82596 driver - Revision: 0.01"
24 
25 static const char sni_82596_string[] = "snirm_82596";
26 
27 #define SYSBUS      0x00004400
28 
29 /* big endian CPU, 82596 little endian */
30 #define SWAP32(x)   cpu_to_le32((u32)(x))
31 #define SWAP16(x)   cpu_to_le16((u16)(x))
32 
33 #define OPT_MPU_16BIT    0x01
34 
35 #include "lib82596.c"
36 
37 MODULE_AUTHOR("Thomas Bogendoerfer");
38 MODULE_DESCRIPTION("i82596 driver");
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS("platform:snirm_82596");
41 module_param(i596_debug, int, 0);
42 MODULE_PARM_DESC(i596_debug, "82596 debug mask");
43 
44 static inline void ca(struct net_device *dev)
45 {
46 	struct i596_private *lp = netdev_priv(dev);
47 
48 	writel(0, lp->ca);
49 }
50 
51 
52 static void mpu_port(struct net_device *dev, int c, dma_addr_t x)
53 {
54 	struct i596_private *lp = netdev_priv(dev);
55 
56 	u32 v = (u32) (c) | (u32) (x);
57 
58 	if (lp->options & OPT_MPU_16BIT) {
59 		writew(v & 0xffff, lp->mpu_port);
60 		wmb();  /* order writes to MPU port */
61 		udelay(1);
62 		writew(v >> 16, lp->mpu_port);
63 	} else {
64 		writel(v, lp->mpu_port);
65 		wmb();  /* order writes to MPU port */
66 		udelay(1);
67 		writel(v, lp->mpu_port);
68 	}
69 }
70 
71 
72 static int sni_82596_probe(struct platform_device *dev)
73 {
74 	struct	net_device *netdevice;
75 	struct i596_private *lp;
76 	struct  resource *res, *ca, *idprom, *options;
77 	int	retval = -ENOMEM;
78 	void __iomem *mpu_addr;
79 	void __iomem *ca_addr;
80 	u8 __iomem *eth_addr;
81 
82 	res = platform_get_resource(dev, IORESOURCE_MEM, 0);
83 	ca = platform_get_resource(dev, IORESOURCE_MEM, 1);
84 	options = platform_get_resource(dev, 0, 0);
85 	idprom = platform_get_resource(dev, IORESOURCE_MEM, 2);
86 	if (!res || !ca || !options || !idprom)
87 		return -ENODEV;
88 	mpu_addr = ioremap(res->start, 4);
89 	if (!mpu_addr)
90 		return -ENOMEM;
91 	ca_addr = ioremap(ca->start, 4);
92 	if (!ca_addr)
93 		goto probe_failed_free_mpu;
94 
95 	printk(KERN_INFO "Found i82596 at 0x%x\n", res->start);
96 
97 	netdevice = alloc_etherdev(sizeof(struct i596_private));
98 	if (!netdevice)
99 		goto probe_failed_free_ca;
100 
101 	SET_NETDEV_DEV(netdevice, &dev->dev);
102 	platform_set_drvdata (dev, netdevice);
103 
104 	netdevice->base_addr = res->start;
105 	netdevice->irq = platform_get_irq(dev, 0);
106 
107 	eth_addr = ioremap(idprom->start, 0x10);
108 	if (!eth_addr)
109 		goto probe_failed;
110 
111 	/* someone seems to like messed up stuff */
112 	netdevice->dev_addr[0] = readb(eth_addr + 0x0b);
113 	netdevice->dev_addr[1] = readb(eth_addr + 0x0a);
114 	netdevice->dev_addr[2] = readb(eth_addr + 0x09);
115 	netdevice->dev_addr[3] = readb(eth_addr + 0x08);
116 	netdevice->dev_addr[4] = readb(eth_addr + 0x07);
117 	netdevice->dev_addr[5] = readb(eth_addr + 0x06);
118 	iounmap(eth_addr);
119 
120 	if (netdevice->irq < 0) {
121 		printk(KERN_ERR "%s: IRQ not found for i82596 at 0x%lx\n",
122 			__FILE__, netdevice->base_addr);
123 		retval = netdevice->irq;
124 		goto probe_failed;
125 	}
126 
127 	lp = netdev_priv(netdevice);
128 	lp->options = options->flags & IORESOURCE_BITS;
129 	lp->ca = ca_addr;
130 	lp->mpu_port = mpu_addr;
131 
132 	lp->dma = dma_alloc_coherent(&dev->dev, sizeof(struct i596_dma),
133 				     &lp->dma_addr, GFP_KERNEL);
134 	if (!lp->dma)
135 		goto probe_failed;
136 
137 	retval = i82596_probe(netdevice);
138 	if (retval)
139 		goto probe_failed_free_dma;
140 	return 0;
141 
142 probe_failed_free_dma:
143 	dma_free_coherent(&dev->dev, sizeof(struct i596_dma), lp->dma,
144 			  lp->dma_addr);
145 probe_failed:
146 	free_netdev(netdevice);
147 probe_failed_free_ca:
148 	iounmap(ca_addr);
149 probe_failed_free_mpu:
150 	iounmap(mpu_addr);
151 	return retval;
152 }
153 
154 static int sni_82596_driver_remove(struct platform_device *pdev)
155 {
156 	struct net_device *dev = platform_get_drvdata(pdev);
157 	struct i596_private *lp = netdev_priv(dev);
158 
159 	unregister_netdev(dev);
160 	dma_free_coherent(&pdev->dev, sizeof(struct i596_private), lp->dma,
161 			  lp->dma_addr);
162 	iounmap(lp->ca);
163 	iounmap(lp->mpu_port);
164 	free_netdev (dev);
165 	return 0;
166 }
167 
168 static struct platform_driver sni_82596_driver = {
169 	.probe	= sni_82596_probe,
170 	.remove	= sni_82596_driver_remove,
171 	.driver	= {
172 		.name	= sni_82596_string,
173 	},
174 };
175 
176 static int sni_82596_init(void)
177 {
178 	printk(KERN_INFO SNI_82596_DRIVER_VERSION "\n");
179 	return platform_driver_register(&sni_82596_driver);
180 }
181 
182 
183 static void __exit sni_82596_exit(void)
184 {
185 	platform_driver_unregister(&sni_82596_driver);
186 }
187 
188 module_init(sni_82596_init);
189 module_exit(sni_82596_exit);
190