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