sh_eth.c 17 KB

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