Lines Matching +full:card +full:- +full:detect
4 * SPDX-License-Identifier: Apache-2.0
80 const struct sam_hsmci_config *config = dev->config; in sam_hsmci_reset()
81 Hsmci *hsmci = config->base; in sam_hsmci_reset()
83 uint32_t mr = hsmci->HSMCI_MR; in sam_hsmci_reset()
84 uint32_t dtor = hsmci->HSMCI_DTOR; in sam_hsmci_reset()
85 uint32_t sdcr = hsmci->HSMCI_SDCR; in sam_hsmci_reset()
86 uint32_t cstor = hsmci->HSMCI_CSTOR; in sam_hsmci_reset()
87 uint32_t cfg = hsmci->HSMCI_CFG; in sam_hsmci_reset()
89 hsmci->HSMCI_CR = HSMCI_CR_SWRST; in sam_hsmci_reset()
90 hsmci->HSMCI_MR = mr; in sam_hsmci_reset()
91 hsmci->HSMCI_DTOR = dtor; in sam_hsmci_reset()
92 hsmci->HSMCI_SDCR = sdcr; in sam_hsmci_reset()
93 hsmci->HSMCI_CSTOR = cstor; in sam_hsmci_reset()
94 hsmci->HSMCI_CFG = cfg; in sam_hsmci_reset()
96 hsmci->HSMCI_CR = HSMCI_CR_PWSEN | HSMCI_CR_MCIEN; in sam_hsmci_reset()
104 props->f_max = _HSMCI_MAX_FREQ; in sam_hsmci_get_host_props()
105 props->f_min = _HSMCI_MIN_FREQ; in sam_hsmci_get_host_props()
106 /* high-speed not working yet due to limitations of the SDHC sm */ in sam_hsmci_get_host_props()
107 props->host_caps.high_spd_support = false; in sam_hsmci_get_host_props()
108 props->power_delay = 500; in sam_hsmci_get_host_props()
109 props->is_spi = false; in sam_hsmci_get_host_props()
110 props->max_current_330 = 4; in sam_hsmci_get_host_props()
117 const struct sam_hsmci_config *config = dev->config; in sam_hsmci_set_io()
118 struct sam_hsmci_data *data = dev->data; in sam_hsmci_set_io()
119 Hsmci *hsmci = config->base; in sam_hsmci_set_io()
124 LOG_DBG("%s(clock=%d, bus_width=%d, timing=%d, mode=%d)", __func__, ios->clock, in sam_hsmci_set_io()
125 ios->bus_width, ios->timing, ios->bus_mode); in sam_hsmci_set_io()
127 if (ios->clock > 0) { in sam_hsmci_set_io()
128 if (ios->clock > _HSMCI_MAX_FREQ) { in sam_hsmci_set_io()
129 return -ENOTSUP; in sam_hsmci_set_io()
133 (clock_control_subsys_t)&config->clock_cfg, in sam_hsmci_set_io()
141 div_val = frequency / ios->clock - 2; in sam_hsmci_set_io()
153 hsmci->HSMCI_MR &= ~HSMCI_MR_CLKDIV_Msk; in sam_hsmci_set_io()
154 hsmci->HSMCI_MR |= in sam_hsmci_set_io()
158 if (ios->bus_width) { in sam_hsmci_set_io()
159 hsmci->HSMCI_SDCR &= ~HSMCI_SDCR_SDCBUS_Msk; in sam_hsmci_set_io()
161 switch (ios->bus_width) { in sam_hsmci_set_io()
163 hsmci->HSMCI_SDCR = HSMCI_SDCR_SDCBUS_1; in sam_hsmci_set_io()
166 hsmci->HSMCI_SDCR = HSMCI_SDCR_SDCBUS_4; in sam_hsmci_set_io()
169 return -ENOTSUP; in sam_hsmci_set_io()
173 data->open_drain = (ios->bus_mode == SDHC_BUSMODE_OPENDRAIN); in sam_hsmci_set_io()
175 if (ios->timing) { in sam_hsmci_set_io()
176 switch (ios->timing) { in sam_hsmci_set_io()
178 hsmci->HSMCI_CFG &= ~HSMCI_CFG_HSMODE; in sam_hsmci_set_io()
181 hsmci->HSMCI_CFG |= HSMCI_CFG_HSMODE; in sam_hsmci_set_io()
184 return -ENOTSUP; in sam_hsmci_set_io()
193 const struct sam_hsmci_config *config = dev->config; in sam_hsmci_init()
197 ret = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT); in sam_hsmci_init()
203 (void)clock_control_on(SAM_DT_PMC_CONTROLLER, (clock_control_subsys_t)&config->clock_cfg); in sam_hsmci_init()
205 /* init carrier detect (if set) */ in sam_hsmci_init()
206 if (config->carrier_detect.port != NULL) { in sam_hsmci_init()
207 if (!gpio_is_ready_dt(&config->carrier_detect)) { in sam_hsmci_init()
208 LOG_ERR("GPIO port for carrier-detect pin is not ready"); in sam_hsmci_init()
209 return -ENODEV; in sam_hsmci_init()
211 ret = gpio_pin_configure_dt(&config->carrier_detect, GPIO_INPUT); in sam_hsmci_init()
213 LOG_ERR("Couldn't configure carrier-detect pin; (%d)", ret); in sam_hsmci_init()
218 Hsmci *hsmci = config->base; in sam_hsmci_init()
221 hsmci->HSMCI_CR = HSMCI_CR_SWRST; in sam_hsmci_init()
222 hsmci->HSMCI_CR = HSMCI_CR_PWSDIS; in sam_hsmci_init()
223 hsmci->HSMCI_CR = HSMCI_CR_MCIEN; in sam_hsmci_init()
225 hsmci->HSMCI_MR = in sam_hsmci_init()
227 hsmci->HSMCI_CR = HSMCI_CR_PWSEN; in sam_hsmci_init()
229 hsmci->HSMCI_MR = HSMCI_MR_RDPROOF | HSMCI_MR_WRPROOF; in sam_hsmci_init()
237 const struct sam_hsmci_config *config = dev->config; in sam_hsmci_get_card_present()
239 if (config->carrier_detect.port == NULL) { in sam_hsmci_get_card_present()
243 return gpio_pin_get_dt(&config->carrier_detect); in sam_hsmci_get_card_present()
248 const struct sam_hsmci_config *config = dev->config; in sam_hsmci_card_busy()
249 Hsmci *hsmci = config->base; in sam_hsmci_card_busy()
251 return (hsmci->HSMCI_SR & HSMCI_SR_NOTBUSY) == 0; in sam_hsmci_card_busy()
256 hsmci->HSMCI_MR &= ~(HSMCI_MR_WRPROOF | HSMCI_MR_RDPROOF | HSMCI_MR_FBYTE); in sam_hsmci_send_clocks()
257 hsmci->HSMCI_ARGR = 0; in sam_hsmci_send_clocks()
258 hsmci->HSMCI_CMDR = in sam_hsmci_send_clocks()
260 while (!(hsmci->HSMCI_SR & HSMCI_SR_CMDRDY)) { in sam_hsmci_send_clocks()
263 hsmci->HSMCI_MR |= HSMCI_MR_WRPROOF | HSMCI_MR_RDPROOF; in sam_hsmci_send_clocks()
271 hsmci->HSMCI_ARGR = cmd->arg; in sam_hsmci_send_cmd()
273 cmdr |= HSMCI_CMDR_CMDNB(cmd->opcode) | HSMCI_CMDR_MAXLAT_64; in sam_hsmci_send_cmd()
274 if (data->open_drain) { in sam_hsmci_send_cmd()
278 uint8_t nrt = cmd->response_type & SDHC_NATIVE_RESPONSE_MASK; in sam_hsmci_send_cmd()
281 return -ENOTSUP; in sam_hsmci_send_cmd()
285 hsmci->HSMCI_CMDR = cmdr; in sam_hsmci_send_cmd()
287 sr = hsmci->HSMCI_SR; in sam_hsmci_send_cmd()
296 return -EIO; in sam_hsmci_send_cmd()
302 sr = hsmci->HSMCI_SR; in sam_hsmci_send_cmd()
307 cmd->response[3] = hsmci->HSMCI_RSPR[0]; in sam_hsmci_send_cmd()
308 cmd->response[2] = hsmci->HSMCI_RSPR[0]; in sam_hsmci_send_cmd()
309 cmd->response[1] = hsmci->HSMCI_RSPR[0]; in sam_hsmci_send_cmd()
310 cmd->response[0] = hsmci->HSMCI_RSPR[0]; in sam_hsmci_send_cmd()
321 sr = hsmci->HSMCI_SR; in sam_hsmci_wait_write_end()
324 return -EIO; in sam_hsmci_wait_write_end()
330 sr = hsmci->HSMCI_SR; in sam_hsmci_wait_write_end()
333 return -EIO; in sam_hsmci_wait_write_end()
337 if (!(hsmci->HSMCI_SR & HSMCI_SR_FIFOEMPTY)) { in sam_hsmci_wait_write_end()
338 return -EIO; in sam_hsmci_wait_write_end()
349 sr = hsmci->HSMCI_SR; in sam_hsmci_wait_read_end()
353 return -EIO; in sam_hsmci_wait_read_end()
359 sr = hsmci->HSMCI_SR; in sam_hsmci_wait_read_end()
361 return -EIO; in sam_hsmci_wait_read_end()
377 hsmci->HSMCI_DTOR = ((i << HSMCI_DTOR_DTOMUL_Pos) & HSMCI_DTOR_DTOMUL_Msk) | in sam_hsmci_write_timeout()
378 HSMCI_DTOR_DTOCYC((clocks + mul - 1) / mul); in sam_hsmci_write_timeout()
386 hsmci->HSMCI_DTOR = HSMCI_DTOR_DTOMUL_Msk | HSMCI_DTOR_DTOCYC_Msk; in sam_hsmci_write_timeout()
395 sr = hsmci->HSMCI_SR; in wait_write_transfer_done()
397 return -EIO; in wait_write_transfer_done()
408 sr = HSMCI->HSMCI_SR; in wait_read_transfer_done()
410 return -EIO; in wait_read_transfer_done()
427 while (transfer_count-- > 0) { in hsmci_do_manual_transfer()
432 hsmci->HSMCI_TDR = *ptr; in hsmci_do_manual_transfer()
438 while (transfer_count-- > 0) { in hsmci_do_manual_transfer()
443 hsmci->HSMCI_TDR = *ptr; in hsmci_do_manual_transfer()
452 while (transfer_count-- > 0) { in hsmci_do_manual_transfer()
457 *ptr = hsmci->HSMCI_RDR; in hsmci_do_manual_transfer()
463 while (transfer_count-- > 0) { in hsmci_do_manual_transfer()
468 *ptr = hsmci->HSMCI_RDR; in hsmci_do_manual_transfer()
482 const struct sam_hsmci_config *config = dev->config; in sam_hsmci_request_inner()
483 struct sam_hsmci_data *data = dev->data; in sam_hsmci_request_inner()
484 Hsmci *hsmci = config->base; in sam_hsmci_request_inner()
492 LOG_DBG("%s(opcode=%d, arg=%08x, data=%08x, rsptype=%d)", __func__, cmd->opcode, cmd->arg, in sam_hsmci_request_inner()
493 (uint32_t)sd_data, cmd->response_type & SDHC_NATIVE_RESPONSE_MASK); in sam_hsmci_request_inner()
495 if (cmd->opcode == SD_GO_IDLE_STATE) { in sam_hsmci_request_inner()
503 ret = sam_hsmci_write_timeout(hsmci, cmd->timeout_ms); in sam_hsmci_request_inner()
508 switch (cmd->opcode) { in sam_hsmci_request_inner()
535 return -ENOTSUP; in sam_hsmci_request_inner()
538 if ((sd_data->block_size & 0x3) == 0 && (((uint32_t)sd_data->data) & 0x3) == 0) { in sam_hsmci_request_inner()
539 size = (sd_data->block_size + 3) >> 2; in sam_hsmci_request_inner()
540 hsmci->HSMCI_MR &= ~HSMCI_MR_FBYTE; in sam_hsmci_request_inner()
543 size = sd_data->block_size; in sam_hsmci_request_inner()
544 hsmci->HSMCI_MR |= HSMCI_MR_FBYTE; in sam_hsmci_request_inner()
548 hsmci->HSMCI_BLKR = in sam_hsmci_request_inner()
549 HSMCI_BLKR_BLKLEN(sd_data->block_size) | HSMCI_BLKR_BCNT(sd_data->blocks); in sam_hsmci_request_inner()
551 transfer_count = size * sd_data->blocks; in sam_hsmci_request_inner()
554 hsmci->HSMCI_MR |= HSMCI_MR_PDCMODE; in sam_hsmci_request_inner()
556 hsmci->HSMCI_RNCR = 0; in sam_hsmci_request_inner()
559 hsmci->HSMCI_TCR = transfer_count; in sam_hsmci_request_inner()
560 hsmci->HSMCI_TPR = (uint32_t)sd_data->data; in sam_hsmci_request_inner()
562 hsmci->HSMCI_RCR = transfer_count; in sam_hsmci_request_inner()
563 hsmci->HSMCI_RPR = (uint32_t)sd_data->data; in sam_hsmci_request_inner()
564 hsmci->HSMCI_PTCR = HSMCI_PTCR_RXTEN; in sam_hsmci_request_inner()
568 hsmci->HSMCI_MR &= ~HSMCI_MR_PDCMODE; in sam_hsmci_request_inner()
578 hsmci->HSMCI_PTCR = HSMCI_PTCR_TXTEN; in sam_hsmci_request_inner()
584 hsmci->HSMCI_PTCR = HSMCI_PTCR_TXTDIS | HSMCI_PTCR_RXTDIS; in sam_hsmci_request_inner()
585 hsmci->HSMCI_MR &= ~HSMCI_MR_PDCMODE; in sam_hsmci_request_inner()
588 ret = hsmci_do_manual_transfer(hsmci, byte_mode, is_write, sd_data->data, in sam_hsmci_request_inner()
594 sr = hsmci->HSMCI_SR; in sam_hsmci_request_inner()
596 LOG_DBG("RSP0=%08x, RPS1=%08x, RPS2=%08x,RSP3=%08x, SR=%08x", cmd->response[0], in sam_hsmci_request_inner()
597 cmd->response[1], cmd->response[2], cmd->response[3], sr); in sam_hsmci_request_inner()
605 const struct sam_hsmci_config *config = dev->config; in sam_hsmci_abort()
606 Hsmci *hsmci = config->base; in sam_hsmci_abort()
608 hsmci->HSMCI_PTCR = HSMCI_PTCR_RXTDIS | HSMCI_PTCR_TXTDIS; in sam_hsmci_abort()
619 struct sam_hsmci_data *dev_data = dev->data; in sam_hsmci_request()
623 ret = k_mutex_lock(&dev_data->mtx, K_MSEC(cmd->timeout_ms)); in sam_hsmci_request()
625 LOG_ERR("Could not access card"); in sam_hsmci_request()
626 return -EBUSY; in sam_hsmci_request()
630 const struct sam_hsmci_config *config = dev->config; in sam_hsmci_request()
631 Hsmci *hsmci = config->base; in sam_hsmci_request()
633 hsmci->HSMCI_CR = HSMCI_CR_PWSDIS; in sam_hsmci_request()
638 if (sd_data && (ret || sd_data->blocks > 1)) { in sam_hsmci_request()
645 busy_timeout -= 125; in sam_hsmci_request()
648 LOG_ERR("Card did not idle after CMD12"); in sam_hsmci_request()
649 ret = -ETIMEDOUT; in sam_hsmci_request()
652 } while (ret != 0 && (cmd->retries-- > 0)); in sam_hsmci_request()
655 hsmci->HSMCI_CR = HSMCI_CR_PWSEN; in sam_hsmci_request()
658 k_mutex_unlock(&dev_data->mtx); in sam_hsmci_request()