1 /* lasi_82596.c -- driver for the intel 82596 ethernet controller, as
2    munged into HPPA boxen .
3 
4    This driver is based upon 82596.c, original credits are below...
5    but there were too many hoops which HP wants jumped through to
6    keep this code in there in a sane manner.
7 
8    3 primary sources of the mess --
9    1) hppa needs *lots* of cacheline flushing to keep this kind of
10    MMIO running.
11 
12    2) The 82596 needs to see all of its pointers as their physical
13    address.  Thus virt_to_bus/bus_to_virt are *everywhere*.
14 
15    3) The implementation HP is using seems to be significantly pickier
16    about when and how the command and RX units are started.  some
17    command ordering was changed.
18 
19    Examination of the mach driver leads one to believe that there
20    might be a saner way to pull this off...  anyone who feels like a
21    full rewrite can be my guest.
22 
23    Split 02/13/2000 Sam Creasey (sammy@oh.verio.com)
24 
25    02/01/2000  Initial modifications for parisc by Helge Deller (deller@gmx.de)
26    03/02/2000  changes for better/correct(?) cache-flushing (deller)
27 */
28 
29 /* 82596.c: A generic 82596 ethernet driver for linux. */
30 /*
31    Based on Apricot.c
32    Written 1994 by Mark Evans.
33    This driver is for the Apricot 82596 bus-master interface
34 
35    Modularised 12/94 Mark Evans
36 
37 
38    Modified to support the 82596 ethernet chips on 680x0 VME boards.
39    by Richard Hirst <richard@sleepie.demon.co.uk>
40    Renamed to be 82596.c
41 
42    980825:  Changed to receive directly in to sk_buffs which are
43    allocated at open() time.  Eliminates copy on incoming frames
44    (small ones are still copied).  Shared data now held in a
45    non-cached page, so we can run on 68060 in copyback mode.
46 
47    TBD:
48    * look at deferring rx frames rather than discarding (as per tulip)
49    * handle tx ring full as per tulip
50    * performance test to tune rx_copybreak
51 
52    Most of my modifications relate to the braindead big-endian
53    implementation by Intel.  When the i596 is operating in
54    'big-endian' mode, it thinks a 32 bit value of 0x12345678
55    should be stored as 0x56781234.  This is a real pain, when
56    you have linked lists which are shared by the 680x0 and the
57    i596.
58 
59    Driver skeleton
60    Written 1993 by Donald Becker.
61    Copyright 1993 United States Government as represented by the Director,
62    National Security Agency. This software may only be used and distributed
63    according to the terms of the GNU General Public License as modified by SRC,
64    incorporated herein by reference.
65 
66    The author may be reached as becker@scyld.com, or C/O
67    Scyld Computing Corporation, 410 Severn Ave., Suite 210, Annapolis MD 21403
68 
69  */
70 
71 #include <linux/module.h>
72 #include <linux/kernel.h>
73 #include <linux/string.h>
74 #include <linux/ptrace.h>
75 #include <linux/errno.h>
76 #include <linux/ioport.h>
77 #include <linux/interrupt.h>
78 #include <linux/delay.h>
79 #include <linux/netdevice.h>
80 #include <linux/etherdevice.h>
81 #include <linux/skbuff.h>
82 #include <linux/types.h>
83 #include <linux/bitops.h>
84 #include <linux/dma-mapping.h>
85 
86 #include <asm/io.h>
87 #include <asm/irq.h>
88 #include <asm/pdc.h>
89 #include <asm/parisc-device.h>
90 
91 #define LASI_82596_DRIVER_VERSION "LASI 82596 driver - Revision: 1.30"
92 
93 #define PA_I82596_RESET		0	/* Offsets relative to LASI-LAN-Addr.*/
94 #define PA_CPU_PORT_L_ACCESS	4
95 #define PA_CHANNEL_ATTENTION	8
96 
97 #define OPT_SWAP_PORT	0x0001	/* Need to wordswp on the MPU port */
98 
99 #define SYSBUS      0x0000006c
100 
101 /* big endian CPU, 82596 "big" endian mode */
102 #define SWAP32(x)   (((u32)(x)<<16) | ((((u32)(x)))>>16))
103 #define SWAP16(x)   (x)
104 
105 #define NONCOHERENT_DMA 1
106 
107 #include "lib82596.c"
108 
109 MODULE_AUTHOR("Richard Hirst");
110 MODULE_DESCRIPTION("i82596 driver");
111 MODULE_LICENSE("GPL");
112 module_param(i596_debug, int, 0);
113 MODULE_PARM_DESC(i596_debug, "lasi_82596 debug mask");
114 
115 static inline void ca(struct net_device *dev)
116 {
117 	gsc_writel(0, dev->base_addr + PA_CHANNEL_ATTENTION);
118 }
119 
120 
121 static void mpu_port(struct net_device *dev, int c, dma_addr_t x)
122 {
123 	struct i596_private *lp = netdev_priv(dev);
124 
125 	u32 v = (u32) (c) | (u32) (x);
126 	u16 a, b;
127 
128 	if (lp->options & OPT_SWAP_PORT) {
129 		a = v >> 16;
130 		b = v & 0xffff;
131 	} else {
132 		a = v & 0xffff;
133 		b = v >> 16;
134 	}
135 
136 	gsc_writel(a, dev->base_addr + PA_CPU_PORT_L_ACCESS);
137 	if (!running_on_qemu)
138 		udelay(1);
139 	gsc_writel(b, dev->base_addr + PA_CPU_PORT_L_ACCESS);
140 }
141 
142 #define LAN_PROM_ADDR	0xF0810000
143 
144 static int __init
145 lan_init_chip(struct parisc_device *dev)
146 {
147 	struct	net_device *netdevice;
148 	struct i596_private *lp;
149 	int retval = -ENOMEM;
150 	u8 addr[ETH_ALEN];
151 	int i;
152 
153 	if (!dev->irq) {
154 		printk(KERN_ERR "%s: IRQ not found for i82596 at 0x%lx\n",
155 			__FILE__, (unsigned long)dev->hpa.start);
156 		return -ENODEV;
157 	}
158 
159 	printk(KERN_INFO "Found i82596 at 0x%lx, IRQ %d\n",
160 			(unsigned long)dev->hpa.start, dev->irq);
161 
162 	netdevice = alloc_etherdev(sizeof(struct i596_private));
163 	if (!netdevice)
164 		return -ENOMEM;
165 	SET_NETDEV_DEV(netdevice, &dev->dev);
166 	parisc_set_drvdata (dev, netdevice);
167 
168 	netdevice->base_addr = dev->hpa.start;
169 	netdevice->irq = dev->irq;
170 
171 	if (pdc_lan_station_id(addr, netdevice->base_addr)) {
172 		for (i = 0; i < 6; i++) {
173 			addr[i] = gsc_readb(LAN_PROM_ADDR + i);
174 		}
175 		printk(KERN_INFO
176 		       "%s: MAC of HP700 LAN read from EEPROM\n", __FILE__);
177 	}
178 	eth_hw_addr_set(netdevice, addr);
179 
180 	lp = netdev_priv(netdevice);
181 	lp->options = dev->id.sversion == 0x72 ? OPT_SWAP_PORT : 0;
182 	lp->dma = dma_alloc_noncoherent(&dev->dev,
183 			sizeof(struct i596_dma), &lp->dma_addr,
184 			DMA_BIDIRECTIONAL, GFP_KERNEL);
185 	if (!lp->dma)
186 		goto out_free_netdev;
187 
188 	retval = i82596_probe(netdevice);
189 	if (retval)
190 		goto out_free_dma;
191 	return 0;
192 
193 out_free_dma:
194 	dma_free_noncoherent(&dev->dev, sizeof(struct i596_dma),
195 		       lp->dma, lp->dma_addr, DMA_BIDIRECTIONAL);
196 out_free_netdev:
197 	free_netdev(netdevice);
198 	return retval;
199 }
200 
201 static void __exit lan_remove_chip(struct parisc_device *pdev)
202 {
203 	struct net_device *dev = parisc_get_drvdata(pdev);
204 	struct i596_private *lp = netdev_priv(dev);
205 
206 	unregister_netdev (dev);
207 	dma_free_noncoherent(&pdev->dev, sizeof(struct i596_private), lp->dma,
208 		       lp->dma_addr, DMA_BIDIRECTIONAL);
209 	free_netdev (dev);
210 }
211 
212 static const struct parisc_device_id lan_tbl[] __initconst = {
213 	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0008a },
214 	{ HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00072 },
215 	{ 0, }
216 };
217 
218 MODULE_DEVICE_TABLE(parisc, lan_tbl);
219 
220 static struct parisc_driver lan_driver __refdata = {
221 	.name		= "lasi_82596",
222 	.id_table	= lan_tbl,
223 	.probe		= lan_init_chip,
224 	.remove         = __exit_p(lan_remove_chip),
225 };
226 
227 static int lasi_82596_init(void)
228 {
229 	printk(KERN_INFO LASI_82596_DRIVER_VERSION "\n");
230 	return register_parisc_driver(&lan_driver);
231 }
232 
233 module_init(lasi_82596_init);
234 
235 static void __exit lasi_82596_exit(void)
236 {
237 	unregister_parisc_driver(&lan_driver);
238 }
239 
240 module_exit(lasi_82596_exit);
241