dwc_eth_qos.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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(__func__, &eqos->mac_regs->mdio_address,
  321. EQOS_MAC_MDIO_ADDRESS_GB, false, 1000000, true);
  322. }
  323. static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
  324. int mdio_reg)
  325. {
  326. struct eqos_priv *eqos = bus->priv;
  327. u32 val;
  328. int ret;
  329. debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
  330. mdio_reg);
  331. ret = eqos_mdio_wait_idle(eqos);
  332. if (ret) {
  333. error("MDIO not idle at entry");
  334. return ret;
  335. }
  336. val = readl(&eqos->mac_regs->mdio_address);
  337. val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
  338. EQOS_MAC_MDIO_ADDRESS_C45E;
  339. val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
  340. (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
  341. (EQOS_MAC_MDIO_ADDRESS_CR_20_35 <<
  342. EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
  343. (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
  344. EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
  345. EQOS_MAC_MDIO_ADDRESS_GB;
  346. writel(val, &eqos->mac_regs->mdio_address);
  347. udelay(10);
  348. ret = eqos_mdio_wait_idle(eqos);
  349. if (ret) {
  350. error("MDIO read didn't complete");
  351. return ret;
  352. }
  353. val = readl(&eqos->mac_regs->mdio_data);
  354. val &= EQOS_MAC_MDIO_DATA_GD_MASK;
  355. debug("%s: val=%x\n", __func__, val);
  356. return val;
  357. }
  358. static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
  359. int mdio_reg, u16 mdio_val)
  360. {
  361. struct eqos_priv *eqos = bus->priv;
  362. u32 val;
  363. int ret;
  364. debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
  365. mdio_addr, mdio_reg, mdio_val);
  366. ret = eqos_mdio_wait_idle(eqos);
  367. if (ret) {
  368. error("MDIO not idle at entry");
  369. return ret;
  370. }
  371. writel(mdio_val, &eqos->mac_regs->mdio_data);
  372. val = readl(&eqos->mac_regs->mdio_address);
  373. val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
  374. EQOS_MAC_MDIO_ADDRESS_C45E;
  375. val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
  376. (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
  377. (EQOS_MAC_MDIO_ADDRESS_CR_20_35 <<
  378. EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
  379. (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
  380. EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
  381. EQOS_MAC_MDIO_ADDRESS_GB;
  382. writel(val, &eqos->mac_regs->mdio_address);
  383. udelay(10);
  384. ret = eqos_mdio_wait_idle(eqos);
  385. if (ret) {
  386. error("MDIO read didn't complete");
  387. return ret;
  388. }
  389. return 0;
  390. }
  391. static int eqos_start_clks_tegra186(struct udevice *dev)
  392. {
  393. struct eqos_priv *eqos = dev_get_priv(dev);
  394. int ret;
  395. debug("%s(dev=%p):\n", __func__, dev);
  396. ret = clk_enable(&eqos->clk_slave_bus);
  397. if (ret < 0) {
  398. error("clk_enable(clk_slave_bus) failed: %d", ret);
  399. goto err;
  400. }
  401. ret = clk_enable(&eqos->clk_master_bus);
  402. if (ret < 0) {
  403. error("clk_enable(clk_master_bus) failed: %d", ret);
  404. goto err_disable_clk_slave_bus;
  405. }
  406. ret = clk_enable(&eqos->clk_rx);
  407. if (ret < 0) {
  408. error("clk_enable(clk_rx) failed: %d", ret);
  409. goto err_disable_clk_master_bus;
  410. }
  411. ret = clk_enable(&eqos->clk_ptp_ref);
  412. if (ret < 0) {
  413. error("clk_enable(clk_ptp_ref) failed: %d", ret);
  414. goto err_disable_clk_rx;
  415. }
  416. ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
  417. if (ret < 0) {
  418. error("clk_set_rate(clk_ptp_ref) failed: %d", ret);
  419. goto err_disable_clk_ptp_ref;
  420. }
  421. ret = clk_enable(&eqos->clk_tx);
  422. if (ret < 0) {
  423. error("clk_enable(clk_tx) failed: %d", ret);
  424. goto err_disable_clk_ptp_ref;
  425. }
  426. debug("%s: OK\n", __func__);
  427. return 0;
  428. err_disable_clk_ptp_ref:
  429. clk_disable(&eqos->clk_ptp_ref);
  430. err_disable_clk_rx:
  431. clk_disable(&eqos->clk_rx);
  432. err_disable_clk_master_bus:
  433. clk_disable(&eqos->clk_master_bus);
  434. err_disable_clk_slave_bus:
  435. clk_disable(&eqos->clk_slave_bus);
  436. err:
  437. debug("%s: FAILED: %d\n", __func__, ret);
  438. return ret;
  439. }
  440. void eqos_stop_clks_tegra186(struct udevice *dev)
  441. {
  442. struct eqos_priv *eqos = dev_get_priv(dev);
  443. debug("%s(dev=%p):\n", __func__, dev);
  444. clk_disable(&eqos->clk_tx);
  445. clk_disable(&eqos->clk_ptp_ref);
  446. clk_disable(&eqos->clk_rx);
  447. clk_disable(&eqos->clk_master_bus);
  448. clk_disable(&eqos->clk_slave_bus);
  449. debug("%s: OK\n", __func__);
  450. }
  451. static int eqos_start_resets_tegra186(struct udevice *dev)
  452. {
  453. struct eqos_priv *eqos = dev_get_priv(dev);
  454. int ret;
  455. debug("%s(dev=%p):\n", __func__, dev);
  456. ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
  457. if (ret < 0) {
  458. error("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
  459. return ret;
  460. }
  461. udelay(2);
  462. ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
  463. if (ret < 0) {
  464. error("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
  465. return ret;
  466. }
  467. ret = reset_assert(&eqos->reset_ctl);
  468. if (ret < 0) {
  469. error("reset_assert() failed: %d", ret);
  470. return ret;
  471. }
  472. udelay(2);
  473. ret = reset_deassert(&eqos->reset_ctl);
  474. if (ret < 0) {
  475. error("reset_deassert() failed: %d", ret);
  476. return ret;
  477. }
  478. debug("%s: OK\n", __func__);
  479. return 0;
  480. }
  481. static int eqos_stop_resets_tegra186(struct udevice *dev)
  482. {
  483. struct eqos_priv *eqos = dev_get_priv(dev);
  484. reset_assert(&eqos->reset_ctl);
  485. dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
  486. return 0;
  487. }
  488. static int eqos_calibrate_pads_tegra186(struct udevice *dev)
  489. {
  490. struct eqos_priv *eqos = dev_get_priv(dev);
  491. int ret;
  492. debug("%s(dev=%p):\n", __func__, dev);
  493. setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
  494. EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
  495. udelay(1);
  496. setbits_le32(&eqos->tegra186_regs->auto_cal_config,
  497. EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
  498. ret = wait_for_bit(__func__, &eqos->tegra186_regs->auto_cal_status,
  499. EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
  500. if (ret) {
  501. error("calibrate didn't start");
  502. goto failed;
  503. }
  504. ret = wait_for_bit(__func__, &eqos->tegra186_regs->auto_cal_status,
  505. EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
  506. if (ret) {
  507. error("calibrate didn't finish");
  508. goto failed;
  509. }
  510. ret = 0;
  511. failed:
  512. clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
  513. EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
  514. debug("%s: returns %d\n", __func__, ret);
  515. return ret;
  516. }
  517. static int eqos_disable_calibration_tegra186(struct udevice *dev)
  518. {
  519. struct eqos_priv *eqos = dev_get_priv(dev);
  520. debug("%s(dev=%p):\n", __func__, dev);
  521. clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
  522. EQOS_AUTO_CAL_CONFIG_ENABLE);
  523. return 0;
  524. }
  525. static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
  526. {
  527. struct eqos_priv *eqos = dev_get_priv(dev);
  528. return clk_get_rate(&eqos->clk_slave_bus);
  529. }
  530. static int eqos_set_full_duplex(struct udevice *dev)
  531. {
  532. struct eqos_priv *eqos = dev_get_priv(dev);
  533. debug("%s(dev=%p):\n", __func__, dev);
  534. setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
  535. return 0;
  536. }
  537. static int eqos_set_half_duplex(struct udevice *dev)
  538. {
  539. struct eqos_priv *eqos = dev_get_priv(dev);
  540. debug("%s(dev=%p):\n", __func__, dev);
  541. clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
  542. /* WAR: Flush TX queue when switching to half-duplex */
  543. setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
  544. EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
  545. return 0;
  546. }
  547. static int eqos_set_gmii_speed(struct udevice *dev)
  548. {
  549. struct eqos_priv *eqos = dev_get_priv(dev);
  550. debug("%s(dev=%p):\n", __func__, dev);
  551. clrbits_le32(&eqos->mac_regs->configuration,
  552. EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
  553. return 0;
  554. }
  555. static int eqos_set_mii_speed_100(struct udevice *dev)
  556. {
  557. struct eqos_priv *eqos = dev_get_priv(dev);
  558. debug("%s(dev=%p):\n", __func__, dev);
  559. setbits_le32(&eqos->mac_regs->configuration,
  560. EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
  561. return 0;
  562. }
  563. static int eqos_set_mii_speed_10(struct udevice *dev)
  564. {
  565. struct eqos_priv *eqos = dev_get_priv(dev);
  566. debug("%s(dev=%p):\n", __func__, dev);
  567. clrsetbits_le32(&eqos->mac_regs->configuration,
  568. EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
  569. return 0;
  570. }
  571. static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
  572. {
  573. struct eqos_priv *eqos = dev_get_priv(dev);
  574. ulong rate;
  575. int ret;
  576. debug("%s(dev=%p):\n", __func__, dev);
  577. switch (eqos->phy->speed) {
  578. case SPEED_1000:
  579. rate = 125 * 1000 * 1000;
  580. break;
  581. case SPEED_100:
  582. rate = 25 * 1000 * 1000;
  583. break;
  584. case SPEED_10:
  585. rate = 2.5 * 1000 * 1000;
  586. break;
  587. default:
  588. error("invalid speed %d", eqos->phy->speed);
  589. return -EINVAL;
  590. }
  591. ret = clk_set_rate(&eqos->clk_tx, rate);
  592. if (ret < 0) {
  593. error("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
  594. return ret;
  595. }
  596. return 0;
  597. }
  598. static int eqos_adjust_link(struct udevice *dev)
  599. {
  600. struct eqos_priv *eqos = dev_get_priv(dev);
  601. int ret;
  602. bool en_calibration;
  603. debug("%s(dev=%p):\n", __func__, dev);
  604. if (eqos->phy->duplex)
  605. ret = eqos_set_full_duplex(dev);
  606. else
  607. ret = eqos_set_half_duplex(dev);
  608. if (ret < 0) {
  609. error("eqos_set_*_duplex() failed: %d", ret);
  610. return ret;
  611. }
  612. switch (eqos->phy->speed) {
  613. case SPEED_1000:
  614. en_calibration = true;
  615. ret = eqos_set_gmii_speed(dev);
  616. break;
  617. case SPEED_100:
  618. en_calibration = true;
  619. ret = eqos_set_mii_speed_100(dev);
  620. break;
  621. case SPEED_10:
  622. en_calibration = false;
  623. ret = eqos_set_mii_speed_10(dev);
  624. break;
  625. default:
  626. error("invalid speed %d", eqos->phy->speed);
  627. return -EINVAL;
  628. }
  629. if (ret < 0) {
  630. error("eqos_set_*mii_speed*() failed: %d", ret);
  631. return ret;
  632. }
  633. if (en_calibration) {
  634. ret = eqos_calibrate_pads_tegra186(dev);
  635. if (ret < 0) {
  636. error("eqos_calibrate_pads_tegra186() failed: %d", ret);
  637. return ret;
  638. }
  639. } else {
  640. ret = eqos_disable_calibration_tegra186(dev);
  641. if (ret < 0) {
  642. error("eqos_disable_calibration_tegra186() failed: %d",
  643. ret);
  644. return ret;
  645. }
  646. }
  647. ret = eqos_set_tx_clk_speed_tegra186(dev);
  648. if (ret < 0) {
  649. error("eqos_set_tx_clk_speed_tegra186() failed: %d", ret);
  650. return ret;
  651. }
  652. return 0;
  653. }
  654. static int eqos_write_hwaddr(struct udevice *dev)
  655. {
  656. struct eth_pdata *plat = dev_get_platdata(dev);
  657. struct eqos_priv *eqos = dev_get_priv(dev);
  658. uint32_t val;
  659. /*
  660. * This function may be called before start() or after stop(). At that
  661. * time, on at least some configurations of the EQoS HW, all clocks to
  662. * the EQoS HW block will be stopped, and a reset signal applied. If
  663. * any register access is attempted in this state, bus timeouts or CPU
  664. * hangs may occur. This check prevents that.
  665. *
  666. * A simple solution to this problem would be to not implement
  667. * write_hwaddr(), since start() always writes the MAC address into HW
  668. * anyway. However, it is desirable to implement write_hwaddr() to
  669. * support the case of SW that runs subsequent to U-Boot which expects
  670. * the MAC address to already be programmed into the EQoS registers,
  671. * which must happen irrespective of whether the U-Boot user (or
  672. * scripts) actually made use of the EQoS device, and hence
  673. * irrespective of whether start() was ever called.
  674. *
  675. * Note that this requirement by subsequent SW is not valid for
  676. * Tegra186, and is likely not valid for any non-PCI instantiation of
  677. * the EQoS HW block. This function is implemented solely as
  678. * future-proofing with the expectation the driver will eventually be
  679. * ported to some system where the expectation above is true.
  680. */
  681. if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
  682. return 0;
  683. /* Update the MAC address */
  684. val = (plat->enetaddr[5] << 8) |
  685. (plat->enetaddr[4]);
  686. writel(val, &eqos->mac_regs->address0_high);
  687. val = (plat->enetaddr[3] << 24) |
  688. (plat->enetaddr[2] << 16) |
  689. (plat->enetaddr[1] << 8) |
  690. (plat->enetaddr[0]);
  691. writel(val, &eqos->mac_regs->address0_low);
  692. return 0;
  693. }
  694. static int eqos_start(struct udevice *dev)
  695. {
  696. struct eqos_priv *eqos = dev_get_priv(dev);
  697. int ret, i;
  698. ulong rate;
  699. u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
  700. ulong last_rx_desc;
  701. debug("%s(dev=%p):\n", __func__, dev);
  702. eqos->tx_desc_idx = 0;
  703. eqos->rx_desc_idx = 0;
  704. ret = eqos_start_clks_tegra186(dev);
  705. if (ret < 0) {
  706. error("eqos_start_clks_tegra186() failed: %d", ret);
  707. goto err;
  708. }
  709. ret = eqos_start_resets_tegra186(dev);
  710. if (ret < 0) {
  711. error("eqos_start_resets_tegra186() failed: %d", ret);
  712. goto err_stop_clks;
  713. }
  714. udelay(10);
  715. eqos->reg_access_ok = true;
  716. ret = wait_for_bit(__func__, &eqos->dma_regs->mode,
  717. EQOS_DMA_MODE_SWR, false, 10, false);
  718. if (ret) {
  719. error("EQOS_DMA_MODE_SWR stuck");
  720. goto err_stop_resets;
  721. }
  722. ret = eqos_calibrate_pads_tegra186(dev);
  723. if (ret < 0) {
  724. error("eqos_calibrate_pads_tegra186() failed: %d", ret);
  725. goto err_stop_resets;
  726. }
  727. rate = eqos_get_tick_clk_rate_tegra186(dev);
  728. val = (rate / 1000000) - 1;
  729. writel(val, &eqos->mac_regs->us_tic_counter);
  730. eqos->phy = phy_connect(eqos->mii, 0, dev, 0);
  731. if (!eqos->phy) {
  732. error("phy_connect() failed");
  733. goto err_stop_resets;
  734. }
  735. ret = phy_config(eqos->phy);
  736. if (ret < 0) {
  737. error("phy_config() failed: %d", ret);
  738. goto err_shutdown_phy;
  739. }
  740. ret = phy_startup(eqos->phy);
  741. if (ret < 0) {
  742. error("phy_startup() failed: %d", ret);
  743. goto err_shutdown_phy;
  744. }
  745. if (!eqos->phy->link) {
  746. error("No link");
  747. goto err_shutdown_phy;
  748. }
  749. ret = eqos_adjust_link(dev);
  750. if (ret < 0) {
  751. error("eqos_adjust_link() failed: %d", ret);
  752. goto err_shutdown_phy;
  753. }
  754. /* Configure MTL */
  755. /* Enable Store and Forward mode for TX */
  756. /* Program Tx operating mode */
  757. setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
  758. EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
  759. (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
  760. EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
  761. /* Transmit Queue weight */
  762. writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
  763. /* Enable Store and Forward mode for RX, since no jumbo frame */
  764. setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
  765. EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
  766. /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
  767. val = readl(&eqos->mac_regs->hw_feature1);
  768. tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
  769. EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
  770. rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
  771. EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
  772. /*
  773. * r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting.
  774. * r/tqs is encoded as (n / 256) - 1.
  775. */
  776. tqs = (128 << tx_fifo_sz) / 256 - 1;
  777. rqs = (128 << rx_fifo_sz) / 256 - 1;
  778. clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
  779. EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
  780. EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
  781. tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
  782. clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
  783. EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
  784. EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
  785. rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
  786. /* Flow control used only if each channel gets 4KB or more FIFO */
  787. if (rqs >= ((4096 / 256) - 1)) {
  788. u32 rfd, rfa;
  789. setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
  790. EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
  791. /*
  792. * Set Threshold for Activating Flow Contol space for min 2
  793. * frames ie, (1500 * 1) = 1500 bytes.
  794. *
  795. * Set Threshold for Deactivating Flow Contol for space of
  796. * min 1 frame (frame size 1500bytes) in receive fifo
  797. */
  798. if (rqs == ((4096 / 256) - 1)) {
  799. /*
  800. * This violates the above formula because of FIFO size
  801. * limit therefore overflow may occur inspite of this.
  802. */
  803. rfd = 0x3; /* Full-3K */
  804. rfa = 0x1; /* Full-1.5K */
  805. } else if (rqs == ((8192 / 256) - 1)) {
  806. rfd = 0x6; /* Full-4K */
  807. rfa = 0xa; /* Full-6K */
  808. } else if (rqs == ((16384 / 256) - 1)) {
  809. rfd = 0x6; /* Full-4K */
  810. rfa = 0x12; /* Full-10K */
  811. } else {
  812. rfd = 0x6; /* Full-4K */
  813. rfa = 0x1E; /* Full-16K */
  814. }
  815. clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
  816. (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
  817. EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
  818. (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
  819. EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
  820. (rfd <<
  821. EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
  822. (rfa <<
  823. EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
  824. }
  825. /* Configure MAC */
  826. clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
  827. EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
  828. EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
  829. EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB <<
  830. EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
  831. /* Set TX flow control parameters */
  832. /* Set Pause Time */
  833. setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
  834. 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
  835. /* Assign priority for TX flow control */
  836. clrbits_le32(&eqos->mac_regs->txq_prty_map0,
  837. EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
  838. EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
  839. /* Assign priority for RX flow control */
  840. clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
  841. EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
  842. EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
  843. /* Enable flow control */
  844. setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
  845. EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
  846. setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
  847. EQOS_MAC_RX_FLOW_CTRL_RFE);
  848. clrsetbits_le32(&eqos->mac_regs->configuration,
  849. EQOS_MAC_CONFIGURATION_GPSLCE |
  850. EQOS_MAC_CONFIGURATION_WD |
  851. EQOS_MAC_CONFIGURATION_JD |
  852. EQOS_MAC_CONFIGURATION_JE,
  853. EQOS_MAC_CONFIGURATION_CST |
  854. EQOS_MAC_CONFIGURATION_ACS);
  855. eqos_write_hwaddr(dev);
  856. /* Configure DMA */
  857. /* Enable OSP mode */
  858. setbits_le32(&eqos->dma_regs->ch0_tx_control,
  859. EQOS_DMA_CH0_TX_CONTROL_OSP);
  860. /* RX buffer size. Must be a multiple of bus width */
  861. clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
  862. EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
  863. EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
  864. EQOS_MAX_PACKET_SIZE <<
  865. EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
  866. setbits_le32(&eqos->dma_regs->ch0_control,
  867. EQOS_DMA_CH0_CONTROL_PBLX8);
  868. /*
  869. * Burst length must be < 1/2 FIFO size.
  870. * FIFO size in tqs is encoded as (n / 256) - 1.
  871. * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
  872. * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
  873. */
  874. pbl = tqs + 1;
  875. if (pbl > 32)
  876. pbl = 32;
  877. clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
  878. EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
  879. EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
  880. pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
  881. clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
  882. EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
  883. EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
  884. 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
  885. /* DMA performance configuration */
  886. val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
  887. EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
  888. EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
  889. writel(val, &eqos->dma_regs->sysbus_mode);
  890. /* Set up descriptors */
  891. memset(eqos->descs, 0, EQOS_DESCRIPTORS_SIZE);
  892. for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
  893. struct eqos_desc *rx_desc = &(eqos->rx_descs[i]);
  894. rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
  895. (i * EQOS_MAX_PACKET_SIZE));
  896. rx_desc->des3 |= EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
  897. }
  898. flush_cache((unsigned long)eqos->descs, EQOS_DESCRIPTORS_SIZE);
  899. writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
  900. writel((ulong)eqos->tx_descs, &eqos->dma_regs->ch0_txdesc_list_address);
  901. writel(EQOS_DESCRIPTORS_TX - 1,
  902. &eqos->dma_regs->ch0_txdesc_ring_length);
  903. writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
  904. writel((ulong)eqos->rx_descs, &eqos->dma_regs->ch0_rxdesc_list_address);
  905. writel(EQOS_DESCRIPTORS_RX - 1,
  906. &eqos->dma_regs->ch0_rxdesc_ring_length);
  907. /* Enable everything */
  908. setbits_le32(&eqos->mac_regs->configuration,
  909. EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
  910. setbits_le32(&eqos->dma_regs->ch0_tx_control,
  911. EQOS_DMA_CH0_TX_CONTROL_ST);
  912. setbits_le32(&eqos->dma_regs->ch0_rx_control,
  913. EQOS_DMA_CH0_RX_CONTROL_SR);
  914. /* TX tail pointer not written until we need to TX a packet */
  915. /*
  916. * Point RX tail pointer at last descriptor. Ideally, we'd point at the
  917. * first descriptor, implying all descriptors were available. However,
  918. * that's not distinguishable from none of the descriptors being
  919. * available.
  920. */
  921. last_rx_desc = (ulong)&(eqos->rx_descs[(EQOS_DESCRIPTORS_RX - 1)]);
  922. writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
  923. eqos->started = true;
  924. debug("%s: OK\n", __func__);
  925. return 0;
  926. err_shutdown_phy:
  927. phy_shutdown(eqos->phy);
  928. eqos->phy = NULL;
  929. err_stop_resets:
  930. eqos_stop_resets_tegra186(dev);
  931. err_stop_clks:
  932. eqos_stop_clks_tegra186(dev);
  933. err:
  934. error("FAILED: %d", ret);
  935. return ret;
  936. }
  937. void eqos_stop(struct udevice *dev)
  938. {
  939. struct eqos_priv *eqos = dev_get_priv(dev);
  940. int i;
  941. debug("%s(dev=%p):\n", __func__, dev);
  942. if (!eqos->started)
  943. return;
  944. eqos->started = false;
  945. eqos->reg_access_ok = false;
  946. /* Disable TX DMA */
  947. clrbits_le32(&eqos->dma_regs->ch0_tx_control,
  948. EQOS_DMA_CH0_TX_CONTROL_ST);
  949. /* Wait for TX all packets to drain out of MTL */
  950. for (i = 0; i < 1000000; i++) {
  951. u32 val = readl(&eqos->mtl_regs->txq0_debug);
  952. u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
  953. EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
  954. u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
  955. if ((trcsts != 1) && (!txqsts))
  956. break;
  957. }
  958. /* Turn off MAC TX and RX */
  959. clrbits_le32(&eqos->mac_regs->configuration,
  960. EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
  961. /* Wait for all RX packets to drain out of MTL */
  962. for (i = 0; i < 1000000; i++) {
  963. u32 val = readl(&eqos->mtl_regs->rxq0_debug);
  964. u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
  965. EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
  966. u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
  967. EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
  968. if ((!prxq) && (!rxqsts))
  969. break;
  970. }
  971. /* Turn off RX DMA */
  972. clrbits_le32(&eqos->dma_regs->ch0_rx_control,
  973. EQOS_DMA_CH0_RX_CONTROL_SR);
  974. if (eqos->phy) {
  975. phy_shutdown(eqos->phy);
  976. eqos->phy = NULL;
  977. }
  978. eqos_stop_resets_tegra186(dev);
  979. eqos_stop_clks_tegra186(dev);
  980. debug("%s: OK\n", __func__);
  981. }
  982. int eqos_send(struct udevice *dev, void *packet, int length)
  983. {
  984. struct eqos_priv *eqos = dev_get_priv(dev);
  985. struct eqos_desc *tx_desc;
  986. int i;
  987. debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
  988. length);
  989. memcpy(eqos->tx_dma_buf, packet, length);
  990. eqos_flush_buffer(eqos->tx_dma_buf, length);
  991. tx_desc = &(eqos->tx_descs[eqos->tx_desc_idx]);
  992. eqos->tx_desc_idx++;
  993. eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
  994. tx_desc->des0 = (ulong)eqos->tx_dma_buf;
  995. tx_desc->des1 = 0;
  996. tx_desc->des2 = length;
  997. /*
  998. * Make sure that if HW sees the _OWN write below, it will see all the
  999. * writes to the rest of the descriptor too.
  1000. */
  1001. mb();
  1002. tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
  1003. eqos_flush_desc(tx_desc);
  1004. writel((ulong)(tx_desc + 1), &eqos->dma_regs->ch0_txdesc_tail_pointer);
  1005. for (i = 0; i < 1000000; i++) {
  1006. eqos_inval_desc(tx_desc);
  1007. if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
  1008. return 0;
  1009. udelay(1);
  1010. }
  1011. debug("%s: TX timeout\n", __func__);
  1012. return -ETIMEDOUT;
  1013. }
  1014. int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
  1015. {
  1016. struct eqos_priv *eqos = dev_get_priv(dev);
  1017. struct eqos_desc *rx_desc;
  1018. int length;
  1019. debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
  1020. rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
  1021. if (rx_desc->des3 & EQOS_DESC3_OWN) {
  1022. debug("%s: RX packet not available\n", __func__);
  1023. return -EAGAIN;
  1024. }
  1025. *packetp = eqos->rx_dma_buf +
  1026. (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
  1027. length = rx_desc->des3 & 0x7fff;
  1028. debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
  1029. eqos_inval_buffer(*packetp, length);
  1030. return length;
  1031. }
  1032. int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
  1033. {
  1034. struct eqos_priv *eqos = dev_get_priv(dev);
  1035. uchar *packet_expected;
  1036. struct eqos_desc *rx_desc;
  1037. debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
  1038. packet_expected = eqos->rx_dma_buf +
  1039. (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
  1040. if (packet != packet_expected) {
  1041. debug("%s: Unexpected packet (expected %p)\n", __func__,
  1042. packet_expected);
  1043. return -EINVAL;
  1044. }
  1045. rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
  1046. rx_desc->des0 = (u32)(ulong)packet;
  1047. rx_desc->des1 = 0;
  1048. rx_desc->des2 = 0;
  1049. /*
  1050. * Make sure that if HW sees the _OWN write below, it will see all the
  1051. * writes to the rest of the descriptor too.
  1052. */
  1053. mb();
  1054. rx_desc->des3 |= EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
  1055. eqos_flush_desc(rx_desc);
  1056. writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
  1057. eqos->rx_desc_idx++;
  1058. eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
  1059. return 0;
  1060. }
  1061. static int eqos_probe_resources_core(struct udevice *dev)
  1062. {
  1063. struct eqos_priv *eqos = dev_get_priv(dev);
  1064. int ret;
  1065. debug("%s(dev=%p):\n", __func__, dev);
  1066. eqos->descs = eqos_alloc_descs(EQOS_DESCRIPTORS_TX +
  1067. EQOS_DESCRIPTORS_RX);
  1068. if (!eqos->descs) {
  1069. debug("%s: eqos_alloc_descs() failed\n", __func__);
  1070. ret = -ENOMEM;
  1071. goto err;
  1072. }
  1073. eqos->tx_descs = (struct eqos_desc *)eqos->descs;
  1074. eqos->rx_descs = (eqos->tx_descs + EQOS_DESCRIPTORS_TX);
  1075. debug("%s: tx_descs=%p, rx_descs=%p\n", __func__, eqos->tx_descs,
  1076. eqos->rx_descs);
  1077. eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
  1078. if (!eqos->tx_dma_buf) {
  1079. debug("%s: memalign(tx_dma_buf) failed\n", __func__);
  1080. ret = -ENOMEM;
  1081. goto err_free_descs;
  1082. }
  1083. debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
  1084. eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
  1085. if (!eqos->rx_dma_buf) {
  1086. debug("%s: memalign(rx_dma_buf) failed\n", __func__);
  1087. ret = -ENOMEM;
  1088. goto err_free_tx_dma_buf;
  1089. }
  1090. debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
  1091. eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
  1092. if (!eqos->rx_pkt) {
  1093. debug("%s: malloc(rx_pkt) failed\n", __func__);
  1094. ret = -ENOMEM;
  1095. goto err_free_rx_dma_buf;
  1096. }
  1097. debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
  1098. debug("%s: OK\n", __func__);
  1099. return 0;
  1100. err_free_rx_dma_buf:
  1101. free(eqos->rx_dma_buf);
  1102. err_free_tx_dma_buf:
  1103. free(eqos->tx_dma_buf);
  1104. err_free_descs:
  1105. eqos_free_descs(eqos->descs);
  1106. err:
  1107. debug("%s: returns %d\n", __func__, ret);
  1108. return ret;
  1109. }
  1110. static int eqos_remove_resources_core(struct udevice *dev)
  1111. {
  1112. struct eqos_priv *eqos = dev_get_priv(dev);
  1113. debug("%s(dev=%p):\n", __func__, dev);
  1114. free(eqos->rx_pkt);
  1115. free(eqos->rx_dma_buf);
  1116. free(eqos->tx_dma_buf);
  1117. eqos_free_descs(eqos->descs);
  1118. debug("%s: OK\n", __func__);
  1119. return 0;
  1120. }
  1121. static int eqos_probe_resources_tegra186(struct udevice *dev)
  1122. {
  1123. struct eqos_priv *eqos = dev_get_priv(dev);
  1124. int ret;
  1125. debug("%s(dev=%p):\n", __func__, dev);
  1126. ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
  1127. if (ret) {
  1128. error("reset_get_by_name(rst) failed: %d", ret);
  1129. return ret;
  1130. }
  1131. ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
  1132. &eqos->phy_reset_gpio,
  1133. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  1134. if (ret) {
  1135. error("gpio_request_by_name(phy reset) failed: %d", ret);
  1136. goto err_free_reset_eqos;
  1137. }
  1138. ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
  1139. if (ret) {
  1140. error("clk_get_by_name(slave_bus) failed: %d", ret);
  1141. goto err_free_gpio_phy_reset;
  1142. }
  1143. ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
  1144. if (ret) {
  1145. error("clk_get_by_name(master_bus) failed: %d", ret);
  1146. goto err_free_clk_slave_bus;
  1147. }
  1148. ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
  1149. if (ret) {
  1150. error("clk_get_by_name(rx) failed: %d", ret);
  1151. goto err_free_clk_master_bus;
  1152. }
  1153. ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
  1154. if (ret) {
  1155. error("clk_get_by_name(ptp_ref) failed: %d", ret);
  1156. goto err_free_clk_rx;
  1157. return ret;
  1158. }
  1159. ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
  1160. if (ret) {
  1161. error("clk_get_by_name(tx) failed: %d", ret);
  1162. goto err_free_clk_ptp_ref;
  1163. }
  1164. debug("%s: OK\n", __func__);
  1165. return 0;
  1166. err_free_clk_ptp_ref:
  1167. clk_free(&eqos->clk_ptp_ref);
  1168. err_free_clk_rx:
  1169. clk_free(&eqos->clk_rx);
  1170. err_free_clk_master_bus:
  1171. clk_free(&eqos->clk_master_bus);
  1172. err_free_clk_slave_bus:
  1173. clk_free(&eqos->clk_slave_bus);
  1174. err_free_gpio_phy_reset:
  1175. dm_gpio_free(dev, &eqos->phy_reset_gpio);
  1176. err_free_reset_eqos:
  1177. reset_free(&eqos->reset_ctl);
  1178. debug("%s: returns %d\n", __func__, ret);
  1179. return ret;
  1180. }
  1181. static int eqos_remove_resources_tegra186(struct udevice *dev)
  1182. {
  1183. struct eqos_priv *eqos = dev_get_priv(dev);
  1184. debug("%s(dev=%p):\n", __func__, dev);
  1185. clk_free(&eqos->clk_tx);
  1186. clk_free(&eqos->clk_ptp_ref);
  1187. clk_free(&eqos->clk_rx);
  1188. clk_free(&eqos->clk_slave_bus);
  1189. clk_free(&eqos->clk_master_bus);
  1190. dm_gpio_free(dev, &eqos->phy_reset_gpio);
  1191. reset_free(&eqos->reset_ctl);
  1192. debug("%s: OK\n", __func__);
  1193. return 0;
  1194. }
  1195. static int eqos_probe(struct udevice *dev)
  1196. {
  1197. struct eqos_priv *eqos = dev_get_priv(dev);
  1198. int ret;
  1199. debug("%s(dev=%p):\n", __func__, dev);
  1200. eqos->dev = dev;
  1201. eqos->config = (void *)dev_get_driver_data(dev);
  1202. eqos->regs = dev_get_addr(dev);
  1203. if (eqos->regs == FDT_ADDR_T_NONE) {
  1204. error("dev_get_addr() failed");
  1205. return -ENODEV;
  1206. }
  1207. eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
  1208. eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
  1209. eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
  1210. eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
  1211. ret = eqos_probe_resources_core(dev);
  1212. if (ret < 0) {
  1213. error("eqos_probe_resources_core() failed: %d", ret);
  1214. return ret;
  1215. }
  1216. ret = eqos_probe_resources_tegra186(dev);
  1217. if (ret < 0) {
  1218. error("eqos_probe_resources_tegra186() failed: %d", ret);
  1219. goto err_remove_resources_core;
  1220. }
  1221. eqos->mii = mdio_alloc();
  1222. if (!eqos->mii) {
  1223. error("mdio_alloc() failed");
  1224. goto err_remove_resources_tegra;
  1225. }
  1226. eqos->mii->read = eqos_mdio_read;
  1227. eqos->mii->write = eqos_mdio_write;
  1228. eqos->mii->priv = eqos;
  1229. strcpy(eqos->mii->name, dev->name);
  1230. ret = mdio_register(eqos->mii);
  1231. if (ret < 0) {
  1232. error("mdio_register() failed: %d", ret);
  1233. goto err_free_mdio;
  1234. }
  1235. debug("%s: OK\n", __func__);
  1236. return 0;
  1237. err_free_mdio:
  1238. mdio_free(eqos->mii);
  1239. err_remove_resources_tegra:
  1240. eqos_remove_resources_tegra186(dev);
  1241. err_remove_resources_core:
  1242. eqos_remove_resources_core(dev);
  1243. debug("%s: returns %d\n", __func__, ret);
  1244. return ret;
  1245. }
  1246. static int eqos_remove(struct udevice *dev)
  1247. {
  1248. struct eqos_priv *eqos = dev_get_priv(dev);
  1249. debug("%s(dev=%p):\n", __func__, dev);
  1250. mdio_unregister(eqos->mii);
  1251. mdio_free(eqos->mii);
  1252. eqos_remove_resources_tegra186(dev);
  1253. eqos_probe_resources_core(dev);
  1254. debug("%s: OK\n", __func__);
  1255. return 0;
  1256. }
  1257. static const struct eth_ops eqos_ops = {
  1258. .start = eqos_start,
  1259. .stop = eqos_stop,
  1260. .send = eqos_send,
  1261. .recv = eqos_recv,
  1262. .free_pkt = eqos_free_pkt,
  1263. .write_hwaddr = eqos_write_hwaddr,
  1264. };
  1265. static const struct eqos_config eqos_tegra186_config = {
  1266. .reg_access_always_ok = false,
  1267. };
  1268. static const struct udevice_id eqos_ids[] = {
  1269. {
  1270. .compatible = "nvidia,tegra186-eqos",
  1271. .data = (ulong)&eqos_tegra186_config
  1272. },
  1273. { }
  1274. };
  1275. U_BOOT_DRIVER(eth_eqos) = {
  1276. .name = "eth_eqos",
  1277. .id = UCLASS_ETH,
  1278. .of_match = eqos_ids,
  1279. .probe = eqos_probe,
  1280. .remove = eqos_remove,
  1281. .ops = &eqos_ops,
  1282. .priv_auto_alloc_size = sizeof(struct eqos_priv),
  1283. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  1284. };