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