Initial commit
[wrt350n-kernel.git] / drivers / net / stnic.c
blobb65be5d70fecfee4d21f8d9d66505e411ed08164
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>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
14 #include <linux/ioport.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <asm/system.h>
21 #include <asm/io.h>
22 #include <asm/se.h>
23 #include <asm/machvec.h>
24 #ifdef CONFIG_SH_STANDARD_BIOS
25 #include <asm/sh_bios.h>
26 #endif
28 #include "8390.h"
30 #define DRV_NAME "stnic"
32 #define byte unsigned char
33 #define half unsigned short
34 #define word unsigned int
35 #define vbyte volatile unsigned char
36 #define vhalf volatile unsigned short
37 #define vword volatile unsigned int
39 #define STNIC_RUN 0x01 /* 1 == Run, 0 == reset. */
41 #define START_PG 0 /* First page of TX buffer */
42 #define STOP_PG 128 /* Last page +1 of RX ring */
44 /* Alias */
45 #define STNIC_CR E8390_CMD
46 #define PG0_RSAR0 EN0_RSARLO
47 #define PG0_RSAR1 EN0_RSARHI
48 #define PG0_RBCR0 EN0_RCNTLO
49 #define PG0_RBCR1 EN0_RCNTHI
51 #define CR_RRD E8390_RREAD
52 #define CR_RWR E8390_RWRITE
53 #define CR_PG0 E8390_PAGE0
54 #define CR_STA E8390_START
55 #define CR_RDMA E8390_NODMA
57 /* FIXME! YOU MUST SET YOUR OWN ETHER ADDRESS. */
58 static byte stnic_eadr[6] =
59 {0x00, 0xc0, 0x6e, 0x00, 0x00, 0x07};
61 static struct net_device *stnic_dev;
63 static int stnic_open (struct net_device *dev);
64 static int stnic_close (struct net_device *dev);
65 static void stnic_reset (struct net_device *dev);
66 static void stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
67 int ring_page);
68 static void stnic_block_input (struct net_device *dev, int count,
69 struct sk_buff *skb , int ring_offset);
70 static void stnic_block_output (struct net_device *dev, int count,
71 const unsigned char *buf, int start_page);
73 static void stnic_init (struct net_device *dev);
75 /* SH7750 specific read/write io. */
76 static inline void
77 STNIC_DELAY (void)
79 vword trash;
80 trash = *(vword *) 0xa0000000;
81 trash = *(vword *) 0xa0000000;
82 trash = *(vword *) 0xa0000000;
85 static inline byte
86 STNIC_READ (int reg)
88 byte val;
90 val = (*(vhalf *) (PA_83902 + ((reg) << 1)) >> 8) & 0xff;
91 STNIC_DELAY ();
92 return val;
95 static inline void
96 STNIC_WRITE (int reg, byte val)
98 *(vhalf *) (PA_83902 + ((reg) << 1)) = ((half) (val) << 8);
99 STNIC_DELAY ();
102 static int __init stnic_probe(void)
104 struct net_device *dev;
105 int i, err;
107 /* If we are not running on a SolutionEngine, give up now */
108 if (! MACH_SE)
109 return -ENODEV;
111 /* New style probing API */
112 dev = alloc_ei_netdev();
113 if (!dev)
114 return -ENOMEM;
116 #ifdef CONFIG_SH_STANDARD_BIOS
117 sh_bios_get_node_addr (stnic_eadr);
118 #endif
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;
127 #ifdef CONFIG_NET_POLL_CONTROLLER
128 dev->poll_controller = ei_poll;
129 #endif
131 /* Snarf the interrupt now. There's no point in waiting since we cannot
132 share and the board will usually be enabled. */
133 err = request_irq (dev->irq, ei_interrupt, 0, DRV_NAME, dev);
134 if (err) {
135 printk (KERN_EMERG " unable to get IRQ %d.\n", dev->irq);
136 free_netdev(dev);
137 return err;
140 ei_status.name = dev->name;
141 ei_status.word16 = 1;
142 #ifdef __LITTLE_ENDIAN__
143 ei_status.bigendian = 0;
144 #else
145 ei_status.bigendian = 1;
146 #endif
147 ei_status.tx_start_page = START_PG;
148 ei_status.rx_start_page = START_PG + TX_PAGES;
149 ei_status.stop_page = STOP_PG;
151 ei_status.reset_8390 = &stnic_reset;
152 ei_status.get_8390_hdr = &stnic_get_hdr;
153 ei_status.block_input = &stnic_block_input;
154 ei_status.block_output = &stnic_block_output;
156 stnic_init (dev);
158 err = register_netdev(dev);
159 if (err) {
160 free_irq(dev->irq, dev);
161 free_netdev(dev);
162 return err;
164 stnic_dev = dev;
166 printk (KERN_INFO "NS ST-NIC 83902A\n");
168 return 0;
171 static int
172 stnic_open (struct net_device *dev)
174 #if 0
175 printk (KERN_DEBUG "stnic open\n");
176 #endif
177 ei_open (dev);
178 return 0;
181 static int
182 stnic_close (struct net_device *dev)
184 ei_close (dev);
185 return 0;
188 static void
189 stnic_reset (struct net_device *dev)
191 *(vhalf *) PA_83902_RST = 0;
192 udelay (5);
193 if (ei_debug > 1)
194 printk (KERN_WARNING "8390 reset done (%ld).\n", jiffies);
195 *(vhalf *) PA_83902_RST = ~0;
196 udelay (5);
199 static void
200 stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
201 int ring_page)
203 half buf[2];
205 STNIC_WRITE (PG0_RSAR0, 0);
206 STNIC_WRITE (PG0_RSAR1, ring_page);
207 STNIC_WRITE (PG0_RBCR0, 4);
208 STNIC_WRITE (PG0_RBCR1, 0);
209 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
211 buf[0] = *(vhalf *) PA_83902_IF;
212 STNIC_DELAY ();
213 buf[1] = *(vhalf *) PA_83902_IF;
214 STNIC_DELAY ();
215 hdr->next = buf[0] >> 8;
216 hdr->status = buf[0] & 0xff;
217 #ifdef __LITTLE_ENDIAN__
218 hdr->count = buf[1];
219 #else
220 hdr->count = ((buf[1] >> 8) & 0xff) | (buf[1] << 8);
221 #endif
223 if (ei_debug > 1)
224 printk (KERN_DEBUG "ring %x status %02x next %02x count %04x.\n",
225 ring_page, hdr->status, hdr->next, hdr->count);
227 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
230 /* Block input and output, similar to the Crynwr packet driver. If you are
231 porting to a new ethercard look at the packet driver source for hints.
232 The HP LAN doesn't use shared memory -- we put the packet
233 out through the "remote DMA" dataport. */
235 static void
236 stnic_block_input (struct net_device *dev, int length, struct sk_buff *skb,
237 int offset)
239 char *buf = skb->data;
240 half val;
242 STNIC_WRITE (PG0_RSAR0, offset & 0xff);
243 STNIC_WRITE (PG0_RSAR1, offset >> 8);
244 STNIC_WRITE (PG0_RBCR0, length & 0xff);
245 STNIC_WRITE (PG0_RBCR1, length >> 8);
246 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
248 if (length & 1)
249 length++;
251 while (length > 0)
253 val = *(vhalf *) PA_83902_IF;
254 #ifdef __LITTLE_ENDIAN__
255 *buf++ = val & 0xff;
256 *buf++ = val >> 8;
257 #else
258 *buf++ = val >> 8;
259 *buf++ = val & 0xff;
260 #endif
261 STNIC_DELAY ();
262 length -= sizeof (half);
265 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
268 static void
269 stnic_block_output (struct net_device *dev, int length,
270 const unsigned char *buf, int output_page)
272 STNIC_WRITE (PG0_RBCR0, 1); /* Write non-zero value */
273 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
274 STNIC_DELAY ();
276 STNIC_WRITE (PG0_RBCR0, length & 0xff);
277 STNIC_WRITE (PG0_RBCR1, length >> 8);
278 STNIC_WRITE (PG0_RSAR0, 0);
279 STNIC_WRITE (PG0_RSAR1, output_page);
280 STNIC_WRITE (STNIC_CR, CR_RWR | CR_PG0 | CR_STA);
282 if (length & 1)
283 length++;
285 while (length > 0)
287 #ifdef __LITTLE_ENDIAN__
288 *(vhalf *) PA_83902_IF = ((half) buf[1] << 8) | buf[0];
289 #else
290 *(vhalf *) PA_83902_IF = ((half) buf[0] << 8) | buf[1];
291 #endif
292 STNIC_DELAY ();
293 buf += sizeof (half);
294 length -= sizeof (half);
297 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
300 /* This function resets the STNIC if something screws up. */
301 static void
302 stnic_init (struct net_device *dev)
304 stnic_reset (dev);
305 NS8390_init (dev, 0);
306 return;
309 static void __exit stnic_cleanup(void)
311 unregister_netdev(stnic_dev);
312 free_irq(stnic_dev->irq, stnic_dev);
313 free_netdev(stnic_dev);
316 module_init(stnic_probe);
317 module_exit(stnic_cleanup);
318 MODULE_LICENSE("GPL");