1 /*
2  * Copyright (c) 2019, Linaro Limited
3  * Copyright (c) 2019, Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef RPI3_SDHOST_H
9 #define	RPI3_SDHOST_H
10 
11 #include <drivers/mmc.h>
12 #include <stdint.h>
13 #include <platform_def.h>
14 
15 struct rpi3_sdhost_params {
16 	uintptr_t	reg_base;
17 	uint32_t	clk_rate;
18 	uint32_t	bus_width;
19 	uint32_t        flags;
20 	uint32_t	current_cmd;
21 	uint8_t		cmdbusy;
22 	uint8_t		mmc_app_cmd;
23 	uint32_t	ns_per_fifo_word;
24 
25 	uint32_t	sdcard_rca;
26 	uint32_t	gpio48_pinselect[6];
27 };
28 
29 void rpi3_sdhost_init(struct rpi3_sdhost_params *params,
30 		      struct mmc_device_info *mmc_dev_info);
31 void rpi3_sdhost_stop(void);
32 
33 /* Registers */
34 #define HC_COMMAND			0x00        /* Command and flags */
35 #define HC_ARGUMENT			0x04
36 #define HC_TIMEOUTCOUNTER		0x08
37 #define HC_CLOCKDIVISOR			0x0c
38 #define HC_RESPONSE_0			0x10
39 #define HC_RESPONSE_1			0x14
40 #define HC_RESPONSE_2			0x18
41 #define HC_RESPONSE_3			0x1c
42 #define HC_HOSTSTATUS			0x20
43 #define HC_POWER			0x30
44 #define HC_DEBUG			0x34
45 #define HC_HOSTCONFIG			0x38
46 #define HC_BLOCKSIZE			0x3c
47 #define HC_DATAPORT			0x40
48 #define HC_BLOCKCOUNT			0x50
49 
50 /* Flags for HC_COMMAND register */
51 #define HC_CMD_ENABLE			0x8000
52 #define HC_CMD_FAILED			0x4000
53 #define HC_CMD_BUSY			0x0800
54 #define HC_CMD_RESPONSE_NONE		0x0400
55 #define HC_CMD_RESPONSE_LONG		0x0200
56 #define HC_CMD_WRITE			0x0080
57 #define HC_CMD_READ			0x0040
58 #define HC_CMD_COMMAND_MASK		0x003f
59 
60 #define HC_CLOCKDIVISOR_MAXVAL		0x07ff
61 #define HC_CLOCKDIVISOR_PREFERVAL	0x027b
62 #define HC_CLOCKDIVISOR_SLOWVAL		0x0148
63 #define HC_CLOCKDIVISOR_STOPVAL		0x01fb
64 
65 /* Flags for HC_HOSTSTATUS register */
66 #define HC_HSTST_HAVEDATA		0x0001
67 #define HC_HSTST_ERROR_FIFO		0x0008
68 #define HC_HSTST_ERROR_CRC7		0x0010
69 #define HC_HSTST_ERROR_CRC16		0x0020
70 #define HC_HSTST_TIMEOUT_CMD		0x0040
71 #define HC_HSTST_TIMEOUT_DATA		0x0080
72 #define HC_HSTST_INT_BLOCK		0x0200
73 #define HC_HSTST_INT_BUSY		0x0400
74 
75 #define HC_HSTST_RESET			0xffff
76 
77 #define HC_HSTST_MASK_ERROR_DATA	(HC_HSTST_ERROR_FIFO | \
78 					 HC_HSTST_ERROR_CRC7 | \
79 					 HC_HSTST_ERROR_CRC16 | \
80 					 HC_HSTST_TIMEOUT_DATA)
81 
82 #define HC_HSTST_MASK_ERROR_ALL		(HC_HSTST_MASK_ERROR_DATA | \
83 					 HC_HSTST_TIMEOUT_CMD)
84 
85 /* Flags for HC_HOSTCONFIG register */
86 #define HC_HSTCF_INTBUS_WIDE		0x0002
87 #define HC_HSTCF_EXTBUS_4BIT		0x0004
88 #define HC_HSTCF_SLOW_CARD		0x0008
89 #define HC_HSTCF_INT_DATA		0x0010
90 #define HC_HSTCF_INT_BLOCK		0x0100
91 #define HC_HSTCF_INT_BUSY		0x0400
92 
93 /* Flags for HC_DEBUG register */
94 #define HC_DBG_FIFO_THRESH_WRITE_SHIFT	9
95 #define HC_DBG_FIFO_THRESH_READ_SHIFT	14
96 #define HC_DBG_FIFO_THRESH_MASK		0x001f
97 #define HC_DBG_FSM_MASK			0xf
98 #define HC_DBG_FSM_IDENTMODE		0x0
99 #define HC_DBG_FSM_DATAMODE		0x1
100 #define HC_DBG_FSM_READDATA		0x2
101 #define HC_DBG_FSM_WRITEDATA		0x3
102 #define HC_DBG_FSM_READWAIT		0x4
103 #define HC_DBG_FSM_READCRC		0x5
104 #define HC_DBG_FSM_WRITECRC		0x6
105 #define HC_DBG_FSM_WRITEWAIT1		0x7
106 #define HC_DBG_FSM_POWERDOWN		0x8
107 #define HC_DBG_FSM_POWERUP		0x9
108 #define HC_DBG_FSM_WRITESTART1		0xa
109 #define HC_DBG_FSM_WRITESTART2		0xb
110 #define HC_DBG_FSM_GENPULSES		0xc
111 #define HC_DBG_FSM_WRITEWAIT2		0xd
112 #define HC_DBG_FSM_STARTPOWDOWN		0xf
113 #define HC_DBG_FORCE_DATA_MODE		0x40000
114 
115 /* Settings */
116 #define HC_FIFO_SIZE			16
117 #define HC_FIFO_THRESH_READ		4
118 #define HC_FIFO_THRESH_WRITE		4
119 
120 #define HC_TIMEOUT_DEFAULT		0x00f00000
121 #define HC_TIMEOUT_IDLE			0x00a00000
122 
123 #endif  /* RPI3_SDHOST_H */
124