![]() |
|
|||
0001 /* 0002 * Copyright 2021 Advanced Micro Devices, Inc. 0003 * 0004 * Permission is hereby granted, free of charge, to any person obtaining a 0005 * copy of this software and associated documentation files (the "Software"), 0006 * to deal in the Software without restriction, including without limitation 0007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 0008 * and/or sell copies of the Software, and to permit persons to whom the 0009 * Software is furnished to do so, subject to the following conditions: 0010 * 0011 * The above copyright notice and this permission notice shall be included in 0012 * all copies or substantial portions of the Software. 0013 * 0014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 0017 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 0018 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 0019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 0020 * OTHER DEALINGS IN THE SOFTWARE. 0021 * 0022 */ 0023 0024 #include "amdgpu_eeprom.h" 0025 #include "amdgpu.h" 0026 0027 /* AT24CM02 and M24M02-R have a 256-byte write page size. 0028 */ 0029 #define EEPROM_PAGE_BITS 8 0030 #define EEPROM_PAGE_SIZE (1U << EEPROM_PAGE_BITS) 0031 #define EEPROM_PAGE_MASK (EEPROM_PAGE_SIZE - 1) 0032 0033 #define EEPROM_OFFSET_SIZE 2 0034 0035 /* EEPROM memory addresses are 19-bits long, which can 0036 * be partitioned into 3, 8, 8 bits, for a total of 19. 0037 * The upper 3 bits are sent as part of the 7-bit 0038 * "Device Type Identifier"--an I2C concept, which for EEPROM devices 0039 * is hard-coded as 1010b, indicating that it is an EEPROM 0040 * device--this is the wire format, followed by the upper 0041 * 3 bits of the 19-bit address, followed by the direction, 0042 * followed by two bytes holding the rest of the 16-bits of 0043 * the EEPROM memory address. The format on the wire for EEPROM 0044 * devices is: 1010XYZD, A15:A8, A7:A0, 0045 * Where D is the direction and sequenced out by the hardware. 0046 * Bits XYZ are memory address bits 18, 17 and 16. 0047 * These bits are compared to how pins 1-3 of the part are connected, 0048 * depending on the size of the part, more on that later. 0049 * 0050 * Note that of this wire format, a client is in control 0051 * of, and needs to specify only XYZ, A15:A8, A7:0, bits, 0052 * which is exactly the EEPROM memory address, or offset, 0053 * in order to address up to 8 EEPROM devices on the I2C bus. 0054 * 0055 * For instance, a 2-Mbit I2C EEPROM part, addresses all its bytes, 0056 * using an 18-bit address, bit 17 to 0 and thus would use all but one bit of 0057 * the 19 bits previously mentioned. The designer would then not connect 0058 * pins 1 and 2, and pin 3 usually named "A_2" or "E2", would be connected to 0059 * either Vcc or GND. This would allow for up to two 2-Mbit parts on 0060 * the same bus, where one would be addressable with bit 18 as 1, and 0061 * the other with bit 18 of the address as 0. 0062 * 0063 * For a 2-Mbit part, bit 18 is usually known as the "Chip Enable" or 0064 * "Hardware Address Bit". This bit is compared to the load on pin 3 0065 * of the device, described above, and if there is a match, then this 0066 * device responds to the command. This way, you can connect two 0067 * 2-Mbit EEPROM devices on the same bus, but see one contiguous 0068 * memory from 0 to 7FFFFh, where address 0 to 3FFFF is in the device 0069 * whose pin 3 is connected to GND, and address 40000 to 7FFFFh is in 0070 * the 2nd device, whose pin 3 is connected to Vcc. 0071 * 0072 * This addressing you encode in the 32-bit "eeprom_addr" below, 0073 * namely the 19-bits "XYZ,A15:A0", as a single 19-bit address. For 0074 * instance, eeprom_addr = 0x6DA01, is 110_1101_1010_0000_0001, where 0075 * XYZ=110b, and A15:A0=DA01h. The XYZ bits become part of the device 0076 * address, and the rest of the address bits are sent as the memory 0077 * address bytes. 0078 * 0079 * That is, for an I2C EEPROM driver everything is controlled by 0080 * the "eeprom_addr". 0081 * 0082 * P.S. If you need to write, lock and read the Identification Page, 0083 * (M24M02-DR device only, which we do not use), change the "7" to 0084 * "0xF" in the macro below, and let the client set bit 20 to 1 in 0085 * "eeprom_addr", and set A10 to 0 to write into it, and A10 and A1 to 0086 * 1 to lock it permanently. 0087 */ 0088 #define MAKE_I2C_ADDR(_aa) ((0xA << 3) | (((_aa) >> 16) & 7)) 0089 0090 static int __amdgpu_eeprom_xfer(struct i2c_adapter *i2c_adap, u32 eeprom_addr, 0091 u8 *eeprom_buf, u16 buf_size, bool read) 0092 { 0093 u8 eeprom_offset_buf[EEPROM_OFFSET_SIZE]; 0094 struct i2c_msg msgs[] = { 0095 { 0096 .flags = 0, 0097 .len = EEPROM_OFFSET_SIZE, 0098 .buf = eeprom_offset_buf, 0099 }, 0100 { 0101 .flags = read ? I2C_M_RD : 0, 0102 }, 0103 }; 0104 const u8 *p = eeprom_buf; 0105 int r; 0106 u16 len; 0107 0108 for (r = 0; buf_size > 0; 0109 buf_size -= len, eeprom_addr += len, eeprom_buf += len) { 0110 /* Set the EEPROM address we want to write to/read from. 0111 */ 0112 msgs[0].addr = MAKE_I2C_ADDR(eeprom_addr); 0113 msgs[1].addr = msgs[0].addr; 0114 msgs[0].buf[0] = (eeprom_addr >> 8) & 0xff; 0115 msgs[0].buf[1] = eeprom_addr & 0xff; 0116 0117 if (!read) { 0118 /* Write the maximum amount of data, without 0119 * crossing the device's page boundary, as per 0120 * its spec. Partial page writes are allowed, 0121 * starting at any location within the page, 0122 * so long as the page boundary isn't crossed 0123 * over (actually the page pointer rolls 0124 * over). 0125 * 0126 * As per the AT24CM02 EEPROM spec, after 0127 * writing into a page, the I2C driver should 0128 * terminate the transfer, i.e. in 0129 * "i2c_transfer()" below, with a STOP 0130 * condition, so that the self-timed write 0131 * cycle begins. This is implied for the 0132 * "i2c_transfer()" abstraction. 0133 */ 0134 len = min(EEPROM_PAGE_SIZE - (eeprom_addr & 0135 EEPROM_PAGE_MASK), 0136 (u32)buf_size); 0137 } else { 0138 /* Reading from the EEPROM has no limitation 0139 * on the number of bytes read from the EEPROM 0140 * device--they are simply sequenced out. 0141 */ 0142 len = buf_size; 0143 } 0144 msgs[1].len = len; 0145 msgs[1].buf = eeprom_buf; 0146 0147 /* This constitutes a START-STOP transaction. 0148 */ 0149 r = i2c_transfer(i2c_adap, msgs, ARRAY_SIZE(msgs)); 0150 if (r != ARRAY_SIZE(msgs)) 0151 break; 0152 0153 if (!read) { 0154 /* According to EEPROM specs the length of the 0155 * self-writing cycle, tWR (tW), is 10 ms. 0156 * 0157 * TODO: Use polling on ACK, aka Acknowledge 0158 * Polling, to minimize waiting for the 0159 * internal write cycle to complete, as it is 0160 * usually smaller than tWR (tW). 0161 */ 0162 msleep(10); 0163 } 0164 } 0165 0166 return r < 0 ? r : eeprom_buf - p; 0167 } 0168 0169 /** 0170 * amdgpu_eeprom_xfer -- Read/write from/to an I2C EEPROM device 0171 * @i2c_adap: pointer to the I2C adapter to use 0172 * @eeprom_addr: EEPROM address from which to read/write 0173 * @eeprom_buf: pointer to data buffer to read into/write from 0174 * @buf_size: the size of @eeprom_buf 0175 * @read: True if reading from the EEPROM, false if writing 0176 * 0177 * Returns the number of bytes read/written; -errno on error. 0178 */ 0179 static int amdgpu_eeprom_xfer(struct i2c_adapter *i2c_adap, u32 eeprom_addr, 0180 u8 *eeprom_buf, u16 buf_size, bool read) 0181 { 0182 const struct i2c_adapter_quirks *quirks = i2c_adap->quirks; 0183 u16 limit; 0184 0185 if (!quirks) 0186 limit = 0; 0187 else if (read) 0188 limit = quirks->max_read_len; 0189 else 0190 limit = quirks->max_write_len; 0191 0192 if (limit == 0) { 0193 return __amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, 0194 eeprom_buf, buf_size, read); 0195 } else if (limit <= EEPROM_OFFSET_SIZE) { 0196 dev_err_ratelimited(&i2c_adap->dev, 0197 "maddr:0x%04X size:0x%02X:quirk max_%s_len must be > %d", 0198 eeprom_addr, buf_size, 0199 read ? "read" : "write", EEPROM_OFFSET_SIZE); 0200 return -EINVAL; 0201 } else { 0202 u16 ps; /* Partial size */ 0203 int res = 0, r; 0204 0205 /* The "limit" includes all data bytes sent/received, 0206 * which would include the EEPROM_OFFSET_SIZE bytes. 0207 * Account for them here. 0208 */ 0209 limit -= EEPROM_OFFSET_SIZE; 0210 for ( ; buf_size > 0; 0211 buf_size -= ps, eeprom_addr += ps, eeprom_buf += ps) { 0212 ps = min(limit, buf_size); 0213 0214 r = __amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, 0215 eeprom_buf, ps, read); 0216 if (r < 0) 0217 return r; 0218 res += r; 0219 } 0220 0221 return res; 0222 } 0223 } 0224 0225 int amdgpu_eeprom_read(struct i2c_adapter *i2c_adap, 0226 u32 eeprom_addr, u8 *eeprom_buf, 0227 u16 bytes) 0228 { 0229 return amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, eeprom_buf, bytes, 0230 true); 0231 } 0232 0233 int amdgpu_eeprom_write(struct i2c_adapter *i2c_adap, 0234 u32 eeprom_addr, u8 *eeprom_buf, 0235 u16 bytes) 0236 { 0237 return amdgpu_eeprom_xfer(i2c_adap, eeprom_addr, eeprom_buf, bytes, 0238 false); 0239 }
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |