1233542Sjchandra/*-
2233542Sjchandra * Copyright (c) 2003-2012 Broadcom Corporation
3233542Sjchandra * All Rights Reserved
4233542Sjchandra *
5233542Sjchandra * Redistribution and use in source and binary forms, with or without
6233542Sjchandra * modification, are permitted provided that the following conditions
7233542Sjchandra * are met:
8233542Sjchandra *
9233542Sjchandra * 1. Redistributions of source code must retain the above copyright
10233542Sjchandra *    notice, this list of conditions and the following disclaimer.
11233542Sjchandra * 2. Redistributions in binary form must reproduce the above copyright
12233542Sjchandra *    notice, this list of conditions and the following disclaimer in
13233542Sjchandra *    the documentation and/or other materials provided with the
14233542Sjchandra *    distribution.
15233542Sjchandra *
16233542Sjchandra * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
17233542Sjchandra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18233542Sjchandra * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19233542Sjchandra * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
20233542Sjchandra * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21233542Sjchandra * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22233542Sjchandra * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23233542Sjchandra * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24233542Sjchandra * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25233542Sjchandra * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26233542Sjchandra * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27233542Sjchandra */
28233542Sjchandra
29233542Sjchandra#include <sys/cdefs.h>
30233542Sjchandra__FBSDID("$FreeBSD$");
31233542Sjchandra#include <sys/param.h>
32233542Sjchandra#include <sys/systm.h>
33233542Sjchandra#include <sys/endian.h>
34233542Sjchandra
35233542Sjchandra#include <mips/nlm/hal/mips-extns.h>
36233542Sjchandra#include <mips/nlm/hal/haldefs.h>
37233542Sjchandra#include <mips/nlm/hal/iomap.h>
38233542Sjchandra#include <mips/nlm/hal/gbu.h>
39233542Sjchandra
40233542Sjchandra#include <mips/nlm/board.h>
41233542Sjchandra
42233542Sjchandra#define CPLD_REVISION		0x0
43233542Sjchandra#define CPLD_RESET		0x1
44233542Sjchandra#define CPLD_CTRL		0x2
45233542Sjchandra#define CPLD_RSVD		0x3
46233542Sjchandra#define CPLD_PWR_CTRL		0x4
47233542Sjchandra#define CPLD_MISC		0x5
48233542Sjchandra#define CPLD_CTRL_STATUS	0x6
49233542Sjchandra#define CPLD_PWR_INTR_STATUS	0x7
50233542Sjchandra#define CPLD_DATA		0x8
51233542Sjchandra
52233542Sjchandrastatic __inline
53233542Sjchandraint nlm_cpld_read(uint64_t base, int reg)
54233542Sjchandra{
55233542Sjchandra	uint16_t val;
56233542Sjchandra
57233542Sjchandra	val = *(volatile uint16_t *)(long)(base + reg * 2);
58255368Sjchandra	return le16toh(val);
59233542Sjchandra}
60233542Sjchandra
61233542Sjchandrastatic __inline void
62233542Sjchandranlm_cpld_write(uint64_t base, int reg, uint16_t data)
63233542Sjchandra{
64255368Sjchandra	data = htole16(data);
65233542Sjchandra	*(volatile uint16_t *)(long)(base + reg * 2) = data;
66233542Sjchandra}
67233542Sjchandra
68233542Sjchandraint
69233542Sjchandranlm_board_cpld_majorversion(uint64_t base)
70233542Sjchandra{
71233542Sjchandra	return (nlm_cpld_read(base, CPLD_REVISION) >> 8);
72233542Sjchandra}
73233542Sjchandra
74233542Sjchandraint
75233542Sjchandranlm_board_cpld_minorversion(uint64_t base)
76233542Sjchandra{
77233542Sjchandra	return (nlm_cpld_read(base, CPLD_REVISION) & 0xff);
78233542Sjchandra}
79233542Sjchandra
80233542Sjchandrauint64_t nlm_board_cpld_base(int node, int chipselect)
81233542Sjchandra{
82233542Sjchandra	uint64_t gbubase, cpld_phys;
83233542Sjchandra
84233542Sjchandra	gbubase = nlm_get_gbu_regbase(node);
85233542Sjchandra	cpld_phys = nlm_read_gbu_reg(gbubase, GBU_CS_BASEADDR(chipselect));
86233542Sjchandra	return (MIPS_PHYS_TO_KSEG1(cpld_phys << 8));
87233542Sjchandra}
88233542Sjchandra
89233542Sjchandravoid
90233542Sjchandranlm_board_cpld_reset(uint64_t base)
91233542Sjchandra{
92233542Sjchandra
93233542Sjchandra	nlm_cpld_write(base, CPLD_RESET, 1 << 15);
94233542Sjchandra	for(;;)
95233542Sjchandra		__asm __volatile("wait");
96233542Sjchandra}
97233542Sjchandra
98233542Sjchandra/* get daughter board type */
99233542Sjchandraint
100233542Sjchandranlm_board_cpld_dboard_type(uint64_t base, int slot)
101233542Sjchandra{
102233542Sjchandra	uint16_t val;
103233542Sjchandra	int shift = 0;
104233542Sjchandra
105233542Sjchandra	switch (slot) {
106233542Sjchandra	case 0: shift = 0; break;
107233542Sjchandra	case 1: shift = 4; break;
108233542Sjchandra	case 2: shift = 2; break;
109233542Sjchandra	case 3: shift = 6; break;
110233542Sjchandra	}
111233542Sjchandra	val = nlm_cpld_read(base, CPLD_CTRL_STATUS) >> shift;
112233542Sjchandra	return (val & 0x3);
113233542Sjchandra}
114