blob: 0bd12ece86b0a07553560b4ec87e47071daabaf7 [file] [log] [blame]
Detlev Zundel7b5611c2009-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Detlev Zundel7b5611c2009-03-30 00:31:34 +02008 */
9
10#include <asm/io.h>
11#include <common.h>
12#include <config.h>
13#include <mpc5xxx.h>
14#include <pci.h>
15
16#include <command.h>
17
18/* This is needed for the includes in ns16550.h */
19#define CONFIG_SYS_NS16550_REG_SIZE 1
20#include <ns16550.h>
21
22#define GPIO_BASE ((u_char *)CONFIG_SYS_CS3_START)
23
24#define DIGIN_TOUCHSCR_MASK 0x00003000 /* Inputs 12-13 */
25#define DIGIN_KEYB_MASK 0x00010000 /* Input 16 */
26
27#define DIGIN_DRAWER_SW1 0x00400000 /* Input 22 */
28#define DIGIN_DRAWER_SW2 0x00800000 /* Input 23 */
29
30#define DIGIO_LED0 0x00000001 /* Output 0 */
31#define DIGIO_LED1 0x00000002 /* Output 1 */
32#define DIGIO_LED2 0x00000004 /* Output 2 */
33#define DIGIO_LED3 0x00000008 /* Output 3 */
34#define DIGIO_LED4 0x00000010 /* Output 4 */
35#define DIGIO_LED5 0x00000020 /* Output 5 */
36
37#define DIGIO_DRAWER1 0x00000100 /* Output 8 */
38#define DIGIO_DRAWER2 0x00000200 /* Output 9 */
39
40#define SERIAL_PORT_BASE ((u_char *)CONFIG_SYS_CS2_START)
41
42#define PSC_OP1_RTS 0x01
43#define PSC_OP0_RTS 0x01
44
45/*
46 * Table with supported baudrates (defined in inka4x0.h)
47 */
48static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
49#define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
50
51static unsigned int inka_digin_get_input(void)
52{
53 return in_8(GPIO_BASE + 0) << 0 | in_8(GPIO_BASE + 1) << 8 |
54 in_8(GPIO_BASE + 2) << 16 | in_8(GPIO_BASE + 3) << 24;
55}
56
57#define LED_HIGH(NUM) \
58 do { \
59 setbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
60 } while (0)
61
62#define LED_LOW(NUM) \
63 do { \
64 clrbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
65 } while (0)
66
67#define CHECK_LED(NUM) \
68 do { \
69 if (state & (1 << NUM)) { \
70 LED_HIGH(NUM); \
71 } else { \
72 LED_LOW(NUM); \
73 } \
74 } while (0)
75
76static void inka_digio_set_output(unsigned int state, int which)
77{
78 volatile struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
79
80 if (which == 0) {
81 /* other */
82 CHECK_LED(0);
83 CHECK_LED(1);
84 CHECK_LED(2);
85 CHECK_LED(3);
86 CHECK_LED(4);
87 CHECK_LED(5);
88 } else {
89 if (which == 1) {
90 /* drawer1 */
91 if (state) {
92 clrbits_be32(&gpio->simple_dvo, 0x1000);
93 udelay(1);
94 setbits_be32(&gpio->simple_dvo, 0x1000);
95 } else {
96 setbits_be32(&gpio->simple_dvo, 0x1000);
97 udelay(1);
98 clrbits_be32(&gpio->simple_dvo, 0x1000);
99 }
100 }
101 if (which == 2) {
102 /* drawer 2 */
103 if (state) {
104 clrbits_be32(&gpio->simple_dvo, 0x2000);
105 udelay(1);
106 setbits_be32(&gpio->simple_dvo, 0x2000);
107 } else {
108 setbits_be32(&gpio->simple_dvo, 0x2000);
109 udelay(1);
110 clrbits_be32(&gpio->simple_dvo, 0x2000);
111 }
112 }
113 }
114 udelay(1);
115}
116
117static int do_inkadiag_io(cmd_tbl_t *cmdtp, int flag, int argc,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200118 char * const argv[]) {
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200119 unsigned int state, val;
120
121 switch (argc) {
122 case 3:
123 /* Write a value */
124 val = simple_strtol(argv[2], NULL, 16);
125
126 if (strcmp(argv[1], "drawer1") == 0) {
127 inka_digio_set_output(val, 1);
128 } else if (strcmp(argv[1], "drawer2") == 0) {
129 inka_digio_set_output(val, 2);
130 } else if (strcmp(argv[1], "other") == 0)
131 inka_digio_set_output(val, 0);
132 else {
133 printf("Invalid argument: %s\n", argv[1]);
134 return -1;
135 }
136 /* fall through */
137 case 2:
138 /* Read a value */
139 state = inka_digin_get_input();
140
141 if (strcmp(argv[1], "drawer1") == 0) {
142 val = (state & DIGIN_DRAWER_SW1) >> (ffs(DIGIN_DRAWER_SW1) - 1);
143 } else if (strcmp(argv[1], "drawer2") == 0) {
144 val = (state & DIGIN_DRAWER_SW2) >> (ffs(DIGIN_DRAWER_SW2) - 1);
145 } else if (strcmp(argv[1], "other") == 0) {
146 val = ((state & DIGIN_KEYB_MASK) >> (ffs(DIGIN_KEYB_MASK) - 1))
147 | (state & DIGIN_TOUCHSCR_MASK) >> (ffs(DIGIN_TOUCHSCR_MASK) - 2);
148 } else {
149 printf("Invalid argument: %s\n", argv[1]);
150 return -1;
151 }
152 printf("exit code: 0x%X\n", val);
153 return 0;
154 default:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200155 return cmd_usage(cmdtp);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200156 }
157
158 return -1;
159}
160
161DECLARE_GLOBAL_DATA_PTR;
162
163static int ser_init(volatile struct mpc5xxx_psc *psc, int baudrate)
164{
165 unsigned long baseclk;
166 int div;
167
168 /* reset PSC */
169 out_8(&psc->command, PSC_SEL_MODE_REG_1);
170
171 /* select clock sources */
172
173 out_be16(&psc->psc_clock_select, 0);
Simon Glassb2877492012-12-13 20:48:53 +0000174 baseclk = (gd->arch.ipb_clk + 16) / 32;
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200175
176 /* switch to UART mode */
177 out_be32(&psc->sicr, 0);
178
179 /* configure parity, bit length and so on */
180
181 out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
182 out_8(&psc->mode, PSC_MODE_ONE_STOP);
183
184 /* set up UART divisor */
185 div = (baseclk + (baudrate / 2)) / baudrate;
186 out_8(&psc->ctur, (div >> 8) & 0xff);
187 out_8(&psc->ctlr, div & 0xff);
188
189 /* disable all interrupts */
190 out_be16(&psc->psc_imr, 0);
191
192 /* reset and enable Rx/Tx */
193 out_8(&psc->command, PSC_RST_RX);
194 out_8(&psc->command, PSC_RST_TX);
195 out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
196
197 return 0;
198}
199
200static void ser_putc(volatile struct mpc5xxx_psc *psc, const char c)
201{
202 /* Wait 1 second for last character to go. */
203 int i = 0;
204
205 while (!(psc->psc_status & PSC_SR_TXEMP) && (i++ < 1000000/10))
206 udelay(10);
207 psc->psc_buffer_8 = c;
208
209}
210
211static int ser_getc(volatile struct mpc5xxx_psc *psc)
212{
213 /* Wait for a character to arrive. */
214 int i = 0;
215
216 while (!(in_be16(&psc->psc_status) & PSC_SR_RXRDY) && (i++ < 1000000/10))
217 udelay(10);
218
219 return in_8(&psc->psc_buffer_8);
220}
221
222static int do_inkadiag_serial(cmd_tbl_t *cmdtp, int flag, int argc,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200223 char * const argv[]) {
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200224 volatile struct NS16550 *uart;
225 volatile struct mpc5xxx_psc *psc;
226 unsigned int num, mode;
227 int combrd, baudrate, i, j, len;
228 int address;
229
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200230 if (argc < 5)
231 return cmd_usage(cmdtp);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200232
233 argc--;
234 argv++;
235
236 num = simple_strtol(argv[0], NULL, 0);
237 if (num < 0 || num > 11) {
238 printf("invalid argument for num: %d\n", num);
239 return -1;
240 }
241
242 mode = simple_strtol(argv[1], NULL, 0);
243
244 combrd = 0;
245 baudrate = simple_strtoul(argv[2], NULL, 10);
246 for (i=0; i<N_BAUDRATES; ++i) {
247 if (baudrate == baudrate_table[i])
248 break;
249 }
250 if (i == N_BAUDRATES) {
251 printf("## Baudrate %d bps not supported\n",
252 baudrate);
253 return 1;
254 }
255 combrd = 115200 / baudrate;
256
257 uart = (struct NS16550 *)(SERIAL_PORT_BASE + (num << 3));
258
259 printf("Testing uart %d.\n\n", num);
260
261 if ((num >= 0) && (num <= 7)) {
262 if (mode & 1) {
263 /* turn on 'loopback' mode */
Detlev Zundel200779e2009-04-03 11:53:01 +0200264 out_8(&uart->mcr, UART_MCR_LOOP);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200265 } else {
266 /*
267 * establish the UART's operational parameters
268 * set DLAB=1, so rbr accesses DLL
269 */
Detlev Zundel200779e2009-04-03 11:53:01 +0200270 out_8(&uart->lcr, UART_LCR_DLAB);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200271 /* set baudrate */
272 out_8(&uart->rbr, combrd);
273 /* set data-format: 8-N-1 */
Detlev Zundel200779e2009-04-03 11:53:01 +0200274 out_8(&uart->lcr, UART_LCR_WLS_8);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200275 }
276
277 if (mode & 2) {
278 /* set request to send */
Detlev Zundel200779e2009-04-03 11:53:01 +0200279 out_8(&uart->mcr, UART_MCR_RTS);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200280 udelay(10);
281 /* check clear to send */
Detlev Zundel200779e2009-04-03 11:53:01 +0200282 if ((in_8(&uart->msr) & UART_MSR_CTS) == 0x00)
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200283 return -1;
284 }
285 if (mode & 4) {
286 /* set data terminal ready */
Detlev Zundel200779e2009-04-03 11:53:01 +0200287 out_8(&uart->mcr, UART_MCR_DTR);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200288 udelay(10);
289 /* check data set ready and carrier detect */
Detlev Zundel200779e2009-04-03 11:53:01 +0200290 if ((in_8(&uart->msr) & (UART_MSR_DSR | UART_MSR_DCD))
291 != (UART_MSR_DSR | UART_MSR_DCD))
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200292 return -1;
293 }
294
295 /* write each message-character, read it back, and display it */
296 for (i = 0, len = strlen(argv[3]); i < len; ++i) {
297 j = 0;
Detlev Zundel200779e2009-04-03 11:53:01 +0200298 while ((in_8(&uart->lsr) & UART_LSR_THRE) == 0x00) {
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200299 if (j++ > CONFIG_SYS_HZ)
300 break;
301 udelay(10);
302 }
303 out_8(&uart->rbr, argv[3][i]);
304 j = 0;
Detlev Zundel200779e2009-04-03 11:53:01 +0200305 while ((in_8(&uart->lsr) & UART_LSR_DR) == 0x00) {
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200306 if (j++ > CONFIG_SYS_HZ)
307 break;
308 udelay(10);
309 }
310 printf("%c", in_8(&uart->rbr));
311 }
312 printf("\n\n");
313 out_8(&uart->mcr, 0x00);
314 } else {
315 address = 0;
316
317 switch (num) {
318 case 8:
319 address = MPC5XXX_PSC6;
320 break;
321 case 9:
322 address = MPC5XXX_PSC3;
323 break;
324 case 10:
325 address = MPC5XXX_PSC2;
326 break;
327 case 11:
328 address = MPC5XXX_PSC1;
329 break;
330 }
331 psc = (struct mpc5xxx_psc *)address;
332 ser_init(psc, simple_strtol(argv[2], NULL, 0));
333 if (mode & 2) {
334 /* set request to send */
335 out_8(&psc->op0, PSC_OP0_RTS);
336 udelay(10);
337 /* check clear to send */
338 if ((in_8(&psc->ip) & PSC_IPCR_CTS) == 0)
339 return -1;
340 }
341 len = strlen(argv[3]);
342 for (i = 0; i < len; ++i) {
343 ser_putc(psc, argv[3][i]);
344 printf("%c", ser_getc(psc));
345 }
346 printf("\n\n");
347 }
348 return 0;
349}
350
351#define BUZZER_GPT (MPC5XXX_GPT + 0x60) /* GPT6 */
352static void buzzer_turn_on(unsigned int freq)
353{
354 volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
355
Simon Glassb2877492012-12-13 20:48:53 +0000356 const u32 prescale = gd->arch.ipb_clk / freq / 128;
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200357 const u32 count = 128;
358 const u32 width = 64;
359
360 gpt->cir = (prescale << 16) | count;
361 gpt->pwmcr = width << 16;
362 gpt->emsr = 3; /* Timer enabled for PWM */
363}
364
365static void buzzer_turn_off(void)
366{
367 volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
368
369 gpt->emsr = 0;
370}
371
372static int do_inkadiag_buzzer(cmd_tbl_t *cmdtp, int flag, int argc,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200373 char * const argv[]) {
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200374
375 unsigned int period, freq;
376 int prev, i;
377
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200378 if (argc != 3)
379 return cmd_usage(cmdtp);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200380
381 argc--;
382 argv++;
383
384 period = simple_strtol(argv[0], NULL, 0);
385 if (!period)
386 printf("Zero period is senseless\n");
387 argc--;
388 argv++;
389
390 freq = simple_strtol(argv[0], NULL, 0);
391 /* avoid zero prescale in buzzer_turn_on() */
Simon Glassb2877492012-12-13 20:48:53 +0000392 if (freq > gd->arch.ipb_clk / 128) {
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200393 printf("%dHz exceeds maximum (%ldHz)\n", freq,
Simon Glassb2877492012-12-13 20:48:53 +0000394 gd->arch.ipb_clk / 128);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200395 } else if (!freq)
396 printf("Zero frequency is senseless\n");
397 else
398 buzzer_turn_on(freq);
399
400 clear_ctrlc();
401 prev = disable_ctrlc(0);
402
403 printf("Buzzing for %d ms. Type ^C to abort!\n\n", period);
404
405 i = 0;
406 while (!ctrlc() && (i++ < CONFIG_SYS_HZ))
407 udelay(period);
408
409 clear_ctrlc();
410 disable_ctrlc(prev);
411
412 buzzer_turn_off();
413
414 return 0;
415}
416
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200417static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200418
419cmd_tbl_t cmd_inkadiag_sub[] = {
420 U_BOOT_CMD_MKENT(io, 1, 1, do_inkadiag_io, "read digital input",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200421 "<drawer1|drawer2|other> [value] - get or set specified signal"),
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200422 U_BOOT_CMD_MKENT(serial, 4, 1, do_inkadiag_serial, "test serial port",
423 "<num> <mode> <baudrate> <msg> - test uart num [0..11] in mode\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200424 "and baudrate with msg"),
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200425 U_BOOT_CMD_MKENT(buzzer, 2, 1, do_inkadiag_buzzer, "activate buzzer",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200426 "<period> <freq> - turn buzzer on for period ms with freq hz"),
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200427 U_BOOT_CMD_MKENT(help, 4, 1, do_inkadiag_help, "get help",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200428 "[command] - get help for command"),
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200429};
430
431static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200432 int argc, char * const argv[]) {
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200433 extern int _do_help (cmd_tbl_t *cmd_start, int cmd_items,
434 cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200435 int argc, char * const argv[]);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200436 /* do_help prints command name - we prepend inkadiag to our subcommands! */
437#ifdef CONFIG_SYS_LONGHELP
438 puts ("inkadiag ");
439#endif
440 return _do_help(&cmd_inkadiag_sub[0],
441 ARRAY_SIZE(cmd_inkadiag_sub), cmdtp, flag, argc, argv);
442}
443
444static int do_inkadiag(cmd_tbl_t *cmdtp, int flag, int argc,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200445 char * const argv[]) {
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200446 cmd_tbl_t *c;
447
448 c = find_cmd_tbl(argv[1], &cmd_inkadiag_sub[0], ARRAY_SIZE(cmd_inkadiag_sub));
449
450 if (c) {
451 argc--;
452 argv++;
453 return c->cmd(c, flag, argc, argv);
454 } else {
455 /* Unrecognized command */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200456 return cmd_usage(cmdtp);
Detlev Zundel7b5611c2009-03-30 00:31:34 +0200457 }
458}
459
460U_BOOT_CMD(inkadiag, 6, 1, do_inkadiag,
461 "inkadiag - inka diagnosis\n",
462 "[inkadiag what ...]\n"
463 " - perform a diagnosis on inka hardware\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200464 "'inkadiag' performs hardware tests.");