1 // SPDX-License-Identifier: GPL-2.0+
15 #define PCI_CFDA_PSM 0x43
17 #define CFRV_RN 0x000000f0 /* Revision Number */
19 #define WAKEUP 0x00 /* Power Saving Wakeup */
20 #define SLEEP 0x80 /* Power Saving Sleep Mode */
22 #define DC2114x_BRK 0x0020 /* CFRV break between DC21142 & DC21143 */
24 /* Ethernet chip registers. */
25 #define DE4X5_BMR 0x000 /* Bus Mode Register */
26 #define DE4X5_TPD 0x008 /* Transmit Poll Demand Reg */
27 #define DE4X5_RRBA 0x018 /* RX Ring Base Address Reg */
28 #define DE4X5_TRBA 0x020 /* TX Ring Base Address Reg */
29 #define DE4X5_STS 0x028 /* Status Register */
30 #define DE4X5_OMR 0x030 /* Operation Mode Register */
31 #define DE4X5_SICR 0x068 /* SIA Connectivity Register */
32 #define DE4X5_APROM 0x048 /* Ethernet Address PROM */
35 #define BMR_SWR 0x00000001 /* Software Reset */
36 #define STS_TS 0x00700000 /* Transmit Process State */
37 #define STS_RS 0x000e0000 /* Receive Process State */
38 #define OMR_ST 0x00002000 /* Start/Stop Transmission Command */
39 #define OMR_SR 0x00000002 /* Start/Stop Receive */
40 #define OMR_PS 0x00040000 /* Port Select */
41 #define OMR_SDP 0x02000000 /* SD Polarity - MUST BE ASSERTED */
42 #define OMR_PM 0x00000080 /* Pass All Multicast */
44 /* Descriptor bits. */
45 #define R_OWN 0x80000000 /* Own Bit */
46 #define RD_RER 0x02000000 /* Receive End Of Ring */
47 #define RD_LS 0x00000100 /* Last Descriptor */
48 #define RD_ES 0x00008000 /* Error Summary */
49 #define TD_TER 0x02000000 /* Transmit End Of Ring */
50 #define T_OWN 0x80000000 /* Own Bit */
51 #define TD_LS 0x40000000 /* Last Segment */
52 #define TD_FS 0x20000000 /* First Segment */
53 #define TD_ES 0x00008000 /* Error Summary */
54 #define TD_SET 0x08000000 /* Setup Packet */
56 /* The EEPROM commands include the alway-set leading bit. */
57 #define SROM_WRITE_CMD 5
58 #define SROM_READ_CMD 6
59 #define SROM_ERASE_CMD 7
61 #define SROM_HWADD 0x0014 /* Hardware Address offset in SROM */
62 #define SROM_RD 0x00004000 /* Read from Boot ROM */
63 #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
64 #define EE_WRITE_0 0x4801
65 #define EE_WRITE_1 0x4805
66 #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
67 #define SROM_SR 0x00000800 /* Select Serial ROM when set */
69 #define DT_IN 0x00000004 /* Serial Data In */
70 #define DT_CLK 0x00000002 /* Serial ROM Clock */
71 #define DT_CS 0x00000001 /* Serial ROM Chip Select */
75 #if defined(CONFIG_E500)
76 #define phys_to_bus(a) (a)
78 #define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
81 #define NUM_RX_DESC PKTBUFSRX
82 #define NUM_TX_DESC 1 /* Number of TX descriptors */
83 #define RX_BUFF_SZ PKTSIZE_ALIGN
85 #define TOUT_LOOP 1000000
87 #define SETUP_FRAME_LEN 192
96 /* RX and TX descriptor ring */
97 static struct de4x5_desc rx_ring[NUM_RX_DESC] __aligned(32);
98 static struct de4x5_desc tx_ring[NUM_TX_DESC] __aligned(32);
99 static int rx_new; /* RX descriptor ring pointer */
100 static int tx_new; /* TX descriptor ring pointer */
102 static char rx_ring_size;
103 static char tx_ring_size;
105 static u32 dc2114x_inl(struct eth_device *dev, u32 addr)
107 return le32_to_cpu(*(volatile u32 *)(addr + dev->iobase));
110 static void dc2114x_outl(struct eth_device *dev, u32 command, u32 addr)
112 *(volatile u32 *)(addr + dev->iobase) = cpu_to_le32(command);
115 static void reset_de4x5(struct eth_device *dev)
119 i = dc2114x_inl(dev, DE4X5_BMR);
121 dc2114x_outl(dev, i | BMR_SWR, DE4X5_BMR);
123 dc2114x_outl(dev, i, DE4X5_BMR);
126 for (i = 0; i < 5; i++) {
127 dc2114x_inl(dev, DE4X5_BMR);
134 static void start_de4x5(struct eth_device *dev)
138 omr = dc2114x_inl(dev, DE4X5_OMR);
139 omr |= OMR_ST | OMR_SR;
140 dc2114x_outl(dev, omr, DE4X5_OMR); /* Enable the TX and/or RX */
143 static void stop_de4x5(struct eth_device *dev)
147 omr = dc2114x_inl(dev, DE4X5_OMR);
148 omr &= ~(OMR_ST | OMR_SR);
149 dc2114x_outl(dev, omr, DE4X5_OMR); /* Disable the TX and/or RX */
152 /* SROM Read and write routines. */
153 static void sendto_srom(struct eth_device *dev, u_int command, u_long addr)
155 dc2114x_outl(dev, command, addr);
159 static int getfrom_srom(struct eth_device *dev, u_long addr)
161 u32 tmp = dc2114x_inl(dev, addr);
167 /* Note: this routine returns extra data bits for size detection. */
168 static int do_read_eeprom(struct eth_device *dev, u_long ioaddr, int location,
171 int read_cmd = location | (SROM_READ_CMD << addr_len);
172 unsigned int retval = 0;
175 sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
176 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
178 debug_cond(SROM_DLEVEL >= 1, " EEPROM read at %d ", location);
180 /* Shift the read command bits out. */
181 for (i = 4 + addr_len; i >= 0; i--) {
182 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
184 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | dataval,
187 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | dataval | DT_CLK,
190 debug_cond(SROM_DLEVEL >= 2, "%X",
191 getfrom_srom(dev, ioaddr) & 15);
192 retval = (retval << 1) |
193 !!(getfrom_srom(dev, ioaddr) & EE_DATA_READ);
196 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
198 debug_cond(SROM_DLEVEL >= 2, " :%X:", getfrom_srom(dev, ioaddr) & 15);
200 for (i = 16; i > 0; i--) {
201 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
203 debug_cond(SROM_DLEVEL >= 2, "%X",
204 getfrom_srom(dev, ioaddr) & 15);
205 retval = (retval << 1) |
206 !!(getfrom_srom(dev, ioaddr) & EE_DATA_READ);
207 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
211 /* Terminate the EEPROM access. */
212 sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
214 debug_cond(SROM_DLEVEL >= 2, " EEPROM value at %d is %5.5x.\n",
221 * This executes a generic EEPROM command, typically a write or write
222 * enable. It returns the data output from the EEPROM, and thus may
223 * also be used for reads.
225 static int do_eeprom_cmd(struct eth_device *dev, u_long ioaddr, int cmd,
228 unsigned int retval = 0;
230 debug_cond(SROM_DLEVEL >= 1, " EEPROM op 0x%x: ", cmd);
232 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
234 /* Shift the command bits out. */
236 short dataval = (cmd & BIT(cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
238 sendto_srom(dev, dataval, ioaddr);
241 debug_cond(SROM_DLEVEL >= 2, "%X",
242 getfrom_srom(dev, ioaddr) & 15);
244 sendto_srom(dev, dataval | DT_CLK, ioaddr);
246 retval = (retval << 1) |
247 !!(getfrom_srom(dev, ioaddr) & EE_DATA_READ);
248 } while (--cmd_len >= 0);
250 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
252 /* Terminate the EEPROM access. */
253 sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
255 debug_cond(SROM_DLEVEL >= 1, " EEPROM result is 0x%5.5x.\n", retval);
260 static int read_srom(struct eth_device *dev, u_long ioaddr, int index)
264 ee_addr_size = (do_read_eeprom(dev, ioaddr, 0xff, 8) & BIT(18)) ? 8 : 6;
266 return do_eeprom_cmd(dev, ioaddr, 0xffff |
267 (((SROM_READ_CMD << ee_addr_size) | index) << 16),
268 3 + ee_addr_size + 16);
272 static int write_srom(struct eth_device *dev, u_long ioaddr, int index,
275 unsigned short newval;
279 ee_addr_size = (do_read_eeprom(dev, ioaddr, 0xff, 8) & BIT(18)) ? 8 : 6;
281 udelay(10 * 1000); /* test-only */
283 debug_cond(SROM_DLEVEL >= 1, "ee_addr_size=%d.\n", ee_addr_size);
284 debug_cond(SROM_DLEVEL >= 1,
285 "Writing new entry 0x%4.4x to offset %d.\n",
288 /* Enable programming modes. */
289 do_eeprom_cmd(dev, ioaddr, 0x4f << (ee_addr_size - 4),
292 /* Do the actual write. */
293 do_eeprom_cmd(dev, ioaddr, new_value |
294 (((SROM_WRITE_CMD << ee_addr_size) | index) << 16),
295 3 + ee_addr_size + 16);
297 /* Poll for write finished. */
298 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
299 for (i = 0; i < 10000; i++) { /* Typical 2000 ticks */
300 if (getfrom_srom(dev, ioaddr) & EE_DATA_READ)
304 debug_cond(SROM_DLEVEL >= 1, " Write finished after %d ticks.\n", i);
306 /* Disable programming. */
307 do_eeprom_cmd(dev, ioaddr, (0x40 << (ee_addr_size - 4)),
310 /* And read the result. */
311 newval = do_eeprom_cmd(dev, ioaddr,
312 (((SROM_READ_CMD << ee_addr_size) | index) << 16)
313 | 0xffff, 3 + ee_addr_size + 16);
315 debug_cond(SROM_DLEVEL >= 1, " New value at offset %d is %4.4x.\n",
321 static void update_srom(struct eth_device *dev, bd_t *bis)
323 static unsigned short eeprom[0x40] = {
324 0x140b, 0x6610, 0x0000, 0x0000, /* 00 */
325 0x0000, 0x0000, 0x0000, 0x0000, /* 04 */
326 0x00a3, 0x0103, 0x0000, 0x0000, /* 08 */
327 0x0000, 0x1f00, 0x0000, 0x0000, /* 0c */
328 0x0108, 0x038d, 0x0000, 0x0000, /* 10 */
329 0xe078, 0x0001, 0x0040, 0x0018, /* 14 */
330 0x0000, 0x0000, 0x0000, 0x0000, /* 18 */
331 0x0000, 0x0000, 0x0000, 0x0000, /* 1c */
332 0x0000, 0x0000, 0x0000, 0x0000, /* 20 */
333 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
334 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
335 0x0000, 0x0000, 0x0000, 0x0000, /* 2c */
336 0x0000, 0x0000, 0x0000, 0x0000, /* 30 */
337 0x0000, 0x0000, 0x0000, 0x0000, /* 34 */
338 0x0000, 0x0000, 0x0000, 0x0000, /* 38 */
339 0x0000, 0x0000, 0x0000, 0x4e07, /* 3c */
344 /* Ethernet Addr... */
345 if (!eth_env_get_enetaddr("ethaddr", enetaddr))
348 eeprom[0x0a] = (enetaddr[1] << 8) | enetaddr[0];
349 eeprom[0x0b] = (enetaddr[3] << 8) | enetaddr[2];
350 eeprom[0x0c] = (enetaddr[5] << 8) | enetaddr[4];
352 for (i = 0; i < 0x40; i++)
353 write_srom(dev, DE4X5_APROM, i, eeprom[i]);
355 #endif /* UPDATE_SROM */
357 static void send_setup_frame(struct eth_device *dev, bd_t *bis)
359 char setup_frame[SETUP_FRAME_LEN];
360 char *pa = &setup_frame[0];
363 memset(pa, 0xff, SETUP_FRAME_LEN);
365 for (i = 0; i < ETH_ALEN; i++) {
366 *(pa + (i & 1)) = dev->enetaddr[i];
371 for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
375 printf("%s: tx error buffer not ready\n", dev->name);
379 tx_ring[tx_new].buf = cpu_to_le32(phys_to_bus((u32)&setup_frame[0]));
380 tx_ring[tx_new].des1 = cpu_to_le32(TD_TER | TD_SET | SETUP_FRAME_LEN);
381 tx_ring[tx_new].status = cpu_to_le32(T_OWN);
383 dc2114x_outl(dev, POLL_DEMAND, DE4X5_TPD);
385 for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
389 printf("%s: tx buffer not ready\n", dev->name);
393 if (le32_to_cpu(tx_ring[tx_new].status) != 0x7FFFFFFF) {
394 printf("TX error status2 = 0x%08X\n",
395 le32_to_cpu(tx_ring[tx_new].status));
398 tx_new = (tx_new + 1) % NUM_TX_DESC;
401 static int dc21x4x_send(struct eth_device *dev, void *packet, int length)
407 printf("%s: bad packet size: %d\n", dev->name, length);
411 for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
415 printf("%s: tx error buffer not ready\n", dev->name);
419 tx_ring[tx_new].buf = cpu_to_le32(phys_to_bus((u32)packet));
420 tx_ring[tx_new].des1 = cpu_to_le32(TD_TER | TD_LS | TD_FS | length);
421 tx_ring[tx_new].status = cpu_to_le32(T_OWN);
423 dc2114x_outl(dev, POLL_DEMAND, DE4X5_TPD);
425 for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
429 printf(".%s: tx buffer not ready\n", dev->name);
433 if (le32_to_cpu(tx_ring[tx_new].status) & TD_ES) {
434 tx_ring[tx_new].status = 0x0;
441 tx_new = (tx_new + 1) % NUM_TX_DESC;
445 static int dc21x4x_recv(struct eth_device *dev)
451 status = le32_to_cpu(rx_ring[rx_new].status);
456 if (status & RD_LS) {
457 /* Valid frame status. */
458 if (status & RD_ES) {
459 /* There was an error. */
460 printf("RX error status = 0x%08X\n", status);
462 /* A valid frame received. */
463 length = (le32_to_cpu(rx_ring[rx_new].status)
466 /* Pass the packet up to the protocol layers */
467 net_process_received_packet
468 (net_rx_packets[rx_new], length - 4);
472 * Change buffer ownership for this frame,
473 * back to the adapter.
475 rx_ring[rx_new].status = cpu_to_le32(R_OWN);
478 /* Update entry information. */
479 rx_new = (rx_new + 1) % rx_ring_size;
485 static int dc21x4x_init(struct eth_device *dev, bd_t *bis)
488 int devbusfn = (int)dev->priv;
490 /* Ensure we're not sleeping. */
491 pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
495 if (dc2114x_inl(dev, DE4X5_STS) & (STS_TS | STS_RS)) {
496 printf("Error: Cannot reset ethernet controller.\n");
500 dc2114x_outl(dev, OMR_SDP | OMR_PS | OMR_PM, DE4X5_OMR);
502 for (i = 0; i < NUM_RX_DESC; i++) {
503 rx_ring[i].status = cpu_to_le32(R_OWN);
504 rx_ring[i].des1 = cpu_to_le32(RX_BUFF_SZ);
506 cpu_to_le32(phys_to_bus((u32)net_rx_packets[i]));
510 for (i = 0; i < NUM_TX_DESC; i++) {
511 tx_ring[i].status = 0;
517 rx_ring_size = NUM_RX_DESC;
518 tx_ring_size = NUM_TX_DESC;
520 /* Write the end of list marker to the descriptor lists. */
521 rx_ring[rx_ring_size - 1].des1 |= cpu_to_le32(RD_RER);
522 tx_ring[tx_ring_size - 1].des1 |= cpu_to_le32(TD_TER);
524 /* Tell the adapter where the TX/RX rings are located. */
525 dc2114x_outl(dev, phys_to_bus((u32)&rx_ring), DE4X5_RRBA);
526 dc2114x_outl(dev, phys_to_bus((u32)&tx_ring), DE4X5_TRBA);
533 send_setup_frame(dev, bis);
538 static void dc21x4x_halt(struct eth_device *dev)
540 int devbusfn = (int)dev->priv;
543 dc2114x_outl(dev, 0, DE4X5_SICR);
545 pci_write_config_byte(devbusfn, PCI_CFDA_PSM, SLEEP);
548 static void read_hw_addr(struct eth_device *dev, bd_t *bis)
550 u_short tmp, *p = (u_short *)(&dev->enetaddr[0]);
553 for (i = 0; i < (ETH_ALEN >> 1); i++) {
554 tmp = read_srom(dev, DE4X5_APROM, (SROM_HWADD >> 1) + i);
555 *p = le16_to_cpu(tmp);
559 if (!j || j == 0x2fffd) {
560 memset(dev->enetaddr, 0, ETH_ALEN);
561 debug("Warning: can't read HW address from SROM.\n");
563 update_srom(dev, bis);
568 static struct pci_device_id supported[] = {
569 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST },
570 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142 },
574 int dc21x4x_initialize(bd_t *bis)
576 struct eth_device *dev;
577 unsigned short status;
586 devbusfn = pci_find_devices(supported, idx++);
590 /* Get the chip configuration revision register. */
591 pci_read_config_dword(devbusfn, PCI_REVISION_ID, &cfrv);
593 if ((cfrv & CFRV_RN) < DC2114x_BRK) {
594 printf("Error: The chip is not DC21143.\n");
598 pci_read_config_word(devbusfn, PCI_COMMAND, &status);
599 status |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
600 pci_write_config_word(devbusfn, PCI_COMMAND, status);
602 pci_read_config_word(devbusfn, PCI_COMMAND, &status);
603 if (!(status & PCI_COMMAND_MEMORY)) {
604 printf("Error: Can not enable MEMORY access.\n");
608 if (!(status & PCI_COMMAND_MASTER)) {
609 printf("Error: Can not enable Bus Mastering.\n");
613 /* Check the latency timer for values >= 0x60. */
614 pci_read_config_byte(devbusfn, PCI_LATENCY_TIMER, &timer);
617 pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER,
621 /* read BAR for memory space access */
622 pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_1, &iobase);
623 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
624 debug("dc21x4x: DEC 21142 PCI Device @0x%x\n", iobase);
626 dev = (struct eth_device *)malloc(sizeof(*dev));
628 printf("Can not allocalte memory of dc21x4x\n");
632 memset(dev, 0, sizeof(*dev));
634 sprintf(dev->name, "dc21x4x#%d", card_number);
636 dev->iobase = pci_mem_to_phys(devbusfn, iobase);
637 dev->priv = (void *)devbusfn;
638 dev->init = dc21x4x_init;
639 dev->halt = dc21x4x_halt;
640 dev->send = dc21x4x_send;
641 dev->recv = dc21x4x_recv;
643 /* Ensure we're not sleeping. */
644 pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
648 read_hw_addr(dev, bis);