2008-10-15 16:18:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/mmcsd/mmcsd_spi.c
|
|
|
|
*
|
2013-02-01 16:32:39 +01:00
|
|
|
* Copyright (C) 2008-2010, 2011-2013 Gregory Nutt. All rights reserved.
|
2012-05-02 17:36:19 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2008-10-15 16:18:17 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
|
|
|
|
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>
|
2008-10-15 16:18:17 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2009-05-26 18:07:25 +02:00
|
|
|
#include <nuttx/clock.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 ************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_MMCSD_NSLOTS
|
2009-02-07 03:07:42 +01:00
|
|
|
# ifdef CONFIG_CPP_HAVE_WARNING
|
|
|
|
# warning "CONFIG_MMCSD_NSLOTS not defined"
|
|
|
|
# endif
|
2008-10-15 16:18:17 +02:00
|
|
|
# define CONFIG_MMCSD_NSLOTS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MMCSD_IDMODE_CLOCK (400000)
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
|
|
|
|
# define MMCSD_MODE 0666
|
|
|
|
#else
|
|
|
|
# define MMCSD_MODE 0444
|
|
|
|
#endif
|
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
#ifndef CONFIG_MMCSD_SPICLOCK
|
|
|
|
# define CONFIG_MMCSD_SPICLOCK 20000000
|
|
|
|
#endif
|
|
|
|
|
2013-06-17 20:31:24 +02:00
|
|
|
#ifndef CONFIG_MMCSD_SPIMODE
|
|
|
|
# define CONFIG_MMCSD_SPIMODE SPIDEV_MODE0
|
|
|
|
#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 *********************************************************/
|
|
|
|
/* 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 ***************************************/
|
|
|
|
/* 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
|
|
|
|
2011-04-13 23:44:24 +02:00
|
|
|
#define ELAPSED_TIME(t) (clock_systimer()-(t))
|
|
|
|
#define START_TIME (clock_systimer())
|
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 */
|
2015-03-04 13:51:56 +01:00
|
|
|
sem_t sem; /* 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
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
static void mmcsd_semtake(FAR struct mmcsd_slot_s *slot);
|
|
|
|
static void mmcsd_semgive(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,
|
|
|
|
const struct mmcsd_cmdinfo_s *cmd, uint32_t arg);
|
|
|
|
static void mmcsd_setblklen(FAR struct mmcsd_slot_s *slot,
|
|
|
|
uint32_t length);
|
|
|
|
static uint32_t mmcsd_nsac(FAR struct mmcsd_slot_s *slot, uint8_t *csd,
|
|
|
|
uint32_t frequency);
|
|
|
|
static uint32_t mmcsd_taac(FAR struct mmcsd_slot_s *slot, uint8_t *csd);
|
|
|
|
static void mmcsd_decodecsd(FAR struct mmcsd_slot_s *slot, uint8_t *csd);
|
|
|
|
static void mmcsd_checkwrprotect(FAR struct mmcsd_slot_s *slot,
|
|
|
|
uint8_t *csd);
|
|
|
|
static int mmcsd_getcardinfo(FAR struct mmcsd_slot_s *slot,
|
|
|
|
uint8_t *buffer, 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,
|
|
|
|
uint8_t *buffer, int nbytes);
|
2009-05-26 18:07:25 +02:00
|
|
|
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_xmitblock(FAR struct mmcsd_slot_s *slot,
|
|
|
|
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);
|
|
|
|
static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
|
|
|
|
size_t start_sector, unsigned int nsectors);
|
2008-10-15 16:18:17 +02:00
|
|
|
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
|
2009-12-15 15:25:14 +01:00
|
|
|
static ssize_t mmcsd_write(FAR struct inode *inode,
|
|
|
|
const unsigned char *buffer, size_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,
|
|
|
|
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);
|
|
|
|
static void mmcsd_mediachanged(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 */
|
|
|
|
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
|
|
|
|
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
|
|
|
{
|
2015-03-04 13:51:56 +01:00
|
|
|
10000, /* 0: 10 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
|
|
|
{
|
|
|
|
0, 10, 12, 13, /* 0-3: Reserved, 1.0, 1.1, 1.2, 1.3 */
|
|
|
|
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
|
|
|
|
|
|
|
1, /* 0: 1 ns */
|
|
|
|
10, /* 1: 10 ns */
|
|
|
|
100, /* 2: 100 ns */
|
|
|
|
|
2009-05-27 22:45:39 +02:00
|
|
|
/* Units of microseconds */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
|
|
|
1, /* 3: 1 us 1,000 ns */
|
|
|
|
10, /* 4: 10 us 10,000 ns */
|
|
|
|
100, /* 5: 100 us 100,000 ns */
|
2015-10-10 18:41:00 +02:00
|
|
|
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 *****************************************************************/
|
|
|
|
|
2009-05-27 22:45:39 +02: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};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_cmd10 = {CMD10, MMCSD_CMDRESP_R1, 0xff};
|
|
|
|
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};
|
|
|
|
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};
|
|
|
|
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};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_acmd23 = {ACMD23, MMCSD_CMDRESP_R1, 0xff};
|
|
|
|
static const struct mmcsd_cmdinfo_s g_acmd41 = {ACMD41, MMCSD_CMDRESP_R1, 0xff};
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_semtake
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
static void mmcsd_semtake(FAR struct mmcsd_slot_s *slot)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
2013-02-08 16:28:07 +01:00
|
|
|
/* Get exclusive access to the SPI bus (if necessary) */
|
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
(void)SPI_LOCK(slot->spi, true);
|
|
|
|
|
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);
|
2016-01-23 23:18:13 +01:00
|
|
|
(void)SPI_HWFEATURES(slot->spi, 0);
|
|
|
|
(void)SPI_SETFREQUENCY(slot->spi, slot->spispeed);
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
/* Get exclusive access to the MMC/SD device (possibly unnecessary if
|
|
|
|
* SPI_LOCK is also implemented as a semaphore).
|
2013-02-08 16:28:07 +01:00
|
|
|
*/
|
2013-02-28 18:34:57 +01:00
|
|
|
|
|
|
|
while (sem_wait(&slot->sem) != 0)
|
2008-10-15 16:18:17 +02:00
|
|
|
{
|
|
|
|
/* The only case that an error should occur here is if the wait was
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(errno == EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-17 20:31:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_semgive
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
static void mmcsd_semgive(FAR struct mmcsd_slot_s *slot)
|
2013-02-08 16:28:07 +01:00
|
|
|
{
|
|
|
|
/* Relinquish the lock on the MMC/SD device */
|
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
sem_post(&slot->sem);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)SPI_SEND(slot->spi, 0xff);
|
|
|
|
|
|
|
|
/* Relinquish exclusive access to the SPI bus */
|
|
|
|
|
|
|
|
(void)SPI_LOCK(slot->spi, false);
|
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;
|
2016-01-21 18:54:26 +01:00
|
|
|
systime_t start;
|
|
|
|
systime_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);
|
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,
|
|
|
|
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;
|
2016-01-21 18:54:26 +01:00
|
|
|
systime_t start;
|
|
|
|
systime_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)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed: card still busy (%02x)\n", busy);
|
2009-12-15 15:25:14 +01:00
|
|
|
return (uint32_t)-1;
|
2009-05-26 18:07:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("CMD%d[%08x] R1B=%02x\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:
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("CMD%d[%08x] R1=%02x\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
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("CMD%d[%08x] R2=%04x\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
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("CMD%d[%08x] R1=%02x OCR=%08x\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
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("CMD%d[%08x] R1=%02x R7=%08x\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
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Set block length to %d\n", length);
|
2009-05-27 01:45:45 +02:00
|
|
|
response = mmcsd_sendcmd(slot, &g_cmd16, length);
|
|
|
|
if (response != MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Failed to set block length: %02x\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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static uint32_t mmcsd_nsac(FAR struct mmcsd_slot_s *slot, uint8_t *csd,
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t nsac = MMCSD_CSD_NSAC(csd) * ((uint32_t)100*1000);
|
|
|
|
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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static uint32_t mmcsd_taac(FAR struct mmcsd_slot_s *slot, 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
|
|
|
|
* 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
|
|
|
|
2009-05-27 01:45:45 +02: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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static void mmcsd_decodecsd(FAR struct mmcsd_slot_s *slot, 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
|
|
|
|
* taccess = (1,500 * 100) / 100,000) + 1 = 2 (ideal, 1.5)
|
|
|
|
* twrite = (1,500 * 4 * 100) / 100,000) + 1 = 7 (ideal 6.0)
|
|
|
|
*
|
|
|
|
* First get the access time in microseconds
|
2009-05-26 18:07:25 +02:00
|
|
|
*/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t taccessus = mmcsd_taac(slot, csd) + mmcsd_nsac(slot, csd, frequency);
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2009-05-27 01:45:45 +02: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, or
|
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
|
|
|
|
2009-05-27 01:45:45 +02: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
|
|
|
|
2009-05-27 01:45:45 +02: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");
|
|
|
|
finfo(" Maximum: %d Hz\n", maxfrequency);
|
|
|
|
finfo(" Actual: %d Hz\n", frequency);
|
|
|
|
finfo("Read access time: %d ticks\n", slot->taccess);
|
|
|
|
finfo("Write access time: %d 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 */
|
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 */
|
|
|
|
/* 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
|
|
|
|
* 512 byes. SDC ver1.x cards with capacity of 2Gb will report readbllen
|
|
|
|
* 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));
|
|
|
|
finfo("Number of sectors: %d\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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static void mmcsd_checkwrprotect(FAR struct mmcsd_slot_s *slot, 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.
|
|
|
|
*/
|
|
|
|
|
2009-03-29 20:53:20 +02:00
|
|
|
if ((SPI_STATUS(spi, SPIDEV_MMCSD) & 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
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static int mmcsd_getcardinfo(FAR struct mmcsd_slot_s *slot, uint8_t *buffer,
|
2008-10-15 16:18:17 +02:00
|
|
|
const struct mmcsd_cmdinfo_s *cmd)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: CMD9/10 failed: R1=%02x\n", result);
|
2009-05-27 01:45:45 +02:00
|
|
|
return -EIO;
|
2008-10-15 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try up to 8 times to find the start of block (or until an error occurs) */
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: %d. 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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-04 13:51:56 +01:00
|
|
|
static int mmcsd_recvblock(FAR struct mmcsd_slot_s *slot, uint8_t *buffer,
|
|
|
|
int nbytes)
|
2009-05-26 18:07:25 +02:00
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi = slot->spi;
|
2016-01-21 18:54:26 +01:00
|
|
|
systime_t start;
|
|
|
|
systime_t elapsed;
|
|
|
|
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
|
|
|
|
* received */
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_WRITABLE) && !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 */
|
|
|
|
|
|
|
|
(void)SPI_SNDBLOCK(spi, buffer, nbytes);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_FS_WRITABLE && !CONFIG_MMCSD_READONLY */
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
slot = (FAR struct mmcsd_slot_s *)inode->i_private;
|
|
|
|
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 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;
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semtake(slot);
|
2009-05-29 15:32:00 +02:00
|
|
|
if ((SPI_STATUS(spi, SPIDEV_MMCSD) & SPI_STATUS_PRESENT) != 0)
|
|
|
|
{
|
|
|
|
/* 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");
|
2009-05-29 15:32:00 +02:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, true);
|
2009-05-29 15:32:00 +02:00
|
|
|
ret = mmcsd_waitready(slot);
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2009-05-29 15:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_sem:
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(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
|
|
|
|
*
|
|
|
|
* Description: Read the specified numer of sectors
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer,
|
2008-10-15 19:26:39 +02:00
|
|
|
size_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;
|
|
|
|
size_t nbytes;
|
|
|
|
off_t offset;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t response;
|
2009-05-26 18:07:25 +02:00
|
|
|
int i;
|
2008-10-15 16:18:17 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("start_sector=%d nsectors=%d\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 */
|
|
|
|
|
|
|
|
slot = (FAR struct mmcsd_slot_s *)inode->i_private;
|
|
|
|
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;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("nbytes=%d sector offset=%d\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);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("nbytes=%d byte offset=%d\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
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semtake(slot);
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, 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");
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_eio;
|
|
|
|
}
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2009-05-28 19:51:24 +02:00
|
|
|
buffer += SECTORSIZE(slot);
|
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 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
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2009-05-26 18:07:25 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(slot);
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Read %d 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:
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
|
|
|
|
static ssize_t mmcsd_write(FAR struct inode *inode, const unsigned char *buffer,
|
|
|
|
size_t start_sector, unsigned int nsectors)
|
|
|
|
{
|
|
|
|
FAR struct mmcsd_slot_s *slot;
|
|
|
|
FAR struct spi_dev_s *spi;
|
|
|
|
size_t nbytes;
|
|
|
|
off_t offset;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t response;
|
2009-05-26 18:07:25 +02:00
|
|
|
int i;
|
2008-10-15 19:26:39 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("start_sector=%d nsectors=%d\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 */
|
|
|
|
|
|
|
|
slot = (FAR struct mmcsd_slot_s *)inode->i_private;
|
|
|
|
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;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("nbytes=%d sector offset=%d\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);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("nbytes=%d byte offset=%d\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
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semtake(slot);
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, 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
|
|
|
{
|
2009-05-26 18:07:25 +02:00
|
|
|
/* Send CMD24 (WRITE_BLOCK) and verify that good R1 status is returned */
|
|
|
|
|
|
|
|
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);
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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");
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
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);
|
2015-03-13 14:26:16 +01:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
|
|
|
|
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);
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
|
|
|
}
|
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);
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
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");
|
2009-05-26 18:07:25 +02:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
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");
|
2015-03-13 14:26:16 +01:00
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
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 */
|
|
|
|
|
2013-06-16 01:00:52 +02:00
|
|
|
(void)mmcsd_waitready(slot);
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2009-05-26 18:07:25 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(slot);
|
2008-10-15 16:18:17 +02:00
|
|
|
|
|
|
|
/* The success return value is the number of sectors written */
|
|
|
|
|
|
|
|
return nsectors;
|
|
|
|
|
|
|
|
errout_with_sem:
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry)
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
|
|
|
slot = (FAR struct mmcsd_slot_s *)inode->i_private;
|
|
|
|
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 */
|
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semtake(slot);
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, true);
|
2008-10-15 16:18:17 +02:00
|
|
|
ret = mmcsd_getcsd(slot, csd);
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2009-05-27 01:45:45 +02:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(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 */
|
|
|
|
|
|
|
|
geometry->geo_available =
|
2015-10-10 18:41:00 +02: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);
|
|
|
|
#if defined(CONFIG_FS_WRITABLE) && !defined(CONFIG_MMCSD_READONLY)
|
|
|
|
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;
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(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);
|
|
|
|
finfo("geo_nsectors: %d\n", geometry->geo_nsectors);
|
|
|
|
finfo("geo_sectorsize: %d\n", geometry->geo_sectorsize);
|
2008-10-16 00:52:42 +02:00
|
|
|
|
2008-10-15 16:18:17 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Initialization
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_mediainitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Detect media and initialize
|
|
|
|
*
|
2013-02-08 16:28:07 +01:00
|
|
|
* Precondition:
|
|
|
|
* Semaphore 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;
|
2016-01-21 18:54:26 +01:00
|
|
|
systime_t start;
|
|
|
|
systime_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;
|
|
|
|
|
|
|
|
/* 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
|
2015-03-04 13:51:56 +01:00
|
|
|
* functionality in the SPI interface, we encapsulate the SPI MMC/SD
|
2008-10-15 16:18:17 +02:00
|
|
|
* interface
|
|
|
|
*/
|
|
|
|
|
2009-03-29 20:53:20 +02:00
|
|
|
if ((SPI_STATUS(spi, SPIDEV_MMCSD) & 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 */
|
|
|
|
|
2013-06-17 20:31:24 +02:00
|
|
|
slot->spispeed = MMCSD_IDMODE_CLOCK;
|
2016-01-23 23:18:13 +01:00
|
|
|
(void)SPI_SETFREQUENCY(spi, 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");
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, 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 */
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, 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)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Send CMD0 failed: R1=%02x\n", result);
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(slot);
|
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
|
|
|
{
|
2009-05-27 22:45:39 +02:00
|
|
|
/* Verify the operating voltage and that the 0xaa was correctly echoed */
|
2009-05-26 18:07:25 +02:00
|
|
|
|
2009-05-27 22:45:39 +02:00
|
|
|
if (((slot->r7 & MMCSD_SPIR7_VOLTAGE_MASK) == MMCSD_SPIR7_VOLTAGE_27) &&
|
|
|
|
((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
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("%d. Send CMD55/ACMD41\n", elapsed);
|
2009-05-26 18:07:25 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd55, 0);
|
|
|
|
if (result == MMCSD_SPIR1_IDLESTATE || result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
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)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Send CMD58\n");
|
2013-02-08 16:28:07 +01:00
|
|
|
|
2009-05-26 18:07:25 +02:00
|
|
|
SPI_SEND(spi, 0xff);
|
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd58, 0);
|
|
|
|
if (result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("OCR: %08x\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");
|
2015-10-10 18:41:00 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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))
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("%d. Send CMD55/ACMD41\n", elapsed);
|
2009-05-26 18:07:25 +02:00
|
|
|
result = mmcsd_sendcmd(slot, &g_cmd55, 0);
|
|
|
|
if (result == MMCSD_SPIR1_IDLESTATE || result == MMCSD_SPIR1_OK)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2009-05-26 18:07:25 +02: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");
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(slot);
|
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");
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(slot);
|
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)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: mmcsd_getcsd(CMD9) failed: %d\n", result);
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(slot);
|
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
|
|
|
|
* 512 byes. SDC ver1.x cards with capacity of 2Gb will report readbllen
|
|
|
|
* 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
|
|
|
|
/* 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.
|
|
|
|
*/
|
2012-06-03 18:09:59 +02:00
|
|
|
|
2009-12-15 15:25:14 +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;
|
2009-12-15 15:25:14 +01:00
|
|
|
SPI_SELECT(spi, SPIDEV_MMCSD, false);
|
2008-10-15 16:18:17 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mmcsd_mediachanged
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle initialization/media change events
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void mmcsd_mediachanged(void *arg)
|
|
|
|
{
|
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 */
|
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semtake(slot);
|
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);
|
|
|
|
|
2009-03-29 20:53:20 +02:00
|
|
|
if ((SPI_STATUS(spi, SPIDEV_MMCSD) & 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
|
|
|
*/
|
|
|
|
|
2015-10-10 18:41:00 +02: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
|
|
|
|
|
|
|
mmcsd_semgive(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
|
|
|
|
* approprite xyz_spibus_initialize() function for the MCU "xyz" with
|
|
|
|
* 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
|
2008-10-15 19:26:39 +02:00
|
|
|
if ((unsigned)slotno >= CONFIG_MMCSD_NSLOTS || (unsigned)minor > 255 || !spi)
|
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));
|
|
|
|
sem_init(&slot->sem, 0, 1);
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-15 16:18:17 +02:00
|
|
|
if (slot->spi)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Already registered\n");
|
2008-10-15 16:18:17 +02:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Bind the SPI port to the slot */
|
|
|
|
|
2016-01-24 01:54:36 +01:00
|
|
|
slot->spi = spi;
|
2013-02-28 18:34:57 +01:00
|
|
|
slot->spispeed = 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
|
|
|
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semtake(slot);
|
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);
|
2013-02-28 18:34:57 +01:00
|
|
|
mmcsd_semgive(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 */
|
|
|
|
|
|
|
|
snprintf(devname, 16, "/dev/mmcsd%d", minor);
|
|
|
|
|
|
|
|
/* 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);
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
(void)SPI_REGISTERCALLBACK(spi, mmcsd_mediachanged, (FAR void *)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) */
|