1 /* SPDX-License-Identifier:    GPL-2.0
2  *
3  * Copyright (C) 2019 Marvell International Ltd.
4  *
5  * https://spdx.org/licenses
6  */
7 #ifndef __OCTEONTX_HSMMC_H__
8 #define __OCTEONTX_HSMMC_H__
9 #include <asm/gpio.h>
10 
11 /** Name of our driver */
12 #define OCTEONTX_MMC_DRIVER_NAME	"octeontx-hsmmc"
13 
14 /** Maximum supported MMC slots */
15 #define OCTEONTX_MAX_MMC_SLOT		3
16 
17 #define POWER_ON_TIME			40 /** See SD 4.1 spec figure 6-5 */
18 
19 /**
20  * Timeout used when waiting for commands to complete.  We need to keep this
21  * above the hardware watchdog timeout which is usually limited to 1000ms
22  */
23 #define WATCHDOG_COUNT			(1100)	/* in msecs */
24 
25 /**
26  * Long timeout for commands which might take a while to complete.
27  */
28 #define MMC_TIMEOUT_LONG		1000
29 
30 /**
31  * Short timeout used for most commands in msecs
32  */
33 #define MMC_TIMEOUT_SHORT		20
34 
35 #define NSEC_PER_SEC			1000000000L
36 
37 #define MAX_NO_OF_TAPS			64
38 
39 #define EXT_CSD_POWER_CLASS		187	/* R/W */
40 
41 /* default HS400 tuning block number */
42 #define DEFAULT_HS400_TUNING_BLOCK	1
43 
44 struct octeontx_mmc_host;
45 
46 /** MMC/SD slot data structure */
47 struct octeontx_mmc_slot {
48 	struct mmc		mmc;
49 	struct mmc_config	cfg;
50 	struct octeontx_mmc_host *host;
51 	struct udevice		*dev;
52 	void			*base_addr;	/** Same as host base_addr */
53 	u64			clock;
54 	int			bus_id;		/** slot number */
55 	uint			bus_width;
56 	uint			max_width;
57 	int			hs200_tap_adj;
58 	int			hs400_tap_adj;
59 	int			hs400_tuning_block;
60 	struct gpio_desc	cd_gpio;
61 	struct gpio_desc	wp_gpio;
62 	struct gpio_desc	power_gpio;
63 	enum bus_mode		mode;
64 	union mio_emm_switch	cached_switch;
65 	union mio_emm_switch	want_switch;
66 	union mio_emm_rca	cached_rca;
67 	union mio_emm_timing	taps;	/* otx2: MIO_EMM_TIMING */
68 	union mio_emm_timing	hs200_taps;
69 	union mio_emm_timing	hs400_taps;
70 	/* These are used to see if our tuning is still valid or not */
71 	enum bus_mode		last_mode;
72 	u32			last_clock;
73 	u32			block_len;
74 	u32			block_count;
75 	int			cmd_clk_skew;
76 	int			dat_clk_skew;
77 	uint			cmd_cnt;	/* otx: sample cmd in delay */
78 	uint			dat_cnt;	/* otx: sample data in delay */
79 	uint			drive;		/* Current drive */
80 	uint			slew;		/* clock skew */
81 	uint			cmd_out_hs200_delay;
82 	uint			data_out_hs200_delay;
83 	uint			cmd_out_hs400_delay;
84 	uint			data_out_hs400_delay;
85 	uint			clk_period;
86 	bool			valid:1;
87 	bool			is_acmd:1;
88 	bool			tuned:1;
89 	bool			hs200_tuned:1;
90 	bool			hs400_tuned:1;
91 	bool			is_1_8v:1;
92 	bool			is_3_3v:1;
93 	bool			is_ddr:1;
94 	bool			is_asim:1;
95 	bool			is_emul:1;
96 	bool			cd_inverted:1;
97 	bool			wp_inverted:1;
98 	bool			disable_ddr:1;
99 	bool			non_removable:1;
100 };
101 
102 struct octeontx_mmc_cr_mods {
103 	u8 ctype_xor;
104 	u8 rtype_xor;
105 };
106 
107 struct octeontx_mmc_cr {
108 	u8 c;
109 	u8 r;
110 };
111 
112 struct octeontx_sd_mods {
113 	struct octeontx_mmc_cr mmc;
114 	struct octeontx_mmc_cr sd;
115 	struct octeontx_mmc_cr sdacmd;
116 };
117 
118 /** Host controller data structure */
119 struct octeontx_mmc_host {
120 	struct		udevice *dev;
121 	void		*base_addr;
122 	struct octeontx_mmc_slot slots[OCTEONTX_MAX_MMC_SLOT + 1];
123 	pci_dev_t	pdev;
124 	u64		sys_freq;
125 	union mio_emm_cfg emm_cfg;
126 	u64		timing_taps;
127 	struct mmc	*last_mmc;	/** Last mmc used */
128 	ofnode		node;
129 	int		cur_slotid;
130 	int		last_slotid;
131 	int		max_width;
132 	uint		per_tap_delay;
133 	uint		num_slots;
134 	uint		dma_wait_delay;	/* Delay before polling DMA in usecs */
135 	bool		initialized:1;
136 	bool		timing_calibrated:1;
137 	bool		is_asim:1;
138 	bool		is_emul:1;
139 	bool		calibrate_glitch:1;
140 	bool		cond_clock_glitch:1;
141 	bool		tap_requires_noclk:1;
142 	bool		hs400_skew_needed:1;
143 };
144 
145 /*
146  * NOTE: This was copied from the Linux kernel.
147  *
148  * MMC status in R1, for native mode (SPI bits are different)
149  * Type
150  *	e:error bit
151  *	s:status bit
152  *	r:detected and set for the actual command response
153  *	x:detected and set during command execution. the host must poll
154  *	    the card by sending status command in order to read these bits.
155  * Clear condition
156  *	a:according to the card state
157  *	b:always related to the previous command. Reception of
158  *	    a valid command will clear it (with a delay of one command)
159  *	c:clear by read
160  */
161 #define R1_OUT_OF_RANGE		BIT(31)		/* er, c */
162 #define R1_ADDRESS_ERROR	BIT(30)		/* erx, c */
163 #define R1_BLOCK_LEN_ERROR	BIT(29)		/* er, c */
164 #define R1_ERASE_SEQ_ERROR	BIT(28)		/* er, c */
165 #define R1_ERASE_PARAM          BIT(27)		/* ex, c */
166 #define R1_WP_VIOLATION		BIT(26)		/* erx, c */
167 #define R1_CARD_IS_LOCKED	BIT(25)		/* sx, a */
168 #define R1_LOCK_UNLOCK_FAILED	BIT(24)		/* erx, c */
169 #define R1_COM_CRC_ERROR	BIT(23)		/* er, b */
170 /*#define R1_ILLEGAL_COMMAND	BIT(22)*/		/* er, b */
171 #define R1_CARD_ECC_FAILED	BIT(21)		/* ex, c */
172 #define R1_CC_ERROR		BIT(20)		/* erx, c */
173 #define R1_ERROR		BIT(19)		/* erx, c */
174 #define R1_UNDERRUN		BIT(18)		/* ex, c */
175 #define R1_OVERRUN		BIT(17)		/* ex, c */
176 #define R1_CID_CSD_OVERWRITE	BIT(16)		/* erx, c, CID/CSD overwrite */
177 #define R1_WP_ERASE_SKIP	BIT(15)		/* sx, c */
178 #define R1_CARD_ECC_DISABLED	BIT(14)		/* sx, a */
179 #define R1_ERASE_RESET		BIT(13)		/* sr, c */
180 #define R1_STATUS(x)		((x) & 0xFFFFE000)
181 #define R1_CURRENT_STATE(x)	(((x) & 0x00001E00) >> 9) /* sx, b (4 bits) */
182 #define R1_READY_FOR_DATA	BIT(8)		/* sx, a */
183 #define R1_SWITCH_ERROR		BIT(7)		/* sx, c */
184 
185 #define R1_BLOCK_READ_MASK	R1_OUT_OF_RANGE |	\
186 				R1_ADDRESS_ERROR |	\
187 				R1_BLOCK_LEN_ERROR |	\
188 				R1_CARD_IS_LOCKED |	\
189 				R1_COM_CRC_ERROR |	\
190 				R1_ILLEGAL_COMMAND |	\
191 				R1_CARD_ECC_FAILED |	\
192 				R1_CC_ERROR |		\
193 				R1_ERROR
194 #define R1_BLOCK_WRITE_MASK	R1_OUT_OF_RANGE |	\
195 				R1_ADDRESS_ERROR |	\
196 				R1_BLOCK_LEN_ERROR |	\
197 				R1_WP_VIOLATION |	\
198 				R1_CARD_IS_LOCKED |	\
199 				R1_COM_CRC_ERROR |	\
200 				R1_ILLEGAL_COMMAND |	\
201 				R1_CARD_ECC_FAILED |	\
202 				R1_CC_ERROR |		\
203 				R1_ERROR |		\
204 				R1_UNDERRUN |		\
205 				R1_OVERRUN
206 
207 #endif /* __OCTEONTX_HSMMC_H__ */
208