Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016 Intel Corporation. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <string.h> |
| 9 | #include <device.h> |
| 10 | #include <misc/util.h> |
| 11 | #include <atomic.h> |
| 12 | |
| 13 | extern struct device __device_init_start[]; |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 14 | extern struct device __device_PRE_KERNEL_1_start[]; |
| 15 | extern struct device __device_PRE_KERNEL_2_start[]; |
| 16 | extern struct device __device_POST_KERNEL_start[]; |
| 17 | extern struct device __device_APPLICATION_start[]; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 18 | extern struct device __device_init_end[]; |
| 19 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 20 | |
| 21 | #ifdef CONFIG_DEVICE_POWER_MANAGEMENT |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 22 | extern u32_t __device_busy_start[]; |
| 23 | extern u32_t __device_busy_end[]; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 24 | #define DEVICE_BUSY_SIZE (__device_busy_end - __device_busy_start) |
| 25 | #endif |
| 26 | |
| 27 | /** |
| 28 | * @brief Execute all the device initialization functions at a given level |
| 29 | * |
| 30 | * @details Invokes the initialization routine for each device object |
| 31 | * created by the DEVICE_INIT() macro using the specified level. |
| 32 | * The linker script places the device objects in memory in the order |
| 33 | * they need to be invoked, with symbols indicating where one level leaves |
| 34 | * off and the next one begins. |
| 35 | * |
| 36 | * @param level init level to run. |
| 37 | */ |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 38 | void _sys_device_do_config_level(s32_t level) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 39 | { |
| 40 | struct device *info; |
Flavio Ceolin | ac14685 | 2018-11-01 17:42:07 -0700 | [diff] [blame] | 41 | static struct device *config_levels[] = { |
| 42 | __device_PRE_KERNEL_1_start, |
| 43 | __device_PRE_KERNEL_2_start, |
| 44 | __device_POST_KERNEL_start, |
| 45 | __device_APPLICATION_start, |
| 46 | /* End marker */ |
| 47 | __device_init_end, |
| 48 | }; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 49 | |
Amir Kaplan | 61b6f5a | 2017-03-22 14:49:34 +0200 | [diff] [blame] | 50 | for (info = config_levels[level]; info < config_levels[level+1]; |
| 51 | info++) { |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 52 | struct device_config *device_conf = info->config; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 53 | |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 54 | (void)device_conf->init(info); |
Andrew Boie | 5bd891d | 2017-09-27 12:59:28 -0700 | [diff] [blame] | 55 | _k_object_init(info); |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | struct device *device_get_binding(const char *name) |
| 60 | { |
| 61 | struct device *info; |
| 62 | |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 63 | /* Split the search into two loops: in the common scenario, where |
| 64 | * device names are stored in ROM (and are referenced by the user |
| 65 | * with CONFIG_* macros), only cheap pointer comparisons will be |
| 66 | * performed. Reserve string comparisons for a fallback. |
| 67 | */ |
| 68 | for (info = __device_init_start; info != __device_init_end; info++) { |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 69 | if ((info->driver_api != NULL) && |
| 70 | (info->config->name == name)) { |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 71 | return info; |
| 72 | } |
| 73 | } |
| 74 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 75 | for (info = __device_init_start; info != __device_init_end; info++) { |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 76 | if (info->driver_api == NULL) { |
Leandro Pereira | d24daa4 | 2017-10-19 14:17:37 -0700 | [diff] [blame] | 77 | continue; |
| 78 | } |
| 79 | |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 80 | if (strcmp(name, info->config->name) == 0) { |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 81 | return info; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | #ifdef CONFIG_DEVICE_POWER_MANAGEMENT |
Ramesh Thomas | 6249c56 | 2016-10-07 17:07:04 -0700 | [diff] [blame] | 89 | int device_pm_control_nop(struct device *unused_device, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 90 | u32_t unused_ctrl_command, void *unused_context) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 91 | { |
| 92 | return 0; |
| 93 | } |
Ramesh Thomas | 6249c56 | 2016-10-07 17:07:04 -0700 | [diff] [blame] | 94 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 95 | void device_list_get(struct device **device_list, int *device_count) |
| 96 | { |
| 97 | |
| 98 | *device_list = __device_init_start; |
| 99 | *device_count = __device_init_end - __device_init_start; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | int device_any_busy_check(void) |
| 104 | { |
| 105 | int i = 0; |
| 106 | |
| 107 | for (i = 0; i < DEVICE_BUSY_SIZE; i++) { |
| 108 | if (__device_busy_start[i] != 0) { |
| 109 | return -EBUSY; |
| 110 | } |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int device_busy_check(struct device *chk_dev) |
| 116 | { |
| 117 | if (atomic_test_bit((const atomic_t *)__device_busy_start, |
| 118 | (chk_dev - __device_init_start))) { |
| 119 | return -EBUSY; |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | #endif |
| 125 | |
| 126 | void device_busy_set(struct device *busy_dev) |
| 127 | { |
| 128 | #ifdef CONFIG_DEVICE_POWER_MANAGEMENT |
| 129 | atomic_set_bit((atomic_t *) __device_busy_start, |
| 130 | (busy_dev - __device_init_start)); |
Flavio Santes | b80db0a | 2016-12-11 00:19:26 -0600 | [diff] [blame] | 131 | #else |
| 132 | ARG_UNUSED(busy_dev); |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 133 | #endif |
| 134 | } |
| 135 | |
| 136 | void device_busy_clear(struct device *busy_dev) |
| 137 | { |
| 138 | #ifdef CONFIG_DEVICE_POWER_MANAGEMENT |
| 139 | atomic_clear_bit((atomic_t *) __device_busy_start, |
| 140 | (busy_dev - __device_init_start)); |
Flavio Santes | b80db0a | 2016-12-11 00:19:26 -0600 | [diff] [blame] | 141 | #else |
| 142 | ARG_UNUSED(busy_dev); |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 143 | #endif |
| 144 | } |