1 /* 2 * Access various peripherals on the MIPS Creator CI20 board. 3 * 4 * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include "common.h" 23 #include "defs.h" 24 25 #include <l4/devices/dma.h> 26 #include <l4/devices/memory.h> 27 28 #include <l4/io/io.h> 29 #include <l4/re/env.h> 30 #include <l4/re/c/util/cap_alloc.h> 31 #include <l4/sys/cache.h> 32 #include <l4/sys/factory.h> 33 #include <l4/sys/icu.h> 34 #include <l4/sys/ipc.h> 35 #include <l4/sys/irq.h> 36 #include <l4/sys/rcv_endpoint.h> 37 #include <l4/vbus/vbus.h> 38 39 #include <stdio.h> 40 #include <unistd.h> 41 #include <stdint.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 46 47 /* Common configuration. */ 48 49 static l4_cap_idx_t icucap; 50 51 /* Memory definitions. */ 52 53 static const unsigned int num_memory_regions = 8; 54 55 static struct memory_region memory_regions[] = { 56 {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 57 }; 58 59 60 61 /* Device and resource discovery. */ 62 63 static long item_in_range(long start, long end, long index) 64 { 65 if (start < end) 66 return start + index; 67 else if (start > end) 68 return start - index; 69 else 70 return start; 71 } 72 73 74 75 /* Common functions. */ 76 77 static int init_irq(int num, l4_cap_idx_t irq, l4_uint32_t start, l4_uint32_t end) 78 { 79 /* Create interrupt object. */ 80 81 long err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq)); 82 83 if (err) 84 { 85 printf("Could not create IRQ object: %ld\n", err); 86 return 1; 87 } 88 89 /* Bind interrupt objects to IRQ numbers. */ 90 91 err = l4_error(l4_icu_bind(icucap, 92 item_in_range(start, end, num), 93 irq)); 94 95 if (err) 96 { 97 printf("Could not bind IRQ to the ICU: %ld\n", err); 98 return 1; 99 } 100 101 /* Attach ourselves to the interrupt handler. */ 102 103 err = l4_error(l4_rcv_ep_bind_thread(irq, l4re_env()->main_thread, num)); 104 105 if (err) 106 { 107 printf("Could not attach to IRQs: %ld\n", err); 108 return 1; 109 } 110 111 return 0; 112 } 113 114 static const char *delimiters = " \n"; 115 116 static char *read_token(const char *message) 117 { 118 char *token; 119 120 if ((token = strtok(NULL, delimiters)) == NULL) 121 { 122 if (message != NULL) 123 printf("%s?\n", message); 124 } 125 126 return token; 127 } 128 129 static int read_flag(const char *message, int *value, const char *flag, size_t n) 130 { 131 char *token = read_token(message); 132 133 if (token == NULL) 134 return 0; 135 136 *value = !strncmp(token, flag, n); 137 return 1; 138 } 139 140 static int read_encoded_number(const char *message, const char *format, unsigned int *num) 141 { 142 char *token = read_token(message); 143 144 if (token == NULL) 145 return 0; 146 147 sscanf(token, format, num); 148 return 1; 149 } 150 151 static int read_number(const char *message, unsigned int *num) 152 { 153 char *token = read_token(message); 154 155 if (token == NULL) 156 return 0; 157 158 if (!strncmp(token, "0x", 2) || !strncmp(token, "0X", 2)) 159 sscanf(token, "%x", num); 160 else 161 *num = atoi(token); 162 163 return 1; 164 } 165 166 static int get_resource_number(const char *type, unsigned int available) 167 { 168 unsigned int num; 169 170 if (!read_number(type, &num)) 171 return -1; 172 173 if (num >= available) 174 { 175 printf("%s number out of range.\n", type); 176 return -1; 177 } 178 179 return num; 180 } 181 182 static int get_channel_number(unsigned int available) 183 { 184 return get_resource_number("Channel", available); 185 } 186 187 static int get_region_number(unsigned int available) 188 { 189 return get_resource_number("Region", available); 190 } 191 192 static void *get_channel(int num_channels, void *channels[], int *num) 193 { 194 int n = get_channel_number(num_channels); 195 void *channel; 196 197 if (n < 0) 198 return NULL; 199 200 channel = channels[n]; 201 202 if (channel == NULL) 203 printf("Channel needs creating.\n"); 204 205 if (num != NULL) 206 *num = n; 207 208 return channel; 209 } 210 211 static void list_channels(int num_channels, void *channels[]) 212 { 213 int num; 214 void *channel; 215 216 for (num = 0; num < num_channels; num++) 217 { 218 printf("Channel %d: ", num); 219 220 channel = channels[num]; 221 222 if (channel == NULL) 223 printf("(inactive)\n"); 224 else 225 printf("active\n"); 226 } 227 } 228 229 static struct dma_region *_get_dma_region(void) 230 { 231 int num = get_region_number(num_dma_regions); 232 233 if (num < 0) 234 return NULL; 235 236 return &dma_regions[num]; 237 } 238 239 static struct memory_region *_get_memory_region(void) 240 { 241 int num = get_region_number(num_memory_regions); 242 243 if (num < 0) 244 return NULL; 245 246 return &memory_regions[num]; 247 } 248 249 static int get_memory_region(unsigned int size, l4_addr_t *addr) 250 { 251 void *a = calloc(size, sizeof(char)); 252 253 *addr = (l4_addr_t) a; 254 return a == NULL ? 1 : 0; 255 } 256 257 258 259 /* AIC/I2S operations. */ 260 261 static void new_aic_channel(void *aic) 262 { 263 int num = get_channel_number(num_aic_channels); 264 void *channel; 265 266 if (num < 0) 267 return; 268 269 channel = get_channel(num_dma_channels, dma_channels, NULL); 270 271 if (channel == NULL) 272 return; 273 274 aic_channels[num] = aic_get_channel(aic, num, channel); 275 } 276 277 static void aic_transfer_data(void) 278 { 279 void *channel = get_channel(num_aic_channels, aic_channels, NULL); 280 struct dma_region *region; 281 uint32_t count, rate, resolution, transferred; 282 283 if (channel == NULL) 284 return; 285 286 region = _get_dma_region(); 287 288 if (region == NULL) 289 return; 290 291 if (!read_number("Sample size", &count)) 292 return; 293 294 if (!read_number("Sample rate", &rate)) 295 return; 296 297 if (!read_number("Sample resolution", &resolution)) 298 return; 299 300 transferred = aic_transfer(channel, region->paddr, count, rate, resolution); 301 302 printf("Transferred: %d\n", transferred); 303 } 304 305 306 307 /* CPM operations. */ 308 309 static const char *clock_id(enum Clock_identifiers clock) 310 { 311 for (int i = 0; clocks[i].id != NULL; i++) 312 if (clock == clocks[i].clock) 313 return clocks[i].id; 314 315 return NULL; 316 } 317 318 static enum Clock_identifiers get_clock(char *id) 319 { 320 if (id != NULL) 321 { 322 for (int i = 0; clocks[i].id != NULL; i++) 323 if (!strcmp(id, clocks[i].id)) 324 return clocks[i].clock; 325 } 326 327 return Clock_none; 328 } 329 330 static const char list_clocks_separator[] = "--------------------------------"; 331 332 #define sep(x) (list_clocks_separator + sizeof(list_clocks_separator) - 1 - x) 333 334 static void list_clocks(void *cpm) 335 { 336 /* Read information from the clock and power management unit. */ 337 338 printf("%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n" 339 "%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n", 340 "Id", "Clock", "Type", "Source", "Source Clock", "Source Freq.", 341 "Parameters", "Frequency", "On", 342 sep(10), sep(15), sep(7), sep(6), sep(12), sep(12), 343 sep(24), sep(12), sep(3)); 344 345 for (int i = 0; clocks[i].id != NULL; i++) 346 { 347 uint32_t parameters[4]; 348 char parameter_str[25] = {0}; 349 const char *source_id = clock_id(cpm_get_source_clock(cpm, clocks[i].clock)); 350 int num_parameters = cpm_get_parameters(cpm, clocks[i].clock, parameters); 351 352 for (int p = 0, pos = 0; p < num_parameters; p++) 353 { 354 int result = sprintf(parameter_str + pos, "%-7d ", parameters[p]); 355 if (result < 0) 356 break; 357 pos += result; 358 } 359 360 printf("%-10s %-15s %-7s %-6d %-12s %-12lld %-24s %-12lld %-3s\n", 361 clocks[i].id, 362 clocks[i].name, 363 cpm_clock_type(cpm, clocks[i].clock), 364 cpm_get_source(cpm, clocks[i].clock), 365 source_id != NULL ? source_id : "...", 366 cpm_get_source_frequency(cpm, clocks[i].clock), 367 parameter_str, 368 cpm_get_frequency(cpm, clocks[i].clock), 369 cpm_have_clock(cpm, clocks[i].clock) ? "on" : "off"); 370 } 371 } 372 373 static enum Clock_identifiers get_clock_by_name(void) 374 { 375 char *token; 376 enum Clock_identifiers clock; 377 378 if ((token = read_token("Clock")) == NULL) 379 return Clock_none; 380 381 clock = get_clock(token); 382 383 if (clock == Clock_none) 384 { 385 printf("Unrecognised clock: %s\n", token); 386 return Clock_none; 387 } 388 389 return clock; 390 } 391 392 static void set_frequency(void *cpm) 393 { 394 enum Clock_identifiers clock; 395 unsigned int frequency; 396 397 clock = get_clock_by_name(); 398 399 if (clock == Clock_none) 400 return; 401 402 if (!read_number("Frequency", &frequency)) 403 return; 404 405 if (!cpm_set_frequency(cpm, clock, frequency)) 406 printf("Frequency not set.\n"); 407 } 408 409 static void set_parameters(void *cpm) 410 { 411 char *token; 412 enum Clock_identifiers clock; 413 uint32_t parameters[4]; 414 int num_parameters; 415 416 clock = get_clock_by_name(); 417 418 if (clock == Clock_none) 419 return; 420 421 for (num_parameters = 0; num_parameters < 4; num_parameters++) 422 { 423 if ((token = read_token(NULL)) == NULL) 424 break; 425 426 parameters[num_parameters] = atoi(token); 427 } 428 429 if (!num_parameters) 430 { 431 printf("Parameters?\n"); 432 return; 433 } 434 435 if (!cpm_set_parameters(cpm, clock, num_parameters, parameters)) 436 printf("Too few or invalid parameters.\n"); 437 } 438 439 static void set_source(void *cpm) 440 { 441 enum Clock_identifiers clock; 442 unsigned int source; 443 444 clock = get_clock_by_name(); 445 446 if (clock == Clock_none) 447 return; 448 449 if (!read_number("Source", &source)) 450 return; 451 452 cpm_set_source(cpm, clock, source); 453 } 454 455 static void start_clock(void *cpm) 456 { 457 enum Clock_identifiers clock; 458 459 clock = get_clock_by_name(); 460 461 if (clock == Clock_none) 462 return; 463 464 cpm_start_clock(cpm, clock); 465 } 466 467 static void stop_clock(void *cpm) 468 { 469 enum Clock_identifiers clock; 470 471 clock = get_clock_by_name(); 472 473 if (clock == Clock_none) 474 return; 475 476 cpm_stop_clock(cpm, clock); 477 } 478 479 480 481 /* DMA configuration. */ 482 483 static l4_uint32_t dma_irq_start = 0, dma_irq_end = 0; 484 485 /* DMA operations. */ 486 487 static int init_dma(void) 488 { 489 unsigned int i; 490 491 for (i = 0; i < num_dma_regions; i++) 492 dma_regions[i].mem = L4_INVALID_CAP; 493 494 /* Here, only one IRQ is used. */ 495 496 dma_irq = l4re_util_cap_alloc(); 497 return init_irq(0, dma_irq, dma_irq_start, dma_irq_end); 498 } 499 500 static void list_dma_regions(void) 501 { 502 unsigned int num; 503 struct dma_region *region; 504 unsigned int i; 505 506 for (num = 0; num < num_dma_regions; num++) 507 { 508 printf("Region %d: ", num); 509 510 region = &dma_regions[num]; 511 512 if (l4_is_invalid_cap(region->mem)) 513 printf("(inactive)\n"); 514 else 515 { 516 printf("size = %d; align = %d; virtual = 0x%lx; physical = 0x%llx\ndata =", 517 region->size, region->align, region->vaddr, region->paddr); 518 519 for (i = 0; (i < region->size) && (i < 16); i++) 520 printf(" %02x", *((uint8_t *) region->vaddr + i)); 521 522 printf("\n"); 523 } 524 } 525 } 526 527 static void new_dma_channel(void *dma) 528 { 529 int num = get_channel_number(num_dma_channels); 530 531 if (num < 0) 532 return; 533 534 if (dma_channels[num] != NULL) 535 { 536 printf("Channel already defined.\n"); 537 return; 538 } 539 540 dma_channels[num] = dma_get_channel(dma, num, dma_irq); 541 } 542 543 static void new_dma_region(void) 544 { 545 struct dma_region *region = _get_dma_region(); 546 547 if (region == NULL) 548 { 549 list_dma_regions(); 550 return; 551 } 552 553 if (l4_is_valid_cap(region->mem)) 554 { 555 printf("Region already defined.\n"); 556 return; 557 } 558 559 if (!read_number("Size", ®ion->size)) 560 return; 561 562 if (!read_number("Alignment", ®ion->align)) 563 return; 564 565 if (get_dma_region(region->size, region->align, ®ion->vaddr, ®ion->paddr, ®ion->mem)) 566 printf("Could not allocate region.\n"); 567 } 568 569 static void set_dma_region(void) 570 { 571 char *token; 572 struct dma_region *region = _get_dma_region(); 573 FILE *fp; 574 575 if (region == NULL) 576 { 577 list_dma_regions(); 578 return; 579 } 580 581 if (l4_is_invalid_cap(region->mem)) 582 { 583 printf("Region needs creating.\n"); 584 return; 585 } 586 587 memset((void *) region->vaddr, 0, region->size); 588 589 if ((token = read_token("Filename")) == NULL) 590 return; 591 592 /* Populate the region from the file. */ 593 594 fp = fopen(token, "r"); 595 596 if (fp == NULL) 597 { 598 printf("File not readable.\n"); 599 return; 600 } 601 602 fread((char *) region->vaddr, sizeof(char), region->size, fp); 603 fclose(fp); 604 605 l4_cache_flush_data(region->vaddr, region->vaddr + region->size); 606 } 607 608 static void dma_transfer_data(void) 609 { 610 uint32_t count, to_transfer, transferred; 611 unsigned int source_address, destination_address, source_width, destination_width, 612 transfer_unit_size, request_type; 613 int source_increment, destination_increment; 614 void *channel = get_channel(num_dma_channels, dma_channels, NULL); 615 unsigned int i; 616 617 if (channel == NULL) 618 return; 619 620 if (!read_number("Source address", &source_address)) 621 return; 622 623 if (!read_number("Destination address", &destination_address)) 624 return; 625 626 if (!read_number("Transfer size", &count)) 627 return; 628 629 if (!read_flag("Source increment", &source_increment, "i", 1)) 630 return; 631 632 if (!read_flag("Destination increment", &destination_increment, "i", 1)) 633 return; 634 635 if (!read_number("Source width", &source_width)) 636 return; 637 638 if (!read_number("Destination width", &destination_width)) 639 return; 640 641 if (!read_number("Transfer unit size", &transfer_unit_size)) 642 return; 643 644 if (!read_number("Request type", &request_type)) 645 return; 646 647 /* Invalidate DMA region contents in case of transfers between regions. */ 648 649 for (i = 0; i < num_dma_regions; i++) 650 l4_cache_inv_data(dma_regions[i].vaddr, dma_regions[i].vaddr + dma_regions[i].size); 651 652 to_transfer = dma_transfer(channel, source_address, destination_address, 653 count, 654 source_increment, destination_increment, 655 source_width, destination_width, 656 transfer_unit_size, 657 request_type); 658 659 transferred = to_transfer ? count - dma_wait(channel) : 0; 660 661 printf("Transferred: %d\n", transferred); 662 } 663 664 665 666 /* GPIO operations. */ 667 668 static char get_gpio_dir_label(unsigned int value, unsigned int level) 669 { 670 return value == Fix_input ? (level ? 'I' : 'i') : (level ? 'O' : 'o'); 671 } 672 673 static char get_gpio_irq_label(unsigned int value) 674 { 675 switch (value) 676 { 677 case L4_IRQ_F_NEG_EDGE: return 'f'; 678 case L4_IRQ_F_POS_EDGE: return 'r'; 679 case L4_IRQ_F_LEVEL_LOW: return 'l'; 680 case L4_IRQ_F_LEVEL_HIGH: return 'h'; 681 default: return '?'; 682 } 683 } 684 685 static void list_gpios(void *gpio[]) 686 { 687 unsigned int port, pin; 688 unsigned int func, value, level; 689 690 printf("gpio: I = input high; i = input low; O = output high; o = output low\n" 691 "irq: h = high level; l = low level; r = rising edge; f = falling edge\n" 692 "alt: function number\n\n"); 693 694 /* Show pin numbering. */ 695 696 printf("Port/Pin "); 697 698 for (pin = 0; pin < 32; pin++) 699 { 700 if (!(pin % 10)) 701 printf(" %d", pin); 702 else 703 printf(" %d", pin % 10); 704 } 705 706 printf("\n"); 707 708 for (port = 0; port < num_gpio_ports; port++) 709 { 710 /* Show port and pin configuration. */ 711 712 printf("%c ", gpio_port_labels[port]); 713 714 for (pin = 0; pin < 32; pin++) 715 { 716 /* Pad below the first pin digit for multiples of ten other than zero. */ 717 718 if (pin && !(pin % 10)) 719 printf(" "); 720 721 gpio_config_pad_get(gpio[port], pin, &func, &value); 722 723 switch (func) 724 { 725 case Function_alt: printf(" %d", value); break; 726 case Function_irq: printf(" %c", get_gpio_irq_label(value)); break; 727 case Function_gpio: 728 { 729 level = gpio_get(gpio[port], pin); 730 printf(" %c", get_gpio_dir_label(value, level)); 731 break; 732 } 733 default: printf(" ?"); break; 734 } 735 } 736 737 printf("\n"); 738 } 739 } 740 741 static int get_port_and_pin(unsigned int *port, unsigned int *pin) 742 { 743 char *token; 744 745 if ((token = read_token("Port")) == NULL) 746 return 0; 747 748 if ((token[0] < 'A') || ((unsigned int) token[0] - (unsigned int) 'A' >= num_gpio_ports)) 749 { 750 printf("Bad port: %c\n", token[0]); 751 return 0; 752 } 753 754 *port = (unsigned int) token[0] - (unsigned int) 'A'; 755 756 if (!read_number("Pin", pin)) 757 return 0; 758 759 return 1; 760 } 761 762 static void set_gpio_alt_func(void *gpio[]) 763 { 764 unsigned int port, pin, value; 765 766 if (!get_port_and_pin(&port, &pin)) 767 return; 768 769 if (!read_number("Function", &value)) 770 return; 771 772 gpio_config_pad(gpio[port], pin, Function_alt, value); 773 } 774 775 static void set_gpio_pad(void *gpio[]) 776 { 777 char *token; 778 unsigned int port, pin, mode, value = 0; 779 780 if (!get_port_and_pin(&port, &pin)) 781 return; 782 783 if ((token = read_token("Mode")) == NULL) 784 return; 785 786 if (!strcmp(token, "in")) 787 mode = Fix_input; 788 else if (!strcmp(token, "irq")) 789 mode = Fix_irq; 790 else if (!strcmp(token, "out")) 791 { 792 mode = Fix_output; 793 794 if ((token = read_token(NULL)) != NULL) 795 value = atoi(token); 796 } 797 else 798 { 799 printf("Mode not recognised.\n"); 800 return; 801 } 802 803 gpio_setup(gpio[port], pin, mode, value); 804 } 805 806 static void set_gpio_pull(void *gpio[]) 807 { 808 char *token; 809 unsigned int port, pin, mode; 810 811 if (!get_port_and_pin(&port, &pin)) 812 return; 813 814 if ((token = read_token("Mode")) == NULL) 815 return; 816 817 if (!strcmp(token, "down")) 818 mode = Pull_down; 819 else if (!strcmp(token, "none")) 820 mode = Pull_none; 821 else if (!strcmp(token, "up")) 822 mode = Pull_up; 823 else 824 { 825 printf("Mode not recognised.\n"); 826 return; 827 } 828 829 gpio_config_pull(gpio[port], pin, mode); 830 } 831 832 833 834 /* I2C configuration. */ 835 836 static l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; 837 838 /* I2C operations. */ 839 840 static int _i2c_read(void *channel, uint8_t *buf, unsigned length, 841 int stop, l4_cap_idx_t irqcap, unsigned timeout) 842 { 843 l4_msgtag_t tag; 844 845 i2c_start_read(channel, buf, length, stop); 846 847 while (!i2c_read_done(channel)) 848 { 849 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); 850 851 // NOTE: Error not returned. 852 853 if (l4_ipc_error(tag, l4_utcb())) 854 break; 855 856 if (i2c_failed(channel)) 857 break; 858 859 i2c_read(channel); 860 } 861 862 if (stop) 863 i2c_stop(channel); 864 865 return i2c_have_read(channel); 866 } 867 868 static int _i2c_write(void *channel, uint8_t *buf, unsigned length, 869 int stop, l4_cap_idx_t irqcap, unsigned timeout) 870 { 871 l4_msgtag_t tag; 872 873 i2c_start_write(channel, buf, length, stop); 874 875 while (!i2c_write_done(channel)) 876 { 877 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); 878 879 // NOTE: Error not returned. 880 881 if (l4_ipc_error(tag, l4_utcb())) 882 break; 883 884 if (i2c_failed(channel)) 885 break; 886 887 i2c_write(channel); 888 } 889 890 if (stop) 891 i2c_stop(channel); 892 893 return i2c_have_written(channel); 894 } 895 896 static void list_i2c_channels(void) 897 { 898 unsigned int num; 899 void *channel; 900 901 for (num = 0; num < num_i2c_channels; num++) 902 { 903 printf("Channel %d: ", num); 904 905 channel = i2c_channels[num]; 906 907 if (channel == NULL) 908 printf("(inactive)\n"); 909 else 910 printf("%d Hz\n", i2c_get_frequency(channel)); 911 } 912 } 913 914 static void new_i2c_channel(void *i2c) 915 { 916 l4_cap_idx_t irqcap; 917 int num = get_channel_number(num_i2c_channels); 918 919 if (num < 0) 920 return; 921 922 irqcap = l4re_util_cap_alloc(); 923 924 if (init_irq(num, irqcap, i2c_irq_start, i2c_irq_end)) 925 return; 926 927 i2c_channels[num] = i2c_get_channel(i2c, num); 928 i2c_irqs[num] = irqcap; 929 } 930 931 static void i2c_get(void) 932 { 933 void *channel; 934 int num; 935 uint8_t buffer[32]; 936 unsigned int address, reg, count; 937 int i, transferred; 938 939 channel = get_channel(num_i2c_channels, i2c_channels, &num); 940 941 if (channel == NULL) 942 return; 943 944 if (!read_encoded_number("Address", "%2x", &address)) 945 return; 946 947 if (address >= 0x80) 948 { 949 printf("Address must be less than 80.\n"); 950 return; 951 } 952 953 if (!read_encoded_number("Register", "%2x", ®)) 954 return; 955 956 if (!read_number(NULL, &count)) 957 count = 1; 958 959 buffer[0] = (uint8_t) (reg & 0xff); 960 961 i2c_set_target(channel, address); 962 963 if (!_i2c_write(channel, buffer, 1, 0, i2c_irqs[num], 1000000)) 964 { 965 printf("Register request failed.\n"); 966 return; 967 } 968 969 transferred = _i2c_read(channel, buffer, count, 1, i2c_irqs[num], 1000000); 970 971 if (!transferred) 972 { 973 printf("Register read failed.\n"); 974 return; 975 } 976 977 for (i = 0; i < transferred; i++) 978 printf("%02x ", buffer[i]); 979 printf("\n"); 980 } 981 982 static void i2c_scan(void) 983 { 984 void *channel; 985 unsigned int address; 986 uint8_t buffer[1]; 987 int num; 988 989 channel = get_channel(num_i2c_channels, i2c_channels, &num); 990 991 if (channel == NULL) 992 return; 993 994 for (address = 0; address < 0x20; address++) 995 printf("%02x ", address); 996 printf("\n"); 997 998 for (address = 0; address < 0x20; address++) 999 printf("-- "); 1000 1001 for (address = 0; address < 0x80; address++) 1002 { 1003 if ((address % 32) == 0) 1004 printf("\n"); 1005 1006 i2c_set_target(channel, address); 1007 1008 if (_i2c_read(channel, buffer, 1, 1, i2c_irqs[num], 1000)) 1009 printf("%02x ", address); 1010 else 1011 printf("-- "); 1012 } 1013 1014 printf("\n"); 1015 for (address = 0; address < 0x20; address++) 1016 printf("-- "); 1017 printf("\n\n"); 1018 } 1019 1020 1021 1022 /* Memory operations. */ 1023 1024 static void list_memory_regions(void) 1025 { 1026 unsigned int num; 1027 struct memory_region *region; 1028 unsigned int i; 1029 1030 for (num = 0; num < num_memory_regions; num++) 1031 { 1032 printf("Region %d: ", num); 1033 1034 region = &memory_regions[num]; 1035 1036 if (!region->addr) 1037 printf("(inactive)\n"); 1038 else 1039 { 1040 printf("size = %d; virtual = 0x%lx; data =", 1041 region->size, region->addr); 1042 1043 for (i = 0; (i < region->size) && (i < 16); i++) 1044 printf(" %02x", *((uint8_t *) region->addr + i)); 1045 1046 printf("\n"); 1047 } 1048 } 1049 } 1050 1051 static void new_memory_region(void) 1052 { 1053 struct memory_region *region = _get_memory_region(); 1054 1055 if (region == NULL) 1056 { 1057 list_memory_regions(); 1058 return; 1059 } 1060 1061 if (region->addr) 1062 { 1063 printf("Region already defined.\n"); 1064 return; 1065 } 1066 1067 if (!read_number("Size", ®ion->size)) 1068 return; 1069 1070 if (get_memory_region(region->size, ®ion->addr)) 1071 printf("Could not allocate region.\n"); 1072 } 1073 1074 static void set_memory_region(void) 1075 { 1076 char *token; 1077 struct memory_region *region = _get_memory_region(); 1078 FILE *fp; 1079 1080 if (region == NULL) 1081 { 1082 list_memory_regions(); 1083 return; 1084 } 1085 1086 if (!region->addr) 1087 { 1088 printf("Region needs creating.\n"); 1089 return; 1090 } 1091 1092 memset((void *) region->addr, 0, region->size); 1093 1094 if ((token = read_token("Filename")) == NULL) 1095 return; 1096 1097 /* Populate the region from the file. */ 1098 1099 fp = fopen(token, "r"); 1100 1101 if (fp == NULL) 1102 { 1103 printf("File not readable.\n"); 1104 return; 1105 } 1106 1107 fread((char *) region->addr, sizeof(char), region->size, fp); 1108 fclose(fp); 1109 1110 l4_cache_flush_data(region->addr, region->addr + region->size); 1111 } 1112 1113 1114 1115 /* MSC configuration. */ 1116 1117 static l4_uint32_t msc_irq_start = 0, msc_irq_end = 0; 1118 1119 /* MSC operations. */ 1120 1121 static void list_msc_channels(void) 1122 { 1123 unsigned int num; 1124 void *channel; 1125 1126 printf("Channel Status\n"); 1127 printf("------- --------\n"); 1128 1129 for (num = 0; num < num_msc_channels; num++) 1130 { 1131 printf("%d ", num); 1132 1133 channel = msc_channels[num]; 1134 1135 if (channel == NULL) 1136 printf("inactive\n"); 1137 else 1138 printf("%08x\n", msc_get_status(channel)); 1139 } 1140 } 1141 1142 static void new_msc_channel(void *msc) 1143 { 1144 l4_cap_idx_t irqcap; 1145 int num = get_channel_number(num_msc_channels); 1146 void *channel; 1147 1148 if (num < 0) 1149 return; 1150 1151 channel = get_channel(num_dma_channels, dma_channels, NULL); 1152 1153 if (channel == NULL) 1154 return; 1155 1156 irqcap = l4re_util_cap_alloc(); 1157 1158 if (init_irq(num, irqcap, msc_irq_start, msc_irq_end)) 1159 return; 1160 1161 msc_channels[num] = msc_get_channel(msc, num, irqcap, channel); 1162 msc_irqs[num] = irqcap; 1163 } 1164 1165 static void enable_msc_channel(void) 1166 { 1167 void *channel = get_channel(num_msc_channels, msc_channels, NULL); 1168 1169 if (channel == NULL) 1170 return; 1171 1172 msc_enable(channel); 1173 } 1174 1175 static void read_block_from_msc(void) 1176 { 1177 void *channel = get_channel(num_msc_channels, msc_channels, NULL); 1178 struct dma_region *dma_region = NULL; 1179 uint32_t card, transferred; 1180 1181 if (channel == NULL) 1182 return; 1183 1184 dma_region = _get_dma_region(); 1185 1186 if (dma_region == NULL) 1187 return; 1188 1189 if (!read_number("Card", &card)) 1190 return; 1191 1192 l4_cache_inv_data(dma_region->vaddr, dma_region->vaddr + dma_region->size); 1193 1194 transferred = msc_read_block(channel, (uint8_t) card, dma_region->paddr); 1195 1196 printf("Transferred: %d\n", transferred); 1197 } 1198 1199 1200 1201 /* RTC operations. */ 1202 1203 static void rtc_reset(void *rtc, void *cpm) 1204 { 1205 unsigned int seconds; 1206 1207 if (!read_number("Seconds", &seconds)) 1208 return; 1209 1210 /* NOTE: Assuming EXCLK/512 as RTC source. */ 1211 1212 uint32_t rtc_seconds = rtc_get_seconds(rtc); 1213 uint32_t value = seconds * cpm_get_frequency(cpm, Clock_external) / 512; 1214 1215 rtc_alarm_disable(rtc); 1216 rtc_set_alarm_seconds(rtc, rtc_seconds + value); 1217 rtc_hibernate(rtc); 1218 } 1219 1220 static void _rtc_set_seconds(void *rtc, int alarm) 1221 { 1222 unsigned int seconds; 1223 1224 if (!read_number("Seconds", &seconds)) 1225 return; 1226 1227 (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); 1228 } 1229 1230 1231 1232 /* SPI operations. */ 1233 1234 static void new_spi_channel(void *spi, void *gpio[]) 1235 { 1236 unsigned int control_port, control_pin, control_alt_func_input; 1237 int control_alt_func; 1238 void *control_chip; 1239 int num = get_channel_number(num_spi_channels); 1240 void *channel; 1241 uint32_t frequency; 1242 1243 if (num < 0) 1244 return; 1245 1246 channel = get_channel(num_dma_channels, dma_channels, NULL); 1247 1248 if (channel == NULL) 1249 return; 1250 1251 if (!read_number("Frequency", &frequency)) 1252 return; 1253 1254 if (!get_port_and_pin(&control_port, &control_pin)) 1255 control_chip = NULL; 1256 else 1257 control_chip = gpio[control_port]; 1258 1259 if (!read_number("Function", &control_alt_func_input)) 1260 control_alt_func = -1; 1261 else 1262 control_alt_func = control_alt_func_input; 1263 1264 spi_channels[num] = spi_get_channel(spi, num, channel, frequency, 1265 control_chip, control_pin, 1266 control_alt_func); 1267 } 1268 1269 static void new_spi_channel_gpio(void *gpio[]) 1270 { 1271 unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, 1272 enable_pin, control_port, control_pin; 1273 void *control_chip; 1274 int num = get_channel_number(num_spi_channels); 1275 uint32_t frequency; 1276 1277 if (num < 0) 1278 return; 1279 1280 if (!read_number("Frequency", &frequency)) 1281 return; 1282 1283 if (!get_port_and_pin(&clock_port, &clock_pin)) 1284 return; 1285 1286 if (!get_port_and_pin(&data_port, &data_pin)) 1287 return; 1288 1289 if (!get_port_and_pin(&enable_port, &enable_pin)) 1290 return; 1291 1292 if (!get_port_and_pin(&control_port, &control_pin)) 1293 control_chip = NULL; 1294 else 1295 control_chip = gpio[control_port]; 1296 1297 spi_channels[num] = spi_get_channel_gpio(frequency, 1298 gpio[clock_port], clock_pin, 1299 gpio[data_port], data_pin, 1300 gpio[enable_port], enable_pin, 1301 control_chip, control_pin); 1302 } 1303 1304 static void spi_control(int acquire) 1305 { 1306 unsigned int level; 1307 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1308 1309 if (acquire) 1310 { 1311 if (!read_number("Level", &level)) 1312 return; 1313 1314 spi_acquire_control(channel, level); 1315 } 1316 else 1317 spi_release_control(channel); 1318 } 1319 1320 static void spi_send_data(void) 1321 { 1322 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1323 int bytes; 1324 unsigned int byte; 1325 uint8_t buffer[256]; 1326 1327 bytes = 0; 1328 1329 while (read_encoded_number(NULL, "%2x", &byte)) 1330 { 1331 buffer[bytes] = (uint8_t) (byte & 0xff); 1332 bytes++; 1333 } 1334 1335 spi_send(channel, bytes, buffer); 1336 } 1337 1338 static void spi_send_data_units(void) 1339 { 1340 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1341 unsigned int char_size, unit_size, value; 1342 uint8_t buffer[256]; 1343 int byte = 0; 1344 1345 if (!read_number("Unit size", &unit_size)) 1346 return; 1347 1348 if (!read_number("Character size", &char_size)) 1349 return; 1350 1351 /* Read hex digits for bytes. Multiple bytes make up each unit and are read 1352 from most to least significant. Where the unit size exceeds the character 1353 size, the last bit before the character indicates the GPC bit. */ 1354 1355 while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) 1356 buffer[byte++] = value; 1357 1358 /* Explicitly indicate big endian data. */ 1359 1360 spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); 1361 } 1362 1363 static void spi_transfer_data(void) 1364 { 1365 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1366 struct dma_region *dma_region = NULL, *desc_region; 1367 struct memory_region *memory_region = NULL; 1368 unsigned int char_size, unit_size; 1369 uint32_t count, transferred; 1370 int using_dma; 1371 1372 if (channel == NULL) 1373 return; 1374 1375 if (!read_flag("Using DMA", &using_dma, "d", 1)) 1376 return; 1377 1378 if (using_dma) 1379 dma_region = _get_dma_region(); 1380 else 1381 memory_region = _get_memory_region(); 1382 1383 if ((dma_region == NULL) && (memory_region == NULL)) 1384 return; 1385 1386 if (!read_number("Transfer size", &count)) 1387 return; 1388 1389 if (!read_number("Unit size", &unit_size)) 1390 return; 1391 1392 if (!read_number("Character size", &char_size)) 1393 return; 1394 1395 if (using_dma) 1396 { 1397 desc_region = _get_dma_region(); 1398 1399 transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, 1400 unit_size, char_size, 1401 desc_region != NULL ? desc_region->vaddr : 0, 1402 desc_region != NULL ? desc_region->paddr : 0); 1403 } 1404 else 1405 transferred = spi_transfer(channel, memory_region->addr, 0, count, 1406 unit_size, char_size, 0, 0); 1407 1408 printf("Transferred: %d\n", transferred); 1409 } 1410 1411 1412 1413 /* TCU configuration. */ 1414 1415 static l4_uint32_t tcu_irq_start = 0, tcu_irq_end = 0; 1416 1417 /* TCU operations. */ 1418 1419 static int init_tcu(void) 1420 { 1421 /* Here, only one IRQ is used. */ 1422 1423 tcu_irq = l4re_util_cap_alloc(); 1424 return init_irq(0, tcu_irq, tcu_irq_start, tcu_irq_end); 1425 } 1426 1427 static void disable_tcu_counter(void) 1428 { 1429 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1430 1431 if (channel == NULL) 1432 return; 1433 1434 tcu_disable(channel); 1435 } 1436 1437 static void enable_tcu_counter(void) 1438 { 1439 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1440 1441 if (channel == NULL) 1442 return; 1443 1444 tcu_enable(channel); 1445 } 1446 1447 static void new_tcu_channel(void *tcu) 1448 { 1449 int num = get_channel_number(num_tcu_channels); 1450 1451 if (num < 0) 1452 return; 1453 1454 tcu_channels[num] = tcu_get_channel(tcu, num, tcu_irq); 1455 } 1456 1457 static void list_tcu_channels(void) 1458 { 1459 unsigned int num; 1460 void *channel; 1461 1462 printf(" Clock.. Counter......... Mask..............\n"); 1463 printf("Channel Status C Pre Cnt Half Full Half Full Int\n"); 1464 printf("------- -------- -- ---- ---- ---- ---- -------- -------- ----\n"); 1465 1466 for (num = 0; num < num_tcu_channels; num++) 1467 { 1468 printf("%d ", num); 1469 1470 channel = tcu_channels[num]; 1471 1472 if (channel == NULL) 1473 printf("inactive\n"); 1474 else 1475 printf("%s %d %4d %04x %04x %04x %s %s %s\n", 1476 tcu_is_enabled(channel) ? "enabled " : "disabled", 1477 tcu_get_clock(channel), 1478 tcu_get_prescale(channel), 1479 tcu_get_counter(channel), 1480 tcu_get_half_data_value(channel), 1481 tcu_get_full_data_value(channel), 1482 tcu_get_half_data_mask(channel) ? "masked " : "unmasked", 1483 tcu_get_full_data_mask(channel) ? "masked " : "unmasked", 1484 tcu_have_interrupt(channel) ? "!" : "_"); 1485 } 1486 } 1487 1488 static void set_tcu_counter(void) 1489 { 1490 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1491 uint32_t counter; 1492 1493 if (channel == NULL) 1494 return; 1495 1496 if (!read_number("Value", &counter)) 1497 return; 1498 1499 tcu_set_counter(channel, counter); 1500 } 1501 1502 static void set_tcu_clock(void) 1503 { 1504 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1505 uint32_t clock; 1506 1507 if (channel == NULL) 1508 return; 1509 1510 if (!read_number("Clock", &clock)) 1511 return; 1512 1513 tcu_set_clock(channel, clock); 1514 } 1515 1516 static void set_tcu_full(void) 1517 { 1518 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1519 uint32_t value; 1520 1521 if (channel == NULL) 1522 return; 1523 1524 if (!read_number("Full value", &value)) 1525 return; 1526 1527 tcu_set_full_data_value(channel, value); 1528 } 1529 1530 static void set_tcu_half(void) 1531 { 1532 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1533 uint32_t value; 1534 1535 if (channel == NULL) 1536 return; 1537 1538 if (!read_number("Half value", &value)) 1539 return; 1540 1541 tcu_set_half_data_value(channel, value); 1542 } 1543 1544 static void set_tcu_mask(int full_mask, int masked) 1545 { 1546 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1547 1548 if (channel == NULL) 1549 return; 1550 1551 if (full_mask) 1552 tcu_set_full_data_mask(channel, masked); 1553 else 1554 tcu_set_half_data_mask(channel, masked); 1555 } 1556 1557 static void set_tcu_prescale(void) 1558 { 1559 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1560 uint32_t prescale; 1561 1562 if (channel == NULL) 1563 return; 1564 1565 if (!read_number("Prescale", &prescale)) 1566 return; 1567 1568 tcu_set_prescale(channel, prescale); 1569 } 1570 1571 static void tcu_wait(void) 1572 { 1573 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1574 uint32_t timeout; 1575 1576 if (channel == NULL) 1577 return; 1578 1579 if (!read_number("Timeout", &timeout)) 1580 return; 1581 1582 printf("IRQ received: %s\n", tcu_wait_for_irq(channel, timeout) ? "yes" : "no"); 1583 } 1584 1585 1586 1587 /* Command processing. */ 1588 1589 static void handle_aic(void *aic) 1590 { 1591 char *token; 1592 1593 if ((token = read_token(NULL)) != NULL) 1594 { 1595 if (!strcmp(token, "l") || !strcmp(token, "list")) 1596 list_channels(num_aic_channels, aic_channels); 1597 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1598 new_aic_channel(aic); 1599 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1600 aic_transfer_data(); 1601 else 1602 printf("aic channel | list | transfer\n"); 1603 } 1604 else 1605 list_channels(num_aic_channels, aic_channels); 1606 } 1607 1608 static void handle_cpm(void *cpm) 1609 { 1610 char *token; 1611 1612 if ((token = read_token(NULL)) != NULL) 1613 { 1614 if (!strcmp(token, "l") || !strcmp(token, "list")) 1615 list_clocks(cpm); 1616 else if (!strcmp(token, "f") || !strcmp(token, "frequency")) 1617 set_frequency(cpm); 1618 else if (!strcmp(token, "p") || !strcmp(token, "parameters")) 1619 set_parameters(cpm); 1620 else if (!strcmp(token, "s") || !strcmp(token, "source")) 1621 set_source(cpm); 1622 else if (!strcmp(token, "start")) 1623 start_clock(cpm); 1624 else if (!strcmp(token, "stop")) 1625 stop_clock(cpm); 1626 else 1627 printf("cpm list | frequency | parameters | source | start | stop\n"); 1628 } 1629 else 1630 list_clocks(cpm); 1631 } 1632 1633 static void handle_dma(void *dma) 1634 { 1635 char *token; 1636 1637 if ((token = read_token(NULL)) != NULL) 1638 { 1639 if (!strcmp(token, "l") || !strcmp(token, "list")) 1640 list_channels(num_dma_channels, dma_channels); 1641 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1642 new_dma_channel(dma); 1643 else if (!strcmp(token, "r") || !strcmp(token, "region")) 1644 new_dma_region(); 1645 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1646 set_dma_region(); 1647 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1648 dma_transfer_data(); 1649 else 1650 printf("dma channel | list | region | set | transfer\n"); 1651 } 1652 else 1653 list_channels(num_dma_channels, dma_channels); 1654 } 1655 1656 static void handle_gpio(void *gpio[]) 1657 { 1658 char *token; 1659 1660 if ((token = read_token(NULL)) != NULL) 1661 { 1662 if (!strcmp(token, "l") || !strcmp(token, "list")) 1663 list_gpios(gpio); 1664 else if (!strcmp(token, "a") || !strcmp(token, "alt")) 1665 set_gpio_alt_func(gpio); 1666 else if (!strcmp(token, "i") || !strcmp(token, "io")) 1667 set_gpio_pad(gpio); 1668 else if (!strcmp(token, "p") || !strcmp(token, "pull")) 1669 set_gpio_pull(gpio); 1670 else 1671 printf("gpio list | alt | io | pull\n"); 1672 } 1673 else 1674 list_gpios(gpio); 1675 } 1676 1677 static void handle_i2c(void *i2c) 1678 { 1679 char *token; 1680 1681 if ((token = read_token(NULL)) != NULL) 1682 { 1683 if (!strcmp(token, "l") || !strcmp(token, "list")) 1684 list_i2c_channels(); 1685 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1686 new_i2c_channel(i2c); 1687 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1688 i2c_get(); 1689 else if (!strcmp(token, "s") || !strcmp(token, "scan")) 1690 i2c_scan(); 1691 else 1692 printf("i2c channel | get | list | scan\n"); 1693 } 1694 else 1695 list_i2c_channels(); 1696 } 1697 1698 static void handle_memory(void) 1699 { 1700 char *token; 1701 1702 if ((token = read_token(NULL)) != NULL) 1703 { 1704 if (!strcmp(token, "r") || !strcmp(token, "region")) 1705 new_memory_region(); 1706 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1707 set_memory_region(); 1708 else 1709 printf("memory region | set\n"); 1710 } 1711 else 1712 list_memory_regions(); 1713 } 1714 1715 static void handle_msc(void *msc) 1716 { 1717 char *token; 1718 1719 if ((token = read_token(NULL)) != NULL) 1720 { 1721 if (!strcmp(token, "l") || !strcmp(token, "list")) 1722 list_msc_channels(); 1723 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1724 new_msc_channel(msc); 1725 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1726 enable_msc_channel(); 1727 else if (!strcmp(token, "r") || !strcmp(token, "read")) 1728 read_block_from_msc(); 1729 else 1730 printf("msc channel | enable | read\n"); 1731 } 1732 else 1733 list_msc_channels(); 1734 } 1735 1736 static void handle_rtc(void *rtc, void *cpm) 1737 { 1738 char *token; 1739 1740 if ((token = read_token(NULL)) != NULL) 1741 { 1742 if (!strcmp(token, "d") || !strcmp(token, "disable")) 1743 rtc_disable(rtc); 1744 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1745 rtc_enable(rtc); 1746 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1747 printf("seconds = %d\n", rtc_get_seconds(rtc)); 1748 else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) 1749 printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); 1750 else if (!strcmp(token, "p") || !strcmp(token, "power-down")) 1751 rtc_power_down(rtc); 1752 else if (!strcmp(token, "r") || !strcmp(token, "reset")) 1753 rtc_reset(rtc, cpm); 1754 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1755 _rtc_set_seconds(rtc, 0); 1756 else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) 1757 _rtc_set_seconds(rtc, 1); 1758 else 1759 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1760 } 1761 else 1762 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1763 } 1764 1765 static void handle_spi(void *spi, void *gpio[]) 1766 { 1767 char *token; 1768 1769 if ((token = read_token(NULL)) != NULL) 1770 { 1771 if (!strcmp(token, "l") || !strcmp(token, "list")) 1772 list_channels(num_spi_channels, spi_channels); 1773 else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) 1774 spi_control(1); 1775 else if (!strcmp(token, "r") || !strcmp(token, "control-release")) 1776 spi_control(0); 1777 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1778 new_spi_channel(spi, gpio); 1779 else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) 1780 new_spi_channel_gpio(gpio); 1781 else if (!strcmp(token, "s") || !strcmp(token, "send")) 1782 spi_send_data(); 1783 else if (!strcmp(token, "S") || !strcmp(token, "send-units")) 1784 spi_send_data_units(); 1785 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1786 spi_transfer_data(); 1787 else 1788 printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); 1789 } 1790 else 1791 list_channels(num_spi_channels, spi_channels); 1792 } 1793 1794 static void handle_tcu(void *tcu) 1795 { 1796 char *token; 1797 1798 if ((token = read_token(NULL)) != NULL) 1799 { 1800 if (!strcmp(token, "l") || !strcmp(token, "list")) 1801 list_tcu_channels(); 1802 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1803 new_tcu_channel(tcu); 1804 else if (!strcmp(token, "C") || !strcmp(token, "clock")) 1805 set_tcu_clock(); 1806 else if (!strcmp(token, "d") || !strcmp(token, "disable")) 1807 disable_tcu_counter(); 1808 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1809 enable_tcu_counter(); 1810 else if (!strcmp(token, "f") || !strcmp(token, "full")) 1811 set_tcu_full(); 1812 else if (!strcmp(token, "fm") || !strcmp(token, "full-mask")) 1813 set_tcu_mask(1, 1); 1814 else if (!strcmp(token, "fu") || !strcmp(token, "full-unmask")) 1815 set_tcu_mask(1, 0); 1816 else if (!strcmp(token, "h") || !strcmp(token, "half")) 1817 set_tcu_half(); 1818 else if (!strcmp(token, "hm") || !strcmp(token, "half-mask")) 1819 set_tcu_mask(0, 1); 1820 else if (!strcmp(token, "hu") || !strcmp(token, "half-unmask")) 1821 set_tcu_mask(0, 0); 1822 else if (!strcmp(token, "p") || !strcmp(token, "prescale")) 1823 set_tcu_prescale(); 1824 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1825 set_tcu_counter(); 1826 else if (!strcmp(token, "w") || !strcmp(token, "wait")) 1827 tcu_wait(); 1828 else 1829 printf("tcu channel | clock | disable | enable | full | full-mask | " \ 1830 "full-unmask | half | half-mask | half-unmask | list | mask | " \ 1831 "prescale | set | unmask | wait\n"); 1832 } 1833 else 1834 list_tcu_channels(); 1835 } 1836 1837 1838 1839 int main(void) 1840 { 1841 l4_addr_t aic_base = 0, aic_base_end = 0; 1842 l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; 1843 l4_addr_t cpm_base = 0, cpm_base_end = 0; 1844 l4_addr_t dma_base = 0, dma_base_end = 0; 1845 l4_addr_t gpio_base = 0, gpio_base_end = 0; 1846 l4_addr_t i2c_base = 0, i2c_base_end = 0; 1847 l4_addr_t msc_base = 0, msc_base_end = 0; 1848 l4_addr_t msc_phys_base = 0, msc_phys_base_end = 0; 1849 l4_addr_t rtc_base = 0, rtc_base_end = 0; 1850 l4_addr_t ssi_base = 0, ssi_base_end = 0; 1851 l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; 1852 l4_addr_t tcu_base = 0, tcu_base_end = 0; 1853 void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *msc, *rtc, *spi, *tcu; 1854 int result = 0; 1855 unsigned int port; 1856 1857 icucap = l4re_env_get_cap("icu"); 1858 1859 /* Obtain resource details describing I/O memory. */ 1860 1861 printf("Access CPM...\n"); 1862 1863 if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) 1864 return 1; 1865 1866 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 1867 1868 cpm = cpm_init(cpm_base); 1869 1870 printf("Access DMA...\n"); 1871 1872 if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) 1873 return 1; 1874 1875 printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); 1876 1877 dma = dma_init(dma_base, dma_base_end, cpm); 1878 1879 if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) 1880 return 1; 1881 1882 printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); 1883 1884 if (init_dma()) 1885 return 1; 1886 1887 dma_enable(dma); 1888 1889 printf("Access GPIO...\n"); 1890 1891 if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) 1892 return 1; 1893 1894 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 1895 1896 for (port = 0; port < num_gpio_ports; port++) 1897 gpio[port] = gpio_init(gpio_base + port * 0x100, gpio_base + (port + 1) * 0x100, 1898 32, gpio_ports[port].pull_ups, gpio_ports[port].pull_downs); 1899 1900 printf("Access I2C...\n"); 1901 1902 if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) 1903 return 1; 1904 1905 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 1906 1907 i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); 1908 1909 if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) 1910 return 1; 1911 1912 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 1913 1914 printf("Access AIC...\n"); 1915 1916 if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, 1917 &aic_phys_base, &aic_phys_base_end)) < 0) 1918 return 1; 1919 1920 printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); 1921 1922 aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); 1923 1924 printf("Access MSC...\n"); 1925 1926 if ((result = get_memory_complete(io_memory_regions[MSC], &msc_base, &msc_base_end, 1927 &msc_phys_base, &msc_phys_base_end)) < 0) 1928 return 1; 1929 1930 printf("MSC at 0x%lx...0x%lx.\n", msc_base, msc_base_end); 1931 1932 msc = msc_init(msc_phys_base, msc_base, msc_base_end); 1933 1934 if (get_irq(io_memory_regions[MSC], &msc_irq_start, &msc_irq_end) < 0) 1935 return 1; 1936 1937 printf("IRQ range at %d...%d.\n", msc_irq_start, msc_irq_end); 1938 1939 printf("Access RTC...\n"); 1940 1941 if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) 1942 return 1; 1943 1944 printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); 1945 1946 rtc = rtc_init(rtc_base, cpm); 1947 1948 printf("Access SSI...\n"); 1949 1950 if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, 1951 &ssi_phys_base, &ssi_phys_base_end)) < 0) 1952 return 1; 1953 1954 printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); 1955 1956 spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); 1957 1958 printf("Access TCU...\n"); 1959 1960 if ((result = get_memory(io_memory_regions[TCU], &tcu_base, &tcu_base_end)) < 0) 1961 return 1; 1962 1963 printf("TCU at 0x%lx...0x%lx.\n", tcu_base, tcu_base_end); 1964 1965 tcu = tcu_init(tcu_base, tcu_base_end); 1966 1967 if (get_irq(io_memory_regions[TCU], &tcu_irq_start, &tcu_irq_end) < 0) 1968 return 1; 1969 1970 printf("IRQ range at %d...%d.\n", tcu_irq_start, tcu_irq_end); 1971 1972 if (init_tcu()) 1973 return 1; 1974 1975 /* Start the interactive session. */ 1976 1977 printf("aic, cpm, dma, gpio, i2c, msc, rtc, spi, tcu\n"); 1978 1979 while (1) 1980 { 1981 char cmdline[256], *token; 1982 1983 printf("> "); 1984 1985 token = fgets(cmdline, 256, stdin); 1986 1987 if (token == NULL) 1988 break; 1989 1990 if ((token = strtok(cmdline, delimiters)) == NULL) 1991 continue; 1992 1993 /* AIC/I2S commands. */ 1994 1995 if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) 1996 handle_aic(aic); 1997 1998 /* CPM commands. */ 1999 2000 else if (!strcmp(token, "c") || !strcmp(token, "cpm")) 2001 handle_cpm(cpm); 2002 2003 /* DMA commands. */ 2004 2005 else if (!strcmp(token, "d") || !strcmp(token, "dma")) 2006 handle_dma(dma); 2007 2008 /* GPIO commands. */ 2009 2010 else if (!strcmp(token, "g") || !strcmp(token, "gpio")) 2011 handle_gpio(gpio); 2012 2013 /* I2C commands. */ 2014 2015 else if (!strcmp(token, "i") || !strcmp(token, "i2c")) 2016 handle_i2c(i2c); 2017 2018 /* MSC commands. */ 2019 2020 else if (!strcmp(token, "M") || !strcmp(token, "msc")) 2021 handle_msc(msc); 2022 2023 /* Generic memory commands. */ 2024 2025 else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) 2026 handle_memory(); 2027 2028 /* RTC commands. */ 2029 2030 else if (!strcmp(token, "r") || !strcmp(token, "rtc")) 2031 handle_rtc(rtc, cpm); 2032 2033 /* SPI commands. */ 2034 2035 else if (!strcmp(token, "s") || !strcmp(token, "spi")) 2036 handle_spi(spi, gpio); 2037 2038 /* TCU commands. */ 2039 2040 else if (!strcmp(token, "t") || !strcmp(token, "tcu")) 2041 handle_tcu(tcu); 2042 2043 /* Comments and blank lines. */ 2044 2045 else if (strncmp(token, "#", 1) && strlen(token)) 2046 printf("Command?\n"); 2047 } 2048 2049 printf("End of session.\n"); 2050 return 0; 2051 }