]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/pdk.git/blob - packages/ti/boot/sbl/soc/omapl137/sbl_soc.c
sbl: add to PDK
[processor-sdk/pdk.git] / packages / ti / boot / sbl / soc / omapl137 / sbl_soc.c
1 /*
2  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
34 #include "sbl_soc.h"
36 #include <ti/board/src/evmOMAPL137/include/board_cfg.h>
37 #include <ti/board/src/flash/include/board_flash.h>
38 #include <ti/board/src/flash/nor/device/w25x32vsfig.h>
39 #include <ti/drv/spi/soc/SPI_soc.h>
41 int32_t SBL_socInit()
42 {
43     Board_initCfg boardCfg;
44   boardCfg = BOARD_INIT_UNLOCK_MMR |
45         BOARD_INIT_PLL             |
46         BOARD_INIT_MODULE_CLOCK    |
47         BOARD_INIT_DDR             |
48         BOARD_INIT_PINMUX_CONFIG   |
49         BOARD_INIT_EMIF_PINMUX     |
50         BOARD_INIT_UART_STDIO;
52     /* Board Library Init. */
53     if (Board_init(boardCfg))
54     {
55         return -1;
56     }
57     return 0;
58 }
60 #if defined (BOOT_SPI)
61 int32_t SBL_spiInit(void *handle)
62 {
63     SPI_v0_HWAttrs spi_cfg;
64     SPI_Params       spiParams;
66     /* Get the default SPI init configurations */
67     SPI_socGetInitCfg(BOARD_SPI_NOR_INSTANCE, &spi_cfg);
69     /* Modify the default SPI configurations if necessary */
70     /* Turning off interrupts for baremetal mode. May be re-enabled by app */
71     spi_cfg.enableIntr = false;
73     /* Set the default SPI init configurations */
74     SPI_socSetInitCfg(BOARD_SPI_NOR_INSTANCE, &spi_cfg);
76     /* Default SPI configuration parameters */
77     SPI_Params_init(&spiParams);
78     spiParams.frameFormat  = SPI_POL0_PHA1;
80     *(Board_flashHandle *) handle = Board_flashOpen(BOARD_FLASH_ID_W25X32VSFIG,
81                             BOARD_SPI_NOR_INSTANCE, &spiParams);
83     if (*(uint32_t *) handle == 0)
84     {
85         return -1;
86     }
88     return 0;
89 }
91 int32_t SBL_spiFlashWrite(void *handle, uint8_t *src, uint32_t length,
92     uint32_t offset)
93 {
94     Board_flashHandle h = *(Board_flashHandle *) handle;
95     int32_t ret;
96     uint32_t blockNum, pageNum, len, lenOffset;
97     uint8_t dst[1];
99     /* Iterate through block size = 0x10000 */
100     len = length;
101     lenOffset = 0;
102     while (len > 0x1000)
103     {
104         if ((ret = Board_flashOffsetToBlkPage(h, offset+lenOffset, &blockNum, &pageNum)))
105         {
106             return ret;
107         }
109         /* Erase block, to which data has to be written */
110         if ((ret = Board_flashEraseBlk(h, blockNum)))
111         {
112             return ret;
113         }
114         /* Write buffer to flash */
115         if ((ret = Board_flashWrite(h, offset+lenOffset, src+lenOffset, 0x1000, NULL)))
116         {
117             return ret;
118         }
120         /* dummy read */
121         Board_flashRead(h, offset+lenOffset, dst, 1, NULL);
122         len -= 0x1000;
123         lenOffset += 0x1000;
124     }
126     /* Flash remainder as necessary */
127     if ((ret = Board_flashOffsetToBlkPage(h, offset+lenOffset, &blockNum, &pageNum)))
128     {
129         return ret;
130     }
132     /* Erase block, to which data has to be written */
133     if ((ret = Board_flashEraseBlk(h, blockNum)))
134     {
135         return ret;
136     }
138     /* Write buffer to flash */
139     if ((ret = Board_flashWrite(h, offset+lenOffset, src+lenOffset, len, NULL)))
140     {
141         return ret;
142     }
144     return 0;
147 int32_t SBL_spiFlashRead(void *handle, uint8_t *dst, uint32_t length,
148     uint32_t offset)
150     Board_flashHandle h = *(Board_flashHandle *) handle;
152     if (Board_flashRead(h, offset, dst, length, NULL))
153     {
154         return -1;
155     }
157     return 0;
160 int32_t SBL_spiClose(void *handle)
162     Board_flashHandle h = *(Board_flashHandle *) handle;
163     Board_flashClose(h);
164     return 0;
166 #endif