macb.c 16 KB

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