100e3a289SAlex Deucher /*
200e3a289SAlex Deucher * Copyright 2021 Advanced Micro Devices, Inc.
300e3a289SAlex Deucher *
400e3a289SAlex Deucher * Permission is hereby granted, free of charge, to any person obtaining a
500e3a289SAlex Deucher * copy of this software and associated documentation files (the "Software"),
600e3a289SAlex Deucher * to deal in the Software without restriction, including without limitation
700e3a289SAlex Deucher * the rights to use, copy, modify, merge, publish, distribute, sublicense,
800e3a289SAlex Deucher * and/or sell copies of the Software, and to permit persons to whom the
900e3a289SAlex Deucher * Software is furnished to do so, subject to the following conditions:
1000e3a289SAlex Deucher *
1100e3a289SAlex Deucher * The above copyright notice and this permission notice shall be included in
1200e3a289SAlex Deucher * all copies or substantial portions of the Software.
1300e3a289SAlex Deucher *
1400e3a289SAlex Deucher * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1500e3a289SAlex Deucher * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1600e3a289SAlex Deucher * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1700e3a289SAlex Deucher * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1800e3a289SAlex Deucher * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1900e3a289SAlex Deucher * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2000e3a289SAlex Deucher * OTHER DEALINGS IN THE SOFTWARE.
2100e3a289SAlex Deucher *
2200e3a289SAlex Deucher */
2300e3a289SAlex Deucher
2400e3a289SAlex Deucher #include "amdgpu_eeprom.h"
2500e3a289SAlex Deucher #include "amdgpu.h"
2600e3a289SAlex Deucher
27025a64a5SLuben Tuikov /* AT24CM02 and M24M02-R have a 256-byte write page size.
28746b5847SLuben Tuikov */
29746b5847SLuben Tuikov #define EEPROM_PAGE_BITS 8
30746b5847SLuben Tuikov #define EEPROM_PAGE_SIZE (1U << EEPROM_PAGE_BITS)
31746b5847SLuben Tuikov #define EEPROM_PAGE_MASK (EEPROM_PAGE_SIZE - 1)
3200e3a289SAlex Deucher
33746b5847SLuben Tuikov #define EEPROM_OFFSET_SIZE 2
34746b5847SLuben Tuikov
35025a64a5SLuben Tuikov /* EEPROM memory addresses are 19-bits long, which can
36025a64a5SLuben Tuikov * be partitioned into 3, 8, 8 bits, for a total of 19.
37025a64a5SLuben Tuikov * The upper 3 bits are sent as part of the 7-bit
38025a64a5SLuben Tuikov * "Device Type Identifier"--an I2C concept, which for EEPROM devices
39025a64a5SLuben Tuikov * is hard-coded as 1010b, indicating that it is an EEPROM
40025a64a5SLuben Tuikov * device--this is the wire format, followed by the upper
41025a64a5SLuben Tuikov * 3 bits of the 19-bit address, followed by the direction,
42025a64a5SLuben Tuikov * followed by two bytes holding the rest of the 16-bits of
43025a64a5SLuben Tuikov * the EEPROM memory address. The format on the wire for EEPROM
44025a64a5SLuben Tuikov * devices is: 1010XYZD, A15:A8, A7:A0,
45025a64a5SLuben Tuikov * Where D is the direction and sequenced out by the hardware.
46025a64a5SLuben Tuikov * Bits XYZ are memory address bits 18, 17 and 16.
47025a64a5SLuben Tuikov * These bits are compared to how pins 1-3 of the part are connected,
48025a64a5SLuben Tuikov * depending on the size of the part, more on that later.
49025a64a5SLuben Tuikov *
50025a64a5SLuben Tuikov * Note that of this wire format, a client is in control
51025a64a5SLuben Tuikov * of, and needs to specify only XYZ, A15:A8, A7:0, bits,
52025a64a5SLuben Tuikov * which is exactly the EEPROM memory address, or offset,
53025a64a5SLuben Tuikov * in order to address up to 8 EEPROM devices on the I2C bus.
54025a64a5SLuben Tuikov *
55025a64a5SLuben Tuikov * For instance, a 2-Mbit I2C EEPROM part, addresses all its bytes,
56025a64a5SLuben Tuikov * using an 18-bit address, bit 17 to 0 and thus would use all but one bit of
57025a64a5SLuben Tuikov * the 19 bits previously mentioned. The designer would then not connect
58025a64a5SLuben Tuikov * pins 1 and 2, and pin 3 usually named "A_2" or "E2", would be connected to
59025a64a5SLuben Tuikov * either Vcc or GND. This would allow for up to two 2-Mbit parts on
60025a64a5SLuben Tuikov * the same bus, where one would be addressable with bit 18 as 1, and
61025a64a5SLuben Tuikov * the other with bit 18 of the address as 0.
62025a64a5SLuben Tuikov *
63025a64a5SLuben Tuikov * For a 2-Mbit part, bit 18 is usually known as the "Chip Enable" or
64025a64a5SLuben Tuikov * "Hardware Address Bit". This bit is compared to the load on pin 3
65025a64a5SLuben Tuikov * of the device, described above, and if there is a match, then this
66025a64a5SLuben Tuikov * device responds to the command. This way, you can connect two
67025a64a5SLuben Tuikov * 2-Mbit EEPROM devices on the same bus, but see one contiguous
68025a64a5SLuben Tuikov * memory from 0 to 7FFFFh, where address 0 to 3FFFF is in the device
69025a64a5SLuben Tuikov * whose pin 3 is connected to GND, and address 40000 to 7FFFFh is in
70025a64a5SLuben Tuikov * the 2nd device, whose pin 3 is connected to Vcc.
71025a64a5SLuben Tuikov *
72025a64a5SLuben Tuikov * This addressing you encode in the 32-bit "eeprom_addr" below,
73025a64a5SLuben Tuikov * namely the 19-bits "XYZ,A15:A0", as a single 19-bit address. For
74025a64a5SLuben Tuikov * instance, eeprom_addr = 0x6DA01, is 110_1101_1010_0000_0001, where
75025a64a5SLuben Tuikov * XYZ=110b, and A15:A0=DA01h. The XYZ bits become part of the device
76025a64a5SLuben Tuikov * address, and the rest of the address bits are sent as the memory
77025a64a5SLuben Tuikov * address bytes.
78025a64a5SLuben Tuikov *
79025a64a5SLuben Tuikov * That is, for an I2C EEPROM driver everything is controlled by
80025a64a5SLuben Tuikov * the "eeprom_addr".
81025a64a5SLuben Tuikov *
82da858deaSLuben Tuikov * See also top of amdgpu_ras_eeprom.c.
83da858deaSLuben Tuikov *
84025a64a5SLuben Tuikov * P.S. If you need to write, lock and read the Identification Page,
85025a64a5SLuben Tuikov * (M24M02-DR device only, which we do not use), change the "7" to
86025a64a5SLuben Tuikov * "0xF" in the macro below, and let the client set bit 20 to 1 in
87025a64a5SLuben Tuikov * "eeprom_addr", and set A10 to 0 to write into it, and A10 and A1 to
88025a64a5SLuben Tuikov * 1 to lock it permanently.
89025a64a5SLuben Tuikov */
906a4a745cSLuben Tuikov #define MAKE_I2C_ADDR(_aa) ((0xA << 3) | (((_aa) >> 16) & 0xF))
91025a64a5SLuben Tuikov
__amdgpu_eeprom_xfer(struct i2c_adapter * i2c_adap,u32 eeprom_addr,u8 * eeprom_buf,u32 buf_size,bool read)92025a64a5SLuben Tuikov static int __amdgpu_eeprom_xfer(struct i2c_adapter *i2c_adap, u32 eeprom_addr,
9376bec2a0STao Zhou u8 *eeprom_buf, u32 buf_size, bool read)
9400e3a289SAlex Deucher {
95746b5847SLuben Tuikov u8 eeprom_offset_buf[EEPROM_OFFSET_SIZE];
9600e3a289SAlex Deucher struct i2c_msg msgs[] = {
9700e3a289SAlex Deucher {
9800e3a289SAlex Deucher .flags = 0,
99746b5847SLuben Tuikov .len = EEPROM_OFFSET_SIZE,
10000e3a289SAlex Deucher .buf = eeprom_offset_buf,
10100e3a289SAlex Deucher },
10200e3a289SAlex Deucher {
10300e3a289SAlex Deucher .flags = read ? I2C_M_RD : 0,
104daaa75fdSLuben Tuikov },
10500e3a289SAlex Deucher };
106746b5847SLuben Tuikov const u8 *p = eeprom_buf;
10700e3a289SAlex Deucher int r;
108746b5847SLuben Tuikov u16 len;
10900e3a289SAlex Deucher
110cf696091SLuben Tuikov for (r = 0; buf_size > 0;
111746b5847SLuben Tuikov buf_size -= len, eeprom_addr += len, eeprom_buf += len) {
112746b5847SLuben Tuikov /* Set the EEPROM address we want to write to/read from.
113746b5847SLuben Tuikov */
114025a64a5SLuben Tuikov msgs[0].addr = MAKE_I2C_ADDR(eeprom_addr);
115025a64a5SLuben Tuikov msgs[1].addr = msgs[0].addr;
116746b5847SLuben Tuikov msgs[0].buf[0] = (eeprom_addr >> 8) & 0xff;
117746b5847SLuben Tuikov msgs[0].buf[1] = eeprom_addr & 0xff;
11800e3a289SAlex Deucher
119746b5847SLuben Tuikov if (!read) {
120746b5847SLuben Tuikov /* Write the maximum amount of data, without
121746b5847SLuben Tuikov * crossing the device's page boundary, as per
122746b5847SLuben Tuikov * its spec. Partial page writes are allowed,
123746b5847SLuben Tuikov * starting at any location within the page,
124746b5847SLuben Tuikov * so long as the page boundary isn't crossed
125746b5847SLuben Tuikov * over (actually the page pointer rolls
126746b5847SLuben Tuikov * over).
127746b5847SLuben Tuikov *
128746b5847SLuben Tuikov * As per the AT24CM02 EEPROM spec, after
129025a64a5SLuben Tuikov * writing into a page, the I2C driver should
130746b5847SLuben Tuikov * terminate the transfer, i.e. in
131746b5847SLuben Tuikov * "i2c_transfer()" below, with a STOP
132746b5847SLuben Tuikov * condition, so that the self-timed write
133746b5847SLuben Tuikov * cycle begins. This is implied for the
134746b5847SLuben Tuikov * "i2c_transfer()" abstraction.
135746b5847SLuben Tuikov */
13676bec2a0STao Zhou len = min(EEPROM_PAGE_SIZE - (eeprom_addr & EEPROM_PAGE_MASK),
13776bec2a0STao Zhou buf_size);
138746b5847SLuben Tuikov } else {
139746b5847SLuben Tuikov /* Reading from the EEPROM has no limitation
140746b5847SLuben Tuikov * on the number of bytes read from the EEPROM
141746b5847SLuben Tuikov * device--they are simply sequenced out.
14276bec2a0STao Zhou * Keep in mind that i2c_msg.len is u16 type.
143746b5847SLuben Tuikov */
14476bec2a0STao Zhou len = min(U16_MAX, buf_size);
145746b5847SLuben Tuikov }
146746b5847SLuben Tuikov msgs[1].len = len;
147746b5847SLuben Tuikov msgs[1].buf = eeprom_buf;
148746b5847SLuben Tuikov
149025a64a5SLuben Tuikov /* This constitutes a START-STOP transaction.
150025a64a5SLuben Tuikov */
15100e3a289SAlex Deucher r = i2c_transfer(i2c_adap, msgs, ARRAY_SIZE(msgs));
1521d864f10SDan Carpenter if (r != ARRAY_SIZE(msgs))
153746b5847SLuben Tuikov break;
1542485f8cfSAndrey Grodzovsky
155746b5847SLuben Tuikov if (!read) {
156025a64a5SLuben Tuikov /* According to EEPROM specs the length of the
157025a64a5SLuben Tuikov * self-writing cycle, tWR (tW), is 10 ms.
1582485f8cfSAndrey Grodzovsky *
159025a64a5SLuben Tuikov * TODO: Use polling on ACK, aka Acknowledge
160025a64a5SLuben Tuikov * Polling, to minimize waiting for the
161025a64a5SLuben Tuikov * internal write cycle to complete, as it is
162025a64a5SLuben Tuikov * usually smaller than tWR (tW).
1632485f8cfSAndrey Grodzovsky */
1642485f8cfSAndrey Grodzovsky msleep(10);
165746b5847SLuben Tuikov }
16600e3a289SAlex Deucher }
16700e3a289SAlex Deucher
168746b5847SLuben Tuikov return r < 0 ? r : eeprom_buf - p;
16900e3a289SAlex Deucher }
17093ade343SLuben Tuikov
17193ade343SLuben Tuikov /**
17293ade343SLuben Tuikov * amdgpu_eeprom_xfer -- Read/write from/to an I2C EEPROM device
17393ade343SLuben Tuikov * @i2c_adap: pointer to the I2C adapter to use
17493ade343SLuben Tuikov * @eeprom_addr: EEPROM address from which to read/write
17593ade343SLuben Tuikov * @eeprom_buf: pointer to data buffer to read into/write from
17693ade343SLuben Tuikov * @buf_size: the size of @eeprom_buf
17793ade343SLuben Tuikov * @read: True if reading from the EEPROM, false if writing
17893ade343SLuben Tuikov *
17993ade343SLuben Tuikov * Returns the number of bytes read/written; -errno on error.
18093ade343SLuben Tuikov */
amdgpu_eeprom_xfer(struct i2c_adapter * i2c_adap,u32 eeprom_addr,u8 * eeprom_buf,u32 buf_size,bool read)18163d4c081SLuben Tuikov static int amdgpu_eeprom_xfer(struct i2c_adapter *i2c_adap, u32 eeprom_addr,
1822aadb520STao Zhou u8 *eeprom_buf, u32 buf_size, bool read)
18393ade343SLuben Tuikov {
18493ade343SLuben Tuikov const struct i2c_adapter_quirks *quirks = i2c_adap->quirks;
18593ade343SLuben Tuikov u16 limit;
1866cf20211SSrinivasan Shanmugam u16 ps; /* Partial size */
1876cf20211SSrinivasan Shanmugam int res = 0, r;
18893ade343SLuben Tuikov
18993ade343SLuben Tuikov if (!quirks)
19093ade343SLuben Tuikov limit = 0;
19193ade343SLuben Tuikov else if (read)
19293ade343SLuben Tuikov limit = quirks->max_read_len;
19393ade343SLuben Tuikov else
19493ade343SLuben Tuikov limit = quirks->max_write_len;
19593ade343SLuben Tuikov
19693ade343SLuben Tuikov if (limit == 0) {
197025a64a5SLuben Tuikov return __amdgpu_eeprom_xfer(i2c_adap, eeprom_addr,
19893ade343SLuben Tuikov eeprom_buf, buf_size, read);
19993ade343SLuben Tuikov } else if (limit <= EEPROM_OFFSET_SIZE) {
20093ade343SLuben Tuikov dev_err_ratelimited(&i2c_adap->dev,
20193ade343SLuben Tuikov "maddr:0x%04X size:0x%02X:quirk max_%s_len must be > %d",
20293ade343SLuben Tuikov eeprom_addr, buf_size,
203*b95264cfSR Sundar str_read_write(read), EEPROM_OFFSET_SIZE);
20493ade343SLuben Tuikov return -EINVAL;
2056cf20211SSrinivasan Shanmugam }
20693ade343SLuben Tuikov
20793ade343SLuben Tuikov /* The "limit" includes all data bytes sent/received,
20893ade343SLuben Tuikov * which would include the EEPROM_OFFSET_SIZE bytes.
20993ade343SLuben Tuikov * Account for them here.
21093ade343SLuben Tuikov */
21193ade343SLuben Tuikov limit -= EEPROM_OFFSET_SIZE;
21293ade343SLuben Tuikov for ( ; buf_size > 0;
21393ade343SLuben Tuikov buf_size -= ps, eeprom_addr += ps, eeprom_buf += ps) {
21493ade343SLuben Tuikov ps = min(limit, buf_size);
21593ade343SLuben Tuikov
216025a64a5SLuben Tuikov r = __amdgpu_eeprom_xfer(i2c_adap, eeprom_addr,
21793ade343SLuben Tuikov eeprom_buf, ps, read);
21893ade343SLuben Tuikov if (r < 0)
21993ade343SLuben Tuikov return r;
22093ade343SLuben Tuikov res += r;
22193ade343SLuben Tuikov }
22293ade343SLuben Tuikov
22393ade343SLuben Tuikov return res;
22493ade343SLuben Tuikov }
22563d4c081SLuben Tuikov
amdgpu_eeprom_read(struct i2c_adapter * i2c_adap,u32 eeprom_addr,u8 * eeprom_buf,u32 bytes)22663d4c081SLuben Tuikov int amdgpu_eeprom_read(struct i2c_adapter *i2c_adap,
22763d4c081SLuben Tuikov u32 eeprom_addr, u8 *eeprom_buf,
2282aadb520STao Zhou u32 bytes)
22963d4c081SLuben Tuikov {
23063d4c081SLuben Tuikov return amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, eeprom_buf, bytes,
23163d4c081SLuben Tuikov true);
23263d4c081SLuben Tuikov }
23363d4c081SLuben Tuikov
amdgpu_eeprom_write(struct i2c_adapter * i2c_adap,u32 eeprom_addr,u8 * eeprom_buf,u32 bytes)23463d4c081SLuben Tuikov int amdgpu_eeprom_write(struct i2c_adapter *i2c_adap,
23563d4c081SLuben Tuikov u32 eeprom_addr, u8 *eeprom_buf,
2362aadb520STao Zhou u32 bytes)
23763d4c081SLuben Tuikov {
23863d4c081SLuben Tuikov return amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, eeprom_buf, bytes,
23963d4c081SLuben Tuikov false);
24063d4c081SLuben Tuikov }
241