sh_eth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * sh_eth.c - Driver for Renesas SH7763's ethernet controler.
  3. *
  4. * Copyright (C) 2008 Renesas Solutions Corp.
  5. * Copyright (c) 2008 Nobuhiro Iwamatsu
  6. * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
  7. *
  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.
  12. *
  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.
  17. *
  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.
  21. */
  22. #include <config.h>
  23. #include <common.h>
  24. #include <malloc.h>
  25. #include <net.h>
  26. #include <netdev.h>
  27. #include <asm/errno.h>
  28. #include <asm/io.h>
  29. #include "sh_eth.h"
  30. #ifndef CONFIG_SH_ETHER_USE_PORT
  31. # error "Please define CONFIG_SH_ETHER_USE_PORT"
  32. #endif
  33. #ifndef CONFIG_SH_ETHER_PHY_ADDR
  34. # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
  35. #endif
  36. #define SH_ETH_PHY_DELAY 50000
  37. /*
  38. * Bits are written to the PHY serially using the
  39. * PIR register, just like a bit banger.
  40. */
  41. static void sh_eth_mii_write_phy_bits(int port, u32 val, int len)
  42. {
  43. int i;
  44. u32 pir;
  45. /* Bit positions is 1 less than the number of bits */
  46. for (i = len - 1; i >= 0; i--) {
  47. /* Write direction, bit to write, clock is low */
  48. pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
  49. outl(pir, PIR(port));
  50. udelay(1);
  51. /* Write direction, bit to write, clock is high */
  52. pir = 3 | ((val & 1 << i) ? 1 << 2 : 0);
  53. outl(pir, PIR(port));
  54. udelay(1);
  55. /* Write direction, bit to write, clock is low */
  56. pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
  57. outl(pir, PIR(port));
  58. udelay(1);
  59. }
  60. }
  61. static void sh_eth_mii_bus_release(int port)
  62. {
  63. /* Read direction, clock is low */
  64. outl(0, PIR(port));
  65. udelay(1);
  66. /* Read direction, clock is high */
  67. outl(1, PIR(port));
  68. udelay(1);
  69. /* Read direction, clock is low */
  70. outl(0, PIR(port));
  71. udelay(1);
  72. }
  73. static void sh_eth_mii_ind_bus_release(int port)
  74. {
  75. /* Read direction, clock is low */
  76. outl(0, PIR(port));
  77. udelay(1);
  78. }
  79. static void sh_eth_mii_read_phy_bits(int port, u32 *val, int len)
  80. {
  81. int i;
  82. u32 pir;
  83. *val = 0;
  84. for (i = len - 1; i >= 0; i--) {
  85. /* Read direction, clock is high */
  86. outl(1, PIR(port));
  87. udelay(1);
  88. /* Read bit */
  89. pir = inl(PIR(port));
  90. *val |= (pir & 8) ? 1 << i : 0;
  91. /* Read direction, clock is low */
  92. outl(0, PIR(port));
  93. udelay(1);
  94. }
  95. }
  96. #define PHY_INIT 0xFFFFFFFF
  97. #define PHY_READ 0x02
  98. #define PHY_WRITE 0x01
  99. /*
  100. * To read a phy register, mii managements frames are sent to the phy.
  101. * The frames look like this:
  102. * pre (32 bits): 0xffff ffff
  103. * st (2 bits): 01
  104. * op (2bits): 10: read 01: write
  105. * phyad (5 bits): xxxxx
  106. * regad (5 bits): xxxxx
  107. * ta (Bus release):
  108. * data (16 bits): read data
  109. */
  110. static u32 sh_eth_mii_read_phy_reg(int port, u8 phy_addr, int reg)
  111. {
  112. u32 val;
  113. /* Sent mii management frame */
  114. /* pre */
  115. sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
  116. /* st (start of frame) */
  117. sh_eth_mii_write_phy_bits(port, 0x1, 2);
  118. /* op (code) */
  119. sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
  120. /* phy address */
  121. sh_eth_mii_write_phy_bits(port, phy_addr, 5);
  122. /* Register to read */
  123. sh_eth_mii_write_phy_bits(port, reg, 5);
  124. /* Bus release */
  125. sh_eth_mii_bus_release(port);
  126. /* Read register */
  127. sh_eth_mii_read_phy_bits(port, &val, 16);
  128. return val;
  129. }
  130. /*
  131. * To write a phy register, mii managements frames are sent to the phy.
  132. * The frames look like this:
  133. * pre (32 bits): 0xffff ffff
  134. * st (2 bits): 01
  135. * op (2bits): 10: read 01: write
  136. * phyad (5 bits): xxxxx
  137. * regad (5 bits): xxxxx
  138. * ta (2 bits): 10
  139. * data (16 bits): write data
  140. * idle (Independent bus release)
  141. */
  142. static void sh_eth_mii_write_phy_reg(int port, u8 phy_addr, int reg, u16 val)
  143. {
  144. /* Sent mii management frame */
  145. /* pre */
  146. sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
  147. /* st (start of frame) */
  148. sh_eth_mii_write_phy_bits(port, 0x1, 2);
  149. /* op (code) */
  150. sh_eth_mii_write_phy_bits(port, PHY_WRITE, 2);
  151. /* phy address */
  152. sh_eth_mii_write_phy_bits(port, phy_addr, 5);
  153. /* Register to read */
  154. sh_eth_mii_write_phy_bits(port, reg, 5);
  155. /* ta */
  156. sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
  157. /* Write register data */
  158. sh_eth_mii_write_phy_bits(port, val, 16);
  159. /* Independent bus release */
  160. sh_eth_mii_ind_bus_release(port);
  161. }
  162. int sh_eth_send(struct eth_device *dev, volatile void *packet, int len)
  163. {
  164. struct sh_eth_dev *eth = dev->priv;
  165. int port = eth->port, ret = 0, timeout;
  166. struct sh_eth_info *port_info = &eth->port_info[port];
  167. if (!packet || len > 0xffff) {
  168. printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
  169. ret = -EINVAL;
  170. goto err;
  171. }
  172. /* packet must be a 4 byte boundary */
  173. if ((int)packet & (4 - 1)) {
  174. printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
  175. ret = -EFAULT;
  176. goto err;
  177. }
  178. /* Update tx descriptor */
  179. port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
  180. port_info->tx_desc_cur->td1 = len << 16;
  181. /* Must preserve the end of descriptor list indication */
  182. if (port_info->tx_desc_cur->td0 & TD_TDLE)
  183. port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
  184. else
  185. port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
  186. /* Restart the transmitter if disabled */
  187. if (!(inl(EDTRR(port)) & EDTRR_TRNS))
  188. outl(EDTRR_TRNS, EDTRR(port));
  189. /* Wait until packet is transmitted */
  190. timeout = 1000;
  191. while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
  192. udelay(100);
  193. if (timeout < 0) {
  194. printf(SHETHER_NAME ": transmit timeout\n");
  195. ret = -ETIMEDOUT;
  196. goto err;
  197. }
  198. port_info->tx_desc_cur++;
  199. if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
  200. port_info->tx_desc_cur = port_info->tx_desc_base;
  201. return ret;
  202. err:
  203. return ret;
  204. }
  205. int sh_eth_recv(struct eth_device *dev)
  206. {
  207. struct sh_eth_dev *eth = dev->priv;
  208. int port = eth->port, len = 0;
  209. struct sh_eth_info *port_info = &eth->port_info[port];
  210. volatile u8 *packet;
  211. /* Check if the rx descriptor is ready */
  212. if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
  213. /* Check for errors */
  214. if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
  215. len = port_info->rx_desc_cur->rd1 & 0xffff;
  216. packet = (volatile u8 *)
  217. ADDR_TO_P2(port_info->rx_desc_cur->rd2);
  218. NetReceive(packet, len);
  219. }
  220. /* Make current descriptor available again */
  221. if (port_info->rx_desc_cur->rd0 & RD_RDLE)
  222. port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
  223. else
  224. port_info->rx_desc_cur->rd0 = RD_RACT;
  225. /* Point to the next descriptor */
  226. port_info->rx_desc_cur++;
  227. if (port_info->rx_desc_cur >=
  228. port_info->rx_desc_base + NUM_RX_DESC)
  229. port_info->rx_desc_cur = port_info->rx_desc_base;
  230. }
  231. /* Restart the receiver if disabled */
  232. if (!(inl(EDRRR(port)) & EDRRR_R))
  233. outl(EDRRR_R, EDRRR(port));
  234. return len;
  235. }
  236. #define EDMR_INIT_CNT 1000
  237. static int sh_eth_reset(struct sh_eth_dev *eth)
  238. {
  239. int port = eth->port;
  240. int ret = 0, i;
  241. /* Start e-dmac transmitter and receiver */
  242. outl(EDSR_ENALL, EDSR(port));
  243. /* Perform a software reset and wait for it to complete */
  244. outl(EDMR_SRST, EDMR(port));
  245. for (i = 0; i < EDMR_INIT_CNT; i++) {
  246. if (!(inl(EDMR(port)) & EDMR_SRST))
  247. break;
  248. udelay(1000);
  249. }
  250. if (i == EDMR_INIT_CNT) {
  251. printf(SHETHER_NAME ": Software reset timeout\n");
  252. ret = -EIO;
  253. }
  254. return ret;
  255. }
  256. static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
  257. {
  258. int port = eth->port, i, ret = 0;
  259. u32 tmp_addr;
  260. struct sh_eth_info *port_info = &eth->port_info[port];
  261. struct tx_desc_s *cur_tx_desc;
  262. /*
  263. * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
  264. */
  265. port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
  266. sizeof(struct tx_desc_s) +
  267. TX_DESC_SIZE - 1);
  268. if (!port_info->tx_desc_malloc) {
  269. printf(SHETHER_NAME ": malloc failed\n");
  270. ret = -ENOMEM;
  271. goto err;
  272. }
  273. tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
  274. ~(TX_DESC_SIZE - 1));
  275. /* Make sure we use a P2 address (non-cacheable) */
  276. port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
  277. port_info->tx_desc_cur = port_info->tx_desc_base;
  278. /* Initialize all descriptors */
  279. for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
  280. cur_tx_desc++, i++) {
  281. cur_tx_desc->td0 = 0x00;
  282. cur_tx_desc->td1 = 0x00;
  283. cur_tx_desc->td2 = 0x00;
  284. }
  285. /* Mark the end of the descriptors */
  286. cur_tx_desc--;
  287. cur_tx_desc->td0 |= TD_TDLE;
  288. /* Point the controller to the tx descriptor list. Must use physical
  289. addresses */
  290. outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
  291. outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
  292. outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
  293. outl(0x01, TDFFR(port));/* Last discriptor bit */
  294. err:
  295. return ret;
  296. }
  297. static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
  298. {
  299. int port = eth->port, i , ret = 0;
  300. struct sh_eth_info *port_info = &eth->port_info[port];
  301. struct rx_desc_s *cur_rx_desc;
  302. u32 tmp_addr;
  303. u8 *rx_buf;
  304. /*
  305. * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
  306. */
  307. port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
  308. sizeof(struct rx_desc_s) +
  309. RX_DESC_SIZE - 1);
  310. if (!port_info->rx_desc_malloc) {
  311. printf(SHETHER_NAME ": malloc failed\n");
  312. ret = -ENOMEM;
  313. goto err;
  314. }
  315. tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
  316. ~(RX_DESC_SIZE - 1));
  317. /* Make sure we use a P2 address (non-cacheable) */
  318. port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
  319. port_info->rx_desc_cur = port_info->rx_desc_base;
  320. /*
  321. * Allocate rx data buffers. They must be 32 bytes aligned and in
  322. * P2 area
  323. */
  324. port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
  325. if (!port_info->rx_buf_malloc) {
  326. printf(SHETHER_NAME ": malloc failed\n");
  327. ret = -ENOMEM;
  328. goto err_buf_malloc;
  329. }
  330. tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
  331. ~(32 - 1));
  332. port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
  333. /* Initialize all descriptors */
  334. for (cur_rx_desc = port_info->rx_desc_base,
  335. rx_buf = port_info->rx_buf_base, i = 0;
  336. i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
  337. cur_rx_desc->rd0 = RD_RACT;
  338. cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
  339. cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
  340. }
  341. /* Mark the end of the descriptors */
  342. cur_rx_desc--;
  343. cur_rx_desc->rd0 |= RD_RDLE;
  344. /* Point the controller to the rx descriptor list */
  345. outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
  346. outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
  347. outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
  348. outl(RDFFR_RDLF, RDFFR(port));
  349. return ret;
  350. err_buf_malloc:
  351. free(port_info->rx_desc_malloc);
  352. port_info->rx_desc_malloc = NULL;
  353. err:
  354. return ret;
  355. }
  356. static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
  357. {
  358. int port = eth->port;
  359. struct sh_eth_info *port_info = &eth->port_info[port];
  360. if (port_info->tx_desc_malloc) {
  361. free(port_info->tx_desc_malloc);
  362. port_info->tx_desc_malloc = NULL;
  363. }
  364. }
  365. static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
  366. {
  367. int port = eth->port;
  368. struct sh_eth_info *port_info = &eth->port_info[port];
  369. if (port_info->rx_desc_malloc) {
  370. free(port_info->rx_desc_malloc);
  371. port_info->rx_desc_malloc = NULL;
  372. }
  373. if (port_info->rx_buf_malloc) {
  374. free(port_info->rx_buf_malloc);
  375. port_info->rx_buf_malloc = NULL;
  376. }
  377. }
  378. static int sh_eth_desc_init(struct sh_eth_dev *eth)
  379. {
  380. int ret = 0;
  381. ret = sh_eth_tx_desc_init(eth);
  382. if (ret)
  383. goto err_tx_init;
  384. ret = sh_eth_rx_desc_init(eth);
  385. if (ret)
  386. goto err_rx_init;
  387. return ret;
  388. err_rx_init:
  389. sh_eth_tx_desc_free(eth);
  390. err_tx_init:
  391. return ret;
  392. }
  393. static int sh_eth_phy_config(struct sh_eth_dev *eth)
  394. {
  395. int port = eth->port, timeout, ret = 0;
  396. struct sh_eth_info *port_info = &eth->port_info[port];
  397. u32 val;
  398. /* Reset phy */
  399. sh_eth_mii_write_phy_reg
  400. (port, port_info->phy_addr, PHY_CTRL, PHY_C_RESET);
  401. timeout = 10;
  402. while (timeout--) {
  403. val = sh_eth_mii_read_phy_reg(port,
  404. port_info->phy_addr, PHY_CTRL);
  405. if (!(val & PHY_C_RESET))
  406. break;
  407. udelay(SH_ETH_PHY_DELAY);
  408. }
  409. if (timeout < 0) {
  410. printf(SHETHER_NAME ": phy reset timeout\n");
  411. ret = -EIO;
  412. goto err_tout;
  413. }
  414. /* Advertise 100/10 baseT full/half duplex */
  415. sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_ANA,
  416. (PHY_A_FDX|PHY_A_HDX|PHY_A_10FDX|PHY_A_10HDX|PHY_A_EXT));
  417. /* Autonegotiation, normal operation, full duplex, enable tx */
  418. sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL,
  419. (PHY_C_ANEGEN|PHY_C_RANEG));
  420. /* Wait for autonegotiation to complete */
  421. timeout = 100;
  422. while (timeout--) {
  423. val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
  424. if (val & PHY_S_ANEGC)
  425. break;
  426. udelay(SH_ETH_PHY_DELAY);
  427. }
  428. if (timeout < 0) {
  429. printf(SHETHER_NAME ": phy auto-negotiation failed\n");
  430. ret = -ETIMEDOUT;
  431. goto err_tout;
  432. }
  433. return ret;
  434. err_tout:
  435. return ret;
  436. }
  437. static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
  438. {
  439. int port = eth->port, ret = 0;
  440. u32 val, phy_status;
  441. struct sh_eth_info *port_info = &eth->port_info[port];
  442. /* Configure e-dmac registers */
  443. outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
  444. outl(0, EESIPR(port));
  445. outl(0, TRSCER(port));
  446. outl(0, TFTR(port));
  447. outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
  448. outl(RMCR_RST, RMCR(port));
  449. outl(0, RPADIR(port));
  450. outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
  451. /* Configure e-mac registers */
  452. outl(0, ECSIPR(port));
  453. /* Set Mac address */
  454. val = bd->bi_enetaddr[0] << 24 | bd->bi_enetaddr[1] << 16 |
  455. bd->bi_enetaddr[2] << 8 | bd->bi_enetaddr[3];
  456. outl(val, MAHR(port));
  457. val = bd->bi_enetaddr[4] << 8 | bd->bi_enetaddr[5];
  458. outl(val, MALR(port));
  459. outl(RFLR_RFL_MIN, RFLR(port));
  460. outl(0, PIPR(port));
  461. outl(APR_AP, APR(port));
  462. outl(MPR_MP, MPR(port));
  463. outl(TPAUSER_TPAUSE, TPAUSER(port));
  464. /* Configure phy */
  465. ret = sh_eth_phy_config(eth);
  466. if (ret) {
  467. printf(SHETHER_NAME ":i phy config timeout\n");
  468. goto err_phy_cfg;
  469. }
  470. /* Read phy status to finish configuring the e-mac */
  471. phy_status = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
  472. /* Set the transfer speed */
  473. if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) {
  474. printf(SHETHER_NAME ": 100Base/");
  475. outl(GECMR_100B, GECMR(port));
  476. } else {
  477. printf(SHETHER_NAME ": 10Base/");
  478. outl(GECMR_10B, GECMR(port));
  479. }
  480. /* Check if full duplex mode is supported by the phy */
  481. if (phy_status & (PHY_S_100X_F|PHY_S_10T_F)) {
  482. printf("Full\n");
  483. outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
  484. } else {
  485. printf("Half\n");
  486. outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR(port));
  487. }
  488. return ret;
  489. err_phy_cfg:
  490. return ret;
  491. }
  492. static void sh_eth_start(struct sh_eth_dev *eth)
  493. {
  494. /*
  495. * Enable the e-dmac receiver only. The transmitter will be enabled when
  496. * we have something to transmit
  497. */
  498. outl(EDRRR_R, EDRRR(eth->port));
  499. }
  500. static void sh_eth_stop(struct sh_eth_dev *eth)
  501. {
  502. outl(~EDRRR_R, EDRRR(eth->port));
  503. }
  504. static int sh_eth_get_mac(bd_t *bd)
  505. {
  506. char *s, *e;
  507. s = getenv("ethaddr");
  508. if (s != NULL) {
  509. int i;
  510. for (i = 0; i < 6; ++i) {
  511. bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
  512. if (s)
  513. s = (*e) ? e + 1 : e;
  514. }
  515. } else {
  516. puts("Please set MAC address\n");
  517. }
  518. return 0;
  519. }
  520. int sh_eth_init(struct eth_device *dev, bd_t *bd)
  521. {
  522. int ret = 0;
  523. struct sh_eth_dev *eth = dev->priv;
  524. ret = sh_eth_reset(eth);
  525. if (ret)
  526. goto err;
  527. ret = sh_eth_desc_init(eth);
  528. if (ret)
  529. goto err;
  530. ret = sh_eth_config(eth, bd);
  531. if (ret)
  532. goto err_config;
  533. sh_eth_start(eth);
  534. return ret;
  535. err_config:
  536. sh_eth_tx_desc_free(eth);
  537. sh_eth_rx_desc_free(eth);
  538. err:
  539. return ret;
  540. }
  541. void sh_eth_halt(struct eth_device *dev)
  542. {
  543. struct sh_eth_dev *eth = dev->priv;
  544. sh_eth_reset(eth);
  545. sh_eth_stop(eth);
  546. }
  547. int sh_eth_initialize(bd_t *bd)
  548. {
  549. int ret = 0;
  550. struct sh_eth_dev *eth = NULL;
  551. struct eth_device *dev = NULL;
  552. eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
  553. if (!eth) {
  554. printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
  555. ret = -ENOMEM;
  556. goto err;
  557. }
  558. dev = (struct eth_device *)malloc(sizeof(struct eth_device));
  559. if (!dev) {
  560. printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
  561. ret = -ENOMEM;
  562. goto err;
  563. }
  564. memset(dev, 0, sizeof(struct eth_device));
  565. memset(eth, 0, sizeof(struct sh_eth_dev));
  566. eth->port = CONFIG_SH_ETHER_USE_PORT;
  567. eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
  568. dev->priv = (void *)eth;
  569. dev->iobase = 0;
  570. dev->init = sh_eth_init;
  571. dev->halt = sh_eth_halt;
  572. dev->send = sh_eth_send;
  573. dev->recv = sh_eth_recv;
  574. eth->port_info[eth->port].dev = dev;
  575. sprintf(dev->name, SHETHER_NAME);
  576. /* Register Device to EtherNet subsystem */
  577. eth_register(dev);
  578. sh_eth_get_mac(bd);
  579. return ret;
  580. err:
  581. if (dev)
  582. free(dev);
  583. if (eth)
  584. free(eth);
  585. printf(SHETHER_NAME ": Failed\n");
  586. return ret;
  587. }