blob: 0617d24b1a37c8a2b800c0cc4f4047dc50e55c34 [file] [log] [blame]
Kevin O'Connor72691a52014-12-16 09:55:16 -05001// PCI SD Host Controller Interface
2//
3// Copyright (C) 2014 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "block.h" // struct drive_s
Kevin O'Connor72691a52014-12-16 09:55:16 -05008#include "malloc.h" // malloc_fseg
9#include "output.h" // znprintf
10#include "pci.h" // pci_config_readl
11#include "pci_ids.h" // PCI_CLASS_SYSTEM_SDHCI
12#include "pci_regs.h" // PCI_BASE_ADDRESS_0
Kevin O'Connor9f7b2362015-08-10 12:51:41 -040013#include "romfile.h" // romfile_findprefix
Kevin O'Connor72691a52014-12-16 09:55:16 -050014#include "stacks.h" // wait_preempt
15#include "std/disk.h" // DISK_RET_SUCCESS
16#include "string.h" // memset
17#include "util.h" // boot_add_hd
18#include "x86.h" // writel
19
20// SDHCI MMIO registers
21struct sdhci_s {
22 u32 sdma_addr;
23 u16 block_size;
24 u16 block_count;
25 u32 arg;
26 u16 transfer_mode;
27 u16 cmd;
28 u32 response[4];
29 u32 data;
30 u32 present_state;
31 u8 host_control;
32 u8 power_control;
33 u8 block_gap_control;
34 u8 wakeup_control;
35 u16 clock_control;
36 u8 timeout_control;
37 u8 software_reset;
38 u16 irq_status;
39 u16 error_irq_status;
40 u16 irq_enable;
41 u16 error_irq_enable;
42 u16 irq_signal;
43 u16 error_signal;
44 u16 auto_cmd12;
Kevin O'Connord5b78212015-08-11 09:34:19 -040045 u16 host_control2;
Kevin O'Connor67bb4552015-07-28 13:40:04 -040046 u32 cap_lo, cap_hi;
Kevin O'Connor72691a52014-12-16 09:55:16 -050047 u64 max_current;
48 u16 force_auto_cmd12;
49 u16 force_error;
50 u8 adma_error;
51 u8 pad_55[3];
52 u64 adma_addr;
53 u8 pad_60[156];
54 u16 slot_irq;
55 u16 controller_version;
56} PACKED;
57
58// SDHCI commands
Kevin O'Connorcfe326b2015-08-10 23:40:50 -040059#define SCB_R0 0x00 // No response
60#define SCB_R48 0x1a // Response R1 (no data), R5, R6, R7
61#define SCB_R48d 0x3a // Response R1 (with data)
62#define SCB_R48b 0x1b // Response R1b, R5b
63#define SCB_R48o 0x02 // Response R3, R4
64#define SCB_R136 0x09 // Response R2
65#define SC_GO_IDLE_STATE ((0<<8) | SCB_R0)
Kevin O'Connord501de12015-08-11 13:29:55 -040066#define SC_SEND_OP_COND ((1<<8) | SCB_R48o)
Kevin O'Connorcfe326b2015-08-10 23:40:50 -040067#define SC_ALL_SEND_CID ((2<<8) | SCB_R136)
68#define SC_SEND_RELATIVE_ADDR ((3<<8) | SCB_R48)
69#define SC_SELECT_DESELECT_CARD ((7<<8) | SCB_R48b)
70#define SC_SEND_IF_COND ((8<<8) | SCB_R48)
Kevin O'Connore344da42015-08-24 13:48:54 -040071#define SC_SEND_EXT_CSD ((8<<8) | SCB_R48d)
72#define SC_SEND_CSD ((9<<8) | SCB_R136)
Kevin O'Connorcfe326b2015-08-10 23:40:50 -040073#define SC_READ_SINGLE ((17<<8) | SCB_R48d)
74#define SC_READ_MULTIPLE ((18<<8) | SCB_R48d)
75#define SC_WRITE_SINGLE ((24<<8) | SCB_R48d)
76#define SC_WRITE_MULTIPLE ((25<<8) | SCB_R48d)
77#define SC_APP_CMD ((55<<8) | SCB_R48)
78#define SC_APP_SEND_OP_COND ((41<<8) | SCB_R48o)
Kevin O'Connor72691a52014-12-16 09:55:16 -050079
80// SDHCI irqs
81#define SI_CMD_COMPLETE (1<<0)
82#define SI_TRANS_DONE (1<<1)
83#define SI_WRITE_READY (1<<4)
84#define SI_READ_READY (1<<5)
Kevin O'Connorbf8f2662015-08-12 12:07:17 -040085#define SI_ERROR (1<<15)
Kevin O'Connor72691a52014-12-16 09:55:16 -050086
87// SDHCI present_state flags
Kevin O'Connor11198db2015-07-28 13:06:41 -040088#define SP_CMD_INHIBIT (1<<0)
89#define SP_DAT_INHIBIT (1<<1)
90#define SP_CARD_INSERTED (1<<16)
Kevin O'Connor72691a52014-12-16 09:55:16 -050091
92// SDHCI transfer_mode flags
93#define ST_BLOCKCOUNT (1<<1)
94#define ST_AUTO_CMD12 (1<<2)
95#define ST_READ (1<<4)
96#define ST_MULTIPLE (1<<5)
97
Kevin O'Connor67bb4552015-07-28 13:40:04 -040098// SDHCI capabilities flags
Kevin O'Connor86efbb62015-08-04 09:48:30 -040099#define SD_CAPLO_V33 (1<<24)
Kevin O'Connorad0c9ea2015-08-11 11:56:51 -0400100#define SD_CAPLO_V30 (1<<25)
101#define SD_CAPLO_V18 (1<<26)
Kevin O'Connor67bb4552015-07-28 13:40:04 -0400102#define SD_CAPLO_BASECLOCK_SHIFT 8
Kevin O'Connord5b78212015-08-11 09:34:19 -0400103#define SD_CAPLO_BASECLOCK_MASK 0xff
Kevin O'Connor67bb4552015-07-28 13:40:04 -0400104
105// SDHCI clock control flags
106#define SCC_INTERNAL_ENABLE (1<<0)
107#define SCC_STABLE (1<<1)
108#define SCC_CLOCK_ENABLE (1<<2)
Kevin O'Connord5b78212015-08-11 09:34:19 -0400109#define SCC_SDCLK_MASK 0xff
Kevin O'Connor67bb4552015-07-28 13:40:04 -0400110#define SCC_SDCLK_SHIFT 8
Kevin O'Connord5b78212015-08-11 09:34:19 -0400111#define SCC_SDCLK_HI_MASK 0x300
112#define SCC_SDCLK_HI_RSHIFT 2
Kevin O'Connor67bb4552015-07-28 13:40:04 -0400113
Kevin O'Connor86efbb62015-08-04 09:48:30 -0400114// SDHCI power control flags
115#define SPC_POWER_ON (1<<0)
Kevin O'Connorad0c9ea2015-08-11 11:56:51 -0400116#define SPC_V18 0x0a
117#define SPC_V30 0x0c
Kevin O'Connor86efbb62015-08-04 09:48:30 -0400118#define SPC_V33 0x0e
119
Kevin O'Connor9b73aa62015-08-12 12:02:16 -0400120// SDHCI software reset flags
121#define SRF_ALL 0x01
122#define SRF_CMD 0x02
123#define SRF_DATA 0x04
124
Kevin O'Connor72691a52014-12-16 09:55:16 -0500125// SDHCI result flags
Kevin O'Connor43312af2015-07-30 10:56:00 -0400126#define SR_OCR_CCS (1<<30)
127#define SR_OCR_NOTBUSY (1<<31)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500128
129// SDHCI timeouts
Kevin O'Connor4c6de9e2015-08-04 10:01:02 -0400130#define SDHCI_POWER_OFF_TIME 1
131#define SDHCI_POWER_ON_TIME 1
132#define SDHCI_CLOCK_ON_TIME 1 // 74 clock cycles
Kevin O'Connor43312af2015-07-30 10:56:00 -0400133#define SDHCI_POWERUP_TIMEOUT 1000
Kevin O'Connor18254b32015-08-12 12:58:23 -0400134#define SDHCI_PIO_TIMEOUT 1000 // XXX - this is just made up
Kevin O'Connor72691a52014-12-16 09:55:16 -0500135
136// Internal 'struct drive_s' storage for a detected card
137struct sddrive_s {
138 struct drive_s drive;
139 struct sdhci_s *regs;
140 int card_type;
141};
142
143// SD card types
Kevin O'Connor065b3412015-08-24 12:36:56 -0400144#define SF_MMC (1<<0)
145#define SF_HIGHCAPACITY (1<<1)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500146
Kevin O'Connor33e205e2015-08-11 13:03:22 -0400147// Repeatedly read a u16 register until any bit in a given mask is set
Kevin O'Connor72691a52014-12-16 09:55:16 -0500148static int
Kevin O'Connor33e205e2015-08-11 13:03:22 -0400149sdcard_waitw(u16 *reg, u16 mask)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500150{
Kevin O'Connor33e205e2015-08-11 13:03:22 -0400151 u32 end = timer_calc(SDHCI_PIO_TIMEOUT);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500152 for (;;) {
153 u16 v = readw(reg);
Kevin O'Connor33e205e2015-08-11 13:03:22 -0400154 if (v & mask)
155 return v;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500156 if (timer_check(end)) {
157 warn_timeout();
158 return -1;
159 }
160 yield();
161 }
162}
163
Kevin O'Connor9b73aa62015-08-12 12:02:16 -0400164// Send an sdhci reset
165static int
166sdcard_reset(struct sdhci_s *regs, int flags)
167{
168 writeb(&regs->software_reset, flags);
169 u32 end = timer_calc(SDHCI_PIO_TIMEOUT);
170 while (readb(&regs->software_reset))
171 if (timer_check(end)) {
172 warn_timeout();
173 return -1;
174 }
175 return 0;
176}
177
Kevin O'Connor72691a52014-12-16 09:55:16 -0500178// Send a command to the card.
179static int
180sdcard_pio(struct sdhci_s *regs, int cmd, u32 *param)
181{
Kevin O'Connor34cc2fd2015-08-11 12:59:53 -0400182 u32 state = readl(&regs->present_state);
183 dprintf(9, "sdcard_pio cmd %x %x %x\n", cmd, *param, state);
184 if ((state & SP_CMD_INHIBIT)
185 || ((cmd & 0x03) == 0x03 && state & SP_DAT_INHIBIT)) {
186 dprintf(1, "sdcard_pio not ready %x\n", state);
187 return -1;
188 }
Kevin O'Connor72691a52014-12-16 09:55:16 -0500189 // Send command
190 writel(&regs->arg, *param);
191 writew(&regs->cmd, cmd);
Kevin O'Connorbf8f2662015-08-12 12:07:17 -0400192 int ret = sdcard_waitw(&regs->irq_status, SI_ERROR|SI_CMD_COMPLETE);
Kevin O'Connor33e205e2015-08-11 13:03:22 -0400193 if (ret < 0)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500194 return ret;
Kevin O'Connorbf8f2662015-08-12 12:07:17 -0400195 if (ret & SI_ERROR) {
196 u16 err = readw(&regs->error_irq_status);
197 dprintf(3, "sdcard_pio command stop (code=%x)\n", err);
198 sdcard_reset(regs, SRF_CMD|SRF_DATA);
199 writew(&regs->error_irq_status, err);
200 return -1;
201 }
Kevin O'Connor72691a52014-12-16 09:55:16 -0500202 writew(&regs->irq_status, SI_CMD_COMPLETE);
203 // Read response
204 memcpy(param, regs->response, sizeof(regs->response));
Kevin O'Connor43312af2015-07-30 10:56:00 -0400205 dprintf(9, "sdcard cmd %x response %x %x %x %x\n"
206 , cmd, param[0], param[1], param[2], param[3]);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500207 return 0;
208}
209
210// Send an "app specific" command to the card.
211static int
212sdcard_pio_app(struct sdhci_s *regs, int cmd, u32 *param)
213{
214 u32 aparam[4] = {};
215 int ret = sdcard_pio(regs, SC_APP_CMD, aparam);
216 if (ret)
217 return ret;
218 return sdcard_pio(regs, cmd, param);
219}
220
221// Send a command to the card which transfers data.
222static int
223sdcard_pio_transfer(struct sddrive_s *drive, int cmd, u32 addr
224 , void *data, int count)
225{
226 // Send command
227 writel(&drive->regs->block_size, DISK_SECTOR_SIZE);
Kevin O'Connor18254b32015-08-12 12:58:23 -0400228 writew(&drive->regs->block_count, count);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500229 int isread = cmd != SC_WRITE_SINGLE && cmd != SC_WRITE_MULTIPLE;
230 u16 tmode = ((count > 1 ? ST_MULTIPLE|ST_AUTO_CMD12|ST_BLOCKCOUNT : 0)
231 | (isread ? ST_READ : 0));
232 writew(&drive->regs->transfer_mode, tmode);
Kevin O'Connor065b3412015-08-24 12:36:56 -0400233 if (!(drive->card_type & SF_HIGHCAPACITY))
Kevin O'Connor72691a52014-12-16 09:55:16 -0500234 addr *= DISK_SECTOR_SIZE;
235 u32 param[4] = { addr };
236 int ret = sdcard_pio(drive->regs, cmd, param);
237 if (ret)
238 return ret;
239 // Read/write data
Kevin O'Connor72691a52014-12-16 09:55:16 -0500240 u16 cbit = isread ? SI_READ_READY : SI_WRITE_READY;
241 while (count--) {
Kevin O'Connor33e205e2015-08-11 13:03:22 -0400242 ret = sdcard_waitw(&drive->regs->irq_status, cbit);
243 if (ret < 0)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500244 return ret;
245 writew(&drive->regs->irq_status, cbit);
246 int i;
247 for (i=0; i<DISK_SECTOR_SIZE/4; i++) {
248 if (isread)
249 *(u32*)data = readl(&drive->regs->data);
250 else
251 writel(&drive->regs->data, *(u32*)data);
252 data += 4;
253 }
254 }
255 // Complete command
Kevin O'Connor33e205e2015-08-11 13:03:22 -0400256 ret = sdcard_waitw(&drive->regs->irq_status, SI_TRANS_DONE);
257 if (ret < 0)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500258 return ret;
259 writew(&drive->regs->irq_status, SI_TRANS_DONE);
260 return 0;
261}
262
263// Read/write a block of data to/from the card.
264static int
265sdcard_readwrite(struct disk_op_s *op, int iswrite)
266{
267 struct sddrive_s *drive = container_of(
268 op->drive_gf, struct sddrive_s, drive);
269 int cmd = iswrite ? SC_WRITE_SINGLE : SC_READ_SINGLE;
270 if (op->count > 1)
271 cmd = iswrite ? SC_WRITE_MULTIPLE : SC_READ_MULTIPLE;
272 int ret = sdcard_pio_transfer(drive, cmd, op->lba, op->buf_fl, op->count);
273 if (ret)
274 return DISK_RET_EBADTRACK;
275 return DISK_RET_SUCCESS;
276}
277
Kevin O'Connorc7fa7892015-07-07 08:35:51 -0400278int
Kevin O'Connor17856452015-07-07 14:56:20 -0400279sdcard_process_op(struct disk_op_s *op)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500280{
281 if (!CONFIG_SDCARD)
282 return 0;
283 switch (op->command) {
284 case CMD_READ:
285 return sdcard_readwrite(op, 0);
286 case CMD_WRITE:
287 return sdcard_readwrite(op, 1);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500288 default:
Kevin O'Connor85c72c62015-07-07 09:01:52 -0400289 return default_process_op(op);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500290 }
291}
292
293
294/****************************************************************
295 * Setup
296 ****************************************************************/
297
Kevin O'Connor91a9f5b2015-08-18 11:22:25 -0400298static int
299sdcard_set_power(struct sdhci_s *regs)
300{
301 u32 cap = readl(&regs->cap_lo);
302 u32 volt, vbits;
303 if (cap & SD_CAPLO_V33) {
304 volt = 1<<20;
305 vbits = SPC_V33;
306 } else if (cap & SD_CAPLO_V30) {
307 volt = 1<<18;
308 vbits = SPC_V30;
309 } else if (cap & SD_CAPLO_V18) {
310 volt = 1<<7;
311 vbits = SPC_V18;
312 } else {
313 dprintf(1, "SD controller unsupported volt range (%x)\n", cap);
314 return -1;
315 }
316 writeb(&regs->power_control, 0);
317 msleep(SDHCI_POWER_OFF_TIME);
318 writeb(&regs->power_control, vbits | SPC_POWER_ON);
319 msleep(SDHCI_POWER_ON_TIME);
320 return volt;
321}
322
323static int
324sdcard_set_frequency(struct sdhci_s *regs, u32 khz)
325{
326 u16 ver = readw(&regs->controller_version);
327 u32 cap = readl(&regs->cap_lo);
328 u32 base_freq = (cap >> SD_CAPLO_BASECLOCK_SHIFT) & SD_CAPLO_BASECLOCK_MASK;
329 if (!base_freq) {
330 dprintf(1, "Unknown base frequency for SD controller\n");
331 return -1;
332 }
333 // Set new frequency
334 u32 divisor = DIV_ROUND_UP(base_freq * 1000, khz);
335 u16 creg;
336 if ((ver & 0xff) <= 0x01) {
337 divisor = divisor > 1 ? 1 << __fls(divisor-1) : 0;
338 creg = (divisor & SCC_SDCLK_MASK) << SCC_SDCLK_SHIFT;
339 } else {
340 divisor = DIV_ROUND_UP(divisor, 2);
341 creg = (divisor & SCC_SDCLK_MASK) << SCC_SDCLK_SHIFT;
342 creg |= (divisor & SCC_SDCLK_HI_MASK) >> SCC_SDCLK_HI_RSHIFT;
343 }
344 dprintf(3, "sdcard_set_frequency %d %d %x\n", base_freq, khz, creg);
345 writew(&regs->clock_control, 0);
346 writew(&regs->clock_control, creg | SCC_INTERNAL_ENABLE);
347 // Wait for frequency to become active
348 int ret = sdcard_waitw(&regs->clock_control, SCC_STABLE);
349 if (ret < 0)
350 return ret;
351 // Enable SD clock
352 writew(&regs->clock_control, creg | SCC_INTERNAL_ENABLE | SCC_CLOCK_ENABLE);
353 return 0;
354}
355
Kevin O'Connore344da42015-08-24 13:48:54 -0400356// Obtain the disk size of an SD card
357static int
358sdcard_get_capacity(struct sddrive_s *drive, u8 *csd)
359{
360 // Original MMC/SD card capacity formula
361 u16 C_SIZE = (csd[6] >> 6) | (csd[7] << 2) | ((csd[8] & 0x03) << 10);
362 u8 C_SIZE_MULT = (csd[4] >> 7) | ((csd[5] & 0x03) << 1);
363 u8 READ_BL_LEN = csd[9] & 0x0f;
364 u32 count = (C_SIZE+1) << (C_SIZE_MULT + 2 + READ_BL_LEN - 9);
365 // Check for newer encoding formats.
366 u8 CSD_STRUCTURE = csd[14] >> 6;
367 if ((drive->card_type & SF_MMC) && CSD_STRUCTURE >= 2) {
368 // Get capacity from EXT_CSD register
369 u8 ext_csd[512];
370 int ret = sdcard_pio_transfer(drive, SC_SEND_EXT_CSD, 0, ext_csd, 1);
371 if (ret)
372 return ret;
373 count = *(u32*)&ext_csd[212];
374 } else if (!(drive->card_type & SF_MMC) && CSD_STRUCTURE >= 1) {
375 // High capacity SD card
376 u32 C_SIZE2 = csd[5] | (csd[6] << 8) | ((csd[7] & 0x3f) << 16);
377 count = (C_SIZE2+1) << (19-9);
378 }
379 // Fill drive struct and return
380 drive->drive.blksize = DISK_SECTOR_SIZE;
381 drive->drive.sectors = count;
382 return 0;
383}
384
Kevin O'Connor72691a52014-12-16 09:55:16 -0500385// Initialize an SD card
386static int
Kevin O'Connorebe40ba2015-08-18 11:44:46 -0400387sdcard_card_setup(struct sddrive_s *drive, int volt, int prio)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500388{
Kevin O'Connorebe40ba2015-08-18 11:44:46 -0400389 struct sdhci_s *regs = drive->regs;
Kevin O'Connor17b811b2015-08-18 11:33:41 -0400390 // Set controller to initialization clock rate
391 int ret = sdcard_set_frequency(regs, 400);
392 if (ret)
393 return ret;
394 msleep(SDHCI_CLOCK_ON_TIME);
Kevin O'Connor43312af2015-07-30 10:56:00 -0400395 // Reset card
396 u32 param[4] = { };
Kevin O'Connor17b811b2015-08-18 11:33:41 -0400397 ret = sdcard_pio(regs, SC_GO_IDLE_STATE, param);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500398 if (ret)
399 return ret;
Kevin O'Connor43312af2015-07-30 10:56:00 -0400400 // Let card know SDHC/SDXC is supported and confirm voltage
Kevin O'Connor065b3412015-08-24 12:36:56 -0400401 u32 hcs = 0, vrange = (volt >= (1<<15) ? 0x100 : 0x200) | 0xaa;
Kevin O'Connor4b999222015-08-11 11:47:57 -0400402 param[0] = vrange;
Kevin O'Connor12441a82015-08-10 19:11:15 -0400403 ret = sdcard_pio(regs, SC_SEND_IF_COND, param);
Kevin O'Connord501de12015-08-11 13:29:55 -0400404 if (!ret && param[0] == vrange)
405 hcs = (1<<30);
Kevin O'Connor43312af2015-07-30 10:56:00 -0400406 // Verify SD card (instead of MMC or SDIO)
407 param[0] = 0x00;
408 ret = sdcard_pio_app(regs, SC_APP_SEND_OP_COND, param);
Kevin O'Connord501de12015-08-11 13:29:55 -0400409 if (ret) {
410 // Check for MMC card
411 param[0] = 0x00;
412 ret = sdcard_pio(regs, SC_SEND_OP_COND, param);
413 if (ret)
414 return ret;
Kevin O'Connor065b3412015-08-24 12:36:56 -0400415 drive->card_type |= SF_MMC;
Kevin O'Connord501de12015-08-11 13:29:55 -0400416 hcs = (1<<30);
417 }
Kevin O'Connor43312af2015-07-30 10:56:00 -0400418 // Init card
419 u32 end = timer_calc(SDHCI_POWERUP_TIMEOUT);
420 for (;;) {
Kevin O'Connord501de12015-08-11 13:29:55 -0400421 param[0] = hcs | volt; // high-capacity support and voltage level
Kevin O'Connor065b3412015-08-24 12:36:56 -0400422 if (drive->card_type & SF_MMC)
Kevin O'Connord501de12015-08-11 13:29:55 -0400423 ret = sdcard_pio(regs, SC_SEND_OP_COND, param);
424 else
425 ret = sdcard_pio_app(regs, SC_APP_SEND_OP_COND, param);
Kevin O'Connor43312af2015-07-30 10:56:00 -0400426 if (ret)
427 return ret;
428 if (param[0] & SR_OCR_NOTBUSY)
429 break;
430 if (timer_check(end)) {
431 warn_timeout();
432 return -1;
433 }
Kevin O'Connord501de12015-08-11 13:29:55 -0400434 msleep(5); // Avoid flooding log when debugging
Kevin O'Connor43312af2015-07-30 10:56:00 -0400435 }
Kevin O'Connor065b3412015-08-24 12:36:56 -0400436 drive->card_type |= (param[0] & SR_OCR_CCS) ? SF_HIGHCAPACITY : 0;
Kevin O'Connore344da42015-08-24 13:48:54 -0400437 // Select card (get cid, set rca, get csd, select card)
Kevin O'Connord501de12015-08-11 13:29:55 -0400438 param[0] = 0x00;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500439 ret = sdcard_pio(regs, SC_ALL_SEND_CID, param);
440 if (ret)
441 return ret;
Kevin O'Connor39a8ea52015-08-18 12:43:04 -0400442 u8 cid[16];
443 memcpy(cid, param, sizeof(cid));
Kevin O'Connor065b3412015-08-24 12:36:56 -0400444 param[0] = drive->card_type & SF_MMC ? 0x0001 << 16 : 0x00;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500445 ret = sdcard_pio(regs, SC_SEND_RELATIVE_ADDR, param);
446 if (ret)
447 return ret;
Kevin O'Connor065b3412015-08-24 12:36:56 -0400448 u16 rca = drive->card_type & SF_MMC ? 0x0001 : param[0] >> 16;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500449 param[0] = rca << 16;
Kevin O'Connore344da42015-08-24 13:48:54 -0400450 ret = sdcard_pio(regs, SC_SEND_CSD, param);
451 if (ret)
452 return ret;
453 u8 csd[16];
454 memcpy(csd, param, sizeof(csd));
455 param[0] = rca << 16;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500456 ret = sdcard_pio(regs, SC_SELECT_DESELECT_CARD, param);
457 if (ret)
458 return ret;
Kevin O'Connor17b811b2015-08-18 11:33:41 -0400459 // Set controller to data transfer clock rate
460 ret = sdcard_set_frequency(regs, 25000);
461 if (ret)
462 return ret;
Kevin O'Connorebe40ba2015-08-18 11:44:46 -0400463 // Register drive
Kevin O'Connore344da42015-08-24 13:48:54 -0400464 ret = sdcard_get_capacity(drive, csd);
465 if (ret)
466 return ret;
Kevin O'Connor39a8ea52015-08-18 12:43:04 -0400467 char pnm[7] = {};
468 int i;
469 for (i=0; i < (drive->card_type & SF_MMC ? 6 : 5); i++)
470 pnm[i] = cid[11-i];
Kevin O'Connore344da42015-08-24 13:48:54 -0400471 char *desc = znprintf(MAXDESCSIZE, "%s %s %dMiB"
Kevin O'Connor39a8ea52015-08-18 12:43:04 -0400472 , drive->card_type & SF_MMC ? "MMC drive" : "SD card"
Kevin O'Connore344da42015-08-24 13:48:54 -0400473 , pnm, (u32)(drive->drive.sectors >> 11));
Kevin O'Connor39a8ea52015-08-18 12:43:04 -0400474 dprintf(1, "Found sdcard at %p: %s\n", regs, desc);
Kevin O'Connorebe40ba2015-08-18 11:44:46 -0400475 boot_add_hd(&drive->drive, desc, prio);
476 return 0;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500477}
478
479// Setup and configure an SD card controller
480static void
Kevin O'Connor9f7b2362015-08-10 12:51:41 -0400481sdcard_controller_setup(struct sdhci_s *regs, int prio)
Kevin O'Connor72691a52014-12-16 09:55:16 -0500482{
Kevin O'Connor72691a52014-12-16 09:55:16 -0500483 // Initialize controller
Kevin O'Connor11198db2015-07-28 13:06:41 -0400484 u32 present_state = readl(&regs->present_state);
485 if (!(present_state & SP_CARD_INSERTED))
486 // No card present
487 return;
Kevin O'Connord5b78212015-08-11 09:34:19 -0400488 dprintf(3, "sdhci@%p ver=%x cap=%x %x\n", regs
489 , readw(&regs->controller_version)
490 , readl(&regs->cap_lo), readl(&regs->cap_hi));
Kevin O'Connor9b73aa62015-08-12 12:02:16 -0400491 sdcard_reset(regs, SRF_ALL);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500492 writew(&regs->irq_signal, 0);
Kevin O'Connorbf8f2662015-08-12 12:07:17 -0400493 writew(&regs->irq_enable, 0x01ff);
494 writew(&regs->irq_status, readw(&regs->irq_status));
Kevin O'Connor72691a52014-12-16 09:55:16 -0500495 writew(&regs->error_signal, 0);
Kevin O'Connor460e9aa2015-11-17 14:52:23 -0500496 writew(&regs->error_irq_enable, 0x01ff);
Kevin O'Connorbf8f2662015-08-12 12:07:17 -0400497 writew(&regs->error_irq_status, readw(&regs->error_irq_status));
Kevin O'Connor9637a672015-07-28 14:15:37 -0400498 writeb(&regs->timeout_control, 0x0e); // Set to max timeout
Kevin O'Connor4b999222015-08-11 11:47:57 -0400499 int volt = sdcard_set_power(regs);
500 if (volt < 0)
501 return;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500502
503 // Initialize card
Kevin O'Connor72691a52014-12-16 09:55:16 -0500504 struct sddrive_s *drive = malloc_fseg(sizeof(*drive));
505 if (!drive) {
506 warn_noalloc();
Kevin O'Connor263fb192015-08-11 11:59:34 -0400507 goto fail;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500508 }
509 memset(drive, 0, sizeof(*drive));
510 drive->drive.type = DTYPE_SDCARD;
Kevin O'Connor72691a52014-12-16 09:55:16 -0500511 drive->regs = regs;
Kevin O'Connorebe40ba2015-08-18 11:44:46 -0400512 int ret = sdcard_card_setup(drive, volt, prio);
513 if (ret) {
514 free(drive);
515 goto fail;
516 }
Kevin O'Connor263fb192015-08-11 11:59:34 -0400517 return;
518fail:
519 writeb(&regs->power_control, 0);
520 writew(&regs->clock_control, 0);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500521}
522
Kevin O'Connor9f7b2362015-08-10 12:51:41 -0400523static void
524sdcard_pci_setup(void *data)
525{
526 struct pci_device *pci = data;
527 wait_preempt(); // Avoid pci_config_readl when preempting
528 // XXX - bars dependent on slot index register in pci config space
529 u32 regs = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0);
Kyösti Mälkkicfc17bd2015-11-04 07:40:12 +0200530 regs &= PCI_BASE_ADDRESS_MEM_MASK;
Kevin O'Connor9f7b2362015-08-10 12:51:41 -0400531 pci_config_maskw(pci->bdf, PCI_COMMAND, 0,
532 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
533 int prio = bootprio_find_pci_device(pci);
534 sdcard_controller_setup((void*)regs, prio);
535}
536
537static void
538sdcard_romfile_setup(void *data)
539{
540 struct romfile_s *file = data;
541 int prio = bootprio_find_named_rom(file->name, 0);
542 u32 addr = romfile_loadint(file->name, 0);
543 dprintf(1, "Starting sdcard controller check at addr %x\n", addr);
544 sdcard_controller_setup((void*)addr, prio);
545}
546
Kevin O'Connor72691a52014-12-16 09:55:16 -0500547void
548sdcard_setup(void)
549{
550 if (!CONFIG_SDCARD)
551 return;
552
Kevin O'Connor9f7b2362015-08-10 12:51:41 -0400553 struct romfile_s *file = NULL;
554 for (;;) {
555 file = romfile_findprefix("etc/sdcard", file);
556 if (!file)
557 break;
558 run_thread(sdcard_romfile_setup, file);
559 }
560
Kevin O'Connor72691a52014-12-16 09:55:16 -0500561 struct pci_device *pci;
562 foreachpci(pci) {
563 if (pci->class != PCI_CLASS_SYSTEM_SDHCI || pci->prog_if >= 2)
564 // Not an SDHCI controller following SDHCI spec
565 continue;
Kevin O'Connor9f7b2362015-08-10 12:51:41 -0400566 run_thread(sdcard_pci_setup, pci);
Kevin O'Connor72691a52014-12-16 09:55:16 -0500567 }
568}