1 // vim: set et sw=4 sts=4 :
7 /** Tamaño del frame */
10 // Próximo frame a obtener
19 /// Tamaño de la cabecera de los buffers de la placa de red
20 #define BUF_HDR_SIZE 4
22 /// Cambia de página sin modificar los demás bits del CR
23 #define SELECT_REG_PAGE(page) \
26 write_reg(CR, read_reg(CR) & ~(PS1 | PS0)); \
27 write_reg(CR, read_reg(CR) | (page << 6)); \
31 /// Aborta (o completa) el DMA limpiando el ISR
32 #define ABORT_DMA(flags) \
35 write_reg(CR, flags); \
36 write_reg(ISR, RDC); \
41 static void write_reg(unsigned char reg, unsigned char wr_data)
44 // Select register address.
45 ADDR_PORT &= ~ADDR_PORT_MASK;
48 // Output register data to port.
51 // Clock register data into RTL8019AS.
52 // IOR & IOW are both active low.
58 // Set register data port as input again.
59 DATA_PORT = DATA_PORT_MASK;
64 static unsigned char read_reg(unsigned char reg)
67 // Select register address.
68 ADDR_PORT &= ~ADDR_PORT_MASK;
71 // Enable register data output from RTL8019AS.
75 // Read register data from port.
78 // Disable register data output from RTL8019AS.
86 /** Resetea placa de red en caso de buffer overflow */
89 bit retransmit = read_reg(CR) & TXP;
91 // If the receive buffer ring has overflowed we dump the whole
92 // thing and start over. There is no way of knowing whether the
93 // data it contains is uncorrupted, or will cause us grief.
95 // Stop RTL8019AS and abort DMA operation.
98 // Wait for controller to halt after any current tx completes.
99 while(!(read_reg(ISR) & RST)) continue;
101 // Reset remote byte count registers.
102 write_reg(RBCR0, 0x00);
103 write_reg(RBCR1, 0x00);
105 // Check whether currently transmitting a packet.
108 // If neither a successful transmission nor a tx abort error
109 // has occured, then flag current tx packet for resend.
110 if(read_reg(ISR) & (PTX | TXE))
116 // Set transmit configuration register to loopback internally.
117 write_reg(TCR, MODE1);
119 // Restart the RTL8019AS.
120 write_reg(CR, START);
122 // Re-initialise last receive buffer read pointer.
123 write_reg(BNRY, RX_PAGE_START);
125 // Select RTL8019AS register page 1.
128 // Re-initialise current packet receive buffer page pointer.
129 write_reg(CURR, RX_PAGE_START + 1);
131 // Select RTL8019AS register page 0.
134 // Clear rx buffer overflow & packet received interrupt flags.
135 write_reg(ISR, PRX | OVW);
137 // Re-itialise transmit configuration reg for normal operation.
138 write_reg(TCR, MODE0);
142 // Retransmit packet in RTL8019AS local tx buffer.
143 write_reg(CR, START | TXP);
148 /** Inicializa dispositivo de red
149 * @return true si se inicializó correctamente, false si no
155 // Set IOR & IOW as they're active low.
160 // Set register data port as input.
161 DATA_PORT = DATA_PORT_MASK;
163 // Configure RTL8019AS ethernet controller.
165 // Keil startup code takes 4ms to execute (18.432MHz, X1 mode).
166 // That leaves plenty of time for the RTL8019AS to read it's
167 // configuration in from the 9346 EEPROM before we get here.
169 // Select RTL8019AS register page 0.
172 // Check if RTL8019AS fully reset.
173 if(!(read_reg(ISR) & RST))
178 // Stop RTL8019AS, select page 0 and abort DMA operation.
181 // Initialise data configuration register.
182 // FIFO threshold 8 bytes, no loopback, don't use auto send packet.
183 write_reg(DCR, FT1 | LS);
185 // Reset remote byte count registers.
186 write_reg(RBCR0, 0u);
187 write_reg(RBCR1, 0u);
189 // Receive configuration register to monitor mode.
192 // Initialise transmit configuration register to loopback internally.
193 write_reg(TCR, MODE1);
195 // Clear interrupt status register bits by writing 1 to each.
198 // Mask all interrupts in mask register.
199 write_reg(IMR, NONE);
201 // Obtengo MAC de la placa
202 write_reg(RBCR0, 12u); // Vamos a leer 12 bytes (2 x 6)
203 write_reg(RBCR1, 0u);
204 write_reg(RSAR0, 0u); // En la dirección 0x0000
205 write_reg(RSAR1, 0u);
206 write_reg(CR, READ); // Comienza lectura
207 for (i = 0; i < ETH_ADDR_SIZE; ++i)
209 eth_addr_local[i] = read_reg(RDMA);
210 read_reg(RDMA); // Ignoramos porque viene como un word
213 // Abort/ complete DMA operation.
216 // Set transmit page start.
217 write_reg(TPSR, TX_PAGE_START);
219 // Set receive buffer page start.
220 write_reg(PSTART, RX_PAGE_START);
222 // Initialise last receive buffer read pointer.
223 write_reg(BNRY, RX_PAGE_START);
225 // Set receive buffer page stop.
226 write_reg(PSTOP, RX_PAGE_STOP);
228 // Select RTL8019AS register page 1.
231 // Initialise current packet receive buffer page pointer
232 write_reg(CURR, RX_PAGE_START + 1);
234 // Set physical address
235 for (i = 0; i < ETH_ADDR_SIZE; ++i)
236 write_reg(PAR_BASE + i, eth_addr_local[i]);
238 // Restart RTL8019AS.
239 write_reg(CR, START);
241 // Initialise transmit configuration register for normal operation.
242 write_reg(TCR, MODE0);
244 // Receive configuration register to accept broadcast packets.
251 /** Comienza el envío de un nuevo frame */
252 void netdev_send_start()
254 // Wait until pending transmit operation completes.
255 while (read_reg(CR) & TXP) continue;
256 write_reg(ISR, PTX); // Limpio bit de interrupción
258 // Set remote DMA start address registers to indicate where to load packet.
259 write_reg(RSAR0, 0u);
260 write_reg(RSAR1, TX_PAGE_START);
263 /** Finaliza el envío del frame
264 * @precond netdev_send_start() debe haber sido ejecutada
265 * @precond se copiaron datos al dispositivo para enviar
266 * @param len Cantidad de bytes a transmitir
268 void netdev_send_end(byte len)
270 // Set transmit page start to indicate packet start.
271 write_reg(TPSR, TX_PAGE_START);
273 // Ethernet packets must be > 60 bytes, otherwise are rejected as runts.
274 if (len < MIN_PACKET_LEN)
275 len = MIN_PACKET_LEN;
277 // Set transmit byte count registers to indicate packet length.
278 write_reg(TBCR0, len);
279 write_reg(TBCR1, 0u);
281 // Issue command for RTL8019AS to transmit packet from it's local buffer.
282 write_reg(CR, START | TXP);
285 void netdev_write_start(byte len)
287 // Set remote DMA byte count registers to indicate length of packet load.
288 write_reg(RBCR0, len);
289 write_reg(RBCR1, 0u);
291 // Initiate DMA transfer of uip_buf & uip_appdata buffers to RTL8019AS.
292 write_reg(CR, WRITE);
295 void netdev_write_start_at(byte offset, byte len)
297 // Set remote DMA start address registers to packet data.
298 write_reg(RSAR0, offset);
299 write_reg(RSAR1, TX_PAGE_START);
301 // Set remote DMA byte count registers to indicate length of packet load.
302 write_reg(RBCR0, len);
303 write_reg(RBCR1, 0u);
305 // Initiate DMA transfer of uip_buf & uip_appdata buffers to RTL8019AS.
306 write_reg(CR, WRITE);
309 /** Escribe un byte al buffer de la placa de red para ser enviado
310 * @precond netdev_send_start() debe haber sido ejecutada
311 * @param b Byte a enviar
313 void netdev_write_byte(byte b)
318 /** Escribe un word al buffer de la placa de red para ser enviado
319 * @precond netdev_send_start() debe haber sido ejecutada
320 * @param w Word a enviar
322 void netdev_write_word(uint16 w)
324 write_reg(RDMA, HIGH(w));
325 write_reg(RDMA, LOW(w));
328 void netdev_write_end()
330 // Abort/ complete DMA operation.
334 /** Comienza la lectura de un nuevo frame
335 * @return Cantidad de bytes a recibir
337 byte netdev_recv_start()
339 // Check if the rx buffer has overflowed.
340 if (read_reg(ISR) & OVW)
345 current = read_reg(CURR);
348 // Hack: a veces reporta mal el flag de OVW, así que verificamos que
349 // relamente haya habido overflow.
350 if (read_reg(BNRY) == current)
352 printb(read_reg(ISR), 0x01);
353 printb(read_reg(BNRY), 0x02);
354 printb(current, 0x04);
360 // Check if there is a packet in the rx buffer.
361 else if (read_reg(ISR) & PRX)
367 // Retrieve packet header. (status, next_ptr, length_l, length_h)
369 // Obtiene el buffer a leer actualmente
370 recv_state.curr_buf = read_reg(BNRY) + 1;
371 if (recv_state.curr_buf >= RX_PAGE_STOP)
372 recv_state.curr_buf = RX_PAGE_START;
374 // Select RTL8019AS register page 1.
377 // Retrieve current receive buffer page
378 current = read_reg(CURR);
380 // Select RTL8019AS register page 1.
383 // Check if last packet has been removed from rx buffer.
384 if(recv_state.curr_buf == current)
386 // Clear packet received interrupt flag.
387 write_reg(ISR, PRX | RXE);
391 // Set remote DMA byte count registers to packet header length.
392 recv_state.curr_off = 0;
393 netdev_read_start(BUF_HDR_SIZE);
396 status = netdev_read_byte();
398 // Save next packet pointer.
399 recv_state.next_buf = netdev_read_byte() - 1;
401 // Retrieve packet data length and subtract CRC bytes.
402 len = netdev_read_byte() - BUF_HDR_SIZE;
404 // Si es muy grande, muy chico o hubo error, lo descartamos
405 if ((len < MIN_PACKET_LEN) || (len > MAX_PACKET_LEN)
406 || ((status & 0x0F) != RXSOK)
407 || netdev_read_byte()) // Parte alta del tamaño
409 // Terminamos DMA y pasamos al próximo frame
411 write_reg(BNRY, recv_state.next_buf);
415 // Abort/ complete DMA operation.
424 /** Finaliza la recepción del frame
425 * @precond netdev_recv_start() debe haber sido ejecutada
427 void netdev_recv_end()
429 // Pasa el próximo frame
430 write_reg(BNRY, recv_state.next_buf);
433 void netdev_read_start(byte len)
435 // Set remote DMA start address registers to packet data.
436 write_reg(RSAR0, recv_state.curr_off);
437 write_reg(RSAR1, recv_state.curr_buf);
438 recv_state.curr_off += len;
440 // Set remote DMA byte count registers to packet data length.
441 write_reg(RBCR0, len);
444 // Initiate DMA transfer of packet data.
448 /** Lee un byte del buffer de la placa de red
449 * @precond netdev_recv_start() debe haber sido ejecutada
451 byte netdev_read_byte()
453 return read_reg(RDMA);
456 /** Lee un word del buffer de la placa de red
457 * @precond netdev_recv_start() debe haber sido ejecutada
459 uint16 netdev_read_word()
461 uint16 w = read_reg(RDMA) << 8;
462 return w + read_reg(RDMA);
465 /** Finaliza la lectura del frame
466 * @precond netdev_recv_start() debe haber sido ejecutada
468 void netdev_read_end()