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