dwc_eth_qos.c 42 KB

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