]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
9131589a PW |
2 | /* |
3 | * (C) Copyright 2009 | |
4 | * Marvell Semiconductor <www.marvell.com> | |
5 | * Written-by: Prafulla Wadaskar <[email protected]> | |
6 | * | |
7 | * (C) Copyright 2003 | |
8 | * Ingo Assmus <[email protected]> | |
9 | * | |
10 | * based on - Driver for MV64360X ethernet ports | |
11 | * Copyright (C) 2002 [email protected] | |
9131589a PW |
12 | */ |
13 | ||
14 | #include <common.h> | |
fb731076 | 15 | #include <dm.h> |
f7ae49fc | 16 | #include <log.h> |
9131589a PW |
17 | #include <net.h> |
18 | #include <malloc.h> | |
19 | #include <miiphy.h> | |
5194ed7e | 20 | #include <wait_bit.h> |
a7efd719 | 21 | #include <asm/io.h> |
c05ed00a | 22 | #include <linux/delay.h> |
1221ce45 | 23 | #include <linux/errno.h> |
9131589a | 24 | #include <asm/types.h> |
a7efd719 | 25 | #include <asm/system.h> |
9131589a | 26 | #include <asm/byteorder.h> |
36aaa918 | 27 | #include <asm/arch/cpu.h> |
d44265ad | 28 | |
bb0fb4c0 | 29 | #if defined(CONFIG_ARCH_KIRKWOOD) |
3dc23f78 | 30 | #include <asm/arch/soc.h> |
b16a3316 | 31 | #elif defined(CONFIG_ARCH_ORION5X) |
d3c9ffd0 | 32 | #include <asm/arch/orion5x.h> |
d44265ad AA |
33 | #endif |
34 | ||
9b6bcdcb | 35 | #include "mvgbe.h" |
9131589a | 36 | |
49fa6ed8 AA |
37 | DECLARE_GLOBAL_DATA_PTR; |
38 | ||
5aa2297d LP |
39 | #ifndef CONFIG_MVGBE_PORTS |
40 | # define CONFIG_MVGBE_PORTS {0, 0} | |
41 | #endif | |
42 | ||
d44265ad AA |
43 | #define MV_PHY_ADR_REQUEST 0xee |
44 | #define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi) | |
bb1ca3b2 | 45 | |
cd3ca3ff | 46 | #if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
5194ed7e CP |
47 | static int smi_wait_ready(struct mvgbe_device *dmvgbe) |
48 | { | |
49 | int ret; | |
50 | ||
51 | ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false, | |
52 | MVGBE_PHY_SMI_TIMEOUT_MS, false); | |
53 | if (ret) { | |
54 | printf("Error: SMI busy timeout\n"); | |
55 | return ret; | |
56 | } | |
57 | ||
58 | return 0; | |
59 | } | |
60 | ||
e9bf75c9 CP |
61 | static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr, |
62 | int devad, int reg_ofs) | |
9131589a | 63 | { |
d44265ad | 64 | struct mvgbe_registers *regs = dmvgbe->regs; |
9131589a | 65 | u32 smi_reg; |
7b05f5e0 | 66 | u32 timeout; |
e9bf75c9 | 67 | u16 data = 0; |
9131589a PW |
68 | |
69 | /* Phyadr read request */ | |
d44265ad AA |
70 | if (phy_adr == MV_PHY_ADR_REQUEST && |
71 | reg_ofs == MV_PHY_ADR_REQUEST) { | |
9131589a | 72 | /* */ |
5a49f174 JH |
73 | data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK); |
74 | return data; | |
9131589a PW |
75 | } |
76 | /* check parameters */ | |
77 | if (phy_adr > PHYADR_MASK) { | |
78 | printf("Err..(%s) Invalid PHY address %d\n", | |
1fd92db8 | 79 | __func__, phy_adr); |
9131589a PW |
80 | return -EFAULT; |
81 | } | |
82 | if (reg_ofs > PHYREG_MASK) { | |
83 | printf("Err..(%s) Invalid register offset %d\n", | |
1fd92db8 | 84 | __func__, reg_ofs); |
9131589a PW |
85 | return -EFAULT; |
86 | } | |
87 | ||
9131589a | 88 | /* wait till the SMI is not busy */ |
5194ed7e CP |
89 | if (smi_wait_ready(dmvgbe) < 0) |
90 | return -EFAULT; | |
9131589a PW |
91 | |
92 | /* fill the phy address and regiser offset and read opcode */ | |
d44265ad AA |
93 | smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS) |
94 | | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS) | |
95 | | MVGBE_PHY_SMI_OPCODE_READ; | |
9131589a PW |
96 | |
97 | /* write the smi register */ | |
d44265ad | 98 | MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg); |
9131589a PW |
99 | |
100 | /*wait till read value is ready */ | |
d44265ad | 101 | timeout = MVGBE_PHY_SMI_TIMEOUT; |
9131589a PW |
102 | |
103 | do { | |
104 | /* read smi register */ | |
d44265ad | 105 | smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG); |
9131589a PW |
106 | if (timeout-- == 0) { |
107 | printf("Err..(%s) SMI read ready timeout\n", | |
1fd92db8 | 108 | __func__); |
9131589a PW |
109 | return -EFAULT; |
110 | } | |
d44265ad | 111 | } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK)); |
9131589a PW |
112 | |
113 | /* Wait for the data to update in the SMI register */ | |
d44265ad AA |
114 | for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++) |
115 | ; | |
9131589a | 116 | |
5a49f174 | 117 | data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK); |
9131589a | 118 | |
1fd92db8 | 119 | debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs, |
5a49f174 | 120 | data); |
9131589a | 121 | |
5a49f174 | 122 | return data; |
9131589a PW |
123 | } |
124 | ||
125 | /* | |
e9bf75c9 | 126 | * smi_reg_read - miiphy_read callback function. |
9131589a | 127 | * |
e9bf75c9 | 128 | * Returns 16bit phy register value, or -EFAULT on error |
9131589a | 129 | */ |
e9bf75c9 CP |
130 | static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad, |
131 | int reg_ofs) | |
9131589a | 132 | { |
fb731076 CP |
133 | #ifdef CONFIG_DM_ETH |
134 | struct mvgbe_device *dmvgbe = bus->priv; | |
135 | #else | |
5a49f174 | 136 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
d44265ad | 137 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
fb731076 | 138 | #endif |
e9bf75c9 CP |
139 | |
140 | return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs); | |
141 | } | |
142 | ||
143 | static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr, | |
144 | int devad, int reg_ofs, u16 data) | |
145 | { | |
d44265ad | 146 | struct mvgbe_registers *regs = dmvgbe->regs; |
9131589a | 147 | u32 smi_reg; |
9131589a PW |
148 | |
149 | /* Phyadr write request*/ | |
d44265ad AA |
150 | if (phy_adr == MV_PHY_ADR_REQUEST && |
151 | reg_ofs == MV_PHY_ADR_REQUEST) { | |
152 | MVGBE_REG_WR(regs->phyadr, data); | |
9131589a PW |
153 | return 0; |
154 | } | |
155 | ||
156 | /* check parameters */ | |
157 | if (phy_adr > PHYADR_MASK) { | |
1fd92db8 | 158 | printf("Err..(%s) Invalid phy address\n", __func__); |
9131589a PW |
159 | return -EINVAL; |
160 | } | |
161 | if (reg_ofs > PHYREG_MASK) { | |
1fd92db8 | 162 | printf("Err..(%s) Invalid register offset\n", __func__); |
5194ed7e | 163 | return -EFAULT; |
9131589a PW |
164 | } |
165 | ||
166 | /* wait till the SMI is not busy */ | |
5194ed7e CP |
167 | if (smi_wait_ready(dmvgbe) < 0) |
168 | return -EFAULT; | |
9131589a PW |
169 | |
170 | /* fill the phy addr and reg offset and write opcode and data */ | |
d44265ad AA |
171 | smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS); |
172 | smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS) | |
173 | | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS); | |
174 | smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ; | |
9131589a PW |
175 | |
176 | /* write the smi register */ | |
d44265ad | 177 | MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg); |
9131589a PW |
178 | |
179 | return 0; | |
180 | } | |
e9bf75c9 CP |
181 | |
182 | /* | |
183 | * smi_reg_write - miiphy_write callback function. | |
184 | * | |
185 | * Returns 0 if write succeed, -EFAULT on error | |
186 | */ | |
187 | static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad, | |
188 | int reg_ofs, u16 data) | |
189 | { | |
fb731076 CP |
190 | #ifdef CONFIG_DM_ETH |
191 | struct mvgbe_device *dmvgbe = bus->priv; | |
192 | #else | |
e9bf75c9 CP |
193 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
194 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); | |
fb731076 | 195 | #endif |
e9bf75c9 CP |
196 | |
197 | return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data); | |
198 | } | |
cc79697c | 199 | #endif |
9131589a PW |
200 | |
201 | /* Stop and checks all queues */ | |
202 | static void stop_queue(u32 * qreg) | |
203 | { | |
204 | u32 reg_data; | |
205 | ||
206 | reg_data = readl(qreg); | |
207 | ||
208 | if (reg_data & 0xFF) { | |
209 | /* Issue stop command for active channels only */ | |
210 | writel((reg_data << 8), qreg); | |
211 | ||
212 | /* Wait for all queue activity to terminate. */ | |
213 | do { | |
214 | /* | |
215 | * Check port cause register that all queues | |
216 | * are stopped | |
217 | */ | |
218 | reg_data = readl(qreg); | |
219 | } | |
220 | while (reg_data & 0xFF); | |
221 | } | |
222 | } | |
223 | ||
224 | /* | |
225 | * set_access_control - Config address decode parameters for Ethernet unit | |
226 | * | |
227 | * This function configures the address decode parameters for the Gigabit | |
228 | * Ethernet Controller according the given parameters struct. | |
229 | * | |
230 | * @regs Register struct pointer. | |
231 | * @param Address decode parameter struct. | |
232 | */ | |
d44265ad AA |
233 | static void set_access_control(struct mvgbe_registers *regs, |
234 | struct mvgbe_winparam *param) | |
9131589a PW |
235 | { |
236 | u32 access_prot_reg; | |
237 | ||
238 | /* Set access control register */ | |
d44265ad | 239 | access_prot_reg = MVGBE_REG_RD(regs->epap); |
9131589a PW |
240 | /* clear window permission */ |
241 | access_prot_reg &= (~(3 << (param->win * 2))); | |
242 | access_prot_reg |= (param->access_ctrl << (param->win * 2)); | |
d44265ad | 243 | MVGBE_REG_WR(regs->epap, access_prot_reg); |
9131589a PW |
244 | |
245 | /* Set window Size reg (SR) */ | |
d44265ad | 246 | MVGBE_REG_WR(regs->barsz[param->win].size, |
9131589a PW |
247 | (((param->size / 0x10000) - 1) << 16)); |
248 | ||
249 | /* Set window Base address reg (BA) */ | |
d44265ad | 250 | MVGBE_REG_WR(regs->barsz[param->win].bar, |
9131589a PW |
251 | (param->target | param->attrib | param->base_addr)); |
252 | /* High address remap reg (HARR) */ | |
253 | if (param->win < 4) | |
d44265ad | 254 | MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr); |
9131589a PW |
255 | |
256 | /* Base address enable reg (BARER) */ | |
257 | if (param->enable == 1) | |
d44265ad | 258 | MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win)); |
9131589a | 259 | else |
d44265ad | 260 | MVGBE_REG_BITS_SET(regs->bare, (1 << param->win)); |
9131589a PW |
261 | } |
262 | ||
d44265ad | 263 | static void set_dram_access(struct mvgbe_registers *regs) |
9131589a | 264 | { |
d44265ad | 265 | struct mvgbe_winparam win_param; |
9131589a PW |
266 | int i; |
267 | ||
268 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { | |
269 | /* Set access parameters for DRAM bank i */ | |
270 | win_param.win = i; /* Use Ethernet window i */ | |
271 | /* Window target - DDR */ | |
d44265ad | 272 | win_param.target = MVGBE_TARGET_DRAM; |
9131589a PW |
273 | /* Enable full access */ |
274 | win_param.access_ctrl = EWIN_ACCESS_FULL; | |
275 | win_param.high_addr = 0; | |
49fa6ed8 AA |
276 | /* Get bank base and size */ |
277 | win_param.base_addr = gd->bd->bi_dram[i].start; | |
278 | win_param.size = gd->bd->bi_dram[i].size; | |
9131589a PW |
279 | if (win_param.size == 0) |
280 | win_param.enable = 0; | |
281 | else | |
282 | win_param.enable = 1; /* Enable the access */ | |
283 | ||
284 | /* Enable DRAM bank */ | |
285 | switch (i) { | |
286 | case 0: | |
287 | win_param.attrib = EBAR_DRAM_CS0; | |
288 | break; | |
289 | case 1: | |
290 | win_param.attrib = EBAR_DRAM_CS1; | |
291 | break; | |
292 | case 2: | |
293 | win_param.attrib = EBAR_DRAM_CS2; | |
294 | break; | |
295 | case 3: | |
296 | win_param.attrib = EBAR_DRAM_CS3; | |
297 | break; | |
298 | default: | |
49fa6ed8 | 299 | /* invalid bank, disable access */ |
9131589a PW |
300 | win_param.enable = 0; |
301 | win_param.attrib = 0; | |
302 | break; | |
303 | } | |
304 | /* Set the access control for address window(EPAPR) RD/WR */ | |
305 | set_access_control(regs, &win_param); | |
306 | } | |
307 | } | |
308 | ||
309 | /* | |
310 | * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables | |
311 | * | |
312 | * Go through all the DA filter tables (Unicast, Special Multicast & Other | |
313 | * Multicast) and set each entry to 0. | |
314 | */ | |
d44265ad | 315 | static void port_init_mac_tables(struct mvgbe_registers *regs) |
9131589a PW |
316 | { |
317 | int table_index; | |
318 | ||
319 | /* Clear DA filter unicast table (Ex_dFUT) */ | |
320 | for (table_index = 0; table_index < 4; ++table_index) | |
d44265ad | 321 | MVGBE_REG_WR(regs->dfut[table_index], 0); |
9131589a PW |
322 | |
323 | for (table_index = 0; table_index < 64; ++table_index) { | |
324 | /* Clear DA filter special multicast table (Ex_dFSMT) */ | |
d44265ad | 325 | MVGBE_REG_WR(regs->dfsmt[table_index], 0); |
9131589a | 326 | /* Clear DA filter other multicast table (Ex_dFOMT) */ |
d44265ad | 327 | MVGBE_REG_WR(regs->dfomt[table_index], 0); |
9131589a PW |
328 | } |
329 | } | |
330 | ||
331 | /* | |
332 | * port_uc_addr - This function Set the port unicast address table | |
333 | * | |
334 | * This function locates the proper entry in the Unicast table for the | |
335 | * specified MAC nibble and sets its properties according to function | |
336 | * parameters. | |
337 | * This function add/removes MAC addresses from the port unicast address | |
338 | * table. | |
339 | * | |
340 | * @uc_nibble Unicast MAC Address last nibble. | |
341 | * @option 0 = Add, 1 = remove address. | |
342 | * | |
343 | * RETURN: 1 if output succeeded. 0 if option parameter is invalid. | |
344 | */ | |
d44265ad | 345 | static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble, |
9131589a PW |
346 | int option) |
347 | { | |
348 | u32 unicast_reg; | |
349 | u32 tbl_offset; | |
350 | u32 reg_offset; | |
351 | ||
352 | /* Locate the Unicast table entry */ | |
353 | uc_nibble = (0xf & uc_nibble); | |
354 | /* Register offset from unicast table base */ | |
355 | tbl_offset = (uc_nibble / 4); | |
356 | /* Entry offset within the above register */ | |
357 | reg_offset = uc_nibble % 4; | |
358 | ||
359 | switch (option) { | |
360 | case REJECT_MAC_ADDR: | |
361 | /* | |
362 | * Clear accepts frame bit at specified unicast | |
363 | * DA table entry | |
364 | */ | |
d44265ad | 365 | unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]); |
9131589a | 366 | unicast_reg &= (0xFF << (8 * reg_offset)); |
d44265ad | 367 | MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg); |
9131589a PW |
368 | break; |
369 | case ACCEPT_MAC_ADDR: | |
370 | /* Set accepts frame bit at unicast DA filter table entry */ | |
d44265ad | 371 | unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]); |
9131589a PW |
372 | unicast_reg &= (0xFF << (8 * reg_offset)); |
373 | unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset)); | |
d44265ad | 374 | MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg); |
9131589a PW |
375 | break; |
376 | default: | |
377 | return 0; | |
378 | } | |
379 | return 1; | |
380 | } | |
381 | ||
382 | /* | |
383 | * port_uc_addr_set - This function Set the port Unicast address. | |
384 | */ | |
e9bf75c9 | 385 | static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr) |
9131589a | 386 | { |
e9bf75c9 | 387 | struct mvgbe_registers *regs = dmvgbe->regs; |
9131589a PW |
388 | u32 mac_h; |
389 | u32 mac_l; | |
390 | ||
391 | mac_l = (p_addr[4] << 8) | (p_addr[5]); | |
392 | mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) | | |
393 | (p_addr[3] << 0); | |
394 | ||
d44265ad AA |
395 | MVGBE_REG_WR(regs->macal, mac_l); |
396 | MVGBE_REG_WR(regs->macah, mac_h); | |
9131589a PW |
397 | |
398 | /* Accept frames of this address */ | |
399 | port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR); | |
400 | } | |
401 | ||
402 | /* | |
d44265ad | 403 | * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory. |
9131589a | 404 | */ |
d44265ad | 405 | static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe) |
9131589a | 406 | { |
d44265ad | 407 | struct mvgbe_rxdesc *p_rx_desc; |
9131589a PW |
408 | int i; |
409 | ||
410 | /* initialize the Rx descriptors ring */ | |
d44265ad | 411 | p_rx_desc = dmvgbe->p_rxdesc; |
9131589a PW |
412 | for (i = 0; i < RINGSZ; i++) { |
413 | p_rx_desc->cmd_sts = | |
d44265ad | 414 | MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT; |
9131589a PW |
415 | p_rx_desc->buf_size = PKTSIZE_ALIGN; |
416 | p_rx_desc->byte_cnt = 0; | |
d44265ad | 417 | p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN; |
9131589a | 418 | if (i == (RINGSZ - 1)) |
d44265ad | 419 | p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc; |
9131589a | 420 | else { |
d44265ad AA |
421 | p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *) |
422 | ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE); | |
9131589a PW |
423 | p_rx_desc = p_rx_desc->nxtdesc_p; |
424 | } | |
425 | } | |
d44265ad | 426 | dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc; |
9131589a PW |
427 | } |
428 | ||
fb731076 CP |
429 | static int __mvgbe_init(struct mvgbe_device *dmvgbe, u8 *enetaddr, |
430 | const char *name) | |
9131589a | 431 | { |
d44265ad | 432 | struct mvgbe_registers *regs = dmvgbe->regs; |
0611c601 SS |
433 | #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \ |
434 | !defined(CONFIG_PHYLIB) && \ | |
fb731076 | 435 | !defined(CONFIG_DM_ETH) && \ |
0611c601 | 436 | defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) |
cad713bf | 437 | int i; |
aba82372 | 438 | #endif |
9131589a | 439 | /* setup RX rings */ |
d44265ad | 440 | mvgbe_init_rx_desc_ring(dmvgbe); |
9131589a PW |
441 | |
442 | /* Clear the ethernet port interrupts */ | |
d44265ad AA |
443 | MVGBE_REG_WR(regs->ic, 0); |
444 | MVGBE_REG_WR(regs->ice, 0); | |
9131589a | 445 | /* Unmask RX buffer and TX end interrupt */ |
d44265ad | 446 | MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL); |
9131589a | 447 | /* Unmask phy and link status changes interrupts */ |
d44265ad | 448 | MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT); |
9131589a PW |
449 | |
450 | set_dram_access(regs); | |
451 | port_init_mac_tables(regs); | |
fb731076 | 452 | port_uc_addr_set(dmvgbe, enetaddr); |
9131589a PW |
453 | |
454 | /* Assign port configuration and command. */ | |
d44265ad AA |
455 | MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL); |
456 | MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE); | |
457 | MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE); | |
9131589a PW |
458 | |
459 | /* Assign port SDMA configuration */ | |
d44265ad AA |
460 | MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE); |
461 | MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL); | |
462 | MVGBE_REG_WR(regs->tqx[0].tqxtbc, | |
463 | (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL); | |
9131589a | 464 | /* Turn off the port/RXUQ bandwidth limitation */ |
d44265ad | 465 | MVGBE_REG_WR(regs->pmtu, 0); |
9131589a PW |
466 | |
467 | /* Set maximum receive buffer to 9700 bytes */ | |
d44265ad AA |
468 | MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE |
469 | | (MVGBE_REG_RD(regs->psc0) & MRU_MASK)); | |
9131589a | 470 | |
f0588fdf | 471 | /* Enable port initially */ |
d44265ad | 472 | MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN); |
f0588fdf | 473 | |
9131589a PW |
474 | /* |
475 | * Set ethernet MTU for leaky bucket mechanism to 0 - this will | |
476 | * disable the leaky bucket mechanism . | |
477 | */ | |
d44265ad | 478 | MVGBE_REG_WR(regs->pmtu, 0); |
9131589a PW |
479 | |
480 | /* Assignment of Rx CRDB of given RXUQ */ | |
d44265ad | 481 | MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr); |
c19a20d5 AA |
482 | /* ensure previous write is done before enabling Rx DMA */ |
483 | isb(); | |
9131589a | 484 | /* Enable port Rx. */ |
d44265ad | 485 | MVGBE_REG_WR(regs->rqc, (1 << RXUQ)); |
9131589a | 486 | |
cd3ca3ff SH |
487 | #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \ |
488 | !defined(CONFIG_PHYLIB) && \ | |
fb731076 | 489 | !defined(CONFIG_DM_ETH) && \ |
cd3ca3ff | 490 | defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) |
cad713bf SK |
491 | /* Wait up to 5s for the link status */ |
492 | for (i = 0; i < 5; i++) { | |
493 | u16 phyadr; | |
494 | ||
fb731076 | 495 | miiphy_read(name, MV_PHY_ADR_REQUEST, |
d44265ad | 496 | MV_PHY_ADR_REQUEST, &phyadr); |
cad713bf | 497 | /* Return if we get link up */ |
fb731076 | 498 | if (miiphy_link(name, phyadr)) |
cad713bf SK |
499 | return 0; |
500 | udelay(1000000); | |
9131589a | 501 | } |
cad713bf | 502 | |
fb731076 | 503 | printf("No link on %s\n", name); |
cad713bf | 504 | return -1; |
9131589a PW |
505 | #endif |
506 | return 0; | |
507 | } | |
508 | ||
fb731076 | 509 | #ifndef CONFIG_DM_ETH |
e9bf75c9 | 510 | static int mvgbe_init(struct eth_device *dev) |
9131589a | 511 | { |
d44265ad | 512 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
e9bf75c9 | 513 | |
fb731076 | 514 | return __mvgbe_init(dmvgbe, dmvgbe->dev.enetaddr, dmvgbe->dev.name); |
e9bf75c9 | 515 | } |
fb731076 | 516 | #endif |
e9bf75c9 CP |
517 | |
518 | static void __mvgbe_halt(struct mvgbe_device *dmvgbe) | |
519 | { | |
d44265ad | 520 | struct mvgbe_registers *regs = dmvgbe->regs; |
9131589a PW |
521 | |
522 | /* Disable all gigE address decoder */ | |
d44265ad | 523 | MVGBE_REG_WR(regs->bare, 0x3f); |
9131589a PW |
524 | |
525 | stop_queue(®s->tqc); | |
526 | stop_queue(®s->rqc); | |
527 | ||
f0588fdf | 528 | /* Disable port */ |
d44265ad | 529 | MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN); |
9131589a | 530 | /* Set port is not reset */ |
d44265ad | 531 | MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4); |
9131589a PW |
532 | #ifdef CONFIG_SYS_MII_MODE |
533 | /* Set MMI interface up */ | |
d44265ad | 534 | MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3); |
9131589a PW |
535 | #endif |
536 | /* Disable & mask ethernet port interrupts */ | |
d44265ad AA |
537 | MVGBE_REG_WR(regs->ic, 0); |
538 | MVGBE_REG_WR(regs->ice, 0); | |
539 | MVGBE_REG_WR(regs->pim, 0); | |
540 | MVGBE_REG_WR(regs->peim, 0); | |
e9bf75c9 CP |
541 | } |
542 | ||
fb731076 | 543 | #ifndef CONFIG_DM_ETH |
e9bf75c9 CP |
544 | static int mvgbe_halt(struct eth_device *dev) |
545 | { | |
546 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); | |
547 | ||
548 | __mvgbe_halt(dmvgbe); | |
9131589a PW |
549 | |
550 | return 0; | |
551 | } | |
fb731076 | 552 | #endif |
9131589a | 553 | |
fb731076 CP |
554 | #ifdef CONFIG_DM_ETH |
555 | static int mvgbe_write_hwaddr(struct udevice *dev) | |
556 | { | |
c69cda25 | 557 | struct eth_pdata *pdata = dev_get_plat(dev); |
fb731076 CP |
558 | |
559 | port_uc_addr_set(dev_get_priv(dev), pdata->enetaddr); | |
560 | ||
561 | return 0; | |
562 | } | |
563 | #else | |
d44265ad | 564 | static int mvgbe_write_hwaddr(struct eth_device *dev) |
b5ce63ed | 565 | { |
d44265ad | 566 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
b5ce63ed PW |
567 | |
568 | /* Programs net device MAC address after initialization */ | |
e9bf75c9 | 569 | port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr); |
b5ce63ed PW |
570 | return 0; |
571 | } | |
fb731076 | 572 | #endif |
b5ce63ed | 573 | |
e9bf75c9 CP |
574 | static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr, |
575 | int datasize) | |
9131589a | 576 | { |
d44265ad AA |
577 | struct mvgbe_registers *regs = dmvgbe->regs; |
578 | struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc; | |
477fa637 | 579 | void *p = (void *)dataptr; |
7b05f5e0 | 580 | u32 cmd_sts; |
e6e556c1 | 581 | u32 txuq0_reg_addr; |
9131589a | 582 | |
477fa637 | 583 | /* Copy buffer if it's misaligned */ |
9131589a | 584 | if ((u32) dataptr & 0x07) { |
477fa637 SK |
585 | if (datasize > PKTSIZE_ALIGN) { |
586 | printf("Non-aligned data too large (%d)\n", | |
587 | datasize); | |
588 | return -1; | |
589 | } | |
590 | ||
d44265ad AA |
591 | memcpy(dmvgbe->p_aligned_txbuf, p, datasize); |
592 | p = dmvgbe->p_aligned_txbuf; | |
9131589a | 593 | } |
477fa637 | 594 | |
d44265ad AA |
595 | p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC; |
596 | p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC; | |
597 | p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA; | |
598 | p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT; | |
477fa637 | 599 | p_txdesc->buf_ptr = (u8 *) p; |
9131589a PW |
600 | p_txdesc->byte_cnt = datasize; |
601 | ||
c19a20d5 | 602 | /* Set this tc desc as zeroth TXUQ */ |
e6e556c1 AG |
603 | txuq0_reg_addr = (u32)®s->tcqdp[TXUQ]; |
604 | writel((u32) p_txdesc, txuq0_reg_addr); | |
c19a20d5 AA |
605 | |
606 | /* ensure tx desc writes above are performed before we start Tx DMA */ | |
607 | isb(); | |
608 | ||
609 | /* Apply send command using zeroth TXUQ */ | |
d44265ad | 610 | MVGBE_REG_WR(regs->tqc, (1 << TXUQ)); |
9131589a PW |
611 | |
612 | /* | |
613 | * wait for packet xmit completion | |
614 | */ | |
7b05f5e0 | 615 | cmd_sts = readl(&p_txdesc->cmd_sts); |
d44265ad | 616 | while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) { |
9131589a | 617 | /* return fail if error is detected */ |
d44265ad AA |
618 | if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) == |
619 | (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) && | |
620 | cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) { | |
1fd92db8 | 621 | printf("Err..(%s) in xmit packet\n", __func__); |
9131589a PW |
622 | return -1; |
623 | } | |
7b05f5e0 | 624 | cmd_sts = readl(&p_txdesc->cmd_sts); |
9131589a PW |
625 | }; |
626 | return 0; | |
627 | } | |
628 | ||
fb731076 | 629 | #ifndef CONFIG_DM_ETH |
e9bf75c9 | 630 | static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize) |
9131589a | 631 | { |
d44265ad | 632 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
e9bf75c9 CP |
633 | |
634 | return __mvgbe_send(dmvgbe, dataptr, datasize); | |
635 | } | |
fb731076 | 636 | #endif |
e9bf75c9 CP |
637 | |
638 | static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp) | |
639 | { | |
d44265ad | 640 | struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr; |
7b05f5e0 SK |
641 | u32 cmd_sts; |
642 | u32 timeout = 0; | |
e6e556c1 | 643 | u32 rxdesc_curr_addr; |
e9bf75c9 CP |
644 | unsigned char *data; |
645 | int rx_bytes = 0; | |
646 | ||
647 | *packetp = NULL; | |
9131589a PW |
648 | |
649 | /* wait untill rx packet available or timeout */ | |
650 | do { | |
d44265ad | 651 | if (timeout < MVGBE_PHY_SMI_TIMEOUT) |
9131589a PW |
652 | timeout++; |
653 | else { | |
1fd92db8 | 654 | debug("%s time out...\n", __func__); |
9131589a PW |
655 | return -1; |
656 | } | |
d44265ad | 657 | } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA); |
9131589a PW |
658 | |
659 | if (p_rxdesc_curr->byte_cnt != 0) { | |
660 | debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n", | |
1fd92db8 | 661 | __func__, (u32) p_rxdesc_curr->byte_cnt, |
9131589a PW |
662 | (u32) p_rxdesc_curr->buf_ptr, |
663 | (u32) p_rxdesc_curr->cmd_sts); | |
664 | } | |
665 | ||
666 | /* | |
667 | * In case received a packet without first/last bits on | |
668 | * OR the error summary bit is on, | |
669 | * the packets needs to be dropeed. | |
670 | */ | |
7b05f5e0 SK |
671 | cmd_sts = readl(&p_rxdesc_curr->cmd_sts); |
672 | ||
673 | if ((cmd_sts & | |
d44265ad AA |
674 | (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) |
675 | != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) { | |
9131589a PW |
676 | |
677 | printf("Err..(%s) Dropping packet spread on" | |
1fd92db8 | 678 | " multiple descriptors\n", __func__); |
9131589a | 679 | |
d44265ad | 680 | } else if (cmd_sts & MVGBE_ERROR_SUMMARY) { |
9131589a PW |
681 | |
682 | printf("Err..(%s) Dropping packet with errors\n", | |
1fd92db8 | 683 | __func__); |
9131589a PW |
684 | |
685 | } else { | |
686 | /* !!! call higher layer processing */ | |
687 | debug("%s: Sending Received packet to" | |
1fd92db8 JH |
688 | " upper layer (net_process_received_packet)\n", |
689 | __func__); | |
9131589a | 690 | |
e9bf75c9 CP |
691 | data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET); |
692 | rx_bytes = (int)(p_rxdesc_curr->byte_cnt - | |
693 | RX_BUF_OFFSET); | |
694 | ||
695 | *packetp = data; | |
9131589a PW |
696 | } |
697 | /* | |
698 | * free these descriptors and point next in the ring | |
699 | */ | |
700 | p_rxdesc_curr->cmd_sts = | |
d44265ad | 701 | MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT; |
9131589a PW |
702 | p_rxdesc_curr->buf_size = PKTSIZE_ALIGN; |
703 | p_rxdesc_curr->byte_cnt = 0; | |
704 | ||
e6e556c1 AG |
705 | rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr; |
706 | writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr); | |
7b05f5e0 | 707 | |
e9bf75c9 CP |
708 | return rx_bytes; |
709 | } | |
710 | ||
fb731076 | 711 | #ifndef CONFIG_DM_ETH |
e9bf75c9 CP |
712 | static int mvgbe_recv(struct eth_device *dev) |
713 | { | |
714 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); | |
715 | uchar *packet; | |
716 | int ret; | |
717 | ||
718 | ret = __mvgbe_recv(dmvgbe, &packet); | |
719 | if (ret < 0) | |
720 | return ret; | |
721 | ||
722 | net_process_received_packet(packet, ret); | |
723 | ||
9131589a PW |
724 | return 0; |
725 | } | |
fb731076 | 726 | #endif |
9131589a | 727 | |
fb731076 CP |
728 | #if defined(CONFIG_PHYLIB) || defined(CONFIG_DM_ETH) |
729 | #if defined(CONFIG_DM_ETH) | |
730 | static struct phy_device *__mvgbe_phy_init(struct udevice *dev, | |
731 | struct mii_dev *bus, | |
732 | phy_interface_t phy_interface, | |
733 | int phyid) | |
734 | #else | |
735 | static struct phy_device *__mvgbe_phy_init(struct eth_device *dev, | |
736 | struct mii_dev *bus, | |
737 | phy_interface_t phy_interface, | |
738 | int phyid) | |
739 | #endif | |
740 | { | |
741 | struct phy_device *phydev; | |
742 | ||
743 | /* Set phy address of the port */ | |
744 | miiphy_write(dev->name, MV_PHY_ADR_REQUEST, MV_PHY_ADR_REQUEST, | |
745 | phyid); | |
746 | ||
747 | phydev = phy_connect(bus, phyid, dev, phy_interface); | |
748 | if (!phydev) { | |
749 | printf("phy_connect failed\n"); | |
750 | return NULL; | |
751 | } | |
752 | ||
753 | phy_config(phydev); | |
754 | phy_startup(phydev); | |
755 | ||
756 | return phydev; | |
757 | } | |
758 | #endif /* CONFIG_PHYLIB || CONFIG_DM_ETH */ | |
759 | ||
760 | #if defined(CONFIG_PHYLIB) && !defined(CONFIG_DM_ETH) | |
cd3ca3ff SH |
761 | int mvgbe_phylib_init(struct eth_device *dev, int phyid) |
762 | { | |
763 | struct mii_dev *bus; | |
764 | struct phy_device *phydev; | |
765 | int ret; | |
766 | ||
767 | bus = mdio_alloc(); | |
768 | if (!bus) { | |
769 | printf("mdio_alloc failed\n"); | |
770 | return -ENOMEM; | |
771 | } | |
6ecf9e21 CP |
772 | bus->read = smi_reg_read; |
773 | bus->write = smi_reg_write; | |
192bc694 | 774 | strcpy(bus->name, dev->name); |
cd3ca3ff SH |
775 | |
776 | ret = mdio_register(bus); | |
777 | if (ret) { | |
778 | printf("mdio_register failed\n"); | |
779 | free(bus); | |
780 | return -ENOMEM; | |
781 | } | |
782 | ||
fb731076 CP |
783 | phydev = __mvgbe_phy_init(dev, bus, PHY_INTERFACE_MODE_RGMII, phyid); |
784 | if (!phydev) | |
cd3ca3ff | 785 | return -ENODEV; |
cd3ca3ff SH |
786 | |
787 | return 0; | |
788 | } | |
789 | #endif | |
790 | ||
fb731076 CP |
791 | static int mvgbe_alloc_buffers(struct mvgbe_device *dmvgbe) |
792 | { | |
793 | dmvgbe->p_rxdesc = memalign(PKTALIGN, | |
794 | MV_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1); | |
795 | if (!dmvgbe->p_rxdesc) | |
796 | goto error1; | |
797 | ||
798 | dmvgbe->p_rxbuf = memalign(PKTALIGN, | |
799 | RINGSZ * PKTSIZE_ALIGN + 1); | |
800 | if (!dmvgbe->p_rxbuf) | |
801 | goto error2; | |
802 | ||
803 | dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN); | |
804 | if (!dmvgbe->p_aligned_txbuf) | |
805 | goto error3; | |
806 | ||
807 | dmvgbe->p_txdesc = memalign(PKTALIGN, sizeof(struct mvgbe_txdesc) + 1); | |
808 | if (!dmvgbe->p_txdesc) | |
809 | goto error4; | |
810 | ||
811 | return 0; | |
812 | ||
813 | error4: | |
814 | free(dmvgbe->p_aligned_txbuf); | |
815 | error3: | |
816 | free(dmvgbe->p_rxbuf); | |
817 | error2: | |
818 | free(dmvgbe->p_rxdesc); | |
819 | error1: | |
820 | return -ENOMEM; | |
821 | } | |
822 | ||
823 | #ifndef CONFIG_DM_ETH | |
b75d8dc5 | 824 | int mvgbe_initialize(struct bd_info *bis) |
9131589a | 825 | { |
d44265ad | 826 | struct mvgbe_device *dmvgbe; |
9131589a PW |
827 | struct eth_device *dev; |
828 | int devnum; | |
fb731076 | 829 | int ret; |
d44265ad | 830 | u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS; |
9131589a | 831 | |
d44265ad | 832 | for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) { |
9131589a PW |
833 | /*skip if port is configured not to use */ |
834 | if (used_ports[devnum] == 0) | |
835 | continue; | |
836 | ||
d44265ad | 837 | dmvgbe = malloc(sizeof(struct mvgbe_device)); |
d44265ad | 838 | if (!dmvgbe) |
fb731076 | 839 | return -ENOMEM; |
9131589a | 840 | |
d44265ad | 841 | memset(dmvgbe, 0, sizeof(struct mvgbe_device)); |
fb731076 CP |
842 | ret = mvgbe_alloc_buffers(dmvgbe); |
843 | if (ret) { | |
9131589a | 844 | printf("Err.. %s Failed to allocate memory\n", |
1fd92db8 | 845 | __func__); |
fb731076 CP |
846 | free(dmvgbe); |
847 | return ret; | |
9131589a PW |
848 | } |
849 | ||
d44265ad | 850 | dev = &dmvgbe->dev; |
9131589a | 851 | |
f6add132 | 852 | /* must be less than sizeof(dev->name) */ |
9131589a PW |
853 | sprintf(dev->name, "egiga%d", devnum); |
854 | ||
9131589a PW |
855 | switch (devnum) { |
856 | case 0: | |
d44265ad | 857 | dmvgbe->regs = (void *)MVGBE0_BASE; |
9131589a | 858 | break; |
d44265ad | 859 | #if defined(MVGBE1_BASE) |
9131589a | 860 | case 1: |
d44265ad | 861 | dmvgbe->regs = (void *)MVGBE1_BASE; |
9131589a | 862 | break; |
d44265ad | 863 | #endif |
9131589a PW |
864 | default: /* this should never happen */ |
865 | printf("Err..(%s) Invalid device number %d\n", | |
1fd92db8 | 866 | __func__, devnum); |
9131589a PW |
867 | return -1; |
868 | } | |
869 | ||
d44265ad AA |
870 | dev->init = (void *)mvgbe_init; |
871 | dev->halt = (void *)mvgbe_halt; | |
872 | dev->send = (void *)mvgbe_send; | |
873 | dev->recv = (void *)mvgbe_recv; | |
874 | dev->write_hwaddr = (void *)mvgbe_write_hwaddr; | |
9131589a PW |
875 | |
876 | eth_register(dev); | |
877 | ||
cd3ca3ff SH |
878 | #if defined(CONFIG_PHYLIB) |
879 | mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum); | |
880 | #elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII) | |
5a49f174 JH |
881 | int retval; |
882 | struct mii_dev *mdiodev = mdio_alloc(); | |
883 | if (!mdiodev) | |
884 | return -ENOMEM; | |
885 | strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN); | |
886 | mdiodev->read = smi_reg_read; | |
887 | mdiodev->write = smi_reg_write; | |
888 | ||
889 | retval = mdio_register(mdiodev); | |
890 | if (retval < 0) | |
891 | return retval; | |
9131589a | 892 | /* Set phy address of the port */ |
d44265ad AA |
893 | miiphy_write(dev->name, MV_PHY_ADR_REQUEST, |
894 | MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum); | |
9131589a PW |
895 | #endif |
896 | } | |
897 | return 0; | |
0b785ddd | 898 | } |
fb731076 CP |
899 | #endif |
900 | ||
901 | #ifdef CONFIG_DM_ETH | |
902 | static int mvgbe_port_is_fixed_link(struct mvgbe_device *dmvgbe) | |
903 | { | |
904 | return dmvgbe->phyaddr > PHY_MAX_ADDR; | |
905 | } | |
906 | ||
907 | static int mvgbe_start(struct udevice *dev) | |
908 | { | |
c69cda25 | 909 | struct eth_pdata *pdata = dev_get_plat(dev); |
fb731076 CP |
910 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
911 | int ret; | |
912 | ||
913 | ret = __mvgbe_init(dmvgbe, pdata->enetaddr, dev->name); | |
914 | if (ret) | |
915 | return ret; | |
916 | ||
917 | if (!mvgbe_port_is_fixed_link(dmvgbe)) { | |
918 | dmvgbe->phydev = __mvgbe_phy_init(dev, dmvgbe->bus, | |
919 | dmvgbe->phy_interface, | |
920 | dmvgbe->phyaddr); | |
921 | if (!dmvgbe->phydev) | |
922 | return -ENODEV; | |
923 | } | |
924 | ||
925 | return 0; | |
926 | } | |
927 | ||
928 | static int mvgbe_send(struct udevice *dev, void *packet, int length) | |
929 | { | |
930 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); | |
931 | ||
932 | return __mvgbe_send(dmvgbe, packet, length); | |
933 | } | |
934 | ||
935 | static int mvgbe_recv(struct udevice *dev, int flags, uchar **packetp) | |
936 | { | |
937 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); | |
938 | ||
939 | return __mvgbe_recv(dmvgbe, packetp); | |
940 | } | |
941 | ||
942 | static void mvgbe_stop(struct udevice *dev) | |
943 | { | |
944 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); | |
945 | ||
946 | __mvgbe_halt(dmvgbe); | |
947 | } | |
948 | ||
949 | static int mvgbe_probe(struct udevice *dev) | |
950 | { | |
c69cda25 | 951 | struct eth_pdata *pdata = dev_get_plat(dev); |
fb731076 CP |
952 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
953 | struct mii_dev *bus; | |
954 | int ret; | |
955 | ||
956 | ret = mvgbe_alloc_buffers(dmvgbe); | |
957 | if (ret) | |
958 | return ret; | |
959 | ||
960 | dmvgbe->regs = (void __iomem *)pdata->iobase; | |
961 | ||
962 | bus = mdio_alloc(); | |
963 | if (!bus) { | |
964 | printf("Failed to allocate MDIO bus\n"); | |
965 | return -ENOMEM; | |
966 | } | |
967 | ||
968 | bus->read = smi_reg_read; | |
969 | bus->write = smi_reg_write; | |
970 | snprintf(bus->name, sizeof(bus->name), dev->name); | |
971 | bus->priv = dmvgbe; | |
972 | dmvgbe->bus = bus; | |
973 | ||
974 | ret = mdio_register(bus); | |
975 | if (ret < 0) | |
976 | return ret; | |
977 | ||
978 | return 0; | |
979 | } | |
980 | ||
981 | static const struct eth_ops mvgbe_ops = { | |
982 | .start = mvgbe_start, | |
983 | .send = mvgbe_send, | |
984 | .recv = mvgbe_recv, | |
985 | .stop = mvgbe_stop, | |
986 | .write_hwaddr = mvgbe_write_hwaddr, | |
987 | }; | |
988 | ||
d1998a9f | 989 | static int mvgbe_of_to_plat(struct udevice *dev) |
fb731076 | 990 | { |
c69cda25 | 991 | struct eth_pdata *pdata = dev_get_plat(dev); |
fb731076 CP |
992 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
993 | void *blob = (void *)gd->fdt_blob; | |
994 | int node = dev_of_offset(dev); | |
995 | const char *phy_mode; | |
996 | int fl_node; | |
997 | int pnode; | |
998 | unsigned long addr; | |
999 | ||
2548493a | 1000 | pdata->iobase = dev_read_addr(dev); |
fb731076 CP |
1001 | pdata->phy_interface = -1; |
1002 | ||
1003 | pnode = fdt_node_offset_by_compatible(blob, node, | |
1004 | "marvell,kirkwood-eth-port"); | |
1005 | ||
1006 | /* Get phy-mode / phy_interface from DT */ | |
1007 | phy_mode = fdt_getprop(gd->fdt_blob, pnode, "phy-mode", NULL); | |
1008 | if (phy_mode) | |
1009 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); | |
92f129f4 CP |
1010 | else |
1011 | pdata->phy_interface = PHY_INTERFACE_MODE_GMII; | |
fb731076 CP |
1012 | |
1013 | dmvgbe->phy_interface = pdata->phy_interface; | |
1014 | ||
1015 | /* fetch 'fixed-link' property */ | |
1016 | fl_node = fdt_subnode_offset(blob, pnode, "fixed-link"); | |
1017 | if (fl_node != -FDT_ERR_NOTFOUND) { | |
1018 | /* set phy_addr to invalid value for fixed link */ | |
1019 | dmvgbe->phyaddr = PHY_MAX_ADDR + 1; | |
1020 | dmvgbe->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex"); | |
1021 | dmvgbe->speed = fdtdec_get_int(blob, fl_node, "speed", 0); | |
1022 | } else { | |
1023 | /* Now read phyaddr from DT */ | |
1024 | addr = fdtdec_lookup_phandle(blob, pnode, "phy-handle"); | |
1025 | if (addr > 0) | |
1026 | dmvgbe->phyaddr = fdtdec_get_int(blob, addr, "reg", 0); | |
1027 | } | |
1028 | ||
1029 | return 0; | |
1030 | } | |
1031 | ||
1032 | static const struct udevice_id mvgbe_ids[] = { | |
1033 | { .compatible = "marvell,kirkwood-eth" }, | |
1034 | { } | |
1035 | }; | |
1036 | ||
1037 | U_BOOT_DRIVER(mvgbe) = { | |
1038 | .name = "mvgbe", | |
1039 | .id = UCLASS_ETH, | |
1040 | .of_match = mvgbe_ids, | |
d1998a9f | 1041 | .of_to_plat = mvgbe_of_to_plat, |
fb731076 CP |
1042 | .probe = mvgbe_probe, |
1043 | .ops = &mvgbe_ops, | |
41575d8e | 1044 | .priv_auto = sizeof(struct mvgbe_device), |
caa4daa2 | 1045 | .plat_auto = sizeof(struct eth_pdata), |
fb731076 CP |
1046 | }; |
1047 | #endif /* CONFIG_DM_ETH */ |