162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2021 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "amdgpu_eeprom.h"
2562306a36Sopenharmony_ci#include "amdgpu.h"
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/* AT24CM02 and M24M02-R have a 256-byte write page size.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci#define EEPROM_PAGE_BITS   8
3062306a36Sopenharmony_ci#define EEPROM_PAGE_SIZE   (1U << EEPROM_PAGE_BITS)
3162306a36Sopenharmony_ci#define EEPROM_PAGE_MASK   (EEPROM_PAGE_SIZE - 1)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define EEPROM_OFFSET_SIZE 2
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* EEPROM memory addresses are 19-bits long, which can
3662306a36Sopenharmony_ci * be partitioned into 3, 8, 8 bits, for a total of 19.
3762306a36Sopenharmony_ci * The upper 3 bits are sent as part of the 7-bit
3862306a36Sopenharmony_ci * "Device Type Identifier"--an I2C concept, which for EEPROM devices
3962306a36Sopenharmony_ci * is hard-coded as 1010b, indicating that it is an EEPROM
4062306a36Sopenharmony_ci * device--this is the wire format, followed by the upper
4162306a36Sopenharmony_ci * 3 bits of the 19-bit address, followed by the direction,
4262306a36Sopenharmony_ci * followed by two bytes holding the rest of the 16-bits of
4362306a36Sopenharmony_ci * the EEPROM memory address. The format on the wire for EEPROM
4462306a36Sopenharmony_ci * devices is: 1010XYZD, A15:A8, A7:A0,
4562306a36Sopenharmony_ci * Where D is the direction and sequenced out by the hardware.
4662306a36Sopenharmony_ci * Bits XYZ are memory address bits 18, 17 and 16.
4762306a36Sopenharmony_ci * These bits are compared to how pins 1-3 of the part are connected,
4862306a36Sopenharmony_ci * depending on the size of the part, more on that later.
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * Note that of this wire format, a client is in control
5162306a36Sopenharmony_ci * of, and needs to specify only XYZ, A15:A8, A7:0, bits,
5262306a36Sopenharmony_ci * which is exactly the EEPROM memory address, or offset,
5362306a36Sopenharmony_ci * in order to address up to 8 EEPROM devices on the I2C bus.
5462306a36Sopenharmony_ci *
5562306a36Sopenharmony_ci * For instance, a 2-Mbit I2C EEPROM part, addresses all its bytes,
5662306a36Sopenharmony_ci * using an 18-bit address, bit 17 to 0 and thus would use all but one bit of
5762306a36Sopenharmony_ci * the 19 bits previously mentioned. The designer would then not connect
5862306a36Sopenharmony_ci * pins 1 and 2, and pin 3 usually named "A_2" or "E2", would be connected to
5962306a36Sopenharmony_ci * either Vcc or GND. This would allow for up to two 2-Mbit parts on
6062306a36Sopenharmony_ci * the same bus, where one would be addressable with bit 18 as 1, and
6162306a36Sopenharmony_ci * the other with bit 18 of the address as 0.
6262306a36Sopenharmony_ci *
6362306a36Sopenharmony_ci * For a 2-Mbit part, bit 18 is usually known as the "Chip Enable" or
6462306a36Sopenharmony_ci * "Hardware Address Bit". This bit is compared to the load on pin 3
6562306a36Sopenharmony_ci * of the device, described above, and if there is a match, then this
6662306a36Sopenharmony_ci * device responds to the command. This way, you can connect two
6762306a36Sopenharmony_ci * 2-Mbit EEPROM devices on the same bus, but see one contiguous
6862306a36Sopenharmony_ci * memory from 0 to 7FFFFh, where address 0 to 3FFFF is in the device
6962306a36Sopenharmony_ci * whose pin 3 is connected to GND, and address 40000 to 7FFFFh is in
7062306a36Sopenharmony_ci * the 2nd device, whose pin 3 is connected to Vcc.
7162306a36Sopenharmony_ci *
7262306a36Sopenharmony_ci * This addressing you encode in the 32-bit "eeprom_addr" below,
7362306a36Sopenharmony_ci * namely the 19-bits "XYZ,A15:A0", as a single 19-bit address. For
7462306a36Sopenharmony_ci * instance, eeprom_addr = 0x6DA01, is 110_1101_1010_0000_0001, where
7562306a36Sopenharmony_ci * XYZ=110b, and A15:A0=DA01h. The XYZ bits become part of the device
7662306a36Sopenharmony_ci * address, and the rest of the address bits are sent as the memory
7762306a36Sopenharmony_ci * address bytes.
7862306a36Sopenharmony_ci *
7962306a36Sopenharmony_ci * That is, for an I2C EEPROM driver everything is controlled by
8062306a36Sopenharmony_ci * the "eeprom_addr".
8162306a36Sopenharmony_ci *
8262306a36Sopenharmony_ci * See also top of amdgpu_ras_eeprom.c.
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci * P.S. If you need to write, lock and read the Identification Page,
8562306a36Sopenharmony_ci * (M24M02-DR device only, which we do not use), change the "7" to
8662306a36Sopenharmony_ci * "0xF" in the macro below, and let the client set bit 20 to 1 in
8762306a36Sopenharmony_ci * "eeprom_addr", and set A10 to 0 to write into it, and A10 and A1 to
8862306a36Sopenharmony_ci * 1 to lock it permanently.
8962306a36Sopenharmony_ci */
9062306a36Sopenharmony_ci#define MAKE_I2C_ADDR(_aa) ((0xA << 3) | (((_aa) >> 16) & 0xF))
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_cistatic int __amdgpu_eeprom_xfer(struct i2c_adapter *i2c_adap, u32 eeprom_addr,
9362306a36Sopenharmony_ci				u8 *eeprom_buf, u16 buf_size, bool read)
9462306a36Sopenharmony_ci{
9562306a36Sopenharmony_ci	u8 eeprom_offset_buf[EEPROM_OFFSET_SIZE];
9662306a36Sopenharmony_ci	struct i2c_msg msgs[] = {
9762306a36Sopenharmony_ci		{
9862306a36Sopenharmony_ci			.flags = 0,
9962306a36Sopenharmony_ci			.len = EEPROM_OFFSET_SIZE,
10062306a36Sopenharmony_ci			.buf = eeprom_offset_buf,
10162306a36Sopenharmony_ci		},
10262306a36Sopenharmony_ci		{
10362306a36Sopenharmony_ci			.flags = read ? I2C_M_RD : 0,
10462306a36Sopenharmony_ci		},
10562306a36Sopenharmony_ci	};
10662306a36Sopenharmony_ci	const u8 *p = eeprom_buf;
10762306a36Sopenharmony_ci	int r;
10862306a36Sopenharmony_ci	u16 len;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	for (r = 0; buf_size > 0;
11162306a36Sopenharmony_ci	      buf_size -= len, eeprom_addr += len, eeprom_buf += len) {
11262306a36Sopenharmony_ci		/* Set the EEPROM address we want to write to/read from.
11362306a36Sopenharmony_ci		 */
11462306a36Sopenharmony_ci		msgs[0].addr = MAKE_I2C_ADDR(eeprom_addr);
11562306a36Sopenharmony_ci		msgs[1].addr = msgs[0].addr;
11662306a36Sopenharmony_ci		msgs[0].buf[0] = (eeprom_addr >> 8) & 0xff;
11762306a36Sopenharmony_ci		msgs[0].buf[1] = eeprom_addr & 0xff;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci		if (!read) {
12062306a36Sopenharmony_ci			/* Write the maximum amount of data, without
12162306a36Sopenharmony_ci			 * crossing the device's page boundary, as per
12262306a36Sopenharmony_ci			 * its spec. Partial page writes are allowed,
12362306a36Sopenharmony_ci			 * starting at any location within the page,
12462306a36Sopenharmony_ci			 * so long as the page boundary isn't crossed
12562306a36Sopenharmony_ci			 * over (actually the page pointer rolls
12662306a36Sopenharmony_ci			 * over).
12762306a36Sopenharmony_ci			 *
12862306a36Sopenharmony_ci			 * As per the AT24CM02 EEPROM spec, after
12962306a36Sopenharmony_ci			 * writing into a page, the I2C driver should
13062306a36Sopenharmony_ci			 * terminate the transfer, i.e. in
13162306a36Sopenharmony_ci			 * "i2c_transfer()" below, with a STOP
13262306a36Sopenharmony_ci			 * condition, so that the self-timed write
13362306a36Sopenharmony_ci			 * cycle begins. This is implied for the
13462306a36Sopenharmony_ci			 * "i2c_transfer()" abstraction.
13562306a36Sopenharmony_ci			 */
13662306a36Sopenharmony_ci			len = min(EEPROM_PAGE_SIZE - (eeprom_addr &
13762306a36Sopenharmony_ci						      EEPROM_PAGE_MASK),
13862306a36Sopenharmony_ci				  (u32)buf_size);
13962306a36Sopenharmony_ci		} else {
14062306a36Sopenharmony_ci			/* Reading from the EEPROM has no limitation
14162306a36Sopenharmony_ci			 * on the number of bytes read from the EEPROM
14262306a36Sopenharmony_ci			 * device--they are simply sequenced out.
14362306a36Sopenharmony_ci			 */
14462306a36Sopenharmony_ci			len = buf_size;
14562306a36Sopenharmony_ci		}
14662306a36Sopenharmony_ci		msgs[1].len = len;
14762306a36Sopenharmony_ci		msgs[1].buf = eeprom_buf;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci		/* This constitutes a START-STOP transaction.
15062306a36Sopenharmony_ci		 */
15162306a36Sopenharmony_ci		r = i2c_transfer(i2c_adap, msgs, ARRAY_SIZE(msgs));
15262306a36Sopenharmony_ci		if (r != ARRAY_SIZE(msgs))
15362306a36Sopenharmony_ci			break;
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci		if (!read) {
15662306a36Sopenharmony_ci			/* According to EEPROM specs the length of the
15762306a36Sopenharmony_ci			 * self-writing cycle, tWR (tW), is 10 ms.
15862306a36Sopenharmony_ci			 *
15962306a36Sopenharmony_ci			 * TODO: Use polling on ACK, aka Acknowledge
16062306a36Sopenharmony_ci			 * Polling, to minimize waiting for the
16162306a36Sopenharmony_ci			 * internal write cycle to complete, as it is
16262306a36Sopenharmony_ci			 * usually smaller than tWR (tW).
16362306a36Sopenharmony_ci			 */
16462306a36Sopenharmony_ci			msleep(10);
16562306a36Sopenharmony_ci		}
16662306a36Sopenharmony_ci	}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	return r < 0 ? r : eeprom_buf - p;
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/**
17262306a36Sopenharmony_ci * amdgpu_eeprom_xfer -- Read/write from/to an I2C EEPROM device
17362306a36Sopenharmony_ci * @i2c_adap: pointer to the I2C adapter to use
17462306a36Sopenharmony_ci * @eeprom_addr: EEPROM address from which to read/write
17562306a36Sopenharmony_ci * @eeprom_buf: pointer to data buffer to read into/write from
17662306a36Sopenharmony_ci * @buf_size: the size of @eeprom_buf
17762306a36Sopenharmony_ci * @read: True if reading from the EEPROM, false if writing
17862306a36Sopenharmony_ci *
17962306a36Sopenharmony_ci * Returns the number of bytes read/written; -errno on error.
18062306a36Sopenharmony_ci */
18162306a36Sopenharmony_cistatic int amdgpu_eeprom_xfer(struct i2c_adapter *i2c_adap, u32 eeprom_addr,
18262306a36Sopenharmony_ci			      u8 *eeprom_buf, u16 buf_size, bool read)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	const struct i2c_adapter_quirks *quirks = i2c_adap->quirks;
18562306a36Sopenharmony_ci	u16 limit;
18662306a36Sopenharmony_ci	u16 ps; /* Partial size */
18762306a36Sopenharmony_ci	int res = 0, r;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	if (!quirks)
19062306a36Sopenharmony_ci		limit = 0;
19162306a36Sopenharmony_ci	else if (read)
19262306a36Sopenharmony_ci		limit = quirks->max_read_len;
19362306a36Sopenharmony_ci	else
19462306a36Sopenharmony_ci		limit = quirks->max_write_len;
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	if (limit == 0) {
19762306a36Sopenharmony_ci		return __amdgpu_eeprom_xfer(i2c_adap, eeprom_addr,
19862306a36Sopenharmony_ci					    eeprom_buf, buf_size, read);
19962306a36Sopenharmony_ci	} else if (limit <= EEPROM_OFFSET_SIZE) {
20062306a36Sopenharmony_ci		dev_err_ratelimited(&i2c_adap->dev,
20162306a36Sopenharmony_ci				    "maddr:0x%04X size:0x%02X:quirk max_%s_len must be > %d",
20262306a36Sopenharmony_ci				    eeprom_addr, buf_size,
20362306a36Sopenharmony_ci				    read ? "read" : "write", EEPROM_OFFSET_SIZE);
20462306a36Sopenharmony_ci		return -EINVAL;
20562306a36Sopenharmony_ci	}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	/* The "limit" includes all data bytes sent/received,
20862306a36Sopenharmony_ci	 * which would include the EEPROM_OFFSET_SIZE bytes.
20962306a36Sopenharmony_ci	 * Account for them here.
21062306a36Sopenharmony_ci	 */
21162306a36Sopenharmony_ci	limit -= EEPROM_OFFSET_SIZE;
21262306a36Sopenharmony_ci	for ( ; buf_size > 0;
21362306a36Sopenharmony_ci	      buf_size -= ps, eeprom_addr += ps, eeprom_buf += ps) {
21462306a36Sopenharmony_ci		ps = min(limit, buf_size);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci		r = __amdgpu_eeprom_xfer(i2c_adap, eeprom_addr,
21762306a36Sopenharmony_ci					 eeprom_buf, ps, read);
21862306a36Sopenharmony_ci		if (r < 0)
21962306a36Sopenharmony_ci			return r;
22062306a36Sopenharmony_ci		res += r;
22162306a36Sopenharmony_ci	}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	return res;
22462306a36Sopenharmony_ci}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ciint amdgpu_eeprom_read(struct i2c_adapter *i2c_adap,
22762306a36Sopenharmony_ci		       u32 eeprom_addr, u8 *eeprom_buf,
22862306a36Sopenharmony_ci		       u16 bytes)
22962306a36Sopenharmony_ci{
23062306a36Sopenharmony_ci	return amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, eeprom_buf, bytes,
23162306a36Sopenharmony_ci				  true);
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciint amdgpu_eeprom_write(struct i2c_adapter *i2c_adap,
23562306a36Sopenharmony_ci			u32 eeprom_addr, u8 *eeprom_buf,
23662306a36Sopenharmony_ci			u16 bytes)
23762306a36Sopenharmony_ci{
23862306a36Sopenharmony_ci	return amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, eeprom_buf, bytes,
23962306a36Sopenharmony_ci				  false);
24062306a36Sopenharmony_ci}
241