2013-08-06 00:21:24 +02:00
|
|
|
/****************************************************************************
|
2020-03-07 12:36:39 +01:00
|
|
|
* boards/arm/sama5/sama5d3x-ek/src/sam_hsmci.c
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
2021-03-17 18:14:12 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
2021-03-17 18:14:12 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
2021-03-17 18:14:12 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2019-08-15 18:19:17 +02:00
|
|
|
|
|
|
|
/* The SAMA5D3x-EK provides a two SD memory card slots:
|
|
|
|
* (1) a full size SD card slot (J7 labeled MCI0), and
|
|
|
|
* (2) a microSD memory card slot (J6 labeled MCI1).
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
2019-08-15 18:19:17 +02:00
|
|
|
* The full size SD card slot connects via HSMCI0.
|
|
|
|
* The card detect discrete is available on PB17 (pulled high).
|
|
|
|
* The write protect descrete is tied to ground (via PP6) and not available
|
|
|
|
* to software.
|
|
|
|
* The slot supports 8-bit wide transfer mode, but the NuttX driver
|
|
|
|
* currently uses only the 4-bit wide transfer mode
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
|
|
|
* PD17 MCI0_CD
|
|
|
|
* PD1 MCI0_DA0
|
|
|
|
* PD2 MCI0_DA1
|
|
|
|
* PD3 MCI0_DA2
|
|
|
|
* PD4 MCI0_DA3
|
|
|
|
* PD5 MCI0_DA4
|
|
|
|
* PD6 MCI0_DA5
|
|
|
|
* PD7 MCI0_DA6
|
|
|
|
* PD8 MCI0_DA7
|
|
|
|
* PD9 MCI0_CK
|
|
|
|
* PD0 MCI0_CDA
|
|
|
|
*
|
2019-08-15 18:19:17 +02:00
|
|
|
* The microSD connects vi HSMCI1.
|
|
|
|
* The card detect discrete is available on PB18 (pulled high):
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
|
|
|
* PD18 MCI1_CD
|
|
|
|
* PB20 MCI1_DA0
|
|
|
|
* PB21 MCI1_DA1
|
|
|
|
* PB22 MCI1_DA2
|
|
|
|
* PB23 MCI1_DA3
|
|
|
|
* PB24 MCI1_CK
|
|
|
|
* PB19 MCI1_CDA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/sdio.h>
|
|
|
|
#include <nuttx/mmcsd.h>
|
|
|
|
|
2013-08-06 18:20:17 +02:00
|
|
|
#include "sam_pio.h"
|
2013-08-06 00:21:24 +02:00
|
|
|
#include "sam_hsmci.h"
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-06 00:21:24 +02:00
|
|
|
#include "sama5d3x-ek.h"
|
|
|
|
|
2013-09-11 17:50:36 +02:00
|
|
|
#ifdef HAVE_HSMCI
|
2013-08-07 19:31:31 +02:00
|
|
|
|
2013-08-06 00:21:24 +02:00
|
|
|
/****************************************************************************
|
2013-08-06 18:20:17 +02:00
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2019-08-15 18:19:17 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
/* This structure holds static information unique to one HSMCI peripheral */
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
struct sam_hsmci_state_s
|
2013-08-06 18:20:17 +02:00
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
struct sdio_dev_s *hsmci; /* R/W device handle */
|
2020-02-23 09:50:23 +01:00
|
|
|
pio_pinset_t pincfg; /* Card detect PIO pin configuration */
|
2013-08-06 19:29:53 +02:00
|
|
|
uint8_t irq; /* Interrupt number (same as pid) */
|
2013-08-11 19:13:11 +02:00
|
|
|
uint8_t slotno; /* Slot number */
|
|
|
|
bool cd; /* TRUE: card is inserted */
|
2013-08-06 19:29:53 +02:00
|
|
|
xcpt_t handler; /* Interrupt handler */
|
2013-08-06 18:20:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
2013-08-06 00:21:24 +02:00
|
|
|
****************************************************************************/
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
/* HSCMI device state */
|
2013-08-06 18:20:17 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_HSMCI0
|
2017-02-27 13:27:56 +01:00
|
|
|
static int sam_hsmci0_cardetect(int irq, void *regs, FAR void *arg);
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
static struct sam_hsmci_state_s g_hsmci0 =
|
2013-08-06 18:20:17 +02:00
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
.pincfg = PIO_MCI0_CD,
|
|
|
|
.irq = IRQ_MCI0_CD,
|
|
|
|
.slotno = 0,
|
|
|
|
.handler = sam_hsmci0_cardetect,
|
2013-08-06 18:20:17 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_HSMCI1
|
2017-02-27 13:27:56 +01:00
|
|
|
static int sam_hsmci1_cardetect(int irq, void *regs, FAR void *arg);
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
static struct sam_hsmci_state_s g_hsmci1 =
|
2013-08-06 18:20:17 +02:00
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
.pincfg = PIO_MCI1_CD,
|
|
|
|
.irq = IRQ_MCI1_CD,
|
|
|
|
.slotno = 1,
|
|
|
|
.handler = sam_hsmci1_cardetect,
|
2013-08-06 18:20:17 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-08-11 19:13:11 +02:00
|
|
|
* Name: sam_cardinserted_internal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if a card is inserted into the selected HSMCI slot
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool sam_cardinserted_internal(struct sam_hsmci_state_s *state)
|
|
|
|
{
|
|
|
|
bool inserted;
|
|
|
|
|
|
|
|
/* Get the state of the PIO pin */
|
|
|
|
|
|
|
|
inserted = sam_pioread(state->pincfg);
|
2016-06-20 19:59:15 +02:00
|
|
|
finfo("Slot %d inserted: %s\n", state->slotno, inserted ? "NO" : "YES");
|
2013-08-11 19:13:11 +02:00
|
|
|
return !inserted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sam_hsmci_cardetect, sam_hsmci0_cardetect, and sam_hsmci1_cardetect
|
2013-08-06 18:20:17 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Card detect interrupt handlers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
static int sam_hsmci_cardetect(struct sam_hsmci_state_s *state)
|
|
|
|
{
|
|
|
|
/* Get the current card insertion state */
|
|
|
|
|
|
|
|
bool cd = sam_cardinserted_internal(state);
|
|
|
|
|
|
|
|
/* Has the card detect state changed? */
|
|
|
|
|
|
|
|
if (cd != state->cd)
|
|
|
|
{
|
|
|
|
/* Yes... remember that new state and inform the HSMCI driver */
|
|
|
|
|
|
|
|
state->cd = cd;
|
|
|
|
|
|
|
|
/* Report the new state to the SDIO driver */
|
|
|
|
|
|
|
|
sdio_mediachange(state->hsmci, cd);
|
|
|
|
}
|
|
|
|
|
2019-08-15 18:19:17 +02:00
|
|
|
return OK;
|
2013-08-11 19:13:11 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 18:20:17 +02:00
|
|
|
#ifdef CONFIG_SAMA5_HSMCI0
|
2017-02-27 13:27:56 +01:00
|
|
|
static int sam_hsmci0_cardetect(int irq, void *regs, FAR void *arg)
|
2013-08-06 18:20:17 +02:00
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
return sam_hsmci_cardetect(&g_hsmci0);
|
2013-08-06 18:20:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_HSMCI1
|
2017-02-27 13:27:56 +01:00
|
|
|
static int sam_hsmci1_cardetect(int irq, void *regs, FAR void *arg)
|
2013-08-06 18:20:17 +02:00
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
return sam_hsmci_cardetect(&g_hsmci1);
|
2013-08-06 18:20:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-08-11 19:13:11 +02:00
|
|
|
* Name: sam_hsmci_state
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2013-08-06 18:20:17 +02:00
|
|
|
* Initialize HSMCI PIOs.
|
2013-08-06 00:21:24 +02:00
|
|
|
*
|
2013-08-06 18:20:17 +02:00
|
|
|
****************************************************************************/
|
2013-08-06 00:21:24 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
static struct sam_hsmci_state_s *sam_hsmci_state(int slotno)
|
2013-08-06 00:21:24 +02:00
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
struct sam_hsmci_state_s *state = NULL;
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-06 00:21:24 +02:00
|
|
|
#ifdef CONFIG_SAMA5_HSMCI0
|
|
|
|
#ifdef CONFIG_SAMA5_HSMCI1
|
|
|
|
if (slotno == 0)
|
|
|
|
#endif
|
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
state = &g_hsmci0;
|
2013-08-06 00:21:24 +02:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_SAMA5_HSMCI1
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_HSMCI1
|
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
state = &g_hsmci1;
|
2013-08-06 00:21:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
return state;
|
2013-08-06 00:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sam_hsmci_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform architecture specific initialization
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int sam_hsmci_initialize(int slotno, int minor)
|
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
struct sam_hsmci_state_s *state;
|
2013-08-06 00:21:24 +02:00
|
|
|
int ret;
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
/* Get the static HSMI description */
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
state = sam_hsmci_state(slotno);
|
|
|
|
if (!state)
|
2013-08-06 18:20:17 +02:00
|
|
|
{
|
2016-06-13 15:43:55 +02:00
|
|
|
ferr("ERROR: No state for slotno %d\n", slotno);
|
2013-08-06 18:20:17 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize card-detect and write-protect PIOs */
|
2013-08-06 00:21:24 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
sam_configpio(state->pincfg);
|
2013-08-06 00:21:24 +02:00
|
|
|
|
|
|
|
/* Mount the SDIO-based MMC/SD block driver */
|
2019-08-15 18:19:17 +02:00
|
|
|
|
2013-08-06 00:21:24 +02:00
|
|
|
/* First, get an instance of the SDIO interface */
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
state->hsmci = sdio_initialize(slotno);
|
|
|
|
if (!state->hsmci)
|
2013-08-06 00:21:24 +02:00
|
|
|
{
|
2016-06-13 15:43:55 +02:00
|
|
|
ferr("ERROR: Failed to initialize SDIO slot %d\n", slotno);
|
2013-08-06 00:21:24 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now bind the SDIO interface to the MMC/SD driver */
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
ret = mmcsd_slotinitialize(minor, state->hsmci);
|
2013-08-06 00:21:24 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-13 15:43:55 +02:00
|
|
|
ferr("ERROR: Failed to bind SDIO to the MMC/SD driver: %d\n", ret);
|
2013-08-06 00:21:24 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-06 18:20:17 +02:00
|
|
|
/* Configure card detect interrupts */
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
sam_pioirq(state->pincfg);
|
2020-01-02 17:49:34 +01:00
|
|
|
irq_attach(state->irq, state->handler, NULL);
|
2013-08-06 18:20:17 +02:00
|
|
|
|
2013-08-06 00:21:24 +02:00
|
|
|
/* Then inform the HSMCI driver if there is or is not a card in the slot. */
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
state->cd = sam_cardinserted_internal(state);
|
|
|
|
sdio_mediachange(state->hsmci, state->cd);
|
|
|
|
|
|
|
|
/* Enable card detect interrupts */
|
2013-08-06 00:21:24 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
sam_pioirqenable(state->irq);
|
2013-08-06 00:21:24 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-08-06 18:20:17 +02:00
|
|
|
/****************************************************************************
|
2013-08-06 00:21:24 +02:00
|
|
|
* Name: sam_cardinserted
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if a card is inserted into the selected HSMCI slot
|
|
|
|
*
|
2013-08-06 18:20:17 +02:00
|
|
|
****************************************************************************/
|
2013-08-06 00:21:24 +02:00
|
|
|
|
|
|
|
bool sam_cardinserted(int slotno)
|
|
|
|
{
|
2013-08-11 19:13:11 +02:00
|
|
|
struct sam_hsmci_state_s *state;
|
2013-08-06 00:21:24 +02:00
|
|
|
|
2013-08-06 18:20:17 +02:00
|
|
|
/* Get the HSMI description */
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
state = sam_hsmci_state(slotno);
|
|
|
|
if (!state)
|
2013-08-06 00:21:24 +02:00
|
|
|
{
|
2016-06-13 15:43:55 +02:00
|
|
|
ferr("ERROR: No state for slotno %d\n", slotno);
|
2013-08-06 18:20:17 +02:00
|
|
|
return false;
|
2013-08-06 00:21:24 +02:00
|
|
|
}
|
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
/* Return the state of the PIO pin */
|
2013-08-06 00:21:24 +02:00
|
|
|
|
2013-08-11 19:13:11 +02:00
|
|
|
return sam_cardinserted_internal(state);
|
2013-08-06 00:21:24 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 18:20:17 +02:00
|
|
|
/****************************************************************************
|
2013-08-06 00:21:24 +02:00
|
|
|
* Name: sam_writeprotected
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if a card is inserted into the selected HSMCI slot
|
|
|
|
*
|
2013-08-06 18:20:17 +02:00
|
|
|
****************************************************************************/
|
2013-08-06 00:21:24 +02:00
|
|
|
|
|
|
|
bool sam_writeprotected(int slotno)
|
|
|
|
{
|
|
|
|
/* There are no write protect pins */
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-11 17:50:36 +02:00
|
|
|
#endif /* HAVE_HSMCI */
|