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