emmc.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. /* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. * THE SOFTWARE.
  18. */
  19. /* Provides an interface to the EMMC controller and commands for interacting
  20. * with an sd card */
  21. /* References:
  22. *
  23. * PLSS - SD Group Physical Layer Simplified Specification ver 3.00
  24. * HCSS - SD Group Host Controller Simplified Specification ver 3.00
  25. *
  26. * Broadcom BCM2835 Peripherals Guide
  27. */
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <assert.h>
  33. #include "mmio.h"
  34. #include "block.h"
  35. #include "timer.h"
  36. #include "util.h"
  37. //#define EMMC_DEBUG
  38. // Configuration options
  39. // Enable 1.8V support
  40. #define SD_1_8V_SUPPORT
  41. // Enable 4-bit support
  42. #define SD_4BIT_DATA
  43. // SD Clock Frequencies (in Hz)
  44. #define SD_CLOCK_ID 400000
  45. #define SD_CLOCK_NORMAL 25000000
  46. #define SD_CLOCK_HIGH 50000000
  47. #define SD_CLOCK_100 100000000
  48. #define SD_CLOCK_208 208000000
  49. // Enable SDXC maximum performance mode
  50. // Requires 150 mA power so disabled on the RPi for now
  51. //#define SDXC_MAXIMUM_PERFORMANCE
  52. // Enable SDMA support
  53. //#define SDMA_SUPPORT
  54. // SDMA buffer address
  55. #define SDMA_BUFFER 0x6000
  56. #define SDMA_BUFFER_PA (SDMA_BUFFER + 0xC0000000)
  57. // Enable card interrupts
  58. //#define SD_CARD_INTERRUPTS
  59. // Enable EXPERIMENTAL (and possibly DANGEROUS) SD write support
  60. #define SD_WRITE_SUPPORT
  61. // The particular SDHCI implementation
  62. #define SDHCI_IMPLEMENTATION_GENERIC 0
  63. #define SDHCI_IMPLEMENTATION_BCM_2708 1
  64. #define SDHCI_IMPLEMENTATION SDHCI_IMPLEMENTATION_BCM_2708
  65. static char driver_name[] = "emmc";
  66. static char device_name[] = "emmc0"; // We use a single device name as there is only
  67. // one card slot in the RPi
  68. static uint32_t hci_ver = 0;
  69. static uint32_t capabilities_0 = 0;
  70. static uint32_t capabilities_1 = 0;
  71. struct sd_scr
  72. {
  73. uint32_t scr[2];
  74. uint32_t sd_bus_widths;
  75. int sd_version;
  76. };
  77. struct emmc_block_dev
  78. {
  79. struct block_device bd;
  80. uint32_t card_supports_sdhc;
  81. uint32_t card_supports_18v;
  82. uint32_t card_ocr;
  83. uint32_t card_rca;
  84. uint32_t last_interrupt;
  85. uint32_t last_error;
  86. struct sd_scr *scr;
  87. int failed_voltage_switch;
  88. uint32_t last_cmd_reg;
  89. uint32_t last_cmd;
  90. uint32_t last_cmd_success;
  91. uint32_t last_r0;
  92. uint32_t last_r1;
  93. uint32_t last_r2;
  94. uint32_t last_r3;
  95. void *buf;
  96. int blocks_to_transfer;
  97. size_t block_size;
  98. int use_sdma;
  99. int card_removal;
  100. uint32_t base_clock;
  101. };
  102. #define EMMC_BASE 0x3f300000
  103. #define EMMC_ARG2 0
  104. #define EMMC_BLKSIZECNT 4
  105. #define EMMC_ARG1 8
  106. #define EMMC_CMDTM 0xC
  107. #define EMMC_RESP0 0x10
  108. #define EMMC_RESP1 0x14
  109. #define EMMC_RESP2 0x18
  110. #define EMMC_RESP3 0x1C
  111. #define EMMC_DATA 0x20
  112. #define EMMC_STATUS 0x24
  113. #define EMMC_CONTROL0 0x28
  114. #define EMMC_CONTROL1 0x2C
  115. #define EMMC_INTERRUPT 0x30
  116. #define EMMC_IRPT_MASK 0x34
  117. #define EMMC_IRPT_EN 0x38
  118. #define EMMC_CONTROL2 0x3C
  119. #define EMMC_CAPABILITIES_0 0x40
  120. #define EMMC_CAPABILITIES_1 0x44
  121. #define EMMC_FORCE_IRPT 0x50
  122. #define EMMC_BOOT_TIMEOUT 0x70
  123. #define EMMC_DBG_SEL 0x74
  124. #define EMMC_EXRDFIFO_CFG 0x80
  125. #define EMMC_EXRDFIFO_EN 0x84
  126. #define EMMC_TUNE_STEP 0x88
  127. #define EMMC_TUNE_STEPS_STD 0x8C
  128. #define EMMC_TUNE_STEPS_DDR 0x90
  129. #define EMMC_SPI_INT_SPT 0xF0
  130. #define EMMC_SLOTISR_VER 0xFC
  131. #define SD_CMD_INDEX(a) ((a) << 24)
  132. #define SD_CMD_TYPE_NORMAL 0x0
  133. #define SD_CMD_TYPE_SUSPEND (1 << 22)
  134. #define SD_CMD_TYPE_RESUME (2 << 22)
  135. #define SD_CMD_TYPE_ABORT (3 << 22)
  136. #define SD_CMD_TYPE_MASK (3 << 22)
  137. #define SD_CMD_ISDATA (1 << 21)
  138. #define SD_CMD_IXCHK_EN (1 << 20)
  139. #define SD_CMD_CRCCHK_EN (1 << 19)
  140. #define SD_CMD_RSPNS_TYPE_NONE 0 // For no response
  141. #define SD_CMD_RSPNS_TYPE_136 (1 << 16) // For response R2 (with CRC), R3,4 (no CRC)
  142. #define SD_CMD_RSPNS_TYPE_48 (2 << 16) // For responses R1, R5, R6, R7 (with CRC)
  143. #define SD_CMD_RSPNS_TYPE_48B (3 << 16) // For responses R1b, R5b (with CRC)
  144. #define SD_CMD_RSPNS_TYPE_MASK (3 << 16)
  145. #define SD_CMD_MULTI_BLOCK (1 << 5)
  146. #define SD_CMD_DAT_DIR_HC 0
  147. #define SD_CMD_DAT_DIR_CH (1 << 4)
  148. #define SD_CMD_AUTO_CMD_EN_NONE 0
  149. #define SD_CMD_AUTO_CMD_EN_CMD12 (1 << 2)
  150. #define SD_CMD_AUTO_CMD_EN_CMD23 (2 << 2)
  151. #define SD_CMD_BLKCNT_EN (1 << 1)
  152. #define SD_CMD_DMA 1
  153. #define SD_ERR_CMD_TIMEOUT 0
  154. #define SD_ERR_CMD_CRC 1
  155. #define SD_ERR_CMD_END_BIT 2
  156. #define SD_ERR_CMD_INDEX 3
  157. #define SD_ERR_DATA_TIMEOUT 4
  158. #define SD_ERR_DATA_CRC 5
  159. #define SD_ERR_DATA_END_BIT 6
  160. #define SD_ERR_CURRENT_LIMIT 7
  161. #define SD_ERR_AUTO_CMD12 8
  162. #define SD_ERR_ADMA 9
  163. #define SD_ERR_TUNING 10
  164. #define SD_ERR_RSVD 11
  165. #define SD_ERR_MASK_CMD_TIMEOUT (1 << (16 + SD_ERR_CMD_TIMEOUT))
  166. #define SD_ERR_MASK_CMD_CRC (1 << (16 + SD_ERR_CMD_CRC))
  167. #define SD_ERR_MASK_CMD_END_BIT (1 << (16 + SD_ERR_CMD_END_BIT))
  168. #define SD_ERR_MASK_CMD_INDEX (1 << (16 + SD_ERR_CMD_INDEX))
  169. #define SD_ERR_MASK_DATA_TIMEOUT (1 << (16 + SD_ERR_CMD_TIMEOUT))
  170. #define SD_ERR_MASK_DATA_CRC (1 << (16 + SD_ERR_CMD_CRC))
  171. #define SD_ERR_MASK_DATA_END_BIT (1 << (16 + SD_ERR_CMD_END_BIT))
  172. #define SD_ERR_MASK_CURRENT_LIMIT (1 << (16 + SD_ERR_CMD_CURRENT_LIMIT))
  173. #define SD_ERR_MASK_AUTO_CMD12 (1 << (16 + SD_ERR_CMD_AUTO_CMD12))
  174. #define SD_ERR_MASK_ADMA (1 << (16 + SD_ERR_CMD_ADMA))
  175. #define SD_ERR_MASK_TUNING (1 << (16 + SD_ERR_CMD_TUNING))
  176. #define SD_COMMAND_COMPLETE 1
  177. #define SD_TRANSFER_COMPLETE (1 << 1)
  178. #define SD_BLOCK_GAP_EVENT (1 << 2)
  179. #define SD_DMA_INTERRUPT (1 << 3)
  180. #define SD_BUFFER_WRITE_READY (1 << 4)
  181. #define SD_BUFFER_READ_READY (1 << 5)
  182. #define SD_CARD_INSERTION (1 << 6)
  183. #define SD_CARD_REMOVAL (1 << 7)
  184. #define SD_CARD_INTERRUPT (1 << 8)
  185. #define SD_RESP_NONE SD_CMD_RSPNS_TYPE_NONE
  186. #define SD_RESP_R1 (SD_CMD_RSPNS_TYPE_48 | SD_CMD_CRCCHK_EN)
  187. #define SD_RESP_R1b (SD_CMD_RSPNS_TYPE_48B | SD_CMD_CRCCHK_EN)
  188. #define SD_RESP_R2 (SD_CMD_RSPNS_TYPE_136 | SD_CMD_CRCCHK_EN)
  189. #define SD_RESP_R3 SD_CMD_RSPNS_TYPE_48
  190. #define SD_RESP_R4 SD_CMD_RSPNS_TYPE_136
  191. #define SD_RESP_R5 (SD_CMD_RSPNS_TYPE_48 | SD_CMD_CRCCHK_EN)
  192. #define SD_RESP_R5b (SD_CMD_RSPNS_TYPE_48B | SD_CMD_CRCCHK_EN)
  193. #define SD_RESP_R6 (SD_CMD_RSPNS_TYPE_48 | SD_CMD_CRCCHK_EN)
  194. #define SD_RESP_R7 (SD_CMD_RSPNS_TYPE_48 | SD_CMD_CRCCHK_EN)
  195. #define SD_DATA_READ (SD_CMD_ISDATA | SD_CMD_DAT_DIR_CH)
  196. #define SD_DATA_WRITE (SD_CMD_ISDATA | SD_CMD_DAT_DIR_HC)
  197. #define SD_CMD_RESERVED(a) 0xffffffff
  198. #define SUCCESS(a) (a->last_cmd_success)
  199. #define FAIL(a) (a->last_cmd_success == 0)
  200. #define TIMEOUT(a) (FAIL(a) && (a->last_error == 0))
  201. #define CMD_TIMEOUT(a) (FAIL(a) && (a->last_error & (1 << 16)))
  202. #define CMD_CRC(a) (FAIL(a) && (a->last_error & (1 << 17)))
  203. #define CMD_END_BIT(a) (FAIL(a) && (a->last_error & (1 << 18)))
  204. #define CMD_INDEX(a) (FAIL(a) && (a->last_error & (1 << 19)))
  205. #define DATA_TIMEOUT(a) (FAIL(a) && (a->last_error & (1 << 20)))
  206. #define DATA_CRC(a) (FAIL(a) && (a->last_error & (1 << 21)))
  207. #define DATA_END_BIT(a) (FAIL(a) && (a->last_error & (1 << 22)))
  208. #define CURRENT_LIMIT(a) (FAIL(a) && (a->last_error & (1 << 23)))
  209. #define ACMD12_ERROR(a) (FAIL(a) && (a->last_error & (1 << 24)))
  210. #define ADMA_ERROR(a) (FAIL(a) && (a->last_error & (1 << 25)))
  211. #define TUNING_ERROR(a) (FAIL(a) && (a->last_error & (1 << 26)))
  212. #define SD_VER_UNKNOWN 0
  213. #define SD_VER_1 1
  214. #define SD_VER_1_1 2
  215. #define SD_VER_2 3
  216. #define SD_VER_3 4
  217. #define SD_VER_4 5
  218. static char *sd_versions[] = { "unknown", "1.0 and 1.01", "1.10",
  219. "2.00", "3.0x", "4.xx" };
  220. #ifdef EMMC_DEBUG
  221. static char *err_irpts[] = { "CMD_TIMEOUT", "CMD_CRC", "CMD_END_BIT", "CMD_INDEX",
  222. "DATA_TIMEOUT", "DATA_CRC", "DATA_END_BIT", "CURRENT_LIMIT",
  223. "AUTO_CMD12", "ADMA", "TUNING", "RSVD" };
  224. #endif
  225. int sd_read(struct block_device *, uint8_t *, size_t buf_size, uint32_t);
  226. int sd_write(struct block_device *, uint8_t *, size_t buf_size, uint32_t);
  227. static uint32_t sd_commands[] = {
  228. SD_CMD_INDEX(0),
  229. SD_CMD_RESERVED(1),
  230. SD_CMD_INDEX(2) | SD_RESP_R2,
  231. SD_CMD_INDEX(3) | SD_RESP_R6,
  232. SD_CMD_INDEX(4),
  233. SD_CMD_INDEX(5) | SD_RESP_R4,
  234. SD_CMD_INDEX(6) | SD_RESP_R1,
  235. SD_CMD_INDEX(7) | SD_RESP_R1b,
  236. SD_CMD_INDEX(8) | SD_RESP_R7,
  237. SD_CMD_INDEX(9) | SD_RESP_R2,
  238. SD_CMD_INDEX(10) | SD_RESP_R2,
  239. SD_CMD_INDEX(11) | SD_RESP_R1,
  240. SD_CMD_INDEX(12) | SD_RESP_R1b | SD_CMD_TYPE_ABORT,
  241. SD_CMD_INDEX(13) | SD_RESP_R1,
  242. SD_CMD_RESERVED(14),
  243. SD_CMD_INDEX(15),
  244. SD_CMD_INDEX(16) | SD_RESP_R1,
  245. SD_CMD_INDEX(17) | SD_RESP_R1 | SD_DATA_READ,
  246. SD_CMD_INDEX(18) | SD_RESP_R1 | SD_DATA_READ | SD_CMD_MULTI_BLOCK | SD_CMD_BLKCNT_EN,
  247. SD_CMD_INDEX(19) | SD_RESP_R1 | SD_DATA_READ,
  248. SD_CMD_INDEX(20) | SD_RESP_R1b,
  249. SD_CMD_RESERVED(21),
  250. SD_CMD_RESERVED(22),
  251. SD_CMD_INDEX(23) | SD_RESP_R1,
  252. SD_CMD_INDEX(24) | SD_RESP_R1 | SD_DATA_WRITE,
  253. SD_CMD_INDEX(25) | SD_RESP_R1 | SD_DATA_WRITE | SD_CMD_MULTI_BLOCK | SD_CMD_BLKCNT_EN,
  254. SD_CMD_RESERVED(26),
  255. SD_CMD_INDEX(27) | SD_RESP_R1 | SD_DATA_WRITE,
  256. SD_CMD_INDEX(28) | SD_RESP_R1b,
  257. SD_CMD_INDEX(29) | SD_RESP_R1b,
  258. SD_CMD_INDEX(30) | SD_RESP_R1 | SD_DATA_READ,
  259. SD_CMD_RESERVED(31),
  260. SD_CMD_INDEX(32) | SD_RESP_R1,
  261. SD_CMD_INDEX(33) | SD_RESP_R1,
  262. SD_CMD_RESERVED(34),
  263. SD_CMD_RESERVED(35),
  264. SD_CMD_RESERVED(36),
  265. SD_CMD_RESERVED(37),
  266. SD_CMD_INDEX(38) | SD_RESP_R1b,
  267. SD_CMD_RESERVED(39),
  268. SD_CMD_RESERVED(40),
  269. SD_CMD_RESERVED(41),
  270. SD_CMD_RESERVED(42) | SD_RESP_R1,
  271. SD_CMD_RESERVED(43),
  272. SD_CMD_RESERVED(44),
  273. SD_CMD_RESERVED(45),
  274. SD_CMD_RESERVED(46),
  275. SD_CMD_RESERVED(47),
  276. SD_CMD_RESERVED(48),
  277. SD_CMD_RESERVED(49),
  278. SD_CMD_RESERVED(50),
  279. SD_CMD_RESERVED(51),
  280. SD_CMD_RESERVED(52),
  281. SD_CMD_RESERVED(53),
  282. SD_CMD_RESERVED(54),
  283. SD_CMD_INDEX(55) | SD_RESP_R1,
  284. SD_CMD_INDEX(56) | SD_RESP_R1 | SD_CMD_ISDATA,
  285. SD_CMD_RESERVED(57),
  286. SD_CMD_RESERVED(58),
  287. SD_CMD_RESERVED(59),
  288. SD_CMD_RESERVED(60),
  289. SD_CMD_RESERVED(61),
  290. SD_CMD_RESERVED(62),
  291. SD_CMD_RESERVED(63)
  292. };
  293. static uint32_t sd_acommands[] = {
  294. SD_CMD_RESERVED(0),
  295. SD_CMD_RESERVED(1),
  296. SD_CMD_RESERVED(2),
  297. SD_CMD_RESERVED(3),
  298. SD_CMD_RESERVED(4),
  299. SD_CMD_RESERVED(5),
  300. SD_CMD_INDEX(6) | SD_RESP_R1,
  301. SD_CMD_RESERVED(7),
  302. SD_CMD_RESERVED(8),
  303. SD_CMD_RESERVED(9),
  304. SD_CMD_RESERVED(10),
  305. SD_CMD_RESERVED(11),
  306. SD_CMD_RESERVED(12),
  307. SD_CMD_INDEX(13) | SD_RESP_R1,
  308. SD_CMD_RESERVED(14),
  309. SD_CMD_RESERVED(15),
  310. SD_CMD_RESERVED(16),
  311. SD_CMD_RESERVED(17),
  312. SD_CMD_RESERVED(18),
  313. SD_CMD_RESERVED(19),
  314. SD_CMD_RESERVED(20),
  315. SD_CMD_RESERVED(21),
  316. SD_CMD_INDEX(22) | SD_RESP_R1 | SD_DATA_READ,
  317. SD_CMD_INDEX(23) | SD_RESP_R1,
  318. SD_CMD_RESERVED(24),
  319. SD_CMD_RESERVED(25),
  320. SD_CMD_RESERVED(26),
  321. SD_CMD_RESERVED(27),
  322. SD_CMD_RESERVED(28),
  323. SD_CMD_RESERVED(29),
  324. SD_CMD_RESERVED(30),
  325. SD_CMD_RESERVED(31),
  326. SD_CMD_RESERVED(32),
  327. SD_CMD_RESERVED(33),
  328. SD_CMD_RESERVED(34),
  329. SD_CMD_RESERVED(35),
  330. SD_CMD_RESERVED(36),
  331. SD_CMD_RESERVED(37),
  332. SD_CMD_RESERVED(38),
  333. SD_CMD_RESERVED(39),
  334. SD_CMD_RESERVED(40),
  335. SD_CMD_INDEX(41) | SD_RESP_R3,
  336. SD_CMD_INDEX(42) | SD_RESP_R1,
  337. SD_CMD_RESERVED(43),
  338. SD_CMD_RESERVED(44),
  339. SD_CMD_RESERVED(45),
  340. SD_CMD_RESERVED(46),
  341. SD_CMD_RESERVED(47),
  342. SD_CMD_RESERVED(48),
  343. SD_CMD_RESERVED(49),
  344. SD_CMD_RESERVED(50),
  345. SD_CMD_INDEX(51) | SD_RESP_R1 | SD_DATA_READ,
  346. SD_CMD_RESERVED(52),
  347. SD_CMD_RESERVED(53),
  348. SD_CMD_RESERVED(54),
  349. SD_CMD_RESERVED(55),
  350. SD_CMD_RESERVED(56),
  351. SD_CMD_RESERVED(57),
  352. SD_CMD_RESERVED(58),
  353. SD_CMD_RESERVED(59),
  354. SD_CMD_RESERVED(60),
  355. SD_CMD_RESERVED(61),
  356. SD_CMD_RESERVED(62),
  357. SD_CMD_RESERVED(63)
  358. };
  359. // The actual command indices
  360. #define GO_IDLE_STATE 0
  361. #define ALL_SEND_CID 2
  362. #define SEND_RELATIVE_ADDR 3
  363. #define SET_DSR 4
  364. #define IO_SET_OP_COND 5
  365. #define SWITCH_FUNC 6
  366. #define SELECT_CARD 7
  367. #define DESELECT_CARD 7
  368. #define SELECT_DESELECT_CARD 7
  369. #define SEND_IF_COND 8
  370. #define SEND_CSD 9
  371. #define SEND_CID 10
  372. #define VOLTAGE_SWITCH 11
  373. #define STOP_TRANSMISSION 12
  374. #define SEND_STATUS 13
  375. #define GO_INACTIVE_STATE 15
  376. #define SET_BLOCKLEN 16
  377. #define READ_SINGLE_BLOCK 17
  378. #define READ_MULTIPLE_BLOCK 18
  379. #define SEND_TUNING_BLOCK 19
  380. #define SPEED_CLASS_CONTROL 20
  381. #define SET_BLOCK_COUNT 23
  382. #define WRITE_BLOCK 24
  383. #define WRITE_MULTIPLE_BLOCK 25
  384. #define PROGRAM_CSD 27
  385. #define SET_WRITE_PROT 28
  386. #define CLR_WRITE_PROT 29
  387. #define SEND_WRITE_PROT 30
  388. #define ERASE_WR_BLK_START 32
  389. #define ERASE_WR_BLK_END 33
  390. #define ERASE 38
  391. #define LOCK_UNLOCK 42
  392. #define APP_CMD 55
  393. #define GEN_CMD 56
  394. #define IS_APP_CMD 0x80000000
  395. #define ACMD(a) (a | IS_APP_CMD)
  396. #define SET_BUS_WIDTH (6 | IS_APP_CMD)
  397. #define SD_STATUS (13 | IS_APP_CMD)
  398. #define SEND_NUM_WR_BLOCKS (22 | IS_APP_CMD)
  399. #define SET_WR_BLK_ERASE_COUNT (23 | IS_APP_CMD)
  400. #define SD_SEND_OP_COND (41 | IS_APP_CMD)
  401. #define SET_CLR_CARD_DETECT (42 | IS_APP_CMD)
  402. #define SEND_SCR (51 | IS_APP_CMD)
  403. #define SD_RESET_CMD (1 << 25)
  404. #define SD_RESET_DAT (1 << 26)
  405. #define SD_RESET_ALL (1 << 24)
  406. #define SD_GET_CLOCK_DIVIDER_FAIL 0xffffffff
  407. // Get the current base clock rate in Hz
  408. #if SDHCI_IMPLEMENTATION == SDHCI_IMPLEMENTATION_BCM_2708
  409. #include "mbox.h"
  410. #endif
  411. static void sd_power_off()
  412. {
  413. /* Power off the SD card */
  414. uint32_t control0 = mmio_read(EMMC_BASE + EMMC_CONTROL0);
  415. control0 &= ~(1 << 8); // Set SD Bus Power bit off in Power Control Register
  416. mmio_write(EMMC_BASE + EMMC_CONTROL0, control0);
  417. }
  418. static volatile uint32_t mailbuffer[8] __attribute__ ((aligned (16)));
  419. static volatile uint32_t mb_addr[8] __attribute__ ((aligned (16)));
  420. static uint32_t sd_get_base_clock_hz()
  421. {
  422. uint32_t base_clock;
  423. #if SDHCI_IMPLEMENTATION == SDHCI_IMPLEMENTATION_GENERIC
  424. capabilities_0 = mmio_read(EMMC_BASE + EMMC_CAPABILITIES_0);
  425. base_clock = ((capabilities_0 >> 8) & 0xff) * 1000000;
  426. #elif SDHCI_IMPLEMENTATION == SDHCI_IMPLEMENTATION_BCM_2708
  427. //uint32_t mb_addr = 0x40007000; // 0x7000 in L2 cache coherent mode
  428. //volatile uint32_t *mailbuffer = (uint32_t *)mb_addr;
  429. /* Get the base clock rate */
  430. // set up the buffer
  431. mailbuffer[0] = 8 * 4; // size of this message
  432. mailbuffer[1] = 0; // this is a request
  433. // next comes the first tag
  434. mailbuffer[2] = 0x00030002; // get clock rate tag
  435. mailbuffer[3] = 0x8; // value buffer size
  436. mailbuffer[4] = 0x4; // is a request, value length = 4
  437. mailbuffer[5] = 0x1; // clock id + space to return clock id
  438. mailbuffer[6] = 0; // space to return rate (in Hz)
  439. // closing tag
  440. mailbuffer[7] = 0;
  441. // send the message
  442. mbox_write(MBOX_PROP, (uint32_t)mailbuffer);
  443. // read the response
  444. mbox_read(MBOX_PROP);
  445. if(mailbuffer[1] != MBOX_SUCCESS)
  446. {
  447. printf("EMMC: property mailbox did not return a valid response.\r\n");
  448. return 0;
  449. }
  450. if(mailbuffer[5] != 0x1)
  451. {
  452. printf("EMMC: property mailbox did not return a valid clock id.\r\n");
  453. return 0;
  454. }
  455. base_clock = mailbuffer[6];
  456. #else
  457. printf("EMMC: get_base_clock_hz() is not implemented for this "
  458. "architecture.\r\n");
  459. return 0;
  460. #endif
  461. #ifdef EMMC_DEBUG
  462. printf("EMMC: base clock rate is %i Hz\r\n", base_clock);
  463. #endif
  464. return base_clock;
  465. }
  466. #if SDHCI_IMPLEMENTATION == SDHCI_IMPLEMENTATION_BCM_2708
  467. static int bcm_2708_power_off()
  468. {
  469. //uint32_t mb_addr = 0x40007000; // 0x7000 in L2 cache coherent mode
  470. volatile uint32_t *mailbuffer = (uint32_t *)mb_addr;
  471. /* Power off the SD card */
  472. // set up the buffer
  473. mailbuffer[0] = 8 * 4; // size of this message
  474. mailbuffer[1] = 0; // this is a request
  475. // next comes the first tag
  476. mailbuffer[2] = 0x00028001; // set power state tag
  477. mailbuffer[3] = 0x8; // value buffer size
  478. mailbuffer[4] = 0x8; // is a request, value length = 8
  479. mailbuffer[5] = 0x0; // device id and device id also returned here
  480. mailbuffer[6] = 0x2; // set power off, wait for stable and returns state
  481. // closing tag
  482. mailbuffer[7] = 0;
  483. // send the message
  484. mbox_write(MBOX_PROP, (uint32_t)mb_addr);
  485. // read the response
  486. mbox_read(MBOX_PROP);
  487. if(mailbuffer[1] != MBOX_SUCCESS)
  488. {
  489. printf("EMMC: bcm_2708_power_off(): property mailbox did not return a valid response.\r\n");
  490. return -1;
  491. }
  492. if(mailbuffer[5] != 0x0)
  493. {
  494. printf("EMMC: property mailbox did not return a valid device id.\r\n");
  495. return -1;
  496. }
  497. if((mailbuffer[6] & 0x3) != 0)
  498. {
  499. #ifdef EMMC_DEBUG
  500. printf("EMMC: bcm_2708_power_off(): device did not power off successfully (%08x).\r\n", mailbuffer[6]);
  501. #endif
  502. return 1;
  503. }
  504. return 0;
  505. }
  506. static int bcm_2708_power_on()
  507. {
  508. //uint32_t mb_addr = 0x40007000; // 0x7000 in L2 cache coherent mode
  509. //volatile uint32_t mb_addr[8] __attribute__ ((aligned (16)));
  510. volatile uint32_t *mailbuffer = (uint32_t *)mb_addr;
  511. /* Power on the SD card */
  512. // set up the buffer
  513. mailbuffer[0] = 8 * 4; // size of this message
  514. mailbuffer[1] = 0; // this is a request
  515. // next comes the first tag
  516. mailbuffer[2] = 0x00028001; // set power state tag
  517. mailbuffer[3] = 0x8; // value buffer size
  518. mailbuffer[4] = 0x8; // is a request, value length = 8
  519. mailbuffer[5] = 0x0; // device id and device id also returned here
  520. mailbuffer[6] = 0x3; // set power off, wait for stable and returns state
  521. // closing tag
  522. mailbuffer[7] = 0;
  523. // send the message
  524. mbox_write(MBOX_PROP, (uint32_t)mb_addr);
  525. // read the response
  526. mbox_read(MBOX_PROP);
  527. if(mailbuffer[1] != MBOX_SUCCESS)
  528. {
  529. printf("EMMC: bcm_2708_power_on(): property mailbox did not return a valid response.\r\n");
  530. return -1;
  531. }
  532. if(mailbuffer[5] != 0x0)
  533. {
  534. printf("EMMC: property mailbox did not return a valid device id.\r\n");
  535. return -1;
  536. }
  537. if((mailbuffer[6] & 0x3) != 1)
  538. {
  539. #ifdef EMMC_DEBUG
  540. printf("EMMC: bcm_2708_power_on(): device did not power on successfully (%08x).\r\n", mailbuffer[6]);
  541. #endif
  542. return 1;
  543. }
  544. return 0;
  545. }
  546. static int bcm_2708_power_cycle()
  547. {
  548. if(bcm_2708_power_off() < 0)
  549. return -1;
  550. usleep(5000);
  551. return bcm_2708_power_on();
  552. }
  553. #endif
  554. // Set the clock dividers to generate a target value
  555. static uint32_t sd_get_clock_divider(uint32_t base_clock, uint32_t target_rate)
  556. {
  557. // TODO: implement use of preset value registers
  558. uint32_t targetted_divisor = 0;
  559. if(target_rate > base_clock)
  560. targetted_divisor = 1;
  561. else
  562. {
  563. targetted_divisor = base_clock / target_rate;
  564. uint32_t mod = base_clock % target_rate;
  565. if(mod)
  566. targetted_divisor--;
  567. }
  568. // Decide on the clock mode to use
  569. // Currently only 10-bit divided clock mode is supported
  570. if(hci_ver >= 2)
  571. {
  572. // HCI version 3 or greater supports 10-bit divided clock mode
  573. // This requires a power-of-two divider
  574. // Find the first bit set
  575. int divisor = -1;
  576. for(int first_bit = 31; first_bit >= 0; first_bit--)
  577. {
  578. uint32_t bit_test = (1 << first_bit);
  579. if(targetted_divisor & bit_test)
  580. {
  581. divisor = first_bit;
  582. targetted_divisor &= ~bit_test;
  583. if(targetted_divisor)
  584. {
  585. // The divisor is not a power-of-two, increase it
  586. divisor++;
  587. }
  588. break;
  589. }
  590. }
  591. if(divisor == -1)
  592. divisor = 31;
  593. if(divisor >= 32)
  594. divisor = 31;
  595. if(divisor != 0)
  596. divisor = (1 << (divisor - 1));
  597. if(divisor >= 0x400)
  598. divisor = 0x3ff;
  599. uint32_t freq_select = divisor & 0xff;
  600. uint32_t upper_bits = (divisor >> 8) & 0x3;
  601. uint32_t ret = (freq_select << 8) | (upper_bits << 6) | (0 << 5);
  602. #ifdef EMMC_DEBUG
  603. int denominator = 1;
  604. if(divisor != 0)
  605. denominator = divisor * 2;
  606. int actual_clock = base_clock / denominator;
  607. printf("EMMC: base_clock: %i, target_rate: %i, divisor: %08x, "
  608. "actual_clock: %i, ret: %08x\r\n", base_clock, target_rate,
  609. divisor, actual_clock, ret);
  610. #endif
  611. return ret;
  612. }
  613. else
  614. {
  615. printf("EMMC: unsupported host version\r\n");
  616. return SD_GET_CLOCK_DIVIDER_FAIL;
  617. }
  618. }
  619. // Switch the clock rate whilst running
  620. static int sd_switch_clock_rate(uint32_t base_clock, uint32_t target_rate)
  621. {
  622. // Decide on an appropriate divider
  623. uint32_t divider = sd_get_clock_divider(base_clock, target_rate);
  624. if(divider == SD_GET_CLOCK_DIVIDER_FAIL)
  625. {
  626. printf("EMMC: couldn't get a valid divider for target rate %i Hz\r\n",
  627. target_rate);
  628. return -1;
  629. }
  630. // Wait for the command inhibit (CMD and DAT) bits to clear
  631. while(mmio_read(EMMC_BASE + EMMC_STATUS) & 0x3)
  632. usleep(1000);
  633. // Set the SD clock off
  634. uint32_t control1 = mmio_read(EMMC_BASE + EMMC_CONTROL1);
  635. control1 &= ~(1 << 2);
  636. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  637. usleep(2000);
  638. // Write the new divider
  639. control1 &= ~0xffe0; // Clear old setting + clock generator select
  640. control1 |= divider;
  641. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  642. usleep(2000);
  643. // Enable the SD clock
  644. control1 |= (1 << 2);
  645. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  646. usleep(2000);
  647. #ifdef EMMC_DEBUG
  648. printf("EMMC: successfully set clock rate to %i Hz\r\n", target_rate);
  649. #endif
  650. return 0;
  651. }
  652. // Reset the CMD line
  653. static int sd_reset_cmd()
  654. {
  655. uint32_t control1 = mmio_read(EMMC_BASE + EMMC_CONTROL1);
  656. control1 |= SD_RESET_CMD;
  657. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  658. TIMEOUT_WAIT((mmio_read(EMMC_BASE + EMMC_CONTROL1) & SD_RESET_CMD) == 0, 1000000);
  659. if((mmio_read(EMMC_BASE + EMMC_CONTROL1) & SD_RESET_CMD) != 0)
  660. {
  661. printf("EMMC: CMD line did not reset properly\r\n");
  662. return -1;
  663. }
  664. return 0;
  665. }
  666. // Reset the CMD line
  667. static int sd_reset_dat()
  668. {
  669. uint32_t control1 = mmio_read(EMMC_BASE + EMMC_CONTROL1);
  670. control1 |= SD_RESET_DAT;
  671. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  672. TIMEOUT_WAIT((mmio_read(EMMC_BASE + EMMC_CONTROL1) & SD_RESET_DAT) == 0, 1000000);
  673. if((mmio_read(EMMC_BASE + EMMC_CONTROL1) & SD_RESET_DAT) != 0)
  674. {
  675. printf("EMMC: DAT line did not reset properly\r\n");
  676. return -1;
  677. }
  678. return 0;
  679. }
  680. static void sd_issue_command_int(struct emmc_block_dev *dev, uint32_t cmd_reg, uint32_t argument, useconds_t timeout)
  681. {
  682. dev->last_cmd_reg = cmd_reg;
  683. dev->last_cmd_success = 0;
  684. // This is as per HCSS 3.7.1.1/3.7.2.2
  685. // Check Command Inhibit
  686. while(mmio_read(EMMC_BASE + EMMC_STATUS) & 0x1)
  687. usleep(1000);
  688. // Is the command with busy?
  689. if((cmd_reg & SD_CMD_RSPNS_TYPE_MASK) == SD_CMD_RSPNS_TYPE_48B)
  690. {
  691. // With busy
  692. // Is is an abort command?
  693. if((cmd_reg & SD_CMD_TYPE_MASK) != SD_CMD_TYPE_ABORT)
  694. {
  695. // Not an abort command
  696. // Wait for the data line to be free
  697. while(mmio_read(EMMC_BASE + EMMC_STATUS) & 0x2)
  698. usleep(1000);
  699. }
  700. }
  701. // Is this a DMA transfer?
  702. int is_sdma = 0;
  703. if((cmd_reg & SD_CMD_ISDATA) && (dev->use_sdma))
  704. {
  705. #ifdef EMMC_DEBUG
  706. printf("SD: performing SDMA transfer, current INTERRUPT: %08x\r\n",
  707. mmio_read(EMMC_BASE + EMMC_INTERRUPT));
  708. #endif
  709. is_sdma = 1;
  710. }
  711. if(is_sdma)
  712. {
  713. // Set system address register (ARGUMENT2 in RPi)
  714. // We need to define a 4 kiB aligned buffer to use here
  715. // Then convert its virtual address to a bus address
  716. mmio_write(EMMC_BASE + EMMC_ARG2, SDMA_BUFFER_PA);
  717. }
  718. // Set block size and block count
  719. // For now, block size = 512 bytes, block count = 1,
  720. // host SDMA buffer boundary = 4 kiB
  721. if(dev->blocks_to_transfer > 0xffff)
  722. {
  723. printf("SD: blocks_to_transfer too great (%i)\r\n",
  724. dev->blocks_to_transfer);
  725. dev->last_cmd_success = 0;
  726. return;
  727. }
  728. uint32_t blksizecnt = dev->block_size | (dev->blocks_to_transfer << 16);
  729. mmio_write(EMMC_BASE + EMMC_BLKSIZECNT, blksizecnt);
  730. // Set argument 1 reg
  731. mmio_write(EMMC_BASE + EMMC_ARG1, argument);
  732. if(is_sdma)
  733. {
  734. // Set Transfer mode register
  735. cmd_reg |= SD_CMD_DMA;
  736. }
  737. // Set command reg
  738. mmio_write(EMMC_BASE + EMMC_CMDTM, cmd_reg);
  739. usleep(2000);
  740. // Wait for command complete interrupt
  741. TIMEOUT_WAIT(mmio_read(EMMC_BASE + EMMC_INTERRUPT) & 0x8001, timeout);
  742. uint32_t irpts = mmio_read(EMMC_BASE + EMMC_INTERRUPT);
  743. // Clear command complete status
  744. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffff0001);
  745. // Test for errors
  746. if((irpts & 0xffff0001) != 0x1)
  747. {
  748. #ifdef EMMC_DEBUG
  749. printf("SD: error occured whilst waiting for command complete interrupt\r\n");
  750. #endif
  751. dev->last_error = irpts & 0xffff0000;
  752. dev->last_interrupt = irpts;
  753. return;
  754. }
  755. usleep(2000);
  756. // Get response data
  757. switch(cmd_reg & SD_CMD_RSPNS_TYPE_MASK)
  758. {
  759. case SD_CMD_RSPNS_TYPE_48:
  760. case SD_CMD_RSPNS_TYPE_48B:
  761. dev->last_r0 = mmio_read(EMMC_BASE + EMMC_RESP0);
  762. break;
  763. case SD_CMD_RSPNS_TYPE_136:
  764. dev->last_r0 = mmio_read(EMMC_BASE + EMMC_RESP0);
  765. dev->last_r1 = mmio_read(EMMC_BASE + EMMC_RESP1);
  766. dev->last_r2 = mmio_read(EMMC_BASE + EMMC_RESP2);
  767. dev->last_r3 = mmio_read(EMMC_BASE + EMMC_RESP3);
  768. break;
  769. }
  770. // If with data, wait for the appropriate interrupt
  771. if((cmd_reg & SD_CMD_ISDATA) && (is_sdma == 0))
  772. {
  773. uint32_t wr_irpt;
  774. int is_write = 0;
  775. if(cmd_reg & SD_CMD_DAT_DIR_CH)
  776. wr_irpt = (1 << 5); // read
  777. else
  778. {
  779. is_write = 1;
  780. wr_irpt = (1 << 4); // write
  781. }
  782. int cur_block = 0;
  783. uint32_t *cur_buf_addr = (uint32_t *)dev->buf;
  784. while(cur_block < dev->blocks_to_transfer)
  785. {
  786. #ifdef EMMC_DEBUG
  787. if(dev->blocks_to_transfer > 1)
  788. printf("SD: multi block transfer, awaiting block %i ready\r\n",
  789. cur_block);
  790. #endif
  791. TIMEOUT_WAIT(mmio_read(EMMC_BASE + EMMC_INTERRUPT) & (wr_irpt | 0x8000), timeout);
  792. irpts = mmio_read(EMMC_BASE + EMMC_INTERRUPT);
  793. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffff0000 | wr_irpt);
  794. if((irpts & (0xffff0000 | wr_irpt)) != wr_irpt)
  795. {
  796. #ifdef EMMC_DEBUG
  797. printf("SD: error occured whilst waiting for data ready interrupt\r\n");
  798. #endif
  799. dev->last_error = irpts & 0xffff0000;
  800. dev->last_interrupt = irpts;
  801. return;
  802. }
  803. // Transfer the block
  804. size_t cur_byte_no = 0;
  805. while(cur_byte_no < dev->block_size)
  806. {
  807. if(is_write)
  808. {
  809. uint32_t data = read_word((uint8_t *)cur_buf_addr, 0);
  810. //printf("emmc write_word %lx\r\n",data);
  811. mmio_write(EMMC_BASE + EMMC_DATA, data);
  812. }
  813. else
  814. {
  815. uint32_t data = mmio_read(EMMC_BASE + EMMC_DATA);
  816. write_word(data, (uint8_t *)cur_buf_addr, 0);
  817. }
  818. cur_byte_no += 4;
  819. cur_buf_addr++;
  820. }
  821. #ifdef EMMC_DEBUG
  822. printf("SD: block %i transfer complete\r\n", cur_block);
  823. #endif
  824. cur_block++;
  825. }
  826. }
  827. // Wait for transfer complete (set if read/write transfer or with busy)
  828. if((((cmd_reg & SD_CMD_RSPNS_TYPE_MASK) == SD_CMD_RSPNS_TYPE_48B) ||
  829. (cmd_reg & SD_CMD_ISDATA)) && (is_sdma == 0))
  830. {
  831. // First check command inhibit (DAT) is not already 0
  832. if((mmio_read(EMMC_BASE + EMMC_STATUS) & 0x2) == 0)
  833. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffff0002);
  834. else
  835. {
  836. TIMEOUT_WAIT(mmio_read(EMMC_BASE + EMMC_INTERRUPT) & 0x8002, timeout);
  837. irpts = mmio_read(EMMC_BASE + EMMC_INTERRUPT);
  838. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffff0002);
  839. // Handle the case where both data timeout and transfer complete
  840. // are set - transfer complete overrides data timeout: HCSS 2.2.17
  841. if(((irpts & 0xffff0002) != 0x2) && ((irpts & 0xffff0002) != 0x100002))
  842. {
  843. #ifdef EMMC_DEBUG
  844. printf("SD: error occured whilst waiting for transfer complete interrupt\r\n");
  845. #endif
  846. dev->last_error = irpts & 0xffff0000;
  847. dev->last_interrupt = irpts;
  848. return;
  849. }
  850. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffff0002);
  851. }
  852. }
  853. else if (is_sdma)
  854. {
  855. // For SDMA transfers, we have to wait for either transfer complete,
  856. // DMA int or an error
  857. // First check command inhibit (DAT) is not already 0
  858. if((mmio_read(EMMC_BASE + EMMC_STATUS) & 0x2) == 0)
  859. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffff000a);
  860. else
  861. {
  862. TIMEOUT_WAIT(mmio_read(EMMC_BASE + EMMC_INTERRUPT) & 0x800a, timeout);
  863. irpts = mmio_read(EMMC_BASE + EMMC_INTERRUPT);
  864. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffff000a);
  865. // Detect errors
  866. if((irpts & 0x8000) && ((irpts & 0x2) != 0x2))
  867. {
  868. #ifdef EMMC_DEBUG
  869. printf("SD: error occured whilst waiting for transfer complete interrupt\r\n");
  870. #endif
  871. dev->last_error = irpts & 0xffff0000;
  872. dev->last_interrupt = irpts;
  873. return;
  874. }
  875. // Detect DMA interrupt without transfer complete
  876. // Currently not supported - all block sizes should fit in the
  877. // buffer
  878. if((irpts & 0x8) && ((irpts & 0x2) != 0x2))
  879. {
  880. #ifdef EMMC_DEBUG
  881. printf("SD: error: DMA interrupt occured without transfer complete\r\n");
  882. #endif
  883. dev->last_error = irpts & 0xffff0000;
  884. dev->last_interrupt = irpts;
  885. return;
  886. }
  887. // Detect transfer complete
  888. if(irpts & 0x2)
  889. {
  890. #ifdef EMMC_DEBUG
  891. printf("SD: SDMA transfer complete");
  892. #endif
  893. // Transfer the data to the user buffer
  894. memcpy(dev->buf, (const void *)SDMA_BUFFER, dev->block_size);
  895. }
  896. else
  897. {
  898. // Unknown error
  899. #ifdef EMMC_DEBUG
  900. if(irpts == 0)
  901. printf("SD: timeout waiting for SDMA transfer to complete\r\n");
  902. else
  903. printf("SD: unknown SDMA transfer error\r\n");
  904. printf("SD: INTERRUPT: %08x, STATUS %08x\r\n", irpts,
  905. mmio_read(EMMC_BASE + EMMC_STATUS));
  906. #endif
  907. if((irpts == 0) && ((mmio_read(EMMC_BASE + EMMC_STATUS) & 0x3) == 0x2))
  908. {
  909. // The data transfer is ongoing, we should attempt to stop
  910. // it
  911. #ifdef EMMC_DEBUG
  912. printf("SD: aborting transfer\r\n");
  913. #endif
  914. mmio_write(EMMC_BASE + EMMC_CMDTM, sd_commands[STOP_TRANSMISSION]);
  915. #ifdef EMMC_DEBUG
  916. // pause to let us read the screen
  917. usleep(2000000);
  918. #endif
  919. }
  920. dev->last_error = irpts & 0xffff0000;
  921. dev->last_interrupt = irpts;
  922. return;
  923. }
  924. }
  925. }
  926. // Return success
  927. dev->last_cmd_success = 1;
  928. }
  929. static void sd_handle_card_interrupt(struct emmc_block_dev *dev)
  930. {
  931. // Handle a card interrupt
  932. #ifdef EMMC_DEBUG
  933. uint32_t status = mmio_read(EMMC_BASE + EMMC_STATUS);
  934. printf("SD: card interrupt\r\n");
  935. printf("SD: controller status: %08x\r\n", status);
  936. #endif
  937. // Get the card status
  938. if(dev->card_rca)
  939. {
  940. sd_issue_command_int(dev, sd_commands[SEND_STATUS], dev->card_rca << 16,
  941. 500000);
  942. if(FAIL(dev))
  943. {
  944. #ifdef EMMC_DEBUG
  945. printf("SD: unable to get card status\r\n");
  946. #endif
  947. }
  948. else
  949. {
  950. #ifdef EMMC_DEBUG
  951. printf("SD: card status: %08x\r\n", dev->last_r0);
  952. #endif
  953. }
  954. }
  955. else
  956. {
  957. #ifdef EMMC_DEBUG
  958. printf("SD: no card currently selected\r\n");
  959. #endif
  960. }
  961. }
  962. static void sd_handle_interrupts(struct emmc_block_dev *dev)
  963. {
  964. uint32_t irpts = mmio_read(EMMC_BASE + EMMC_INTERRUPT);
  965. uint32_t reset_mask = 0;
  966. if(irpts & SD_COMMAND_COMPLETE)
  967. {
  968. #ifdef EMMC_DEBUG
  969. printf("SD: spurious command complete interrupt\r\n");
  970. #endif
  971. reset_mask |= SD_COMMAND_COMPLETE;
  972. }
  973. if(irpts & SD_TRANSFER_COMPLETE)
  974. {
  975. #ifdef EMMC_DEBUG
  976. printf("SD: spurious transfer complete interrupt\r\n");
  977. #endif
  978. reset_mask |= SD_TRANSFER_COMPLETE;
  979. }
  980. if(irpts & SD_BLOCK_GAP_EVENT)
  981. {
  982. #ifdef EMMC_DEBUG
  983. printf("SD: spurious block gap event interrupt\r\n");
  984. #endif
  985. reset_mask |= SD_BLOCK_GAP_EVENT;
  986. }
  987. if(irpts & SD_DMA_INTERRUPT)
  988. {
  989. #ifdef EMMC_DEBUG
  990. printf("SD: spurious DMA interrupt\r\n");
  991. #endif
  992. reset_mask |= SD_DMA_INTERRUPT;
  993. }
  994. if(irpts & SD_BUFFER_WRITE_READY)
  995. {
  996. #ifdef EMMC_DEBUG
  997. printf("SD: spurious buffer write ready interrupt\r\n");
  998. #endif
  999. reset_mask |= SD_BUFFER_WRITE_READY;
  1000. sd_reset_dat();
  1001. }
  1002. if(irpts & SD_BUFFER_READ_READY)
  1003. {
  1004. #ifdef EMMC_DEBUG
  1005. printf("SD: spurious buffer read ready interrupt\r\n");
  1006. #endif
  1007. reset_mask |= SD_BUFFER_READ_READY;
  1008. sd_reset_dat();
  1009. }
  1010. if(irpts & SD_CARD_INSERTION)
  1011. {
  1012. #ifdef EMMC_DEBUG
  1013. printf("SD: card insertion detected\r\n");
  1014. #endif
  1015. reset_mask |= SD_CARD_INSERTION;
  1016. }
  1017. if(irpts & SD_CARD_REMOVAL)
  1018. {
  1019. #ifdef EMMC_DEBUG
  1020. printf("SD: card removal detected\r\n");
  1021. #endif
  1022. reset_mask |= SD_CARD_REMOVAL;
  1023. dev->card_removal = 1;
  1024. }
  1025. if(irpts & SD_CARD_INTERRUPT)
  1026. {
  1027. #ifdef EMMC_DEBUG
  1028. printf("SD: card interrupt detected\r\n");
  1029. #endif
  1030. sd_handle_card_interrupt(dev);
  1031. reset_mask |= SD_CARD_INTERRUPT;
  1032. }
  1033. if(irpts & 0x8000)
  1034. {
  1035. #ifdef EMMC_DEBUG
  1036. printf("SD: spurious error interrupt: %08x\r\n", irpts);
  1037. #endif
  1038. reset_mask |= 0xffff0000;
  1039. }
  1040. mmio_write(EMMC_BASE + EMMC_INTERRUPT, reset_mask);
  1041. }
  1042. static void sd_issue_command(struct emmc_block_dev *dev, uint32_t command, uint32_t argument, useconds_t timeout)
  1043. {
  1044. // First, handle any pending interrupts
  1045. sd_handle_interrupts(dev);
  1046. // Stop the command issue if it was the card remove interrupt that was
  1047. // handled
  1048. if(dev->card_removal)
  1049. {
  1050. printf("SD: card removal detected\r\n");
  1051. dev->last_cmd_success = 0;
  1052. return;
  1053. }
  1054. // Now run the appropriate commands by calling sd_issue_command_int()
  1055. if(command & IS_APP_CMD)
  1056. {
  1057. command &= 0xff;
  1058. #ifdef EMMC_DEBUG
  1059. printf("SD: issuing command ACMD%i\r\n", command);
  1060. #endif
  1061. if(sd_acommands[command] == SD_CMD_RESERVED(0))
  1062. {
  1063. printf("SD: invalid command ACMD%i\r\n", command);
  1064. dev->last_cmd_success = 0;
  1065. return;
  1066. }
  1067. dev->last_cmd = APP_CMD;
  1068. uint32_t rca = 0;
  1069. if(dev->card_rca)
  1070. rca = dev->card_rca << 16;
  1071. sd_issue_command_int(dev, sd_commands[APP_CMD], rca, timeout);
  1072. if(dev->last_cmd_success)
  1073. {
  1074. dev->last_cmd = command | IS_APP_CMD;
  1075. sd_issue_command_int(dev, sd_acommands[command], argument, timeout);
  1076. }
  1077. }
  1078. else
  1079. {
  1080. #ifdef EMMC_DEBUG
  1081. printf("SD: issuing command CMD%i\r\n", command);
  1082. #endif
  1083. if(sd_commands[command] == SD_CMD_RESERVED(0))
  1084. {
  1085. printf("SD: invalid command CMD%i\r\n", command);
  1086. dev->last_cmd_success = 0;
  1087. return;
  1088. }
  1089. dev->last_cmd = command;
  1090. sd_issue_command_int(dev, sd_commands[command], argument, timeout);
  1091. }
  1092. #ifdef EMMC_DEBUG
  1093. if(FAIL(dev))
  1094. {
  1095. printf("SD: error issuing command: %i interrupts %08x: ", command, dev->last_interrupt);
  1096. if(dev->last_error == 0)
  1097. printf("TIMEOUT");
  1098. else
  1099. {
  1100. for(int i = 0; i < SD_ERR_RSVD; i++)
  1101. {
  1102. if(dev->last_error & (1 << (i + 16)))
  1103. {
  1104. printf(err_irpts[i]);
  1105. printf(" ");
  1106. }
  1107. }
  1108. }
  1109. printf("\r\n");
  1110. }
  1111. else
  1112. printf("SD: command completed successfully\r\n");
  1113. #endif
  1114. }
  1115. int sd_card_init(struct block_device **dev)
  1116. {
  1117. // Check the sanity of the sd_commands and sd_acommands structures
  1118. if(sizeof(sd_commands) != (64 * sizeof(uint32_t)))
  1119. {
  1120. printf("EMMC: fatal error, sd_commands of incorrect size: %i"
  1121. " expected %i\r\n", sizeof(sd_commands),
  1122. 64 * sizeof(uint32_t));
  1123. return -1;
  1124. }
  1125. if(sizeof(sd_acommands) != (64 * sizeof(uint32_t)))
  1126. {
  1127. printf("EMMC: fatal error, sd_acommands of incorrect size: %i"
  1128. " expected %i\r\n", sizeof(sd_acommands),
  1129. 64 * sizeof(uint32_t));
  1130. return -1;
  1131. }
  1132. #if SDHCI_IMPLEMENTATION == SDHCI_IMPLEMENTATION_BCM_2708
  1133. // Power cycle the card to ensure its in its startup state
  1134. if(bcm_2708_power_cycle() != 0)
  1135. {
  1136. printf("EMMC: BCM2708 controller did not power cycle successfully\r\n");
  1137. return -1;
  1138. }
  1139. #ifdef EMMC_DEBUG
  1140. printf("EMMC: BCM2708 controller power-cycled\r\n");
  1141. #endif
  1142. #endif
  1143. // Read the controller version
  1144. uint32_t ver = mmio_read(EMMC_BASE + EMMC_SLOTISR_VER);
  1145. uint32_t vendor = ver >> 24;
  1146. uint32_t sdversion = (ver >> 16) & 0xff;
  1147. uint32_t slot_status = ver & 0xff;
  1148. printf("EMMC: vendor %x, sdversion %x, slot_status %x\r\n", vendor, sdversion, slot_status);
  1149. hci_ver = sdversion;
  1150. if(hci_ver < 2)
  1151. {
  1152. printf("EMMC: only SDHCI versions >= 3.0 are supported\r\n");
  1153. return -1;
  1154. }
  1155. // Reset the controller
  1156. #ifdef EMMC_DEBUG
  1157. printf("EMMC: resetting controller\r\n");
  1158. #endif
  1159. uint32_t control1 = mmio_read(EMMC_BASE + EMMC_CONTROL1);
  1160. control1 |= (1 << 24);
  1161. // Disable clock
  1162. control1 &= ~(1 << 2);
  1163. control1 &= ~(1 << 0);
  1164. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  1165. TIMEOUT_WAIT((mmio_read(EMMC_BASE + EMMC_CONTROL1) & (0x7 << 24)) == 0, 1000000);
  1166. if((mmio_read(EMMC_BASE + EMMC_CONTROL1) & (0x7 << 24)) != 0)
  1167. {
  1168. printf("EMMC: controller did not reset properly\r\n");
  1169. return -1;
  1170. }
  1171. #ifdef EMMC_DEBUG
  1172. printf("EMMC: control0: %08x, control1: %08x, control2: %08x\r\n",
  1173. mmio_read(EMMC_BASE + EMMC_CONTROL0),
  1174. mmio_read(EMMC_BASE + EMMC_CONTROL1),
  1175. mmio_read(EMMC_BASE + EMMC_CONTROL2));
  1176. #endif
  1177. // Read the capabilities registers
  1178. capabilities_0 = mmio_read(EMMC_BASE + EMMC_CAPABILITIES_0);
  1179. capabilities_1 = mmio_read(EMMC_BASE + EMMC_CAPABILITIES_1);
  1180. #ifdef EMMC_DEBUG
  1181. printf("EMMC: capabilities: %08x%08x\r\n", capabilities_1, capabilities_0);
  1182. #endif
  1183. // Check for a valid card
  1184. #ifdef EMMC_DEBUG
  1185. printf("EMMC: checking for an inserted card\r\n");
  1186. #endif
  1187. TIMEOUT_WAIT(mmio_read(EMMC_BASE + EMMC_STATUS) & (1 << 16), 500000);
  1188. uint32_t status_reg = mmio_read(EMMC_BASE + EMMC_STATUS);
  1189. if((status_reg & (1 << 16)) == 0)
  1190. {
  1191. printf("EMMC: no card inserted\r\n");
  1192. return -1;
  1193. }
  1194. #ifdef EMMC_DEBUG
  1195. printf("EMMC: status: %08x\r\n", status_reg);
  1196. #endif
  1197. // Clear control2
  1198. mmio_write(EMMC_BASE + EMMC_CONTROL2, 0);
  1199. // Get the base clock rate
  1200. uint32_t base_clock = sd_get_base_clock_hz();
  1201. if(base_clock == 0)
  1202. {
  1203. printf("EMMC: assuming clock rate to be 100MHz\r\n");
  1204. base_clock = 100000000;
  1205. }
  1206. // Set clock rate to something slow
  1207. #ifdef EMMC_DEBUG
  1208. printf("EMMC: setting clock rate\r\n");
  1209. #endif
  1210. control1 = mmio_read(EMMC_BASE + EMMC_CONTROL1);
  1211. control1 |= 1; // enable clock
  1212. // Set to identification frequency (400 kHz)
  1213. uint32_t f_id = sd_get_clock_divider(base_clock, SD_CLOCK_ID);
  1214. if(f_id == SD_GET_CLOCK_DIVIDER_FAIL)
  1215. {
  1216. printf("EMMC: unable to get a valid clock divider for ID frequency\r\n");
  1217. return -1;
  1218. }
  1219. control1 |= f_id;
  1220. control1 |= (7 << 16); // data timeout = TMCLK * 2^10
  1221. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  1222. TIMEOUT_WAIT(mmio_read(EMMC_BASE + EMMC_CONTROL1) & 0x2, 0x1000000);
  1223. if((mmio_read(EMMC_BASE + EMMC_CONTROL1) & 0x2) == 0)
  1224. {
  1225. printf("EMMC: controller's clock did not stabilise within 1 second\r\n");
  1226. return -1;
  1227. }
  1228. #ifdef EMMC_DEBUG
  1229. printf("EMMC: control0: %08x, control1: %08x\r\n",
  1230. mmio_read(EMMC_BASE + EMMC_CONTROL0),
  1231. mmio_read(EMMC_BASE + EMMC_CONTROL1));
  1232. #endif
  1233. // Enable the SD clock
  1234. #ifdef EMMC_DEBUG
  1235. printf("EMMC: enabling SD clock\r\n");
  1236. #endif
  1237. usleep(2000);
  1238. control1 = mmio_read(EMMC_BASE + EMMC_CONTROL1);
  1239. control1 |= 4;
  1240. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  1241. usleep(2000);
  1242. #ifdef EMMC_DEBUG
  1243. printf("EMMC: SD clock enabled\r\n");
  1244. #endif
  1245. // Mask off sending interrupts to the ARM
  1246. mmio_write(EMMC_BASE + EMMC_IRPT_EN, 0);
  1247. // Reset interrupts
  1248. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffffffff);
  1249. // Have all interrupts sent to the INTERRUPT register
  1250. uint32_t irpt_mask = 0xffffffff & (~SD_CARD_INTERRUPT);
  1251. #ifdef SD_CARD_INTERRUPTS
  1252. irpt_mask |= SD_CARD_INTERRUPT;
  1253. #endif
  1254. mmio_write(EMMC_BASE + EMMC_IRPT_MASK, irpt_mask);
  1255. #ifdef EMMC_DEBUG
  1256. printf("EMMC: interrupts disabled\r\n");
  1257. #endif
  1258. usleep(2000);
  1259. // Prepare the device structure
  1260. struct emmc_block_dev *ret;
  1261. if(*dev == NULL)
  1262. ret = (struct emmc_block_dev *)malloc(sizeof(struct emmc_block_dev));
  1263. else
  1264. ret = (struct emmc_block_dev *)*dev;
  1265. assert(ret);
  1266. memset(ret, 0, sizeof(struct emmc_block_dev));
  1267. ret->bd.driver_name = driver_name;
  1268. ret->bd.device_name = device_name;
  1269. ret->bd.block_size = 512;
  1270. ret->bd.read = sd_read;
  1271. #ifdef SD_WRITE_SUPPORT
  1272. ret->bd.write = sd_write;
  1273. #endif
  1274. ret->bd.supports_multiple_block_read = 1; // FIXME
  1275. ret->bd.supports_multiple_block_write = 1; // FIXME
  1276. ret->base_clock = base_clock;
  1277. #ifdef EMMC_DEBUG
  1278. printf("EMMC: device structure created\r\n");
  1279. #endif
  1280. // Send CMD0 to the card (reset to idle state)
  1281. sd_issue_command(ret, GO_IDLE_STATE, 0, 500000);
  1282. if(FAIL(ret))
  1283. {
  1284. printf("SD: no CMD0 response\r\n");
  1285. return -1;
  1286. }
  1287. // Send CMD8 to the card
  1288. // Voltage supplied = 0x1 = 2.7-3.6V (standard)
  1289. // Check pattern = 10101010b (as per PLSS 4.3.13) = 0xAA
  1290. #ifdef EMMC_DEBUG
  1291. printf("SD: note a timeout error on the following command (CMD8) is normal "
  1292. "and expected if the SD card version is less than 2.0\r\n");
  1293. #endif
  1294. sd_issue_command(ret, SEND_IF_COND, 0x1aa, 500000);
  1295. int v2_later = 0;
  1296. if(TIMEOUT(ret))
  1297. v2_later = 0;
  1298. else if(CMD_TIMEOUT(ret))
  1299. {
  1300. if(sd_reset_cmd() == -1)
  1301. return -1;
  1302. mmio_write(EMMC_BASE + EMMC_INTERRUPT, SD_ERR_MASK_CMD_TIMEOUT);
  1303. v2_later = 0;
  1304. }
  1305. else if(FAIL(ret))
  1306. {
  1307. printf("SD: failure sending CMD8 (%08x)\r\n", ret->last_interrupt);
  1308. return -1;
  1309. }
  1310. else
  1311. {
  1312. if((ret->last_r0 & 0xfff) != 0x1aa)
  1313. {
  1314. printf("SD: unusable card\r\n");
  1315. #ifdef EMMC_DEBUG
  1316. printf("SD: CMD8 response %08x\r\n", ret->last_r0);
  1317. #endif
  1318. return -1;
  1319. }
  1320. else
  1321. v2_later = 1;
  1322. }
  1323. // Here we are supposed to check the response to CMD5 (HCSS 3.6)
  1324. // It only returns if the card is a SDIO card
  1325. #ifdef EMMC_DEBUG
  1326. printf("SD: note that a timeout error on the following command (CMD5) is "
  1327. "normal and expected if the card is not a SDIO card.\r\n");
  1328. #endif
  1329. sd_issue_command(ret, IO_SET_OP_COND, 0, 10000);
  1330. if(!TIMEOUT(ret))
  1331. {
  1332. if(CMD_TIMEOUT(ret))
  1333. {
  1334. if(sd_reset_cmd() == -1)
  1335. return -1;
  1336. mmio_write(EMMC_BASE + EMMC_INTERRUPT, SD_ERR_MASK_CMD_TIMEOUT);
  1337. }
  1338. else
  1339. {
  1340. printf("SD: SDIO card detected - not currently supported\r\n");
  1341. #ifdef EMMC_DEBUG
  1342. printf("SD: CMD5 returned %08x\r\n", ret->last_r0);
  1343. #endif
  1344. return -1;
  1345. }
  1346. }
  1347. // Call an inquiry ACMD41 (voltage window = 0) to get the OCR
  1348. #ifdef EMMC_DEBUG
  1349. printf("SD: sending inquiry ACMD41\r\n");
  1350. #endif
  1351. sd_issue_command(ret, ACMD(41), 0, 500000);
  1352. if(FAIL(ret))
  1353. {
  1354. printf("SD: inquiry ACMD41 failed\r\n");
  1355. return -1;
  1356. }
  1357. #ifdef EMMC_DEBUG
  1358. printf("SD: inquiry ACMD41 returned %08x\r\n", ret->last_r0);
  1359. #endif
  1360. // Call initialization ACMD41
  1361. int card_is_busy = 1;
  1362. while(card_is_busy)
  1363. {
  1364. uint32_t v2_flags = 0;
  1365. if(v2_later)
  1366. {
  1367. // Set SDHC support
  1368. v2_flags |= (1 << 30);
  1369. // Set 1.8v support
  1370. #ifdef SD_1_8V_SUPPORT
  1371. if(!ret->failed_voltage_switch)
  1372. v2_flags |= (1 << 24);
  1373. #endif
  1374. // Enable SDXC maximum performance
  1375. #ifdef SDXC_MAXIMUM_PERFORMANCE
  1376. v2_flags |= (1 << 28);
  1377. #endif
  1378. }
  1379. sd_issue_command(ret, ACMD(41), 0x00ff8000 | v2_flags, 500000);
  1380. if(FAIL(ret))
  1381. {
  1382. printf("SD: error issuing ACMD41\r\n");
  1383. return -1;
  1384. }
  1385. if((ret->last_r0 >> 31) & 0x1)
  1386. {
  1387. // Initialization is complete
  1388. ret->card_ocr = (ret->last_r0 >> 8) & 0xffff;
  1389. ret->card_supports_sdhc = (ret->last_r0 >> 30) & 0x1;
  1390. #ifdef SD_1_8V_SUPPORT
  1391. if(!ret->failed_voltage_switch)
  1392. ret->card_supports_18v = (ret->last_r0 >> 24) & 0x1;
  1393. #endif
  1394. card_is_busy = 0;
  1395. }
  1396. else
  1397. {
  1398. // Card is still busy
  1399. #ifdef EMMC_DEBUG
  1400. printf("SD: card is busy, retrying\r\n");
  1401. #endif
  1402. usleep(500000);
  1403. }
  1404. }
  1405. #ifdef EMMC_DEBUG
  1406. printf("SD: card identified: OCR: %04x, 1.8v support: %i, SDHC support: %i\r\n",
  1407. ret->card_ocr, ret->card_supports_18v, ret->card_supports_sdhc);
  1408. #endif
  1409. // At this point, we know the card is definitely an SD card, so will definitely
  1410. // support SDR12 mode which runs at 25 MHz
  1411. sd_switch_clock_rate(base_clock, SD_CLOCK_NORMAL);
  1412. // A small wait before the voltage switch
  1413. usleep(5000);
  1414. // Switch to 1.8V mode if possible
  1415. if(ret->card_supports_18v)
  1416. {
  1417. #ifdef EMMC_DEBUG
  1418. printf("SD: switching to 1.8V mode\r\n");
  1419. #endif
  1420. // As per HCSS 3.6.1
  1421. // Send VOLTAGE_SWITCH
  1422. sd_issue_command(ret, VOLTAGE_SWITCH, 0, 500000);
  1423. if(FAIL(ret))
  1424. {
  1425. #ifdef EMMC_DEBUG
  1426. printf("SD: error issuing VOLTAGE_SWITCH\r\n");
  1427. #endif
  1428. ret->failed_voltage_switch = 1;
  1429. sd_power_off();
  1430. return sd_card_init((struct block_device **)&ret);
  1431. }
  1432. // Disable SD clock
  1433. control1 = mmio_read(EMMC_BASE + EMMC_CONTROL1);
  1434. control1 &= ~(1 << 2);
  1435. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  1436. // Check DAT[3:0]
  1437. status_reg = mmio_read(EMMC_BASE + EMMC_STATUS);
  1438. uint32_t dat30 = (status_reg >> 20) & 0xf;
  1439. if(dat30 != 0)
  1440. {
  1441. #ifdef EMMC_DEBUG
  1442. printf("SD: DAT[3:0] did not settle to 0\r\n");
  1443. #endif
  1444. ret->failed_voltage_switch = 1;
  1445. sd_power_off();
  1446. return sd_card_init((struct block_device **)&ret);
  1447. }
  1448. // Set 1.8V signal enable to 1
  1449. uint32_t control0 = mmio_read(EMMC_BASE + EMMC_CONTROL0);
  1450. control0 |= (1 << 8);
  1451. mmio_write(EMMC_BASE + EMMC_CONTROL0, control0);
  1452. // Wait 5 ms
  1453. usleep(5000);
  1454. // Check the 1.8V signal enable is set
  1455. control0 = mmio_read(EMMC_BASE + EMMC_CONTROL0);
  1456. if(((control0 >> 8) & 0x1) == 0)
  1457. {
  1458. #ifdef EMMC_DEBUG
  1459. printf("SD: controller did not keep 1.8V signal enable high\r\n");
  1460. #endif
  1461. ret->failed_voltage_switch = 1;
  1462. sd_power_off();
  1463. return sd_card_init((struct block_device **)&ret);
  1464. }
  1465. // Re-enable the SD clock
  1466. control1 = mmio_read(EMMC_BASE + EMMC_CONTROL1);
  1467. control1 |= (1 << 2);
  1468. mmio_write(EMMC_BASE + EMMC_CONTROL1, control1);
  1469. // Wait 1 ms
  1470. usleep(10000);
  1471. // Check DAT[3:0]
  1472. status_reg = mmio_read(EMMC_BASE + EMMC_STATUS);
  1473. dat30 = (status_reg >> 20) & 0xf;
  1474. if(dat30 != 0xf)
  1475. {
  1476. #ifdef EMMC_DEBUG
  1477. printf("SD: DAT[3:0] did not settle to 1111b (%01x)\r\n", dat30);
  1478. #endif
  1479. ret->failed_voltage_switch = 1;
  1480. sd_power_off();
  1481. return sd_card_init((struct block_device **)&ret);
  1482. }
  1483. #ifdef EMMC_DEBUG
  1484. printf("SD: voltage switch complete\r\n");
  1485. #endif
  1486. }
  1487. // Send CMD2 to get the cards CID
  1488. sd_issue_command(ret, ALL_SEND_CID, 0, 500000);
  1489. if(FAIL(ret))
  1490. {
  1491. printf("SD: error sending ALL_SEND_CID\r\n");
  1492. return -1;
  1493. }
  1494. uint32_t card_cid_0 = ret->last_r0;
  1495. uint32_t card_cid_1 = ret->last_r1;
  1496. uint32_t card_cid_2 = ret->last_r2;
  1497. uint32_t card_cid_3 = ret->last_r3;
  1498. #ifdef EMMC_DEBUG
  1499. printf("SD: card CID: %08x%08x%08x%08x\r\n", card_cid_3, card_cid_2, card_cid_1, card_cid_0);
  1500. #endif
  1501. uint32_t *dev_id = (uint32_t *)malloc(4 * sizeof(uint32_t));
  1502. dev_id[0] = card_cid_0;
  1503. dev_id[1] = card_cid_1;
  1504. dev_id[2] = card_cid_2;
  1505. dev_id[3] = card_cid_3;
  1506. ret->bd.device_id = (uint8_t *)dev_id;
  1507. ret->bd.dev_id_len = 4 * sizeof(uint32_t);
  1508. // Send CMD3 to enter the data state
  1509. sd_issue_command(ret, SEND_RELATIVE_ADDR, 0, 500000);
  1510. if(FAIL(ret))
  1511. {
  1512. printf("SD: error sending SEND_RELATIVE_ADDR\r\n");
  1513. free(ret);
  1514. return -1;
  1515. }
  1516. uint32_t cmd3_resp = ret->last_r0;
  1517. #ifdef EMMC_DEBUG
  1518. printf("SD: CMD3 response: %08x\r\n", cmd3_resp);
  1519. #endif
  1520. ret->card_rca = (cmd3_resp >> 16) & 0xffff;
  1521. uint32_t crc_error = (cmd3_resp >> 15) & 0x1;
  1522. uint32_t illegal_cmd = (cmd3_resp >> 14) & 0x1;
  1523. uint32_t error = (cmd3_resp >> 13) & 0x1;
  1524. uint32_t status = (cmd3_resp >> 9) & 0xf;
  1525. uint32_t ready = (cmd3_resp >> 8) & 0x1;
  1526. if(crc_error)
  1527. {
  1528. printf("SD: CRC error\r\n");
  1529. free(ret);
  1530. free(dev_id);
  1531. return -1;
  1532. }
  1533. if(illegal_cmd)
  1534. {
  1535. printf("SD: illegal command\r\n");
  1536. free(ret);
  1537. free(dev_id);
  1538. return -1;
  1539. }
  1540. if(error)
  1541. {
  1542. printf("SD: generic error\r\n");
  1543. free(ret);
  1544. free(dev_id);
  1545. return -1;
  1546. }
  1547. if(!ready)
  1548. {
  1549. printf("SD: not ready for data\r\n");
  1550. free(ret);
  1551. free(dev_id);
  1552. return -1;
  1553. }
  1554. #ifdef EMMC_DEBUG
  1555. printf("SD: RCA: %04x\r\n", ret->card_rca);
  1556. #endif
  1557. // Now select the card (toggles it to transfer state)
  1558. sd_issue_command(ret, SELECT_CARD, ret->card_rca << 16, 500000);
  1559. if(FAIL(ret))
  1560. {
  1561. printf("SD: error sending CMD7\r\n");
  1562. free(ret);
  1563. return -1;
  1564. }
  1565. uint32_t cmd7_resp = ret->last_r0;
  1566. status = (cmd7_resp >> 9) & 0xf;
  1567. if((status != 3) && (status != 4))
  1568. {
  1569. printf("SD: invalid status (%i)\r\n", status);
  1570. free(ret);
  1571. free(dev_id);
  1572. return -1;
  1573. }
  1574. // If not an SDHC card, ensure BLOCKLEN is 512 bytes
  1575. if(!ret->card_supports_sdhc)
  1576. {
  1577. sd_issue_command(ret, SET_BLOCKLEN, 512, 500000);
  1578. if(FAIL(ret))
  1579. {
  1580. printf("SD: error sending SET_BLOCKLEN\r\n");
  1581. free(ret);
  1582. return -1;
  1583. }
  1584. }
  1585. ret->block_size = 512;
  1586. uint32_t controller_block_size = mmio_read(EMMC_BASE + EMMC_BLKSIZECNT);
  1587. controller_block_size &= (~0xfff);
  1588. controller_block_size |= 0x200;
  1589. mmio_write(EMMC_BASE + EMMC_BLKSIZECNT, controller_block_size);
  1590. // Get the cards SCR register
  1591. ret->scr = (struct sd_scr *)malloc(sizeof(struct sd_scr));
  1592. ret->buf = &ret->scr->scr[0];
  1593. ret->block_size = 8;
  1594. ret->blocks_to_transfer = 1;
  1595. sd_issue_command(ret, SEND_SCR, 0, 500000);
  1596. ret->block_size = 512;
  1597. if(FAIL(ret))
  1598. {
  1599. printf("SD: error sending SEND_SCR\r\n");
  1600. free(ret->scr);
  1601. free(ret);
  1602. return -1;
  1603. }
  1604. // Determine card version
  1605. // Note that the SCR is big-endian
  1606. uint32_t scr0 = byte_swap(ret->scr->scr[0]);
  1607. ret->scr->sd_version = SD_VER_UNKNOWN;
  1608. uint32_t sd_spec = (scr0 >> (56 - 32)) & 0xf;
  1609. uint32_t sd_spec3 = (scr0 >> (47 - 32)) & 0x1;
  1610. uint32_t sd_spec4 = (scr0 >> (42 - 32)) & 0x1;
  1611. ret->scr->sd_bus_widths = (scr0 >> (48 - 32)) & 0xf;
  1612. if(sd_spec == 0)
  1613. ret->scr->sd_version = SD_VER_1;
  1614. else if(sd_spec == 1)
  1615. ret->scr->sd_version = SD_VER_1_1;
  1616. else if(sd_spec == 2)
  1617. {
  1618. if(sd_spec3 == 0)
  1619. ret->scr->sd_version = SD_VER_2;
  1620. else if(sd_spec3 == 1)
  1621. {
  1622. if(sd_spec4 == 0)
  1623. ret->scr->sd_version = SD_VER_3;
  1624. else if(sd_spec4 == 1)
  1625. ret->scr->sd_version = SD_VER_4;
  1626. }
  1627. }
  1628. #ifdef EMMC_DEBUG
  1629. printf("SD: &scr: %08x\r\n", &ret->scr->scr[0]);
  1630. printf("SD: SCR[0]: %08x, SCR[1]: %08x\r\n", ret->scr->scr[0], ret->scr->scr[1]);;
  1631. printf("SD: SCR: %08x%08x\r\n", byte_swap(ret->scr->scr[0]), byte_swap(ret->scr->scr[1]));
  1632. printf("SD: SCR: version %s, bus_widths %01x\r\n", sd_versions[ret->scr->sd_version],
  1633. ret->scr->sd_bus_widths);
  1634. #endif
  1635. if(ret->scr->sd_bus_widths & 0x4)
  1636. {
  1637. // Set 4-bit transfer mode (ACMD6)
  1638. // See HCSS 3.4 for the algorithm
  1639. #ifdef SD_4BIT_DATA
  1640. #ifdef EMMC_DEBUG
  1641. printf("SD: switching to 4-bit data mode\r\n");
  1642. #endif
  1643. // Disable card interrupt in host
  1644. uint32_t old_irpt_mask = mmio_read(EMMC_BASE + EMMC_IRPT_MASK);
  1645. uint32_t new_iprt_mask = old_irpt_mask & ~(1 << 8);
  1646. mmio_write(EMMC_BASE + EMMC_IRPT_MASK, new_iprt_mask);
  1647. // Send ACMD6 to change the card's bit mode
  1648. sd_issue_command(ret, SET_BUS_WIDTH, 0x2, 500000);
  1649. if(FAIL(ret))
  1650. printf("SD: switch to 4-bit data mode failed\r\n");
  1651. else
  1652. {
  1653. // Change bit mode for Host
  1654. uint32_t control0 = mmio_read(EMMC_BASE + EMMC_CONTROL0);
  1655. control0 |= 0x2;
  1656. mmio_write(EMMC_BASE + EMMC_CONTROL0, control0);
  1657. // Re-enable card interrupt in host
  1658. mmio_write(EMMC_BASE + EMMC_IRPT_MASK, old_irpt_mask);
  1659. #ifdef EMMC_DEBUG
  1660. printf("SD: switch to 4-bit complete\r\n");
  1661. #endif
  1662. }
  1663. #endif
  1664. }
  1665. printf("SD: found a valid version %s SD card\r\n", sd_versions[ret->scr->sd_version]);
  1666. #ifdef EMMC_DEBUG
  1667. printf("SD: setup successful (status %i)\r\n", status);
  1668. #endif
  1669. // Reset interrupt register
  1670. mmio_write(EMMC_BASE + EMMC_INTERRUPT, 0xffffffff);
  1671. *dev = (struct block_device *)ret;
  1672. return 0;
  1673. }
  1674. static int sd_ensure_data_mode(struct emmc_block_dev *edev)
  1675. {
  1676. if(edev->card_rca == 0)
  1677. {
  1678. // Try again to initialise the card
  1679. int ret = sd_card_init((struct block_device **)&edev);
  1680. if(ret != 0)
  1681. return ret;
  1682. }
  1683. #ifdef EMMC_DEBUG
  1684. printf("SD: ensure_data_mode() obtaining status register for card_rca %08x: \r\n",
  1685. edev->card_rca);
  1686. #endif
  1687. sd_issue_command(edev, SEND_STATUS, edev->card_rca << 16, 500000);
  1688. if(FAIL(edev))
  1689. {
  1690. printf("SD: ensure_data_mode() error sending CMD13\r\n");
  1691. edev->card_rca = 0;
  1692. return -1;
  1693. }
  1694. uint32_t status = edev->last_r0;
  1695. uint32_t cur_state = (status >> 9) & 0xf;
  1696. #ifdef EMMC_DEBUG
  1697. printf("status %i\r\n", cur_state);
  1698. #endif
  1699. if(cur_state == 3)
  1700. {
  1701. // Currently in the stand-by state - select it
  1702. sd_issue_command(edev, SELECT_CARD, edev->card_rca << 16, 500000);
  1703. if(FAIL(edev))
  1704. {
  1705. printf("SD: ensure_data_mode() no response from CMD17\r\n");
  1706. edev->card_rca = 0;
  1707. return -1;
  1708. }
  1709. }
  1710. else if(cur_state == 5)
  1711. {
  1712. // In the data transfer state - cancel the transmission
  1713. sd_issue_command(edev, STOP_TRANSMISSION, 0, 500000);
  1714. if(FAIL(edev))
  1715. {
  1716. printf("SD: ensure_data_mode() no response from CMD12\r\n");
  1717. edev->card_rca = 0;
  1718. return -1;
  1719. }
  1720. // Reset the data circuit
  1721. sd_reset_dat();
  1722. }
  1723. else if(cur_state != 4)
  1724. {
  1725. // Not in the transfer state - re-initialise
  1726. int ret = sd_card_init((struct block_device **)&edev);
  1727. if(ret != 0)
  1728. return ret;
  1729. }
  1730. // Check again that we're now in the correct mode
  1731. if(cur_state != 4)
  1732. {
  1733. #ifdef EMMC_DEBUG
  1734. printf("SD: ensure_data_mode() rechecking status: ");
  1735. #endif
  1736. sd_issue_command(edev, SEND_STATUS, edev->card_rca << 16, 500000);
  1737. if(FAIL(edev))
  1738. {
  1739. printf("SD: ensure_data_mode() no response from CMD13\r\n");
  1740. edev->card_rca = 0;
  1741. return -1;
  1742. }
  1743. status = edev->last_r0;
  1744. cur_state = (status >> 9) & 0xf;
  1745. #ifdef EMMC_DEBUG
  1746. printf("%i\r\n", cur_state);
  1747. #endif
  1748. if(cur_state != 4)
  1749. {
  1750. printf("SD: unable to initialise SD card to "
  1751. "data mode (state %i)\r\n", cur_state);
  1752. edev->card_rca = 0;
  1753. return -1;
  1754. }
  1755. }
  1756. return 0;
  1757. }
  1758. #ifdef SDMA_SUPPORT
  1759. // We only support DMA transfers to buffers aligned on a 4 kiB boundary
  1760. static int sd_suitable_for_dma(void *buf)
  1761. {
  1762. if((uintptr_t)buf & 0xfff)
  1763. return 0;
  1764. else
  1765. return 1;
  1766. }
  1767. #endif
  1768. static int sd_do_data_command(struct emmc_block_dev *edev, int is_write, uint8_t *buf, size_t buf_size, uint32_t block_no)
  1769. {
  1770. // PLSS table 4.20 - SDSC cards use byte addresses rather than block addresses
  1771. if(!edev->card_supports_sdhc)
  1772. block_no *= 512;
  1773. // This is as per HCSS 3.7.2.1
  1774. if(buf_size < edev->block_size)
  1775. {
  1776. printf("SD: do_data_command() called with buffer size (%i) less than "
  1777. "block size (%i)\r\n", buf_size, edev->block_size);
  1778. return -1;
  1779. }
  1780. edev->blocks_to_transfer = buf_size / edev->block_size;
  1781. if(buf_size % edev->block_size)
  1782. {
  1783. printf("SD: do_data_command() called with buffer size (%i) not an "
  1784. "exact multiple of block size (%i)\r\n", buf_size, edev->block_size);
  1785. return -1;
  1786. }
  1787. edev->buf = buf;
  1788. // Decide on the command to use
  1789. int command;
  1790. if(is_write)
  1791. {
  1792. if(edev->blocks_to_transfer > 1) {
  1793. command = WRITE_MULTIPLE_BLOCK;
  1794. printf("SD: WRITE_MULTIPLE_BLOCK\r\n");
  1795. }
  1796. else {
  1797. command = WRITE_BLOCK;
  1798. printf("SD: WRITE_BLOCK\r\n");
  1799. }
  1800. }
  1801. else
  1802. {
  1803. if(edev->blocks_to_transfer > 1)
  1804. command = READ_MULTIPLE_BLOCK;
  1805. else
  1806. command = READ_SINGLE_BLOCK;
  1807. }
  1808. int retry_count = 0;
  1809. int max_retries = 3;
  1810. while(retry_count < max_retries)
  1811. {
  1812. #ifdef SDMA_SUPPORT
  1813. // use SDMA for the first try only
  1814. if((retry_count == 0) && sd_suitable_for_dma(buf))
  1815. edev->use_sdma = 1;
  1816. else
  1817. {
  1818. #ifdef EMMC_DEBUG
  1819. printf("SD: retrying without SDMA\r\n");
  1820. #endif
  1821. edev->use_sdma = 0;
  1822. }
  1823. #else
  1824. edev->use_sdma = 0;
  1825. #endif
  1826. sd_issue_command(edev, command, block_no, 5000000);
  1827. if(SUCCESS(edev))
  1828. break;
  1829. else
  1830. {
  1831. printf("SD: error sending CMD%i, ", command);
  1832. printf("error = %08x. ", edev->last_error);
  1833. retry_count++;
  1834. if(retry_count < max_retries)
  1835. printf("Retrying...\r\n");
  1836. else
  1837. printf("Giving up.\r\n");
  1838. }
  1839. }
  1840. if(retry_count == max_retries)
  1841. {
  1842. edev->card_rca = 0;
  1843. return -1;
  1844. }
  1845. return 0;
  1846. }
  1847. int sd_read(struct block_device *dev, uint8_t *buf, size_t buf_size, uint32_t block_no)
  1848. {
  1849. // Check the status of the card
  1850. struct emmc_block_dev *edev = (struct emmc_block_dev *)dev;
  1851. if(sd_ensure_data_mode(edev) != 0)
  1852. return -1;
  1853. #ifdef EMMC_DEBUG
  1854. printf("SD: read() card ready, reading from block %u\r\n", block_no);
  1855. #endif
  1856. if(sd_do_data_command(edev, 0, buf, buf_size, block_no) < 0)
  1857. return -1;
  1858. #ifdef EMMC_DEBUG
  1859. printf("SD: data read successful\r\n");
  1860. #endif
  1861. return buf_size;
  1862. }
  1863. #ifdef SD_WRITE_SUPPORT
  1864. int sd_write(struct block_device *dev, uint8_t *buf, size_t buf_size, uint32_t block_no)
  1865. {
  1866. // Check the status of the card
  1867. struct emmc_block_dev *edev = (struct emmc_block_dev *)dev;
  1868. if(sd_ensure_data_mode(edev) != 0)
  1869. return -1;
  1870. #ifdef EMMC_DEBUG
  1871. printf("SD: write() card ready, writing to block %u\r\n", block_no);
  1872. #endif
  1873. if(sd_do_data_command(edev, 1, buf, buf_size, block_no) < 0)
  1874. return -1;
  1875. #ifdef EMMC_DEBUG
  1876. printf("SD: data write successful\r\n");
  1877. #endif
  1878. return buf_size;
  1879. }
  1880. #endif