macb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * Copyright (C) 2005-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <common.h>
  19. /*
  20. * The u-boot networking stack is a little weird. It seems like the
  21. * networking core allocates receive buffers up front without any
  22. * regard to the hardware that's supposed to actually receive those
  23. * packets.
  24. *
  25. * The MACB receives packets into 128-byte receive buffers, so the
  26. * buffers allocated by the core isn't very practical to use. We'll
  27. * allocate our own, but we need one such buffer in case a packet
  28. * wraps around the DMA ring so that we have to copy it.
  29. *
  30. * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
  31. * configuration header. This way, the core allocates one RX buffer
  32. * and one TX buffer, each of which can hold a ethernet packet of
  33. * maximum size.
  34. *
  35. * For some reason, the networking core unconditionally specifies a
  36. * 32-byte packet "alignment" (which really should be called
  37. * "padding"). MACB shouldn't need that, but we'll refrain from any
  38. * core modifications here...
  39. */
  40. #include <net.h>
  41. #include <netdev.h>
  42. #include <malloc.h>
  43. #include <miiphy.h>
  44. #include <linux/mii.h>
  45. #include <asm/io.h>
  46. #include <asm/dma-mapping.h>
  47. #include <asm/arch/clk.h>
  48. #include "macb.h"
  49. #define CONFIG_SYS_MACB_RX_BUFFER_SIZE 4096
  50. #define CONFIG_SYS_MACB_RX_RING_SIZE (CONFIG_SYS_MACB_RX_BUFFER_SIZE / 128)
  51. #define CONFIG_SYS_MACB_TX_RING_SIZE 16
  52. #define CONFIG_SYS_MACB_TX_TIMEOUT 1000
  53. #define CONFIG_SYS_MACB_AUTONEG_TIMEOUT 5000000
  54. struct macb_dma_desc {
  55. u32 addr;
  56. u32 ctrl;
  57. };
  58. #define RXADDR_USED 0x00000001
  59. #define RXADDR_WRAP 0x00000002
  60. #define RXBUF_FRMLEN_MASK 0x00000fff
  61. #define RXBUF_FRAME_START 0x00004000
  62. #define RXBUF_FRAME_END 0x00008000
  63. #define RXBUF_TYPEID_MATCH 0x00400000
  64. #define RXBUF_ADDR4_MATCH 0x00800000
  65. #define RXBUF_ADDR3_MATCH 0x01000000
  66. #define RXBUF_ADDR2_MATCH 0x02000000
  67. #define RXBUF_ADDR1_MATCH 0x04000000
  68. #define RXBUF_BROADCAST 0x80000000
  69. #define TXBUF_FRMLEN_MASK 0x000007ff
  70. #define TXBUF_FRAME_END 0x00008000
  71. #define TXBUF_NOCRC 0x00010000
  72. #define TXBUF_EXHAUSTED 0x08000000
  73. #define TXBUF_UNDERRUN 0x10000000
  74. #define TXBUF_MAXRETRY 0x20000000
  75. #define TXBUF_WRAP 0x40000000
  76. #define TXBUF_USED 0x80000000
  77. struct macb_device {
  78. void *regs;
  79. unsigned int rx_tail;
  80. unsigned int tx_head;
  81. unsigned int tx_tail;
  82. void *rx_buffer;
  83. void *tx_buffer;
  84. struct macb_dma_desc *rx_ring;
  85. struct macb_dma_desc *tx_ring;
  86. unsigned long rx_buffer_dma;
  87. unsigned long rx_ring_dma;
  88. unsigned long tx_ring_dma;
  89. const struct device *dev;
  90. struct eth_device netdev;
  91. unsigned short phy_addr;
  92. struct mii_dev *bus;
  93. };
  94. #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
  95. static int macb_is_gem(struct macb_device *macb)
  96. {
  97. return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
  98. }
  99. static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
  100. {
  101. unsigned long netctl;
  102. unsigned long netstat;
  103. unsigned long frame;
  104. netctl = macb_readl(macb, NCR);
  105. netctl |= MACB_BIT(MPE);
  106. macb_writel(macb, NCR, netctl);
  107. frame = (MACB_BF(SOF, 1)
  108. | MACB_BF(RW, 1)
  109. | MACB_BF(PHYA, macb->phy_addr)
  110. | MACB_BF(REGA, reg)
  111. | MACB_BF(CODE, 2)
  112. | MACB_BF(DATA, value));
  113. macb_writel(macb, MAN, frame);
  114. do {
  115. netstat = macb_readl(macb, NSR);
  116. } while (!(netstat & MACB_BIT(IDLE)));
  117. netctl = macb_readl(macb, NCR);
  118. netctl &= ~MACB_BIT(MPE);
  119. macb_writel(macb, NCR, netctl);
  120. }
  121. static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
  122. {
  123. unsigned long netctl;
  124. unsigned long netstat;
  125. unsigned long frame;
  126. netctl = macb_readl(macb, NCR);
  127. netctl |= MACB_BIT(MPE);
  128. macb_writel(macb, NCR, netctl);
  129. frame = (MACB_BF(SOF, 1)
  130. | MACB_BF(RW, 2)
  131. | MACB_BF(PHYA, macb->phy_addr)
  132. | MACB_BF(REGA, reg)
  133. | MACB_BF(CODE, 2));
  134. macb_writel(macb, MAN, frame);
  135. do {
  136. netstat = macb_readl(macb, NSR);
  137. } while (!(netstat & MACB_BIT(IDLE)));
  138. frame = macb_readl(macb, MAN);
  139. netctl = macb_readl(macb, NCR);
  140. netctl &= ~MACB_BIT(MPE);
  141. macb_writel(macb, NCR, netctl);
  142. return MACB_BFEXT(DATA, frame);
  143. }
  144. void __weak arch_get_mdio_control(const char *name)
  145. {
  146. return;
  147. }
  148. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  149. int macb_miiphy_read(const char *devname, u8 phy_adr, u8 reg, u16 *value)
  150. {
  151. struct eth_device *dev = eth_get_dev_by_name(devname);
  152. struct macb_device *macb = to_macb(dev);
  153. if ( macb->phy_addr != phy_adr )
  154. return -1;
  155. arch_get_mdio_control(devname);
  156. *value = macb_mdio_read(macb, reg);
  157. return 0;
  158. }
  159. int macb_miiphy_write(const char *devname, u8 phy_adr, u8 reg, u16 value)
  160. {
  161. struct eth_device *dev = eth_get_dev_by_name(devname);
  162. struct macb_device *macb = to_macb(dev);
  163. if ( macb->phy_addr != phy_adr )
  164. return -1;
  165. arch_get_mdio_control(devname);
  166. macb_mdio_write(macb, reg, value);
  167. return 0;
  168. }
  169. #endif
  170. #if defined(CONFIG_CMD_NET)
  171. static int macb_send(struct eth_device *netdev, void *packet, int length)
  172. {
  173. struct macb_device *macb = to_macb(netdev);
  174. unsigned long paddr, ctrl;
  175. unsigned int tx_head = macb->tx_head;
  176. int i;
  177. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  178. ctrl = length & TXBUF_FRMLEN_MASK;
  179. ctrl |= TXBUF_FRAME_END;
  180. if (tx_head == (CONFIG_SYS_MACB_TX_RING_SIZE - 1)) {
  181. ctrl |= TXBUF_WRAP;
  182. macb->tx_head = 0;
  183. } else
  184. macb->tx_head++;
  185. macb->tx_ring[tx_head].ctrl = ctrl;
  186. macb->tx_ring[tx_head].addr = paddr;
  187. barrier();
  188. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  189. /*
  190. * I guess this is necessary because the networking core may
  191. * re-use the transmit buffer as soon as we return...
  192. */
  193. for (i = 0; i <= CONFIG_SYS_MACB_TX_TIMEOUT; i++) {
  194. barrier();
  195. ctrl = macb->tx_ring[tx_head].ctrl;
  196. if (ctrl & TXBUF_USED)
  197. break;
  198. udelay(1);
  199. }
  200. dma_unmap_single(packet, length, paddr);
  201. if (i <= CONFIG_SYS_MACB_TX_TIMEOUT) {
  202. if (ctrl & TXBUF_UNDERRUN)
  203. printf("%s: TX underrun\n", netdev->name);
  204. if (ctrl & TXBUF_EXHAUSTED)
  205. printf("%s: TX buffers exhausted in mid frame\n",
  206. netdev->name);
  207. } else {
  208. printf("%s: TX timeout\n", netdev->name);
  209. }
  210. /* No one cares anyway */
  211. return 0;
  212. }
  213. static void reclaim_rx_buffers(struct macb_device *macb,
  214. unsigned int new_tail)
  215. {
  216. unsigned int i;
  217. i = macb->rx_tail;
  218. while (i > new_tail) {
  219. macb->rx_ring[i].addr &= ~RXADDR_USED;
  220. i++;
  221. if (i > CONFIG_SYS_MACB_RX_RING_SIZE)
  222. i = 0;
  223. }
  224. while (i < new_tail) {
  225. macb->rx_ring[i].addr &= ~RXADDR_USED;
  226. i++;
  227. }
  228. barrier();
  229. macb->rx_tail = new_tail;
  230. }
  231. static int macb_recv(struct eth_device *netdev)
  232. {
  233. struct macb_device *macb = to_macb(netdev);
  234. unsigned int rx_tail = macb->rx_tail;
  235. void *buffer;
  236. int length;
  237. int wrapped = 0;
  238. u32 status;
  239. for (;;) {
  240. if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
  241. return -1;
  242. status = macb->rx_ring[rx_tail].ctrl;
  243. if (status & RXBUF_FRAME_START) {
  244. if (rx_tail != macb->rx_tail)
  245. reclaim_rx_buffers(macb, rx_tail);
  246. wrapped = 0;
  247. }
  248. if (status & RXBUF_FRAME_END) {
  249. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  250. length = status & RXBUF_FRMLEN_MASK;
  251. if (wrapped) {
  252. unsigned int headlen, taillen;
  253. headlen = 128 * (CONFIG_SYS_MACB_RX_RING_SIZE
  254. - macb->rx_tail);
  255. taillen = length - headlen;
  256. memcpy((void *)NetRxPackets[0],
  257. buffer, headlen);
  258. memcpy((void *)NetRxPackets[0] + headlen,
  259. macb->rx_buffer, taillen);
  260. buffer = (void *)NetRxPackets[0];
  261. }
  262. NetReceive(buffer, length);
  263. if (++rx_tail >= CONFIG_SYS_MACB_RX_RING_SIZE)
  264. rx_tail = 0;
  265. reclaim_rx_buffers(macb, rx_tail);
  266. } else {
  267. if (++rx_tail >= CONFIG_SYS_MACB_RX_RING_SIZE) {
  268. wrapped = 1;
  269. rx_tail = 0;
  270. }
  271. }
  272. barrier();
  273. }
  274. return 0;
  275. }
  276. static void macb_phy_reset(struct macb_device *macb)
  277. {
  278. struct eth_device *netdev = &macb->netdev;
  279. int i;
  280. u16 status, adv;
  281. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  282. macb_mdio_write(macb, MII_ADVERTISE, adv);
  283. printf("%s: Starting autonegotiation...\n", netdev->name);
  284. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  285. | BMCR_ANRESTART));
  286. for (i = 0; i < CONFIG_SYS_MACB_AUTONEG_TIMEOUT / 100; i++) {
  287. status = macb_mdio_read(macb, MII_BMSR);
  288. if (status & BMSR_ANEGCOMPLETE)
  289. break;
  290. udelay(100);
  291. }
  292. if (status & BMSR_ANEGCOMPLETE)
  293. printf("%s: Autonegotiation complete\n", netdev->name);
  294. else
  295. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  296. netdev->name, status);
  297. }
  298. #ifdef CONFIG_MACB_SEARCH_PHY
  299. static int macb_phy_find(struct macb_device *macb)
  300. {
  301. int i;
  302. u16 phy_id;
  303. /* Search for PHY... */
  304. for (i = 0; i < 32; i++) {
  305. macb->phy_addr = i;
  306. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  307. if (phy_id != 0xffff) {
  308. printf("%s: PHY present at %d\n", macb->netdev.name, i);
  309. return 1;
  310. }
  311. }
  312. /* PHY isn't up to snuff */
  313. printf("%s: PHY not found\n", macb->netdev.name);
  314. return 0;
  315. }
  316. #endif /* CONFIG_MACB_SEARCH_PHY */
  317. static int macb_phy_init(struct macb_device *macb)
  318. {
  319. struct eth_device *netdev = &macb->netdev;
  320. #ifdef CONFIG_PHYLIB
  321. struct phy_device *phydev;
  322. #endif
  323. u32 ncfgr;
  324. u16 phy_id, status, adv, lpa;
  325. int media, speed, duplex;
  326. int i;
  327. arch_get_mdio_control(netdev->name);
  328. #ifdef CONFIG_MACB_SEARCH_PHY
  329. /* Auto-detect phy_addr */
  330. if (!macb_phy_find(macb)) {
  331. return 0;
  332. }
  333. #endif /* CONFIG_MACB_SEARCH_PHY */
  334. /* Check if the PHY is up to snuff... */
  335. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  336. if (phy_id == 0xffff) {
  337. printf("%s: No PHY present\n", netdev->name);
  338. return 0;
  339. }
  340. #ifdef CONFIG_PHYLIB
  341. phydev->bus = macb->bus;
  342. phydev->dev = netdev;
  343. phydev->addr = macb->phy_addr;
  344. phy_config(phydev);
  345. #endif
  346. status = macb_mdio_read(macb, MII_BMSR);
  347. if (!(status & BMSR_LSTATUS)) {
  348. /* Try to re-negotiate if we don't have link already. */
  349. macb_phy_reset(macb);
  350. for (i = 0; i < CONFIG_SYS_MACB_AUTONEG_TIMEOUT / 100; i++) {
  351. status = macb_mdio_read(macb, MII_BMSR);
  352. if (status & BMSR_LSTATUS)
  353. break;
  354. udelay(100);
  355. }
  356. }
  357. if (!(status & BMSR_LSTATUS)) {
  358. printf("%s: link down (status: 0x%04x)\n",
  359. netdev->name, status);
  360. return 0;
  361. }
  362. /* First check for GMAC */
  363. if (macb_is_gem(macb)) {
  364. lpa = macb_mdio_read(macb, MII_STAT1000);
  365. if (lpa & (1 << 11)) {
  366. speed = 1000;
  367. duplex = 1;
  368. } else {
  369. if (lpa & (1 << 10)) {
  370. speed = 1000;
  371. duplex = 1;
  372. } else {
  373. speed = 0;
  374. }
  375. }
  376. if (speed == 1000) {
  377. printf("%s: link up, %dMbps %s-duplex (lpa: 0x%04x)\n",
  378. netdev->name,
  379. speed,
  380. duplex ? "full" : "half",
  381. lpa);
  382. ncfgr = macb_readl(macb, NCFGR);
  383. ncfgr &= ~(GEM_BIT(GBE) | MACB_BIT(SPD) | MACB_BIT(FD));
  384. if (speed)
  385. ncfgr |= GEM_BIT(GBE);
  386. if (duplex)
  387. ncfgr |= MACB_BIT(FD);
  388. macb_writel(macb, NCFGR, ncfgr);
  389. return 1;
  390. }
  391. }
  392. /* fall back for EMAC checking */
  393. adv = macb_mdio_read(macb, MII_ADVERTISE);
  394. lpa = macb_mdio_read(macb, MII_LPA);
  395. media = mii_nway_result(lpa & adv);
  396. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  397. ? 1 : 0);
  398. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  399. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  400. netdev->name,
  401. speed ? "100" : "10",
  402. duplex ? "full" : "half",
  403. lpa);
  404. ncfgr = macb_readl(macb, NCFGR);
  405. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  406. if (speed)
  407. ncfgr |= MACB_BIT(SPD);
  408. if (duplex)
  409. ncfgr |= MACB_BIT(FD);
  410. macb_writel(macb, NCFGR, ncfgr);
  411. return 1;
  412. }
  413. static int macb_init(struct eth_device *netdev, bd_t *bd)
  414. {
  415. struct macb_device *macb = to_macb(netdev);
  416. unsigned long paddr;
  417. int i;
  418. /*
  419. * macb_halt should have been called at some point before now,
  420. * so we'll assume the controller is idle.
  421. */
  422. /* initialize DMA descriptors */
  423. paddr = macb->rx_buffer_dma;
  424. for (i = 0; i < CONFIG_SYS_MACB_RX_RING_SIZE; i++) {
  425. if (i == (CONFIG_SYS_MACB_RX_RING_SIZE - 1))
  426. paddr |= RXADDR_WRAP;
  427. macb->rx_ring[i].addr = paddr;
  428. macb->rx_ring[i].ctrl = 0;
  429. paddr += 128;
  430. }
  431. for (i = 0; i < CONFIG_SYS_MACB_TX_RING_SIZE; i++) {
  432. macb->tx_ring[i].addr = 0;
  433. if (i == (CONFIG_SYS_MACB_TX_RING_SIZE - 1))
  434. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  435. else
  436. macb->tx_ring[i].ctrl = TXBUF_USED;
  437. }
  438. macb->rx_tail = macb->tx_head = macb->tx_tail = 0;
  439. macb_writel(macb, RBQP, macb->rx_ring_dma);
  440. macb_writel(macb, TBQP, macb->tx_ring_dma);
  441. if (macb_is_gem(macb)) {
  442. #ifdef CONFIG_RGMII
  443. gem_writel(macb, UR, GEM_BIT(RGMII));
  444. #else
  445. gem_writel(macb, UR, 0);
  446. #endif
  447. } else {
  448. /* choose RMII or MII mode. This depends on the board */
  449. #ifdef CONFIG_RMII
  450. #ifdef CONFIG_AT91FAMILY
  451. macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
  452. #else
  453. macb_writel(macb, USRIO, 0);
  454. #endif
  455. #else
  456. #ifdef CONFIG_AT91FAMILY
  457. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  458. #else
  459. macb_writel(macb, USRIO, MACB_BIT(MII));
  460. #endif
  461. #endif /* CONFIG_RMII */
  462. }
  463. if (!macb_phy_init(macb))
  464. return -1;
  465. /* Enable TX and RX */
  466. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  467. return 0;
  468. }
  469. static void macb_halt(struct eth_device *netdev)
  470. {
  471. struct macb_device *macb = to_macb(netdev);
  472. u32 ncr, tsr;
  473. /* Halt the controller and wait for any ongoing transmission to end. */
  474. ncr = macb_readl(macb, NCR);
  475. ncr |= MACB_BIT(THALT);
  476. macb_writel(macb, NCR, ncr);
  477. do {
  478. tsr = macb_readl(macb, TSR);
  479. } while (tsr & MACB_BIT(TGO));
  480. /* Disable TX and RX, and clear statistics */
  481. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  482. }
  483. static int macb_write_hwaddr(struct eth_device *dev)
  484. {
  485. struct macb_device *macb = to_macb(dev);
  486. u32 hwaddr_bottom;
  487. u16 hwaddr_top;
  488. /* set hardware address */
  489. hwaddr_bottom = dev->enetaddr[0] | dev->enetaddr[1] << 8 |
  490. dev->enetaddr[2] << 16 | dev->enetaddr[3] << 24;
  491. macb_writel(macb, SA1B, hwaddr_bottom);
  492. hwaddr_top = dev->enetaddr[4] | dev->enetaddr[5] << 8;
  493. macb_writel(macb, SA1T, hwaddr_top);
  494. return 0;
  495. }
  496. static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
  497. {
  498. u32 config;
  499. unsigned long macb_hz = get_macb_pclk_rate(id);
  500. if (macb_hz < 20000000)
  501. config = MACB_BF(CLK, MACB_CLK_DIV8);
  502. else if (macb_hz < 40000000)
  503. config = MACB_BF(CLK, MACB_CLK_DIV16);
  504. else if (macb_hz < 80000000)
  505. config = MACB_BF(CLK, MACB_CLK_DIV32);
  506. else
  507. config = MACB_BF(CLK, MACB_CLK_DIV64);
  508. return config;
  509. }
  510. static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
  511. {
  512. u32 config;
  513. unsigned long macb_hz = get_macb_pclk_rate(id);
  514. if (macb_hz < 20000000)
  515. config = GEM_BF(CLK, GEM_CLK_DIV8);
  516. else if (macb_hz < 40000000)
  517. config = GEM_BF(CLK, GEM_CLK_DIV16);
  518. else if (macb_hz < 80000000)
  519. config = GEM_BF(CLK, GEM_CLK_DIV32);
  520. else if (macb_hz < 120000000)
  521. config = GEM_BF(CLK, GEM_CLK_DIV48);
  522. else if (macb_hz < 160000000)
  523. config = GEM_BF(CLK, GEM_CLK_DIV64);
  524. else
  525. config = GEM_BF(CLK, GEM_CLK_DIV96);
  526. return config;
  527. }
  528. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  529. {
  530. struct macb_device *macb;
  531. struct eth_device *netdev;
  532. u32 ncfgr;
  533. macb = malloc(sizeof(struct macb_device));
  534. if (!macb) {
  535. printf("Error: Failed to allocate memory for MACB%d\n", id);
  536. return -1;
  537. }
  538. memset(macb, 0, sizeof(struct macb_device));
  539. netdev = &macb->netdev;
  540. macb->rx_buffer = dma_alloc_coherent(CONFIG_SYS_MACB_RX_BUFFER_SIZE,
  541. &macb->rx_buffer_dma);
  542. macb->rx_ring = dma_alloc_coherent(CONFIG_SYS_MACB_RX_RING_SIZE
  543. * sizeof(struct macb_dma_desc),
  544. &macb->rx_ring_dma);
  545. macb->tx_ring = dma_alloc_coherent(CONFIG_SYS_MACB_TX_RING_SIZE
  546. * sizeof(struct macb_dma_desc),
  547. &macb->tx_ring_dma);
  548. macb->regs = regs;
  549. macb->phy_addr = phy_addr;
  550. if (macb_is_gem(macb))
  551. sprintf(netdev->name, "gmac%d", id);
  552. else
  553. sprintf(netdev->name, "macb%d", id);
  554. netdev->init = macb_init;
  555. netdev->halt = macb_halt;
  556. netdev->send = macb_send;
  557. netdev->recv = macb_recv;
  558. netdev->write_hwaddr = macb_write_hwaddr;
  559. /*
  560. * Do some basic initialization so that we at least can talk
  561. * to the PHY
  562. */
  563. if (macb_is_gem(macb)) {
  564. ncfgr = gem_mdc_clk_div(id, macb);
  565. ncfgr |= GEM_BF(DBW, 1);
  566. } else {
  567. ncfgr = macb_mdc_clk_div(id, macb);
  568. }
  569. macb_writel(macb, NCFGR, ncfgr);
  570. eth_register(netdev);
  571. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  572. miiphy_register(netdev->name, macb_miiphy_read, macb_miiphy_write);
  573. macb->bus = miiphy_get_dev_by_name(netdev->name);
  574. #endif
  575. return 0;
  576. }
  577. #endif