xref: /minix3/minix/drivers/net/dp8390/3c503.c (revision f7df02e7476731c31f12548e38bcadbaf0233f6a)
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 static void milli_delay(unsigned long millis);
30433d6423SLionel Sambuc 
31433d6423SLionel Sambuc /*===========================================================================*
32433d6423SLionel Sambuc  *				el2_init				     *
33433d6423SLionel Sambuc  *===========================================================================*/
34433d6423SLionel Sambuc static void el2_init(dep)
35433d6423SLionel Sambuc dpeth_t * dep;
36433d6423SLionel Sambuc {
37433d6423SLionel Sambuc   /* Initalize hardware and data structures. */
38433d6423SLionel Sambuc   int ix, irq;
39433d6423SLionel Sambuc   int sendq_nr;
40433d6423SLionel Sambuc   int cntr;
41433d6423SLionel Sambuc 
42433d6423SLionel Sambuc   /* Map the address PROM to lower I/O address range */
43433d6423SLionel Sambuc   cntr = inb_el2(dep, EL2_CNTR);
44433d6423SLionel Sambuc   outb_el2(dep, EL2_CNTR, cntr | ECNTR_SAPROM);
45433d6423SLionel Sambuc 
46433d6423SLionel Sambuc   /* Read station address from PROM */
47433d6423SLionel Sambuc   for (ix = EL2_EA0; ix <= EL2_EA5; ix += 1)
48*f7df02e7SDavid van Moolenbroek 	dep->de_address.na_addr[ix] = inb_el2(dep, ix);
49433d6423SLionel Sambuc 
50433d6423SLionel Sambuc   /* Map the 8390 back to lower I/O address range */
51433d6423SLionel Sambuc   outb_el2(dep, EL2_CNTR, cntr);
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc   /* Enable memory, but turn off interrupts until we are ready */
54433d6423SLionel Sambuc   outb_el2(dep, EL2_CFGR, ECFGR_IRQOFF);
55433d6423SLionel Sambuc 
56433d6423SLionel Sambuc   dep->de_data_port = dep->de_dp8390_port = dep->de_base_port;
57433d6423SLionel Sambuc   dep->de_prog_IO = 0;		/* Programmed I/O not yet available */
58433d6423SLionel Sambuc 
59433d6423SLionel Sambuc   /* Check width of data bus:
60433d6423SLionel Sambuc    * 1. Write 0 to WTS bit.  The board will drive it to 1 if it is a
61433d6423SLionel Sambuc    *    16-bit card.
62433d6423SLionel Sambuc    * 2. Select page 2
63433d6423SLionel Sambuc    * 3. See if it is a 16-bit card
64433d6423SLionel Sambuc    * 4. Select page 0
65433d6423SLionel Sambuc    */
66433d6423SLionel Sambuc   outb_el2(dep, DP_CR, CR_PS_P0|CR_DM_ABORT|CR_STP);
67433d6423SLionel Sambuc   outb_el2(dep, DP_DCR, 0);
68433d6423SLionel Sambuc   outb_el2(dep, DP_CR, CR_PS_P2|CR_DM_ABORT|CR_STP);
69433d6423SLionel Sambuc   dep->de_16bit = (inb_el2(dep, DP_DCR) & DCR_WTS) != 0;
70433d6423SLionel Sambuc   outb_el2(dep, DP_CR, CR_PS_P0|CR_DM_ABORT|CR_STP);
71433d6423SLionel Sambuc 
72433d6423SLionel Sambuc   /* Allocate one send buffer (1.5KB) per 8KB of on board memory. */
73433d6423SLionel Sambuc   sendq_nr = (dep->de_ramsize - dep->de_offset_page) / 0x2000;
74433d6423SLionel Sambuc   if (sendq_nr < 1)
75433d6423SLionel Sambuc 	sendq_nr = 1;
76433d6423SLionel Sambuc   else if (sendq_nr > SENDQ_NR)
77433d6423SLionel Sambuc 	sendq_nr = SENDQ_NR;
78433d6423SLionel Sambuc 
79433d6423SLionel Sambuc   dep->de_sendq_nr = sendq_nr;
80433d6423SLionel Sambuc   for (ix = 0; ix < sendq_nr; ix++)
81433d6423SLionel Sambuc 	dep->de_sendq[ix].sq_sendpage = (ix * SENDQ_PAGES) + EL2_SM_START_PG;
82433d6423SLionel Sambuc 
83433d6423SLionel Sambuc   dep->de_startpage = (ix * SENDQ_PAGES) + EL2_SM_START_PG;
84433d6423SLionel Sambuc   dep->de_stoppage = EL2_SM_STOP_PG;
85433d6423SLionel Sambuc 
86433d6423SLionel Sambuc   outb_el2(dep, EL2_STARTPG, dep->de_startpage);
87433d6423SLionel Sambuc   outb_el2(dep, EL2_STOPPG, dep->de_stoppage);
88433d6423SLionel Sambuc 
89433d6423SLionel Sambuc   /* Point the vector pointer registers somewhere ?harmless?. */
90433d6423SLionel Sambuc   outb_el2(dep, EL2_VP2, 0xFF);	/* Point at the ROM restart location    */
91433d6423SLionel Sambuc   outb_el2(dep, EL2_VP1, 0xFF);	/* 0xFFFF:0000  (from original sources) */
92433d6423SLionel Sambuc   outb_el2(dep, EL2_VP0, 0x00);	/*           - What for protected mode? */
93433d6423SLionel Sambuc 
94433d6423SLionel Sambuc   /* Set interrupt level for 3c503 */
95433d6423SLionel Sambuc   irq = (dep->de_irq &= ~DEI_DEFAULT);	/* Strip the default flag. */
96433d6423SLionel Sambuc   if (irq == 9) irq = 2;
97433d6423SLionel Sambuc   if (irq < 2 || irq > 5) panic("bad 3c503 irq configuration: %d", irq);
98433d6423SLionel Sambuc   outb_el2(dep, EL2_IDCFG, (0x04 << irq));
99433d6423SLionel Sambuc 
100433d6423SLionel Sambuc   outb_el2(dep, EL2_DRQCNT, 0x08);	/* Set burst size to 8 */
101433d6423SLionel Sambuc   outb_el2(dep, EL2_DMAAH, EL2_SM_START_PG);	/* Put start of TX  */
102433d6423SLionel Sambuc   outb_el2(dep, EL2_DMAAL, 0x00);	/* buffer in the GA DMA reg */
103433d6423SLionel Sambuc 
104433d6423SLionel Sambuc   outb_el2(dep, EL2_CFGR, ECFGR_NORM);	/* Enable shared memory */
105433d6423SLionel Sambuc 
106433d6423SLionel Sambuc   if (!debug) {
107433d6423SLionel Sambuc 	printf("%s: 3c503 at %X:%d:%lX\n",
108*f7df02e7SDavid van Moolenbroek 		netdriver_name(), dep->de_base_port, dep->de_irq,
109433d6423SLionel Sambuc 		dep->de_linmem + dep->de_offset_page);
110433d6423SLionel Sambuc   } else {
111433d6423SLionel Sambuc 	printf("%s: 3Com Etherlink II %sat I/O address 0x%X, "
112433d6423SLionel Sambuc 			"memory address 0x%lX, irq %d\n",
113*f7df02e7SDavid van Moolenbroek 		netdriver_name(), dep->de_16bit ? "(16-bit) " : "",
114433d6423SLionel Sambuc 		dep->de_base_port,
115433d6423SLionel Sambuc 		dep->de_linmem + dep->de_offset_page,
116433d6423SLionel Sambuc 		dep->de_irq);
117433d6423SLionel Sambuc   }
118433d6423SLionel Sambuc }
119433d6423SLionel Sambuc 
120433d6423SLionel Sambuc /*===========================================================================*
121433d6423SLionel Sambuc  *				el2_stop				     *
122433d6423SLionel Sambuc  *===========================================================================*/
123433d6423SLionel Sambuc static void el2_stop(dep)
124433d6423SLionel Sambuc dpeth_t * dep;
125433d6423SLionel Sambuc {
126433d6423SLionel Sambuc   /* Stops board by disabling interrupts. */
127433d6423SLionel Sambuc 
128433d6423SLionel Sambuc #if DEBUG
129*f7df02e7SDavid van Moolenbroek   printf("%s: stopping Etherlink\n", netdriver_name());
130433d6423SLionel Sambuc #endif
131433d6423SLionel Sambuc   outb_el2(dep, EL2_CFGR, ECFGR_IRQOFF);
132433d6423SLionel Sambuc   return;
133433d6423SLionel Sambuc }
134433d6423SLionel Sambuc 
135433d6423SLionel Sambuc /*===========================================================================*
136433d6423SLionel Sambuc  *				el2_probe				     *
137433d6423SLionel Sambuc  *===========================================================================*/
138433d6423SLionel Sambuc int el2_probe(dep)
139433d6423SLionel Sambuc dpeth_t * dep;
140433d6423SLionel Sambuc {
141433d6423SLionel Sambuc   /* Probe for the presence of an EtherLink II card.  Initialize memory
142433d6423SLionel Sambuc    * addressing if card detected.
143433d6423SLionel Sambuc    */
144433d6423SLionel Sambuc   int iobase, membase;
145433d6423SLionel Sambuc   int thin;
146433d6423SLionel Sambuc 
147433d6423SLionel Sambuc   /* Thin ethernet or AUI? */
148433d6423SLionel Sambuc   thin = (dep->de_linmem & 1) ? ECNTR_AUI : ECNTR_THIN;
149433d6423SLionel Sambuc 
150433d6423SLionel Sambuc   /* Location registers should have 1 bit set */
151433d6423SLionel Sambuc   if (!(iobase = inb_el2(dep, EL2_IOBASE))) return 0;
152433d6423SLionel Sambuc   if (!((membase = inb_el2(dep, EL2_MEMBASE)) & 0xF0)) return 0;
153433d6423SLionel Sambuc   if ((iobase & (iobase - 1)) || (membase & (membase - 1))) return 0;
154433d6423SLionel Sambuc 
155433d6423SLionel Sambuc   /* Resets board */
156433d6423SLionel Sambuc   outb_el2(dep, EL2_CNTR, ECNTR_RESET | thin);
157433d6423SLionel Sambuc   milli_delay(1);
158433d6423SLionel Sambuc   outb_el2(dep, EL2_CNTR, thin);
159433d6423SLionel Sambuc   milli_delay(5);
160433d6423SLionel Sambuc 
161433d6423SLionel Sambuc   /* Map the address PROM to lower I/O address range */
162433d6423SLionel Sambuc   outb_el2(dep, EL2_CNTR, ECNTR_SAPROM | thin);
163433d6423SLionel Sambuc   if (inb_el2(dep, EL2_EA0) != 0x02 ||	/* Etherlink II Station address */
164433d6423SLionel Sambuc       inb_el2(dep, EL2_EA1) != 0x60 ||	/* MUST be 02:60:8c:xx:xx:xx */
165433d6423SLionel Sambuc       inb_el2(dep, EL2_EA2) != 0x8C)
166433d6423SLionel Sambuc 	return 0;		/* No Etherlink board at this address */
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc   /* Map the 8390 back to lower I/O address range */
169433d6423SLionel Sambuc   outb_el2(dep, EL2_CNTR, thin);
170433d6423SLionel Sambuc 
171433d6423SLionel Sambuc   /* Setup shared memory addressing for 3c503 */
172433d6423SLionel Sambuc   dep->de_linmem = ((membase & 0xC0) ? EL2_BASE_0D8000 : EL2_BASE_0C8000) +
173433d6423SLionel Sambuc 	((membase & 0xA0) ? (EL2_BASE_0CC000 - EL2_BASE_0C8000) : 0x0000);
174433d6423SLionel Sambuc   dep->de_offset_page = (EL2_SM_START_PG * DP_PAGESIZE);
175433d6423SLionel Sambuc   dep->de_ramsize = (EL2_SM_STOP_PG - EL2_SM_START_PG) * DP_PAGESIZE;
176433d6423SLionel Sambuc 
177433d6423SLionel Sambuc   /* (Bad kludge, something Philip needs to look into. -- kjb) */
178433d6423SLionel Sambuc   dep->de_linmem -= dep->de_offset_page;
179433d6423SLionel Sambuc   dep->de_ramsize += dep->de_offset_page;
180433d6423SLionel Sambuc 
181433d6423SLionel Sambuc   /* Board initialization and stop functions */
182433d6423SLionel Sambuc   dep->de_initf = el2_init;
183433d6423SLionel Sambuc   dep->de_stopf = el2_stop;
184433d6423SLionel Sambuc   return 1;
185433d6423SLionel Sambuc }
186433d6423SLionel Sambuc 
187433d6423SLionel Sambuc static void milli_delay(unsigned long millis)
188433d6423SLionel Sambuc {
189433d6423SLionel Sambuc 	tickdelay(MILLIS_TO_TICKS(millis));
190433d6423SLionel Sambuc }
191433d6423SLionel Sambuc 
192433d6423SLionel Sambuc #endif /* ENABLE_3C503 */
193433d6423SLionel Sambuc 
194433d6423SLionel Sambuc /** 3c503.c **/
195433d6423SLionel Sambuc 
196433d6423SLionel Sambuc /*
197433d6423SLionel Sambuc  * $PchId: 3c503.c,v 1.3 2003/09/10 15:33:04 philip Exp $
198433d6423SLionel Sambuc  */
199