blob: dc155ad0e5b3bdb70133369dba8290a9e311f5c2 [file] [log] [blame]
Przemyslaw Marczak4b19ed62013-10-23 14:30:45 +02001/*
2 * Copyright (C) 2013 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <usb_mass_storage.h>
10#include <part.h>
11
12static int ums_read_sector(struct ums *ums_dev,
13 ulong start, lbaint_t blkcnt, void *buf)
14{
15 block_dev_desc_t *block_dev = &ums_dev->mmc->block_dev;
16 lbaint_t blkstart = start + ums_dev->start_sector;
17 int dev_num = block_dev->dev;
18
19 return block_dev->block_read(dev_num, blkstart, blkcnt, buf);
20}
21
22static int ums_write_sector(struct ums *ums_dev,
23 ulong start, lbaint_t blkcnt, const void *buf)
24{
25 block_dev_desc_t *block_dev = &ums_dev->mmc->block_dev;
26 lbaint_t blkstart = start + ums_dev->start_sector;
27 int dev_num = block_dev->dev;
28
29 return block_dev->block_write(dev_num, blkstart, blkcnt, buf);
30}
31
32static struct ums ums_dev = {
33 .read_sector = ums_read_sector,
34 .write_sector = ums_write_sector,
35 .name = "UMS disk",
36};
37
38static struct ums *ums_disk_init(struct mmc *mmc)
39{
40 uint64_t mmc_end_sector = mmc->capacity / SECTOR_SIZE;
41 uint64_t ums_end_sector = UMS_NUM_SECTORS + UMS_START_SECTOR;
42
43 if (!mmc_end_sector) {
44 error("MMC capacity is not valid");
45 return NULL;
46 }
47
48 ums_dev.mmc = mmc;
49
50 if (ums_end_sector <= mmc_end_sector) {
51 ums_dev.start_sector = UMS_START_SECTOR;
52 if (UMS_NUM_SECTORS)
53 ums_dev.num_sectors = UMS_NUM_SECTORS;
54 else
55 ums_dev.num_sectors = mmc_end_sector - UMS_START_SECTOR;
56 } else {
57 ums_dev.num_sectors = mmc_end_sector;
58 puts("UMS: defined bad disk parameters. Using default.\n");
59 }
60
61 printf("UMS: disk start sector: %#x, count: %#x\n",
62 ums_dev.start_sector, ums_dev.num_sectors);
63
64 return &ums_dev;
65}
66
67struct ums *ums_init(unsigned int dev_num)
68{
69 struct mmc *mmc = NULL;
70
71 mmc = find_mmc_device(dev_num);
72 if (!mmc)
73 return NULL;
74
75 return ums_disk_init(mmc);
76}