allwinner-sdhost.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * Allwinner (sun4i and above) SD Host Controller emulation
  3. *
  4. * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/log.h"
  21. #include "qemu/module.h"
  22. #include "qemu/units.h"
  23. #include "qapi/error.h"
  24. #include "system/blockdev.h"
  25. #include "system/dma.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/irq.h"
  28. #include "hw/sd/allwinner-sdhost.h"
  29. #include "migration/vmstate.h"
  30. #include "trace.h"
  31. #include "qom/object.h"
  32. #define TYPE_AW_SDHOST_BUS "allwinner-sdhost-bus"
  33. /* This is reusing the SDBus typedef from SD_BUS */
  34. DECLARE_INSTANCE_CHECKER(SDBus, AW_SDHOST_BUS,
  35. TYPE_AW_SDHOST_BUS)
  36. /* SD Host register offsets */
  37. enum {
  38. REG_SD_GCTL = 0x00, /* Global Control */
  39. REG_SD_CKCR = 0x04, /* Clock Control */
  40. REG_SD_TMOR = 0x08, /* Timeout */
  41. REG_SD_BWDR = 0x0C, /* Bus Width */
  42. REG_SD_BKSR = 0x10, /* Block Size */
  43. REG_SD_BYCR = 0x14, /* Byte Count */
  44. REG_SD_CMDR = 0x18, /* Command */
  45. REG_SD_CAGR = 0x1C, /* Command Argument */
  46. REG_SD_RESP0 = 0x20, /* Response Zero */
  47. REG_SD_RESP1 = 0x24, /* Response One */
  48. REG_SD_RESP2 = 0x28, /* Response Two */
  49. REG_SD_RESP3 = 0x2C, /* Response Three */
  50. REG_SD_IMKR = 0x30, /* Interrupt Mask */
  51. REG_SD_MISR = 0x34, /* Masked Interrupt Status */
  52. REG_SD_RISR = 0x38, /* Raw Interrupt Status */
  53. REG_SD_STAR = 0x3C, /* Status */
  54. REG_SD_FWLR = 0x40, /* FIFO Water Level */
  55. REG_SD_FUNS = 0x44, /* FIFO Function Select */
  56. REG_SD_DBGC = 0x50, /* Debug Enable */
  57. REG_SD_A12A = 0x58, /* Auto command 12 argument */
  58. REG_SD_NTSR = 0x5C, /* SD NewTiming Set */
  59. REG_SD_SDBG = 0x60, /* SD newTiming Set Debug */
  60. REG_SD_HWRST = 0x78, /* Hardware Reset Register */
  61. REG_SD_DMAC = 0x80, /* Internal DMA Controller Control */
  62. REG_SD_DLBA = 0x84, /* Descriptor List Base Address */
  63. REG_SD_IDST = 0x88, /* Internal DMA Controller Status */
  64. REG_SD_IDIE = 0x8C, /* Internal DMA Controller IRQ Enable */
  65. REG_SD_THLDC = 0x100, /* Card Threshold Control / FIFO (sun4i only)*/
  66. REG_SD_DSBD = 0x10C, /* eMMC DDR Start Bit Detection Control */
  67. REG_SD_RES_CRC = 0x110, /* Response CRC from card/eMMC */
  68. REG_SD_DATA7_CRC = 0x114, /* CRC Data 7 from card/eMMC */
  69. REG_SD_DATA6_CRC = 0x118, /* CRC Data 6 from card/eMMC */
  70. REG_SD_DATA5_CRC = 0x11C, /* CRC Data 5 from card/eMMC */
  71. REG_SD_DATA4_CRC = 0x120, /* CRC Data 4 from card/eMMC */
  72. REG_SD_DATA3_CRC = 0x124, /* CRC Data 3 from card/eMMC */
  73. REG_SD_DATA2_CRC = 0x128, /* CRC Data 2 from card/eMMC */
  74. REG_SD_DATA1_CRC = 0x12C, /* CRC Data 1 from card/eMMC */
  75. REG_SD_DATA0_CRC = 0x130, /* CRC Data 0 from card/eMMC */
  76. REG_SD_CRC_STA = 0x134, /* CRC status from card/eMMC during write */
  77. REG_SD_SAMP_DL = 0x144, /* Sample Delay Control (sun50i-a64) */
  78. REG_SD_FIFO = 0x200, /* Read/Write FIFO */
  79. };
  80. /* SD Host register flags */
  81. enum {
  82. SD_GCTL_FIFO_AC_MOD = (1 << 31),
  83. SD_GCTL_DDR_MOD_SEL = (1 << 10),
  84. SD_GCTL_CD_DBC_ENB = (1 << 8),
  85. SD_GCTL_DMA_ENB = (1 << 5),
  86. SD_GCTL_INT_ENB = (1 << 4),
  87. SD_GCTL_DMA_RST = (1 << 2),
  88. SD_GCTL_FIFO_RST = (1 << 1),
  89. SD_GCTL_SOFT_RST = (1 << 0),
  90. };
  91. enum {
  92. SD_CMDR_LOAD = (1 << 31),
  93. SD_CMDR_CLKCHANGE = (1 << 21),
  94. SD_CMDR_WRITE = (1 << 10),
  95. SD_CMDR_AUTOSTOP = (1 << 12),
  96. SD_CMDR_DATA = (1 << 9),
  97. SD_CMDR_RESPONSE_LONG = (1 << 7),
  98. SD_CMDR_RESPONSE = (1 << 6),
  99. SD_CMDR_CMDID_MASK = (0x3f),
  100. };
  101. enum {
  102. SD_RISR_CARD_REMOVE = (1 << 31),
  103. SD_RISR_CARD_INSERT = (1 << 30),
  104. SD_RISR_SDIO_INTR = (1 << 16),
  105. SD_RISR_AUTOCMD_DONE = (1 << 14),
  106. SD_RISR_DATA_COMPLETE = (1 << 3),
  107. SD_RISR_CMD_COMPLETE = (1 << 2),
  108. SD_RISR_NO_RESPONSE = (1 << 1),
  109. };
  110. enum {
  111. SD_STAR_FIFO_EMPTY = (1 << 2),
  112. SD_STAR_CARD_PRESENT = (1 << 8),
  113. SD_STAR_FIFO_LEVEL_1 = (1 << 17),
  114. };
  115. enum {
  116. SD_IDST_INT_SUMMARY = (1 << 8),
  117. SD_IDST_RECEIVE_IRQ = (1 << 1),
  118. SD_IDST_TRANSMIT_IRQ = (1 << 0),
  119. SD_IDST_IRQ_MASK = (1 << 1) | (1 << 0) | (1 << 8),
  120. SD_IDST_WR_MASK = (0x3ff),
  121. };
  122. /* SD Host register reset values */
  123. enum {
  124. REG_SD_GCTL_RST = 0x00000300,
  125. REG_SD_CKCR_RST = 0x0,
  126. REG_SD_TMOR_RST = 0xFFFFFF40,
  127. REG_SD_BWDR_RST = 0x0,
  128. REG_SD_BKSR_RST = 0x00000200,
  129. REG_SD_BYCR_RST = 0x00000200,
  130. REG_SD_CMDR_RST = 0x0,
  131. REG_SD_CAGR_RST = 0x0,
  132. REG_SD_RESP_RST = 0x0,
  133. REG_SD_IMKR_RST = 0x0,
  134. REG_SD_MISR_RST = 0x0,
  135. REG_SD_RISR_RST = 0x0,
  136. REG_SD_STAR_RST = 0x00000100,
  137. REG_SD_FWLR_RST = 0x000F0000,
  138. REG_SD_FUNS_RST = 0x0,
  139. REG_SD_DBGC_RST = 0x0,
  140. REG_SD_A12A_RST = 0x0000FFFF,
  141. REG_SD_NTSR_RST = 0x00000001,
  142. REG_SD_SDBG_RST = 0x0,
  143. REG_SD_HWRST_RST = 0x00000001,
  144. REG_SD_DMAC_RST = 0x0,
  145. REG_SD_DLBA_RST = 0x0,
  146. REG_SD_IDST_RST = 0x0,
  147. REG_SD_IDIE_RST = 0x0,
  148. REG_SD_THLDC_RST = 0x0,
  149. REG_SD_DSBD_RST = 0x0,
  150. REG_SD_RES_CRC_RST = 0x0,
  151. REG_SD_DATA_CRC_RST = 0x0,
  152. REG_SD_CRC_STA_RST = 0x0,
  153. REG_SD_SAMPLE_DL_RST = 0x00002000,
  154. REG_SD_FIFO_RST = 0x0,
  155. };
  156. /* Data transfer descriptor for DMA */
  157. typedef struct TransferDescriptor {
  158. uint32_t status; /* Status flags */
  159. uint32_t size; /* Data buffer size */
  160. uint32_t addr; /* Data buffer address */
  161. uint32_t next; /* Physical address of next descriptor */
  162. } TransferDescriptor;
  163. /* Data transfer descriptor flags */
  164. enum {
  165. DESC_STATUS_HOLD = (1 << 31), /* Set when descriptor is in use by DMA */
  166. DESC_STATUS_ERROR = (1 << 30), /* Set when DMA transfer error occurred */
  167. DESC_STATUS_CHAIN = (1 << 4), /* Indicates chained descriptor. */
  168. DESC_STATUS_FIRST = (1 << 3), /* Set on the first descriptor */
  169. DESC_STATUS_LAST = (1 << 2), /* Set on the last descriptor */
  170. DESC_STATUS_NOIRQ = (1 << 1), /* Skip raising interrupt after transfer */
  171. DESC_SIZE_MASK = (0xfffffffc)
  172. };
  173. static void allwinner_sdhost_update_irq(AwSdHostState *s)
  174. {
  175. uint32_t irq;
  176. if (s->global_ctl & SD_GCTL_INT_ENB) {
  177. irq = s->irq_status & s->irq_mask;
  178. } else {
  179. irq = 0;
  180. }
  181. trace_allwinner_sdhost_update_irq(irq);
  182. qemu_set_irq(s->irq, !!irq);
  183. }
  184. static void allwinner_sdhost_update_transfer_cnt(AwSdHostState *s,
  185. uint32_t bytes)
  186. {
  187. if (s->transfer_cnt > bytes) {
  188. s->transfer_cnt -= bytes;
  189. } else {
  190. s->transfer_cnt = 0;
  191. }
  192. if (!s->transfer_cnt) {
  193. s->irq_status |= SD_RISR_DATA_COMPLETE;
  194. }
  195. }
  196. static void allwinner_sdhost_set_inserted(DeviceState *dev, bool inserted)
  197. {
  198. AwSdHostState *s = AW_SDHOST(dev);
  199. trace_allwinner_sdhost_set_inserted(inserted);
  200. if (inserted) {
  201. s->irq_status |= SD_RISR_CARD_INSERT;
  202. s->irq_status &= ~SD_RISR_CARD_REMOVE;
  203. s->status |= SD_STAR_CARD_PRESENT;
  204. } else {
  205. s->irq_status &= ~SD_RISR_CARD_INSERT;
  206. s->irq_status |= SD_RISR_CARD_REMOVE;
  207. s->status &= ~SD_STAR_CARD_PRESENT;
  208. }
  209. allwinner_sdhost_update_irq(s);
  210. }
  211. static void allwinner_sdhost_send_command(AwSdHostState *s)
  212. {
  213. SDRequest request;
  214. uint8_t resp[16];
  215. int rlen;
  216. /* Auto clear load flag */
  217. s->command &= ~SD_CMDR_LOAD;
  218. /* Clock change does not actually interact with the SD bus */
  219. if (!(s->command & SD_CMDR_CLKCHANGE)) {
  220. /* Prepare request */
  221. request.cmd = s->command & SD_CMDR_CMDID_MASK;
  222. request.arg = s->command_arg;
  223. /* Send request to SD bus */
  224. rlen = sdbus_do_command(&s->sdbus, &request, resp);
  225. if (rlen < 0) {
  226. goto error;
  227. }
  228. /* If the command has a response, store it in the response registers */
  229. if ((s->command & SD_CMDR_RESPONSE)) {
  230. if (rlen == 4 && !(s->command & SD_CMDR_RESPONSE_LONG)) {
  231. s->response[0] = ldl_be_p(&resp[0]);
  232. s->response[1] = s->response[2] = s->response[3] = 0;
  233. } else if (rlen == 16 && (s->command & SD_CMDR_RESPONSE_LONG)) {
  234. s->response[0] = ldl_be_p(&resp[12]);
  235. s->response[1] = ldl_be_p(&resp[8]);
  236. s->response[2] = ldl_be_p(&resp[4]);
  237. s->response[3] = ldl_be_p(&resp[0]);
  238. } else {
  239. goto error;
  240. }
  241. }
  242. }
  243. /* Set interrupt status bits */
  244. s->irq_status |= SD_RISR_CMD_COMPLETE;
  245. return;
  246. error:
  247. s->irq_status |= SD_RISR_NO_RESPONSE;
  248. }
  249. static void allwinner_sdhost_auto_stop(AwSdHostState *s)
  250. {
  251. /*
  252. * The stop command (CMD12) ensures the SD bus
  253. * returns to the transfer state.
  254. */
  255. if ((s->command & SD_CMDR_AUTOSTOP) && (s->transfer_cnt == 0)) {
  256. /* First save current command registers */
  257. uint32_t saved_cmd = s->command;
  258. uint32_t saved_arg = s->command_arg;
  259. /* Prepare stop command (CMD12) */
  260. s->command &= ~SD_CMDR_CMDID_MASK;
  261. s->command |= 12; /* CMD12 */
  262. s->command_arg = 0;
  263. /* Put the command on SD bus */
  264. allwinner_sdhost_send_command(s);
  265. /* Restore command values */
  266. s->command = saved_cmd;
  267. s->command_arg = saved_arg;
  268. /* Set IRQ status bit for automatic stop done */
  269. s->irq_status |= SD_RISR_AUTOCMD_DONE;
  270. }
  271. }
  272. static void read_descriptor(AwSdHostState *s, hwaddr desc_addr,
  273. TransferDescriptor *desc)
  274. {
  275. uint32_t desc_words[4];
  276. dma_memory_read(&s->dma_as, desc_addr, &desc_words, sizeof(desc_words),
  277. MEMTXATTRS_UNSPECIFIED);
  278. desc->status = le32_to_cpu(desc_words[0]);
  279. desc->size = le32_to_cpu(desc_words[1]);
  280. desc->addr = le32_to_cpu(desc_words[2]);
  281. desc->next = le32_to_cpu(desc_words[3]);
  282. }
  283. static void write_descriptor(AwSdHostState *s, hwaddr desc_addr,
  284. const TransferDescriptor *desc)
  285. {
  286. uint32_t desc_words[4];
  287. desc_words[0] = cpu_to_le32(desc->status);
  288. desc_words[1] = cpu_to_le32(desc->size);
  289. desc_words[2] = cpu_to_le32(desc->addr);
  290. desc_words[3] = cpu_to_le32(desc->next);
  291. dma_memory_write(&s->dma_as, desc_addr, &desc_words, sizeof(desc_words),
  292. MEMTXATTRS_UNSPECIFIED);
  293. }
  294. static uint32_t allwinner_sdhost_process_desc(AwSdHostState *s,
  295. hwaddr desc_addr,
  296. TransferDescriptor *desc,
  297. bool is_write, uint32_t max_bytes)
  298. {
  299. AwSdHostClass *klass = AW_SDHOST_GET_CLASS(s);
  300. uint32_t num_done = 0;
  301. uint32_t num_bytes = max_bytes;
  302. uint8_t buf[1024];
  303. read_descriptor(s, desc_addr, desc);
  304. if (desc->size == 0) {
  305. desc->size = klass->max_desc_size;
  306. } else if (desc->size > klass->max_desc_size) {
  307. qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA descriptor buffer size "
  308. " is out-of-bounds: %" PRIu32 " > %zu",
  309. __func__, desc->size, klass->max_desc_size);
  310. desc->size = klass->max_desc_size;
  311. }
  312. if (desc->size < num_bytes) {
  313. num_bytes = desc->size;
  314. }
  315. trace_allwinner_sdhost_process_desc(desc_addr, desc->size,
  316. is_write, max_bytes);
  317. while (num_done < num_bytes) {
  318. /* Try to completely fill the local buffer */
  319. uint32_t buf_bytes = num_bytes - num_done;
  320. if (buf_bytes > sizeof(buf)) {
  321. buf_bytes = sizeof(buf);
  322. }
  323. /* Write to SD bus */
  324. if (is_write) {
  325. dma_memory_read(&s->dma_as,
  326. (desc->addr & DESC_SIZE_MASK) + num_done, buf,
  327. buf_bytes, MEMTXATTRS_UNSPECIFIED);
  328. sdbus_write_data(&s->sdbus, buf, buf_bytes);
  329. /* Read from SD bus */
  330. } else {
  331. sdbus_read_data(&s->sdbus, buf, buf_bytes);
  332. dma_memory_write(&s->dma_as,
  333. (desc->addr & DESC_SIZE_MASK) + num_done, buf,
  334. buf_bytes, MEMTXATTRS_UNSPECIFIED);
  335. }
  336. num_done += buf_bytes;
  337. }
  338. /* Clear hold flag and flush descriptor */
  339. desc->status &= ~DESC_STATUS_HOLD;
  340. write_descriptor(s, desc_addr, desc);
  341. return num_done;
  342. }
  343. static void allwinner_sdhost_dma(AwSdHostState *s)
  344. {
  345. TransferDescriptor desc;
  346. hwaddr desc_addr = s->desc_base;
  347. bool is_write = (s->command & SD_CMDR_WRITE);
  348. uint32_t bytes_done = 0;
  349. /* Check if DMA can be performed */
  350. if (s->byte_count == 0 || s->block_size == 0 ||
  351. !(s->global_ctl & SD_GCTL_DMA_ENB)) {
  352. return;
  353. }
  354. /*
  355. * For read operations, data must be available on the SD bus
  356. * If not, it is an error and we should not act at all
  357. */
  358. if (!is_write && !sdbus_data_ready(&s->sdbus)) {
  359. return;
  360. }
  361. /* Process the DMA descriptors until all data is copied */
  362. while (s->byte_count > 0) {
  363. bytes_done = allwinner_sdhost_process_desc(s, desc_addr, &desc,
  364. is_write, s->byte_count);
  365. allwinner_sdhost_update_transfer_cnt(s, bytes_done);
  366. if (bytes_done <= s->byte_count) {
  367. s->byte_count -= bytes_done;
  368. } else {
  369. s->byte_count = 0;
  370. }
  371. if (desc.status & DESC_STATUS_LAST) {
  372. break;
  373. } else {
  374. desc_addr = desc.next;
  375. }
  376. }
  377. /* Raise IRQ to signal DMA is completed */
  378. s->irq_status |= SD_RISR_DATA_COMPLETE | SD_RISR_SDIO_INTR;
  379. /* Update DMAC bits */
  380. s->dmac_status |= SD_IDST_INT_SUMMARY;
  381. if (is_write) {
  382. s->dmac_status |= SD_IDST_TRANSMIT_IRQ;
  383. } else {
  384. s->dmac_status |= SD_IDST_RECEIVE_IRQ;
  385. }
  386. }
  387. static uint32_t allwinner_sdhost_fifo_read(AwSdHostState *s)
  388. {
  389. uint32_t res = 0;
  390. if (sdbus_data_ready(&s->sdbus)) {
  391. sdbus_read_data(&s->sdbus, &res, sizeof(uint32_t));
  392. le32_to_cpus(&res);
  393. allwinner_sdhost_update_transfer_cnt(s, sizeof(uint32_t));
  394. allwinner_sdhost_auto_stop(s);
  395. allwinner_sdhost_update_irq(s);
  396. } else {
  397. qemu_log_mask(LOG_GUEST_ERROR, "%s: no data ready on SD bus\n",
  398. __func__);
  399. }
  400. return res;
  401. }
  402. static uint64_t allwinner_sdhost_read(void *opaque, hwaddr offset,
  403. unsigned size)
  404. {
  405. AwSdHostState *s = AW_SDHOST(opaque);
  406. AwSdHostClass *sc = AW_SDHOST_GET_CLASS(s);
  407. bool out_of_bounds = false;
  408. uint32_t res = 0;
  409. switch (offset) {
  410. case REG_SD_GCTL: /* Global Control */
  411. res = s->global_ctl;
  412. break;
  413. case REG_SD_CKCR: /* Clock Control */
  414. res = s->clock_ctl;
  415. break;
  416. case REG_SD_TMOR: /* Timeout */
  417. res = s->timeout;
  418. break;
  419. case REG_SD_BWDR: /* Bus Width */
  420. res = s->bus_width;
  421. break;
  422. case REG_SD_BKSR: /* Block Size */
  423. res = s->block_size;
  424. break;
  425. case REG_SD_BYCR: /* Byte Count */
  426. res = s->byte_count;
  427. break;
  428. case REG_SD_CMDR: /* Command */
  429. res = s->command;
  430. break;
  431. case REG_SD_CAGR: /* Command Argument */
  432. res = s->command_arg;
  433. break;
  434. case REG_SD_RESP0: /* Response Zero */
  435. res = s->response[0];
  436. break;
  437. case REG_SD_RESP1: /* Response One */
  438. res = s->response[1];
  439. break;
  440. case REG_SD_RESP2: /* Response Two */
  441. res = s->response[2];
  442. break;
  443. case REG_SD_RESP3: /* Response Three */
  444. res = s->response[3];
  445. break;
  446. case REG_SD_IMKR: /* Interrupt Mask */
  447. res = s->irq_mask;
  448. break;
  449. case REG_SD_MISR: /* Masked Interrupt Status */
  450. res = s->irq_status & s->irq_mask;
  451. break;
  452. case REG_SD_RISR: /* Raw Interrupt Status */
  453. res = s->irq_status;
  454. break;
  455. case REG_SD_STAR: /* Status */
  456. res = s->status;
  457. if (sdbus_data_ready(&s->sdbus)) {
  458. res |= SD_STAR_FIFO_LEVEL_1;
  459. } else {
  460. res |= SD_STAR_FIFO_EMPTY;
  461. }
  462. break;
  463. case REG_SD_FWLR: /* FIFO Water Level */
  464. res = s->fifo_wlevel;
  465. break;
  466. case REG_SD_FUNS: /* FIFO Function Select */
  467. res = s->fifo_func_sel;
  468. break;
  469. case REG_SD_DBGC: /* Debug Enable */
  470. res = s->debug_enable;
  471. break;
  472. case REG_SD_A12A: /* Auto command 12 argument */
  473. res = s->auto12_arg;
  474. break;
  475. case REG_SD_NTSR: /* SD NewTiming Set */
  476. res = s->newtiming_set;
  477. break;
  478. case REG_SD_SDBG: /* SD newTiming Set Debug */
  479. res = s->newtiming_debug;
  480. break;
  481. case REG_SD_HWRST: /* Hardware Reset Register */
  482. res = s->hardware_rst;
  483. break;
  484. case REG_SD_DMAC: /* Internal DMA Controller Control */
  485. res = s->dmac;
  486. break;
  487. case REG_SD_DLBA: /* Descriptor List Base Address */
  488. res = s->desc_base;
  489. break;
  490. case REG_SD_IDST: /* Internal DMA Controller Status */
  491. res = s->dmac_status;
  492. break;
  493. case REG_SD_IDIE: /* Internal DMA Controller Interrupt Enable */
  494. res = s->dmac_irq;
  495. break;
  496. case REG_SD_THLDC: /* Card Threshold Control or FIFO register (sun4i) */
  497. if (sc->is_sun4i) {
  498. res = allwinner_sdhost_fifo_read(s);
  499. } else {
  500. res = s->card_threshold;
  501. }
  502. break;
  503. case REG_SD_DSBD: /* eMMC DDR Start Bit Detection Control */
  504. res = s->startbit_detect;
  505. break;
  506. case REG_SD_RES_CRC: /* Response CRC from card/eMMC */
  507. res = s->response_crc;
  508. break;
  509. case REG_SD_DATA7_CRC: /* CRC Data 7 from card/eMMC */
  510. case REG_SD_DATA6_CRC: /* CRC Data 6 from card/eMMC */
  511. case REG_SD_DATA5_CRC: /* CRC Data 5 from card/eMMC */
  512. case REG_SD_DATA4_CRC: /* CRC Data 4 from card/eMMC */
  513. case REG_SD_DATA3_CRC: /* CRC Data 3 from card/eMMC */
  514. case REG_SD_DATA2_CRC: /* CRC Data 2 from card/eMMC */
  515. case REG_SD_DATA1_CRC: /* CRC Data 1 from card/eMMC */
  516. case REG_SD_DATA0_CRC: /* CRC Data 0 from card/eMMC */
  517. res = s->data_crc[((offset - REG_SD_DATA7_CRC) / sizeof(uint32_t))];
  518. break;
  519. case REG_SD_CRC_STA: /* CRC status from card/eMMC in write operation */
  520. res = s->status_crc;
  521. break;
  522. case REG_SD_FIFO: /* Read/Write FIFO */
  523. res = allwinner_sdhost_fifo_read(s);
  524. break;
  525. case REG_SD_SAMP_DL: /* Sample Delay */
  526. if (sc->can_calibrate) {
  527. res = s->sample_delay;
  528. } else {
  529. out_of_bounds = true;
  530. }
  531. break;
  532. default:
  533. out_of_bounds = true;
  534. res = 0;
  535. break;
  536. }
  537. if (out_of_bounds) {
  538. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset %"
  539. HWADDR_PRIx"\n", __func__, offset);
  540. }
  541. trace_allwinner_sdhost_read(offset, res, size);
  542. return res;
  543. }
  544. static void allwinner_sdhost_fifo_write(AwSdHostState *s, uint64_t value)
  545. {
  546. uint32_t u32 = cpu_to_le32(value);
  547. sdbus_write_data(&s->sdbus, &u32, sizeof(u32));
  548. allwinner_sdhost_update_transfer_cnt(s, sizeof(u32));
  549. allwinner_sdhost_auto_stop(s);
  550. allwinner_sdhost_update_irq(s);
  551. }
  552. static void allwinner_sdhost_write(void *opaque, hwaddr offset,
  553. uint64_t value, unsigned size)
  554. {
  555. AwSdHostState *s = AW_SDHOST(opaque);
  556. AwSdHostClass *sc = AW_SDHOST_GET_CLASS(s);
  557. bool out_of_bounds = false;
  558. trace_allwinner_sdhost_write(offset, value, size);
  559. switch (offset) {
  560. case REG_SD_GCTL: /* Global Control */
  561. s->global_ctl = value;
  562. s->global_ctl &= ~(SD_GCTL_DMA_RST | SD_GCTL_FIFO_RST |
  563. SD_GCTL_SOFT_RST);
  564. allwinner_sdhost_update_irq(s);
  565. break;
  566. case REG_SD_CKCR: /* Clock Control */
  567. s->clock_ctl = value;
  568. break;
  569. case REG_SD_TMOR: /* Timeout */
  570. s->timeout = value;
  571. break;
  572. case REG_SD_BWDR: /* Bus Width */
  573. s->bus_width = value;
  574. break;
  575. case REG_SD_BKSR: /* Block Size */
  576. s->block_size = value;
  577. break;
  578. case REG_SD_BYCR: /* Byte Count */
  579. s->byte_count = value;
  580. s->transfer_cnt = value;
  581. break;
  582. case REG_SD_CMDR: /* Command */
  583. s->command = value;
  584. if (value & SD_CMDR_LOAD) {
  585. allwinner_sdhost_send_command(s);
  586. allwinner_sdhost_dma(s);
  587. allwinner_sdhost_auto_stop(s);
  588. }
  589. allwinner_sdhost_update_irq(s);
  590. break;
  591. case REG_SD_CAGR: /* Command Argument */
  592. s->command_arg = value;
  593. break;
  594. case REG_SD_RESP0: /* Response Zero */
  595. s->response[0] = value;
  596. break;
  597. case REG_SD_RESP1: /* Response One */
  598. s->response[1] = value;
  599. break;
  600. case REG_SD_RESP2: /* Response Two */
  601. s->response[2] = value;
  602. break;
  603. case REG_SD_RESP3: /* Response Three */
  604. s->response[3] = value;
  605. break;
  606. case REG_SD_IMKR: /* Interrupt Mask */
  607. s->irq_mask = value;
  608. allwinner_sdhost_update_irq(s);
  609. break;
  610. case REG_SD_MISR: /* Masked Interrupt Status */
  611. case REG_SD_RISR: /* Raw Interrupt Status */
  612. s->irq_status &= ~value;
  613. allwinner_sdhost_update_irq(s);
  614. break;
  615. case REG_SD_STAR: /* Status */
  616. s->status &= ~value;
  617. allwinner_sdhost_update_irq(s);
  618. break;
  619. case REG_SD_FWLR: /* FIFO Water Level */
  620. s->fifo_wlevel = value;
  621. break;
  622. case REG_SD_FUNS: /* FIFO Function Select */
  623. s->fifo_func_sel = value;
  624. break;
  625. case REG_SD_DBGC: /* Debug Enable */
  626. s->debug_enable = value;
  627. break;
  628. case REG_SD_A12A: /* Auto command 12 argument */
  629. s->auto12_arg = value;
  630. break;
  631. case REG_SD_NTSR: /* SD NewTiming Set */
  632. s->newtiming_set = value;
  633. break;
  634. case REG_SD_SDBG: /* SD newTiming Set Debug */
  635. s->newtiming_debug = value;
  636. break;
  637. case REG_SD_HWRST: /* Hardware Reset Register */
  638. s->hardware_rst = value;
  639. break;
  640. case REG_SD_DMAC: /* Internal DMA Controller Control */
  641. s->dmac = value;
  642. allwinner_sdhost_update_irq(s);
  643. break;
  644. case REG_SD_DLBA: /* Descriptor List Base Address */
  645. s->desc_base = value;
  646. break;
  647. case REG_SD_IDST: /* Internal DMA Controller Status */
  648. s->dmac_status &= (~SD_IDST_WR_MASK) | (~value & SD_IDST_WR_MASK);
  649. allwinner_sdhost_update_irq(s);
  650. break;
  651. case REG_SD_IDIE: /* Internal DMA Controller Interrupt Enable */
  652. s->dmac_irq = value;
  653. allwinner_sdhost_update_irq(s);
  654. break;
  655. case REG_SD_THLDC: /* Card Threshold Control or FIFO (sun4i) */
  656. if (sc->is_sun4i) {
  657. allwinner_sdhost_fifo_write(s, value);
  658. } else {
  659. s->card_threshold = value;
  660. }
  661. break;
  662. case REG_SD_DSBD: /* eMMC DDR Start Bit Detection Control */
  663. s->startbit_detect = value;
  664. break;
  665. case REG_SD_FIFO: /* Read/Write FIFO */
  666. allwinner_sdhost_fifo_write(s, value);
  667. break;
  668. case REG_SD_RES_CRC: /* Response CRC from card/eMMC */
  669. case REG_SD_DATA7_CRC: /* CRC Data 7 from card/eMMC */
  670. case REG_SD_DATA6_CRC: /* CRC Data 6 from card/eMMC */
  671. case REG_SD_DATA5_CRC: /* CRC Data 5 from card/eMMC */
  672. case REG_SD_DATA4_CRC: /* CRC Data 4 from card/eMMC */
  673. case REG_SD_DATA3_CRC: /* CRC Data 3 from card/eMMC */
  674. case REG_SD_DATA2_CRC: /* CRC Data 2 from card/eMMC */
  675. case REG_SD_DATA1_CRC: /* CRC Data 1 from card/eMMC */
  676. case REG_SD_DATA0_CRC: /* CRC Data 0 from card/eMMC */
  677. case REG_SD_CRC_STA: /* CRC status from card/eMMC in write operation */
  678. break;
  679. case REG_SD_SAMP_DL: /* Sample delay control */
  680. if (sc->can_calibrate) {
  681. s->sample_delay = value;
  682. } else {
  683. out_of_bounds = true;
  684. }
  685. break;
  686. default:
  687. out_of_bounds = true;
  688. break;
  689. }
  690. if (out_of_bounds) {
  691. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset %"
  692. HWADDR_PRIx"\n", __func__, offset);
  693. }
  694. }
  695. static const MemoryRegionOps allwinner_sdhost_ops = {
  696. .read = allwinner_sdhost_read,
  697. .write = allwinner_sdhost_write,
  698. .endianness = DEVICE_LITTLE_ENDIAN,
  699. .valid = {
  700. .min_access_size = 4,
  701. .max_access_size = 4,
  702. },
  703. .impl.min_access_size = 4,
  704. };
  705. static const VMStateDescription vmstate_allwinner_sdhost = {
  706. .name = "allwinner-sdhost",
  707. .version_id = 1,
  708. .minimum_version_id = 1,
  709. .fields = (const VMStateField[]) {
  710. VMSTATE_UINT32(global_ctl, AwSdHostState),
  711. VMSTATE_UINT32(clock_ctl, AwSdHostState),
  712. VMSTATE_UINT32(timeout, AwSdHostState),
  713. VMSTATE_UINT32(bus_width, AwSdHostState),
  714. VMSTATE_UINT32(block_size, AwSdHostState),
  715. VMSTATE_UINT32(byte_count, AwSdHostState),
  716. VMSTATE_UINT32(transfer_cnt, AwSdHostState),
  717. VMSTATE_UINT32(command, AwSdHostState),
  718. VMSTATE_UINT32(command_arg, AwSdHostState),
  719. VMSTATE_UINT32_ARRAY(response, AwSdHostState, 4),
  720. VMSTATE_UINT32(irq_mask, AwSdHostState),
  721. VMSTATE_UINT32(irq_status, AwSdHostState),
  722. VMSTATE_UINT32(status, AwSdHostState),
  723. VMSTATE_UINT32(fifo_wlevel, AwSdHostState),
  724. VMSTATE_UINT32(fifo_func_sel, AwSdHostState),
  725. VMSTATE_UINT32(debug_enable, AwSdHostState),
  726. VMSTATE_UINT32(auto12_arg, AwSdHostState),
  727. VMSTATE_UINT32(newtiming_set, AwSdHostState),
  728. VMSTATE_UINT32(newtiming_debug, AwSdHostState),
  729. VMSTATE_UINT32(hardware_rst, AwSdHostState),
  730. VMSTATE_UINT32(dmac, AwSdHostState),
  731. VMSTATE_UINT32(desc_base, AwSdHostState),
  732. VMSTATE_UINT32(dmac_status, AwSdHostState),
  733. VMSTATE_UINT32(dmac_irq, AwSdHostState),
  734. VMSTATE_UINT32(card_threshold, AwSdHostState),
  735. VMSTATE_UINT32(startbit_detect, AwSdHostState),
  736. VMSTATE_UINT32(response_crc, AwSdHostState),
  737. VMSTATE_UINT32_ARRAY(data_crc, AwSdHostState, 8),
  738. VMSTATE_UINT32(status_crc, AwSdHostState),
  739. VMSTATE_UINT32(sample_delay, AwSdHostState),
  740. VMSTATE_END_OF_LIST()
  741. }
  742. };
  743. static const Property allwinner_sdhost_properties[] = {
  744. DEFINE_PROP_LINK("dma-memory", AwSdHostState, dma_mr,
  745. TYPE_MEMORY_REGION, MemoryRegion *),
  746. };
  747. static void allwinner_sdhost_init(Object *obj)
  748. {
  749. AwSdHostState *s = AW_SDHOST(obj);
  750. qbus_init(&s->sdbus, sizeof(s->sdbus),
  751. TYPE_AW_SDHOST_BUS, DEVICE(s), "sd-bus");
  752. memory_region_init_io(&s->iomem, obj, &allwinner_sdhost_ops, s,
  753. TYPE_AW_SDHOST, 4 * KiB);
  754. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
  755. sysbus_init_irq(SYS_BUS_DEVICE(s), &s->irq);
  756. }
  757. static void allwinner_sdhost_realize(DeviceState *dev, Error **errp)
  758. {
  759. AwSdHostState *s = AW_SDHOST(dev);
  760. if (!s->dma_mr) {
  761. error_setg(errp, TYPE_AW_SDHOST " 'dma-memory' link not set");
  762. return;
  763. }
  764. address_space_init(&s->dma_as, s->dma_mr, "sdhost-dma");
  765. }
  766. static void allwinner_sdhost_reset(DeviceState *dev)
  767. {
  768. AwSdHostState *s = AW_SDHOST(dev);
  769. AwSdHostClass *sc = AW_SDHOST_GET_CLASS(s);
  770. s->global_ctl = REG_SD_GCTL_RST;
  771. s->clock_ctl = REG_SD_CKCR_RST;
  772. s->timeout = REG_SD_TMOR_RST;
  773. s->bus_width = REG_SD_BWDR_RST;
  774. s->block_size = REG_SD_BKSR_RST;
  775. s->byte_count = REG_SD_BYCR_RST;
  776. s->transfer_cnt = 0;
  777. s->command = REG_SD_CMDR_RST;
  778. s->command_arg = REG_SD_CAGR_RST;
  779. for (int i = 0; i < ARRAY_SIZE(s->response); i++) {
  780. s->response[i] = REG_SD_RESP_RST;
  781. }
  782. s->irq_mask = REG_SD_IMKR_RST;
  783. s->irq_status = REG_SD_RISR_RST;
  784. s->status = REG_SD_STAR_RST;
  785. s->fifo_wlevel = REG_SD_FWLR_RST;
  786. s->fifo_func_sel = REG_SD_FUNS_RST;
  787. s->debug_enable = REG_SD_DBGC_RST;
  788. s->auto12_arg = REG_SD_A12A_RST;
  789. s->newtiming_set = REG_SD_NTSR_RST;
  790. s->newtiming_debug = REG_SD_SDBG_RST;
  791. s->hardware_rst = REG_SD_HWRST_RST;
  792. s->dmac = REG_SD_DMAC_RST;
  793. s->desc_base = REG_SD_DLBA_RST;
  794. s->dmac_status = REG_SD_IDST_RST;
  795. s->dmac_irq = REG_SD_IDIE_RST;
  796. s->card_threshold = REG_SD_THLDC_RST;
  797. s->startbit_detect = REG_SD_DSBD_RST;
  798. s->response_crc = REG_SD_RES_CRC_RST;
  799. for (int i = 0; i < ARRAY_SIZE(s->data_crc); i++) {
  800. s->data_crc[i] = REG_SD_DATA_CRC_RST;
  801. }
  802. s->status_crc = REG_SD_CRC_STA_RST;
  803. if (sc->can_calibrate) {
  804. s->sample_delay = REG_SD_SAMPLE_DL_RST;
  805. }
  806. }
  807. static void allwinner_sdhost_bus_class_init(ObjectClass *klass, void *data)
  808. {
  809. SDBusClass *sbc = SD_BUS_CLASS(klass);
  810. sbc->set_inserted = allwinner_sdhost_set_inserted;
  811. }
  812. static void allwinner_sdhost_class_init(ObjectClass *klass, void *data)
  813. {
  814. DeviceClass *dc = DEVICE_CLASS(klass);
  815. device_class_set_legacy_reset(dc, allwinner_sdhost_reset);
  816. dc->vmsd = &vmstate_allwinner_sdhost;
  817. dc->realize = allwinner_sdhost_realize;
  818. device_class_set_props(dc, allwinner_sdhost_properties);
  819. }
  820. static void allwinner_sdhost_sun4i_class_init(ObjectClass *klass, void *data)
  821. {
  822. AwSdHostClass *sc = AW_SDHOST_CLASS(klass);
  823. sc->max_desc_size = 8 * KiB;
  824. sc->is_sun4i = true;
  825. sc->can_calibrate = false;
  826. }
  827. static void allwinner_sdhost_sun5i_class_init(ObjectClass *klass, void *data)
  828. {
  829. AwSdHostClass *sc = AW_SDHOST_CLASS(klass);
  830. sc->max_desc_size = 64 * KiB;
  831. sc->is_sun4i = false;
  832. sc->can_calibrate = false;
  833. }
  834. static void allwinner_sdhost_sun50i_a64_class_init(ObjectClass *klass,
  835. void *data)
  836. {
  837. AwSdHostClass *sc = AW_SDHOST_CLASS(klass);
  838. sc->max_desc_size = 64 * KiB;
  839. sc->is_sun4i = false;
  840. sc->can_calibrate = true;
  841. }
  842. static void allwinner_sdhost_sun50i_a64_emmc_class_init(ObjectClass *klass,
  843. void *data)
  844. {
  845. AwSdHostClass *sc = AW_SDHOST_CLASS(klass);
  846. sc->max_desc_size = 8 * KiB;
  847. sc->is_sun4i = false;
  848. sc->can_calibrate = true;
  849. }
  850. static const TypeInfo allwinner_sdhost_info = {
  851. .name = TYPE_AW_SDHOST,
  852. .parent = TYPE_SYS_BUS_DEVICE,
  853. .instance_init = allwinner_sdhost_init,
  854. .instance_size = sizeof(AwSdHostState),
  855. .class_init = allwinner_sdhost_class_init,
  856. .class_size = sizeof(AwSdHostClass),
  857. .abstract = true,
  858. };
  859. static const TypeInfo allwinner_sdhost_sun4i_info = {
  860. .name = TYPE_AW_SDHOST_SUN4I,
  861. .parent = TYPE_AW_SDHOST,
  862. .class_init = allwinner_sdhost_sun4i_class_init,
  863. };
  864. static const TypeInfo allwinner_sdhost_sun5i_info = {
  865. .name = TYPE_AW_SDHOST_SUN5I,
  866. .parent = TYPE_AW_SDHOST,
  867. .class_init = allwinner_sdhost_sun5i_class_init,
  868. };
  869. static const TypeInfo allwinner_sdhost_sun50i_a64_info = {
  870. .name = TYPE_AW_SDHOST_SUN50I_A64,
  871. .parent = TYPE_AW_SDHOST,
  872. .class_init = allwinner_sdhost_sun50i_a64_class_init,
  873. };
  874. static const TypeInfo allwinner_sdhost_sun50i_a64_emmc_info = {
  875. .name = TYPE_AW_SDHOST_SUN50I_A64_EMMC,
  876. .parent = TYPE_AW_SDHOST,
  877. .class_init = allwinner_sdhost_sun50i_a64_emmc_class_init,
  878. };
  879. static const TypeInfo allwinner_sdhost_bus_info = {
  880. .name = TYPE_AW_SDHOST_BUS,
  881. .parent = TYPE_SD_BUS,
  882. .instance_size = sizeof(SDBus),
  883. .class_init = allwinner_sdhost_bus_class_init,
  884. };
  885. static void allwinner_sdhost_register_types(void)
  886. {
  887. type_register_static(&allwinner_sdhost_info);
  888. type_register_static(&allwinner_sdhost_sun4i_info);
  889. type_register_static(&allwinner_sdhost_sun5i_info);
  890. type_register_static(&allwinner_sdhost_sun50i_a64_info);
  891. type_register_static(&allwinner_sdhost_sun50i_a64_emmc_info);
  892. type_register_static(&allwinner_sdhost_bus_info);
  893. }
  894. type_init(allwinner_sdhost_register_types)