1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc rtl8029.c
3433d6423SLionel Sambuc
4433d6423SLionel Sambuc Initialization of PCI DP8390-based ethernet cards
5433d6423SLionel Sambuc
6433d6423SLionel Sambuc Created: April 2000 by Philip Homburg <philip@f-mnx.phicoh.com>
7433d6423SLionel Sambuc */
8433d6423SLionel Sambuc
9433d6423SLionel Sambuc #include <minix/drivers.h>
103913e490SDavid van Moolenbroek #include <minix/netdriver.h>
11433d6423SLionel Sambuc
12433d6423SLionel Sambuc #include <stdlib.h>
13433d6423SLionel Sambuc #include <sys/types.h>
14433d6423SLionel Sambuc #include <machine/pci.h>
15433d6423SLionel Sambuc
16433d6423SLionel Sambuc #include "assert.h"
17433d6423SLionel Sambuc
18433d6423SLionel Sambuc #include "local.h"
19433d6423SLionel Sambuc #include "dp8390.h"
20433d6423SLionel Sambuc #include "rtl8029.h"
21433d6423SLionel Sambuc
22433d6423SLionel Sambuc static void rtl_init(struct dpeth *dep);
23433d6423SLionel Sambuc #if 0
24433d6423SLionel Sambuc static u16_t get_ee_word(dpeth_t *dep, int a);
25433d6423SLionel Sambuc static void ee_wen(dpeth_t *dep);
26433d6423SLionel Sambuc static void set_ee_word(dpeth_t *dep, int a, u16_t w);
27433d6423SLionel Sambuc static void ee_wds(dpeth_t *dep);
28433d6423SLionel Sambuc #endif
29433d6423SLionel Sambuc
rtl_probe(dep,skip)30433d6423SLionel Sambuc int rtl_probe(dep, skip)
31433d6423SLionel Sambuc struct dpeth *dep;
32433d6423SLionel Sambuc int skip;
33433d6423SLionel Sambuc {
34433d6423SLionel Sambuc int r, devind;
35433d6423SLionel Sambuc u16_t vid, did;
36433d6423SLionel Sambuc u32_t bar;
37433d6423SLionel Sambuc u8_t ilr;
38*f7df02e7SDavid van Moolenbroek const char *dname;
39433d6423SLionel Sambuc
40433d6423SLionel Sambuc pci_init();
41433d6423SLionel Sambuc
42433d6423SLionel Sambuc r= pci_first_dev(&devind, &vid, &did);
43433d6423SLionel Sambuc if (r == 0)
44433d6423SLionel Sambuc return 0;
45433d6423SLionel Sambuc
46433d6423SLionel Sambuc while (skip--)
47433d6423SLionel Sambuc {
48433d6423SLionel Sambuc r= pci_next_dev(&devind, &vid, &did);
49433d6423SLionel Sambuc if (!r)
50433d6423SLionel Sambuc return 0;
51433d6423SLionel Sambuc }
52433d6423SLionel Sambuc
53433d6423SLionel Sambuc dname= pci_dev_name(vid, did);
54433d6423SLionel Sambuc if (!dname)
55433d6423SLionel Sambuc dname= "unknown device";
56433d6423SLionel Sambuc printf("%s: %s (%04X/%04X) at %s\n",
57*f7df02e7SDavid van Moolenbroek netdriver_name(), dname, vid, did, pci_slot_name(devind));
58433d6423SLionel Sambuc if(pci_reserve_ok(devind) != OK)
59433d6423SLionel Sambuc return 0;
60433d6423SLionel Sambuc /* printf("cr = 0x%x\n", pci_attr_r16(devind, PCI_CR)); */
61433d6423SLionel Sambuc bar= pci_attr_r32(devind, PCI_BAR) & 0xffffffe0;
62433d6423SLionel Sambuc
63433d6423SLionel Sambuc if (bar < 0x400)
64433d6423SLionel Sambuc panic("base address is not properly configured");
65433d6423SLionel Sambuc
66433d6423SLionel Sambuc dep->de_base_port= bar;
67433d6423SLionel Sambuc
68433d6423SLionel Sambuc ilr= pci_attr_r8(devind, PCI_ILR);
69433d6423SLionel Sambuc dep->de_irq= ilr;
70433d6423SLionel Sambuc if (debug)
71433d6423SLionel Sambuc {
72433d6423SLionel Sambuc printf("%s: using I/O address 0x%lx, IRQ %d\n",
73*f7df02e7SDavid van Moolenbroek netdriver_name(), (unsigned long)bar, ilr);
74433d6423SLionel Sambuc }
75433d6423SLionel Sambuc dep->de_initf= rtl_init;
76433d6423SLionel Sambuc
77433d6423SLionel Sambuc return TRUE;
78433d6423SLionel Sambuc }
79433d6423SLionel Sambuc
rtl_init(dep)80433d6423SLionel Sambuc static void rtl_init(dep)
81433d6423SLionel Sambuc dpeth_t *dep;
82433d6423SLionel Sambuc {
83433d6423SLionel Sambuc u8_t reg_a, reg_b, cr, config0, config2, config3;
84433d6423SLionel Sambuc
85433d6423SLionel Sambuc #if DEBUG
86433d6423SLionel Sambuc printf("rtl_init called\n");
87433d6423SLionel Sambuc #endif
88433d6423SLionel Sambuc ne_init(dep);
89433d6423SLionel Sambuc
90433d6423SLionel Sambuc /* ID */
91433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P0);
92433d6423SLionel Sambuc reg_a = inb_reg0(dep, DP_DUM1);
93433d6423SLionel Sambuc reg_b = inb_reg0(dep, DP_DUM2);
94433d6423SLionel Sambuc
95433d6423SLionel Sambuc #if DEBUG
96433d6423SLionel Sambuc printf("rtl_init: '%c', '%c'\n", reg_a, reg_b);
97433d6423SLionel Sambuc #endif
98433d6423SLionel Sambuc
99433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P3);
100433d6423SLionel Sambuc config0 = inb_reg3(dep, 3);
101433d6423SLionel Sambuc config2 = inb_reg3(dep, 5);
102433d6423SLionel Sambuc config3 = inb_reg3(dep, 6);
103433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P0);
104433d6423SLionel Sambuc
105433d6423SLionel Sambuc #if DEBUG
106433d6423SLionel Sambuc printf("rtl_init: config 0/2/3 = %x/%x/%x\n",
107433d6423SLionel Sambuc config0, config2, config3);
108433d6423SLionel Sambuc #endif
109433d6423SLionel Sambuc
110433d6423SLionel Sambuc if (getenv("RTL8029FD"))
111433d6423SLionel Sambuc {
112433d6423SLionel Sambuc printf("rtl_init: setting full-duplex mode\n");
113433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P3);
114433d6423SLionel Sambuc
115433d6423SLionel Sambuc cr= inb_reg3(dep, 1);
116433d6423SLionel Sambuc outb_reg3(dep, 1, cr | 0xc0);
117433d6423SLionel Sambuc
118433d6423SLionel Sambuc outb_reg3(dep, 6, config3 | 0x40);
119433d6423SLionel Sambuc config3 = inb_reg3(dep, 6);
120433d6423SLionel Sambuc
121433d6423SLionel Sambuc config2= inb_reg3(dep, 5);
122433d6423SLionel Sambuc outb_reg3(dep, 5, config2 | 0x20);
123433d6423SLionel Sambuc config2= inb_reg3(dep, 5);
124433d6423SLionel Sambuc
125433d6423SLionel Sambuc outb_reg3(dep, 1, cr);
126433d6423SLionel Sambuc
127433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P0);
128433d6423SLionel Sambuc
129433d6423SLionel Sambuc #if DEBUG
130433d6423SLionel Sambuc printf("rtl_init: config 2 = %x\n", config2);
131433d6423SLionel Sambuc printf("rtl_init: config 3 = %x\n", config3);
132433d6423SLionel Sambuc #endif
133433d6423SLionel Sambuc }
134433d6423SLionel Sambuc
135433d6423SLionel Sambuc #if 0
136433d6423SLionel Sambuc for (i= 0; i<64; i++)
137433d6423SLionel Sambuc printf("%x ", get_ee_word(dep, i));
138433d6423SLionel Sambuc printf("\n");
139433d6423SLionel Sambuc #endif
140433d6423SLionel Sambuc
141433d6423SLionel Sambuc #if 0
142433d6423SLionel Sambuc if (getenv("RTL8029MN"))
143433d6423SLionel Sambuc {
144433d6423SLionel Sambuc ee_wen(dep);
145433d6423SLionel Sambuc
146433d6423SLionel Sambuc set_ee_word(dep, 0x78/2, 0x10ec);
147433d6423SLionel Sambuc set_ee_word(dep, 0x7A/2, 0x8029);
148433d6423SLionel Sambuc set_ee_word(dep, 0x7C/2, 0x10ec);
149433d6423SLionel Sambuc set_ee_word(dep, 0x7E/2, 0x8029);
150433d6423SLionel Sambuc
151433d6423SLionel Sambuc ee_wds(dep);
152433d6423SLionel Sambuc
153433d6423SLionel Sambuc assert(get_ee_word(dep, 0x78/2) == 0x10ec);
154433d6423SLionel Sambuc assert(get_ee_word(dep, 0x7A/2) == 0x8029);
155433d6423SLionel Sambuc assert(get_ee_word(dep, 0x7C/2) == 0x10ec);
156433d6423SLionel Sambuc assert(get_ee_word(dep, 0x7E/2) == 0x8029);
157433d6423SLionel Sambuc }
158433d6423SLionel Sambuc
159433d6423SLionel Sambuc if (getenv("RTL8029XXX"))
160433d6423SLionel Sambuc {
161433d6423SLionel Sambuc ee_wen(dep);
162433d6423SLionel Sambuc
163433d6423SLionel Sambuc set_ee_word(dep, 0x76/2, 0x8029);
164433d6423SLionel Sambuc
165433d6423SLionel Sambuc ee_wds(dep);
166433d6423SLionel Sambuc
167433d6423SLionel Sambuc assert(get_ee_word(dep, 0x76/2) == 0x8029);
168433d6423SLionel Sambuc }
169433d6423SLionel Sambuc #endif
170433d6423SLionel Sambuc }
171433d6423SLionel Sambuc
172433d6423SLionel Sambuc #if 0
173433d6423SLionel Sambuc static u16_t get_ee_word(dep, a)
174433d6423SLionel Sambuc dpeth_t *dep;
175433d6423SLionel Sambuc int a;
176433d6423SLionel Sambuc {
177433d6423SLionel Sambuc int b, i, cmd;
178433d6423SLionel Sambuc u16_t w;
179433d6423SLionel Sambuc
180433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P3); /* Bank 3 */
181433d6423SLionel Sambuc
182433d6423SLionel Sambuc /* Switch to 9346 mode and enable CS */
183433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8);
184433d6423SLionel Sambuc
185433d6423SLionel Sambuc cmd= 0x180 | (a & 0x3f); /* 1 1 0 a5 a4 a3 a2 a1 a0 */
186433d6423SLionel Sambuc for (i= 8; i >= 0; i--)
187433d6423SLionel Sambuc {
188433d6423SLionel Sambuc b= (cmd & (1 << i));
189433d6423SLionel Sambuc b= (b ? 2 : 0);
190433d6423SLionel Sambuc
191433d6423SLionel Sambuc /* Cmd goes out on the rising edge of the clock */
192433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | b);
193433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | 0x4 | b);
194433d6423SLionel Sambuc }
195433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8); /* End of cmd */
196433d6423SLionel Sambuc
197433d6423SLionel Sambuc w= 0;
198433d6423SLionel Sambuc for (i= 0; i<16; i++)
199433d6423SLionel Sambuc {
200433d6423SLionel Sambuc w <<= 1;
201433d6423SLionel Sambuc
202433d6423SLionel Sambuc /* Data is shifted out on the rising edge. Read at the
203433d6423SLionel Sambuc * falling edge.
204433d6423SLionel Sambuc */
205433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | 0x4);
206433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | b);
207433d6423SLionel Sambuc b= inb_reg3(dep, 1);
208433d6423SLionel Sambuc w |= (b & 1);
209433d6423SLionel Sambuc }
210433d6423SLionel Sambuc
211433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80); /* drop CS */
212433d6423SLionel Sambuc outb_reg3(dep, 1, 0x00); /* back to normal */
213433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P0); /* back to bank 0 */
214433d6423SLionel Sambuc
215433d6423SLionel Sambuc return w;
216433d6423SLionel Sambuc }
217433d6423SLionel Sambuc
218433d6423SLionel Sambuc static void ee_wen(dep)
219433d6423SLionel Sambuc dpeth_t *dep;
220433d6423SLionel Sambuc {
221433d6423SLionel Sambuc int b, i, cmd;
222433d6423SLionel Sambuc
223433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P3); /* Bank 3 */
224433d6423SLionel Sambuc
225433d6423SLionel Sambuc /* Switch to 9346 mode and enable CS */
226433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8);
227433d6423SLionel Sambuc
228433d6423SLionel Sambuc cmd= 0x130; /* 1 0 0 1 1 x x x x */
229433d6423SLionel Sambuc for (i= 8; i >= 0; i--)
230433d6423SLionel Sambuc {
231433d6423SLionel Sambuc b= (cmd & (1 << i));
232433d6423SLionel Sambuc b= (b ? 2 : 0);
233433d6423SLionel Sambuc
234433d6423SLionel Sambuc /* Cmd goes out on the rising edge of the clock */
235433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | b);
236433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | 0x4 | b);
237433d6423SLionel Sambuc }
238433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8); /* End of cmd */
239433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80); /* Drop CS */
240433d6423SLionel Sambuc micro_delay(1); /* Is this required? */
241433d6423SLionel Sambuc }
242433d6423SLionel Sambuc
243433d6423SLionel Sambuc static void set_ee_word(dep, a, w)
244433d6423SLionel Sambuc dpeth_t *dep;
245433d6423SLionel Sambuc int a;
246433d6423SLionel Sambuc u16_t w;
247433d6423SLionel Sambuc {
248433d6423SLionel Sambuc int b, i, cmd;
249433d6423SLionel Sambuc
250433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8); /* Set CS */
251433d6423SLionel Sambuc
252433d6423SLionel Sambuc cmd= 0x140 | (a & 0x3f); /* 1 0 1 a5 a4 a3 a2 a1 a0 */
253433d6423SLionel Sambuc for (i= 8; i >= 0; i--)
254433d6423SLionel Sambuc {
255433d6423SLionel Sambuc b= (cmd & (1 << i));
256433d6423SLionel Sambuc b= (b ? 2 : 0);
257433d6423SLionel Sambuc
258433d6423SLionel Sambuc /* Cmd goes out on the rising edge of the clock */
259433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | b);
260433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | 0x4 | b);
261433d6423SLionel Sambuc }
262433d6423SLionel Sambuc for (i= 15; i >= 0; i--)
263433d6423SLionel Sambuc {
264433d6423SLionel Sambuc b= (w & (1 << i));
265433d6423SLionel Sambuc b= (b ? 2 : 0);
266433d6423SLionel Sambuc
267433d6423SLionel Sambuc /* Cmd goes out on the rising edge of the clock */
268433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | b);
269433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | 0x4 | b);
270433d6423SLionel Sambuc }
271433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8); /* End of data */
272433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80); /* Drop CS */
273433d6423SLionel Sambuc micro_delay(1); /* Is this required? */
274433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8); /* Set CS */
275433d6423SLionel Sambuc for (i= 0; i<10000; i++)
276433d6423SLionel Sambuc {
277433d6423SLionel Sambuc if (inb_reg3(dep, 1) & 1)
278433d6423SLionel Sambuc break;
279433d6423SLionel Sambuc micro_delay(1);
280433d6423SLionel Sambuc }
281433d6423SLionel Sambuc if (!(inb_reg3(dep, 1) & 1))
282433d6423SLionel Sambuc panic("set_ee_word: device remains busy");
283433d6423SLionel Sambuc }
284433d6423SLionel Sambuc
285433d6423SLionel Sambuc static void ee_wds(dep)
286433d6423SLionel Sambuc dpeth_t *dep;
287433d6423SLionel Sambuc {
288433d6423SLionel Sambuc int b, i, cmd;
289433d6423SLionel Sambuc
290433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P3); /* Bank 3 */
291433d6423SLionel Sambuc
292433d6423SLionel Sambuc /* Switch to 9346 mode and enable CS */
293433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8);
294433d6423SLionel Sambuc
295433d6423SLionel Sambuc cmd= 0x100; /* 1 0 0 0 0 x x x x */
296433d6423SLionel Sambuc for (i= 8; i >= 0; i--)
297433d6423SLionel Sambuc {
298433d6423SLionel Sambuc b= (cmd & (1 << i));
299433d6423SLionel Sambuc b= (b ? 2 : 0);
300433d6423SLionel Sambuc
301433d6423SLionel Sambuc /* Cmd goes out on the rising edge of the clock */
302433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | b);
303433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8 | 0x4 | b);
304433d6423SLionel Sambuc }
305433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80 | 0x8); /* End of cmd */
306433d6423SLionel Sambuc outb_reg3(dep, 1, 0x80); /* Drop CS */
307433d6423SLionel Sambuc outb_reg3(dep, 1, 0x00); /* back to normal */
308433d6423SLionel Sambuc outb_reg0(dep, DP_CR, CR_PS_P0); /* back to bank 0 */
309433d6423SLionel Sambuc }
310433d6423SLionel Sambuc #endif
311433d6423SLionel Sambuc
312433d6423SLionel Sambuc /*
313433d6423SLionel Sambuc * $PchId: rtl8029.c,v 1.7 2004/08/03 12:16:58 philip Exp $
314433d6423SLionel Sambuc */
315