summaryrefslogtreecommitdiffstats
path: root/sys/mips/nlm/board_eeprom.c
blob: c202acf4895d383cc0fffd26e923c5a92e64c388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*-
 * Copyright (c) 2003-2012 Broadcom Corporation
 * All Rights Reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/endian.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/limits.h>
#include <sys/bus.h>

#include <dev/iicbus/iicoc.h>

#include <mips/nlm/hal/haldefs.h>
#include <mips/nlm/hal/iomap.h>
#include <mips/nlm/hal/mips-extns.h> /* needed by board.h */

#include <mips/nlm/board.h>

/*
 * We have to read the EEPROM in early boot (now only for MAC addr)
 * but later for board information.  Use simple polled mode driver
 * for I2C
 */
#define	oc_read_reg(reg)	nlm_read_reg(eeprom_i2c_base, reg)
#define	oc_write_reg(reg, val)	nlm_write_reg(eeprom_i2c_base, reg, val)

static uint64_t eeprom_i2c_base;

static int
oc_wait_on_status(uint8_t bit)
{
	int tries = I2C_TIMEOUT;
	uint8_t status;

	do {
		status = oc_read_reg(OC_I2C_STATUS_REG);
	} while ((status & bit) != 0 && --tries > 0);

	return (tries == 0 ? -1: 0);
}

static int
oc_rd_cmd(uint8_t cmd)
{
	uint8_t data;

		oc_write_reg(OC_I2C_CMD_REG, cmd);
		if (oc_wait_on_status(OC_STATUS_TIP) < 0)
			return (-1);

	data = oc_read_reg(OC_I2C_DATA_REG);
	return (data);
}

static int
oc_wr_cmd(uint8_t data, uint8_t cmd)
{
	oc_write_reg(OC_I2C_DATA_REG, data);
	oc_write_reg(OC_I2C_CMD_REG, cmd);

	if (oc_wait_on_status(OC_STATUS_TIP) < 0)
		return (-1);
	return (0);
}

int
nlm_board_eeprom_read(int node, int bus, int addr, int offs, uint8_t *buf,
    int sz)
{
	int rd, i;
	char *err = NULL;

	eeprom_i2c_base = nlm_pcicfg_base(XLP_IO_I2C_OFFSET(node, bus)) +
	    XLP_IO_PCI_HDRSZ;

	if (oc_wait_on_status(OC_STATUS_BUSY) < 0) {
		err = "Not idle";
		goto err_exit;
	}

	/* write start */
	if (oc_wr_cmd(addr, OC_COMMAND_START)) {
		err = "I2C write start failed.";
		goto err_exit;
	}

	if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
		err = "No ack after start";
		goto err_exit_stop;
	}

	if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_AL) {
		err = "I2C Bus Arbitration Lost";
		goto err_exit_stop;
	}

	/* Write offset */
	if (oc_wr_cmd(offs, OC_COMMAND_WRITE)) {
		err = "I2C write slave offset failed.";
		goto err_exit_stop;
	}

	if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
		err = "No ack after write";
		goto err_exit_stop;
	}

	/* read start */
	if (oc_wr_cmd(addr | 1, OC_COMMAND_START)) {
		err = "I2C read start failed.";
		goto err_exit_stop;
	}

	if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
		err = "No ack after read start";
		goto err_exit_stop;
	}

	for (i = 0; i < sz - 1; i++) {
		if ((rd = oc_rd_cmd(OC_COMMAND_READ)) < 0) {
			err = "I2C read data byte failed.";
			goto err_exit_stop;
		}
	buf[i] = rd;
	}

	/* last byte */
	if ((rd = oc_rd_cmd(OC_COMMAND_RDNACK)) < 0) {
		err = "I2C read last data byte failed.";
		goto err_exit_stop;
	}
	buf[sz - 1] = rd;

err_exit_stop:
	oc_write_reg(OC_I2C_CMD_REG, OC_COMMAND_STOP);
	if (oc_wait_on_status(OC_STATUS_BUSY) < 0)
		printf("%s: stop failed", __func__);

err_exit:
	if (err) {
		printf("%s: Failed (%s)\n", __func__, err);
		return (-1);
	}
	return (0);
}
OpenPOWER on IntegriCloud