sh_eth.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * sh_eth.c - Driver for Renesas ethernet controler.
  3. *
  4. * Copyright (C) 2008, 2011 Renesas Solutions Corp.
  5. * Copyright (c) 2008, 2011 Nobuhiro Iwamatsu
  6. * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
  7. * Copyright (C) 2013 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 <asm/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 alligned\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. /* Restart the transmitter if disabled */
  76. if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
  77. sh_eth_write(eth, EDTRR_TRNS, EDTRR);
  78. /* Wait until packet is transmitted */
  79. timeout = TIMEOUT_CNT;
  80. do {
  81. invalidate_cache(port_info->tx_desc_cur,
  82. sizeof(struct tx_desc_s));
  83. udelay(100);
  84. } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
  85. if (timeout < 0) {
  86. printf(SHETHER_NAME ": transmit timeout\n");
  87. ret = -ETIMEDOUT;
  88. goto err;
  89. }
  90. port_info->tx_desc_cur++;
  91. if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
  92. port_info->tx_desc_cur = port_info->tx_desc_base;
  93. err:
  94. return ret;
  95. }
  96. int sh_eth_recv(struct eth_device *dev)
  97. {
  98. struct sh_eth_dev *eth = dev->priv;
  99. int port = eth->port, len = 0;
  100. struct sh_eth_info *port_info = &eth->port_info[port];
  101. uchar *packet;
  102. /* Check if the rx descriptor is ready */
  103. invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
  104. if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
  105. /* Check for errors */
  106. if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
  107. len = port_info->rx_desc_cur->rd1 & 0xffff;
  108. packet = (uchar *)
  109. ADDR_TO_P2(port_info->rx_desc_cur->rd2);
  110. invalidate_cache(packet, len);
  111. NetReceive(packet, len);
  112. }
  113. /* Make current descriptor available again */
  114. if (port_info->rx_desc_cur->rd0 & RD_RDLE)
  115. port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
  116. else
  117. port_info->rx_desc_cur->rd0 = RD_RACT;
  118. /* Point to the next descriptor */
  119. port_info->rx_desc_cur++;
  120. if (port_info->rx_desc_cur >=
  121. port_info->rx_desc_base + NUM_RX_DESC)
  122. port_info->rx_desc_cur = port_info->rx_desc_base;
  123. }
  124. /* Restart the receiver if disabled */
  125. if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
  126. sh_eth_write(eth, EDRRR_R, EDRRR);
  127. return len;
  128. }
  129. static int sh_eth_reset(struct sh_eth_dev *eth)
  130. {
  131. #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
  132. int ret = 0, i;
  133. /* Start e-dmac transmitter and receiver */
  134. sh_eth_write(eth, EDSR_ENALL, EDSR);
  135. /* Perform a software reset and wait for it to complete */
  136. sh_eth_write(eth, EDMR_SRST, EDMR);
  137. for (i = 0; i < TIMEOUT_CNT; i++) {
  138. if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
  139. break;
  140. udelay(1000);
  141. }
  142. if (i == TIMEOUT_CNT) {
  143. printf(SHETHER_NAME ": Software reset timeout\n");
  144. ret = -EIO;
  145. }
  146. return ret;
  147. #else
  148. sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
  149. udelay(3000);
  150. sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
  151. return 0;
  152. #endif
  153. }
  154. static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
  155. {
  156. int port = eth->port, i, ret = 0;
  157. u32 tmp_addr;
  158. struct sh_eth_info *port_info = &eth->port_info[port];
  159. struct tx_desc_s *cur_tx_desc;
  160. /*
  161. * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
  162. */
  163. port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
  164. sizeof(struct tx_desc_s) +
  165. TX_DESC_SIZE - 1);
  166. if (!port_info->tx_desc_malloc) {
  167. printf(SHETHER_NAME ": malloc failed\n");
  168. ret = -ENOMEM;
  169. goto err;
  170. }
  171. tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
  172. ~(TX_DESC_SIZE - 1));
  173. flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
  174. /* Make sure we use a P2 address (non-cacheable) */
  175. port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
  176. port_info->tx_desc_cur = port_info->tx_desc_base;
  177. /* Initialize all descriptors */
  178. for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
  179. cur_tx_desc++, i++) {
  180. cur_tx_desc->td0 = 0x00;
  181. cur_tx_desc->td1 = 0x00;
  182. cur_tx_desc->td2 = 0x00;
  183. }
  184. /* Mark the end of the descriptors */
  185. cur_tx_desc--;
  186. cur_tx_desc->td0 |= TD_TDLE;
  187. /* Point the controller to the tx descriptor list. Must use physical
  188. addresses */
  189. sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
  190. #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
  191. sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
  192. sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
  193. sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
  194. #endif
  195. err:
  196. return ret;
  197. }
  198. static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
  199. {
  200. int port = eth->port, i , ret = 0;
  201. struct sh_eth_info *port_info = &eth->port_info[port];
  202. struct rx_desc_s *cur_rx_desc;
  203. u32 tmp_addr;
  204. u8 *rx_buf;
  205. /*
  206. * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
  207. */
  208. port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
  209. sizeof(struct rx_desc_s) +
  210. RX_DESC_SIZE - 1);
  211. if (!port_info->rx_desc_malloc) {
  212. printf(SHETHER_NAME ": malloc failed\n");
  213. ret = -ENOMEM;
  214. goto err;
  215. }
  216. tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
  217. ~(RX_DESC_SIZE - 1));
  218. flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
  219. /* Make sure we use a P2 address (non-cacheable) */
  220. port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
  221. port_info->rx_desc_cur = port_info->rx_desc_base;
  222. /*
  223. * Allocate rx data buffers. They must be 32 bytes aligned and in
  224. * P2 area
  225. */
  226. port_info->rx_buf_malloc = malloc(
  227. NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
  228. if (!port_info->rx_buf_malloc) {
  229. printf(SHETHER_NAME ": malloc failed\n");
  230. ret = -ENOMEM;
  231. goto err_buf_malloc;
  232. }
  233. tmp_addr = (u32)(((int)port_info->rx_buf_malloc
  234. + (RX_BUF_ALIGNE_SIZE - 1)) &
  235. ~(RX_BUF_ALIGNE_SIZE - 1));
  236. port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
  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_malloc:
  257. free(port_info->rx_desc_malloc);
  258. port_info->rx_desc_malloc = 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_malloc) {
  267. free(port_info->tx_desc_malloc);
  268. port_info->tx_desc_malloc = 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_malloc) {
  276. free(port_info->rx_desc_malloc);
  277. port_info->rx_desc_malloc = NULL;
  278. }
  279. if (port_info->rx_buf_malloc) {
  280. free(port_info->rx_buf_malloc);
  281. port_info->rx_buf_malloc = 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_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_R8A7794)
  376. val = ECMR_RTM;
  377. #endif
  378. } else if (phy->speed == 10) {
  379. printf(SHETHER_NAME ": 10Base/");
  380. #if defined(SH_ETH_TYPE_GETHER)
  381. sh_eth_write(eth, GECMR_10B, GECMR);
  382. #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
  383. sh_eth_write(eth, 0, RTRATE);
  384. #endif
  385. }
  386. #if defined(SH_ETH_TYPE_GETHER)
  387. else if (phy->speed == 1000) {
  388. printf(SHETHER_NAME ": 1000Base/");
  389. sh_eth_write(eth, GECMR_1000B, GECMR);
  390. }
  391. #endif
  392. /* Check if full duplex mode is supported by the phy */
  393. if (phy->duplex) {
  394. printf("Full\n");
  395. sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
  396. ECMR);
  397. } else {
  398. printf("Half\n");
  399. sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
  400. }
  401. return ret;
  402. err_phy_cfg:
  403. return ret;
  404. }
  405. static void sh_eth_start(struct sh_eth_dev *eth)
  406. {
  407. /*
  408. * Enable the e-dmac receiver only. The transmitter will be enabled when
  409. * we have something to transmit
  410. */
  411. sh_eth_write(eth, EDRRR_R, EDRRR);
  412. }
  413. static void sh_eth_stop(struct sh_eth_dev *eth)
  414. {
  415. sh_eth_write(eth, ~EDRRR_R, EDRRR);
  416. }
  417. int sh_eth_init(struct eth_device *dev, bd_t *bd)
  418. {
  419. int ret = 0;
  420. struct sh_eth_dev *eth = dev->priv;
  421. ret = sh_eth_reset(eth);
  422. if (ret)
  423. goto err;
  424. ret = sh_eth_desc_init(eth);
  425. if (ret)
  426. goto err;
  427. ret = sh_eth_config(eth, bd);
  428. if (ret)
  429. goto err_config;
  430. sh_eth_start(eth);
  431. return ret;
  432. err_config:
  433. sh_eth_tx_desc_free(eth);
  434. sh_eth_rx_desc_free(eth);
  435. err:
  436. return ret;
  437. }
  438. void sh_eth_halt(struct eth_device *dev)
  439. {
  440. struct sh_eth_dev *eth = dev->priv;
  441. sh_eth_stop(eth);
  442. }
  443. int sh_eth_initialize(bd_t *bd)
  444. {
  445. int ret = 0;
  446. struct sh_eth_dev *eth = NULL;
  447. struct eth_device *dev = NULL;
  448. eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
  449. if (!eth) {
  450. printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
  451. ret = -ENOMEM;
  452. goto err;
  453. }
  454. dev = (struct eth_device *)malloc(sizeof(struct eth_device));
  455. if (!dev) {
  456. printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
  457. ret = -ENOMEM;
  458. goto err;
  459. }
  460. memset(dev, 0, sizeof(struct eth_device));
  461. memset(eth, 0, sizeof(struct sh_eth_dev));
  462. eth->port = CONFIG_SH_ETHER_USE_PORT;
  463. eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
  464. dev->priv = (void *)eth;
  465. dev->iobase = 0;
  466. dev->init = sh_eth_init;
  467. dev->halt = sh_eth_halt;
  468. dev->send = sh_eth_send;
  469. dev->recv = sh_eth_recv;
  470. eth->port_info[eth->port].dev = dev;
  471. sprintf(dev->name, SHETHER_NAME);
  472. /* Register Device to EtherNet subsystem */
  473. eth_register(dev);
  474. bb_miiphy_buses[0].priv = eth;
  475. miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
  476. if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
  477. puts("Please set MAC address\n");
  478. return ret;
  479. err:
  480. if (dev)
  481. free(dev);
  482. if (eth)
  483. free(eth);
  484. printf(SHETHER_NAME ": Failed\n");
  485. return ret;
  486. }
  487. /******* for bb_miiphy *******/
  488. static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
  489. {
  490. return 0;
  491. }
  492. static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
  493. {
  494. struct sh_eth_dev *eth = bus->priv;
  495. sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
  496. return 0;
  497. }
  498. static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
  499. {
  500. struct sh_eth_dev *eth = bus->priv;
  501. sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
  502. return 0;
  503. }
  504. static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
  505. {
  506. struct sh_eth_dev *eth = bus->priv;
  507. if (v)
  508. sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
  509. else
  510. sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
  511. return 0;
  512. }
  513. static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
  514. {
  515. struct sh_eth_dev *eth = bus->priv;
  516. *v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
  517. return 0;
  518. }
  519. static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
  520. {
  521. struct sh_eth_dev *eth = bus->priv;
  522. if (v)
  523. sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
  524. else
  525. sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
  526. return 0;
  527. }
  528. static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
  529. {
  530. udelay(10);
  531. return 0;
  532. }
  533. struct bb_miiphy_bus bb_miiphy_buses[] = {
  534. {
  535. .name = "sh_eth",
  536. .init = sh_eth_bb_init,
  537. .mdio_active = sh_eth_bb_mdio_active,
  538. .mdio_tristate = sh_eth_bb_mdio_tristate,
  539. .set_mdio = sh_eth_bb_set_mdio,
  540. .get_mdio = sh_eth_bb_get_mdio,
  541. .set_mdc = sh_eth_bb_set_mdc,
  542. .delay = sh_eth_bb_delay,
  543. }
  544. };
  545. int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);