Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | /* ipm_dummy.c - Fake IPM driver for testing upper-level drivers */
/*
* Copyright (c) 2015 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <zephyr.h>
#include <ipm.h>
#include <errno.h>
#include <device.h>
#include <init.h>
#include <misc/printk.h>
#include <irq_offload.h>
#include "ipm_dummy.h"
/* Implemented as a software interrupt so that callbacks are executed
* in the expected context
*/
static void ipm_dummy_isr(void *data)
{
struct device *d = (struct device *)data;
struct ipm_dummy_driver_data *driver_data = d->driver_data;
/* In a real driver the interrupt simply wouldn't fire, we fake
* that here
*/
if (!driver_data->regs.enabled || !driver_data->regs.busy)
return;
if (driver_data->cb) {
driver_data->cb(driver_data->cb_context, driver_data->regs.id,
(volatile void *)&driver_data->regs.data);
}
driver_data->regs.busy = 0;
}
/* IPM API functions for the dummy driver */
static int ipm_dummy_send(struct device *d, int wait, uint32_t id,
const void *data, int size)
{
struct ipm_dummy_driver_data *driver_data;
volatile uint8_t *datareg;
const uint8_t *data8;
int i;
driver_data = d->driver_data;
if (size > ipm_max_data_size_get(d)) {
return -EMSGSIZE;
}
if (driver_data->regs.busy) {
return -EBUSY;
}
data8 = (const uint8_t *)data;
datareg = (volatile uint8_t *)driver_data->regs.data;
for (i = 0; i < size; ++i) {
datareg[i] = data8[i];
}
driver_data->regs.id = id;
driver_data->regs.busy = 1;
irq_offload(ipm_dummy_isr, d);
if (wait) {
while (driver_data->regs.busy) {
/* busy-wait */
}
}
return 0;
}
static void ipm_dummy_register_callback(struct device *d, ipm_callback_t cb,
void *cb_context)
{
struct ipm_dummy_driver_data *driver_data;
driver_data = d->driver_data;
driver_data->cb = cb;
driver_data->cb_context = cb_context;
}
static int ipm_dummy_set_enabled(struct device *d, int enable)
{
struct ipm_dummy_driver_data *driver_data = d->driver_data;
driver_data->regs.enabled = enable;
if (enable) {
/* In case there are pending messages */
irq_offload(ipm_dummy_isr, d);
}
return 0;
}
static uint32_t ipm_dummy_max_id_val_get(struct device *d)
{
return 0xFFFFFFFF;
}
static int ipm_dummy_max_data_size_get(struct device *d)
{
return DUMMY_IPM_DATA_WORDS * sizeof(uint32_t);
}
struct ipm_driver_api ipm_dummy_api = {
.send = ipm_dummy_send,
.register_callback = ipm_dummy_register_callback,
.max_data_size_get = ipm_dummy_max_data_size_get,
.max_id_val_get = ipm_dummy_max_id_val_get,
.set_enabled = ipm_dummy_set_enabled
};
/* Dummy IPM driver initialization, will be bound at runtime
* to high-level drivers under test
*/
int ipm_dummy_init(struct device *d)
{
struct ipm_dummy_driver_data *driver_data;
driver_data = d->driver_data;
d->driver_api = &ipm_dummy_api;
return 0;
}
|