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++) 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_scan(void) 1013 { 1014 void *channel; 1015 unsigned int address; 1016 uint8_t buffer[1]; 1017 int num; 1018 1019 channel = get_channel(num_i2c_channels, i2c_channels, &num); 1020 1021 if (channel == NULL) 1022 return; 1023 1024 for (address = 0; address < 0x20; address++) 1025 printf("%02x ", address); 1026 printf("\n"); 1027 1028 for (address = 0; address < 0x20; address++) 1029 printf("-- "); 1030 1031 for (address = 0; address < 0x80; address++) 1032 { 1033 if ((address % 32) == 0) 1034 printf("\n"); 1035 1036 i2c_set_target(channel, address); 1037 1038 if (_i2c_read(channel, buffer, 1, 1, i2c_irqs[num], 1000)) 1039 printf("%02x ", address); 1040 else 1041 printf("-- "); 1042 } 1043 1044 printf("\n"); 1045 for (address = 0; address < 0x20; address++) 1046 printf("-- "); 1047 printf("\n\n"); 1048 } 1049 1050 1051 1052 /* Memory operations. */ 1053 1054 static void list_memory_regions(void) 1055 { 1056 unsigned int num; 1057 struct memory_region *region; 1058 unsigned int i; 1059 1060 for (num = 0; num < num_memory_regions; num++) 1061 { 1062 printf("Region %d: ", num); 1063 1064 region = &memory_regions[num]; 1065 1066 if (!region->addr) 1067 printf("(inactive)\n"); 1068 else 1069 { 1070 printf("size = %d; virtual = 0x%lx; data =", 1071 region->size, region->addr); 1072 1073 for (i = 0; (i < region->size) && (i < 16); i++) 1074 printf(" %02x", *((uint8_t *) region->addr + i)); 1075 1076 printf("\n"); 1077 } 1078 } 1079 } 1080 1081 static void new_memory_region(void) 1082 { 1083 struct memory_region *region = _get_memory_region(); 1084 1085 if (region == NULL) 1086 { 1087 list_memory_regions(); 1088 return; 1089 } 1090 1091 if (region->addr) 1092 { 1093 printf("Region already defined.\n"); 1094 return; 1095 } 1096 1097 if (!read_number("Size", ®ion->size)) 1098 return; 1099 1100 if (get_memory_region(region->size, ®ion->addr)) 1101 printf("Could not allocate region.\n"); 1102 } 1103 1104 static void set_memory_region(void) 1105 { 1106 char *token; 1107 struct memory_region *region = _get_memory_region(); 1108 FILE *fp; 1109 1110 if (region == NULL) 1111 { 1112 list_memory_regions(); 1113 return; 1114 } 1115 1116 if (!region->addr) 1117 { 1118 printf("Region needs creating.\n"); 1119 return; 1120 } 1121 1122 memset((void *) region->addr, 0, region->size); 1123 1124 if ((token = read_token("Filename")) == NULL) 1125 return; 1126 1127 /* Populate the region from the file. */ 1128 1129 fp = fopen(token, "r"); 1130 1131 if (fp == NULL) 1132 { 1133 printf("File not readable.\n"); 1134 return; 1135 } 1136 1137 fread((char *) region->addr, sizeof(char), region->size, fp); 1138 fclose(fp); 1139 1140 l4_cache_flush_data(region->addr, region->addr + region->size); 1141 } 1142 1143 1144 1145 /* MSC configuration. */ 1146 1147 static l4_uint32_t msc_irq_start = 0, msc_irq_end = 0; 1148 1149 /* MSC operations. */ 1150 1151 static void list_msc_cards(void) 1152 { 1153 void *channel = get_channel(num_msc_channels, msc_channels, NULL); 1154 struct msc_card *cards; 1155 struct CSD *csd; 1156 uint8_t num; 1157 1158 if (channel == NULL) 1159 return; 1160 1161 cards = msc_get_cards(channel); 1162 1163 printf("Card RCA Bits Size\n"); 1164 printf("---- ---- ---- --------------\n"); 1165 1166 for (num = 0; num < msc_num_cards(channel); num++) 1167 { 1168 csd = &cards[num].csd; 1169 1170 printf("%d %04x %d %d\n", num, cards[num].rca, cards[num].bus_width, 1171 (1 << csd->read_blocklen) * 1172 (csd->device_size + 1) * 1173 (1 << (csd->device_size_multiplier + 2))); 1174 } 1175 } 1176 1177 static void list_msc_card_partitions(void) 1178 { 1179 void *channel = get_channel(num_msc_channels, msc_channels, NULL); 1180 struct dma_region *dma_region; 1181 struct partition_table_entry *entry; 1182 uint32_t card, transferred; 1183 uint8_t num; 1184 1185 if (channel == NULL) 1186 return; 1187 1188 dma_region = _get_dma_region(); 1189 1190 if (dma_region == NULL) 1191 return; 1192 1193 if (!read_number("Card", &card)) 1194 return; 1195 1196 l4_cache_inv_data(dma_region->vaddr, dma_region->vaddr + dma_region->size); 1197 1198 transferred = msc_read_blocks(channel, (uint8_t) card, dma_region, 0, 1); 1199 1200 if (!transferred) 1201 { 1202 printf("Could not read partitions.\n"); 1203 return; 1204 } 1205 1206 entry = get_partition_table((uint8_t *) dma_region->vaddr); 1207 1208 if (entry == NULL) 1209 { 1210 printf("Invalid partition table.\n"); 1211 return; 1212 } 1213 1214 printf("Partition Status Type Start End Address Sectors\n"); 1215 printf("--------- ------ ---- --------- --------- -------- --------\n"); 1216 1217 for (num = 0; num < 4; num++, entry++) 1218 { 1219 printf("%d %02x %02x %03x %02x %02x %03x %02x %02x %08x %08x\n", 1220 num, entry->status, entry->partition_type, 1221 entry->chs_first_sector.cylinder, 1222 entry->chs_first_sector.head, 1223 entry->chs_first_sector.sector, 1224 entry->chs_last_sector.cylinder, 1225 entry->chs_last_sector.head, 1226 entry->chs_last_sector.sector, 1227 entry->lba_first_sector, 1228 entry->num_sectors); 1229 } 1230 } 1231 1232 static void list_msc_channels(void) 1233 { 1234 unsigned int num; 1235 void *channel; 1236 1237 printf("Channel Cards\n"); 1238 printf("------- -----\n"); 1239 1240 for (num = 0; num < num_msc_channels; num++) 1241 { 1242 printf("%d ", num); 1243 1244 channel = msc_channels[num]; 1245 1246 if (channel == NULL) 1247 printf("inactive\n"); 1248 else 1249 printf("%d\n", msc_num_cards(channel)); 1250 } 1251 } 1252 1253 static void new_msc_channel(void *msc) 1254 { 1255 l4_cap_idx_t irqcap; 1256 int num = get_channel_number(num_msc_channels); 1257 void *channel; 1258 1259 if (num < 0) 1260 return; 1261 1262 channel = get_channel(num_dma_channels, dma_channels, NULL); 1263 1264 if (channel == NULL) 1265 return; 1266 1267 irqcap = l4re_util_cap_alloc(); 1268 1269 if (init_irq(num, irqcap, msc_irq_start, msc_irq_end)) 1270 return; 1271 1272 msc_channels[num] = msc_get_channel(msc, num, irqcap, channel); 1273 msc_irqs[num] = irqcap; 1274 } 1275 1276 static void enable_msc_channel(void) 1277 { 1278 void *channel = get_channel(num_msc_channels, msc_channels, NULL); 1279 1280 if (channel == NULL) 1281 return; 1282 1283 msc_enable(channel); 1284 } 1285 1286 static void read_blocks_from_msc(void) 1287 { 1288 void *channel = get_channel(num_msc_channels, msc_channels, NULL); 1289 struct dma_region *dma_region; 1290 uint32_t card, transferred, block_address, block_count; 1291 1292 if (channel == NULL) 1293 return; 1294 1295 dma_region = _get_dma_region(); 1296 1297 if (dma_region == NULL) 1298 return; 1299 1300 if (!read_number("Card", &card)) 1301 return; 1302 1303 if (!read_number("Block address", &block_address)) 1304 return; 1305 1306 if (!read_number("Block count", &block_count)) 1307 return; 1308 1309 /* NOTE: Assuming 512-byte blocks. */ 1310 1311 if (block_count * 512 > dma_region->size) 1312 { 1313 printf("Too many blocks for region.\n"); 1314 return; 1315 } 1316 1317 l4_cache_inv_data(dma_region->vaddr, dma_region->vaddr + dma_region->size); 1318 1319 transferred = msc_read_blocks(channel, (uint8_t) card, dma_region, 1320 block_address, block_count); 1321 1322 printf("Transferred: %d\n", transferred); 1323 } 1324 1325 1326 1327 /* RTC operations. */ 1328 1329 static void rtc_reset(void *rtc, void *cpm) 1330 { 1331 unsigned int seconds; 1332 1333 if (!read_number("Seconds", &seconds)) 1334 return; 1335 1336 /* NOTE: Assuming EXCLK/512 as RTC source. */ 1337 1338 uint32_t rtc_seconds = rtc_get_seconds(rtc); 1339 uint32_t value = seconds * cpm_get_frequency(cpm, Clock_external) / 512; 1340 1341 rtc_alarm_disable(rtc); 1342 rtc_set_alarm_seconds(rtc, rtc_seconds + value); 1343 rtc_hibernate(rtc); 1344 } 1345 1346 static void _rtc_set_seconds(void *rtc, int alarm) 1347 { 1348 unsigned int seconds; 1349 1350 if (!read_number("Seconds", &seconds)) 1351 return; 1352 1353 (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); 1354 } 1355 1356 1357 1358 /* SPI operations. */ 1359 1360 static void new_spi_channel(void *spi, void *gpio[]) 1361 { 1362 unsigned int control_port, control_pin, control_alt_func_input; 1363 int control_alt_func; 1364 void *control_chip; 1365 int num = get_channel_number(num_spi_channels); 1366 void *channel; 1367 uint32_t frequency; 1368 1369 if (num < 0) 1370 return; 1371 1372 channel = get_channel(num_dma_channels, dma_channels, NULL); 1373 1374 if (channel == NULL) 1375 return; 1376 1377 if (!read_number("Frequency", &frequency)) 1378 return; 1379 1380 if (!get_port_and_pin(&control_port, &control_pin)) 1381 control_chip = NULL; 1382 else 1383 control_chip = gpio[control_port]; 1384 1385 if (!read_number("Function", &control_alt_func_input)) 1386 control_alt_func = -1; 1387 else 1388 control_alt_func = control_alt_func_input; 1389 1390 spi_channels[num] = spi_get_channel(spi, num, channel, frequency, 1391 control_chip, control_pin, 1392 control_alt_func); 1393 } 1394 1395 static void new_spi_channel_gpio(void *gpio[]) 1396 { 1397 unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, 1398 enable_pin, control_port, control_pin; 1399 void *control_chip; 1400 int num = get_channel_number(num_spi_channels); 1401 uint32_t frequency; 1402 1403 if (num < 0) 1404 return; 1405 1406 if (!read_number("Frequency", &frequency)) 1407 return; 1408 1409 if (!get_port_and_pin(&clock_port, &clock_pin)) 1410 return; 1411 1412 if (!get_port_and_pin(&data_port, &data_pin)) 1413 return; 1414 1415 if (!get_port_and_pin(&enable_port, &enable_pin)) 1416 return; 1417 1418 if (!get_port_and_pin(&control_port, &control_pin)) 1419 control_chip = NULL; 1420 else 1421 control_chip = gpio[control_port]; 1422 1423 spi_channels[num] = spi_get_channel_gpio(frequency, 1424 gpio[clock_port], clock_pin, 1425 gpio[data_port], data_pin, 1426 gpio[enable_port], enable_pin, 1427 control_chip, control_pin); 1428 } 1429 1430 static void spi_control(int acquire) 1431 { 1432 unsigned int level; 1433 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1434 1435 if (acquire) 1436 { 1437 if (!read_number("Level", &level)) 1438 return; 1439 1440 spi_acquire_control(channel, level); 1441 } 1442 else 1443 spi_release_control(channel); 1444 } 1445 1446 static void spi_send_data(void) 1447 { 1448 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1449 int bytes; 1450 unsigned int byte; 1451 uint8_t buffer[256]; 1452 1453 bytes = 0; 1454 1455 while (read_encoded_number(NULL, "%2x", &byte)) 1456 { 1457 buffer[bytes] = (uint8_t) (byte & 0xff); 1458 bytes++; 1459 } 1460 1461 spi_send(channel, bytes, buffer); 1462 } 1463 1464 static void spi_send_data_units(void) 1465 { 1466 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1467 unsigned int char_size, unit_size, value; 1468 uint8_t buffer[256]; 1469 int byte = 0; 1470 1471 if (!read_number("Unit size", &unit_size)) 1472 return; 1473 1474 if (!read_number("Character size", &char_size)) 1475 return; 1476 1477 /* Read hex digits for bytes. Multiple bytes make up each unit and are read 1478 from most to least significant. Where the unit size exceeds the character 1479 size, the last bit before the character indicates the GPC bit. */ 1480 1481 while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) 1482 buffer[byte++] = value; 1483 1484 /* Explicitly indicate big endian data. */ 1485 1486 spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); 1487 } 1488 1489 static void spi_transfer_data(void) 1490 { 1491 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1492 struct dma_region *dma_region = NULL, *desc_region; 1493 struct memory_region *memory_region = NULL; 1494 unsigned int char_size, unit_size; 1495 uint32_t count, transferred; 1496 int using_dma; 1497 1498 if (channel == NULL) 1499 return; 1500 1501 if (!read_flag("Using DMA", &using_dma, "d", 1)) 1502 return; 1503 1504 if (using_dma) 1505 dma_region = _get_dma_region(); 1506 else 1507 memory_region = _get_memory_region(); 1508 1509 if ((dma_region == NULL) && (memory_region == NULL)) 1510 return; 1511 1512 if (!read_number("Transfer size", &count)) 1513 return; 1514 1515 if (!read_number("Unit size", &unit_size)) 1516 return; 1517 1518 if (!read_number("Character size", &char_size)) 1519 return; 1520 1521 if (using_dma) 1522 { 1523 desc_region = _get_dma_region(); 1524 1525 transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, 1526 unit_size, char_size, 1527 desc_region != NULL ? desc_region->vaddr : 0, 1528 desc_region != NULL ? desc_region->paddr : 0); 1529 } 1530 else 1531 transferred = spi_transfer(channel, memory_region->addr, 0, count, 1532 unit_size, char_size, 0, 0); 1533 1534 printf("Transferred: %d\n", transferred); 1535 } 1536 1537 1538 1539 /* TCU configuration. */ 1540 1541 static l4_uint32_t tcu_irq_start = 0, tcu_irq_end = 0; 1542 1543 /* TCU operations. */ 1544 1545 static int init_tcu(void) 1546 { 1547 /* Here, only one IRQ is used. */ 1548 1549 tcu_irq = l4re_util_cap_alloc(); 1550 return init_irq(tcu_irq_num, tcu_irq, tcu_irq_start, tcu_irq_end); 1551 } 1552 1553 static void disable_tcu_counter(void) 1554 { 1555 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1556 1557 if (channel == NULL) 1558 return; 1559 1560 tcu_disable(channel); 1561 } 1562 1563 static void enable_tcu_counter(void) 1564 { 1565 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1566 1567 if (channel == NULL) 1568 return; 1569 1570 tcu_enable(channel); 1571 } 1572 1573 static void new_tcu_channel(void *tcu) 1574 { 1575 int num = get_channel_number(num_tcu_channels); 1576 1577 if (num < 0) 1578 return; 1579 1580 tcu_channels[num] = tcu_get_channel(tcu, num, tcu_irq); 1581 } 1582 1583 static void list_tcu_channels(void) 1584 { 1585 unsigned int num; 1586 void *channel; 1587 1588 printf(" Clock.. Counter......... Mask..............\n"); 1589 printf("Channel Status C Pre Cnt Half Full Half Full Int\n"); 1590 printf("------- -------- -- ---- ---- ---- ---- -------- -------- ----\n"); 1591 1592 for (num = 0; num < num_tcu_channels; num++) 1593 { 1594 printf("%d ", num); 1595 1596 channel = tcu_channels[num]; 1597 1598 if (channel == NULL) 1599 printf("inactive\n"); 1600 else 1601 printf("%s %d %4d %04x %04x %04x %s %s %s\n", 1602 tcu_is_enabled(channel) ? "enabled " : "disabled", 1603 tcu_get_clock(channel), 1604 tcu_get_prescale(channel), 1605 tcu_get_counter(channel), 1606 tcu_get_half_data_value(channel), 1607 tcu_get_full_data_value(channel), 1608 tcu_get_half_data_mask(channel) ? "masked " : "unmasked", 1609 tcu_get_full_data_mask(channel) ? "masked " : "unmasked", 1610 tcu_have_interrupt(channel) ? "!" : "_"); 1611 } 1612 } 1613 1614 static void set_tcu_counter(void) 1615 { 1616 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1617 uint32_t counter; 1618 1619 if (channel == NULL) 1620 return; 1621 1622 if (!read_number("Value", &counter)) 1623 return; 1624 1625 tcu_set_counter(channel, counter); 1626 } 1627 1628 static void set_tcu_clock(void) 1629 { 1630 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1631 uint32_t clock; 1632 1633 if (channel == NULL) 1634 return; 1635 1636 if (!read_number("Clock", &clock)) 1637 return; 1638 1639 tcu_set_clock(channel, clock); 1640 } 1641 1642 static void set_tcu_full(void) 1643 { 1644 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1645 uint32_t value; 1646 1647 if (channel == NULL) 1648 return; 1649 1650 if (!read_number("Full value", &value)) 1651 return; 1652 1653 tcu_set_full_data_value(channel, value); 1654 } 1655 1656 static void set_tcu_half(void) 1657 { 1658 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1659 uint32_t value; 1660 1661 if (channel == NULL) 1662 return; 1663 1664 if (!read_number("Half value", &value)) 1665 return; 1666 1667 tcu_set_half_data_value(channel, value); 1668 } 1669 1670 static void set_tcu_mask(int full_mask, int masked) 1671 { 1672 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1673 1674 if (channel == NULL) 1675 return; 1676 1677 if (full_mask) 1678 tcu_set_full_data_mask(channel, masked); 1679 else 1680 tcu_set_half_data_mask(channel, masked); 1681 } 1682 1683 static void set_tcu_prescale(void) 1684 { 1685 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1686 uint32_t prescale; 1687 1688 if (channel == NULL) 1689 return; 1690 1691 if (!read_number("Prescale", &prescale)) 1692 return; 1693 1694 tcu_set_prescale(channel, prescale); 1695 } 1696 1697 static void tcu_wait(void) 1698 { 1699 void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); 1700 uint32_t timeout; 1701 1702 if (channel == NULL) 1703 return; 1704 1705 if (!read_number("Timeout", &timeout)) 1706 return; 1707 1708 printf("IRQ received: %s\n", tcu_wait_for_irq(channel, timeout) ? "yes" : "no"); 1709 } 1710 1711 1712 1713 /* Command processing. */ 1714 1715 static void handle_aic(void *aic) 1716 { 1717 char *token; 1718 1719 if ((token = read_token(NULL)) != NULL) 1720 { 1721 if (!strcmp(token, "l") || !strcmp(token, "list")) 1722 list_channels(num_aic_channels, aic_channels); 1723 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1724 new_aic_channel(aic); 1725 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1726 aic_transfer_data(); 1727 else 1728 printf("aic channel | list | transfer\n"); 1729 } 1730 else 1731 list_channels(num_aic_channels, aic_channels); 1732 } 1733 1734 static void handle_cpm(void *cpm) 1735 { 1736 char *token; 1737 1738 if ((token = read_token(NULL)) != NULL) 1739 { 1740 if (!strcmp(token, "l") || !strcmp(token, "list")) 1741 list_clocks(cpm); 1742 else if (!strcmp(token, "f") || !strcmp(token, "frequency")) 1743 set_frequency(cpm); 1744 else if (!strcmp(token, "p") || !strcmp(token, "parameters")) 1745 set_parameters(cpm); 1746 else if (!strcmp(token, "s") || !strcmp(token, "source")) 1747 set_source(cpm); 1748 else if (!strcmp(token, "start")) 1749 start_clock(cpm); 1750 else if (!strcmp(token, "stop")) 1751 stop_clock(cpm); 1752 else 1753 printf("cpm list | frequency | parameters | source | start | stop\n"); 1754 } 1755 else 1756 list_clocks(cpm); 1757 } 1758 1759 static void handle_dma(void *dma) 1760 { 1761 char *token; 1762 1763 if ((token = read_token(NULL)) != NULL) 1764 { 1765 if (!strcmp(token, "l") || !strcmp(token, "list")) 1766 list_channels(num_dma_channels, dma_channels); 1767 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1768 new_dma_channel(dma); 1769 else if (!strcmp(token, "r") || !strcmp(token, "region")) 1770 new_dma_region(); 1771 else if (!strcmp(token, "R") || !strcmp(token, "show")) 1772 show_dma_region(); 1773 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1774 set_dma_region(); 1775 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1776 dma_transfer_data(); 1777 else 1778 printf("dma channel | list | region | set | transfer\n"); 1779 } 1780 else 1781 list_channels(num_dma_channels, dma_channels); 1782 } 1783 1784 static void handle_gpio(void *gpio[]) 1785 { 1786 char *token; 1787 1788 if ((token = read_token(NULL)) != NULL) 1789 { 1790 if (!strcmp(token, "l") || !strcmp(token, "list")) 1791 list_gpios(gpio); 1792 else if (!strcmp(token, "a") || !strcmp(token, "alt")) 1793 set_gpio_alt_func(gpio); 1794 else if (!strcmp(token, "i") || !strcmp(token, "io")) 1795 set_gpio_pad(gpio); 1796 else if (!strcmp(token, "p") || !strcmp(token, "pull")) 1797 set_gpio_pull(gpio); 1798 else 1799 printf("gpio list | alt | io | pull\n"); 1800 } 1801 else 1802 list_gpios(gpio); 1803 } 1804 1805 static void handle_i2c(void *i2c) 1806 { 1807 char *token; 1808 1809 if ((token = read_token(NULL)) != NULL) 1810 { 1811 if (!strcmp(token, "l") || !strcmp(token, "list")) 1812 list_i2c_channels(); 1813 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1814 new_i2c_channel(i2c); 1815 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1816 i2c_get(); 1817 else if (!strcmp(token, "s") || !strcmp(token, "scan")) 1818 i2c_scan(); 1819 else 1820 printf("i2c channel | get | list | scan\n"); 1821 } 1822 else 1823 list_i2c_channels(); 1824 } 1825 1826 static void handle_memory(void) 1827 { 1828 char *token; 1829 1830 if ((token = read_token(NULL)) != NULL) 1831 { 1832 if (!strcmp(token, "r") || !strcmp(token, "region")) 1833 new_memory_region(); 1834 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1835 set_memory_region(); 1836 else 1837 printf("memory region | set\n"); 1838 } 1839 else 1840 list_memory_regions(); 1841 } 1842 1843 static void handle_msc(void *msc) 1844 { 1845 char *token; 1846 1847 if ((token = read_token(NULL)) != NULL) 1848 { 1849 if (!strcmp(token, "l") || !strcmp(token, "list")) 1850 list_msc_channels(); 1851 else if (!strcmp(token, "C") || !strcmp(token, "cards")) 1852 list_msc_cards(); 1853 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1854 new_msc_channel(msc); 1855 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1856 enable_msc_channel(); 1857 else if (!strcmp(token, "p") || !strcmp(token, "partitions")) 1858 list_msc_card_partitions(); 1859 else if (!strcmp(token, "r") || !strcmp(token, "read")) 1860 read_blocks_from_msc(); 1861 else 1862 printf("msc cards | channel | enable | partitions | read\n"); 1863 } 1864 else 1865 list_msc_channels(); 1866 } 1867 1868 static void handle_rtc(void *rtc, void *cpm) 1869 { 1870 char *token; 1871 1872 if ((token = read_token(NULL)) != NULL) 1873 { 1874 if (!strcmp(token, "d") || !strcmp(token, "disable")) 1875 rtc_disable(rtc); 1876 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1877 rtc_enable(rtc); 1878 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1879 printf("seconds = %d\n", rtc_get_seconds(rtc)); 1880 else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) 1881 printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); 1882 else if (!strcmp(token, "p") || !strcmp(token, "power-down")) 1883 rtc_power_down(rtc); 1884 else if (!strcmp(token, "r") || !strcmp(token, "reset")) 1885 rtc_reset(rtc, cpm); 1886 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1887 _rtc_set_seconds(rtc, 0); 1888 else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) 1889 _rtc_set_seconds(rtc, 1); 1890 else 1891 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1892 } 1893 else 1894 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1895 } 1896 1897 static void handle_spi(void *spi, void *gpio[]) 1898 { 1899 char *token; 1900 1901 if ((token = read_token(NULL)) != NULL) 1902 { 1903 if (!strcmp(token, "l") || !strcmp(token, "list")) 1904 list_channels(num_spi_channels, spi_channels); 1905 else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) 1906 spi_control(1); 1907 else if (!strcmp(token, "r") || !strcmp(token, "control-release")) 1908 spi_control(0); 1909 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1910 new_spi_channel(spi, gpio); 1911 else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) 1912 new_spi_channel_gpio(gpio); 1913 else if (!strcmp(token, "s") || !strcmp(token, "send")) 1914 spi_send_data(); 1915 else if (!strcmp(token, "S") || !strcmp(token, "send-units")) 1916 spi_send_data_units(); 1917 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1918 spi_transfer_data(); 1919 else 1920 printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); 1921 } 1922 else 1923 list_channels(num_spi_channels, spi_channels); 1924 } 1925 1926 static void handle_tcu(void *tcu) 1927 { 1928 char *token; 1929 1930 if ((token = read_token(NULL)) != NULL) 1931 { 1932 if (!strcmp(token, "l") || !strcmp(token, "list")) 1933 list_tcu_channels(); 1934 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1935 new_tcu_channel(tcu); 1936 else if (!strcmp(token, "C") || !strcmp(token, "clock")) 1937 set_tcu_clock(); 1938 else if (!strcmp(token, "d") || !strcmp(token, "disable")) 1939 disable_tcu_counter(); 1940 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1941 enable_tcu_counter(); 1942 else if (!strcmp(token, "f") || !strcmp(token, "full")) 1943 set_tcu_full(); 1944 else if (!strcmp(token, "fm") || !strcmp(token, "full-mask")) 1945 set_tcu_mask(1, 1); 1946 else if (!strcmp(token, "fu") || !strcmp(token, "full-unmask")) 1947 set_tcu_mask(1, 0); 1948 else if (!strcmp(token, "h") || !strcmp(token, "half")) 1949 set_tcu_half(); 1950 else if (!strcmp(token, "hm") || !strcmp(token, "half-mask")) 1951 set_tcu_mask(0, 1); 1952 else if (!strcmp(token, "hu") || !strcmp(token, "half-unmask")) 1953 set_tcu_mask(0, 0); 1954 else if (!strcmp(token, "p") || !strcmp(token, "prescale")) 1955 set_tcu_prescale(); 1956 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1957 set_tcu_counter(); 1958 else if (!strcmp(token, "w") || !strcmp(token, "wait")) 1959 tcu_wait(); 1960 else 1961 printf("tcu channel | clock | disable | enable | full | full-mask | " \ 1962 "full-unmask | half | half-mask | half-unmask | list | " \ 1963 "prescale | set | wait\n"); 1964 } 1965 else 1966 list_tcu_channels(); 1967 } 1968 1969 1970 1971 int main(void) 1972 { 1973 l4_addr_t aic_base = 0, aic_base_end = 0; 1974 l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; 1975 l4_addr_t cpm_base = 0, cpm_base_end = 0; 1976 l4_addr_t dma_base = 0, dma_base_end = 0; 1977 l4_addr_t gpio_base = 0, gpio_base_end = 0; 1978 l4_addr_t i2c_base = 0, i2c_base_end = 0; 1979 l4_addr_t msc_base = 0, msc_base_end = 0; 1980 l4_addr_t msc_phys_base = 0, msc_phys_base_end = 0; 1981 l4_addr_t rtc_base = 0, rtc_base_end = 0; 1982 l4_addr_t ssi_base = 0, ssi_base_end = 0; 1983 l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; 1984 l4_addr_t tcu_base = 0, tcu_base_end = 0; 1985 void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *msc, *rtc, *spi, *tcu; 1986 int result = 0; 1987 unsigned int port; 1988 1989 icucap = l4re_env_get_cap("icu"); 1990 1991 /* Obtain resource details describing I/O memory. */ 1992 1993 printf("Access CPM...\n"); 1994 1995 if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) 1996 return 1; 1997 1998 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 1999 2000 cpm = cpm_init(cpm_base); 2001 2002 printf("Access DMA...\n"); 2003 2004 if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) 2005 return 1; 2006 2007 printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); 2008 2009 dma = dma_init(dma_base, dma_base_end, cpm); 2010 2011 if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) 2012 return 1; 2013 2014 printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); 2015 2016 if (init_dma()) 2017 return 1; 2018 2019 dma_enable(dma); 2020 2021 printf("Access GPIO...\n"); 2022 2023 if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) 2024 return 1; 2025 2026 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 2027 2028 for (port = 0; port < num_gpio_ports; port++) 2029 gpio[port] = gpio_init_shadow(gpio_base, port); 2030 2031 printf("Access I2C...\n"); 2032 2033 if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) 2034 return 1; 2035 2036 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 2037 2038 i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); 2039 2040 if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) 2041 return 1; 2042 2043 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 2044 2045 printf("Access AIC...\n"); 2046 2047 if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, 2048 &aic_phys_base, &aic_phys_base_end)) < 0) 2049 return 1; 2050 2051 printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); 2052 2053 aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); 2054 2055 printf("Access MSC...\n"); 2056 2057 if ((result = get_memory_complete(io_memory_regions[MSC], &msc_base, &msc_base_end, 2058 &msc_phys_base, &msc_phys_base_end)) < 0) 2059 return 1; 2060 2061 printf("MSC at 0x%lx...0x%lx.\n", msc_base, msc_base_end); 2062 2063 msc = msc_init(msc_phys_base, msc_base, msc_base_end, cpm); 2064 2065 if (get_irq(io_memory_regions[MSC], &msc_irq_start, &msc_irq_end) < 0) 2066 return 1; 2067 2068 printf("IRQ range at %d...%d.\n", msc_irq_start, msc_irq_end); 2069 2070 printf("Access RTC...\n"); 2071 2072 if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) 2073 return 1; 2074 2075 printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); 2076 2077 rtc = rtc_init(rtc_base, cpm); 2078 2079 printf("Access SSI...\n"); 2080 2081 if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, 2082 &ssi_phys_base, &ssi_phys_base_end)) < 0) 2083 return 1; 2084 2085 printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); 2086 2087 spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); 2088 2089 printf("Access TCU...\n"); 2090 2091 if ((result = get_memory(io_memory_regions[TCU], &tcu_base, &tcu_base_end)) < 0) 2092 return 1; 2093 2094 printf("TCU at 0x%lx...0x%lx.\n", tcu_base, tcu_base_end); 2095 2096 tcu = tcu_init(tcu_base, tcu_base_end); 2097 2098 if (get_irq(io_memory_regions[TCU], &tcu_irq_start, &tcu_irq_end) < 0) 2099 return 1; 2100 2101 printf("IRQ range at %d...%d.\n", tcu_irq_start, tcu_irq_end); 2102 2103 if (init_tcu()) 2104 return 1; 2105 2106 /* Start the interactive session. */ 2107 2108 printf("aic, cpm, dma, gpio, i2c, msc, rtc, spi, tcu\n"); 2109 2110 while (1) 2111 { 2112 char cmdline[256], *token; 2113 2114 printf("> "); 2115 2116 token = fgets(cmdline, 256, stdin); 2117 2118 if (token == NULL) 2119 break; 2120 2121 if ((token = strtok(cmdline, delimiters)) == NULL) 2122 continue; 2123 2124 /* AIC/I2S commands. */ 2125 2126 if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) 2127 handle_aic(aic); 2128 2129 /* CPM commands. */ 2130 2131 else if (!strcmp(token, "c") || !strcmp(token, "cpm")) 2132 handle_cpm(cpm); 2133 2134 /* DMA commands. */ 2135 2136 else if (!strcmp(token, "d") || !strcmp(token, "dma")) 2137 handle_dma(dma); 2138 2139 /* GPIO commands. */ 2140 2141 else if (!strcmp(token, "g") || !strcmp(token, "gpio")) 2142 handle_gpio(gpio); 2143 2144 /* I2C commands. */ 2145 2146 else if (!strcmp(token, "i") || !strcmp(token, "i2c")) 2147 handle_i2c(i2c); 2148 2149 /* MSC commands. */ 2150 2151 else if (!strcmp(token, "M") || !strcmp(token, "msc")) 2152 handle_msc(msc); 2153 2154 /* Generic memory commands. */ 2155 2156 else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) 2157 handle_memory(); 2158 2159 /* RTC commands. */ 2160 2161 else if (!strcmp(token, "r") || !strcmp(token, "rtc")) 2162 handle_rtc(rtc, cpm); 2163 2164 /* SPI commands. */ 2165 2166 else if (!strcmp(token, "s") || !strcmp(token, "spi")) 2167 handle_spi(spi, gpio); 2168 2169 /* TCU commands. */ 2170 2171 else if (!strcmp(token, "t") || !strcmp(token, "tcu")) 2172 handle_tcu(tcu); 2173 2174 /* Comments and blank lines. */ 2175 2176 else if (strncmp(token, "#", 1) && strlen(token)) 2177 printf("Command?\n"); 2178 } 2179 2180 printf("End of session.\n"); 2181 return 0; 2182 }