macb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 MACB_RX_BUFFER_SIZE 4096
  39. #define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128)
  40. #define MACB_TX_RING_SIZE 16
  41. #define MACB_TX_TIMEOUT 1000
  42. #define MACB_AUTONEG_TIMEOUT 5000000
  43. struct macb_dma_desc {
  44. u32 addr;
  45. u32 ctrl;
  46. };
  47. #define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
  48. #define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
  49. #define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
  50. #define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
  51. #define RXADDR_USED 0x00000001
  52. #define RXADDR_WRAP 0x00000002
  53. #define RXBUF_FRMLEN_MASK 0x00000fff
  54. #define RXBUF_FRAME_START 0x00004000
  55. #define RXBUF_FRAME_END 0x00008000
  56. #define RXBUF_TYPEID_MATCH 0x00400000
  57. #define RXBUF_ADDR4_MATCH 0x00800000
  58. #define RXBUF_ADDR3_MATCH 0x01000000
  59. #define RXBUF_ADDR2_MATCH 0x02000000
  60. #define RXBUF_ADDR1_MATCH 0x04000000
  61. #define RXBUF_BROADCAST 0x80000000
  62. #define TXBUF_FRMLEN_MASK 0x000007ff
  63. #define TXBUF_FRAME_END 0x00008000
  64. #define TXBUF_NOCRC 0x00010000
  65. #define TXBUF_EXHAUSTED 0x08000000
  66. #define TXBUF_UNDERRUN 0x10000000
  67. #define TXBUF_MAXRETRY 0x20000000
  68. #define TXBUF_WRAP 0x40000000
  69. #define TXBUF_USED 0x80000000
  70. struct macb_device {
  71. void *regs;
  72. unsigned int rx_tail;
  73. unsigned int tx_head;
  74. unsigned int tx_tail;
  75. unsigned int next_rx_tail;
  76. bool wrapped;
  77. void *rx_buffer;
  78. void *tx_buffer;
  79. struct macb_dma_desc *rx_ring;
  80. struct macb_dma_desc *tx_ring;
  81. unsigned long rx_buffer_dma;
  82. unsigned long rx_ring_dma;
  83. unsigned long tx_ring_dma;
  84. struct macb_dma_desc *dummy_desc;
  85. unsigned long dummy_desc_dma;
  86. const struct device *dev;
  87. struct eth_device netdev;
  88. unsigned short phy_addr;
  89. struct mii_dev *bus;
  90. };
  91. #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
  92. static int macb_is_gem(struct macb_device *macb)
  93. {
  94. return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
  95. }
  96. #ifndef cpu_is_sama5d2
  97. #define cpu_is_sama5d2() 0
  98. #endif
  99. #ifndef cpu_is_sama5d4
  100. #define cpu_is_sama5d4() 0
  101. #endif
  102. static int gem_is_gigabit_capable(struct macb_device *macb)
  103. {
  104. /*
  105. * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
  106. * configured to support only 10/100.
  107. */
  108. return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
  109. }
  110. static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
  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, 1)
  120. | MACB_BF(PHYA, macb->phy_addr)
  121. | MACB_BF(REGA, reg)
  122. | MACB_BF(CODE, 2)
  123. | MACB_BF(DATA, value));
  124. macb_writel(macb, MAN, frame);
  125. do {
  126. netstat = macb_readl(macb, NSR);
  127. } while (!(netstat & MACB_BIT(IDLE)));
  128. netctl = macb_readl(macb, NCR);
  129. netctl &= ~MACB_BIT(MPE);
  130. macb_writel(macb, NCR, netctl);
  131. }
  132. static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
  133. {
  134. unsigned long netctl;
  135. unsigned long netstat;
  136. unsigned long frame;
  137. netctl = macb_readl(macb, NCR);
  138. netctl |= MACB_BIT(MPE);
  139. macb_writel(macb, NCR, netctl);
  140. frame = (MACB_BF(SOF, 1)
  141. | MACB_BF(RW, 2)
  142. | MACB_BF(PHYA, macb->phy_addr)
  143. | MACB_BF(REGA, reg)
  144. | MACB_BF(CODE, 2));
  145. macb_writel(macb, MAN, frame);
  146. do {
  147. netstat = macb_readl(macb, NSR);
  148. } while (!(netstat & MACB_BIT(IDLE)));
  149. frame = macb_readl(macb, MAN);
  150. netctl = macb_readl(macb, NCR);
  151. netctl &= ~MACB_BIT(MPE);
  152. macb_writel(macb, NCR, netctl);
  153. return MACB_BFEXT(DATA, frame);
  154. }
  155. void __weak arch_get_mdio_control(const char *name)
  156. {
  157. return;
  158. }
  159. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  160. int macb_miiphy_read(const char *devname, u8 phy_adr, u8 reg, u16 *value)
  161. {
  162. struct eth_device *dev = eth_get_dev_by_name(devname);
  163. struct macb_device *macb = to_macb(dev);
  164. if (macb->phy_addr != phy_adr)
  165. return -1;
  166. arch_get_mdio_control(devname);
  167. *value = macb_mdio_read(macb, reg);
  168. return 0;
  169. }
  170. int macb_miiphy_write(const char *devname, u8 phy_adr, u8 reg, u16 value)
  171. {
  172. struct eth_device *dev = eth_get_dev_by_name(devname);
  173. struct macb_device *macb = to_macb(dev);
  174. if (macb->phy_addr != phy_adr)
  175. return -1;
  176. arch_get_mdio_control(devname);
  177. macb_mdio_write(macb, reg, value);
  178. return 0;
  179. }
  180. #endif
  181. #define RX 1
  182. #define TX 0
  183. static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
  184. {
  185. if (rx)
  186. invalidate_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  187. MACB_RX_DMA_DESC_SIZE);
  188. else
  189. invalidate_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  190. MACB_TX_DMA_DESC_SIZE);
  191. }
  192. static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
  193. {
  194. if (rx)
  195. flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  196. MACB_RX_DMA_DESC_SIZE);
  197. else
  198. flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  199. MACB_TX_DMA_DESC_SIZE);
  200. }
  201. static inline void macb_flush_rx_buffer(struct macb_device *macb)
  202. {
  203. flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  204. MACB_RX_BUFFER_SIZE);
  205. }
  206. static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
  207. {
  208. invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  209. MACB_RX_BUFFER_SIZE);
  210. }
  211. #if defined(CONFIG_CMD_NET)
  212. static int _macb_send(struct macb_device *macb, const char *name, void *packet,
  213. int length)
  214. {
  215. unsigned long paddr, ctrl;
  216. unsigned int tx_head = macb->tx_head;
  217. int i;
  218. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  219. ctrl = length & TXBUF_FRMLEN_MASK;
  220. ctrl |= TXBUF_FRAME_END;
  221. if (tx_head == (MACB_TX_RING_SIZE - 1)) {
  222. ctrl |= TXBUF_WRAP;
  223. macb->tx_head = 0;
  224. } else {
  225. macb->tx_head++;
  226. }
  227. macb->tx_ring[tx_head].ctrl = ctrl;
  228. macb->tx_ring[tx_head].addr = paddr;
  229. barrier();
  230. macb_flush_ring_desc(macb, TX);
  231. /* Do we need check paddr and length is dcache line aligned? */
  232. flush_dcache_range(paddr, paddr + length);
  233. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  234. /*
  235. * I guess this is necessary because the networking core may
  236. * re-use the transmit buffer as soon as we return...
  237. */
  238. for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
  239. barrier();
  240. macb_invalidate_ring_desc(macb, TX);
  241. ctrl = macb->tx_ring[tx_head].ctrl;
  242. if (ctrl & TXBUF_USED)
  243. break;
  244. udelay(1);
  245. }
  246. dma_unmap_single(packet, length, paddr);
  247. if (i <= MACB_TX_TIMEOUT) {
  248. if (ctrl & TXBUF_UNDERRUN)
  249. printf("%s: TX underrun\n", name);
  250. if (ctrl & TXBUF_EXHAUSTED)
  251. printf("%s: TX buffers exhausted in mid frame\n", name);
  252. } else {
  253. printf("%s: TX timeout\n", name);
  254. }
  255. /* No one cares anyway */
  256. return 0;
  257. }
  258. static void reclaim_rx_buffers(struct macb_device *macb,
  259. unsigned int new_tail)
  260. {
  261. unsigned int i;
  262. i = macb->rx_tail;
  263. macb_invalidate_ring_desc(macb, RX);
  264. while (i > new_tail) {
  265. macb->rx_ring[i].addr &= ~RXADDR_USED;
  266. i++;
  267. if (i > MACB_RX_RING_SIZE)
  268. i = 0;
  269. }
  270. while (i < new_tail) {
  271. macb->rx_ring[i].addr &= ~RXADDR_USED;
  272. i++;
  273. }
  274. barrier();
  275. macb_flush_ring_desc(macb, RX);
  276. macb->rx_tail = new_tail;
  277. }
  278. static int _macb_recv(struct macb_device *macb, uchar **packetp)
  279. {
  280. unsigned int next_rx_tail = macb->next_rx_tail;
  281. void *buffer;
  282. int length;
  283. u32 status;
  284. macb->wrapped = false;
  285. for (;;) {
  286. macb_invalidate_ring_desc(macb, RX);
  287. if (!(macb->rx_ring[next_rx_tail].addr & RXADDR_USED))
  288. return -EAGAIN;
  289. status = macb->rx_ring[next_rx_tail].ctrl;
  290. if (status & RXBUF_FRAME_START) {
  291. if (next_rx_tail != macb->rx_tail)
  292. reclaim_rx_buffers(macb, next_rx_tail);
  293. macb->wrapped = false;
  294. }
  295. if (status & RXBUF_FRAME_END) {
  296. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  297. length = status & RXBUF_FRMLEN_MASK;
  298. macb_invalidate_rx_buffer(macb);
  299. if (macb->wrapped) {
  300. unsigned int headlen, taillen;
  301. headlen = 128 * (MACB_RX_RING_SIZE
  302. - macb->rx_tail);
  303. taillen = length - headlen;
  304. memcpy((void *)net_rx_packets[0],
  305. buffer, headlen);
  306. memcpy((void *)net_rx_packets[0] + headlen,
  307. macb->rx_buffer, taillen);
  308. *packetp = (void *)net_rx_packets[0];
  309. } else {
  310. *packetp = buffer;
  311. }
  312. if (++next_rx_tail >= MACB_RX_RING_SIZE)
  313. next_rx_tail = 0;
  314. macb->next_rx_tail = next_rx_tail;
  315. return length;
  316. } else {
  317. if (++next_rx_tail >= MACB_RX_RING_SIZE) {
  318. macb->wrapped = true;
  319. next_rx_tail = 0;
  320. }
  321. }
  322. barrier();
  323. }
  324. }
  325. static void macb_phy_reset(struct macb_device *macb, const char *name)
  326. {
  327. int i;
  328. u16 status, adv;
  329. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  330. macb_mdio_write(macb, MII_ADVERTISE, adv);
  331. printf("%s: Starting autonegotiation...\n", name);
  332. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  333. | BMCR_ANRESTART));
  334. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  335. status = macb_mdio_read(macb, MII_BMSR);
  336. if (status & BMSR_ANEGCOMPLETE)
  337. break;
  338. udelay(100);
  339. }
  340. if (status & BMSR_ANEGCOMPLETE)
  341. printf("%s: Autonegotiation complete\n", name);
  342. else
  343. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  344. name, status);
  345. }
  346. #ifdef CONFIG_MACB_SEARCH_PHY
  347. static int macb_phy_find(struct macb_device *macb)
  348. {
  349. int i;
  350. u16 phy_id;
  351. /* Search for PHY... */
  352. for (i = 0; i < 32; i++) {
  353. macb->phy_addr = i;
  354. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  355. if (phy_id != 0xffff) {
  356. printf("%s: PHY present at %d\n", macb->netdev.name, i);
  357. return 1;
  358. }
  359. }
  360. /* PHY isn't up to snuff */
  361. printf("%s: PHY not found\n", macb->netdev.name);
  362. return 0;
  363. }
  364. #endif /* CONFIG_MACB_SEARCH_PHY */
  365. static int macb_phy_init(struct macb_device *macb, const char *name)
  366. {
  367. #ifdef CONFIG_PHYLIB
  368. struct phy_device *phydev;
  369. #endif
  370. u32 ncfgr;
  371. u16 phy_id, status, adv, lpa;
  372. int media, speed, duplex;
  373. int i;
  374. arch_get_mdio_control(name);
  375. #ifdef CONFIG_MACB_SEARCH_PHY
  376. /* Auto-detect phy_addr */
  377. if (!macb_phy_find(macb))
  378. return 0;
  379. #endif /* CONFIG_MACB_SEARCH_PHY */
  380. /* Check if the PHY is up to snuff... */
  381. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  382. if (phy_id == 0xffff) {
  383. printf("%s: No PHY present\n", name);
  384. return 0;
  385. }
  386. #ifdef CONFIG_PHYLIB
  387. /* need to consider other phy interface mode */
  388. phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
  389. PHY_INTERFACE_MODE_RGMII);
  390. if (!phydev) {
  391. printf("phy_connect failed\n");
  392. return -ENODEV;
  393. }
  394. phy_config(phydev);
  395. #endif
  396. status = macb_mdio_read(macb, MII_BMSR);
  397. if (!(status & BMSR_LSTATUS)) {
  398. /* Try to re-negotiate if we don't have link already. */
  399. macb_phy_reset(macb, name);
  400. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  401. status = macb_mdio_read(macb, MII_BMSR);
  402. if (status & BMSR_LSTATUS)
  403. break;
  404. udelay(100);
  405. }
  406. }
  407. if (!(status & BMSR_LSTATUS)) {
  408. printf("%s: link down (status: 0x%04x)\n",
  409. name, status);
  410. return 0;
  411. }
  412. /* First check for GMAC and that it is GiB capable */
  413. if (gem_is_gigabit_capable(macb)) {
  414. lpa = macb_mdio_read(macb, MII_STAT1000);
  415. if (lpa & (LPA_1000FULL | LPA_1000HALF)) {
  416. duplex = ((lpa & LPA_1000FULL) ? 1 : 0);
  417. printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
  418. name,
  419. duplex ? "full" : "half",
  420. lpa);
  421. ncfgr = macb_readl(macb, NCFGR);
  422. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  423. ncfgr |= GEM_BIT(GBE);
  424. if (duplex)
  425. ncfgr |= MACB_BIT(FD);
  426. macb_writel(macb, NCFGR, ncfgr);
  427. return 1;
  428. }
  429. }
  430. /* fall back for EMAC checking */
  431. adv = macb_mdio_read(macb, MII_ADVERTISE);
  432. lpa = macb_mdio_read(macb, MII_LPA);
  433. media = mii_nway_result(lpa & adv);
  434. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  435. ? 1 : 0);
  436. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  437. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  438. name,
  439. speed ? "100" : "10",
  440. duplex ? "full" : "half",
  441. lpa);
  442. ncfgr = macb_readl(macb, NCFGR);
  443. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
  444. if (speed)
  445. ncfgr |= MACB_BIT(SPD);
  446. if (duplex)
  447. ncfgr |= MACB_BIT(FD);
  448. macb_writel(macb, NCFGR, ncfgr);
  449. return 1;
  450. }
  451. static int gmac_init_multi_queues(struct macb_device *macb)
  452. {
  453. int i, num_queues = 1;
  454. u32 queue_mask;
  455. /* bit 0 is never set but queue 0 always exists */
  456. queue_mask = gem_readl(macb, DCFG6) & 0xff;
  457. queue_mask |= 0x1;
  458. for (i = 1; i < MACB_MAX_QUEUES; i++)
  459. if (queue_mask & (1 << i))
  460. num_queues++;
  461. macb->dummy_desc->ctrl = TXBUF_USED;
  462. macb->dummy_desc->addr = 0;
  463. flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
  464. MACB_TX_DUMMY_DMA_DESC_SIZE);
  465. for (i = 1; i < num_queues; i++)
  466. gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
  467. return 0;
  468. }
  469. static int _macb_init(struct macb_device *macb, const char *name)
  470. {
  471. unsigned long paddr;
  472. int i;
  473. /*
  474. * macb_halt should have been called at some point before now,
  475. * so we'll assume the controller is idle.
  476. */
  477. /* initialize DMA descriptors */
  478. paddr = macb->rx_buffer_dma;
  479. for (i = 0; i < MACB_RX_RING_SIZE; i++) {
  480. if (i == (MACB_RX_RING_SIZE - 1))
  481. paddr |= RXADDR_WRAP;
  482. macb->rx_ring[i].addr = paddr;
  483. macb->rx_ring[i].ctrl = 0;
  484. paddr += 128;
  485. }
  486. macb_flush_ring_desc(macb, RX);
  487. macb_flush_rx_buffer(macb);
  488. for (i = 0; i < MACB_TX_RING_SIZE; i++) {
  489. macb->tx_ring[i].addr = 0;
  490. if (i == (MACB_TX_RING_SIZE - 1))
  491. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  492. else
  493. macb->tx_ring[i].ctrl = TXBUF_USED;
  494. }
  495. macb_flush_ring_desc(macb, TX);
  496. macb->rx_tail = 0;
  497. macb->tx_head = 0;
  498. macb->tx_tail = 0;
  499. macb->next_rx_tail = 0;
  500. macb_writel(macb, RBQP, macb->rx_ring_dma);
  501. macb_writel(macb, TBQP, macb->tx_ring_dma);
  502. if (macb_is_gem(macb)) {
  503. /* Check the multi queue and initialize the queue for tx */
  504. gmac_init_multi_queues(macb);
  505. /*
  506. * When the GMAC IP with GE feature, this bit is used to
  507. * select interface between RGMII and GMII.
  508. * When the GMAC IP without GE feature, this bit is used
  509. * to select interface between RMII and MII.
  510. */
  511. #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
  512. gem_writel(macb, UR, GEM_BIT(RGMII));
  513. #else
  514. gem_writel(macb, UR, 0);
  515. #endif
  516. } else {
  517. /* choose RMII or MII mode. This depends on the board */
  518. #ifdef CONFIG_RMII
  519. #ifdef CONFIG_AT91FAMILY
  520. macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
  521. #else
  522. macb_writel(macb, USRIO, 0);
  523. #endif
  524. #else
  525. #ifdef CONFIG_AT91FAMILY
  526. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  527. #else
  528. macb_writel(macb, USRIO, MACB_BIT(MII));
  529. #endif
  530. #endif /* CONFIG_RMII */
  531. }
  532. if (!macb_phy_init(macb, name))
  533. return -1;
  534. /* Enable TX and RX */
  535. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  536. return 0;
  537. }
  538. static void _macb_halt(struct macb_device *macb)
  539. {
  540. u32 ncr, tsr;
  541. /* Halt the controller and wait for any ongoing transmission to end. */
  542. ncr = macb_readl(macb, NCR);
  543. ncr |= MACB_BIT(THALT);
  544. macb_writel(macb, NCR, ncr);
  545. do {
  546. tsr = macb_readl(macb, TSR);
  547. } while (tsr & MACB_BIT(TGO));
  548. /* Disable TX and RX, and clear statistics */
  549. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  550. }
  551. static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
  552. {
  553. u32 hwaddr_bottom;
  554. u16 hwaddr_top;
  555. /* set hardware address */
  556. hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
  557. enetaddr[2] << 16 | enetaddr[3] << 24;
  558. macb_writel(macb, SA1B, hwaddr_bottom);
  559. hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
  560. macb_writel(macb, SA1T, hwaddr_top);
  561. return 0;
  562. }
  563. static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
  564. {
  565. u32 config;
  566. unsigned long macb_hz = get_macb_pclk_rate(id);
  567. if (macb_hz < 20000000)
  568. config = MACB_BF(CLK, MACB_CLK_DIV8);
  569. else if (macb_hz < 40000000)
  570. config = MACB_BF(CLK, MACB_CLK_DIV16);
  571. else if (macb_hz < 80000000)
  572. config = MACB_BF(CLK, MACB_CLK_DIV32);
  573. else
  574. config = MACB_BF(CLK, MACB_CLK_DIV64);
  575. return config;
  576. }
  577. static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
  578. {
  579. u32 config;
  580. unsigned long macb_hz = get_macb_pclk_rate(id);
  581. if (macb_hz < 20000000)
  582. config = GEM_BF(CLK, GEM_CLK_DIV8);
  583. else if (macb_hz < 40000000)
  584. config = GEM_BF(CLK, GEM_CLK_DIV16);
  585. else if (macb_hz < 80000000)
  586. config = GEM_BF(CLK, GEM_CLK_DIV32);
  587. else if (macb_hz < 120000000)
  588. config = GEM_BF(CLK, GEM_CLK_DIV48);
  589. else if (macb_hz < 160000000)
  590. config = GEM_BF(CLK, GEM_CLK_DIV64);
  591. else
  592. config = GEM_BF(CLK, GEM_CLK_DIV96);
  593. return config;
  594. }
  595. /*
  596. * Get the DMA bus width field of the network configuration register that we
  597. * should program. We find the width from decoding the design configuration
  598. * register to find the maximum supported data bus width.
  599. */
  600. static u32 macb_dbw(struct macb_device *macb)
  601. {
  602. switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
  603. case 4:
  604. return GEM_BF(DBW, GEM_DBW128);
  605. case 2:
  606. return GEM_BF(DBW, GEM_DBW64);
  607. case 1:
  608. default:
  609. return GEM_BF(DBW, GEM_DBW32);
  610. }
  611. }
  612. static void _macb_eth_initialize(struct macb_device *macb)
  613. {
  614. int id = 0; /* This is not used by functions we call */
  615. u32 ncfgr;
  616. /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
  617. macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
  618. &macb->rx_buffer_dma);
  619. macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
  620. &macb->rx_ring_dma);
  621. macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
  622. &macb->tx_ring_dma);
  623. macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
  624. &macb->dummy_desc_dma);
  625. /*
  626. * Do some basic initialization so that we at least can talk
  627. * to the PHY
  628. */
  629. if (macb_is_gem(macb)) {
  630. ncfgr = gem_mdc_clk_div(id, macb);
  631. ncfgr |= macb_dbw(macb);
  632. } else {
  633. ncfgr = macb_mdc_clk_div(id, macb);
  634. }
  635. macb_writel(macb, NCFGR, ncfgr);
  636. }
  637. static int macb_send(struct eth_device *netdev, void *packet, int length)
  638. {
  639. struct macb_device *macb = to_macb(netdev);
  640. return _macb_send(macb, netdev->name, packet, length);
  641. }
  642. static int macb_recv(struct eth_device *netdev)
  643. {
  644. struct macb_device *macb = to_macb(netdev);
  645. uchar *packet;
  646. int length;
  647. macb->wrapped = false;
  648. for (;;) {
  649. macb->next_rx_tail = macb->rx_tail;
  650. length = _macb_recv(macb, &packet);
  651. if (length >= 0) {
  652. net_process_received_packet(packet, length);
  653. reclaim_rx_buffers(macb, macb->next_rx_tail);
  654. } else if (length < 0) {
  655. return length;
  656. }
  657. }
  658. }
  659. static int macb_init(struct eth_device *netdev, bd_t *bd)
  660. {
  661. struct macb_device *macb = to_macb(netdev);
  662. return _macb_init(macb, netdev->name);
  663. }
  664. static void macb_halt(struct eth_device *netdev)
  665. {
  666. struct macb_device *macb = to_macb(netdev);
  667. return _macb_halt(macb);
  668. }
  669. static int macb_write_hwaddr(struct eth_device *netdev)
  670. {
  671. struct macb_device *macb = to_macb(netdev);
  672. return _macb_write_hwaddr(macb, netdev->enetaddr);
  673. }
  674. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  675. {
  676. struct macb_device *macb;
  677. struct eth_device *netdev;
  678. macb = malloc(sizeof(struct macb_device));
  679. if (!macb) {
  680. printf("Error: Failed to allocate memory for MACB%d\n", id);
  681. return -1;
  682. }
  683. memset(macb, 0, sizeof(struct macb_device));
  684. netdev = &macb->netdev;
  685. macb->regs = regs;
  686. macb->phy_addr = phy_addr;
  687. if (macb_is_gem(macb))
  688. sprintf(netdev->name, "gmac%d", id);
  689. else
  690. sprintf(netdev->name, "macb%d", id);
  691. netdev->init = macb_init;
  692. netdev->halt = macb_halt;
  693. netdev->send = macb_send;
  694. netdev->recv = macb_recv;
  695. netdev->write_hwaddr = macb_write_hwaddr;
  696. _macb_eth_initialize(macb);
  697. eth_register(netdev);
  698. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  699. miiphy_register(netdev->name, macb_miiphy_read, macb_miiphy_write);
  700. macb->bus = miiphy_get_dev_by_name(netdev->name);
  701. #endif
  702. return 0;
  703. }
  704. #endif