]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/inka4x0/inkadiag.c
gpio: remove gpiolib.c and define remaining functions as static inline in asm/gpio.h
[karo-tx-uboot.git] / 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 <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  */
48 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
49 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
50
51 static 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
76 static 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
117 static int do_inkadiag_io(cmd_tbl_t *cmdtp, int flag, int argc,
118                           char * const argv[]) {
119         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:
155                 return cmd_usage(cmdtp);
156         }
157
158         return -1;
159 }
160
161 DECLARE_GLOBAL_DATA_PTR;
162
163 static 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);
174         baseclk = (gd->arch.ipb_clk + 16) / 32;
175
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
200 static 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
211 static 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
222 static int do_inkadiag_serial(cmd_tbl_t *cmdtp, int flag, int argc,
223                               char * const argv[]) {
224         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
230         if (argc < 5)
231                 return cmd_usage(cmdtp);
232
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 */
264                         out_8(&uart->mcr, UART_MCR_LOOP);
265                 } else {
266                         /*
267                          * establish the UART's operational parameters
268                          * set DLAB=1, so rbr accesses DLL
269                          */
270                         out_8(&uart->lcr, UART_LCR_DLAB);
271                         /* set baudrate */
272                         out_8(&uart->rbr, combrd);
273                         /* set data-format: 8-N-1 */
274                         out_8(&uart->lcr, UART_LCR_WLS_8);
275                 }
276
277                 if (mode & 2) {
278                         /* set request to send */
279                         out_8(&uart->mcr, UART_MCR_RTS);
280                         udelay(10);
281                         /* check clear to send */
282                         if ((in_8(&uart->msr) & UART_MSR_CTS) == 0x00)
283                                 return -1;
284                 }
285                 if (mode & 4) {
286                         /* set data terminal ready */
287                         out_8(&uart->mcr, UART_MCR_DTR);
288                         udelay(10);
289                         /* check data set ready and carrier detect */
290                         if ((in_8(&uart->msr) & (UART_MSR_DSR | UART_MSR_DCD))
291                             != (UART_MSR_DSR | UART_MSR_DCD))
292                                 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;
298                         while ((in_8(&uart->lsr) & UART_LSR_THRE) ==    0x00) {
299                                 if (j++ > CONFIG_SYS_HZ)
300                                         break;
301                                 udelay(10);
302                         }
303                         out_8(&uart->rbr, argv[3][i]);
304                         j = 0;
305                         while ((in_8(&uart->lsr) & UART_LSR_DR) == 0x00) {
306                                 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 */
352 static void buzzer_turn_on(unsigned int freq)
353 {
354         volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
355
356         const u32 prescale = gd->arch.ipb_clk / freq / 128;
357         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
365 static void buzzer_turn_off(void)
366 {
367         volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
368
369         gpt->emsr = 0;
370 }
371
372 static int do_inkadiag_buzzer(cmd_tbl_t *cmdtp, int flag, int argc,
373                               char * const argv[]) {
374
375         unsigned int period, freq;
376         int prev, i;
377
378         if (argc != 3)
379                 return cmd_usage(cmdtp);
380
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() */
392         if (freq > gd->arch.ipb_clk / 128) {
393                 printf("%dHz exceeds maximum (%ldHz)\n", freq,
394                        gd->arch.ipb_clk / 128);
395         } 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
417 static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
418
419 cmd_tbl_t cmd_inkadiag_sub[] = {
420         U_BOOT_CMD_MKENT(io, 1, 1, do_inkadiag_io, "read digital input",
421          "<drawer1|drawer2|other> [value] - get or set specified signal"),
422         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"
424          "and baudrate with msg"),
425         U_BOOT_CMD_MKENT(buzzer, 2, 1, do_inkadiag_buzzer, "activate buzzer",
426          "<period> <freq> - turn buzzer on for period ms with freq hz"),
427         U_BOOT_CMD_MKENT(help, 4, 1, do_inkadiag_help, "get help",
428          "[command] - get help for command"),
429 };
430
431 static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag,
432                             int argc, char * const argv[]) {
433         extern int _do_help (cmd_tbl_t *cmd_start, int cmd_items,
434                              cmd_tbl_t *cmdtp, int flag,
435                              int argc, char * const argv[]);
436         /* 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
444 static int do_inkadiag(cmd_tbl_t *cmdtp, int flag, int argc,
445                        char * const argv[]) {
446         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 */
456                 return cmd_usage(cmdtp);
457         }
458 }
459
460 U_BOOT_CMD(inkadiag, 6, 1, do_inkadiag,
461            "inkadiag - inka diagnosis\n",
462            "[inkadiag what ...]\n"
463            "    - perform a diagnosis on inka hardware\n"
464            "'inkadiag' performs hardware tests.");