2 * (C) Copyright 2007 Semihalf
4 * Written by: Rafal Jaworowski <raj@semihalf.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <environment.h>
31 #include <linux/types.h>
32 #include <api_public.h>
34 #include "api_private.h"
39 /*****************************************************************************
41 * This is the API core.
43 * API_ functions are part of U-Boot code and constitute the lowest level
46 * - they know what values they need as arguments
47 * - their direct return value pertains to the API_ "shell" itself (0 on
48 * success, some error code otherwise)
49 * - if the call returns a value it is buried within arguments
51 ****************************************************************************/
54 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
56 #define debugf(fmt, args...)
59 typedef int (*cfp_t)(va_list argp);
66 * int API_getc(int *c)
68 static int API_getc(va_list ap)
72 if ((c = (int *)va_arg(ap, u_int32_t)) == NULL)
82 * int API_tstc(int *c)
84 static int API_tstc(va_list ap)
88 if ((t = (int *)va_arg(ap, u_int32_t)) == NULL)
98 * int API_putc(char *ch)
100 static int API_putc(va_list ap)
104 if ((c = (char *)va_arg(ap, u_int32_t)) == NULL)
114 * int API_puts(char **s)
116 static int API_puts(va_list ap)
120 if ((s = (char *)va_arg(ap, u_int32_t)) == NULL)
130 * int API_reset(void)
132 static int API_reset(va_list ap)
134 do_reset(NULL, 0, 0, NULL);
143 * int API_get_sys_info(struct sys_info *si)
145 * fill out the sys_info struct containing selected parameters about the
148 static int API_get_sys_info(va_list ap)
152 si = (struct sys_info *)va_arg(ap, u_int32_t);
156 return (platform_sys_info(si)) ? 0 : API_ENODEV;
162 * int API_udelay(unsigned long *udelay)
164 static int API_udelay(va_list ap)
168 if ((d = (unsigned long *)va_arg(ap, u_int32_t)) == NULL)
178 * int API_get_timer(unsigned long *current, unsigned long *base)
180 static int API_get_timer(va_list ap)
182 unsigned long *base, *cur;
184 cur = (unsigned long *)va_arg(ap, u_int32_t);
188 base = (unsigned long *)va_arg(ap, u_int32_t);
192 *cur = get_timer(*base);
197 /*****************************************************************************
201 * int API_dev_enum(struct device_info *)
204 * cookies uniqely identify the previously enumerated device instance and
205 * provide a hint for what to inspect in current enum iteration:
207 * - net: ð_device struct address from list pointed to by eth_devices
209 * - storage: block_dev_desc_t struct address from &ide_dev_desc[n],
210 * &scsi_dev_desc[n] and similar tables
212 ****************************************************************************/
214 static int API_dev_enum(va_list ap)
216 struct device_info *di;
218 /* arg is ptr to the device_info struct we are going to fill out */
219 di = (struct device_info *)va_arg(ap, u_int32_t);
223 if (di->cookie == NULL) {
224 /* start over - clean up enumeration */
225 dev_enum_reset(); /* XXX shouldn't the name contain 'stor'? */
226 debugf("RESTART ENUM\n");
228 /* net device enumeration first */
229 if (dev_enum_net(di))
234 * The hidden assumption is there can only be one active network
235 * device and it is identified upon enumeration (re)start, so there's
236 * no point in trying to find network devices in other cases than the
237 * (re)start and hence the 'next' device can only be storage
239 if (!dev_enum_storage(di))
240 /* make sure we mark there are no more devices */
247 static int API_dev_open(va_list ap)
249 struct device_info *di;
252 /* arg is ptr to the device_info struct */
253 di = (struct device_info *)va_arg(ap, u_int32_t);
257 /* Allow only one consumer of the device at a time */
258 if (di->state == DEV_STA_OPEN)
261 if (di->cookie == NULL)
264 if (di->type & DEV_TYP_STOR)
265 err = dev_open_stor(di->cookie);
267 else if (di->type & DEV_TYP_NET)
268 err = dev_open_net(di->cookie);
273 di->state = DEV_STA_OPEN;
279 static int API_dev_close(va_list ap)
281 struct device_info *di;
284 /* arg is ptr to the device_info struct */
285 di = (struct device_info *)va_arg(ap, u_int32_t);
289 if (di->state == DEV_STA_CLOSED)
292 if (di->cookie == NULL)
295 if (di->type & DEV_TYP_STOR)
296 err = dev_close_stor(di->cookie);
298 else if (di->type & DEV_TYP_NET)
299 err = dev_close_net(di->cookie);
302 * In case of unknown device we cannot change its state, so
303 * only return error code
308 di->state = DEV_STA_CLOSED;
315 * Notice: this is for sending network packets only, as U-Boot does not
316 * support writing to storage at the moment (12.2007)
321 * struct device_info *di,
326 * buf: ptr to buffer from where to get the data to send
328 * len: length of packet to be sent (in bytes)
331 static int API_dev_write(va_list ap)
333 struct device_info *di;
338 /* 1. arg is ptr to the device_info struct */
339 di = (struct device_info *)va_arg(ap, u_int32_t);
343 /* XXX should we check if device is open? i.e. the ->state ? */
345 if (di->cookie == NULL)
348 /* 2. arg is ptr to buffer from where to get data to write */
349 buf = (void *)va_arg(ap, u_int32_t);
353 /* 3. arg is length of buffer */
354 len = (int *)va_arg(ap, u_int32_t);
360 if (di->type & DEV_TYP_STOR)
362 * write to storage is currently not supported by U-Boot:
363 * no storage device implements block_write() method
367 else if (di->type & DEV_TYP_NET)
368 err = dev_write_net(di->cookie, buf, *len);
380 * struct device_info *di,
383 * unsigned long *start
387 * buf: ptr to buffer where to put the read data
389 * len: ptr to length to be read
390 * - network: len of packet to read (in bytes)
391 * - storage: # of blocks to read (can vary in size depending on define)
393 * start: ptr to start block (only used for storage devices, ignored for
396 * act_len: ptr to where to put the len actually read
398 static int API_dev_read(va_list ap)
400 struct device_info *di;
402 lbasize_t *len_stor, *act_len_stor;
404 int *len_net, *act_len_net;
406 /* 1. arg is ptr to the device_info struct */
407 di = (struct device_info *)va_arg(ap, u_int32_t);
411 /* XXX should we check if device is open? i.e. the ->state ? */
413 if (di->cookie == NULL)
416 /* 2. arg is ptr to buffer from where to put the read data */
417 buf = (void *)va_arg(ap, u_int32_t);
421 if (di->type & DEV_TYP_STOR) {
422 /* 3. arg - ptr to var with # of blocks to read */
423 len_stor = (lbasize_t *)va_arg(ap, u_int32_t);
429 /* 4. arg - ptr to var with start block */
430 start = (lbastart_t *)va_arg(ap, u_int32_t);
432 /* 5. arg - ptr to var where to put the len actually read */
433 act_len_stor = (lbasize_t *)va_arg(ap, u_int32_t);
437 *act_len_stor = dev_read_stor(di->cookie, buf, *len_stor, *start);
439 } else if (di->type & DEV_TYP_NET) {
441 /* 3. arg points to the var with length of packet to read */
442 len_net = (int *)va_arg(ap, u_int32_t);
448 /* 4. - ptr to var where to put the len actually read */
449 act_len_net = (int *)va_arg(ap, u_int32_t);
453 *act_len_net = dev_read_net(di->cookie, buf, *len_net);
465 * int API_env_get(const char *name, char **value)
467 * name: ptr to name of env var
469 static int API_env_get(va_list ap)
473 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
475 if ((value = (char **)va_arg(ap, u_int32_t)) == NULL)
478 *value = getenv(name);
486 * int API_env_set(const char *name, const char *value)
488 * name: ptr to name of env var
490 * value: ptr to value to be set
492 static int API_env_set(va_list ap)
496 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
498 if ((value = (char *)va_arg(ap, u_int32_t)) == NULL)
509 * int API_env_enum(const char *last, char **next)
511 * last: ptr to name of env var found in last iteration
513 static int API_env_enum(va_list ap)
518 last = (char *)va_arg(ap, u_int32_t);
520 if ((next = (char **)va_arg(ap, u_int32_t)) == NULL)
525 *next = ((char *)env_get_addr(0));
529 for (i = 0; env_get_char(i) != '\0'; i = n + 1) {
530 for (n = i; env_get_char(n) != '\0'; ++n) {
531 if (n >= CONFIG_ENV_SIZE) {
532 /* XXX shouldn't we set *next = NULL?? */
537 if (envmatch((uchar *)last, i) < 0)
540 /* try to get next name */
542 if (env_get_char(i) == '\0') {
548 *next = ((char *)env_get_addr(i));
559 * int API_display_get_info(int type, struct display_info *di)
561 static int API_display_get_info(va_list ap)
564 struct display_info *di;
566 type = va_arg(ap, int);
567 di = va_arg(ap, struct display_info *);
569 return display_get_info(type, di);
575 * int API_display_draw_bitmap(ulong bitmap, int x, int y)
577 static int API_display_draw_bitmap(va_list ap)
582 bitmap = va_arg(ap, ulong);
586 return display_draw_bitmap(bitmap, x, y);
592 * void API_display_clear(void)
594 static int API_display_clear(va_list ap)
600 static cfp_t calls_table[API_MAXCALL] = { NULL, };
603 * The main syscall entry point - this is not reentrant, only one call is
604 * serviced until finished.
606 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
608 * call: syscall number
610 * retval: points to the return value placeholder, this is the place the
611 * syscall puts its return value, if NULL the caller does not
612 * expect a return value
614 * ... syscall arguments (variable number)
616 * returns: 0 if the call not found, 1 if serviced
618 int syscall(int call, int *retval, ...)
623 if (call < 0 || call >= calls_no) {
624 debugf("invalid call #%d\n", call);
628 if (calls_table[call] == NULL) {
629 debugf("syscall #%d does not have a handler\n", call);
633 va_start(ap, retval);
634 rv = calls_table[call](ap);
643 struct api_signature *sig = NULL;
645 /* TODO put this into linker set one day... */
646 calls_table[API_RSVD] = NULL;
647 calls_table[API_GETC] = &API_getc;
648 calls_table[API_PUTC] = &API_putc;
649 calls_table[API_TSTC] = &API_tstc;
650 calls_table[API_PUTS] = &API_puts;
651 calls_table[API_RESET] = &API_reset;
652 calls_table[API_GET_SYS_INFO] = &API_get_sys_info;
653 calls_table[API_UDELAY] = &API_udelay;
654 calls_table[API_GET_TIMER] = &API_get_timer;
655 calls_table[API_DEV_ENUM] = &API_dev_enum;
656 calls_table[API_DEV_OPEN] = &API_dev_open;
657 calls_table[API_DEV_CLOSE] = &API_dev_close;
658 calls_table[API_DEV_READ] = &API_dev_read;
659 calls_table[API_DEV_WRITE] = &API_dev_write;
660 calls_table[API_ENV_GET] = &API_env_get;
661 calls_table[API_ENV_SET] = &API_env_set;
662 calls_table[API_ENV_ENUM] = &API_env_enum;
663 calls_table[API_DISPLAY_GET_INFO] = &API_display_get_info;
664 calls_table[API_DISPLAY_DRAW_BITMAP] = &API_display_draw_bitmap;
665 calls_table[API_DISPLAY_CLEAR] = &API_display_clear;
666 calls_no = API_MAXCALL;
668 debugf("API initialized with %d calls\n", calls_no);
673 * Produce the signature so the API consumers can find it
675 sig = malloc(sizeof(struct api_signature));
677 printf("API: could not allocate memory for the signature!\n");
681 debugf("API sig @ 0x%08x\n", sig);
682 memcpy(sig->magic, API_SIG_MAGIC, 8);
683 sig->version = API_SIG_VERSION;
684 sig->syscall = &syscall;
686 sig->checksum = crc32(0, (unsigned char *)sig,
687 sizeof(struct api_signature));
688 debugf("syscall entry: 0x%08x\n", sig->syscall);
691 void platform_set_mr(struct sys_info *si, unsigned long start, unsigned long size,
696 if (!si->mr || !size || (flags == 0))
700 for (i = 0; i < si->mr_no; i++)
701 if (si->mr[i].flags == 0) {
702 /* insert new mem region */
703 si->mr[i].start = start;
704 si->mr[i].size = size;
705 si->mr[i].flags = flags;