Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:56

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSBSPsPowerPCMPC55XX
0007  *
0008  * @brief SD Card initialization code.
0009  */
0010 
0011 /*
0012  * Copyright (c) 2008 embedded brains GmbH & Co. KG
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #include <stdio.h>
0036 
0037 #include <mpc55xx/mpc55xx.h>
0038 #include <mpc55xx/regs.h>
0039 #include <mpc55xx/dspi.h>
0040 
0041 #include <bsp.h>
0042 
0043 #include <rtems/status-checks.h>
0044 
0045 #ifdef MPC55XX_BOARD_MPC5566EVB
0046 
0047 static rtems_status_code mpc55xx_dspi_init(void)
0048 {
0049     int rv = 0;
0050     int i = 0;
0051     char device_name [] = "/dev/spi0";
0052     union SIU_PCR_tag pcr = MPC55XX_ZERO_FLAGS;
0053 
0054     rv = rtems_libi2c_initialize();
0055     RTEMS_CHECK_RV_SC( rv, "rtems_libi2c_initialize");
0056 
0057     /* DSPI D inputs are taken from DSPI C */
0058     SIU.DISR.R = 0x000000FC;
0059 
0060     /* DSPI A signals */
0061     pcr.B.PA = 1;
0062     pcr.B.ODE = 0;
0063     pcr.B.HYS = 0;
0064     pcr.B.SRC = 3;
0065     pcr.B.WPE = 1;
0066     pcr.B.WPS = 1;
0067 
0068     /* SCK */
0069     pcr.B.OBE = 1;
0070     pcr.B.IBE = 0;
0071     SIU.PCR [93].R = pcr.R;
0072 
0073     /* SIN */
0074     pcr.B.OBE = 0;
0075     pcr.B.IBE = 1;
0076     SIU.PCR [94].R = pcr.R;
0077 
0078     /* SOUT */
0079     pcr.B.OBE = 1;
0080     pcr.B.IBE = 0;
0081     SIU.PCR [95].R = pcr.R;
0082 
0083     /* PCSx */
0084     pcr.B.OBE = 1;
0085     pcr.B.IBE = 0;
0086     SIU.PCR [96].R = pcr.R;
0087     SIU.PCR [97].R = pcr.R;
0088     SIU.PCR [98].R = pcr.R;
0089     SIU.PCR [99].R = pcr.R;
0090     SIU.PCR [100].R = pcr.R;
0091     SIU.PCR [101].R = pcr.R;
0092 
0093     mpc55xx_dspi_bus_table [3].master = 0;
0094     for (i = 0; i < MPC55XX_DSPI_NUMBER; ++i) {
0095         device_name [8] = (char) ('0' + i);
0096         rv = rtems_libi2c_register_bus( device_name, (rtems_libi2c_bus_t *) &mpc55xx_dspi_bus_table [i]);
0097         RTEMS_CHECK_RV_SC( rv, device_name);
0098     }
0099 
0100     return RTEMS_SUCCESSFUL;
0101 }
0102 
0103 #include <stdio.h>
0104 #include <rtems/fsmount.h>
0105 #include <rtems/dosfs.h>
0106 #include <rtems/bdpart.h>
0107 #include <rtems/console.h>
0108 
0109 #include <libchip/spi-sd-card.h>
0110 
0111 #define MPC55XX_DEVICE "sd-card-a"
0112 #define MPC55XX_DEVICE_FILE "/dev/" MPC55XX_DEVICE
0113 #define MPC55XX_PARTITION "/dev/sd-card-a1"
0114 #define MPC55XX_MOUNT_POINT "/mnt"
0115 
0116 static fstab_t mpc55xx_fs_table [] = { {
0117         MPC55XX_PARTITION, MPC55XX_MOUNT_POINT,
0118         "dosfs", RTEMS_FILESYSTEM_READ_WRITE,
0119         FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
0120         FSMOUNT_MNT_OK
0121     }, {
0122         MPC55XX_DEVICE_FILE, MPC55XX_MOUNT_POINT,
0123         "dosfs", RTEMS_FILESYSTEM_READ_WRITE,
0124         FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
0125         0
0126     }
0127 };
0128 
0129 sd_card_driver_entry sd_card_driver_table [] = {
0130     {
0131         .device_name = "/dev/sd-card-a",
0132         .bus = 0,
0133         .transfer_mode = SD_CARD_TRANSFER_MODE_DEFAULT,
0134         .command = SD_CARD_COMMAND_DEFAULT,
0135         /* response : whatever, */
0136         .response_index = SD_CARD_COMMAND_SIZE,
0137         .n_ac_max = SD_CARD_N_AC_MAX_DEFAULT,
0138         .block_number = 0,
0139         .block_size = 0,
0140         .block_size_shift = 0,
0141         .busy = true,
0142         .verbose = true,
0143         .schedule_if_busy = false
0144     }
0145 };
0146 
0147 size_t sd_card_driver_table_size = sizeof( sd_card_driver_table) / sizeof( sd_card_driver_table [0]);
0148 
0149 rtems_status_code mpc55xx_sd_card_init( bool mount)
0150 {
0151     rtems_status_code sc = RTEMS_SUCCESSFUL;
0152     int rv = 0;
0153     sd_card_driver_entry *e = &sd_card_driver_table [0];
0154 
0155     RTEMS_DEBUG_PRINT( "Task started\n");
0156 
0157     sc = mpc55xx_dspi_init();
0158     RTEMS_CHECK_SC( rv, "Intitalize DSPI bus");
0159 
0160     e->bus = mpc55xx_dspi_bus_table [0].bus_number;
0161 
0162     sc = sd_card_register();
0163     RTEMS_CHECK_SC( sc, "Register SD Card");
0164 
0165     if (mount) {
0166         sc = rtems_bdpart_register_from_disk( MPC55XX_DEVICE_FILE);
0167         RTEMS_CHECK_SC( sc, "Initialize IDE partition table");
0168 
0169         rv = rtems_fsmount( mpc55xx_fs_table, sizeof( mpc55xx_fs_table) / sizeof( mpc55xx_fs_table [0]), NULL);
0170         RTEMS_CHECK_RV_SC( rv, "Mount file systems");
0171     }
0172 
0173     return RTEMS_SUCCESSFUL;
0174 }
0175 
0176 #endif /* MPC55XX_BOARD_MPC5566EVB */