uboot/drivers/dfu/dfu_sf.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
   4 */
   5
   6#include <common.h>
   7#include <malloc.h>
   8#include <errno.h>
   9#include <div64.h>
  10#include <dfu.h>
  11#include <spi.h>
  12#include <spi_flash.h>
  13#include <jffs2/load_kernel.h>
  14#include <linux/mtd/mtd.h>
  15
  16static int dfu_get_medium_size_sf(struct dfu_entity *dfu, u64 *size)
  17{
  18        *size = dfu->data.sf.size;
  19
  20        return 0;
  21}
  22
  23static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
  24                              long *len)
  25{
  26        return spi_flash_read(dfu->data.sf.dev, dfu->data.sf.start + offset,
  27                *len, buf);
  28}
  29
  30static u64 find_sector(struct dfu_entity *dfu, u64 start, u64 offset)
  31{
  32        return (lldiv((start + offset), dfu->data.sf.dev->sector_size)) *
  33                dfu->data.sf.dev->sector_size;
  34}
  35
  36static int dfu_write_medium_sf(struct dfu_entity *dfu,
  37                               u64 offset, void *buf, long *len)
  38{
  39        int ret;
  40
  41        ret = spi_flash_erase(dfu->data.sf.dev,
  42                              find_sector(dfu, dfu->data.sf.start, offset),
  43                              dfu->data.sf.dev->sector_size);
  44        if (ret)
  45                return ret;
  46
  47        ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
  48                              *len, buf);
  49        if (ret)
  50                return ret;
  51
  52        return 0;
  53}
  54
  55static int dfu_flush_medium_sf(struct dfu_entity *dfu)
  56{
  57        u64 off, length;
  58
  59        if (!CONFIG_IS_ENABLED(DFU_SF_PART) || !dfu->data.sf.ubi)
  60                return 0;
  61
  62        /* in case of ubi partition, erase rest of the partition */
  63        off = find_sector(dfu, dfu->data.sf.start, dfu->offset);
  64        /* last write ended with unaligned length jump to next */
  65        if (off != dfu->data.sf.start + dfu->offset)
  66                off += dfu->data.sf.dev->sector_size;
  67        length = dfu->data.sf.start + dfu->data.sf.size - off;
  68        if (length)
  69                return spi_flash_erase(dfu->data.sf.dev, off, length);
  70
  71        return 0;
  72}
  73
  74static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
  75{
  76        /*
  77         * Currently, Poll Timeout != 0 is only needed on nand
  78         * ubi partition, as sectors which are not used need
  79         * to be erased
  80         */
  81        if (CONFIG_IS_ENABLED(DFU_SF_PART) && dfu->data.sf.ubi)
  82                return DFU_MANIFEST_POLL_TIMEOUT;
  83
  84        return DFU_DEFAULT_POLL_TIMEOUT;
  85}
  86
  87static void dfu_free_entity_sf(struct dfu_entity *dfu)
  88{
  89        spi_flash_free(dfu->data.sf.dev);
  90}
  91
  92static struct spi_flash *parse_dev(char *devstr)
  93{
  94        unsigned int bus;
  95        unsigned int cs;
  96        unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
  97        unsigned int mode = CONFIG_SF_DEFAULT_MODE;
  98        char *s, *endp;
  99        struct spi_flash *dev;
 100
 101        s = strsep(&devstr, ":");
 102        if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) {
 103                printf("Invalid SPI bus %s\n", s);
 104                return NULL;
 105        }
 106
 107        s = strsep(&devstr, ":");
 108        if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) {
 109                printf("Invalid SPI chip-select %s\n", s);
 110                return NULL;
 111        }
 112
 113        s = strsep(&devstr, ":");
 114        if (s && *s) {
 115                speed = simple_strtoul(s, &endp, 0);
 116                if (*endp || !speed) {
 117                        printf("Invalid SPI speed %s\n", s);
 118                        return NULL;
 119                }
 120        }
 121
 122        s = strsep(&devstr, ":");
 123        if (s && *s) {
 124                mode = simple_strtoul(s, &endp, 0);
 125                if (*endp || mode > 3) {
 126                        printf("Invalid SPI mode %s\n", s);
 127                        return NULL;
 128                }
 129        }
 130
 131        dev = spi_flash_probe(bus, cs, speed, mode);
 132        if (!dev) {
 133                printf("Failed to create SPI flash at %d:%d:%d:%d\n",
 134                       bus, cs, speed, mode);
 135                return NULL;
 136        }
 137
 138        return dev;
 139}
 140
 141int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s)
 142{
 143        char *st;
 144        char *devstr_bkup = strdup(devstr);
 145
 146        dfu->data.sf.dev = parse_dev(devstr_bkup);
 147        free(devstr_bkup);
 148        if (!dfu->data.sf.dev)
 149                return -ENODEV;
 150
 151        dfu->dev_type = DFU_DEV_SF;
 152        dfu->max_buf_size = dfu->data.sf.dev->sector_size;
 153
 154        st = strsep(&s, " ");
 155        if (!strcmp(st, "raw")) {
 156                dfu->layout = DFU_RAW_ADDR;
 157                dfu->data.sf.start = simple_strtoul(s, &s, 16);
 158                s++;
 159                dfu->data.sf.size = simple_strtoul(s, &s, 16);
 160        } else if (CONFIG_IS_ENABLED(DFU_SF_PART) &&
 161                   (!strcmp(st, "part") || !strcmp(st, "partubi"))) {
 162                char mtd_id[32];
 163                struct mtd_device *mtd_dev;
 164                u8 part_num;
 165                struct part_info *pi;
 166                int ret, dev, part;
 167
 168                dfu->layout = DFU_RAW_ADDR;
 169
 170                dev = simple_strtoul(s, &s, 10);
 171                s++;
 172                part = simple_strtoul(s, &s, 10);
 173
 174                sprintf(mtd_id, "%s%d,%d", "nor", dev, part - 1);
 175                printf("using id '%s'\n", mtd_id);
 176
 177                mtdparts_init();
 178
 179                ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
 180                if (ret != 0) {
 181                        printf("Could not locate '%s'\n", mtd_id);
 182                        return -1;
 183                }
 184                dfu->data.sf.start = pi->offset;
 185                dfu->data.sf.size = pi->size;
 186                if (!strcmp(st, "partubi"))
 187                        dfu->data.sf.ubi = 1;
 188        } else {
 189                printf("%s: Memory layout (%s) not supported!\n", __func__, st);
 190                spi_flash_free(dfu->data.sf.dev);
 191                return -1;
 192        }
 193
 194        dfu->get_medium_size = dfu_get_medium_size_sf;
 195        dfu->read_medium = dfu_read_medium_sf;
 196        dfu->write_medium = dfu_write_medium_sf;
 197        dfu->flush_medium = dfu_flush_medium_sf;
 198        dfu->poll_timeout = dfu_polltimeout_sf;
 199        dfu->free_entity = dfu_free_entity_sf;
 200
 201        /* initial state */
 202        dfu->inited = 0;
 203
 204        return 0;
 205}
 206