sh_eth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * sh_eth.c - Driver for Renesas ethernet controller.
  3. *
  4. * Copyright (C) 2008, 2011 Renesas Solutions Corp.
  5. * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu
  6. * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
  7. * Copyright (C) 2013, 2014 Renesas Electronics Corporation
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <malloc.h>
  14. #include <net.h>
  15. #include <netdev.h>
  16. #include <miiphy.h>
  17. #include <linux/errno.h>
  18. #include <asm/io.h>
  19. #include "sh_eth.h"
  20. #ifndef CONFIG_SH_ETHER_USE_PORT
  21. # error "Please define CONFIG_SH_ETHER_USE_PORT"
  22. #endif
  23. #ifndef CONFIG_SH_ETHER_PHY_ADDR
  24. # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
  25. #endif
  26. #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF)
  27. #define flush_cache_wback(addr, len) \
  28. flush_dcache_range((u32)addr, \
  29. (u32)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE)))
  30. #else
  31. #define flush_cache_wback(...)
  32. #endif
  33. #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
  34. #define invalidate_cache(addr, len) \
  35. { \
  36. u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
  37. u32 start, end; \
  38. \
  39. start = (u32)addr; \
  40. end = start + len; \
  41. start &= ~(line_size - 1); \
  42. end = ((end + line_size - 1) & ~(line_size - 1)); \
  43. \
  44. invalidate_dcache_range(start, end); \
  45. }
  46. #else
  47. #define invalidate_cache(...)
  48. #endif
  49. #define TIMEOUT_CNT 1000
  50. int sh_eth_send(struct eth_device *dev, void *packet, int len)
  51. {
  52. struct sh_eth_dev *eth = dev->priv;
  53. int port = eth->port, ret = 0, timeout;
  54. struct sh_eth_info *port_info = &eth->port_info[port];
  55. if (!packet || len > 0xffff) {
  56. printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
  57. ret = -EINVAL;
  58. goto err;
  59. }
  60. /* packet must be a 4 byte boundary */
  61. if ((int)packet & 3) {
  62. printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
  63. , __func__);
  64. ret = -EFAULT;
  65. goto err;
  66. }
  67. /* Update tx descriptor */
  68. flush_cache_wback(packet, len);
  69. port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
  70. port_info->tx_desc_cur->td1 = len << 16;
  71. /* Must preserve the end of descriptor list indication */
  72. if (port_info->tx_desc_cur->td0 & TD_TDLE)
  73. port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
  74. else
  75. port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
  76. flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
  77. /* Restart the transmitter if disabled */
  78. if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS))
  79. sh_eth_write(port_info, EDTRR_TRNS, EDTRR);
  80. /* Wait until packet is transmitted */
  81. timeout = TIMEOUT_CNT;
  82. do {
  83. invalidate_cache(port_info->tx_desc_cur,
  84. sizeof(struct tx_desc_s));
  85. udelay(100);
  86. } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
  87. if (timeout < 0) {
  88. printf(SHETHER_NAME ": transmit timeout\n");
  89. ret = -ETIMEDOUT;
  90. goto err;
  91. }
  92. port_info->tx_desc_cur++;
  93. if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
  94. port_info->tx_desc_cur = port_info->tx_desc_base;
  95. err:
  96. return ret;
  97. }
  98. int sh_eth_recv(struct eth_device *dev)
  99. {
  100. struct sh_eth_dev *eth = dev->priv;
  101. int port = eth->port, len = 0;
  102. struct sh_eth_info *port_info = &eth->port_info[port];
  103. uchar *packet;
  104. /* Check if the rx descriptor is ready */
  105. invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
  106. if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
  107. /* Check for errors */
  108. if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
  109. len = port_info->rx_desc_cur->rd1 & 0xffff;
  110. packet = (uchar *)
  111. ADDR_TO_P2(port_info->rx_desc_cur->rd2);
  112. invalidate_cache(packet, len);
  113. net_process_received_packet(packet, len);
  114. }
  115. /* Make current descriptor available again */
  116. if (port_info->rx_desc_cur->rd0 & RD_RDLE)
  117. port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
  118. else
  119. port_info->rx_desc_cur->rd0 = RD_RACT;
  120. flush_cache_wback(port_info->rx_desc_cur,
  121. sizeof(struct rx_desc_s));
  122. /* Point to the next descriptor */
  123. port_info->rx_desc_cur++;
  124. if (port_info->rx_desc_cur >=
  125. port_info->rx_desc_base + NUM_RX_DESC)
  126. port_info->rx_desc_cur = port_info->rx_desc_base;
  127. }
  128. /* Restart the receiver if disabled */
  129. if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
  130. sh_eth_write(port_info, EDRRR_R, EDRRR);
  131. return len;
  132. }
  133. static int sh_eth_reset(struct sh_eth_dev *eth)
  134. {
  135. struct sh_eth_info *port_info = &eth->port_info[eth->port];
  136. #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
  137. int ret = 0, i;
  138. /* Start e-dmac transmitter and receiver */
  139. sh_eth_write(port_info, EDSR_ENALL, EDSR);
  140. /* Perform a software reset and wait for it to complete */
  141. sh_eth_write(port_info, EDMR_SRST, EDMR);
  142. for (i = 0; i < TIMEOUT_CNT; i++) {
  143. if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
  144. break;
  145. udelay(1000);
  146. }
  147. if (i == TIMEOUT_CNT) {
  148. printf(SHETHER_NAME ": Software reset timeout\n");
  149. ret = -EIO;
  150. }
  151. return ret;
  152. #else
  153. sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
  154. udelay(3000);
  155. sh_eth_write(port_info,
  156. sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
  157. return 0;
  158. #endif
  159. }
  160. static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
  161. {
  162. int port = eth->port, i, ret = 0;
  163. u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
  164. struct sh_eth_info *port_info = &eth->port_info[port];
  165. struct tx_desc_s *cur_tx_desc;
  166. /*
  167. * Allocate rx descriptors. They must be aligned to size of struct
  168. * tx_desc_s.
  169. */
  170. port_info->tx_desc_alloc =
  171. memalign(sizeof(struct tx_desc_s), alloc_desc_size);
  172. if (!port_info->tx_desc_alloc) {
  173. printf(SHETHER_NAME ": memalign failed\n");
  174. ret = -ENOMEM;
  175. goto err;
  176. }
  177. flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
  178. /* Make sure we use a P2 address (non-cacheable) */
  179. port_info->tx_desc_base =
  180. (struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc);
  181. port_info->tx_desc_cur = port_info->tx_desc_base;
  182. /* Initialize all descriptors */
  183. for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
  184. cur_tx_desc++, i++) {
  185. cur_tx_desc->td0 = 0x00;
  186. cur_tx_desc->td1 = 0x00;
  187. cur_tx_desc->td2 = 0x00;
  188. }
  189. /* Mark the end of the descriptors */
  190. cur_tx_desc--;
  191. cur_tx_desc->td0 |= TD_TDLE;
  192. /*
  193. * Point the controller to the tx descriptor list. Must use physical
  194. * addresses
  195. */
  196. sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
  197. #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
  198. sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
  199. sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
  200. sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
  201. #endif
  202. err:
  203. return ret;
  204. }
  205. static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
  206. {
  207. int port = eth->port, i, ret = 0;
  208. u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
  209. struct sh_eth_info *port_info = &eth->port_info[port];
  210. struct rx_desc_s *cur_rx_desc;
  211. u8 *rx_buf;
  212. /*
  213. * Allocate rx descriptors. They must be aligned to size of struct
  214. * rx_desc_s.
  215. */
  216. port_info->rx_desc_alloc =
  217. memalign(sizeof(struct rx_desc_s), alloc_desc_size);
  218. if (!port_info->rx_desc_alloc) {
  219. printf(SHETHER_NAME ": memalign failed\n");
  220. ret = -ENOMEM;
  221. goto err;
  222. }
  223. flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
  224. /* Make sure we use a P2 address (non-cacheable) */
  225. port_info->rx_desc_base =
  226. (struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc);
  227. port_info->rx_desc_cur = port_info->rx_desc_base;
  228. /*
  229. * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
  230. * aligned and in P2 area.
  231. */
  232. port_info->rx_buf_alloc =
  233. memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
  234. if (!port_info->rx_buf_alloc) {
  235. printf(SHETHER_NAME ": alloc failed\n");
  236. ret = -ENOMEM;
  237. goto err_buf_alloc;
  238. }
  239. port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc);
  240. /* Initialize all descriptors */
  241. for (cur_rx_desc = port_info->rx_desc_base,
  242. rx_buf = port_info->rx_buf_base, i = 0;
  243. i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
  244. cur_rx_desc->rd0 = RD_RACT;
  245. cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
  246. cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
  247. }
  248. /* Mark the end of the descriptors */
  249. cur_rx_desc--;
  250. cur_rx_desc->rd0 |= RD_RDLE;
  251. /* Point the controller to the rx descriptor list */
  252. sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
  253. #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
  254. sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
  255. sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
  256. sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
  257. #endif
  258. return ret;
  259. err_buf_alloc:
  260. free(port_info->rx_desc_alloc);
  261. port_info->rx_desc_alloc = NULL;
  262. err:
  263. return ret;
  264. }
  265. static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
  266. {
  267. int port = eth->port;
  268. struct sh_eth_info *port_info = &eth->port_info[port];
  269. if (port_info->tx_desc_alloc) {
  270. free(port_info->tx_desc_alloc);
  271. port_info->tx_desc_alloc = NULL;
  272. }
  273. }
  274. static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
  275. {
  276. int port = eth->port;
  277. struct sh_eth_info *port_info = &eth->port_info[port];
  278. if (port_info->rx_desc_alloc) {
  279. free(port_info->rx_desc_alloc);
  280. port_info->rx_desc_alloc = NULL;
  281. }
  282. if (port_info->rx_buf_alloc) {
  283. free(port_info->rx_buf_alloc);
  284. port_info->rx_buf_alloc = NULL;
  285. }
  286. }
  287. static int sh_eth_desc_init(struct sh_eth_dev *eth)
  288. {
  289. int ret = 0;
  290. ret = sh_eth_tx_desc_init(eth);
  291. if (ret)
  292. goto err_tx_init;
  293. ret = sh_eth_rx_desc_init(eth);
  294. if (ret)
  295. goto err_rx_init;
  296. return ret;
  297. err_rx_init:
  298. sh_eth_tx_desc_free(eth);
  299. err_tx_init:
  300. return ret;
  301. }
  302. static int sh_eth_phy_config(struct sh_eth_dev *eth)
  303. {
  304. int port = eth->port, ret = 0;
  305. struct sh_eth_info *port_info = &eth->port_info[port];
  306. struct eth_device *dev = port_info->dev;
  307. struct phy_device *phydev;
  308. phydev = phy_connect(
  309. miiphy_get_dev_by_name(dev->name),
  310. port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
  311. port_info->phydev = phydev;
  312. phy_config(phydev);
  313. return ret;
  314. }
  315. static int sh_eth_config(struct sh_eth_dev *eth)
  316. {
  317. int port = eth->port, ret = 0;
  318. u32 val;
  319. struct sh_eth_info *port_info = &eth->port_info[port];
  320. struct eth_device *dev = port_info->dev;
  321. struct phy_device *phy;
  322. /* Configure e-dmac registers */
  323. sh_eth_write(port_info, (sh_eth_read(port_info, EDMR) & ~EMDR_DESC_R) |
  324. (EMDR_DESC | EDMR_EL), EDMR);
  325. sh_eth_write(port_info, 0, EESIPR);
  326. sh_eth_write(port_info, 0, TRSCER);
  327. sh_eth_write(port_info, 0, TFTR);
  328. sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
  329. sh_eth_write(port_info, RMCR_RST, RMCR);
  330. #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
  331. sh_eth_write(port_info, 0, RPADIR);
  332. #endif
  333. sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
  334. /* Configure e-mac registers */
  335. sh_eth_write(port_info, 0, ECSIPR);
  336. /* Set Mac address */
  337. val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
  338. dev->enetaddr[2] << 8 | dev->enetaddr[3];
  339. sh_eth_write(port_info, val, MAHR);
  340. val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
  341. sh_eth_write(port_info, val, MALR);
  342. sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
  343. #if defined(SH_ETH_TYPE_GETHER)
  344. sh_eth_write(port_info, 0, PIPR);
  345. #endif
  346. #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
  347. sh_eth_write(port_info, APR_AP, APR);
  348. sh_eth_write(port_info, MPR_MP, MPR);
  349. sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
  350. #endif
  351. #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
  352. sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
  353. #elif defined(CONFIG_RCAR_GEN2)
  354. sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
  355. #endif
  356. /* Configure phy */
  357. ret = sh_eth_phy_config(eth);
  358. if (ret) {
  359. printf(SHETHER_NAME ": phy config timeout\n");
  360. goto err_phy_cfg;
  361. }
  362. phy = port_info->phydev;
  363. ret = phy_startup(phy);
  364. if (ret) {
  365. printf(SHETHER_NAME ": phy startup failure\n");
  366. return ret;
  367. }
  368. val = 0;
  369. /* Set the transfer speed */
  370. if (phy->speed == 100) {
  371. printf(SHETHER_NAME ": 100Base/");
  372. #if defined(SH_ETH_TYPE_GETHER)
  373. sh_eth_write(port_info, GECMR_100B, GECMR);
  374. #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
  375. sh_eth_write(port_info, 1, RTRATE);
  376. #elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_RCAR_GEN2)
  377. val = ECMR_RTM;
  378. #endif
  379. } else if (phy->speed == 10) {
  380. printf(SHETHER_NAME ": 10Base/");
  381. #if defined(SH_ETH_TYPE_GETHER)
  382. sh_eth_write(port_info, GECMR_10B, GECMR);
  383. #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
  384. sh_eth_write(port_info, 0, RTRATE);
  385. #endif
  386. }
  387. #if defined(SH_ETH_TYPE_GETHER)
  388. else if (phy->speed == 1000) {
  389. printf(SHETHER_NAME ": 1000Base/");
  390. sh_eth_write(port_info, GECMR_1000B, GECMR);
  391. }
  392. #endif
  393. /* Check if full duplex mode is supported by the phy */
  394. if (phy->duplex) {
  395. printf("Full\n");
  396. sh_eth_write(port_info,
  397. val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
  398. ECMR);
  399. } else {
  400. printf("Half\n");
  401. sh_eth_write(port_info,
  402. val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
  403. ECMR);
  404. }
  405. return ret;
  406. err_phy_cfg:
  407. return ret;
  408. }
  409. static void sh_eth_start(struct sh_eth_dev *eth)
  410. {
  411. struct sh_eth_info *port_info = &eth->port_info[eth->port];
  412. /*
  413. * Enable the e-dmac receiver only. The transmitter will be enabled when
  414. * we have something to transmit
  415. */
  416. sh_eth_write(port_info, EDRRR_R, EDRRR);
  417. }
  418. static void sh_eth_stop(struct sh_eth_dev *eth)
  419. {
  420. struct sh_eth_info *port_info = &eth->port_info[eth->port];
  421. sh_eth_write(port_info, ~EDRRR_R, EDRRR);
  422. }
  423. int sh_eth_init(struct eth_device *dev, bd_t *bd)
  424. {
  425. int ret = 0;
  426. struct sh_eth_dev *eth = dev->priv;
  427. ret = sh_eth_reset(eth);
  428. if (ret)
  429. goto err;
  430. ret = sh_eth_desc_init(eth);
  431. if (ret)
  432. goto err;
  433. ret = sh_eth_config(eth);
  434. if (ret)
  435. goto err_config;
  436. sh_eth_start(eth);
  437. return ret;
  438. err_config:
  439. sh_eth_tx_desc_free(eth);
  440. sh_eth_rx_desc_free(eth);
  441. err:
  442. return ret;
  443. }
  444. void sh_eth_halt(struct eth_device *dev)
  445. {
  446. struct sh_eth_dev *eth = dev->priv;
  447. sh_eth_stop(eth);
  448. }
  449. int sh_eth_initialize(bd_t *bd)
  450. {
  451. int ret = 0;
  452. struct sh_eth_dev *eth = NULL;
  453. struct eth_device *dev = NULL;
  454. struct mii_dev *mdiodev;
  455. eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
  456. if (!eth) {
  457. printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
  458. ret = -ENOMEM;
  459. goto err;
  460. }
  461. dev = (struct eth_device *)malloc(sizeof(struct eth_device));
  462. if (!dev) {
  463. printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
  464. ret = -ENOMEM;
  465. goto err;
  466. }
  467. memset(dev, 0, sizeof(struct eth_device));
  468. memset(eth, 0, sizeof(struct sh_eth_dev));
  469. eth->port = CONFIG_SH_ETHER_USE_PORT;
  470. eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
  471. eth->port_info[eth->port].iobase =
  472. (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
  473. dev->priv = (void *)eth;
  474. dev->iobase = 0;
  475. dev->init = sh_eth_init;
  476. dev->halt = sh_eth_halt;
  477. dev->send = sh_eth_send;
  478. dev->recv = sh_eth_recv;
  479. eth->port_info[eth->port].dev = dev;
  480. strcpy(dev->name, SHETHER_NAME);
  481. /* Register Device to EtherNet subsystem */
  482. eth_register(dev);
  483. bb_miiphy_buses[0].priv = eth;
  484. mdiodev = mdio_alloc();
  485. if (!mdiodev)
  486. return -ENOMEM;
  487. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  488. mdiodev->read = bb_miiphy_read;
  489. mdiodev->write = bb_miiphy_write;
  490. ret = mdio_register(mdiodev);
  491. if (ret < 0)
  492. return ret;
  493. if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
  494. puts("Please set MAC address\n");
  495. return ret;
  496. err:
  497. if (dev)
  498. free(dev);
  499. if (eth)
  500. free(eth);
  501. printf(SHETHER_NAME ": Failed\n");
  502. return ret;
  503. }
  504. /******* for bb_miiphy *******/
  505. static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
  506. {
  507. return 0;
  508. }
  509. static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
  510. {
  511. struct sh_eth_dev *eth = bus->priv;
  512. struct sh_eth_info *port_info = &eth->port_info[eth->port];
  513. sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
  514. return 0;
  515. }
  516. static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
  517. {
  518. struct sh_eth_dev *eth = bus->priv;
  519. struct sh_eth_info *port_info = &eth->port_info[eth->port];
  520. sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
  521. return 0;
  522. }
  523. static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
  524. {
  525. struct sh_eth_dev *eth = bus->priv;
  526. struct sh_eth_info *port_info = &eth->port_info[eth->port];
  527. if (v)
  528. sh_eth_write(port_info,
  529. sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
  530. else
  531. sh_eth_write(port_info,
  532. sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
  533. return 0;
  534. }
  535. static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
  536. {
  537. struct sh_eth_dev *eth = bus->priv;
  538. struct sh_eth_info *port_info = &eth->port_info[eth->port];
  539. *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
  540. return 0;
  541. }
  542. static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
  543. {
  544. struct sh_eth_dev *eth = bus->priv;
  545. struct sh_eth_info *port_info = &eth->port_info[eth->port];
  546. if (v)
  547. sh_eth_write(port_info,
  548. sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
  549. else
  550. sh_eth_write(port_info,
  551. sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
  552. return 0;
  553. }
  554. static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
  555. {
  556. udelay(10);
  557. return 0;
  558. }
  559. struct bb_miiphy_bus bb_miiphy_buses[] = {
  560. {
  561. .name = "sh_eth",
  562. .init = sh_eth_bb_init,
  563. .mdio_active = sh_eth_bb_mdio_active,
  564. .mdio_tristate = sh_eth_bb_mdio_tristate,
  565. .set_mdio = sh_eth_bb_set_mdio,
  566. .get_mdio = sh_eth_bb_get_mdio,
  567. .set_mdc = sh_eth_bb_set_mdc,
  568. .delay = sh_eth_bb_delay,
  569. }
  570. };
  571. int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);