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