blob: 8b18ad0b0d5ef54fd8306ac0701a0528f34112e5 [file] [log] [blame]
Thomas Chou7bb1b9b2010-04-20 12:49:52 +08001/*
2 * Altera 10/100/1000 triple speed ethernet mac driver
3 *
4 * Copyright (C) 2008 Altera Corporation.
5 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <config.h>
12#include <common.h>
13#include <malloc.h>
14#include <net.h>
15#include <command.h>
16#include <asm/cache.h>
17#include <asm/dma-mapping.h>
18#include <miiphy.h>
19#include "altera_tse.h"
20
21/* sgdma debug - print descriptor */
22static void alt_sgdma_print_desc(volatile struct alt_sgdma_descriptor *desc)
23{
24 debug("SGDMA DEBUG :\n");
25 debug("desc->source : 0x%x \n", (unsigned int)desc->source);
26 debug("desc->destination : 0x%x \n", (unsigned int)desc->destination);
27 debug("desc->next : 0x%x \n", (unsigned int)desc->next);
28 debug("desc->source_pad : 0x%x \n", (unsigned int)desc->source_pad);
29 debug("desc->destination_pad : 0x%x \n",
30 (unsigned int)desc->destination_pad);
31 debug("desc->next_pad : 0x%x \n", (unsigned int)desc->next_pad);
32 debug("desc->bytes_to_transfer : 0x%x \n",
33 (unsigned int)desc->bytes_to_transfer);
34 debug("desc->actual_bytes_transferred : 0x%x \n",
35 (unsigned int)desc->actual_bytes_transferred);
36 debug("desc->descriptor_status : 0x%x \n",
37 (unsigned int)desc->descriptor_status);
38 debug("desc->descriptor_control : 0x%x \n",
39 (unsigned int)desc->descriptor_control);
40}
41
42/* This is a generic routine that the SGDMA mode-specific routines
43 * call to populate a descriptor.
44 * arg1 :pointer to first SGDMA descriptor.
45 * arg2 :pointer to next SGDMA descriptor.
46 * arg3 :Address to where data to be written.
47 * arg4 :Address from where data to be read.
48 * arg5 :no of byte to transaction.
49 * arg6 :variable indicating to generate start of packet or not
50 * arg7 :read fixed
51 * arg8 :write fixed
52 * arg9 :read burst
53 * arg10 :write burst
54 * arg11 :atlantic_channel number
55 */
56static void alt_sgdma_construct_descriptor_burst(
57 volatile struct alt_sgdma_descriptor *desc,
58 volatile struct alt_sgdma_descriptor *next,
59 unsigned int *read_addr,
60 unsigned int *write_addr,
61 unsigned short length_or_eop,
62 int generate_eop,
63 int read_fixed,
64 int write_fixed_or_sop,
65 int read_burst,
66 int write_burst,
67 unsigned char atlantic_channel)
68{
69 /*
70 * Mark the "next" descriptor as "not" owned by hardware. This prevents
71 * The SGDMA controller from continuing to process the chain. This is
72 * done as a single IO write to bypass cache, without flushing
73 * the entire descriptor, since only the 8-bit descriptor status must
74 * be flushed.
75 */
76 if (!next)
77 debug("Next descriptor not defined!!\n");
78
79 next->descriptor_control = (next->descriptor_control &
80 ~ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK);
81
82 desc->source = (unsigned int *)((unsigned int)read_addr & 0x1FFFFFFF);
83 desc->destination =
84 (unsigned int *)((unsigned int)write_addr & 0x1FFFFFFF);
85 desc->next = (unsigned int *)((unsigned int)next & 0x1FFFFFFF);
86 desc->source_pad = 0x0;
87 desc->destination_pad = 0x0;
88 desc->next_pad = 0x0;
89 desc->bytes_to_transfer = length_or_eop;
90 desc->actual_bytes_transferred = 0;
91 desc->descriptor_status = 0x0;
92
93 /* SGDMA burst not currently supported */
94 desc->read_burst = 0;
95 desc->write_burst = 0;
96
97 /*
98 * Set the descriptor control block as follows:
99 * - Set "owned by hardware" bit
100 * - Optionally set "generate EOP" bit
101 * - Optionally set the "read from fixed address" bit
102 * - Optionally set the "write to fixed address bit (which serves
103 * serves as a "generate SOP" control bit in memory-to-stream mode).
104 * - Set the 4-bit atlantic channel, if specified
105 *
106 * Note this step is performed after all other descriptor information
107 * has been filled out so that, if the controller already happens to be
108 * pointing at this descriptor, it will not run (via the "owned by
109 * hardware" bit) until all other descriptor has been set up.
110 */
111
112 desc->descriptor_control =
113 ((ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK) |
114 (generate_eop ?
115 ALT_SGDMA_DESCRIPTOR_CONTROL_GENERATE_EOP_MSK : 0x0) |
116 (read_fixed ?
117 ALT_SGDMA_DESCRIPTOR_CONTROL_READ_FIXED_ADDRESS_MSK : 0x0) |
118 (write_fixed_or_sop ?
119 ALT_SGDMA_DESCRIPTOR_CONTROL_WRITE_FIXED_ADDRESS_MSK : 0x0) |
120 (atlantic_channel ? ((atlantic_channel & 0x0F) << 3) : 0)
121 );
122}
123
124static int alt_sgdma_do_sync_transfer(volatile struct alt_sgdma_registers *dev,
125 volatile struct alt_sgdma_descriptor *desc)
126{
127 unsigned int status;
128 int counter = 0;
129
130 /* Wait for any pending transfers to complete */
131 alt_sgdma_print_desc(desc);
132 status = dev->status;
133
134 counter = 0;
135 while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK) {
136 if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
137 break;
138 }
139
140 if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
141 debug("Timeout waiting sgdma in do sync!\n");
142
143 /*
144 * Clear any (previous) status register information
145 * that might occlude our error checking later.
146 */
147 dev->status = 0xFF;
148
149 /* Point the controller at the descriptor */
150 dev->next_descriptor_pointer = (unsigned int)desc & 0x1FFFFFFF;
151 debug("next desc in sgdma 0x%x\n",
152 (unsigned int)dev->next_descriptor_pointer);
153
154 /*
155 * Set up SGDMA controller to:
156 * - Disable interrupt generation
157 * - Run once a valid descriptor is written to controller
158 * - Stop on an error with any particular descriptor
159 */
160 dev->control = (ALT_SGDMA_CONTROL_RUN_MSK |
161 ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK);
162
163 /* Wait for the descriptor (chain) to complete */
164 status = dev->status;
165 debug("wait for sgdma....");
166 while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK)
167 ;
168 debug("done\n");
169
170 /* Clear Run */
171 dev->control = (dev->control & (~ALT_SGDMA_CONTROL_RUN_MSK));
172
173 /* Get & clear status register contents */
174 status = dev->status;
175 dev->status = 0xFF;
176
177 /* we really should check if the transfer completes properly */
178 debug("tx sgdma status = 0x%x", status);
179 return 0;
180}
181
182static int alt_sgdma_do_async_transfer(volatile struct alt_sgdma_registers *dev,
183 volatile struct alt_sgdma_descriptor *desc)
184{
185 unsigned int status;
186 int counter = 0;
187
188 /* Wait for any pending transfers to complete */
189 alt_sgdma_print_desc(desc);
190 status = dev->status;
191
192 counter = 0;
193 while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK) {
194 if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
195 break;
196 }
197
198 if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
199 debug("Timeout waiting sgdma in do async!\n");
200
201 /*
Joachim Foersterfc883c22011-10-25 22:39:54 +0000202 * Clear the RUN bit in the control register. This is needed
203 * to restart the SGDMA engine later on.
204 */
205 dev->control = 0;
206
207 /*
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800208 * Clear any (previous) status register information
209 * that might occlude our error checking later.
210 */
211 dev->status = 0xFF;
212
213 /* Point the controller at the descriptor */
214 dev->next_descriptor_pointer = (unsigned int)desc & 0x1FFFFFFF;
215
216 /*
217 * Set up SGDMA controller to:
218 * - Disable interrupt generation
219 * - Run once a valid descriptor is written to controller
220 * - Stop on an error with any particular descriptor
221 */
222 dev->control = (ALT_SGDMA_CONTROL_RUN_MSK |
223 ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK);
224
225 /* we really should check if the transfer completes properly */
226 return 0;
227}
228
229/* u-boot interface */
230static int tse_adjust_link(struct altera_tse_priv *priv)
231{
232 unsigned int refvar;
233
234 refvar = priv->mac_dev->command_config.image;
235
236 if (!(priv->duplexity))
237 refvar |= ALTERA_TSE_CMD_HD_ENA_MSK;
238 else
239 refvar &= ~ALTERA_TSE_CMD_HD_ENA_MSK;
240
241 switch (priv->speed) {
242 case 1000:
243 refvar |= ALTERA_TSE_CMD_ETH_SPEED_MSK;
244 refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
245 break;
246 case 100:
247 refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
248 refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
249 break;
250 case 10:
251 refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
252 refvar |= ALTERA_TSE_CMD_ENA_10_MSK;
253 break;
254 }
255 priv->mac_dev->command_config.image = refvar;
256
257 return 0;
258}
259
260static int tse_eth_send(struct eth_device *dev,
261 volatile void *packet, int length)
262{
263 struct altera_tse_priv *priv = dev->priv;
264 volatile struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
265 volatile struct alt_sgdma_descriptor *tx_desc =
266 (volatile struct alt_sgdma_descriptor *)priv->tx_desc;
267
268 volatile struct alt_sgdma_descriptor *tx_desc_cur =
269 (volatile struct alt_sgdma_descriptor *)&tx_desc[0];
270
271 flush_dcache((unsigned long)packet, length);
272 alt_sgdma_construct_descriptor_burst(
273 (volatile struct alt_sgdma_descriptor *)&tx_desc[0],
274 (volatile struct alt_sgdma_descriptor *)&tx_desc[1],
275 (unsigned int *)packet, /* read addr */
276 (unsigned int *)0,
277 length, /* length or EOP ,will change for each tx */
278 0x1, /* gen eop */
279 0x0, /* read fixed */
280 0x1, /* write fixed or sop */
281 0x0, /* read burst */
282 0x0, /* write burst */
283 0x0 /* channel */
284 );
285 debug("TX Packet @ 0x%x,0x%x bytes", (unsigned int)packet, length);
286
287 /* send the packet */
288 debug("sending packet\n");
289 alt_sgdma_do_sync_transfer(tx_sgdma, tx_desc_cur);
290 debug("sent %d bytes\n", tx_desc_cur->actual_bytes_transferred);
291 return tx_desc_cur->actual_bytes_transferred;
292}
293
294static int tse_eth_rx(struct eth_device *dev)
295{
296 int packet_length = 0;
297 struct altera_tse_priv *priv = dev->priv;
298 volatile struct alt_sgdma_descriptor *rx_desc =
299 (volatile struct alt_sgdma_descriptor *)priv->rx_desc;
300 volatile struct alt_sgdma_descriptor *rx_desc_cur = &rx_desc[0];
301
302 if (rx_desc_cur->descriptor_status &
303 ALT_SGDMA_DESCRIPTOR_STATUS_TERMINATED_BY_EOP_MSK) {
304 debug("got packet\n");
305 packet_length = rx_desc->actual_bytes_transferred;
306 NetReceive(NetRxPackets[0], packet_length);
307
308 /* start descriptor again */
309 flush_dcache((unsigned long)(NetRxPackets[0]), PKTSIZE_ALIGN);
310 alt_sgdma_construct_descriptor_burst(
311 (volatile struct alt_sgdma_descriptor *)&rx_desc[0],
312 (volatile struct alt_sgdma_descriptor *)&rx_desc[1],
313 (unsigned int)0x0, /* read addr */
314 (unsigned int *)NetRxPackets[0],
315 0x0, /* length or EOP */
316 0x0, /* gen eop */
317 0x0, /* read fixed */
318 0x0, /* write fixed or sop */
319 0x0, /* read burst */
320 0x0, /* write burst */
321 0x0 /* channel */
322 );
323
324 /* setup the sgdma */
325 alt_sgdma_do_async_transfer(priv->sgdma_rx, &rx_desc[0]);
326 }
327
328 return -1;
329}
330
331static void tse_eth_halt(struct eth_device *dev)
332{
333 /* don't do anything! */
334 /* this gets called after each uboot */
335 /* network command. don't need to reset the thing all of the time */
336}
337
338static void tse_eth_reset(struct eth_device *dev)
339{
340 /* stop sgdmas, disable tse receive */
341 struct altera_tse_priv *priv = dev->priv;
342 volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
343 volatile struct alt_sgdma_registers *rx_sgdma = priv->sgdma_rx;
344 volatile struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
345 int counter;
346 volatile struct alt_sgdma_descriptor *rx_desc =
347 (volatile struct alt_sgdma_descriptor *)&priv->rx_desc[0];
348
349 /* clear rx desc & wait for sgdma to complete */
350 rx_desc->descriptor_control = 0;
351 rx_sgdma->control = 0;
352 counter = 0;
353 while (rx_sgdma->status & ALT_SGDMA_STATUS_BUSY_MSK) {
354 if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
355 break;
356 }
357
358 if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR) {
359 debug("Timeout waiting for rx sgdma!\n");
Joachim Foerster953cc0c2011-10-17 05:24:43 +0000360 rx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
361 rx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800362 }
363
364 counter = 0;
365 tx_sgdma->control = 0;
366 while (tx_sgdma->status & ALT_SGDMA_STATUS_BUSY_MSK) {
367 if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
368 break;
369 }
370
371 if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR) {
372 debug("Timeout waiting for tx sgdma!\n");
Joachim Foerster953cc0c2011-10-17 05:24:43 +0000373 tx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
374 tx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800375 }
376 /* reset the mac */
377 mac_dev->command_config.bits.transmit_enable = 1;
378 mac_dev->command_config.bits.receive_enable = 1;
379 mac_dev->command_config.bits.software_reset = 1;
380
381 counter = 0;
382 while (mac_dev->command_config.bits.software_reset) {
383 if (counter++ > ALT_TSE_SW_RESET_WATCHDOG_CNTR)
384 break;
385 }
386
387 if (counter >= ALT_TSE_SW_RESET_WATCHDOG_CNTR)
388 debug("TSEMAC SW reset bit never cleared!\n");
389}
390
391static int tse_mdio_read(struct altera_tse_priv *priv, unsigned int regnum)
392{
393 volatile struct alt_tse_mac *mac_dev;
394 unsigned int *mdio_regs;
395 unsigned int data;
396 u16 value;
397
398 mac_dev = priv->mac_dev;
399
400 /* set mdio address */
401 mac_dev->mdio_phy1_addr = priv->phyaddr;
402 mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
403
404 /* get the data */
405 data = mdio_regs[regnum];
406
407 value = data & 0xffff;
408
409 return value;
410}
411
412static int tse_mdio_write(struct altera_tse_priv *priv, unsigned int regnum,
413 unsigned int value)
414{
415 volatile struct alt_tse_mac *mac_dev;
416 unsigned int *mdio_regs;
417 unsigned int data;
418
419 mac_dev = priv->mac_dev;
420
421 /* set mdio address */
422 mac_dev->mdio_phy1_addr = priv->phyaddr;
423 mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
424
425 /* get the data */
426 data = (unsigned int)value;
427
428 mdio_regs[regnum] = data;
429
430 return 0;
431}
432
433/* MDIO access to phy */
434#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -0400435static int altera_tse_miiphy_write(const char *devname, unsigned char addr,
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800436 unsigned char reg, unsigned short value)
437{
438 struct eth_device *dev;
439 struct altera_tse_priv *priv;
440 dev = eth_get_dev_by_name(devname);
441 priv = dev->priv;
442
443 tse_mdio_write(priv, (uint) reg, (uint) value);
444
445 return 0;
446}
447
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -0400448static int altera_tse_miiphy_read(const char *devname, unsigned char addr,
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800449 unsigned char reg, unsigned short *value)
450{
451 struct eth_device *dev;
452 struct altera_tse_priv *priv;
453 volatile struct alt_tse_mac *mac_dev;
454 unsigned int *mdio_regs;
455
456 dev = eth_get_dev_by_name(devname);
457 priv = dev->priv;
458
459 mac_dev = priv->mac_dev;
460 mac_dev->mdio_phy1_addr = (int)addr;
461 mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
462
463 *value = 0xffff & mdio_regs[reg];
464
465 return 0;
466
467}
468#endif
469
470/*
471 * Also copied from tsec.c
472 */
473/* Parse the status register for link, and then do
474 * auto-negotiation
475 */
476static uint mii_parse_sr(uint mii_reg, struct altera_tse_priv *priv)
477{
478 /*
479 * Wait if the link is up, and autonegotiation is in progress
480 * (ie - we're capable and it's not done)
481 */
482 mii_reg = tse_mdio_read(priv, MIIM_STATUS);
483
Mike Frysingerd63ee712010-12-23 15:40:12 -0500484 if (!(mii_reg & MIIM_STATUS_LINK) && (mii_reg & BMSR_ANEGCAPABLE)
485 && !(mii_reg & BMSR_ANEGCOMPLETE)) {
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800486 int i = 0;
487
488 puts("Waiting for PHY auto negotiation to complete");
Mike Frysingerd63ee712010-12-23 15:40:12 -0500489 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800490 /*
491 * Timeout reached ?
492 */
493 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
494 puts(" TIMEOUT !\n");
495 priv->link = 0;
496 return 0;
497 }
498
499 if ((i++ % 1000) == 0)
500 putc('.');
501 udelay(1000); /* 1 ms */
502 mii_reg = tse_mdio_read(priv, MIIM_STATUS);
503 }
504 puts(" done\n");
505 priv->link = 1;
506 udelay(500000); /* another 500 ms (results in faster booting) */
507 } else {
508 if (mii_reg & MIIM_STATUS_LINK) {
509 debug("Link is up\n");
510 priv->link = 1;
511 } else {
512 debug("Link is down\n");
513 priv->link = 0;
514 }
515 }
516
517 return 0;
518}
519
520/* Parse the 88E1011's status register for speed and duplex
521 * information
522 */
523static uint mii_parse_88E1011_psr(uint mii_reg, struct altera_tse_priv *priv)
524{
525 uint speed;
526
527 mii_reg = tse_mdio_read(priv, MIIM_88E1011_PHY_STATUS);
528
529 if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
530 !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
531 int i = 0;
532
533 puts("Waiting for PHY realtime link");
534 while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
535 /* Timeout reached ? */
536 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
537 puts(" TIMEOUT !\n");
538 priv->link = 0;
539 break;
540 }
541
542 if ((i++ == 1000) == 0) {
543 i = 0;
544 puts(".");
545 }
546 udelay(1000); /* 1 ms */
547 mii_reg = tse_mdio_read(priv, MIIM_88E1011_PHY_STATUS);
548 }
549 puts(" done\n");
550 udelay(500000); /* another 500 ms (results in faster booting) */
551 } else {
552 if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
553 priv->link = 1;
554 else
555 priv->link = 0;
556 }
557
558 if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
559 priv->duplexity = 1;
560 else
561 priv->duplexity = 0;
562
563 speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
564
565 switch (speed) {
566 case MIIM_88E1011_PHYSTAT_GBIT:
567 priv->speed = 1000;
568 debug("PHY Speed is 1000Mbit\n");
569 break;
570 case MIIM_88E1011_PHYSTAT_100:
571 debug("PHY Speed is 100Mbit\n");
572 priv->speed = 100;
573 break;
574 default:
575 debug("PHY Speed is 10Mbit\n");
576 priv->speed = 10;
577 }
578
579 return 0;
580}
581
582static uint mii_m88e1111s_setmode_sr(uint mii_reg, struct altera_tse_priv *priv)
583{
584 uint mii_data = tse_mdio_read(priv, mii_reg);
585 mii_data &= 0xfff0;
Joachim Foerster7ce99292011-10-25 22:39:57 +0000586 if ((priv->flags >= 1) && (priv->flags <= 4))
587 mii_data |= 0xb;
588 else if (priv->flags == 5)
589 mii_data |= 0x4;
590
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800591 return mii_data;
592}
593
594static uint mii_m88e1111s_setmode_cr(uint mii_reg, struct altera_tse_priv *priv)
595{
596 uint mii_data = tse_mdio_read(priv, mii_reg);
597 mii_data &= ~0x82;
Joachim Foerster7ce99292011-10-25 22:39:57 +0000598 if ((priv->flags >= 1) && (priv->flags <= 4))
599 mii_data |= 0x82;
600
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800601 return mii_data;
602}
603
604/*
605 * Returns which value to write to the control register.
606 * For 10/100, the value is slightly different
607 */
608static uint mii_cr_init(uint mii_reg, struct altera_tse_priv *priv)
609{
610 return MIIM_CONTROL_INIT;
611}
612
613/*
614 * PHY & MDIO code
615 * Need to add SGMII stuff
616 *
617 */
618
619static struct phy_info phy_info_M88E1111S = {
620 0x01410cc,
621 "Marvell 88E1111S",
622 4,
623 (struct phy_cmd[]){ /* config */
624 /* Reset and configure the PHY */
625 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
626 {MIIM_88E1111_PHY_EXT_SR, 0x848f,
627 &mii_m88e1111s_setmode_sr},
628 /* Delay RGMII TX and RX */
629 {MIIM_88E1111_PHY_EXT_CR, 0x0cd2,
630 &mii_m88e1111s_setmode_cr},
631 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
632 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
633 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
634 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
635 {miim_end,}
636 },
637 (struct phy_cmd[]){ /* startup */
638 /* Status is read once to clear old link state */
639 {MIIM_STATUS, miim_read, NULL},
640 /* Auto-negotiate */
641 {MIIM_STATUS, miim_read, &mii_parse_sr},
642 /* Read the status */
643 {MIIM_88E1011_PHY_STATUS, miim_read,
644 &mii_parse_88E1011_psr},
645 {miim_end,}
646 },
647 (struct phy_cmd[]){ /* shutdown */
648 {miim_end,}
649 },
650};
651
652/* a generic flavor. */
653static struct phy_info phy_info_generic = {
654 0,
655 "Unknown/Generic PHY",
656 32,
657 (struct phy_cmd[]){ /* config */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500658 {MII_BMCR, BMCR_RESET, NULL},
659 {MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART, NULL},
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800660 {miim_end,}
661 },
662 (struct phy_cmd[]){ /* startup */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500663 {MII_BMSR, miim_read, NULL},
664 {MII_BMSR, miim_read, &mii_parse_sr},
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800665 {miim_end,}
666 },
667 (struct phy_cmd[]){ /* shutdown */
668 {miim_end,}
669 }
670};
671
672static struct phy_info *phy_info[] = {
673 &phy_info_M88E1111S,
674 NULL
675};
676
677 /* Grab the identifier of the device's PHY, and search through
678 * all of the known PHYs to see if one matches. If so, return
679 * it, if not, return NULL
680 */
681static struct phy_info *get_phy_info(struct eth_device *dev)
682{
683 struct altera_tse_priv *priv = (struct altera_tse_priv *)dev->priv;
684 uint phy_reg, phy_ID;
685 int i;
686 struct phy_info *theInfo = NULL;
687
688 /* Grab the bits from PHYIR1, and put them in the upper half */
689 phy_reg = tse_mdio_read(priv, MIIM_PHYIR1);
690 phy_ID = (phy_reg & 0xffff) << 16;
691
692 /* Grab the bits from PHYIR2, and put them in the lower half */
693 phy_reg = tse_mdio_read(priv, MIIM_PHYIR2);
694 phy_ID |= (phy_reg & 0xffff);
695
696 /* loop through all the known PHY types, and find one that */
697 /* matches the ID we read from the PHY. */
698 for (i = 0; phy_info[i]; i++) {
699 if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
700 theInfo = phy_info[i];
701 break;
702 }
703 }
704
705 if (theInfo == NULL) {
706 theInfo = &phy_info_generic;
707 debug("%s: No support for PHY id %x; assuming generic\n",
708 dev->name, phy_ID);
709 } else
710 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
711
712 return theInfo;
713}
714
715/* Execute the given series of commands on the given device's
716 * PHY, running functions as necessary
717 */
718static void phy_run_commands(struct altera_tse_priv *priv, struct phy_cmd *cmd)
719{
720 int i;
721 uint result;
722
723 for (i = 0; cmd->mii_reg != miim_end; i++) {
724 if (cmd->mii_data == miim_read) {
725 result = tse_mdio_read(priv, cmd->mii_reg);
726
727 if (cmd->funct != NULL)
728 (*(cmd->funct)) (result, priv);
729
730 } else {
731 if (cmd->funct != NULL)
732 result = (*(cmd->funct)) (cmd->mii_reg, priv);
733 else
734 result = cmd->mii_data;
735
736 tse_mdio_write(priv, cmd->mii_reg, result);
737
738 }
739 cmd++;
740 }
741}
742
743/* Phy init code */
744static int init_phy(struct eth_device *dev)
745{
746 struct altera_tse_priv *priv = (struct altera_tse_priv *)dev->priv;
747 struct phy_info *curphy;
748
749 /* Get the cmd structure corresponding to the attached
750 * PHY */
751 curphy = get_phy_info(dev);
752
753 if (curphy == NULL) {
754 priv->phyinfo = NULL;
755 debug("%s: No PHY found\n", dev->name);
756
757 return 0;
758 } else
759 debug("%s found\n", curphy->name);
760 priv->phyinfo = curphy;
761
762 phy_run_commands(priv, priv->phyinfo->config);
763
764 return 1;
765}
766
Thomas Chou53d01502010-04-27 20:15:10 +0800767static int tse_set_mac_address(struct eth_device *dev)
768{
769 struct altera_tse_priv *priv = dev->priv;
770 volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
771
772 debug("Setting MAC address to 0x%02x%02x%02x%02x%02x%02x\n",
773 dev->enetaddr[5], dev->enetaddr[4],
774 dev->enetaddr[3], dev->enetaddr[2],
775 dev->enetaddr[1], dev->enetaddr[0]);
776 mac_dev->mac_addr_0 = ((dev->enetaddr[3]) << 24 |
777 (dev->enetaddr[2]) << 16 |
778 (dev->enetaddr[1]) << 8 | (dev->enetaddr[0]));
779
780 mac_dev->mac_addr_1 = ((dev->enetaddr[5] << 8 |
781 (dev->enetaddr[4])) & 0xFFFF);
782
783 /* Set the MAC address */
784 mac_dev->supp_mac_addr_0_0 = mac_dev->mac_addr_0;
785 mac_dev->supp_mac_addr_0_1 = mac_dev->mac_addr_1;
786
787 /* Set the MAC address */
788 mac_dev->supp_mac_addr_1_0 = mac_dev->mac_addr_0;
789 mac_dev->supp_mac_addr_1_1 = mac_dev->mac_addr_1;
790
791 /* Set the MAC address */
792 mac_dev->supp_mac_addr_2_0 = mac_dev->mac_addr_0;
793 mac_dev->supp_mac_addr_2_1 = mac_dev->mac_addr_1;
794
795 /* Set the MAC address */
796 mac_dev->supp_mac_addr_3_0 = mac_dev->mac_addr_0;
797 mac_dev->supp_mac_addr_3_1 = mac_dev->mac_addr_1;
798 return 0;
799}
800
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800801static int tse_eth_init(struct eth_device *dev, bd_t * bd)
802{
803 int dat;
804 struct altera_tse_priv *priv = dev->priv;
805 volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
806 volatile struct alt_sgdma_descriptor *tx_desc = priv->tx_desc;
807 volatile struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
808 volatile struct alt_sgdma_descriptor *rx_desc_cur =
809 (volatile struct alt_sgdma_descriptor *)&rx_desc[0];
810
811 /* stop controller */
812 debug("Reseting TSE & SGDMAs\n");
813 tse_eth_reset(dev);
814
815 /* start the phy */
816 debug("Configuring PHY\n");
817 phy_run_commands(priv, priv->phyinfo->startup);
818
819 /* need to create sgdma */
820 debug("Configuring tx desc\n");
821 alt_sgdma_construct_descriptor_burst(
822 (volatile struct alt_sgdma_descriptor *)&tx_desc[0],
823 (volatile struct alt_sgdma_descriptor *)&tx_desc[1],
824 (unsigned int *)NULL, /* read addr */
825 (unsigned int *)0,
826 0, /* length or EOP ,will change for each tx */
827 0x1, /* gen eop */
828 0x0, /* read fixed */
829 0x1, /* write fixed or sop */
830 0x0, /* read burst */
831 0x0, /* write burst */
832 0x0 /* channel */
833 );
834 debug("Configuring rx desc\n");
835 flush_dcache((unsigned long)(NetRxPackets[0]), PKTSIZE_ALIGN);
836 alt_sgdma_construct_descriptor_burst(
837 (volatile struct alt_sgdma_descriptor *)&rx_desc[0],
838 (volatile struct alt_sgdma_descriptor *)&rx_desc[1],
839 (unsigned int)0x0, /* read addr */
840 (unsigned int *)NetRxPackets[0],
841 0x0, /* length or EOP */
842 0x0, /* gen eop */
843 0x0, /* read fixed */
844 0x0, /* write fixed or sop */
845 0x0, /* read burst */
846 0x0, /* write burst */
847 0x0 /* channel */
848 );
849 /* start rx async transfer */
850 debug("Starting rx sgdma\n");
851 alt_sgdma_do_async_transfer(priv->sgdma_rx, rx_desc_cur);
852
853 /* start TSE */
854 debug("Configuring TSE Mac\n");
855 /* Initialize MAC registers */
856 mac_dev->max_frame_length = PKTSIZE_ALIGN;
857 mac_dev->rx_almost_empty_threshold = 8;
858 mac_dev->rx_almost_full_threshold = 8;
859 mac_dev->tx_almost_empty_threshold = 8;
860 mac_dev->tx_almost_full_threshold = 3;
861 mac_dev->tx_sel_empty_threshold =
862 CONFIG_SYS_ALTERA_TSE_TX_FIFO - 16;
863 mac_dev->tx_sel_full_threshold = 0;
864 mac_dev->rx_sel_empty_threshold =
865 CONFIG_SYS_ALTERA_TSE_TX_FIFO - 16;
866 mac_dev->rx_sel_full_threshold = 0;
867
868 /* NO Shift */
869 mac_dev->rx_cmd_stat.bits.rx_shift16 = 0;
870 mac_dev->tx_cmd_stat.bits.tx_shift16 = 0;
871
872 /* enable MAC */
873 dat = 0;
874 dat = ALTERA_TSE_CMD_TX_ENA_MSK | ALTERA_TSE_CMD_RX_ENA_MSK;
875
876 mac_dev->command_config.image = dat;
877
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800878 /* configure the TSE core */
879 /* -- output clocks, */
880 /* -- and later config stuff for SGMII */
881 if (priv->link) {
882 debug("Adjusting TSE to link speed\n");
883 tse_adjust_link(priv);
884 }
885
886 return priv->link ? 0 : -1;
887}
888
889/* TSE init code */
890int altera_tse_initialize(u8 dev_num, int mac_base,
Joachim Foerstercb0ddaf2011-10-17 05:24:44 +0000891 int sgdma_rx_base, int sgdma_tx_base,
892 u32 sgdma_desc_base, u32 sgdma_desc_size)
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800893{
894 struct altera_tse_priv *priv;
895 struct eth_device *dev;
896 struct alt_sgdma_descriptor *rx_desc;
897 struct alt_sgdma_descriptor *tx_desc;
898 unsigned long dma_handle;
899
900 dev = (struct eth_device *)malloc(sizeof *dev);
901
902 if (NULL == dev)
903 return 0;
904
905 memset(dev, 0, sizeof *dev);
906
907 priv = malloc(sizeof(*priv));
908
909 if (!priv) {
910 free(dev);
911 return 0;
912 }
Joachim Foerstercb0ddaf2011-10-17 05:24:44 +0000913 if (sgdma_desc_size) {
914 if (sgdma_desc_size < (sizeof(*tx_desc) * (3 + PKTBUFSRX))) {
915 printf("ALTERA_TSE-%hu: "
916 "descriptor memory is too small\n", dev_num);
917 free(priv);
918 free(dev);
919 return 0;
920 }
921 tx_desc = (struct alt_sgdma_descriptor *)sgdma_desc_base;
922 } else {
923 tx_desc = dma_alloc_coherent(sizeof(*tx_desc) * (3 + PKTBUFSRX),
924 &dma_handle);
925 }
926
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800927 rx_desc = tx_desc + 2;
928 debug("tx desc: address = 0x%x\n", (unsigned int)tx_desc);
929 debug("rx desc: address = 0x%x\n", (unsigned int)rx_desc);
930
931 if (!tx_desc) {
932 free(priv);
933 free(dev);
934 return 0;
935 }
936 memset(rx_desc, 0, (sizeof *rx_desc) * (PKTBUFSRX + 1));
937 memset(tx_desc, 0, (sizeof *tx_desc) * 2);
938
939 /* initialize tse priv */
940 priv->mac_dev = (volatile struct alt_tse_mac *)mac_base;
941 priv->sgdma_rx = (volatile struct alt_sgdma_registers *)sgdma_rx_base;
942 priv->sgdma_tx = (volatile struct alt_sgdma_registers *)sgdma_tx_base;
943 priv->phyaddr = CONFIG_SYS_ALTERA_TSE_PHY_ADDR;
944 priv->flags = CONFIG_SYS_ALTERA_TSE_FLAGS;
945 priv->rx_desc = rx_desc;
946 priv->tx_desc = tx_desc;
947
948 /* init eth structure */
949 dev->priv = priv;
950 dev->init = tse_eth_init;
951 dev->halt = tse_eth_halt;
952 dev->send = tse_eth_send;
953 dev->recv = tse_eth_rx;
Thomas Chou53d01502010-04-27 20:15:10 +0800954 dev->write_hwaddr = tse_set_mac_address;
Thomas Chou7bb1b9b2010-04-20 12:49:52 +0800955 sprintf(dev->name, "%s-%hu", "ALTERA_TSE", dev_num);
956
957 eth_register(dev);
958
959#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
960 miiphy_register(dev->name, altera_tse_miiphy_read,
961 altera_tse_miiphy_write);
962#endif
963
964 init_phy(dev);
965
966 return 1;
967}