?? mmc_qspi.c
字號:
#include <linux/config.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/init.h>#include <linux/kmod.h>#include <linux/slab.h>#include <linux/delay.h>#include <asm/byteorder.h>#include <asm/errno.h>#include "qspi.h"#include "mmc_qspi.h"#include "mmc_crc.h"#define MMC_DEVICE 0 /* MMC device number on the qspi bus */#define CMD_SIZE 6#define MMC_BLOCK_SIZE 512#define MMC_BLOCK_NB 16 * 1024 * 2#define REG_SIZE 16/* MMC command */#define CMD_0 0 /* reset */#define CMD_1 1 /* initialization */#define CMD_9 2 /* send CSD register */#define CMD_10 3 /* send CID register */#define CMD_13 4 /* send status */#define CMD_17 5 /* read block */#define CMD_24 6 /* write block */#define CMD_59 7 /* set/unset CRC mode */#include "klog.h"struct cmd{ unsigned char cmd; unsigned char a1; unsigned char a2; unsigned char a3; unsigned char a4; unsigned char checksum;} __attribute__((packed));static struct cmd mmc_cmd_array[] ={ {0x40, 0x00, 0x00, 0x00, 0x00, 0x95}, {0x41, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x49, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x4a, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x4d, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x51, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x58, 0x00, 0x00, 0x00, 0x00, 0xff}, {0x7b, 0x00, 0x00, 0x00, 0x00, 0xff}};static int read_answer (unsigned char *);static int send_cmd (unsigned int, unsigned char *);static int get_status (unsigned char *);static int dummy_write (void);static char *get_card_register (unsigned int); /* dummy write function :) */static int dummy_write (void){ int retval; unsigned char dummy_char = 0xff; retval = qspi_ioctl (MMC_DEVICE, CMD_CS_HIGHT, 0); if (retval < 0) { return (retval); } retval = qspi_write (MMC_DEVICE, &dummy_char, sizeof (unsigned char)); if (retval < 0) return (retval); retval = qspi_ioctl (MMC_DEVICE, CMD_CS_LOW, 0); if (retval < 0) { return (retval); } return (0);}static int get_status (unsigned char *status){ int retval, count = 10; /* test cmd_num */ mmc_cmd_array[CMD_13].checksum = 0; mmc_cmd_array[CMD_13].checksum = crc7 (&mmc_cmd_array[CMD_13].cmd, 5); retval = qspi_write (MMC_DEVICE, (char*) &mmc_cmd_array[CMD_13].cmd, CMD_SIZE); if (retval < 0) { return (retval); } if (retval != CMD_SIZE) { err("%s - incomplete command send (%d bytes instead of %d)", __FUNCTION__, retval, CMD_SIZE); return (-EIO); } /* CMD_13 return a 2 bytes answer (R2 format) */ do { retval = qspi_read (MMC_DEVICE, &status[0], sizeof (unsigned char)); if (retval < 0) return (retval); } while ((status[0] == 0xff) && (count--)); retval = qspi_read (MMC_DEVICE, &status[1], sizeof (unsigned char)); if (retval < 0) return (retval); return (0);}static int read_answer (unsigned char *answer){ int retval = 0; unsigned int count = 50; do { retval = qspi_read (MMC_DEVICE, answer, sizeof (unsigned char)); if (retval < 0) return (retval); /* must handle the case retval == 0 */ } while ((*answer == 0xff) && (count--)); /* if the MMC is not ready, 0xff is returned as answer... * we retry count times... */ if (*answer == 0xff) return (-EBUSY); return (0); }static int send_cmd (unsigned int cmd_num, unsigned char *answer){ int retval; /* test cmd_num */ mmc_cmd_array[cmd_num].checksum = 0; mmc_cmd_array[cmd_num].checksum = crc7 (&mmc_cmd_array[cmd_num].cmd, 5); dbg("%s - cmd %d - checksum : 0x%x", __FUNCTION__, cmd_num, mmc_cmd_array[cmd_num].checksum & 0xff); retval = qspi_write (MMC_DEVICE, (char*) &mmc_cmd_array[cmd_num].cmd, CMD_SIZE); if (retval < 0) { return (retval); } if (retval != CMD_SIZE) { err("%s - incomplete command send (%d bytes instead of %d)", __FUNCTION__, retval, CMD_SIZE); return (-EIO); } return (read_answer (answer));}int write_block (unsigned char a1, unsigned char a2, unsigned char a3, unsigned char a4, char *block){ int nb_bytes_write, retval; int i; unsigned int count = 5000; char init_transfer = 0xfe; unsigned short checksum = 0xffff; unsigned char answer; for (i = 0; i < DUMMY_NBR; i++) { retval = dummy_write (); if (retval < 0) { err("%s - dummy_write() error", __FUNCTION__); return (retval); } } /* must lock the mmc cmd array */ mmc_cmd_array[CMD_24].a1 = a1; mmc_cmd_array[CMD_24].a2 = a2; mmc_cmd_array[CMD_24].a3 = a3; mmc_cmd_array[CMD_24].a4 = a4; retval = send_cmd (CMD_24, &answer); if (retval < 0) { err("%s - failed to send CMD_24 (retval %d - answer 0x%x)", __FUNCTION__, retval, answer & 0xff); return (retval); } /* can unlock the mmc cmd array */ if (answer != 0x00) { /* may be we must be patient and * read_answer() more times */ err("%s - CMD_24 - answer 0x%x instead of 0x00", __FUNCTION__, answer & 0xff); err("%s : block %d %d %d %d", __FUNCTION__, a1, a2, a3, a4); return (-EPROTO); } retval = dummy_write (); if (retval < 0) { err("%s - dummy_write () error", __FUNCTION__); return (retval); } /* compute the block checksum and then send it to the MMC */ checksum = (unsigned short) crc16 (block, MMC_BLOCK_SIZE); checksum = __cpu_to_be16(checksum); /* endianness */ /* send 0xfe to initiate block transfer */ retval = qspi_write (MMC_DEVICE, &init_transfer, sizeof (char)); if (retval < 0) { err("%s - failed to init transfer", __FUNCTION__); return (retval); } retval = qspi_write (MMC_DEVICE, block, MMC_BLOCK_SIZE * sizeof (unsigned char)); if (retval < 0) { err("%s - failed to write block", __FUNCTION__); return (retval); } if (retval != MMC_BLOCK_SIZE) { err("%s - %d bytes write instead of %d", __FUNCTION__, retval, MMC_BLOCK_SIZE); } nb_bytes_write = retval; /* send 0xffff as a checksum */ retval = qspi_write (MMC_DEVICE, (char *) &checksum, sizeof (unsigned short)); if (retval < 0) { dbg("%s - failed to init transfer", __FUNCTION__); return (retval); } count = 5000; /* wait for 0xff as a MMC acknowledgement */ do { retval = qspi_read (MMC_DEVICE, &answer, sizeof (unsigned char)); if (retval < 0) return (retval); /* must handle the case retval == 0 */ } while ((answer != 0xff) && (count--)); if (!count) return (-EPROTO); for (i = 0; i < DUMMY_NBR; i++) { retval = dummy_write (); if (retval < 0) { err("%s - dummy_write() error", __FUNCTION__); return (retval); } } return (nb_bytes_write);}int read_block (unsigned char a1, unsigned char a2, unsigned char a3, unsigned char a4, char *block){ int nb_bytes_read, retval; unsigned int count = 10; unsigned char answer; unsigned short checksum, block_crc; int i; if (!block) { err("%s - first argument is a NULL pointer", __FUNCTION__); return (-EINVAL); } for (i = 0; i < DUMMY_NBR; i++) { retval = dummy_write (); if (retval < 0) { err("%s - dummy_write() error", __FUNCTION__); return (retval); } } /* TODO : must lock the mmc cmd array */ mmc_cmd_array[CMD_17].a1 = a1; mmc_cmd_array[CMD_17].a2 = a2; mmc_cmd_array[CMD_17].a3 = a3; mmc_cmd_array[CMD_17].a4 = a4; /* send the single read block command */ retval = send_cmd (CMD_17, &answer); if (retval < 0) { err("%s - failed to send CMD_17", __FUNCTION__); return (retval); } /* to do : can unlock the mmc cmd array */ while (((answer & 0xff) != 0xfe) && (count--)) { retval = read_answer (&answer); if (retval < 0) { err("%s - failed to read answer", __FUNCTION__); return (retval); } } if (answer != 0xfe) { err("%s - CMD_17 failure", __FUNCTION__); /* may be we must be patient and * read_answer() more times */ return (-EPROTO); } dbg("%s - CMD_17 success", __FUNCTION__); retval = qspi_read (MMC_DEVICE, block, MMC_BLOCK_SIZE * sizeof (unsigned char)); if (retval < 0) { err("%s - failed to read block", __FUNCTION__); return (retval); } if (retval != MMC_BLOCK_SIZE) { err("%s - %d bytes read instead of %d", __FUNCTION__, retval, MMC_BLOCK_SIZE); } nb_bytes_read = retval; /* read the checksum (2 bytes) */ retval = qspi_read (MMC_DEVICE, (char *) &checksum, sizeof (unsigned short)); if (retval < 0) { err("%s - failed to read checksum", __FUNCTION__); return (retval); } if (retval != sizeof (unsigned short)) { err("%s - failed to read checksum", __FUNCTION__); return (-EIO); } /* compute the block checksum and compare with * the checksum send by the MMC */ block_crc = (unsigned short) crc16 (block, MMC_BLOCK_SIZE); block_crc = __cpu_to_be16(block_crc); /* endianness */ if (block_crc != checksum) { err("%s - bad checksum : compute %d instead of %d", __FUNCTION__, block_crc, checksum); return (-EIO); } for (i = 0; i < DUMMY_NBR; i++) { retval = dummy_write (); if (retval < 0) { err("%s - dummy_write() error", __FUNCTION__); return (retval); } } return (nb_bytes_read);}char *get_cid (void){ return (get_card_register (CMD_10));}char *get_csd (void){ return (get_card_register (CMD_9));}/* funcion who permit to read card register as the CSD (Card Specific Data) * and the CID (Card Identification Data) */static char *get_card_register (unsigned int cmd){ char *reg; int retval; unsigned int count = 10; unsigned char answer; unsigned short csd_crc, checksum; reg = (char *) kmalloc (REG_SIZE, GFP_KERNEL); if (reg == NULL) return (NULL); retval = send_cmd (cmd, &answer); if (retval < 0) { err("%s - failed to send command (%d)", __FUNCTION__, cmd); return (NULL); } while (((answer & 0xff) != 0xfe) && (count--)) { retval = read_answer (&answer); if (retval < 0) return (NULL); } if (answer != 0xfe) { err("%s - failed to send command (%d)", __FUNCTION__, cmd); return (NULL); } retval = qspi_read (MMC_DEVICE, reg, REG_SIZE); if (retval != REG_SIZE) { return (NULL); } retval = qspi_read (MMC_DEVICE, (char *) &checksum, sizeof (unsigned short)); if (retval != sizeof (unsigned short)) { err("%s - failed to read checksum", __FUNCTION__); return (NULL); } /* compute the block checksum and compare with * the checksum send by the MMC */ csd_crc = (unsigned short) crc16 (reg, REG_SIZE); csd_crc = __cpu_to_be16(csd_crc); /* endianness */ if (csd_crc != checksum) { err("%s - bad checksum : compute %d instead of %d", __FUNCTION__, csd_crc, checksum); return (NULL); } return (reg);}/* function to initiate the MMC */int open_mmc (void){ int retval; int i; unsigned char kbuffer[10]; unsigned char answer; int count = 10; dbg("enter %s", __FUNCTION__); /* in first, we must open the qspi device associate * to the MMC */ retval = qspi_open (MMC_DEVICE); if (retval < 0) { err("%s - qspi_open faillure", __FUNCTION__); return (retval); } /* put the chip select MMC_DEVICE hight during the * initialization */ retval = qspi_ioctl (MMC_DEVICE, CMD_CS_HIGHT, 0); if (retval < 0) { err("%s - qspi_ioctl faillure", __FUNCTION__); return (retval); } /* generate 80 clock cycle to initialize the * MMC card */ for (i = 0; i < 10; i++) { kbuffer[i] = 0xff; } retval = qspi_write (MMC_DEVICE, kbuffer, 10 * sizeof (unsigned char)); if (retval < 0) { err("%s - qspi_write faillure", __FUNCTION__); return (retval); } if (retval < 10 * sizeof (unsigned char)) { err("%s - only %d bytes send to the mmc instead of %d", __FUNCTION__, retval, 10); return (-EIO); } /* put the chip select MMC_DEVICE low to transfert data */ retval = qspi_ioctl (MMC_DEVICE, CMD_CS_LOW, 0); if (retval < 0) { err("%s - qspi_ioctl failure", __FUNCTION__); return (retval); } /* send the CMD0 to set the MMC in qspi mode * as the MMC is in MMC mode during the CMD0 sending, * CMD0 must have a valid checksum */ do { retval = send_cmd (CMD_0, &answer); if (retval < 0) { err("%s - failed to send CMD0", __FUNCTION__); return (retval); } mdelay(100); } while ((count--) && (answer != 0x01)); /* the MMC must acquit the CMD_0 with 0x01 */ if (answer != 0x01) { err("%s - CMD0 failure", __FUNCTION__); return (-EPROTO); } count = 1000; do { retval = dummy_write (); if (retval < 0) { err("%s - dummy_write() error", __FUNCTION__); return (retval); } retval = send_cmd (CMD_1, &answer); if (retval < 0) { err("%s - failed to send CMD_1", __FUNCTION__); return (retval); } } while ((count--) && (answer != 0x00)); retval = dummy_write (); if (retval < 0) { err("%s - dummy_write () error", __FUNCTION__); return (retval); } if (answer != 0x00) { err("%s - CMD_1 failure", __FUNCTION__); return (-EPROTO); } info("%s - MMC init success", __FUNCTION__); /* send command 59 to turn on the checksum control */ mmc_cmd_array[CMD_59].a4 = 1; retval = send_cmd (CMD_59, &answer); if (retval < 0) { err("%s - failed to send CMD_59", __FUNCTION__); return (retval); } if (answer != 0x00) { err("%s - CMD_59 failure (answer = 0x%x)", __FUNCTION__, answer & 0xff); /* may be we must be patient and * read_answer() more times */ return (-EPROTO); } return (0);}/* method to release the MMC device */int release_mmc (void){ int retval; retval = qspi_close (MMC_DEVICE); if (retval < 0) { err("%s - qspi_close faillure", __FUNCTION__); } else { info("%s success to release the MMC", __FUNCTION__); } return (retval);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -