blob: 044052b32f93fdc6456f76d95d18e07bf66f80f7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR7cc4c462010-01-15 19:15:48 +05302/*
3 * (C) Copyright 2009
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
Vipin KUMAR7cc4c462010-01-15 19:15:48 +05305 */
6
7#include <common.h>
8#include <command.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07009#include <cpu_func.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060010#include <env.h>
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053011#include <i2c.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070012#include <init.h>
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053013#include <net.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Amit Virdi47462dd2012-05-07 13:06:49 +053015#include <linux/mtd/st_smi.h>
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053016#include <asm/io.h>
17#include <asm/arch/hardware.h>
Vipin KUMAR9ab7f852010-01-15 19:15:51 +053018#include <asm/arch/spr_emi.h>
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053019#include <asm/arch/spr_defs.h>
20
21#define CPU 0
22#define DDR 1
23#define SRAM_REL 0xD2801000
24
25DECLARE_GLOBAL_DATA_PTR;
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053026
Vipin KUMARf6b56782012-05-07 13:06:41 +053027#if defined(CONFIG_CMD_NET)
28static int i2c_read_mac(uchar *buffer);
29#endif
30
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053031int dram_init(void)
32{
Amit Virdi29b7a832012-05-07 13:06:39 +053033 /* Store complete RAM size and return */
Shiraz Hashim6bef6c82011-06-11 21:32:57 +000034 gd->ram_size = get_ram_size(PHYS_SDRAM_1, PHYS_SDRAM_1_MAXSIZE);
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053035
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053036 return 0;
37}
38
Simon Glass2f949c32017-03-31 08:40:32 -060039int dram_init_banksize(void)
Shiraz Hashim6bef6c82011-06-11 21:32:57 +000040{
41 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
42 gd->bd->bi_dram[0].size = gd->ram_size;
Simon Glass2f949c32017-03-31 08:40:32 -060043
44 return 0;
Shiraz Hashim6bef6c82011-06-11 21:32:57 +000045}
46
Amit Virdi47462dd2012-05-07 13:06:49 +053047int board_early_init_f()
48{
49#if defined(CONFIG_ST_SMI)
50 smi_init();
51#endif
52 return 0;
53}
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053054int misc_init_r(void)
55{
Vipin KUMARf8c72782010-01-15 19:15:49 +053056#if defined(CONFIG_CMD_NET)
57 uchar mac_id[6];
58
Simon Glass399a9ce2017-08-03 12:22:14 -060059 if (!eth_env_get_enetaddr("ethaddr", mac_id) && !i2c_read_mac(mac_id))
Simon Glass8551d552017-08-03 12:22:11 -060060 eth_env_set_enetaddr("ethaddr", mac_id);
Vipin KUMARf8c72782010-01-15 19:15:49 +053061#endif
Simon Glass6a38e412017-08-03 12:22:09 -060062 env_set("verify", "n");
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053063
64#if defined(CONFIG_SPEAR_USBTTY)
Simon Glass6a38e412017-08-03 12:22:09 -060065 env_set("stdin", "usbtty");
66 env_set("stdout", "usbtty");
67 env_set("stderr", "usbtty");
Shiraz Hashime35e2872012-05-07 13:06:53 +053068
69#ifndef CONFIG_SYS_NO_DCACHE
70 dcache_enable();
71#endif
Vipin KUMAR7cc4c462010-01-15 19:15:48 +053072#endif
73 return 0;
74}
75
Vipin KUMAR9ab7f852010-01-15 19:15:51 +053076#ifdef CONFIG_SPEAR_EMI
77struct cust_emi_para {
78 unsigned int tap;
79 unsigned int tsdp;
80 unsigned int tdpw;
81 unsigned int tdpr;
82 unsigned int tdcs;
83};
84
85/* EMI timing setting of m28w640hc of linux kernel */
86const struct cust_emi_para emi_timing_m28w640hc = {
87 .tap = 0x10,
88 .tsdp = 0x05,
89 .tdpw = 0x0a,
90 .tdpr = 0x0a,
91 .tdcs = 0x05,
92};
93
94/* EMI timing setting of bootrom */
95const struct cust_emi_para emi_timing_bootrom = {
96 .tap = 0xf,
97 .tsdp = 0x0,
98 .tdpw = 0xff,
99 .tdpr = 0x111,
100 .tdcs = 0x02,
101};
102
103void spear_emi_init(void)
104{
105 const struct cust_emi_para *p = &emi_timing_m28w640hc;
106 struct emi_regs *emi_regs_p = (struct emi_regs *)CONFIG_SPEAR_EMIBASE;
107 unsigned int cs;
108 unsigned int val, tmp;
109
110 val = readl(CONFIG_SPEAR_RASBASE);
111
112 if (val & EMI_ACKMSK)
113 tmp = 0x3f;
114 else
115 tmp = 0x0;
116
117 writel(tmp, &emi_regs_p->ack);
118
119 for (cs = 0; cs < CONFIG_SYS_MAX_FLASH_BANKS; cs++) {
120 writel(p->tap, &emi_regs_p->bank_regs[cs].tap);
121 writel(p->tsdp, &emi_regs_p->bank_regs[cs].tsdp);
122 writel(p->tdpw, &emi_regs_p->bank_regs[cs].tdpw);
123 writel(p->tdpr, &emi_regs_p->bank_regs[cs].tdpr);
124 writel(p->tdcs, &emi_regs_p->bank_regs[cs].tdcs);
125 writel(EMI_CNTL_ENBBYTERW | ((val & 0x18) >> 3),
126 &emi_regs_p->bank_regs[cs].control);
127 }
128}
129#endif
130
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530131int spear_board_init(ulong mach_type)
132{
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530133 gd->bd->bi_arch_number = mach_type;
134
135 /* adress of boot parameters */
136 gd->bd->bi_boot_params = CONFIG_BOOT_PARAMS_ADDR;
137
Vipin KUMAR9ab7f852010-01-15 19:15:51 +0530138#ifdef CONFIG_SPEAR_EMI
139 spear_emi_init();
140#endif
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530141 return 0;
142}
143
Vipin KUMARf6b56782012-05-07 13:06:41 +0530144#if defined(CONFIG_CMD_NET)
Vipin KUMARf8c72782010-01-15 19:15:49 +0530145static int i2c_read_mac(uchar *buffer)
146{
147 u8 buf[2];
148
149 i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
150
151 /* Check if mac in i2c memory is valid */
152 if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
153 /* Valid mac address is saved in i2c eeprom */
154 i2c_read(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, buffer, MAC_LEN);
155 return 0;
156 }
157
158 return -1;
159}
160
161static int write_mac(uchar *mac)
162{
163 u8 buf[2];
164
165 buf[0] = (u8)MAGIC_BYTE0;
166 buf[1] = (u8)MAGIC_BYTE1;
167 i2c_write(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
168
169 buf[0] = (u8)~MAGIC_BYTE0;
170 buf[1] = (u8)~MAGIC_BYTE1;
171
172 i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
173
174 /* check if valid MAC address is saved in I2C EEPROM or not? */
175 if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
176 i2c_write(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, mac, MAC_LEN);
177 puts("I2C EEPROM written with mac address \n");
178 return 0;
179 }
180
Vipin KUMARf6b56782012-05-07 13:06:41 +0530181 puts("I2C EEPROM writing failed\n");
Vipin KUMARf8c72782010-01-15 19:15:49 +0530182 return -1;
183}
Vipin KUMARf6b56782012-05-07 13:06:41 +0530184#endif
Vipin KUMARf8c72782010-01-15 19:15:49 +0530185
Simon Glassed38aef2020-05-10 11:40:03 -0600186int do_chip_config(struct cmd_tbl *cmdtp, int flag, int argc,
187 char *const argv[])
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530188{
189 void (*sram_setfreq) (unsigned int, unsigned int);
Vipin KUMARf6b56782012-05-07 13:06:41 +0530190 unsigned int frequency;
191#if defined(CONFIG_CMD_NET)
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530192 unsigned char mac[6];
Vipin KUMARf6b56782012-05-07 13:06:41 +0530193#endif
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530194
Wolfgang Denk3b683112010-07-17 01:06:04 +0200195 if ((argc > 3) || (argc < 2))
196 return cmd_usage(cmdtp);
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530197
198 if ((!strcmp(argv[1], "cpufreq")) || (!strcmp(argv[1], "ddrfreq"))) {
199
200 frequency = simple_strtoul(argv[2], NULL, 0);
201
202 if (frequency > 333) {
203 printf("Frequency is limited to 333MHz\n");
204 return 1;
205 }
206
207 sram_setfreq = memcpy((void *)SRAM_REL, setfreq, setfreq_sz);
208
209 if (!strcmp(argv[1], "cpufreq")) {
210 sram_setfreq(CPU, frequency);
211 printf("CPU frequency changed to %u\n", frequency);
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530212 } else {
213 sram_setfreq(DDR, frequency);
214 printf("DDR frequency changed to %u\n", frequency);
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530215 }
216
217 return 0;
Vipin KUMARf6b56782012-05-07 13:06:41 +0530218
219#if defined(CONFIG_CMD_NET)
Vipin KUMARf8c72782010-01-15 19:15:49 +0530220 } else if (!strcmp(argv[1], "ethaddr")) {
221
Vipin KUMARf6b56782012-05-07 13:06:41 +0530222 u32 reg;
223 char *e, *s = argv[2];
Vipin KUMARf8c72782010-01-15 19:15:49 +0530224 for (reg = 0; reg < 6; ++reg) {
225 mac[reg] = s ? simple_strtoul(s, &e, 16) : 0;
226 if (s)
227 s = (*e) ? e + 1 : e;
228 }
229 write_mac(mac);
230
231 return 0;
Vipin KUMARf6b56782012-05-07 13:06:41 +0530232#endif
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530233 } else if (!strcmp(argv[1], "print")) {
Vipin KUMARf6b56782012-05-07 13:06:41 +0530234#if defined(CONFIG_CMD_NET)
Vipin KUMARf8c72782010-01-15 19:15:49 +0530235 if (!i2c_read_mac(mac)) {
Vipin KUMARf6b56782012-05-07 13:06:41 +0530236 printf("Ethaddr (from i2c mem) = %pM\n", mac);
Vipin KUMARf8c72782010-01-15 19:15:49 +0530237 } else {
238 printf("Ethaddr (from i2c mem) = Not set\n");
239 }
Vipin KUMARf6b56782012-05-07 13:06:41 +0530240#endif
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530241 return 0;
242 }
243
Wolfgang Denk3b683112010-07-17 01:06:04 +0200244 return cmd_usage(cmdtp);
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530245}
246
247U_BOOT_CMD(chip_config, 3, 1, do_chip_config,
248 "configure chip",
249 "chip_config cpufreq/ddrfreq frequency\n"
Vipin KUMARf6b56782012-05-07 13:06:41 +0530250#if defined(CONFIG_CMD_NET)
251 "chip_config ethaddr XX:XX:XX:XX:XX:XX\n"
252#endif
Vipin KUMAR7cc4c462010-01-15 19:15:48 +0530253 "chip_config print");