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 /* RTC operations. */ 1116 1117 static void rtc_reset(void *rtc, void *cpm) 1118 { 1119 unsigned int seconds; 1120 1121 if (!read_number("Seconds", &seconds)) 1122 return; 1123 1124 /* NOTE: Assuming EXCLK/512 as RTC source. */ 1125 1126 uint32_t rtc_seconds = rtc_get_seconds(rtc); 1127 uint32_t value = seconds * cpm_get_frequency(cpm, Clock_external) / 512; 1128 1129 rtc_alarm_disable(rtc); 1130 rtc_set_alarm_seconds(rtc, rtc_seconds + value); 1131 rtc_hibernate(rtc); 1132 } 1133 1134 static void _rtc_set_seconds(void *rtc, int alarm) 1135 { 1136 unsigned int seconds; 1137 1138 if (!read_number("Seconds", &seconds)) 1139 return; 1140 1141 (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); 1142 } 1143 1144 1145 1146 /* SPI operations. */ 1147 1148 static void new_spi_channel(void *spi, void *gpio[]) 1149 { 1150 unsigned int control_port, control_pin, control_alt_func_input; 1151 int control_alt_func; 1152 void *control_chip; 1153 int num = get_channel_number(num_spi_channels); 1154 void *channel; 1155 uint32_t frequency; 1156 1157 if (num < 0) 1158 return; 1159 1160 channel = get_channel(num_dma_channels, dma_channels, NULL); 1161 1162 if (channel == NULL) 1163 return; 1164 1165 if (!read_number("Frequency", &frequency)) 1166 return; 1167 1168 if (!get_port_and_pin(&control_port, &control_pin)) 1169 control_chip = NULL; 1170 else 1171 control_chip = gpio[control_port]; 1172 1173 if (!read_number("Function", &control_alt_func_input)) 1174 control_alt_func = -1; 1175 else 1176 control_alt_func = control_alt_func_input; 1177 1178 spi_channels[num] = spi_get_channel(spi, num, channel, frequency, 1179 control_chip, control_pin, 1180 control_alt_func); 1181 } 1182 1183 static void new_spi_channel_gpio(void *gpio[]) 1184 { 1185 unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, 1186 enable_pin, control_port, control_pin; 1187 void *control_chip; 1188 int num = get_channel_number(num_spi_channels); 1189 uint32_t frequency; 1190 1191 if (num < 0) 1192 return; 1193 1194 if (!read_number("Frequency", &frequency)) 1195 return; 1196 1197 if (!get_port_and_pin(&clock_port, &clock_pin)) 1198 return; 1199 1200 if (!get_port_and_pin(&data_port, &data_pin)) 1201 return; 1202 1203 if (!get_port_and_pin(&enable_port, &enable_pin)) 1204 return; 1205 1206 if (!get_port_and_pin(&control_port, &control_pin)) 1207 control_chip = NULL; 1208 else 1209 control_chip = gpio[control_port]; 1210 1211 spi_channels[num] = spi_get_channel_gpio(frequency, 1212 gpio[clock_port], clock_pin, 1213 gpio[data_port], data_pin, 1214 gpio[enable_port], enable_pin, 1215 control_chip, control_pin); 1216 } 1217 1218 static void spi_control(int acquire) 1219 { 1220 unsigned int level; 1221 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1222 1223 if (acquire) 1224 { 1225 if (!read_number("Level", &level)) 1226 return; 1227 1228 spi_acquire_control(channel, level); 1229 } 1230 else 1231 spi_release_control(channel); 1232 } 1233 1234 static void spi_send_data(void) 1235 { 1236 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1237 int bytes; 1238 unsigned int byte; 1239 uint8_t buffer[256]; 1240 1241 bytes = 0; 1242 1243 while (read_encoded_number(NULL, "%2x", &byte)) 1244 { 1245 buffer[bytes] = (uint8_t) (byte & 0xff); 1246 bytes++; 1247 } 1248 1249 spi_send(channel, bytes, buffer); 1250 } 1251 1252 static void spi_send_data_units(void) 1253 { 1254 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1255 unsigned int char_size, unit_size, value; 1256 uint8_t buffer[256]; 1257 int byte = 0; 1258 1259 if (!read_number("Unit size", &unit_size)) 1260 return; 1261 1262 if (!read_number("Character size", &char_size)) 1263 return; 1264 1265 /* Read hex digits for bytes. Multiple bytes make up each unit and are read 1266 from most to least significant. Where the unit size exceeds the character 1267 size, the last bit before the character indicates the GPC bit. */ 1268 1269 while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) 1270 buffer[byte++] = value; 1271 1272 /* Explicitly indicate big endian data. */ 1273 1274 spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); 1275 } 1276 1277 static void spi_transfer_data(void) 1278 { 1279 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1280 struct dma_region *dma_region = NULL, *desc_region; 1281 struct memory_region *memory_region = NULL; 1282 unsigned int char_size, unit_size; 1283 uint32_t count, transferred; 1284 int using_dma; 1285 1286 if (channel == NULL) 1287 return; 1288 1289 if (!read_flag("Using DMA", &using_dma, "d", 1)) 1290 return; 1291 1292 if (using_dma) 1293 dma_region = _get_dma_region(); 1294 else 1295 memory_region = _get_memory_region(); 1296 1297 if ((dma_region == NULL) && (memory_region == NULL)) 1298 return; 1299 1300 if (!read_number("Transfer size", &count)) 1301 return; 1302 1303 if (!read_number("Unit size", &unit_size)) 1304 return; 1305 1306 if (!read_number("Character size", &char_size)) 1307 return; 1308 1309 if (using_dma) 1310 { 1311 desc_region = _get_dma_region(); 1312 1313 transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, 1314 unit_size, char_size, 1315 desc_region != NULL ? desc_region->vaddr : 0, 1316 desc_region != NULL ? desc_region->paddr : 0); 1317 } 1318 else 1319 transferred = spi_transfer(channel, memory_region->addr, 0, count, 1320 unit_size, char_size, 0, 0); 1321 1322 printf("Transferred: %d\n", transferred); 1323 } 1324 1325 1326 1327 /* TCU configuration. */ 1328 1329 static l4_uint32_t tcu_irq_start = 0, tcu_irq_end = 0; 1330 1331 /* TCU operations. */ 1332 1333 static int init_tcu(void) 1334 { 1335 /* Here, only one IRQ is used. */ 1336 1337 tcu_irq = l4re_util_cap_alloc(); 1338 return init_irq(0, tcu_irq, tcu_irq_start, tcu_irq_end); 1339 } 1340 1341 static void disable_tcu_counter(void) 1342 { 1343 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1344 1345 if (channel == NULL) 1346 return; 1347 1348 tcu_disable(channel); 1349 } 1350 1351 static void enable_tcu_counter(void) 1352 { 1353 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1354 1355 if (channel == NULL) 1356 return; 1357 1358 tcu_enable(channel); 1359 } 1360 1361 static void new_tcu_channel(void *tcu) 1362 { 1363 int num = get_channel_number(num_tcu_channels); 1364 1365 if (num < 0) 1366 return; 1367 1368 tcu_channels[num] = tcu_get_channel(tcu, num, tcu_irq); 1369 } 1370 1371 static void list_tcu_channels(void) 1372 { 1373 unsigned int num; 1374 void *channel; 1375 1376 printf(" Clock.. Counter......... Mask..............\n"); 1377 printf("Channel Status C Pre Cnt Half Full Half Full Int\n"); 1378 1379 for (num = 0; num < num_tcu_channels; num++) 1380 { 1381 printf("%d ", num); 1382 1383 channel = tcu_channels[num]; 1384 1385 if (channel == NULL) 1386 printf("inactive\n"); 1387 else 1388 printf("%s %d %4d %04x %04x %04x %s %s %s\n", 1389 tcu_is_enabled(channel) ? "enabled " : "disabled", 1390 tcu_get_clock(channel), 1391 tcu_get_prescale(channel), 1392 tcu_get_counter(channel), 1393 tcu_get_half_data_value(channel), 1394 tcu_get_full_data_value(channel), 1395 tcu_get_half_data_mask(channel) ? "masked " : "unmasked", 1396 tcu_get_full_data_mask(channel) ? "masked " : "unmasked", 1397 tcu_have_interrupt(channel) ? "!" : "_"); 1398 } 1399 } 1400 1401 static void set_tcu_counter(void) 1402 { 1403 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1404 uint32_t counter; 1405 1406 if (channel == NULL) 1407 return; 1408 1409 if (!read_number("Value", &counter)) 1410 return; 1411 1412 tcu_set_counter(channel, counter); 1413 } 1414 1415 static void set_tcu_clock(void) 1416 { 1417 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1418 uint32_t clock; 1419 1420 if (channel == NULL) 1421 return; 1422 1423 if (!read_number("Clock", &clock)) 1424 return; 1425 1426 tcu_set_clock(channel, clock); 1427 } 1428 1429 static void set_tcu_full(void) 1430 { 1431 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1432 uint32_t value; 1433 1434 if (channel == NULL) 1435 return; 1436 1437 if (!read_number("Full value", &value)) 1438 return; 1439 1440 tcu_set_full_data_value(channel, value); 1441 } 1442 1443 static void set_tcu_half(void) 1444 { 1445 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1446 uint32_t value; 1447 1448 if (channel == NULL) 1449 return; 1450 1451 if (!read_number("Half value", &value)) 1452 return; 1453 1454 tcu_set_half_data_value(channel, value); 1455 } 1456 1457 static void set_tcu_mask(int full_mask, int masked) 1458 { 1459 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1460 1461 if (channel == NULL) 1462 return; 1463 1464 if (full_mask) 1465 tcu_set_full_data_mask(channel, masked); 1466 else 1467 tcu_set_half_data_mask(channel, masked); 1468 } 1469 1470 static void set_tcu_prescale(void) 1471 { 1472 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1473 uint32_t prescale; 1474 1475 if (channel == NULL) 1476 return; 1477 1478 if (!read_number("Prescale", &prescale)) 1479 return; 1480 1481 tcu_set_prescale(channel, prescale); 1482 } 1483 1484 static void tcu_wait(void) 1485 { 1486 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1487 uint32_t timeout; 1488 1489 if (channel == NULL) 1490 return; 1491 1492 if (!read_number("Timeout", &timeout)) 1493 return; 1494 1495 printf("IRQ received: %s\n", tcu_wait_for_irq(channel, timeout) ? "yes" : "no"); 1496 } 1497 1498 1499 1500 /* Command processing. */ 1501 1502 static void handle_aic(void *aic) 1503 { 1504 char *token; 1505 1506 if ((token = read_token(NULL)) != NULL) 1507 { 1508 if (!strcmp(token, "l") || !strcmp(token, "list")) 1509 list_channels(num_aic_channels, aic_channels); 1510 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1511 new_aic_channel(aic); 1512 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1513 aic_transfer_data(); 1514 else 1515 printf("aic channel | list | transfer\n"); 1516 } 1517 else 1518 list_channels(num_aic_channels, aic_channels); 1519 } 1520 1521 static void handle_cpm(void *cpm) 1522 { 1523 char *token; 1524 1525 if ((token = read_token(NULL)) != NULL) 1526 { 1527 if (!strcmp(token, "l") || !strcmp(token, "list")) 1528 list_clocks(cpm); 1529 else if (!strcmp(token, "f") || !strcmp(token, "frequency")) 1530 set_frequency(cpm); 1531 else if (!strcmp(token, "p") || !strcmp(token, "parameters")) 1532 set_parameters(cpm); 1533 else if (!strcmp(token, "s") || !strcmp(token, "source")) 1534 set_source(cpm); 1535 else if (!strcmp(token, "start")) 1536 start_clock(cpm); 1537 else if (!strcmp(token, "stop")) 1538 stop_clock(cpm); 1539 else 1540 printf("cpm list | frequency | parameters | source | start | stop\n"); 1541 } 1542 else 1543 list_clocks(cpm); 1544 } 1545 1546 static void handle_dma(void *dma) 1547 { 1548 char *token; 1549 1550 if ((token = read_token(NULL)) != NULL) 1551 { 1552 if (!strcmp(token, "l") || !strcmp(token, "list")) 1553 list_channels(num_dma_channels, dma_channels); 1554 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1555 new_dma_channel(dma); 1556 else if (!strcmp(token, "r") || !strcmp(token, "region")) 1557 new_dma_region(); 1558 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1559 set_dma_region(); 1560 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1561 dma_transfer_data(); 1562 else 1563 printf("dma channel | list | region | set | transfer\n"); 1564 } 1565 else 1566 list_channels(num_dma_channels, dma_channels); 1567 } 1568 1569 static void handle_gpio(void *gpio[]) 1570 { 1571 char *token; 1572 1573 if ((token = read_token(NULL)) != NULL) 1574 { 1575 if (!strcmp(token, "l") || !strcmp(token, "list")) 1576 list_gpios(gpio); 1577 else if (!strcmp(token, "a") || !strcmp(token, "alt")) 1578 set_gpio_alt_func(gpio); 1579 else if (!strcmp(token, "i") || !strcmp(token, "io")) 1580 set_gpio_pad(gpio); 1581 else if (!strcmp(token, "p") || !strcmp(token, "pull")) 1582 set_gpio_pull(gpio); 1583 else 1584 printf("gpio list | alt | io | pull\n"); 1585 } 1586 else 1587 list_gpios(gpio); 1588 } 1589 1590 static void handle_i2c(void *i2c) 1591 { 1592 char *token; 1593 1594 if ((token = read_token(NULL)) != NULL) 1595 { 1596 if (!strcmp(token, "l") || !strcmp(token, "list")) 1597 list_i2c_channels(); 1598 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1599 new_i2c_channel(i2c); 1600 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1601 i2c_get(); 1602 else if (!strcmp(token, "s") || !strcmp(token, "scan")) 1603 i2c_scan(); 1604 else 1605 printf("i2c channel | get | list | scan\n"); 1606 } 1607 else 1608 list_i2c_channels(); 1609 } 1610 1611 static void handle_memory(void) 1612 { 1613 char *token; 1614 1615 if ((token = read_token(NULL)) != NULL) 1616 { 1617 if (!strcmp(token, "r") || !strcmp(token, "region")) 1618 new_memory_region(); 1619 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1620 set_memory_region(); 1621 else 1622 printf("memory region | set\n"); 1623 } 1624 else 1625 list_memory_regions(); 1626 } 1627 1628 static void handle_rtc(void *rtc, void *cpm) 1629 { 1630 char *token; 1631 1632 if ((token = read_token(NULL)) != NULL) 1633 { 1634 if (!strcmp(token, "d") || !strcmp(token, "disable")) 1635 rtc_disable(rtc); 1636 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1637 rtc_enable(rtc); 1638 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1639 printf("seconds = %d\n", rtc_get_seconds(rtc)); 1640 else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) 1641 printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); 1642 else if (!strcmp(token, "p") || !strcmp(token, "power-down")) 1643 rtc_power_down(rtc); 1644 else if (!strcmp(token, "r") || !strcmp(token, "reset")) 1645 rtc_reset(rtc, cpm); 1646 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1647 _rtc_set_seconds(rtc, 0); 1648 else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) 1649 _rtc_set_seconds(rtc, 1); 1650 else 1651 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1652 } 1653 else 1654 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1655 } 1656 1657 static void handle_spi(void *spi, void *gpio[]) 1658 { 1659 char *token; 1660 1661 if ((token = read_token(NULL)) != NULL) 1662 { 1663 if (!strcmp(token, "l") || !strcmp(token, "list")) 1664 list_channels(num_spi_channels, spi_channels); 1665 else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) 1666 spi_control(1); 1667 else if (!strcmp(token, "r") || !strcmp(token, "control-release")) 1668 spi_control(0); 1669 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1670 new_spi_channel(spi, gpio); 1671 else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) 1672 new_spi_channel_gpio(gpio); 1673 else if (!strcmp(token, "s") || !strcmp(token, "send")) 1674 spi_send_data(); 1675 else if (!strcmp(token, "S") || !strcmp(token, "send-units")) 1676 spi_send_data_units(); 1677 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1678 spi_transfer_data(); 1679 else 1680 printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); 1681 } 1682 else 1683 list_channels(num_spi_channels, spi_channels); 1684 } 1685 1686 static void handle_tcu(void *tcu) 1687 { 1688 char *token; 1689 1690 if ((token = read_token(NULL)) != NULL) 1691 { 1692 if (!strcmp(token, "l") || !strcmp(token, "list")) 1693 list_tcu_channels(); 1694 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1695 new_tcu_channel(tcu); 1696 else if (!strcmp(token, "C") || !strcmp(token, "clock")) 1697 set_tcu_clock(); 1698 else if (!strcmp(token, "d") || !strcmp(token, "disable")) 1699 disable_tcu_counter(); 1700 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1701 enable_tcu_counter(); 1702 else if (!strcmp(token, "f") || !strcmp(token, "full")) 1703 set_tcu_full(); 1704 else if (!strcmp(token, "fm") || !strcmp(token, "full-mask")) 1705 set_tcu_mask(1, 1); 1706 else if (!strcmp(token, "fu") || !strcmp(token, "full-unmask")) 1707 set_tcu_mask(1, 0); 1708 else if (!strcmp(token, "h") || !strcmp(token, "half")) 1709 set_tcu_half(); 1710 else if (!strcmp(token, "hm") || !strcmp(token, "half-mask")) 1711 set_tcu_mask(0, 1); 1712 else if (!strcmp(token, "hu") || !strcmp(token, "half-unmask")) 1713 set_tcu_mask(0, 0); 1714 else if (!strcmp(token, "p") || !strcmp(token, "prescale")) 1715 set_tcu_prescale(); 1716 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1717 set_tcu_counter(); 1718 else if (!strcmp(token, "w") || !strcmp(token, "wait")) 1719 tcu_wait(); 1720 else 1721 printf("tcu channel | clock | disable | enable | full | full-mask | " \ 1722 "full-unmask | half | half-mask | half-unmask | list | mask | " \ 1723 "prescale | set | unmask | wait\n"); 1724 } 1725 else 1726 list_tcu_channels(); 1727 } 1728 1729 1730 1731 int main(void) 1732 { 1733 l4_addr_t aic_base = 0, aic_base_end = 0; 1734 l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; 1735 l4_addr_t cpm_base = 0, cpm_base_end = 0; 1736 l4_addr_t dma_base = 0, dma_base_end = 0; 1737 l4_addr_t gpio_base = 0, gpio_base_end = 0; 1738 l4_addr_t i2c_base = 0, i2c_base_end = 0; 1739 l4_addr_t rtc_base = 0, rtc_base_end = 0; 1740 l4_addr_t ssi_base = 0, ssi_base_end = 0; 1741 l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; 1742 l4_addr_t tcu_base = 0, tcu_base_end = 0; 1743 void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *rtc, *spi, *tcu; 1744 int result = 0; 1745 unsigned int port; 1746 1747 icucap = l4re_env_get_cap("icu"); 1748 1749 /* Obtain resource details describing I/O memory. */ 1750 1751 printf("Access CPM...\n"); 1752 1753 if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) 1754 return 1; 1755 1756 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 1757 1758 cpm = cpm_init(cpm_base); 1759 1760 printf("Access DMA...\n"); 1761 1762 if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) 1763 return 1; 1764 1765 printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); 1766 1767 dma = dma_init(dma_base, dma_base_end, cpm); 1768 1769 if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) 1770 return 1; 1771 1772 printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); 1773 1774 if (init_dma()) 1775 return 1; 1776 1777 dma_enable(dma); 1778 1779 printf("Access GPIO...\n"); 1780 1781 if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) 1782 return 1; 1783 1784 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 1785 1786 for (port = 0; port < num_gpio_ports; port++) 1787 gpio[port] = gpio_init(gpio_base + port * 0x100, gpio_base + (port + 1) * 0x100, 1788 32, gpio_ports[port].pull_ups, gpio_ports[port].pull_downs); 1789 1790 printf("Access I2C...\n"); 1791 1792 if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) 1793 return 1; 1794 1795 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 1796 1797 i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); 1798 1799 if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) 1800 return 1; 1801 1802 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 1803 1804 printf("Access AIC...\n"); 1805 1806 if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, 1807 &aic_phys_base, &aic_phys_base_end)) < 0) 1808 return 1; 1809 1810 printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); 1811 1812 aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); 1813 1814 printf("Access RTC...\n"); 1815 1816 if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) 1817 return 1; 1818 1819 printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); 1820 1821 rtc = rtc_init(rtc_base, cpm); 1822 1823 printf("Access SSI...\n"); 1824 1825 if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, 1826 &ssi_phys_base, &ssi_phys_base_end)) < 0) 1827 return 1; 1828 1829 printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); 1830 1831 spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); 1832 1833 printf("Access TCU...\n"); 1834 1835 if ((result = get_memory(io_memory_regions[TCU], &tcu_base, &tcu_base_end)) < 0) 1836 return 1; 1837 1838 printf("TCU at 0x%lx...0x%lx.\n", tcu_base, tcu_base_end); 1839 1840 tcu = tcu_init(tcu_base, tcu_base_end); 1841 1842 if (get_irq(io_memory_regions[TCU], &tcu_irq_start, &tcu_irq_end) < 0) 1843 return 1; 1844 1845 printf("IRQ range at %d...%d.\n", tcu_irq_start, tcu_irq_end); 1846 1847 if (init_tcu()) 1848 return 1; 1849 1850 /* Start the interactive session. */ 1851 1852 printf("aic, cpm, dma, gpio, i2c, rtc, spi, tcu\n"); 1853 1854 while (1) 1855 { 1856 char cmdline[256], *token; 1857 1858 printf("> "); 1859 1860 token = fgets(cmdline, 256, stdin); 1861 1862 if (token == NULL) 1863 break; 1864 1865 if ((token = strtok(cmdline, delimiters)) == NULL) 1866 continue; 1867 1868 /* AIC/I2S commands. */ 1869 1870 if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) 1871 handle_aic(aic); 1872 1873 /* CPM commands. */ 1874 1875 else if (!strcmp(token, "c") || !strcmp(token, "cpm")) 1876 handle_cpm(cpm); 1877 1878 /* DMA commands. */ 1879 1880 else if (!strcmp(token, "d") || !strcmp(token, "dma")) 1881 handle_dma(dma); 1882 1883 /* GPIO commands. */ 1884 1885 else if (!strcmp(token, "g") || !strcmp(token, "gpio")) 1886 handle_gpio(gpio); 1887 1888 /* I2C commands. */ 1889 1890 else if (!strcmp(token, "i") || !strcmp(token, "i2c")) 1891 handle_i2c(i2c); 1892 1893 /* Generic memory commands. */ 1894 1895 else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) 1896 handle_memory(); 1897 1898 /* RTC commands. */ 1899 1900 else if (!strcmp(token, "r") || !strcmp(token, "rtc")) 1901 handle_rtc(rtc, cpm); 1902 1903 /* SPI commands. */ 1904 1905 else if (!strcmp(token, "s") || !strcmp(token, "spi")) 1906 handle_spi(spi, gpio); 1907 1908 /* TCU commands. */ 1909 1910 else if (!strcmp(token, "t") || !strcmp(token, "tcu")) 1911 handle_tcu(tcu); 1912 1913 /* Comments and blank lines. */ 1914 1915 else if (strncmp(token, "#", 1) && strlen(token)) 1916 printf("Command?\n"); 1917 } 1918 1919 printf("End of session.\n"); 1920 return 0; 1921 }