summaryrefslogtreecommitdiffstats
path: root/bsps/arm/altera-cyclone-v/i2c/i2cdrv.c
blob: 83ecdd55028e7f9d63b89dd86826385c3b3a4d5a (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSBSPsARMCycVI2C
 */

/*
 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <bsp.h>
#include <bsp/i2cdrv.h>
#include <assert.h>
#include <rtems/libio.h>
#include "i2cdrv-config.h"

typedef struct {
  ALT_I2C_DEV_t i2c_dev;
  rtems_id mutex;
} i2cdrv_entry;

i2cdrv_entry i2cdrv_table[CYCLONE_V_NO_I2C];

static ALT_I2C_DEV_t *get_device(i2cdrv_entry *e)
{
  return &e->i2c_dev;
}

static rtems_status_code init_i2c_module(
  i2cdrv_entry *e,
  const i2cdrv_configuration *cfg
)
{
  ALT_STATUS_CODE asc = ALT_E_SUCCESS;
  ALT_I2C_CTLR_t controller = cfg->controller;
  ALT_I2C_DEV_t *dev = get_device(e);
  ALT_I2C_MASTER_CONFIG_t i2c_cfg = {
    .addr_mode = ALT_I2C_ADDR_MODE_7_BIT,
    .restart_enable = false,
  };

  asc = alt_i2c_init(controller, dev);
  if ( asc != ALT_E_SUCCESS ) {
    return RTEMS_IO_ERROR;
  }
  asc = alt_i2c_op_mode_set(dev, ALT_I2C_MODE_MASTER);
  if ( asc != ALT_E_SUCCESS ) {
    return RTEMS_IO_ERROR;
  }
  asc = alt_i2c_master_config_speed_set(dev, &i2c_cfg, cfg->speed);
  if ( asc != ALT_E_SUCCESS ) {
    return RTEMS_IO_ERROR;
  }
  asc = alt_i2c_master_config_set(dev, &i2c_cfg);
  if ( asc != ALT_E_SUCCESS ) {
    return RTEMS_IO_ERROR;
  }
  asc = alt_i2c_enable(dev);
  if ( asc != ALT_E_SUCCESS ) {
    return RTEMS_IO_ERROR;
  }

  return RTEMS_SUCCESSFUL;
}

rtems_device_driver i2cdrv_initialize(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  for ( size_t i = 0; i < CYCLONE_V_NO_I2C; ++i ) {
    i2cdrv_entry *e = &i2cdrv_table[i];
    const i2cdrv_configuration *cfg = &i2cdrv_config[i];

    sc = rtems_io_register_name(cfg->device_name, major, i);
    assert(sc == RTEMS_SUCCESSFUL);

    sc = rtems_semaphore_create(
      rtems_build_name ('I', '2', 'C', '0' + i),
      0,
      RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY,
      0,
      &e->mutex
    );
    assert(sc == RTEMS_SUCCESSFUL);

    sc = init_i2c_module(e, cfg);
    if ( sc != RTEMS_SUCCESSFUL ) {
      /* I2C is not usable at this point. Releasing the mutex would allow the
       * usage which could lead to undefined behaviour. */
      return sc;
    }

    sc = rtems_semaphore_release(e->mutex);
    assert(sc == RTEMS_SUCCESSFUL);
  }

  return sc;
}

rtems_device_driver i2cdrv_open(
  rtems_device_major_number major,
  rtems_device_major_number minor,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  i2cdrv_entry *e = &i2cdrv_table[minor];

  sc = rtems_semaphore_obtain(e->mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  return sc;
}

rtems_device_driver i2cdrv_close(
  rtems_device_major_number major,
  rtems_device_major_number minor,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  i2cdrv_entry *e = &i2cdrv_table[minor];

  sc = rtems_semaphore_release(e->mutex);
  return sc;
}

rtems_device_driver i2cdrv_read(
  rtems_device_major_number major,
  rtems_device_major_number minor,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  i2cdrv_entry *e = &i2cdrv_table[minor];
  rtems_libio_rw_args_t *rw  = arg;
  ALT_I2C_DEV_t *dev = get_device(e);
  ALT_STATUS_CODE asc = ALT_E_SUCCESS;

  asc = alt_i2c_master_receive(dev, rw->buffer, rw->count, true, true);
  if ( asc == ALT_E_SUCCESS ) {
    rw->bytes_moved = rw->count;
  } else {
    sc = RTEMS_IO_ERROR;
  }

  return sc;
}

rtems_device_driver i2cdrv_write(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  i2cdrv_entry *e = &i2cdrv_table[minor];
  rtems_libio_rw_args_t *rw  = arg;
  ALT_I2C_DEV_t *dev = get_device(e);
  ALT_STATUS_CODE asc = ALT_E_SUCCESS;

  asc = alt_i2c_master_transmit(dev, rw->buffer, rw->count, true, true);
  if ( asc == ALT_E_SUCCESS ) {
    rw->bytes_moved = rw->count;
  } else {
    sc = RTEMS_IO_ERROR;
  }

  return sc;
}

static rtems_status_code ioctl_set_slave_address(
  i2cdrv_entry *e,
  rtems_libio_ioctl_args_t *args
)
{
  ALT_I2C_DEV_t *dev = get_device(e);
  ALT_STATUS_CODE asc = ALT_E_SUCCESS;
  uint32_t address = (uint32_t) args->buffer;

  asc = alt_i2c_master_target_set(dev, address);
  if ( asc != ALT_E_SUCCESS ) {
    return RTEMS_IO_ERROR;
  }

  return RTEMS_SUCCESSFUL;
}

rtems_device_driver i2cdrv_ioctl(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  i2cdrv_entry *e = &i2cdrv_table[minor];
  rtems_libio_ioctl_args_t *args = arg;

  switch (args->command) {
    case I2C_IOC_SET_SLAVE_ADDRESS:
      sc = ioctl_set_slave_address(e, args);
      break;
    default:
      sc = RTEMS_INVALID_NUMBER;
      break;
  }

  return sc;
}