Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions arch/arm64/boot/dts/apple/t8152-j873-qspi.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@
#size-cells = <1>;

/*
* 64 KiB NVRAM v3 banks ("3VVN") from 0x24000
* to 0x1f4000, as on T8140 J700.
* 13 x 64 KiB NVRAM v3 banks ("3VVN") from 0x24000
* to 0xf4000 (ADT /arm-io/qspi/spinor/anvram on the
* 1 MiB W25Q80 boot NOR).
*/
nvram: partition@24000 {
label = "nvram";
reg = <0x24000 0x1d0000>;
reg = <0x24000 0xd0000>;
};
};
};
Expand Down
22 changes: 16 additions & 6 deletions drivers/spi/spi-apple-qspi-policy-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ static void qspi_policy_status(struct kunit *test)
KUNIT_EXPECT_TRUE(test, apple_qspi_status_allows_write(0x04, 0x02, 0, 0x1c));
/* The J813 W25Q64: QE and the SR3 drive strength, no block protection. */
KUNIT_EXPECT_TRUE(test, apple_qspi_status_allows_write(0x00, 0x02, 0x60, 0));
/* The J873g W25Q80: QE and SR3 0x20, no block protection. */
KUNIT_EXPECT_TRUE(test, apple_qspi_status_allows_write(0x00, 0x02, 0x20, 0));
KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0x04, 0x02, 0x20, 0));
/* CMP, SUS and WPS are refused everywhere. */
KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0, 0x40, 0, 0x1c));
KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0, 0x80, 0, 0x1c));
Expand Down Expand Up @@ -277,15 +280,17 @@ static void qspi_policy_parts(struct kunit *test)
static const unsigned char known[][3] = {
{ 0xef, 0x65, 0x17 }, { 0xc2, 0x2b, 0x27 },
{ 0xef, 0x65, 0x15 }, { 0xc8, 0x83, 0x15 },
{ 0xc8, 0x83, 0x17 },
{ 0xc8, 0x83, 0x17 }, { 0xef, 0x65, 0x14 },
};
static const unsigned int sizes[] = { 0x800000, 0x800000, 0x200000, 0x200000,
0x800000 };
static const unsigned char bp_ok[] = { 0, 0, 0x1c, 0x1c, 0 };
0x800000, 0x100000 };
static const unsigned int sfdp_sizes[] = { 0x800000, 0x800000, 0x200000, 0x200000,
0x800000, 0x200000 };
static const unsigned char bp_ok[] = { 0, 0, 0x1c, 0x1c, 0, 0 };
static const unsigned char unknown[][3] = {
{ 0xef, 0x65, 0x16 }, { 0xef, 0x40, 0x17 }, { 0xc2, 0x2b, 0x28 },
{ 0xc8, 0x60, 0x15 }, { 0xc8, 0x83, 0x16 }, { 0xc8, 0x83, 0x18 },
{ 0, 0, 0 }, { 0xff, 0xff, 0xff },
{ 0xef, 0x65, 0x13 }, { 0xef, 0x65, 0x16 }, { 0xef, 0x40, 0x17 },
{ 0xc2, 0x2b, 0x28 }, { 0xc8, 0x60, 0x15 }, { 0xc8, 0x83, 0x16 },
{ 0xc8, 0x83, 0x18 }, { 0, 0, 0 }, { 0xff, 0xff, 0xff },
};
const struct apple_qspi_part *part;
unsigned int i;
Expand All @@ -294,6 +299,7 @@ static void qspi_policy_parts(struct kunit *test)
part = apple_qspi_find_part(known[i]);
KUNIT_ASSERT_NOT_NULL(test, part);
KUNIT_EXPECT_EQ(test, part->size, sizes[i]);
KUNIT_EXPECT_EQ(test, apple_qspi_part_sfdp_size(part), sfdp_sizes[i]);
KUNIT_EXPECT_EQ(test, part->bp_ok, bp_ok[i]);
}
for (i = 0; i < ARRAY_SIZE(unknown); i++)
Expand All @@ -302,6 +308,10 @@ static void qspi_policy_parts(struct kunit *test)
part = apple_qspi_find_part(known[2]);
KUNIT_EXPECT_LE(test, 0x1f4000ULL, (unsigned long long)part->size);
KUNIT_EXPECT_GT(test, 0x800000ULL, (unsigned long long)part->size);
/* The J873g 13-bank window fits 1 MiB; an oversized 2 MiB J700 window does not. */
part = apple_qspi_find_part(known[5]);
KUNIT_EXPECT_LE(test, 0xf4000ULL, (unsigned long long)part->size);
KUNIT_EXPECT_GT(test, 0x1f4000ULL, (unsigned long long)part->size);
}

static void qspi_policy_protected(struct kunit *test)
Expand Down
23 changes: 19 additions & 4 deletions drivers/spi/spi-apple-qspi-policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,22 @@ static inline int apple_qspi_nvram_bank_state(const unsigned char h[24])

/*
* The boot NOR parts whose nvram partition may be written: the JEDEC ID, the
* size, which bounds the window (a three-byte address past it wraps around)
* and must match the part's SFDP density, and the SR1 block-protect bits the
* part's firmware leaves set. Those stay set; apple_qspi_protected_range()
* says what they protect.
* physical size, which bounds the window (a three-byte address past it wraps
* around), the SR1 block-protect bits the part's firmware leaves set, and
* optionally sfdp_size when a part's factory SFDP density differs from its
* physical size (0 means SFDP density must equal size). Protected bits stay
* set; apple_qspi_protected_range() says what they protect.
*/
struct apple_qspi_part {
unsigned char jedec[3];
unsigned int size;
unsigned char bp_ok;
unsigned int sfdp_size;
};

#define APPLE_QSPI_JEDEC_W25Q64 { 0xef, 0x65, 0x17 }
#define APPLE_QSPI_JEDEC_W25Q16 { 0xef, 0x65, 0x15 }
#define APPLE_QSPI_JEDEC_W25Q80 { 0xef, 0x65, 0x14 }
#define APPLE_QSPI_JEDEC_MX_2B27 { 0xc2, 0x2b, 0x27 }
#define APPLE_QSPI_JEDEC_GD25UF16E { 0xc8, 0x83, 0x15 }
#define APPLE_QSPI_JEDEC_GD25UF64E { 0xc8, 0x83, 0x17 }
Expand All @@ -184,8 +187,20 @@ static const struct apple_qspi_part apple_qspi_parts[] = {
{ APPLE_QSPI_JEDEC_GD25UF16E, 0x200000, 0x1c },
/* J775D (M5 Ultra): no block protection left set, SR1 0x00 */
{ APPLE_QSPI_JEDEC_GD25UF64E, 0x800000, 0 },
/*
* J873g (Mac mini, M6): 1 MiB die (wraps at 0x100000, ADT spinor
* ranges 0x100000), no BP bits (SR1 0x00, SR2 0x02, SR3 0x20),
* factory SFDP BFPT reports 16 Mbit (0x00ffffff = 0x200000).
*/
{ APPLE_QSPI_JEDEC_W25Q80, 0x100000, 0, 0x200000 },
};

static inline unsigned int
apple_qspi_part_sfdp_size(const struct apple_qspi_part *part)
{
return part->sfdp_size ? part->sfdp_size : part->size;
}

static inline const struct apple_qspi_part *
apple_qspi_find_part(const unsigned char id[3])
{
Expand Down
3 changes: 2 additions & 1 deletion drivers/spi/spi-apple-qspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,8 @@ static int apple_qspi_qualify(struct spi_mem *mem, struct apple_qspi *qspi,
ret = apple_qspi_read_sfdp(mem, table, dword, sizeof(dword));
if (ret)
return ret;
if (apple_qspi_sfdp_size(get_unaligned_le32(&dword[4])) != part->size) {
if (apple_qspi_sfdp_size(get_unaligned_le32(&dword[4])) !=
apple_qspi_part_sfdp_size(part)) {
*denial = "part-size";
ret = -ENODEV;
goto latch;
Expand Down
40 changes: 33 additions & 7 deletions tools/asahi/qspi-nvram-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,17 @@ def function(source, signature):
static bool sent(unsigned int o){for(unsigned int i=0;i<wire_count;i++)if(wire[i]==o)return true;return false;}
static void denied(struct spi_mem_op o){wire_count=0;assert(apple_qspi_exec_op(&mem,&o)<0);assert(!wire_count);}
int main(void){
const u8 known[][3]={{0xef,0x65,0x17},{0xc2,0x2b,0x27},{0xef,0x65,0x15},{0xc8,0x83,0x15},{0xc8,0x83,0x17}};
const u32 sizes[]={0x800000,0x800000,0x200000,0x200000,0x800000};
const u8 tolerated[]={0,0,0x1c,0x1c,0};
const u8 known[][3]={{0xef,0x65,0x17},{0xc2,0x2b,0x27},{0xef,0x65,0x15},{0xc8,0x83,0x15},{0xc8,0x83,0x17},{0xef,0x65,0x14}};
const u32 sizes[]={0x800000,0x800000,0x200000,0x200000,0x800000,0x100000};
const u32 sfdp_sizes[]={0x800000,0x800000,0x200000,0x200000,0x800000,0x200000};
const u8 tolerated[]={0,0,0x1c,0x1c,0,0};
for(unsigned int i=0;i<ARRAY_SIZE(known);i++){
const struct apple_qspi_part *part=apple_qspi_find_part(known[i]);
assert(part&&part->size==sizes[i]&&part->bp_ok==tolerated[i]);
assert(part&&part->size==sizes[i]&&apple_qspi_part_sfdp_size(part)==sfdp_sizes[i]&&part->bp_ok==tolerated[i]);
}
const u8 unknown[][3]={{0xef,0x65,0x16},{0xef,0x65,0x18},{0xc2,0x2b,0x28},{0xc8,0x60,0x15},{0xef,0x40,0x17},{0,0,0},{0xff,0xff,0xff}};
const u8 unknown[][3]={{0xef,0x65,0x13},{0xef,0x65,0x16},{0xef,0x65,0x18},{0xc2,0x2b,0x28},{0xc8,0x60,0x15},{0xef,0x40,0x17},{0,0,0},{0xff,0xff,0xff}};
for(unsigned int i=0;i<ARRAY_SIZE(unknown);i++)assert(!apple_qspi_find_part(unknown[i]));
puts("PASS part table: W25Q64, Macronix 2B27, W25Q16, GD25UF16E and GD25UF64E with their size and tolerated BP bits; other IDs refused");
puts("PASS part table: W25Q64, Macronix 2B27, W25Q16, GD25UF16E, GD25UF64E and W25Q80 with their size, SFDP size and tolerated BP bits; other IDs refused");
u8 data[256];memset(data,0x55,sizeof(data));reset();
struct spi_mem_op wren=SPI_MEM_OP(SPI_MEM_OP_CMD(0x06,1),SPI_MEM_OP_NO_ADDR,SPI_MEM_OP_NO_DUMMY,SPI_MEM_OP_NO_DATA);
/* T8140 (J700) shares the verified policy at its DT nvram window; its data
Expand Down Expand Up @@ -327,6 +328,26 @@ def function(source, signature):
sr[0]=0x00;sr[1]=0x02;sr[2]=0x20;wire_count=0;o=program(0x700000,256,data);
assert(!apple_qspi_exec_op(&mem,&o)&&sent(0x5a)&&sent(0x02)&&q.qualified);
puts("PASS GD25UF64E J775D part: GigaDevice SR1/SR2/SR3 checks with SR1 0x00, SR2 0x02, SR3 0x20; qualify and BP0 refusal");
/* J873g W25Q80 part: 1 MiB die (wraps at 0x100000), factory SFDP 0x00ffffff (2 MiB), 13-bank window. */
const u8 w25q80[3]={0xef,0x65,0x14};
reset();q.qualified=0;memcpy(jedec,w25q80,3);sfdp_set(0x00ffffff);window(0x24000,0xf4000);
store_at(0x34000);arm();memset(data,0x55,sizeof(data));
sr[0]=0x00;sr[1]=0x02;sr[2]=0x20;wire_count=0;o=program(0x24000,256,data);
assert(!apple_qspi_exec_op(&mem,&o)&&sent(0x5a)&&sent(0x02)&&q.qualified);
assert(!memcmp(flash+0x24000,data,256));
/* BP0 is refused on W25Q80 (no BP bits tolerated). */
reset();memcpy(jedec,w25q80,3);sfdp_set(0x00ffffff);window(0x24000,0xf4000);arm();
sr[0]=0x04;sr[1]=0x02;sr[2]=0x20;wire_count=0;o=program(0x24000,1,data);
assert(apple_qspi_exec_op(&mem,&o)==-EACCES&&!sent(0x06)&&!sent(0x02));
/* An oversized 2 MiB window past the 1 MiB physical size is refused before WREN or SFDP. */
reset();q.qualified=0;memcpy(jedec,w25q80,3);sfdp_set(0x00ffffff);window(0x24000,0x1f4000);
store_at(0x24000);arm();wire_count=0;o=program(0x24000,1,data);
assert(apple_qspi_exec_op(&mem,&o)==-ENODEV&&!sent(0x06)&&!sent(0x5a));
/* A mismatched SFDP density is refused. */
reset();q.qualified=0;memcpy(jedec,w25q80,3);sfdp_set(0x03ffffff);window(0x24000,0xf4000);
store_at(0x24000);arm();wire_count=0;o=program(0x24000,1,data);
assert(apple_qspi_exec_op(&mem,&o)==-ENODEV&&!sent(0x06)&&q.qualified==-ENODEV&&!strcmp(q.qualify_denial,"part-size"));
puts("PASS W25Q80 J873g part: 1 MiB physical bound, factory 2 MiB SFDP match, SR1 0x00/SR2 0x02/SR3 0x20, BP0 and oversized window refused");
/* BP0 protects the top 64 KiB of a 2 MiB part, the J700 window's last 16 KiB: refused before WREN there. */
reset();memcpy(jedec,w25q16,3);window(0x24000,0x1f4000);arm();sr[0]=0x04;
wire_count=0;o=program(0x1f0000,1,data);assert(apple_qspi_exec_op(&mem,&o)==-EACCES&&!sent(0x06));
Expand Down Expand Up @@ -472,7 +493,8 @@ def main():
('bank-blank', 'if (memchr_inv(buf, 0xff, APPLE_QSPI_ERASE_SIZE))', 'if (0)'),
('protected-bank', 'op->addr.val < qspi->write_start || op->addr.val + len > qspi->write_end', '0'),
('protected-qualify', '!apple_qspi_unprotected_banks(', '0 && !apple_qspi_unprotected_banks('),
('sfdp-size', '!= part->size) {', '!= part->size && 0) {'),
('sfdp-size', 'apple_qspi_part_sfdp_size(part)) {',
'apple_qspi_part_sfdp_size(part) && 0) {'),
('window-read-only', '!read_only && size &&', 'size &&'),
('window-bank', '!(size % APPLE_QSPI_NVRAM_BANK) &&', ''),
('range', 'addr >= start &&', 'addr >= 1 &&'),
Expand All @@ -489,6 +511,10 @@ def main():
'APPLE_QSPI_JEDEC_GD25UF16E\t{ 0xc8, 0x83, 0x16 }'),
('j775d-gd-id', 'APPLE_QSPI_JEDEC_GD25UF64E\t{ 0xc8, 0x83, 0x17 }',
'APPLE_QSPI_JEDEC_GD25UF64E\t{ 0xc8, 0x83, 0x18 }'),
('j873g-w25q80-id', 'APPLE_QSPI_JEDEC_W25Q80\t\t{ 0xef, 0x65, 0x14 }',
'APPLE_QSPI_JEDEC_W25Q80\t\t{ 0xef, 0x65, 0x13 }'),
('j873g-w25q80-size', '{ APPLE_QSPI_JEDEC_W25Q80, 0x100000, 0, 0x200000 }',
'{ APPLE_QSPI_JEDEC_W25Q80, 0x200000, 0, 0x200000 }'),
('read-only', 'read_only || !qspi->nvram_write_candidate || qspi->write_fault',
'!qspi->nvram_write_candidate || qspi->write_fault'),
('fault-latch', 'write_fault_latched = true;', 'write_fault_latched = false;'),
Expand Down
Loading