1/*-
2 * Copyright (c) 2003-2012 Broadcom Corporation
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in
13 *    the documentation and/or other materials provided with the
14 *    distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: releng/10.2/sys/mips/nlm/board_eeprom.c 233542 2012-03-27 12:25:47Z jchandra $");
31#include <sys/param.h>
32#include <sys/endian.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/limits.h>
36#include <sys/bus.h>
37
38#include <dev/iicbus/iicoc.h>
39
40#include <mips/nlm/hal/haldefs.h>
41#include <mips/nlm/hal/iomap.h>
42#include <mips/nlm/hal/mips-extns.h> /* needed by board.h */
43
44#include <mips/nlm/board.h>
45
46/*
47 * We have to read the EEPROM in early boot (now only for MAC addr)
48 * but later for board information.  Use simple polled mode driver
49 * for I2C
50 */
51#define	oc_read_reg(reg)	nlm_read_reg(eeprom_i2c_base, reg)
52#define	oc_write_reg(reg, val)	nlm_write_reg(eeprom_i2c_base, reg, val)
53
54static uint64_t eeprom_i2c_base;
55
56static int
57oc_wait_on_status(uint8_t bit)
58{
59	int tries = I2C_TIMEOUT;
60	uint8_t status;
61
62	do {
63		status = oc_read_reg(OC_I2C_STATUS_REG);
64	} while ((status & bit) != 0 && --tries > 0);
65
66	return (tries == 0 ? -1: 0);
67}
68
69static int
70oc_rd_cmd(uint8_t cmd)
71{
72	uint8_t data;
73
74		oc_write_reg(OC_I2C_CMD_REG, cmd);
75		if (oc_wait_on_status(OC_STATUS_TIP) < 0)
76			return (-1);
77
78	data = oc_read_reg(OC_I2C_DATA_REG);
79	return (data);
80}
81
82static int
83oc_wr_cmd(uint8_t data, uint8_t cmd)
84{
85	oc_write_reg(OC_I2C_DATA_REG, data);
86	oc_write_reg(OC_I2C_CMD_REG, cmd);
87
88	if (oc_wait_on_status(OC_STATUS_TIP) < 0)
89		return (-1);
90	return (0);
91}
92
93int
94nlm_board_eeprom_read(int node, int bus, int addr, int offs, uint8_t *buf,
95    int sz)
96{
97	int rd, i;
98	char *err = NULL;
99
100	eeprom_i2c_base = nlm_pcicfg_base(XLP_IO_I2C_OFFSET(node, bus)) +
101	    XLP_IO_PCI_HDRSZ;
102
103	if (oc_wait_on_status(OC_STATUS_BUSY) < 0) {
104		err = "Not idle";
105		goto err_exit;
106	}
107
108	/* write start */
109	if (oc_wr_cmd(addr, OC_COMMAND_START)) {
110		err = "I2C write start failed.";
111		goto err_exit;
112	}
113
114	if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
115		err = "No ack after start";
116		goto err_exit_stop;
117	}
118
119	if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_AL) {
120		err = "I2C Bus Arbitration Lost";
121		goto err_exit_stop;
122	}
123
124	/* Write offset */
125	if (oc_wr_cmd(offs, OC_COMMAND_WRITE)) {
126		err = "I2C write slave offset failed.";
127		goto err_exit_stop;
128	}
129
130	if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
131		err = "No ack after write";
132		goto err_exit_stop;
133	}
134
135	/* read start */
136	if (oc_wr_cmd(addr | 1, OC_COMMAND_START)) {
137		err = "I2C read start failed.";
138		goto err_exit_stop;
139	}
140
141	if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
142		err = "No ack after read start";
143		goto err_exit_stop;
144	}
145
146	for (i = 0; i < sz - 1; i++) {
147		if ((rd = oc_rd_cmd(OC_COMMAND_READ)) < 0) {
148			err = "I2C read data byte failed.";
149			goto err_exit_stop;
150		}
151	buf[i] = rd;
152	}
153
154	/* last byte */
155	if ((rd = oc_rd_cmd(OC_COMMAND_RDNACK)) < 0) {
156		err = "I2C read last data byte failed.";
157		goto err_exit_stop;
158	}
159	buf[sz - 1] = rd;
160
161err_exit_stop:
162	oc_write_reg(OC_I2C_CMD_REG, OC_COMMAND_STOP);
163	if (oc_wait_on_status(OC_STATUS_BUSY) < 0)
164		printf("%s: stop failed", __func__);
165
166err_exit:
167	if (err) {
168		printf("%s: Failed (%s)\n", __func__, err);
169		return (-1);
170	}
171	return (0);
172}
173