]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
42d1f039 | 2 | /* |
97d80fc3 | 3 | * Freescale Three Speed Ethernet Controller driver |
42d1f039 | 4 | * |
aec84bf6 | 5 | * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc. |
42d1f039 | 6 | * (C) Copyright 2003, Motorola, Inc. |
42d1f039 | 7 | * author Andy Fleming |
42d1f039 WD |
8 | */ |
9 | ||
10 | #include <config.h> | |
9a1d6af5 | 11 | #include <dm.h> |
42d1f039 WD |
12 | #include <malloc.h> |
13 | #include <net.h> | |
14 | #include <command.h> | |
dd3d1f56 | 15 | #include <tsec.h> |
063c1263 | 16 | #include <fsl_mdio.h> |
cd93d625 | 17 | #include <linux/bitops.h> |
c05ed00a | 18 | #include <linux/delay.h> |
1221ce45 | 19 | #include <linux/errno.h> |
b4eb9cfc | 20 | #include <miiphy.h> |
aada81de | 21 | #include <asm/processor.h> |
52d00a81 | 22 | #include <asm/io.h> |
42d1f039 | 23 | |
2abe361c AF |
24 | #define TBIANA_SETTINGS ( \ |
25 | TBIANA_ASYMMETRIC_PAUSE \ | |
26 | | TBIANA_SYMMETRIC_PAUSE \ | |
27 | | TBIANA_FULL_DUPLEX \ | |
28 | ) | |
29 | ||
90b5bf21 | 30 | /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */ |
e7cebff6 TR |
31 | #ifndef CFG_TSEC_TBICR_SETTINGS |
32 | #define CFG_TSEC_TBICR_SETTINGS ( \ | |
2abe361c | 33 | TBICR_PHY_RESET \ |
72c96a68 | 34 | | TBICR_ANEG_ENABLE \ |
2abe361c AF |
35 | | TBICR_FULL_DUPLEX \ |
36 | | TBICR_SPEED1_SET \ | |
37 | ) | |
e7cebff6 | 38 | #endif /* CFG_TSEC_TBICR_SETTINGS */ |
46e91674 | 39 | |
2abe361c AF |
40 | /* Configure the TBI for SGMII operation */ |
41 | static void tsec_configure_serdes(struct tsec_private *priv) | |
42 | { | |
9872b736 BM |
43 | /* |
44 | * Access TBI PHY registers at given TSEC register offset as opposed | |
45 | * to the register offset used for external PHY accesses | |
46 | */ | |
063c1263 | 47 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
d38de338 | 48 | 0, TBI_ANA, TBIANA_SETTINGS); |
063c1263 | 49 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
d38de338 | 50 | 0, TBI_TBICON, TBICON_CLK_SELECT); |
063c1263 | 51 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
e7cebff6 | 52 | 0, TBI_CR, CFG_TSEC_TBICR_SETTINGS); |
97d80fc3 | 53 | } |
42d1f039 | 54 | |
1a4af5c5 CP |
55 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
56 | * and this is the ethernet-crc method needed for TSEC -- and perhaps | |
57 | * some other adapter -- hash tables | |
58 | */ | |
59 | #define CRCPOLY_LE 0xedb88320 | |
60 | static u32 ether_crc(size_t len, unsigned char const *p) | |
61 | { | |
62 | int i; | |
63 | u32 crc; | |
64 | ||
65 | crc = ~0; | |
66 | while (len--) { | |
67 | crc ^= *p++; | |
68 | for (i = 0; i < 8; i++) | |
69 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); | |
70 | } | |
71 | /* an reverse the bits, cuz of way they arrive -- last-first */ | |
72 | crc = (crc >> 16) | (crc << 16); | |
73 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); | |
74 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); | |
75 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); | |
76 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); | |
77 | return crc; | |
78 | } | |
79 | ||
90751910 MH |
80 | /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */ |
81 | ||
82 | /* Set the appropriate hash bit for the given addr */ | |
83 | ||
9872b736 BM |
84 | /* |
85 | * The algorithm works like so: | |
90751910 MH |
86 | * 1) Take the Destination Address (ie the multicast address), and |
87 | * do a CRC on it (little endian), and reverse the bits of the | |
88 | * result. | |
89 | * 2) Use the 8 most significant bits as a hash into a 256-entry | |
90 | * table. The table is controlled through 8 32-bit registers: | |
876d4515 CM |
91 | * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is entry |
92 | * 255. This means that the 3 most significant bits in the | |
90751910 MH |
93 | * hash index which gaddr register to use, and the 5 other bits |
94 | * indicate which bit (assuming an IBM numbering scheme, which | |
876d4515 | 95 | * for PowerPC (tm) is usually the case) in the register holds |
9872b736 BM |
96 | * the entry. |
97 | */ | |
67bb9842 | 98 | static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int join) |
90751910 | 99 | { |
0fd3d911 SG |
100 | struct tsec_private *priv; |
101 | struct tsec __iomem *regs; | |
876d4515 CM |
102 | u32 result, value; |
103 | u8 whichbit, whichreg; | |
104 | ||
0fd3d911 | 105 | priv = dev_get_priv(dev); |
0fd3d911 | 106 | regs = priv->regs; |
876d4515 CM |
107 | result = ether_crc(MAC_ADDR_LEN, mcast_mac); |
108 | whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */ | |
109 | whichreg = result >> 29; /* the 3 MSB = which reg to set it in */ | |
110 | ||
d38de338 | 111 | value = BIT(31 - whichbit); |
876d4515 | 112 | |
67bb9842 | 113 | if (join) |
876d4515 CM |
114 | setbits_be32(®s->hash.gaddr0 + whichreg, value); |
115 | else | |
116 | clrbits_be32(®s->hash.gaddr0 + whichreg, value); | |
117 | ||
90751910 MH |
118 | return 0; |
119 | } | |
90751910 | 120 | |
bee3551e | 121 | static int __maybe_unused tsec_set_promisc(struct udevice *dev, bool enable) |
9dcb810b VO |
122 | { |
123 | struct tsec_private *priv = dev_get_priv(dev); | |
124 | struct tsec __iomem *regs = priv->regs; | |
125 | ||
126 | if (enable) | |
127 | setbits_be32(®s->rctrl, RCTRL_PROM); | |
128 | else | |
129 | clrbits_be32(®s->rctrl, RCTRL_PROM); | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
9872b736 BM |
134 | /* |
135 | * Initialized required registers to appropriate values, zeroing | |
90751910 MH |
136 | * those we don't care about (unless zero is bad, in which case, |
137 | * choose a more appropriate value) | |
138 | */ | |
aec84bf6 | 139 | static void init_registers(struct tsec __iomem *regs) |
90751910 MH |
140 | { |
141 | /* Clear IEVENT */ | |
142 | out_be32(®s->ievent, IEVENT_INIT_CLEAR); | |
143 | ||
144 | out_be32(®s->imask, IMASK_INIT_CLEAR); | |
145 | ||
146 | out_be32(®s->hash.iaddr0, 0); | |
147 | out_be32(®s->hash.iaddr1, 0); | |
148 | out_be32(®s->hash.iaddr2, 0); | |
149 | out_be32(®s->hash.iaddr3, 0); | |
150 | out_be32(®s->hash.iaddr4, 0); | |
151 | out_be32(®s->hash.iaddr5, 0); | |
152 | out_be32(®s->hash.iaddr6, 0); | |
153 | out_be32(®s->hash.iaddr7, 0); | |
154 | ||
155 | out_be32(®s->hash.gaddr0, 0); | |
156 | out_be32(®s->hash.gaddr1, 0); | |
157 | out_be32(®s->hash.gaddr2, 0); | |
158 | out_be32(®s->hash.gaddr3, 0); | |
159 | out_be32(®s->hash.gaddr4, 0); | |
160 | out_be32(®s->hash.gaddr5, 0); | |
161 | out_be32(®s->hash.gaddr6, 0); | |
162 | out_be32(®s->hash.gaddr7, 0); | |
163 | ||
90751910 | 164 | /* Init RMON mib registers */ |
82ef75ca | 165 | memset((void *)®s->rmon, 0, sizeof(regs->rmon)); |
90751910 MH |
166 | |
167 | out_be32(®s->rmon.cam1, 0xffffffff); | |
168 | out_be32(®s->rmon.cam2, 0xffffffff); | |
169 | ||
170 | out_be32(®s->mrblr, MRBLR_INIT_SETTINGS); | |
171 | ||
172 | out_be32(®s->minflr, MINFLR_INIT_SETTINGS); | |
173 | ||
174 | out_be32(®s->attr, ATTR_INIT_SETTINGS); | |
175 | out_be32(®s->attreli, ATTRELI_INIT_SETTINGS); | |
90751910 MH |
176 | } |
177 | ||
9872b736 BM |
178 | /* |
179 | * Configure maccfg2 based on negotiated speed and duplex | |
90751910 MH |
180 | * reported by PHY handling code |
181 | */ | |
063c1263 | 182 | static void adjust_link(struct tsec_private *priv, struct phy_device *phydev) |
90751910 | 183 | { |
aec84bf6 | 184 | struct tsec __iomem *regs = priv->regs; |
90751910 MH |
185 | u32 ecntrl, maccfg2; |
186 | ||
063c1263 AF |
187 | if (!phydev->link) { |
188 | printf("%s: No link.\n", phydev->dev->name); | |
90751910 MH |
189 | return; |
190 | } | |
191 | ||
192 | /* clear all bits relative with interface mode */ | |
193 | ecntrl = in_be32(®s->ecntrl); | |
194 | ecntrl &= ~ECNTRL_R100; | |
195 | ||
196 | maccfg2 = in_be32(®s->maccfg2); | |
197 | maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX); | |
198 | ||
063c1263 | 199 | if (phydev->duplex) |
90751910 MH |
200 | maccfg2 |= MACCFG2_FULL_DUPLEX; |
201 | ||
063c1263 | 202 | switch (phydev->speed) { |
90751910 MH |
203 | case 1000: |
204 | maccfg2 |= MACCFG2_GMII; | |
205 | break; | |
206 | case 100: | |
207 | case 10: | |
208 | maccfg2 |= MACCFG2_MII; | |
209 | ||
9872b736 BM |
210 | /* |
211 | * Set R100 bit in all modes although | |
90751910 MH |
212 | * it is only used in RGMII mode |
213 | */ | |
063c1263 | 214 | if (phydev->speed == 100) |
90751910 MH |
215 | ecntrl |= ECNTRL_R100; |
216 | break; | |
217 | default: | |
063c1263 | 218 | printf("%s: Speed was bad\n", phydev->dev->name); |
90751910 MH |
219 | break; |
220 | } | |
221 | ||
222 | out_be32(®s->ecntrl, ecntrl); | |
223 | out_be32(®s->maccfg2, maccfg2); | |
3dd7f0f0 | 224 | |
063c1263 | 225 | printf("Speed: %d, %s duplex%s\n", phydev->speed, |
d38de338 MS |
226 | (phydev->duplex) ? "full" : "half", |
227 | (phydev->port == PORT_FIBRE) ? ", fiber mode" : ""); | |
90751910 | 228 | } |
9d46ea4a | 229 | |
8ba50176 BM |
230 | /* |
231 | * This returns the status bits of the device. The return value | |
232 | * is never checked, and this is what the 8260 driver did, so we | |
233 | * do the same. Presumably, this would be zero if there were no | |
234 | * errors | |
235 | */ | |
9a1d6af5 | 236 | static int tsec_send(struct udevice *dev, void *packet, int length) |
8ba50176 | 237 | { |
0fd3d911 SG |
238 | struct tsec_private *priv; |
239 | struct tsec __iomem *regs; | |
8ba50176 | 240 | int result = 0; |
07bd39f0 | 241 | u16 status; |
8ba50176 BM |
242 | int i; |
243 | ||
0fd3d911 | 244 | priv = dev_get_priv(dev); |
0fd3d911 | 245 | regs = priv->regs; |
8ba50176 BM |
246 | /* Find an empty buffer descriptor */ |
247 | for (i = 0; | |
248 | in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY; | |
249 | i++) { | |
250 | if (i >= TOUT_LOOP) { | |
b7be7767 | 251 | printf("%s: tsec: tx buffers full\n", dev->name); |
8ba50176 BM |
252 | return result; |
253 | } | |
254 | } | |
255 | ||
256 | out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet); | |
257 | out_be16(&priv->txbd[priv->tx_idx].length, length); | |
258 | status = in_be16(&priv->txbd[priv->tx_idx].status); | |
259 | out_be16(&priv->txbd[priv->tx_idx].status, status | | |
260 | (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT)); | |
261 | ||
262 | /* Tell the DMA to go */ | |
263 | out_be32(®s->tstat, TSTAT_CLEAR_THALT); | |
264 | ||
265 | /* Wait for buffer to be transmitted */ | |
266 | for (i = 0; | |
267 | in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY; | |
268 | i++) { | |
269 | if (i >= TOUT_LOOP) { | |
b7be7767 | 270 | printf("%s: tsec: tx error\n", dev->name); |
8ba50176 BM |
271 | return result; |
272 | } | |
273 | } | |
274 | ||
275 | priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT; | |
276 | result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS; | |
277 | ||
278 | return result; | |
279 | } | |
280 | ||
9a1d6af5 BM |
281 | static int tsec_recv(struct udevice *dev, int flags, uchar **packetp) |
282 | { | |
0fd3d911 | 283 | struct tsec_private *priv = (struct tsec_private *)dev_get_priv(dev); |
9a1d6af5 BM |
284 | struct tsec __iomem *regs = priv->regs; |
285 | int ret = -1; | |
286 | ||
287 | if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) { | |
288 | int length = in_be16(&priv->rxbd[priv->rx_idx].length); | |
d38de338 MS |
289 | u16 status = in_be16(&priv->rxbd[priv->rx_idx].status); |
290 | u32 buf; | |
9a1d6af5 BM |
291 | |
292 | /* Send the packet up if there were no errors */ | |
293 | if (!(status & RXBD_STATS)) { | |
294 | buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr); | |
295 | *packetp = (uchar *)buf; | |
296 | ret = length - 4; | |
297 | } else { | |
298 | printf("Got error %x\n", (status & RXBD_STATS)); | |
299 | } | |
300 | } | |
301 | ||
302 | if (in_be32(®s->ievent) & IEVENT_BSY) { | |
303 | out_be32(®s->ievent, IEVENT_BSY); | |
304 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); | |
305 | } | |
306 | ||
307 | return ret; | |
308 | } | |
309 | ||
310 | static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length) | |
311 | { | |
0fd3d911 | 312 | struct tsec_private *priv = (struct tsec_private *)dev_get_priv(dev); |
d38de338 | 313 | u16 status; |
9a1d6af5 BM |
314 | |
315 | out_be16(&priv->rxbd[priv->rx_idx].length, 0); | |
316 | ||
317 | status = RXBD_EMPTY; | |
318 | /* Set the wrap bit if this is the last element in the list */ | |
319 | if ((priv->rx_idx + 1) == PKTBUFSRX) | |
320 | status |= RXBD_WRAP; | |
321 | out_be16(&priv->rxbd[priv->rx_idx].status, status); | |
322 | ||
323 | priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX; | |
324 | ||
325 | return 0; | |
326 | } | |
8ba50176 | 327 | |
9a1d6af5 | 328 | static void tsec_halt(struct udevice *dev) |
8ba50176 | 329 | { |
0fd3d911 SG |
330 | struct tsec_private *priv; |
331 | struct tsec __iomem *regs; | |
0fd3d911 | 332 | priv = dev_get_priv(dev); |
0fd3d911 | 333 | regs = priv->regs; |
8ba50176 BM |
334 | |
335 | clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); | |
336 | setbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); | |
337 | ||
338 | while ((in_be32(®s->ievent) & (IEVENT_GRSC | IEVENT_GTSC)) | |
339 | != (IEVENT_GRSC | IEVENT_GTSC)) | |
340 | ; | |
341 | ||
342 | clrbits_be32(®s->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN); | |
343 | ||
344 | /* Shut down the PHY, as needed */ | |
345 | phy_shutdown(priv->phydev); | |
346 | } | |
347 | ||
aada81de | 348 | #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129 |
349 | /* | |
350 | * When MACCFG1[Rx_EN] is enabled during system boot as part | |
351 | * of the eTSEC port initialization sequence, | |
352 | * the eTSEC Rx logic may not be properly initialized. | |
353 | */ | |
10aaefba | 354 | static void redundant_init(struct tsec_private *priv) |
aada81de | 355 | { |
aec84bf6 | 356 | struct tsec __iomem *regs = priv->regs; |
aada81de | 357 | uint t, count = 0; |
358 | int fail = 1; | |
359 | static const u8 pkt[] = { | |
360 | 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25, | |
361 | 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00, | |
362 | 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01, | |
363 | 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1, | |
364 | 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00, | |
365 | 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, | |
366 | 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, | |
367 | 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, | |
368 | 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, | |
369 | 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, | |
370 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, | |
371 | 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, | |
372 | 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, | |
373 | 0x71, 0x72}; | |
374 | ||
375 | /* Enable promiscuous mode */ | |
9dcb810b | 376 | setbits_be32(®s->rctrl, RCTRL_PROM); |
aada81de | 377 | /* Enable loopback mode */ |
378 | setbits_be32(®s->maccfg1, MACCFG1_LOOPBACK); | |
379 | /* Enable transmit and receive */ | |
380 | setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN); | |
381 | ||
382 | /* Tell the DMA it is clear to go */ | |
383 | setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS); | |
384 | out_be32(®s->tstat, TSTAT_CLEAR_THALT); | |
385 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); | |
386 | clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); | |
387 | ||
388 | do { | |
d38de338 MS |
389 | u16 status; |
390 | ||
56a27a1e | 391 | tsec_send(priv->dev, (void *)pkt, sizeof(pkt)); |
aada81de | 392 | |
393 | /* Wait for buffer to be received */ | |
e677da97 BM |
394 | for (t = 0; |
395 | in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY; | |
362b123f | 396 | t++) { |
aada81de | 397 | if (t >= 10 * TOUT_LOOP) { |
56a27a1e | 398 | printf("%s: tsec: rx error\n", priv->dev->name); |
aada81de | 399 | break; |
400 | } | |
401 | } | |
402 | ||
362b123f | 403 | if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt))) |
aada81de | 404 | fail = 0; |
405 | ||
e677da97 | 406 | out_be16(&priv->rxbd[priv->rx_idx].length, 0); |
9c9141fd | 407 | status = RXBD_EMPTY; |
362b123f | 408 | if ((priv->rx_idx + 1) == PKTBUFSRX) |
9c9141fd | 409 | status |= RXBD_WRAP; |
e677da97 | 410 | out_be16(&priv->rxbd[priv->rx_idx].status, status); |
362b123f | 411 | priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX; |
aada81de | 412 | |
413 | if (in_be32(®s->ievent) & IEVENT_BSY) { | |
414 | out_be32(®s->ievent, IEVENT_BSY); | |
415 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); | |
416 | } | |
417 | if (fail) { | |
418 | printf("loopback recv packet error!\n"); | |
419 | clrbits_be32(®s->maccfg1, MACCFG1_RX_EN); | |
420 | udelay(1000); | |
421 | setbits_be32(®s->maccfg1, MACCFG1_RX_EN); | |
422 | } | |
423 | } while ((count++ < 4) && (fail == 1)); | |
424 | ||
425 | if (fail) | |
426 | panic("eTSEC init fail!\n"); | |
427 | /* Disable promiscuous mode */ | |
9dcb810b | 428 | clrbits_be32(®s->rctrl, RCTRL_PROM); |
aada81de | 429 | /* Disable loopback mode */ |
430 | clrbits_be32(®s->maccfg1, MACCFG1_LOOPBACK); | |
431 | } | |
432 | #endif | |
433 | ||
9872b736 BM |
434 | /* |
435 | * Set up the buffers and their descriptors, and bring up the | |
90751910 | 436 | * interface |
89875e96 | 437 | */ |
56a27a1e | 438 | static void startup_tsec(struct tsec_private *priv) |
be5048f1 | 439 | { |
aec84bf6 | 440 | struct tsec __iomem *regs = priv->regs; |
d38de338 | 441 | u16 status; |
9c9141fd | 442 | int i; |
be5048f1 | 443 | |
063c1263 | 444 | /* reset the indices to zero */ |
362b123f BM |
445 | priv->rx_idx = 0; |
446 | priv->tx_idx = 0; | |
aada81de | 447 | #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129 |
448 | uint svr; | |
449 | #endif | |
063c1263 | 450 | |
90751910 | 451 | /* Point to the buffer descriptors */ |
e677da97 BM |
452 | out_be32(®s->tbase, (u32)&priv->txbd[0]); |
453 | out_be32(®s->rbase, (u32)&priv->rxbd[0]); | |
be5048f1 | 454 | |
90751910 MH |
455 | /* Initialize the Rx Buffer descriptors */ |
456 | for (i = 0; i < PKTBUFSRX; i++) { | |
e677da97 BM |
457 | out_be16(&priv->rxbd[i].status, RXBD_EMPTY); |
458 | out_be16(&priv->rxbd[i].length, 0); | |
459 | out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]); | |
90751910 | 460 | } |
e677da97 BM |
461 | status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status); |
462 | out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP); | |
be5048f1 | 463 | |
90751910 MH |
464 | /* Initialize the TX Buffer Descriptors */ |
465 | for (i = 0; i < TX_BUF_CNT; i++) { | |
e677da97 BM |
466 | out_be16(&priv->txbd[i].status, 0); |
467 | out_be16(&priv->txbd[i].length, 0); | |
468 | out_be32(&priv->txbd[i].bufptr, 0); | |
be5048f1 | 469 | } |
e677da97 BM |
470 | status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status); |
471 | out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP); | |
be5048f1 | 472 | |
aada81de | 473 | #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129 |
474 | svr = get_svr(); | |
475 | if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0)) | |
56a27a1e | 476 | redundant_init(priv); |
aada81de | 477 | #endif |
90751910 MH |
478 | /* Enable Transmit and Receive */ |
479 | setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN); | |
480 | ||
481 | /* Tell the DMA it is clear to go */ | |
482 | setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS); | |
483 | out_be32(®s->tstat, TSTAT_CLEAR_THALT); | |
484 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); | |
485 | clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); | |
be5048f1 WD |
486 | } |
487 | ||
9872b736 BM |
488 | /* |
489 | * Initializes data structures and registers for the controller, | |
490 | * and brings the interface up. Returns the link status, meaning | |
90751910 | 491 | * that it returns success if the link is up, failure otherwise. |
9872b736 | 492 | * This allows U-Boot to find the first active controller. |
89875e96 | 493 | */ |
9a1d6af5 | 494 | static int tsec_init(struct udevice *dev) |
97d80fc3 | 495 | { |
0fd3d911 SG |
496 | struct tsec_private *priv; |
497 | struct tsec __iomem *regs; | |
c69cda25 | 498 | struct eth_pdata *pdata = dev_get_plat(dev); |
b1690bc3 | 499 | u32 tempval; |
11af8d65 | 500 | int ret; |
97d80fc3 | 501 | |
0fd3d911 | 502 | priv = dev_get_priv(dev); |
0fd3d911 | 503 | regs = priv->regs; |
90751910 MH |
504 | /* Make sure the controller is stopped */ |
505 | tsec_halt(dev); | |
97d80fc3 | 506 | |
90751910 MH |
507 | /* Init MACCFG2. Defaults to GMII */ |
508 | out_be32(®s->maccfg2, MACCFG2_INIT_SETTINGS); | |
97d80fc3 | 509 | |
90751910 MH |
510 | /* Init ECNTRL */ |
511 | out_be32(®s->ecntrl, ECNTRL_INIT_SETTINGS); | |
97d80fc3 | 512 | |
9872b736 BM |
513 | /* |
514 | * Copy the station address into the address registers. | |
b1690bc3 CM |
515 | * For a station address of 0x12345678ABCD in transmission |
516 | * order (BE), MACnADDR1 is set to 0xCDAB7856 and | |
517 | * MACnADDR2 is set to 0x34120000. | |
518 | */ | |
9a1d6af5 BM |
519 | tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) | |
520 | (pdata->enetaddr[3] << 8) | pdata->enetaddr[2]; | |
97d80fc3 | 521 | |
90751910 | 522 | out_be32(®s->macstnaddr1, tempval); |
97d80fc3 | 523 | |
9a1d6af5 | 524 | tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16); |
97d80fc3 | 525 | |
90751910 | 526 | out_be32(®s->macstnaddr2, tempval); |
97d80fc3 | 527 | |
90751910 MH |
528 | /* Clear out (for the most part) the other registers */ |
529 | init_registers(regs); | |
530 | ||
531 | /* Ready the device for tx/rx */ | |
56a27a1e | 532 | startup_tsec(priv); |
90751910 | 533 | |
063c1263 | 534 | /* Start up the PHY */ |
11af8d65 TT |
535 | ret = phy_startup(priv->phydev); |
536 | if (ret) { | |
537 | printf("Could not initialize PHY %s\n", | |
538 | priv->phydev->dev->name); | |
539 | return ret; | |
540 | } | |
063c1263 AF |
541 | |
542 | adjust_link(priv, priv->phydev); | |
543 | ||
90751910 | 544 | /* If there's no link, fail */ |
063c1263 AF |
545 | return priv->phydev->link ? 0 : -1; |
546 | } | |
547 | ||
596ec9ba | 548 | static phy_interface_t __maybe_unused tsec_get_interface(struct tsec_private *priv) |
063c1263 | 549 | { |
aec84bf6 | 550 | struct tsec __iomem *regs = priv->regs; |
063c1263 AF |
551 | u32 ecntrl; |
552 | ||
553 | ecntrl = in_be32(®s->ecntrl); | |
554 | ||
555 | if (ecntrl & ECNTRL_SGMII_MODE) | |
556 | return PHY_INTERFACE_MODE_SGMII; | |
557 | ||
558 | if (ecntrl & ECNTRL_TBI_MODE) { | |
559 | if (ecntrl & ECNTRL_REDUCED_MODE) | |
560 | return PHY_INTERFACE_MODE_RTBI; | |
561 | else | |
562 | return PHY_INTERFACE_MODE_TBI; | |
563 | } | |
564 | ||
565 | if (ecntrl & ECNTRL_REDUCED_MODE) { | |
d38de338 MS |
566 | phy_interface_t interface; |
567 | ||
063c1263 AF |
568 | if (ecntrl & ECNTRL_REDUCED_MII_MODE) |
569 | return PHY_INTERFACE_MODE_RMII; | |
d38de338 MS |
570 | |
571 | interface = priv->interface; | |
572 | ||
573 | /* | |
574 | * This isn't autodetected, so it must | |
575 | * be set by the platform code. | |
576 | */ | |
577 | if (interface == PHY_INTERFACE_MODE_RGMII_ID || | |
578 | interface == PHY_INTERFACE_MODE_RGMII_TXID || | |
579 | interface == PHY_INTERFACE_MODE_RGMII_RXID) | |
580 | return interface; | |
581 | ||
582 | return PHY_INTERFACE_MODE_RGMII; | |
063c1263 AF |
583 | } |
584 | ||
585 | if (priv->flags & TSEC_GIGABIT) | |
586 | return PHY_INTERFACE_MODE_GMII; | |
587 | ||
588 | return PHY_INTERFACE_MODE_MII; | |
90751910 MH |
589 | } |
590 | ||
9872b736 BM |
591 | /* |
592 | * Discover which PHY is attached to the device, and configure it | |
90751910 MH |
593 | * properly. If the PHY is not recognized, then return 0 |
594 | * (failure). Otherwise, return 1 | |
7abf0c58 | 595 | */ |
56a27a1e | 596 | static int init_phy(struct tsec_private *priv) |
7abf0c58 | 597 | { |
063c1263 | 598 | struct phy_device *phydev; |
aec84bf6 | 599 | struct tsec __iomem *regs = priv->regs; |
063c1263 AF |
600 | u32 supported = (SUPPORTED_10baseT_Half | |
601 | SUPPORTED_10baseT_Full | | |
602 | SUPPORTED_100baseT_Half | | |
603 | SUPPORTED_100baseT_Full); | |
604 | ||
605 | if (priv->flags & TSEC_GIGABIT) | |
606 | supported |= SUPPORTED_1000baseT_Full; | |
97d80fc3 | 607 | |
90751910 | 608 | /* Assign a Physical address to the TBI */ |
a1c76c15 | 609 | out_be32(®s->tbipa, priv->tbiaddr); |
90751910 | 610 | |
063c1263 AF |
611 | if (priv->interface == PHY_INTERFACE_MODE_SGMII) |
612 | tsec_configure_serdes(priv); | |
90751910 | 613 | |
2f420f13 | 614 | #if defined(CONFIG_DM_MDIO) |
3c56251f | 615 | phydev = dm_eth_phy_connect(priv->dev); |
b4eb9cfc | 616 | #else |
56a27a1e BM |
617 | phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev, |
618 | priv->interface); | |
b4eb9cfc | 619 | #endif |
7f233c05 CM |
620 | if (!phydev) |
621 | return 0; | |
7abf0c58 | 622 | |
063c1263 AF |
623 | phydev->supported &= supported; |
624 | phydev->advertising = phydev->supported; | |
7abf0c58 | 625 | |
063c1263 | 626 | priv->phydev = phydev; |
90751910 | 627 | |
063c1263 | 628 | phy_config(phydev); |
90751910 MH |
629 | |
630 | return 1; | |
7abf0c58 WD |
631 | } |
632 | ||
9a1d6af5 BM |
633 | int tsec_probe(struct udevice *dev) |
634 | { | |
c69cda25 | 635 | struct eth_pdata *pdata = dev_get_plat(dev); |
07bd39f0 | 636 | struct tsec_private *priv = dev_get_priv(dev); |
1313aaf0 | 637 | struct ofnode_phandle_args phandle_args; |
65cc0e2a | 638 | u32 tbiaddr = CFG_SYS_TBIPA_VALUE; |
7fb568de | 639 | struct tsec_data *data; |
a081546d | 640 | ofnode parent, child; |
bca686a4 | 641 | fdt_addr_t reg; |
50dae8eb | 642 | u32 max_speed; |
9a1d6af5 BM |
643 | int ret; |
644 | ||
7fb568de HZ |
645 | data = (struct tsec_data *)dev_get_driver_data(dev); |
646 | ||
1313aaf0 | 647 | pdata->iobase = (phys_addr_t)dev_read_addr(dev); |
a081546d BM |
648 | if (pdata->iobase == FDT_ADDR_T_NONE) { |
649 | ofnode_for_each_subnode(child, dev_ofnode(dev)) { | |
650 | if (strncmp(ofnode_get_name(child), "queue-group", | |
651 | strlen("queue-group"))) | |
652 | continue; | |
653 | ||
654 | reg = ofnode_get_addr(child); | |
655 | if (reg == FDT_ADDR_T_NONE) { | |
656 | printf("No 'reg' property of <queue-group>\n"); | |
657 | return -ENOENT; | |
658 | } | |
659 | pdata->iobase = reg; | |
660 | ||
661 | /* | |
662 | * if there are multiple queue groups, | |
663 | * only the first one is used. | |
664 | */ | |
665 | break; | |
666 | } | |
667 | ||
668 | if (!ofnode_valid(child)) { | |
669 | printf("No child node for <queue-group>?\n"); | |
670 | return -ENOENT; | |
671 | } | |
672 | } | |
673 | ||
408f056e | 674 | priv->regs = map_physmem(pdata->iobase, 0, MAP_NOCACHE); |
9a1d6af5 | 675 | |
29db3107 VO |
676 | ret = dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0, |
677 | &phandle_args); | |
a0f47e01 | 678 | if (ret == 0) { |
29db3107 VO |
679 | ofnode_read_u32(phandle_args.node, "reg", &tbiaddr); |
680 | ||
a0f47e01 HZ |
681 | parent = ofnode_get_parent(phandle_args.node); |
682 | if (!ofnode_valid(parent)) { | |
683 | printf("No parent node for TBI PHY?\n"); | |
684 | return -ENOENT; | |
685 | } | |
686 | ||
687 | reg = ofnode_get_addr_index(parent, 0); | |
688 | if (reg == FDT_ADDR_T_NONE) { | |
689 | printf("No 'reg' property of MII for TBI PHY\n"); | |
690 | return -ENOENT; | |
691 | } | |
692 | ||
7fb568de | 693 | priv->phyregs_sgmii = map_physmem(reg + data->mdio_regs_off, |
a0f47e01 HZ |
694 | 0, MAP_NOCACHE); |
695 | } | |
696 | ||
29db3107 | 697 | priv->tbiaddr = tbiaddr; |
a1c76c15 | 698 | |
123ca114 | 699 | pdata->phy_interface = dev_read_phy_mode(dev); |
ffb0f6f4 | 700 | if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) |
d883a5fb VO |
701 | pdata->phy_interface = tsec_get_interface(priv); |
702 | ||
9a1d6af5 BM |
703 | priv->interface = pdata->phy_interface; |
704 | ||
50dae8eb AG |
705 | /* Check for speed limit, default is 1000Mbps */ |
706 | max_speed = dev_read_u32_default(dev, "max-speed", 1000); | |
707 | ||
9a1d6af5 | 708 | /* Initialize flags */ |
50dae8eb AG |
709 | if (max_speed == 1000) |
710 | priv->flags = TSEC_GIGABIT; | |
9a1d6af5 BM |
711 | if (priv->interface == PHY_INTERFACE_MODE_SGMII) |
712 | priv->flags |= TSEC_SGMII; | |
713 | ||
9a1d6af5 BM |
714 | /* Reset the MAC */ |
715 | setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET); | |
716 | udelay(2); /* Soft Reset must be asserted for 3 TX clocks */ | |
717 | clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET); | |
718 | ||
719 | priv->dev = dev; | |
720 | priv->bus = miiphy_get_dev_by_name(dev->name); | |
721 | ||
722 | /* Try to initialize PHY here, and return */ | |
723 | return !init_phy(priv); | |
724 | } | |
725 | ||
726 | int tsec_remove(struct udevice *dev) | |
727 | { | |
0fd3d911 | 728 | struct tsec_private *priv = dev_get_priv(dev); |
9a1d6af5 BM |
729 | |
730 | free(priv->phydev); | |
731 | mdio_unregister(priv->bus); | |
732 | mdio_free(priv->bus); | |
733 | ||
734 | return 0; | |
735 | } | |
736 | ||
737 | static const struct eth_ops tsec_ops = { | |
738 | .start = tsec_init, | |
739 | .send = tsec_send, | |
740 | .recv = tsec_recv, | |
741 | .free_pkt = tsec_free_pkt, | |
742 | .stop = tsec_halt, | |
9a1d6af5 | 743 | .mcast = tsec_mcast_addr, |
9dcb810b | 744 | .set_promisc = tsec_set_promisc, |
9a1d6af5 BM |
745 | }; |
746 | ||
7fb568de HZ |
747 | static struct tsec_data etsec2_data = { |
748 | .mdio_regs_off = TSEC_MDIO_REGS_OFFSET, | |
749 | }; | |
750 | ||
751 | static struct tsec_data gianfar_data = { | |
752 | .mdio_regs_off = 0x0, | |
753 | }; | |
754 | ||
9a1d6af5 | 755 | static const struct udevice_id tsec_ids[] = { |
7fb568de HZ |
756 | { .compatible = "fsl,etsec2", .data = (ulong)&etsec2_data }, |
757 | { .compatible = "gianfar", .data = (ulong)&gianfar_data }, | |
9a1d6af5 BM |
758 | { } |
759 | }; | |
760 | ||
761 | U_BOOT_DRIVER(eth_tsec) = { | |
762 | .name = "tsec", | |
763 | .id = UCLASS_ETH, | |
764 | .of_match = tsec_ids, | |
765 | .probe = tsec_probe, | |
766 | .remove = tsec_remove, | |
767 | .ops = &tsec_ops, | |
41575d8e | 768 | .priv_auto = sizeof(struct tsec_private), |
caa4daa2 | 769 | .plat_auto = sizeof(struct eth_pdata), |
9a1d6af5 BM |
770 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
771 | }; |