xref: /openbmc/linux/arch/mips/include/asm/octeon/cvmx.h (revision 26afc5e3)
158f07778SDavid Daney /***********************license start***************
258f07778SDavid Daney  * Author: Cavium Networks
358f07778SDavid Daney  *
458f07778SDavid Daney  * Contact: support@caviumnetworks.com
558f07778SDavid Daney  * This file is part of the OCTEON SDK
658f07778SDavid Daney  *
758f07778SDavid Daney  * Copyright (c) 2003-2008 Cavium Networks
858f07778SDavid Daney  *
958f07778SDavid Daney  * This file is free software; you can redistribute it and/or modify
1058f07778SDavid Daney  * it under the terms of the GNU General Public License, Version 2, as
1158f07778SDavid Daney  * published by the Free Software Foundation.
1258f07778SDavid Daney  *
1358f07778SDavid Daney  * This file is distributed in the hope that it will be useful, but
1458f07778SDavid Daney  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
1558f07778SDavid Daney  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
1658f07778SDavid Daney  * NONINFRINGEMENT.  See the GNU General Public License for more
1758f07778SDavid Daney  * details.
1858f07778SDavid Daney  *
1958f07778SDavid Daney  * You should have received a copy of the GNU General Public License
2058f07778SDavid Daney  * along with this file; if not, write to the Free Software
2158f07778SDavid Daney  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2258f07778SDavid Daney  * or visit http://www.gnu.org/licenses/.
2358f07778SDavid Daney  *
2458f07778SDavid Daney  * This file may also be available under a different license from Cavium.
2558f07778SDavid Daney  * Contact Cavium Networks for more information
2658f07778SDavid Daney  ***********************license end**************************************/
2758f07778SDavid Daney 
2858f07778SDavid Daney #ifndef __CVMX_H__
2958f07778SDavid Daney #define __CVMX_H__
3058f07778SDavid Daney 
3158f07778SDavid Daney #include <linux/kernel.h>
3258f07778SDavid Daney #include <linux/string.h>
3358f07778SDavid Daney 
3426afc5e3SDavid Daney enum cvmx_mips_space {
3526afc5e3SDavid Daney 	CVMX_MIPS_SPACE_XKSEG = 3LL,
3626afc5e3SDavid Daney 	CVMX_MIPS_SPACE_XKPHYS = 2LL,
3726afc5e3SDavid Daney 	CVMX_MIPS_SPACE_XSSEG = 1LL,
3826afc5e3SDavid Daney 	CVMX_MIPS_SPACE_XUSEG = 0LL
3926afc5e3SDavid Daney };
4026afc5e3SDavid Daney 
4126afc5e3SDavid Daney /* These macros for use when using 32 bit pointers. */
4226afc5e3SDavid Daney #define CVMX_MIPS32_SPACE_KSEG0 1l
4326afc5e3SDavid Daney #define CVMX_ADD_SEG32(segment, add) \
4426afc5e3SDavid Daney 	(((int32_t)segment << 31) | (int32_t)(add))
4526afc5e3SDavid Daney 
4626afc5e3SDavid Daney #define CVMX_IO_SEG CVMX_MIPS_SPACE_XKPHYS
4726afc5e3SDavid Daney 
4826afc5e3SDavid Daney /* These macros simplify the process of creating common IO addresses */
4926afc5e3SDavid Daney #define CVMX_ADD_SEG(segment, add) \
5026afc5e3SDavid Daney 	((((uint64_t)segment) << 62) | (add))
5126afc5e3SDavid Daney #ifndef CVMX_ADD_IO_SEG
5226afc5e3SDavid Daney #define CVMX_ADD_IO_SEG(add) CVMX_ADD_SEG(CVMX_IO_SEG, (add))
5326afc5e3SDavid Daney #endif
5426afc5e3SDavid Daney 
5558f07778SDavid Daney #include "cvmx-asm.h"
5658f07778SDavid Daney #include "cvmx-packet.h"
5758f07778SDavid Daney #include "cvmx-sysinfo.h"
5858f07778SDavid Daney 
5958f07778SDavid Daney #include "cvmx-ciu-defs.h"
6058f07778SDavid Daney #include "cvmx-gpio-defs.h"
6158f07778SDavid Daney #include "cvmx-iob-defs.h"
6258f07778SDavid Daney #include "cvmx-ipd-defs.h"
6358f07778SDavid Daney #include "cvmx-l2c-defs.h"
6458f07778SDavid Daney #include "cvmx-l2d-defs.h"
6558f07778SDavid Daney #include "cvmx-l2t-defs.h"
6658f07778SDavid Daney #include "cvmx-led-defs.h"
6758f07778SDavid Daney #include "cvmx-mio-defs.h"
6858f07778SDavid Daney #include "cvmx-pow-defs.h"
6958f07778SDavid Daney 
7058f07778SDavid Daney #include "cvmx-bootinfo.h"
7158f07778SDavid Daney #include "cvmx-bootmem.h"
7258f07778SDavid Daney #include "cvmx-l2c.h"
7358f07778SDavid Daney 
7458f07778SDavid Daney #ifndef CVMX_ENABLE_DEBUG_PRINTS
7558f07778SDavid Daney #define CVMX_ENABLE_DEBUG_PRINTS 1
7658f07778SDavid Daney #endif
7758f07778SDavid Daney 
7858f07778SDavid Daney #if CVMX_ENABLE_DEBUG_PRINTS
7958f07778SDavid Daney #define cvmx_dprintf        printk
8058f07778SDavid Daney #else
8158f07778SDavid Daney #define cvmx_dprintf(...)   {}
8258f07778SDavid Daney #endif
8358f07778SDavid Daney 
8458f07778SDavid Daney #define CVMX_MAX_CORES          (16)
8558f07778SDavid Daney #define CVMX_CACHE_LINE_SIZE    (128)	/* In bytes */
8658f07778SDavid Daney #define CVMX_CACHE_LINE_MASK    (CVMX_CACHE_LINE_SIZE - 1)	/* In bytes */
8758f07778SDavid Daney #define CVMX_CACHE_LINE_ALIGNED __attribute__ ((aligned(CVMX_CACHE_LINE_SIZE)))
8858f07778SDavid Daney #define CAST64(v) ((long long)(long)(v))
8958f07778SDavid Daney #define CASTPTR(type, v) ((type *)(long)(v))
9058f07778SDavid Daney 
9158f07778SDavid Daney /*
9258f07778SDavid Daney  * Returns processor ID, different Linux and simple exec versions
9358f07778SDavid Daney  * provided in the cvmx-app-init*.c files.
9458f07778SDavid Daney  */
9558f07778SDavid Daney static inline uint32_t cvmx_get_proc_id(void) __attribute__ ((pure));
9658f07778SDavid Daney static inline uint32_t cvmx_get_proc_id(void)
9758f07778SDavid Daney {
9858f07778SDavid Daney 	uint32_t id;
9958f07778SDavid Daney 	asm("mfc0 %0, $15,0" : "=r"(id));
10058f07778SDavid Daney 	return id;
10158f07778SDavid Daney }
10258f07778SDavid Daney 
10358f07778SDavid Daney /* turn the variable name into a string */
10458f07778SDavid Daney #define CVMX_TMP_STR(x) CVMX_TMP_STR2(x)
10558f07778SDavid Daney #define CVMX_TMP_STR2(x) #x
10658f07778SDavid Daney 
10758f07778SDavid Daney /**
10858f07778SDavid Daney  * Builds a bit mask given the required size in bits.
10958f07778SDavid Daney  *
11058f07778SDavid Daney  * @bits:   Number of bits in the mask
11158f07778SDavid Daney  * Returns The mask
11258f07778SDavid Daney  */ static inline uint64_t cvmx_build_mask(uint64_t bits)
11358f07778SDavid Daney {
11458f07778SDavid Daney 	return ~((~0x0ull) << bits);
11558f07778SDavid Daney }
11658f07778SDavid Daney 
11758f07778SDavid Daney /**
11858f07778SDavid Daney  * Builds a memory address for I/O based on the Major and Sub DID.
11958f07778SDavid Daney  *
12058f07778SDavid Daney  * @major_did: 5 bit major did
12158f07778SDavid Daney  * @sub_did:   3 bit sub did
12258f07778SDavid Daney  * Returns I/O base address
12358f07778SDavid Daney  */
12458f07778SDavid Daney static inline uint64_t cvmx_build_io_address(uint64_t major_did,
12558f07778SDavid Daney 					     uint64_t sub_did)
12658f07778SDavid Daney {
12758f07778SDavid Daney 	return (0x1ull << 48) | (major_did << 43) | (sub_did << 40);
12858f07778SDavid Daney }
12958f07778SDavid Daney 
13058f07778SDavid Daney /**
13158f07778SDavid Daney  * Perform mask and shift to place the supplied value into
13258f07778SDavid Daney  * the supplied bit rage.
13358f07778SDavid Daney  *
13458f07778SDavid Daney  * Example: cvmx_build_bits(39,24,value)
13558f07778SDavid Daney  * <pre>
13658f07778SDavid Daney  * 6       5       4       3       3       2       1
13758f07778SDavid Daney  * 3       5       7       9       1       3       5       7      0
13858f07778SDavid Daney  * +-------+-------+-------+-------+-------+-------+-------+------+
13958f07778SDavid Daney  * 000000000000000000000000___________value000000000000000000000000
14058f07778SDavid Daney  * </pre>
14158f07778SDavid Daney  *
14258f07778SDavid Daney  * @high_bit: Highest bit value can occupy (inclusive) 0-63
14358f07778SDavid Daney  * @low_bit:  Lowest bit value can occupy inclusive 0-high_bit
14458f07778SDavid Daney  * @value:    Value to use
14558f07778SDavid Daney  * Returns Value masked and shifted
14658f07778SDavid Daney  */
14758f07778SDavid Daney static inline uint64_t cvmx_build_bits(uint64_t high_bit,
14858f07778SDavid Daney 				       uint64_t low_bit, uint64_t value)
14958f07778SDavid Daney {
15058f07778SDavid Daney 	return (value & cvmx_build_mask(high_bit - low_bit + 1)) << low_bit;
15158f07778SDavid Daney }
15258f07778SDavid Daney 
15358f07778SDavid Daney /**
15425985edcSLucas De Marchi  * Convert a memory pointer (void*) into a hardware compatible
15558f07778SDavid Daney  * memory address (uint64_t). Octeon hardware widgets don't
15658f07778SDavid Daney  * understand logical addresses.
15758f07778SDavid Daney  *
15858f07778SDavid Daney  * @ptr:    C style memory pointer
15958f07778SDavid Daney  * Returns Hardware physical address
16058f07778SDavid Daney  */
16158f07778SDavid Daney static inline uint64_t cvmx_ptr_to_phys(void *ptr)
16258f07778SDavid Daney {
16358f07778SDavid Daney 	if (sizeof(void *) == 8) {
16458f07778SDavid Daney 		/*
16558f07778SDavid Daney 		 * We're running in 64 bit mode. Normally this means
16658f07778SDavid Daney 		 * that we can use 40 bits of address space (the
16758f07778SDavid Daney 		 * hardware limit). Unfortunately there is one case
16858f07778SDavid Daney 		 * were we need to limit this to 30 bits, sign
16958f07778SDavid Daney 		 * extended 32 bit. Although these are 64 bits wide,
17058f07778SDavid Daney 		 * only 30 bits can be used.
17158f07778SDavid Daney 		 */
17258f07778SDavid Daney 		if ((CAST64(ptr) >> 62) == 3)
17358f07778SDavid Daney 			return CAST64(ptr) & cvmx_build_mask(30);
17458f07778SDavid Daney 		else
17558f07778SDavid Daney 			return CAST64(ptr) & cvmx_build_mask(40);
17658f07778SDavid Daney 	} else {
17758f07778SDavid Daney 		return (long)(ptr) & 0x1fffffff;
17858f07778SDavid Daney 	}
17958f07778SDavid Daney }
18058f07778SDavid Daney 
18158f07778SDavid Daney /**
18258f07778SDavid Daney  * Convert a hardware physical address (uint64_t) into a
18358f07778SDavid Daney  * memory pointer (void *).
18458f07778SDavid Daney  *
18558f07778SDavid Daney  * @physical_address:
18658f07778SDavid Daney  *               Hardware physical address to memory
18758f07778SDavid Daney  * Returns Pointer to memory
18858f07778SDavid Daney  */
18958f07778SDavid Daney static inline void *cvmx_phys_to_ptr(uint64_t physical_address)
19058f07778SDavid Daney {
19158f07778SDavid Daney 	if (sizeof(void *) == 8) {
19258f07778SDavid Daney 		/* Just set the top bit, avoiding any TLB uglyness */
19358f07778SDavid Daney 		return CASTPTR(void,
19458f07778SDavid Daney 			       CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
19558f07778SDavid Daney 					    physical_address));
19658f07778SDavid Daney 	} else {
19758f07778SDavid Daney 		return CASTPTR(void,
19858f07778SDavid Daney 			       CVMX_ADD_SEG32(CVMX_MIPS32_SPACE_KSEG0,
19958f07778SDavid Daney 					      physical_address));
20058f07778SDavid Daney 	}
20158f07778SDavid Daney }
20258f07778SDavid Daney 
20358f07778SDavid Daney /* The following #if controls the definition of the macro
20458f07778SDavid Daney     CVMX_BUILD_WRITE64. This macro is used to build a store operation to
20558f07778SDavid Daney     a full 64bit address. With a 64bit ABI, this can be done with a simple
20658f07778SDavid Daney     pointer access. 32bit ABIs require more complicated assembly */
20758f07778SDavid Daney 
20858f07778SDavid Daney /* We have a full 64bit ABI. Writing to a 64bit address can be done with
20958f07778SDavid Daney     a simple volatile pointer */
21058f07778SDavid Daney #define CVMX_BUILD_WRITE64(TYPE, ST)                                    \
21158f07778SDavid Daney static inline void cvmx_write64_##TYPE(uint64_t addr, TYPE##_t val)     \
21258f07778SDavid Daney {                                                                       \
21358f07778SDavid Daney     *CASTPTR(volatile TYPE##_t, addr) = val;                            \
21458f07778SDavid Daney }
21558f07778SDavid Daney 
21658f07778SDavid Daney 
21758f07778SDavid Daney /* The following #if controls the definition of the macro
21858f07778SDavid Daney     CVMX_BUILD_READ64. This macro is used to build a load operation from
21958f07778SDavid Daney     a full 64bit address. With a 64bit ABI, this can be done with a simple
22058f07778SDavid Daney     pointer access. 32bit ABIs require more complicated assembly */
22158f07778SDavid Daney 
22258f07778SDavid Daney /* We have a full 64bit ABI. Writing to a 64bit address can be done with
22358f07778SDavid Daney     a simple volatile pointer */
22458f07778SDavid Daney #define CVMX_BUILD_READ64(TYPE, LT)                                     \
22558f07778SDavid Daney static inline TYPE##_t cvmx_read64_##TYPE(uint64_t addr)                \
22658f07778SDavid Daney {                                                                       \
22758f07778SDavid Daney 	return *CASTPTR(volatile TYPE##_t, addr);			\
22858f07778SDavid Daney }
22958f07778SDavid Daney 
23058f07778SDavid Daney 
23158f07778SDavid Daney /* The following defines 8 functions for writing to a 64bit address. Each
23258f07778SDavid Daney     takes two arguments, the address and the value to write.
23358f07778SDavid Daney     cvmx_write64_int64      cvmx_write64_uint64
23458f07778SDavid Daney     cvmx_write64_int32      cvmx_write64_uint32
23558f07778SDavid Daney     cvmx_write64_int16      cvmx_write64_uint16
23658f07778SDavid Daney     cvmx_write64_int8       cvmx_write64_uint8 */
23758f07778SDavid Daney CVMX_BUILD_WRITE64(int64, "sd");
23858f07778SDavid Daney CVMX_BUILD_WRITE64(int32, "sw");
23958f07778SDavid Daney CVMX_BUILD_WRITE64(int16, "sh");
24058f07778SDavid Daney CVMX_BUILD_WRITE64(int8, "sb");
24158f07778SDavid Daney CVMX_BUILD_WRITE64(uint64, "sd");
24258f07778SDavid Daney CVMX_BUILD_WRITE64(uint32, "sw");
24358f07778SDavid Daney CVMX_BUILD_WRITE64(uint16, "sh");
24458f07778SDavid Daney CVMX_BUILD_WRITE64(uint8, "sb");
24558f07778SDavid Daney #define cvmx_write64 cvmx_write64_uint64
24658f07778SDavid Daney 
24758f07778SDavid Daney /* The following defines 8 functions for reading from a 64bit address. Each
24858f07778SDavid Daney     takes the address as the only argument
24958f07778SDavid Daney     cvmx_read64_int64       cvmx_read64_uint64
25058f07778SDavid Daney     cvmx_read64_int32       cvmx_read64_uint32
25158f07778SDavid Daney     cvmx_read64_int16       cvmx_read64_uint16
25258f07778SDavid Daney     cvmx_read64_int8        cvmx_read64_uint8 */
25358f07778SDavid Daney CVMX_BUILD_READ64(int64, "ld");
25458f07778SDavid Daney CVMX_BUILD_READ64(int32, "lw");
25558f07778SDavid Daney CVMX_BUILD_READ64(int16, "lh");
25658f07778SDavid Daney CVMX_BUILD_READ64(int8, "lb");
25758f07778SDavid Daney CVMX_BUILD_READ64(uint64, "ld");
25858f07778SDavid Daney CVMX_BUILD_READ64(uint32, "lw");
25958f07778SDavid Daney CVMX_BUILD_READ64(uint16, "lhu");
26058f07778SDavid Daney CVMX_BUILD_READ64(uint8, "lbu");
26158f07778SDavid Daney #define cvmx_read64 cvmx_read64_uint64
26258f07778SDavid Daney 
26358f07778SDavid Daney 
26458f07778SDavid Daney static inline void cvmx_write_csr(uint64_t csr_addr, uint64_t val)
26558f07778SDavid Daney {
26658f07778SDavid Daney 	cvmx_write64(csr_addr, val);
26758f07778SDavid Daney 
26858f07778SDavid Daney 	/*
26958f07778SDavid Daney 	 * Perform an immediate read after every write to an RSL
27058f07778SDavid Daney 	 * register to force the write to complete. It doesn't matter
27158f07778SDavid Daney 	 * what RSL read we do, so we choose CVMX_MIO_BOOT_BIST_STAT
27258f07778SDavid Daney 	 * because it is fast and harmless.
27358f07778SDavid Daney 	 */
27426084411SDavid Daney 	if (((csr_addr >> 40) & 0x7ffff) == (0x118))
27558f07778SDavid Daney 		cvmx_read64(CVMX_MIO_BOOT_BIST_STAT);
27658f07778SDavid Daney }
27758f07778SDavid Daney 
27858f07778SDavid Daney static inline void cvmx_write_io(uint64_t io_addr, uint64_t val)
27958f07778SDavid Daney {
28058f07778SDavid Daney 	cvmx_write64(io_addr, val);
28158f07778SDavid Daney 
28258f07778SDavid Daney }
28358f07778SDavid Daney 
28458f07778SDavid Daney static inline uint64_t cvmx_read_csr(uint64_t csr_addr)
28558f07778SDavid Daney {
28658f07778SDavid Daney 	uint64_t val = cvmx_read64(csr_addr);
28758f07778SDavid Daney 	return val;
28858f07778SDavid Daney }
28958f07778SDavid Daney 
29058f07778SDavid Daney 
29158f07778SDavid Daney static inline void cvmx_send_single(uint64_t data)
29258f07778SDavid Daney {
29358f07778SDavid Daney 	const uint64_t CVMX_IOBDMA_SENDSINGLE = 0xffffffffffffa200ull;
29458f07778SDavid Daney 	cvmx_write64(CVMX_IOBDMA_SENDSINGLE, data);
29558f07778SDavid Daney }
29658f07778SDavid Daney 
29758f07778SDavid Daney static inline void cvmx_read_csr_async(uint64_t scraddr, uint64_t csr_addr)
29858f07778SDavid Daney {
29958f07778SDavid Daney 	union {
30058f07778SDavid Daney 		uint64_t u64;
30158f07778SDavid Daney 		struct {
30258f07778SDavid Daney 			uint64_t scraddr:8;
30358f07778SDavid Daney 			uint64_t len:8;
30458f07778SDavid Daney 			uint64_t addr:48;
30558f07778SDavid Daney 		} s;
30658f07778SDavid Daney 	} addr;
30758f07778SDavid Daney 	addr.u64 = csr_addr;
30858f07778SDavid Daney 	addr.s.scraddr = scraddr >> 3;
30958f07778SDavid Daney 	addr.s.len = 1;
31058f07778SDavid Daney 	cvmx_send_single(addr.u64);
31158f07778SDavid Daney }
31258f07778SDavid Daney 
31358f07778SDavid Daney /* Return true if Octeon is CN38XX pass 1 */
31458f07778SDavid Daney static inline int cvmx_octeon_is_pass1(void)
31558f07778SDavid Daney {
31658f07778SDavid Daney #if OCTEON_IS_COMMON_BINARY()
31758f07778SDavid Daney 	return 0;	/* Pass 1 isn't supported for common binaries */
31858f07778SDavid Daney #else
31958f07778SDavid Daney /* Now that we know we're built for a specific model, only check CN38XX */
32058f07778SDavid Daney #if OCTEON_IS_MODEL(OCTEON_CN38XX)
32158f07778SDavid Daney 	return cvmx_get_proc_id() == OCTEON_CN38XX_PASS1;
32258f07778SDavid Daney #else
32358f07778SDavid Daney 	return 0;	/* Built for non CN38XX chip, we're not CN38XX pass1 */
32458f07778SDavid Daney #endif
32558f07778SDavid Daney #endif
32658f07778SDavid Daney }
32758f07778SDavid Daney 
32858f07778SDavid Daney static inline unsigned int cvmx_get_core_num(void)
32958f07778SDavid Daney {
33058f07778SDavid Daney 	unsigned int core_num;
33158f07778SDavid Daney 	CVMX_RDHWRNV(core_num, 0);
33258f07778SDavid Daney 	return core_num;
33358f07778SDavid Daney }
33458f07778SDavid Daney 
33558f07778SDavid Daney /**
33658f07778SDavid Daney  * Returns the number of bits set in the provided value.
33758f07778SDavid Daney  * Simple wrapper for POP instruction.
33858f07778SDavid Daney  *
33958f07778SDavid Daney  * @val:    32 bit value to count set bits in
34058f07778SDavid Daney  *
34158f07778SDavid Daney  * Returns Number of bits set
34258f07778SDavid Daney  */
34358f07778SDavid Daney static inline uint32_t cvmx_pop(uint32_t val)
34458f07778SDavid Daney {
34558f07778SDavid Daney 	uint32_t pop;
34658f07778SDavid Daney 	CVMX_POP(pop, val);
34758f07778SDavid Daney 	return pop;
34858f07778SDavid Daney }
34958f07778SDavid Daney 
35058f07778SDavid Daney /**
35158f07778SDavid Daney  * Returns the number of bits set in the provided value.
35258f07778SDavid Daney  * Simple wrapper for DPOP instruction.
35358f07778SDavid Daney  *
35458f07778SDavid Daney  * @val:    64 bit value to count set bits in
35558f07778SDavid Daney  *
35658f07778SDavid Daney  * Returns Number of bits set
35758f07778SDavid Daney  */
35858f07778SDavid Daney static inline int cvmx_dpop(uint64_t val)
35958f07778SDavid Daney {
36058f07778SDavid Daney 	int pop;
36158f07778SDavid Daney 	CVMX_DPOP(pop, val);
36258f07778SDavid Daney 	return pop;
36358f07778SDavid Daney }
36458f07778SDavid Daney 
36558f07778SDavid Daney /**
36658f07778SDavid Daney  * Provide current cycle counter as a return value
36758f07778SDavid Daney  *
36858f07778SDavid Daney  * Returns current cycle counter
36958f07778SDavid Daney  */
37058f07778SDavid Daney 
37158f07778SDavid Daney static inline uint64_t cvmx_get_cycle(void)
37258f07778SDavid Daney {
37358f07778SDavid Daney 	uint64_t cycle;
37458f07778SDavid Daney 	CVMX_RDHWR(cycle, 31);
37558f07778SDavid Daney 	return cycle;
37658f07778SDavid Daney }
37758f07778SDavid Daney 
37858f07778SDavid Daney /**
379e8635b48SDavid Daney  * Wait for the specified number of cycle
380e8635b48SDavid Daney  *
381e8635b48SDavid Daney  */
382e8635b48SDavid Daney static inline void cvmx_wait(uint64_t cycles)
383e8635b48SDavid Daney {
384e8635b48SDavid Daney 	uint64_t done = cvmx_get_cycle() + cycles;
385e8635b48SDavid Daney 
386e8635b48SDavid Daney 	while (cvmx_get_cycle() < done)
387e8635b48SDavid Daney 		; /* Spin */
388e8635b48SDavid Daney }
389e8635b48SDavid Daney 
390e8635b48SDavid Daney /**
39158f07778SDavid Daney  * Reads a chip global cycle counter.  This counts CPU cycles since
39258f07778SDavid Daney  * chip reset.  The counter is 64 bit.
39358f07778SDavid Daney  * This register does not exist on CN38XX pass 1 silicion
39458f07778SDavid Daney  *
39558f07778SDavid Daney  * Returns Global chip cycle count since chip reset.
39658f07778SDavid Daney  */
39758f07778SDavid Daney static inline uint64_t cvmx_get_cycle_global(void)
39858f07778SDavid Daney {
39958f07778SDavid Daney 	if (cvmx_octeon_is_pass1())
40058f07778SDavid Daney 		return 0;
40158f07778SDavid Daney 	else
40258f07778SDavid Daney 		return cvmx_read64(CVMX_IPD_CLK_COUNT);
40358f07778SDavid Daney }
40458f07778SDavid Daney 
40558f07778SDavid Daney /**
40658f07778SDavid Daney  * This macro spins on a field waiting for it to reach a value. It
40758f07778SDavid Daney  * is common in code to need to wait for a specific field in a CSR
40858f07778SDavid Daney  * to match a specific value. Conceptually this macro expands to:
40958f07778SDavid Daney  *
41058f07778SDavid Daney  * 1) read csr at "address" with a csr typedef of "type"
41158f07778SDavid Daney  * 2) Check if ("type".s."field" "op" "value")
41258f07778SDavid Daney  * 3) If #2 isn't true loop to #1 unless too much time has passed.
41358f07778SDavid Daney  */
41458f07778SDavid Daney #define CVMX_WAIT_FOR_FIELD64(address, type, field, op, value, timeout_usec)\
41558f07778SDavid Daney     (									\
41658f07778SDavid Daney {									\
41758f07778SDavid Daney 	int result;							\
41858f07778SDavid Daney 	do {								\
41958f07778SDavid Daney 		uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
42058f07778SDavid Daney 			cvmx_sysinfo_get()->cpu_clock_hz / 1000000;	\
42158f07778SDavid Daney 		type c;							\
42258f07778SDavid Daney 		while (1) {						\
42358f07778SDavid Daney 			c.u64 = cvmx_read_csr(address);			\
42458f07778SDavid Daney 			if ((c.s.field) op(value)) {			\
42558f07778SDavid Daney 				result = 0;				\
42658f07778SDavid Daney 				break;					\
42758f07778SDavid Daney 			} else if (cvmx_get_cycle() > done) {		\
42858f07778SDavid Daney 				result = -1;				\
42958f07778SDavid Daney 				break;					\
43058f07778SDavid Daney 			} else						\
43158f07778SDavid Daney 				cvmx_wait(100);				\
43258f07778SDavid Daney 		}							\
43358f07778SDavid Daney 	} while (0);							\
43458f07778SDavid Daney 	result;								\
43558f07778SDavid Daney })
43658f07778SDavid Daney 
43758f07778SDavid Daney /***************************************************************************/
43858f07778SDavid Daney 
43958f07778SDavid Daney static inline void cvmx_reset_octeon(void)
44058f07778SDavid Daney {
44158f07778SDavid Daney 	union cvmx_ciu_soft_rst ciu_soft_rst;
44258f07778SDavid Daney 	ciu_soft_rst.u64 = 0;
44358f07778SDavid Daney 	ciu_soft_rst.s.soft_rst = 1;
44458f07778SDavid Daney 	cvmx_write_csr(CVMX_CIU_SOFT_RST, ciu_soft_rst.u64);
44558f07778SDavid Daney }
44658f07778SDavid Daney 
44758f07778SDavid Daney /* Return the number of cores available in the chip */
44858f07778SDavid Daney static inline uint32_t cvmx_octeon_num_cores(void)
44958f07778SDavid Daney {
45058f07778SDavid Daney 	uint32_t ciu_fuse = (uint32_t) cvmx_read_csr(CVMX_CIU_FUSE) & 0xffff;
45158f07778SDavid Daney 	return cvmx_pop(ciu_fuse);
45258f07778SDavid Daney }
45358f07778SDavid Daney 
45458f07778SDavid Daney /**
45558f07778SDavid Daney  * Read a byte of fuse data
45658f07778SDavid Daney  * @byte_addr:   address to read
45758f07778SDavid Daney  *
45858f07778SDavid Daney  * Returns fuse value: 0 or 1
45958f07778SDavid Daney  */
46058f07778SDavid Daney static uint8_t cvmx_fuse_read_byte(int byte_addr)
46158f07778SDavid Daney {
46258f07778SDavid Daney 	union cvmx_mio_fus_rcmd read_cmd;
46358f07778SDavid Daney 
46458f07778SDavid Daney 	read_cmd.u64 = 0;
46558f07778SDavid Daney 	read_cmd.s.addr = byte_addr;
46658f07778SDavid Daney 	read_cmd.s.pend = 1;
46758f07778SDavid Daney 	cvmx_write_csr(CVMX_MIO_FUS_RCMD, read_cmd.u64);
46858f07778SDavid Daney 	while ((read_cmd.u64 = cvmx_read_csr(CVMX_MIO_FUS_RCMD))
46958f07778SDavid Daney 	       && read_cmd.s.pend)
47058f07778SDavid Daney 		;
47158f07778SDavid Daney 	return read_cmd.s.dat;
47258f07778SDavid Daney }
47358f07778SDavid Daney 
47458f07778SDavid Daney /**
47558f07778SDavid Daney  * Read a single fuse bit
47658f07778SDavid Daney  *
47758f07778SDavid Daney  * @fuse:   Fuse number (0-1024)
47858f07778SDavid Daney  *
47958f07778SDavid Daney  * Returns fuse value: 0 or 1
48058f07778SDavid Daney  */
48158f07778SDavid Daney static inline int cvmx_fuse_read(int fuse)
48258f07778SDavid Daney {
48358f07778SDavid Daney 	return (cvmx_fuse_read_byte(fuse >> 3) >> (fuse & 0x7)) & 1;
48458f07778SDavid Daney }
48558f07778SDavid Daney 
48658f07778SDavid Daney static inline int cvmx_octeon_model_CN36XX(void)
48758f07778SDavid Daney {
48858f07778SDavid Daney 	return OCTEON_IS_MODEL(OCTEON_CN38XX)
48958f07778SDavid Daney 		&& !cvmx_octeon_is_pass1()
49058f07778SDavid Daney 		&& cvmx_fuse_read(264);
49158f07778SDavid Daney }
49258f07778SDavid Daney 
49358f07778SDavid Daney static inline int cvmx_octeon_zip_present(void)
49458f07778SDavid Daney {
49558f07778SDavid Daney 	return octeon_has_feature(OCTEON_FEATURE_ZIP);
49658f07778SDavid Daney }
49758f07778SDavid Daney 
49858f07778SDavid Daney static inline int cvmx_octeon_dfa_present(void)
49958f07778SDavid Daney {
50058f07778SDavid Daney 	if (!OCTEON_IS_MODEL(OCTEON_CN38XX)
50158f07778SDavid Daney 	    && !OCTEON_IS_MODEL(OCTEON_CN31XX)
50258f07778SDavid Daney 	    && !OCTEON_IS_MODEL(OCTEON_CN58XX))
50358f07778SDavid Daney 		return 0;
50458f07778SDavid Daney 	else if (OCTEON_IS_MODEL(OCTEON_CN3020))
50558f07778SDavid Daney 		return 0;
50658f07778SDavid Daney 	else if (cvmx_octeon_is_pass1())
50758f07778SDavid Daney 		return 1;
50858f07778SDavid Daney 	else
50958f07778SDavid Daney 		return !cvmx_fuse_read(120);
51058f07778SDavid Daney }
51158f07778SDavid Daney 
51258f07778SDavid Daney static inline int cvmx_octeon_crypto_present(void)
51358f07778SDavid Daney {
51458f07778SDavid Daney 	return octeon_has_feature(OCTEON_FEATURE_CRYPTO);
51558f07778SDavid Daney }
51658f07778SDavid Daney 
51758f07778SDavid Daney #endif /*  __CVMX_H__  */
518