sh_eth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. #if defined(CONFIG_CPU_SH7763)
  248. int ret = 0, i;
  249. /* Start e-dmac transmitter and receiver */
  250. outl(EDSR_ENALL, EDSR(port));
  251. /* Perform a software reset and wait for it to complete */
  252. outl(EDMR_SRST, EDMR(port));
  253. for (i = 0; i < EDMR_INIT_CNT; i++) {
  254. if (!(inl(EDMR(port)) & EDMR_SRST))
  255. break;
  256. udelay(1000);
  257. }
  258. if (i == EDMR_INIT_CNT) {
  259. printf(SHETHER_NAME ": Software reset timeout\n");
  260. ret = -EIO;
  261. }
  262. return ret;
  263. #else
  264. outl(inl(EDMR(port)) | EDMR_SRST, EDMR(port));
  265. udelay(3000);
  266. outl(inl(EDMR(port)) & ~EDMR_SRST, EDMR(port));
  267. return 0;
  268. #endif
  269. }
  270. static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
  271. {
  272. int port = eth->port, i, ret = 0;
  273. u32 tmp_addr;
  274. struct sh_eth_info *port_info = &eth->port_info[port];
  275. struct tx_desc_s *cur_tx_desc;
  276. /*
  277. * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
  278. */
  279. port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
  280. sizeof(struct tx_desc_s) +
  281. TX_DESC_SIZE - 1);
  282. if (!port_info->tx_desc_malloc) {
  283. printf(SHETHER_NAME ": malloc failed\n");
  284. ret = -ENOMEM;
  285. goto err;
  286. }
  287. tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
  288. ~(TX_DESC_SIZE - 1));
  289. flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
  290. /* Make sure we use a P2 address (non-cacheable) */
  291. port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
  292. port_info->tx_desc_cur = port_info->tx_desc_base;
  293. /* Initialize all descriptors */
  294. for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
  295. cur_tx_desc++, i++) {
  296. cur_tx_desc->td0 = 0x00;
  297. cur_tx_desc->td1 = 0x00;
  298. cur_tx_desc->td2 = 0x00;
  299. }
  300. /* Mark the end of the descriptors */
  301. cur_tx_desc--;
  302. cur_tx_desc->td0 |= TD_TDLE;
  303. /* Point the controller to the tx descriptor list. Must use physical
  304. addresses */
  305. outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
  306. #if defined(CONFIG_CPU_SH7763)
  307. outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
  308. outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
  309. outl(0x01, TDFFR(port));/* Last discriptor bit */
  310. #endif
  311. err:
  312. return ret;
  313. }
  314. static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
  315. {
  316. int port = eth->port, i , ret = 0;
  317. struct sh_eth_info *port_info = &eth->port_info[port];
  318. struct rx_desc_s *cur_rx_desc;
  319. u32 tmp_addr;
  320. u8 *rx_buf;
  321. /*
  322. * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
  323. */
  324. port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
  325. sizeof(struct rx_desc_s) +
  326. RX_DESC_SIZE - 1);
  327. if (!port_info->rx_desc_malloc) {
  328. printf(SHETHER_NAME ": malloc failed\n");
  329. ret = -ENOMEM;
  330. goto err;
  331. }
  332. tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
  333. ~(RX_DESC_SIZE - 1));
  334. flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
  335. /* Make sure we use a P2 address (non-cacheable) */
  336. port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
  337. port_info->rx_desc_cur = port_info->rx_desc_base;
  338. /*
  339. * Allocate rx data buffers. They must be 32 bytes aligned and in
  340. * P2 area
  341. */
  342. port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
  343. if (!port_info->rx_buf_malloc) {
  344. printf(SHETHER_NAME ": malloc failed\n");
  345. ret = -ENOMEM;
  346. goto err_buf_malloc;
  347. }
  348. tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
  349. ~(32 - 1));
  350. port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
  351. /* Initialize all descriptors */
  352. for (cur_rx_desc = port_info->rx_desc_base,
  353. rx_buf = port_info->rx_buf_base, i = 0;
  354. i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
  355. cur_rx_desc->rd0 = RD_RACT;
  356. cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
  357. cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
  358. }
  359. /* Mark the end of the descriptors */
  360. cur_rx_desc--;
  361. cur_rx_desc->rd0 |= RD_RDLE;
  362. /* Point the controller to the rx descriptor list */
  363. outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
  364. #if defined(CONFIG_CPU_SH7763)
  365. outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
  366. outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
  367. outl(RDFFR_RDLF, RDFFR(port));
  368. #endif
  369. return ret;
  370. err_buf_malloc:
  371. free(port_info->rx_desc_malloc);
  372. port_info->rx_desc_malloc = NULL;
  373. err:
  374. return ret;
  375. }
  376. static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
  377. {
  378. int port = eth->port;
  379. struct sh_eth_info *port_info = &eth->port_info[port];
  380. if (port_info->tx_desc_malloc) {
  381. free(port_info->tx_desc_malloc);
  382. port_info->tx_desc_malloc = NULL;
  383. }
  384. }
  385. static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
  386. {
  387. int port = eth->port;
  388. struct sh_eth_info *port_info = &eth->port_info[port];
  389. if (port_info->rx_desc_malloc) {
  390. free(port_info->rx_desc_malloc);
  391. port_info->rx_desc_malloc = NULL;
  392. }
  393. if (port_info->rx_buf_malloc) {
  394. free(port_info->rx_buf_malloc);
  395. port_info->rx_buf_malloc = NULL;
  396. }
  397. }
  398. static int sh_eth_desc_init(struct sh_eth_dev *eth)
  399. {
  400. int ret = 0;
  401. ret = sh_eth_tx_desc_init(eth);
  402. if (ret)
  403. goto err_tx_init;
  404. ret = sh_eth_rx_desc_init(eth);
  405. if (ret)
  406. goto err_rx_init;
  407. return ret;
  408. err_rx_init:
  409. sh_eth_tx_desc_free(eth);
  410. err_tx_init:
  411. return ret;
  412. }
  413. static int sh_eth_phy_config(struct sh_eth_dev *eth)
  414. {
  415. int port = eth->port, timeout, ret = 0;
  416. struct sh_eth_info *port_info = &eth->port_info[port];
  417. u32 val;
  418. /* Reset phy */
  419. sh_eth_mii_write_phy_reg
  420. (port, port_info->phy_addr, PHY_CTRL, PHY_C_RESET);
  421. timeout = 10;
  422. while (timeout--) {
  423. val = sh_eth_mii_read_phy_reg(port,
  424. port_info->phy_addr, PHY_CTRL);
  425. if (!(val & PHY_C_RESET))
  426. break;
  427. udelay(SH_ETH_PHY_DELAY);
  428. }
  429. if (timeout < 0) {
  430. printf(SHETHER_NAME ": phy reset timeout\n");
  431. ret = -EIO;
  432. goto err_tout;
  433. }
  434. /* Advertise 100/10 baseT full/half duplex */
  435. sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_ANA,
  436. (PHY_A_FDX|PHY_A_HDX|PHY_A_10FDX|PHY_A_10HDX|PHY_A_EXT));
  437. /* Autonegotiation, normal operation, full duplex, enable tx */
  438. sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL,
  439. (PHY_C_ANEGEN|PHY_C_RANEG));
  440. /* Wait for autonegotiation to complete */
  441. timeout = 100;
  442. while (timeout--) {
  443. val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
  444. if (val & PHY_S_ANEGC)
  445. break;
  446. udelay(SH_ETH_PHY_DELAY);
  447. }
  448. if (timeout < 0) {
  449. printf(SHETHER_NAME ": phy auto-negotiation failed\n");
  450. ret = -ETIMEDOUT;
  451. goto err_tout;
  452. }
  453. return ret;
  454. err_tout:
  455. return ret;
  456. }
  457. static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
  458. {
  459. int port = eth->port, ret = 0;
  460. u32 val, phy_status;
  461. struct sh_eth_info *port_info = &eth->port_info[port];
  462. struct eth_device *dev = port_info->dev;
  463. /* Configure e-dmac registers */
  464. outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
  465. outl(0, EESIPR(port));
  466. outl(0, TRSCER(port));
  467. outl(0, TFTR(port));
  468. outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
  469. outl(RMCR_RST, RMCR(port));
  470. #ifndef CONFIG_CPU_SH7757
  471. outl(0, RPADIR(port));
  472. #endif
  473. outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
  474. /* Configure e-mac registers */
  475. #if defined(CONFIG_CPU_SH7757)
  476. outl(ECSIPR_BRCRXIP | ECSIPR_PSRTOIP | ECSIPR_LCHNGIP |
  477. ECSIPR_MPDIP | ECSIPR_ICDIP, ECSIPR(port));
  478. #else
  479. outl(0, ECSIPR(port));
  480. #endif
  481. /* Set Mac address */
  482. val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
  483. dev->enetaddr[2] << 8 | dev->enetaddr[3];
  484. outl(val, MAHR(port));
  485. val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
  486. outl(val, MALR(port));
  487. outl(RFLR_RFL_MIN, RFLR(port));
  488. #ifndef CONFIG_CPU_SH7757
  489. outl(0, PIPR(port));
  490. #endif
  491. outl(APR_AP, APR(port));
  492. outl(MPR_MP, MPR(port));
  493. #ifdef CONFIG_CPU_SH7757
  494. outl(TPAUSER_UNLIMITED, TPAUSER(port));
  495. #else
  496. outl(TPAUSER_TPAUSE, TPAUSER(port));
  497. #endif
  498. /* Configure phy */
  499. ret = sh_eth_phy_config(eth);
  500. if (ret) {
  501. printf(SHETHER_NAME ": phy config timeout\n");
  502. goto err_phy_cfg;
  503. }
  504. /* Read phy status to finish configuring the e-mac */
  505. phy_status = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
  506. /* Set the transfer speed */
  507. #ifdef CONFIG_CPU_SH7763
  508. if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) {
  509. printf(SHETHER_NAME ": 100Base/");
  510. outl(GECMR_100B, GECMR(port));
  511. } else {
  512. printf(SHETHER_NAME ": 10Base/");
  513. outl(GECMR_10B, GECMR(port));
  514. }
  515. #endif
  516. #if defined(CONFIG_CPU_SH7757)
  517. if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) {
  518. printf("100Base/");
  519. outl(1, RTRATE(port));
  520. } else {
  521. printf("10Base/");
  522. outl(0, RTRATE(port));
  523. }
  524. #endif
  525. /* Check if full duplex mode is supported by the phy */
  526. if (phy_status & (PHY_S_100X_F|PHY_S_10T_F)) {
  527. printf("Full\n");
  528. outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
  529. } else {
  530. printf("Half\n");
  531. outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR(port));
  532. }
  533. return ret;
  534. err_phy_cfg:
  535. return ret;
  536. }
  537. static void sh_eth_start(struct sh_eth_dev *eth)
  538. {
  539. /*
  540. * Enable the e-dmac receiver only. The transmitter will be enabled when
  541. * we have something to transmit
  542. */
  543. outl(EDRRR_R, EDRRR(eth->port));
  544. }
  545. static void sh_eth_stop(struct sh_eth_dev *eth)
  546. {
  547. outl(~EDRRR_R, EDRRR(eth->port));
  548. }
  549. int sh_eth_init(struct eth_device *dev, bd_t *bd)
  550. {
  551. int ret = 0;
  552. struct sh_eth_dev *eth = dev->priv;
  553. ret = sh_eth_reset(eth);
  554. if (ret)
  555. goto err;
  556. ret = sh_eth_desc_init(eth);
  557. if (ret)
  558. goto err;
  559. ret = sh_eth_config(eth, bd);
  560. if (ret)
  561. goto err_config;
  562. sh_eth_start(eth);
  563. return ret;
  564. err_config:
  565. sh_eth_tx_desc_free(eth);
  566. sh_eth_rx_desc_free(eth);
  567. err:
  568. return ret;
  569. }
  570. void sh_eth_halt(struct eth_device *dev)
  571. {
  572. struct sh_eth_dev *eth = dev->priv;
  573. sh_eth_stop(eth);
  574. }
  575. int sh_eth_initialize(bd_t *bd)
  576. {
  577. int ret = 0;
  578. struct sh_eth_dev *eth = NULL;
  579. struct eth_device *dev = NULL;
  580. eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
  581. if (!eth) {
  582. printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
  583. ret = -ENOMEM;
  584. goto err;
  585. }
  586. dev = (struct eth_device *)malloc(sizeof(struct eth_device));
  587. if (!dev) {
  588. printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
  589. ret = -ENOMEM;
  590. goto err;
  591. }
  592. memset(dev, 0, sizeof(struct eth_device));
  593. memset(eth, 0, sizeof(struct sh_eth_dev));
  594. eth->port = CONFIG_SH_ETHER_USE_PORT;
  595. eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
  596. dev->priv = (void *)eth;
  597. dev->iobase = 0;
  598. dev->init = sh_eth_init;
  599. dev->halt = sh_eth_halt;
  600. dev->send = sh_eth_send;
  601. dev->recv = sh_eth_recv;
  602. eth->port_info[eth->port].dev = dev;
  603. sprintf(dev->name, SHETHER_NAME);
  604. /* Register Device to EtherNet subsystem */
  605. eth_register(dev);
  606. if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
  607. puts("Please set MAC address\n");
  608. return ret;
  609. err:
  610. if (dev)
  611. free(dev);
  612. if (eth)
  613. free(eth);
  614. printf(SHETHER_NAME ": Failed\n");
  615. return ret;
  616. }