* Select the bus width among 4-bit and 8-bit(SDR). * If the bus width is changed successfully, return the selected width value. * Zero is returned instead of error value if the wide width is not supported. */
| 325 | * Zero is returned instead of error value if the wide width is not supported. |
| 326 | */ |
| 327 | static int mmc_select_bus_width(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd) |
| 328 | { |
| 329 | rt_uint32_t ext_csd_bits[][2] = |
| 330 | { |
| 331 | {EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8}, |
| 332 | {EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4}, |
| 333 | {EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1}, |
| 334 | }; |
| 335 | rt_uint32_t bus_widths[] = |
| 336 | { |
| 337 | MMCSD_BUS_WIDTH_8, |
| 338 | MMCSD_BUS_WIDTH_4, |
| 339 | MMCSD_BUS_WIDTH_1 |
| 340 | }; |
| 341 | struct rt_mmcsd_host *host = card->host; |
| 342 | unsigned idx, bus_width = 0; |
| 343 | int err = 0, ddr = 0; |
| 344 | |
| 345 | if (GET_BITS(card->resp_csd, 122, 4) < 4) |
| 346 | return 0; |
| 347 | |
| 348 | if (card->flags & CARD_FLAG_HIGHSPEED_DDR) |
| 349 | { |
| 350 | ddr = 2; |
| 351 | } |
| 352 | /* |
| 353 | * Unlike SD, MMC cards don't have a configuration register to notify |
| 354 | * supported bus width. So bus test command should be run to identify |
| 355 | * the supported bus width or compare the EXT_CSD values of current |
| 356 | * bus width and EXT_CSD values of 1 bit mode read earlier. |
| 357 | */ |
| 358 | for (idx = 0; idx < sizeof(bus_widths) / sizeof(rt_uint32_t); idx++) |
| 359 | { |
| 360 | /* |
| 361 | * Determine BUS WIDTH mode according to the capability of host |
| 362 | */ |
| 363 | if (((ext_csd_bits[idx][0] == EXT_CSD_BUS_WIDTH_8) && ((host->flags & MMCSD_BUSWIDTH_8) == 0)) || |
| 364 | ((ext_csd_bits[idx][0] == EXT_CSD_BUS_WIDTH_4) && ((host->flags & MMCSD_BUSWIDTH_4) == 0))) |
| 365 | { |
| 366 | continue; |
| 367 | } |
| 368 | bus_width = bus_widths[idx]; |
| 369 | if (bus_width == MMCSD_BUS_WIDTH_1) |
| 370 | { |
| 371 | ddr = 0; |
| 372 | } |
| 373 | |
| 374 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 375 | EXT_CSD_BUS_WIDTH, |
| 376 | ext_csd_bits[idx][0]); |
| 377 | |
| 378 | if (err) |
| 379 | continue; |
| 380 | |
| 381 | mmcsd_set_bus_width(host, bus_width); |
| 382 | err = mmc_compare_ext_csds(card, ext_csd, bus_width); |
| 383 | if (!err) |
| 384 | { |
no test coverage detected