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, void *cpm) 1373 { 1374 unsigned int seconds; 1375 1376 if (!read_number("Seconds", &seconds)) 1377 return; 1378 1379 enum Clock_identifiers clock = rtc_get_clock(rtc); 1380 uint32_t rtc_seconds = rtc_get_seconds(rtc); 1381 uint32_t value = seconds * cpm_get_frequency(cpm, clock); 1382 1383 rtc_alarm_disable(rtc); 1384 rtc_set_alarm_seconds(rtc, rtc_seconds + value); 1385 rtc_hibernate(rtc); 1386 } 1387 1388 static void _rtc_set_seconds(void *rtc, int alarm) 1389 { 1390 unsigned int seconds; 1391 1392 if (!read_number("Seconds", &seconds)) 1393 return; 1394 1395 (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); 1396 } 1397 1398 1399 1400 /* SPI operations. */ 1401 1402 static void new_spi_channel(void *spi, void *gpio[]) 1403 { 1404 unsigned int control_port, control_pin, control_alt_func_input; 1405 int control_alt_func; 1406 void *control_chip; 1407 int num = get_channel_number(num_spi_channels); 1408 void *channel; 1409 uint32_t frequency; 1410 1411 if (num < 0) 1412 return; 1413 1414 channel = get_channel(num_dma_channels, dma_channels, NULL); 1415 1416 if (channel == NULL) 1417 return; 1418 1419 if (!read_number("Frequency", &frequency)) 1420 return; 1421 1422 if (!get_port_and_pin(&control_port, &control_pin)) 1423 control_chip = NULL; 1424 else 1425 control_chip = gpio[control_port]; 1426 1427 if (!read_number("Function", &control_alt_func_input)) 1428 control_alt_func = -1; 1429 else 1430 control_alt_func = control_alt_func_input; 1431 1432 spi_channels[num] = spi_get_channel(spi, num, channel, frequency, 1433 control_chip, control_pin, 1434 control_alt_func); 1435 } 1436 1437 static void new_spi_channel_gpio(void *gpio[]) 1438 { 1439 unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, 1440 enable_pin, control_port, control_pin; 1441 void *control_chip; 1442 int num = get_channel_number(num_spi_channels); 1443 uint32_t frequency; 1444 1445 if (num < 0) 1446 return; 1447 1448 if (!read_number("Frequency", &frequency)) 1449 return; 1450 1451 if (!get_port_and_pin(&clock_port, &clock_pin)) 1452 return; 1453 1454 if (!get_port_and_pin(&data_port, &data_pin)) 1455 return; 1456 1457 if (!get_port_and_pin(&enable_port, &enable_pin)) 1458 return; 1459 1460 if (!get_port_and_pin(&control_port, &control_pin)) 1461 control_chip = NULL; 1462 else 1463 control_chip = gpio[control_port]; 1464 1465 spi_channels[num] = spi_get_channel_gpio(frequency, 1466 gpio[clock_port], clock_pin, 1467 gpio[data_port], data_pin, 1468 gpio[enable_port], enable_pin, 1469 control_chip, control_pin); 1470 } 1471 1472 static void spi_control(int acquire) 1473 { 1474 unsigned int level; 1475 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1476 1477 if (acquire) 1478 { 1479 if (!read_number("Level", &level)) 1480 return; 1481 1482 spi_acquire_control(channel, level); 1483 } 1484 else 1485 spi_release_control(channel); 1486 } 1487 1488 static void spi_send_data(void) 1489 { 1490 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1491 int bytes; 1492 unsigned int byte; 1493 uint8_t buffer[256]; 1494 1495 bytes = 0; 1496 1497 while (read_encoded_number(NULL, "%2x", &byte)) 1498 { 1499 buffer[bytes] = (uint8_t) (byte & 0xff); 1500 bytes++; 1501 } 1502 1503 spi_send(channel, bytes, buffer); 1504 } 1505 1506 static void spi_send_data_units(void) 1507 { 1508 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1509 unsigned int char_size, unit_size, value; 1510 uint8_t buffer[256]; 1511 int byte = 0; 1512 1513 if (!read_number("Unit size", &unit_size)) 1514 return; 1515 1516 if (!read_number("Character size", &char_size)) 1517 return; 1518 1519 /* Read hex digits for bytes. Multiple bytes make up each unit and are read 1520 from most to least significant. Where the unit size exceeds the character 1521 size, the last bit before the character indicates the GPC bit. */ 1522 1523 while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) 1524 buffer[byte++] = value; 1525 1526 /* Explicitly indicate big endian data. */ 1527 1528 spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); 1529 } 1530 1531 static void spi_transfer_data(void) 1532 { 1533 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1534 struct dma_region *dma_region = NULL, *desc_region; 1535 struct memory_region *memory_region = NULL; 1536 unsigned int char_size, unit_size; 1537 uint32_t count, transferred; 1538 int using_dma; 1539 1540 if (channel == NULL) 1541 return; 1542 1543 if (!read_flag("Using DMA", &using_dma, "d", 1)) 1544 return; 1545 1546 if (using_dma) 1547 dma_region = _get_dma_region(); 1548 else 1549 memory_region = _get_memory_region(); 1550 1551 if ((dma_region == NULL) && (memory_region == NULL)) 1552 return; 1553 1554 if (!read_number("Transfer size", &count)) 1555 return; 1556 1557 if (!read_number("Unit size", &unit_size)) 1558 return; 1559 1560 if (!read_number("Character size", &char_size)) 1561 return; 1562 1563 if (using_dma) 1564 { 1565 desc_region = _get_dma_region(); 1566 1567 transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, 1568 unit_size, char_size, 1569 desc_region != NULL ? desc_region->vaddr : 0, 1570 desc_region != NULL ? desc_region->paddr : 0); 1571 } 1572 else 1573 transferred = spi_transfer(channel, memory_region->addr, 0, count, 1574 unit_size, char_size, 0, 0); 1575 1576 printf("Transferred: %d\n", transferred); 1577 } 1578 1579 1580 1581 /* TCU configuration. */ 1582 1583 static l4_uint32_t tcu_irq_start = 0, tcu_irq_end = 0; 1584 1585 /* TCU operations. */ 1586 1587 static int init_tcu(void) 1588 { 1589 /* Here, only one IRQ is used. */ 1590 1591 tcu_irq = l4re_util_cap_alloc(); 1592 return init_irq(tcu_irq_num, tcu_irq, tcu_irq_start, tcu_irq_end); 1593 } 1594 1595 static void disable_tcu_counter(void) 1596 { 1597 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1598 1599 if (channel == NULL) 1600 return; 1601 1602 tcu_disable(channel); 1603 } 1604 1605 static void enable_tcu_counter(void) 1606 { 1607 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1608 1609 if (channel == NULL) 1610 return; 1611 1612 tcu_enable(channel); 1613 } 1614 1615 static void new_tcu_channel(void *tcu) 1616 { 1617 int num = get_channel_number(num_tcu_channels); 1618 1619 if (num < 0) 1620 return; 1621 1622 tcu_channels[num] = tcu_get_channel(tcu, num, tcu_irq); 1623 } 1624 1625 static void list_tcu_channels(void) 1626 { 1627 unsigned int num; 1628 void *channel; 1629 1630 printf(" Clock.. Counter......... Mask..............\n"); 1631 printf("Channel Status C Pre Cnt Half Full Half Full Int\n"); 1632 printf("------- -------- -- ---- ---- ---- ---- -------- -------- ----\n"); 1633 1634 for (num = 0; num < num_tcu_channels; num++) 1635 { 1636 printf("%d ", num); 1637 1638 channel = tcu_channels[num]; 1639 1640 if (channel == NULL) 1641 printf("inactive\n"); 1642 else 1643 printf("%s %d %4d %04x %04x %04x %s %s %s\n", 1644 tcu_is_enabled(channel) ? "enabled " : "disabled", 1645 tcu_get_clock(channel), 1646 tcu_get_prescale(channel), 1647 tcu_get_counter(channel), 1648 tcu_get_half_data_value(channel), 1649 tcu_get_full_data_value(channel), 1650 tcu_get_half_data_mask(channel) ? "masked " : "unmasked", 1651 tcu_get_full_data_mask(channel) ? "masked " : "unmasked", 1652 tcu_have_interrupt(channel) ? "!" : "_"); 1653 } 1654 } 1655 1656 static void set_tcu_counter(void) 1657 { 1658 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1659 uint32_t counter; 1660 1661 if (channel == NULL) 1662 return; 1663 1664 if (!read_number("Value", &counter)) 1665 return; 1666 1667 tcu_set_counter(channel, counter); 1668 } 1669 1670 static void set_tcu_clock(void) 1671 { 1672 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1673 uint32_t clock; 1674 1675 if (channel == NULL) 1676 return; 1677 1678 if (!read_number("Clock", &clock)) 1679 return; 1680 1681 tcu_set_clock(channel, clock); 1682 } 1683 1684 static void set_tcu_full(void) 1685 { 1686 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1687 uint32_t value; 1688 1689 if (channel == NULL) 1690 return; 1691 1692 if (!read_number("Full value", &value)) 1693 return; 1694 1695 tcu_set_full_data_value(channel, value); 1696 } 1697 1698 static void set_tcu_half(void) 1699 { 1700 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1701 uint32_t value; 1702 1703 if (channel == NULL) 1704 return; 1705 1706 if (!read_number("Half value", &value)) 1707 return; 1708 1709 tcu_set_half_data_value(channel, value); 1710 } 1711 1712 static void set_tcu_mask(int full_mask, int masked) 1713 { 1714 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1715 1716 if (channel == NULL) 1717 return; 1718 1719 if (full_mask) 1720 tcu_set_full_data_mask(channel, masked); 1721 else 1722 tcu_set_half_data_mask(channel, masked); 1723 } 1724 1725 static void set_tcu_prescale(void) 1726 { 1727 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1728 uint32_t prescale; 1729 1730 if (channel == NULL) 1731 return; 1732 1733 if (!read_number("Prescale", &prescale)) 1734 return; 1735 1736 tcu_set_prescale(channel, prescale); 1737 } 1738 1739 static void tcu_wait(void) 1740 { 1741 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1742 uint32_t timeout; 1743 1744 if (channel == NULL) 1745 return; 1746 1747 if (!read_number("Timeout", &timeout)) 1748 return; 1749 1750 printf("IRQ received: %s\n", tcu_wait_for_irq(channel, timeout) ? "yes" : "no"); 1751 } 1752 1753 1754 1755 /* Command processing. */ 1756 1757 static void handle_aic(void *aic) 1758 { 1759 char *token; 1760 1761 if ((token = read_token(NULL)) != NULL) 1762 { 1763 if (!strcmp(token, "l") || !strcmp(token, "list")) 1764 list_channels(num_aic_channels, aic_channels); 1765 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1766 new_aic_channel(aic); 1767 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1768 aic_transfer_data(); 1769 else 1770 printf("aic channel | list | transfer\n"); 1771 } 1772 else 1773 list_channels(num_aic_channels, aic_channels); 1774 } 1775 1776 static void handle_cpm(void *cpm) 1777 { 1778 char *token; 1779 1780 if ((token = read_token(NULL)) != NULL) 1781 { 1782 if (!strcmp(token, "l") || !strcmp(token, "list")) 1783 list_clocks(cpm); 1784 else if (!strcmp(token, "f") || !strcmp(token, "frequency")) 1785 set_frequency(cpm); 1786 else if (!strcmp(token, "p") || !strcmp(token, "parameters")) 1787 set_parameters(cpm); 1788 else if (!strcmp(token, "s") || !strcmp(token, "source")) 1789 set_source(cpm); 1790 else if (!strcmp(token, "start")) 1791 start_clock(cpm); 1792 else if (!strcmp(token, "stop")) 1793 stop_clock(cpm); 1794 else 1795 printf("cpm list | frequency | parameters | source | start | stop\n"); 1796 } 1797 else 1798 list_clocks(cpm); 1799 } 1800 1801 static void handle_dma(void *dma) 1802 { 1803 char *token; 1804 1805 if ((token = read_token(NULL)) != NULL) 1806 { 1807 if (!strcmp(token, "l") || !strcmp(token, "list")) 1808 list_channels(num_dma_channels, dma_channels); 1809 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1810 new_dma_channel(dma); 1811 else if (!strcmp(token, "r") || !strcmp(token, "region")) 1812 new_dma_region(); 1813 else if (!strcmp(token, "R") || !strcmp(token, "show")) 1814 show_dma_region(); 1815 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1816 set_dma_region(); 1817 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1818 dma_transfer_data(); 1819 else 1820 printf("dma channel | list | region | set | transfer\n"); 1821 } 1822 else 1823 list_channels(num_dma_channels, dma_channels); 1824 } 1825 1826 static void handle_gpio(void *gpio[]) 1827 { 1828 char *token; 1829 1830 if ((token = read_token(NULL)) != NULL) 1831 { 1832 if (!strcmp(token, "l") || !strcmp(token, "list")) 1833 list_gpios(gpio); 1834 else if (!strcmp(token, "a") || !strcmp(token, "alt")) 1835 set_gpio_alt_func(gpio); 1836 else if (!strcmp(token, "i") || !strcmp(token, "io")) 1837 set_gpio_pad(gpio); 1838 else if (!strcmp(token, "p") || !strcmp(token, "pull")) 1839 set_gpio_pull(gpio); 1840 else 1841 printf("gpio list | alt | io | pull\n"); 1842 } 1843 else 1844 list_gpios(gpio); 1845 } 1846 1847 static void handle_i2c(void *i2c) 1848 { 1849 char *token; 1850 1851 if ((token = read_token(NULL)) != NULL) 1852 { 1853 if (!strcmp(token, "l") || !strcmp(token, "list")) 1854 list_i2c_channels(); 1855 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1856 new_i2c_channel(i2c); 1857 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1858 i2c_get(); 1859 else if (!strcmp(token, "S") || !strcmp(token, "scan")) 1860 i2c_scan(); 1861 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1862 i2c_set(); 1863 else 1864 printf("i2c channel | get | list | scan | set\n"); 1865 } 1866 else 1867 list_i2c_channels(); 1868 } 1869 1870 static void handle_memory(void) 1871 { 1872 char *token; 1873 1874 if ((token = read_token(NULL)) != NULL) 1875 { 1876 if (!strcmp(token, "r") || !strcmp(token, "region")) 1877 new_memory_region(); 1878 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1879 set_memory_region(); 1880 else 1881 printf("memory region | set\n"); 1882 } 1883 else 1884 list_memory_regions(); 1885 } 1886 1887 static void handle_msc(void *msc) 1888 { 1889 char *token; 1890 1891 if ((token = read_token(NULL)) != NULL) 1892 { 1893 if (!strcmp(token, "l") || !strcmp(token, "list")) 1894 list_msc_channels(); 1895 else if (!strcmp(token, "C") || !strcmp(token, "cards")) 1896 list_msc_cards(); 1897 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1898 new_msc_channel(msc); 1899 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1900 enable_msc_channel(); 1901 else if (!strcmp(token, "p") || !strcmp(token, "partitions")) 1902 list_msc_card_partitions(); 1903 else if (!strcmp(token, "r") || !strcmp(token, "read")) 1904 read_blocks_from_msc(); 1905 else 1906 printf("msc cards | channel | enable | partitions | read\n"); 1907 } 1908 else 1909 list_msc_channels(); 1910 } 1911 1912 static void handle_rtc(void *rtc, void *cpm) 1913 { 1914 char *token; 1915 1916 if ((token = read_token(NULL)) != NULL) 1917 { 1918 if (!strcmp(token, "c") || !strcmp(token, "clock-frequency")) 1919 rtc_clock_frequency(rtc, cpm); 1920 else if (!strcmp(token, "d") || !strcmp(token, "disable")) 1921 rtc_disable(rtc); 1922 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1923 rtc_enable(rtc); 1924 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1925 printf("seconds = %d\n", rtc_get_seconds(rtc)); 1926 else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) 1927 printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); 1928 else if (!strcmp(token, "p") || !strcmp(token, "power-down")) 1929 rtc_power_down(rtc); 1930 else if (!strcmp(token, "r") || !strcmp(token, "reset")) 1931 rtc_reset(rtc, cpm); 1932 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1933 _rtc_set_seconds(rtc, 0); 1934 else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) 1935 _rtc_set_seconds(rtc, 1); 1936 else 1937 printf("rtc clock-frequency | disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1938 } 1939 else 1940 printf("rtc clock-frequency | disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1941 } 1942 1943 static void handle_spi(void *spi, void *gpio[]) 1944 { 1945 char *token; 1946 1947 if ((token = read_token(NULL)) != NULL) 1948 { 1949 if (!strcmp(token, "l") || !strcmp(token, "list")) 1950 list_channels(num_spi_channels, spi_channels); 1951 else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) 1952 spi_control(1); 1953 else if (!strcmp(token, "r") || !strcmp(token, "control-release")) 1954 spi_control(0); 1955 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1956 new_spi_channel(spi, gpio); 1957 else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) 1958 new_spi_channel_gpio(gpio); 1959 else if (!strcmp(token, "s") || !strcmp(token, "send")) 1960 spi_send_data(); 1961 else if (!strcmp(token, "S") || !strcmp(token, "send-units")) 1962 spi_send_data_units(); 1963 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1964 spi_transfer_data(); 1965 else 1966 printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); 1967 } 1968 else 1969 list_channels(num_spi_channels, spi_channels); 1970 } 1971 1972 static void handle_tcu(void *tcu) 1973 { 1974 char *token; 1975 1976 if ((token = read_token(NULL)) != NULL) 1977 { 1978 if (!strcmp(token, "l") || !strcmp(token, "list")) 1979 list_tcu_channels(); 1980 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1981 new_tcu_channel(tcu); 1982 else if (!strcmp(token, "C") || !strcmp(token, "clock")) 1983 set_tcu_clock(); 1984 else if (!strcmp(token, "d") || !strcmp(token, "disable")) 1985 disable_tcu_counter(); 1986 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1987 enable_tcu_counter(); 1988 else if (!strcmp(token, "f") || !strcmp(token, "full")) 1989 set_tcu_full(); 1990 else if (!strcmp(token, "fm") || !strcmp(token, "full-mask")) 1991 set_tcu_mask(1, 1); 1992 else if (!strcmp(token, "fu") || !strcmp(token, "full-unmask")) 1993 set_tcu_mask(1, 0); 1994 else if (!strcmp(token, "h") || !strcmp(token, "half")) 1995 set_tcu_half(); 1996 else if (!strcmp(token, "hm") || !strcmp(token, "half-mask")) 1997 set_tcu_mask(0, 1); 1998 else if (!strcmp(token, "hu") || !strcmp(token, "half-unmask")) 1999 set_tcu_mask(0, 0); 2000 else if (!strcmp(token, "p") || !strcmp(token, "prescale")) 2001 set_tcu_prescale(); 2002 else if (!strcmp(token, "s") || !strcmp(token, "set")) 2003 set_tcu_counter(); 2004 else if (!strcmp(token, "w") || !strcmp(token, "wait")) 2005 tcu_wait(); 2006 else 2007 printf("tcu channel | clock | disable | enable | full | full-mask | " \ 2008 "full-unmask | half | half-mask | half-unmask | list | " \ 2009 "prescale | set | wait\n"); 2010 } 2011 else 2012 list_tcu_channels(); 2013 } 2014 2015 2016 2017 int main(void) 2018 { 2019 l4_addr_t aic_base = 0, aic_base_end = 0; 2020 l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; 2021 l4_addr_t cpm_base = 0, cpm_base_end = 0; 2022 l4_addr_t dma_base = 0, dma_base_end = 0; 2023 l4_addr_t gpio_base = 0, gpio_base_end = 0; 2024 l4_addr_t i2c_base = 0, i2c_base_end = 0; 2025 l4_addr_t msc_base = 0, msc_base_end = 0; 2026 l4_addr_t msc_phys_base = 0, msc_phys_base_end = 0; 2027 l4_addr_t rtc_base = 0, rtc_base_end = 0; 2028 l4_addr_t ssi_base = 0, ssi_base_end = 0; 2029 l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; 2030 l4_addr_t tcu_base = 0, tcu_base_end = 0; 2031 void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *msc, *rtc, *spi, *tcu; 2032 int result = 0; 2033 unsigned int port; 2034 2035 icucap = l4re_env_get_cap("icu"); 2036 2037 /* Obtain resource details describing I/O memory. */ 2038 2039 printf("Access CPM...\n"); 2040 2041 if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) 2042 return 1; 2043 2044 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 2045 2046 cpm = cpm_init(cpm_base); 2047 2048 printf("Access DMA...\n"); 2049 2050 if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) 2051 return 1; 2052 2053 printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); 2054 2055 dma = dma_init(dma_base, dma_base_end, cpm); 2056 2057 if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) 2058 return 1; 2059 2060 printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); 2061 2062 if (init_dma()) 2063 return 1; 2064 2065 dma_enable(dma); 2066 2067 printf("Access GPIO...\n"); 2068 2069 if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) 2070 return 1; 2071 2072 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 2073 2074 for (port = 0; port < num_gpio_ports; port++) 2075 gpio[port] = gpio_init_shadow(gpio_base, port); 2076 2077 printf("Access I2C...\n"); 2078 2079 if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) 2080 return 1; 2081 2082 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 2083 2084 i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); 2085 2086 if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) 2087 return 1; 2088 2089 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 2090 2091 printf("Access AIC...\n"); 2092 2093 if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, 2094 &aic_phys_base, &aic_phys_base_end)) < 0) 2095 return 1; 2096 2097 printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); 2098 2099 aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); 2100 2101 printf("Access MSC...\n"); 2102 2103 if ((result = get_memory_complete(io_memory_regions[MSC], &msc_base, &msc_base_end, 2104 &msc_phys_base, &msc_phys_base_end)) < 0) 2105 return 1; 2106 2107 printf("MSC at 0x%lx...0x%lx.\n", msc_base, msc_base_end); 2108 2109 msc = msc_init(msc_phys_base, msc_base, msc_base_end, cpm); 2110 2111 if (get_irq(io_memory_regions[MSC], &msc_irq_start, &msc_irq_end) < 0) 2112 return 1; 2113 2114 printf("IRQ range at %d...%d.\n", msc_irq_start, msc_irq_end); 2115 2116 printf("Access RTC...\n"); 2117 2118 if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) 2119 return 1; 2120 2121 printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); 2122 2123 rtc = rtc_init(rtc_base, cpm); 2124 2125 printf("Access SSI...\n"); 2126 2127 if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, 2128 &ssi_phys_base, &ssi_phys_base_end)) < 0) 2129 return 1; 2130 2131 printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); 2132 2133 spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); 2134 2135 printf("Access TCU...\n"); 2136 2137 if ((result = get_memory(io_memory_regions[TCU], &tcu_base, &tcu_base_end)) < 0) 2138 return 1; 2139 2140 printf("TCU at 0x%lx...0x%lx.\n", tcu_base, tcu_base_end); 2141 2142 tcu = tcu_init(tcu_base, tcu_base_end); 2143 2144 if (get_irq(io_memory_regions[TCU], &tcu_irq_start, &tcu_irq_end) < 0) 2145 return 1; 2146 2147 printf("IRQ range at %d...%d.\n", tcu_irq_start, tcu_irq_end); 2148 2149 if (init_tcu()) 2150 return 1; 2151 2152 /* Start the interactive session. */ 2153 2154 printf("aic, cpm, dma, gpio, i2c, msc, rtc, spi, tcu\n"); 2155 2156 while (1) 2157 { 2158 char cmdline[256], *token; 2159 2160 printf("> "); 2161 2162 token = fgets(cmdline, 256, stdin); 2163 2164 if (token == NULL) 2165 break; 2166 2167 if ((token = strtok(cmdline, delimiters)) == NULL) 2168 continue; 2169 2170 /* AIC/I2S commands. */ 2171 2172 if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) 2173 handle_aic(aic); 2174 2175 /* CPM commands. */ 2176 2177 else if (!strcmp(token, "c") || !strcmp(token, "cpm")) 2178 handle_cpm(cpm); 2179 2180 /* DMA commands. */ 2181 2182 else if (!strcmp(token, "d") || !strcmp(token, "dma")) 2183 handle_dma(dma); 2184 2185 /* GPIO commands. */ 2186 2187 else if (!strcmp(token, "g") || !strcmp(token, "gpio")) 2188 handle_gpio(gpio); 2189 2190 /* I2C commands. */ 2191 2192 else if (!strcmp(token, "i") || !strcmp(token, "i2c")) 2193 handle_i2c(i2c); 2194 2195 /* MSC commands. */ 2196 2197 else if (!strcmp(token, "M") || !strcmp(token, "msc")) 2198 handle_msc(msc); 2199 2200 /* Generic memory commands. */ 2201 2202 else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) 2203 handle_memory(); 2204 2205 /* RTC commands. */ 2206 2207 else if (!strcmp(token, "r") || !strcmp(token, "rtc")) 2208 handle_rtc(rtc, cpm); 2209 2210 /* SPI commands. */ 2211 2212 else if (!strcmp(token, "s") || !strcmp(token, "spi")) 2213 handle_spi(spi, gpio); 2214 2215 /* TCU commands. */ 2216 2217 else if (!strcmp(token, "t") || !strcmp(token, "tcu")) 2218 handle_tcu(tcu); 2219 2220 /* Comments and blank lines. */ 2221 2222 else if (strncmp(token, "#", 1) && strlen(token)) 2223 printf("Command?\n"); 2224 } 2225 2226 printf("End of session.\n"); 2227 return 0; 2228 }