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("%-5s %-10s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n" 338 "%-5s %-10s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n", 339 "Id", "Clock", "Type", "Source", "Source Clock", "Source Freq.", 340 "Parameters", "Frequency", "On", 341 sep(5), sep(10), 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("%-5s %-10s %-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_set_seconds(void *rtc, int alarm) 1119 { 1120 unsigned int seconds; 1121 1122 if (!read_number("Seconds", &seconds)) 1123 return; 1124 1125 (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); 1126 } 1127 1128 1129 1130 /* SPI operations. */ 1131 1132 static void new_spi_channel(void *spi, void *gpio[]) 1133 { 1134 unsigned int control_port, control_pin, control_alt_func_input; 1135 int control_alt_func; 1136 void *control_chip; 1137 int num = get_channel_number(num_spi_channels); 1138 void *channel; 1139 uint32_t frequency; 1140 1141 if (num < 0) 1142 return; 1143 1144 channel = get_channel(num_dma_channels, dma_channels, NULL); 1145 1146 if (channel == NULL) 1147 return; 1148 1149 if (!read_number("Frequency", &frequency)) 1150 return; 1151 1152 if (!get_port_and_pin(&control_port, &control_pin)) 1153 control_chip = NULL; 1154 else 1155 control_chip = gpio[control_port]; 1156 1157 if (!read_number("Function", &control_alt_func_input)) 1158 control_alt_func = -1; 1159 else 1160 control_alt_func = control_alt_func_input; 1161 1162 spi_channels[num] = spi_get_channel(spi, num, channel, frequency, 1163 control_chip, control_pin, 1164 control_alt_func); 1165 } 1166 1167 static void new_spi_channel_gpio(void *gpio[]) 1168 { 1169 unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, 1170 enable_pin, control_port, control_pin; 1171 void *control_chip; 1172 int num = get_channel_number(num_spi_channels); 1173 uint32_t frequency; 1174 1175 if (num < 0) 1176 return; 1177 1178 if (!read_number("Frequency", &frequency)) 1179 return; 1180 1181 if (!get_port_and_pin(&clock_port, &clock_pin)) 1182 return; 1183 1184 if (!get_port_and_pin(&data_port, &data_pin)) 1185 return; 1186 1187 if (!get_port_and_pin(&enable_port, &enable_pin)) 1188 return; 1189 1190 if (!get_port_and_pin(&control_port, &control_pin)) 1191 control_chip = NULL; 1192 else 1193 control_chip = gpio[control_port]; 1194 1195 spi_channels[num] = spi_get_channel_gpio(frequency, 1196 gpio[clock_port], clock_pin, 1197 gpio[data_port], data_pin, 1198 gpio[enable_port], enable_pin, 1199 control_chip, control_pin); 1200 } 1201 1202 static void spi_control(int acquire) 1203 { 1204 unsigned int level; 1205 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1206 1207 if (acquire) 1208 { 1209 if (!read_number("Level", &level)) 1210 return; 1211 1212 spi_acquire_control(channel, level); 1213 } 1214 else 1215 spi_release_control(channel); 1216 } 1217 1218 static void spi_send_data(void) 1219 { 1220 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1221 int bytes; 1222 unsigned int byte; 1223 uint8_t buffer[256]; 1224 1225 bytes = 0; 1226 1227 while (read_encoded_number(NULL, "%2x", &byte)) 1228 { 1229 buffer[bytes] = (uint8_t) (byte & 0xff); 1230 bytes++; 1231 } 1232 1233 spi_send(channel, bytes, buffer); 1234 } 1235 1236 static void spi_send_data_units(void) 1237 { 1238 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1239 unsigned int char_size, unit_size, value; 1240 uint8_t buffer[256]; 1241 int byte = 0; 1242 1243 if (!read_number("Unit size", &unit_size)) 1244 return; 1245 1246 if (!read_number("Character size", &char_size)) 1247 return; 1248 1249 /* Read hex digits for bytes. Multiple bytes make up each unit and are read 1250 from most to least significant. Where the unit size exceeds the character 1251 size, the last bit before the character indicates the GPC bit. */ 1252 1253 while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) 1254 buffer[byte++] = value; 1255 1256 /* Explicitly indicate big endian data. */ 1257 1258 spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); 1259 } 1260 1261 static void spi_transfer_data(void) 1262 { 1263 void *channel = get_channel(num_spi_channels, spi_channels, NULL); 1264 struct dma_region *dma_region = NULL, *desc_region; 1265 struct memory_region *memory_region = NULL; 1266 unsigned int char_size, unit_size; 1267 uint32_t count, transferred; 1268 int using_dma; 1269 1270 if (channel == NULL) 1271 return; 1272 1273 if (!read_flag("Using DMA", &using_dma, "d", 1)) 1274 return; 1275 1276 if (using_dma) 1277 dma_region = _get_dma_region(); 1278 else 1279 memory_region = _get_memory_region(); 1280 1281 if ((dma_region == NULL) && (memory_region == NULL)) 1282 return; 1283 1284 if (!read_number("Transfer size", &count)) 1285 return; 1286 1287 if (!read_number("Unit size", &unit_size)) 1288 return; 1289 1290 if (!read_number("Character size", &char_size)) 1291 return; 1292 1293 if (using_dma) 1294 { 1295 desc_region = _get_dma_region(); 1296 1297 transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, 1298 unit_size, char_size, 1299 desc_region != NULL ? desc_region->vaddr : 0, 1300 desc_region != NULL ? desc_region->paddr : 0); 1301 } 1302 else 1303 transferred = spi_transfer(channel, memory_region->addr, 0, count, 1304 unit_size, char_size, 0, 0); 1305 1306 printf("Transferred: %d\n", transferred); 1307 } 1308 1309 1310 1311 /* Command processing. */ 1312 1313 static void handle_aic(void *aic) 1314 { 1315 char *token; 1316 1317 if ((token = read_token(NULL)) != NULL) 1318 { 1319 if (!strcmp(token, "l") || !strcmp(token, "list")) 1320 list_channels(num_aic_channels, aic_channels); 1321 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1322 new_aic_channel(aic); 1323 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1324 aic_transfer_data(); 1325 else 1326 printf("aic channel | list | transfer\n"); 1327 } 1328 else 1329 list_channels(num_aic_channels, aic_channels); 1330 } 1331 1332 static void handle_cpm(void *cpm) 1333 { 1334 char *token; 1335 1336 if ((token = read_token(NULL)) != NULL) 1337 { 1338 if (!strcmp(token, "l") || !strcmp(token, "list")) 1339 list_clocks(cpm); 1340 else if (!strcmp(token, "f") || !strcmp(token, "frequency")) 1341 set_frequency(cpm); 1342 else if (!strcmp(token, "p") || !strcmp(token, "parameters")) 1343 set_parameters(cpm); 1344 else if (!strcmp(token, "s") || !strcmp(token, "source")) 1345 set_source(cpm); 1346 else if (!strcmp(token, "start")) 1347 start_clock(cpm); 1348 else if (!strcmp(token, "stop")) 1349 stop_clock(cpm); 1350 else 1351 printf("cpm list | frequency | parameters | source | start | stop\n"); 1352 } 1353 else 1354 list_clocks(cpm); 1355 } 1356 1357 static void handle_dma(void *dma) 1358 { 1359 char *token; 1360 1361 if ((token = read_token(NULL)) != NULL) 1362 { 1363 if (!strcmp(token, "l") || !strcmp(token, "list")) 1364 list_channels(num_dma_channels, dma_channels); 1365 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1366 new_dma_channel(dma); 1367 else if (!strcmp(token, "r") || !strcmp(token, "region")) 1368 new_dma_region(); 1369 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1370 set_dma_region(); 1371 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1372 dma_transfer_data(); 1373 else 1374 printf("dma channel | list | region | set | transfer\n"); 1375 } 1376 else 1377 list_channels(num_dma_channels, dma_channels); 1378 } 1379 1380 static void handle_gpio(void *gpio[]) 1381 { 1382 char *token; 1383 1384 if ((token = read_token(NULL)) != NULL) 1385 { 1386 if (!strcmp(token, "l") || !strcmp(token, "list")) 1387 list_gpios(gpio); 1388 else if (!strcmp(token, "a") || !strcmp(token, "alt")) 1389 set_gpio_alt_func(gpio); 1390 else if (!strcmp(token, "i") || !strcmp(token, "io")) 1391 set_gpio_pad(gpio); 1392 else if (!strcmp(token, "p") || !strcmp(token, "pull")) 1393 set_gpio_pull(gpio); 1394 else 1395 printf("gpio list | alt | io | pull\n"); 1396 } 1397 else 1398 list_gpios(gpio); 1399 } 1400 1401 static void handle_i2c(void *i2c) 1402 { 1403 char *token; 1404 1405 if ((token = read_token(NULL)) != NULL) 1406 { 1407 if (!strcmp(token, "l") || !strcmp(token, "list")) 1408 list_i2c_channels(); 1409 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1410 new_i2c_channel(i2c); 1411 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1412 i2c_get(); 1413 else if (!strcmp(token, "s") || !strcmp(token, "scan")) 1414 i2c_scan(); 1415 else 1416 printf("i2c channel | get | list | scan\n"); 1417 } 1418 else 1419 list_i2c_channels(); 1420 } 1421 1422 static void handle_memory(void) 1423 { 1424 char *token; 1425 1426 if ((token = read_token(NULL)) != NULL) 1427 { 1428 if (!strcmp(token, "r") || !strcmp(token, "region")) 1429 new_memory_region(); 1430 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1431 set_memory_region(); 1432 else 1433 printf("memory region | set\n"); 1434 } 1435 else 1436 list_memory_regions(); 1437 } 1438 1439 static void handle_rtc(void *rtc) 1440 { 1441 char *token; 1442 1443 if ((token = read_token(NULL)) != NULL) 1444 { 1445 if (!strcmp(token, "d") || !strcmp(token, "disable")) 1446 rtc_disable(rtc); 1447 else if (!strcmp(token, "e") || !strcmp(token, "enable")) 1448 rtc_enable(rtc); 1449 else if (!strcmp(token, "g") || !strcmp(token, "get")) 1450 printf("seconds = %d\n", rtc_get_seconds(rtc)); 1451 else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) 1452 printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); 1453 else if (!strcmp(token, "p") || !strcmp(token, "power-down")) 1454 rtc_power_down(rtc); 1455 else if (!strcmp(token, "s") || !strcmp(token, "set")) 1456 _rtc_set_seconds(rtc, 0); 1457 else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) 1458 _rtc_set_seconds(rtc, 1); 1459 else 1460 printf("rtc disable | enable | get | get-alarm | power-down | set | set-alarm\n"); 1461 } 1462 else 1463 printf("rtc disable | enable | get | get-alarm | power-down | set | set-alarm\n"); 1464 } 1465 1466 static void handle_spi(void *spi, void *gpio[]) 1467 { 1468 char *token; 1469 1470 if ((token = read_token(NULL)) != NULL) 1471 { 1472 if (!strcmp(token, "l") || !strcmp(token, "list")) 1473 list_channels(num_spi_channels, spi_channels); 1474 else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) 1475 spi_control(1); 1476 else if (!strcmp(token, "r") || !strcmp(token, "control-release")) 1477 spi_control(0); 1478 else if (!strcmp(token, "c") || !strcmp(token, "channel")) 1479 new_spi_channel(spi, gpio); 1480 else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) 1481 new_spi_channel_gpio(gpio); 1482 else if (!strcmp(token, "s") || !strcmp(token, "send")) 1483 spi_send_data(); 1484 else if (!strcmp(token, "S") || !strcmp(token, "send-units")) 1485 spi_send_data_units(); 1486 else if (!strcmp(token, "t") || !strcmp(token, "transfer")) 1487 spi_transfer_data(); 1488 else 1489 printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); 1490 } 1491 else 1492 list_channels(num_spi_channels, spi_channels); 1493 } 1494 1495 1496 1497 int main(void) 1498 { 1499 l4_addr_t aic_base = 0, aic_base_end = 0; 1500 l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; 1501 l4_addr_t cpm_base = 0, cpm_base_end = 0; 1502 l4_addr_t dma_base = 0, dma_base_end = 0; 1503 l4_addr_t gpio_base = 0, gpio_base_end = 0; 1504 l4_addr_t i2c_base = 0, i2c_base_end = 0; 1505 l4_addr_t rtc_base = 0, rtc_base_end = 0; 1506 l4_addr_t ssi_base = 0, ssi_base_end = 0; 1507 l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; 1508 void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *rtc, *spi; 1509 int result = 0; 1510 unsigned int port; 1511 1512 icucap = l4re_env_get_cap("icu"); 1513 1514 /* Obtain resource details describing I/O memory. */ 1515 1516 printf("Access CPM...\n"); 1517 1518 if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) 1519 return 1; 1520 1521 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 1522 1523 cpm = cpm_init(cpm_base); 1524 1525 printf("Access DMA...\n"); 1526 1527 if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) 1528 return 1; 1529 1530 printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); 1531 1532 dma = dma_init(dma_base, dma_base_end, cpm); 1533 1534 if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) 1535 return 1; 1536 1537 printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); 1538 1539 if (init_dma()) 1540 return 1; 1541 1542 dma_enable(dma); 1543 1544 printf("Access GPIO...\n"); 1545 1546 if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) 1547 return 1; 1548 1549 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 1550 1551 for (port = 0; port < num_gpio_ports; port++) 1552 gpio[port] = gpio_init(gpio_base + port * 0x100, gpio_base + (port + 1) * 0x100, 1553 32, gpio_ports[port].pull_ups, gpio_ports[port].pull_downs); 1554 1555 printf("Access I2C...\n"); 1556 1557 if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) 1558 return 1; 1559 1560 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 1561 1562 i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); 1563 1564 if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) 1565 return 1; 1566 1567 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 1568 1569 printf("Access AIC...\n"); 1570 1571 if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, 1572 &aic_phys_base, &aic_phys_base_end)) < 0) 1573 return 1; 1574 1575 printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); 1576 1577 aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); 1578 1579 printf("Access RTC...\n"); 1580 1581 if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) 1582 return 1; 1583 1584 printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); 1585 1586 rtc = rtc_init(rtc_base); 1587 1588 printf("Access SSI...\n"); 1589 1590 if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, 1591 &ssi_phys_base, &ssi_phys_base_end)) < 0) 1592 return 1; 1593 1594 printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); 1595 1596 spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); 1597 1598 /* Start the interactive session. */ 1599 1600 printf("aic, cpm, dma, gpio, i2c, rtc, spi\n"); 1601 1602 while (1) 1603 { 1604 char cmdline[256], *token; 1605 1606 printf("> "); 1607 1608 token = fgets(cmdline, 256, stdin); 1609 1610 if (token == NULL) 1611 break; 1612 1613 if ((token = strtok(cmdline, delimiters)) == NULL) 1614 continue; 1615 1616 /* AIC/I2S commands. */ 1617 1618 if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) 1619 handle_aic(aic); 1620 1621 /* CPM commands. */ 1622 1623 else if (!strcmp(token, "c") || !strcmp(token, "cpm")) 1624 handle_cpm(cpm); 1625 1626 /* DMA commands. */ 1627 1628 else if (!strcmp(token, "d") || !strcmp(token, "dma")) 1629 handle_dma(dma); 1630 1631 /* GPIO commands. */ 1632 1633 else if (!strcmp(token, "g") || !strcmp(token, "gpio")) 1634 handle_gpio(gpio); 1635 1636 /* I2C commands. */ 1637 1638 else if (!strcmp(token, "i") || !strcmp(token, "i2c")) 1639 handle_i2c(i2c); 1640 1641 /* Generic memory commands. */ 1642 1643 else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) 1644 handle_memory(); 1645 1646 /* RTC commands. */ 1647 1648 else if (!strcmp(token, "r") || !strcmp(token, "rtc")) 1649 handle_rtc(rtc); 1650 1651 /* SPI commands. */ 1652 1653 else if (!strcmp(token, "s") || !strcmp(token, "spi")) 1654 handle_spi(spi, gpio); 1655 1656 /* Comments and blank lines. */ 1657 1658 else if (strncmp(token, "#", 1) && strlen(token)) 1659 printf("Command?\n"); 1660 } 1661 1662 printf("End of session.\n"); 1663 return 0; 1664 }