1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "mtd_spi_common.h"
10 #include "hdf_log.h"
11
SpiFlashDumpDefualt(struct MtdDevice * mtdDevice)12 static void SpiFlashDumpDefualt(struct MtdDevice *mtdDevice)
13 {
14 struct MtdSpiConfig *cfg = NULL;
15 struct SpiFlash *spi = NULL;
16
17 if (mtdDevice == NULL) {
18 return;
19 }
20 MTD_DEVICE_DUMP(mtdDevice);
21
22 spi = CONTAINER_OF(mtdDevice, struct SpiFlash, mtd);
23 HDF_LOGD("%s: cs = %u, addrCycle = %u", __func__, spi->cs, spi->addrCycle);
24
25 cfg = &spi->readCfg;
26 HDF_LOGD("%s: readCfg -> ifType:%u, cmd:0x%x, dummy:%u, size:%u, clock:%u",
27 __func__, cfg->ifType, cfg->cmd, cfg->dummy, cfg->size, cfg->clock);
28 cfg = &spi->writeCfg;
29 HDF_LOGD("%s: writeCfg -> ifType:%u, cmd:0x%x, dummy:%u, size:%u, clock:%u",
30 __func__, cfg->ifType, cfg->cmd, cfg->dummy, cfg->size, cfg->clock);
31 cfg = &spi->eraseCfg;
32 HDF_LOGD("%s: eraseCfg -> ifType:%u, cmd:0x%x, dummy:%u, size:%u, clock:%u",
33 __func__, cfg->ifType, cfg->cmd, cfg->dummy, cfg->size, cfg->clock);
34 }
35
SpiFlashAdd(struct SpiFlash * spi)36 int32_t SpiFlashAdd(struct SpiFlash *spi)
37 {
38 if (spi == NULL || spi->mtd.ops == NULL) {
39 return HDF_ERR_INVALID_OBJECT;
40 }
41 if (spi->mtd.ops->dump == NULL) {
42 spi->mtd.ops->dump = SpiFlashDumpDefualt;
43 }
44 return MtdDeviceAdd(&spi->mtd);
45 }
46
SpiFlashDel(struct SpiFlash * spi)47 void SpiFlashDel(struct SpiFlash *spi)
48 {
49 if (spi != NULL) {
50 MtdDeviceDel(&spi->mtd);
51 }
52 }
53
SpiFlashWaitReady(struct SpiFlash * spi)54 int32_t SpiFlashWaitReady(struct SpiFlash *spi)
55 {
56 int32_t ret;
57
58 if (spi == NULL) {
59 return HDF_ERR_INVALID_OBJECT;
60 }
61 if (spi->spiOps.waitReady == NULL) {
62 return HDF_ERR_NOT_SUPPORT;
63 }
64 ret = (spi->spiOps.waitReady(spi));
65 if (ret != HDF_SUCCESS) {
66 HDF_LOGD("%s: wait dev ready failed, ret=%d", __func__, ret);
67 }
68 return ret;
69 }
70
SpiFlashWriteEnable(struct SpiFlash * spi)71 int32_t SpiFlashWriteEnable(struct SpiFlash *spi)
72 {
73 int32_t ret;
74
75 if (spi == NULL) {
76 return HDF_ERR_INVALID_OBJECT;
77 }
78 if (spi->spiOps.writeEnable == NULL) {
79 return HDF_ERR_NOT_SUPPORT;
80 }
81 ret = (spi->spiOps.writeEnable(spi));
82 if (ret != HDF_SUCCESS) {
83 HDF_LOGD("%s: dev write enable failed, ret=%d", __func__, ret);
84 }
85 return ret;
86 }
87
SpiFlashQeEnable(struct SpiFlash * spi)88 int32_t SpiFlashQeEnable(struct SpiFlash *spi)
89 {
90 int32_t ret;
91
92 if (spi == NULL) {
93 return HDF_ERR_INVALID_OBJECT;
94 }
95 if (spi->spiOps.qeEnable == NULL) {
96 return HDF_ERR_NOT_SUPPORT;
97 }
98 ret = (spi->spiOps.qeEnable(spi));
99 if (ret != HDF_SUCCESS) {
100 HDF_LOGD("%s: dev qe enable failed, ret=%d", __func__, ret);
101 }
102 return ret;
103 }
104
SpiFlashEntry4Addr(struct SpiFlash * spi,int enable)105 int32_t SpiFlashEntry4Addr(struct SpiFlash *spi, int enable)
106 {
107 int32_t ret;
108
109 if (spi == NULL) {
110 return HDF_ERR_INVALID_OBJECT;
111 }
112 if (spi->spiOps.entry4Addr == NULL) {
113 return HDF_ERR_NOT_SUPPORT;
114 }
115 ret = (spi->spiOps.entry4Addr(spi, enable));
116 if (ret != HDF_SUCCESS) {
117 HDF_LOGD("%s: dev set 4addr failed, enabl=%d, ret=%d", __func__, enable, ret);
118 }
119 return ret;
120 }
121