blob: fc8904e2bd3626b598f6a65349b42a4a95fd9b28 [file] [log] [blame]
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -05001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
Avi Shchislowskidc7ab962016-03-08 14:22:41 -050015 *
16 * Modified to add field firmware update support,
17 * those modifications are Copyright (c) 2016 SanDisk Corp.
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -050018 */
19
20#include <asm-generic/int-ll64.h>
21#include <linux/mmc/ioctl.h>
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -050022#include <stdio.h>
23
24#define CHECK(expr, msg, err_stmt) { if (expr) { fprintf(stderr, msg); err_stmt; } }
25
Roman Peniaev023cc7c2014-08-12 23:25:45 +090026#ifndef offsetof
27#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
28#endif
29
Oleg Matcovschi294bf862013-05-23 17:11:06 -070030/* From kernel linux/major.h */
31#define MMC_BLOCK_MAJOR 179
32
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -050033/* From kernel linux/mmc/mmc.h */
34#define MMC_SWITCH 6 /* ac [31:0] See below R1b */
35#define MMC_SEND_EXT_CSD 8 /* adtc R1 */
Ben Gardiner27c357d2013-05-30 17:12:47 -040036#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
Julius Wernerbcc3e2e2016-04-21 16:53:02 -070037#define MMC_SET_WRITE_PROT 28 /* ac [31:0] block number R1b */
38#define MMC_CLR_WRITE_PROT 29 /* ac [31:0] block number R1b */
39#define MMC_SEND_WRITE_PROT_TYPE 31 /* adtc [31:0] block number R1 */
Ben Gardiner27c357d2013-05-30 17:12:47 -040040#define R1_SWITCH_ERROR (1 << 7) /* sx, c */
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -050041#define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */
Roman Peniaev023cc7c2014-08-12 23:25:45 +090042#define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */
Avi Shchislowskidc7ab962016-03-08 14:22:41 -050043#define MMC_WRITE_BLOCK 24 /* adtc [31:0] data addr R1 */
Roman Peniaev023cc7c2014-08-12 23:25:45 +090044#define MMC_WRITE_MULTIPLE_BLOCK 25 /* adtc R1 */
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -050045
46/*
47 * EXT_CSD fields
48 */
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +010049#define EXT_CSD_S_CMD_SET 504
50#define EXT_CSD_HPI_FEATURE 503
Jaehoon Chung86496512012-09-21 10:08:05 +000051#define EXT_CSD_BKOPS_SUPPORT 502 /* RO */
Avi Shchislowskidc7ab962016-03-08 14:22:41 -050052#define EXT_CSD_SUPPORTED_MODES 493 /* RO */
53#define EXT_CSD_FFU_FEATURES 492 /* RO */
54#define EXT_CSD_FFU_ARG_3 490 /* RO */
55#define EXT_CSD_FFU_ARG_2 489 /* RO */
56#define EXT_CSD_FFU_ARG_1 488 /* RO */
57#define EXT_CSD_FFU_ARG_0 487 /* RO */
58#define EXT_CSD_NUM_OF_FW_SEC_PROG_3 305 /* RO */
59#define EXT_CSD_NUM_OF_FW_SEC_PROG_2 304 /* RO */
60#define EXT_CSD_NUM_OF_FW_SEC_PROG_1 303 /* RO */
61#define EXT_CSD_NUM_OF_FW_SEC_PROG_0 302 /* RO */
62#define EXT_CSD_FIRMWARE_VERSION 254 /* RO */
Al Cooper786418c2015-04-29 18:12:35 -040063#define EXT_CSD_CACHE_SIZE_3 252
64#define EXT_CSD_CACHE_SIZE_2 251
65#define EXT_CSD_CACHE_SIZE_1 250
66#define EXT_CSD_CACHE_SIZE_0 249
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +010067#define EXT_CSD_BOOT_INFO 228 /* R/W */
Ben Gardiner4e850232013-05-30 17:12:49 -040068#define EXT_CSD_SEC_COUNT_3 215
69#define EXT_CSD_SEC_COUNT_2 214
70#define EXT_CSD_SEC_COUNT_1 213
71#define EXT_CSD_SEC_COUNT_0 212
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +010072#define EXT_CSD_PART_SWITCH_TIME 199
Al Cooper786418c2015-04-29 18:12:35 -040073#define EXT_CSD_REV 192
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +010074#define EXT_CSD_BOOT_CFG 179
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +020075#define EXT_CSD_PART_CONFIG 179
Al Cooper794314c2015-05-01 08:24:37 -040076#define EXT_CSD_BOOT_BUS_CONDITIONS 177
Ben Gardinerd91d3692013-05-30 17:12:51 -040077#define EXT_CSD_ERASE_GROUP_DEF 175
Saugata Dasb7e25992012-05-17 09:26:34 -040078#define EXT_CSD_BOOT_WP 173
Julius Wernerbcc3e2e2016-04-21 16:53:02 -070079#define EXT_CSD_USER_WP 171
Avi Shchislowskidc7ab962016-03-08 14:22:41 -050080#define EXT_CSD_FW_CONFIG 169 /* R/W */
Al Cooper794314c2015-05-01 08:24:37 -040081#define EXT_CSD_WR_REL_SET 167
Saugata Dasb7e25992012-05-17 09:26:34 -040082#define EXT_CSD_WR_REL_PARAM 166
Yaniv Gardi21bb4732013-05-26 13:25:33 -040083#define EXT_CSD_SANITIZE_START 165
Jaehoon Chung86496512012-09-21 10:08:05 +000084#define EXT_CSD_BKOPS_EN 163 /* R/W */
Chris Ballf74dfe22012-10-19 16:49:55 -040085#define EXT_CSD_RST_N_FUNCTION 162 /* R/W */
Ben Gardiner82bd9502013-06-27 11:04:10 -040086#define EXT_CSD_PARTITIONING_SUPPORT 160 /* RO */
Oliver Metz22f26412013-09-23 08:40:51 +020087#define EXT_CSD_MAX_ENH_SIZE_MULT_2 159
88#define EXT_CSD_MAX_ENH_SIZE_MULT_1 158
89#define EXT_CSD_MAX_ENH_SIZE_MULT_0 157
Ben Gardinerd91d3692013-05-30 17:12:51 -040090#define EXT_CSD_PARTITIONS_ATTRIBUTE 156 /* R/W */
Ben Gardinera6cd98d2013-05-30 17:12:46 -040091#define EXT_CSD_PARTITION_SETTING_COMPLETED 155 /* R/W */
Balaji T K1fdb7f92015-04-29 18:12:32 -040092#define EXT_CSD_GP_SIZE_MULT_4_2 154
93#define EXT_CSD_GP_SIZE_MULT_4_1 153
94#define EXT_CSD_GP_SIZE_MULT_4_0 152
95#define EXT_CSD_GP_SIZE_MULT_3_2 151
96#define EXT_CSD_GP_SIZE_MULT_3_1 150
97#define EXT_CSD_GP_SIZE_MULT_3_0 149
98#define EXT_CSD_GP_SIZE_MULT_2_2 148
99#define EXT_CSD_GP_SIZE_MULT_2_1 147
100#define EXT_CSD_GP_SIZE_MULT_2_0 146
101#define EXT_CSD_GP_SIZE_MULT_1_2 145
102#define EXT_CSD_GP_SIZE_MULT_1_1 144
103#define EXT_CSD_GP_SIZE_MULT_1_0 143
Ben Gardinerf82e27a2013-05-30 17:12:50 -0400104#define EXT_CSD_ENH_SIZE_MULT_2 142
105#define EXT_CSD_ENH_SIZE_MULT_1 141
106#define EXT_CSD_ENH_SIZE_MULT_0 140
Ben Gardiner68f490b2013-05-30 17:12:48 -0400107#define EXT_CSD_ENH_START_ADDR_3 139
108#define EXT_CSD_ENH_START_ADDR_2 138
109#define EXT_CSD_ENH_START_ADDR_1 137
110#define EXT_CSD_ENH_START_ADDR_0 136
Gwendal Grignou771984c2014-07-01 12:46:18 -0700111#define EXT_CSD_REV 192
Saugata Dasb7e25992012-05-17 09:26:34 -0400112#define EXT_CSD_NATIVE_SECTOR_SIZE 63 /* R */
113#define EXT_CSD_USE_NATIVE_SECTOR 62 /* R/W */
114#define EXT_CSD_DATA_SECTOR_SIZE 61 /* R */
Julius Wernerbcc3e2e2016-04-21 16:53:02 -0700115#define EXT_CSD_CLASS_6_CTRL 59
Balaji T Kd78ce082015-04-29 18:12:33 -0400116#define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_1 53
117#define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_0 52
Al Cooper786418c2015-04-29 18:12:35 -0400118#define EXT_CSD_CACHE_CTRL 33
Avi Shchislowskidc7ab962016-03-08 14:22:41 -0500119#define EXT_CSD_MODE_CONFIG 30
120#define EXT_CSD_MODE_OPERATION_CODES 29 /* W */
121#define EXT_CSD_FFU_STATUS 26 /* R */
Saugata Dasb7e25992012-05-17 09:26:34 -0400122
123/*
124 * WR_REL_PARAM field definitions
125 */
126#define HS_CTRL_REL (1<<0)
127#define EN_REL_WR (1<<2)
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -0500128
129/*
Jaehoon Chung86496512012-09-21 10:08:05 +0000130 * BKOPS_EN field definition
131 */
132#define BKOPS_ENABLE (1<<0)
133
Gwendal Grignou0f757342014-10-16 16:52:46 -0700134#define MMC_FFU_INVOKE_OP 302
Gwendal Grignou771984c2014-07-01 12:46:18 -0700135
Jaehoon Chung86496512012-09-21 10:08:05 +0000136/*
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -0500137 * EXT_CSD field definitions
138 */
Avi Shchislowskidc7ab962016-03-08 14:22:41 -0500139#define EXT_CSD_FFU_INSTALL (0x01)
140#define EXT_CSD_FFU_MODE (0x01)
141#define EXT_CSD_NORMAL_MODE (0x00)
142#define EXT_CSD_FFU (1<<0)
143#define EXT_CSD_UPDATE_DISABLE (1<<0)
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100144#define EXT_CSD_HPI_SUPP (1<<0)
145#define EXT_CSD_HPI_IMPL (1<<1)
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -0500146#define EXT_CSD_CMD_SET_NORMAL (1<<0)
147#define EXT_CSD_BOOT_WP_B_PWR_WP_DIS (0x40)
148#define EXT_CSD_BOOT_WP_B_PERM_WP_DIS (0x10)
149#define EXT_CSD_BOOT_WP_B_PERM_WP_EN (0x04)
150#define EXT_CSD_BOOT_WP_B_PWR_WP_EN (0x01)
Giuseppe CAVALLAROa5bf4a22012-02-20 09:45:29 +0100151#define EXT_CSD_BOOT_INFO_HS_MODE (1<<2)
152#define EXT_CSD_BOOT_INFO_DDR_DDR (1<<1)
153#define EXT_CSD_BOOT_INFO_ALT (1<<0)
154#define EXT_CSD_BOOT_CFG_ACK (1<<6)
155#define EXT_CSD_BOOT_CFG_EN (0x38)
Mario Schuknecht8c0c40d2013-05-15 08:28:04 +0200156#define EXT_CSD_BOOT_CFG_ACC (0x07)
Chris Ballf74dfe22012-10-19 16:49:55 -0400157#define EXT_CSD_RST_N_EN_MASK (0x03)
158#define EXT_CSD_HW_RESET_EN (0x01)
159#define EXT_CSD_HW_RESET_DIS (0x02)
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +0200160#define EXT_CSD_PART_CONFIG_ACC_MASK (0x7)
Markus Schuetterlefbc0e6c2016-03-19 08:42:41 +0100161#define EXT_CSD_PART_CONFIG_ACC_NONE (0x0)
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +0200162#define EXT_CSD_PART_CONFIG_ACC_BOOT0 (0x1)
163#define EXT_CSD_PART_CONFIG_ACC_BOOT1 (0x2)
164#define EXT_CSD_PART_CONFIG_ACC_USER_AREA (0x7)
165#define EXT_CSD_PART_CONFIG_ACC_ACK (0x40)
Ben Gardiner82bd9502013-06-27 11:04:10 -0400166#define EXT_CSD_PARTITIONING_EN (1<<0)
167#define EXT_CSD_ENH_ATTRIBUTE_EN (1<<1)
Balaji T K1fdb7f92015-04-29 18:12:32 -0400168#define EXT_CSD_ENH_4 (1<<4)
169#define EXT_CSD_ENH_3 (1<<3)
170#define EXT_CSD_ENH_2 (1<<2)
171#define EXT_CSD_ENH_1 (1<<1)
Ben Gardinerd91d3692013-05-30 17:12:51 -0400172#define EXT_CSD_ENH_USR (1<<0)
Julius Wernerbcc3e2e2016-04-21 16:53:02 -0700173#define EXT_CSD_US_PERM_WP_DIS (1<<4)
174#define EXT_CSD_US_PWR_WP_DIS (1<<3)
175#define EXT_CSD_US_PERM_WP_EN (1<<2)
176#define EXT_CSD_US_PWR_WP_EN (1<<0)
Al Cooper786418c2015-04-29 18:12:35 -0400177#define EXT_CSD_REV_V5_1 8
178#define EXT_CSD_REV_V5_0 7
179#define EXT_CSD_REV_V4_5 6
180#define EXT_CSD_REV_V4_4_1 5
181#define EXT_CSD_REV_V4_3 3
182#define EXT_CSD_REV_V4_2 2
183#define EXT_CSD_REV_V4_1 1
184#define EXT_CSD_REV_V4_0 0
185
Johan RUDHOLMa8bfde72012-02-12 11:46:44 -0500186
187/* From kernel linux/mmc/core.h */
188#define MMC_RSP_PRESENT (1 << 0)
189#define MMC_RSP_136 (1 << 1) /* 136 bit response */
190#define MMC_RSP_CRC (1 << 2) /* expect valid crc */
191#define MMC_RSP_BUSY (1 << 3) /* card may send busy */
192#define MMC_RSP_OPCODE (1 << 4) /* response contains opcode */
193
194#define MMC_CMD_AC (0 << 5)
195#define MMC_CMD_ADTC (1 << 5)
196
197#define MMC_RSP_SPI_S1 (1 << 7) /* one status byte */
198#define MMC_RSP_SPI_BUSY (1 << 10) /* card may send busy */
199
200#define MMC_RSP_SPI_R1 (MMC_RSP_SPI_S1)
201#define MMC_RSP_SPI_R1B (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
202
203#define MMC_RSP_R1 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
204#define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)