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