Changeset 1442


Ignore:
Timestamp:
09/16/11 01:38:14 (8 months ago)
Author:
stefanct
Message:

serprog: add SPI support

Adds a new opcode (0x13) that just relays SPI bytes and wires it up to be
usable within serprog.c. Checks for mandatory opcodes are moved around and
changed a bit, but non-SPI programmers should not be harmed by this patch.

Signed-off-by: Urja Rannikko <urjaman@…>
Signed-off-by: Stefan Tauner <stefan.tauner@…>
Acked-by: Uwe Hermann <uwe@…>

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/programmer.h

    r1433 r1442  
    546546        SPI_CONTROLLER_LINUX, 
    547547#endif 
     548#if CONFIG_SERPROG == 1 
     549        SPI_CONTROLLER_SERPROG, 
     550#endif 
    548551}; 
    549552extern const int spi_programmer_count; 
     
    606609void serprog_chip_readn(uint8_t *buf, const chipaddr addr, size_t len); 
    607610void serprog_delay(int delay); 
     611int serprog_spi_send_command(unsigned int writecnt, unsigned int readcnt, 
     612                        const unsigned char *writearr, unsigned char *readarr); 
     613int serprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len); 
    608614#endif 
    609615 
  • trunk/serprog-protocol.txt

    r1367 r1442  
    32320x11    Query maximum read-n length     none                            ACK + 24-bit length (0==2^24) / NAK 
    33330x12    Set used bustype                8-bit flags (as with 0x05)      ACK / NAK 
     340x13    Perform SPI operation           24-bit slen + 24-bit rlen       ACK + rlen bytes of data / NAK 
     35                                         + slen bytes of data 
    34360x??    unimplemented command - invalid. 
    3537 
     
    5153        0x05 (Q_BUSTYPE): 
    5254                The bit's are defined as follows: 
    53                 bit 0: PARALLEL, bit 1: LPC, bit 2: FWH, bit 3: SPI (if ever supported). 
     55                bit 0: PARALLEL, bit 1: LPC, bit 2: FWH, bit 3: SPI. 
    5456        0x06 (Q_CHIPSIZE): 
    5557                Only applicable to parallel programmers. 
     
    6769                Sending a byte with more than 1 bit set will make the programmer decide among them 
    6870                on it's own. Bit values as with Q_BUSTYPE. 
     71        0x13 (O_SPIOP): 
     72                Send and receive bytes via SPI. 
     73                Maximum slen is Q_WRNMAXLEN in case Q_BUSTYPE returns SPI only or S_BUSTYPE was used 
     74                to set SPI exclusively before. Same for rlen and Q_RDNMAXLEN. 
     75                This operation is immediate, meaning it doesnt use the operation buffer. 
    6976        About mandatory commands: 
    7077                The only truly mandatory commands for any device are 0x00, 0x01, 0x02 and 0x10, 
     
    100107#define S_CMD_Q_RDNMAXLEN       0x11            /* Query read-n maximum length                  */ 
    101108#define S_CMD_S_BUSTYPE         0x12            /* Set used bustype(s).                         */ 
     109#define S_CMD_O_SPIOP           0x13            /* Perform SPI operation.                       */ 
  • trunk/serprog.c

    r1396 r1442  
    22 * This file is part of the flashrom project. 
    33 * 
    4  * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com> 
     4 * Copyright (C) 2009, 2011 Urja Rannikko <urjaman@gmail.com> 
    55 * Copyright (C) 2009 Carl-Daniel Hailfinger 
    66 * 
     
    3737#include "flash.h" 
    3838#include "programmer.h" 
     39#include "chipdrivers.h" 
    3940 
    4041#define MSGHEADER "serprog:" 
     
    6869#define S_CMD_Q_RDNMAXLEN       0x11    /* Query read-n maximum length                  */ 
    6970#define S_CMD_S_BUSTYPE         0x12    /* Set used bustype(s).                         */ 
     71#define S_CMD_O_SPIOP           0x13    /* Perform SPI operation.                       */ 
    7072 
    7173static uint16_t sp_device_serbuf_size = 16; 
     
    303305} 
    304306 
     307static struct spi_programmer spi_programmer_serprog = { 
     308        .type           = SPI_CONTROLLER_SERPROG, 
     309        .max_data_read  = MAX_DATA_READ_UNLIMITED, 
     310        .max_data_write = MAX_DATA_WRITE_UNLIMITED, 
     311        .command        = serprog_spi_send_command, 
     312        .multicommand   = default_spi_send_multicommand, 
     313        .read           = serprog_spi_read, 
     314        .write_256      = default_spi_write_256, 
     315}; 
     316 
    305317int serprog_init(void) 
    306318{ 
     
    326338                                 "Use flashrom -p serprog:dev=/dev/device:baud\n"); 
    327339                        free(device); 
    328                         return 1;                
     340                        return 1; 
    329341                } 
    330342                if (strlen(device)) { 
     
    359371                                 "Use flashrom -p serprog:ip=ipaddr:port\n"); 
    360372                        free(device); 
    361                         return 1;                
     373                        return 1; 
    362374                } 
    363375                if (strlen(device)) { 
     
    411423        sp_check_avail_automatic = 1; 
    412424 
    413         /* Check for the minimum operational set of commands */ 
    414         if (sp_check_commandavail(S_CMD_R_BYTE) == 0) { 
    415                 msg_perr("Error: Single byte read not supported\n"); 
    416                 exit(1); 
    417         } 
    418         /* This could be translated to single byte reads (if missing),  * 
    419          * but now we dont support that.                                */ 
    420         if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) { 
    421                 msg_perr("Error: Read n bytes not supported\n"); 
    422                 exit(1); 
    423         } 
    424         /* In the future one could switch to read-only mode if these    * 
    425          * are not available.                                           */ 
    426         if (sp_check_commandavail(S_CMD_O_INIT) == 0) { 
    427                 msg_perr("Error: Initialize operation buffer not supported\n"); 
    428                 exit(1); 
    429         } 
    430         if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) { 
    431                 msg_perr("Error: Write to opbuf: write byte not supported\n"); 
    432                 exit(1); 
    433         } 
    434         if (sp_check_commandavail(S_CMD_O_DELAY) == 0) { 
    435                 msg_perr("Error: Write to opbuf: delay not supported\n"); 
    436                 exit(1); 
    437         } 
    438         if (sp_check_commandavail(S_CMD_O_EXEC) == 0) { 
    439                 msg_perr( 
    440                         "Error: Execute operation buffer not supported\n"); 
    441                 exit(1); 
     425 
     426        if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) { 
     427                msg_perr("Warning: NAK to query supported buses\n"); 
     428                c = BUS_NONSPI; /* A reasonable default for now. */ 
     429        } 
     430        buses_supported = c; 
     431        /* Check for the minimum operational set of commands. */ 
     432        if (buses_supported & BUS_SPI) { 
     433                uint8_t bt = BUS_SPI; 
     434                if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) { 
     435                        msg_perr("Error: SPI operation not supported while the " 
     436                                 "bustype is SPI\n"); 
     437                        exit(1); 
     438                } 
     439                /* Success of any of these commands is optional. We don't need 
     440                   the programmer to tell us its limits, but if it doesn't, we 
     441                   will assume stuff, so it's in the programmers best interest 
     442                   to tell us. */ 
     443                sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL); 
     444                if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { 
     445                        uint32_t v; 
     446                        v = ((unsigned int)(rbuf[0]) << 0); 
     447                        v |= ((unsigned int)(rbuf[1]) << 8); 
     448                        v |= ((unsigned int)(rbuf[2]) << 16); 
     449                        if (v == 0) 
     450                                v = (1 << 24) - 1; /* SPI-op maximum. */ 
     451                        spi_programmer_serprog.max_data_write = v; 
     452                        msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v); 
     453                } 
     454                if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) { 
     455                        uint32_t v; 
     456                        v = ((unsigned int)(rbuf[0]) << 0); 
     457                        v |= ((unsigned int)(rbuf[1]) << 8); 
     458                        v |= ((unsigned int)(rbuf[2]) << 16); 
     459                        if (v == 0) 
     460                                v = (1 << 24) - 1; /* SPI-op maximum. */ 
     461                        spi_programmer_serprog.max_data_read = v; 
     462                        msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v); 
     463                } 
     464                bt = buses_supported; 
     465                sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL); 
     466                register_spi_programmer(&spi_programmer_serprog); 
     467        } 
     468 
     469        if (buses_supported & BUS_NONSPI) { 
     470                if (sp_check_commandavail(S_CMD_O_INIT) == 0) { 
     471                        msg_perr("Error: Initialize operation buffer " 
     472                                 "not supported\n"); 
     473                        exit(1); 
     474                } 
     475 
     476                if (sp_check_commandavail(S_CMD_O_DELAY) == 0) { 
     477                        msg_perr("Error: Write to opbuf: " 
     478                                 "delay not supported\n"); 
     479                        exit(1); 
     480                } 
     481 
     482                /* S_CMD_O_EXEC availability checked later. */ 
     483 
     484                if (sp_check_commandavail(S_CMD_R_BYTE) == 0) { 
     485                        msg_perr("Error: Single byte read not supported\n"); 
     486                        exit(1); 
     487                } 
     488                /* This could be translated to single byte reads (if missing), 
     489                 * but now we don't support that. */ 
     490                if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) { 
     491                        msg_perr("Error: Read n bytes not supported\n"); 
     492                        exit(1); 
     493                } 
     494                if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) { 
     495                        msg_perr("Error: Write to opbuf: " 
     496                                 "write byte not supported\n"); 
     497                        exit(1); 
     498                } 
     499 
     500                if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { 
     501                        msg_pdbg(MSGHEADER "Write-n not supported"); 
     502                        sp_max_write_n = 0; 
     503                } else { 
     504                        sp_max_write_n = ((unsigned int)(rbuf[0]) << 0); 
     505                        sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8); 
     506                        sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16); 
     507                        if (!sp_max_write_n) { 
     508                                sp_max_write_n = (1 << 24); 
     509                        } 
     510                        msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", 
     511                                 sp_max_write_n); 
     512                        sp_write_n_buf = malloc(sp_max_write_n); 
     513                        if (!sp_write_n_buf) { 
     514                                msg_perr("Error: cannot allocate memory for " 
     515                                         "Write-n buffer\n"); 
     516                                exit(1); 
     517                        } 
     518                        sp_write_n_bytes = 0; 
     519                } 
     520 
     521                if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) && 
     522                    (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) { 
     523                        sp_max_read_n = ((unsigned int)(rbuf[0]) << 0); 
     524                        sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8); 
     525                        sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16); 
     526                        msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", 
     527                                 sp_max_read_n ? sp_max_read_n : (1 << 24)); 
     528                } else { 
     529                        msg_pdbg(MSGHEADER "Maximum read-n length " 
     530                                 "not reported\n"); 
     531                        sp_max_read_n = 0; 
     532                } 
     533 
    442534        } 
    443535 
     
    455547                     sp_device_serbuf_size); 
    456548 
    457         if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, &sp_device_opbuf_size)) { 
    458                 msg_perr("Warning: NAK to query operation buffer size\n"); 
    459         } 
    460         msg_pdbg(MSGHEADER "operation buffer size %d\n", 
    461                      sp_device_opbuf_size); 
    462  
    463         if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) { 
    464                 msg_perr("Warning: NAK to query supported buses\n"); 
    465                 c = BUS_NONSPI; /* A reasonable default for now. */ 
    466         } 
    467         buses_supported = c; 
    468  
    469         if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) { 
    470                 msg_perr("Error: NAK to initialize operation buffer\n"); 
    471                 exit(1); 
    472         } 
    473  
    474         if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { 
    475                 msg_pdbg(MSGHEADER "Write-n not supported"); 
    476                 sp_max_write_n = 0; 
    477         } else { 
    478                 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0); 
    479                 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8); 
    480                 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16); 
    481                 msg_pdbg(MSGHEADER "Maximum write-n length %d\n", 
    482                              sp_max_write_n); 
    483                 sp_write_n_buf = malloc(sp_max_write_n); 
    484                 if (!sp_write_n_buf) { 
    485                         msg_perr("Error: cannot allocate memory for Write-n buffer\n"); 
     549        if (sp_check_commandavail(S_CMD_O_INIT)) { 
     550                /* This would be inconsistent. */ 
     551                if (sp_check_commandavail(S_CMD_O_EXEC) == 0) { 
     552                        msg_perr("Error: Execute operation buffer not " 
     553                                 "supported\n"); 
    486554                        exit(1); 
    487555                } 
    488                 sp_write_n_bytes = 0; 
    489         } 
    490          
    491         if ((sp_check_commandavail(S_CMD_Q_RDNMAXLEN)) 
    492                 &&((sp_docommand(S_CMD_Q_RDNMAXLEN,0,NULL, 3, rbuf) == 0))) { 
    493                 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0); 
    494                 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8); 
    495                 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16); 
    496                 msg_pdbg(MSGHEADER "Maximum read-n length %d\n", 
    497                         sp_max_read_n ? sp_max_read_n : (1<<24)); 
    498         } else { 
    499                 msg_pdbg(MSGHEADER "Maximum read-n length not reported\n"); 
    500                 sp_max_read_n = 0; 
    501         } 
     556 
     557                if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) { 
     558                        msg_perr("Error: NAK to initialize operation buffer\n"); 
     559                        exit(1); 
     560                } 
     561 
     562                if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, 
     563                    &sp_device_opbuf_size)) { 
     564                        msg_perr("Warning: NAK to query operation buffer " 
     565                                 "size\n"); 
     566                } 
     567                msg_pdbg(MSGHEADER "operation buffer size %d\n", 
     568                         sp_device_opbuf_size); 
     569        } 
    502570 
    503571        sp_prev_was_write = 0; 
     
    680748        unsigned char buf[4]; 
    681749        msg_pspew("%s\n", __func__); 
     750        if (!sp_check_commandavail(S_CMD_O_DELAY)) { 
     751                internal_delay(delay); 
     752                msg_pdbg("Note: serprog_delay used, but the programmer doesn't " 
     753                         "support delay\n"); 
     754                return; 
     755        } 
    682756        if ((sp_max_write_n) && (sp_write_n_bytes)) 
    683757                sp_pass_writen(); 
     
    691765        sp_prev_was_write = 0; 
    692766} 
     767 
     768int serprog_spi_send_command(unsigned int writecnt, unsigned int readcnt, 
     769                             const unsigned char *writearr, 
     770                             unsigned char *readarr) 
     771{ 
     772        unsigned char *parmbuf; 
     773        int ret; 
     774        msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); 
     775        if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) 
     776                sp_execute_opbuf(); 
     777        parmbuf = malloc(writecnt + 6); 
     778        if (!parmbuf) 
     779                sp_die("Error: cannot malloc SPI send param buffer"); 
     780        parmbuf[0] = (writecnt >> 0) & 0xFF; 
     781        parmbuf[1] = (writecnt >> 8) & 0xFF; 
     782        parmbuf[2] = (writecnt >> 16) & 0xFF; 
     783        parmbuf[3] = (readcnt >> 0) & 0xFF; 
     784        parmbuf[4] = (readcnt >> 8) & 0xFF; 
     785        parmbuf[5] = (readcnt >> 16) & 0xFF; 
     786        memcpy(parmbuf + 6, writearr, writecnt); 
     787        ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt, 
     788                           readarr); 
     789        free(parmbuf); 
     790        return ret; 
     791} 
     792 
     793/* FIXME: This function is optimized so that it does not split each transaction 
     794 * into chip page_size long blocks unnecessarily like spi_read_chunked. This has 
     795 * the advantage that it is much faster for most chips, but breaks those with 
     796 * non-contiguous address space (like AT45DB161D). When spi_read_chunked is 
     797 * fixed this method can be removed. */ 
     798int serprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) 
     799{ 
     800        int i; 
     801        int cur_len; 
     802        const int max_read = spi_programmer_serprog.max_data_read; 
     803        for (i = 0; i < len; i += cur_len) { 
     804                int ret; 
     805                cur_len = min(max_read, (len - i)); 
     806                ret = spi_nbyte_read(start + i, buf + i, cur_len); 
     807                if (ret) 
     808                        return ret; 
     809        } 
     810        return 0; 
     811} 
Note: See TracChangeset for help on using the changeset viewer.