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