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,
28 #if defined(CONFIG_API)
33 #include <linux/types.h>
34 #include <api_public.h>
36 #include "api_private.h"
41 /* U-Boot routines needed */
42 extern int do_reset (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[]);
43 extern uchar (*env_get_char
)(int);
44 extern uchar
*env_get_addr(int);
46 /*****************************************************************************
48 * This is the API core.
50 * API_ functions are part of U-Boot code and constitute the lowest level
53 * - they know what values they need as arguments
54 * - their direct return value pertains to the API_ "shell" itself (0 on
55 * success, some error code otherwise)
56 * - if the call returns a value it is buried within arguments
58 ****************************************************************************/
61 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
63 #define debugf(fmt, args...)
66 typedef int (*cfp_t
)(va_list argp
);
73 * int API_getc(int *c)
75 static int API_getc(va_list ap
)
79 if ((c
= (int *)va_arg(ap
, u_int32_t
)) == NULL
)
89 * int API_tstc(int *c)
91 static int API_tstc(va_list ap
)
95 if ((t
= (int *)va_arg(ap
, u_int32_t
)) == NULL
)
105 * int API_putc(char *ch)
107 static int API_putc(va_list ap
)
111 if ((c
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
121 * int API_puts(char **s)
123 static int API_puts(va_list ap
)
127 if ((s
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
137 * int API_reset(void)
139 static int API_reset(va_list ap
)
141 do_reset(NULL
, 0, 0, NULL
);
150 * int API_get_sys_info(struct sys_info *si)
152 * fill out the sys_info struct containing selected parameters about the
155 static int API_get_sys_info(va_list ap
)
159 si
= (struct sys_info
*)va_arg(ap
, u_int32_t
);
163 return (platform_sys_info(si
)) ? 0 : API_ENODEV
;
169 * int API_udelay(unsigned long *udelay)
171 static int API_udelay(va_list ap
)
175 if ((d
= (unsigned long *)va_arg(ap
, u_int32_t
)) == NULL
)
185 * int API_get_timer(unsigned long *current, unsigned long *base)
187 static int API_get_timer(va_list ap
)
189 unsigned long *base
, *cur
;
191 cur
= (unsigned long *)va_arg(ap
, u_int32_t
);
195 base
= (unsigned long *)va_arg(ap
, u_int32_t
);
199 *cur
= get_timer(*base
);
204 /*****************************************************************************
208 * int API_dev_enum(struct device_info *)
211 * cookies uniqely identify the previously enumerated device instance and
212 * provide a hint for what to inspect in current enum iteration:
214 * - net: ð_device struct address from list pointed to by eth_devices
216 * - storage: block_dev_desc_t struct address from &ide_dev_desc[n],
217 * &scsi_dev_desc[n] and similar tables
219 ****************************************************************************/
221 static int API_dev_enum(va_list ap
)
223 struct device_info
*di
;
225 /* arg is ptr to the device_info struct we are going to fill out */
226 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
230 if (di
->cookie
== NULL
) {
231 /* start over - clean up enumeration */
232 dev_enum_reset(); /* XXX shouldn't the name contain 'stor'? */
233 debugf("RESTART ENUM\n");
235 /* net device enumeration first */
236 if (dev_enum_net(di
))
241 * The hidden assumption is there can only be one active network
242 * device and it is identified upon enumeration (re)start, so there's
243 * no point in trying to find network devices in other cases than the
244 * (re)start and hence the 'next' device can only be storage
246 if (!dev_enum_storage(di
))
247 /* make sure we mark there are no more devices */
254 static int API_dev_open(va_list ap
)
256 struct device_info
*di
;
259 /* arg is ptr to the device_info struct */
260 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
264 /* Allow only one consumer of the device at a time */
265 if (di
->state
== DEV_STA_OPEN
)
268 if (di
->cookie
== NULL
)
271 if (di
->type
& DEV_TYP_STOR
)
272 err
= dev_open_stor(di
->cookie
);
274 else if (di
->type
& DEV_TYP_NET
)
275 err
= dev_open_net(di
->cookie
);
280 di
->state
= DEV_STA_OPEN
;
286 static int API_dev_close(va_list ap
)
288 struct device_info
*di
;
291 /* arg is ptr to the device_info struct */
292 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
296 if (di
->state
== DEV_STA_CLOSED
)
299 if (di
->cookie
== NULL
)
302 if (di
->type
& DEV_TYP_STOR
)
303 err
= dev_close_stor(di
->cookie
);
305 else if (di
->type
& DEV_TYP_NET
)
306 err
= dev_close_net(di
->cookie
);
309 * In case of unknown device we cannot change its state, so
310 * only return error code
315 di
->state
= DEV_STA_CLOSED
;
322 * Notice: this is for sending network packets only, as U-Boot does not
323 * support writing to storage at the moment (12.2007)
328 * struct device_info *di,
333 * buf: ptr to buffer from where to get the data to send
335 * len: length of packet to be sent (in bytes)
338 static int API_dev_write(va_list ap
)
340 struct device_info
*di
;
345 /* 1. arg is ptr to the device_info struct */
346 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
350 /* XXX should we check if device is open? i.e. the ->state ? */
352 if (di
->cookie
== NULL
)
355 /* 2. arg is ptr to buffer from where to get data to write */
356 buf
= (void *)va_arg(ap
, u_int32_t
);
360 /* 3. arg is length of buffer */
361 len
= (int *)va_arg(ap
, u_int32_t
);
367 if (di
->type
& DEV_TYP_STOR
)
369 * write to storage is currently not supported by U-Boot:
370 * no storage device implements block_write() method
374 else if (di
->type
& DEV_TYP_NET
)
375 err
= dev_write_net(di
->cookie
, buf
, *len
);
387 * struct device_info *di,
390 * unsigned long *start
394 * buf: ptr to buffer where to put the read data
396 * len: ptr to length to be read
397 * - network: len of packet to read (in bytes)
398 * - storage: # of blocks to read (can vary in size depending on define)
400 * start: ptr to start block (only used for storage devices, ignored for
403 * act_len: ptr to where to put the len actually read
405 static int API_dev_read(va_list ap
)
407 struct device_info
*di
;
409 lbasize_t
*len_stor
, *act_len_stor
;
411 int *len_net
, *act_len_net
;
413 /* 1. arg is ptr to the device_info struct */
414 di
= (struct device_info
*)va_arg(ap
, u_int32_t
);
418 /* XXX should we check if device is open? i.e. the ->state ? */
420 if (di
->cookie
== NULL
)
423 /* 2. arg is ptr to buffer from where to put the read data */
424 buf
= (void *)va_arg(ap
, u_int32_t
);
428 if (di
->type
& DEV_TYP_STOR
) {
429 /* 3. arg - ptr to var with # of blocks to read */
430 len_stor
= (lbasize_t
*)va_arg(ap
, u_int32_t
);
436 /* 4. arg - ptr to var with start block */
437 start
= (lbastart_t
*)va_arg(ap
, u_int32_t
);
439 /* 5. arg - ptr to var where to put the len actually read */
440 act_len_stor
= (lbasize_t
*)va_arg(ap
, u_int32_t
);
444 *act_len_stor
= dev_read_stor(di
->cookie
, buf
, *len_stor
, *start
);
446 } else if (di
->type
& DEV_TYP_NET
) {
448 /* 3. arg points to the var with length of packet to read */
449 len_net
= (int *)va_arg(ap
, u_int32_t
);
455 /* 4. - ptr to var where to put the len actually read */
456 act_len_net
= (int *)va_arg(ap
, u_int32_t
);
460 *act_len_net
= dev_read_net(di
->cookie
, buf
, *len_net
);
472 * int API_env_get(const char *name, char **value)
474 * name: ptr to name of env var
476 static int API_env_get(va_list ap
)
480 if ((name
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
482 if ((value
= (char **)va_arg(ap
, u_int32_t
)) == NULL
)
485 *value
= getenv(name
);
493 * int API_env_set(const char *name, const char *value)
495 * name: ptr to name of env var
497 * value: ptr to value to be set
499 static int API_env_set(va_list ap
)
503 if ((name
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
505 if ((value
= (char *)va_arg(ap
, u_int32_t
)) == NULL
)
516 * int API_env_enum(const char *last, char **next)
518 * last: ptr to name of env var found in last iteration
520 static int API_env_enum(va_list ap
)
525 last
= (char *)va_arg(ap
, u_int32_t
);
527 if ((next
= (char **)va_arg(ap
, u_int32_t
)) == NULL
)
532 *next
= ((char *)env_get_addr(0));
536 for (i
= 0; env_get_char(i
) != '\0'; i
= n
+ 1) {
537 for (n
= i
; env_get_char(n
) != '\0'; ++n
) {
538 if (n
>= CFG_ENV_SIZE
) {
539 /* XXX shouldn't we set *next = NULL?? */
544 if (envmatch((uchar
*)last
, i
) < 0)
547 /* try to get next name */
549 if (env_get_char(i
) == '\0') {
555 *next
= ((char *)env_get_addr(i
));
563 static cfp_t calls_table
[API_MAXCALL
] = { NULL
, };
566 * The main syscall entry point - this is not reentrant, only one call is
567 * serviced until finished.
569 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
571 * call: syscall number
573 * retval: points to the return value placeholder, this is the place the
574 * syscall puts its return value, if NULL the caller does not
575 * expect a return value
577 * ... syscall arguments (variable number)
579 * returns: 0 if the call not found, 1 if serviced
581 int syscall(int call
, int *retval
, ...)
586 if (call
< 0 || call
>= calls_no
|| calls_table
[call
] == NULL
) {
587 debugf("invalid call #%d\n", call
);
591 if (calls_table
[call
] == NULL
) {
592 debugf("syscall #%d does not have a handler\n", call
);
596 va_start(ap
, retval
);
597 rv
= calls_table
[call
](ap
);
606 struct api_signature
*sig
= NULL
;
608 /* TODO put this into linker set one day... */
609 calls_table
[API_RSVD
] = NULL
;
610 calls_table
[API_GETC
] = &API_getc
;
611 calls_table
[API_PUTC
] = &API_putc
;
612 calls_table
[API_TSTC
] = &API_tstc
;
613 calls_table
[API_PUTS
] = &API_puts
;
614 calls_table
[API_RESET
] = &API_reset
;
615 calls_table
[API_GET_SYS_INFO
] = &API_get_sys_info
;
616 calls_table
[API_UDELAY
] = &API_udelay
;
617 calls_table
[API_GET_TIMER
] = &API_get_timer
;
618 calls_table
[API_DEV_ENUM
] = &API_dev_enum
;
619 calls_table
[API_DEV_OPEN
] = &API_dev_open
;
620 calls_table
[API_DEV_CLOSE
] = &API_dev_close
;
621 calls_table
[API_DEV_READ
] = &API_dev_read
;
622 calls_table
[API_DEV_WRITE
] = &API_dev_write
;
623 calls_table
[API_ENV_GET
] = &API_env_get
;
624 calls_table
[API_ENV_SET
] = &API_env_set
;
625 calls_table
[API_ENV_ENUM
] = &API_env_enum
;
626 calls_no
= API_MAXCALL
;
628 debugf("API initialized with %d calls\n", calls_no
);
633 * Produce the signature so the API consumers can find it
635 sig
= malloc(sizeof(struct api_signature
));
637 printf("API: could not allocate memory for the signature!\n");
641 debugf("API sig @ 0x%08x\n", sig
);
642 memcpy(sig
->magic
, API_SIG_MAGIC
, 8);
643 sig
->version
= API_SIG_VERSION
;
644 sig
->syscall
= &syscall
;
646 sig
->checksum
= crc32(0, (unsigned char *)sig
,
647 sizeof(struct api_signature
));
648 debugf("syscall entry: 0x%08x\n", sig
->syscall
);
651 void platform_set_mr(struct sys_info
*si
, unsigned long start
, unsigned long size
,
656 if (!si
->mr
|| !size
|| (flags
== 0))
660 for (i
= 0; i
< si
->mr_no
; i
++)
661 if (si
->mr
[i
].flags
== 0) {
662 /* insert new mem region */
663 si
->mr
[i
].start
= start
;
664 si
->mr
[i
].size
= size
;
665 si
->mr
[i
].flags
= flags
;
670 #endif /* CONFIG_API */