ep93xx_eth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * Cirrus Logic EP93xx ethernet MAC / MII driver.
  3. *
  4. * Copyright (C) 2010, 2009
  5. * Matthias Kaehlcke <matthias@kaehlcke.net>
  6. *
  7. * Copyright (C) 2004, 2005
  8. * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
  9. *
  10. * Based on the original eth.[ch] Cirrus Logic EP93xx Rev D. Ethernet Driver,
  11. * which is
  12. *
  13. * (C) Copyright 2002 2003
  14. * Adam Bezanson, Network Audio Technologies, Inc.
  15. * <bezanson@netaudiotech.com>
  16. *
  17. * See file CREDITS for list of people who contributed to this project.
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  26. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  27. * for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <command.h>
  34. #include <common.h>
  35. #include <asm/arch/ep93xx.h>
  36. #include <asm/io.h>
  37. #include <malloc.h>
  38. #include <miiphy.h>
  39. #include <linux/types.h>
  40. #include "ep93xx_eth.h"
  41. #define GET_PRIV(eth_dev) ((struct ep93xx_priv *)(eth_dev)->priv)
  42. #define GET_REGS(eth_dev) (GET_PRIV(eth_dev)->regs)
  43. /* ep93xx_miiphy ops forward declarations */
  44. static int ep93xx_miiphy_read(const char * const dev, unsigned char const addr,
  45. unsigned char const reg, unsigned short * const value);
  46. static int ep93xx_miiphy_write(const char * const dev, unsigned char const addr,
  47. unsigned char const reg, unsigned short const value);
  48. #if defined(EP93XX_MAC_DEBUG)
  49. /**
  50. * Dump ep93xx_mac values to the terminal.
  51. */
  52. static void dump_dev(struct eth_device *dev)
  53. {
  54. struct ep93xx_priv *priv = GET_PRIV(dev);
  55. int i;
  56. printf("\ndump_dev()\n");
  57. printf(" rx_dq.base %p\n", priv->rx_dq.base);
  58. printf(" rx_dq.current %p\n", priv->rx_dq.current);
  59. printf(" rx_dq.end %p\n", priv->rx_dq.end);
  60. printf(" rx_sq.base %p\n", priv->rx_sq.base);
  61. printf(" rx_sq.current %p\n", priv->rx_sq.current);
  62. printf(" rx_sq.end %p\n", priv->rx_sq.end);
  63. for (i = 0; i < NUMRXDESC; i++)
  64. printf(" rx_buffer[%2.d] %p\n", i, NetRxPackets[i]);
  65. printf(" tx_dq.base %p\n", priv->tx_dq.base);
  66. printf(" tx_dq.current %p\n", priv->tx_dq.current);
  67. printf(" tx_dq.end %p\n", priv->tx_dq.end);
  68. printf(" tx_sq.base %p\n", priv->tx_sq.base);
  69. printf(" tx_sq.current %p\n", priv->tx_sq.current);
  70. printf(" tx_sq.end %p\n", priv->tx_sq.end);
  71. }
  72. /**
  73. * Dump all RX status queue entries to the terminal.
  74. */
  75. static void dump_rx_status_queue(struct eth_device *dev)
  76. {
  77. struct ep93xx_priv *priv = GET_PRIV(dev);
  78. int i;
  79. printf("\ndump_rx_status_queue()\n");
  80. printf(" descriptor address word1 word2\n");
  81. for (i = 0; i < NUMRXDESC; i++) {
  82. printf(" [ %p ] %08X %08X\n",
  83. priv->rx_sq.base + i,
  84. (priv->rx_sq.base + i)->word1,
  85. (priv->rx_sq.base + i)->word2);
  86. }
  87. }
  88. /**
  89. * Dump all RX descriptor queue entries to the terminal.
  90. */
  91. static void dump_rx_descriptor_queue(struct eth_device *dev)
  92. {
  93. struct ep93xx_priv *priv = GET_PRIV(dev);
  94. int i;
  95. printf("\ndump_rx_descriptor_queue()\n");
  96. printf(" descriptor address word1 word2\n");
  97. for (i = 0; i < NUMRXDESC; i++) {
  98. printf(" [ %p ] %08X %08X\n",
  99. priv->rx_dq.base + i,
  100. (priv->rx_dq.base + i)->word1,
  101. (priv->rx_dq.base + i)->word2);
  102. }
  103. }
  104. /**
  105. * Dump all TX descriptor queue entries to the terminal.
  106. */
  107. static void dump_tx_descriptor_queue(struct eth_device *dev)
  108. {
  109. struct ep93xx_priv *priv = GET_PRIV(dev);
  110. int i;
  111. printf("\ndump_tx_descriptor_queue()\n");
  112. printf(" descriptor address word1 word2\n");
  113. for (i = 0; i < NUMTXDESC; i++) {
  114. printf(" [ %p ] %08X %08X\n",
  115. priv->tx_dq.base + i,
  116. (priv->tx_dq.base + i)->word1,
  117. (priv->tx_dq.base + i)->word2);
  118. }
  119. }
  120. /**
  121. * Dump all TX status queue entries to the terminal.
  122. */
  123. static void dump_tx_status_queue(struct eth_device *dev)
  124. {
  125. struct ep93xx_priv *priv = GET_PRIV(dev);
  126. int i;
  127. printf("\ndump_tx_status_queue()\n");
  128. printf(" descriptor address word1\n");
  129. for (i = 0; i < NUMTXDESC; i++) {
  130. printf(" [ %p ] %08X\n",
  131. priv->rx_sq.base + i,
  132. (priv->rx_sq.base + i)->word1);
  133. }
  134. }
  135. #else
  136. #define dump_dev(x)
  137. #define dump_rx_descriptor_queue(x)
  138. #define dump_rx_status_queue(x)
  139. #define dump_tx_descriptor_queue(x)
  140. #define dump_tx_status_queue(x)
  141. #endif /* defined(EP93XX_MAC_DEBUG) */
  142. /**
  143. * Reset the EP93xx MAC by twiddling the soft reset bit and spinning until
  144. * it's cleared.
  145. */
  146. static void ep93xx_mac_reset(struct eth_device *dev)
  147. {
  148. struct mac_regs *mac = GET_REGS(dev);
  149. uint32_t value;
  150. debug("+ep93xx_mac_reset");
  151. value = readl(&mac->selfctl);
  152. value |= SELFCTL_RESET;
  153. writel(value, &mac->selfctl);
  154. while (readl(&mac->selfctl) & SELFCTL_RESET)
  155. ; /* noop */
  156. debug("-ep93xx_mac_reset");
  157. }
  158. /* Eth device open */
  159. static int ep93xx_eth_open(struct eth_device *dev, bd_t *bd)
  160. {
  161. struct ep93xx_priv *priv = GET_PRIV(dev);
  162. struct mac_regs *mac = GET_REGS(dev);
  163. uchar *mac_addr = dev->enetaddr;
  164. int i;
  165. debug("+ep93xx_eth_open");
  166. /* Reset the MAC */
  167. ep93xx_mac_reset(dev);
  168. /* Reset the descriptor queues' current and end address values */
  169. priv->tx_dq.current = priv->tx_dq.base;
  170. priv->tx_dq.end = (priv->tx_dq.base + NUMTXDESC);
  171. priv->tx_sq.current = priv->tx_sq.base;
  172. priv->tx_sq.end = (priv->tx_sq.base + NUMTXDESC);
  173. priv->rx_dq.current = priv->rx_dq.base;
  174. priv->rx_dq.end = (priv->rx_dq.base + NUMRXDESC);
  175. priv->rx_sq.current = priv->rx_sq.base;
  176. priv->rx_sq.end = (priv->rx_sq.base + NUMRXDESC);
  177. /*
  178. * Set the transmit descriptor and status queues' base address,
  179. * current address, and length registers. Set the maximum frame
  180. * length and threshold. Enable the transmit descriptor processor.
  181. */
  182. writel((uint32_t)priv->tx_dq.base, &mac->txdq.badd);
  183. writel((uint32_t)priv->tx_dq.base, &mac->txdq.curadd);
  184. writel(sizeof(struct tx_descriptor) * NUMTXDESC, &mac->txdq.blen);
  185. writel((uint32_t)priv->tx_sq.base, &mac->txstsq.badd);
  186. writel((uint32_t)priv->tx_sq.base, &mac->txstsq.curadd);
  187. writel(sizeof(struct tx_status) * NUMTXDESC, &mac->txstsq.blen);
  188. writel(0x00040000, &mac->txdthrshld);
  189. writel(0x00040000, &mac->txststhrshld);
  190. writel((TXSTARTMAX << 0) | (PKTSIZE_ALIGN << 16), &mac->maxfrmlen);
  191. writel(BMCTL_TXEN, &mac->bmctl);
  192. /*
  193. * Set the receive descriptor and status queues' base address,
  194. * current address, and length registers. Enable the receive
  195. * descriptor processor.
  196. */
  197. writel((uint32_t)priv->rx_dq.base, &mac->rxdq.badd);
  198. writel((uint32_t)priv->rx_dq.base, &mac->rxdq.curadd);
  199. writel(sizeof(struct rx_descriptor) * NUMRXDESC, &mac->rxdq.blen);
  200. writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.badd);
  201. writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.curadd);
  202. writel(sizeof(struct rx_status) * NUMRXDESC, &mac->rxstsq.blen);
  203. writel(0x00040000, &mac->rxdthrshld);
  204. writel(BMCTL_RXEN, &mac->bmctl);
  205. writel(0x00040000, &mac->rxststhrshld);
  206. /* Wait until the receive descriptor processor is active */
  207. while (!(readl(&mac->bmsts) & BMSTS_RXACT))
  208. ; /* noop */
  209. /*
  210. * Initialize the RX descriptor queue. Clear the TX descriptor queue.
  211. * Clear the RX and TX status queues. Enqueue the RX descriptor and
  212. * status entries to the MAC.
  213. */
  214. for (i = 0; i < NUMRXDESC; i++) {
  215. /* set buffer address */
  216. (priv->rx_dq.base + i)->word1 = (uint32_t)NetRxPackets[i];
  217. /* set buffer length, clear buffer index and NSOF */
  218. (priv->rx_dq.base + i)->word2 = PKTSIZE_ALIGN;
  219. }
  220. memset(priv->tx_dq.base, 0,
  221. (sizeof(struct tx_descriptor) * NUMTXDESC));
  222. memset(priv->rx_sq.base, 0,
  223. (sizeof(struct rx_status) * NUMRXDESC));
  224. memset(priv->tx_sq.base, 0,
  225. (sizeof(struct tx_status) * NUMTXDESC));
  226. writel(NUMRXDESC, &mac->rxdqenq);
  227. writel(NUMRXDESC, &mac->rxstsqenq);
  228. /* Set the primary MAC address */
  229. writel(AFP_IAPRIMARY, &mac->afp);
  230. writel(mac_addr[0] | (mac_addr[1] << 8) |
  231. (mac_addr[2] << 16) | (mac_addr[3] << 24),
  232. &mac->indad);
  233. writel(mac_addr[4] | (mac_addr[5] << 8), &mac->indad_upper);
  234. /* Turn on RX and TX */
  235. writel(RXCTL_IA0 | RXCTL_BA | RXCTL_SRXON |
  236. RXCTL_RCRCA | RXCTL_MA, &mac->rxctl);
  237. writel(TXCTL_STXON, &mac->txctl);
  238. /* Dump data structures if we're debugging */
  239. dump_dev(dev);
  240. dump_rx_descriptor_queue(dev);
  241. dump_rx_status_queue(dev);
  242. dump_tx_descriptor_queue(dev);
  243. dump_tx_status_queue(dev);
  244. debug("-ep93xx_eth_open");
  245. return 1;
  246. }
  247. /**
  248. * Halt EP93xx MAC transmit and receive by clearing the TxCTL and RxCTL
  249. * registers.
  250. */
  251. static void ep93xx_eth_close(struct eth_device *dev)
  252. {
  253. struct mac_regs *mac = GET_REGS(dev);
  254. debug("+ep93xx_eth_close");
  255. writel(0x00000000, &mac->rxctl);
  256. writel(0x00000000, &mac->txctl);
  257. debug("-ep93xx_eth_close");
  258. }
  259. /**
  260. * Copy a frame of data from the MAC into the protocol layer for further
  261. * processing.
  262. */
  263. static int ep93xx_eth_rcv_packet(struct eth_device *dev)
  264. {
  265. struct mac_regs *mac = GET_REGS(dev);
  266. struct ep93xx_priv *priv = GET_PRIV(dev);
  267. int len = -1;
  268. debug("+ep93xx_eth_rcv_packet");
  269. if (RX_STATUS_RFP(priv->rx_sq.current)) {
  270. if (RX_STATUS_RWE(priv->rx_sq.current)) {
  271. /*
  272. * We have a good frame. Extract the frame's length
  273. * from the current rx_status_queue entry, and copy
  274. * the frame's data into NetRxPackets[] of the
  275. * protocol stack. We track the total number of
  276. * bytes in the frame (nbytes_frame) which will be
  277. * used when we pass the data off to the protocol
  278. * layer via NetReceive().
  279. */
  280. len = RX_STATUS_FRAME_LEN(priv->rx_sq.current);
  281. NetReceive((uchar *)priv->rx_dq.current->word1, len);
  282. debug("reporting %d bytes...\n", len);
  283. } else {
  284. /* Do we have an erroneous packet? */
  285. error("packet rx error, status %08X %08X",
  286. priv->rx_sq.current->word1,
  287. priv->rx_sq.current->word2);
  288. dump_rx_descriptor_queue(dev);
  289. dump_rx_status_queue(dev);
  290. }
  291. /*
  292. * Clear the associated status queue entry, and
  293. * increment our current pointers to the next RX
  294. * descriptor and status queue entries (making sure
  295. * we wrap properly).
  296. */
  297. memset((void *)priv->rx_sq.current, 0,
  298. sizeof(struct rx_status));
  299. priv->rx_sq.current++;
  300. if (priv->rx_sq.current >= priv->rx_sq.end)
  301. priv->rx_sq.current = priv->rx_sq.base;
  302. priv->rx_dq.current++;
  303. if (priv->rx_dq.current >= priv->rx_dq.end)
  304. priv->rx_dq.current = priv->rx_dq.base;
  305. /*
  306. * Finally, return the RX descriptor and status entries
  307. * back to the MAC engine, and loop again, checking for
  308. * more descriptors to process.
  309. */
  310. writel(1, &mac->rxdqenq);
  311. writel(1, &mac->rxstsqenq);
  312. } else {
  313. len = 0;
  314. }
  315. debug("-ep93xx_eth_rcv_packet %d", len);
  316. return len;
  317. }
  318. /**
  319. * Send a block of data via ethernet.
  320. */
  321. static int ep93xx_eth_send_packet(struct eth_device *dev,
  322. volatile void * const packet, int const length)
  323. {
  324. struct mac_regs *mac = GET_REGS(dev);
  325. struct ep93xx_priv *priv = GET_PRIV(dev);
  326. int ret = -1;
  327. debug("+ep93xx_eth_send_packet");
  328. /* Parameter check */
  329. BUG_ON(packet == NULL);
  330. /*
  331. * Initialize the TX descriptor queue with the new packet's info.
  332. * Clear the associated status queue entry. Enqueue the packet
  333. * to the MAC for transmission.
  334. */
  335. /* set buffer address */
  336. priv->tx_dq.current->word1 = (uint32_t)packet;
  337. /* set buffer length and EOF bit */
  338. priv->tx_dq.current->word2 = length | TX_DESC_EOF;
  339. /* clear tx status */
  340. priv->tx_sq.current->word1 = 0;
  341. /* enqueue the TX descriptor */
  342. writel(1, &mac->txdqenq);
  343. /* wait for the frame to become processed */
  344. while (!TX_STATUS_TXFP(priv->tx_sq.current))
  345. ; /* noop */
  346. if (!TX_STATUS_TXWE(priv->tx_sq.current)) {
  347. error("packet tx error, status %08X",
  348. priv->tx_sq.current->word1);
  349. dump_tx_descriptor_queue(dev);
  350. dump_tx_status_queue(dev);
  351. /* TODO: Add better error handling? */
  352. goto eth_send_out;
  353. }
  354. ret = 0;
  355. /* Fall through */
  356. eth_send_out:
  357. debug("-ep93xx_eth_send_packet %d", ret);
  358. return ret;
  359. }
  360. #if defined(CONFIG_MII)
  361. int ep93xx_miiphy_initialize(bd_t * const bd)
  362. {
  363. miiphy_register("ep93xx_eth0", ep93xx_miiphy_read, ep93xx_miiphy_write);
  364. return 0;
  365. }
  366. #endif
  367. /**
  368. * Initialize the EP93xx MAC. The MAC hardware is reset. Buffers are
  369. * allocated, if necessary, for the TX and RX descriptor and status queues,
  370. * as well as for received packets. The EP93XX MAC hardware is initialized.
  371. * Transmit and receive operations are enabled.
  372. */
  373. int ep93xx_eth_initialize(u8 dev_num, int base_addr)
  374. {
  375. int ret = -1;
  376. struct eth_device *dev;
  377. struct ep93xx_priv *priv;
  378. debug("+ep93xx_eth_initialize");
  379. priv = malloc(sizeof(*priv));
  380. if (!priv) {
  381. error("malloc() failed");
  382. goto eth_init_failed_0;
  383. }
  384. memset(priv, 0, sizeof(*priv));
  385. priv->regs = (struct mac_regs *)base_addr;
  386. priv->tx_dq.base = calloc(NUMTXDESC,
  387. sizeof(struct tx_descriptor));
  388. if (priv->tx_dq.base == NULL) {
  389. error("calloc() failed");
  390. goto eth_init_failed_1;
  391. }
  392. priv->tx_sq.base = calloc(NUMTXDESC,
  393. sizeof(struct tx_status));
  394. if (priv->tx_sq.base == NULL) {
  395. error("calloc() failed");
  396. goto eth_init_failed_2;
  397. }
  398. priv->rx_dq.base = calloc(NUMRXDESC,
  399. sizeof(struct rx_descriptor));
  400. if (priv->rx_dq.base == NULL) {
  401. error("calloc() failed");
  402. goto eth_init_failed_3;
  403. }
  404. priv->rx_sq.base = calloc(NUMRXDESC,
  405. sizeof(struct rx_status));
  406. if (priv->rx_sq.base == NULL) {
  407. error("calloc() failed");
  408. goto eth_init_failed_4;
  409. }
  410. dev = malloc(sizeof *dev);
  411. if (dev == NULL) {
  412. error("malloc() failed");
  413. goto eth_init_failed_5;
  414. }
  415. memset(dev, 0, sizeof *dev);
  416. dev->iobase = base_addr;
  417. dev->priv = priv;
  418. dev->init = ep93xx_eth_open;
  419. dev->halt = ep93xx_eth_close;
  420. dev->send = ep93xx_eth_send_packet;
  421. dev->recv = ep93xx_eth_rcv_packet;
  422. sprintf(dev->name, "ep93xx_eth-%hu", dev_num);
  423. eth_register(dev);
  424. /* Done! */
  425. ret = 1;
  426. goto eth_init_done;
  427. eth_init_failed_5:
  428. free(priv->rx_sq.base);
  429. /* Fall through */
  430. eth_init_failed_4:
  431. free(priv->rx_dq.base);
  432. /* Fall through */
  433. eth_init_failed_3:
  434. free(priv->tx_sq.base);
  435. /* Fall through */
  436. eth_init_failed_2:
  437. free(priv->tx_dq.base);
  438. /* Fall through */
  439. eth_init_failed_1:
  440. free(priv);
  441. /* Fall through */
  442. eth_init_failed_0:
  443. /* Fall through */
  444. eth_init_done:
  445. debug("-ep93xx_eth_initialize %d", ret);
  446. return ret;
  447. }
  448. #if defined(CONFIG_MII)
  449. /**
  450. * Maximum MII address we support
  451. */
  452. #define MII_ADDRESS_MAX 31
  453. /**
  454. * Maximum MII register address we support
  455. */
  456. #define MII_REGISTER_MAX 31
  457. /**
  458. * Read a 16-bit value from an MII register.
  459. */
  460. static int ep93xx_miiphy_read(const char * const dev, unsigned char const addr,
  461. unsigned char const reg, unsigned short * const value)
  462. {
  463. struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
  464. int ret = -1;
  465. uint32_t self_ctl;
  466. debug("+ep93xx_miiphy_read");
  467. /* Parameter checks */
  468. BUG_ON(dev == NULL);
  469. BUG_ON(addr > MII_ADDRESS_MAX);
  470. BUG_ON(reg > MII_REGISTER_MAX);
  471. BUG_ON(value == NULL);
  472. /*
  473. * Save the current SelfCTL register value. Set MAC to suppress
  474. * preamble bits. Wait for any previous MII command to complete
  475. * before issuing the new command.
  476. */
  477. self_ctl = readl(&mac->selfctl);
  478. #if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
  479. writel(self_ctl & ~(1 << 8), &mac->selfctl);
  480. #endif /* defined(CONFIG_MII_SUPPRESS_PREAMBLE) */
  481. while (readl(&mac->miists) & MIISTS_BUSY)
  482. ; /* noop */
  483. /*
  484. * Issue the MII 'read' command. Wait for the command to complete.
  485. * Read the MII data value.
  486. */
  487. writel(MIICMD_OPCODE_READ | ((uint32_t)addr << 5) | (uint32_t)reg,
  488. &mac->miicmd);
  489. while (readl(&mac->miists) & MIISTS_BUSY)
  490. ; /* noop */
  491. *value = (unsigned short)readl(&mac->miidata);
  492. /* Restore the saved SelfCTL value and return. */
  493. writel(self_ctl, &mac->selfctl);
  494. ret = 0;
  495. /* Fall through */
  496. debug("-ep93xx_miiphy_read");
  497. return ret;
  498. }
  499. /**
  500. * Write a 16-bit value to an MII register.
  501. */
  502. static int ep93xx_miiphy_write(const char * const dev, unsigned char const addr,
  503. unsigned char const reg, unsigned short const value)
  504. {
  505. struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
  506. int ret = -1;
  507. uint32_t self_ctl;
  508. debug("+ep93xx_miiphy_write");
  509. /* Parameter checks */
  510. BUG_ON(dev == NULL);
  511. BUG_ON(addr > MII_ADDRESS_MAX);
  512. BUG_ON(reg > MII_REGISTER_MAX);
  513. /*
  514. * Save the current SelfCTL register value. Set MAC to suppress
  515. * preamble bits. Wait for any previous MII command to complete
  516. * before issuing the new command.
  517. */
  518. self_ctl = readl(&mac->selfctl);
  519. #if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
  520. writel(self_ctl & ~(1 << 8), &mac->selfctl);
  521. #endif /* defined(CONFIG_MII_SUPPRESS_PREAMBLE) */
  522. while (readl(&mac->miists) & MIISTS_BUSY)
  523. ; /* noop */
  524. /* Issue the MII 'write' command. Wait for the command to complete. */
  525. writel((uint32_t)value, &mac->miidata);
  526. writel(MIICMD_OPCODE_WRITE | ((uint32_t)addr << 5) | (uint32_t)reg,
  527. &mac->miicmd);
  528. while (readl(&mac->miists) & MIISTS_BUSY)
  529. ; /* noop */
  530. /* Restore the saved SelfCTL value and return. */
  531. writel(self_ctl, &mac->selfctl);
  532. ret = 0;
  533. /* Fall through */
  534. debug("-ep93xx_miiphy_write");
  535. return ret;
  536. }
  537. #endif /* defined(CONFIG_MII) */