From: Will Sowerbutts Date: Mon, 29 Dec 2014 20:58:35 +0000 (+0000) Subject: Kernel: n8vem-mark4: Optimise SD SPI routines to run faster, and drop X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=2211ec2ba652c9a5906fd1e280b583ccf87f3160;p=FUZIX.git Kernel: n8vem-mark4: Optimise SD SPI routines to run faster, and drop the large precomputed table. Rename block send/receive routines with clearer names. --- diff --git a/Kernel/dev/devsd.c b/Kernel/dev/devsd.c index d5ecb433..0dc0ccfd 100644 --- a/Kernel/dev/devsd.c +++ b/Kernel/dev/devsd.c @@ -33,8 +33,8 @@ static int sd_readwrite(uint8_t minor, uint8_t rawflag, bool do_write); static int sd_spi_init(uint8_t drive); static void sd_spi_release(uint8_t drive); static int sd_spi_wait_ready(uint8_t drive); -static bool sd_spi_transmit_block(uint8_t drive, void *ptr, unsigned int length); -static bool sd_spi_receive_block(uint8_t drive, void *ptr, unsigned int length); +static bool sd_spi_transmit_sector(uint8_t drive, void *ptr, unsigned int length); +static bool sd_spi_receive_sector(uint8_t drive, void *ptr, unsigned int length); static int sd_send_command(uint8_t drive, unsigned char cmd, uint32_t arg); static uint32_t sd_get_size_sectors(uint8_t drive); static bool sd_read_sector(uint8_t drive, void *ptr, uint32_t lba); @@ -233,7 +233,7 @@ static int sd_spi_wait_ready(uint8_t drive) return res; } -static bool sd_spi_transmit_block(uint8_t drive, void *ptr, unsigned int length) +static bool sd_spi_transmit_sector(uint8_t drive, void *ptr, unsigned int length) { unsigned char reply; @@ -241,7 +241,7 @@ static bool sd_spi_transmit_block(uint8_t drive, void *ptr, unsigned int length) return false; /* failed */ sd_spi_transmit_byte(drive, 0xFE); - sd_spi_transmit_from_memory(drive, ptr, length); + sd_spi_transmit_block(drive, ptr, length); sd_spi_transmit_byte(drive, 0xFF); /* dummy CRC */ sd_spi_transmit_byte(drive, 0xFF); reply = sd_spi_receive_byte(drive); @@ -250,7 +250,7 @@ static bool sd_spi_transmit_block(uint8_t drive, void *ptr, unsigned int length) return true; /* hooray! */ } -static bool sd_spi_receive_block(uint8_t drive, void *ptr, unsigned int length) +static bool sd_spi_receive_sector(uint8_t drive, void *ptr, unsigned int length) { unsigned int timer; unsigned char b; @@ -260,7 +260,7 @@ static bool sd_spi_receive_block(uint8_t drive, void *ptr, unsigned int length) do{ b = sd_spi_receive_byte(drive); if(timer_expired(timer)){ - kputs("sd_spi_receive_block: timeout\n"); + kputs("sd_spi_receive_sector: timeout\n"); return false; } }while(b == 0xFF); @@ -268,7 +268,7 @@ static bool sd_spi_receive_block(uint8_t drive, void *ptr, unsigned int length) if(b != 0xFE) return false; /* failed */ - return sd_spi_receive_to_memory(drive, ptr, length); /* returns true on success */ + return sd_spi_receive_block(drive, ptr, length); /* returns true on success */ } static int sd_send_command(uint8_t drive, unsigned char cmd, uint32_t arg) @@ -325,7 +325,7 @@ static uint32_t sd_get_size_sectors(uint8_t drive) unsigned char csd[16], n; uint32_t sectors = 0; - if(sd_send_command(drive, CMD9, 0) == 0 && sd_spi_receive_block(drive, csd, 16)){ + if(sd_send_command(drive, CMD9, 0) == 0 && sd_spi_receive_sector(drive, csd, 16)){ if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */ sectors = ((uint32_t)csd[9] + (uint32_t)((unsigned int)csd[8] << 8) + 1) << 10; } else { /* SDC ver 1.XX or MMC*/ @@ -347,7 +347,7 @@ static bool sd_read_sector(uint8_t drive, void *ptr, uint32_t lba) lba = lba << 9; /* multiply by 512 to get byte address */ if(sd_send_command(drive, CMD17, lba) == 0) - r = sd_spi_receive_block(drive, ptr, 512); + r = sd_spi_receive_sector(drive, ptr, 512); sd_spi_release(drive); } @@ -364,7 +364,7 @@ static bool sd_write_sector(uint8_t drive, void *ptr, uint32_t lba) lba = lba << 9; /* multiply by 512 to get byte address */ if(sd_send_command(drive, CMD24, lba) == 0) - r = sd_spi_transmit_block(drive, ptr, 512); + r = sd_spi_transmit_sector(drive, ptr, 512); sd_spi_release(drive); } diff --git a/Kernel/dev/devsd.h b/Kernel/dev/devsd.h index 62a98a40..5ab232b5 100644 --- a/Kernel/dev/devsd.h +++ b/Kernel/dev/devsd.h @@ -26,8 +26,8 @@ void sd_spi_raise_cs(uint8_t drive); void sd_spi_lower_cs(uint8_t drive); void sd_spi_transmit_byte(uint8_t drive, uint8_t byte); uint8_t sd_spi_receive_byte(uint8_t drive); -bool sd_spi_receive_to_memory(uint8_t drive, uint8_t *ptr, unsigned int length); -bool sd_spi_transmit_from_memory(uint8_t drive, uint8_t *ptr, unsigned int length); +bool sd_spi_receive_block(uint8_t drive, uint8_t *ptr, unsigned int length); +bool sd_spi_transmit_block(uint8_t drive, uint8_t *ptr, unsigned int length); /* Definitions for MMC/SDC command */ #define CMD0 (0x40+0) /* GO_IDLE_STATE */ diff --git a/Kernel/platform-n8vem-mark4/devsdspi.c b/Kernel/platform-n8vem-mark4/devsdspi.c index a90e5489..802e9f30 100644 --- a/Kernel/platform-n8vem-mark4/devsdspi.c +++ b/Kernel/platform-n8vem-mark4/devsdspi.c @@ -1,6 +1,7 @@ /*-----------------------------------------------------------------------*/ /* N8VEM Mark IV Z180 CSI/O SPI SD driver */ /* 2014-12-27 Will Sowerbutts */ +/* 2014-12-29 Optimised for size/speed */ /*-----------------------------------------------------------------------*/ #include @@ -23,26 +24,35 @@ __sfr __at (MARK4_IO_BASE + 0x09) MARK4_SD; -/* the CSI/O and SD card send bits in opposite orders, so we need to flip them over */ -static const uint8_t reverse_byte[256] = { - 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, - 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, - 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, - 0x0c,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, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, - 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff -}; - +/* the CSI/O and SD card send the bits of each byte in opposite orders, so we need to flip them over */ +static uint8_t reverse_byte(uint8_t byte) __naked +{ + /* code by John Metcalf, from http://www.retroprogramming.com/2014/01/fast-z80-bit-reversal.html */ + __asm + ld hl, #2 + add hl, sp + ld a, (hl) +reverse_byte_a: + ; reverse bits in A + ld l,a ; a = 76543210 + rlca + rlca ; a = 54321076 + xor l + and #0xAA + xor l ; a = 56341270 + ld l,a + rlca + rlca + rlca ; a = 41270563 + rrc l ; l = 05634127 + xor l + and #0x66 + xor l ; a = 01234567 + ld l, a ; return value in L + ret + __endasm; + byte; /* squelch compiler warning */ +} void sd_spi_clock(uint8_t drive, bool go_fast) { @@ -77,7 +87,7 @@ void sd_spi_transmit_byte(uint8_t drive, unsigned char byte) drive; /* not used */ /* reverse the bits before we busywait */ - byte = reverse_byte[byte]; + byte = reverse_byte(byte); /* wait for any current transmit operation to complete */ do{ @@ -106,46 +116,91 @@ uint8_t sd_spi_receive_byte(uint8_t drive) while(CSIO_CNTR & CSIO_CNTR_RE); /* read byte */ - return reverse_byte[CSIO_TRDR]; + return reverse_byte(CSIO_TRDR); } -bool sd_spi_receive_to_memory(uint8_t drive, uint8_t *ptr, unsigned int length) +#if 0 +/* WRS: measured byte transfer time as 16.88us with Z180 @ 36.864MHz */ +bool sd_spi_receive_block(uint8_t drive, uint8_t *ptr, unsigned int length) { - unsigned char c; - drive; /* not used */ - - /* wait for any current transmit or receive operation to complete */ - do{ - c = CSIO_CNTR; - }while(c & (CSIO_CNTR_TE | CSIO_CNTR_RE)); - - while(length){ - /* enable receive operation */ - CSIO_CNTR = c | CSIO_CNTR_RE; - - /* wait for receive to complete */ - do{ - c = CSIO_CNTR; - }while(c & CSIO_CNTR_RE); - - /* read byte */ - *ptr = reverse_byte[CSIO_TRDR]; - - /* next byte */ - ptr++; - length--; + while(length--){ + *(ptr++) = sd_spi_receive_byte(drive); } - return true; } +#else +/* WRS: measured byte transfer time as approx 5.66us with Z180 @ 36.864MHz, + three times faster. Main change is to start the next receive operation + as soon as possible and overlap the loop housekeeping with the receive. */ +bool sd_spi_receive_block(uint8_t drive, uint8_t *ptr, unsigned int length) __naked +{ + __asm +waitrx: + in0 a, (_CSIO_CNTR) ; wait for any current transmit or receive operation to complete + tst a, #0x30 + jr nz, waitrx + set 5, a ; set CSIO_CNTR_RE, enable receive operation for first byte + out0 (_CSIO_CNTR), a + ld h, a ; stash value for reuse later + ; load parameters from the stack + ld iy, #3 + add iy, sp + ld e, 0(iy) ; ptr -> DE + ld d, 1(iy) + ld c, 2(iy) ; count -> BC + ld b, 3(iy) +rxnextbyte: + dec bc ; length-- + ld a, b + or c + jr nz, waitrx2 + res 5, h ; final byte: clear CSIO_CNTR_RE bit in H +waitrx2: + ld l, c ; store C temporarily + ld c, #_CSIO_CNTR ; load IO port address +waitrx3: + tstio #0x20 ; test bits in IO port (C) + jr nz, waitrx3 ; wait for receive to complete + in0 a, (_CSIO_TRDR) ; load received byte + out0 (_CSIO_CNTR), h ; start next receive (or NOP, if this is the final byte) + ld c, l ; restore C + ; reverse bits in A + ld l,a ; a = 76543210 + rlca + rlca ; a = 54321076 + xor l + and #0xAA + xor l ; a = 56341270 + ld l,a + rlca + rlca + rlca ; a = 41270563 + rrc l ; l = 05634127 + xor l + and #0x66 + xor l ; a = 01234567 + ld (de), a ; store reversed byte value + inc de ; ptr++ + bit 5, h + jr nz, rxnextbyte ; go again if not yet done + ld l, #1 ; return true + ret + __endasm; + drive; ptr; length; /* squelch compiler warnings */ +} +#endif -bool sd_spi_transmit_from_memory(uint8_t drive, uint8_t *ptr, int length) +#if 0 +/* WRS: measured byte transfer time as 10.36us with Z180 @ 36.864MHz */ +bool sd_spi_transmit_block(uint8_t drive, uint8_t *ptr, unsigned int length) { - unsigned char c, b; + register uint8_t c, b, e; drive; /* not used */ + e = (CSIO_CNTR & ~CSIO_CNTR_RE) | CSIO_CNTR_TE; + while(length){ - b = reverse_byte[*ptr]; + b = reverse_byte(*ptr); /* wait for transmit to complete */ do{ @@ -154,7 +209,7 @@ bool sd_spi_transmit_from_memory(uint8_t drive, uint8_t *ptr, int length) /* write the byte and enable transmitter */ CSIO_TRDR = b; - CSIO_CNTR = c | CSIO_CNTR_TE; + CSIO_CNTR = e; /* next byte */ ptr++; @@ -162,3 +217,53 @@ bool sd_spi_transmit_from_memory(uint8_t drive, uint8_t *ptr, int length) } return true; } +#else +/* WRS: measured byte transfer time as 5.64us with Z180 @ 36.864MHz */ +bool sd_spi_transmit_block(uint8_t drive, uint8_t *ptr, unsigned int length) __naked +{ + __asm + ; load parameters from the stack + ld iy, #3 + add iy, sp + ld e, 0(iy) ; ptr -> DE + ld d, 1(iy) + ld l, 2(iy) ; count -> HL + ld h, 3(iy) + in0 a, (_CSIO_CNTR) + and #0xDF ; mask off RE bit + or #0x10 ; set TE bit + ld b, a ; B now contains CNTR register value to start transmission +txnextbyte: + ld a, (de) + ; reverse bits in A + ld c,a + rlca + rlca + xor c + and #0xAA + xor c + ld c,a + rlca + rlca + rlca + rrc c + xor c + and #0x66 + xor c + ld c, #_CSIO_CNTR ; load IO port address +waittx: + tstio #0x10 ; test bits in IO port (C) + jr nz, waittx ; wait for transmit to complete + out0 (_CSIO_TRDR), a ; write byte to transmit + out0 (_CSIO_CNTR), b ; start transmit + inc de ; ptr++ + dec hl ; length-- + ld a, h + or l + jr nz, txnextbyte ; length != 0, go again + ld l, #1 ; return true + ret + __endasm; + drive; ptr; length; /* squelch compiler warnings */ +} +#endif