macb.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * Copyright (C) 2005-2006 Atmel Corporation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. /*
  9. * The u-boot networking stack is a little weird. It seems like the
  10. * networking core allocates receive buffers up front without any
  11. * regard to the hardware that's supposed to actually receive those
  12. * packets.
  13. *
  14. * The MACB receives packets into 128-byte receive buffers, so the
  15. * buffers allocated by the core isn't very practical to use. We'll
  16. * allocate our own, but we need one such buffer in case a packet
  17. * wraps around the DMA ring so that we have to copy it.
  18. *
  19. * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
  20. * configuration header. This way, the core allocates one RX buffer
  21. * and one TX buffer, each of which can hold a ethernet packet of
  22. * maximum size.
  23. *
  24. * For some reason, the networking core unconditionally specifies a
  25. * 32-byte packet "alignment" (which really should be called
  26. * "padding"). MACB shouldn't need that, but we'll refrain from any
  27. * core modifications here...
  28. */
  29. #include <net.h>
  30. #ifndef CONFIG_DM_ETH
  31. #include <netdev.h>
  32. #endif
  33. #include <malloc.h>
  34. #include <miiphy.h>
  35. #include <linux/mii.h>
  36. #include <asm/io.h>
  37. #include <asm/dma-mapping.h>
  38. #include <asm/arch/clk.h>
  39. #include <linux/errno.h>
  40. #include "macb.h"
  41. DECLARE_GLOBAL_DATA_PTR;
  42. #define MACB_RX_BUFFER_SIZE 4096
  43. #define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128)
  44. #define MACB_TX_RING_SIZE 16
  45. #define MACB_TX_TIMEOUT 1000
  46. #define MACB_AUTONEG_TIMEOUT 5000000
  47. struct macb_dma_desc {
  48. u32 addr;
  49. u32 ctrl;
  50. };
  51. #define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
  52. #define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
  53. #define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
  54. #define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
  55. #define RXADDR_USED 0x00000001
  56. #define RXADDR_WRAP 0x00000002
  57. #define RXBUF_FRMLEN_MASK 0x00000fff
  58. #define RXBUF_FRAME_START 0x00004000
  59. #define RXBUF_FRAME_END 0x00008000
  60. #define RXBUF_TYPEID_MATCH 0x00400000
  61. #define RXBUF_ADDR4_MATCH 0x00800000
  62. #define RXBUF_ADDR3_MATCH 0x01000000
  63. #define RXBUF_ADDR2_MATCH 0x02000000
  64. #define RXBUF_ADDR1_MATCH 0x04000000
  65. #define RXBUF_BROADCAST 0x80000000
  66. #define TXBUF_FRMLEN_MASK 0x000007ff
  67. #define TXBUF_FRAME_END 0x00008000
  68. #define TXBUF_NOCRC 0x00010000
  69. #define TXBUF_EXHAUSTED 0x08000000
  70. #define TXBUF_UNDERRUN 0x10000000
  71. #define TXBUF_MAXRETRY 0x20000000
  72. #define TXBUF_WRAP 0x40000000
  73. #define TXBUF_USED 0x80000000
  74. struct macb_device {
  75. void *regs;
  76. unsigned int rx_tail;
  77. unsigned int tx_head;
  78. unsigned int tx_tail;
  79. unsigned int next_rx_tail;
  80. bool wrapped;
  81. void *rx_buffer;
  82. void *tx_buffer;
  83. struct macb_dma_desc *rx_ring;
  84. struct macb_dma_desc *tx_ring;
  85. unsigned long rx_buffer_dma;
  86. unsigned long rx_ring_dma;
  87. unsigned long tx_ring_dma;
  88. struct macb_dma_desc *dummy_desc;
  89. unsigned long dummy_desc_dma;
  90. const struct device *dev;
  91. #ifndef CONFIG_DM_ETH
  92. struct eth_device netdev;
  93. #endif
  94. unsigned short phy_addr;
  95. struct mii_dev *bus;
  96. #ifdef CONFIG_DM_ETH
  97. phy_interface_t phy_interface;
  98. #endif
  99. };
  100. #ifndef CONFIG_DM_ETH
  101. #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
  102. #endif
  103. static int macb_is_gem(struct macb_device *macb)
  104. {
  105. return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
  106. }
  107. #ifndef cpu_is_sama5d2
  108. #define cpu_is_sama5d2() 0
  109. #endif
  110. #ifndef cpu_is_sama5d4
  111. #define cpu_is_sama5d4() 0
  112. #endif
  113. static int gem_is_gigabit_capable(struct macb_device *macb)
  114. {
  115. /*
  116. * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
  117. * configured to support only 10/100.
  118. */
  119. return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
  120. }
  121. static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
  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, 1)
  131. | MACB_BF(PHYA, macb->phy_addr)
  132. | MACB_BF(REGA, reg)
  133. | MACB_BF(CODE, 2)
  134. | MACB_BF(DATA, value));
  135. macb_writel(macb, MAN, frame);
  136. do {
  137. netstat = macb_readl(macb, NSR);
  138. } while (!(netstat & MACB_BIT(IDLE)));
  139. netctl = macb_readl(macb, NCR);
  140. netctl &= ~MACB_BIT(MPE);
  141. macb_writel(macb, NCR, netctl);
  142. }
  143. static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
  144. {
  145. unsigned long netctl;
  146. unsigned long netstat;
  147. unsigned long frame;
  148. netctl = macb_readl(macb, NCR);
  149. netctl |= MACB_BIT(MPE);
  150. macb_writel(macb, NCR, netctl);
  151. frame = (MACB_BF(SOF, 1)
  152. | MACB_BF(RW, 2)
  153. | MACB_BF(PHYA, macb->phy_addr)
  154. | MACB_BF(REGA, reg)
  155. | MACB_BF(CODE, 2));
  156. macb_writel(macb, MAN, frame);
  157. do {
  158. netstat = macb_readl(macb, NSR);
  159. } while (!(netstat & MACB_BIT(IDLE)));
  160. frame = macb_readl(macb, MAN);
  161. netctl = macb_readl(macb, NCR);
  162. netctl &= ~MACB_BIT(MPE);
  163. macb_writel(macb, NCR, netctl);
  164. return MACB_BFEXT(DATA, frame);
  165. }
  166. void __weak arch_get_mdio_control(const char *name)
  167. {
  168. return;
  169. }
  170. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  171. int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
  172. {
  173. u16 value = 0;
  174. #ifdef CONFIG_DM_ETH
  175. struct udevice *dev = eth_get_dev_by_name(bus->name);
  176. struct macb_device *macb = dev_get_priv(dev);
  177. #else
  178. struct eth_device *dev = eth_get_dev_by_name(bus->name);
  179. struct macb_device *macb = to_macb(dev);
  180. #endif
  181. if (macb->phy_addr != phy_adr)
  182. return -1;
  183. arch_get_mdio_control(bus->name);
  184. value = macb_mdio_read(macb, reg);
  185. return value;
  186. }
  187. int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
  188. u16 value)
  189. {
  190. #ifdef CONFIG_DM_ETH
  191. struct udevice *dev = eth_get_dev_by_name(bus->name);
  192. struct macb_device *macb = dev_get_priv(dev);
  193. #else
  194. struct eth_device *dev = eth_get_dev_by_name(bus->name);
  195. struct macb_device *macb = to_macb(dev);
  196. #endif
  197. if (macb->phy_addr != phy_adr)
  198. return -1;
  199. arch_get_mdio_control(bus->name);
  200. macb_mdio_write(macb, reg, value);
  201. return 0;
  202. }
  203. #endif
  204. #define RX 1
  205. #define TX 0
  206. static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
  207. {
  208. if (rx)
  209. invalidate_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  210. MACB_RX_DMA_DESC_SIZE);
  211. else
  212. invalidate_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  213. MACB_TX_DMA_DESC_SIZE);
  214. }
  215. static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
  216. {
  217. if (rx)
  218. flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  219. MACB_RX_DMA_DESC_SIZE);
  220. else
  221. flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  222. MACB_TX_DMA_DESC_SIZE);
  223. }
  224. static inline void macb_flush_rx_buffer(struct macb_device *macb)
  225. {
  226. flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  227. MACB_RX_BUFFER_SIZE);
  228. }
  229. static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
  230. {
  231. invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  232. MACB_RX_BUFFER_SIZE);
  233. }
  234. #if defined(CONFIG_CMD_NET)
  235. static int _macb_send(struct macb_device *macb, const char *name, void *packet,
  236. int length)
  237. {
  238. unsigned long paddr, ctrl;
  239. unsigned int tx_head = macb->tx_head;
  240. int i;
  241. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  242. ctrl = length & TXBUF_FRMLEN_MASK;
  243. ctrl |= TXBUF_FRAME_END;
  244. if (tx_head == (MACB_TX_RING_SIZE - 1)) {
  245. ctrl |= TXBUF_WRAP;
  246. macb->tx_head = 0;
  247. } else {
  248. macb->tx_head++;
  249. }
  250. macb->tx_ring[tx_head].ctrl = ctrl;
  251. macb->tx_ring[tx_head].addr = paddr;
  252. barrier();
  253. macb_flush_ring_desc(macb, TX);
  254. /* Do we need check paddr and length is dcache line aligned? */
  255. flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN));
  256. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  257. /*
  258. * I guess this is necessary because the networking core may
  259. * re-use the transmit buffer as soon as we return...
  260. */
  261. for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
  262. barrier();
  263. macb_invalidate_ring_desc(macb, TX);
  264. ctrl = macb->tx_ring[tx_head].ctrl;
  265. if (ctrl & TXBUF_USED)
  266. break;
  267. udelay(1);
  268. }
  269. dma_unmap_single(packet, length, paddr);
  270. if (i <= MACB_TX_TIMEOUT) {
  271. if (ctrl & TXBUF_UNDERRUN)
  272. printf("%s: TX underrun\n", name);
  273. if (ctrl & TXBUF_EXHAUSTED)
  274. printf("%s: TX buffers exhausted in mid frame\n", name);
  275. } else {
  276. printf("%s: TX timeout\n", name);
  277. }
  278. /* No one cares anyway */
  279. return 0;
  280. }
  281. static void reclaim_rx_buffers(struct macb_device *macb,
  282. unsigned int new_tail)
  283. {
  284. unsigned int i;
  285. i = macb->rx_tail;
  286. macb_invalidate_ring_desc(macb, RX);
  287. while (i > new_tail) {
  288. macb->rx_ring[i].addr &= ~RXADDR_USED;
  289. i++;
  290. if (i > MACB_RX_RING_SIZE)
  291. i = 0;
  292. }
  293. while (i < new_tail) {
  294. macb->rx_ring[i].addr &= ~RXADDR_USED;
  295. i++;
  296. }
  297. barrier();
  298. macb_flush_ring_desc(macb, RX);
  299. macb->rx_tail = new_tail;
  300. }
  301. static int _macb_recv(struct macb_device *macb, uchar **packetp)
  302. {
  303. unsigned int next_rx_tail = macb->next_rx_tail;
  304. void *buffer;
  305. int length;
  306. u32 status;
  307. macb->wrapped = false;
  308. for (;;) {
  309. macb_invalidate_ring_desc(macb, RX);
  310. if (!(macb->rx_ring[next_rx_tail].addr & RXADDR_USED))
  311. return -EAGAIN;
  312. status = macb->rx_ring[next_rx_tail].ctrl;
  313. if (status & RXBUF_FRAME_START) {
  314. if (next_rx_tail != macb->rx_tail)
  315. reclaim_rx_buffers(macb, next_rx_tail);
  316. macb->wrapped = false;
  317. }
  318. if (status & RXBUF_FRAME_END) {
  319. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  320. length = status & RXBUF_FRMLEN_MASK;
  321. macb_invalidate_rx_buffer(macb);
  322. if (macb->wrapped) {
  323. unsigned int headlen, taillen;
  324. headlen = 128 * (MACB_RX_RING_SIZE
  325. - macb->rx_tail);
  326. taillen = length - headlen;
  327. memcpy((void *)net_rx_packets[0],
  328. buffer, headlen);
  329. memcpy((void *)net_rx_packets[0] + headlen,
  330. macb->rx_buffer, taillen);
  331. *packetp = (void *)net_rx_packets[0];
  332. } else {
  333. *packetp = buffer;
  334. }
  335. if (++next_rx_tail >= MACB_RX_RING_SIZE)
  336. next_rx_tail = 0;
  337. macb->next_rx_tail = next_rx_tail;
  338. return length;
  339. } else {
  340. if (++next_rx_tail >= MACB_RX_RING_SIZE) {
  341. macb->wrapped = true;
  342. next_rx_tail = 0;
  343. }
  344. }
  345. barrier();
  346. }
  347. }
  348. static void macb_phy_reset(struct macb_device *macb, const char *name)
  349. {
  350. int i;
  351. u16 status, adv;
  352. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  353. macb_mdio_write(macb, MII_ADVERTISE, adv);
  354. printf("%s: Starting autonegotiation...\n", name);
  355. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  356. | BMCR_ANRESTART));
  357. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  358. status = macb_mdio_read(macb, MII_BMSR);
  359. if (status & BMSR_ANEGCOMPLETE)
  360. break;
  361. udelay(100);
  362. }
  363. if (status & BMSR_ANEGCOMPLETE)
  364. printf("%s: Autonegotiation complete\n", name);
  365. else
  366. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  367. name, status);
  368. }
  369. #ifdef CONFIG_MACB_SEARCH_PHY
  370. static int macb_phy_find(struct macb_device *macb, const char *name)
  371. {
  372. int i;
  373. u16 phy_id;
  374. /* Search for PHY... */
  375. for (i = 0; i < 32; i++) {
  376. macb->phy_addr = i;
  377. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  378. if (phy_id != 0xffff) {
  379. printf("%s: PHY present at %d\n", name, i);
  380. return 1;
  381. }
  382. }
  383. /* PHY isn't up to snuff */
  384. printf("%s: PHY not found\n", name);
  385. return 0;
  386. }
  387. #endif /* CONFIG_MACB_SEARCH_PHY */
  388. #ifdef CONFIG_DM_ETH
  389. static int macb_phy_init(struct udevice *dev, const char *name)
  390. #else
  391. static int macb_phy_init(struct macb_device *macb, const char *name)
  392. #endif
  393. {
  394. #ifdef CONFIG_DM_ETH
  395. struct macb_device *macb = dev_get_priv(dev);
  396. #endif
  397. #ifdef CONFIG_PHYLIB
  398. struct phy_device *phydev;
  399. #endif
  400. u32 ncfgr;
  401. u16 phy_id, status, adv, lpa;
  402. int media, speed, duplex;
  403. int i;
  404. arch_get_mdio_control(name);
  405. #ifdef CONFIG_MACB_SEARCH_PHY
  406. /* Auto-detect phy_addr */
  407. if (!macb_phy_find(macb, name))
  408. return 0;
  409. #endif /* CONFIG_MACB_SEARCH_PHY */
  410. /* Check if the PHY is up to snuff... */
  411. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  412. if (phy_id == 0xffff) {
  413. printf("%s: No PHY present\n", name);
  414. return 0;
  415. }
  416. #ifdef CONFIG_PHYLIB
  417. #ifdef CONFIG_DM_ETH
  418. phydev = phy_connect(macb->bus, macb->phy_addr, dev,
  419. macb->phy_interface);
  420. #else
  421. /* need to consider other phy interface mode */
  422. phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
  423. PHY_INTERFACE_MODE_RGMII);
  424. #endif
  425. if (!phydev) {
  426. printf("phy_connect failed\n");
  427. return -ENODEV;
  428. }
  429. phy_config(phydev);
  430. #endif
  431. status = macb_mdio_read(macb, MII_BMSR);
  432. if (!(status & BMSR_LSTATUS)) {
  433. /* Try to re-negotiate if we don't have link already. */
  434. macb_phy_reset(macb, name);
  435. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  436. status = macb_mdio_read(macb, MII_BMSR);
  437. if (status & BMSR_LSTATUS)
  438. break;
  439. udelay(100);
  440. }
  441. }
  442. if (!(status & BMSR_LSTATUS)) {
  443. printf("%s: link down (status: 0x%04x)\n",
  444. name, status);
  445. return 0;
  446. }
  447. /* First check for GMAC and that it is GiB capable */
  448. if (gem_is_gigabit_capable(macb)) {
  449. lpa = macb_mdio_read(macb, MII_STAT1000);
  450. if (lpa & (LPA_1000FULL | LPA_1000HALF)) {
  451. duplex = ((lpa & LPA_1000FULL) ? 1 : 0);
  452. printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
  453. name,
  454. duplex ? "full" : "half",
  455. lpa);
  456. ncfgr = macb_readl(macb, NCFGR);
  457. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  458. ncfgr |= GEM_BIT(GBE);
  459. if (duplex)
  460. ncfgr |= MACB_BIT(FD);
  461. macb_writel(macb, NCFGR, ncfgr);
  462. return 1;
  463. }
  464. }
  465. /* fall back for EMAC checking */
  466. adv = macb_mdio_read(macb, MII_ADVERTISE);
  467. lpa = macb_mdio_read(macb, MII_LPA);
  468. media = mii_nway_result(lpa & adv);
  469. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  470. ? 1 : 0);
  471. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  472. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  473. name,
  474. speed ? "100" : "10",
  475. duplex ? "full" : "half",
  476. lpa);
  477. ncfgr = macb_readl(macb, NCFGR);
  478. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
  479. if (speed)
  480. ncfgr |= MACB_BIT(SPD);
  481. if (duplex)
  482. ncfgr |= MACB_BIT(FD);
  483. macb_writel(macb, NCFGR, ncfgr);
  484. return 1;
  485. }
  486. static int gmac_init_multi_queues(struct macb_device *macb)
  487. {
  488. int i, num_queues = 1;
  489. u32 queue_mask;
  490. /* bit 0 is never set but queue 0 always exists */
  491. queue_mask = gem_readl(macb, DCFG6) & 0xff;
  492. queue_mask |= 0x1;
  493. for (i = 1; i < MACB_MAX_QUEUES; i++)
  494. if (queue_mask & (1 << i))
  495. num_queues++;
  496. macb->dummy_desc->ctrl = TXBUF_USED;
  497. macb->dummy_desc->addr = 0;
  498. flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
  499. MACB_TX_DUMMY_DMA_DESC_SIZE);
  500. for (i = 1; i < num_queues; i++)
  501. gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
  502. return 0;
  503. }
  504. #ifdef CONFIG_DM_ETH
  505. static int _macb_init(struct udevice *dev, const char *name)
  506. #else
  507. static int _macb_init(struct macb_device *macb, const char *name)
  508. #endif
  509. {
  510. #ifdef CONFIG_DM_ETH
  511. struct macb_device *macb = dev_get_priv(dev);
  512. #endif
  513. unsigned long paddr;
  514. int i;
  515. /*
  516. * macb_halt should have been called at some point before now,
  517. * so we'll assume the controller is idle.
  518. */
  519. /* initialize DMA descriptors */
  520. paddr = macb->rx_buffer_dma;
  521. for (i = 0; i < MACB_RX_RING_SIZE; i++) {
  522. if (i == (MACB_RX_RING_SIZE - 1))
  523. paddr |= RXADDR_WRAP;
  524. macb->rx_ring[i].addr = paddr;
  525. macb->rx_ring[i].ctrl = 0;
  526. paddr += 128;
  527. }
  528. macb_flush_ring_desc(macb, RX);
  529. macb_flush_rx_buffer(macb);
  530. for (i = 0; i < MACB_TX_RING_SIZE; i++) {
  531. macb->tx_ring[i].addr = 0;
  532. if (i == (MACB_TX_RING_SIZE - 1))
  533. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  534. else
  535. macb->tx_ring[i].ctrl = TXBUF_USED;
  536. }
  537. macb_flush_ring_desc(macb, TX);
  538. macb->rx_tail = 0;
  539. macb->tx_head = 0;
  540. macb->tx_tail = 0;
  541. macb->next_rx_tail = 0;
  542. macb_writel(macb, RBQP, macb->rx_ring_dma);
  543. macb_writel(macb, TBQP, macb->tx_ring_dma);
  544. if (macb_is_gem(macb)) {
  545. /* Check the multi queue and initialize the queue for tx */
  546. gmac_init_multi_queues(macb);
  547. /*
  548. * When the GMAC IP with GE feature, this bit is used to
  549. * select interface between RGMII and GMII.
  550. * When the GMAC IP without GE feature, this bit is used
  551. * to select interface between RMII and MII.
  552. */
  553. #ifdef CONFIG_DM_ETH
  554. if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
  555. gem_writel(macb, UR, GEM_BIT(RGMII));
  556. else
  557. gem_writel(macb, UR, 0);
  558. #else
  559. #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
  560. gem_writel(macb, UR, GEM_BIT(RGMII));
  561. #else
  562. gem_writel(macb, UR, 0);
  563. #endif
  564. #endif
  565. } else {
  566. /* choose RMII or MII mode. This depends on the board */
  567. #ifdef CONFIG_DM_ETH
  568. #ifdef CONFIG_AT91FAMILY
  569. if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
  570. macb_writel(macb, USRIO,
  571. MACB_BIT(RMII) | MACB_BIT(CLKEN));
  572. } else {
  573. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  574. }
  575. #else
  576. if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
  577. macb_writel(macb, USRIO, 0);
  578. else
  579. macb_writel(macb, USRIO, MACB_BIT(MII));
  580. #endif
  581. #else
  582. #ifdef CONFIG_RMII
  583. #ifdef CONFIG_AT91FAMILY
  584. macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
  585. #else
  586. macb_writel(macb, USRIO, 0);
  587. #endif
  588. #else
  589. #ifdef CONFIG_AT91FAMILY
  590. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  591. #else
  592. macb_writel(macb, USRIO, MACB_BIT(MII));
  593. #endif
  594. #endif /* CONFIG_RMII */
  595. #endif
  596. }
  597. #ifdef CONFIG_DM_ETH
  598. if (!macb_phy_init(dev, name))
  599. #else
  600. if (!macb_phy_init(macb, name))
  601. #endif
  602. return -1;
  603. /* Enable TX and RX */
  604. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  605. return 0;
  606. }
  607. static void _macb_halt(struct macb_device *macb)
  608. {
  609. u32 ncr, tsr;
  610. /* Halt the controller and wait for any ongoing transmission to end. */
  611. ncr = macb_readl(macb, NCR);
  612. ncr |= MACB_BIT(THALT);
  613. macb_writel(macb, NCR, ncr);
  614. do {
  615. tsr = macb_readl(macb, TSR);
  616. } while (tsr & MACB_BIT(TGO));
  617. /* Disable TX and RX, and clear statistics */
  618. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  619. }
  620. static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
  621. {
  622. u32 hwaddr_bottom;
  623. u16 hwaddr_top;
  624. /* set hardware address */
  625. hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
  626. enetaddr[2] << 16 | enetaddr[3] << 24;
  627. macb_writel(macb, SA1B, hwaddr_bottom);
  628. hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
  629. macb_writel(macb, SA1T, hwaddr_top);
  630. return 0;
  631. }
  632. static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
  633. {
  634. u32 config;
  635. unsigned long macb_hz = get_macb_pclk_rate(id);
  636. if (macb_hz < 20000000)
  637. config = MACB_BF(CLK, MACB_CLK_DIV8);
  638. else if (macb_hz < 40000000)
  639. config = MACB_BF(CLK, MACB_CLK_DIV16);
  640. else if (macb_hz < 80000000)
  641. config = MACB_BF(CLK, MACB_CLK_DIV32);
  642. else
  643. config = MACB_BF(CLK, MACB_CLK_DIV64);
  644. return config;
  645. }
  646. static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
  647. {
  648. u32 config;
  649. unsigned long macb_hz = get_macb_pclk_rate(id);
  650. if (macb_hz < 20000000)
  651. config = GEM_BF(CLK, GEM_CLK_DIV8);
  652. else if (macb_hz < 40000000)
  653. config = GEM_BF(CLK, GEM_CLK_DIV16);
  654. else if (macb_hz < 80000000)
  655. config = GEM_BF(CLK, GEM_CLK_DIV32);
  656. else if (macb_hz < 120000000)
  657. config = GEM_BF(CLK, GEM_CLK_DIV48);
  658. else if (macb_hz < 160000000)
  659. config = GEM_BF(CLK, GEM_CLK_DIV64);
  660. else
  661. config = GEM_BF(CLK, GEM_CLK_DIV96);
  662. return config;
  663. }
  664. /*
  665. * Get the DMA bus width field of the network configuration register that we
  666. * should program. We find the width from decoding the design configuration
  667. * register to find the maximum supported data bus width.
  668. */
  669. static u32 macb_dbw(struct macb_device *macb)
  670. {
  671. switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
  672. case 4:
  673. return GEM_BF(DBW, GEM_DBW128);
  674. case 2:
  675. return GEM_BF(DBW, GEM_DBW64);
  676. case 1:
  677. default:
  678. return GEM_BF(DBW, GEM_DBW32);
  679. }
  680. }
  681. static void _macb_eth_initialize(struct macb_device *macb)
  682. {
  683. int id = 0; /* This is not used by functions we call */
  684. u32 ncfgr;
  685. /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
  686. macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
  687. &macb->rx_buffer_dma);
  688. macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
  689. &macb->rx_ring_dma);
  690. macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
  691. &macb->tx_ring_dma);
  692. macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
  693. &macb->dummy_desc_dma);
  694. /*
  695. * Do some basic initialization so that we at least can talk
  696. * to the PHY
  697. */
  698. if (macb_is_gem(macb)) {
  699. ncfgr = gem_mdc_clk_div(id, macb);
  700. ncfgr |= macb_dbw(macb);
  701. } else {
  702. ncfgr = macb_mdc_clk_div(id, macb);
  703. }
  704. macb_writel(macb, NCFGR, ncfgr);
  705. }
  706. #ifndef CONFIG_DM_ETH
  707. static int macb_send(struct eth_device *netdev, void *packet, int length)
  708. {
  709. struct macb_device *macb = to_macb(netdev);
  710. return _macb_send(macb, netdev->name, packet, length);
  711. }
  712. static int macb_recv(struct eth_device *netdev)
  713. {
  714. struct macb_device *macb = to_macb(netdev);
  715. uchar *packet;
  716. int length;
  717. macb->wrapped = false;
  718. for (;;) {
  719. macb->next_rx_tail = macb->rx_tail;
  720. length = _macb_recv(macb, &packet);
  721. if (length >= 0) {
  722. net_process_received_packet(packet, length);
  723. reclaim_rx_buffers(macb, macb->next_rx_tail);
  724. } else if (length < 0) {
  725. return length;
  726. }
  727. }
  728. }
  729. static int macb_init(struct eth_device *netdev, bd_t *bd)
  730. {
  731. struct macb_device *macb = to_macb(netdev);
  732. return _macb_init(macb, netdev->name);
  733. }
  734. static void macb_halt(struct eth_device *netdev)
  735. {
  736. struct macb_device *macb = to_macb(netdev);
  737. return _macb_halt(macb);
  738. }
  739. static int macb_write_hwaddr(struct eth_device *netdev)
  740. {
  741. struct macb_device *macb = to_macb(netdev);
  742. return _macb_write_hwaddr(macb, netdev->enetaddr);
  743. }
  744. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  745. {
  746. struct macb_device *macb;
  747. struct eth_device *netdev;
  748. macb = malloc(sizeof(struct macb_device));
  749. if (!macb) {
  750. printf("Error: Failed to allocate memory for MACB%d\n", id);
  751. return -1;
  752. }
  753. memset(macb, 0, sizeof(struct macb_device));
  754. netdev = &macb->netdev;
  755. macb->regs = regs;
  756. macb->phy_addr = phy_addr;
  757. if (macb_is_gem(macb))
  758. sprintf(netdev->name, "gmac%d", id);
  759. else
  760. sprintf(netdev->name, "macb%d", id);
  761. netdev->init = macb_init;
  762. netdev->halt = macb_halt;
  763. netdev->send = macb_send;
  764. netdev->recv = macb_recv;
  765. netdev->write_hwaddr = macb_write_hwaddr;
  766. _macb_eth_initialize(macb);
  767. eth_register(netdev);
  768. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  769. int retval;
  770. struct mii_dev *mdiodev = mdio_alloc();
  771. if (!mdiodev)
  772. return -ENOMEM;
  773. strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
  774. mdiodev->read = macb_miiphy_read;
  775. mdiodev->write = macb_miiphy_write;
  776. retval = mdio_register(mdiodev);
  777. if (retval < 0)
  778. return retval;
  779. macb->bus = miiphy_get_dev_by_name(netdev->name);
  780. #endif
  781. return 0;
  782. }
  783. #endif /* !CONFIG_DM_ETH */
  784. #ifdef CONFIG_DM_ETH
  785. static int macb_start(struct udevice *dev)
  786. {
  787. return _macb_init(dev, dev->name);
  788. }
  789. static int macb_send(struct udevice *dev, void *packet, int length)
  790. {
  791. struct macb_device *macb = dev_get_priv(dev);
  792. return _macb_send(macb, dev->name, packet, length);
  793. }
  794. static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
  795. {
  796. struct macb_device *macb = dev_get_priv(dev);
  797. macb->next_rx_tail = macb->rx_tail;
  798. macb->wrapped = false;
  799. return _macb_recv(macb, packetp);
  800. }
  801. static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
  802. {
  803. struct macb_device *macb = dev_get_priv(dev);
  804. reclaim_rx_buffers(macb, macb->next_rx_tail);
  805. return 0;
  806. }
  807. static void macb_stop(struct udevice *dev)
  808. {
  809. struct macb_device *macb = dev_get_priv(dev);
  810. _macb_halt(macb);
  811. }
  812. static int macb_write_hwaddr(struct udevice *dev)
  813. {
  814. struct eth_pdata *plat = dev_get_platdata(dev);
  815. struct macb_device *macb = dev_get_priv(dev);
  816. return _macb_write_hwaddr(macb, plat->enetaddr);
  817. }
  818. static const struct eth_ops macb_eth_ops = {
  819. .start = macb_start,
  820. .send = macb_send,
  821. .recv = macb_recv,
  822. .stop = macb_stop,
  823. .free_pkt = macb_free_pkt,
  824. .write_hwaddr = macb_write_hwaddr,
  825. };
  826. static int macb_eth_probe(struct udevice *dev)
  827. {
  828. struct eth_pdata *pdata = dev_get_platdata(dev);
  829. struct macb_device *macb = dev_get_priv(dev);
  830. #ifdef CONFIG_DM_ETH
  831. const char *phy_mode;
  832. phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
  833. if (phy_mode)
  834. macb->phy_interface = phy_get_interface_by_name(phy_mode);
  835. if (macb->phy_interface == -1) {
  836. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  837. return -EINVAL;
  838. }
  839. #endif
  840. macb->regs = (void *)pdata->iobase;
  841. _macb_eth_initialize(macb);
  842. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  843. int retval;
  844. struct mii_dev *mdiodev = mdio_alloc();
  845. if (!mdiodev)
  846. return -ENOMEM;
  847. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  848. mdiodev->read = macb_miiphy_read;
  849. mdiodev->write = macb_miiphy_write;
  850. retval = mdio_register(mdiodev);
  851. if (retval < 0)
  852. return retval;
  853. macb->bus = miiphy_get_dev_by_name(dev->name);
  854. #endif
  855. return 0;
  856. }
  857. static int macb_eth_ofdata_to_platdata(struct udevice *dev)
  858. {
  859. struct eth_pdata *pdata = dev_get_platdata(dev);
  860. pdata->iobase = dev_get_addr(dev);
  861. return 0;
  862. }
  863. static const struct udevice_id macb_eth_ids[] = {
  864. { .compatible = "cdns,macb" },
  865. { }
  866. };
  867. U_BOOT_DRIVER(eth_macb) = {
  868. .name = "eth_macb",
  869. .id = UCLASS_ETH,
  870. .of_match = macb_eth_ids,
  871. .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
  872. .probe = macb_eth_probe,
  873. .ops = &macb_eth_ops,
  874. .priv_auto_alloc_size = sizeof(struct macb_device),
  875. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  876. };
  877. #endif
  878. #endif