1 // PCI SD Host Controller Interface
3 // Copyright (C) 2014 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "block.h" // struct drive_s
8 #include "fw/paravirt.h" // runningOnQEMU
9 #include "malloc.h" // malloc_fseg
10 #include "output.h" // znprintf
11 #include "pci.h" // pci_config_readl
12 #include "pci_ids.h" // PCI_CLASS_SYSTEM_SDHCI
13 #include "pci_regs.h" // PCI_BASE_ADDRESS_0
14 #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
20 // SDHCI MMIO registers
55 u16 controller_version;
59 #define SC_ALL_SEND_CID ((2<<8) | 0x21)
60 #define SC_SEND_RELATIVE_ADDR ((3<<8) | 0x22)
61 #define SC_SELECT_DESELECT_CARD ((7<<8) | 0x23)
62 #define SC_READ_SINGLE ((17<<8) | 0x22)
63 #define SC_READ_MULTIPLE ((18<<8) | 0x22)
64 #define SC_WRITE_SINGLE ((24<<8) | 0x22)
65 #define SC_WRITE_MULTIPLE ((25<<8) | 0x22)
66 #define SC_APP_CMD ((55<<8) | 0x22)
67 #define SC_APP_SEND_OP_COND ((41<<8) | 0x22)
70 #define SI_CMD_COMPLETE (1<<0)
71 #define SI_TRANS_DONE (1<<1)
72 #define SI_WRITE_READY (1<<4)
73 #define SI_READ_READY (1<<5)
75 // SDHCI present_state flags
76 #define SP_CMD_INHIBIT (1<<0)
77 #define SP_DAT_INHIBIT (1<<1)
79 // SDHCI transfer_mode flags
80 #define ST_BLOCKCOUNT (1<<1)
81 #define ST_AUTO_CMD12 (1<<2)
82 #define ST_READ (1<<4)
83 #define ST_MULTIPLE (1<<5)
86 #define SR_OCR_CCS (1<<30)
89 #define SDHCI_PIO_TIMEOUT 1000 // XXX - these are just made up
90 #define SDHCI_TRANSFER_TIMEOUT 10000
92 // Internal 'struct drive_s' storage for a detected card
104 // Repeatedly read a u16 register until the specific value is found
106 waitw(u16 *reg, u16 mask, u16 value, u32 end)
110 if ((v & mask) == value)
112 if (timer_check(end)) {
120 // Send a command to the card.
122 sdcard_pio(struct sdhci_s *regs, int cmd, u32 *param)
124 u32 end = timer_calc(SDHCI_PIO_TIMEOUT);
125 u16 busyf = SP_CMD_INHIBIT | ((cmd & 0x03) == 0x03 ? SP_DAT_INHIBIT : 0);
126 int ret = waitw((u16*)®s->present_state, busyf, 0, end);
130 writel(®s->arg, *param);
131 writew(®s->cmd, cmd);
132 ret = waitw(®s->irq_status, SI_CMD_COMPLETE, SI_CMD_COMPLETE, end);
135 writew(®s->irq_status, SI_CMD_COMPLETE);
137 memcpy(param, regs->response, sizeof(regs->response));
141 // Send an "app specific" command to the card.
143 sdcard_pio_app(struct sdhci_s *regs, int cmd, u32 *param)
146 int ret = sdcard_pio(regs, SC_APP_CMD, aparam);
149 return sdcard_pio(regs, cmd, param);
152 // Send a command to the card which transfers data.
154 sdcard_pio_transfer(struct sddrive_s *drive, int cmd, u32 addr
155 , void *data, int count)
158 writel(&drive->regs->block_size, DISK_SECTOR_SIZE);
159 writew(&drive->regs->block_count, count); // XXX - SC_SET_BLOCK_COUNT?
160 int isread = cmd != SC_WRITE_SINGLE && cmd != SC_WRITE_MULTIPLE;
161 u16 tmode = ((count > 1 ? ST_MULTIPLE|ST_AUTO_CMD12|ST_BLOCKCOUNT : 0)
162 | (isread ? ST_READ : 0));
163 writew(&drive->regs->transfer_mode, tmode);
164 if (drive->card_type < SF_SDHC)
165 addr *= DISK_SECTOR_SIZE;
166 u32 param[4] = { addr };
167 int ret = sdcard_pio(drive->regs, cmd, param);
171 u32 end = timer_calc(SDHCI_TRANSFER_TIMEOUT);
172 u16 cbit = isread ? SI_READ_READY : SI_WRITE_READY;
174 ret = waitw(&drive->regs->irq_status, cbit, cbit, end);
177 writew(&drive->regs->irq_status, cbit);
179 for (i=0; i<DISK_SECTOR_SIZE/4; i++) {
181 *(u32*)data = readl(&drive->regs->data);
183 writel(&drive->regs->data, *(u32*)data);
188 // XXX - SC_STOP_TRANSMISSION?
189 ret = waitw(&drive->regs->irq_status, SI_TRANS_DONE, SI_TRANS_DONE, end);
192 writew(&drive->regs->irq_status, SI_TRANS_DONE);
196 // Read/write a block of data to/from the card.
198 sdcard_readwrite(struct disk_op_s *op, int iswrite)
200 struct sddrive_s *drive = container_of(
201 op->drive_gf, struct sddrive_s, drive);
202 int cmd = iswrite ? SC_WRITE_SINGLE : SC_READ_SINGLE;
204 cmd = iswrite ? SC_WRITE_MULTIPLE : SC_READ_MULTIPLE;
205 int ret = sdcard_pio_transfer(drive, cmd, op->lba, op->buf_fl, op->count);
207 return DISK_RET_EBADTRACK;
208 return DISK_RET_SUCCESS;
212 process_sdcard_op(struct disk_op_s *op)
216 switch (op->command) {
218 return sdcard_readwrite(op, 0);
220 return sdcard_readwrite(op, 1);
226 return DISK_RET_SUCCESS;
228 return DISK_RET_EPARAM;
233 /****************************************************************
235 ****************************************************************/
237 // Initialize an SD card
239 sdcard_card_setup(struct sdhci_s *regs)
241 // XXX - works on QEMU; probably wont on real hardware!
242 u32 param[4] = { 0x01 };
243 int ret = sdcard_pio_app(regs, SC_APP_SEND_OP_COND, param);
246 int card_type = (param[0] & SR_OCR_CCS) ? SF_SDHC : SF_SDSC;
248 ret = sdcard_pio(regs, SC_ALL_SEND_CID, param);
251 param[0] = 0x01 << 16;
252 ret = sdcard_pio(regs, SC_SEND_RELATIVE_ADDR, param);
255 u16 rca = param[0] >> 16;
256 param[0] = rca << 16;
257 ret = sdcard_pio(regs, SC_SELECT_DESELECT_CARD, param);
263 // Setup and configure an SD card controller
265 sdcard_controller_setup(void *data)
267 struct pci_device *pci = data;
269 wait_preempt(); // Avoid pci_config_readl when preempting
270 struct sdhci_s *regs = (void*)pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
271 pci_config_maskw(bdf, PCI_COMMAND, 0,
272 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
274 // Initialize controller
275 if (!runningOnQEMU())
276 // XXX - this init logic will probably only work on qemu!
278 writew(®s->irq_signal, 0);
279 writew(®s->irq_enable, 0xffff);
280 writew(®s->error_signal, 0);
281 writeb(®s->power_control, 0x0f);
282 writew(®s->clock_control, 0x0005);
285 int card_type = sdcard_card_setup(regs);
290 struct sddrive_s *drive = malloc_fseg(sizeof(*drive));
295 memset(drive, 0, sizeof(*drive));
296 drive->drive.type = DTYPE_SDCARD;
297 drive->drive.blksize = DISK_SECTOR_SIZE;
298 drive->drive.sectors = (u64)-1; // XXX
300 drive->card_type = card_type;
302 dprintf(1, "Found SD Card at %02x:%02x.%x\n"
303 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), pci_bdf_to_fn(bdf));
304 char *desc = znprintf(MAXDESCSIZE, "SD Card"); // XXX
305 boot_add_hd(&drive->drive, desc, bootprio_find_pci_device(pci));
314 struct pci_device *pci;
316 if (pci->class != PCI_CLASS_SYSTEM_SDHCI || pci->prog_if >= 2)
317 // Not an SDHCI controller following SDHCI spec
319 run_thread(sdcard_controller_setup, pci);