The LDT fixes in particular fix some potentially random strange behaviour.
[davej-history.git] / drivers / net / stnic.c
blob373e535fbe05a7c7ea664f7ea83c86b245f70d77
1 /* stnic.c : A SH7750 specific part of driver for NS DP83902A ST-NIC.
3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
5 * for more details.
7 * Copyright (C) 1999 kaz Kojima
8 */
10 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <asm/system.h>
21 #include <asm/io.h>
22 #include <asm/hitachi_se.h>
23 #include <asm/machvec.h>
25 #include "8390.h"
27 #define byte unsigned char
28 #define half unsigned short
29 #define word unsigned int
30 #define vbyte volatile unsigned char
31 #define vhalf volatile unsigned short
32 #define vword volatile unsigned int
34 #define STNIC_RUN 0x01 /* 1 == Run, 0 == reset. */
36 #define START_PG 0 /* First page of TX buffer */
37 #define STOP_PG 128 /* Last page +1 of RX ring */
39 /* Alias */
40 #define STNIC_CR E8390_CMD
41 #define PG0_RSAR0 EN0_RSARLO
42 #define PG0_RSAR1 EN0_RSARHI
43 #define PG0_RBCR0 EN0_RCNTLO
44 #define PG0_RBCR1 EN0_RCNTHI
46 #define CR_RRD E8390_RREAD
47 #define CR_RWR E8390_RWRITE
48 #define CR_PG0 E8390_PAGE0
49 #define CR_STA E8390_START
50 #define CR_RDMA E8390_NODMA
52 /* FIXME! YOU MUST SET YOUR OWN ETHER ADDRESS. */
53 static byte stnic_eadr[6] =
54 {0x00, 0xc0, 0x6e, 0x00, 0x00, 0x07};
56 static struct net_device *stnic_dev;
58 static int stnic_open (struct net_device *dev);
59 static int stnic_close (struct net_device *dev);
60 static void stnic_reset (struct net_device *dev);
61 static void stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
62 int ring_page);
63 static void stnic_block_input (struct net_device *dev, int count,
64 struct sk_buff *skb , int ring_offset);
65 static void stnic_block_output (struct net_device *dev, int count,
66 const unsigned char *buf, int start_page);
68 static void stnic_init (struct net_device *dev);
70 /* SH7750 specific read/write io. */
71 static inline void
72 STNIC_DELAY (void)
74 vword trash;
75 trash = *(vword *) 0xa0000000;
76 trash = *(vword *) 0xa0000000;
79 static inline byte
80 STNIC_READ (int reg)
82 byte val;
84 val = (*(vhalf *) (PA_83902 + ((reg) << 1)) >> 8) & 0xff;
85 STNIC_DELAY ();
86 return val;
89 static inline void
90 STNIC_WRITE (int reg, byte val)
92 *(vhalf *) (PA_83902 + ((reg) << 1)) = ((half) (val) << 8);
93 STNIC_DELAY ();
96 int __init stnic_probe(void)
98 struct net_device *dev;
99 int i;
101 /* If we are not running on a SolutionEngine, give up now */
102 if (! MACH_SE)
103 return -ENODEV;
105 /* New style probing API */
106 dev = init_etherdev (NULL, 0);
107 if (!dev)
108 return -ENOMEM;
109 SET_MODULE_OWNER(dev);
110 stnic_dev = dev;
112 /* Allocate dev->priv and fill in 8390 specific dev fields. */
113 if (ethdev_init (dev))
115 printk ("Unable to get memory for dev->priv.\n");
116 return -ENOMEM;
119 for (i = 0; i < ETHER_ADDR_LEN; i++)
120 dev->dev_addr[i] = stnic_eadr[i];
122 /* Set the base address to point to the NIC, not the "real" base! */
123 dev->base_addr = 0x1000;
124 dev->irq = IRQ_STNIC;
125 dev->open = &stnic_open;
126 dev->stop = &stnic_close;
128 /* Snarf the interrupt now. There's no point in waiting since we cannot
129 share and the board will usually be enabled. */
130 i = request_irq (dev->irq, ei_interrupt, 0, dev->name, dev);
131 if (i) {
132 printk (" unable to get IRQ %d.\n", dev->irq);
133 unregister_netdev(dev);
134 kfree(dev->priv);
135 kfree(dev);
136 return i;
139 ei_status.name = dev->name;
140 ei_status.word16 = 1;
141 ei_status.tx_start_page = START_PG;
142 ei_status.rx_start_page = START_PG + TX_PAGES;
143 ei_status.stop_page = STOP_PG;
145 ei_status.reset_8390 = &stnic_reset;
146 ei_status.get_8390_hdr = &stnic_get_hdr;
147 ei_status.block_input = &stnic_block_input;
148 ei_status.block_output = &stnic_block_output;
149 stnic_init (dev);
151 printk (KERN_INFO "NS ST-NIC 83902A\n");
153 return 0;
156 static int
157 stnic_open (struct net_device *dev)
159 #if 0
160 printk ("stnic open\n");
161 #endif
162 ei_open (dev);
163 return 0;
166 static int
167 stnic_close (struct net_device *dev)
169 ei_close (dev);
170 return 0;
173 static void
174 stnic_reset (struct net_device *dev)
176 *(vhalf *) PA_83902_RST = 0;
177 udelay (5);
178 if (ei_debug > 1)
179 printk("8390 reset done (%ld).\n", jiffies);
180 *(vhalf *) PA_83902_RST = ~0;
181 udelay (5);
184 static void
185 stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
186 int ring_page)
188 half buf[2];
190 STNIC_WRITE (PG0_RSAR0, 0);
191 STNIC_WRITE (PG0_RSAR1, ring_page);
192 STNIC_WRITE (PG0_RBCR0, 4);
193 STNIC_WRITE (PG0_RBCR1, 0);
194 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
196 buf[0] = *(vhalf *) PA_83902_IF;
197 STNIC_DELAY ();
198 buf[1] = *(vhalf *) PA_83902_IF;
199 STNIC_DELAY ();
200 hdr->next = buf[0] >> 8;
201 hdr->status = buf[0] & 0xff;
202 #ifdef __LITTLE_ENDIAN__
203 hdr->count = buf[1];
204 #else
205 hdr->count = ((buf[1] >> 8) & 0xff) | (buf[1] << 8);
206 #endif
208 if (ei_debug > 1)
209 printk ("ring %x status %02x next %02x count %04x.\n",
210 ring_page, hdr->status, hdr->next, hdr->count);
212 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
215 /* Block input and output, similar to the Crynwr packet driver. If you are
216 porting to a new ethercard look at the packet driver source for hints.
217 The HP LAN doesn't use shared memory -- we put the packet
218 out through the "remote DMA" dataport. */
220 static void
221 stnic_block_input (struct net_device *dev, int length, struct sk_buff *skb,
222 int offset)
224 char *buf = skb->data;
225 half val;
227 STNIC_WRITE (PG0_RSAR0, offset & 0xff);
228 STNIC_WRITE (PG0_RSAR1, offset >> 8);
229 STNIC_WRITE (PG0_RBCR0, length & 0xff);
230 STNIC_WRITE (PG0_RBCR1, length >> 8);
231 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
233 if (length & 1)
234 length++;
236 while (length > 0)
238 val = *(vhalf *) PA_83902_IF;
239 #ifdef __LITTLE_ENDIAN__
240 *buf++ = val & 0xff;
241 *buf++ = val >> 8;
242 #else
243 *buf++ = val >> 8;
244 *buf++ = val & 0xff;
245 #endif
246 STNIC_DELAY ();
247 length -= sizeof (half);
250 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
253 static void
254 stnic_block_output (struct net_device *dev, int length,
255 const unsigned char *buf, int output_page)
257 #if 0
258 STNIC_WRITE (PG0_RBCR0, 1);
259 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
260 #else /* XXX: I don't know why but this works. -- gniibe */
261 STNIC_WRITE (PG0_RBCR0, 0x42);
262 STNIC_WRITE (PG0_RBCR1, 0x00);
263 STNIC_WRITE (PG0_RBCR0, 0x42);
264 STNIC_WRITE (PG0_RBCR1, 0x00);
265 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
266 STNIC_DELAY ();
267 #endif
269 STNIC_WRITE (PG0_RSAR0, 0);
270 STNIC_WRITE (PG0_RSAR1, output_page);
271 STNIC_WRITE (PG0_RBCR0, length & 0xff);
272 STNIC_WRITE (PG0_RBCR1, length >> 8);
273 STNIC_WRITE (STNIC_CR, CR_RWR | CR_PG0 | CR_STA);
275 if (length & 1)
276 length++;
278 while (length > 0)
280 #ifdef __LITTLE_ENDIAN__
281 *(vhalf *) PA_83902_IF = ((half) buf[1] << 8) | buf[0];
282 #else
283 *(vhalf *) PA_83902_IF = ((half) buf[0] << 8) | buf[1];
284 #endif
285 STNIC_DELAY ();
286 buf += sizeof (half);
287 length -= sizeof (half);
290 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
293 /* This function resets the STNIC if something screws up. */
294 static void
295 stnic_init (struct net_device *dev)
297 stnic_reset (dev);
298 NS8390_init (dev, 0);
299 return;
302 /* Hardware interrupt handler. */
303 extern void ei_interrupt (int irq, void *dev_id, struct pt_regs *regs);
305 void
306 do_stnic_intr (int irq, void *dev_id, struct pt_regs *regs)
308 ei_interrupt (0, stnic_dev, regs);
311 module_init(stnic_probe);
312 /* No cleanup routine. */