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