15ca02815Sjsg /* 25ca02815Sjsg * Copyright 2021 Advanced Micro Devices, Inc. 35ca02815Sjsg * 45ca02815Sjsg * Permission is hereby granted, free of charge, to any person obtaining a 55ca02815Sjsg * copy of this software and associated documentation files (the "Software"), 65ca02815Sjsg * to deal in the Software without restriction, including without limitation 75ca02815Sjsg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 85ca02815Sjsg * and/or sell copies of the Software, and to permit persons to whom the 95ca02815Sjsg * Software is furnished to do so, subject to the following conditions: 105ca02815Sjsg * 115ca02815Sjsg * The above copyright notice and this permission notice shall be included in 125ca02815Sjsg * all copies or substantial portions of the Software. 135ca02815Sjsg * 145ca02815Sjsg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 155ca02815Sjsg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 165ca02815Sjsg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 175ca02815Sjsg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 185ca02815Sjsg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 195ca02815Sjsg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 205ca02815Sjsg * OTHER DEALINGS IN THE SOFTWARE. 215ca02815Sjsg * 225ca02815Sjsg */ 235ca02815Sjsg 245ca02815Sjsg #include "amdgpu_eeprom.h" 255ca02815Sjsg #include "amdgpu.h" 265ca02815Sjsg 275ca02815Sjsg /* AT24CM02 and M24M02-R have a 256-byte write page size. 285ca02815Sjsg */ 295ca02815Sjsg #define EEPROM_PAGE_BITS 8 305ca02815Sjsg #define EEPROM_PAGE_SIZE (1U << EEPROM_PAGE_BITS) 315ca02815Sjsg #define EEPROM_PAGE_MASK (EEPROM_PAGE_SIZE - 1) 325ca02815Sjsg 335ca02815Sjsg #define EEPROM_OFFSET_SIZE 2 345ca02815Sjsg 355ca02815Sjsg /* EEPROM memory addresses are 19-bits long, which can 365ca02815Sjsg * be partitioned into 3, 8, 8 bits, for a total of 19. 375ca02815Sjsg * The upper 3 bits are sent as part of the 7-bit 385ca02815Sjsg * "Device Type Identifier"--an I2C concept, which for EEPROM devices 395ca02815Sjsg * is hard-coded as 1010b, indicating that it is an EEPROM 405ca02815Sjsg * device--this is the wire format, followed by the upper 415ca02815Sjsg * 3 bits of the 19-bit address, followed by the direction, 425ca02815Sjsg * followed by two bytes holding the rest of the 16-bits of 435ca02815Sjsg * the EEPROM memory address. The format on the wire for EEPROM 445ca02815Sjsg * devices is: 1010XYZD, A15:A8, A7:A0, 455ca02815Sjsg * Where D is the direction and sequenced out by the hardware. 465ca02815Sjsg * Bits XYZ are memory address bits 18, 17 and 16. 475ca02815Sjsg * These bits are compared to how pins 1-3 of the part are connected, 485ca02815Sjsg * depending on the size of the part, more on that later. 495ca02815Sjsg * 505ca02815Sjsg * Note that of this wire format, a client is in control 515ca02815Sjsg * of, and needs to specify only XYZ, A15:A8, A7:0, bits, 525ca02815Sjsg * which is exactly the EEPROM memory address, or offset, 535ca02815Sjsg * in order to address up to 8 EEPROM devices on the I2C bus. 545ca02815Sjsg * 555ca02815Sjsg * For instance, a 2-Mbit I2C EEPROM part, addresses all its bytes, 565ca02815Sjsg * using an 18-bit address, bit 17 to 0 and thus would use all but one bit of 575ca02815Sjsg * the 19 bits previously mentioned. The designer would then not connect 585ca02815Sjsg * pins 1 and 2, and pin 3 usually named "A_2" or "E2", would be connected to 595ca02815Sjsg * either Vcc or GND. This would allow for up to two 2-Mbit parts on 605ca02815Sjsg * the same bus, where one would be addressable with bit 18 as 1, and 615ca02815Sjsg * the other with bit 18 of the address as 0. 625ca02815Sjsg * 635ca02815Sjsg * For a 2-Mbit part, bit 18 is usually known as the "Chip Enable" or 645ca02815Sjsg * "Hardware Address Bit". This bit is compared to the load on pin 3 655ca02815Sjsg * of the device, described above, and if there is a match, then this 665ca02815Sjsg * device responds to the command. This way, you can connect two 675ca02815Sjsg * 2-Mbit EEPROM devices on the same bus, but see one contiguous 685ca02815Sjsg * memory from 0 to 7FFFFh, where address 0 to 3FFFF is in the device 695ca02815Sjsg * whose pin 3 is connected to GND, and address 40000 to 7FFFFh is in 705ca02815Sjsg * the 2nd device, whose pin 3 is connected to Vcc. 715ca02815Sjsg * 725ca02815Sjsg * This addressing you encode in the 32-bit "eeprom_addr" below, 735ca02815Sjsg * namely the 19-bits "XYZ,A15:A0", as a single 19-bit address. For 745ca02815Sjsg * instance, eeprom_addr = 0x6DA01, is 110_1101_1010_0000_0001, where 755ca02815Sjsg * XYZ=110b, and A15:A0=DA01h. The XYZ bits become part of the device 765ca02815Sjsg * address, and the rest of the address bits are sent as the memory 775ca02815Sjsg * address bytes. 785ca02815Sjsg * 795ca02815Sjsg * That is, for an I2C EEPROM driver everything is controlled by 805ca02815Sjsg * the "eeprom_addr". 815ca02815Sjsg * 8202860839Sjsg * See also top of amdgpu_ras_eeprom.c. 8302860839Sjsg * 845ca02815Sjsg * P.S. If you need to write, lock and read the Identification Page, 855ca02815Sjsg * (M24M02-DR device only, which we do not use), change the "7" to 865ca02815Sjsg * "0xF" in the macro below, and let the client set bit 20 to 1 in 875ca02815Sjsg * "eeprom_addr", and set A10 to 0 to write into it, and A10 and A1 to 885ca02815Sjsg * 1 to lock it permanently. 895ca02815Sjsg */ 90f005ef32Sjsg #define MAKE_I2C_ADDR(_aa) ((0xA << 3) | (((_aa) >> 16) & 0xF)) 915ca02815Sjsg 925ca02815Sjsg static int __amdgpu_eeprom_xfer(struct i2c_adapter *i2c_adap, u32 eeprom_addr, 935ca02815Sjsg u8 *eeprom_buf, u16 buf_size, bool read) 945ca02815Sjsg { 955ca02815Sjsg u8 eeprom_offset_buf[EEPROM_OFFSET_SIZE]; 965ca02815Sjsg struct i2c_msg msgs[] = { 975ca02815Sjsg { 985ca02815Sjsg .flags = 0, 995ca02815Sjsg .len = EEPROM_OFFSET_SIZE, 1005ca02815Sjsg .buf = eeprom_offset_buf, 1015ca02815Sjsg }, 1025ca02815Sjsg { 1035ca02815Sjsg .flags = read ? I2C_M_RD : 0, 1045ca02815Sjsg }, 1055ca02815Sjsg }; 1065ca02815Sjsg const u8 *p = eeprom_buf; 1075ca02815Sjsg int r; 1085ca02815Sjsg u16 len; 1095ca02815Sjsg 1105ca02815Sjsg for (r = 0; buf_size > 0; 1115ca02815Sjsg buf_size -= len, eeprom_addr += len, eeprom_buf += len) { 1125ca02815Sjsg /* Set the EEPROM address we want to write to/read from. 1135ca02815Sjsg */ 1145ca02815Sjsg msgs[0].addr = MAKE_I2C_ADDR(eeprom_addr); 1155ca02815Sjsg msgs[1].addr = msgs[0].addr; 1165ca02815Sjsg msgs[0].buf[0] = (eeprom_addr >> 8) & 0xff; 1175ca02815Sjsg msgs[0].buf[1] = eeprom_addr & 0xff; 1185ca02815Sjsg 1195ca02815Sjsg if (!read) { 1205ca02815Sjsg /* Write the maximum amount of data, without 1215ca02815Sjsg * crossing the device's page boundary, as per 1225ca02815Sjsg * its spec. Partial page writes are allowed, 1235ca02815Sjsg * starting at any location within the page, 1245ca02815Sjsg * so long as the page boundary isn't crossed 1255ca02815Sjsg * over (actually the page pointer rolls 1265ca02815Sjsg * over). 1275ca02815Sjsg * 1285ca02815Sjsg * As per the AT24CM02 EEPROM spec, after 1295ca02815Sjsg * writing into a page, the I2C driver should 1305ca02815Sjsg * terminate the transfer, i.e. in 1315ca02815Sjsg * "i2c_transfer()" below, with a STOP 1325ca02815Sjsg * condition, so that the self-timed write 1335ca02815Sjsg * cycle begins. This is implied for the 1345ca02815Sjsg * "i2c_transfer()" abstraction. 1355ca02815Sjsg */ 1365ca02815Sjsg len = min(EEPROM_PAGE_SIZE - (eeprom_addr & 1375ca02815Sjsg EEPROM_PAGE_MASK), 1385ca02815Sjsg (u32)buf_size); 1395ca02815Sjsg } else { 1405ca02815Sjsg /* Reading from the EEPROM has no limitation 1415ca02815Sjsg * on the number of bytes read from the EEPROM 1425ca02815Sjsg * device--they are simply sequenced out. 1435ca02815Sjsg */ 1445ca02815Sjsg len = buf_size; 1455ca02815Sjsg } 1465ca02815Sjsg msgs[1].len = len; 1475ca02815Sjsg msgs[1].buf = eeprom_buf; 1485ca02815Sjsg 1495ca02815Sjsg /* This constitutes a START-STOP transaction. 1505ca02815Sjsg */ 1515ca02815Sjsg r = i2c_transfer(i2c_adap, msgs, ARRAY_SIZE(msgs)); 1525ca02815Sjsg if (r != ARRAY_SIZE(msgs)) 1535ca02815Sjsg break; 1545ca02815Sjsg 1555ca02815Sjsg if (!read) { 1565ca02815Sjsg /* According to EEPROM specs the length of the 1575ca02815Sjsg * self-writing cycle, tWR (tW), is 10 ms. 1585ca02815Sjsg * 1595ca02815Sjsg * TODO: Use polling on ACK, aka Acknowledge 1605ca02815Sjsg * Polling, to minimize waiting for the 1615ca02815Sjsg * internal write cycle to complete, as it is 1625ca02815Sjsg * usually smaller than tWR (tW). 1635ca02815Sjsg */ 1645ca02815Sjsg drm_msleep(10); 1655ca02815Sjsg } 1665ca02815Sjsg } 1675ca02815Sjsg 1685ca02815Sjsg return r < 0 ? r : eeprom_buf - p; 1695ca02815Sjsg } 1705ca02815Sjsg 1715ca02815Sjsg /** 1725ca02815Sjsg * amdgpu_eeprom_xfer -- Read/write from/to an I2C EEPROM device 1735ca02815Sjsg * @i2c_adap: pointer to the I2C adapter to use 1745ca02815Sjsg * @eeprom_addr: EEPROM address from which to read/write 1755ca02815Sjsg * @eeprom_buf: pointer to data buffer to read into/write from 1765ca02815Sjsg * @buf_size: the size of @eeprom_buf 1775ca02815Sjsg * @read: True if reading from the EEPROM, false if writing 1785ca02815Sjsg * 1795ca02815Sjsg * Returns the number of bytes read/written; -errno on error. 1805ca02815Sjsg */ 1815ca02815Sjsg static int amdgpu_eeprom_xfer(struct i2c_adapter *i2c_adap, u32 eeprom_addr, 182*e2686fb7Sjsg u8 *eeprom_buf, u32 buf_size, bool read) 1835ca02815Sjsg { 1845ca02815Sjsg const struct i2c_adapter_quirks *quirks = i2c_adap->quirks; 1855ca02815Sjsg u16 limit; 186f005ef32Sjsg u16 ps; /* Partial size */ 187f005ef32Sjsg int res = 0, r; 1885ca02815Sjsg 1895ca02815Sjsg if (!quirks) 1905ca02815Sjsg limit = 0; 1915ca02815Sjsg else if (read) 1925ca02815Sjsg limit = quirks->max_read_len; 1935ca02815Sjsg else 1945ca02815Sjsg limit = quirks->max_write_len; 1955ca02815Sjsg 1965ca02815Sjsg if (limit == 0) { 1975ca02815Sjsg return __amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, 1985ca02815Sjsg eeprom_buf, buf_size, read); 1995ca02815Sjsg } else if (limit <= EEPROM_OFFSET_SIZE) { 2005ca02815Sjsg dev_err_ratelimited(&i2c_adap->dev, 2015ca02815Sjsg "maddr:0x%04X size:0x%02X:quirk max_%s_len must be > %d", 2025ca02815Sjsg eeprom_addr, buf_size, 2035ca02815Sjsg read ? "read" : "write", EEPROM_OFFSET_SIZE); 2045ca02815Sjsg return -EINVAL; 205f005ef32Sjsg } 2065ca02815Sjsg 2075ca02815Sjsg /* The "limit" includes all data bytes sent/received, 2085ca02815Sjsg * which would include the EEPROM_OFFSET_SIZE bytes. 2095ca02815Sjsg * Account for them here. 2105ca02815Sjsg */ 2115ca02815Sjsg limit -= EEPROM_OFFSET_SIZE; 2125ca02815Sjsg for ( ; buf_size > 0; 2135ca02815Sjsg buf_size -= ps, eeprom_addr += ps, eeprom_buf += ps) { 2145ca02815Sjsg ps = min(limit, buf_size); 2155ca02815Sjsg 2165ca02815Sjsg r = __amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, 2175ca02815Sjsg eeprom_buf, ps, read); 2185ca02815Sjsg if (r < 0) 2195ca02815Sjsg return r; 2205ca02815Sjsg res += r; 2215ca02815Sjsg } 2225ca02815Sjsg 2235ca02815Sjsg return res; 2245ca02815Sjsg } 2255ca02815Sjsg 2265ca02815Sjsg int amdgpu_eeprom_read(struct i2c_adapter *i2c_adap, 2275ca02815Sjsg u32 eeprom_addr, u8 *eeprom_buf, 228*e2686fb7Sjsg u32 bytes) 2295ca02815Sjsg { 2305ca02815Sjsg return amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, eeprom_buf, bytes, 2315ca02815Sjsg true); 2325ca02815Sjsg } 2335ca02815Sjsg 2345ca02815Sjsg int amdgpu_eeprom_write(struct i2c_adapter *i2c_adap, 2355ca02815Sjsg u32 eeprom_addr, u8 *eeprom_buf, 236*e2686fb7Sjsg u32 bytes) 2375ca02815Sjsg { 2385ca02815Sjsg return amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, eeprom_buf, bytes, 2395ca02815Sjsg false); 2405ca02815Sjsg } 241