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 int num = get_channel_number(num_tcu_channels); 1344 void *channel; 1345 1346 if (num < 0) 1347 return; 1348 1349 channel = tcu_channels[num]; 1350 1351 if (channel == NULL) 1352 return; 1353 1354 tcu_disable(channel); 1355 } 1356 1357 static void enable_tcu_counter(void) 1358 { 1359 int num = get_channel_number(num_tcu_channels); 1360 void *channel; 1361 1362 if (num < 0) 1363 return; 1364 1365 channel = tcu_channels[num]; 1366 1367 if (channel == NULL) 1368 return; 1369 1370 tcu_enable(channel); 1371 } 1372 1373 static void new_tcu_channel(void *tcu) 1374 { 1375 int num = get_channel_number(num_tcu_channels); 1376 1377 if (num < 0) 1378 return; 1379 1380 tcu_channels[num] = tcu_get_channel(tcu, num, tcu_irq); 1381 } 1382 1383 static void list_tcu_channels(void) 1384 { 1385 unsigned int num; 1386 void *channel; 1387 1388 printf(" Clock.. Counter......... Mask..............\n"); 1389 printf("Channel Status C Pre Cnt Half Full Half Full Int\n"); 1390 1391 for (num = 0; num < num_tcu_channels; num++) 1392 { 1393 printf("%d ", num); 1394 1395 channel = tcu_channels[num]; 1396 1397 if (channel == NULL) 1398 printf("inactive\n"); 1399 else 1400 printf("%s %d %4d %04x %04x %04x %s %s %s\n", 1401 tcu_is_enabled(channel) ? "enabled " : "disabled", 1402 tcu_get_clock(channel), 1403 tcu_get_prescale(channel), 1404 tcu_get_counter(channel), 1405 tcu_get_half_data_value(channel), 1406 tcu_get_full_data_value(channel), 1407 tcu_get_half_data_mask(channel) ? "masked " : "unmasked", 1408 tcu_get_full_data_mask(channel) ? "masked " : "unmasked", 1409 tcu_have_interrupt(channel) ? "!" : "_"); 1410 } 1411 } 1412 1413 static void set_tcu_counter(void) 1414 { 1415 int num = get_channel_number(num_tcu_channels); 1416 void *channel; 1417 uint32_t counter; 1418 1419 if (num < 0) 1420 return; 1421 1422 channel = tcu_channels[num]; 1423 1424 if (channel == NULL) 1425 return; 1426 1427 if (!read_number("Value", &counter)) 1428 return; 1429 1430 tcu_set_counter(channel, counter); 1431 } 1432 1433 static void set_tcu_clock(void) 1434 { 1435 int num = get_channel_number(num_tcu_channels); 1436 void *channel; 1437 uint32_t clock; 1438 1439 if (num < 0) 1440 return; 1441 1442 channel = tcu_channels[num]; 1443 1444 if (channel == NULL) 1445 return; 1446 1447 if (!read_number("Clock", &clock)) 1448 return; 1449 1450 tcu_set_clock(channel, clock); 1451 } 1452 1453 static void set_tcu_full(void) 1454 { 1455 int num = get_channel_number(num_tcu_channels); 1456 void *channel; 1457 uint32_t value; 1458 1459 if (num < 0) 1460 return; 1461 1462 channel = tcu_channels[num]; 1463 1464 if (channel == NULL) 1465 return; 1466 1467 if (!read_number("Full value", &value)) 1468 return; 1469 1470 tcu_set_full_data_value(channel, value); 1471 } 1472 1473 static void set_tcu_half(void) 1474 { 1475 int num = get_channel_number(num_tcu_channels); 1476 void *channel; 1477 uint32_t value; 1478 1479 if (num < 0) 1480 return; 1481 1482 channel = tcu_channels[num]; 1483 1484 if (channel == NULL) 1485 return; 1486 1487 if (!read_number("Half value", &value)) 1488 return; 1489 1490 tcu_set_half_data_value(channel, value); 1491 } 1492 1493 static void set_tcu_mask(int full_mask, int masked) 1494 { 1495 int num = get_channel_number(num_tcu_channels); 1496 void *channel; 1497 1498 if (num < 0) 1499 return; 1500 1501 channel = tcu_channels[num]; 1502 1503 if (channel == NULL) 1504 return; 1505 1506 if (full_mask) 1507 tcu_set_full_data_mask(channel, masked); 1508 else 1509 tcu_set_half_data_mask(channel, masked); 1510 } 1511 1512 static void set_tcu_prescale(void) 1513 { 1514 int num = get_channel_number(num_tcu_channels); 1515 void *channel; 1516 uint32_t prescale; 1517 1518 if (num < 0) 1519 return; 1520 1521 channel = tcu_channels[num]; 1522 1523 if (channel == NULL) 1524 return; 1525 1526 if (!read_number("Prescale", &prescale)) 1527 return; 1528 1529 tcu_set_prescale(channel, prescale); 1530 } 1531 1532 static void tcu_wait(void) 1533 { 1534 int num = get_channel_number(num_tcu_channels); 1535 void *channel; 1536 uint32_t timeout; 1537 1538 if (num < 0) 1539 return; 1540 1541 channel = tcu_channels[num]; 1542 1543 if (channel == NULL) 1544 return; 1545 1546 if (!read_number("Timeout", &timeout)) 1547 return; 1548 1549 printf("IRQ received: %s\n", tcu_wait_for_irq(channel, timeout) ? "yes" : "no"); 1550 } 1551 1552 1553 1554 /* Command processing. */ 1555 1556 static void handle_aic(void *aic) 1557 { 1558 char *token; 1559 1560 if ((token = read_token(NULL)) != NULL) 1561 { 1562 if (!strcmp(token, "l") || !strcmp(token, "list")) 1563 list_channels(num_aic_channels, aic_channels); 1564 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1565 new_aic_channel(aic); 1566 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1567 aic_transfer_data(); 1568 else 1569 printf("aic channel | list | transfer\n"); 1570 } 1571 else 1572 list_channels(num_aic_channels, aic_channels); 1573 } 1574 1575 static void handle_cpm(void *cpm) 1576 { 1577 char *token; 1578 1579 if ((token = read_token(NULL)) != NULL) 1580 { 1581 if (!strcmp(token, "l") || !strcmp(token, "list")) 1582 list_clocks(cpm); 1583 else if (!strcmp(token, "f") || !strcmp(token, "frequency")) 1584 set_frequency(cpm); 1585 else if (!strcmp(token, "p") || !strcmp(token, "parameters")) 1586 set_parameters(cpm); 1587 else if (!strcmp(token, "s") || !strcmp(token, "source")) 1588 set_source(cpm); 1589 else if (!strcmp(token, "start")) 1590 start_clock(cpm); 1591 else if (!strcmp(token, "stop")) 1592 stop_clock(cpm); 1593 else 1594 printf("cpm list | frequency | parameters | source | start | stop\n"); 1595 } 1596 else 1597 list_clocks(cpm); 1598 } 1599 1600 static void handle_dma(void *dma) 1601 { 1602 char *token; 1603 1604 if ((token = read_token(NULL)) != NULL) 1605 { 1606 if (!strcmp(token, "l") || !strcmp(token, "list")) 1607 list_channels(num_dma_channels, dma_channels); 1608 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1609 new_dma_channel(dma); 1610 else if (!strcmp(token, "r") || !strcmp(token, "region")) 1611 new_dma_region(); 1612 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1613 set_dma_region(); 1614 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1615 dma_transfer_data(); 1616 else 1617 printf("dma channel | list | region | set | transfer\n"); 1618 } 1619 else 1620 list_channels(num_dma_channels, dma_channels); 1621 } 1622 1623 static void handle_gpio(void *gpio[]) 1624 { 1625 char *token; 1626 1627 if ((token = read_token(NULL)) != NULL) 1628 { 1629 if (!strcmp(token, "l") || !strcmp(token, "list")) 1630 list_gpios(gpio); 1631 else if (!strcmp(token, "a") || !strcmp(token, "alt")) 1632 set_gpio_alt_func(gpio); 1633 else if (!strcmp(token, "i") || !strcmp(token, "io")) 1634 set_gpio_pad(gpio); 1635 else if (!strcmp(token, "p") || !strcmp(token, "pull")) 1636 set_gpio_pull(gpio); 1637 else 1638 printf("gpio list | alt | io | pull\n"); 1639 } 1640 else 1641 list_gpios(gpio); 1642 } 1643 1644 static void handle_i2c(void *i2c) 1645 { 1646 char *token; 1647 1648 if ((token = read_token(NULL)) != NULL) 1649 { 1650 if (!strcmp(token, "l") || !strcmp(token, "list")) 1651 list_i2c_channels(); 1652 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1653 new_i2c_channel(i2c); 1654 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1655 i2c_get(); 1656 else if (!strcmp(token, "s") || !strcmp(token, "scan")) 1657 i2c_scan(); 1658 else 1659 printf("i2c channel | get | list | scan\n"); 1660 } 1661 else 1662 list_i2c_channels(); 1663 } 1664 1665 static void handle_memory(void) 1666 { 1667 char *token; 1668 1669 if ((token = read_token(NULL)) != NULL) 1670 { 1671 if (!strcmp(token, "r") || !strcmp(token, "region")) 1672 new_memory_region(); 1673 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1674 set_memory_region(); 1675 else 1676 printf("memory region | set\n"); 1677 } 1678 else 1679 list_memory_regions(); 1680 } 1681 1682 static void handle_rtc(void *rtc, void *cpm) 1683 { 1684 char *token; 1685 1686 if ((token = read_token(NULL)) != NULL) 1687 { 1688 if (!strcmp(token, "d") || !strcmp(token, "disable")) 1689 rtc_disable(rtc); 1690 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1691 rtc_enable(rtc); 1692 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1693 printf("seconds = %d\n", rtc_get_seconds(rtc)); 1694 else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) 1695 printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); 1696 else if (!strcmp(token, "p") || !strcmp(token, "power-down")) 1697 rtc_power_down(rtc); 1698 else if (!strcmp(token, "r") || !strcmp(token, "reset")) 1699 rtc_reset(rtc, cpm); 1700 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1701 _rtc_set_seconds(rtc, 0); 1702 else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) 1703 _rtc_set_seconds(rtc, 1); 1704 else 1705 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1706 } 1707 else 1708 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1709 } 1710 1711 static void handle_spi(void *spi, void *gpio[]) 1712 { 1713 char *token; 1714 1715 if ((token = read_token(NULL)) != NULL) 1716 { 1717 if (!strcmp(token, "l") || !strcmp(token, "list")) 1718 list_channels(num_spi_channels, spi_channels); 1719 else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) 1720 spi_control(1); 1721 else if (!strcmp(token, "r") || !strcmp(token, "control-release")) 1722 spi_control(0); 1723 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1724 new_spi_channel(spi, gpio); 1725 else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) 1726 new_spi_channel_gpio(gpio); 1727 else if (!strcmp(token, "s") || !strcmp(token, "send")) 1728 spi_send_data(); 1729 else if (!strcmp(token, "S") || !strcmp(token, "send-units")) 1730 spi_send_data_units(); 1731 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1732 spi_transfer_data(); 1733 else 1734 printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); 1735 } 1736 else 1737 list_channels(num_spi_channels, spi_channels); 1738 } 1739 1740 static void handle_tcu(void *tcu) 1741 { 1742 char *token; 1743 1744 if ((token = read_token(NULL)) != NULL) 1745 { 1746 if (!strcmp(token, "l") || !strcmp(token, "list")) 1747 list_tcu_channels(); 1748 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1749 new_tcu_channel(tcu); 1750 else if (!strcmp(token, "C") || !strcmp(token, "clock")) 1751 set_tcu_clock(); 1752 else if (!strcmp(token, "d") || !strcmp(token, "disable")) 1753 disable_tcu_counter(); 1754 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1755 enable_tcu_counter(); 1756 else if (!strcmp(token, "f") || !strcmp(token, "full")) 1757 set_tcu_full(); 1758 else if (!strcmp(token, "fm") || !strcmp(token, "full-mask")) 1759 set_tcu_mask(1, 1); 1760 else if (!strcmp(token, "fu") || !strcmp(token, "full-unmask")) 1761 set_tcu_mask(1, 0); 1762 else if (!strcmp(token, "h") || !strcmp(token, "half")) 1763 set_tcu_half(); 1764 else if (!strcmp(token, "hm") || !strcmp(token, "half-mask")) 1765 set_tcu_mask(0, 1); 1766 else if (!strcmp(token, "hu") || !strcmp(token, "half-unmask")) 1767 set_tcu_mask(0, 0); 1768 else if (!strcmp(token, "p") || !strcmp(token, "prescale")) 1769 set_tcu_prescale(); 1770 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1771 set_tcu_counter(); 1772 else if (!strcmp(token, "w") || !strcmp(token, "wait")) 1773 tcu_wait(); 1774 else 1775 printf("tcu channel | clock | disable | enable | full | full-mask | " \ 1776 "full-unmask | half | half-mask | half-unmask | list | mask | " \ 1777 "prescale | set | unmask | wait\n"); 1778 } 1779 else 1780 list_tcu_channels(); 1781 } 1782 1783 1784 1785 int main(void) 1786 { 1787 l4_addr_t aic_base = 0, aic_base_end = 0; 1788 l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; 1789 l4_addr_t cpm_base = 0, cpm_base_end = 0; 1790 l4_addr_t dma_base = 0, dma_base_end = 0; 1791 l4_addr_t gpio_base = 0, gpio_base_end = 0; 1792 l4_addr_t i2c_base = 0, i2c_base_end = 0; 1793 l4_addr_t rtc_base = 0, rtc_base_end = 0; 1794 l4_addr_t ssi_base = 0, ssi_base_end = 0; 1795 l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; 1796 l4_addr_t tcu_base = 0, tcu_base_end = 0; 1797 void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *rtc, *spi, *tcu; 1798 int result = 0; 1799 unsigned int port; 1800 1801 icucap = l4re_env_get_cap("icu"); 1802 1803 /* Obtain resource details describing I/O memory. */ 1804 1805 printf("Access CPM...\n"); 1806 1807 if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) 1808 return 1; 1809 1810 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 1811 1812 cpm = cpm_init(cpm_base); 1813 1814 printf("Access DMA...\n"); 1815 1816 if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) 1817 return 1; 1818 1819 printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); 1820 1821 dma = dma_init(dma_base, dma_base_end, cpm); 1822 1823 if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) 1824 return 1; 1825 1826 printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); 1827 1828 if (init_dma()) 1829 return 1; 1830 1831 dma_enable(dma); 1832 1833 printf("Access GPIO...\n"); 1834 1835 if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) 1836 return 1; 1837 1838 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 1839 1840 for (port = 0; port < num_gpio_ports; port++) 1841 gpio[port] = gpio_init(gpio_base + port * 0x100, gpio_base + (port + 1) * 0x100, 1842 32, gpio_ports[port].pull_ups, gpio_ports[port].pull_downs); 1843 1844 printf("Access I2C...\n"); 1845 1846 if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) 1847 return 1; 1848 1849 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 1850 1851 i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); 1852 1853 if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) 1854 return 1; 1855 1856 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 1857 1858 printf("Access AIC...\n"); 1859 1860 if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, 1861 &aic_phys_base, &aic_phys_base_end)) < 0) 1862 return 1; 1863 1864 printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); 1865 1866 aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); 1867 1868 printf("Access RTC...\n"); 1869 1870 if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) 1871 return 1; 1872 1873 printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); 1874 1875 rtc = rtc_init(rtc_base, cpm); 1876 1877 printf("Access SSI...\n"); 1878 1879 if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, 1880 &ssi_phys_base, &ssi_phys_base_end)) < 0) 1881 return 1; 1882 1883 printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); 1884 1885 spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); 1886 1887 printf("Access TCU...\n"); 1888 1889 if ((result = get_memory(io_memory_regions[TCU], &tcu_base, &tcu_base_end)) < 0) 1890 return 1; 1891 1892 printf("TCU at 0x%lx...0x%lx.\n", tcu_base, tcu_base_end); 1893 1894 tcu = tcu_init(tcu_base, tcu_base_end); 1895 1896 if (get_irq(io_memory_regions[TCU], &tcu_irq_start, &tcu_irq_end) < 0) 1897 return 1; 1898 1899 printf("IRQ range at %d...%d.\n", tcu_irq_start, tcu_irq_end); 1900 1901 if (init_tcu()) 1902 return 1; 1903 1904 /* Start the interactive session. */ 1905 1906 printf("aic, cpm, dma, gpio, i2c, rtc, spi, tcu\n"); 1907 1908 while (1) 1909 { 1910 char cmdline[256], *token; 1911 1912 printf("> "); 1913 1914 token = fgets(cmdline, 256, stdin); 1915 1916 if (token == NULL) 1917 break; 1918 1919 if ((token = strtok(cmdline, delimiters)) == NULL) 1920 continue; 1921 1922 /* AIC/I2S commands. */ 1923 1924 if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) 1925 handle_aic(aic); 1926 1927 /* CPM commands. */ 1928 1929 else if (!strcmp(token, "c") || !strcmp(token, "cpm")) 1930 handle_cpm(cpm); 1931 1932 /* DMA commands. */ 1933 1934 else if (!strcmp(token, "d") || !strcmp(token, "dma")) 1935 handle_dma(dma); 1936 1937 /* GPIO commands. */ 1938 1939 else if (!strcmp(token, "g") || !strcmp(token, "gpio")) 1940 handle_gpio(gpio); 1941 1942 /* I2C commands. */ 1943 1944 else if (!strcmp(token, "i") || !strcmp(token, "i2c")) 1945 handle_i2c(i2c); 1946 1947 /* Generic memory commands. */ 1948 1949 else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) 1950 handle_memory(); 1951 1952 /* RTC commands. */ 1953 1954 else if (!strcmp(token, "r") || !strcmp(token, "rtc")) 1955 handle_rtc(rtc, cpm); 1956 1957 /* SPI commands. */ 1958 1959 else if (!strcmp(token, "s") || !strcmp(token, "spi")) 1960 handle_spi(spi, gpio); 1961 1962 /* TCU commands. */ 1963 1964 else if (!strcmp(token, "t") || !strcmp(token, "tcu")) 1965 handle_tcu(tcu); 1966 1967 /* Comments and blank lines. */ 1968 1969 else if (strncmp(token, "#", 1) && strlen(token)) 1970 printf("Command?\n"); 1971 } 1972 1973 printf("End of session.\n"); 1974 return 0; 1975 }