ark_eth_qos.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. */
  5. #include <common.h>
  6. #include <clk.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <memalign.h>
  10. #include <miiphy.h>
  11. #include <net.h>
  12. #include <netdev.h>
  13. #include <phy.h>
  14. #include <reset.h>
  15. #include <wait_bit.h>
  16. #include <asm/gpio.h>
  17. #include <asm/io.h>
  18. /* Core registers */
  19. #define EQOS_MAC_REGS_BASE 0x000
  20. struct eqos_mac_regs {
  21. uint32_t configuration; /* 0x000 */
  22. uint32_t unused_004[(0x070 - 0x004) / 4]; /* 0x004 */
  23. uint32_t q0_tx_flow_ctrl; /* 0x070 */
  24. uint32_t unused_070[(0x090 - 0x074) / 4]; /* 0x074 */
  25. uint32_t rx_flow_ctrl; /* 0x090 */
  26. uint32_t unused_094; /* 0x094 */
  27. uint32_t txq_prty_map0; /* 0x098 */
  28. uint32_t unused_09c; /* 0x09c */
  29. uint32_t rxq_ctrl0; /* 0x0a0 */
  30. uint32_t unused_0a4; /* 0x0a4 */
  31. uint32_t rxq_ctrl2; /* 0x0a8 */
  32. uint32_t unused_0ac[(0x0dc - 0x0ac) / 4]; /* 0x0ac */
  33. uint32_t us_tic_counter; /* 0x0dc */
  34. uint32_t unused_0e0[(0x11c - 0x0e0) / 4]; /* 0x0e0 */
  35. uint32_t hw_feature0; /* 0x11c */
  36. uint32_t hw_feature1; /* 0x120 */
  37. uint32_t hw_feature2; /* 0x124 */
  38. uint32_t unused_128[(0x200 - 0x128) / 4]; /* 0x128 */
  39. uint32_t mdio_address; /* 0x200 */
  40. uint32_t mdio_data; /* 0x204 */
  41. uint32_t unused_208[(0x300 - 0x208) / 4]; /* 0x208 */
  42. uint32_t address0_high; /* 0x300 */
  43. uint32_t address0_low; /* 0x304 */
  44. };
  45. #define EQOS_MAC_CONFIGURATION_GPSLCE BIT(23)
  46. #define EQOS_MAC_CONFIGURATION_CST BIT(21)
  47. #define EQOS_MAC_CONFIGURATION_ACS BIT(20)
  48. #define EQOS_MAC_CONFIGURATION_WD BIT(19)
  49. #define EQOS_MAC_CONFIGURATION_JD BIT(17)
  50. #define EQOS_MAC_CONFIGURATION_JE BIT(16)
  51. #define EQOS_MAC_CONFIGURATION_PS BIT(15)
  52. #define EQOS_MAC_CONFIGURATION_FES BIT(14)
  53. #define EQOS_MAC_CONFIGURATION_DM BIT(13)
  54. #define EQOS_MAC_CONFIGURATION_TE BIT(1)
  55. #define EQOS_MAC_CONFIGURATION_RE BIT(0)
  56. #define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT 16
  57. #define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_MASK 0xffff
  58. #define EQOS_MAC_Q0_TX_FLOW_CTRL_TFE BIT(1)
  59. #define EQOS_MAC_RX_FLOW_CTRL_RFE BIT(0)
  60. #define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT 0
  61. #define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK 0xff
  62. #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT 0
  63. #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK 3
  64. #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_NOT_ENABLED 0
  65. #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB 2
  66. #define EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT 0
  67. #define EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK 0xff
  68. #define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT 6
  69. #define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK 0x1f
  70. #define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT 0
  71. #define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK 0x1f
  72. #define EQOS_MAC_MDIO_ADDRESS_PA_SHIFT 21
  73. #define EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT 16
  74. #define EQOS_MAC_MDIO_ADDRESS_CR_SHIFT 8
  75. #define EQOS_MAC_MDIO_ADDRESS_CR_20_35 2
  76. #define EQOS_MAC_MDIO_ADDRESS_SKAP BIT(4)
  77. #define EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT 2
  78. #define EQOS_MAC_MDIO_ADDRESS_GOC_READ 3
  79. #define EQOS_MAC_MDIO_ADDRESS_GOC_WRITE 1
  80. #define EQOS_MAC_MDIO_ADDRESS_C45E BIT(1)
  81. #define EQOS_MAC_MDIO_ADDRESS_GB BIT(0)
  82. #define EQOS_MAC_MDIO_DATA_GD_MASK 0xffff
  83. #define EQOS_MTL_REGS_BASE 0xd00
  84. struct eqos_mtl_regs {
  85. uint32_t txq0_operation_mode; /* 0xd00 */
  86. uint32_t unused_d04; /* 0xd04 */
  87. uint32_t txq0_debug; /* 0xd08 */
  88. uint32_t unused_d0c[(0xd18 - 0xd0c) / 4]; /* 0xd0c */
  89. uint32_t txq0_quantum_weight; /* 0xd18 */
  90. uint32_t unused_d1c[(0xd30 - 0xd1c) / 4]; /* 0xd1c */
  91. uint32_t rxq0_operation_mode; /* 0xd30 */
  92. uint32_t unused_d34; /* 0xd34 */
  93. uint32_t rxq0_debug; /* 0xd38 */
  94. };
  95. #define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT 16
  96. #define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK 0xf
  97. #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT 2
  98. #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_MASK 3
  99. #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED 2
  100. #define EQOS_MTL_TXQ0_OPERATION_MODE_TSF BIT(1)
  101. #define EQOS_MTL_TXQ0_OPERATION_MODE_FTQ BIT(0)
  102. #define EQOS_MTL_TXQ0_DEBUG_TXQSTS BIT(4)
  103. #define EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT 1
  104. #define EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK 3
  105. #define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT 20
  106. #define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK 0x3f
  107. #define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT 14
  108. #define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK 0x1f
  109. #define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT 8
  110. #define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK 0x1f
  111. #define EQOS_MTL_RXQ0_OPERATION_MODE_EHFC BIT(7)
  112. #define EQOS_MTL_RXQ0_OPERATION_MODE_RSF BIT(5)
  113. #define EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT 16
  114. #define EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK 0x7fff
  115. #define EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT 4
  116. #define EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK 3
  117. #define EQOS_DMA_REGS_BASE 0x1000
  118. struct eqos_dma_regs {
  119. uint32_t mode; /* 0x1000 */
  120. uint32_t sysbus_mode; /* 0x1004 */
  121. uint32_t unused_1008[(0x1100 - 0x1008) / 4]; /* 0x1008 */
  122. uint32_t ch0_control; /* 0x1100 */
  123. uint32_t ch0_tx_control; /* 0x1104 */
  124. uint32_t ch0_rx_control; /* 0x1108 */
  125. uint32_t unused_110c; /* 0x110c */
  126. uint32_t ch0_txdesc_list_haddress; /* 0x1110 */
  127. uint32_t ch0_txdesc_list_address; /* 0x1114 */
  128. uint32_t ch0_rxdesc_list_haddress; /* 0x1118 */
  129. uint32_t ch0_rxdesc_list_address; /* 0x111c */
  130. uint32_t ch0_txdesc_tail_pointer; /* 0x1120 */
  131. uint32_t unused_1124; /* 0x1124 */
  132. uint32_t ch0_rxdesc_tail_pointer; /* 0x1128 */
  133. uint32_t ch0_txdesc_ring_length; /* 0x112c */
  134. uint32_t ch0_rxdesc_ring_length; /* 0x1130 */
  135. };
  136. #define EQOS_DMA_MODE_SWR BIT(0)
  137. #define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT 16
  138. #define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK 0xf
  139. #define EQOS_DMA_SYSBUS_MODE_EAME BIT(11)
  140. #define EQOS_DMA_SYSBUS_MODE_BLEN16 BIT(3)
  141. #define EQOS_DMA_SYSBUS_MODE_BLEN8 BIT(2)
  142. #define EQOS_DMA_SYSBUS_MODE_BLEN4 BIT(1)
  143. #define EQOS_DMA_CH0_CONTROL_PBLX8 BIT(16)
  144. #define EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT 16
  145. #define EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK 0x3f
  146. #define EQOS_DMA_CH0_TX_CONTROL_OSP BIT(4)
  147. #define EQOS_DMA_CH0_TX_CONTROL_ST BIT(0)
  148. #define EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT 16
  149. #define EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK 0x3f
  150. #define EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT 5
  151. #define EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK 0x3ff
  152. #define EQOS_DMA_CH0_RX_CONTROL_SR BIT(0)
  153. #define EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD BIT(31)
  154. #define EQOS_AUTO_CAL_CONFIG_START BIT(31)
  155. #define EQOS_AUTO_CAL_CONFIG_ENABLE BIT(29)
  156. #define EQOS_AUTO_CAL_STATUS_ACTIVE BIT(31)
  157. /* Descriptors */
  158. #define EQOS_DESCRIPTOR_WORDS 4
  159. #define EQOS_DESCRIPTOR_SIZE (EQOS_DESCRIPTOR_WORDS * 4)
  160. /* We assume ARCH_DMA_MINALIGN >= 16; 16 is the EQOS HW minimum */
  161. #define EQOS_DESCRIPTOR_ALIGN ARCH_DMA_MINALIGN
  162. #define EQOS_DESCRIPTORS_TX 4
  163. #define EQOS_DESCRIPTORS_RX 4
  164. #define EQOS_DESCRIPTORS_NUM (EQOS_DESCRIPTORS_TX + EQOS_DESCRIPTORS_RX)
  165. #define EQOS_DESCRIPTORS_SIZE ALIGN(EQOS_DESCRIPTORS_NUM * \
  166. EQOS_DESCRIPTOR_SIZE, ARCH_DMA_MINALIGN)
  167. #define EQOS_BUFFER_ALIGN ARCH_DMA_MINALIGN
  168. #define EQOS_MAX_PACKET_SIZE ALIGN(1568, ARCH_DMA_MINALIGN)
  169. #define EQOS_RX_BUFFER_SIZE (EQOS_DESCRIPTORS_RX * EQOS_MAX_PACKET_SIZE)
  170. /*
  171. * Warn if the cache-line size is larger than the descriptor size. In such
  172. * cases the driver will likely fail because the CPU needs to flush the cache
  173. * when requeuing RX buffers, therefore descriptors written by the hardware
  174. * may be discarded. Architectures with full IO coherence, such as x86, do not
  175. * experience this issue, and hence are excluded from this condition.
  176. *
  177. * This can be fixed by defining CONFIG_SYS_NONCACHED_MEMORY which will cause
  178. * the driver to allocate descriptors from a pool of non-cached memory.
  179. */
  180. #if EQOS_DESCRIPTOR_SIZE < ARCH_DMA_MINALIGN
  181. #if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \
  182. !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_X86)
  183. #warning Cache line size is larger than descriptor size
  184. #endif
  185. #endif
  186. struct eqos_desc {
  187. u32 des0;
  188. u32 des1;
  189. u32 des2;
  190. u32 des3;
  191. };
  192. #define EQOS_DESC3_OWN BIT(31)
  193. #define EQOS_DESC3_FD BIT(29)
  194. #define EQOS_DESC3_LD BIT(28)
  195. #define EQOS_DESC3_BUF1V BIT(24)
  196. struct eqos_config {
  197. bool reg_access_always_ok;
  198. };
  199. struct eqos_priv {
  200. struct udevice *dev;
  201. const struct eqos_config *config;
  202. fdt_addr_t regs;
  203. struct eqos_mac_regs *mac_regs;
  204. struct eqos_mtl_regs *mtl_regs;
  205. struct eqos_dma_regs *dma_regs;
  206. struct gpio_desc phy_reset_gpio;
  207. struct clk clk_master_bus;
  208. struct clk clk_rx;
  209. struct clk clk_ptp_ref;
  210. struct clk clk_tx;
  211. struct clk clk_slave_bus;
  212. struct mii_dev *mii;
  213. struct phy_device *phy;
  214. void *descs;
  215. struct eqos_desc *tx_descs;
  216. struct eqos_desc *rx_descs;
  217. int tx_desc_idx, rx_desc_idx;
  218. void *tx_dma_buf;
  219. void *rx_dma_buf;
  220. void *rx_pkt;
  221. bool started;
  222. bool reg_access_ok;
  223. };
  224. /*
  225. * TX and RX descriptors are 16 bytes. This causes problems with the cache
  226. * maintenance on CPUs where the cache-line size exceeds the size of these
  227. * descriptors. What will happen is that when the driver receives a packet
  228. * it will be immediately requeued for the hardware to reuse. The CPU will
  229. * therefore need to flush the cache-line containing the descriptor, which
  230. * will cause all other descriptors in the same cache-line to be flushed
  231. * along with it. If one of those descriptors had been written to by the
  232. * device those changes (and the associated packet) will be lost.
  233. *
  234. * To work around this, we make use of non-cached memory if available. If
  235. * descriptors are mapped uncached there's no need to manually flush them
  236. * or invalidate them.
  237. *
  238. * Note that this only applies to descriptors. The packet data buffers do
  239. * not have the same constraints since they are 1536 bytes large, so they
  240. * are unlikely to share cache-lines.
  241. */
  242. static void *eqos_alloc_descs(unsigned int num)
  243. {
  244. #ifdef CONFIG_SYS_NONCACHED_MEMORY
  245. return (void *)noncached_alloc(EQOS_DESCRIPTORS_SIZE,
  246. EQOS_DESCRIPTOR_ALIGN);
  247. #else
  248. return memalign(EQOS_DESCRIPTOR_ALIGN, EQOS_DESCRIPTORS_SIZE);
  249. #endif
  250. }
  251. static void eqos_free_descs(void *descs)
  252. {
  253. #ifdef CONFIG_SYS_NONCACHED_MEMORY
  254. /* FIXME: noncached_alloc() has no opposite */
  255. #else
  256. free(descs);
  257. #endif
  258. }
  259. static void eqos_inval_desc(void *desc)
  260. {
  261. #ifndef CONFIG_SYS_NONCACHED_MEMORY
  262. unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
  263. unsigned long end = ALIGN(start + EQOS_DESCRIPTOR_SIZE,
  264. ARCH_DMA_MINALIGN);
  265. invalidate_dcache_range(start, end);
  266. #endif
  267. }
  268. static void eqos_flush_desc(void *desc)
  269. {
  270. #ifndef CONFIG_SYS_NONCACHED_MEMORY
  271. flush_cache((unsigned long)desc, EQOS_DESCRIPTOR_SIZE);
  272. #endif
  273. }
  274. static void eqos_inval_buffer(void *buf, size_t size)
  275. {
  276. unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
  277. unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
  278. invalidate_dcache_range(start, end);
  279. }
  280. static void eqos_flush_buffer(void *buf, size_t size)
  281. {
  282. flush_cache((unsigned long)buf, size);
  283. }
  284. static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
  285. {
  286. return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
  287. EQOS_MAC_MDIO_ADDRESS_GB, false,
  288. 1000000, true);
  289. }
  290. static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
  291. int mdio_reg)
  292. {
  293. struct eqos_priv *eqos = bus->priv;
  294. u32 val;
  295. int ret;
  296. debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
  297. mdio_reg);
  298. ret = eqos_mdio_wait_idle(eqos);
  299. if (ret) {
  300. pr_err("MDIO not idle at entry");
  301. return ret;
  302. }
  303. val = readl(&eqos->mac_regs->mdio_address);
  304. val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
  305. EQOS_MAC_MDIO_ADDRESS_C45E;
  306. val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
  307. (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
  308. (EQOS_MAC_MDIO_ADDRESS_CR_20_35 <<
  309. EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
  310. (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
  311. EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
  312. EQOS_MAC_MDIO_ADDRESS_GB;
  313. writel(val, &eqos->mac_regs->mdio_address);
  314. udelay(10);
  315. ret = eqos_mdio_wait_idle(eqos);
  316. if (ret) {
  317. pr_err("MDIO read didn't complete");
  318. return ret;
  319. }
  320. val = readl(&eqos->mac_regs->mdio_data);
  321. val &= EQOS_MAC_MDIO_DATA_GD_MASK;
  322. debug("%s: val=%x\n", __func__, val);
  323. return val;
  324. }
  325. static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
  326. int mdio_reg, u16 mdio_val)
  327. {
  328. struct eqos_priv *eqos = bus->priv;
  329. u32 val;
  330. int ret;
  331. debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
  332. mdio_addr, mdio_reg, mdio_val);
  333. ret = eqos_mdio_wait_idle(eqos);
  334. if (ret) {
  335. pr_err("MDIO not idle at entry");
  336. return ret;
  337. }
  338. writel(mdio_val, &eqos->mac_regs->mdio_data);
  339. val = readl(&eqos->mac_regs->mdio_address);
  340. val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
  341. EQOS_MAC_MDIO_ADDRESS_C45E;
  342. val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
  343. (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
  344. (EQOS_MAC_MDIO_ADDRESS_CR_20_35 <<
  345. EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
  346. (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
  347. EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
  348. EQOS_MAC_MDIO_ADDRESS_GB;
  349. writel(val, &eqos->mac_regs->mdio_address);
  350. udelay(10);
  351. ret = eqos_mdio_wait_idle(eqos);
  352. if (ret) {
  353. pr_err("MDIO read didn't complete");
  354. return ret;
  355. }
  356. return 0;
  357. }
  358. static int eqos_start_clks_ark1668e(struct udevice *dev)
  359. {
  360. //struct eqos_priv *eqos = dev_get_priv(dev);
  361. int ret = 0;
  362. debug("%s: FAILED: %d\n", __func__, ret);
  363. return ret;
  364. }
  365. void eqos_stop_clks_ark1668e(struct udevice *dev)
  366. {
  367. //struct eqos_priv *eqos = dev_get_priv(dev);
  368. debug("%s(dev=%p):\n", __func__, dev);
  369. debug("%s: OK\n", __func__);
  370. }
  371. static int eqos_start_resets_ark1668e(struct udevice *dev)
  372. {
  373. struct eqos_priv *eqos = dev_get_priv(dev);
  374. int ret;
  375. debug("%s(dev=%p):\n", __func__, dev);
  376. /* ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
  377. if (ret < 0) {
  378. pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
  379. return ret;
  380. }
  381. udelay(2);
  382. ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
  383. if (ret < 0) {
  384. pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
  385. return ret;
  386. } */
  387. debug("%s: OK\n", __func__);
  388. return 0;
  389. }
  390. static int eqos_stop_resets_ark1668e(struct udevice *dev)
  391. {
  392. struct eqos_priv *eqos = dev_get_priv(dev);
  393. //dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
  394. return 0;
  395. }
  396. static ulong eqos_get_tick_clk_rate_ark1668e(struct udevice *dev)
  397. {
  398. struct eqos_priv *eqos = dev_get_priv(dev);
  399. return clk_get_rate(&eqos->clk_slave_bus);
  400. }
  401. static int eqos_set_full_duplex(struct udevice *dev)
  402. {
  403. struct eqos_priv *eqos = dev_get_priv(dev);
  404. debug("%s(dev=%p):\n", __func__, dev);
  405. setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
  406. return 0;
  407. }
  408. static int eqos_set_half_duplex(struct udevice *dev)
  409. {
  410. struct eqos_priv *eqos = dev_get_priv(dev);
  411. debug("%s(dev=%p):\n", __func__, dev);
  412. clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
  413. /* WAR: Flush TX queue when switching to half-duplex */
  414. setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
  415. EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
  416. return 0;
  417. }
  418. static int eqos_set_gmii_speed(struct udevice *dev)
  419. {
  420. struct eqos_priv *eqos = dev_get_priv(dev);
  421. debug("%s(dev=%p):\n", __func__, dev);
  422. clrbits_le32(&eqos->mac_regs->configuration,
  423. EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
  424. return 0;
  425. }
  426. static int eqos_set_mii_speed_100(struct udevice *dev)
  427. {
  428. struct eqos_priv *eqos = dev_get_priv(dev);
  429. debug("%s(dev=%p):\n", __func__, dev);
  430. setbits_le32(&eqos->mac_regs->configuration,
  431. EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
  432. return 0;
  433. }
  434. static int eqos_set_mii_speed_10(struct udevice *dev)
  435. {
  436. struct eqos_priv *eqos = dev_get_priv(dev);
  437. debug("%s(dev=%p):\n", __func__, dev);
  438. clrsetbits_le32(&eqos->mac_regs->configuration,
  439. EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
  440. return 0;
  441. }
  442. static int eqos_set_tx_clk_speed_ark1668e(struct udevice *dev)
  443. {
  444. //struct eqos_priv *eqos = dev_get_priv(dev);
  445. debug("%s(dev=%p):\n", __func__, dev);
  446. return 0;
  447. }
  448. static int eqos_adjust_link(struct udevice *dev)
  449. {
  450. struct eqos_priv *eqos = dev_get_priv(dev);
  451. int ret;
  452. debug("%s(dev=%p):\n", __func__, dev);
  453. if (eqos->phy->duplex)
  454. ret = eqos_set_full_duplex(dev);
  455. else
  456. ret = eqos_set_half_duplex(dev);
  457. if (ret < 0) {
  458. pr_err("eqos_set_*_duplex() failed: %d", ret);
  459. return ret;
  460. }
  461. switch (eqos->phy->speed) {
  462. case SPEED_1000:
  463. ret = eqos_set_gmii_speed(dev);
  464. break;
  465. case SPEED_100:
  466. ret = eqos_set_mii_speed_100(dev);
  467. break;
  468. case SPEED_10:
  469. ret = eqos_set_mii_speed_10(dev);
  470. break;
  471. default:
  472. pr_err("invalid speed %d", eqos->phy->speed);
  473. return -EINVAL;
  474. }
  475. if (ret < 0) {
  476. pr_err("eqos_set_*mii_speed*() failed: %d", ret);
  477. return ret;
  478. }
  479. ret = eqos_set_tx_clk_speed_ark1668e(dev);
  480. if (ret < 0) {
  481. pr_err("eqos_set_tx_clk_speed_ark1668e() failed: %d", ret);
  482. return ret;
  483. }
  484. return 0;
  485. }
  486. static int eqos_write_hwaddr(struct udevice *dev)
  487. {
  488. struct eth_pdata *plat = dev_get_platdata(dev);
  489. struct eqos_priv *eqos = dev_get_priv(dev);
  490. uint32_t val;
  491. /*
  492. * This function may be called before start() or after stop(). At that
  493. * time, on at least some configurations of the EQoS HW, all clocks to
  494. * the EQoS HW block will be stopped, and a reset signal applied. If
  495. * any register access is attempted in this state, bus timeouts or CPU
  496. * hangs may occur. This check prevents that.
  497. *
  498. * A simple solution to this problem would be to not implement
  499. * write_hwaddr(), since start() always writes the MAC address into HW
  500. * anyway. However, it is desirable to implement write_hwaddr() to
  501. * support the case of SW that runs subsequent to U-Boot which expects
  502. * the MAC address to already be programmed into the EQoS registers,
  503. * which must happen irrespective of whether the U-Boot user (or
  504. * scripts) actually made use of the EQoS device, and hence
  505. * irrespective of whether start() was ever called.
  506. *
  507. * Note that this requirement by subsequent SW is not valid for
  508. * Tegra186, and is likely not valid for any non-PCI instantiation of
  509. * the EQoS HW block. This function is implemented solely as
  510. * future-proofing with the expectation the driver will eventually be
  511. * ported to some system where the expectation above is true.
  512. */
  513. if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
  514. return 0;
  515. /* Update the MAC address */
  516. val = (plat->enetaddr[5] << 8) |
  517. (plat->enetaddr[4]);
  518. writel(val, &eqos->mac_regs->address0_high);
  519. val = (plat->enetaddr[3] << 24) |
  520. (plat->enetaddr[2] << 16) |
  521. (plat->enetaddr[1] << 8) |
  522. (plat->enetaddr[0]);
  523. writel(val, &eqos->mac_regs->address0_low);
  524. return 0;
  525. }
  526. static int eqos_start(struct udevice *dev)
  527. {
  528. struct eqos_priv *eqos = dev_get_priv(dev);
  529. int ret, i;
  530. ulong rate;
  531. u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
  532. ulong last_rx_desc;
  533. debug("%s(dev=%p):\n", __func__, dev);
  534. eqos->tx_desc_idx = 0;
  535. eqos->rx_desc_idx = 0;
  536. ret = eqos_start_clks_ark1668e(dev);
  537. if (ret < 0) {
  538. pr_err("eqos_start_clks_ark1668e() failed: %d", ret);
  539. goto err;
  540. }
  541. ret = eqos_start_resets_ark1668e(dev);
  542. if (ret < 0) {
  543. pr_err("eqos_start_resets_ark1668e() failed: %d", ret);
  544. goto err_stop_clks;
  545. }
  546. udelay(10);
  547. eqos->reg_access_ok = true;
  548. ret = wait_for_bit_le32(&eqos->dma_regs->mode,
  549. EQOS_DMA_MODE_SWR, false, 10, false);
  550. if (ret) {
  551. pr_err("EQOS_DMA_MODE_SWR stuck");
  552. goto err_stop_resets;
  553. }
  554. rate = eqos_get_tick_clk_rate_ark1668e(dev);
  555. val = (rate / 1000000) - 1;
  556. writel(val, &eqos->mac_regs->us_tic_counter);
  557. for (i = 0; i < 8; i++) {
  558. eqos->phy = phy_connect(eqos->mii, i, dev, 0);
  559. if (eqos->phy)
  560. break;
  561. }
  562. if (!eqos->phy) {
  563. pr_err("phy_connect() failed");
  564. goto err_stop_resets;
  565. }
  566. ret = phy_config(eqos->phy);
  567. if (ret < 0) {
  568. pr_err("phy_config() failed: %d", ret);
  569. goto err_shutdown_phy;
  570. }
  571. ret = phy_startup(eqos->phy);
  572. if (ret < 0) {
  573. pr_err("phy_startup() failed: %d", ret);
  574. goto err_shutdown_phy;
  575. }
  576. if (!eqos->phy->link) {
  577. pr_err("No link");
  578. goto err_shutdown_phy;
  579. }
  580. ret = eqos_adjust_link(dev);
  581. if (ret < 0) {
  582. pr_err("eqos_adjust_link() failed: %d", ret);
  583. goto err_shutdown_phy;
  584. }
  585. /* Configure MTL */
  586. /* Enable Store and Forward mode for TX */
  587. /* Program Tx operating mode */
  588. setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
  589. EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
  590. (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
  591. EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
  592. /* Transmit Queue weight */
  593. writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
  594. /* Enable Store and Forward mode for RX, since no jumbo frame */
  595. setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
  596. EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
  597. /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
  598. val = readl(&eqos->mac_regs->hw_feature1);
  599. tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
  600. EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
  601. rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
  602. EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
  603. /*
  604. * r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting.
  605. * r/tqs is encoded as (n / 256) - 1.
  606. */
  607. tqs = (128 << tx_fifo_sz) / 256 - 1;
  608. rqs = (128 << rx_fifo_sz) / 256 - 1;
  609. clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
  610. EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
  611. EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
  612. tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
  613. clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
  614. EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
  615. EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
  616. rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
  617. /* Flow control used only if each channel gets 4KB or more FIFO */
  618. if (rqs >= ((4096 / 256) - 1)) {
  619. u32 rfd, rfa;
  620. setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
  621. EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
  622. /*
  623. * Set Threshold for Activating Flow Contol space for min 2
  624. * frames ie, (1500 * 1) = 1500 bytes.
  625. *
  626. * Set Threshold for Deactivating Flow Contol for space of
  627. * min 1 frame (frame size 1500bytes) in receive fifo
  628. */
  629. if (rqs == ((4096 / 256) - 1)) {
  630. /*
  631. * This violates the above formula because of FIFO size
  632. * limit therefore overflow may occur inspite of this.
  633. */
  634. rfd = 0x3; /* Full-3K */
  635. rfa = 0x1; /* Full-1.5K */
  636. } else if (rqs == ((8192 / 256) - 1)) {
  637. rfd = 0x6; /* Full-4K */
  638. rfa = 0xa; /* Full-6K */
  639. } else if (rqs == ((16384 / 256) - 1)) {
  640. rfd = 0x6; /* Full-4K */
  641. rfa = 0x12; /* Full-10K */
  642. } else {
  643. rfd = 0x6; /* Full-4K */
  644. rfa = 0x1E; /* Full-16K */
  645. }
  646. clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
  647. (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
  648. EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
  649. (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
  650. EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
  651. (rfd <<
  652. EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
  653. (rfa <<
  654. EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
  655. }
  656. /* Configure MAC */
  657. clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
  658. EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
  659. EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
  660. EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB <<
  661. EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
  662. /* Set TX flow control parameters */
  663. /* Set Pause Time */
  664. setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
  665. 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
  666. /* Assign priority for TX flow control */
  667. clrbits_le32(&eqos->mac_regs->txq_prty_map0,
  668. EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
  669. EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
  670. /* Assign priority for RX flow control */
  671. clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
  672. EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
  673. EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
  674. /* Enable flow control */
  675. setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
  676. EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
  677. setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
  678. EQOS_MAC_RX_FLOW_CTRL_RFE);
  679. clrsetbits_le32(&eqos->mac_regs->configuration,
  680. EQOS_MAC_CONFIGURATION_GPSLCE |
  681. EQOS_MAC_CONFIGURATION_WD |
  682. EQOS_MAC_CONFIGURATION_JD |
  683. EQOS_MAC_CONFIGURATION_JE,
  684. EQOS_MAC_CONFIGURATION_CST |
  685. EQOS_MAC_CONFIGURATION_ACS);
  686. eqos_write_hwaddr(dev);
  687. /* Configure DMA */
  688. /* Enable OSP mode */
  689. setbits_le32(&eqos->dma_regs->ch0_tx_control,
  690. EQOS_DMA_CH0_TX_CONTROL_OSP);
  691. /* RX buffer size. Must be a multiple of bus width */
  692. clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
  693. EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
  694. EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
  695. EQOS_MAX_PACKET_SIZE <<
  696. EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
  697. setbits_le32(&eqos->dma_regs->ch0_control,
  698. EQOS_DMA_CH0_CONTROL_PBLX8);
  699. /*
  700. * Burst length must be < 1/2 FIFO size.
  701. * FIFO size in tqs is encoded as (n / 256) - 1.
  702. * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
  703. * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
  704. */
  705. pbl = tqs + 1;
  706. if (pbl > 32)
  707. pbl = 32;
  708. clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
  709. EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
  710. EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
  711. pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
  712. clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
  713. EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
  714. EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
  715. 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
  716. /* DMA performance configuration */
  717. val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
  718. EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
  719. EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
  720. writel(val, &eqos->dma_regs->sysbus_mode);
  721. /* Set up descriptors */
  722. memset(eqos->descs, 0, EQOS_DESCRIPTORS_SIZE);
  723. for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
  724. struct eqos_desc *rx_desc = &(eqos->rx_descs[i]);
  725. rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
  726. (i * EQOS_MAX_PACKET_SIZE));
  727. rx_desc->des3 |= EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
  728. }
  729. #ifndef CONFIG_SYS_NONCACHED_MEMORY
  730. flush_cache((unsigned long)eqos->descs, EQOS_DESCRIPTORS_SIZE);
  731. #endif
  732. writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
  733. writel((ulong)eqos->tx_descs, &eqos->dma_regs->ch0_txdesc_list_address);
  734. writel(EQOS_DESCRIPTORS_TX - 1,
  735. &eqos->dma_regs->ch0_txdesc_ring_length);
  736. writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
  737. writel((ulong)eqos->rx_descs, &eqos->dma_regs->ch0_rxdesc_list_address);
  738. writel(EQOS_DESCRIPTORS_RX - 1,
  739. &eqos->dma_regs->ch0_rxdesc_ring_length);
  740. /* Enable everything */
  741. setbits_le32(&eqos->mac_regs->configuration,
  742. EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
  743. setbits_le32(&eqos->dma_regs->ch0_tx_control,
  744. EQOS_DMA_CH0_TX_CONTROL_ST);
  745. setbits_le32(&eqos->dma_regs->ch0_rx_control,
  746. EQOS_DMA_CH0_RX_CONTROL_SR);
  747. /* TX tail pointer not written until we need to TX a packet */
  748. /*
  749. * Point RX tail pointer at last descriptor. Ideally, we'd point at the
  750. * first descriptor, implying all descriptors were available. However,
  751. * that's not distinguishable from none of the descriptors being
  752. * available.
  753. */
  754. last_rx_desc = (ulong)&(eqos->rx_descs[(EQOS_DESCRIPTORS_RX - 1)]);
  755. writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
  756. eqos->started = true;
  757. debug("%s: OK\n", __func__);
  758. return 0;
  759. err_shutdown_phy:
  760. phy_shutdown(eqos->phy);
  761. eqos->phy = NULL;
  762. err_stop_resets:
  763. eqos_stop_resets_ark1668e(dev);
  764. err_stop_clks:
  765. eqos_stop_clks_ark1668e(dev);
  766. err:
  767. pr_err("FAILED: %d", ret);
  768. return ret;
  769. }
  770. void eqos_stop(struct udevice *dev)
  771. {
  772. struct eqos_priv *eqos = dev_get_priv(dev);
  773. int i;
  774. debug("%s(dev=%p):\n", __func__, dev);
  775. if (!eqos->started)
  776. return;
  777. eqos->started = false;
  778. eqos->reg_access_ok = false;
  779. /* Disable TX DMA */
  780. clrbits_le32(&eqos->dma_regs->ch0_tx_control,
  781. EQOS_DMA_CH0_TX_CONTROL_ST);
  782. /* Wait for TX all packets to drain out of MTL */
  783. for (i = 0; i < 1000000; i++) {
  784. u32 val = readl(&eqos->mtl_regs->txq0_debug);
  785. u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
  786. EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
  787. u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
  788. if ((trcsts != 1) && (!txqsts))
  789. break;
  790. }
  791. /* Turn off MAC TX and RX */
  792. clrbits_le32(&eqos->mac_regs->configuration,
  793. EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
  794. /* Wait for all RX packets to drain out of MTL */
  795. for (i = 0; i < 1000000; i++) {
  796. u32 val = readl(&eqos->mtl_regs->rxq0_debug);
  797. u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
  798. EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
  799. u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
  800. EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
  801. if ((!prxq) && (!rxqsts))
  802. break;
  803. }
  804. /* Turn off RX DMA */
  805. clrbits_le32(&eqos->dma_regs->ch0_rx_control,
  806. EQOS_DMA_CH0_RX_CONTROL_SR);
  807. if (eqos->phy) {
  808. phy_shutdown(eqos->phy);
  809. eqos->phy = NULL;
  810. }
  811. eqos_stop_resets_ark1668e(dev);
  812. eqos_stop_clks_ark1668e(dev);
  813. debug("%s: OK\n", __func__);
  814. }
  815. int eqos_send(struct udevice *dev, void *packet, int length)
  816. {
  817. struct eqos_priv *eqos = dev_get_priv(dev);
  818. struct eqos_desc *tx_desc;
  819. int i;
  820. debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
  821. length);
  822. memcpy(eqos->tx_dma_buf, packet, length);
  823. eqos_flush_buffer(eqos->tx_dma_buf, length);
  824. tx_desc = &(eqos->tx_descs[eqos->tx_desc_idx]);
  825. eqos->tx_desc_idx++;
  826. eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
  827. tx_desc->des0 = (ulong)eqos->tx_dma_buf;
  828. tx_desc->des1 = 0;
  829. tx_desc->des2 = length;
  830. /*
  831. * Make sure that if HW sees the _OWN write below, it will see all the
  832. * writes to the rest of the descriptor too.
  833. */
  834. mb();
  835. tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
  836. eqos_flush_desc(tx_desc);
  837. writel((ulong)(tx_desc + 1), &eqos->dma_regs->ch0_txdesc_tail_pointer);
  838. for (i = 0; i < 1000000; i++) {
  839. eqos_inval_desc(tx_desc);
  840. if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
  841. return 0;
  842. udelay(1);
  843. }
  844. debug("%s: TX timeout\n", __func__);
  845. return -ETIMEDOUT;
  846. }
  847. int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
  848. {
  849. struct eqos_priv *eqos = dev_get_priv(dev);
  850. struct eqos_desc *rx_desc;
  851. int length;
  852. debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
  853. rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
  854. if (rx_desc->des3 & EQOS_DESC3_OWN) {
  855. debug("%s: RX packet not available\n", __func__);
  856. return -EAGAIN;
  857. }
  858. *packetp = eqos->rx_dma_buf +
  859. (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
  860. length = rx_desc->des3 & 0x7fff;
  861. debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
  862. eqos_inval_buffer(*packetp, length);
  863. return length;
  864. }
  865. int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
  866. {
  867. struct eqos_priv *eqos = dev_get_priv(dev);
  868. uchar *packet_expected;
  869. struct eqos_desc *rx_desc;
  870. debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
  871. packet_expected = eqos->rx_dma_buf +
  872. (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
  873. if (packet != packet_expected) {
  874. debug("%s: Unexpected packet (expected %p)\n", __func__,
  875. packet_expected);
  876. return -EINVAL;
  877. }
  878. rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
  879. rx_desc->des0 = (u32)(ulong)packet;
  880. rx_desc->des1 = 0;
  881. rx_desc->des2 = 0;
  882. /*
  883. * Make sure that if HW sees the _OWN write below, it will see all the
  884. * writes to the rest of the descriptor too.
  885. */
  886. mb();
  887. rx_desc->des3 |= EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
  888. eqos_flush_desc(rx_desc);
  889. writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
  890. eqos->rx_desc_idx++;
  891. eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
  892. return 0;
  893. }
  894. static int eqos_probe_resources_core(struct udevice *dev)
  895. {
  896. struct eqos_priv *eqos = dev_get_priv(dev);
  897. int ret;
  898. debug("%s(dev=%p):\n", __func__, dev);
  899. eqos->descs = eqos_alloc_descs(EQOS_DESCRIPTORS_TX +
  900. EQOS_DESCRIPTORS_RX);
  901. if (!eqos->descs) {
  902. debug("%s: eqos_alloc_descs() failed\n", __func__);
  903. ret = -ENOMEM;
  904. goto err;
  905. }
  906. eqos->tx_descs = (struct eqos_desc *)eqos->descs;
  907. eqos->rx_descs = (eqos->tx_descs + EQOS_DESCRIPTORS_TX);
  908. debug("%s: tx_descs=%p, rx_descs=%p\n", __func__, eqos->tx_descs,
  909. eqos->rx_descs);
  910. eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
  911. if (!eqos->tx_dma_buf) {
  912. debug("%s: memalign(tx_dma_buf) failed\n", __func__);
  913. ret = -ENOMEM;
  914. goto err_free_descs;
  915. }
  916. debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
  917. eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
  918. if (!eqos->rx_dma_buf) {
  919. debug("%s: memalign(rx_dma_buf) failed\n", __func__);
  920. ret = -ENOMEM;
  921. goto err_free_tx_dma_buf;
  922. }
  923. debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
  924. eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
  925. if (!eqos->rx_pkt) {
  926. debug("%s: malloc(rx_pkt) failed\n", __func__);
  927. ret = -ENOMEM;
  928. goto err_free_rx_dma_buf;
  929. }
  930. debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
  931. debug("%s: OK\n", __func__);
  932. return 0;
  933. err_free_rx_dma_buf:
  934. free(eqos->rx_dma_buf);
  935. err_free_tx_dma_buf:
  936. free(eqos->tx_dma_buf);
  937. err_free_descs:
  938. eqos_free_descs(eqos->descs);
  939. err:
  940. debug("%s: returns %d\n", __func__, ret);
  941. return ret;
  942. }
  943. static int eqos_remove_resources_core(struct udevice *dev)
  944. {
  945. struct eqos_priv *eqos = dev_get_priv(dev);
  946. debug("%s(dev=%p):\n", __func__, dev);
  947. free(eqos->rx_pkt);
  948. free(eqos->rx_dma_buf);
  949. free(eqos->tx_dma_buf);
  950. eqos_free_descs(eqos->descs);
  951. debug("%s: OK\n", __func__);
  952. return 0;
  953. }
  954. static int eqos_probe_resources_ark1668e(struct udevice *dev)
  955. {
  956. struct eqos_priv *eqos = dev_get_priv(dev);
  957. int ret;
  958. debug("%s(dev=%p):\n", __func__, dev);
  959. /* ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
  960. &eqos->phy_reset_gpio,
  961. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  962. if (ret) {
  963. pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
  964. goto err_free_reset_eqos;
  965. } */
  966. ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
  967. if (ret) {
  968. pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
  969. goto err_free_gpio_phy_reset;
  970. }
  971. ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
  972. if (ret) {
  973. pr_err("clk_get_by_name(master_bus) failed: %d", ret);
  974. goto err_free_clk_slave_bus;
  975. }
  976. ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
  977. if (ret) {
  978. pr_err("clk_get_by_name(rx) failed: %d", ret);
  979. goto err_free_clk_master_bus;
  980. }
  981. ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
  982. if (ret) {
  983. pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
  984. goto err_free_clk_rx;
  985. return ret;
  986. }
  987. ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
  988. if (ret) {
  989. pr_err("clk_get_by_name(tx) failed: %d", ret);
  990. goto err_free_clk_ptp_ref;
  991. }
  992. debug("%s: OK\n", __func__);
  993. return 0;
  994. err_free_clk_ptp_ref:
  995. clk_free(&eqos->clk_ptp_ref);
  996. err_free_clk_rx:
  997. clk_free(&eqos->clk_rx);
  998. err_free_clk_master_bus:
  999. clk_free(&eqos->clk_master_bus);
  1000. err_free_clk_slave_bus:
  1001. clk_free(&eqos->clk_slave_bus);
  1002. err_free_gpio_phy_reset:
  1003. //dm_gpio_free(dev, &eqos->phy_reset_gpio);
  1004. err_free_reset_eqos:
  1005. debug("%s: returns %d\n", __func__, ret);
  1006. return ret;
  1007. }
  1008. static int eqos_remove_resources_ark1668e(struct udevice *dev)
  1009. {
  1010. struct eqos_priv *eqos = dev_get_priv(dev);
  1011. debug("%s(dev=%p):\n", __func__, dev);
  1012. clk_free(&eqos->clk_tx);
  1013. clk_free(&eqos->clk_ptp_ref);
  1014. clk_free(&eqos->clk_rx);
  1015. clk_free(&eqos->clk_slave_bus);
  1016. clk_free(&eqos->clk_master_bus);
  1017. //dm_gpio_free(dev, &eqos->phy_reset_gpio);
  1018. debug("%s: OK\n", __func__);
  1019. return 0;
  1020. }
  1021. static int eqos_probe(struct udevice *dev)
  1022. {
  1023. struct eqos_priv *eqos = dev_get_priv(dev);
  1024. int ret;
  1025. debug("%s(dev=%p):\n", __func__, dev);
  1026. eqos->dev = dev;
  1027. eqos->config = (void *)dev_get_driver_data(dev);
  1028. eqos->regs = devfdt_get_addr(dev);
  1029. if (eqos->regs == FDT_ADDR_T_NONE) {
  1030. pr_err("devfdt_get_addr() failed");
  1031. return -ENODEV;
  1032. }
  1033. eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
  1034. eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
  1035. eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
  1036. ret = eqos_probe_resources_core(dev);
  1037. if (ret < 0) {
  1038. pr_err("eqos_probe_resources_core() failed: %d", ret);
  1039. return ret;
  1040. }
  1041. ret = eqos_probe_resources_ark1668e(dev);
  1042. if (ret < 0) {
  1043. pr_err("eqos_probe_resources_ark1668e() failed: %d", ret);
  1044. goto err_remove_resources_core;
  1045. }
  1046. eqos->mii = mdio_alloc();
  1047. if (!eqos->mii) {
  1048. pr_err("mdio_alloc() failed");
  1049. goto err_remove_resources_tegra;
  1050. }
  1051. eqos->mii->read = eqos_mdio_read;
  1052. eqos->mii->write = eqos_mdio_write;
  1053. eqos->mii->priv = eqos;
  1054. strcpy(eqos->mii->name, dev->name);
  1055. ret = mdio_register(eqos->mii);
  1056. if (ret < 0) {
  1057. pr_err("mdio_register() failed: %d", ret);
  1058. goto err_free_mdio;
  1059. }
  1060. debug("%s: OK\n", __func__);
  1061. return 0;
  1062. err_free_mdio:
  1063. mdio_free(eqos->mii);
  1064. err_remove_resources_tegra:
  1065. eqos_remove_resources_ark1668e(dev);
  1066. err_remove_resources_core:
  1067. eqos_remove_resources_core(dev);
  1068. debug("%s: returns %d\n", __func__, ret);
  1069. return ret;
  1070. }
  1071. static int eqos_remove(struct udevice *dev)
  1072. {
  1073. struct eqos_priv *eqos = dev_get_priv(dev);
  1074. debug("%s(dev=%p):\n", __func__, dev);
  1075. mdio_unregister(eqos->mii);
  1076. mdio_free(eqos->mii);
  1077. eqos_remove_resources_ark1668e(dev);
  1078. eqos_probe_resources_core(dev);
  1079. debug("%s: OK\n", __func__);
  1080. return 0;
  1081. }
  1082. static const struct eth_ops eqos_ops = {
  1083. .start = eqos_start,
  1084. .stop = eqos_stop,
  1085. .send = eqos_send,
  1086. .recv = eqos_recv,
  1087. .free_pkt = eqos_free_pkt,
  1088. .write_hwaddr = eqos_write_hwaddr,
  1089. };
  1090. static const struct eqos_config eqos_ark1668e_config = {
  1091. .reg_access_always_ok = false,
  1092. };
  1093. static const struct udevice_id eqos_ids[] = {
  1094. {
  1095. .compatible = "arkmicro,ark1668e-eqos",
  1096. .data = (ulong)&eqos_ark1668e_config
  1097. },
  1098. { }
  1099. };
  1100. U_BOOT_DRIVER(eth_eqos) = {
  1101. .name = "eth_eqos",
  1102. .id = UCLASS_ETH,
  1103. .of_match = eqos_ids,
  1104. .probe = eqos_probe,
  1105. .remove = eqos_remove,
  1106. .ops = &eqos_ops,
  1107. .priv_auto_alloc_size = sizeof(struct eqos_priv),
  1108. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  1109. };