if_cs.c revision 1.1 1 /* $NetBSD: if_cs.c,v 1.1 2003/08/09 08:01:48 igy Exp $ */
2
3 /*
4 * Copyright (c) 2003 Naoto Shimazaki.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY NAOTO SHIMAZAKI AND CONTRIBUTORS ``AS IS''
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE NAOTO OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: if_cs.c,v 1.1 2003/08/09 08:01:48 igy Exp $");
30
31 #include <sys/param.h>
32 #include <netinet/in.h>
33
34 #include <lib/libsa/stand.h>
35 #include <lib/libsa/netif.h>
36
37 #include <dev/ic/cs89x0reg.h>
38
39 #include "extern.h"
40
41 static int cs_match(struct netif *, void *);
42 static int cs_probe(struct netif *, void *);
43 static void cs_init(struct iodesc *, void *);
44 static int cs_get(struct iodesc *, void *, size_t, time_t);
45 static int cs_put(struct iodesc *, void *, size_t);
46 static void cs_end(struct netif *);
47
48 static struct netif_stats cs_stats;
49
50 static struct netif_dif cs_if = {
51 .dif_unit = 0,
52 .dif_nsel = 1,
53 .dif_stats = &cs_stats,
54 .dif_private = NULL,
55 .dif_used = 0,
56 };
57
58 struct netif_driver cs_driver = {
59 .netif_bname = "cs",
60 .netif_match = cs_match,
61 .netif_probe = cs_probe,
62 .netif_init = cs_init,
63 .netif_get = cs_get,
64 .netif_put = cs_put,
65 .netif_end = cs_end,
66 .netif_ifs = &cs_if,
67 .netif_nifs = 1,
68 };
69
70 #define CS_IO_BASE 0x14010300U
71
72 #define CS_READ_1(off) REGREAD_1(CS_IO_BASE, (off))
73 #define CS_READ_2(off) REGREAD_2(CS_IO_BASE, (off))
74 #define CS_WRITE_1(off, val) REGWRITE_1(CS_IO_BASE, (off), (val))
75 #define CS_WRITE_2(off, val) REGWRITE_2(CS_IO_BASE, (off), (val))
76 #define CS_READ_PACKET_PAGE(off) \
77 (REGWRITE_2(CS_IO_BASE, PORT_PKTPG_PTR, (off)), \
78 REGREAD_2(CS_IO_BASE, PORT_PKTPG_DATA))
79 #define CS_WRITE_PACKET_PAGE(off, val) \
80 (REGWRITE_2(CS_IO_BASE, PORT_PKTPG_PTR, (off)), \
81 REGWRITE_2(CS_IO_BASE, PORT_PKTPG_DATA, (val)))
82
83 static inline void
84 delay(int n)
85 {
86 int i = 33 * n;
87
88 while (--i > 0)
89 ;
90 }
91
92 time_t
93 getsecs(void)
94 {
95 return REGREAD_4(VRETIMEL, 0) >> 15;
96 }
97
98 static int
99 cs_match(struct netif *nif, void *machdep_hint)
100 {
101 return 1;
102 }
103
104 static int
105 cs_probe(struct netif *nif, void *machdep_hint)
106 {
107 return 0;
108 }
109
110 static void
111 cs_init(struct iodesc *desc, void *machdep_hint)
112 {
113 int i;
114
115 /* Issue a software reset command to the chip */
116 CS_WRITE_PACKET_PAGE(PKTPG_SELF_CTL, SELF_CTL_RESET);
117
118 /* We cannot touch the chip until calibration is done */
119 delay(10000);
120
121 /*
122 * Transition -SBHE H->L L->H is needed between reset and
123 * the first access to the chip's register.
124 */
125 CS_READ_1(PORT_PKTPG_PTR + 0);
126 CS_READ_1(PORT_PKTPG_PTR + 1);
127 CS_READ_1(PORT_PKTPG_PTR + 0);
128 CS_READ_1(PORT_PKTPG_PTR + 1);
129
130 /* wait for INIT_DONE */
131 for (i = 10000; i > 0; i--) {
132 u_int16_t s;
133
134 s = CS_READ_PACKET_PAGE(PKTPG_SELF_ST);
135 if ((s & SELF_ST_INIT_DONE) && !(s & SELF_ST_SI_BUSY))
136 break;
137 }
138
139 if (i == 0)
140 panic("cannot reset netif");
141
142 for (i = 0; i < 6; i += 2) {
143 u_int16_t ea;
144
145 ea = CS_READ_PACKET_PAGE(PKTPG_IND_ADDR + i);
146
147 /* assuming little endian */
148 desc->myea[i + 0] = (ea >> 0) & 0xff;
149 desc->myea[i + 1] = (ea >> 8) & 0xff;
150 }
151
152 /*
153 * Accepting frames:
154 * RX_CTL_RX_OK_A: correct crc, and valid length
155 * RX_CTL_IND_A: dest addr maches individual address
156 * RX_CTL_BCAST_A: dest addr maches broadcast address
157 */
158 CS_WRITE_PACKET_PAGE(PKTPG_RX_CTL,
159 RX_CTL_RX_OK_A | RX_CTL_IND_A | RX_CTL_BCAST_A);
160 CS_WRITE_PACKET_PAGE(PKTPG_LINE_CTL, LINE_CTL_RX_ON | LINE_CTL_TX_ON);
161 }
162
163 static int
164 cs_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
165 {
166 time_t t;
167 int rlen;
168 int i;
169 u_int16_t *p;
170
171 t = getsecs();
172 rlen = 0;
173 while (getsecs() - t < timeout && rlen == 0) {
174 if (!(CS_READ_PACKET_PAGE(PKTPG_RX_EVENT) & RX_EVENT_RX_OK))
175 continue;
176
177 /* drop status */
178 CS_READ_2(PORT_RXTX_DATA);
179
180 /* get frame length */
181 rlen = CS_READ_2(PORT_RXTX_DATA);
182
183 if (rlen > len) {
184 CS_WRITE_PACKET_PAGE(PKTPG_RX_CFG, RX_CFG_SKIP);
185 rlen = 0;
186 continue;
187 }
188
189 p = pkt;
190 for (i = rlen >> 1; i > 0; i--)
191 *p++ = CS_READ_2(PORT_RXTX_DATA);
192 if (rlen & 1)
193 *((u_int8_t *) p + 1) = CS_READ_1(PORT_RXTX_DATA);
194
195 /* exit while loop */
196 }
197
198 return rlen;
199 }
200
201 static int
202 cs_put(struct iodesc *desc, void *pkt, size_t len)
203 {
204 int timeo;
205 int i;
206 u_int16_t *p;
207
208 CS_WRITE_2(PORT_TX_CMD, TX_CMD_START_ALL);
209 CS_WRITE_2(PORT_TX_LENGTH, len);
210
211 for (timeo = 1000000; timeo > 0; timeo--) {
212 if (CS_READ_PACKET_PAGE(PKTPG_BUS_ST) & BUS_ST_RDY4TXNOW)
213 break;
214 }
215 if (timeo == 0)
216 panic("cs: cannot send frame");
217
218 p = pkt;
219 i = (len + 1) >> 1;
220 while (i > 0) {
221 CS_WRITE_2(PORT_RXTX_DATA, *p++);
222 i--;
223 }
224
225 return len;
226 }
227
228 static void
229 cs_end(struct netif *nif)
230 {
231 CS_WRITE_PACKET_PAGE(PKTPG_LINE_CTL, 0);
232 }
233