1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc * 3c503.c A shared memory driver for Etherlink II board.
3433d6423SLionel Sambuc *
4433d6423SLionel Sambuc * Created: Dec. 20, 1996 by G. Falzoni <falzoni@marina.scn.de>
5433d6423SLionel Sambuc *
6433d6423SLionel Sambuc * Inspired by the TNET package by M. Ostrowski, the driver for Linux
7433d6423SLionel Sambuc * by D. Becker, the Crynwr 3c503 packet driver, and the Amoeba driver.
8433d6423SLionel Sambuc *
9433d6423SLionel Sambuc * It works in shared memory mode and should be used with the
10433d6423SLionel Sambuc * device driver for NS 8390 based cards of Minix. Programmed
11433d6423SLionel Sambuc * I/O could be used as well but would result in poor performance.
12433d6423SLionel Sambuc */
13433d6423SLionel Sambuc
14433d6423SLionel Sambuc #include <minix/drivers.h>
153913e490SDavid van Moolenbroek #include <minix/netdriver.h>
16433d6423SLionel Sambuc
17433d6423SLionel Sambuc #include "local.h"
18433d6423SLionel Sambuc #include "dp8390.h"
19433d6423SLionel Sambuc #include "3c503.h"
20433d6423SLionel Sambuc
21433d6423SLionel Sambuc #if ENABLE_3C503
22433d6423SLionel Sambuc
23433d6423SLionel Sambuc extern u32_t system_hz;
24433d6423SLionel Sambuc
25433d6423SLionel Sambuc #define MILLIS_TO_TICKS(m) (((m)*system_hz/1000)+1)
26433d6423SLionel Sambuc
27433d6423SLionel Sambuc static void el2_init(dpeth_t *dep);
28433d6423SLionel Sambuc static void el2_stop(dpeth_t *dep);
29433d6423SLionel Sambuc
30433d6423SLionel Sambuc /*===========================================================================*
31433d6423SLionel Sambuc * el2_init *
32433d6423SLionel Sambuc *===========================================================================*/
el2_init(dep)33433d6423SLionel Sambuc static void el2_init(dep)
34433d6423SLionel Sambuc dpeth_t * dep;
35433d6423SLionel Sambuc {
36433d6423SLionel Sambuc /* Initalize hardware and data structures. */
37433d6423SLionel Sambuc int ix, irq;
38433d6423SLionel Sambuc int sendq_nr;
39433d6423SLionel Sambuc int cntr;
40433d6423SLionel Sambuc
41433d6423SLionel Sambuc /* Map the address PROM to lower I/O address range */
42433d6423SLionel Sambuc cntr = inb_el2(dep, EL2_CNTR);
43433d6423SLionel Sambuc outb_el2(dep, EL2_CNTR, cntr | ECNTR_SAPROM);
44433d6423SLionel Sambuc
45433d6423SLionel Sambuc /* Read station address from PROM */
46433d6423SLionel Sambuc for (ix = EL2_EA0; ix <= EL2_EA5; ix += 1)
47f7df02e7SDavid van Moolenbroek dep->de_address.na_addr[ix] = inb_el2(dep, ix);
48433d6423SLionel Sambuc
49433d6423SLionel Sambuc /* Map the 8390 back to lower I/O address range */
50433d6423SLionel Sambuc outb_el2(dep, EL2_CNTR, cntr);
51433d6423SLionel Sambuc
52433d6423SLionel Sambuc /* Enable memory, but turn off interrupts until we are ready */
53433d6423SLionel Sambuc outb_el2(dep, EL2_CFGR, ECFGR_IRQOFF);
54433d6423SLionel Sambuc
55433d6423SLionel Sambuc dep->de_data_port = dep->de_dp8390_port = dep->de_base_port;
56433d6423SLionel Sambuc dep->de_prog_IO = 0; /* Programmed I/O not yet available */
57433d6423SLionel Sambuc
58433d6423SLionel Sambuc /* Check width of data bus:
59433d6423SLionel Sambuc * 1. Write 0 to WTS bit. The board will drive it to 1 if it is a
60433d6423SLionel Sambuc * 16-bit card.
61433d6423SLionel Sambuc * 2. Select page 2
62433d6423SLionel Sambuc * 3. See if it is a 16-bit card
63433d6423SLionel Sambuc * 4. Select page 0
64433d6423SLionel Sambuc */
65433d6423SLionel Sambuc outb_el2(dep, DP_CR, CR_PS_P0|CR_DM_ABORT|CR_STP);
66433d6423SLionel Sambuc outb_el2(dep, DP_DCR, 0);
67433d6423SLionel Sambuc outb_el2(dep, DP_CR, CR_PS_P2|CR_DM_ABORT|CR_STP);
68433d6423SLionel Sambuc dep->de_16bit = (inb_el2(dep, DP_DCR) & DCR_WTS) != 0;
69433d6423SLionel Sambuc outb_el2(dep, DP_CR, CR_PS_P0|CR_DM_ABORT|CR_STP);
70433d6423SLionel Sambuc
71433d6423SLionel Sambuc /* Allocate one send buffer (1.5KB) per 8KB of on board memory. */
72433d6423SLionel Sambuc sendq_nr = (dep->de_ramsize - dep->de_offset_page) / 0x2000;
73433d6423SLionel Sambuc if (sendq_nr < 1)
74433d6423SLionel Sambuc sendq_nr = 1;
75433d6423SLionel Sambuc else if (sendq_nr > SENDQ_NR)
76433d6423SLionel Sambuc sendq_nr = SENDQ_NR;
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc dep->de_sendq_nr = sendq_nr;
79433d6423SLionel Sambuc for (ix = 0; ix < sendq_nr; ix++)
80433d6423SLionel Sambuc dep->de_sendq[ix].sq_sendpage = (ix * SENDQ_PAGES) + EL2_SM_START_PG;
81433d6423SLionel Sambuc
82433d6423SLionel Sambuc dep->de_startpage = (ix * SENDQ_PAGES) + EL2_SM_START_PG;
83433d6423SLionel Sambuc dep->de_stoppage = EL2_SM_STOP_PG;
84433d6423SLionel Sambuc
85433d6423SLionel Sambuc outb_el2(dep, EL2_STARTPG, dep->de_startpage);
86433d6423SLionel Sambuc outb_el2(dep, EL2_STOPPG, dep->de_stoppage);
87433d6423SLionel Sambuc
88433d6423SLionel Sambuc /* Point the vector pointer registers somewhere ?harmless?. */
89433d6423SLionel Sambuc outb_el2(dep, EL2_VP2, 0xFF); /* Point at the ROM restart location */
90433d6423SLionel Sambuc outb_el2(dep, EL2_VP1, 0xFF); /* 0xFFFF:0000 (from original sources) */
91433d6423SLionel Sambuc outb_el2(dep, EL2_VP0, 0x00); /* - What for protected mode? */
92433d6423SLionel Sambuc
93433d6423SLionel Sambuc /* Set interrupt level for 3c503 */
94433d6423SLionel Sambuc irq = (dep->de_irq &= ~DEI_DEFAULT); /* Strip the default flag. */
95433d6423SLionel Sambuc if (irq == 9) irq = 2;
96433d6423SLionel Sambuc if (irq < 2 || irq > 5) panic("bad 3c503 irq configuration: %d", irq);
97433d6423SLionel Sambuc outb_el2(dep, EL2_IDCFG, (0x04 << irq));
98433d6423SLionel Sambuc
99433d6423SLionel Sambuc outb_el2(dep, EL2_DRQCNT, 0x08); /* Set burst size to 8 */
100433d6423SLionel Sambuc outb_el2(dep, EL2_DMAAH, EL2_SM_START_PG); /* Put start of TX */
101433d6423SLionel Sambuc outb_el2(dep, EL2_DMAAL, 0x00); /* buffer in the GA DMA reg */
102433d6423SLionel Sambuc
103433d6423SLionel Sambuc outb_el2(dep, EL2_CFGR, ECFGR_NORM); /* Enable shared memory */
104433d6423SLionel Sambuc
105433d6423SLionel Sambuc if (!debug) {
106433d6423SLionel Sambuc printf("%s: 3c503 at %X:%d:%lX\n",
107f7df02e7SDavid van Moolenbroek netdriver_name(), dep->de_base_port, dep->de_irq,
108433d6423SLionel Sambuc dep->de_linmem + dep->de_offset_page);
109433d6423SLionel Sambuc } else {
110433d6423SLionel Sambuc printf("%s: 3Com Etherlink II %sat I/O address 0x%X, "
111433d6423SLionel Sambuc "memory address 0x%lX, irq %d\n",
112f7df02e7SDavid van Moolenbroek netdriver_name(), dep->de_16bit ? "(16-bit) " : "",
113433d6423SLionel Sambuc dep->de_base_port,
114433d6423SLionel Sambuc dep->de_linmem + dep->de_offset_page,
115433d6423SLionel Sambuc dep->de_irq);
116433d6423SLionel Sambuc }
117433d6423SLionel Sambuc }
118433d6423SLionel Sambuc
119433d6423SLionel Sambuc /*===========================================================================*
120433d6423SLionel Sambuc * el2_stop *
121433d6423SLionel Sambuc *===========================================================================*/
el2_stop(dep)122433d6423SLionel Sambuc static void el2_stop(dep)
123433d6423SLionel Sambuc dpeth_t * dep;
124433d6423SLionel Sambuc {
125433d6423SLionel Sambuc /* Stops board by disabling interrupts. */
126433d6423SLionel Sambuc
127433d6423SLionel Sambuc #if DEBUG
128f7df02e7SDavid van Moolenbroek printf("%s: stopping Etherlink\n", netdriver_name());
129433d6423SLionel Sambuc #endif
130433d6423SLionel Sambuc outb_el2(dep, EL2_CFGR, ECFGR_IRQOFF);
131433d6423SLionel Sambuc return;
132433d6423SLionel Sambuc }
133433d6423SLionel Sambuc
134433d6423SLionel Sambuc /*===========================================================================*
135433d6423SLionel Sambuc * el2_probe *
136433d6423SLionel Sambuc *===========================================================================*/
el2_probe(dep)137433d6423SLionel Sambuc int el2_probe(dep)
138433d6423SLionel Sambuc dpeth_t * dep;
139433d6423SLionel Sambuc {
140433d6423SLionel Sambuc /* Probe for the presence of an EtherLink II card. Initialize memory
141433d6423SLionel Sambuc * addressing if card detected.
142433d6423SLionel Sambuc */
143433d6423SLionel Sambuc int iobase, membase;
144433d6423SLionel Sambuc int thin;
145433d6423SLionel Sambuc
146433d6423SLionel Sambuc /* Thin ethernet or AUI? */
147433d6423SLionel Sambuc thin = (dep->de_linmem & 1) ? ECNTR_AUI : ECNTR_THIN;
148433d6423SLionel Sambuc
149433d6423SLionel Sambuc /* Location registers should have 1 bit set */
150433d6423SLionel Sambuc if (!(iobase = inb_el2(dep, EL2_IOBASE))) return 0;
151433d6423SLionel Sambuc if (!((membase = inb_el2(dep, EL2_MEMBASE)) & 0xF0)) return 0;
152433d6423SLionel Sambuc if ((iobase & (iobase - 1)) || (membase & (membase - 1))) return 0;
153433d6423SLionel Sambuc
154433d6423SLionel Sambuc /* Resets board */
155433d6423SLionel Sambuc outb_el2(dep, EL2_CNTR, ECNTR_RESET | thin);
156*d4dd6511Srlfnb micro_delay(1000);
157433d6423SLionel Sambuc outb_el2(dep, EL2_CNTR, thin);
158*d4dd6511Srlfnb micro_delay(5000);
159433d6423SLionel Sambuc
160433d6423SLionel Sambuc /* Map the address PROM to lower I/O address range */
161433d6423SLionel Sambuc outb_el2(dep, EL2_CNTR, ECNTR_SAPROM | thin);
162433d6423SLionel Sambuc if (inb_el2(dep, EL2_EA0) != 0x02 || /* Etherlink II Station address */
163433d6423SLionel Sambuc inb_el2(dep, EL2_EA1) != 0x60 || /* MUST be 02:60:8c:xx:xx:xx */
164433d6423SLionel Sambuc inb_el2(dep, EL2_EA2) != 0x8C)
165433d6423SLionel Sambuc return 0; /* No Etherlink board at this address */
166433d6423SLionel Sambuc
167433d6423SLionel Sambuc /* Map the 8390 back to lower I/O address range */
168433d6423SLionel Sambuc outb_el2(dep, EL2_CNTR, thin);
169433d6423SLionel Sambuc
170433d6423SLionel Sambuc /* Setup shared memory addressing for 3c503 */
171433d6423SLionel Sambuc dep->de_linmem = ((membase & 0xC0) ? EL2_BASE_0D8000 : EL2_BASE_0C8000) +
172433d6423SLionel Sambuc ((membase & 0xA0) ? (EL2_BASE_0CC000 - EL2_BASE_0C8000) : 0x0000);
173433d6423SLionel Sambuc dep->de_offset_page = (EL2_SM_START_PG * DP_PAGESIZE);
174433d6423SLionel Sambuc dep->de_ramsize = (EL2_SM_STOP_PG - EL2_SM_START_PG) * DP_PAGESIZE;
175433d6423SLionel Sambuc
176433d6423SLionel Sambuc /* (Bad kludge, something Philip needs to look into. -- kjb) */
177433d6423SLionel Sambuc dep->de_linmem -= dep->de_offset_page;
178433d6423SLionel Sambuc dep->de_ramsize += dep->de_offset_page;
179433d6423SLionel Sambuc
180433d6423SLionel Sambuc /* Board initialization and stop functions */
181433d6423SLionel Sambuc dep->de_initf = el2_init;
182433d6423SLionel Sambuc dep->de_stopf = el2_stop;
183433d6423SLionel Sambuc return 1;
184433d6423SLionel Sambuc }
185433d6423SLionel Sambuc
186433d6423SLionel Sambuc #endif /* ENABLE_3C503 */
187433d6423SLionel Sambuc
188433d6423SLionel Sambuc /** 3c503.c **/
189433d6423SLionel Sambuc
190433d6423SLionel Sambuc /*
191433d6423SLionel Sambuc * $PchId: 3c503.c,v 1.3 2003/09/10 15:33:04 philip Exp $
192433d6423SLionel Sambuc */
193