Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / hw / sdcard.c
1 // 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
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
19
20 // SDHCI MMIO registers
21 struct 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;
45     u8 pad_3E[2];
46     u64 cap;
47     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
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)
68
69 // SDHCI irqs
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)
74
75 // SDHCI present_state flags
76 #define SP_CMD_INHIBIT (1<<0)
77 #define SP_DAT_INHIBIT (1<<1)
78
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)
84
85 // SDHCI result flags
86 #define SR_OCR_CCS (1<<30)
87
88 // SDHCI timeouts
89 #define SDHCI_PIO_TIMEOUT      1000  // XXX - these are just made up
90 #define SDHCI_TRANSFER_TIMEOUT 10000
91
92 // Internal 'struct drive_s' storage for a detected card
93 struct sddrive_s {
94     struct drive_s drive;
95     struct sdhci_s *regs;
96     int card_type;
97 };
98
99 // SD card types
100 #define SF_MMC  0
101 #define SF_SDSC 1
102 #define SF_SDHC 2
103
104 // Repeatedly read a u16 register until the specific value is found
105 static int
106 waitw(u16 *reg, u16 mask, u16 value, u32 end)
107 {
108     for (;;) {
109         u16 v = readw(reg);
110         if ((v & mask) == value)
111             return 0;
112         if (timer_check(end)) {
113             warn_timeout();
114             return -1;
115         }
116         yield();
117     }
118 }
119
120 // Send a command to the card.
121 static int
122 sdcard_pio(struct sdhci_s *regs, int cmd, u32 *param)
123 {
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*)&regs->present_state, busyf, 0, end);
127     if (ret)
128         return ret;
129     // Send command
130     writel(&regs->arg, *param);
131     writew(&regs->cmd, cmd);
132     ret = waitw(&regs->irq_status, SI_CMD_COMPLETE, SI_CMD_COMPLETE, end);
133     if (ret)
134         return ret;
135     writew(&regs->irq_status, SI_CMD_COMPLETE);
136     // Read response
137     memcpy(param, regs->response, sizeof(regs->response));
138     return 0;
139 }
140
141 // Send an "app specific" command to the card.
142 static int
143 sdcard_pio_app(struct sdhci_s *regs, int cmd, u32 *param)
144 {
145     u32 aparam[4] = {};
146     int ret = sdcard_pio(regs, SC_APP_CMD, aparam);
147     if (ret)
148         return ret;
149     return sdcard_pio(regs, cmd, param);
150 }
151
152 // Send a command to the card which transfers data.
153 static int
154 sdcard_pio_transfer(struct sddrive_s *drive, int cmd, u32 addr
155                     , void *data, int count)
156 {
157     // Send command
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);
168     if (ret)
169         return ret;
170     // Read/write data
171     u32 end = timer_calc(SDHCI_TRANSFER_TIMEOUT);
172     u16 cbit = isread ? SI_READ_READY : SI_WRITE_READY;
173     while (count--) {
174         ret = waitw(&drive->regs->irq_status, cbit, cbit, end);
175         if (ret)
176             return ret;
177         writew(&drive->regs->irq_status, cbit);
178         int i;
179         for (i=0; i<DISK_SECTOR_SIZE/4; i++) {
180             if (isread)
181                 *(u32*)data = readl(&drive->regs->data);
182             else
183                 writel(&drive->regs->data, *(u32*)data);
184             data += 4;
185         }
186     }
187     // Complete command
188     // XXX - SC_STOP_TRANSMISSION?
189     ret = waitw(&drive->regs->irq_status, SI_TRANS_DONE, SI_TRANS_DONE, end);
190     if (ret)
191         return ret;
192     writew(&drive->regs->irq_status, SI_TRANS_DONE);
193     return 0;
194 }
195
196 // Read/write a block of data to/from the card.
197 static int
198 sdcard_readwrite(struct disk_op_s *op, int iswrite)
199 {
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;
203     if (op->count > 1)
204         cmd = iswrite ? SC_WRITE_MULTIPLE : SC_READ_MULTIPLE;
205     int ret = sdcard_pio_transfer(drive, cmd, op->lba, op->buf_fl, op->count);
206     if (ret)
207         return DISK_RET_EBADTRACK;
208     return DISK_RET_SUCCESS;
209 }
210
211 int VISIBLE32FLAT
212 process_sdcard_op(struct disk_op_s *op)
213 {
214     if (!CONFIG_SDCARD)
215         return 0;
216     switch (op->command) {
217     case CMD_READ:
218         return sdcard_readwrite(op, 0);
219     case CMD_WRITE:
220         return sdcard_readwrite(op, 1);
221     case CMD_FORMAT:
222     case CMD_RESET:
223     case CMD_ISREADY:
224     case CMD_VERIFY:
225     case CMD_SEEK:
226         return DISK_RET_SUCCESS;
227     default:
228         return DISK_RET_EPARAM;
229     }
230 }
231
232
233 /****************************************************************
234  * Setup
235  ****************************************************************/
236
237 // Initialize an SD card
238 static int
239 sdcard_card_setup(struct sdhci_s *regs)
240 {
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);
244     if (ret)
245         return ret;
246     int card_type = (param[0] & SR_OCR_CCS) ? SF_SDHC : SF_SDSC;
247     param[0] = 0x00;
248     ret = sdcard_pio(regs, SC_ALL_SEND_CID, param);
249     if (ret)
250         return ret;
251     param[0] = 0x01 << 16;
252     ret = sdcard_pio(regs, SC_SEND_RELATIVE_ADDR, param);
253     if (ret)
254         return ret;
255     u16 rca = param[0] >> 16;
256     param[0] = rca << 16;
257     ret = sdcard_pio(regs, SC_SELECT_DESELECT_CARD, param);
258     if (ret)
259         return ret;
260     return card_type;
261 }
262
263 // Setup and configure an SD card controller
264 static void
265 sdcard_controller_setup(void *data)
266 {
267     struct pci_device *pci = data;
268     u16 bdf = pci->bdf;
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);
273
274     // Initialize controller
275     if (!runningOnQEMU())
276         // XXX - this init logic will probably only work on qemu!
277         return;
278     writew(&regs->irq_signal, 0);
279     writew(&regs->irq_enable, 0xffff);
280     writew(&regs->error_signal, 0);
281     writeb(&regs->power_control, 0x0f);
282     writew(&regs->clock_control, 0x0005);
283
284     // Initialize card
285     int card_type = sdcard_card_setup(regs);
286     if (card_type < 0)
287         return;
288
289     // Register drive
290     struct sddrive_s *drive = malloc_fseg(sizeof(*drive));
291     if (!drive) {
292         warn_noalloc();
293         return;
294     }
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
299     drive->regs = regs;
300     drive->card_type = card_type;
301
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));
306 }
307
308 void
309 sdcard_setup(void)
310 {
311     if (!CONFIG_SDCARD)
312         return;
313
314     struct pci_device *pci;
315     foreachpci(pci) {
316         if (pci->class != PCI_CLASS_SYSTEM_SDHCI || pci->prog_if >= 2)
317             // Not an SDHCI controller following SDHCI spec
318             continue;
319         run_thread(sdcard_controller_setup, pci);
320     }
321 }