rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2010 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <err.h> |
| 24 | #include <debug.h> |
| 25 | #include <trace.h> |
| 26 | #include <platform.h> |
| 27 | #include "platform_p.h" |
| 28 | #include <platform/armemu.h> |
| 29 | #include <lib/bio.h> |
| 30 | #include <reg.h> |
| 31 | |
| 32 | static bdev_t dev; |
| 33 | |
| 34 | static uint64_t get_blkdev_len(void) |
| 35 | { |
| 36 | return *REG64(BDEV_LEN); |
| 37 | } |
| 38 | |
| 39 | ssize_t read_block(struct bdev *dev, void *buf, bnum_t block, uint count) |
| 40 | { |
| 41 | /* assume args have been validated by layer above */ |
| 42 | *REG32(BDEV_CMD_ADDR) = (uint32_t)buf; |
| 43 | *REG64(BDEV_CMD_OFF) = (uint64_t)((uint64_t)block * dev->block_size); |
| 44 | *REG32(BDEV_CMD_LEN) = count * dev->block_size; |
| 45 | |
| 46 | *REG32(BDEV_CMD) = BDEV_CMD_READ; |
| 47 | |
| 48 | uint32_t err = *REG32(BDEV_CMD) & BDEV_CMD_ERRMASK; |
| 49 | if (err == BDEV_CMD_ERR_NONE) |
| 50 | return count * dev->block_size; |
| 51 | else |
| 52 | return ERR_IO; |
| 53 | } |
| 54 | |
| 55 | ssize_t write_block(struct bdev *dev, const void *buf, bnum_t block, uint count) |
| 56 | { |
| 57 | /* assume args have been validated by layer above */ |
| 58 | *REG32(BDEV_CMD_ADDR) = (uint32_t)buf; |
| 59 | *REG64(BDEV_CMD_OFF) = (uint64_t)((uint64_t)block * dev->block_size); |
| 60 | *REG32(BDEV_CMD_LEN) = count * dev->block_size; |
| 61 | |
| 62 | *REG32(BDEV_CMD) = BDEV_CMD_WRITE; |
| 63 | |
| 64 | uint32_t err = *REG32(BDEV_CMD) & BDEV_CMD_ERRMASK; |
| 65 | if (err == BDEV_CMD_ERR_NONE) |
| 66 | return count * dev->block_size; |
| 67 | else |
| 68 | return ERR_IO; |
| 69 | } |
| 70 | |
| 71 | void platform_init_blkdev(void) |
| 72 | { |
| 73 | if ((*REG32(SYSINFO_FEATURES) & SYSINFO_FEATURE_BLOCKDEV) == 0) |
| 74 | return; // no block device |
| 75 | |
| 76 | TRACEF("device len %lld\n", get_blkdev_len()); |
| 77 | |
| 78 | if (get_blkdev_len() == 0) |
| 79 | return; |
| 80 | |
| 81 | bio_initialize_bdev(&dev, "block0", 512, get_blkdev_len() / 512, 0, NULL, |
| 82 | BIO_FLAGS_NONE); |
| 83 | |
| 84 | // fill in hooks |
| 85 | dev.read_block = &read_block; |
| 86 | dev.write_block = &write_block; |
| 87 | |
| 88 | bio_register_device(&dev); |
| 89 | } |
| 90 | |