1 /* 2 * Access various peripherals on the MIPS Creator CI20 board. 3 * 4 * Copyright (C) 2023 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 *addr = (l4_addr_t) calloc(size, sizeof(char)); 252 253 return !(*addr); 254 } 255 256 257 258 /* AIC/I2S operations. */ 259 260 static void new_aic_channel(void *aic) 261 { 262 int num = get_channel_number(num_aic_channels); 263 void *channel; 264 265 if (num < 0) 266 return; 267 268 channel = get_channel(num_dma_channels, dma_channels, NULL); 269 270 if (channel == NULL) 271 return; 272 273 aic_channels[num] = aic_get_channel(aic, num, channel); 274 } 275 276 static void aic_transfer_data(void) 277 { 278 void *channel = get_channel(num_aic_channels, aic_channels, NULL); 279 struct dma_region *region; 280 uint32_t count, rate, resolution, transferred; 281 282 if (channel == NULL) 283 return; 284 285 region = _get_dma_region(); 286 287 if (region == NULL) 288 return; 289 290 if (!read_number("Sample size", &count)) 291 return; 292 293 if (!read_number("Sample rate", &rate)) 294 return; 295 296 if (!read_number("Sample resolution", &resolution)) 297 return; 298 299 transferred = aic_transfer(channel, region->paddr, count, rate, resolution); 300 301 printf("Transferred: %d\n", transferred); 302 } 303 304 305 306 /* CPM operations. */ 307 308 static const char *clock_id(enum Clock_identifiers clock) 309 { 310 for (int i = 0; clocks[i].id != NULL; i++) 311 if (clock == clocks[i].clock) 312 return clocks[i].id; 313 314 return NULL; 315 } 316 317 static enum Clock_identifiers get_clock(char *id) 318 { 319 if (id != NULL) 320 { 321 for (int i = 0; clocks[i].id != NULL; i++) 322 if (!strcmp(id, clocks[i].id)) 323 return clocks[i].clock; 324 } 325 326 return Clock_undefined; 327 } 328 329 static const char list_clocks_separator[] = "--------------------------------"; 330 331 #define sep(x) (list_clocks_separator + sizeof(list_clocks_separator) - 1 - x) 332 333 static void list_clocks(void *cpm) 334 { 335 /* Read information from the clock and power management unit. */ 336 337 printf("%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n" 338 "%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n", 339 "Id", "Clock", "Type", "Source", "Source Clock", "Source Freq.", 340 "Parameters", "Frequency", "On", 341 sep(10), sep(15), sep(7), sep(6), sep(12), sep(12), 342 sep(24), sep(12), sep(3)); 343 344 for (int i = 0; clocks[i].id != NULL; i++) 345 { 346 uint32_t parameters[4]; 347 char parameter_str[25] = {0}; 348 const char *source_id = clock_id(cpm_get_source_clock(cpm, clocks[i].clock)); 349 int num_parameters = cpm_get_parameters(cpm, clocks[i].clock, parameters); 350 351 for (int p = 0, pos = 0; p < num_parameters; p++) 352 { 353 int result = sprintf(parameter_str + pos, "%-7d ", parameters[p]); 354 if (result < 0) 355 break; 356 pos += result; 357 } 358 359 printf("%-10s %-15s %-7s %-6d %-12s %-12lld %-24s %-12lld %-3s\n", 360 clocks[i].id, 361 clocks[i].name, 362 cpm_clock_type(cpm, clocks[i].clock), 363 cpm_get_source(cpm, clocks[i].clock), 364 source_id != NULL ? source_id : "...", 365 cpm_get_source_frequency(cpm, clocks[i].clock), 366 parameter_str, 367 cpm_get_frequency(cpm, clocks[i].clock), 368 cpm_have_clock(cpm, clocks[i].clock) ? "on" : "off"); 369 } 370 } 371 372 static enum Clock_identifiers get_clock_by_name(void) 373 { 374 char *token; 375 enum Clock_identifiers clock; 376 377 if ((token = read_token("Clock")) == NULL) 378 return Clock_undefined; 379 380 clock = get_clock(token); 381 382 if (clock == Clock_undefined) 383 { 384 printf("Unrecognised clock: %s\n", token); 385 return Clock_undefined; 386 } 387 388 return clock; 389 } 390 391 static void set_frequency(void *cpm) 392 { 393 enum Clock_identifiers clock; 394 unsigned int frequency; 395 396 clock = get_clock_by_name(); 397 398 if (clock == Clock_undefined) 399 return; 400 401 if (!read_number("Frequency", &frequency)) 402 return; 403 404 if (!cpm_set_frequency(cpm, clock, frequency)) 405 printf("Frequency not set.\n"); 406 } 407 408 static void set_parameters(void *cpm) 409 { 410 char *token; 411 enum Clock_identifiers clock; 412 uint32_t parameters[4]; 413 int num_parameters; 414 415 clock = get_clock_by_name(); 416 417 if (clock == Clock_undefined) 418 return; 419 420 for (num_parameters = 0; num_parameters < 4; num_parameters++) 421 { 422 if ((token = read_token(NULL)) == NULL) 423 break; 424 425 parameters[num_parameters] = atoi(token); 426 } 427 428 if (!num_parameters) 429 { 430 printf("Parameters?\n"); 431 return; 432 } 433 434 if (!cpm_set_parameters(cpm, clock, num_parameters, parameters)) 435 printf("Too few or invalid parameters.\n"); 436 } 437 438 static void set_source(void *cpm) 439 { 440 enum Clock_identifiers clock; 441 unsigned int source; 442 443 clock = get_clock_by_name(); 444 445 if (clock == Clock_undefined) 446 return; 447 448 if (!read_number("Source", &source)) 449 return; 450 451 cpm_set_source(cpm, clock, source); 452 } 453 454 static void start_clock(void *cpm) 455 { 456 enum Clock_identifiers clock; 457 458 clock = get_clock_by_name(); 459 460 if (clock == Clock_undefined) 461 return; 462 463 cpm_start_clock(cpm, clock); 464 } 465 466 static void stop_clock(void *cpm) 467 { 468 enum Clock_identifiers clock; 469 470 clock = get_clock_by_name(); 471 472 if (clock == Clock_undefined) 473 return; 474 475 cpm_stop_clock(cpm, clock); 476 } 477 478 479 480 /* DMA configuration. */ 481 482 static l4_uint32_t dma_irq_start = 0, dma_irq_end = 0; 483 484 /* DMA operations. */ 485 486 static int init_dma(void) 487 { 488 unsigned int i; 489 490 for (i = 0; i < num_dma_regions; i++) 491 dma_regions[i].mem = L4_INVALID_CAP; 492 493 /* Here, only one IRQ is used. */ 494 495 dma_irq = l4re_util_cap_alloc(); 496 return init_irq(0, dma_irq, dma_irq_start, dma_irq_end); 497 } 498 499 static void list_dma_regions(void) 500 { 501 unsigned int num; 502 struct dma_region *region; 503 unsigned int i; 504 505 for (num = 0; num < num_dma_regions; num++) 506 { 507 printf("Region %d: ", num); 508 509 region = &dma_regions[num]; 510 511 if (l4_is_invalid_cap(region->mem)) 512 printf("(inactive)\n"); 513 else 514 { 515 printf("size = %d; align = %d; virtual = 0x%lx; physical = 0x%llx\ndata =", 516 region->size, region->align, region->vaddr, region->paddr); 517 518 for (i = 0; (i < region->size) && (i < 16); i++) 519 printf(" %02x", *((uint8_t *) region->vaddr + i)); 520 521 printf("\n"); 522 } 523 } 524 } 525 526 static void new_dma_channel(void *dma) 527 { 528 int num = get_channel_number(num_dma_channels); 529 530 if (num < 0) 531 return; 532 533 if (dma_channels[num] != NULL) 534 { 535 printf("Channel already defined.\n"); 536 return; 537 } 538 539 dma_channels[num] = dma_get_channel(dma, num, dma_irq); 540 } 541 542 static void new_dma_region(void) 543 { 544 struct dma_region *region = _get_dma_region(); 545 546 if (region == NULL) 547 { 548 list_dma_regions(); 549 return; 550 } 551 552 if (l4_is_valid_cap(region->mem)) 553 { 554 printf("Region already defined.\n"); 555 return; 556 } 557 558 if (!read_number("Size", ®ion->size)) 559 return; 560 561 if (!read_number("Alignment", ®ion->align)) 562 return; 563 564 if (get_dma_region(region->size, region->align, ®ion->vaddr, ®ion->paddr, ®ion->mem)) 565 printf("Could not allocate region.\n"); 566 } 567 568 static void set_dma_region(void) 569 { 570 char *token; 571 struct dma_region *region = _get_dma_region(); 572 FILE *fp; 573 574 if (region == NULL) 575 { 576 list_dma_regions(); 577 return; 578 } 579 580 if (l4_is_invalid_cap(region->mem)) 581 { 582 printf("Region needs creating.\n"); 583 return; 584 } 585 586 memset((void *) region->vaddr, 0, region->size); 587 588 if ((token = read_token("Filename")) == NULL) 589 return; 590 591 /* Populate the region from the file. */ 592 593 fp = fopen(token, "r"); 594 595 if (fp == NULL) 596 { 597 printf("File not readable.\n"); 598 return; 599 } 600 601 fread((char *) region->vaddr, sizeof(char), region->size, fp); 602 fclose(fp); 603 604 l4_cache_flush_data(region->vaddr, region->vaddr + region->size); 605 } 606 607 static void dma_transfer_data(void) 608 { 609 uint32_t count, to_transfer, transferred; 610 unsigned int source_address, destination_address, source_width, destination_width, 611 transfer_unit_size, request_type; 612 int source_increment, destination_increment; 613 void *channel = get_channel(num_dma_channels, dma_channels, NULL); 614 unsigned int i; 615 616 if (channel == NULL) 617 return; 618 619 if (!read_number("Source address", &source_address)) 620 return; 621 622 if (!read_number("Destination address", &destination_address)) 623 return; 624 625 if (!read_number("Transfer size", &count)) 626 return; 627 628 if (!read_flag("Source increment", &source_increment, "i", 1)) 629 return; 630 631 if (!read_flag("Destination increment", &destination_increment, "i", 1)) 632 return; 633 634 if (!read_number("Source width", &source_width)) 635 return; 636 637 if (!read_number("Destination width", &destination_width)) 638 return; 639 640 if (!read_number("Transfer unit size", &transfer_unit_size)) 641 return; 642 643 if (!read_number("Request type", &request_type)) 644 return; 645 646 /* Invalidate DMA region contents in case of transfers between regions. */ 647 648 for (i = 0; i < num_dma_regions; i++) 649 l4_cache_inv_data(dma_regions[i].vaddr, dma_regions[i].vaddr + dma_regions[i].size); 650 651 to_transfer = dma_transfer(channel, source_address, destination_address, 652 count, 653 source_increment, destination_increment, 654 source_width, destination_width, 655 transfer_unit_size, 656 request_type); 657 658 transferred = to_transfer ? count - dma_wait(channel) : 0; 659 660 printf("Transferred: %d\n", transferred); 661 } 662 663 664 665 /* GPIO operations. */ 666 667 static char get_gpio_dir_label(unsigned int value, unsigned int level) 668 { 669 return value == Fix_input ? (level ? 'I' : 'i') : (level ? 'O' : 'o'); 670 } 671 672 static char get_gpio_irq_label(unsigned int value) 673 { 674 switch (value) 675 { 676 case L4_IRQ_F_NEG_EDGE: return 'f'; 677 case L4_IRQ_F_POS_EDGE: return 'r'; 678 case L4_IRQ_F_LEVEL_LOW: return 'l'; 679 case L4_IRQ_F_LEVEL_HIGH: return 'h'; 680 default: return '?'; 681 } 682 } 683 684 static void list_gpios(void *gpio[]) 685 { 686 unsigned int port, pin; 687 unsigned int func, value, level; 688 689 printf("gpio: I = input high; i = input low; O = output high; o = output low\n" 690 "irq: h = high level; l = low level; r = rising edge; f = falling edge\n" 691 "alt: function number\n\n"); 692 693 /* Show pin numbering. */ 694 695 printf("Port/Pin "); 696 697 for (pin = 0; pin < 32; pin++) 698 { 699 if (!(pin % 10)) 700 printf(" %d", pin); 701 else 702 printf(" %d", pin % 10); 703 } 704 705 printf("\n"); 706 707 for (port = 0; port < num_gpio_ports; port++) 708 { 709 /* Show port and pin configuration. */ 710 711 printf("%c ", gpio_port_labels[port]); 712 713 for (pin = 0; pin < 32; pin++) 714 { 715 /* Pad below the first pin digit for multiples of ten other than zero. */ 716 717 if (pin && !(pin % 10)) 718 printf(" "); 719 720 gpio_config_pad_get(gpio[port], pin, &func, &value); 721 722 switch (func) 723 { 724 case Function_alt: printf(" %d", value); break; 725 case Function_irq: printf(" %c", get_gpio_irq_label(value)); break; 726 case Function_gpio: 727 { 728 level = gpio_get(gpio[port], pin); 729 printf(" %c", get_gpio_dir_label(value, level)); 730 break; 731 } 732 default: printf(" ?"); break; 733 } 734 } 735 736 printf("\n"); 737 } 738 } 739 740 static int get_port_and_pin(unsigned int *port, unsigned int *pin) 741 { 742 char *token; 743 744 if ((token = read_token("Port")) == NULL) 745 return 0; 746 747 if ((token[0] < 'A') || ((unsigned int) token[0] - (unsigned int) 'A' >= num_gpio_ports)) 748 { 749 printf("Bad port: %c\n", token[0]); 750 return 0; 751 } 752 753 *port = (unsigned int) token[0] - (unsigned int) 'A'; 754 755 if (!read_number("Pin", pin)) 756 return 0; 757 758 return 1; 759 } 760 761 static void set_gpio_alt_func(void *gpio[]) 762 { 763 unsigned int port, pin, value; 764 765 if (!get_port_and_pin(&port, &pin)) 766 return; 767 768 if (!read_number("Function", &value)) 769 return; 770 771 gpio_config_pad(gpio[port], pin, Function_alt, value); 772 } 773 774 static void set_gpio_pad(void *gpio[]) 775 { 776 char *token; 777 unsigned int port, pin, mode, value = 0; 778 779 if (!get_port_and_pin(&port, &pin)) 780 return; 781 782 if ((token = read_token("Mode")) == NULL) 783 return; 784 785 if (!strcmp(token, "in")) 786 mode = Fix_input; 787 else if (!strcmp(token, "irq")) 788 mode = Fix_irq; 789 else if (!strcmp(token, "out")) 790 { 791 mode = Fix_output; 792 793 if ((token = read_token(NULL)) != NULL) 794 value = atoi(token); 795 } 796 else 797 { 798 printf("Mode not recognised.\n"); 799 return; 800 } 801 802 gpio_setup(gpio[port], pin, mode, value); 803 } 804 805 static void set_gpio_pull(void *gpio[]) 806 { 807 char *token; 808 unsigned int port, pin, mode; 809 810 if (!get_port_and_pin(&port, &pin)) 811 return; 812 813 if ((token = read_token("Mode")) == NULL) 814 return; 815 816 if (!strcmp(token, "down")) 817 mode = Pull_down; 818 else if (!strcmp(token, "none")) 819 mode = Pull_none; 820 else if (!strcmp(token, "up")) 821 mode = Pull_up; 822 else 823 { 824 printf("Mode not recognised.\n"); 825 return; 826 } 827 828 gpio_config_pull(gpio[port], pin, mode); 829 } 830 831 832 833 /* I2C configuration. */ 834 835 static l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; 836 837 /* I2C operations. */ 838 839 static int _i2c_read(void *channel, uint8_t *buf, unsigned length, 840 int stop, l4_cap_idx_t irqcap, unsigned timeout) 841 { 842 l4_msgtag_t tag; 843 844 i2c_start_read(channel, buf, length, stop); 845 846 while (!i2c_read_done(channel)) 847 { 848 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); 849 850 // NOTE: Error not returned. 851 852 if (l4_ipc_error(tag, l4_utcb())) 853 break; 854 855 if (i2c_failed(channel)) 856 break; 857 858 i2c_read(channel); 859 } 860 861 if (stop) 862 i2c_stop(channel); 863 864 return i2c_have_read(channel); 865 } 866 867 static int _i2c_write(void *channel, uint8_t *buf, unsigned length, 868 int stop, l4_cap_idx_t irqcap, unsigned timeout) 869 { 870 l4_msgtag_t tag; 871 872 i2c_start_write(channel, buf, length, stop); 873 874 while (!i2c_write_done(channel)) 875 { 876 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); 877 878 // NOTE: Error not returned. 879 880 if (l4_ipc_error(tag, l4_utcb())) 881 break; 882 883 if (i2c_failed(channel)) 884 break; 885 886 i2c_write(channel); 887 } 888 889 if (stop) 890 i2c_stop(channel); 891 892 return i2c_have_written(channel); 893 } 894 895 static void list_i2c_channels(void) 896 { 897 unsigned int num; 898 void *channel; 899 900 printf("i2c: channel\n\n"); 901 902 for (num = 0; num < num_i2c_channels; num++) 903 { 904 printf("Channel %d: ", num); 905 906 channel = i2c_channels[num]; 907 908 if (channel == NULL) 909 printf("(inactive)\n"); 910 else 911 printf("%d Hz\n", i2c_get_frequency(channel)); 912 } 913 } 914 915 static void new_i2c_channel(void *i2c) 916 { 917 l4_cap_idx_t irqcap; 918 int num = get_channel_number(num_i2c_channels); 919 920 if (num < 0) 921 return; 922 923 irqcap = l4re_util_cap_alloc(); 924 925 if (init_irq(num, irqcap, i2c_irq_start, i2c_irq_end)) 926 return; 927 928 i2c_channels[num] = i2c_get_channel(i2c, num); 929 i2c_irqs[num] = irqcap; 930 } 931 932 static void i2c_get(void) 933 { 934 void *channel; 935 int num; 936 uint8_t buffer[32]; 937 unsigned int address, reg, count; 938 int i, transferred; 939 940 channel = get_channel(num_i2c_channels, i2c_channels, &num); 941 942 if (channel == NULL) 943 return; 944 945 if (!read_encoded_number("Address", "%2x", &address)) 946 return; 947 948 if (address >= 0x80) 949 { 950 printf("Address must be less than 80.\n"); 951 return; 952 } 953 954 if (!read_encoded_number("Register", "%2x", ®)) 955 return; 956 957 if (!read_number(NULL, &count)) 958 count = 1; 959 960 buffer[0] = (uint8_t) (reg & 0xff); 961 962 i2c_set_target(channel, address); 963 964 if (!_i2c_write(channel, buffer, 1, 0, i2c_irqs[num], 1000000)) 965 { 966 printf("Register request failed.\n"); 967 return; 968 } 969 970 transferred = _i2c_read(channel, buffer, count, 1, i2c_irqs[num], 1000000); 971 972 if (!transferred) 973 { 974 printf("Register read failed.\n"); 975 return; 976 } 977 978 for (i = 0; i < transferred; i++) 979 printf("%02x ", buffer[i]); 980 printf("\n"); 981 } 982 983 static void i2c_scan(void) 984 { 985 void *channel; 986 unsigned int address; 987 uint8_t buffer[1]; 988 int num; 989 990 channel = get_channel(num_i2c_channels, i2c_channels, &num); 991 992 if (channel == NULL) 993 return; 994 995 for (address = 0; address < 0x20; address++) 996 printf("%02x ", address); 997 printf("\n"); 998 999 for (address = 0; address < 0x20; address++) 1000 printf("-- "); 1001 1002 for (address = 0; address < 0x80; address++) 1003 { 1004 if ((address % 32) == 0) 1005 printf("\n"); 1006 1007 i2c_set_target(channel, address); 1008 1009 if (_i2c_read(channel, buffer, 1, 1, i2c_irqs[num], 1000)) 1010 printf("%02x ", address); 1011 else 1012 printf("-- "); 1013 } 1014 1015 printf("\n"); 1016 for (address = 0; address < 0x20; address++) 1017 printf("-- "); 1018 printf("\n\n"); 1019 } 1020 1021 1022 1023 /* Memory operations. */ 1024 1025 static void list_memory_regions(void) 1026 { 1027 unsigned int num; 1028 struct memory_region *region; 1029 unsigned int i; 1030 1031 for (num = 0; num < num_memory_regions; num++) 1032 { 1033 printf("Region %d: ", num); 1034 1035 region = &memory_regions[num]; 1036 1037 if (!region->addr) 1038 printf("(inactive)\n"); 1039 else 1040 { 1041 printf("size = %d; virtual = 0x%lx; data =", 1042 region->size, region->addr); 1043 1044 for (i = 0; (i < region->size) && (i < 16); i++) 1045 printf(" %02x", *((uint8_t *) region->addr + i)); 1046 1047 printf("\n"); 1048 } 1049 } 1050 } 1051 1052 static void new_memory_region(void) 1053 { 1054 struct memory_region *region = _get_memory_region(); 1055 1056 if (region == NULL) 1057 { 1058 list_memory_regions(); 1059 return; 1060 } 1061 1062 if (region->addr) 1063 { 1064 printf("Region already defined.\n"); 1065 return; 1066 } 1067 1068 if (!read_number("Size", ®ion->size)) 1069 return; 1070 1071 if (get_memory_region(region->size, ®ion->addr)) 1072 printf("Could not allocate region.\n"); 1073 } 1074 1075 static void set_memory_region(void) 1076 { 1077 char *token; 1078 struct memory_region *region = _get_memory_region(); 1079 FILE *fp; 1080 1081 if (region == NULL) 1082 { 1083 list_memory_regions(); 1084 return; 1085 } 1086 1087 if (!region->addr) 1088 { 1089 printf("Region needs creating.\n"); 1090 return; 1091 } 1092 1093 memset((void *) region->addr, 0, region->size); 1094 1095 if ((token = read_token("Filename")) == NULL) 1096 return; 1097 1098 /* Populate the region from the file. */ 1099 1100 fp = fopen(token, "r"); 1101 1102 if (fp == NULL) 1103 { 1104 printf("File not readable.\n"); 1105 return; 1106 } 1107 1108 fread((char *) region->addr, sizeof(char), region->size, fp); 1109 fclose(fp); 1110 1111 l4_cache_flush_data(region->addr, region->addr + region->size); 1112 } 1113 1114 1115 1116 /* RTC operations. */ 1117 1118 static void rtc_reset(void *rtc, void *cpm) 1119 { 1120 unsigned int seconds; 1121 1122 if (!read_number("Seconds", &seconds)) 1123 return; 1124 1125 /* NOTE: Assuming EXCLK/512 as RTC source. */ 1126 1127 uint32_t rtc_seconds = rtc_get_seconds(rtc); 1128 uint32_t value = seconds * cpm_get_frequency(cpm, Clock_external) / 512; 1129 1130 rtc_alarm_disable(rtc); 1131 rtc_set_alarm_seconds(rtc, rtc_seconds + value); 1132 rtc_hibernate(rtc); 1133 } 1134 1135 static void _rtc_set_seconds(void *rtc, int alarm) 1136 { 1137 unsigned int seconds; 1138 1139 if (!read_number("Seconds", &seconds)) 1140 return; 1141 1142 (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); 1143 } 1144 1145 1146 1147 /* SPI operations. */ 1148 1149 static void new_spi_channel(void *spi, void *gpio[]) 1150 { 1151 unsigned int control_port, control_pin, control_alt_func_input; 1152 int control_alt_func; 1153 void *control_chip; 1154 int num = get_channel_number(num_spi_channels); 1155 void *channel; 1156 uint32_t frequency; 1157 1158 if (num < 0) 1159 return; 1160 1161 channel = get_channel(num_dma_channels, dma_channels, NULL); 1162 1163 if (channel == NULL) 1164 return; 1165 1166 if (!read_number("Frequency", &frequency)) 1167 return; 1168 1169 if (!get_port_and_pin(&control_port, &control_pin)) 1170 control_chip = NULL; 1171 else 1172 control_chip = gpio[control_port]; 1173 1174 if (!read_number("Function", &control_alt_func_input)) 1175 control_alt_func = -1; 1176 else 1177 control_alt_func = control_alt_func_input; 1178 1179 spi_channels[num] = spi_get_channel(spi, num, channel, frequency, 1180 control_chip, control_pin, 1181 control_alt_func); 1182 } 1183 1184 static void new_spi_channel_gpio(void *gpio[]) 1185 { 1186 unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, 1187 enable_pin, control_port, control_pin; 1188 void *control_chip; 1189 int num = get_channel_number(num_spi_channels); 1190 uint32_t frequency; 1191 1192 if (num < 0) 1193 return; 1194 1195 if (!read_number("Frequency", &frequency)) 1196 return; 1197 1198 if (!get_port_and_pin(&clock_port, &clock_pin)) 1199 return; 1200 1201 if (!get_port_and_pin(&data_port, &data_pin)) 1202 return; 1203 1204 if (!get_port_and_pin(&enable_port, &enable_pin)) 1205 return; 1206 1207 if (!get_port_and_pin(&control_port, &control_pin)) 1208 control_chip = NULL; 1209 else 1210 control_chip = gpio[control_port]; 1211 1212 spi_channels[num] = spi_get_channel_gpio(frequency, 1213 gpio[clock_port], clock_pin, 1214 gpio[data_port], data_pin, 1215 gpio[enable_port], enable_pin, 1216 control_chip, control_pin); 1217 } 1218 1219 static void spi_control(int acquire) 1220 { 1221 unsigned int level; 1222 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1223 1224 if (acquire) 1225 { 1226 if (!read_number("Level", &level)) 1227 return; 1228 1229 spi_acquire_control(channel, level); 1230 } 1231 else 1232 spi_release_control(channel); 1233 } 1234 1235 static void spi_send_data(void) 1236 { 1237 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1238 int bytes; 1239 unsigned int byte; 1240 uint8_t buffer[256]; 1241 1242 bytes = 0; 1243 1244 while (read_encoded_number(NULL, "%2x", &byte)) 1245 { 1246 buffer[bytes] = (uint8_t) (byte & 0xff); 1247 bytes++; 1248 } 1249 1250 spi_send(channel, bytes, buffer); 1251 } 1252 1253 static void spi_send_data_units(void) 1254 { 1255 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1256 unsigned int char_size, unit_size, value; 1257 uint8_t buffer[256]; 1258 int byte = 0; 1259 1260 if (!read_number("Unit size", &unit_size)) 1261 return; 1262 1263 if (!read_number("Character size", &char_size)) 1264 return; 1265 1266 /* Read hex digits for bytes. Multiple bytes make up each unit and are read 1267 from most to least significant. Where the unit size exceeds the character 1268 size, the last bit before the character indicates the GPC bit. */ 1269 1270 while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) 1271 buffer[byte++] = value; 1272 1273 /* Explicitly indicate big endian data. */ 1274 1275 spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); 1276 } 1277 1278 static void spi_transfer_data(void) 1279 { 1280 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1281 struct dma_region *dma_region = NULL, *desc_region; 1282 struct memory_region *memory_region = NULL; 1283 unsigned int char_size, unit_size; 1284 uint32_t count, transferred; 1285 int using_dma; 1286 1287 if (channel == NULL) 1288 return; 1289 1290 if (!read_flag("Using DMA", &using_dma, "d", 1)) 1291 return; 1292 1293 if (using_dma) 1294 dma_region = _get_dma_region(); 1295 else 1296 memory_region = _get_memory_region(); 1297 1298 if ((dma_region == NULL) && (memory_region == NULL)) 1299 return; 1300 1301 if (!read_number("Transfer size", &count)) 1302 return; 1303 1304 if (!read_number("Unit size", &unit_size)) 1305 return; 1306 1307 if (!read_number("Character size", &char_size)) 1308 return; 1309 1310 if (using_dma) 1311 { 1312 desc_region = _get_dma_region(); 1313 1314 transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, 1315 unit_size, char_size, 1316 desc_region != NULL ? desc_region->vaddr : 0, 1317 desc_region != NULL ? desc_region->paddr : 0); 1318 } 1319 else 1320 transferred = spi_transfer(channel, memory_region->addr, 0, count, 1321 unit_size, char_size, 0, 0); 1322 1323 printf("Transferred: %d\n", transferred); 1324 } 1325 1326 1327 1328 /* Command processing. */ 1329 1330 static void handle_aic(void *aic) 1331 { 1332 char *token; 1333 1334 if ((token = read_token(NULL)) != NULL) 1335 { 1336 if (!strcmp(token, "l") || !strcmp(token, "list")) 1337 list_channels(num_aic_channels, aic_channels); 1338 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1339 new_aic_channel(aic); 1340 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1341 aic_transfer_data(); 1342 else 1343 printf("aic channel | list | transfer\n"); 1344 } 1345 else 1346 list_channels(num_aic_channels, aic_channels); 1347 } 1348 1349 static void handle_cpm(void *cpm) 1350 { 1351 char *token; 1352 1353 if ((token = read_token(NULL)) != NULL) 1354 { 1355 if (!strcmp(token, "l") || !strcmp(token, "list")) 1356 list_clocks(cpm); 1357 else if (!strcmp(token, "f") || !strcmp(token, "frequency")) 1358 set_frequency(cpm); 1359 else if (!strcmp(token, "p") || !strcmp(token, "parameters")) 1360 set_parameters(cpm); 1361 else if (!strcmp(token, "s") || !strcmp(token, "source")) 1362 set_source(cpm); 1363 else if (!strcmp(token, "start")) 1364 start_clock(cpm); 1365 else if (!strcmp(token, "stop")) 1366 stop_clock(cpm); 1367 else 1368 printf("cpm list | frequency | parameters | source | start | stop\n"); 1369 } 1370 else 1371 list_clocks(cpm); 1372 } 1373 1374 static void handle_dma(void *dma) 1375 { 1376 char *token; 1377 1378 if ((token = read_token(NULL)) != NULL) 1379 { 1380 if (!strcmp(token, "l") || !strcmp(token, "list")) 1381 list_channels(num_dma_channels, dma_channels); 1382 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1383 new_dma_channel(dma); 1384 else if (!strcmp(token, "r") || !strcmp(token, "region")) 1385 new_dma_region(); 1386 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1387 set_dma_region(); 1388 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1389 dma_transfer_data(); 1390 else 1391 printf("dma channel | list | region | set | transfer\n"); 1392 } 1393 else 1394 list_channels(num_dma_channels, dma_channels); 1395 } 1396 1397 static void handle_gpio(void *gpio[]) 1398 { 1399 char *token; 1400 1401 if ((token = read_token(NULL)) != NULL) 1402 { 1403 if (!strcmp(token, "l") || !strcmp(token, "list")) 1404 list_gpios(gpio); 1405 else if (!strcmp(token, "a") || !strcmp(token, "alt")) 1406 set_gpio_alt_func(gpio); 1407 else if (!strcmp(token, "i") || !strcmp(token, "io")) 1408 set_gpio_pad(gpio); 1409 else if (!strcmp(token, "p") || !strcmp(token, "pull")) 1410 set_gpio_pull(gpio); 1411 else 1412 printf("gpio list | alt | io | pull\n"); 1413 } 1414 else 1415 list_gpios(gpio); 1416 } 1417 1418 static void handle_i2c(void *i2c) 1419 { 1420 char *token; 1421 1422 if ((token = read_token(NULL)) != NULL) 1423 { 1424 if (!strcmp(token, "l") || !strcmp(token, "list")) 1425 list_i2c_channels(); 1426 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1427 new_i2c_channel(i2c); 1428 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1429 i2c_get(); 1430 else if (!strcmp(token, "s") || !strcmp(token, "scan")) 1431 i2c_scan(); 1432 else 1433 printf("i2c channel | get | list | scan\n"); 1434 } 1435 else 1436 list_i2c_channels(); 1437 } 1438 1439 static void handle_memory(void) 1440 { 1441 char *token; 1442 1443 if ((token = read_token(NULL)) != NULL) 1444 { 1445 if (!strcmp(token, "r") || !strcmp(token, "region")) 1446 new_memory_region(); 1447 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1448 set_memory_region(); 1449 else 1450 printf("memory region | set\n"); 1451 } 1452 else 1453 list_memory_regions(); 1454 } 1455 1456 static void handle_rtc(void *rtc, void *cpm) 1457 { 1458 char *token; 1459 1460 if ((token = read_token(NULL)) != NULL) 1461 { 1462 if (!strcmp(token, "d") || !strcmp(token, "disable")) 1463 rtc_disable(rtc); 1464 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1465 rtc_enable(rtc); 1466 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1467 printf("seconds = %d\n", rtc_get_seconds(rtc)); 1468 else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) 1469 printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); 1470 else if (!strcmp(token, "p") || !strcmp(token, "power-down")) 1471 rtc_power_down(rtc); 1472 else if (!strcmp(token, "r") || !strcmp(token, "reset")) 1473 rtc_reset(rtc, cpm); 1474 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1475 _rtc_set_seconds(rtc, 0); 1476 else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) 1477 _rtc_set_seconds(rtc, 1); 1478 else 1479 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1480 } 1481 else 1482 printf("rtc disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); 1483 } 1484 1485 static void handle_spi(void *spi, void *gpio[]) 1486 { 1487 char *token; 1488 1489 if ((token = read_token(NULL)) != NULL) 1490 { 1491 if (!strcmp(token, "l") || !strcmp(token, "list")) 1492 list_channels(num_spi_channels, spi_channels); 1493 else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) 1494 spi_control(1); 1495 else if (!strcmp(token, "r") || !strcmp(token, "control-release")) 1496 spi_control(0); 1497 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1498 new_spi_channel(spi, gpio); 1499 else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) 1500 new_spi_channel_gpio(gpio); 1501 else if (!strcmp(token, "s") || !strcmp(token, "send")) 1502 spi_send_data(); 1503 else if (!strcmp(token, "S") || !strcmp(token, "send-units")) 1504 spi_send_data_units(); 1505 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1506 spi_transfer_data(); 1507 else 1508 printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); 1509 } 1510 else 1511 list_channels(num_spi_channels, spi_channels); 1512 } 1513 1514 1515 1516 int main(void) 1517 { 1518 l4_addr_t aic_base = 0, aic_base_end = 0; 1519 l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; 1520 l4_addr_t cpm_base = 0, cpm_base_end = 0; 1521 l4_addr_t dma_base = 0, dma_base_end = 0; 1522 l4_addr_t gpio_base = 0, gpio_base_end = 0; 1523 l4_addr_t i2c_base = 0, i2c_base_end = 0; 1524 l4_addr_t rtc_base = 0, rtc_base_end = 0; 1525 l4_addr_t ssi_base = 0, ssi_base_end = 0; 1526 l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; 1527 void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *rtc, *spi; 1528 int result = 0; 1529 unsigned int port; 1530 1531 icucap = l4re_env_get_cap("icu"); 1532 1533 /* Obtain resource details describing I/O memory. */ 1534 1535 printf("Access CPM...\n"); 1536 1537 if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) 1538 return 1; 1539 1540 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 1541 1542 cpm = cpm_init(cpm_base); 1543 1544 printf("Access DMA...\n"); 1545 1546 if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) 1547 return 1; 1548 1549 printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); 1550 1551 dma = dma_init(dma_base, dma_base_end, cpm); 1552 1553 if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) 1554 return 1; 1555 1556 printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); 1557 1558 if (init_dma()) 1559 return 1; 1560 1561 dma_enable(dma); 1562 1563 printf("Access GPIO...\n"); 1564 1565 if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) 1566 return 1; 1567 1568 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 1569 1570 for (port = 0; port < num_gpio_ports; port++) 1571 gpio[port] = gpio_init(gpio_base + port * 0x100, gpio_base + (port + 1) * 0x100, 1572 32, gpio_ports[port].pull_ups, gpio_ports[port].pull_downs); 1573 1574 printf("Access I2C...\n"); 1575 1576 if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) 1577 return 1; 1578 1579 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 1580 1581 i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); 1582 1583 if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) 1584 return 1; 1585 1586 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 1587 1588 printf("Access AIC...\n"); 1589 1590 if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, 1591 &aic_phys_base, &aic_phys_base_end)) < 0) 1592 return 1; 1593 1594 printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); 1595 1596 aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); 1597 1598 printf("Access RTC...\n"); 1599 1600 if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) 1601 return 1; 1602 1603 printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); 1604 1605 rtc = rtc_init(rtc_base, cpm); 1606 1607 printf("Access SSI...\n"); 1608 1609 if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, 1610 &ssi_phys_base, &ssi_phys_base_end)) < 0) 1611 return 1; 1612 1613 printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); 1614 1615 spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); 1616 1617 /* Start the interactive session. */ 1618 1619 printf("aic, cpm, dma, gpio, i2c, rtc, spi\n"); 1620 1621 while (1) 1622 { 1623 char cmdline[256], *token; 1624 1625 printf("> "); 1626 1627 token = fgets(cmdline, 256, stdin); 1628 1629 if (token == NULL) 1630 break; 1631 1632 if ((token = strtok(cmdline, delimiters)) == NULL) 1633 continue; 1634 1635 /* AIC/I2S commands. */ 1636 1637 if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) 1638 handle_aic(aic); 1639 1640 /* CPM commands. */ 1641 1642 else if (!strcmp(token, "c") || !strcmp(token, "cpm")) 1643 handle_cpm(cpm); 1644 1645 /* DMA commands. */ 1646 1647 else if (!strcmp(token, "d") || !strcmp(token, "dma")) 1648 handle_dma(dma); 1649 1650 /* GPIO commands. */ 1651 1652 else if (!strcmp(token, "g") || !strcmp(token, "gpio")) 1653 handle_gpio(gpio); 1654 1655 /* I2C commands. */ 1656 1657 else if (!strcmp(token, "i") || !strcmp(token, "i2c")) 1658 handle_i2c(i2c); 1659 1660 /* Generic memory commands. */ 1661 1662 else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) 1663 handle_memory(); 1664 1665 /* RTC commands. */ 1666 1667 else if (!strcmp(token, "r") || !strcmp(token, "rtc")) 1668 handle_rtc(rtc, cpm); 1669 1670 /* SPI commands. */ 1671 1672 else if (!strcmp(token, "s") || !strcmp(token, "spi")) 1673 handle_spi(spi, gpio); 1674 1675 /* Comments and blank lines. */ 1676 1677 else if (strncmp(token, "#", 1) && strlen(token)) 1678 printf("Command?\n"); 1679 } 1680 1681 printf("End of session.\n"); 1682 return 0; 1683 }