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