File: /usr/src/linux/drivers/net/stnic.c
1 /* stnic.c : A SH7750 specific part of driver for NS DP83902A ST-NIC.
2 *
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.
6 *
7 * Copyright (C) 1999 kaz Kojima
8 */
9
10 #include <linux/config.h>
11 #include <linux/module.h>
12
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/errno.h>
16 #include <linux/ioport.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <asm/system.h>
22 #include <asm/io.h>
23 #include <asm/hitachi_se.h>
24 #include <asm/machvec.h>
25 #ifdef CONFIG_SH_STANDARD_BIOS
26 #include <asm/sh_bios.h>
27 #endif
28
29 #include "8390.h"
30
31 #define byte unsigned char
32 #define half unsigned short
33 #define word unsigned int
34 #define vbyte volatile unsigned char
35 #define vhalf volatile unsigned short
36 #define vword volatile unsigned int
37
38 #define STNIC_RUN 0x01 /* 1 == Run, 0 == reset. */
39
40 #define START_PG 0 /* First page of TX buffer */
41 #define STOP_PG 128 /* Last page +1 of RX ring */
42
43 /* Alias */
44 #define STNIC_CR E8390_CMD
45 #define PG0_RSAR0 EN0_RSARLO
46 #define PG0_RSAR1 EN0_RSARHI
47 #define PG0_RBCR0 EN0_RCNTLO
48 #define PG0_RBCR1 EN0_RCNTHI
49
50 #define CR_RRD E8390_RREAD
51 #define CR_RWR E8390_RWRITE
52 #define CR_PG0 E8390_PAGE0
53 #define CR_STA E8390_START
54 #define CR_RDMA E8390_NODMA
55
56 /* FIXME! YOU MUST SET YOUR OWN ETHER ADDRESS. */
57 static byte stnic_eadr[6] =
58 {0x00, 0xc0, 0x6e, 0x00, 0x00, 0x07};
59
60 static struct net_device *stnic_dev;
61
62 static int stnic_open (struct net_device *dev);
63 static int stnic_close (struct net_device *dev);
64 static void stnic_reset (struct net_device *dev);
65 static void stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
66 int ring_page);
67 static void stnic_block_input (struct net_device *dev, int count,
68 struct sk_buff *skb , int ring_offset);
69 static void stnic_block_output (struct net_device *dev, int count,
70 const unsigned char *buf, int start_page);
71
72 static void stnic_init (struct net_device *dev);
73
74 /* SH7750 specific read/write io. */
75 static inline void
76 STNIC_DELAY (void)
77 {
78 vword trash;
79 trash = *(vword *) 0xa0000000;
80 trash = *(vword *) 0xa0000000;
81 trash = *(vword *) 0xa0000000;
82 }
83
84 static inline byte
85 STNIC_READ (int reg)
86 {
87 byte val;
88
89 val = (*(vhalf *) (PA_83902 + ((reg) << 1)) >> 8) & 0xff;
90 STNIC_DELAY ();
91 return val;
92 }
93
94 static inline void
95 STNIC_WRITE (int reg, byte val)
96 {
97 *(vhalf *) (PA_83902 + ((reg) << 1)) = ((half) (val) << 8);
98 STNIC_DELAY ();
99 }
100
101 int __init stnic_probe(void)
102 {
103 struct net_device *dev;
104 int i;
105
106 /* If we are not running on a SolutionEngine, give up now */
107 if (! MACH_SE)
108 return -ENODEV;
109
110 /* New style probing API */
111 dev = init_etherdev (NULL, 0);
112 if (!dev)
113 return -ENOMEM;
114 SET_MODULE_OWNER(dev);
115 stnic_dev = dev;
116
117 /* Allocate dev->priv and fill in 8390 specific dev fields. */
118 if (ethdev_init (dev))
119 {
120 printk (KERN_EMERG "Unable to get memory for dev->priv.\n");
121 return -ENOMEM;
122 }
123
124 #ifdef CONFIG_SH_STANDARD_BIOS
125 sh_bios_get_node_addr (stnic_eadr);
126 #endif
127 for (i = 0; i < ETHER_ADDR_LEN; i++)
128 dev->dev_addr[i] = stnic_eadr[i];
129
130 /* Set the base address to point to the NIC, not the "real" base! */
131 dev->base_addr = 0x1000;
132 dev->irq = IRQ_STNIC;
133 dev->open = &stnic_open;
134 dev->stop = &stnic_close;
135
136 /* Snarf the interrupt now. There's no point in waiting since we cannot
137 share and the board will usually be enabled. */
138 i = request_irq (dev->irq, ei_interrupt, 0, dev->name, dev);
139 if (i) {
140 printk (KERN_EMERG " unable to get IRQ %d.\n", dev->irq);
141 unregister_netdev(dev);
142 kfree(dev->priv);
143 kfree(dev);
144 return i;
145 }
146
147 ei_status.name = dev->name;
148 ei_status.word16 = 1;
149 #ifdef __LITTLE_ENDIAN__
150 ei_status.bigendian = 0;
151 #else
152 ei_status.bigendian = 1;
153 #endif
154 ei_status.tx_start_page = START_PG;
155 ei_status.rx_start_page = START_PG + TX_PAGES;
156 ei_status.stop_page = STOP_PG;
157
158 ei_status.reset_8390 = &stnic_reset;
159 ei_status.get_8390_hdr = &stnic_get_hdr;
160 ei_status.block_input = &stnic_block_input;
161 ei_status.block_output = &stnic_block_output;
162
163 stnic_init (dev);
164
165 printk (KERN_INFO "NS ST-NIC 83902A\n");
166
167 return 0;
168 }
169
170 static int
171 stnic_open (struct net_device *dev)
172 {
173 #if 0
174 printk (KERN_DEBUG "stnic open\n");
175 #endif
176 ei_open (dev);
177 return 0;
178 }
179
180 static int
181 stnic_close (struct net_device *dev)
182 {
183 ei_close (dev);
184 return 0;
185 }
186
187 static void
188 stnic_reset (struct net_device *dev)
189 {
190 *(vhalf *) PA_83902_RST = 0;
191 udelay (5);
192 if (ei_debug > 1)
193 printk (KERN_WARNING "8390 reset done (%ld).\n", jiffies);
194 *(vhalf *) PA_83902_RST = ~0;
195 udelay (5);
196 }
197
198 static void
199 stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
200 int ring_page)
201 {
202 half buf[2];
203
204 STNIC_WRITE (PG0_RSAR0, 0);
205 STNIC_WRITE (PG0_RSAR1, ring_page);
206 STNIC_WRITE (PG0_RBCR0, 4);
207 STNIC_WRITE (PG0_RBCR1, 0);
208 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
209
210 buf[0] = *(vhalf *) PA_83902_IF;
211 STNIC_DELAY ();
212 buf[1] = *(vhalf *) PA_83902_IF;
213 STNIC_DELAY ();
214 hdr->next = buf[0] >> 8;
215 hdr->status = buf[0] & 0xff;
216 #ifdef __LITTLE_ENDIAN__
217 hdr->count = buf[1];
218 #else
219 hdr->count = ((buf[1] >> 8) & 0xff) | (buf[1] << 8);
220 #endif
221
222 if (ei_debug > 1)
223 printk (KERN_DEBUG "ring %x status %02x next %02x count %04x.\n",
224 ring_page, hdr->status, hdr->next, hdr->count);
225
226 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
227 }
228
229 /* Block input and output, similar to the Crynwr packet driver. If you are
230 porting to a new ethercard look at the packet driver source for hints.
231 The HP LAN doesn't use shared memory -- we put the packet
232 out through the "remote DMA" dataport. */
233
234 static void
235 stnic_block_input (struct net_device *dev, int length, struct sk_buff *skb,
236 int offset)
237 {
238 char *buf = skb->data;
239 half val;
240
241 STNIC_WRITE (PG0_RSAR0, offset & 0xff);
242 STNIC_WRITE (PG0_RSAR1, offset >> 8);
243 STNIC_WRITE (PG0_RBCR0, length & 0xff);
244 STNIC_WRITE (PG0_RBCR1, length >> 8);
245 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
246
247 if (length & 1)
248 length++;
249
250 while (length > 0)
251 {
252 val = *(vhalf *) PA_83902_IF;
253 #ifdef __LITTLE_ENDIAN__
254 *buf++ = val & 0xff;
255 *buf++ = val >> 8;
256 #else
257 *buf++ = val >> 8;
258 *buf++ = val & 0xff;
259 #endif
260 STNIC_DELAY ();
261 length -= sizeof (half);
262 }
263
264 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
265 }
266
267 static void
268 stnic_block_output (struct net_device *dev, int length,
269 const unsigned char *buf, int output_page)
270 {
271 STNIC_WRITE (PG0_RBCR0, 1); /* Write non-zero value */
272 STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
273 STNIC_DELAY ();
274
275 STNIC_WRITE (PG0_RBCR0, length & 0xff);
276 STNIC_WRITE (PG0_RBCR1, length >> 8);
277 STNIC_WRITE (PG0_RSAR0, 0);
278 STNIC_WRITE (PG0_RSAR1, output_page);
279 STNIC_WRITE (STNIC_CR, CR_RWR | CR_PG0 | CR_STA);
280
281 if (length & 1)
282 length++;
283
284 while (length > 0)
285 {
286 #ifdef __LITTLE_ENDIAN__
287 *(vhalf *) PA_83902_IF = ((half) buf[1] << 8) | buf[0];
288 #else
289 *(vhalf *) PA_83902_IF = ((half) buf[0] << 8) | buf[1];
290 #endif
291 STNIC_DELAY ();
292 buf += sizeof (half);
293 length -= sizeof (half);
294 }
295
296 STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
297 }
298
299 /* This function resets the STNIC if something screws up. */
300 static void
301 stnic_init (struct net_device *dev)
302 {
303 stnic_reset (dev);
304 NS8390_init (dev, 0);
305 return;
306 }
307
308 /* Hardware interrupt handler. */
309 extern void ei_interrupt (int irq, void *dev_id, struct pt_regs *regs);
310
311 void
312 do_stnic_intr (int irq, void *dev_id, struct pt_regs *regs)
313 {
314 ei_interrupt (0, stnic_dev, regs);
315 }
316
317 module_init(stnic_probe);
318 /* No cleanup routine. */
319