2 * sh_eth.c - Driver for Renesas SH7763's ethernet controler.
4 * Copyright (C) 2008 Renesas Solutions Corp.
5 * Copyright (c) 2008 Nobuhiro Iwamatsu
6 * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <asm/errno.h>
32 #ifndef CONFIG_SH_ETHER_USE_PORT
33 # error "Please define CONFIG_SH_ETHER_USE_PORT"
35 #ifndef CONFIG_SH_ETHER_PHY_ADDR
36 # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
39 extern int eth_init(bd_t *bd);
40 extern void eth_halt(void);
41 extern int eth_rx(void);
42 extern int eth_send(volatile void *packet, int length);
44 static struct dev_info_s *dev;
47 * Bits are written to the PHY serially using the
48 * PIR register, just like a bit banger.
50 static void sh_eth_mii_write_phy_bits(int port, u32 val, int len)
55 /* Bit positions is 1 less than the number of bits */
56 for (i = len - 1; i >= 0; i--) {
57 /* Write direction, bit to write, clock is low */
58 pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
61 /* Write direction, bit to write, clock is high */
62 pir = 3 | ((val & 1 << i) ? 1 << 2 : 0);
65 /* Write direction, bit to write, clock is low */
66 pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
72 static void sh_eth_mii_bus_release(int port)
74 /* Read direction, clock is low */
77 /* Read direction, clock is high */
80 /* Read direction, clock is low */
85 static void sh_eth_mii_ind_bus_release(int port)
87 /* Read direction, clock is low */
92 static int sh_eth_mii_read_phy_bits(int port, u32 * val, int len)
98 for (i = len - 1; i >= 0; i--) {
99 /* Read direction, clock is high */
103 pir = inl(PIR(port));
104 *val |= (pir & 8) ? 1 << i : 0;
105 /* Read direction, clock is low */
113 #define PHY_INIT 0xFFFFFFFF
114 #define PHY_READ 0x02
115 #define PHY_WRITE 0x01
117 * To read a phy register, mii managements frames are sent to the phy.
118 * The frames look like this:
119 * pre (32 bits): 0xffff ffff
121 * op (2bits): 10: read 01: write
122 * phyad (5 bits): xxxxx
123 * regad (5 bits): xxxxx
125 * data (16 bits): read data
127 static u32 sh_eth_mii_read_phy_reg(int port, u8 phy_addr, int reg)
131 /* Sent mii management frame */
133 sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
134 /* st (start of frame) */
135 sh_eth_mii_write_phy_bits(port, 0x1, 2);
137 sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
139 sh_eth_mii_write_phy_bits(port, phy_addr, 5);
140 /* Register to read */
141 sh_eth_mii_write_phy_bits(port, reg, 5);
144 sh_eth_mii_bus_release(port);
147 sh_eth_mii_read_phy_bits(port, &val, 16);
153 * To write a phy register, mii managements frames are sent to the phy.
154 * The frames look like this:
155 * pre (32 bits): 0xffff ffff
157 * op (2bits): 10: read 01: write
158 * phyad (5 bits): xxxxx
159 * regad (5 bits): xxxxx
161 * data (16 bits): write data
162 * idle (Independent bus release)
164 static void sh_eth_mii_write_phy_reg(int port, u8 phy_addr, int reg, u16 val)
166 /* Sent mii management frame */
168 sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
169 /* st (start of frame) */
170 sh_eth_mii_write_phy_bits(port, 0x1, 2);
172 sh_eth_mii_write_phy_bits(port, PHY_WRITE, 2);
174 sh_eth_mii_write_phy_bits(port, phy_addr, 5);
175 /* Register to read */
176 sh_eth_mii_write_phy_bits(port, reg, 5);
178 sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
179 /* Write register data */
180 sh_eth_mii_write_phy_bits(port, val, 16);
182 /* Independent bus release */
183 sh_eth_mii_ind_bus_release(port);
190 int eth_send(volatile void *packet, int len)
192 int port = dev->port;
193 struct port_info_s *port_info = &dev->port_info[port];
197 if (!packet || len > 0xffff) {
198 printf("eth_send: Invalid argument\n");
202 /* packet must be a 4 byte boundary */
203 if ((int)packet & (4 - 1)) {
204 printf("eth_send: packet not 4 byte alligned\n");
208 /* Update tx descriptor */
209 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
210 port_info->tx_desc_cur->td1 = len << 16;
211 /* Must preserve the end of descriptor list indication */
212 if (port_info->tx_desc_cur->td0 & TD_TDLE)
213 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
215 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
217 /* Restart the transmitter if disabled */
218 if (!(inl(EDTRR(port)) & EDTRR_TRNS))
219 outl(EDTRR_TRNS, EDTRR(port));
221 /* Wait until packet is transmitted */
223 while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
227 printf("eth_send: transmit timeout\n");
233 port_info->tx_desc_cur++;
234 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
235 port_info->tx_desc_cur = port_info->tx_desc_base;
242 int port = dev->port;
243 struct port_info_s *port_info = &dev->port_info[port];
247 /* Check if the rx descriptor is ready */
248 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
249 /* Check for errors */
250 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
251 len = port_info->rx_desc_cur->rd1 & 0xffff;
252 packet = (volatile u8 *)
253 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
254 NetReceive(packet, len);
257 /* Make current descriptor available again */
258 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
259 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
261 port_info->rx_desc_cur->rd0 = RD_RACT;
263 /* Point to the next descriptor */
264 port_info->rx_desc_cur++;
265 if (port_info->rx_desc_cur >=
266 port_info->rx_desc_base + NUM_RX_DESC)
267 port_info->rx_desc_cur = port_info->rx_desc_base;
270 /* Restart the receiver if disabled */
271 if (!(inl(EDRRR(port)) & EDRRR_R))
272 outl(EDRRR_R, EDRRR(port));
277 #define EDMR_INIT_CNT 1000
278 static int sh_eth_reset(struct dev_info_s *dev)
280 int port = dev->port;
283 /* Start e-dmac transmitter and receiver */
284 outl(EDSR_ENALL, EDSR(port));
286 /* Perform a software reset and wait for it to complete */
287 outl(EDMR_SRST, EDMR(port));
288 for (i = 0; i < EDMR_INIT_CNT; i++) {
289 if (!(inl(EDMR(port)) & EDMR_SRST))
294 if (i == EDMR_INIT_CNT) {
295 printf("Error: Software reset timeout\n");
301 static int sh_eth_tx_desc_init(struct dev_info_s *dev)
303 int port = dev->port;
304 struct port_info_s *port_info = &dev->port_info[port];
306 struct tx_desc_s *cur_tx_desc;
309 /* Allocate tx descriptors. They must be TX_DESC_SIZE bytes
311 if (!(port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
312 sizeof(struct tx_desc_s) +
313 TX_DESC_SIZE - 1))) {
314 printf("Error: malloc failed\n");
317 tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
318 ~(TX_DESC_SIZE - 1));
319 /* Make sure we use a P2 address (non-cacheable) */
320 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
322 port_info->tx_desc_cur = port_info->tx_desc_base;
324 /* Initialize all descriptors */
325 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
326 cur_tx_desc++, i++) {
327 cur_tx_desc->td0 = 0x00;
328 cur_tx_desc->td1 = 0x00;
329 cur_tx_desc->td2 = 0x00;
332 /* Mark the end of the descriptors */
334 cur_tx_desc->td0 |= TD_TDLE;
336 /* Point the controller to the tx descriptor list. Must use physical
338 outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
339 outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
340 outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
341 outl(0x01, TDFFR(port));/* Last discriptor bit */
346 static int sh_eth_rx_desc_init(struct dev_info_s *dev)
348 int port = dev->port;
349 struct port_info_s *port_info = &dev->port_info[port];
351 struct rx_desc_s *cur_rx_desc;
355 /* Allocate rx descriptors. They must be RX_DESC_SIZE bytes
357 if (!(port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
358 sizeof(struct rx_desc_s) +
359 RX_DESC_SIZE - 1))) {
360 printf("Error: malloc failed\n");
363 tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
364 ~(RX_DESC_SIZE - 1));
365 /* Make sure we use a P2 address (non-cacheable) */
366 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
368 port_info->rx_desc_cur = port_info->rx_desc_base;
370 /* Allocate rx data buffers. They must be 32 bytes aligned and in
372 if (!(port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE +
374 printf("Error: malloc failed\n");
375 free(port_info->rx_desc_malloc);
376 port_info->rx_desc_malloc = NULL;
379 tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
381 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
383 /* Initialize all descriptors */
384 for (cur_rx_desc = port_info->rx_desc_base,
385 rx_buf = port_info->rx_buf_base, i = 0;
386 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
387 cur_rx_desc->rd0 = RD_RACT;
388 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
389 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
392 /* Mark the end of the descriptors */
394 cur_rx_desc->rd0 |= RD_RDLE;
396 /* Point the controller to the rx descriptor list */
397 outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
398 outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
399 outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
400 outl(RDFFR_RDLF, RDFFR(port));
405 static void sh_eth_desc_free(struct dev_info_s *dev)
407 int port = dev->port;
408 struct port_info_s *port_info = &dev->port_info[port];
410 if (port_info->tx_desc_malloc) {
411 free(port_info->tx_desc_malloc);
412 port_info->tx_desc_malloc = NULL;
415 if (port_info->rx_desc_malloc) {
416 free(port_info->rx_desc_malloc);
417 port_info->rx_desc_malloc = NULL;
420 if (port_info->rx_buf_malloc) {
421 free(port_info->rx_buf_malloc);
422 port_info->rx_buf_malloc = NULL;
426 static int sh_eth_desc_init(struct dev_info_s *dev)
430 if ((rc = sh_eth_tx_desc_init(dev)) || (rc = sh_eth_rx_desc_init(dev))) {
431 sh_eth_desc_free(dev);
438 static int sh_eth_phy_config(struct dev_info_s *dev)
440 int port = dev->port;
441 struct port_info_s *port_info = &dev->port_info[port];
445 sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL, PHY_C_RESET);
448 val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, PHY_CTRL);
449 if (!(val & PHY_C_RESET))
454 printf("%s phy reset timeout\n", __func__);
458 /* Advertise 100/10 baseT full/half duplex */
459 sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_ANA,
460 (PHY_A_FDX|PHY_A_HDX|PHY_A_10FDX|PHY_A_10HDX|PHY_A_EXT));
461 /* Autonegotiation, normal operation, full duplex, enable tx */
462 sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL,
463 (PHY_C_ANEGEN|PHY_C_RANEG));
464 /* Wait for autonegotiation to complete */
467 val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
468 if (val & PHY_S_ANEGC)
473 printf("sh_eth_phy_config() phy auto-negotiation failed\n");
480 static int sh_eth_config(struct dev_info_s *dev, bd_t * bd)
482 int port = dev->port;
483 struct port_info_s *port_info = &dev->port_info[port];
488 /* Configure e-dmac registers */
489 outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
490 outl(0, EESIPR(port));
491 outl(0, TRSCER(port));
493 outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
494 outl(RMCR_RST, RMCR(port));
495 outl(0, RPADIR(port));
496 outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
498 /* Configure e-mac registers */
499 outl(0, ECSIPR(port));
501 /* Set Mac address */
502 val = bd->bi_enetaddr[0] << 24 | bd->bi_enetaddr[1] << 16 |
503 bd->bi_enetaddr[2] << 8 | bd->bi_enetaddr[3];
504 outl(val, MAHR(port));
506 val = bd->bi_enetaddr[4] << 8 | bd->bi_enetaddr[5];
507 outl(val, MALR(port));
509 outl(RFLR_RFL_MIN, RFLR(port));
511 outl(APR_AP, APR(port));
512 outl(MPR_MP, MPR(port));
513 outl(TPAUSER_TPAUSE, TPAUSER(port));
516 if ((rc = sh_eth_phy_config(dev)))
519 /* Read phy status to finish configuring the e-mac */
520 phy_status = sh_eth_mii_read_phy_reg(dev->port,
521 dev->port_info[dev->port].phy_addr,
524 /* Set the transfer speed */
525 if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) {
527 outl(GECMR_100B, GECMR(port));
530 outl(GECMR_10B, GECMR(port));
533 /* Check if full duplex mode is supported by the phy */
534 if (phy_status & (PHY_S_100X_F|PHY_S_10T_F)) {
536 outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
539 outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR(port));
544 static int sh_eth_start(struct dev_info_s *dev)
547 * Enable the e-dmac receiver only. The transmitter will be enabled when
548 * we have something to transmit
550 outl(EDRRR_R, EDRRR(dev->port));
555 static int sh_eth_get_mac(bd_t *bd)
560 s = getenv("ethaddr");
562 for (i = 0; i < 6; ++i) {
563 bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
565 s = (*e) ? e + 1 : e;
568 puts("Please set MAC address\n");
573 int eth_init(bd_t *bd)
576 /* Allocate main device information structure */
577 if (!(dev = malloc(sizeof(*dev)))) {
578 printf("eth_init: malloc failed\n");
582 memset(dev, 0, sizeof(*dev));
584 dev->port = CONFIG_SH_ETHER_USE_PORT;
585 dev->port_info[dev->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
589 if ((rc = sh_eth_reset(dev)) || (rc = sh_eth_desc_init(dev)))
592 if ((rc = sh_eth_config(dev, bd)) || (rc = sh_eth_start(dev)))
598 sh_eth_desc_free(dev);
601 printf("eth_init: Failed\n");