blob: 1c01bb45b55bb185363d8a8b343fa1d90564392d [file] [log] [blame]
Detlev Zundel27e2e472009-03-30 00:31:34 +02001/*
2 * (C) Copyright 2008, 2009 Andreas Pfefferle,
3 * DENX Software Engineering, ap@denx.de.
4 * (C) Copyright 2009 Detlev Zundel,
5 * DENX Software Engineering, dzu@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <asm/io.h>
27#include <common.h>
28#include <config.h>
29#include <mpc5xxx.h>
30#include <pci.h>
31
32#include <command.h>
33
34/* This is needed for the includes in ns16550.h */
35#define CONFIG_SYS_NS16550_REG_SIZE 1
36#include <ns16550.h>
37
38#define GPIO_BASE ((u_char *)CONFIG_SYS_CS3_START)
39
40#define DIGIN_TOUCHSCR_MASK 0x00003000 /* Inputs 12-13 */
41#define DIGIN_KEYB_MASK 0x00010000 /* Input 16 */
42
43#define DIGIN_DRAWER_SW1 0x00400000 /* Input 22 */
44#define DIGIN_DRAWER_SW2 0x00800000 /* Input 23 */
45
46#define DIGIO_LED0 0x00000001 /* Output 0 */
47#define DIGIO_LED1 0x00000002 /* Output 1 */
48#define DIGIO_LED2 0x00000004 /* Output 2 */
49#define DIGIO_LED3 0x00000008 /* Output 3 */
50#define DIGIO_LED4 0x00000010 /* Output 4 */
51#define DIGIO_LED5 0x00000020 /* Output 5 */
52
53#define DIGIO_DRAWER1 0x00000100 /* Output 8 */
54#define DIGIO_DRAWER2 0x00000200 /* Output 9 */
55
56#define SERIAL_PORT_BASE ((u_char *)CONFIG_SYS_CS2_START)
57
58#define PSC_OP1_RTS 0x01
59#define PSC_OP0_RTS 0x01
60
61/*
62 * Table with supported baudrates (defined in inka4x0.h)
63 */
64static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
65#define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
66
67static unsigned int inka_digin_get_input(void)
68{
69 return in_8(GPIO_BASE + 0) << 0 | in_8(GPIO_BASE + 1) << 8 |
70 in_8(GPIO_BASE + 2) << 16 | in_8(GPIO_BASE + 3) << 24;
71}
72
73#define LED_HIGH(NUM) \
74 do { \
75 setbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
76 } while (0)
77
78#define LED_LOW(NUM) \
79 do { \
80 clrbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
81 } while (0)
82
83#define CHECK_LED(NUM) \
84 do { \
85 if (state & (1 << NUM)) { \
86 LED_HIGH(NUM); \
87 } else { \
88 LED_LOW(NUM); \
89 } \
90 } while (0)
91
92static void inka_digio_set_output(unsigned int state, int which)
93{
94 volatile struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
95
96 if (which == 0) {
97 /* other */
98 CHECK_LED(0);
99 CHECK_LED(1);
100 CHECK_LED(2);
101 CHECK_LED(3);
102 CHECK_LED(4);
103 CHECK_LED(5);
104 } else {
105 if (which == 1) {
106 /* drawer1 */
107 if (state) {
108 clrbits_be32(&gpio->simple_dvo, 0x1000);
109 udelay(1);
110 setbits_be32(&gpio->simple_dvo, 0x1000);
111 } else {
112 setbits_be32(&gpio->simple_dvo, 0x1000);
113 udelay(1);
114 clrbits_be32(&gpio->simple_dvo, 0x1000);
115 }
116 }
117 if (which == 2) {
118 /* drawer 2 */
119 if (state) {
120 clrbits_be32(&gpio->simple_dvo, 0x2000);
121 udelay(1);
122 setbits_be32(&gpio->simple_dvo, 0x2000);
123 } else {
124 setbits_be32(&gpio->simple_dvo, 0x2000);
125 udelay(1);
126 clrbits_be32(&gpio->simple_dvo, 0x2000);
127 }
128 }
129 }
130 udelay(1);
131}
132
133static int do_inkadiag_io(cmd_tbl_t *cmdtp, int flag, int argc,
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200134 char * const argv[]) {
Detlev Zundel27e2e472009-03-30 00:31:34 +0200135 unsigned int state, val;
136
137 switch (argc) {
138 case 3:
139 /* Write a value */
140 val = simple_strtol(argv[2], NULL, 16);
141
142 if (strcmp(argv[1], "drawer1") == 0) {
143 inka_digio_set_output(val, 1);
144 } else if (strcmp(argv[1], "drawer2") == 0) {
145 inka_digio_set_output(val, 2);
146 } else if (strcmp(argv[1], "other") == 0)
147 inka_digio_set_output(val, 0);
148 else {
149 printf("Invalid argument: %s\n", argv[1]);
150 return -1;
151 }
152 /* fall through */
153 case 2:
154 /* Read a value */
155 state = inka_digin_get_input();
156
157 if (strcmp(argv[1], "drawer1") == 0) {
158 val = (state & DIGIN_DRAWER_SW1) >> (ffs(DIGIN_DRAWER_SW1) - 1);
159 } else if (strcmp(argv[1], "drawer2") == 0) {
160 val = (state & DIGIN_DRAWER_SW2) >> (ffs(DIGIN_DRAWER_SW2) - 1);
161 } else if (strcmp(argv[1], "other") == 0) {
162 val = ((state & DIGIN_KEYB_MASK) >> (ffs(DIGIN_KEYB_MASK) - 1))
163 | (state & DIGIN_TOUCHSCR_MASK) >> (ffs(DIGIN_TOUCHSCR_MASK) - 2);
164 } else {
165 printf("Invalid argument: %s\n", argv[1]);
166 return -1;
167 }
168 printf("exit code: 0x%X\n", val);
169 return 0;
170 default:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200171 return cmd_usage(cmdtp);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200172 }
173
174 return -1;
175}
176
177DECLARE_GLOBAL_DATA_PTR;
178
179static int ser_init(volatile struct mpc5xxx_psc *psc, int baudrate)
180{
181 unsigned long baseclk;
182 int div;
183
184 /* reset PSC */
185 out_8(&psc->command, PSC_SEL_MODE_REG_1);
186
187 /* select clock sources */
188
189 out_be16(&psc->psc_clock_select, 0);
Simon Glass4f8c5f02012-12-13 20:48:53 +0000190 baseclk = (gd->arch.ipb_clk + 16) / 32;
Detlev Zundel27e2e472009-03-30 00:31:34 +0200191
192 /* switch to UART mode */
193 out_be32(&psc->sicr, 0);
194
195 /* configure parity, bit length and so on */
196
197 out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
198 out_8(&psc->mode, PSC_MODE_ONE_STOP);
199
200 /* set up UART divisor */
201 div = (baseclk + (baudrate / 2)) / baudrate;
202 out_8(&psc->ctur, (div >> 8) & 0xff);
203 out_8(&psc->ctlr, div & 0xff);
204
205 /* disable all interrupts */
206 out_be16(&psc->psc_imr, 0);
207
208 /* reset and enable Rx/Tx */
209 out_8(&psc->command, PSC_RST_RX);
210 out_8(&psc->command, PSC_RST_TX);
211 out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
212
213 return 0;
214}
215
216static void ser_putc(volatile struct mpc5xxx_psc *psc, const char c)
217{
218 /* Wait 1 second for last character to go. */
219 int i = 0;
220
221 while (!(psc->psc_status & PSC_SR_TXEMP) && (i++ < 1000000/10))
222 udelay(10);
223 psc->psc_buffer_8 = c;
224
225}
226
227static int ser_getc(volatile struct mpc5xxx_psc *psc)
228{
229 /* Wait for a character to arrive. */
230 int i = 0;
231
232 while (!(in_be16(&psc->psc_status) & PSC_SR_RXRDY) && (i++ < 1000000/10))
233 udelay(10);
234
235 return in_8(&psc->psc_buffer_8);
236}
237
238static int do_inkadiag_serial(cmd_tbl_t *cmdtp, int flag, int argc,
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200239 char * const argv[]) {
Detlev Zundel27e2e472009-03-30 00:31:34 +0200240 volatile struct NS16550 *uart;
241 volatile struct mpc5xxx_psc *psc;
242 unsigned int num, mode;
243 int combrd, baudrate, i, j, len;
244 int address;
245
Wolfgang Denk3b683112010-07-17 01:06:04 +0200246 if (argc < 5)
247 return cmd_usage(cmdtp);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200248
249 argc--;
250 argv++;
251
252 num = simple_strtol(argv[0], NULL, 0);
253 if (num < 0 || num > 11) {
254 printf("invalid argument for num: %d\n", num);
255 return -1;
256 }
257
258 mode = simple_strtol(argv[1], NULL, 0);
259
260 combrd = 0;
261 baudrate = simple_strtoul(argv[2], NULL, 10);
262 for (i=0; i<N_BAUDRATES; ++i) {
263 if (baudrate == baudrate_table[i])
264 break;
265 }
266 if (i == N_BAUDRATES) {
267 printf("## Baudrate %d bps not supported\n",
268 baudrate);
269 return 1;
270 }
271 combrd = 115200 / baudrate;
272
273 uart = (struct NS16550 *)(SERIAL_PORT_BASE + (num << 3));
274
275 printf("Testing uart %d.\n\n", num);
276
277 if ((num >= 0) && (num <= 7)) {
278 if (mode & 1) {
279 /* turn on 'loopback' mode */
Detlev Zundel166fb542009-04-03 11:53:01 +0200280 out_8(&uart->mcr, UART_MCR_LOOP);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200281 } else {
282 /*
283 * establish the UART's operational parameters
284 * set DLAB=1, so rbr accesses DLL
285 */
Detlev Zundel166fb542009-04-03 11:53:01 +0200286 out_8(&uart->lcr, UART_LCR_DLAB);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200287 /* set baudrate */
288 out_8(&uart->rbr, combrd);
289 /* set data-format: 8-N-1 */
Detlev Zundel166fb542009-04-03 11:53:01 +0200290 out_8(&uart->lcr, UART_LCR_WLS_8);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200291 }
292
293 if (mode & 2) {
294 /* set request to send */
Detlev Zundel166fb542009-04-03 11:53:01 +0200295 out_8(&uart->mcr, UART_MCR_RTS);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200296 udelay(10);
297 /* check clear to send */
Detlev Zundel166fb542009-04-03 11:53:01 +0200298 if ((in_8(&uart->msr) & UART_MSR_CTS) == 0x00)
Detlev Zundel27e2e472009-03-30 00:31:34 +0200299 return -1;
300 }
301 if (mode & 4) {
302 /* set data terminal ready */
Detlev Zundel166fb542009-04-03 11:53:01 +0200303 out_8(&uart->mcr, UART_MCR_DTR);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200304 udelay(10);
305 /* check data set ready and carrier detect */
Detlev Zundel166fb542009-04-03 11:53:01 +0200306 if ((in_8(&uart->msr) & (UART_MSR_DSR | UART_MSR_DCD))
307 != (UART_MSR_DSR | UART_MSR_DCD))
Detlev Zundel27e2e472009-03-30 00:31:34 +0200308 return -1;
309 }
310
311 /* write each message-character, read it back, and display it */
312 for (i = 0, len = strlen(argv[3]); i < len; ++i) {
313 j = 0;
Detlev Zundel166fb542009-04-03 11:53:01 +0200314 while ((in_8(&uart->lsr) & UART_LSR_THRE) == 0x00) {
Detlev Zundel27e2e472009-03-30 00:31:34 +0200315 if (j++ > CONFIG_SYS_HZ)
316 break;
317 udelay(10);
318 }
319 out_8(&uart->rbr, argv[3][i]);
320 j = 0;
Detlev Zundel166fb542009-04-03 11:53:01 +0200321 while ((in_8(&uart->lsr) & UART_LSR_DR) == 0x00) {
Detlev Zundel27e2e472009-03-30 00:31:34 +0200322 if (j++ > CONFIG_SYS_HZ)
323 break;
324 udelay(10);
325 }
326 printf("%c", in_8(&uart->rbr));
327 }
328 printf("\n\n");
329 out_8(&uart->mcr, 0x00);
330 } else {
331 address = 0;
332
333 switch (num) {
334 case 8:
335 address = MPC5XXX_PSC6;
336 break;
337 case 9:
338 address = MPC5XXX_PSC3;
339 break;
340 case 10:
341 address = MPC5XXX_PSC2;
342 break;
343 case 11:
344 address = MPC5XXX_PSC1;
345 break;
346 }
347 psc = (struct mpc5xxx_psc *)address;
348 ser_init(psc, simple_strtol(argv[2], NULL, 0));
349 if (mode & 2) {
350 /* set request to send */
351 out_8(&psc->op0, PSC_OP0_RTS);
352 udelay(10);
353 /* check clear to send */
354 if ((in_8(&psc->ip) & PSC_IPCR_CTS) == 0)
355 return -1;
356 }
357 len = strlen(argv[3]);
358 for (i = 0; i < len; ++i) {
359 ser_putc(psc, argv[3][i]);
360 printf("%c", ser_getc(psc));
361 }
362 printf("\n\n");
363 }
364 return 0;
365}
366
367#define BUZZER_GPT (MPC5XXX_GPT + 0x60) /* GPT6 */
368static void buzzer_turn_on(unsigned int freq)
369{
370 volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
371
Simon Glass4f8c5f02012-12-13 20:48:53 +0000372 const u32 prescale = gd->arch.ipb_clk / freq / 128;
Detlev Zundel27e2e472009-03-30 00:31:34 +0200373 const u32 count = 128;
374 const u32 width = 64;
375
376 gpt->cir = (prescale << 16) | count;
377 gpt->pwmcr = width << 16;
378 gpt->emsr = 3; /* Timer enabled for PWM */
379}
380
381static void buzzer_turn_off(void)
382{
383 volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
384
385 gpt->emsr = 0;
386}
387
388static int do_inkadiag_buzzer(cmd_tbl_t *cmdtp, int flag, int argc,
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200389 char * const argv[]) {
Detlev Zundel27e2e472009-03-30 00:31:34 +0200390
391 unsigned int period, freq;
392 int prev, i;
393
Wolfgang Denk3b683112010-07-17 01:06:04 +0200394 if (argc != 3)
395 return cmd_usage(cmdtp);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200396
397 argc--;
398 argv++;
399
400 period = simple_strtol(argv[0], NULL, 0);
401 if (!period)
402 printf("Zero period is senseless\n");
403 argc--;
404 argv++;
405
406 freq = simple_strtol(argv[0], NULL, 0);
407 /* avoid zero prescale in buzzer_turn_on() */
Simon Glass4f8c5f02012-12-13 20:48:53 +0000408 if (freq > gd->arch.ipb_clk / 128) {
Detlev Zundel27e2e472009-03-30 00:31:34 +0200409 printf("%dHz exceeds maximum (%ldHz)\n", freq,
Simon Glass4f8c5f02012-12-13 20:48:53 +0000410 gd->arch.ipb_clk / 128);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200411 } else if (!freq)
412 printf("Zero frequency is senseless\n");
413 else
414 buzzer_turn_on(freq);
415
416 clear_ctrlc();
417 prev = disable_ctrlc(0);
418
419 printf("Buzzing for %d ms. Type ^C to abort!\n\n", period);
420
421 i = 0;
422 while (!ctrlc() && (i++ < CONFIG_SYS_HZ))
423 udelay(period);
424
425 clear_ctrlc();
426 disable_ctrlc(prev);
427
428 buzzer_turn_off();
429
430 return 0;
431}
432
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200433static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200434
435cmd_tbl_t cmd_inkadiag_sub[] = {
436 U_BOOT_CMD_MKENT(io, 1, 1, do_inkadiag_io, "read digital input",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200437 "<drawer1|drawer2|other> [value] - get or set specified signal"),
Detlev Zundel27e2e472009-03-30 00:31:34 +0200438 U_BOOT_CMD_MKENT(serial, 4, 1, do_inkadiag_serial, "test serial port",
439 "<num> <mode> <baudrate> <msg> - test uart num [0..11] in mode\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200440 "and baudrate with msg"),
Detlev Zundel27e2e472009-03-30 00:31:34 +0200441 U_BOOT_CMD_MKENT(buzzer, 2, 1, do_inkadiag_buzzer, "activate buzzer",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200442 "<period> <freq> - turn buzzer on for period ms with freq hz"),
Detlev Zundel27e2e472009-03-30 00:31:34 +0200443 U_BOOT_CMD_MKENT(help, 4, 1, do_inkadiag_help, "get help",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200444 "[command] - get help for command"),
Detlev Zundel27e2e472009-03-30 00:31:34 +0200445};
446
447static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200448 int argc, char * const argv[]) {
Detlev Zundel27e2e472009-03-30 00:31:34 +0200449 extern int _do_help (cmd_tbl_t *cmd_start, int cmd_items,
450 cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200451 int argc, char * const argv[]);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200452 /* do_help prints command name - we prepend inkadiag to our subcommands! */
453#ifdef CONFIG_SYS_LONGHELP
454 puts ("inkadiag ");
455#endif
456 return _do_help(&cmd_inkadiag_sub[0],
457 ARRAY_SIZE(cmd_inkadiag_sub), cmdtp, flag, argc, argv);
458}
459
460static int do_inkadiag(cmd_tbl_t *cmdtp, int flag, int argc,
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200461 char * const argv[]) {
Detlev Zundel27e2e472009-03-30 00:31:34 +0200462 cmd_tbl_t *c;
463
464 c = find_cmd_tbl(argv[1], &cmd_inkadiag_sub[0], ARRAY_SIZE(cmd_inkadiag_sub));
465
466 if (c) {
467 argc--;
468 argv++;
469 return c->cmd(c, flag, argc, argv);
470 } else {
471 /* Unrecognized command */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200472 return cmd_usage(cmdtp);
Detlev Zundel27e2e472009-03-30 00:31:34 +0200473 }
474}
475
476U_BOOT_CMD(inkadiag, 6, 1, do_inkadiag,
477 "inkadiag - inka diagnosis\n",
478 "[inkadiag what ...]\n"
479 " - perform a diagnosis on inka hardware\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200480 "'inkadiag' performs hardware tests.");