1 | /* |
---|
2 | * RTEMS driver for Minimac ethernet IP-core of Milkymist SoC |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.com/license/LICENSE. |
---|
7 | * |
---|
8 | * $Id$ |
---|
9 | * |
---|
10 | * COPYRIGHT (c) Yann Sionneau <yann.sionneau@telecom-sudparis.eu> (GSoC 2010) |
---|
11 | * Telecom SudParis, France |
---|
12 | */ |
---|
13 | |
---|
14 | |
---|
15 | #include <bsp.h> |
---|
16 | #include "../include/system_conf.h" |
---|
17 | #include "bspopts.h" |
---|
18 | #include <stdio.h> |
---|
19 | #include <rtems/rtems_bsdnet.h> |
---|
20 | #include <sys/param.h> |
---|
21 | #include <sys/mbuf.h> |
---|
22 | #include <sys/socket.h> |
---|
23 | #include <sys/sockio.h> |
---|
24 | #include <net/if.h> |
---|
25 | #include <netinet/in.h> |
---|
26 | #include <netinet/if_ether.h> |
---|
27 | #include <rtems.h> |
---|
28 | #include "network.h" |
---|
29 | |
---|
30 | unsigned int mm_ether_crc32(const unsigned char *buffer, unsigned int len); |
---|
31 | static char rxbuff0[ETHERNET_FRAME_LENGTH] __attribute__((aligned (4))); |
---|
32 | static char rxbuff1[ETHERNET_FRAME_LENGTH] __attribute__((aligned (4))); |
---|
33 | static char rxbuff2[ETHERNET_FRAME_LENGTH] __attribute__((aligned (4))); |
---|
34 | static char rxbuff3[ETHERNET_FRAME_LENGTH] __attribute__((aligned (4))); |
---|
35 | |
---|
36 | static char *rxbuffs[4] = {rxbuff0, rxbuff1, rxbuff2, rxbuff3}; |
---|
37 | |
---|
38 | static struct minimac_softc minimac_softc; |
---|
39 | |
---|
40 | static uint32_t rx_slot_state[4] = {MM_MINIMAC_STATE0, MM_MINIMAC_STATE1, |
---|
41 | MM_MINIMAC_STATE2, MM_MINIMAC_STATE3}; |
---|
42 | |
---|
43 | static uint32_t rx_slot_addr[4] = {MM_MINIMAC_ADDR0, MM_MINIMAC_ADDR1, |
---|
44 | MM_MINIMAC_ADDR2, MM_MINIMAC_ADDR3}; |
---|
45 | |
---|
46 | static uint32_t rx_slot_count[4] = {MM_MINIMAC_COUNT0, MM_MINIMAC_COUNT1, |
---|
47 | MM_MINIMAC_COUNT2, MM_MINIMAC_COUNT3}; |
---|
48 | #ifdef CPU_U32_FIX |
---|
49 | |
---|
50 | /* |
---|
51 | * Routine to align the received packet so that the ip header |
---|
52 | * is on a 32-bit boundary. Necessary for cpu's that do not |
---|
53 | * allow unaligned loads and stores and when the 32-bit DMA |
---|
54 | * mode is used. |
---|
55 | * |
---|
56 | * Transfers are done on word basis to avoid possibly slow byte |
---|
57 | * and half-word writes. |
---|
58 | * |
---|
59 | * Copied over from sonic.c driver |
---|
60 | */ |
---|
61 | |
---|
62 | void ipalign(struct mbuf *m) |
---|
63 | { |
---|
64 | unsigned int *first, *last, data; |
---|
65 | unsigned int tmp = 0; |
---|
66 | |
---|
67 | if ((((int) m->m_data) & 2) && (m->m_len)) { |
---|
68 | last = (unsigned int *) ((((int) m->m_data) + m->m_len + 8) & ~3); |
---|
69 | first = (unsigned int *) (((int) m->m_data) & ~3); |
---|
70 | tmp = *first << 16; |
---|
71 | first++; |
---|
72 | do { |
---|
73 | data = *first; |
---|
74 | *first = tmp | (data >> 16); |
---|
75 | tmp = data << 16; |
---|
76 | first++; |
---|
77 | } while (first <= last); |
---|
78 | |
---|
79 | m->m_data = (caddr_t)(((int) m->m_data) + 2); |
---|
80 | } |
---|
81 | } |
---|
82 | #endif |
---|
83 | |
---|
84 | static inline int minimac_read(unsigned int reg) |
---|
85 | { |
---|
86 | return *((int*)(reg)); |
---|
87 | } |
---|
88 | |
---|
89 | static inline void minimac_write(unsigned int reg, int value) |
---|
90 | { |
---|
91 | *((int*)reg) = value; |
---|
92 | } |
---|
93 | |
---|
94 | int rtems_minimac_driver_attach (struct rtems_bsdnet_ifconfig *config, |
---|
95 | int attaching) |
---|
96 | { |
---|
97 | struct minimac_softc *sc; |
---|
98 | struct ifnet *ifp; |
---|
99 | |
---|
100 | |
---|
101 | if (!attaching) { |
---|
102 | printk ("MINIMAC driver cannot be detached.\n"); |
---|
103 | return 0; |
---|
104 | } |
---|
105 | |
---|
106 | sc = &minimac_softc; |
---|
107 | ifp = &(sc->arpcom.ac_if); |
---|
108 | |
---|
109 | if (sc->registered) { |
---|
110 | printk ("Driver already in use.\n"); |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | sc->registered = 1; |
---|
115 | |
---|
116 | /* |
---|
117 | * Mac address of Milkymist One board is 1 by default |
---|
118 | */ |
---|
119 | |
---|
120 | sc->arpcom.ac_enaddr[0] = 0x00; |
---|
121 | sc->arpcom.ac_enaddr[1] = 0x23; |
---|
122 | sc->arpcom.ac_enaddr[2] = 0x8b; |
---|
123 | sc->arpcom.ac_enaddr[3] = 0x47; |
---|
124 | sc->arpcom.ac_enaddr[4] = 0x86; |
---|
125 | sc->arpcom.ac_enaddr[5] = 0x20; |
---|
126 | ifp->if_softc = sc; |
---|
127 | ifp->if_mtu = ETHERMTU; |
---|
128 | ifp->if_unit = 0; |
---|
129 | ifp->if_name = "minimac"; |
---|
130 | ifp->if_init = minimac_init; |
---|
131 | ifp->if_ioctl = minimac_ioctl; |
---|
132 | ifp->if_start = minimac_start; |
---|
133 | ifp->if_output = ether_output; |
---|
134 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX; |
---|
135 | ifp->if_snd.ifq_maxlen = ifqmaxlen; |
---|
136 | |
---|
137 | if_attach (ifp); |
---|
138 | ether_ifattach(ifp); |
---|
139 | printk("[minimac] Ethernet driver attached\n"); |
---|
140 | return 1; |
---|
141 | } |
---|
142 | |
---|
143 | static void minimac_start(struct ifnet *ifp) |
---|
144 | { |
---|
145 | struct minimac_softc *sc = ifp->if_softc; |
---|
146 | rtems_event_send (sc->txDaemonTid, START_TRANSMIT_EVENT); |
---|
147 | ifp->if_flags |= IFF_OACTIVE; |
---|
148 | // printk("[minimac] start();\n"); |
---|
149 | } |
---|
150 | |
---|
151 | /* |
---|
152 | * Initialize and start the device |
---|
153 | */ |
---|
154 | static void minimac_init (void *arg) |
---|
155 | { |
---|
156 | struct minimac_softc *sc = arg; |
---|
157 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
158 | unsigned char j; |
---|
159 | if (sc->txDaemonTid == 0) { |
---|
160 | sc->txDaemonTid = rtems_bsdnet_newproc ("MINIMACtx", 4096, minimac_txDaemon, sc); |
---|
161 | sc->rxDaemonTid = rtems_bsdnet_newproc ("MINIMACrx", 4096, minimac_rxDaemon, sc); |
---|
162 | set_vector(minimac_rx_interrupt_handler, IRQ_ETHRX, 1); |
---|
163 | set_vector(minimac_tx_interrupt_handler, IRQ_ETHTX, 1); |
---|
164 | lm32_interrupt_unmask(MM_ETHRX_IRQMASK); |
---|
165 | lm32_interrupt_unmask(MM_ETHTX_IRQMASK); |
---|
166 | } |
---|
167 | |
---|
168 | /* |
---|
169 | * Tell the world that we're running. |
---|
170 | */ |
---|
171 | ifp->if_flags |= IFF_RUNNING; |
---|
172 | |
---|
173 | /* |
---|
174 | * Start the receiver and transmitter |
---|
175 | */ |
---|
176 | |
---|
177 | lm32_interrupt_ack( MM_ETHTX_IRQMASK | MM_ETHRX_IRQMASK ); |
---|
178 | minimac_write(MM_MINIMAC_TXREMAINING, 0); |
---|
179 | |
---|
180 | for (j = 0 ; j < NB_RX_SLOTS ; j++) { |
---|
181 | minimac_write(rx_slot_addr[j], (unsigned int)rxbuffs[j]); |
---|
182 | minimac_write(rx_slot_state[j], MINIMAC_STATE_LOADED); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | minimac_write(MM_MINIMAC_SETUP, 0); |
---|
187 | rtems_event_send(sc->rxDaemonTid, INTERRUPT_EVENT); |
---|
188 | } |
---|
189 | |
---|
190 | static void minimac_stop (struct minimac_softc *sc) |
---|
191 | { |
---|
192 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
193 | unsigned char j; |
---|
194 | ifp->if_flags &= ~IFF_RUNNING; |
---|
195 | |
---|
196 | /* |
---|
197 | * Shuts down receiver and transmitter |
---|
198 | */ |
---|
199 | for (j = 0 ; j < NB_RX_SLOTS ; j++) |
---|
200 | minimac_write(rx_slot_state[j], MINIMAC_STATE_EMPTY); |
---|
201 | minimac_write(MM_MINIMAC_TXREMAINING, 0); |
---|
202 | minimac_write(MM_MINIMAC_SETUP, MINIMAC_SETUP_RXRST | MINIMAC_SETUP_TXRST); |
---|
203 | } |
---|
204 | |
---|
205 | static int minimac_ioctl(struct ifnet *ifp, ioctl_command_t command, caddr_t data) |
---|
206 | { |
---|
207 | |
---|
208 | struct minimac_softc *sc = ifp->if_softc; |
---|
209 | int error = 0; |
---|
210 | switch (command) { |
---|
211 | case SIOCGIFADDR: |
---|
212 | case SIOCSIFADDR: |
---|
213 | ether_ioctl (ifp, command, data); |
---|
214 | break; |
---|
215 | |
---|
216 | case SIOCSIFFLAGS: |
---|
217 | switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { |
---|
218 | case IFF_RUNNING: |
---|
219 | minimac_stop (sc); |
---|
220 | break; |
---|
221 | |
---|
222 | case IFF_UP: |
---|
223 | minimac_init (sc); |
---|
224 | break; |
---|
225 | |
---|
226 | case IFF_UP | IFF_RUNNING: |
---|
227 | minimac_stop (sc); |
---|
228 | minimac_init (sc); |
---|
229 | break; |
---|
230 | |
---|
231 | default: |
---|
232 | break; |
---|
233 | } |
---|
234 | break; |
---|
235 | |
---|
236 | case SIO_RTEMS_SHOW_STATS: |
---|
237 | minimac_stats (sc); |
---|
238 | break; |
---|
239 | |
---|
240 | /* |
---|
241 | * FIXME: All sorts of multicast commands need to be added here! |
---|
242 | */ |
---|
243 | default: |
---|
244 | error = EINVAL; |
---|
245 | break; |
---|
246 | } |
---|
247 | return error; |
---|
248 | |
---|
249 | |
---|
250 | } |
---|
251 | |
---|
252 | rtems_isr minimac_rx_interrupt_handler(rtems_vector_number vector) |
---|
253 | { |
---|
254 | unsigned int ip; |
---|
255 | struct minimac_softc *sc = &minimac_softc; |
---|
256 | lm32_read_interrupts(ip); |
---|
257 | if (ip & MM_ETHRX_IRQMASK) { |
---|
258 | lm32_interrupt_mask(MM_ETHRX_IRQMASK); |
---|
259 | rtems_event_send(sc->rxDaemonTid, INTERRUPT_EVENT); |
---|
260 | sc->rxInterrupts++; // update stats |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | rtems_isr minimac_tx_interrupt_handler(rtems_vector_number vector) |
---|
265 | { |
---|
266 | int ip; |
---|
267 | struct minimac_softc *sc = &minimac_softc; |
---|
268 | lm32_read_interrupts(ip); |
---|
269 | if (ip & MM_ETHTX_IRQMASK) { |
---|
270 | lm32_interrupt_ack(MM_ETHTX_IRQMASK); |
---|
271 | rtems_event_send(sc->txDaemonTid, INTERRUPT_EVENT); |
---|
272 | sc->txInterrupts++; // update stats |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | static void minimac_rxDaemon(void *arg) |
---|
278 | { |
---|
279 | struct ifnet *ifp = &minimac_softc.arpcom.ac_if; |
---|
280 | rtems_event_set events; |
---|
281 | struct minimac_softc *sc = &minimac_softc; |
---|
282 | for (;;) { |
---|
283 | uint32_t *buf; |
---|
284 | int rxlen; |
---|
285 | uint8_t j; |
---|
286 | struct mbuf *m; |
---|
287 | struct ether_header *eh; |
---|
288 | rtems_bsdnet_event_receive( RTEMS_ALL_EVENTS, |
---|
289 | RTEMS_WAIT | RTEMS_EVENT_ANY, RTEMS_NO_TIMEOUT, &events); |
---|
290 | if(minimac_read(MM_MINIMAC_SETUP) & MINIMAC_SETUP_RXRST ) { |
---|
291 | printk("Minimac RX FIFO overflow!\n"); |
---|
292 | minimac_write(MM_MINIMAC_SETUP, 0); |
---|
293 | lm32_interrupt_ack(MM_ETHRX_IRQMASK); |
---|
294 | lm32_interrupt_unmask(MM_ETHRX_IRQMASK); |
---|
295 | sc->txFifoFull++; // update stats |
---|
296 | } |
---|
297 | |
---|
298 | for (j = 0 ; j < NB_RX_SLOTS ; j++) { |
---|
299 | if (minimac_read(rx_slot_state[j]) == MINIMAC_STATE_PENDING) { |
---|
300 | __asm__ volatile( /* Invalidate Level-1 data cache */ |
---|
301 | "wcsr DCC, r0\n" |
---|
302 | "nop\n" |
---|
303 | ); |
---|
304 | rxlen = minimac_read(rx_slot_count[j]); |
---|
305 | rxlen -= 8; // we drop the preamble |
---|
306 | MGETHDR(m, M_WAIT, MT_DATA); |
---|
307 | MCLGET(m, M_WAIT); |
---|
308 | m->m_pkthdr.rcvif = ifp; |
---|
309 | buf = (uint32_t *) mtod(m, uint32_t*); |
---|
310 | memcpy(buf, (uint8_t *)minimac_read(rx_slot_addr[j]) + 8, rxlen); |
---|
311 | m->m_len = m->m_pkthdr.len = rxlen - sizeof(uint32_t) - sizeof(struct ether_header); |
---|
312 | |
---|
313 | minimac_write(rx_slot_state[j], MINIMAC_STATE_EMPTY); |
---|
314 | minimac_write(rx_slot_state[j], MINIMAC_STATE_LOADED); |
---|
315 | eh = mtod(m, struct ether_header*); |
---|
316 | m->m_data += sizeof(struct ether_header); |
---|
317 | #ifdef CPU_U32_FIX |
---|
318 | ipalign(m); |
---|
319 | #endif |
---|
320 | ether_input(ifp, eh, m); |
---|
321 | } |
---|
322 | } |
---|
323 | lm32_interrupt_ack(MM_ETHRX_IRQMASK); // we ack once for all the rx interruptions |
---|
324 | lm32_interrupt_unmask(MM_ETHRX_IRQMASK); |
---|
325 | } |
---|
326 | } |
---|
327 | static void minimac_txDaemon(void *arg) |
---|
328 | { |
---|
329 | struct ifnet *ifp = &minimac_softc.arpcom.ac_if; |
---|
330 | rtems_event_set events; |
---|
331 | struct mbuf *m; |
---|
332 | int txq; |
---|
333 | |
---|
334 | for (;;) { |
---|
335 | rtems_bsdnet_event_receive (START_TRANSMIT_EVENT | INTERRUPT_EVENT, RTEMS_EVENT_ANY | RTEMS_WAIT, RTEMS_NO_TIMEOUT, &events); |
---|
336 | for (;;) { |
---|
337 | txq = 2048; |
---|
338 | |
---|
339 | if (txq < ifp->if_mtu) |
---|
340 | break; |
---|
341 | |
---|
342 | IF_DEQUEUE(&ifp->if_snd, m); |
---|
343 | |
---|
344 | if (!m) |
---|
345 | break; |
---|
346 | minimac_sendpacket(ifp, m); |
---|
347 | m_freem(m); |
---|
348 | } |
---|
349 | ifp->if_flags &= ~IFF_OACTIVE; |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | static void minimac_stats(struct minimac_softc *sc) |
---|
354 | { |
---|
355 | |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | static void minimac_sendpacket(struct ifnet *ifp, struct mbuf *m) |
---|
360 | { |
---|
361 | struct mbuf *nm = m; |
---|
362 | struct minimac_softc *sc = &minimac_softc; |
---|
363 | unsigned int len = 0; |
---|
364 | struct mm_packet p; |
---|
365 | unsigned int crc; |
---|
366 | uint8_t i; |
---|
367 | for (i = 0 ; i < 7 ; i++) // Preamble |
---|
368 | p.preamble[i] = 0x55; |
---|
369 | p.preamble[7] = 0xd5; |
---|
370 | |
---|
371 | do |
---|
372 | { |
---|
373 | unsigned int mlen; |
---|
374 | mlen = nm->m_len; |
---|
375 | if (nm->m_len > 0) { |
---|
376 | m_copydata(nm, 0, mlen, p.raw_data + len); |
---|
377 | len += nm->m_len; |
---|
378 | } |
---|
379 | |
---|
380 | } while ( (nm = nm->m_next) != 0 ); |
---|
381 | for ( ; len < 60 ; len++) |
---|
382 | p.raw_data[len] = 0x00; // Padding |
---|
383 | |
---|
384 | crc = mm_ether_crc32((uint8_t *)p.raw_data, len); // CRC32 |
---|
385 | |
---|
386 | p.raw_data[len] = crc & 0xff; |
---|
387 | p.raw_data[len+1] = (crc & 0xff00) >> 8; |
---|
388 | p.raw_data[len+2] = (crc & 0xff0000) >> 16; |
---|
389 | p.raw_data[len+3] = crc >> 24; |
---|
390 | |
---|
391 | len += 4; // We add 4 bytes of CRC32 |
---|
392 | |
---|
393 | if (len + 8 < 64) { |
---|
394 | printk("[minimac] Packet is too small !\n"); |
---|
395 | sc->txErrors++; // update stats |
---|
396 | return; |
---|
397 | } |
---|
398 | |
---|
399 | minimac_write(MM_MINIMAC_TXADR, (unsigned int)&p); |
---|
400 | minimac_write(MM_MINIMAC_TXREMAINING, (unsigned int)(len + 8)); |
---|
401 | } |
---|