2008-10-15 16:18:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/mmcsd/mmcsd_spi.c
|
|
|
|
*
|
2020-03-21 17:36:26 +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
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
2020-03-21 17:36:26 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
2020-03-21 17:36:26 +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.
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2012-11-06 15:57:01 +01:00
|
|
|
|
|
|
|
#if defined (CONFIG_MMCSD) && defined (CONFIG_MMCSD_SPI)
|
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-11-22 14:12:03 +01:00
|
|
|
#include <inttypes.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-10-15 16:18:17 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2009-05-26 18:07:25 +02:00
|
|
|
#include <time.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2008-10-15 16:18:17 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2018-11-09 14:46:16 +01:00
|
|
|
#include <unistd.h>
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2009-05-26 18:07:25 +02:00
|
|
|
#include <nuttx/clock.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2018-11-09 14:51:43 +01:00
|
|
|
#include <nuttx/signal.h>
|
2013-07-01 16:11:54 +02:00
|
|
|
#include <nuttx/spi/spi.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2008-10-15 16:18:17 +02:00
|
|
|
#include <nuttx/mmcsd.h>
|
|
|
|
|
|
|
|
#include "mmcsd_spi.h"
|
|
|
|
#include "mmcsd_csd.h"
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "mmcsd.h"
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 15:15:32 +02:00
|
|
|
* Pre-processor Definitions
|
2008-10-15 16:18:17 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2008-10-15 16:18:17 +02:00
|
|
|
# define MMCSD_MODE 0666
|
|
|
|
#else
|
|
|
|
# define MMCSD_MODE 0444
|
|
|
|
#endif
|
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
#ifndef CONFIG_MMCSD_SECTOR512
|
|
|
|
# define CONFIG_MMCSD_SECTOR512 /* Force 512 byte sectors on all cards */
|
|
|
|
#endif
|
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/* Slot struct info *********************************************************/
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/* Slot status definitions */
|
|
|
|
|
|
|
|
#define MMCSD_SLOTSTATUS_NOTREADY 0x01 /* Card not initialized */
|
|
|
|
#define MMCSD_SLOTSTATUS_NODISK 0x02 /* No card in the slot */
|
|
|
|
#define MMCSD_SLOTSTATUS_WRPROTECT 0x04 /* Card is write protected */
|
|
|
|
#define MMCSD_SLOTSTATUS_MEDIACHGD 0x08 /* Media changed in slot */
|
|
|
|
|
|
|
|
/* Values in the MMC/SD command table ***************************************/
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/* These define the value returned by the MMC/SD command */
|
|
|
|
|
|
|
|
#define MMCSD_CMDRESP_R1 0
|
|
|
|
#define MMCSD_CMDRESP_R1B 1
|
|
|
|
#define MMCSD_CMDRESP_R2 2
|
|
|
|
#define MMCSD_CMDRESP_R3 3
|
2009-05-26 18:07:25 +02:00
|
|
|
#define MMCSD_CMDRESP_R7 4
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
#ifdef CONFIG_MMCSD_SECTOR512
|
|
|
|
# define SECTORSIZE(s) (512)
|
|
|
|
#else
|
|
|
|
# define SECTORSIZE(s) ((s)->sectorsize)
|
|
|
|
#endif
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Time delays in units of the system clock. CLK_TCK is the number of clock
|
|
|
|
* ticks per second.
|
2008-10-15 16:18:17 +02:00
|
|
|
*/
|
|
|
|
|
2010-07-16 05:01:05 +02:00
|
|
|
#define MMCSD_DELAY_10MS (CLK_TCK/100 + 1)
|
|
|
|
#define MMCSD_DELAY_50MS (CLK_TCK/20 + 1)
|
|
|
|
#define MMCSD_DELAY_100MS (CLK_TCK/10 + 1)
|
|
|
|
#define MMCSD_DELAY_250MS (CLK_TCK/4 + 1)
|
|
|
|
#define MMCSD_DELAY_500MS (CLK_TCK/2 + 1)
|
|
|
|
#define MMCSD_DELAY_1SEC (CLK_TCK + 1)
|
2009-05-27 22:45:39 +02:00
|
|
|
#define MMCSD_DELAY_10SEC (10 * CLK_TCK + 1)
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
#define ELAPSED_TIME(t) (clock_systime_ticks()-(t))
|
|
|
|
#define START_TIME (clock_systime_ticks())
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
/* SD read timeout: ~100msec, Write Time out ~250ms. Units of clock ticks */
|
|
|
|
|
|
|
|
#define SD_READACCESS MMCSD_DELAY_100MS
|
|
|
|
#define SD_WRITEACCESS MMCSD_DELAY_250MS
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure represents the state of one card slot */
|
|
|
|
|
|
|
|
struct mmcsd_slot_s
|
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi; /* SPI port bound to this slot */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock; /* Assures mutually exclusive access to card and SPI */
|
2014-04-07 23:28:04 +02:00
|
|
|
uint8_t state; /* State of the slot (see MMCSD_SLOTSTATUS_* definitions) */
|
|
|
|
uint8_t type; /* Disk type */
|
|
|
|
uint8_t csd[16]; /* Copy of card CSD */
|
2009-05-28 19:51:24 +02:00
|
|
|
#ifndef CONFIG_MMCSD_SECTOR512
|
2014-04-07 23:28:04 +02:00
|
|
|
uint16_t sectorsize; /* Media block size (in bytes) */
|
2009-05-28 19:51:24 +02:00
|
|
|
#endif
|
2014-04-07 23:28:04 +02:00
|
|
|
uint32_t nsectors; /* Number of blocks on the media */
|
|
|
|
uint32_t taccess; /* Card access time */
|
|
|
|
uint32_t twrite; /* Card write time */
|
|
|
|
uint32_t ocr; /* Last 4 bytes of OCR (R3) */
|
|
|
|
uint32_t r7; /* Last 4 bytes of R7 */
|
|
|
|
uint32_t spispeed; /* Speed to use for SPI in data mode */
|
2008-10-15 16:18:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mmcsd_cmdinfo_s
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t cmd;
|
|
|
|
uint8_t resp;
|
|
|
|
uint8_t chksum;
|
2008-10-15 16:18:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-10-15 21:15:53 +02:00
|
|
|
/* Misc *********************************************************************/
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int mmcsd_lock(FAR struct mmcsd_slot_s *slot);
|
|
|
|
static void mmcsd_unlock(FAR struct mmcsd_slot_s *slot);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Card SPI interface *******************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_waitready(FAR struct mmcsd_slot_s *slot);
|
|
|
|
static uint32_t mmcsd_sendcmd(FAR struct mmcsd_slot_s *slot,
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR const struct mmcsd_cmdinfo_s *cmd,
|
|
|
|
uint32_t arg);
|
2009-12-15 15:25:14 +01:00
|
|
|
static void mmcsd_setblklen(FAR struct mmcsd_slot_s *slot,
|
2024-08-25 01:21:12 +02:00
|
|
|
uint32_t length);
|
|
|
|
static uint32_t mmcsd_nsac(FAR struct mmcsd_slot_s *slot, FAR uint8_t *csd,
|
|
|
|
uint32_t frequency);
|
|
|
|
static uint32_t mmcsd_taac(FAR struct mmcsd_slot_s *slot, FAR uint8_t *csd);
|
|
|
|
static void mmcsd_decodecsd(FAR struct mmcsd_slot_s *slot,
|
|
|
|
FAR uint8_t *csd);
|
2009-12-15 15:25:14 +01:00
|
|
|
static void mmcsd_checkwrprotect(FAR struct mmcsd_slot_s *slot,
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR uint8_t *csd);
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_getcardinfo(FAR struct mmcsd_slot_s *slot,
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR uint8_t *buffer,
|
|
|
|
FAR const struct mmcsd_cmdinfo_s *cmd);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
#define mmcsd_getcsd(slot, csd) mmcsd_getcardinfo(slot, csd, &g_cmd9);
|
|
|
|
#define mmcsd_getcid(slot, cid) mmcsd_getcardinfo(slot, cid, &g_cmd10);
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_recvblock(FAR struct mmcsd_slot_s *slot,
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR uint8_t *buffer, int nbytes);
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_xmitblock(FAR struct mmcsd_slot_s *slot,
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR const uint8_t *buffer, int nbytes,
|
|
|
|
uint8_t token);
|
2009-05-26 18:07:25 +02:00
|
|
|
#endif
|
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/* Block driver interfaces **************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_open(FAR struct inode *inode);
|
|
|
|
static int mmcsd_close(FAR struct inode *inode);
|
2024-08-25 01:21:12 +02:00
|
|
|
static ssize_t mmcsd_read(FAR struct inode *inode,
|
|
|
|
FAR unsigned char *buffer,
|
|
|
|
blkcnt_t start_sector, unsigned int nsectors);
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2009-12-15 15:25:14 +01:00
|
|
|
static ssize_t mmcsd_write(FAR struct inode *inode,
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR const unsigned char *buffer,
|
|
|
|
blkcnt_t start_sector,
|
|
|
|
unsigned int nsectors);
|
2008-10-15 16:18:17 +02:00
|
|
|
#endif
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_geometry(FAR struct inode *inode,
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct geometry *geometry);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Initialization ***********************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot);
|
2024-08-25 01:21:12 +02:00
|
|
|
static void mmcsd_mediachanged(FAR void *arg);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Driver state *************************************************************/
|
|
|
|
|
|
|
|
/* These are the lock driver methods supported by this file */
|
|
|
|
|
|
|
|
static const struct block_operations g_bops =
|
|
|
|
{
|
|
|
|
mmcsd_open, /* open */
|
|
|
|
mmcsd_close, /* close */
|
|
|
|
mmcsd_read, /* read */
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2008-10-15 16:18:17 +02:00
|
|
|
mmcsd_write, /* write */
|
|
|
|
#else
|
2009-05-27 22:45:39 +02:00
|
|
|
NULL, /* write */
|
2008-10-15 16:18:17 +02:00
|
|
|
#endif
|
|
|
|
mmcsd_geometry, /* geometry */
|
2009-05-27 22:45:39 +02:00
|
|
|
NULL /* ioctl */
|
2008-10-15 16:18:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A slot structure allocated for each configured slot */
|
|
|
|
|
|
|
|
static struct mmcsd_slot_s g_mmcsdslot[CONFIG_MMCSD_NSLOTS];
|
|
|
|
|
|
|
|
/* Timing *******************************************************************/
|
|
|
|
|
|
|
|
/* We will use the TRAN_SPEED from the CSD to determine the maximum SPI
|
|
|
|
* clocking (TRAN_SPEED defines the maximum transfer rate per bit per data
|
|
|
|
* line).
|
|
|
|
*
|
|
|
|
* The CSD TRAN_SPEED is provided as a 3 bit rate unit (RU) and a 4 bit time
|
|
|
|
* value (TU). We need the transfer frequency which is: RU*TU bits/sec
|
|
|
|
*
|
|
|
|
* g_transpeedru holds RU/10 and g_transpeedtu holds TU*10 so that the
|
|
|
|
* correct value is returned in the product
|
|
|
|
*/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static const uint32_t g_transpeedru[8] =
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2020-03-21 20:54:03 +01:00
|
|
|
10000, /* 0: 100 Kbit/sec / 10 */
|
|
|
|
100000, /* 1: 1 Mbit/sec / 10 */
|
|
|
|
1000000, /* 2: 10 Mbit/sec / 10 */
|
2015-10-10 18:41:00 +02:00
|
|
|
10000000, /* 3: 100 Mbit/sec / 10 */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
0, 0, 0, 0 /* 4-7: Reserved values */
|
|
|
|
};
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static const uint32_t g_transpeedtu[16] =
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2020-03-21 20:54:03 +01:00
|
|
|
0, 10, 12, 13, /* 0-3: Reserved, 1.0, 1.1, 1.2, 1.3 */
|
2008-10-15 16:18:17 +02:00
|
|
|
15, 20, 25, 30, /* 4-7: 1.5, 2.0, 2.5, 3.0 */
|
|
|
|
35, 40, 45, 50, /* 8-11: 3.5, 4.0, 4.5, 5.0 */
|
|
|
|
55, 60, 70, 80, /* 12-15: 5.5, 6.0, 7.0, 8.0 */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The TAAC defines the asynchronous part of the data access time. The
|
|
|
|
* read access time the sum of the TAAC and the NSAC. These define the
|
|
|
|
* time from the end bit of the read command to start bit of the data block.
|
|
|
|
*
|
|
|
|
* The TAAC consists of a 3-bit time unit (TU) and a 4-bit time value (TV).
|
2009-05-26 18:07:25 +02:00
|
|
|
* TAAC is in units of time; NSAC is in units of SPI clocks.
|
|
|
|
* The access time we need is then given by:
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
2009-05-26 18:07:25 +02:00
|
|
|
* taccess = TU*TV + NSAC/spifrequency
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
2015-03-04 13:51:56 +01:00
|
|
|
* g_taactu holds TU in units of nanoseconds and microseconds (you have to
|
|
|
|
* use the index to distinguish). g_taactv holds TV with 8-bits of
|
|
|
|
* fraction.
|
2008-10-15 16:18:17 +02:00
|
|
|
*/
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
#define MAX_USTUNDX 2
|
2009-12-15 15:25:14 +01:00
|
|
|
static const uint16_t g_taactu[8] =
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-05-27 22:45:39 +02:00
|
|
|
/* Units of nanoseconds */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
1, /* 0: 1 ns */
|
|
|
|
10, /* 1: 10 ns */
|
|
|
|
100, /* 2: 100 ns */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2009-05-27 22:45:39 +02:00
|
|
|
/* Units of microseconds */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
1, /* 3: 1 us 1,000 ns */
|
|
|
|
10, /* 4: 10 us 10,000 ns */
|
|
|
|
100, /* 5: 100 us 100,000 ns */
|
|
|
|
1000, /* 6: 1 ms 1,000,000 ns */
|
2009-05-26 18:07:25 +02:00
|
|
|
10000, /* 7: 10 ms 10,000,000 ns */
|
2008-10-15 16:18:17 +02:00
|
|
|
};
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static const uint16_t g_taactv[] =
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-05-26 18:07:25 +02:00
|
|
|
0x000, 0x100, 0x133, 0x14d, /* 0-3: Reserved, 1.0, 1.2, 1.3 */
|
|
|
|
0x180, 0x200, 0x280, 0x300, /* 4-7: 1.5, 2.0, 2.5, 3.0 */
|
|
|
|
0x380, 0x400, 0x480, 0x500, /* 8-11: 3.5, 4.0, 4.5, 5.0 */
|
|
|
|
0x580, 0x600, 0x700, 0x800 /* 12-15: 5.5, 6.0, 7.0, 8.0 */
|
2008-10-15 16:18:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Commands *****************************************************************/
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd0 =
|
|
|
|
{
|
|
|
|
CMD0, MMCSD_CMDRESP_R1, 0x95
|
|
|
|
};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd1 =
|
|
|
|
{
|
|
|
|
CMD1, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd8 =
|
|
|
|
{
|
|
|
|
CMD8, MMCSD_CMDRESP_R7, 0x87
|
|
|
|
};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd9 =
|
|
|
|
{
|
|
|
|
CMD9, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
2017-07-06 16:32:31 +02:00
|
|
|
#if 0 /* Not used */
|
2020-03-21 20:54:03 +01:00
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd10 =
|
|
|
|
{
|
|
|
|
CMD10, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
2017-07-06 16:32:31 +02:00
|
|
|
#endif
|
2020-03-21 20:54:03 +01:00
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd12 =
|
|
|
|
{
|
|
|
|
CMD12, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd16 =
|
|
|
|
{
|
|
|
|
CMD16, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd17 =
|
|
|
|
{
|
|
|
|
CMD17, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd18 =
|
|
|
|
{
|
|
|
|
CMD18, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2020-03-21 20:54:03 +01:00
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd24 =
|
|
|
|
{
|
|
|
|
CMD24, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd25 =
|
|
|
|
{
|
|
|
|
CMD25, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
2017-08-13 00:33:34 +02:00
|
|
|
#endif
|
2020-03-21 20:54:03 +01:00
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd55 =
|
|
|
|
{
|
|
|
|
CMD55, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd58 =
|
|
|
|
{
|
|
|
|
CMD58, MMCSD_CMDRESP_R3, 0xff
|
|
|
|
};
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2020-03-21 20:54:03 +01:00
|
|
|
static const struct mmcsd_cmdinfo_s g_acmd23 =
|
|
|
|
{
|
|
|
|
ACMD23, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
2017-08-13 00:33:34 +02:00
|
|
|
#endif
|
2020-03-21 20:54:03 +01:00
|
|
|
static const struct mmcsd_cmdinfo_s g_acmd41 =
|
|
|
|
{
|
|
|
|
ACMD41, MMCSD_CMDRESP_R1, 0xff
|
|
|
|
};
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-09-06 08:18:45 +02:00
|
|
|
* Name: mmcsd_lock
|
2008-10-15 16:18:17 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int mmcsd_lock(FAR struct mmcsd_slot_s *slot)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2020-03-31 23:19:07 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the MMC/SD device (possibly unnecessary if
|
2022-09-06 08:18:45 +02:00
|
|
|
* SPI_LOCK is also implemented as a mutex).
|
2020-03-31 23:19:07 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&slot->lock);
|
2020-03-31 23:19:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-08 16:28:07 +01:00
|
|
|
/* Get exclusive access to the SPI bus (if necessary) */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(slot->spi, true);
|
2013-02-28 18:34:57 +01:00
|
|
|
|
2013-06-17 20:31:24 +02:00
|
|
|
/* Set the frequency, bit width and mode, as some other driver could have
|
|
|
|
* changed those since the last time that we had the SPI bus.
|
2013-06-16 01:00:52 +02:00
|
|
|
*/
|
2013-02-28 18:34:57 +01:00
|
|
|
|
2013-06-23 18:45:37 +02:00
|
|
|
SPI_SETMODE(slot->spi, CONFIG_MMCSD_SPIMODE);
|
|
|
|
SPI_SETBITS(slot->spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(slot->spi, 0);
|
|
|
|
SPI_SETFREQUENCY(slot->spi, slot->spispeed);
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2020-03-31 23:19:07 +02:00
|
|
|
return ret;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2013-06-17 20:31:24 +02:00
|
|
|
/****************************************************************************
|
2022-09-06 08:18:45 +02:00
|
|
|
* Name: mmcsd_unlock
|
2013-06-17 20:31:24 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static void mmcsd_unlock(FAR struct mmcsd_slot_s *slot)
|
2013-02-08 16:28:07 +01:00
|
|
|
{
|
|
|
|
/* Relinquish the lock on the SPI bus */
|
2013-02-28 18:34:57 +01:00
|
|
|
|
|
|
|
/* The card may need up to 8 SCLK cycles to sample the CS status
|
|
|
|
* and release the MISO line.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(slot->spi, 0xff);
|
2013-02-28 18:34:57 +01:00
|
|
|
|
|
|
|
/* Relinquish exclusive access to the SPI bus */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(slot->spi, false);
|
2020-03-31 23:19:07 +02:00
|
|
|
|
|
|
|
/* Relinquish the lock on the MMC/SD device */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&slot->lock);
|
2013-02-08 16:28:07 +01:00
|
|
|
}
|
2013-02-28 18:34:57 +01:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_waitready
|
|
|
|
*
|
|
|
|
* Description:
|
2009-06-25 19:44:35 +02:00
|
|
|
* Wait until the card is no longer busy
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
2009-05-27 01:45:45 +02:00
|
|
|
* Assumptions:
|
|
|
|
* MMC/SD card already selected
|
|
|
|
*
|
2008-10-15 16:18:17 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mmcsd_waitready(FAR struct mmcsd_slot_s *slot)
|
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t response;
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t start;
|
|
|
|
clock_t elapsed;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Wait until the card is no longer busy (up to 500MS) */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
start = START_TIME;
|
|
|
|
do
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-04-26 18:06:29 +02:00
|
|
|
response = SPI_SEND(spi, 0xff);
|
2008-10-15 16:18:17 +02:00
|
|
|
if (response == 0xff)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
2015-03-04 13:51:56 +01:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
elapsed = ELAPSED_TIME(start);
|
2018-11-09 14:46:16 +01:00
|
|
|
|
|
|
|
if (elapsed > MMCSD_DELAY_10MS)
|
2020-03-21 20:54:03 +01:00
|
|
|
{
|
|
|
|
/* Give other threads time to run */
|
2018-11-09 14:51:43 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
nxsig_usleep(10000);
|
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
while (elapsed < MMCSD_DELAY_500MS);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
finfo("Card still busy, last response: %02x\n", response);
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_sendcmd
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send a command to MMC
|
|
|
|
*
|
2009-05-27 01:45:45 +02:00
|
|
|
* Assumptions:
|
|
|
|
* MMC/SD card already selected
|
|
|
|
*
|
2008-10-15 16:18:17 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static uint32_t mmcsd_sendcmd(FAR struct mmcsd_slot_s *slot,
|
2020-03-21 20:54:03 +01:00
|
|
|
FAR const struct mmcsd_cmdinfo_s *cmd,
|
|
|
|
uint32_t arg)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t result;
|
|
|
|
uint8_t response = 0xff;
|
2009-05-29 15:32:00 +02:00
|
|
|
int ret;
|
2008-10-15 16:18:17 +02:00
|
|
|
int i;
|
|
|
|
|
2013-02-01 16:32:39 +01:00
|
|
|
/* Wait until the card is not busy. Some SD cards will not enter the IDLE
|
|
|
|
* state until CMD0 is sent for the first time, switching the card to SPI
|
|
|
|
* mode. Having a pull-up resistor on MISO may avoid this problem, but
|
|
|
|
* this check makes it work also without the pull-up.
|
|
|
|
*/
|
2009-05-29 15:32:00 +02:00
|
|
|
|
|
|
|
ret = mmcsd_waitready(slot);
|
2013-02-01 16:32:39 +01:00
|
|
|
if (ret != OK && cmd != &g_cmd0)
|
2009-05-29 15:32:00 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/* Send command code */
|
|
|
|
|
2009-04-26 18:06:29 +02:00
|
|
|
SPI_SEND(spi, cmd->cmd);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
/* Send command's arguments (should be zero if there are no arguments) */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-27 22:45:39 +02:00
|
|
|
SPI_SEND(spi, (arg >> 24) & 0xff);
|
|
|
|
SPI_SEND(spi, (arg >> 16) & 0xff);
|
|
|
|
SPI_SEND(spi, (arg >> 8) & 0xff);
|
|
|
|
SPI_SEND(spi, arg & 0xff);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Send CRC if needed. The SPI interface is initialized in non-protected
|
2009-05-27 01:45:45 +02:00
|
|
|
* mode. However, the reset command (CMD0) and CMD8 are received by the
|
|
|
|
* card while it is still in SD mode and, therefore, must have a valid
|
|
|
|
* CRC field.
|
2008-10-15 16:18:17 +02:00
|
|
|
*/
|
|
|
|
|
2009-05-27 01:45:45 +02:00
|
|
|
SPI_SEND(spi, cmd->chksum);
|
|
|
|
|
|
|
|
/* Skip stuff byte on CMD12 */
|
|
|
|
|
|
|
|
if (cmd->cmd == CMD12)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-04-26 18:06:29 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2009-05-25 23:40:51 +02:00
|
|
|
/* Get the response to the command. A valid response will have bit7=0.
|
|
|
|
* Usually, the non-response is 0xff, but I have seen 0xc0 too.
|
|
|
|
*/
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-25 23:40:51 +02:00
|
|
|
for (i = 0; i < 9 && (response & 0x80) != 0; i++)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-04-26 18:06:29 +02:00
|
|
|
response = SPI_SEND(spi, 0xff);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2009-05-25 23:40:51 +02:00
|
|
|
if ((response & 0x80) != 0)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed: i=%d response=%02x\n", i, response);
|
2009-12-15 15:25:14 +01:00
|
|
|
return (uint32_t)-1;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Interpret the response according to the command */
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
result = response;
|
2008-10-15 16:18:17 +02:00
|
|
|
switch (cmd->resp)
|
|
|
|
{
|
2009-05-26 18:07:25 +02:00
|
|
|
/* The R1B response is two bytes long */
|
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
case MMCSD_CMDRESP_R1B:
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t busy = 0;
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t start;
|
|
|
|
clock_t elapsed;
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
start = START_TIME;
|
|
|
|
do
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-04-26 18:06:29 +02:00
|
|
|
busy = SPI_SEND(spi, 0xff);
|
2009-05-26 18:07:25 +02:00
|
|
|
elapsed = ELAPSED_TIME(start);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
while (elapsed < slot->twrite && busy != 0xff);
|
|
|
|
|
|
|
|
if (busy != 0xff)
|
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
ferr("ERROR: Failed: card still busy (%02" PRIx32 ")\n", busy);
|
2009-12-15 15:25:14 +01:00
|
|
|
return (uint32_t)-1;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("CMD%d[%08" PRIx32 "] R1B=%02" PRIx8 "\n",
|
2009-05-27 22:45:39 +02:00
|
|
|
cmd->cmd & 0x3f, arg, response);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* The R1 response is a single byte */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
case MMCSD_CMDRESP_R1:
|
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("CMD%d[%08" PRIx32 "] R1=%02" PRIx8 "\n",
|
2009-05-27 22:45:39 +02:00
|
|
|
cmd->cmd & 0x3f, arg, response);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* The R2 response is two bytes long */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
case MMCSD_CMDRESP_R2:
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
result = ((uint32_t)(response & 0xff) << 8);
|
2009-04-26 18:06:29 +02:00
|
|
|
result |= SPI_SEND(spi, 0xff) & 0xff;
|
2009-05-27 22:45:39 +02:00
|
|
|
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("CMD%d[%08" PRIx32 "] R2=%04" PRIx32 "\n",
|
2009-05-27 22:45:39 +02:00
|
|
|
cmd->cmd & 0x3f, arg, result);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
break;
|
|
|
|
|
2012-11-01 22:21:54 +01:00
|
|
|
/* The R3 response is 5 bytes long. The first byte is identical to R1. */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
case MMCSD_CMDRESP_R3:
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
slot->ocr = ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 24);
|
|
|
|
slot->ocr |= ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 16);
|
|
|
|
slot->ocr |= ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 8);
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->ocr |= SPI_SEND(spi, 0xff) & 0xff;
|
2009-05-27 22:45:39 +02:00
|
|
|
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("CMD%d[%08" PRIx32 "] R1=%02" PRIx8 " OCR=%08" PRIx32 "\n",
|
2009-05-27 22:45:39 +02:00
|
|
|
cmd->cmd & 0x3f, arg, response, slot->ocr);
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2012-11-01 22:21:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* The R7 response is 5 bytes long. The first byte is identical to R1. */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
case MMCSD_CMDRESP_R7:
|
2008-10-15 16:18:17 +02:00
|
|
|
default:
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
slot->r7 = ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 24);
|
|
|
|
slot->r7 |= ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 16);
|
|
|
|
slot->r7 |= ((uint32_t)(SPI_SEND(spi, 0xff) & 0xff) << 8);
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->r7 |= SPI_SEND(spi, 0xff) & 0xff;
|
2009-05-27 22:45:39 +02:00
|
|
|
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("CMD%d[%08" PRIx32 "] R1=%02" PRIx8 " R7=%08" PRIx32 "\n",
|
2009-05-27 22:45:39 +02:00
|
|
|
cmd->cmd & 0x3f, arg, response, slot->r7);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
break;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
return result;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_setblklen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set block length
|
|
|
|
*
|
2009-05-27 01:45:45 +02:00
|
|
|
* Assumptions:
|
|
|
|
* MMC/SD card already selected
|
|
|
|
*
|
2008-10-15 16:18:17 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static void mmcsd_setblklen(FAR struct mmcsd_slot_s *slot, uint32_t length)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t response;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("Set block length to %" PRId32 "\n", length);
|
2009-05-27 01:45:45 +02:00
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd16, length);
|
|
|
|
if (response != MMCSD_SPIR1_OK)
|
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
ferr("ERROR: Failed to set block length: %02" PRIx32 "\n", response);
|
2009-05-27 01:45:45 +02:00
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_nsac
|
|
|
|
*
|
2009-05-27 01:45:45 +02:00
|
|
|
* Description: Convert the value of the NSAC to microseconds
|
2009-05-26 18:07:25 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static uint32_t mmcsd_nsac(FAR struct mmcsd_slot_s *slot, FAR uint8_t *csd,
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t frequency)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
2015-03-04 13:51:56 +01:00
|
|
|
/* NSAC is 8-bits wide and is in units of 100 clock cycles. Therefore,
|
|
|
|
* the maximum value is 25.5K clock cycles.
|
2009-05-26 18:07:25 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
uint32_t nsac = MMCSD_CSD_NSAC(csd) * ((uint32_t)100 * 1000);
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t fhkz = (frequency + 500) / 1000;
|
|
|
|
return (nsac + (fhkz >> 1)) / fhkz;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_taac
|
|
|
|
*
|
2009-05-27 01:45:45 +02:00
|
|
|
* Description: Convert the value of the TAAC to microseconds
|
2009-05-26 18:07:25 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static uint32_t mmcsd_taac(FAR struct mmcsd_slot_s *slot, FAR uint8_t *csd)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
|
|
|
int tundx;
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
/* The TAAC consists of a 3-bit time unit (TU) and a 4-bit time value (TV).
|
2009-05-26 18:07:25 +02:00
|
|
|
* TAAC is in units of time; NSAC is in units of SPI clocks.
|
|
|
|
* The access time we need is then given by:
|
|
|
|
*
|
|
|
|
* taccess = TU*TV + NSAC/spifrequency
|
|
|
|
*
|
2015-03-04 13:51:56 +01:00
|
|
|
* g_taactu holds TU in units of nanoseconds and microseconds (you have to
|
2020-03-21 20:54:03 +01:00
|
|
|
* use the index to distinguish. g_taactv holds TV with 8-bits of
|
|
|
|
* fraction.
|
2009-05-26 18:07:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
tundx = MMCSD_CSD_TAAC_TIMEUNIT(csd);
|
|
|
|
if (tundx <= MAX_USTUNDX)
|
|
|
|
{
|
|
|
|
/* The maximum value of the nanosecond TAAC is 800 ns. The rounded
|
2009-05-27 01:45:45 +02:00
|
|
|
* answer in microseconds will be at most 1.
|
2009-05-26 18:07:25 +02:00
|
|
|
*/
|
|
|
|
|
2009-05-27 01:45:45 +02:00
|
|
|
return 1;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-27 01:45:45 +02:00
|
|
|
/* Return the answer in microseconds */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
return (g_taactu[tundx] * g_taactv[MMCSD_CSD_TAAC_TIMEVALUE(csd)] +
|
|
|
|
0x80) >> 8;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_decodecsd
|
|
|
|
*
|
2012-06-03 18:09:59 +02:00
|
|
|
* Description:
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static void mmcsd_decodecsd(FAR struct mmcsd_slot_s *slot, FAR uint8_t *csd)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t maxfrequency;
|
|
|
|
uint32_t frequency;
|
|
|
|
uint32_t readbllen;
|
|
|
|
uint32_t csizemult;
|
|
|
|
uint32_t csize;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2013-06-17 20:31:24 +02:00
|
|
|
/* Calculate the SPI max clock frequency */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-27 01:45:45 +02:00
|
|
|
maxfrequency =
|
2009-05-25 23:40:51 +02:00
|
|
|
g_transpeedtu[MMCSD_CSD_TRANSPEED_TIMEVALUE(csd)] *
|
|
|
|
g_transpeedru[MMCSD_CSD_TRANSPEED_TRANSFERRATEUNIT(csd)];
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-27 01:45:45 +02:00
|
|
|
/* Clip the max frequency to account for board limitations */
|
2009-05-29 01:14:27 +02:00
|
|
|
|
2009-05-27 01:45:45 +02:00
|
|
|
frequency = maxfrequency;
|
2009-05-26 18:07:25 +02:00
|
|
|
if (frequency > CONFIG_MMCSD_SPICLOCK)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-05-26 18:07:25 +02:00
|
|
|
frequency = CONFIG_MMCSD_SPICLOCK;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2013-06-17 20:31:24 +02:00
|
|
|
/* Set the actual SPI frequency as close as possible to the max frequency */
|
2013-06-16 01:00:52 +02:00
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
slot->spispeed = frequency;
|
2008-10-15 16:18:17 +02:00
|
|
|
frequency = SPI_SETFREQUENCY(spi, frequency);
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Now determine the delay to access data */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
if (slot->type == MMCSD_CARDTYPE_MMC)
|
|
|
|
{
|
2015-03-04 13:51:56 +01:00
|
|
|
/* The TAAC consists of a 3-bit time unit (TU) and a 4-bit time value
|
|
|
|
* (TV). TAAC is in units of time; NSAC is in units of SPI clocks.
|
2009-05-26 18:07:25 +02:00
|
|
|
* The access time we need is then given by:
|
|
|
|
*
|
|
|
|
* taccess = TU*TV + NSAC/spifrequency
|
|
|
|
*
|
2009-05-27 01:45:45 +02:00
|
|
|
* Example: TAAC = 1.5 ms, NSAC = 0, r2wfactor = 4, CLK_TCK=100
|
|
|
|
* taccessus = 1,500uS
|
2020-03-21 20:54:03 +01:00
|
|
|
* taccess = (1,500 * 100) / 100,000) + 1 = 2
|
|
|
|
* (ideal, 1.5)
|
|
|
|
* twrite = (1,500 * 4 * 100) / 100,000) + 1 = 7
|
|
|
|
* (ideal 6.0)
|
2009-05-27 01:45:45 +02:00
|
|
|
*
|
|
|
|
* First get the access time in microseconds
|
2009-05-26 18:07:25 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
uint32_t taccessus = mmcsd_taac(slot, csd) +
|
|
|
|
mmcsd_nsac(slot, csd, frequency);
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Then convert to system clock ticks. The maximum read access is 10
|
|
|
|
* times the tacc value:
|
|
|
|
*
|
|
|
|
* taccess = 10 * (taccessus / 1,000,000) * CLK_TCK
|
2009-05-26 18:07:25 +02:00
|
|
|
*/
|
|
|
|
|
2009-05-27 01:45:45 +02:00
|
|
|
slot->taccess = (taccessus * CLK_TCK) / 100000 + 1;
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* NOTE that we add one to taccess to assure that we wait at least
|
|
|
|
* this time. The write access time is larger by the R2WFACTOR:
|
|
|
|
*/
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
slot->taccess = (taccessus * MMCSD_CSD_R2WFACTOR(csd) * CLK_TCK) /
|
|
|
|
100000 + 1;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-26 18:07:25 +02:00
|
|
|
/* For SD, the average is still given by the TAAC+NSAC, but the
|
|
|
|
* maximum are the constants 100 and 250MS
|
|
|
|
*/
|
|
|
|
|
|
|
|
slot->taccess = SD_READACCESS;
|
|
|
|
slot->twrite = SD_WRITEACCESS;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("SPI Frequency\n");
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo(" Maximum: %" PRId32 " Hz\n", maxfrequency);
|
|
|
|
finfo(" Actual: %" PRId32 " Hz\n", frequency);
|
|
|
|
finfo("Read access time: %" PRId32 " ticks\n", slot->taccess);
|
|
|
|
finfo("Write access time: %" PRId32 " ticks\n", slot->twrite);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Get the physical geometry of the card: sector size and number of
|
|
|
|
* sectors. The card's total capacity is computed from
|
|
|
|
*
|
2009-05-26 18:07:25 +02:00
|
|
|
* capacity = BLOCKNR * BLOCK_LEN
|
|
|
|
* BLOCKNR = (C_SIZE+1)*MULT
|
|
|
|
* MULT = 2**(C_SIZE_MULT+2) (C_SIZE_MULT < 8)
|
|
|
|
* BLOCK_LEN = 2**READ_BL_LEN (READ_BL_LEN < 12)
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
|
|
|
* Or
|
|
|
|
*
|
|
|
|
* capacity = ((C_SIZE+1) << (READD_BL_LEN + C_SIZE_MULT + 2))
|
|
|
|
*
|
|
|
|
* In units of the sector size (1 << READ_BL_LEN), then simplifies to
|
|
|
|
*
|
|
|
|
* nsectors = ((C_SIZE+1) << (C_SIZE_MULT + 2))
|
|
|
|
*/
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
if (MMCSD_CSD_CSDSTRUCT(csd) != 0)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-05-26 18:07:25 +02:00
|
|
|
/* SDC structure ver 2.xx */
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/* Note: On SD card WRITE_BL_LEN is always the same as READ_BL_LEN */
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
readbllen = SD20_CSD_READBLLEN(csd);
|
|
|
|
csizemult = SD20_CSD_CSIZEMULT(csd) + 2;
|
|
|
|
csize = SD20_CSD_CSIZE(csd) + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* MMC or SD structure ver 1.xx */
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Note: On SD card WRITE_BL_LEN is always the same as READ_BL_LEN */
|
2009-05-25 23:40:51 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
readbllen = MMCSD_CSD_READBLLEN(csd);
|
|
|
|
csizemult = MMCSD_CSD_CSIZEMULT(csd) + 2;
|
|
|
|
csize = MMCSD_CSD_CSIZE(csd) + 1;
|
|
|
|
}
|
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
/* SDHC ver2.x cards have fixed block transfer size of 512 bytes. SDC
|
|
|
|
* ver1.x cards with capacity less than 1Gb, will have sector size
|
2018-07-09 02:24:45 +02:00
|
|
|
* 512 bytes. SDC ver1.x cards with capacity of 2Gb will report readbllen
|
2009-05-28 19:51:24 +02:00
|
|
|
* of 1024 but should use 512 bytes for block transfers. SDC ver1.x 4Gb
|
|
|
|
* cards will report readbllen of 2048 bytes -- are they also 512 bytes?
|
|
|
|
*/
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
#ifdef CONFIG_MMCSD_SECTOR512
|
|
|
|
if (readbllen > 9)
|
|
|
|
{
|
|
|
|
csizemult += (readbllen - 9);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGASSERT(readbllen == 9);
|
|
|
|
}
|
|
|
|
#else
|
2009-05-26 18:07:25 +02:00
|
|
|
if (IS_SDV2(slot->type))
|
|
|
|
{
|
2009-05-25 23:40:51 +02:00
|
|
|
if (readbllen > 9)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
fwarn("WARNING: Forcing 512 byte sector size\n");
|
2009-05-28 19:51:24 +02:00
|
|
|
csizemult += (readbllen - 9);
|
|
|
|
readbllen = 9;
|
2009-05-25 23:40:51 +02:00
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->sectorsize = 1 << readbllen;
|
2009-05-28 19:51:24 +02:00
|
|
|
#endif
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->nsectors = csize << csizemult;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Sector size: %d\n", SECTORSIZE(slot));
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("Number of sectors: %" PRId32 "\n", slot->nsectors);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_checkwrprotect
|
|
|
|
*
|
2009-05-26 18:07:25 +02:00
|
|
|
* Description:
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static void mmcsd_checkwrprotect(FAR struct mmcsd_slot_s *slot,
|
|
|
|
FAR uint8_t *csd)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
|
|
|
|
|
|
|
/* Check if (1) the slot is reporting that reporting that write protection
|
|
|
|
* is set, (2) the card reports permanent write protect, or (2) the card
|
|
|
|
* reports temporary write protect.
|
|
|
|
*/
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
if ((SPI_STATUS(spi, SPIDEV_MMCSD(0)) & SPI_STATUS_WRPROTECTED) != 0 ||
|
2008-10-15 16:18:17 +02:00
|
|
|
MMCSD_CSD_PERMWRITEPROTECT(csd) ||
|
|
|
|
MMCSD_CSD_TMPWRITEPROTECT(csd))
|
|
|
|
{
|
|
|
|
slot->state |= MMCSD_SLOTSTATUS_WRPROTECT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
slot->state &= ~MMCSD_SLOTSTATUS_WRPROTECT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_getcardinfo
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read CSD or CID registers
|
|
|
|
*
|
2009-05-27 01:45:45 +02:00
|
|
|
* Assumptions:
|
|
|
|
* MMC/SD card already selected
|
|
|
|
*
|
2008-10-15 16:18:17 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static int mmcsd_getcardinfo(FAR struct mmcsd_slot_s *slot,
|
|
|
|
FAR uint8_t *buffer,
|
|
|
|
FAR const struct mmcsd_cmdinfo_s *cmd)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t result;
|
|
|
|
uint8_t response;
|
2008-10-15 16:18:17 +02:00
|
|
|
int i;
|
|
|
|
|
2009-04-26 18:06:29 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Send the CMD9 or CMD10 */
|
|
|
|
|
|
|
|
result = mmcsd_sendcmd(slot, cmd, 0);
|
2008-10-15 19:26:39 +02:00
|
|
|
if (result != MMCSD_SPIR1_OK)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
ferr("ERROR: CMD9/10 failed: R1=%02" PRIx32 "\n", result);
|
2009-05-27 01:45:45 +02:00
|
|
|
return -EIO;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 10:08:03 +02:00
|
|
|
/* Try up to 8 times to find the start of block or until an error occurs */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2008-10-15 19:26:39 +02:00
|
|
|
for (i = 0; i < 8; i++)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-04-26 18:06:29 +02:00
|
|
|
response = SPI_SEND(spi, 0xff);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("%d. SPI send returned %02x\n", i, response);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2008-10-15 19:26:39 +02:00
|
|
|
/* If a read operation fails and the card cannot provide the requested
|
2008-10-15 16:18:17 +02:00
|
|
|
* data, it will send a data error token instead. The 4 least
|
|
|
|
* significant bits are the same as those in the R2 response.
|
|
|
|
*/
|
|
|
|
|
2008-10-15 19:26:39 +02:00
|
|
|
if (response != 0 && (response & MMCSD_SPIDET_UPPER) == 0)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: %d. Data transfer error: %02x\n", i, response);
|
2009-05-27 01:45:45 +02:00
|
|
|
return -EIO;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
else if (response == MMCSD_SPIDT_STARTBLKSNGL)
|
|
|
|
{
|
|
|
|
for (i = 0; i < 16; ++i)
|
|
|
|
{
|
2009-04-26 18:06:29 +02:00
|
|
|
*buffer++ = SPI_SEND(spi, 0xff);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CRC receive */
|
|
|
|
|
2009-04-26 18:06:29 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
|
|
|
SPI_SEND(spi, 0xff);
|
2008-10-15 16:18:17 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 07:10:35 +01:00
|
|
|
ferr("ERROR: Did not find start of block\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_recvblock
|
|
|
|
*
|
|
|
|
* Description: Receive a data block from the card
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static int mmcsd_recvblock(FAR struct mmcsd_slot_s *slot,
|
|
|
|
FAR uint8_t *buffer, int nbytes)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t start;
|
|
|
|
clock_t elapsed;
|
2016-01-21 18:54:26 +01:00
|
|
|
uint8_t token;
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
/* Wait up to the maximum to receive a valid data token. taccess is the
|
|
|
|
* time from when the command is sent until the first byte of data is
|
2020-03-21 20:54:03 +01:00
|
|
|
* received.
|
|
|
|
*/
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
start = START_TIME;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
token = SPI_SEND(spi, 0xff);
|
|
|
|
elapsed = ELAPSED_TIME(start);
|
|
|
|
}
|
|
|
|
while (token == 0xff && elapsed < slot->taccess);
|
|
|
|
|
|
|
|
if (token == MMCSD_SPIDT_STARTBLKSNGL)
|
|
|
|
{
|
|
|
|
/* Receive the block */
|
|
|
|
|
|
|
|
SPI_RECVBLOCK(spi, buffer, nbytes);
|
|
|
|
|
|
|
|
/* Discard the CRC */
|
|
|
|
|
|
|
|
SPI_SEND(spi, 0xff);
|
|
|
|
SPI_SEND(spi, 0xff);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Did not receive data token (%02x)\n", token);
|
2009-05-26 18:07:25 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_xmitblock
|
|
|
|
*
|
|
|
|
* Description: Transmit a data block to the card
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2015-03-04 13:51:56 +01:00
|
|
|
static int mmcsd_xmitblock(FAR struct mmcsd_slot_s *slot,
|
|
|
|
FAR const uint8_t *buffer, int nbytes,
|
|
|
|
uint8_t token)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t response;
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
/* Start the block transfer:
|
|
|
|
* 1. 0xff (sync)
|
|
|
|
* 2. 0xfe or 0xfc (start of block token)
|
|
|
|
* 3. Followed by the block of data and 2 byte CRC
|
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SEND(spi, 0xff); /* sync */
|
|
|
|
SPI_SEND(spi, token); /* data token */
|
|
|
|
|
|
|
|
/* Transmit the block to the MMC/SD card */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SNDBLOCK(spi, buffer, nbytes);
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
/* Add the bogus CRC. By default, the SPI interface is initialized in
|
|
|
|
* non-protected mode. However, we still have to send bogus CRC values
|
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SEND(spi, 0xff);
|
|
|
|
SPI_SEND(spi, 0xff);
|
|
|
|
|
|
|
|
/* Now get the data response */
|
|
|
|
|
|
|
|
response = SPI_SEND(spi, 0xff);
|
|
|
|
if ((response & MMCSD_SPIDR_MASK) != MMCSD_SPIDR_ACCEPTED)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Bad data response: %02x\n", response);
|
2009-05-26 18:07:25 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
2014-04-07 23:28:04 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2020-03-21 17:36:26 +01:00
|
|
|
#endif /* !CONFIG_MMCSD_READONLY */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Block Driver Operations
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_open
|
|
|
|
*
|
|
|
|
* Description: Open the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mmcsd_open(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR struct mmcsd_slot_s *slot;
|
|
|
|
FAR struct spi_dev_s *spi;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2008-10-15 19:26:39 +02:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!inode || !inode->i_private)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract our private data from the inode structure */
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
slot = inode->i_private;
|
2008-10-15 16:18:17 +02:00
|
|
|
spi = slot->spi;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!spi)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = mmcsd_lock(slot);
|
2020-03-31 23:19:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-05-29 15:32:00 +02:00
|
|
|
/* Verify that an MMC/SD card has been inserted */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-29 15:32:00 +02:00
|
|
|
ret = -ENODEV;
|
2020-03-31 23:19:07 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
if ((SPI_STATUS(spi, SPIDEV_MMCSD(0)) & SPI_STATUS_PRESENT) != 0)
|
2009-05-29 15:32:00 +02:00
|
|
|
{
|
|
|
|
/* Yes.. a card is present. Has it been initialized? */
|
|
|
|
|
|
|
|
if (slot->type == MMCSD_CARDTYPE_UNKNOWN)
|
|
|
|
{
|
2014-04-07 23:28:04 +02:00
|
|
|
/* Initialize for the media in the slot */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-29 15:32:00 +02:00
|
|
|
ret = mmcsd_mediainitialize(slot);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Failed to initialize card\n");
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2009-05-29 15:32:00 +02:00
|
|
|
}
|
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2012-06-03 18:09:59 +02:00
|
|
|
/* Make sure that the card is ready */
|
2009-05-29 15:32:00 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
|
2009-05-29 15:32:00 +02:00
|
|
|
ret = mmcsd_waitready(slot);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2009-05-29 15:32:00 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
mmcsd_unlock(slot);
|
2008-10-15 16:18:17 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_close
|
|
|
|
*
|
|
|
|
* Description: close the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mmcsd_close(FAR struct inode *inode)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_read
|
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* Description: Read the specified number of sectors
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static ssize_t mmcsd_read(FAR struct inode *inode, FAR unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t start_sector, unsigned int nsectors)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
FAR struct mmcsd_slot_s *slot;
|
|
|
|
FAR struct spi_dev_s *spi;
|
2022-09-06 12:58:00 +02:00
|
|
|
FAR unsigned char *restore = buffer;
|
|
|
|
int retry_count = 0;
|
2008-10-15 16:18:17 +02:00
|
|
|
size_t nbytes;
|
2024-08-25 01:21:12 +02:00
|
|
|
off_t offset;
|
2020-03-31 23:19:07 +02:00
|
|
|
uint8_t response;
|
2024-08-25 01:21:12 +02:00
|
|
|
int i;
|
2020-03-31 23:19:07 +02:00
|
|
|
int ret;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2021-07-21 09:27:36 +02:00
|
|
|
finfo("start_sector=%" PRIuOFF " nsectors=%u\n", start_sector, nsectors);
|
2008-10-15 19:26:39 +02:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!buffer)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Invalid parameters\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inode || !inode->i_private)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract our private data from the inode structure */
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
slot = inode->i_private;
|
2008-10-15 16:18:17 +02:00
|
|
|
spi = slot->spi;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!spi)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-29 15:32:00 +02:00
|
|
|
/* Verify that card is available */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
if (slot->state & MMCSD_SLOTSTATUS_NOTREADY)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Slot not ready\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do nothing on zero-length transfer */
|
|
|
|
|
|
|
|
if (nsectors < 1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert sector and nsectors to nbytes and byte offset */
|
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
nbytes = nsectors * SECTORSIZE(slot);
|
2014-04-07 23:28:04 +02:00
|
|
|
UNUSED(nbytes);
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
if (IS_BLOCK(slot->type))
|
|
|
|
{
|
|
|
|
offset = start_sector;
|
2021-07-21 09:27:36 +02:00
|
|
|
finfo("nbytes=%zu sector offset=%" PRIdOFF "\n", nbytes, offset);
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-28 19:51:24 +02:00
|
|
|
offset = start_sector * SECTORSIZE(slot);
|
2021-07-21 09:27:36 +02:00
|
|
|
finfo("nbytes=%zu byte offset=%" PRIdOFF "\n", nbytes, offset);
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-29 17:36:48 +02:00
|
|
|
/* Select the slave */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = mmcsd_lock(slot);
|
2020-03-31 23:19:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
2022-09-06 12:58:00 +02:00
|
|
|
retry:
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Single or multiple block read? */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
if (nsectors == 1)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Send CMD17: Reads a block of the size selected by the SET_BLOCKLEN
|
|
|
|
* command and verify that good R1 status is returned
|
|
|
|
*/
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd17, offset);
|
|
|
|
if (response != MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: CMD17 failed: R1=%02x\n", response);
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_eio;
|
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Receive the block */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
if (mmcsd_recvblock(slot, buffer, SECTORSIZE(slot)) != 0)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed: to receive the block\n");
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_eio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-29 01:14:27 +02:00
|
|
|
/* Send CMD18: Reads a block of the size selected by the SET_BLOCKLEN
|
2009-05-26 18:07:25 +02:00
|
|
|
* command and verify that good R1 status is returned
|
2008-10-15 19:26:39 +02:00
|
|
|
*/
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd18, offset);
|
|
|
|
if (response != MMCSD_SPIR1_OK)
|
2008-10-15 19:26:39 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: CMD18 failed: R1=%02x\n", response);
|
2008-10-15 19:26:39 +02:00
|
|
|
goto errout_with_eio;
|
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Receive each block */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
for (i = 0; i < nsectors; i++)
|
|
|
|
{
|
2009-05-28 19:51:24 +02:00
|
|
|
if (mmcsd_recvblock(slot, buffer, SECTORSIZE(slot)) != 0)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed: to receive the block\n");
|
2022-07-04 17:29:11 +02:00
|
|
|
|
|
|
|
/* Send CMD12: Stops transmission */
|
|
|
|
|
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd12, 0);
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_eio;
|
|
|
|
}
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
buffer += SECTORSIZE(slot);
|
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Send CMD12: Stops transmission */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd12, 0);
|
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* On success, return the number of sectors transfer */
|
2008-10-15 21:15:53 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2009-05-26 18:07:25 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
2022-09-06 08:18:45 +02:00
|
|
|
mmcsd_unlock(slot);
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2021-09-18 15:41:06 +02:00
|
|
|
finfo("Read %zu bytes:\n", nbytes);
|
2009-06-25 19:44:35 +02:00
|
|
|
mmcsd_dumpbuffer("Read buffer", buffer, nbytes);
|
2009-05-26 18:07:25 +02:00
|
|
|
return nsectors;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
errout_with_eio:
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2022-09-06 12:58:00 +02:00
|
|
|
if (retry_count++ < CONFIG_MMCSD_SPIRETRY_COUNT)
|
|
|
|
{
|
|
|
|
buffer = restore;
|
|
|
|
ret = mmcsd_mediainitialize(slot);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to reinitialize card\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fwarn("ERROR: retry %d\n", retry_count);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
mmcsd_unlock(slot);
|
2008-10-15 19:26:39 +02:00
|
|
|
return -EIO;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write the specified number of sectors
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2020-03-21 20:54:03 +01:00
|
|
|
static ssize_t mmcsd_write(FAR struct inode *inode,
|
|
|
|
FAR const unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t start_sector, unsigned int nsectors)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
FAR struct mmcsd_slot_s *slot;
|
|
|
|
FAR struct spi_dev_s *spi;
|
2022-09-06 12:58:00 +02:00
|
|
|
FAR const unsigned char *restore = buffer;
|
|
|
|
int retry_count = 0;
|
2008-10-15 16:18:17 +02:00
|
|
|
size_t nbytes;
|
2024-08-25 01:21:12 +02:00
|
|
|
off_t offset;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t response;
|
2009-05-26 18:07:25 +02:00
|
|
|
int i;
|
2020-03-31 23:19:07 +02:00
|
|
|
int ret;
|
2008-10-15 19:26:39 +02:00
|
|
|
|
2021-07-21 09:27:36 +02:00
|
|
|
finfo("start_sector=%" PRIuOFF " nsectors=%u\n", start_sector, nsectors);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!buffer)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Invalid parameters\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inode || !inode->i_private)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract our private data from the inode structure */
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
slot = inode->i_private;
|
2008-10-15 16:18:17 +02:00
|
|
|
spi = slot->spi;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!spi)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-29 15:32:00 +02:00
|
|
|
/* Verify that card is available */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
if (slot->state & MMCSD_SLOTSTATUS_NOTREADY)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Slot not ready\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that the card is write enabled */
|
|
|
|
|
|
|
|
if (slot->state & MMCSD_SLOTSTATUS_WRPROTECT)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Not write enabled\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do nothing on zero-length transfer */
|
|
|
|
|
|
|
|
if (nsectors < 1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert sector and nsectors to nbytes and byte offset */
|
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
nbytes = nsectors * SECTORSIZE(slot);
|
2014-04-12 20:53:19 +02:00
|
|
|
UNUSED(nbytes);
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
if (IS_BLOCK(slot->type))
|
|
|
|
{
|
|
|
|
offset = start_sector;
|
2021-07-21 09:27:36 +02:00
|
|
|
finfo("nbytes=%zu sector offset=%" PRIdOFF "\n", nbytes, offset);
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-28 19:51:24 +02:00
|
|
|
offset = start_sector * SECTORSIZE(slot);
|
2021-07-21 09:27:36 +02:00
|
|
|
finfo("nbytes=%zu byte offset=%" PRIdOFF "\n", nbytes, offset);
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2009-06-25 19:44:35 +02:00
|
|
|
mmcsd_dumpbuffer("Write buffer", buffer, nbytes);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-29 17:36:48 +02:00
|
|
|
/* Select the slave */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = mmcsd_lock(slot);
|
2020-03-31 23:19:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
2022-09-06 12:58:00 +02:00
|
|
|
retry:
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Single or multiple block transfer? */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
if (nsectors == 1)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2020-04-13 10:08:03 +02:00
|
|
|
/* Send CMD24(WRITE_BLOCK) and verify that good R1 is returned */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd24, offset);
|
|
|
|
if (response != MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: CMD24 failed: R1=%02x\n", response);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then transfer the sector */
|
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
if (mmcsd_xmitblock(slot, buffer, SECTORSIZE(slot), 0xfe) != 0)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Block transfer failed\n");
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set the number of blocks to be pre-erased (SD only) */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
if (IS_SD(slot->type))
|
|
|
|
{
|
2015-03-13 14:26:16 +01:00
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd55, 0);
|
|
|
|
if (response != MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: CMD55 failed: R1=%02x\n", response);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2015-03-13 14:26:16 +01:00
|
|
|
}
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
response = mmcsd_sendcmd(slot, &g_acmd23, nsectors);
|
|
|
|
if (response != MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: ACMD23 failed: R1=%02x\n", response);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Send CMD25: Continuously write blocks of data until the
|
2014-04-07 23:28:04 +02:00
|
|
|
* transmission is stopped.
|
2009-05-26 18:07:25 +02:00
|
|
|
*/
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd25, offset);
|
|
|
|
if (response != MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: CMD25 failed: R1=%02x\n", response);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Transmit each block */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
for (i = 0; i < nsectors; i++)
|
|
|
|
{
|
2009-05-28 19:51:24 +02:00
|
|
|
if (mmcsd_xmitblock(slot, buffer, SECTORSIZE(slot), 0xfc) != 0)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed: to receive the block\n");
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
buffer += SECTORSIZE(slot);
|
2015-03-13 14:26:16 +01:00
|
|
|
|
|
|
|
if (mmcsd_waitready(slot) != OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed: card is busy\n");
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2015-03-13 14:26:16 +01:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Send the stop transmission token */
|
|
|
|
|
|
|
|
SPI_SEND(spi, MMCSD_SPIDT_STOPTRANS);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until the card is no longer busy */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
mmcsd_waitready(slot);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2009-05-26 18:07:25 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
2022-09-06 08:18:45 +02:00
|
|
|
mmcsd_unlock(slot);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* The success return value is the number of sectors written */
|
|
|
|
|
|
|
|
return nsectors;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2022-09-06 12:58:00 +02:00
|
|
|
if (retry_count++ < CONFIG_MMCSD_SPIRETRY_COUNT)
|
|
|
|
{
|
|
|
|
buffer = restore;
|
|
|
|
ret = mmcsd_mediainitialize(slot);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to reinitialize card\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fwarn("ERROR: retry %d\n", retry_count);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
mmcsd_unlock(slot);
|
2009-05-26 18:07:25 +02:00
|
|
|
return -EIO;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_geometry
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return device geometry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static int mmcsd_geometry(FAR struct inode *inode,
|
|
|
|
FAR struct geometry *geometry)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
FAR struct mmcsd_slot_s *slot;
|
|
|
|
FAR struct spi_dev_s *spi;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t csd[16];
|
2008-10-15 16:18:17 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!geometry)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Invalid parameters\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inode || !inode->i_private)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract our private data from the inode structure */
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
slot = inode->i_private;
|
2008-10-15 16:18:17 +02:00
|
|
|
spi = slot->spi;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!spi)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Re-sample the CSD */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = mmcsd_lock(slot);
|
2020-03-31 23:19:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
|
2008-10-15 16:18:17 +02:00
|
|
|
ret = mmcsd_getcsd(slot, csd);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2009-05-27 01:45:45 +02:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
mmcsd_unlock(slot);
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: mmcsd_getcsd returned %d\n", ret);
|
2008-10-15 16:18:17 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for changes related to write protection */
|
|
|
|
|
|
|
|
mmcsd_checkwrprotect(slot, csd);
|
|
|
|
|
|
|
|
/* Then return the card geometry */
|
|
|
|
|
2021-08-11 06:21:39 +02:00
|
|
|
memset(geometry, 0, sizeof(*geometry));
|
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
geometry->geo_available =
|
2020-03-21 20:54:03 +01:00
|
|
|
((slot->state & (MMCSD_SLOTSTATUS_NOTREADY |
|
|
|
|
MMCSD_SLOTSTATUS_NODISK)) == 0);
|
2008-10-15 16:18:17 +02:00
|
|
|
geometry->geo_mediachanged =
|
|
|
|
((slot->state & MMCSD_SLOTSTATUS_MEDIACHGD) != 0);
|
2020-03-21 17:36:26 +01:00
|
|
|
#if !defined(CONFIG_MMCSD_READONLY)
|
2008-10-15 16:18:17 +02:00
|
|
|
geometry->geo_writeenabled =
|
|
|
|
((slot->state & MMCSD_SLOTSTATUS_WRPROTECT) == 0);
|
|
|
|
#else
|
2009-12-15 15:25:14 +01:00
|
|
|
geometry->geo_writeenabled = false;
|
2008-10-15 16:18:17 +02:00
|
|
|
#endif
|
|
|
|
geometry->geo_nsectors = slot->nsectors;
|
2009-05-28 19:51:24 +02:00
|
|
|
geometry->geo_sectorsize = SECTORSIZE(slot);
|
2008-10-15 19:26:39 +02:00
|
|
|
|
2008-10-16 00:52:42 +02:00
|
|
|
/* After reporting mediachanged, clear the indication so that it is not
|
|
|
|
* reported again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
slot->state &= ~MMCSD_SLOTSTATUS_MEDIACHGD;
|
2022-09-06 08:18:45 +02:00
|
|
|
mmcsd_unlock(slot);
|
2008-10-16 00:52:42 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("geo_available: %d\n", geometry->geo_available);
|
|
|
|
finfo("geo_mediachanged: %d\n", geometry->geo_mediachanged);
|
|
|
|
finfo("geo_writeenabled: %d\n", geometry->geo_writeenabled);
|
2021-07-21 09:27:36 +02:00
|
|
|
finfo("geo_nsectors: %" PRIuOFF "\n", geometry->geo_nsectors);
|
2021-02-17 08:58:11 +01:00
|
|
|
finfo("geo_sectorsize: %" PRIi16 "\n", geometry->geo_sectorsize);
|
2008-10-16 00:52:42 +02:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_mediainitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Detect media and initialize
|
|
|
|
*
|
2013-02-08 16:28:07 +01:00
|
|
|
* Precondition:
|
2022-09-06 08:18:45 +02:00
|
|
|
* Mutex has been taken.
|
2008-10-15 16:18:17 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot)
|
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t csd[16];
|
|
|
|
uint32_t result = MMCSD_SPIR1_IDLESTATE;
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t start;
|
|
|
|
clock_t elapsed;
|
2013-02-08 16:28:07 +01:00
|
|
|
int i;
|
|
|
|
int j;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-27 01:45:45 +02:00
|
|
|
/* Assume that the card is not ready (we'll clear this on successful card
|
2008-10-15 16:18:17 +02:00
|
|
|
* initialization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
slot->state |= MMCSD_SLOTSTATUS_NOTREADY;
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Check if there is a card present in the slot. This is normally a
|
|
|
|
* matter is of GPIO sensing and does not really involve SPI, but by
|
|
|
|
* putting this functionality in the SPI interface, we encapsulate the
|
|
|
|
* SPI MMC/SD interface
|
2008-10-15 16:18:17 +02:00
|
|
|
*/
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
if ((SPI_STATUS(spi, SPIDEV_MMCSD(0)) & SPI_STATUS_PRESENT) == 0)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
fwarn("WARNING: No card present\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
slot->state |= MMCSD_SLOTSTATUS_NODISK;
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clock Freq. Identification Mode < 400kHz */
|
|
|
|
|
2020-09-16 01:39:30 +02:00
|
|
|
slot->spispeed = CONFIG_MMCSD_IDMODE_CLOCK;
|
|
|
|
SPI_SETFREQUENCY(spi, CONFIG_MMCSD_IDMODE_CLOCK);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Set the maximum access time out */
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->taccess = SD_READACCESS;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
/* The SD card wakes up in SD mode. It will enter SPI mode if the chip
|
|
|
|
* select signal is asserted (negative) during the reception of the reset
|
|
|
|
* command (CMD0) and the card is in IDLE state.
|
2008-10-15 16:18:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
2009-05-27 22:45:39 +02:00
|
|
|
/* After power up at least 74 clock cycles are required prior to
|
|
|
|
* starting bus communication
|
|
|
|
*/
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
for (j = 10; j; j--)
|
|
|
|
{
|
2009-04-26 18:06:29 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
2009-05-27 22:45:39 +02:00
|
|
|
/* Send CMD0 (GO_TO_IDLE) with CS asserted to put MMC/SD in
|
|
|
|
* IDLE/SPI mode. Return from CMD0 is R1 which should now
|
|
|
|
* show IDLE STATE
|
2009-05-27 01:45:45 +02:00
|
|
|
*/
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Send CMD0\n");
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), true);
|
2008-10-15 16:18:17 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd0, 0);
|
|
|
|
if (result == MMCSD_SPIR1_IDLESTATE)
|
|
|
|
{
|
2009-05-27 22:45:39 +02:00
|
|
|
/* Break out of the loop with card selected */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Card is in IDLE state\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-05-27 22:45:39 +02:00
|
|
|
|
|
|
|
/* De-select card and try again */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that we exit the above loop with the card reporting IDLE state */
|
|
|
|
|
|
|
|
if (result != MMCSD_SPIR1_IDLESTATE)
|
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
ferr("ERROR: Send CMD0 failed: R1=%02" PRIx32 "\n", result);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->type = MMCSD_CARDTYPE_UNKNOWN;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
/* Check for SDHC Version 2.x. CMD 8 is reserved on SD version 1.0 and
|
|
|
|
* MMC.
|
|
|
|
*/
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Send CMD8\n");
|
2009-05-26 18:07:25 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd8, 0x1aa);
|
|
|
|
if (result == MMCSD_SPIR1_IDLESTATE)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2020-04-13 10:08:03 +02:00
|
|
|
/* Verify the operating voltage and 0xaa was correctly echoed */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
if (((slot->r7 & MMCSD_SPIR7_VOLTAGE_MASK) ==
|
|
|
|
MMCSD_SPIR7_VOLTAGE_27) &&
|
2009-05-27 22:45:39 +02:00
|
|
|
((slot->r7 & MMCSD_SPIR7_ECHO_MASK) == 0xaa))
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
2009-05-27 22:45:39 +02:00
|
|
|
/* Try CMD55/ACMD41 for up to 1 second or until the card exits
|
|
|
|
* the IDLE state
|
|
|
|
*/
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
start = START_TIME;
|
|
|
|
elapsed = 0;
|
|
|
|
do
|
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("%ju. Send CMD55/ACMD41\n", (uintmax_t)elapsed);
|
2009-05-26 18:07:25 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd55, 0);
|
2020-03-21 20:54:03 +01:00
|
|
|
if (result == MMCSD_SPIR1_IDLESTATE ||
|
|
|
|
result == MMCSD_SPIR1_OK)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
2009-12-20 23:35:35 +01:00
|
|
|
result = mmcsd_sendcmd(slot, &g_acmd41, (uint32_t)1 << 30);
|
2009-05-26 18:07:25 +02:00
|
|
|
if (result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
elapsed = ELAPSED_TIME(start);
|
|
|
|
}
|
|
|
|
while (elapsed < MMCSD_DELAY_1SEC);
|
|
|
|
|
|
|
|
/* Check if ACMD41 was sent successfully */
|
|
|
|
|
|
|
|
if (elapsed < MMCSD_DELAY_1SEC)
|
2020-03-21 20:54:03 +01:00
|
|
|
{
|
|
|
|
finfo("Send CMD58\n");
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
SPI_SEND(spi, 0xff);
|
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd58, 0);
|
|
|
|
if (result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("OCR: %08" PRIx32 "\n", slot->ocr);
|
2009-05-26 18:07:25 +02:00
|
|
|
if ((slot->ocr & MMCSD_OCR_CCS) != 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
finfo("Identified SD ver2 card/with block access\n");
|
2020-03-21 20:54:03 +01:00
|
|
|
slot->type = MMCSD_CARDTYPE_SDV2 |
|
|
|
|
MMCSD_CARDTYPE_BLOCK;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
finfo("Identified SD ver2 card\n");
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->type = MMCSD_CARDTYPE_SDV2;
|
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for SDC version 1.x or MMC */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Both the MMC card and the SD card support CMD55 */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Send CMD55/ACMD41\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd55, 0);
|
2009-05-26 18:07:25 +02:00
|
|
|
if (result == MMCSD_SPIR1_IDLESTATE || result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
|
|
|
/* But ACMD41 is supported only on SD */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_acmd41, 0);
|
|
|
|
if (result == MMCSD_SPIR1_IDLESTATE || result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
finfo("Identified SD ver1 card\n");
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->type = MMCSD_CARDTYPE_SDV1;
|
|
|
|
}
|
|
|
|
}
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Make sure that we are out of the Idle state */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
start = START_TIME;
|
|
|
|
elapsed = 0;
|
|
|
|
do
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2009-05-26 18:07:25 +02:00
|
|
|
if (IS_SD(slot->type))
|
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
finfo("%ju. Send CMD55/ACMD41\n", (uintmax_t)elapsed);
|
2009-05-26 18:07:25 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd55, 0);
|
2020-03-21 20:54:03 +01:00
|
|
|
if (result == MMCSD_SPIR1_IDLESTATE ||
|
|
|
|
result == MMCSD_SPIR1_OK)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
|
|
|
result = mmcsd_sendcmd(slot, &g_acmd41, 0);
|
|
|
|
if (result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("%d. Send CMD1\n", i);
|
2008-10-15 16:18:17 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd1, 0);
|
|
|
|
if (result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
finfo("%d. Identified MMC card\n", i);
|
2009-05-26 18:07:25 +02:00
|
|
|
slot->type = MMCSD_CARDTYPE_MMC;
|
|
|
|
break;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
}
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
elapsed = ELAPSED_TIME(start);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
2009-05-26 18:07:25 +02:00
|
|
|
while (elapsed < MMCSD_DELAY_1SEC);
|
|
|
|
|
|
|
|
if (elapsed >= MMCSD_DELAY_1SEC)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed to exit IDLE state\n");
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2009-05-26 18:07:25 +02:00
|
|
|
return -EIO;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
if (slot->type == MMCSD_CARDTYPE_UNKNOWN)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed to identify card\n");
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2009-05-27 22:45:39 +02:00
|
|
|
/* Read CSD. CSD must always be valid */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Get CSD\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
result = mmcsd_getcsd(slot, csd);
|
|
|
|
if (result != OK)
|
|
|
|
{
|
2020-11-22 14:12:03 +01:00
|
|
|
ferr("ERROR: mmcsd_getcsd(CMD9) failed: %" PRId32 "\n", result);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2008-10-15 21:15:53 +02:00
|
|
|
mmcsd_dmpcsd(csd, slot->type);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* CSD data and set block size */
|
|
|
|
|
|
|
|
mmcsd_decodecsd(slot, csd);
|
|
|
|
mmcsd_checkwrprotect(slot, csd);
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
/* SDHC ver2.x cards have fixed block transfer size of 512 bytes. SDC
|
|
|
|
* ver1.x cards with capacity less than 1Gb, will have sector size
|
2018-07-09 02:24:45 +02:00
|
|
|
* 512 bytes. SDC ver1.x cards with capacity of 2Gb will report readbllen
|
2009-05-28 19:51:24 +02:00
|
|
|
* of 1024 but should use 512 bytes for block transfers. SDC ver1.x 4Gb
|
|
|
|
* cards will report readbllen of 2048 bytes -- are they also 512 bytes?
|
|
|
|
* I think that none of these high capacity cards support setting the
|
|
|
|
* block length??
|
|
|
|
*/
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
#ifdef CONFIG_MMCSD_SECTOR512
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Using 512 byte sectors, the maximum ver1.x capacity is 4096 x 512
|
|
|
|
* blocks. The saved slot->nsectors is converted to 512 byte blocks, so
|
|
|
|
* if slot->nsectors exceeds 4096 x 512, then we must be dealing with a
|
|
|
|
* card with read_bl_len of 1024 or 2048.
|
2009-05-28 19:51:24 +02:00
|
|
|
*/
|
2012-06-03 18:09:59 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
if (!IS_SDV2(slot->type) && slot->nsectors <= ((uint32_t)4096 * 12))
|
2009-05-28 19:51:24 +02:00
|
|
|
{
|
|
|
|
/* Don't set the block len on high capacity cards (ver1.x or ver2.x) */
|
|
|
|
|
|
|
|
mmcsd_setblklen(slot, SECTORSIZE(slot));
|
|
|
|
}
|
|
|
|
#else
|
2009-05-26 18:07:25 +02:00
|
|
|
if (!IS_SDV2(slot->type))
|
|
|
|
{
|
2009-05-28 19:51:24 +02:00
|
|
|
/* Don't set the block len on ver2.x cards */
|
|
|
|
|
|
|
|
mmcsd_setblklen(slot, SECTORSIZE(slot));
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
2009-05-28 19:51:24 +02:00
|
|
|
#endif
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
slot->state &= ~MMCSD_SLOTSTATUS_NOTREADY;
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD(0), false);
|
2008-10-15 16:18:17 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_mediachanged
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle initialization/media change events
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static void mmcsd_mediachanged(FAR void *arg)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct mmcsd_slot_s *slot = (FAR struct mmcsd_slot_s *)arg;
|
2008-10-15 16:18:17 +02:00
|
|
|
FAR struct spi_dev_s *spi;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t oldstate;
|
2008-10-15 16:18:17 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (!slot || !slot->spi)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Internal confusion\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-07 23:28:04 +02:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
spi = slot->spi;
|
|
|
|
|
|
|
|
/* Save the current slot state and reassess the new state */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = mmcsd_lock(slot);
|
2020-03-31 23:19:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
nuttx: Fix the nightly build warning
lpc2148_spi1.c:142:24: warning: initialization of 'uint32_t (*)(struct spi_dev_s *, uint32_t)' {aka 'unsigned int (*)(struct spi_dev_s *, unsigned int)'} from incompatible pointer type 'uint16_t (*)(struct spi_dev_s *, uint16_t)' {aka 'short unsigned int (*)(struct spi_dev_s *, short unsigned int)'} [-Wincompatible-pointer-types]
142 | .send = spi_send,
| ^~~~~~~~
lpc2148_spi1.c:142:24: note: (near initialization for 'g_spiops.send')
In file included from ieee802154/mac802154_bind.c:49:
ieee802154/mac802154_internal.h: In function 'mac802154_setdevmode':
ieee802154/mac802154_internal.h:788:42: warning: converting a packed 'enum ieee802154_devmode_e' pointer (alignment 1) to a 'const union ieee802154_attr_u' pointer (alignment 4) may result in an unaligned pointer value [-Waddress-of-packed-member]
788 | (FAR const union ieee802154_attr_u *)&mode);
| ^~~~~~~~~~~~~~~~~
chip/stm32_hciuart.c: In function 'hciuart_read':
chip/stm32_hciuart.c:2104:30: warning: statement with no effect [-Wunused-value]
2104 | ntotal == (ssize_t)ret;
| ~~~~~~~^~~~~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/bcmf_driver.c: In function 'bcmf_wl_auth_event_handler':
wireless/ieee80211/bcm43xxx/bcmf_driver.c:579:23: warning: taking address of packed member of 'struct bcmf_event_s' may result in an unaligned pointer value [-Waddress-of-packed-member]
579 | type = bcmf_getle32(&event->type);
| ^~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/bcmf_driver.c:580:25: warning: taking address of packed member of 'struct bcmf_event_s' may result in an unaligned pointer value [-Waddress-of-packed-member]
580 | status = bcmf_getle32(&event->status);
| ^~~~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/bcmf_driver.c: In function 'bcmf_wl_scan_event_handler':
wireless/ieee80211/bcm43xxx/bcmf_driver.c:619:25: warning: taking address of packed member of 'struct bcmf_event_s' may result in an unaligned pointer value [-Waddress-of-packed-member]
619 | status = bcmf_getle32(&event->status);
| ^~~~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/bcmf_driver.c:620:35: warning: taking address of packed member of 'struct bcmf_event_s' may result in an unaligned pointer value [-Waddress-of-packed-member]
620 | escan_result_len = bcmf_getle32(&event->len);
| ^~~~~~~~~~~
wireless/ieee80211/bcm43xxx/bcmf_bdc.c: In function 'bcmf_bdc_process_event_frame':
wireless/ieee80211/bcm43xxx/bcmf_bdc.c:166:27: warning: taking address of packed member of 'struct bcmf_event_s' may result in an unaligned pointer value [-Waddress-of-packed-member]
166 | event_id = bcmf_getle32(&event_msg->event.type);
| ^~~~~~~~~~~~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c: In function 'sdio_io_rw_direct':
wireless/ieee80211/bcm43xxx/mmc_sdio.c:157:3: warning: converting a packed 'struct sdio_resp_R5' pointer (alignment 1) to a 'uint32_t' {aka 'unsigned int'} pointer (alignment 4) may result in an unaligned pointer value [-Waddress-of-packed-member]
157 | ret = SDIO_RECVR5(dev, SD_ACMD52, (uint32_t *)&resp);
| ^~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c:79:28: note: defined here
79 | begin_packed_struct struct sdio_resp_R5
| ^~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c: In function 'sdio_io_rw_extended':
wireless/ieee80211/bcm43xxx/mmc_sdio.c:239:11: warning: converting a packed 'struct sdio_resp_R5' pointer (alignment 1) to a 'uint32_t' {aka 'unsigned int'} pointer (alignment 4) may result in an unaligned pointer value [-Waddress-of-packed-member]
239 | ret = SDIO_RECVR5(dev, SD_ACMD53, (uint32_t *)&resp);
| ^~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c:79:28: note: defined here
79 | begin_packed_struct struct sdio_resp_R5
| ^~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c:244:11: warning: converting a packed 'struct sdio_resp_R5' pointer (alignment 1) to a 'uint32_t' {aka 'unsigned int'} pointer (alignment 4) may result in an unaligned pointer value [-Waddress-of-packed-member]
244 | ret = SDIO_RECVR5(dev, SD_ACMD53, (uint32_t *)&resp);
| ^~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c:79:28: note: defined here
79 | begin_packed_struct struct sdio_resp_R5
| ^~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c:257:7: warning: converting a packed 'struct sdio_resp_R5' pointer (alignment 1) to a 'uint32_t' {aka 'unsigned int'} pointer (alignment 4) may result in an unaligned pointer value [-Waddress-of-packed-member]
257 | ret = SDIO_RECVR5(dev, SD_ACMD53, (uint32_t *)&resp);
| ^~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c:79:28: note: defined here
79 | begin_packed_struct struct sdio_resp_R5
| ^~~~~~~~~~~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c:265:3: warning: converting a packed 'struct sdio_resp_R5' pointer (alignment 1) to a 'uint32_t' {aka 'unsigned int'} pointer (alignment 4) may result in an unaligned pointer value [-Waddress-of-packed-member]
265 | SDIO_RECVR1(dev, SD_ACMD52ABRT, (uint32_t *)&resp);
| ^~~~~~~~~~~
wireless/ieee80211/bcm43xxx/mmc_sdio.c:79:28: note: defined here
79 | begin_packed_struct struct sdio_resp_R5
| ^~~~~~~~~~~~
chip/stm32_adc.c: In function 'adc_reset':
chip/stm32_adc.c:2860:7: warning: unused variable 'ret' [-Wunused-variable]
2860 | int ret;
| ^~~
chip/stm32_adc.c: In function 'adc_shutdown':
chip/stm32_adc.c:3044:7: warning: unused variable 'ret' [-Wunused-variable]
3044 | int ret;
| ^~~
chip/stm32_i2c.c:722:12: warning: 'stm32_i2c_sem_wait_noncancelable' defined but not used [-Wunused-function]
722 | static int stm32_i2c_sem_wait_noncancelable(FAR struct i2c_master_s *dev)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wireless/gs2200m.c: In function 'gs2200m_read':
wireless/gs2200m.c:727:20: warning: passing argument 1 of 'nxsem_wait' from incompatible pointer type [-Wincompatible-pointer-types]
727 | ret = nxsem_wait(dev);
| ^~~
| |
| struct gs2200m_dev_s *
.config:1207:warning: symbol value '' invalid for TESTING_OSTEST_FPUSIZE
platform/audio/cxd56_audio_analog.c:69:13: warning: inline function 'cxd56_audio_clock_is_enabled' declared but never defined
69 | inline bool cxd56_audio_clock_is_enabled(void);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
platform/audio/cxd56_audio_analog.c:68:13: warning: inline function 'cxd56_audio_clock_disable' declared but never defined
68 | inline void cxd56_audio_clock_disable(void);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
platform/audio/cxd56_audio_analog.c:67:13: warning: inline function 'cxd56_audio_clock_enable' declared but never defined
67 | inline void cxd56_audio_clock_enable(uint32_t clk, uint32_t div);
| ^~~~~~~~~~~~~~~~~~~~~~~~
chip/stm32_adc.c: In function 'adc_reset':
chip/stm32_adc.c:1348:7: warning: unused variable 'ret' [-Wunused-variable]
1348 | int ret;
| ^~~
chip/stm32_adc.c: In function 'adc_shutdown':
chip/stm32_adc.c:1496:7: warning: unused variable 'ret' [-Wunused-variable]
1496 | int ret;
| ^~~
chip/stm32_i2c.c:729:12: warning: 'stm32_i2c_sem_wait_uninterruptble' defined but not used [-Wunused-function]
729 | static int stm32_i2c_sem_wait_uninterruptble(FAR struct i2c_master_s *dev)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wireless/lpwan/sx127x/sx127x.c:147:52: warning: missing terminating ' character
147 | # warning OOK support is not complete, RX+TX doesn't work yet!
| ^
str71_spi.c:435:24: warning: initialization of 'uint32_t (*)(struct spi_dev_s *, uint32_t)' {aka 'unsigned int (*)(struct spi_dev_s *, unsigned int)'} from incompatible pointer type
'uint16_t (*)(struct spi_dev_s *, uint16_t)' {aka 'short unsigned int (*)(struct spi_dev_s *, short unsigned int)'} [-Wincompatible-pointer-types]
435 | .send = spi_send,
| ^~~~~~~~
str71_spi.c:435:24: note: (near initialization for 'g_spiops.send')
chip/pic32mx-lowconsole.c:147:24: warning: 'pic32mx_getreg' defined but not used [-Wunused-function]
static inline uint32_t pic32mx_getreg(uintptr_t uart_base,
^
chip/pic32mx-gpio.c:113:20: warning: 'pic32mx_value' defined but not used [-Wunused-function]
static inline bool pic32mx_value(uint16_t pinset)
^
chip/pic32mz-gpio.c:124:20: warning: 'pic32mz_value' defined but not used [-Wunused-function]
static inline bool pic32mz_value(pinset_t pinset)
^
chip/pic32mx-usbdev.c:3065:1: warning: 'pic32mx_epreserved' defined but not used [-Wunused-function]
pic32mx_epreserved(struct pic32mx_usbdev_s *priv, int epno)
^
mmcsd/mmcsd_spi.c: In function 'mmcsd_mediachanged':
mmcsd/mmcsd_spi.c:1938:7: warning: 'return' with a value, in function returning void
return ret;
^
In file included from partition/fs_partition.c:42:0:
partition/partition.h:66:19: warning: 'read_partition_block' defined but not used [-Wunused-function]
static inline int read_partition_block(FAR struct partition_state_s *state,
^
local/local_netpoll.c: In function 'local_pollsetup':
local/local_netpoll.c:305:1: warning: label 'pollerr' defined but not used [-Wunused-label]
pollerr:
^~~~~~~
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: If3ea8f32b878aa218072130f7c3018f0d3c1aca5
2020-04-12 15:52:28 +02:00
|
|
|
return;
|
2020-03-31 23:19:07 +02:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
oldstate = slot->state;
|
|
|
|
|
|
|
|
/* Check if media was removed or inserted */
|
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
slot->state &= ~(MMCSD_SLOTSTATUS_NODISK | MMCSD_SLOTSTATUS_NOTREADY |
|
|
|
|
MMCSD_SLOTSTATUS_MEDIACHGD);
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
if ((SPI_STATUS(spi, SPIDEV_MMCSD(0)) & SPI_STATUS_PRESENT) == 0)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
/* Media is not present */
|
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
fwarn("WARNING: No card present\n");
|
2015-10-10 18:41:00 +02:00
|
|
|
slot->state |= (MMCSD_SLOTSTATUS_NODISK | MMCSD_SLOTSTATUS_NOTREADY);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Was media removed? */
|
|
|
|
|
|
|
|
if ((oldstate & MMCSD_SLOTSTATUS_NODISK) == 0)
|
|
|
|
{
|
|
|
|
slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
/* Media is present, was it just inserted? Or, if it was previously not
|
|
|
|
* ready, then try re-initializing it
|
2008-10-15 16:18:17 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
else if ((oldstate & (MMCSD_SLOTSTATUS_NODISK |
|
|
|
|
MMCSD_SLOTSTATUS_NOTREADY)) != 0)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2015-03-04 13:51:56 +01:00
|
|
|
/* (Re-)initialize for the media in the slot */
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
ret = mmcsd_mediainitialize(slot);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mmcsd_mediainitialize returned OK\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
|
|
|
|
}
|
|
|
|
}
|
2013-02-28 18:34:57 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
mmcsd_unlock(slot);
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_spislotinitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize one slot for operation using the SPI MMC/SD interface
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* minor - The MMC/SD minor device number. The MMC/SD device will be
|
|
|
|
* registered as /dev/mmcsdN where N is the minor number
|
2015-03-04 13:51:56 +01:00
|
|
|
* slotno - The slot number to use. This is only meaningful for
|
|
|
|
* architectures that support multiple MMC/SD slots. This value must be
|
|
|
|
* in the range {0, ..., CONFIG_MMCSD_NSLOTS}.
|
2016-01-27 16:27:56 +01:00
|
|
|
* spi - And instance of an SPI interface obtained by called the
|
2020-02-23 09:50:23 +01:00
|
|
|
* appropriate xyz_spibus_initialize() function for the MCU "xyz" with
|
2016-01-27 16:27:56 +01:00
|
|
|
* the appropriate port number.
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mmcsd_spislotinitialize(int minor, int slotno, FAR struct spi_dev_s *spi)
|
|
|
|
{
|
|
|
|
struct mmcsd_slot_s *slot;
|
2008-11-10 15:16:12 +01:00
|
|
|
char devname[16];
|
2008-10-15 16:18:17 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2020-03-21 20:54:03 +01:00
|
|
|
if ((unsigned)slotno >= CONFIG_MMCSD_NSLOTS || (unsigned)minor > 255 ||
|
|
|
|
spi == NULL)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Invalid arguments\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Select the slot structure */
|
|
|
|
|
|
|
|
slot = &g_mmcsdslot[slotno];
|
|
|
|
memset(slot, 0, sizeof(struct mmcsd_slot_s));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&slot->lock);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Bind the SPI port to the slot */
|
|
|
|
|
2016-01-24 01:54:36 +01:00
|
|
|
slot->spi = spi;
|
2020-09-16 01:39:30 +02:00
|
|
|
slot->spispeed = CONFIG_MMCSD_IDMODE_CLOCK;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
/* Get exclusive access to the SPI bus and make sure that SPI is properly
|
2013-06-17 20:31:24 +02:00
|
|
|
* configured for the MMC/SD card
|
|
|
|
*/
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = mmcsd_lock(slot);
|
2020-03-31 23:19:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2013-06-17 20:31:24 +02:00
|
|
|
|
2014-04-07 23:28:04 +02:00
|
|
|
/* Initialize for the media in the slot (if any) */
|
2013-06-17 20:31:24 +02:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
ret = mmcsd_mediainitialize(slot);
|
2022-09-06 08:18:45 +02:00
|
|
|
mmcsd_unlock(slot);
|
2008-10-15 16:18:17 +02:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mmcsd_mediainitialize returned OK\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
slot->state |= MMCSD_SLOTSTATUS_MEDIACHGD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a MMC/SD device name */
|
|
|
|
|
2024-08-24 18:54:12 +02:00
|
|
|
snprintf(devname, sizeof(devname), "/dev/mmcsd%d", minor);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* Register the driver, even on a failure condition. A
|
|
|
|
* card may be inserted later, for example.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = register_blockdriver(devname, &g_bops, MMCSD_MODE, slot);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: register_blockdriver failed: %d\n", -ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&slot->lock);
|
2008-10-15 16:18:17 +02:00
|
|
|
slot->spi = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
/* Register a media change callback to handle insertion and
|
2008-10-15 16:18:17 +02:00
|
|
|
* removal of cards.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
SPI_REGISTERCALLBACK(spi, mmcsd_mediachanged, slot);
|
2008-10-15 16:18:17 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2012-11-06 15:57:01 +01:00
|
|
|
|
|
|
|
#endif /* defined (CONFIG_MMCSD) && defined (CONFIG_MMCSD_SPI) */
|