sh_eth.c 16 KB

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