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.
28 #include <asm/errno.h>
33 #ifndef CONFIG_SH_ETHER_USE_PORT
34 # error "Please define CONFIG_SH_ETHER_USE_PORT"
36 #ifndef CONFIG_SH_ETHER_PHY_ADDR
37 # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
40 #define SH_ETH_PHY_DELAY 50000
43 * Bits are written to the PHY serially using the
44 * PIR register, just like a bit banger.
46 static void sh_eth_mii_write_phy_bits(int port, u32 val, int len)
51 /* Bit positions is 1 less than the number of bits */
52 for (i = len - 1; i >= 0; i--) {
53 /* Write direction, bit to write, clock is low */
54 pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
57 /* Write direction, bit to write, clock is high */
58 pir = 3 | ((val & 1 << i) ? 1 << 2 : 0);
61 /* Write direction, bit to write, clock is low */
62 pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
68 static void sh_eth_mii_bus_release(int port)
70 /* Read direction, clock is low */
73 /* Read direction, clock is high */
76 /* Read direction, clock is low */
81 static void sh_eth_mii_ind_bus_release(int port)
83 /* Read direction, clock is low */
88 static void sh_eth_mii_read_phy_bits(int port, u32 *val, int len)
94 for (i = len - 1; i >= 0; i--) {
95 /* Read direction, clock is high */
100 *val |= (pir & 8) ? 1 << i : 0;
101 /* Read direction, clock is low */
107 #define PHY_INIT 0xFFFFFFFF
108 #define PHY_READ 0x02
109 #define PHY_WRITE 0x01
111 * To read a phy register, mii managements frames are sent to the phy.
112 * The frames look like this:
113 * pre (32 bits): 0xffff ffff
115 * op (2bits): 10: read 01: write
116 * phyad (5 bits): xxxxx
117 * regad (5 bits): xxxxx
119 * data (16 bits): read data
121 static u32 sh_eth_mii_read_phy_reg(int port, u8 phy_addr, int reg)
125 /* Sent mii management frame */
127 sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
128 /* st (start of frame) */
129 sh_eth_mii_write_phy_bits(port, 0x1, 2);
131 sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
133 sh_eth_mii_write_phy_bits(port, phy_addr, 5);
134 /* Register to read */
135 sh_eth_mii_write_phy_bits(port, reg, 5);
138 sh_eth_mii_bus_release(port);
141 sh_eth_mii_read_phy_bits(port, &val, 16);
147 * To write a phy register, mii managements frames are sent to the phy.
148 * The frames look like this:
149 * pre (32 bits): 0xffff ffff
151 * op (2bits): 10: read 01: write
152 * phyad (5 bits): xxxxx
153 * regad (5 bits): xxxxx
155 * data (16 bits): write data
156 * idle (Independent bus release)
158 static void sh_eth_mii_write_phy_reg(int port, u8 phy_addr, int reg, u16 val)
160 /* Sent mii management frame */
162 sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
163 /* st (start of frame) */
164 sh_eth_mii_write_phy_bits(port, 0x1, 2);
166 sh_eth_mii_write_phy_bits(port, PHY_WRITE, 2);
168 sh_eth_mii_write_phy_bits(port, phy_addr, 5);
169 /* Register to read */
170 sh_eth_mii_write_phy_bits(port, reg, 5);
172 sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
173 /* Write register data */
174 sh_eth_mii_write_phy_bits(port, val, 16);
176 /* Independent bus release */
177 sh_eth_mii_ind_bus_release(port);
180 int sh_eth_send(struct eth_device *dev, volatile void *packet, int len)
182 struct sh_eth_dev *eth = dev->priv;
183 int port = eth->port, ret = 0, timeout;
184 struct sh_eth_info *port_info = ð->port_info[port];
186 if (!packet || len > 0xffff) {
187 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
192 /* packet must be a 4 byte boundary */
193 if ((int)packet & (4 - 1)) {
194 printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
199 /* Update tx descriptor */
200 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
201 port_info->tx_desc_cur->td1 = len << 16;
202 /* Must preserve the end of descriptor list indication */
203 if (port_info->tx_desc_cur->td0 & TD_TDLE)
204 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
206 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
208 /* Restart the transmitter if disabled */
209 if (!(inl(EDTRR(port)) & EDTRR_TRNS))
210 outl(EDTRR_TRNS, EDTRR(port));
212 /* Wait until packet is transmitted */
214 while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
218 printf(SHETHER_NAME ": transmit timeout\n");
223 port_info->tx_desc_cur++;
224 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
225 port_info->tx_desc_cur = port_info->tx_desc_base;
232 int sh_eth_recv(struct eth_device *dev)
234 struct sh_eth_dev *eth = dev->priv;
235 int port = eth->port, len = 0;
236 struct sh_eth_info *port_info = ð->port_info[port];
239 /* Check if the rx descriptor is ready */
240 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
241 /* Check for errors */
242 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
243 len = port_info->rx_desc_cur->rd1 & 0xffff;
244 packet = (volatile u8 *)
245 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
246 NetReceive(packet, len);
249 /* Make current descriptor available again */
250 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
251 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
253 port_info->rx_desc_cur->rd0 = RD_RACT;
255 /* Point to the next descriptor */
256 port_info->rx_desc_cur++;
257 if (port_info->rx_desc_cur >=
258 port_info->rx_desc_base + NUM_RX_DESC)
259 port_info->rx_desc_cur = port_info->rx_desc_base;
262 /* Restart the receiver if disabled */
263 if (!(inl(EDRRR(port)) & EDRRR_R))
264 outl(EDRRR_R, EDRRR(port));
269 #define EDMR_INIT_CNT 1000
270 static int sh_eth_reset(struct sh_eth_dev *eth)
272 int port = eth->port;
275 /* Start e-dmac transmitter and receiver */
276 outl(EDSR_ENALL, EDSR(port));
278 /* Perform a software reset and wait for it to complete */
279 outl(EDMR_SRST, EDMR(port));
280 for (i = 0; i < EDMR_INIT_CNT; i++) {
281 if (!(inl(EDMR(port)) & EDMR_SRST))
286 if (i == EDMR_INIT_CNT) {
287 printf(SHETHER_NAME ": Software reset timeout\n");
294 static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
296 int port = eth->port, i, ret = 0;
298 struct sh_eth_info *port_info = ð->port_info[port];
299 struct tx_desc_s *cur_tx_desc;
302 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
304 port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
305 sizeof(struct tx_desc_s) +
307 if (!port_info->tx_desc_malloc) {
308 printf(SHETHER_NAME ": malloc failed\n");
313 tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
314 ~(TX_DESC_SIZE - 1));
315 /* Make sure we use a P2 address (non-cacheable) */
316 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
317 port_info->tx_desc_cur = port_info->tx_desc_base;
319 /* Initialize all descriptors */
320 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
321 cur_tx_desc++, i++) {
322 cur_tx_desc->td0 = 0x00;
323 cur_tx_desc->td1 = 0x00;
324 cur_tx_desc->td2 = 0x00;
327 /* Mark the end of the descriptors */
329 cur_tx_desc->td0 |= TD_TDLE;
331 /* Point the controller to the tx descriptor list. Must use physical
333 outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
334 outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
335 outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
336 outl(0x01, TDFFR(port));/* Last discriptor bit */
342 static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
344 int port = eth->port, i , ret = 0;
345 struct sh_eth_info *port_info = ð->port_info[port];
346 struct rx_desc_s *cur_rx_desc;
351 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
353 port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
354 sizeof(struct rx_desc_s) +
356 if (!port_info->rx_desc_malloc) {
357 printf(SHETHER_NAME ": malloc failed\n");
362 tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
363 ~(RX_DESC_SIZE - 1));
364 /* Make sure we use a P2 address (non-cacheable) */
365 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
367 port_info->rx_desc_cur = port_info->rx_desc_base;
370 * Allocate rx data buffers. They must be 32 bytes aligned and in
373 port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
374 if (!port_info->rx_buf_malloc) {
375 printf(SHETHER_NAME ": malloc failed\n");
380 tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
382 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
384 /* Initialize all descriptors */
385 for (cur_rx_desc = port_info->rx_desc_base,
386 rx_buf = port_info->rx_buf_base, i = 0;
387 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
388 cur_rx_desc->rd0 = RD_RACT;
389 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
390 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
393 /* Mark the end of the descriptors */
395 cur_rx_desc->rd0 |= RD_RDLE;
397 /* Point the controller to the rx descriptor list */
398 outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
399 outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
400 outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
401 outl(RDFFR_RDLF, RDFFR(port));
406 free(port_info->rx_desc_malloc);
407 port_info->rx_desc_malloc = NULL;
413 static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
415 int port = eth->port;
416 struct sh_eth_info *port_info = ð->port_info[port];
418 if (port_info->tx_desc_malloc) {
419 free(port_info->tx_desc_malloc);
420 port_info->tx_desc_malloc = NULL;
424 static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
426 int port = eth->port;
427 struct sh_eth_info *port_info = ð->port_info[port];
429 if (port_info->rx_desc_malloc) {
430 free(port_info->rx_desc_malloc);
431 port_info->rx_desc_malloc = NULL;
434 if (port_info->rx_buf_malloc) {
435 free(port_info->rx_buf_malloc);
436 port_info->rx_buf_malloc = NULL;
440 static int sh_eth_desc_init(struct sh_eth_dev *eth)
444 ret = sh_eth_tx_desc_init(eth);
448 ret = sh_eth_rx_desc_init(eth);
454 sh_eth_tx_desc_free(eth);
460 static int sh_eth_phy_config(struct sh_eth_dev *eth)
462 int port = eth->port, timeout, ret = 0;
463 struct sh_eth_info *port_info = ð->port_info[port];
467 sh_eth_mii_write_phy_reg
468 (port, port_info->phy_addr, PHY_CTRL, PHY_C_RESET);
471 val = sh_eth_mii_read_phy_reg(port,
472 port_info->phy_addr, PHY_CTRL);
473 if (!(val & PHY_C_RESET))
475 udelay(SH_ETH_PHY_DELAY);
479 printf(SHETHER_NAME ": phy reset timeout\n");
484 /* Advertise 100/10 baseT full/half duplex */
485 sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_ANA,
486 (PHY_A_FDX|PHY_A_HDX|PHY_A_10FDX|PHY_A_10HDX|PHY_A_EXT));
487 /* Autonegotiation, normal operation, full duplex, enable tx */
488 sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL,
489 (PHY_C_ANEGEN|PHY_C_RANEG));
490 /* Wait for autonegotiation to complete */
493 val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
494 if (val & PHY_S_ANEGC)
497 udelay(SH_ETH_PHY_DELAY);
501 printf(SHETHER_NAME ": phy auto-negotiation failed\n");
512 static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
514 int port = eth->port, ret = 0;
516 struct sh_eth_info *port_info = ð->port_info[port];
517 struct eth_device *dev = port_info->dev;
519 /* Configure e-dmac registers */
520 outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
521 outl(0, EESIPR(port));
522 outl(0, TRSCER(port));
524 outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
525 outl(RMCR_RST, RMCR(port));
526 outl(0, RPADIR(port));
527 outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
529 /* Configure e-mac registers */
530 outl(0, ECSIPR(port));
532 /* Set Mac address */
533 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
534 dev->enetaddr[2] << 8 | dev->enetaddr[3];
535 outl(val, MAHR(port));
537 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
538 outl(val, MALR(port));
540 outl(RFLR_RFL_MIN, RFLR(port));
542 outl(APR_AP, APR(port));
543 outl(MPR_MP, MPR(port));
544 outl(TPAUSER_TPAUSE, TPAUSER(port));
547 ret = sh_eth_phy_config(eth);
549 printf(SHETHER_NAME ": phy config timeout\n");
552 /* Read phy status to finish configuring the e-mac */
553 phy_status = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
555 /* Set the transfer speed */
556 if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) {
557 printf(SHETHER_NAME ": 100Base/");
558 outl(GECMR_100B, GECMR(port));
560 printf(SHETHER_NAME ": 10Base/");
561 outl(GECMR_10B, GECMR(port));
564 /* Check if full duplex mode is supported by the phy */
565 if (phy_status & (PHY_S_100X_F|PHY_S_10T_F)) {
567 outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
570 outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR(port));
579 static void sh_eth_start(struct sh_eth_dev *eth)
582 * Enable the e-dmac receiver only. The transmitter will be enabled when
583 * we have something to transmit
585 outl(EDRRR_R, EDRRR(eth->port));
588 static void sh_eth_stop(struct sh_eth_dev *eth)
590 outl(~EDRRR_R, EDRRR(eth->port));
593 int sh_eth_init(struct eth_device *dev, bd_t *bd)
596 struct sh_eth_dev *eth = dev->priv;
598 ret = sh_eth_reset(eth);
602 ret = sh_eth_desc_init(eth);
606 ret = sh_eth_config(eth, bd);
615 sh_eth_tx_desc_free(eth);
616 sh_eth_rx_desc_free(eth);
622 void sh_eth_halt(struct eth_device *dev)
624 struct sh_eth_dev *eth = dev->priv;
628 int sh_eth_initialize(bd_t *bd)
631 struct sh_eth_dev *eth = NULL;
632 struct eth_device *dev = NULL;
634 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
636 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
641 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
643 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
647 memset(dev, 0, sizeof(struct eth_device));
648 memset(eth, 0, sizeof(struct sh_eth_dev));
650 eth->port = CONFIG_SH_ETHER_USE_PORT;
651 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
653 dev->priv = (void *)eth;
655 dev->init = sh_eth_init;
656 dev->halt = sh_eth_halt;
657 dev->send = sh_eth_send;
658 dev->recv = sh_eth_recv;
659 eth->port_info[eth->port].dev = dev;
661 sprintf(dev->name, SHETHER_NAME);
663 /* Register Device to EtherNet subsystem */
666 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
667 puts("Please set MAC address\n");
678 printf(SHETHER_NAME ": Failed\n");