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