macb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2005-2006 Atmel Corporation
  4. */
  5. #include <common.h>
  6. #include <clk.h>
  7. #include <cpu_func.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <asm/global_data.h>
  11. #include <linux/delay.h>
  12. /*
  13. * The u-boot networking stack is a little weird. It seems like the
  14. * networking core allocates receive buffers up front without any
  15. * regard to the hardware that's supposed to actually receive those
  16. * packets.
  17. *
  18. * The MACB receives packets into 128-byte receive buffers, so the
  19. * buffers allocated by the core isn't very practical to use. We'll
  20. * allocate our own, but we need one such buffer in case a packet
  21. * wraps around the DMA ring so that we have to copy it.
  22. *
  23. * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
  24. * configuration header. This way, the core allocates one RX buffer
  25. * and one TX buffer, each of which can hold a ethernet packet of
  26. * maximum size.
  27. *
  28. * For some reason, the networking core unconditionally specifies a
  29. * 32-byte packet "alignment" (which really should be called
  30. * "padding"). MACB shouldn't need that, but we'll refrain from any
  31. * core modifications here...
  32. */
  33. #include <net.h>
  34. #include <malloc.h>
  35. #include <miiphy.h>
  36. #include <linux/mii.h>
  37. #include <asm/io.h>
  38. #include <linux/dma-mapping.h>
  39. #include <asm/arch/clk.h>
  40. #include <linux/errno.h>
  41. #include "macb.h"
  42. DECLARE_GLOBAL_DATA_PTR;
  43. /*
  44. * These buffer sizes must be power of 2 and divisible
  45. * by RX_BUFFER_MULTIPLE
  46. */
  47. #define MACB_RX_BUFFER_SIZE 128
  48. #define GEM_RX_BUFFER_SIZE 2048
  49. #define RX_BUFFER_MULTIPLE 64
  50. #define MACB_RX_RING_SIZE 32
  51. #define MACB_TX_RING_SIZE 16
  52. #define MACB_TX_TIMEOUT 1000
  53. #define MACB_AUTONEG_TIMEOUT 5000000
  54. #ifdef CONFIG_MACB_ZYNQ
  55. /* INCR4 AHB bursts */
  56. #define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
  57. /* Use full configured addressable space (8 Kb) */
  58. #define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
  59. /* Use full configured addressable space (4 Kb) */
  60. #define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
  61. /* Set RXBUF with use of 128 byte */
  62. #define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
  63. #define MACB_ZYNQ_GEM_DMACR_INIT \
  64. (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
  65. MACB_ZYNQ_GEM_DMACR_RXSIZE | \
  66. MACB_ZYNQ_GEM_DMACR_TXSIZE | \
  67. MACB_ZYNQ_GEM_DMACR_RXBUF)
  68. #endif
  69. struct macb_dma_desc {
  70. u32 addr;
  71. u32 ctrl;
  72. };
  73. struct macb_dma_desc_64 {
  74. u32 addrh;
  75. u32 unused;
  76. };
  77. #define HW_DMA_CAP_32B 0
  78. #define HW_DMA_CAP_64B 1
  79. #define DMA_DESC_SIZE 16
  80. #define DMA_DESC_BYTES(n) ((n) * DMA_DESC_SIZE)
  81. #define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
  82. #define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
  83. #define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
  84. #define DESC_PER_CACHELINE_32 (ARCH_DMA_MINALIGN/sizeof(struct macb_dma_desc))
  85. #define DESC_PER_CACHELINE_64 (ARCH_DMA_MINALIGN/DMA_DESC_SIZE)
  86. #define RXBUF_FRMLEN_MASK 0x00000fff
  87. #define TXBUF_FRMLEN_MASK 0x000007ff
  88. struct macb_device {
  89. void *regs;
  90. bool is_big_endian;
  91. const struct macb_config *config;
  92. unsigned int rx_tail;
  93. unsigned int tx_head;
  94. unsigned int tx_tail;
  95. unsigned int next_rx_tail;
  96. bool wrapped;
  97. void *rx_buffer;
  98. void *tx_buffer;
  99. struct macb_dma_desc *rx_ring;
  100. struct macb_dma_desc *tx_ring;
  101. size_t rx_buffer_size;
  102. unsigned long rx_buffer_dma;
  103. unsigned long rx_ring_dma;
  104. unsigned long tx_ring_dma;
  105. struct macb_dma_desc *dummy_desc;
  106. unsigned long dummy_desc_dma;
  107. const struct device *dev;
  108. unsigned short phy_addr;
  109. struct mii_dev *bus;
  110. #ifdef CONFIG_PHYLIB
  111. struct phy_device *phydev;
  112. #endif
  113. #ifdef CONFIG_CLK
  114. unsigned long pclk_rate;
  115. #endif
  116. phy_interface_t phy_interface;
  117. };
  118. struct macb_usrio_cfg {
  119. unsigned int mii;
  120. unsigned int rmii;
  121. unsigned int rgmii;
  122. unsigned int clken;
  123. };
  124. struct macb_config {
  125. unsigned int dma_burst_length;
  126. unsigned int hw_dma_cap;
  127. unsigned int caps;
  128. int (*clk_init)(struct udevice *dev, ulong rate);
  129. const struct macb_usrio_cfg *usrio;
  130. };
  131. static int macb_is_gem(struct macb_device *macb)
  132. {
  133. return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
  134. }
  135. #ifndef cpu_is_sama5d2
  136. #define cpu_is_sama5d2() 0
  137. #endif
  138. #ifndef cpu_is_sama5d4
  139. #define cpu_is_sama5d4() 0
  140. #endif
  141. static int gem_is_gigabit_capable(struct macb_device *macb)
  142. {
  143. /*
  144. * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
  145. * configured to support only 10/100.
  146. */
  147. return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
  148. }
  149. static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
  150. u16 value)
  151. {
  152. unsigned long netctl;
  153. unsigned long netstat;
  154. unsigned long frame;
  155. netctl = macb_readl(macb, NCR);
  156. netctl |= MACB_BIT(MPE);
  157. macb_writel(macb, NCR, netctl);
  158. frame = (MACB_BF(SOF, 1)
  159. | MACB_BF(RW, 1)
  160. | MACB_BF(PHYA, phy_adr)
  161. | MACB_BF(REGA, reg)
  162. | MACB_BF(CODE, 2)
  163. | MACB_BF(DATA, value));
  164. macb_writel(macb, MAN, frame);
  165. do {
  166. netstat = macb_readl(macb, NSR);
  167. } while (!(netstat & MACB_BIT(IDLE)));
  168. netctl = macb_readl(macb, NCR);
  169. netctl &= ~MACB_BIT(MPE);
  170. macb_writel(macb, NCR, netctl);
  171. }
  172. static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
  173. {
  174. unsigned long netctl;
  175. unsigned long netstat;
  176. unsigned long frame;
  177. netctl = macb_readl(macb, NCR);
  178. netctl |= MACB_BIT(MPE);
  179. macb_writel(macb, NCR, netctl);
  180. frame = (MACB_BF(SOF, 1)
  181. | MACB_BF(RW, 2)
  182. | MACB_BF(PHYA, phy_adr)
  183. | MACB_BF(REGA, reg)
  184. | MACB_BF(CODE, 2));
  185. macb_writel(macb, MAN, frame);
  186. do {
  187. netstat = macb_readl(macb, NSR);
  188. } while (!(netstat & MACB_BIT(IDLE)));
  189. frame = macb_readl(macb, MAN);
  190. netctl = macb_readl(macb, NCR);
  191. netctl &= ~MACB_BIT(MPE);
  192. macb_writel(macb, NCR, netctl);
  193. return MACB_BFEXT(DATA, frame);
  194. }
  195. void __weak arch_get_mdio_control(const char *name)
  196. {
  197. return;
  198. }
  199. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  200. int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
  201. {
  202. u16 value = 0;
  203. struct udevice *dev = eth_get_dev_by_name(bus->name);
  204. struct macb_device *macb = dev_get_priv(dev);
  205. arch_get_mdio_control(bus->name);
  206. value = macb_mdio_read(macb, phy_adr, reg);
  207. return value;
  208. }
  209. int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
  210. u16 value)
  211. {
  212. struct udevice *dev = eth_get_dev_by_name(bus->name);
  213. struct macb_device *macb = dev_get_priv(dev);
  214. arch_get_mdio_control(bus->name);
  215. macb_mdio_write(macb, phy_adr, reg, value);
  216. return 0;
  217. }
  218. #endif
  219. #define RX 1
  220. #define TX 0
  221. static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
  222. {
  223. if (rx)
  224. invalidate_dcache_range(macb->rx_ring_dma,
  225. ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
  226. PKTALIGN));
  227. else
  228. invalidate_dcache_range(macb->tx_ring_dma,
  229. ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
  230. PKTALIGN));
  231. }
  232. static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
  233. {
  234. if (rx)
  235. flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  236. ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
  237. else
  238. flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  239. ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
  240. }
  241. static inline void macb_flush_rx_buffer(struct macb_device *macb)
  242. {
  243. flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  244. ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
  245. PKTALIGN));
  246. }
  247. static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
  248. {
  249. invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  250. ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
  251. PKTALIGN));
  252. }
  253. #if defined(CONFIG_CMD_NET)
  254. static struct macb_dma_desc_64 *macb_64b_desc(struct macb_dma_desc *desc)
  255. {
  256. return (struct macb_dma_desc_64 *)((void *)desc
  257. + sizeof(struct macb_dma_desc));
  258. }
  259. static void macb_set_addr(struct macb_device *macb, struct macb_dma_desc *desc,
  260. ulong addr)
  261. {
  262. struct macb_dma_desc_64 *desc_64;
  263. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
  264. desc_64 = macb_64b_desc(desc);
  265. desc_64->addrh = upper_32_bits(addr);
  266. }
  267. desc->addr = lower_32_bits(addr);
  268. }
  269. static int _macb_send(struct macb_device *macb, const char *name, void *packet,
  270. int length)
  271. {
  272. unsigned long paddr, ctrl;
  273. unsigned int tx_head = macb->tx_head;
  274. int i;
  275. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  276. ctrl = length & TXBUF_FRMLEN_MASK;
  277. ctrl |= MACB_BIT(TX_LAST);
  278. if (tx_head == (MACB_TX_RING_SIZE - 1)) {
  279. ctrl |= MACB_BIT(TX_WRAP);
  280. macb->tx_head = 0;
  281. } else {
  282. macb->tx_head++;
  283. }
  284. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
  285. tx_head = tx_head * 2;
  286. macb->tx_ring[tx_head].ctrl = ctrl;
  287. macb_set_addr(macb, &macb->tx_ring[tx_head], paddr);
  288. barrier();
  289. macb_flush_ring_desc(macb, TX);
  290. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  291. /*
  292. * I guess this is necessary because the networking core may
  293. * re-use the transmit buffer as soon as we return...
  294. */
  295. for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
  296. barrier();
  297. macb_invalidate_ring_desc(macb, TX);
  298. ctrl = macb->tx_ring[tx_head].ctrl;
  299. if (ctrl & MACB_BIT(TX_USED))
  300. break;
  301. udelay(1);
  302. }
  303. dma_unmap_single(paddr, length, DMA_TO_DEVICE);
  304. if (i <= MACB_TX_TIMEOUT) {
  305. if (ctrl & MACB_BIT(TX_UNDERRUN))
  306. printf("%s: TX underrun\n", name);
  307. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  308. printf("%s: TX buffers exhausted in mid frame\n", name);
  309. } else {
  310. printf("%s: TX timeout\n", name);
  311. }
  312. /* No one cares anyway */
  313. return 0;
  314. }
  315. static void reclaim_rx_buffer(struct macb_device *macb,
  316. unsigned int idx)
  317. {
  318. unsigned int mask;
  319. unsigned int shift;
  320. unsigned int i;
  321. /*
  322. * There may be multiple descriptors per CPU cacheline,
  323. * so a cache flush would flush the whole line, meaning the content of other descriptors
  324. * in the cacheline would also flush. If one of the other descriptors had been
  325. * written to by the controller, the flush would cause those changes to be lost.
  326. *
  327. * To circumvent this issue, we do the actual freeing only when we need to free
  328. * the last descriptor in the current cacheline. When the current descriptor is the
  329. * last in the cacheline, we free all the descriptors that belong to that cacheline.
  330. */
  331. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
  332. mask = DESC_PER_CACHELINE_64 - 1;
  333. shift = 1;
  334. } else {
  335. mask = DESC_PER_CACHELINE_32 - 1;
  336. shift = 0;
  337. }
  338. /* we exit without freeing if idx is not the last descriptor in the cacheline */
  339. if ((idx & mask) != mask)
  340. return;
  341. for (i = idx & (~mask); i <= idx; i++)
  342. macb->rx_ring[i << shift].addr &= ~MACB_BIT(RX_USED);
  343. }
  344. static void reclaim_rx_buffers(struct macb_device *macb,
  345. unsigned int new_tail)
  346. {
  347. unsigned int i;
  348. i = macb->rx_tail;
  349. macb_invalidate_ring_desc(macb, RX);
  350. while (i > new_tail) {
  351. reclaim_rx_buffer(macb, i);
  352. i++;
  353. if (i >= MACB_RX_RING_SIZE)
  354. i = 0;
  355. }
  356. while (i < new_tail) {
  357. reclaim_rx_buffer(macb, i);
  358. i++;
  359. }
  360. barrier();
  361. macb_flush_ring_desc(macb, RX);
  362. macb->rx_tail = new_tail;
  363. }
  364. static int _macb_recv(struct macb_device *macb, uchar **packetp)
  365. {
  366. unsigned int next_rx_tail = macb->next_rx_tail;
  367. void *buffer;
  368. int length;
  369. u32 status;
  370. u8 flag = false;
  371. macb->wrapped = false;
  372. for (;;) {
  373. macb_invalidate_ring_desc(macb, RX);
  374. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
  375. next_rx_tail = next_rx_tail * 2;
  376. if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
  377. return -EAGAIN;
  378. status = macb->rx_ring[next_rx_tail].ctrl;
  379. if (status & MACB_BIT(RX_SOF)) {
  380. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
  381. next_rx_tail = next_rx_tail / 2;
  382. flag = true;
  383. }
  384. if (next_rx_tail != macb->rx_tail)
  385. reclaim_rx_buffers(macb, next_rx_tail);
  386. macb->wrapped = false;
  387. }
  388. if (status & MACB_BIT(RX_EOF)) {
  389. buffer = macb->rx_buffer +
  390. macb->rx_buffer_size * macb->rx_tail;
  391. length = status & RXBUF_FRMLEN_MASK;
  392. macb_invalidate_rx_buffer(macb);
  393. if (macb->wrapped) {
  394. unsigned int headlen, taillen;
  395. headlen = macb->rx_buffer_size *
  396. (MACB_RX_RING_SIZE - macb->rx_tail);
  397. taillen = length - headlen;
  398. memcpy((void *)net_rx_packets[0],
  399. buffer, headlen);
  400. memcpy((void *)net_rx_packets[0] + headlen,
  401. macb->rx_buffer, taillen);
  402. *packetp = (void *)net_rx_packets[0];
  403. } else {
  404. *packetp = buffer;
  405. }
  406. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
  407. if (!flag)
  408. next_rx_tail = next_rx_tail / 2;
  409. }
  410. if (++next_rx_tail >= MACB_RX_RING_SIZE)
  411. next_rx_tail = 0;
  412. macb->next_rx_tail = next_rx_tail;
  413. return length;
  414. } else {
  415. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
  416. if (!flag)
  417. next_rx_tail = next_rx_tail / 2;
  418. flag = false;
  419. }
  420. if (++next_rx_tail >= MACB_RX_RING_SIZE) {
  421. macb->wrapped = true;
  422. next_rx_tail = 0;
  423. }
  424. }
  425. barrier();
  426. }
  427. }
  428. static void macb_phy_reset(struct macb_device *macb, const char *name)
  429. {
  430. int i;
  431. u16 status, adv;
  432. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  433. macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
  434. printf("%s: Starting autonegotiation...\n", name);
  435. macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
  436. | BMCR_ANRESTART));
  437. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  438. status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
  439. if (status & BMSR_ANEGCOMPLETE)
  440. break;
  441. udelay(100);
  442. }
  443. if (status & BMSR_ANEGCOMPLETE)
  444. printf("%s: Autonegotiation complete\n", name);
  445. else
  446. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  447. name, status);
  448. }
  449. static int macb_phy_find(struct macb_device *macb, const char *name)
  450. {
  451. int i;
  452. u16 phy_id;
  453. phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
  454. if (phy_id != 0xffff) {
  455. printf("%s: PHY present at %d\n", name, macb->phy_addr);
  456. return 0;
  457. }
  458. /* Search for PHY... */
  459. for (i = 0; i < 32; i++) {
  460. macb->phy_addr = i;
  461. phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
  462. if (phy_id != 0xffff) {
  463. printf("%s: PHY present at %d\n", name, i);
  464. return 0;
  465. }
  466. }
  467. /* PHY isn't up to snuff */
  468. printf("%s: PHY not found\n", name);
  469. return -ENODEV;
  470. }
  471. /**
  472. * macb_linkspd_cb - Linkspeed change callback function
  473. * @dev/@regs: MACB udevice (DM version) or
  474. * Base Register of MACB devices (non-DM version)
  475. * @speed: Linkspeed
  476. * Returns 0 when operation success and negative errno number
  477. * when operation failed.
  478. */
  479. static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
  480. {
  481. void *gemgxl_regs;
  482. gemgxl_regs = dev_read_addr_index_ptr(dev, 1);
  483. if (!gemgxl_regs)
  484. return -ENODEV;
  485. /*
  486. * SiFive GEMGXL TX clock operation mode:
  487. *
  488. * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
  489. * and output clock on GMII output signal GTX_CLK
  490. * 1 = MII mode. Use MII input signal TX_CLK in TX logic
  491. */
  492. writel(rate != 125000000, gemgxl_regs);
  493. return 0;
  494. }
  495. static int macb_sama7g5_clk_init(struct udevice *dev, ulong rate)
  496. {
  497. struct clk clk;
  498. int ret;
  499. ret = clk_get_by_name(dev, "tx_clk", &clk);
  500. if (ret)
  501. return ret;
  502. /*
  503. * This is for using GCK. Clock rate is addressed via assigned-clock
  504. * property, so only clock enable is needed here. The switching to
  505. * proper clock rate depending on link speed is managed by IP logic.
  506. */
  507. return clk_enable(&clk);
  508. }
  509. int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
  510. {
  511. #ifdef CONFIG_CLK
  512. struct macb_device *macb = dev_get_priv(dev);
  513. struct clk tx_clk;
  514. ulong rate;
  515. int ret;
  516. switch (speed) {
  517. case _10BASET:
  518. rate = 2500000; /* 2.5 MHz */
  519. break;
  520. case _100BASET:
  521. rate = 25000000; /* 25 MHz */
  522. break;
  523. case _1000BASET:
  524. rate = 125000000; /* 125 MHz */
  525. break;
  526. default:
  527. /* does not change anything */
  528. return 0;
  529. }
  530. if (macb->config->clk_init)
  531. return macb->config->clk_init(dev, rate);
  532. /*
  533. * "tx_clk" is an optional clock source for MACB.
  534. * Ignore if it does not exist in DT.
  535. */
  536. ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
  537. if (ret)
  538. return 0;
  539. if (tx_clk.dev) {
  540. ret = clk_set_rate(&tx_clk, rate);
  541. if (ret < 0)
  542. return ret;
  543. }
  544. #endif
  545. return 0;
  546. }
  547. static int macb_phy_init(struct udevice *dev, const char *name)
  548. {
  549. struct macb_device *macb = dev_get_priv(dev);
  550. u32 ncfgr;
  551. u16 phy_id, status, adv, lpa;
  552. int media, speed, duplex;
  553. int ret;
  554. int i;
  555. arch_get_mdio_control(name);
  556. /* Auto-detect phy_addr */
  557. ret = macb_phy_find(macb, name);
  558. if (ret)
  559. return ret;
  560. /* Check if the PHY is up to snuff... */
  561. phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
  562. if (phy_id == 0xffff) {
  563. printf("%s: No PHY present\n", name);
  564. return -ENODEV;
  565. }
  566. #ifdef CONFIG_PHYLIB
  567. macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
  568. macb->phy_interface);
  569. if (!macb->phydev) {
  570. printf("phy_connect failed\n");
  571. return -ENODEV;
  572. }
  573. phy_config(macb->phydev);
  574. #endif
  575. status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
  576. if (!(status & BMSR_LSTATUS)) {
  577. /* Try to re-negotiate if we don't have link already. */
  578. macb_phy_reset(macb, name);
  579. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  580. status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
  581. if (status & BMSR_LSTATUS) {
  582. /*
  583. * Delay a bit after the link is established,
  584. * so that the next xfer does not fail
  585. */
  586. mdelay(10);
  587. break;
  588. }
  589. udelay(100);
  590. }
  591. }
  592. if (!(status & BMSR_LSTATUS)) {
  593. printf("%s: link down (status: 0x%04x)\n",
  594. name, status);
  595. return -ENETDOWN;
  596. }
  597. /* First check for GMAC and that it is GiB capable */
  598. if (gem_is_gigabit_capable(macb)) {
  599. lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
  600. if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
  601. LPA_1000XHALF)) {
  602. duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
  603. 1 : 0);
  604. printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
  605. name,
  606. duplex ? "full" : "half",
  607. lpa);
  608. ncfgr = macb_readl(macb, NCFGR);
  609. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  610. ncfgr |= GEM_BIT(GBE);
  611. if (duplex)
  612. ncfgr |= MACB_BIT(FD);
  613. macb_writel(macb, NCFGR, ncfgr);
  614. ret = macb_linkspd_cb(dev, _1000BASET);
  615. if (ret)
  616. return ret;
  617. return 0;
  618. }
  619. }
  620. /* fall back for EMAC checking */
  621. adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
  622. lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
  623. media = mii_nway_result(lpa & adv);
  624. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  625. ? 1 : 0);
  626. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  627. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  628. name,
  629. speed ? "100" : "10",
  630. duplex ? "full" : "half",
  631. lpa);
  632. ncfgr = macb_readl(macb, NCFGR);
  633. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
  634. if (speed) {
  635. ncfgr |= MACB_BIT(SPD);
  636. ret = macb_linkspd_cb(dev, _100BASET);
  637. } else {
  638. ret = macb_linkspd_cb(dev, _10BASET);
  639. }
  640. if (ret)
  641. return ret;
  642. if (duplex)
  643. ncfgr |= MACB_BIT(FD);
  644. macb_writel(macb, NCFGR, ncfgr);
  645. return 0;
  646. }
  647. static int gmac_init_multi_queues(struct macb_device *macb)
  648. {
  649. int i, num_queues = 1;
  650. u32 queue_mask;
  651. unsigned long paddr;
  652. /* bit 0 is never set but queue 0 always exists */
  653. queue_mask = gem_readl(macb, DCFG6) & 0xff;
  654. queue_mask |= 0x1;
  655. for (i = 1; i < MACB_MAX_QUEUES; i++)
  656. if (queue_mask & (1 << i))
  657. num_queues++;
  658. macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
  659. macb->dummy_desc->addr = 0;
  660. flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
  661. ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
  662. paddr = macb->dummy_desc_dma;
  663. for (i = 1; i < num_queues; i++) {
  664. gem_writel_queue_TBQP(macb, lower_32_bits(paddr), i - 1);
  665. gem_writel_queue_RBQP(macb, lower_32_bits(paddr), i - 1);
  666. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
  667. gem_writel_queue_TBQPH(macb, upper_32_bits(paddr),
  668. i - 1);
  669. gem_writel_queue_RBQPH(macb, upper_32_bits(paddr),
  670. i - 1);
  671. }
  672. }
  673. return 0;
  674. }
  675. static void gmac_configure_dma(struct macb_device *macb)
  676. {
  677. u32 buffer_size;
  678. u32 dmacfg;
  679. buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
  680. dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
  681. dmacfg |= GEM_BF(RXBS, buffer_size);
  682. if (macb->config->dma_burst_length)
  683. dmacfg = GEM_BFINS(FBLDO,
  684. macb->config->dma_burst_length, dmacfg);
  685. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  686. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  687. if (macb->is_big_endian)
  688. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  689. else
  690. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  691. dmacfg &= ~GEM_BIT(ADDR64);
  692. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
  693. dmacfg |= GEM_BIT(ADDR64);
  694. gem_writel(macb, DMACFG, dmacfg);
  695. }
  696. static int _macb_init(struct udevice *dev, const char *name)
  697. {
  698. struct macb_device *macb = dev_get_priv(dev);
  699. unsigned int val = 0;
  700. unsigned long paddr;
  701. int ret;
  702. int i;
  703. int count;
  704. /*
  705. * macb_halt should have been called at some point before now,
  706. * so we'll assume the controller is idle.
  707. */
  708. /* initialize DMA descriptors */
  709. paddr = macb->rx_buffer_dma;
  710. for (i = 0; i < MACB_RX_RING_SIZE; i++) {
  711. if (i == (MACB_RX_RING_SIZE - 1))
  712. paddr |= MACB_BIT(RX_WRAP);
  713. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
  714. count = i * 2;
  715. else
  716. count = i;
  717. macb->rx_ring[count].ctrl = 0;
  718. macb_set_addr(macb, &macb->rx_ring[count], paddr);
  719. paddr += macb->rx_buffer_size;
  720. }
  721. macb_flush_ring_desc(macb, RX);
  722. macb_flush_rx_buffer(macb);
  723. for (i = 0; i < MACB_TX_RING_SIZE; i++) {
  724. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
  725. count = i * 2;
  726. else
  727. count = i;
  728. macb_set_addr(macb, &macb->tx_ring[count], 0);
  729. if (i == (MACB_TX_RING_SIZE - 1))
  730. macb->tx_ring[count].ctrl = MACB_BIT(TX_USED) |
  731. MACB_BIT(TX_WRAP);
  732. else
  733. macb->tx_ring[count].ctrl = MACB_BIT(TX_USED);
  734. }
  735. macb_flush_ring_desc(macb, TX);
  736. macb->rx_tail = 0;
  737. macb->tx_head = 0;
  738. macb->tx_tail = 0;
  739. macb->next_rx_tail = 0;
  740. #ifdef CONFIG_MACB_ZYNQ
  741. gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
  742. #endif
  743. macb_writel(macb, RBQP, lower_32_bits(macb->rx_ring_dma));
  744. macb_writel(macb, TBQP, lower_32_bits(macb->tx_ring_dma));
  745. if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
  746. macb_writel(macb, RBQPH, upper_32_bits(macb->rx_ring_dma));
  747. macb_writel(macb, TBQPH, upper_32_bits(macb->tx_ring_dma));
  748. }
  749. if (macb_is_gem(macb)) {
  750. /* Initialize DMA properties */
  751. gmac_configure_dma(macb);
  752. /* Check the multi queue and initialize the queue for tx */
  753. gmac_init_multi_queues(macb);
  754. /*
  755. * When the GMAC IP with GE feature, this bit is used to
  756. * select interface between RGMII and GMII.
  757. * When the GMAC IP without GE feature, this bit is used
  758. * to select interface between RMII and MII.
  759. */
  760. if (macb->phy_interface == PHY_INTERFACE_MODE_RGMII ||
  761. macb->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
  762. macb->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
  763. macb->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
  764. val = macb->config->usrio->rgmii;
  765. else if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
  766. val = macb->config->usrio->rmii;
  767. else if (macb->phy_interface == PHY_INTERFACE_MODE_MII)
  768. val = macb->config->usrio->mii;
  769. if (macb->config->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  770. val |= macb->config->usrio->clken;
  771. gem_writel(macb, USRIO, val);
  772. if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  773. unsigned int ncfgr = macb_readl(macb, NCFGR);
  774. ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  775. macb_writel(macb, NCFGR, ncfgr);
  776. }
  777. } else {
  778. /* choose RMII or MII mode. This depends on the board */
  779. #ifdef CONFIG_AT91FAMILY
  780. if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
  781. macb_writel(macb, USRIO,
  782. macb->config->usrio->rmii |
  783. macb->config->usrio->clken);
  784. } else {
  785. macb_writel(macb, USRIO, macb->config->usrio->clken);
  786. }
  787. #else
  788. if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
  789. macb_writel(macb, USRIO, 0);
  790. else
  791. macb_writel(macb, USRIO, macb->config->usrio->mii);
  792. #endif
  793. }
  794. ret = macb_phy_init(dev, name);
  795. if (ret)
  796. return ret;
  797. /* Enable TX and RX */
  798. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  799. return 0;
  800. }
  801. static void _macb_halt(struct macb_device *macb)
  802. {
  803. u32 ncr, tsr;
  804. /* Halt the controller and wait for any ongoing transmission to end. */
  805. ncr = macb_readl(macb, NCR);
  806. ncr |= MACB_BIT(THALT);
  807. macb_writel(macb, NCR, ncr);
  808. do {
  809. tsr = macb_readl(macb, TSR);
  810. } while (tsr & MACB_BIT(TGO));
  811. /* Disable TX and RX, and clear statistics */
  812. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  813. }
  814. static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
  815. {
  816. u32 hwaddr_bottom;
  817. u16 hwaddr_top;
  818. /* set hardware address */
  819. hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
  820. enetaddr[2] << 16 | enetaddr[3] << 24;
  821. macb_writel(macb, SA1B, hwaddr_bottom);
  822. hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
  823. macb_writel(macb, SA1T, hwaddr_top);
  824. return 0;
  825. }
  826. static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
  827. {
  828. u32 config;
  829. #if defined(CONFIG_CLK)
  830. unsigned long macb_hz = macb->pclk_rate;
  831. #else
  832. unsigned long macb_hz = get_macb_pclk_rate(id);
  833. #endif
  834. if (macb_hz < 20000000)
  835. config = MACB_BF(CLK, MACB_CLK_DIV8);
  836. else if (macb_hz < 40000000)
  837. config = MACB_BF(CLK, MACB_CLK_DIV16);
  838. else if (macb_hz < 80000000)
  839. config = MACB_BF(CLK, MACB_CLK_DIV32);
  840. else
  841. config = MACB_BF(CLK, MACB_CLK_DIV64);
  842. return config;
  843. }
  844. static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
  845. {
  846. u32 config;
  847. #if defined(CONFIG_CLK)
  848. unsigned long macb_hz = macb->pclk_rate;
  849. #else
  850. unsigned long macb_hz = get_macb_pclk_rate(id);
  851. #endif
  852. if (macb_hz < 20000000)
  853. config = GEM_BF(CLK, GEM_CLK_DIV8);
  854. else if (macb_hz < 40000000)
  855. config = GEM_BF(CLK, GEM_CLK_DIV16);
  856. else if (macb_hz < 80000000)
  857. config = GEM_BF(CLK, GEM_CLK_DIV32);
  858. else if (macb_hz < 120000000)
  859. config = GEM_BF(CLK, GEM_CLK_DIV48);
  860. else if (macb_hz < 160000000)
  861. config = GEM_BF(CLK, GEM_CLK_DIV64);
  862. else if (macb_hz < 240000000)
  863. config = GEM_BF(CLK, GEM_CLK_DIV96);
  864. else if (macb_hz < 320000000)
  865. config = GEM_BF(CLK, GEM_CLK_DIV128);
  866. else
  867. config = GEM_BF(CLK, GEM_CLK_DIV224);
  868. return config;
  869. }
  870. /*
  871. * Get the DMA bus width field of the network configuration register that we
  872. * should program. We find the width from decoding the design configuration
  873. * register to find the maximum supported data bus width.
  874. */
  875. static u32 macb_dbw(struct macb_device *macb)
  876. {
  877. switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
  878. case 4:
  879. return GEM_BF(DBW, GEM_DBW128);
  880. case 2:
  881. return GEM_BF(DBW, GEM_DBW64);
  882. case 1:
  883. default:
  884. return GEM_BF(DBW, GEM_DBW32);
  885. }
  886. }
  887. static void _macb_eth_initialize(struct macb_device *macb)
  888. {
  889. int id = 0; /* This is not used by functions we call */
  890. u32 ncfgr;
  891. if (macb_is_gem(macb))
  892. macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
  893. else
  894. macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  895. /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
  896. macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
  897. MACB_RX_RING_SIZE,
  898. &macb->rx_buffer_dma);
  899. macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
  900. &macb->rx_ring_dma);
  901. macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
  902. &macb->tx_ring_dma);
  903. macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
  904. &macb->dummy_desc_dma);
  905. /*
  906. * Do some basic initialization so that we at least can talk
  907. * to the PHY
  908. */
  909. if (macb_is_gem(macb)) {
  910. ncfgr = gem_mdc_clk_div(id, macb);
  911. ncfgr |= macb_dbw(macb);
  912. } else {
  913. ncfgr = macb_mdc_clk_div(id, macb);
  914. }
  915. macb_writel(macb, NCFGR, ncfgr);
  916. }
  917. static int macb_start(struct udevice *dev)
  918. {
  919. return _macb_init(dev, dev->name);
  920. }
  921. static int macb_send(struct udevice *dev, void *packet, int length)
  922. {
  923. struct macb_device *macb = dev_get_priv(dev);
  924. return _macb_send(macb, dev->name, packet, length);
  925. }
  926. static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
  927. {
  928. struct macb_device *macb = dev_get_priv(dev);
  929. macb->next_rx_tail = macb->rx_tail;
  930. macb->wrapped = false;
  931. return _macb_recv(macb, packetp);
  932. }
  933. static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
  934. {
  935. struct macb_device *macb = dev_get_priv(dev);
  936. reclaim_rx_buffers(macb, macb->next_rx_tail);
  937. return 0;
  938. }
  939. static void macb_stop(struct udevice *dev)
  940. {
  941. struct macb_device *macb = dev_get_priv(dev);
  942. _macb_halt(macb);
  943. }
  944. static int macb_write_hwaddr(struct udevice *dev)
  945. {
  946. struct eth_pdata *plat = dev_get_plat(dev);
  947. struct macb_device *macb = dev_get_priv(dev);
  948. return _macb_write_hwaddr(macb, plat->enetaddr);
  949. }
  950. static const struct eth_ops macb_eth_ops = {
  951. .start = macb_start,
  952. .send = macb_send,
  953. .recv = macb_recv,
  954. .stop = macb_stop,
  955. .free_pkt = macb_free_pkt,
  956. .write_hwaddr = macb_write_hwaddr,
  957. };
  958. #ifdef CONFIG_CLK
  959. static int macb_enable_clk(struct udevice *dev)
  960. {
  961. struct macb_device *macb = dev_get_priv(dev);
  962. struct clk clk;
  963. ulong clk_rate;
  964. int ret;
  965. ret = clk_get_by_index(dev, 0, &clk);
  966. if (ret)
  967. return -EINVAL;
  968. /*
  969. * If clock driver didn't support enable or disable then
  970. * we get -ENOSYS from clk_enable(). To handle this, we
  971. * don't fail for ret == -ENOSYS.
  972. */
  973. ret = clk_enable(&clk);
  974. if (ret && ret != -ENOSYS)
  975. return ret;
  976. clk_rate = clk_get_rate(&clk);
  977. if (!clk_rate)
  978. return -EINVAL;
  979. macb->pclk_rate = clk_rate;
  980. return 0;
  981. }
  982. #endif
  983. static const struct macb_usrio_cfg macb_default_usrio = {
  984. .mii = MACB_BIT(MII),
  985. .rmii = MACB_BIT(RMII),
  986. .rgmii = GEM_BIT(RGMII),
  987. .clken = MACB_BIT(CLKEN),
  988. };
  989. static struct macb_config default_gem_config = {
  990. .dma_burst_length = 16,
  991. .hw_dma_cap = HW_DMA_CAP_32B,
  992. .clk_init = NULL,
  993. .usrio = &macb_default_usrio,
  994. };
  995. static int macb_eth_probe(struct udevice *dev)
  996. {
  997. struct eth_pdata *pdata = dev_get_plat(dev);
  998. struct macb_device *macb = dev_get_priv(dev);
  999. struct ofnode_phandle_args phandle_args;
  1000. int ret;
  1001. macb->phy_interface = dev_read_phy_mode(dev);
  1002. if (macb->phy_interface == PHY_INTERFACE_MODE_NA)
  1003. return -EINVAL;
  1004. /* Read phyaddr from DT */
  1005. if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
  1006. &phandle_args))
  1007. macb->phy_addr = ofnode_read_u32_default(phandle_args.node,
  1008. "reg", -1);
  1009. macb->regs = (void *)(uintptr_t)pdata->iobase;
  1010. macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
  1011. macb->config = (struct macb_config *)dev_get_driver_data(dev);
  1012. if (!macb->config) {
  1013. if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT)) {
  1014. if (GEM_BFEXT(DAW64, gem_readl(macb, DCFG6)))
  1015. default_gem_config.hw_dma_cap = HW_DMA_CAP_64B;
  1016. }
  1017. macb->config = &default_gem_config;
  1018. }
  1019. #ifdef CONFIG_CLK
  1020. ret = macb_enable_clk(dev);
  1021. if (ret)
  1022. return ret;
  1023. #endif
  1024. _macb_eth_initialize(macb);
  1025. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  1026. macb->bus = mdio_alloc();
  1027. if (!macb->bus)
  1028. return -ENOMEM;
  1029. strlcpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
  1030. macb->bus->read = macb_miiphy_read;
  1031. macb->bus->write = macb_miiphy_write;
  1032. ret = mdio_register(macb->bus);
  1033. if (ret < 0)
  1034. return ret;
  1035. macb->bus = miiphy_get_dev_by_name(dev->name);
  1036. #endif
  1037. return 0;
  1038. }
  1039. static int macb_eth_remove(struct udevice *dev)
  1040. {
  1041. struct macb_device *macb = dev_get_priv(dev);
  1042. #ifdef CONFIG_PHYLIB
  1043. free(macb->phydev);
  1044. #endif
  1045. mdio_unregister(macb->bus);
  1046. mdio_free(macb->bus);
  1047. return 0;
  1048. }
  1049. /**
  1050. * macb_late_eth_of_to_plat
  1051. * @dev: udevice struct
  1052. * Returns 0 when operation success and negative errno number
  1053. * when operation failed.
  1054. */
  1055. int __weak macb_late_eth_of_to_plat(struct udevice *dev)
  1056. {
  1057. return 0;
  1058. }
  1059. static int macb_eth_of_to_plat(struct udevice *dev)
  1060. {
  1061. struct eth_pdata *pdata = dev_get_plat(dev);
  1062. pdata->iobase = (uintptr_t)dev_remap_addr(dev);
  1063. if (!pdata->iobase)
  1064. return -EINVAL;
  1065. return macb_late_eth_of_to_plat(dev);
  1066. }
  1067. static const struct macb_usrio_cfg sama7g5_usrio = {
  1068. .mii = 0,
  1069. .rmii = 1,
  1070. .rgmii = 2,
  1071. .clken = BIT(2),
  1072. };
  1073. static const struct macb_config sama5d4_config = {
  1074. .dma_burst_length = 4,
  1075. .hw_dma_cap = HW_DMA_CAP_32B,
  1076. .clk_init = NULL,
  1077. .usrio = &macb_default_usrio,
  1078. };
  1079. static const struct macb_config sifive_config = {
  1080. .dma_burst_length = 16,
  1081. .hw_dma_cap = HW_DMA_CAP_32B,
  1082. .clk_init = macb_sifive_clk_init,
  1083. .usrio = &macb_default_usrio,
  1084. };
  1085. static const struct macb_config sama7g5_gmac_config = {
  1086. .dma_burst_length = 16,
  1087. .hw_dma_cap = HW_DMA_CAP_32B,
  1088. .clk_init = macb_sama7g5_clk_init,
  1089. .usrio = &sama7g5_usrio,
  1090. };
  1091. static const struct macb_config sama7g5_emac_config = {
  1092. .caps = MACB_CAPS_USRIO_HAS_CLKEN,
  1093. .dma_burst_length = 16,
  1094. .hw_dma_cap = HW_DMA_CAP_32B,
  1095. .usrio = &sama7g5_usrio,
  1096. };
  1097. static const struct udevice_id macb_eth_ids[] = {
  1098. { .compatible = "cdns,macb" },
  1099. { .compatible = "cdns,at91sam9260-macb" },
  1100. { .compatible = "cdns,sam9x60-macb" },
  1101. { .compatible = "cdns,sama7g5-gem",
  1102. .data = (ulong)&sama7g5_gmac_config },
  1103. { .compatible = "cdns,sama7g5-emac",
  1104. .data = (ulong)&sama7g5_emac_config },
  1105. { .compatible = "atmel,sama5d2-gem" },
  1106. { .compatible = "atmel,sama5d3-gem" },
  1107. { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
  1108. { .compatible = "cdns,zynq-gem" },
  1109. { .compatible = "sifive,fu540-c000-gem",
  1110. .data = (ulong)&sifive_config },
  1111. { }
  1112. };
  1113. U_BOOT_DRIVER(eth_macb) = {
  1114. .name = "eth_macb",
  1115. .id = UCLASS_ETH,
  1116. .of_match = macb_eth_ids,
  1117. .of_to_plat = macb_eth_of_to_plat,
  1118. .probe = macb_eth_probe,
  1119. .remove = macb_eth_remove,
  1120. .ops = &macb_eth_ops,
  1121. .priv_auto = sizeof(struct macb_device),
  1122. .plat_auto = sizeof(struct eth_pdata),
  1123. };
  1124. #endif