2 * Copyright (c) 2003-2012 Broadcom Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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>
38 #include <dev/iicbus/iicoc.h>
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 */
44 #include <mips/nlm/board.h>
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
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)
54 static uint64_t eeprom_i2c_base;
57 oc_wait_on_status(uint8_t bit)
59 int tries = I2C_TIMEOUT;
63 status = oc_read_reg(OC_I2C_STATUS_REG);
64 } while ((status & bit) != 0 && --tries > 0);
66 return (tries == 0 ? -1: 0);
70 oc_rd_cmd(uint8_t cmd)
74 oc_write_reg(OC_I2C_CMD_REG, cmd);
75 if (oc_wait_on_status(OC_STATUS_TIP) < 0)
78 data = oc_read_reg(OC_I2C_DATA_REG);
83 oc_wr_cmd(uint8_t data, uint8_t cmd)
85 oc_write_reg(OC_I2C_DATA_REG, data);
86 oc_write_reg(OC_I2C_CMD_REG, cmd);
88 if (oc_wait_on_status(OC_STATUS_TIP) < 0)
94 nlm_board_eeprom_read(int node, int bus, int addr, int offs, uint8_t *buf,
100 eeprom_i2c_base = nlm_pcicfg_base(XLP_IO_I2C_OFFSET(node, bus)) +
103 if (oc_wait_on_status(OC_STATUS_BUSY) < 0) {
109 if (oc_wr_cmd(addr, OC_COMMAND_START)) {
110 err = "I2C write start failed.";
114 if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
115 err = "No ack after start";
119 if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_AL) {
120 err = "I2C Bus Arbitration Lost";
125 if (oc_wr_cmd(offs, OC_COMMAND_WRITE)) {
126 err = "I2C write slave offset failed.";
130 if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
131 err = "No ack after write";
136 if (oc_wr_cmd(addr | 1, OC_COMMAND_START)) {
137 err = "I2C read start failed.";
141 if (oc_read_reg(OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
142 err = "No ack after read start";
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.";
155 if ((rd = oc_rd_cmd(OC_COMMAND_RDNACK)) < 0) {
156 err = "I2C read last data byte failed.";
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__);
168 printf("%s: Failed (%s)\n", __func__, err);