summaryrefslogtreecommitdiffstats
path: root/bsps/powerpc/gen83xx/spi/spi_init.c
blob: 0455c225b0e5416a2f31c1cd85e72e3a23cacfb6 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* SPDX-License-Identifier: BSD-2-Clause */

/*
 * RTEMS support for MPC83xx
 *
 * This file contains the low level MPC83xx SPI driver parameters
 * and board-specific functions.
 */

/*
 * Copyright (c) 2007 embedded brains GmbH & Co. KG
 *
 * 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 <mpc83xx/mpc83xx_spidrv.h>
#include <bsp/irq.h>
#include <bsp.h>

#if defined(MPC83XX_BOARD_MPC8313ERDB)

#include <libchip/spi-sd-card.h>

#elif defined(MPC83XX_BOARD_MPC8349EAMDS)

#include <libchip/spi-flash-m25p40.h>

#elif defined(MPC83XX_BOARD_HSC_CM01)

#include <libchip/spi-fram-fm25l256.h>

#endif

/*=========================================================================*\
| Board-specific adaptation functions                                       |
\*=========================================================================*/

/*=========================================================================*\
| Function:                                                                 |
\*-------------------------------------------------------------------------*/
static rtems_status_code bsp_spi_sel_addr
(
/*-------------------------------------------------------------------------*\
| Purpose:                                                                  |
|   address a slave device on the bus                                       |
+---------------------------------------------------------------------------+
| Input Parameters:                                                         |
\*-------------------------------------------------------------------------*/
 rtems_libi2c_bus_t *bh,                 /* bus specifier structure        */
 uint32_t addr,                          /* address to send on bus         */
 int rw                                  /* 0=write,1=read                 */
)
/*-------------------------------------------------------------------------*\
| Return Value:                                                             |
|    o = ok or error code                                                   |
\*=========================================================================*/
{

#if defined( MPC83XX_BOARD_MPC8313ERDB)

  /* Check address */
  if (addr > 0) {
    return RTEMS_INVALID_NUMBER;
  }

  /* SCS (active low) */
  mpc83xx.gpio [0].gpdat &= ~0x20000000;

#elif defined( MPC83XX_BOARD_MPC8349EAMDS)

  /*
   * check device address for valid range
   */
  if (addr > 0) {
    return RTEMS_INVALID_NUMBER;
  }
  /*
   * select given device
   * GPIO1[0] is nSEL_SPI for M25P40
   * set it to be active/low
   */
  mpc83xx.gpio[0].gpdat &= ~(1 << (31- 0));

#elif defined( MPC83XX_BOARD_HSC_CM01)

  /*
   * check device address for valid range
   */
  if (addr > 7) {
    return RTEMS_INVALID_NUMBER;
  }
  /*
   * select given device
   */
  /*
   * GPIO1[24] is SPI_A0
   * GPIO1[25] is SPI_A1
   * GPIO1[26] is SPI_A2
   * set pins to address
   */
  mpc83xx.gpio[0].gpdat =
    (mpc83xx.gpio[0].gpdat & ~(0x7  << (31-26)))
    |                         (addr << (31-26));
  /*
   * GPIO1[27] is high-active strobe
   */
  mpc83xx.gpio[0].gpdat |= (1 << (31- 27));

#endif

  return  RTEMS_SUCCESSFUL;
}

/*=========================================================================*\
| Function:                                                                 |
\*-------------------------------------------------------------------------*/
static rtems_status_code bsp_spi_send_start_dummy
(
/*-------------------------------------------------------------------------*\
| Purpose:                                                                  |
|   dummy function, SPI has no start condition                              |
+---------------------------------------------------------------------------+
| Input Parameters:                                                         |
\*-------------------------------------------------------------------------*/
 rtems_libi2c_bus_t *bh                  /* bus specifier structure        */
)
/*-------------------------------------------------------------------------*\
| Return Value:                                                             |
|    o = ok or error code                                                   |
\*=========================================================================*/
{

#if defined( MPC83XX_BOARD_MPC8313ERDB)

  /* SCS (inactive high) */
  mpc83xx.gpio [0].gpdat |= 0x20000000;

#elif defined( MPC83XX_BOARD_MPC8349EAMDS)

  /*
   * GPIO1[0] is nSEL_SPI for M25P40
   * set it to inactive/high
   */
  mpc83xx.gpio[0].gpdat |=  (1 << (31- 0));

#elif defined( MPC83XX_BOARD_HSC_CM01)

  /*
   * GPIO1[27] is high-active strobe
   * set it to inactive/ low
   */
  mpc83xx.gpio[0].gpdat &= ~(0x1 << (31-27));

#endif

  return 0;
}

/*=========================================================================*\
| Function:                                                                 |
\*-------------------------------------------------------------------------*/
static rtems_status_code bsp_spi_send_stop
(
/*-------------------------------------------------------------------------*\
| Purpose:                                                                  |
|   deselect SPI                                                            |
+---------------------------------------------------------------------------+
| Input Parameters:                                                         |
\*-------------------------------------------------------------------------*/
 rtems_libi2c_bus_t *bh                  /* bus specifier structure        */
)
/*-------------------------------------------------------------------------*\
| Return Value:                                                             |
|    o = ok or error code                                                   |
\*=========================================================================*/
{
#if defined(DEBUG)
  printk("bsp_spi_send_stop called... ");
#endif

#if defined( MPC83XX_BOARD_MPC8313ERDB)

  /* SCS (inactive high) */
  mpc83xx.gpio [0].gpdat |= 0x20000000;

#elif defined( MPC83XX_BOARD_MPC8349EAMDS)

  /*
   * deselect given device
   * GPIO1[0] is nSEL_SPI for M25P40
   * set it to be inactive/high
   */
  mpc83xx.gpio[0].gpdat |=  (1 << (31- 0));

#elif defined( MPC83XX_BOARD_HSC_CM01)

  /*
   * deselect device
   * GPIO1[27] is high-active strobe
   */
  mpc83xx.gpio[0].gpdat &= ~(1 << (31- 27));

#endif

#if defined(DEBUG)
  printk("... exit OK\r\n");
#endif
  return 0;
}

/*=========================================================================*\
| list of handlers                                                          |
\*=========================================================================*/

rtems_libi2c_bus_ops_t bsp_spi_ops = {
  .init = mpc83xx_spi_init,
  .send_start = bsp_spi_send_start_dummy,
  .send_stop = bsp_spi_send_stop,
  .send_addr = bsp_spi_sel_addr,
  .read_bytes = mpc83xx_spi_read_bytes,
  .write_bytes = mpc83xx_spi_write_bytes,
  .ioctl = mpc83xx_spi_ioctl
};

static mpc83xx_spi_desc_t bsp_spi_bus_desc = {
  {/* public fields */
    .ops = &bsp_spi_ops,
    .size = sizeof(bsp_spi_bus_desc)
  },
  { /* our private fields */
    .reg_ptr =&mpc83xx.spi,
    .initialized = FALSE,
    .irq_number = BSP_IPIC_IRQ_SPI,
    .base_frq = 0 /* filled in during init */
  }
};

#ifdef MPC83XX_BOARD_MPC8313ERDB

#include <libchip/spi-sd-card.h>

#define SD_CARD_NUMBER 1

size_t sd_card_driver_table_size = SD_CARD_NUMBER;

sd_card_driver_entry sd_card_driver_table [SD_CARD_NUMBER] = {
  {
    .device_name = "/dev/sd-card-a",
    .bus = 0,
    .transfer_mode = SD_CARD_TRANSFER_MODE_DEFAULT,
    .command = SD_CARD_COMMAND_DEFAULT,
    /* .response = whatever, */
    .response_index = SD_CARD_COMMAND_SIZE,
    .n_ac_max = SD_CARD_N_AC_MAX_DEFAULT,
    .block_number = 0,
    .block_size = 0,
    .block_size_shift = 0,
    .busy = true,
    .verbose = true,
    .schedule_if_busy = false
  }
};

#endif /* MPC83XX_BOARD_MPC8313ERDB */


/*=========================================================================*\
| initialization                                                            |
\*=========================================================================*/

/*=========================================================================*\
| Function:                                                                 |
\*-------------------------------------------------------------------------*/
rtems_status_code bsp_register_spi
(
/*-------------------------------------------------------------------------*\
| Purpose:                                                                  |
|   register SPI bus and devices                                            |
+---------------------------------------------------------------------------+
| Input Parameters:                                                         |
\*-------------------------------------------------------------------------*/
 void                                    /* <none>                         */
)
/*-------------------------------------------------------------------------*\
| Return Value:                                                             |
|    0 or error code                                                        |
\*=========================================================================*/
{
  #if defined(MPC83XX_BOARD_MPC8313ERDB)
    rtems_status_code sc = RTEMS_SUCCESSFUL;
  #endif
  unsigned spi_busno;
  int      ret_code;

  /*
   * init I2C library (if not already done)
   */
  rtems_libi2c_initialize ();

  /*
   * init port pins used to address/select SPI devices
   */

#if defined(MPC83XX_BOARD_MPC8313ERDB)

  /*
   * Configured as master (direct connection to SD card)
   *
   * GPIO[28] : SOUT
   * GPIO[29] : SIN
   * GPIO[30] : SCLK
   * GPIO[02] : SCS (inactive high), GPIO[02] is normally connected to U43 at
   * pin 15 of MC74LCX244DT.
   */

  /* Function */
  mpc83xx.syscon.sicrl = (mpc83xx.syscon.sicrl & ~0x03fc0000) | 0x30000000;

  /* Direction */
  mpc83xx.gpio [0].gpdir = (mpc83xx.gpio [0].gpdir & ~0x0000000f) | 0x2000000b;

  /* Data */
  mpc83xx.gpio [0].gpdat |= 0x20000000;

  /* Open Drain */
  /* mpc83xx.gpio [0].gpdr  |= 0x0000000f; */

#elif defined(MPC83XX_BOARD_MPC8349EAMDS)

  /*
   * GPIO1[0] is nSEL_SPI for M25P40
   * set it to be output, high
   */
  mpc83xx.gpio[0].gpdat |=  (1 << (31- 0));
  mpc83xx.gpio[0].gpdir |=  (1 << (31- 0));
  mpc83xx.gpio[0].gpdr  &= ~(1 << (31- 0));

#elif defined(MPC83XX_BOARD_HSC_CM01)

  /*
   * GPIO1[24] is SPI_A0
   * GPIO1[25] is SPI_A1
   * GPIO1[26] is SPI_A2
   * GPIO1[27] is high-active strobe
   * set pins to be output, low
   */
  mpc83xx.gpio[0].gpdat &= ~(0xf << (31-27));
  mpc83xx.gpio[0].gpdir |=  (0xf << (31-27));
  mpc83xx.gpio[0].gpdr  &= ~(0xf << (31-27));

#else

  /*
   * There is no SPI configuration information for this variant.
   */
  (void) spi_busno; /* avoid set but not used warning */
#endif

  /*
   * update base frequency in spi descriptor
   */
  bsp_spi_bus_desc.softc.base_frq = BSP_bus_frequency;

  /*
   * register SPI bus
   */
  ret_code = rtems_libi2c_register_bus("/dev/spi",
				       &(bsp_spi_bus_desc.bus_desc));
  if (ret_code < 0) {
    return -ret_code;
  }
  spi_busno = (unsigned) ret_code;

#if defined(MPC83XX_BOARD_MPC8313ERDB)

  /* Register SD Card driver */
  sd_card_driver_table [0].bus = spi_busno;
  sc = sd_card_register();
  if (sc != RTEMS_SUCCESSFUL) {
    return sc;
  }

#elif defined(MPC83XX_BOARD_MPC8349EAMDS)

  /*
   * register M25P40 Flash
   */
  ret_code = rtems_libi2c_register_drv(RTEMS_BSP_SPI_FLASH_DEVICE_NAME,
				       spi_flash_m25p40_rw_driver_descriptor,
				       spi_busno,0x00);
#elif defined(MPC83XX_BOARD_HSC_CM01)

  /*
   * register FM25L256 FRAM
   */
  ret_code = rtems_libi2c_register_drv(RTEMS_BSP_SPI_FRAM_DEVICE_NAME,
				       spi_fram_fm25l256_rw_driver_descriptor,
				       spi_busno,0x02);

#endif

  if (ret_code < 0) {
    return -ret_code;
  }

  /*
   * FIXME: further drivers, when available
   */
  return 0;
}