blob: 935769b825b3a3323b074bf43506588ff3721044 [file] [log] [blame]
dzu@denx.de6ca24c62006-04-21 18:30:47 +02001/*
2 * (C) Copyright 2005
3 * Stefan Strobl, GERSYS GmbH, stefan.strobl@gersys.de
4 *
5 * (C) Copyright 2005
6 * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
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 as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <command.h>
29
Wolfgang Denk610cf362006-05-03 01:24:04 +020030/*
dzu@denx.de6ca24c62006-04-21 18:30:47 +020031 * BC3450 specific commands
32 */
Jon Loeligerfcec2eb2007-07-09 18:19:09 -050033#if defined(CONFIG_CMD_BSP)
dzu@denx.de6ca24c62006-04-21 18:30:47 +020034
Wolfgang Denk610cf362006-05-03 01:24:04 +020035/*
dzu@denx.de6ca24c62006-04-21 18:30:47 +020036 * Definitions for DS1620 chip
37 */
38#define THERM_START_CONVERT 0xee
39#define THERM_RESET 0xaf
40#define THERM_READ_CONFIG 0xac
41#define THERM_READ_TEMP 0xaa
42#define THERM_READ_TL 0xa2
43#define THERM_READ_TH 0xa1
44#define THERM_WRITE_CONFIG 0x0c
45#define THERM_WRITE_TL 0x02
46#define THERM_WRITE_TH 0x01
47
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020048#define CONFIG_SYS_CPU 2
49#define CONFIG_SYS_1SHOT 1
50#define CONFIG_SYS_STANDALONE 0
dzu@denx.de6ca24c62006-04-21 18:30:47 +020051
52struct therm {
Wolfgang Denk610cf362006-05-03 01:24:04 +020053 int hi;
54 int lo;
dzu@denx.de6ca24c62006-04-21 18:30:47 +020055};
56
57/*
58 * SM501 Register
59 */
60#define SM501_GPIO_CTRL_LOW 0x00000008UL /* gpio pins 0..31 */
61#define SM501_GPIO_CTRL_HIGH 0x0000000CUL /* gpio pins 32..63 */
62#define SM501_POWER_MODE0_GATE 0x00000040UL
63#define SM501_POWER_MODE1_GATE 0x00000048UL
64#define POWER_MODE_GATE_GPIO_PWM_I2C 0x00000040UL
Wolfgang Denk53677ef2008-05-20 16:00:29 +020065#define SM501_GPIO_DATA_LOW 0x00010000UL
dzu@denx.de6ca24c62006-04-21 18:30:47 +020066#define SM501_GPIO_DATA_HIGH 0x00010004UL
67#define SM501_GPIO_DATA_DIR_LOW 0x00010008UL
68#define SM501_GPIO_DATA_DIR_HIGH 0x0001000CUL
69#define SM501_PANEL_DISPLAY_CONTROL 0x00080000UL
70#define SM501_CRT_DISPLAY_CONTROL 0x00080200UL
71
72/* SM501 CRT Display Control Bits */
73#define SM501_CDC_SEL (1 << 9)
74#define SM501_CDC_TE (1 << 8)
75#define SM501_CDC_E (1 << 2)
76
77/* SM501 Panel Display Control Bits */
78#define SM501_PDC_FPEN (1 << 27)
79#define SM501_PDC_BIAS (1 << 26)
80#define SM501_PDC_DATA (1 << 25)
81#define SM501_PDC_VDDEN (1 << 24)
82
83/* SM501 GPIO Data LOW Bits */
84#define SM501_GPIO24 0x01000000
85#define SM501_GPIO25 0x02000000
86#define SM501_GPIO26 0x04000000
87#define SM501_GPIO27 0x08000000
88#define SM501_GPIO28 0x10000000
89#define SM501_GPIO29 0x20000000
90#define SM501_GPIO30 0x40000000
91#define SM501_GPIO31 0x80000000
92
93/* SM501 GPIO Data HIGH Bits */
94#define SM501_GPIO46 0x00004000
95#define SM501_GPIO47 0x00008000
96#define SM501_GPIO48 0x00010000
97#define SM501_GPIO49 0x00020000
98#define SM501_GPIO50 0x00040000
99#define SM501_GPIO51 0x00080000
100
101/* BC3450 GPIOs @ SM501 Data LOW */
102#define DIP (SM501_GPIO24 | SM501_GPIO25 | SM501_GPIO26 | SM501_GPIO27)
103#define DS1620_DQ SM501_GPIO29 /* I/O */
104#define DS1620_CLK SM501_GPIO30 /* High active O/P */
105#define DS1620_RES SM501_GPIO31 /* Low active O/P */
106/* BC3450 GPIOs @ SM501 Data HIGH */
107#define BUZZER SM501_GPIO47 /* Low active O/P */
108#define DS1620_TLOW SM501_GPIO48 /* High active I/P */
109#define PWR_OFF SM501_GPIO49 /* Low active O/P */
110#define FP_DATA_TRI SM501_GPIO50 /* High active O/P */
111
112
113/*
114 * Initialise GPIO on SM501
115 *
116 * This function may be called from several other functions.
117 * Yet, the initialisation sequence is executed only the first
118 * time the function is called.
119 */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200120int sm501_gpio_init (void)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200121{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200122 static int init_done = 0;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200123
Wolfgang Denk610cf362006-05-03 01:24:04 +0200124 if (init_done) {
Wolfgang Denkd0090c32012-07-22 09:56:06 +0200125 debug("sm501_gpio_init: nothing to be done.\n");
Wolfgang Denk610cf362006-05-03 01:24:04 +0200126 return 1;
127 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200128
Wolfgang Denk610cf362006-05-03 01:24:04 +0200129 /* enable SM501 GPIO control (in both power modes) */
130 *(vu_long *) (SM501_MMIO_BASE + SM501_POWER_MODE0_GATE) |=
131 POWER_MODE_GATE_GPIO_PWM_I2C;
132 *(vu_long *) (SM501_MMIO_BASE + SM501_POWER_MODE1_GATE) |=
133 POWER_MODE_GATE_GPIO_PWM_I2C;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200134
Wolfgang Denk610cf362006-05-03 01:24:04 +0200135 /* set up default O/Ps */
136 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) &=
137 ~(DS1620_RES | DS1620_CLK);
138 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) |= DS1620_DQ;
139 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) &=
140 ~(FP_DATA_TRI);
141 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) |=
142 (BUZZER | PWR_OFF);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200143
Wolfgang Denk610cf362006-05-03 01:24:04 +0200144 /* configure directions for SM501 GPIO pins */
145 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_CTRL_LOW) &= ~(0xFF << 24);
146 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_CTRL_HIGH) &=
147 ~(0x3F << 14);
148 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_LOW) &=
149 ~(DIP | DS1620_DQ);
150 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_LOW) |=
151 (DS1620_RES | DS1620_CLK);
152 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_HIGH) &=
153 ~DS1620_TLOW;
154 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_HIGH) |=
155 (PWR_OFF | BUZZER | FP_DATA_TRI);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200156
Wolfgang Denk610cf362006-05-03 01:24:04 +0200157 init_done = 1;
Wolfgang Denkd0090c32012-07-22 09:56:06 +0200158 debug("sm501_gpio_init: done.\n");
159
Wolfgang Denk610cf362006-05-03 01:24:04 +0200160 return 0;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200161}
162
163
164/*
165 * dip - read Config Inputs
166 *
167 * read and prints the dip switch
168 * and/or external config inputs (4bits) 0...0x0F
169 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200170int cmd_dip (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200171{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200172 vu_long rc = 0;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200173
Wolfgang Denk610cf362006-05-03 01:24:04 +0200174 sm501_gpio_init ();
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200175
Wolfgang Denk610cf362006-05-03 01:24:04 +0200176 /* read dip switch */
177 rc = *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW);
178 rc = ~rc;
179 rc &= DIP;
180 rc = (int) (rc >> 24);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200181
Wolfgang Denk610cf362006-05-03 01:24:04 +0200182 /* plausibility check */
183 if (rc > 0x0F)
184 return -1;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200185
Wolfgang Denk9b55a252008-07-11 01:16:00 +0200186 printf ("0x%lx\n", rc);
Wolfgang Denk610cf362006-05-03 01:24:04 +0200187 return 0;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200188}
189
Wolfgang Denk610cf362006-05-03 01:24:04 +0200190U_BOOT_CMD (dip, 1, 1, cmd_dip,
Peter Tyser2fb26042009-01-27 18:03:12 -0600191 "read dip switch and config inputs",
Wolfgang Denk610cf362006-05-03 01:24:04 +0200192 "\n"
193 " - prints the state of the dip switch and/or\n"
194 " external configuration inputs as hex value.\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200195 " - \"Config 1\" is the LSB");
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200196
197
198/*
199 * buz - turns Buzzer on/off
200 */
201#ifdef CONFIG_BC3450_BUZZER
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200202static int cmd_buz (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200203{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200204 if (argc != 2) {
205 printf ("Usage:\nspecify one argument: \"on\" or \"off\"\n");
206 return 1;
207 }
208
209 sm501_gpio_init ();
210
211 if (strncmp (argv[1], "on", 2) == 0) {
212 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) &=
213 ~(BUZZER);
214 return 0;
215 } else if (strncmp (argv[1], "off", 3) == 0) {
216 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) |=
217 BUZZER;
218 return 0;
219 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200220 printf ("Usage:\nspecify one argument: \"on\" or \"off\"\n");
221 return 1;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200222}
223
Wolfgang Denk610cf362006-05-03 01:24:04 +0200224U_BOOT_CMD (buz, 2, 1, cmd_buz,
Peter Tyser2fb26042009-01-27 18:03:12 -0600225 "turns buzzer on/off",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200226 "\n" "buz <on/off>\n" " - turns the buzzer on or off");
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200227#endif /* CONFIG_BC3450_BUZZER */
228
229
230/*
231 * fp - front panel commands
232 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200233static int cmd_fp (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200234{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200235 sm501_gpio_init ();
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200236
Wolfgang Denk610cf362006-05-03 01:24:04 +0200237 if (strncmp (argv[1], "on", 2) == 0) {
238 /* turn on VDD first */
239 *(vu_long *) (SM501_MMIO_BASE +
240 SM501_PANEL_DISPLAY_CONTROL) |= SM501_PDC_VDDEN;
241 udelay (1000);
242 /* then put data on */
243 *(vu_long *) (SM501_MMIO_BASE +
244 SM501_PANEL_DISPLAY_CONTROL) |= SM501_PDC_DATA;
245 /* wait some time and enable backlight */
246 udelay (1000);
247 *(vu_long *) (SM501_MMIO_BASE +
248 SM501_PANEL_DISPLAY_CONTROL) |= SM501_PDC_BIAS;
249 udelay (1000);
250 *(vu_long *) (SM501_MMIO_BASE +
251 SM501_PANEL_DISPLAY_CONTROL) |= SM501_PDC_FPEN;
252 return 0;
253 } else if (strncmp (argv[1], "off", 3) == 0) {
254 /* turn off the backlight first */
255 *(vu_long *) (SM501_MMIO_BASE +
256 SM501_PANEL_DISPLAY_CONTROL) &= ~SM501_PDC_FPEN;
257 udelay (1000);
258 *(vu_long *) (SM501_MMIO_BASE +
259 SM501_PANEL_DISPLAY_CONTROL) &= ~SM501_PDC_BIAS;
260 udelay (200000);
261 /* wait some time, then remove data */
262 *(vu_long *) (SM501_MMIO_BASE +
263 SM501_PANEL_DISPLAY_CONTROL) &= ~SM501_PDC_DATA;
264 udelay (1000);
265 /* and remove VDD last */
266 *(vu_long *) (SM501_MMIO_BASE +
267 SM501_PANEL_DISPLAY_CONTROL) &=
268 ~SM501_PDC_VDDEN;
269 return 0;
270 } else if (strncmp (argv[1], "bl", 2) == 0) {
271 /* turn on/off backlight only */
272 if (strncmp (argv[2], "on", 2) == 0) {
273 *(vu_long *) (SM501_MMIO_BASE +
274 SM501_PANEL_DISPLAY_CONTROL) |=
275 SM501_PDC_BIAS;
276 udelay (1000);
277 *(vu_long *) (SM501_MMIO_BASE +
278 SM501_PANEL_DISPLAY_CONTROL) |=
279 SM501_PDC_FPEN;
280 return 0;
281 } else if (strncmp (argv[2], "off", 3) == 0) {
282 *(vu_long *) (SM501_MMIO_BASE +
283 SM501_PANEL_DISPLAY_CONTROL) &=
284 ~SM501_PDC_FPEN;
285 udelay (1000);
286 *(vu_long *) (SM501_MMIO_BASE +
287 SM501_PANEL_DISPLAY_CONTROL) &=
288 ~SM501_PDC_BIAS;
289 return 0;
290 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200291 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200292#ifdef CONFIG_BC3450_CRT
Wolfgang Denk610cf362006-05-03 01:24:04 +0200293 else if (strncmp (argv[1], "crt", 3) == 0) {
294 /* enables/disables the crt output (debug only) */
295 if (strncmp (argv[2], "on", 2) == 0) {
296 *(vu_long *) (SM501_MMIO_BASE +
297 SM501_CRT_DISPLAY_CONTROL) |=
298 (SM501_CDC_TE | SM501_CDC_E);
299 *(vu_long *) (SM501_MMIO_BASE +
300 SM501_CRT_DISPLAY_CONTROL) &=
301 ~SM501_CDC_SEL;
302 return 0;
303 } else if (strncmp (argv[2], "off", 3) == 0) {
304 *(vu_long *) (SM501_MMIO_BASE +
305 SM501_CRT_DISPLAY_CONTROL) &=
306 ~(SM501_CDC_TE | SM501_CDC_E);
307 *(vu_long *) (SM501_MMIO_BASE +
308 SM501_CRT_DISPLAY_CONTROL) |=
309 SM501_CDC_SEL;
310 return 0;
311 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200312 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200313#endif /* CONFIG_BC3450_CRT */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200314 printf ("Usage:%s\n", cmdtp->help);
315 return 1;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200316}
317
Wolfgang Denk610cf362006-05-03 01:24:04 +0200318U_BOOT_CMD (fp, 3, 1, cmd_fp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600319 "front panes access functions",
Wolfgang Denk610cf362006-05-03 01:24:04 +0200320 "\n"
321 "fp bl <on/off>\n"
322 " - turns the CCFL backlight of the display on/off\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200323 "fp <on/off>\n" " - turns the whole display on/off"
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200324#ifdef CONFIG_BC3450_CRT
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200325 "\n"
Wolfgang Denk610cf362006-05-03 01:24:04 +0200326 "fp crt <on/off>\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200327 " - enables/disables the crt output (debug only)"
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200328#endif /* CONFIG_BC3450_CRT */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200329 );
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200330
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200331/*
332 * temp - DS1620 thermometer
333 */
334/* GERSYS BC3450 specific functions */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200335static inline void bc_ds1620_set_clk (int clk)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200336{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200337 if (clk)
338 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) |=
339 DS1620_CLK;
340 else
341 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) &=
342 ~DS1620_CLK;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200343}
344
Wolfgang Denk610cf362006-05-03 01:24:04 +0200345static inline void bc_ds1620_set_data (int dat)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200346{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200347 if (dat)
348 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) |=
349 DS1620_DQ;
350 else
351 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) &=
352 ~DS1620_DQ;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200353}
354
Wolfgang Denk610cf362006-05-03 01:24:04 +0200355static inline int bc_ds1620_get_data (void)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200356{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200357 vu_long rc;
358
359 rc = *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW);
360 rc &= DS1620_DQ;
361 if (rc != 0)
362 rc = 1;
363 return (int) rc;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200364}
365
Wolfgang Denk610cf362006-05-03 01:24:04 +0200366static inline void bc_ds1620_set_data_dir (int dir)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200367{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200368 if (dir) /* in */
369 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_LOW) &= ~DS1620_DQ;
370 else /* out */
371 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_DIR_LOW) |= DS1620_DQ;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200372}
373
Wolfgang Denk610cf362006-05-03 01:24:04 +0200374static inline void bc_ds1620_set_reset (int res)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200375{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200376 if (res)
377 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) |= DS1620_RES;
378 else
379 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_LOW) &= ~DS1620_RES;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200380}
381
382/* hardware independent functions */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200383static void ds1620_send_bits (int nr, int value)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200384{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200385 int i;
386
387 for (i = 0; i < nr; i++) {
388 bc_ds1620_set_data (value & 1);
389 bc_ds1620_set_clk (0);
390 udelay (1);
391 bc_ds1620_set_clk (1);
392 udelay (1);
393
394 value >>= 1;
395 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200396}
397
Wolfgang Denk610cf362006-05-03 01:24:04 +0200398static unsigned int ds1620_recv_bits (int nr)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200399{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200400 unsigned int value = 0, mask = 1;
401 int i;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200402
Wolfgang Denk610cf362006-05-03 01:24:04 +0200403 bc_ds1620_set_data (0);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200404
Wolfgang Denk610cf362006-05-03 01:24:04 +0200405 for (i = 0; i < nr; i++) {
406 bc_ds1620_set_clk (0);
407 udelay (1);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200408
Wolfgang Denk610cf362006-05-03 01:24:04 +0200409 if (bc_ds1620_get_data ())
410 value |= mask;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200411
Wolfgang Denk610cf362006-05-03 01:24:04 +0200412 mask <<= 1;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200413
Wolfgang Denk610cf362006-05-03 01:24:04 +0200414 bc_ds1620_set_clk (1);
415 udelay (1);
416 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200417
Wolfgang Denk610cf362006-05-03 01:24:04 +0200418 return value;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200419}
420
Wolfgang Denk610cf362006-05-03 01:24:04 +0200421static void ds1620_out (int cmd, int bits, int value)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200422{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200423 bc_ds1620_set_clk (1);
424 bc_ds1620_set_data_dir (0);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200425
Wolfgang Denk610cf362006-05-03 01:24:04 +0200426 bc_ds1620_set_reset (0);
427 udelay (1);
428 bc_ds1620_set_reset (1);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200429
Wolfgang Denk610cf362006-05-03 01:24:04 +0200430 udelay (1);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200431
Wolfgang Denk610cf362006-05-03 01:24:04 +0200432 ds1620_send_bits (8, cmd);
433 if (bits)
434 ds1620_send_bits (bits, value);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200435
Wolfgang Denk610cf362006-05-03 01:24:04 +0200436 udelay (1);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200437
Wolfgang Denk610cf362006-05-03 01:24:04 +0200438 /* go stand alone */
439 bc_ds1620_set_data_dir (1);
440 bc_ds1620_set_reset (0);
441 bc_ds1620_set_clk (0);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200442
Wolfgang Denk610cf362006-05-03 01:24:04 +0200443 udelay (10000);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200444}
445
Wolfgang Denk610cf362006-05-03 01:24:04 +0200446static unsigned int ds1620_in (int cmd, int bits)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200447{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200448 unsigned int value;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200449
Wolfgang Denk610cf362006-05-03 01:24:04 +0200450 bc_ds1620_set_clk (1);
451 bc_ds1620_set_data_dir (0);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200452
Wolfgang Denk610cf362006-05-03 01:24:04 +0200453 bc_ds1620_set_reset (0);
454 udelay (1);
455 bc_ds1620_set_reset (1);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200456
Wolfgang Denk610cf362006-05-03 01:24:04 +0200457 udelay (1);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200458
Wolfgang Denk610cf362006-05-03 01:24:04 +0200459 ds1620_send_bits (8, cmd);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200460
Wolfgang Denk610cf362006-05-03 01:24:04 +0200461 bc_ds1620_set_data_dir (1);
462 value = ds1620_recv_bits (bits);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200463
Wolfgang Denk610cf362006-05-03 01:24:04 +0200464 /* go stand alone */
465 bc_ds1620_set_data_dir (1);
466 bc_ds1620_set_reset (0);
467 bc_ds1620_set_clk (0);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200468
Wolfgang Denk610cf362006-05-03 01:24:04 +0200469 return value;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200470}
471
Wolfgang Denk610cf362006-05-03 01:24:04 +0200472static int cvt_9_to_int (unsigned int val)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200473{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200474 if (val & 0x100)
475 val |= 0xfffffe00;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200476
Wolfgang Denk610cf362006-05-03 01:24:04 +0200477 return val;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200478}
479
480/* set thermostate thresholds */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200481static void ds1620_write_state (struct therm *therm)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200482{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200483 ds1620_out (THERM_WRITE_TL, 9, therm->lo);
484 ds1620_out (THERM_WRITE_TH, 9, therm->hi);
485 ds1620_out (THERM_START_CONVERT, 0, 0);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200486}
487
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200488static int cmd_temp (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200489{
Wolfgang Denk610cf362006-05-03 01:24:04 +0200490 int i;
491 struct therm therm;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200492
Wolfgang Denk610cf362006-05-03 01:24:04 +0200493 sm501_gpio_init ();
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200494
Wolfgang Denk610cf362006-05-03 01:24:04 +0200495 /* print temperature */
496 if (argc == 1) {
497 i = cvt_9_to_int (ds1620_in (THERM_READ_TEMP, 9));
498 printf ("%d.%d C\n", i >> 1, i & 1 ? 5 : 0);
499 return 0;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200500 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200501
Wolfgang Denk610cf362006-05-03 01:24:04 +0200502 /* set to default operation */
503 if (strncmp (argv[1], "set", 3) == 0) {
504 if (strncmp (argv[2], "default", 3) == 0) {
505 therm.hi = +88;
506 therm.lo = -20;
507 therm.hi <<= 1;
508 therm.lo <<= 1;
509 ds1620_write_state (&therm);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200510 ds1620_out (THERM_WRITE_CONFIG, 8, CONFIG_SYS_STANDALONE);
Wolfgang Denk610cf362006-05-03 01:24:04 +0200511 return 0;
512 }
513 }
514
515 printf ("Usage:%s\n", cmdtp->help);
516 return 1;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200517}
518
Wolfgang Denk610cf362006-05-03 01:24:04 +0200519U_BOOT_CMD (temp, 3, 1, cmd_temp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600520 "print current temperature",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200521 "\n" "temp\n" " - print current temperature");
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200522
523#ifdef CONFIG_BC3450_CAN
524/*
525 * Initialise CAN interface
526 *
527 * return 1 on CAN initialization failure
528 * return 0 if no failure
529 */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200530int can_init (void)
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200531{
532 static int init_done = 0;
533 int i;
534 struct mpc5xxx_mscan *can1 =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200535 (struct mpc5xxx_mscan *) (CONFIG_SYS_MBAR + 0x0900);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200536 struct mpc5xxx_mscan *can2 =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200537 (struct mpc5xxx_mscan *) (CONFIG_SYS_MBAR + 0x0980);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200538
539 /* GPIO configuration of the CAN pins is done in BC3450.h */
540
541 if (!init_done) {
542 /* init CAN 1 */
543 can1->canctl1 |= 0x80; /* CAN enable */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200544 udelay (100);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200545
546 i = 0;
547 can1->canctl0 |= 0x02; /* sleep mode */
548 /* wait until sleep mode reached */
549 while (!(can1->canctl1 & 0x02)) {
Wolfgang Denk610cf362006-05-03 01:24:04 +0200550 udelay (10);
551 i++;
552 if (i == 10) {
553 printf ("%s: CAN1 initialize error, "
554 "can not enter sleep mode!\n",
555 __FUNCTION__);
556 return 1;
557 }
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200558 }
559 i = 0;
560 can1->canctl0 = 0x01; /* enter init mode */
561 /* wait until init mode reached */
562 while (!(can1->canctl1 & 0x01)) {
Wolfgang Denk610cf362006-05-03 01:24:04 +0200563 udelay (10);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200564 i++;
565 if (i == 10) {
566 printf ("%s: CAN1 initialize error, "
567 "can not enter init mode!\n",
568 __FUNCTION__);
569 return 1;
570 }
571 }
572 can1->canctl1 = 0x80;
573 can1->canctl1 |= 0x40;
574 can1->canbtr0 = 0x0F;
575 can1->canbtr1 = 0x7F;
576 can1->canidac &= ~(0x30);
577 can1->canidar1 = 0x00;
578 can1->canidar3 = 0x00;
579 can1->canidar5 = 0x00;
580 can1->canidar7 = 0x00;
581 can1->canidmr0 = 0xFF;
582 can1->canidmr1 = 0xFF;
583 can1->canidmr2 = 0xFF;
584 can1->canidmr3 = 0xFF;
585 can1->canidmr4 = 0xFF;
586 can1->canidmr5 = 0xFF;
587 can1->canidmr6 = 0xFF;
588 can1->canidmr7 = 0xFF;
589
590 i = 0;
591 can1->canctl0 &= ~(0x01); /* leave init mode */
592 can1->canctl0 &= ~(0x02);
593 /* wait until init and sleep mode left */
594 while ((can1->canctl1 & 0x01) || (can1->canctl1 & 0x02)) {
Wolfgang Denk610cf362006-05-03 01:24:04 +0200595 udelay (10);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200596 i++;
597 if (i == 10) {
598 printf ("%s: CAN1 initialize error, "
599 "can not leave init/sleep mode!\n",
600 __FUNCTION__);
601 return 1;
602 }
603 }
604
605 /* init CAN 2 */
606 can2->canctl1 |= 0x80; /* CAN enable */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200607 udelay (100);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200608
609 i = 0;
610 can2->canctl0 |= 0x02; /* sleep mode */
611 /* wait until sleep mode reached */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200612 while (!(can2->canctl1 & 0x02)) {
613 udelay (10);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200614 i++;
615 if (i == 10) {
616 printf ("%s: CAN2 initialize error, "
617 "can not enter sleep mode!\n",
618 __FUNCTION__);
619 return 1;
620 }
621 }
622 i = 0;
623 can2->canctl0 = 0x01; /* enter init mode */
624 /* wait until init mode reached */
Wolfgang Denk610cf362006-05-03 01:24:04 +0200625 while (!(can2->canctl1 & 0x01)) {
626 udelay (10);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200627 i++;
628 if (i == 10) {
629 printf ("%s: CAN2 initialize error, "
630 "can not enter init mode!\n",
631 __FUNCTION__);
632 return 1;
633 }
634 }
635 can2->canctl1 = 0x80;
636 can2->canctl1 |= 0x40;
637 can2->canbtr0 = 0x0F;
638 can2->canbtr1 = 0x7F;
639 can2->canidac &= ~(0x30);
640 can2->canidar1 = 0x00;
641 can2->canidar3 = 0x00;
642 can2->canidar5 = 0x00;
643 can2->canidar7 = 0x00;
644 can2->canidmr0 = 0xFF;
645 can2->canidmr1 = 0xFF;
646 can2->canidmr2 = 0xFF;
647 can2->canidmr3 = 0xFF;
648 can2->canidmr4 = 0xFF;
649 can2->canidmr5 = 0xFF;
650 can2->canidmr6 = 0xFF;
651 can2->canidmr7 = 0xFF;
652 can2->canctl0 &= ~(0x01); /* leave init mode */
653 can2->canctl0 &= ~(0x02);
654
655 i = 0;
656 /* wait until init mode left */
657 while ((can2->canctl1 & 0x01) || (can2->canctl1 & 0x02)) {
Wolfgang Denk610cf362006-05-03 01:24:04 +0200658 udelay (10);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200659 i++;
660 if (i == 10) {
661 printf ("%s: CAN2 initialize error, "
662 "can not leave init/sleep mode!\n",
663 __FUNCTION__);
664 return 1;
665 }
666 }
667 init_done = 1;
668 }
669 return 0;
670}
671
672/*
673 * Do CAN test
674 * by sending message between CAN1 and CAN2
675 *
676 * return 1 on CAN failure
677 * return 0 if no failure
678 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200679int do_can (char * const argv[])
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200680{
681 int i;
Wolfgang Denk610cf362006-05-03 01:24:04 +0200682 struct mpc5xxx_mscan *can1 =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200683 (struct mpc5xxx_mscan *) (CONFIG_SYS_MBAR + 0x0900);
Wolfgang Denk610cf362006-05-03 01:24:04 +0200684 struct mpc5xxx_mscan *can2 =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200685 (struct mpc5xxx_mscan *) (CONFIG_SYS_MBAR + 0x0980);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200686
687 /* send a message on CAN1 */
688 can1->cantbsel = 0x01;
689 can1->cantxfg.idr[0] = 0x55;
690 can1->cantxfg.idr[1] = 0x00;
691 can1->cantxfg.idr[1] &= ~0x8;
692 can1->cantxfg.idr[1] &= ~0x10;
693 can1->cantxfg.dsr[0] = 0xCC;
694 can1->cantxfg.dlr = 1;
695 can1->cantxfg.tbpr = 0;
696 can1->cantflg = 0x01;
697
698 i = 0;
699 while ((can1->cantflg & 0x01) == 0) {
700 i++;
701 if (i == 10) {
702 printf ("%s: CAN1 send timeout, "
Wolfgang Denk610cf362006-05-03 01:24:04 +0200703 "can not send message!\n", __FUNCTION__);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200704 return 1;
705 }
Wolfgang Denk610cf362006-05-03 01:24:04 +0200706 udelay (1000);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200707 }
Wolfgang Denk610cf362006-05-03 01:24:04 +0200708 udelay (1000);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200709
710 i = 0;
Wolfgang Denk610cf362006-05-03 01:24:04 +0200711 while (!(can2->canrflg & 0x01)) {
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200712 i++;
713 if (i == 10) {
714 printf ("%s: CAN2 receive timeout, "
Wolfgang Denk610cf362006-05-03 01:24:04 +0200715 "no message received!\n", __FUNCTION__);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200716 return 1;
717 }
Wolfgang Denk610cf362006-05-03 01:24:04 +0200718 udelay (1000);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200719 }
Wolfgang Denk610cf362006-05-03 01:24:04 +0200720
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200721 if (can2->canrxfg.dsr[0] != 0xCC) {
722 printf ("%s: CAN2 receive error, "
Wolfgang Denk610cf362006-05-03 01:24:04 +0200723 "data mismatch!\n", __FUNCTION__);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200724 return 1;
725 }
726
727 /* send a message on CAN2 */
728 can2->cantbsel = 0x01;
729 can2->cantxfg.idr[0] = 0x55;
730 can2->cantxfg.idr[1] = 0x00;
731 can2->cantxfg.idr[1] &= ~0x8;
732 can2->cantxfg.idr[1] &= ~0x10;
733 can2->cantxfg.dsr[0] = 0xCC;
734 can2->cantxfg.dlr = 1;
735 can2->cantxfg.tbpr = 0;
736 can2->cantflg = 0x01;
737
738 i = 0;
739 while ((can2->cantflg & 0x01) == 0) {
740 i++;
741 if (i == 10) {
742 printf ("%s: CAN2 send error, "
Wolfgang Denk610cf362006-05-03 01:24:04 +0200743 "can not send message!\n", __FUNCTION__);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200744 return 1;
745 }
Wolfgang Denk610cf362006-05-03 01:24:04 +0200746 udelay (1000);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200747 }
Wolfgang Denk610cf362006-05-03 01:24:04 +0200748 udelay (1000);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200749
750 i = 0;
Wolfgang Denk610cf362006-05-03 01:24:04 +0200751 while (!(can1->canrflg & 0x01)) {
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200752 i++;
753 if (i == 10) {
754 printf ("%s: CAN1 receive timeout, "
Wolfgang Denk610cf362006-05-03 01:24:04 +0200755 "no message received!\n", __FUNCTION__);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200756 return 1;
757 }
Wolfgang Denk610cf362006-05-03 01:24:04 +0200758 udelay (1000);
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200759 }
760
761 if (can1->canrxfg.dsr[0] != 0xCC) {
762 printf ("%s: CAN1 receive error 0x%02x\n",
763 __FUNCTION__, (can1->canrxfg.dsr[0]));
764 return 1;
765 }
766
767 return 0;
768}
769#endif /* CONFIG_BC3450_CAN */
770
771/*
772 * test - BC3450 HW test routines
773 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200774int cmd_test (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200775{
776#ifdef CONFIG_BC3450_CAN
Wolfgang Denk610cf362006-05-03 01:24:04 +0200777 int rcode;
778
779 can_init ();
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200780#endif /* CONFIG_BC3450_CAN */
781
Wolfgang Denk610cf362006-05-03 01:24:04 +0200782 sm501_gpio_init ();
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200783
Wolfgang Denk610cf362006-05-03 01:24:04 +0200784 if (argc != 2) {
785 printf ("Usage:%s\n", cmdtp->help);
786 return 1;
787 }
788
789 if (strncmp (argv[1], "unit-off", 8) == 0) {
790 printf ("waiting 2 seconds...\n");
791 udelay (2000000);
792 *(vu_long *) (SM501_MMIO_BASE + SM501_GPIO_DATA_HIGH) &=
793 ~PWR_OFF;
794 return 0;
795 }
796#ifdef CONFIG_BC3450_CAN
797 else if (strncmp (argv[1], "can", 2) == 0) {
798 rcode = do_can (argv);
799 if (simple_strtoul (argv[2], NULL, 10) == 2) {
800 if (rcode == 0)
801 printf ("OK\n");
802 else
803 printf ("Error\n");
804 }
805 return rcode;
806 }
807#endif /* CONFIG_BC3450_CAN */
808
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200809 printf ("Usage:%s\n", cmdtp->help);
810 return 1;
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200811}
812
Peter Tyser2fb26042009-01-27 18:03:12 -0600813U_BOOT_CMD (test, 2, 1, cmd_test, "unit test routines", "\n"
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200814#ifdef CONFIG_BC3450_CAN
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200815 "test can\n"
816 " - connect CAN1 (X8) with CAN2 (X9) for this test\n"
dzu@denx.de6ca24c62006-04-21 18:30:47 +0200817#endif /* CONFIG_BC3450_CAN */
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200818 "test unit-off\n"
819 " - turns off the BC3450 unit\n"
820 " WARNING: Unsaved environment variables will be lost!"
821);
Jon Loeliger77a31852007-07-10 10:39:10 -0500822#endif