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