1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
49 * Support library for the SPI4000 card
51 * <hr>$Revision: 70030 $<hr>
53 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
54 #include <linux/module.h>
55 #include <asm/octeon/cvmx.h>
56 #include <asm/octeon/cvmx-spi.h>
57 #include <asm/octeon/cvmx-twsi.h>
58 #include <asm/octeon/cvmx-gmxx-defs.h>
62 #include "cvmx-twsi.h"
65 /* If someone is using an old config, make the SPI4000 act like RGMII for backpressure */
66 #ifndef CVMX_HELPER_DISABLE_SPI4000_BACKPRESSURE
67 #ifndef CVMX_HELPER_DISABLE_RGMII_BACKPRESSURE
68 #define CVMX_HELPER_DISABLE_RGMII_BACKPRESSURE 0
70 #define CVMX_HELPER_DISABLE_SPI4000_BACKPRESSURE CVMX_HELPER_DISABLE_RGMII_BACKPRESSURE
73 #define SPI4000_READ_ADDRESS_HIGH 0xf0
74 #define SPI4000_READ_ADDRESS_LOW 0xf1
75 #define SPI4000_WRITE_ADDRESS_HIGH 0xf2
76 #define SPI4000_WRITE_ADDRESS_LOW 0xf3
77 #define SPI4000_READ_DATA0 0xf4 /* High byte */
78 #define SPI4000_READ_DATA1 0xf5
79 #define SPI4000_READ_DATA2 0xf6
80 #define SPI4000_READ_DATA3 0xf7 /* Low byte */
81 #define SPI4000_WRITE_DATA0 0xf8 /* High byte */
82 #define SPI4000_WRITE_DATA1 0xf9
83 #define SPI4000_WRITE_DATA2 0xfa
84 #define SPI4000_WRITE_DATA3 0xfb /* Low byte */
85 #define SPI4000_DO_READ 0xfc /* Issue a read, returns read status */
86 #define SPI4000_GET_READ_STATUS 0xfd /* 0xff: initial state, 2: Read failed, 1: Read pending, 0: Read success */
87 #define SPI4000_DO_WRITE 0xfe /* Issue a write, returns write status */
88 #define SPI4000_GET_WRITE_STATUS 0xff /* 0xff: initial state, 6: Write failed, 5: Write pending, 4: Write success */
89 #define SPI4000_TWSI_ID(interface) (0x66 + interface)
91 /* MDI Single Command (register 0x680) */
97 uint32_t reserved_21_31 : 11;
98 uint32_t mdi_command : 1; /**< Performs an MDIO access. When set, this bit
99 self clears upon completion of the access. */
100 uint32_t reserved_18_19 : 2;
101 uint32_t op_code : 2; /**< MDIO Op Code
106 uint32_t reserved_13_15 : 3;
107 uint32_t phy_address : 5; /**< Address of external PHY device */
108 uint32_t reserved_5_7 : 3;
109 uint32_t reg_address : 5; /**< Address of register within external PHY */
111 } mdio_single_command_t;
114 static CVMX_SHARED int interface_is_spi4000[2] = {0,0};
119 * Write data to the specified SPI4000 address
121 * @param interface Interface the SPI4000 is on. (0 or 1)
122 * @param address Address to write to
123 * @param data Data to write
125 static void __cvmx_spi4000_write(int interface, int address, uint32_t data)
128 cvmx_twsix_write_ia(0, SPI4000_TWSI_ID(interface), SPI4000_WRITE_ADDRESS_HIGH, 2, 1, address);
129 cvmx_twsix_write_ia(0, SPI4000_TWSI_ID(interface), SPI4000_WRITE_DATA0, 4, 1, data);
131 status = cvmx_twsi_read8(SPI4000_TWSI_ID(interface), SPI4000_DO_WRITE);
132 while ((status == 5) || (status == 0xff))
133 status = cvmx_twsi_read8(SPI4000_TWSI_ID(interface), SPI4000_GET_WRITE_STATUS);
136 cvmx_dprintf("SPI4000: write failed with status=0x%x\n", status);
142 * Read data from the SPI4000.
144 * @param interface Interface the SPI4000 is on. (0 or 1)
145 * @param address Address to read from
147 * @return Value at the specified address
149 static uint32_t __cvmx_spi4000_read(int interface, int address)
154 cvmx_twsix_write_ia(0, SPI4000_TWSI_ID(interface), SPI4000_READ_ADDRESS_HIGH, 2, 1, address);
156 status = cvmx_twsi_read8(SPI4000_TWSI_ID(interface), SPI4000_DO_READ);
157 while ((status == 1) || (status == 0xff))
158 status = cvmx_twsi_read8(SPI4000_TWSI_ID(interface), SPI4000_GET_READ_STATUS);
162 cvmx_dprintf("SPI4000: read failed with %d\n", status);
166 status = cvmx_twsix_read_ia(0, SPI4000_TWSI_ID(interface), SPI4000_READ_DATA0, 4, 1, &data);
169 cvmx_dprintf("SPI4000: read failed with %d\n", status);
179 * Write to a PHY using MDIO on the SPI4000
181 * @param interface Interface the SPI4000 is on. (0 or 1)
182 * @param port SPI4000 RGMII port to write to. (0-9)
183 * @param location MDIO register to write
184 * @param val Value to write
186 static void __cvmx_spi4000_mdio_write(int interface, int port, int location, int val)
188 static int last_value=-1;
189 mdio_single_command_t mdio;
192 mdio.s.mdi_command = 1;
194 mdio.s.phy_address = port;
195 mdio.s.reg_address = location;
197 /* Since the TWSI accesses are very slow, don't update the write value
198 if it is the same as the last value */
199 if (val != last_value)
202 __cvmx_spi4000_write(interface, 0x0681, val);
205 __cvmx_spi4000_write(interface, 0x0680, mdio.u32);
211 * Read from a PHY using MDIO on the SPI4000
213 * @param interface Interface the SPI4000 is on. (0 or 1)
214 * @param port SPI4000 RGMII port to read from. (0-9)
215 * @param location MDIO register to read
216 * @return The MDI read result
218 static int __cvmx_spi4000_mdio_read(int interface, int port, int location)
220 mdio_single_command_t mdio;
223 mdio.s.mdi_command = 1;
225 mdio.s.phy_address = port;
226 mdio.s.reg_address = location;
227 __cvmx_spi4000_write(interface, 0x0680, mdio.u32);
231 mdio.u32 = __cvmx_spi4000_read(interface, 0x0680);
232 } while (mdio.s.mdi_command);
234 return __cvmx_spi4000_read(interface, 0x0681) >> 16;
240 * Configure the SPI4000 MACs
242 static void __cvmx_spi4000_configure_mac(int interface)
245 // IXF1010 configuration
246 // ---------------------
248 // Step 1: Apply soft reset to TxFIFO and MAC
249 // MAC soft reset register. address=0x505
250 // TxFIFO soft reset. address=0x620
251 __cvmx_spi4000_write(interface, 0x0505, 0x3ff); // reset all the MACs
252 __cvmx_spi4000_write(interface, 0x0620, 0x3ff); // reset the TX FIFOs
254 // Global address and Configuration Register. address=0x500
256 // Step 2: Apply soft reset to RxFIFO and SPI.
257 __cvmx_spi4000_write(interface, 0x059e, 0x3ff); // reset the RX FIFOs
259 // Step 3a: Take the MAC out of softreset
260 // MAC soft reset register. address=0x505
261 __cvmx_spi4000_write(interface, 0x0505, 0x0); // reset all the MACs
263 // Step 3b: De-assert port enables.
264 // Global address and Configuration Register. address=0x500
265 __cvmx_spi4000_write(interface, 0x0500, 0x0); // disable all ports
267 // Step 4: Assert Clock mode change En.
268 // Clock and interface mode Change En. address=Serdes base + 0x14
269 // Serdes (Serializer/de-serializer). address=0x780
270 // [Can't find this one]
272 for (port=0; port < 10; port++)
274 int port_offset = port << 7;
276 // Step 5: Set MAC interface mode GMII speed.
277 // MAC interface mode and RGMII speed register.
278 // address=port_index+0x10
280 // OUT port_index+0x10, 0x07 //RGMII 1000 Mbps operation.
281 __cvmx_spi4000_write(interface, port_offset | 0x0010, 0x3);
283 // Set the max packet size to 16383 bytes, including the CRC
284 __cvmx_spi4000_write(interface, port_offset | 0x000f, 0x3fff);
286 // Step 6: Change Interface to Copper mode
287 // Interface mode register. address=0x501
290 // Step 7: MAC configuration
291 // Station address configuration.
292 // Source MAC address low register. Source MAC address 31-0.
293 // address=port_index+0x00
294 // Source MAC address high register. Source MAC address 47-32.
295 // address=port_index+0x01
296 // where Port index is 0x0 to 0x5.
297 // This address is inserted in the source address filed when
298 // transmitting pause frames, and is also used to compare against
299 // unicast pause frames at the receiving side.
301 // OUT port_index+0x00, source MAC address low.
302 __cvmx_spi4000_write(interface, port_offset | 0x0000, 0x0000);
303 // OUT port_index+0x01, source MAC address high.
304 __cvmx_spi4000_write(interface, port_offset | 0x0001, 0x0000);
306 // Step 8: Set desired duplex mode
307 // Desired duplex register. address=port_index+0x02
310 // Step 9: Other configuration.
311 // FC Enable Register. address=port_index+0x12
312 // Discard Unknown Control Frame. address=port_index+0x15
313 // Diverse config write register. address=port_index+0x18
314 // RX Packet Filter register. address=port_index+0x19
316 // Step 9a: Tx FD FC Enabled / Rx FD FC Enabled
317 if (CVMX_HELPER_DISABLE_SPI4000_BACKPRESSURE)
318 __cvmx_spi4000_write(interface, port_offset | 0x0012, 0);
320 __cvmx_spi4000_write(interface, port_offset | 0x0012, 0x7);
322 // Step 9b: Discard unknown control frames
323 __cvmx_spi4000_write(interface, port_offset | 0x0015, 0x1);
325 // Step 9c: Enable auto-CRC and auto-padding
326 __cvmx_spi4000_write(interface, port_offset | 0x0018, 0x11cd); //??
328 // Step 9d: Drop bad CRC / Drop Pause / No DAF
329 __cvmx_spi4000_write(interface, port_offset | 0x0019, 0x00);
332 // Step 9d: Drop frames
333 __cvmx_spi4000_write(interface, 0x059f, 0x03ff);
335 for (port=0; port < 10; port++)
337 // Step 9e: Set the TX FIFO marks
338 __cvmx_spi4000_write(interface, port + 0x0600, 0x0900); // TXFIFO High watermark
339 __cvmx_spi4000_write(interface, port + 0x060a, 0x0800); // TXFIFO Low watermark
340 __cvmx_spi4000_write(interface, port + 0x0614, 0x0380); // TXFIFO threshold
343 // Step 12: De-assert RxFIFO and SPI Rx/Tx reset
344 __cvmx_spi4000_write(interface, 0x059e, 0x0); // reset the RX FIFOs
346 // Step 13: De-assert TxFIFO and MAC reset
347 __cvmx_spi4000_write(interface, 0x0620, 0x0); // reset the TX FIFOs
349 // Step 14: Assert port enable
350 // Global address and Configuration Register. address=0x500
351 __cvmx_spi4000_write(interface, 0x0500, 0x03ff); // enable all ports
353 // Step 15: Disable loopback
354 // [Can't find this one]
360 * Configure the SPI4000 PHYs
362 static void __cvmx_spi4000_configure_phy(int interface)
366 /* We use separate loops below since it allows us to save a write
367 to the SPI4000 for each repeated value. This adds up to a couple
370 /* Update the link state before resets. It takes a while for the links to
371 come back after the resets. Most likely they'll come back the same as
373 for (port=0; port < 10; port++)
374 cvmx_spi4000_check_speed(interface, port);
375 /* Enable RGMII DELAYS for TX_CLK and RX_CLK (see spec) */
376 for (port=0; port < 10; port++)
377 __cvmx_spi4000_mdio_write(interface, port, 0x14, 0x00e2);
378 /* Advertise pause and 100 Full Duplex. Don't advertise half duplex or 10Mbpa */
379 for (port=0; port < 10; port++)
380 __cvmx_spi4000_mdio_write(interface, port, 0x4, 0x0d01);
381 /* Enable PHY reset */
382 for (port=0; port < 10; port++)
383 __cvmx_spi4000_mdio_write(interface, port, 0x0, 0x9140);
388 * Poll all the SPI4000 port and check its speed
390 * @param interface Interface the SPI4000 is on
391 * @param port Port to poll (0-9)
392 * @return Status of the port. 0=down. All other values the port is up.
394 cvmx_gmxx_rxx_rx_inbnd_t cvmx_spi4000_check_speed(int interface, int port)
396 static int phy_status[10] = {0,};
397 cvmx_gmxx_rxx_rx_inbnd_t link;
402 if (!interface_is_spi4000[interface])
407 /* Register 0x11: PHY Specific Status Register
408 Register Function Setting Mode HW Rst SW Rst Notes
410 17.15:14 Speed 11 = Reserved
415 17.13 Duplex 1 = Full-duplex RO 0 Retain note
417 17.12 Page Received 1 = Page received RO, LH 0 0
418 0 = Page not received
419 1 = Resolved RO 0 0 note
421 0 = Not resolved 17.a
424 17.10 Link (real time) 1 = Link up RO 0 0
435 17.6 MDI Crossover 1 = MDIX RO 0 0 note
437 17.5 Downshift Sta- 1 = Downshift RO 0 0
439 17.4 Energy Detect 1 = Sleep RO 0 0
441 17.3 Transmit Pause 1 = Transmit pause enabled RO 0 0 note17.
442 Enabled 0 = Transmit pause disabled a, 17.c
443 17.2 Receive Pause 1 = Receive pause enabled RO 0 0 note17.
444 Enabled 0 = Receive pause disabled a, 17.c
445 17.1 Polarity (real 1 = Reversed RO 0 0
447 17.0 Jabber (real 1 = Jabber RO 0 Retain
450 read_status = __cvmx_spi4000_mdio_read(interface, port, 0x11);
451 if ((read_status & (1<<10)) == 0)
452 read_status = 0; /* If the link is down, force zero */
454 read_status &= 0xe400; /* Strip off all the don't care bits */
455 if (read_status != phy_status[port])
457 phy_status[port] = read_status;
458 if (read_status & (1<<10))
460 /* If the link is up, we need to set the speed based on the PHY status */
461 if (read_status & (1<<15))
462 __cvmx_spi4000_write(interface, (port<<7) | 0x0010, 0x3); /* 1Gbps */
464 __cvmx_spi4000_write(interface, (port<<7) | 0x0010, 0x1); /* 100Mbps */
468 /* If the link is down, force 1Gbps so TX traffic dumps fast */
469 __cvmx_spi4000_write(interface, (port<<7) | 0x0010, 0x3); /* 1Gbps */
473 if (read_status & (1<<10))
475 link.s.status = 1; /* Link up */
476 if (read_status & (1<<15))
483 link.s.speed = 2; /* Use 1Gbps when down */
484 link.s.status = 0; /* Link Down */
486 link.s.duplex = ((read_status & (1<<13)) != 0);
490 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
491 EXPORT_SYMBOL(cvmx_spi4000_check_speed);
496 * Return non-zero if the SPI interface has a SPI4000 attached
498 * @param interface SPI interface the SPI4000 is connected to
502 int cvmx_spi4000_is_present(int interface)
504 if (!(OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)))
506 // Check for the presence of a SPI4000. If it isn't there,
507 // these writes will timeout.
508 if (cvmx_twsi_write8(SPI4000_TWSI_ID(interface), SPI4000_WRITE_ADDRESS_HIGH, 0))
510 if (cvmx_twsi_write8(SPI4000_TWSI_ID(interface), SPI4000_WRITE_ADDRESS_LOW, 0))
512 interface_is_spi4000[interface] = 1;
518 * Initialize the SPI4000 for use
520 * @param interface SPI interface the SPI4000 is connected to
522 int cvmx_spi4000_initialize(int interface)
524 if (!cvmx_spi4000_is_present(interface))
527 __cvmx_spi4000_configure_mac(interface);
528 __cvmx_spi4000_configure_phy(interface);