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