Advertisement
Guest User

GC-Linux SDHC gcn-sd.c

a guest
Feb 13th, 2012
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 40.49 KB | None | 0 0
  1. /*
  2.  * drivers/block/gcn-sd.c
  3.  *
  4.  * MMC/SD card block driver for the Nintendo GameCube/Wii
  5.  * Copyright (C) 2004-2009 The GameCube Linux Team
  6.  * Copyright (C) 2004,2005 Rob Reylink
  7.  * Copyright (C) 2005 Todd Jeffreys
  8.  * Copyright (C) 2005,2006,2007,2008,2009 Albert Herranz
  9.  * Copyleft  (C) 2012, Gerrit Pannek
  10.  *
  11.  * This program is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU General Public License
  13.  * as published by the Free Software Foundation; either version 2
  14.  * of the License, or (at your option) any later version.
  15.  *
  16.  */
  17.  
  18. /*
  19.  * This is a block device driver for the Nintendo SD Card Adapter (DOL-019)
  20.  * and compatible hardware.
  21.  * The driver has been tested with SPI-enabled MMC cards and SD cards.
  22.  *
  23.  * The following table shows the device major and minors needed to access
  24.  * MMC/SD cards:
  25.  *
  26.  * +------+-------------+-------+-------+
  27.  * | Slot | Target      | Major | Minor |
  28.  * +======+=============+=======+=======+
  29.  * | A    | disk        | 61    | 0     |
  30.  * | A    | partition 1 | 61    | 1     |
  31.  * | A    | partition 2 | 61    | 2     |
  32.  * | A    | partition 3 | 61    | 3     |
  33.  * | A    | partition 4 | 61    | 4     |
  34.  * | A    | partition 5 | 61    | 5     |
  35.  * | A    | partition 6 | 61    | 6     |
  36.  * | A    | partition 7 | 61    | 7     |
  37.  * +------+-------------+-------+-------+
  38.  * | B    | disk        | 61    | 8     |
  39.  * | B    | partition 1 | 61    | 9     |
  40.  * | B    | partition 2 | 61    | 10    |
  41.  * | B    | partition 3 | 61    | 11    |
  42.  * | B    | partition 4 | 61    | 12    |
  43.  * | B    | partition 5 | 61    | 13    |
  44.  * | B    | partition 6 | 61    | 14    |
  45.  * | B    | partition 7 | 61    | 15    |
  46.  * +------+-------------+-------+-------+
  47.  *
  48.  * For example, run "mknod /dev/gcnsdb1 b 61 9" to create a device file
  49.  * to access the 1st partition on the card inserted in memcard slot B.
  50.  *
  51.  */
  52.  
  53. #define SD_DEBUG
  54.  
  55. #include <linux/kernel.h>
  56. #include <linux/module.h>
  57. #include <linux/init.h>
  58. #include <linux/delay.h>
  59. #include <linux/kthread.h>
  60. #include <linux/major.h>
  61. #include <linux/blkdev.h>
  62. #include <linux/hdreg.h>
  63. #include <linux/crc-ccitt.h>
  64.  
  65. /*
  66.  * The existing Linux MMC layer does not support SPI operation yet.
  67.  * Anyway, we try to recycle here some common code.
  68.  */
  69. #include <linux/mmc/host.h>
  70. #include <linux/mmc/card.h>
  71. #include <linux/mmc/mmc.h>
  72. #include <linux/mmc/sd.h>
  73.  
  74. #include <linux/exi.h>
  75.  
  76. #define DRV_MODULE_NAME "gcn-sd"
  77. #define DRV_DESCRIPTION "MMC/SD/SDHC card block driver for the Nintendo GameCube/Wii"
  78. #define DRV_AUTHOR  "Rob Reylink, " \
  79.             "Todd Jeffreys, " \
  80.             "Albert Herranz" \
  81.             "Gerrit Pannek"
  82.  
  83. static char sd_driver_version[] = "4.2";
  84.  
  85. #define sd_printk(level, format, arg...) \
  86.     printk(level DRV_MODULE_NAME ": " format , ## arg)
  87.  
  88. #ifdef SD_DEBUG
  89. #  define DBG(fmt, args...) \
  90.        printk(KERN_ERR "%s: " fmt, __func__ , ## args)
  91. #else
  92. #  define DBG(fmt, args...)
  93. #endif
  94.  
  95.  
  96. /*
  97.  *
  98.  * EXI related definitions.
  99.  */
  100. #define SD_SLOTA_CHANNEL    0   /* EXI0xxx */
  101. #define SD_SLOTA_DEVICE     0   /* chip select, EXI0CSB0 */
  102.  
  103. #define SD_SLOTB_CHANNEL    1   /* EXI1xxx */
  104. #define SD_SLOTB_DEVICE     0   /* chip select, EXI1CSB0 */
  105.  
  106. #define SD_SPI_CLK      16000000
  107. #define SD_SPI_CLK_IDX      EXI_CLK_16MHZ
  108.  
  109.  
  110. /*
  111.  *
  112.  * MMC/SD related definitions.
  113.  */
  114.  
  115. /* cycles in 8 clock units */
  116. #define SD_IDLE_CYCLES      80
  117. #define SD_FINISH_CYCLES    8
  118.  
  119. /* several times in 8 clock units */
  120. #define MMC_SPI_N_CR        8   /* card response time */
  121.  
  122. /* data start and stop tokens */
  123. #define MMC_SPI_TOKEN_START_SINGLE_BLOCK_READ       0xfe
  124. #define MMC_SPI_TOKEN_START_MULTIPLE_BLOCK_READ     0xfe
  125. #define MMC_SPI_TOKEN_START_SINGLE_BLOCK_WRITE      0xfe
  126. #define MMC_SPI_TOKEN_START_MULTIPLE_BLOCK_WRITE    0xfc
  127. #define MMC_SPI_TOKEN_STOP_MULTIPLE_BLOCK_WRITE     0xfd
  128.  
  129. /* data response */
  130. #define DR_SPI_MASK             0x1f
  131. #define DR_SPI_DATA_ACCEPTED            0x05
  132. #define DR_SPI_DATA_REJECTED_CRC_ERROR      0x0b
  133. #define DR_SPI_DATA_REJECTED_WRITE_ERROR    0x0d
  134.  
  135. /* this is still a missing command in the current MMC framework ... */
  136. #define MMC_READ_OCR        58
  137.  
  138. /*
  139.  * OCR Bit positions to 10s of Vdd mV.
  140.  */
  141. static const unsigned short mmc_ocr_bit_to_vdd[] = {
  142.     150, 155, 160, 165, 170, 180, 190, 200,
  143.     210, 220, 230, 240, 250, 260, 270, 280,
  144.     290, 300, 310, 320, 330, 340, 350, 360
  145. };
  146.  
  147. static const unsigned int tran_exp[] = {
  148.     10000, 100000, 1000000, 10000000,
  149.     0, 0, 0, 0
  150. };
  151.  
  152. static const unsigned char tran_mant[] = {
  153.     0, 10, 12, 13, 15, 20, 25, 30,
  154.     35, 40, 45, 50, 55, 60, 70, 80,
  155. };
  156.  
  157. static const unsigned int tacc_exp[] = {
  158.     1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  159. };
  160.  
  161. static const unsigned int tacc_mant[] = {
  162.     0, 10, 12, 13, 15, 20, 25, 30,
  163.     35, 40, 45, 50, 55, 60, 70, 80,
  164. };
  165.  
  166. /*
  167.  * Driver settings.
  168.  */
  169. #define MMC_SHIFT       3   /* 8 partitions */
  170.  
  171. #define SD_MAJOR        61
  172. #define SD_NAME         "gcnsd"
  173.  
  174. #define KERNEL_SECTOR_SHIFT     9
  175. #define KERNEL_SECTOR_SIZE      (1 << KERNEL_SECTOR_SHIFT)  /*512 */
  176.  
  177. enum {
  178.     __SD_MEDIA_CHANGED = 0,
  179.     __SD_BAD_CARD,
  180. };
  181.  
  182.  
  183. /*
  184.  * Raw MMC/SD command.
  185.  */
  186. struct sd_command {
  187.     u8 cmd;
  188.     u32 arg;
  189.     u8 crc;
  190. } __attribute__ ((__packed__)); /* do not add padding, please */
  191.  
  192. /*
  193.  * MMC/SD host.
  194.  *
  195.  * We have one host for each memory card slot. And a host can only drive a
  196.  * single card each time.
  197.  */
  198. struct sd_host {
  199.     spinlock_t      lock;
  200.  
  201.     int refcnt;
  202.     unsigned long flags;
  203. #define SD_MEDIA_CHANGED    (1<<__SD_MEDIA_CHANGED)
  204. #define SD_BAD_CARD     (1<<__SD_BAD_CARD)
  205.  
  206.     /* card related info */
  207.     struct mmc_card card;
  208.  
  209.     /* timeouts in 8 clock cycles */
  210.     unsigned long read_timeout;
  211.     unsigned long write_timeout;
  212.  
  213.     /* operations condition register */
  214.     u32 ocr_avail;      /* just 3.3V for the GameCube */
  215.     u32 ocr;
  216.  
  217.     /* last card response */
  218.     u8 resp;
  219.  
  220.     /* frequency */
  221.     unsigned int clock;
  222.     u8 exi_clock;
  223.  
  224.     /* command buffer */
  225.     struct sd_command cmd;
  226.  
  227.     spinlock_t      queue_lock;
  228.     struct request_queue    *queue;
  229.  
  230.     struct gendisk      *disk;
  231.  
  232.     struct task_struct  *io_thread;
  233.     struct mutex        io_mutex;
  234.  
  235.     struct exi_device   *exi_device;
  236. };
  237.  
  238. static void sd_kill(struct sd_host *host);
  239.  
  240.  
  241. static void sd_card_set_bad(struct sd_host *host)
  242. {
  243.     set_bit(__SD_BAD_CARD, &host->flags);
  244. }
  245.  
  246. static int sd_card_is_bad(struct sd_host *host)
  247. {
  248.     return test_bit(__SD_BAD_CARD, &host->flags);
  249. }
  250.  
  251. /*
  252.  *
  253.  * MMC/SD data structures manipulation.
  254.  */
  255.  
  256. /*
  257.  * FIXME: use a faster method (table)
  258.  * (the in-kernel crc 16 (ccitt crc) tables seem not compatible with us)
  259.  */
  260. static u16 crc_xmodem_update(u16 crc, u8 data)
  261. {
  262.     int i;
  263.  
  264.     crc = crc ^ ((u16) data << 8);
  265.  
  266.     for (i = 0; i < 8; i++) {
  267.         if (crc & 0x8000)
  268.             crc = (crc << 1) ^ 0x1021;
  269.         else
  270.             crc <<= 1;
  271.     }
  272.  
  273.     return crc;
  274. }
  275.  
  276. #define UNSTUFF_BITS(resp, start, size)             \
  277.     ({                          \
  278.     const int __size = size;                \
  279.     const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  280.     const int __off = 3 - ((start) / 32);           \
  281.     const int __shft = (start) & 31;            \
  282.     u32 __res;                      \
  283.                                 \
  284.     __res = resp[__off] >> __shft;              \
  285.     if (__size + __shft > 32)               \
  286.         __res |= resp[__off-1] << ((32 - __shft) % 32); \
  287.     __res & __mask;                     \
  288.     })
  289.  
  290. /*
  291.  * Given the decoded CSD structure, decode the raw CID to our CID structure.
  292.  */
  293. static void mmc_decode_cid(struct mmc_card *card)
  294. {
  295.     u32 *resp = card->raw_cid;
  296.  
  297.     memset(&card->cid, 0, sizeof(struct mmc_cid));
  298.  
  299.     if (mmc_card_sd(card)) {
  300.         card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  301.         card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  302.         card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  303.         card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  304.         card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  305.         card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  306.         card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  307.         card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
  308.         card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
  309.         card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
  310.         card->cid.year = UNSTUFF_BITS(resp, 12, 8);
  311.         card->cid.month = UNSTUFF_BITS(resp, 8, 4);
  312.         card->cid.year += 2000;
  313.     } else {
  314.         /*
  315.          * The selection of the format here is guesswork based upon
  316.          * information people have sent to date.
  317.          */
  318.         switch (card->csd.mmca_vsn) {
  319.         case 0: /* MMC v1.0 - v1.2 */
  320.         case 1: /* MMC v1.4 */
  321.             card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  322.             card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  323.             card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  324.             card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  325.             card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  326.             card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  327.             card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  328.             card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  329.             card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  330.             card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  331.             card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  332.             card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  333.             card->cid.year = UNSTUFF_BITS(resp, 8, 4);
  334.             card->cid.year += 1997;
  335.             break;
  336.         case 2: /* MMC v2.0 - v2.2 */
  337.         case 3: /* MMC v3.1 - v3.3 */
  338.             card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  339.             card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  340.             card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  341.             card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  342.             card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  343.             card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  344.             card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  345.             card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  346.             card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  347.             card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  348.             card->cid.year = UNSTUFF_BITS(resp, 8, 4);
  349.             card->cid.year += 1997;
  350.             break;
  351.         default:
  352.             sd_printk(KERN_ERR, "card has unknown MMCA"
  353.                   " version %d\n", card->csd.mmca_vsn);
  354.             break;
  355.         }
  356.     }
  357. }
  358.  
  359. /*
  360.  * Given a 128-bit response, decode to our card CSD structure.
  361.  */
  362. static void mmc_decode_csd(struct mmc_card *card)
  363. {
  364.     struct mmc_csd *csd = &card->csd;
  365.     unsigned int e, m, csd_struct;
  366.     u32 *resp = card->raw_csd;
  367.  
  368.     /*
  369.      * We only understand CSD structure v1.0, v1.1 and v2.
  370.      * v2 has extra information in bits 15, 11 and 10.
  371.      */
  372.     csd_struct = UNSTUFF_BITS(resp, 126, 2);
  373.  
  374.     switch (csd_struct) {
  375.     case 0:
  376.         m = UNSTUFF_BITS(resp, 115, 4);
  377.         e = UNSTUFF_BITS(resp, 112, 3);
  378.         csd->tacc_ns     = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  379.         csd->tacc_clks   = UNSTUFF_BITS(resp, 104, 8) * 100;
  380.  
  381.         m = UNSTUFF_BITS(resp, 99, 4);
  382.         e = UNSTUFF_BITS(resp, 96, 3);
  383.         csd->max_dtr      = tran_exp[e] * tran_mant[m];
  384.         csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
  385.  
  386.         e = UNSTUFF_BITS(resp, 47, 3);
  387.         m = UNSTUFF_BITS(resp, 62, 12);
  388.         csd->capacity     = (1 + m) << (e + 2);
  389.  
  390.         csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  391.         csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  392.         csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  393.         csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  394.         csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  395.         csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  396.         csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  397.         break;
  398.     case 1:
  399.         /*
  400.          * This is a block-addressed SDHC card. Most
  401.          * interesting fields are unused and have fixed
  402.          * values. To avoid getting tripped by buggy cards,
  403.          * we assume those fixed values ourselves.
  404.          */
  405.         mmc_card_set_blockaddr(card);
  406.  
  407.         csd->tacc_ns     = 0; /* Unused */
  408.         csd->tacc_clks   = 0; /* Unused */
  409.  
  410.         m = UNSTUFF_BITS(resp, 99, 4);
  411.         e = UNSTUFF_BITS(resp, 96, 3);
  412.         csd->max_dtr      = tran_exp[e] * tran_mant[m];
  413.         csd->cmdclass     = UNSTUFF_BITS(resp, 84, 12);
  414.  
  415.         m = UNSTUFF_BITS(resp, 48, 22);
  416.         csd->capacity     = (1 + m) << 10;
  417.  
  418.         csd->read_blkbits = 9;
  419.         csd->read_partial = 0;
  420.         csd->write_misalign = 0;
  421.         csd->read_misalign = 0;
  422.         csd->r2w_factor = 4; /* Unused */
  423.         csd->write_blkbits = 9;
  424.         csd->write_partial = 0;
  425.         break;
  426.     default:
  427.         printk("%s: unrecognised CSD structure version %d\n",
  428.             mmc_hostname(card->host), csd_struct);
  429.         return;
  430.     }
  431. }
  432.  
  433. #if 0
  434. static void sd_print_cid(struct mmc_cid *cid)
  435. {
  436.     sd_printk(KERN_INFO,
  437.           "manfid = %d\n"
  438.           "oemid = %d\n"
  439.           "prod_name = %s\n"
  440.           "hwrev = %d\n"
  441.           "fwrev = %d\n"
  442.           "serial = %08x\n"
  443.           "year = %d\n"
  444.           "month = %d\n",
  445.           cid->manfid,
  446.           cid->oemid,
  447.           cid->prod_name,
  448.           cid->hwrev, cid->fwrev, cid->serial, cid->year, cid->month);
  449. }
  450. #endif
  451.  
  452. /* */
  453. static inline unsigned int ms_to_cycles(unsigned int ms, unsigned int clock)
  454. {
  455.     return ms * (clock / 1000);
  456. }
  457.  
  458. /* */
  459. static unsigned int sd_set_clock(struct sd_host *host, unsigned int clock)
  460. {
  461.     if (clock >= 32000000) {
  462.         host->clock = 32000000;
  463.         host->exi_clock = EXI_CLK_32MHZ;
  464.     } else if (clock >= 16000000) {
  465.         host->clock = 16000000;
  466.         host->exi_clock = EXI_CLK_16MHZ;
  467.     } else if (clock >= 8000000) {
  468.         host->clock = 8000000;
  469.         host->exi_clock = EXI_CLK_8MHZ;
  470.     } else if (clock >= 4000000) {
  471.         host->clock = 4000000;
  472.         host->exi_clock = EXI_CLK_4MHZ;
  473.     } else if (clock >= 2000000) {
  474.         host->clock = 2000000;
  475.         host->exi_clock = EXI_CLK_2MHZ;
  476.     } else {
  477.         host->clock = 1000000;
  478.         host->exi_clock = EXI_CLK_1MHZ;
  479.     }
  480.     return host->clock;
  481. }
  482.  
  483. /* */
  484. static void sd_calc_timeouts(struct sd_host *host)
  485. {
  486.     /*
  487.      * FIXME: calculate timeouts from card information
  488.      * (use safe defaults for now)
  489.      */
  490.     host->read_timeout = ms_to_cycles(100, host->clock);
  491.     host->write_timeout = ms_to_cycles(250, host->clock);
  492. }
  493.  
  494. /*
  495.  *
  496.  * SPI I/O support routines, including some handy SPI to EXI language
  497.  * translations.
  498.  */
  499.  
  500. /* */
  501. static inline void spi_cs_low(struct sd_host *host)
  502. {
  503.     exi_dev_take(host->exi_device);
  504.     exi_dev_select(host->exi_device);
  505. }
  506.  
  507. /* */
  508. static inline void spi_cs_high(struct sd_host *host)
  509. {
  510.     exi_dev_deselect(host->exi_device);
  511.     exi_dev_give(host->exi_device);
  512. }
  513.  
  514. /* */
  515. static inline void spi_write(struct sd_host *host, void *data, size_t len)
  516. {
  517.     exi_dev_write(host->exi_device, data, len);
  518. }
  519.  
  520. /* */
  521. static inline void spi_read(struct sd_host *host, void *data, size_t len)
  522. {
  523.     /*
  524.      * Houston, we have a problem.
  525.      *
  526.      * The EXI hardware implementation seems to use a shift register which
  527.      * outputs data from the MSB to the MOSI line and inputs data from
  528.      * the MISO line into the LSB.
  529.      * When a read operation is performed, data from the MISO line
  530.      * is entered into the shift register LSB as expected. But also the
  531.      * data already present in the shift register is sent out through the
  532.      * MOSI line from the MSB.
  533.      * This is in fact the "feature" that enabled tmbinc to dump the IPL.
  534.      *
  535.      * When interfacing with SD cards, this causes us a serious problem.
  536.      *
  537.      * We are required to send all ones (1s) while reading data from
  538.      * the SD card. Otherwise, the card can interpret the data sent as
  539.      * commands (if they start with the bit pattern 01 for example).
  540.      *
  541.      * If we use the EXI immediate mode transfer, we can workaround the
  542.      * situation by writing all 1s to the DATA register before reading
  543.      * (this is indeed automatically done by the EXI layer).
  544.      * But we simply can't do that when using EXI DMA transfers (these
  545.      * kind of transfers do not allow bidirectional operation).
  546.      *
  547.      * Given that no EXI DMA read transfers seem reliable, we fallback
  548.      * to the "interrupt-driven" immediate mode of the EXI layer.
  549.      * This will help reducing CPU monopolization on large reads.
  550.      *
  551.      */
  552.     exi_dev_transfer(host->exi_device, data, len, EXI_OP_READ, EXI_CMD_IDI);
  553. }
  554.  
  555. /* cycles are expressed in 8 clock cycles */
  556. static void spi_burn_cycles(struct sd_host *host, int cycles)
  557. {
  558.     u8 d;
  559.  
  560.     while (cycles-- > 0) {
  561.         d = 0xff;
  562.         spi_write(host, &d, sizeof(d));
  563.     }
  564. }
  565.  
  566. /* cycles are expressed in 8 clock cycles */
  567. static int spi_wait_for_resp(struct sd_host *host,
  568.                  u8 resp, u8 resp_mask, unsigned long cycles)
  569. {
  570.     u8 data;
  571.  
  572.     while (cycles-- > 0) {
  573.         spi_read(host, &data, sizeof(data));
  574.         if ((data & resp_mask) == resp) {
  575.             host->resp = data;
  576.             return data;
  577.         }
  578.     }
  579.     return -ENODATA;
  580. }
  581.  
  582. /*
  583.  *
  584.  */
  585. static int sd_read_data(struct sd_host *host, void *data, size_t len, int token)
  586. {
  587.     int retval = 0;
  588.  
  589.     if (token) {
  590.         retval = spi_wait_for_resp(host, token, 0xff,
  591.                        host->read_timeout);
  592.         if (retval < 0)
  593.             goto out;
  594.     }
  595.     spi_read(host, data, len);
  596.     retval = 0;
  597.  
  598. out:
  599.     return retval;
  600. }
  601.  
  602. /*
  603.  *
  604.  */
  605. static int sd_write_data(struct sd_host *host, void *data, size_t len,
  606.               int token)
  607. {
  608.     u16 crc;
  609.     u8 t, *d;
  610.     size_t l;
  611.     int retval = 0;
  612.  
  613.     /* FIXME, rewrite this a bit */
  614.     {
  615.         crc = 0;
  616.         d = data;
  617.         l = len;
  618.  
  619.         while (l-- > 0)
  620.             crc = crc_xmodem_update(crc, *d++);
  621.     }
  622.  
  623.     /* send the write block token */
  624.     t = token;
  625.     spi_write(host, &t, sizeof(t));
  626.  
  627.     /* send the data */
  628.     spi_write(host, data, len);
  629.  
  630.     /* send the crc */
  631.     spi_write(host, &crc, sizeof(crc));
  632.  
  633.     /* get the card data response */
  634.     retval = spi_wait_for_resp(host, 0x01, 0x11, host->write_timeout);
  635.     if (retval < 0)
  636.         goto out;
  637.     if ((retval & DR_SPI_MASK) != DR_SPI_DATA_ACCEPTED) {
  638.         DBG("data response=%02x\n", retval);
  639.         retval = -EIO;
  640.         goto out;
  641.     }
  642.  
  643.     /* wait for the busy signal to clear */
  644.     retval = spi_wait_for_resp(host, 0xff, 0xff, host->write_timeout);
  645.     if (retval < 0)
  646.         goto out;
  647.  
  648.     retval = 0;
  649.  
  650. out:
  651.     return retval;
  652. }
  653.  
  654. /*
  655.  *
  656.  * MMC/SD command transactions related routines.
  657.  */
  658.  
  659. /* */
  660. static inline void sd_cmd(struct sd_command *cmd, u8 opcode, u32 arg)
  661. {
  662.     cmd->cmd = 0x40 | opcode;
  663.     cmd->arg = arg;
  664.     cmd->crc = 0x01;    /* FIXME, crc is not currently used */
  665. }
  666.  
  667. static inline void sd_cmd_crc(struct sd_command *cmd, u8 opcode, u32 arg, u8 crc)
  668. {
  669.     cmd->cmd = 0x40 + opcode;
  670.     cmd->arg = arg;
  671.     cmd->crc = crc;
  672. }
  673.  
  674. /* */
  675. static inline void sd_cmd_go_idle_state(struct sd_command *cmd)
  676. {
  677.     cmd->cmd = 0x40;
  678.     cmd->arg = 0;
  679.     cmd->crc = 0x95;
  680. }
  681.  
  682. /* */
  683. static inline void sd_debug_print_cmd(struct sd_command *cmd)
  684. {
  685.     DBG("cmd = %d, arg = %08x, crc = %02x\n",
  686.         cmd->cmd & ~0x40, cmd->arg, cmd->crc);
  687. }
  688.  
  689. /*
  690.  *
  691.  */
  692. static int sd_start_command(struct sd_host *host, struct sd_command *cmd)
  693. {
  694.     int retval = 0;
  695.  
  696.     /* select the card by driving CS low */
  697.     spi_cs_low(host);
  698.  
  699.     /* send the command through the MOSI line */
  700.     spi_write(host, cmd, sizeof(*cmd));
  701.  
  702.     /*
  703.      * Wait for the card response.
  704.      * Card responses come in the MISO line and have the most significant
  705.      * bit cleared.
  706.      */
  707.     retval = spi_wait_for_resp(host, 0x00, 0x80, MMC_SPI_N_CR);
  708.  
  709.     if (retval > 0 && !(retval & 0x01) && cmd->cmd != 0x40)
  710.         DBG("command = %d, response = 0x%02x\n", cmd->cmd & ~0x40,
  711.             retval);
  712.  
  713.     return retval;
  714. }
  715.  
  716. /*
  717.  *
  718.  */
  719. static void sd_end_command(struct sd_host *host)
  720. {
  721.     /* wait 8 clock cycles as dictated by the specification */
  722.     spi_burn_cycles(host, SD_FINISH_CYCLES);
  723.  
  724.     /* deselect the card by driving CS high */
  725.     spi_cs_high(host);
  726. }
  727.  
  728. /*
  729.  *
  730.  */
  731. static int sd_run_no_data_command(struct sd_host *host, struct sd_command *cmd)
  732. {
  733.     int retval;
  734.  
  735.     /* send command, wait for response, and burn extra cycles */
  736.     retval = sd_start_command(host, cmd);
  737.     sd_end_command(host);
  738.  
  739.     return retval;
  740. }
  741.  
  742. /*
  743.  *
  744.  */
  745. static int sd_generic_read(struct sd_host *host,
  746.                u8 opcode, u32 arg,
  747.                void *data, size_t len, int token)
  748. {
  749.     struct sd_command *cmd = &host->cmd;
  750.     u16 crc, calc_crc = 0xffff;
  751.     u8 *d;
  752.     size_t l;
  753.     int retval;
  754.  
  755.     /* build raw command */
  756.     sd_cmd(cmd, opcode, arg);
  757.  
  758.     /* select card, send command and wait for response */
  759.     retval = sd_start_command(host, cmd);
  760.     if (retval < 0)
  761.         goto out;
  762.     if (retval != 0x00) {
  763.         retval = -EIO;
  764.         goto out;
  765.     }
  766.  
  767.     /* wait for read token, then read data */
  768.     retval = sd_read_data(host, data, len, token);
  769.     if (retval < 0)
  770.         goto out;  
  771.  
  772.     /* read trailing crc */
  773.     spi_read(host, &crc, sizeof(crc));
  774.  
  775.     retval = 0;
  776.     /* FIXME, rewrite this a bit */
  777.     {
  778.         calc_crc = 0;
  779.         d = data;
  780.         l = len;
  781.  
  782.         while (l-- > 0)
  783.             calc_crc = crc_xmodem_update(calc_crc, *d++);
  784.  
  785.         if (calc_crc != crc)
  786.             retval = -EIO;
  787.     }
  788.  
  789. out:
  790.     /* burn extra cycles and deselect card */
  791.     sd_end_command(host);
  792.  
  793.     if (retval < 0) {
  794.         DBG("read, offset=%u, len=%d\n", arg, len);
  795.         DBG("crc=%04x, calc_crc=%04x, %s\n", crc, calc_crc,
  796.             (retval < 0) ? "failed" : "ok");
  797.     }
  798.  
  799.     return retval;
  800. }
  801.  
  802. /*
  803.  *
  804.  */
  805. static int sd_generic_write(struct sd_host *host,
  806.                 u8 opcode, u32 arg,
  807.                 void *data, size_t len, int token)
  808. {
  809.     struct sd_command *cmd = &host->cmd;
  810.     int retval;
  811.  
  812.     /* build raw command */
  813.     sd_cmd(cmd, opcode, arg);
  814.  
  815.     /* select card, send command and wait for response */
  816.     retval = sd_start_command(host, cmd);
  817.     if (retval < 0)
  818.         goto out;
  819.     if (retval != 0x00) {
  820.         retval = -EIO;
  821.         goto out;
  822.     }
  823.  
  824.     /* send data token, data and crc, get data response */
  825.     retval = sd_write_data(host, data, len, token);
  826.     if (retval < 0)
  827.         goto out;
  828.  
  829.     retval = 0;
  830.  
  831. out:
  832.     /* burn extra cycles and deselect card */
  833.     sd_end_command(host);
  834.  
  835.     if (retval < 0)
  836.         DBG("write, offset=%d, len=%d\n", arg, len);
  837.  
  838.     return retval;
  839. }
  840.  
  841. /*
  842.  *
  843.  */
  844. static int sd_read_ocr(struct sd_host *host)
  845. {
  846.     struct sd_command *cmd = &host->cmd;
  847.     int retval;
  848.  
  849.     memset(&host->ocr, 0, sizeof(host->ocr));
  850.  
  851.     sd_cmd(cmd, MMC_READ_OCR, 0);
  852.  
  853.     /* select card, send command and wait for response */
  854.     retval = sd_start_command(host, cmd);
  855.     if (retval < 0)
  856.         goto out;
  857.  
  858.     /* the OCR contents come immediately after the card response */
  859.     spi_read(host, &host->ocr, sizeof(host->ocr));
  860.     retval = 0;
  861.  
  862. out:
  863.     /* burn extra cycles and deselect card */
  864.     sd_end_command(host);
  865.     return retval;
  866. }
  867.  
  868. /*
  869.  *
  870.  */
  871. static inline int sd_read_csd(struct sd_host *host)
  872. {
  873.     memset(&host->card.raw_csd, 0, sizeof(host->card.raw_csd));
  874.     return sd_generic_read(host, MMC_SEND_CSD, 0,
  875.                    &host->card.raw_csd,
  876.                    sizeof(host->card.raw_csd),
  877.                    MMC_SPI_TOKEN_START_SINGLE_BLOCK_READ);
  878. }
  879.  
  880. /*
  881.  *
  882.  */
  883. static inline int sd_read_cid(struct sd_host *host)
  884. {
  885.     memset(&host->card.raw_cid, 0, sizeof(host->card.raw_cid));
  886.     return sd_generic_read(host, MMC_SEND_CID, 0,
  887.                    &host->card.raw_cid,
  888.                    sizeof(host->card.raw_cid),
  889.                    MMC_SPI_TOKEN_START_SINGLE_BLOCK_READ);
  890. }
  891.  
  892. /*
  893.  *
  894.  */
  895. static inline int sd_read_single_block(struct sd_host *host,
  896.                        unsigned long start,
  897.                        void *data, size_t len)
  898. {
  899.     int retval;
  900.     int attempts = 3;
  901.  
  902.     if (test_bit(__SD_MEDIA_CHANGED, &host->flags))
  903.         attempts = 1;
  904.  
  905.     while (attempts > 0) {
  906.         retval = sd_generic_read(host, MMC_READ_SINGLE_BLOCK, start,
  907.                      data, len,
  908.                      MMC_SPI_TOKEN_START_SINGLE_BLOCK_READ);
  909.         if (retval >= 0)
  910.             break;
  911.         attempts--;
  912.         DBG("start=%lu, data=%p, len=%d, retval = %d\n", start, data,
  913.             len, retval);
  914.     }
  915.     return retval;
  916. }
  917.  
  918. /*
  919.  *
  920.  */
  921. static inline int sd_write_single_block(struct sd_host *host,
  922.                      unsigned long start,
  923.                      void *data, size_t len)
  924. {
  925.     int retval;
  926.  
  927.     retval = sd_generic_write(host, MMC_WRITE_BLOCK, start,
  928.                   data, len,
  929.                   MMC_SPI_TOKEN_START_SINGLE_BLOCK_WRITE);
  930.     if (retval < 0)
  931.         DBG("start=%lu, data=%p, len=%d, retval = %d\n", start, data,
  932.             len, retval);
  933.  
  934.     return retval;
  935. }
  936.  
  937. /*
  938.  *
  939.  */
  940. static int sd_reset_sequence(struct sd_host *host)
  941. {
  942.     struct sd_command *cmd = &host->cmd;
  943.     u8 d;
  944.     int i;
  945.     int retval = 0;
  946.     u32 cmd_ret = 0;
  947.  
  948.     host->card.state = 0;
  949.    
  950.  
  951.  
  952.     /*
  953.      * Wait at least 80 dummy clock cycles with the card deselected
  954.      * and with the MOSI line continuously high.
  955.      */
  956.     exi_dev_take(host->exi_device);
  957.     exi_dev_deselect(host->exi_device);
  958.     for (i = 0; i < SD_IDLE_CYCLES; i++) {
  959.         d = 0xff;
  960.         exi_dev_write(host->exi_device, &d, sizeof(d));
  961.     }
  962.     exi_dev_give(host->exi_device);
  963.  
  964.     /*
  965.      * Send a CMD0, card must ack with "idle state" (0x01).
  966.      * This puts the card into SPI mode and soft resets it.
  967.      * CRC checking is disabled by default.
  968.      */
  969.     for (i = 0; i < 255; i++) {
  970.         /* CMD0 */
  971.         sd_cmd_go_idle_state(cmd);
  972.         retval = sd_run_no_data_command(host, cmd);
  973.         if (retval < 0) {
  974.             retval = -ENODEV;
  975.             goto out;
  976.         }
  977.         if (retval == R1_SPI_IDLE) {
  978.             break;
  979.         }
  980.     }
  981.     if (retval != R1_SPI_IDLE) {
  982.         retval = -ENODEV;
  983.         goto out;
  984.     }
  985.  
  986.     /*
  987.     * Send CMD8 and CMD58 for SDHC support
  988.     */
  989.     sd_cmd_crc(cmd, SD_SEND_IF_COND, 0x01AA, 0x87);
  990.     for (i = 0; i < 8; i++) {
  991.         retval = sd_start_command(host, cmd); //CMD8
  992.         if(retval==0x01) {  
  993.             memset(&cmd_ret, 0, sizeof(cmd_ret));
  994.             spi_read(host, &cmd_ret, sizeof(cmd_ret));
  995.             sd_end_command(host);  
  996.             if (cmd_ret == 0x01AA) {
  997.                 sd_cmd(cmd, MMC_SPI_READ_OCR, 0);
  998.                 retval = sd_start_command(host, cmd);   //CMD58
  999.  
  1000.                 memset(&cmd_ret, 0, sizeof(cmd_ret));
  1001.                 spi_read(host, &cmd_ret, sizeof(cmd_ret));
  1002.                 sd_end_command(host);      
  1003.  
  1004.                 if(retval!=0x01) {
  1005.                     /*Problem with the Card */
  1006.                 }
  1007.             }              
  1008.             break;
  1009.         }      
  1010.         else if(retval==0x05) {
  1011.             /*DBG("CMD8: [ NOSDHC ]\n");*/
  1012.             break;
  1013.         }
  1014.     }  
  1015.    
  1016.     /*
  1017.      * Send a ACMD41 to activate card initialization process.
  1018.      * SD card must ack with "ok" (0x00).
  1019.      * MMC card will report "invalid command" (0x04).
  1020.      */
  1021.     for (i = 0; i < 65535; i++) {
  1022.         /* ACMD41 = CMD55 + CMD41 */
  1023.         sd_cmd(cmd, MMC_APP_CMD, 0);
  1024.         retval = sd_run_no_data_command(host, cmd);
  1025.         if (retval < 0) {
  1026.             retval = -ENODEV;
  1027.             goto out;
  1028.         }
  1029.  
  1030.         sd_cmd(cmd, SD_APP_OP_COND, (1<<30)|0x100000);
  1031.         retval = sd_run_no_data_command(host, cmd);
  1032.         if (retval < 0) {
  1033.             retval = -ENODEV;
  1034.             goto out;
  1035.         }
  1036.  
  1037.         if (retval == 0x00) {
  1038.             /* we found a SD card */
  1039.             mmc_card_set_present(&host->card);
  1040.             host->card.type = MMC_TYPE_SD;
  1041.             break;
  1042.         } else if(retval != 0x01) {
  1043.             DBG("ACMD41 return: %d\n", retval);
  1044.         }
  1045.  
  1046.         if ((retval & R1_SPI_ILLEGAL_COMMAND)) {
  1047.             /* this looks like a MMC card */
  1048.             break;
  1049.         }
  1050.     }
  1051.     /*
  1052.      * MMC cards require CMD1 to activate card initialization process.
  1053.      * MMC card must ack with "ok" (0x00)
  1054.      */
  1055.     if (!mmc_card_sd(&host->card)) {
  1056.         for (i = 0; i < 65535; i++) {
  1057.             sd_cmd(cmd, MMC_SEND_OP_COND, 0);
  1058.             retval = sd_run_no_data_command(host, cmd);
  1059.             if (retval < 0) {
  1060.                 retval = -ENODEV;
  1061.                 goto out;
  1062.             }
  1063.             if (retval == 0x00) {
  1064.                 /* we found a MMC card */
  1065.                 mmc_card_set_present(&host->card);
  1066.                 break;
  1067.             }
  1068.         }
  1069.         if (retval != 0x00) {
  1070.             DBG("MMC card, bad, retval=%02x\n", retval);
  1071.             sd_card_set_bad(host);
  1072.         }
  1073.     }
  1074.  
  1075. out:
  1076.     return retval;
  1077. }
  1078.  
  1079. /*
  1080.  *
  1081.  */
  1082. static int sd_welcome_card(struct sd_host *host)
  1083. {
  1084.     int retval;
  1085.  
  1086.     /* soft reset the card */
  1087.     retval = sd_reset_sequence(host);
  1088.     if (retval < 0 || sd_card_is_bad(host))
  1089.         goto out;
  1090.  
  1091.     /* read Operating Conditions Register */
  1092.     retval = sd_read_ocr(host);
  1093.     if (retval < 0)
  1094.         goto err_bad_card;
  1095.  
  1096.     /* refuse to drive cards reporting voltage ranges out of scope */
  1097.     if (!(host->ocr & host->ocr_avail)) {
  1098.         sd_printk(KERN_WARNING, "reported OCR (%08x)"
  1099.               " indicates that it is not safe to use this"
  1100.               " card with a GameCube\n", host->ocr);
  1101.         retval = -ENODEV;
  1102.         goto err_bad_card;
  1103.     }
  1104.  
  1105.     /* read and decode the Card Specific Data */
  1106.     retval = sd_read_csd(host);
  1107.     if (retval < 0)
  1108.         goto err_bad_card;
  1109.     mmc_decode_csd(&host->card);
  1110.  
  1111.     /* calculate some card access related timeouts */
  1112.     sd_calc_timeouts(host);
  1113.  
  1114.     /* read and decode the Card Identification Data */
  1115.     retval = sd_read_cid(host);
  1116.     if (retval < 0)
  1117.         goto err_bad_card;
  1118.     mmc_decode_cid(&host->card);
  1119.  
  1120.     sd_printk(KERN_INFO, "slot%d: descr \"%s\", size %lub, block %ub,"
  1121.           " serial %08x\n",
  1122.           to_channel(exi_get_exi_channel(host->exi_device)),
  1123.           host->card.cid.prod_name,
  1124.           (unsigned long)((host->card.csd.capacity)),
  1125.           1 << host->card.csd.read_blkbits,
  1126.           host->card.cid.serial);
  1127.     ssleep(1); //Debug: Just to see if everything is alright!
  1128.     retval = 0;
  1129.     goto out;
  1130.  
  1131. err_bad_card:
  1132.     sd_card_set_bad(host);
  1133. out:
  1134.     return retval;
  1135. }
  1136.  
  1137. /*
  1138.  * Block layer.
  1139.  */
  1140.  
  1141. /*
  1142.  * Performs a read request.
  1143.  */
  1144. static int sd_read_request(struct sd_host *host, struct request *req)
  1145. {
  1146.     int i;
  1147.     unsigned long nr_blocks; /* in card blocks */
  1148.     size_t block_len; /* in bytes */
  1149.     unsigned long start;
  1150.     void *buf = req->buffer;
  1151.     int retval;
  1152.  
  1153.     /*
  1154.      * It seems that some cards do not accept single block reads for the
  1155.      * read block length reported by the card.
  1156.      * For now, we perform only 512 byte single block reads.
  1157.      */
  1158.  
  1159.     start = blk_rq_pos(req) /*<< KERNEL_SECTOR_SHIFT*/;
  1160.    
  1161. //  nr_blocks = req->current_nr_sectors >> (host->card.csd.read_blkbits - KERNEL_SECTOR_SHIFT);
  1162. //  block_len = 1 << host->card.csd.read_blkbits;
  1163.  
  1164.     nr_blocks = blk_rq_cur_sectors(req);
  1165.     block_len = 1 << KERNEL_SECTOR_SHIFT;
  1166.  
  1167.  
  1168.     for (i = 0; i < nr_blocks; i++) {
  1169.         retval = sd_read_single_block(host, start, buf, block_len);
  1170.         if (retval < 0)
  1171.             break;
  1172.  
  1173.         start += block_len;
  1174.         buf += block_len;
  1175.     }
  1176.  
  1177.     /* number of kernel sectors transferred */
  1178. #if 0
  1179.     retval = i << (host->card.csd.read_blkbits - KERNEL_SECTOR_SHIFT);
  1180. #else
  1181.     retval = i;
  1182. #endif
  1183.  
  1184.     return retval;
  1185. }
  1186.  
  1187. /*
  1188.  * Performs a write request.
  1189.  */
  1190. static int sd_write_request(struct sd_host *host, struct request *req)
  1191. {
  1192.     int i;
  1193.     unsigned long nr_blocks; /* in card blocks */
  1194.     size_t block_len; /* in bytes */
  1195.     unsigned long start;
  1196.     void *buf = req->buffer;
  1197.     int retval;
  1198.  
  1199.     /* FIXME?, maybe should use 2^WRITE_BL_LEN blocks */
  1200.  
  1201.     /* kernel sectors and card write blocks are both 512 bytes long */
  1202.     start = blk_rq_pos(req) /*<< KERNEL_SECTOR_SHIFT*/;
  1203.     nr_blocks = blk_rq_cur_sectors(req);
  1204.     block_len = 1 << KERNEL_SECTOR_SHIFT;
  1205.  
  1206.     for (i = 0; i < nr_blocks; i++) {
  1207.         retval = sd_write_single_block(host, start, buf, block_len);
  1208.         if (retval < 0)
  1209.             break;
  1210.  
  1211.         start += block_len;
  1212.         buf += block_len;
  1213.     }
  1214.  
  1215.     /* number of kernel sectors transferred */
  1216.     retval = i;
  1217.  
  1218.     return retval;
  1219. }
  1220.  
  1221. /*
  1222.  * Verifies if a request should be dispatched or not.
  1223.  *
  1224.  * Returns:
  1225.  *  <0 in case of error.
  1226.  *  0  if request passes the checks
  1227.  */
  1228. static int sd_check_request(struct sd_host *host, struct request *req)
  1229. {
  1230.     unsigned long nr_sectors;
  1231.  
  1232.     if (!blk_fs_request(req))
  1233.         return -EIO;
  1234.  
  1235.     if (test_bit(__SD_MEDIA_CHANGED, &host->flags)) {
  1236.         sd_printk(KERN_ERR, "media changed, aborting\n");
  1237.         return -ENOMEDIUM;
  1238.     }
  1239.  
  1240.     /* unit is kernel sectors */
  1241.     nr_sectors =
  1242.         host->card.csd.capacity << (host->card.csd.read_blkbits - KERNEL_SECTOR_SHIFT);
  1243.  
  1244.     /* keep our reads within limits */
  1245.  
  1246.     if ((blk_rq_pos(req) + blk_rq_cur_sectors(req)) > nr_sectors) {
  1247.         sd_printk(KERN_ERR, "reading past end, aborting\n");
  1248.         return -EINVAL;
  1249.     }
  1250.  
  1251.     return 0;
  1252. }
  1253.  
  1254. /*
  1255.  * Request dispatcher.
  1256.  */
  1257. static int sd_do_request(struct sd_host *host, struct request *req)
  1258. {
  1259.     int nr_sectors = 0;
  1260.     int error;
  1261.  
  1262.     error = sd_check_request(host, req);
  1263.     if (error) {
  1264.         nr_sectors = error;
  1265.         goto out;
  1266.     }
  1267.  
  1268.     switch (rq_data_dir(req)) {
  1269.     case WRITE:
  1270.         nr_sectors = sd_write_request(host, req);
  1271.         break;
  1272.     case READ:
  1273.         nr_sectors = sd_read_request(host, req);
  1274.         break;
  1275.     }
  1276.  
  1277. out:
  1278.     return nr_sectors;
  1279. }
  1280.  
  1281. /*
  1282.  * Input/Output thread.
  1283.  */
  1284. static int sd_io_thread(void *param)
  1285. {
  1286.     struct sd_host *host = param;
  1287.     struct request *req;
  1288.     unsigned long flags;
  1289.     int nr_sectors;
  1290.     int error;
  1291.  
  1292. #if 0
  1293.     /*
  1294.      * We are going to perfom badly due to the read problem explained
  1295.      * above. At least, be nice with other processes trying to use the
  1296.      * cpu.
  1297.      */
  1298.     set_user_nice(current, 0);
  1299. #endif
  1300.  
  1301.     current->flags |= PF_NOFREEZE|PF_MEMALLOC;
  1302.  
  1303.     mutex_lock(&host->io_mutex);
  1304.     for (;;) {
  1305.         req = NULL;
  1306.         set_current_state(TASK_INTERRUPTIBLE);
  1307.  
  1308.         spin_lock_irqsave(&host->queue_lock, flags);
  1309.         if (!blk_queue_plugged(host->queue))
  1310.             req = blk_fetch_request(host->queue);
  1311.         spin_unlock_irqrestore(&host->queue_lock, flags);
  1312.  
  1313.         if (!req) {
  1314.             if (kthread_should_stop()) {
  1315.                 set_current_state(TASK_RUNNING);
  1316.                 break;
  1317.             }
  1318.             mutex_unlock(&host->io_mutex);
  1319.             schedule();
  1320.             mutex_lock(&host->io_mutex);
  1321.             continue;
  1322.         }
  1323.         set_current_state(TASK_INTERRUPTIBLE);
  1324.         nr_sectors = sd_do_request(host, req);
  1325.         error = (nr_sectors < 0) ? nr_sectors : 0;
  1326.  
  1327.         spin_lock_irqsave(&host->queue_lock, flags);
  1328.         __blk_end_request(req, error, nr_sectors << 9);
  1329.         spin_unlock_irqrestore(&host->queue_lock, flags);
  1330.     }
  1331.     mutex_unlock(&host->io_mutex);
  1332.  
  1333.     return 0;
  1334. }
  1335.  
  1336. /*
  1337.  * Block layer request function.
  1338.  * Wakes up the IO thread.
  1339.  */
  1340. static void sd_request_func(struct request_queue *q)
  1341. {
  1342.     struct sd_host *host = q->queuedata;
  1343.     wake_up_process(host->io_thread);
  1344. }
  1345.  
  1346. /*
  1347.  *
  1348.  * Driver interface.
  1349.  */
  1350.  
  1351. static DECLARE_MUTEX(open_lock);
  1352.  
  1353. /*
  1354.  * Opens the drive device.
  1355.  */
  1356. static int sd_open(struct block_device *bdev, fmode_t mode)
  1357. {
  1358.     struct sd_host *host = bdev->bd_disk->private_data;
  1359.     int retval = 0;
  1360.  
  1361.     if (!host || !host->exi_device)
  1362.         return -ENXIO;
  1363.  
  1364.     /* honor exclusive open mode */
  1365.     if (host->refcnt == -1 ||
  1366.         (host->refcnt && (mode & FMODE_EXCL))) {
  1367.         retval = -EBUSY;
  1368.         goto out;
  1369.     }
  1370.  
  1371.     /* this takes care of revalidating the media if needed */
  1372.     check_disk_change(bdev);
  1373.     if (!host->card.csd.capacity) {
  1374.         retval = -ENOMEDIUM;
  1375.         goto out;
  1376.     }
  1377.  
  1378.     down(&open_lock);
  1379.  
  1380.     if ((mode & FMODE_EXCL))
  1381.         host->refcnt = -1;
  1382.     else
  1383.         host->refcnt++;
  1384.  
  1385.     up(&open_lock);
  1386.  
  1387. out:
  1388.     return retval;
  1389.  
  1390. }
  1391.  
  1392. /*
  1393.  * Releases the drive device.
  1394.  */
  1395. static int sd_release(struct gendisk *disk, fmode_t mode)
  1396. {
  1397.     struct sd_host *host = disk->private_data;
  1398.  
  1399.     if (!host)
  1400.         return -ENXIO;
  1401.  
  1402.     down(&open_lock);
  1403.  
  1404.     if (host->refcnt > 0)
  1405.         host->refcnt--;
  1406.     else
  1407.         host->refcnt = 0;
  1408.  
  1409.     up(&open_lock);
  1410.  
  1411.     /* lazy removal of unreferenced zombies */
  1412.     if (!host->refcnt && !host->exi_device)
  1413.         kfree(host);
  1414.  
  1415.     return 0;
  1416. }
  1417.  
  1418. /*
  1419.  * Checks if media changed.
  1420.  */
  1421. static int sd_media_changed(struct gendisk *disk)
  1422. {
  1423.     struct sd_host *host = disk->private_data;
  1424.     unsigned int last_serial;
  1425.     int retval;
  1426.  
  1427.     /* report a media change for zombies */
  1428.     if (!host)
  1429.         return 1;
  1430.  
  1431.     /* report a media change if someone forced it */
  1432.     if (test_bit(__SD_MEDIA_CHANGED, &host->flags))
  1433.         return 1;
  1434.  
  1435.     /* check if the serial number of the card changed */
  1436.     last_serial = host->card.cid.serial;
  1437.     retval = sd_read_cid(host);
  1438.     if (!retval && last_serial == host->card.cid.serial && last_serial)
  1439.         clear_bit(__SD_MEDIA_CHANGED, &host->flags);
  1440.     else
  1441.         set_bit(__SD_MEDIA_CHANGED, &host->flags);
  1442.  
  1443.     return (host->flags & SD_MEDIA_CHANGED) ? 1 : 0;
  1444. }
  1445.  
  1446. /*
  1447.  * Checks if media is still valid.
  1448.  */
  1449. static int sd_revalidate_disk(struct gendisk *disk)
  1450. {
  1451.     struct sd_host *host = disk->private_data;
  1452.     int retval = 0;
  1453.  
  1454.     /* report missing medium for zombies */
  1455.     if (!host) {
  1456.         retval = -ENOMEDIUM;
  1457.         goto out;
  1458.     }
  1459.  
  1460.     /* the block layer likes to call us multiple times... */
  1461.     if (!sd_media_changed(host->disk))
  1462.         goto out;
  1463.  
  1464.     /* get the card into a known status */
  1465.     retval = sd_welcome_card(host);
  1466.     if (retval < 0 || sd_card_is_bad(host)) {
  1467.         retval = -ENOMEDIUM;
  1468.         goto out;
  1469.     }
  1470.  
  1471.     /* inform the block layer about various sizes */
  1472.     blk_queue_logical_block_size(host->queue, 1 << KERNEL_SECTOR_SHIFT);
  1473.     set_capacity(host->disk, host->card.csd.capacity /*<< (host->card.csd.read_blkbits - KERNEL_SECTOR_SHIFT)*/);
  1474.  
  1475.     clear_bit(__SD_MEDIA_CHANGED, &host->flags);
  1476.  
  1477. out:
  1478.     return retval;
  1479. }
  1480.  
  1481. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1482. {
  1483.     geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  1484.     geo->heads = 4;
  1485.     geo->sectors = 16;
  1486.     return 0;
  1487. }
  1488.  
  1489.  
  1490. static struct block_device_operations sd_fops = {
  1491.     .owner = THIS_MODULE,
  1492.     .open = sd_open,
  1493.     .release = sd_release,
  1494.     .revalidate_disk = sd_revalidate_disk,
  1495.     .media_changed = sd_media_changed,
  1496.     .getgeo = sd_getgeo,
  1497. };
  1498.  
  1499. /*
  1500.  * Initializes the block layer interfaces.
  1501.  */
  1502. static int sd_init_blk_dev(struct sd_host *host)
  1503. {
  1504.     struct gendisk *disk;
  1505.     struct request_queue *queue;
  1506.     int channel;
  1507.     int retval;
  1508.  
  1509.     channel = to_channel(exi_get_exi_channel(host->exi_device));
  1510.  
  1511.     /* queue */
  1512.     retval = -ENOMEM;
  1513.     spin_lock_init(&host->queue_lock);
  1514.     queue = blk_init_queue(sd_request_func, &host->queue_lock);
  1515.     if (!queue) {
  1516.         sd_printk(KERN_ERR, "error initializing queue\n");
  1517.         goto err_blk_init_queue;
  1518.     }
  1519.     blk_queue_dma_alignment(queue, EXI_DMA_ALIGN);
  1520.     blk_queue_max_phys_segments(queue, 1);
  1521.     blk_queue_max_hw_segments(queue, 1);
  1522.     blk_queue_max_sectors(queue, 8);
  1523.     queue_flag_set_unlocked(QUEUE_FLAG_NONROT, queue);
  1524.     queue->queuedata = host;
  1525.     host->queue = queue;
  1526.  
  1527.     /* disk */
  1528.     disk = alloc_disk(1 << MMC_SHIFT);
  1529.     if (!disk) {
  1530.         sd_printk(KERN_ERR, "error allocating disk\n");
  1531.         goto err_alloc_disk;
  1532.     }
  1533.     disk->major = SD_MAJOR;
  1534.     disk->first_minor = channel << MMC_SHIFT;
  1535.     disk->fops = &sd_fops;
  1536.     sprintf(disk->disk_name, "%s%c", SD_NAME, 'a' + channel);
  1537.     disk->private_data = host;
  1538.     disk->queue = host->queue;
  1539.     host->disk = disk;
  1540.  
  1541.     retval = 0;
  1542.     goto out;
  1543.  
  1544. err_alloc_disk:
  1545.     blk_cleanup_queue(host->queue);
  1546.     host->queue = NULL;
  1547. err_blk_init_queue:
  1548. out:
  1549.     return retval;
  1550. }
  1551.  
  1552. /*
  1553.  * Exits the block layer interfaces.
  1554.  */
  1555. static void sd_exit_blk_dev(struct sd_host *host)
  1556. {
  1557.     blk_cleanup_queue(host->queue);
  1558.     put_disk(host->disk);
  1559. }
  1560.  
  1561.  
  1562. /*
  1563.  * Initializes and launches the IO thread.
  1564.  */
  1565. static int sd_init_io_thread(struct sd_host *host)
  1566. {
  1567.     int channel;
  1568.     int result = 0;
  1569.  
  1570.     channel = to_channel(exi_get_exi_channel(host->exi_device));
  1571.  
  1572.     mutex_init(&host->io_mutex);
  1573.     host->io_thread = kthread_run(sd_io_thread, host,
  1574.                       "ksdiod/%c", 'a' + channel);
  1575.     if (IS_ERR(host->io_thread)) {
  1576.         sd_printk(KERN_ERR, "error creating io thread\n");
  1577.         result = PTR_ERR(host->io_thread);
  1578.     }
  1579.     return result;
  1580. }
  1581.  
  1582. /*
  1583.  * Terminates and waits for the IO thread to complete.
  1584.  */
  1585. static void sd_exit_io_thread(struct sd_host *host)
  1586. {
  1587.     if (!IS_ERR(host->io_thread)) {
  1588.         wake_up_process(host->io_thread);
  1589.         kthread_stop(host->io_thread);
  1590.         host->io_thread = ERR_PTR(-EINVAL);
  1591.     }
  1592. }
  1593.  
  1594. /*
  1595.  * Initializes a host.
  1596.  */
  1597. static int sd_init(struct sd_host *host)
  1598. {
  1599.     int retval;
  1600.  
  1601.     spin_lock_init(&host->lock);
  1602.  
  1603.     host->refcnt = 0;
  1604.     set_bit(__SD_MEDIA_CHANGED, &host->flags);
  1605.  
  1606.     host->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1607.     sd_set_clock(host, SD_SPI_CLK);
  1608.     sd_calc_timeouts(host);
  1609.  
  1610.     retval = sd_init_blk_dev(host);
  1611.     if (!retval) {
  1612.         retval = sd_revalidate_disk(host->disk);
  1613.         if (retval < 0 || !mmc_card_present(&host->card)) {
  1614.             retval = -ENODEV;
  1615.             goto err_blk_dev;
  1616.         }
  1617.  
  1618.         retval = sd_init_io_thread(host);
  1619.         if (retval)
  1620.             goto err_blk_dev;
  1621.  
  1622.         add_disk(host->disk);
  1623.     }
  1624.  
  1625.     return retval;
  1626.  
  1627. err_blk_dev:
  1628.     sd_exit_blk_dev(host);
  1629.     return retval;
  1630. }
  1631.  
  1632. /*
  1633.  * Deinitializes (exits) a host.
  1634.  */
  1635. static void sd_exit(struct sd_host *host)
  1636. {
  1637.     del_gendisk(host->disk);
  1638.     sd_exit_io_thread(host);
  1639.     sd_exit_blk_dev(host);
  1640. }
  1641.  
  1642. /*
  1643.  * Terminates a host.
  1644.  */
  1645. static void sd_kill(struct sd_host *host)
  1646. {
  1647.     if (host->refcnt > 0) {
  1648.         sd_printk(KERN_ERR, "hey! card removed while in use!\n");
  1649.         set_bit(__SD_MEDIA_CHANGED, &host->flags);
  1650.     }
  1651.  
  1652.     sd_exit(host);
  1653.     host->exi_device = NULL;
  1654.  
  1655.     /* release the host immediately when not in use */
  1656.     if (!host->refcnt)
  1657.         kfree(host);
  1658. }
  1659.  
  1660.  
  1661. /*
  1662.  * EXI layer interface.
  1663.  *
  1664.  */
  1665.  
  1666. /*
  1667.  * Checks if the given EXI device is a MMC/SD card and makes it available
  1668.  * if true.
  1669.  */
  1670. static int sd_probe(struct exi_device *exi_device)
  1671. {
  1672.     struct sd_host *host;
  1673.     int retval;
  1674.  
  1675.     /* don't try to drive a device which already has a real identifier */
  1676.     if (exi_device->eid.id != EXI_ID_NONE)
  1677.         return -ENODEV;
  1678.  
  1679.     host = kzalloc(sizeof(*host), GFP_KERNEL);
  1680.     if (!host)
  1681.         return -ENOMEM;
  1682.  
  1683.     host->exi_device = exi_device_get(exi_device);
  1684.     WARN_ON(exi_get_drvdata(exi_device));
  1685.     exi_set_drvdata(exi_device, host);
  1686.     retval = sd_init(host);
  1687.     if (retval) {
  1688.         exi_set_drvdata(exi_device, NULL);
  1689.         host->exi_device = NULL;
  1690.         kfree(host);
  1691.         exi_device_put(exi_device);
  1692.     }
  1693.     return retval;
  1694. }
  1695.  
  1696. /*
  1697.  * Makes unavailable the MMC/SD card identified by the EXI device `exi_device'.
  1698.  */
  1699. static void sd_remove(struct exi_device *exi_device)
  1700. {
  1701.     struct sd_host *host = exi_get_drvdata(exi_device);
  1702.  
  1703.     WARN_ON(!host);
  1704.     WARN_ON(!host->exi_device);
  1705.  
  1706.     exi_set_drvdata(exi_device, NULL);
  1707.     if (host)
  1708.         sd_kill(host);
  1709.     exi_device_put(exi_device);
  1710. }
  1711.  
  1712. static struct exi_device_id sd_eid_table[] = {
  1713.     [0] = {
  1714.            .channel = SD_SLOTA_CHANNEL,
  1715.            .device = SD_SLOTA_DEVICE,
  1716.            .id = EXI_ID_NONE,
  1717.            },
  1718.     [1] = {
  1719.            .channel = SD_SLOTB_CHANNEL,
  1720.            .device = SD_SLOTB_DEVICE,
  1721.            .id = EXI_ID_NONE,
  1722.            },
  1723.     {.id = 0}
  1724. };
  1725.  
  1726. static struct exi_driver sd_driver = {
  1727.     .name = DRV_MODULE_NAME,
  1728.     .eid_table = sd_eid_table,
  1729.     .frequency = SD_SPI_CLK_IDX,
  1730.     .probe = sd_probe,
  1731.     .remove = sd_remove,
  1732. };
  1733.  
  1734.  
  1735. /*
  1736.  * Kernel module interface.
  1737.  *
  1738.  */
  1739.  
  1740. /*
  1741.  *
  1742.  */
  1743. static int __init sd_init_module(void)
  1744. {
  1745.     int retval = 0;
  1746.  
  1747.     sd_printk(KERN_INFO, "%s - version %s\n", DRV_DESCRIPTION,
  1748.           sd_driver_version);
  1749.  
  1750.     if (register_blkdev(SD_MAJOR, DRV_MODULE_NAME)) {
  1751.         sd_printk(KERN_ERR, "unable to register major %d\n", SD_MAJOR);
  1752.         retval = -EIO;
  1753.         goto out;
  1754.     }
  1755.  
  1756.     retval = exi_driver_register(&sd_driver);
  1757.  
  1758. out:
  1759.     return retval;
  1760. }
  1761.  
  1762. /*
  1763.  *
  1764.  */
  1765. static void __exit sd_exit_module(void)
  1766. {
  1767.     unregister_blkdev(SD_MAJOR, DRV_MODULE_NAME);
  1768.     exi_driver_unregister(&sd_driver);
  1769. }
  1770.  
  1771. module_init(sd_init_module);
  1772. module_exit(sd_exit_module);
  1773.  
  1774. MODULE_AUTHOR(DRV_AUTHOR);
  1775. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1776. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement