Patrick Delaunay | 16d3771 | 2023-09-26 17:09:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2023, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dfu.h> |
| 8 | #include <dm.h> |
| 9 | #include <misc.h> |
| 10 | #include <asm/arch/stm32prog.h> |
| 11 | #include <power/stpmic1.h> |
| 12 | |
| 13 | static int dfu_otp_read(u64 offset, u8 *buffer, long *size) |
| 14 | { |
| 15 | struct udevice *dev; |
| 16 | int ret; |
| 17 | |
| 18 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 19 | DM_DRIVER_GET(stm32mp_bsec), |
| 20 | &dev); |
| 21 | if (ret) |
| 22 | return ret; |
| 23 | |
| 24 | ret = misc_read(dev, offset + STM32_BSEC_OTP_OFFSET, buffer, *size); |
| 25 | if (ret >= 0) { |
| 26 | *size = ret; |
| 27 | ret = 0; |
| 28 | } |
| 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | static int dfu_pmic_read(u64 offset, u8 *buffer, long *size) |
| 34 | { |
| 35 | int ret; |
| 36 | struct udevice *dev; |
| 37 | |
| 38 | if (!IS_ENABLED(CONFIG_PMIC_STPMIC1)) { |
| 39 | log_err("PMIC update not supported"); |
| 40 | return -EOPNOTSUPP; |
| 41 | } |
| 42 | |
| 43 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 44 | DM_DRIVER_GET(stpmic1_nvm), |
| 45 | &dev); |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | ret = misc_read(dev, 0xF8 + offset, buffer, *size); |
| 50 | if (ret >= 0) { |
| 51 | *size = ret; |
| 52 | ret = 0; |
| 53 | } |
| 54 | if (ret == -EACCES) { |
| 55 | *size = 0; |
| 56 | ret = 0; |
| 57 | } |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | int dfu_read_medium_virt(struct dfu_entity *dfu, u64 offset, |
| 63 | void *buf, long *len) |
| 64 | { |
| 65 | switch (dfu->data.virt.dev_num) { |
| 66 | case 0x0: |
| 67 | return dfu_otp_read(offset, buf, len); |
| 68 | case 0x1: |
| 69 | return dfu_pmic_read(offset, buf, len); |
| 70 | } |
| 71 | |
| 72 | if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) && |
| 73 | dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM) |
| 74 | return stm32prog_read_medium_virt(dfu, offset, buf, len); |
| 75 | |
| 76 | *len = 0; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int dfu_write_medium_virt(struct dfu_entity *dfu, u64 offset, |
| 81 | void *buf, long *len) |
| 82 | { |
| 83 | if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) && |
| 84 | dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM) |
| 85 | return stm32prog_write_medium_virt(dfu, offset, buf, len); |
| 86 | |
| 87 | return -EOPNOTSUPP; |
| 88 | } |
| 89 | |
| 90 | int dfu_get_medium_size_virt(struct dfu_entity *dfu, u64 *size) |
| 91 | { |
| 92 | if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) && |
| 93 | dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM) |
| 94 | return stm32prog_get_medium_size_virt(dfu, size); |
| 95 | |
| 96 | *size = SZ_1K; |
| 97 | |
| 98 | return 0; |
| 99 | } |