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