sh_eth.c 16 KB

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