source: rtems/c/src/lib/libbsp/powerpc/gen83xx/spi/spi_init.c @ d4d60360

4.104.114.95
Last change on this file since d4d60360 was d4d60360, checked in by Thomas Doerfler <Thomas.Doerfler@…>, on 12/04/07 at 20:36:31

added missing file

  • Property mode set to 100644
File size: 10.2 KB
Line 
1/*===============================================================*\
2| Project: RTEMS support for MPC83xx                              |
3+-----------------------------------------------------------------+
4|                    Copyright (c) 2007                           |
5|                    Embedded Brains GmbH                         |
6|                    Obere Lagerstr. 30                           |
7|                    D-82178 Puchheim                             |
8|                    Germany                                      |
9|                    rtems@embedded-brains.de                     |
10+-----------------------------------------------------------------+
11| The license and distribution terms for this file may be         |
12| found in the file LICENSE in this distribution or at            |
13|                                                                 |
14| http://www.rtems.com/license/LICENSE.                           |
15|                                                                 |
16+-----------------------------------------------------------------+
17| this file contains the low level MPC83xx SPI driver parameters  |
18| and board-specific functions                                    |
19\*===============================================================*/
20#include <mpc83xx/mpc83xx_spidrv.h>
21#include <bsp/irq.h>
22#include <bsp.h>
23#if defined(MPC8349EAMDS)
24#include <libchip/i2c-2b-eeprom.h>
25#include <libchip/spi-flash-m25p40.h>
26#endif
27
28/*=========================================================================*\
29| Board-specific adaptation functions                                       |
30\*=========================================================================*/
31
32/*=========================================================================*\
33| Function:                                                                 |
34\*-------------------------------------------------------------------------*/
35static rtems_status_code bsp_spi_sel_addr
36(
37/*-------------------------------------------------------------------------*\
38| Purpose:                                                                  |
39|   address a slave device on the bus                                       |
40+---------------------------------------------------------------------------+
41| Input Parameters:                                                         |
42\*-------------------------------------------------------------------------*/
43 rtems_libi2c_bus_t *bh,                 /* bus specifier structure        */
44 uint32_t addr,                          /* address to send on bus         */
45 int rw                                  /* 0=write,1=read                 */
46)
47/*-------------------------------------------------------------------------*\
48| Return Value:                                                             |
49|    o = ok or error code                                                   |
50\*=========================================================================*/
51{
52#if defined(MPC8349EAMDS)
53  /*
54   * check device address for valid range
55   */
56  if (addr > 0) {
57    return RTEMS_INVALID_NUMBER;
58  }
59  /*
60   * select given device
61   * GPIO1[0] is nSEL_SPI for M25P40
62   * set it to be active/low
63   */
64  mpc83xx.gpio[0].gpdat &= ~(1 << (31- 0));
65#endif
66#if defined(HSC_CM01)
67  /*
68   * check device address for valid range
69   */
70  if (addr > 7) {
71    return RTEMS_INVALID_NUMBER;
72  }
73  /*
74   * select given device
75   */
76  /*
77   * GPIO1[24] is SPI_A0
78   * GPIO1[25] is SPI_A1
79   * GPIO1[26] is SPI_A2
80   * set pins to address
81   */
82  mpc83xx.gpio[0].gpdat =
83    (mpc83xx.gpio[0].gpdat & ~(0x7  << (31-26)))
84    |                         (addr << (31-26));
85  /*
86   * GPIO1[27] is high-active strobe
87   */
88  mpc83xx.gpio[0].gpdat |= (1 << (31- 27));
89#endif
90  return  RTEMS_SUCCESSFUL;
91}
92
93/*=========================================================================*\
94| Function:                                                                 |
95\*-------------------------------------------------------------------------*/
96static rtems_status_code bsp_spi_send_start_dummy
97(
98/*-------------------------------------------------------------------------*\
99| Purpose:                                                                  |
100|   dummy function, SPI has no start condition                              |
101+---------------------------------------------------------------------------+
102| Input Parameters:                                                         |
103\*-------------------------------------------------------------------------*/
104 rtems_libi2c_bus_t *bh                  /* bus specifier structure        */
105)
106/*-------------------------------------------------------------------------*\
107| Return Value:                                                             |
108|    o = ok or error code                                                   |
109\*=========================================================================*/
110{
111#if defined(MPC8349EAMDS)
112  /*
113   * GPIO1[0] is nSEL_SPI for M25P40
114   * set it to inactive/high
115   */
116  mpc83xx.gpio[0].gpdat |=  (1 << (31- 0));
117#endif
118#if defined(HSC_CM01)
119  /*
120   * GPIO1[27] is high-active strobe
121   * set it to inactive/ low
122   */
123  mpc83xx.gpio[0].gpdat &= ~(0x1 << (31-27));
124#endif
125  return 0;
126}
127
128/*=========================================================================*\
129| Function:                                                                 |
130\*-------------------------------------------------------------------------*/
131static rtems_status_code bsp_spi_send_stop
132(
133/*-------------------------------------------------------------------------*\
134| Purpose:                                                                  |
135|   deselect SPI                                                            |
136+---------------------------------------------------------------------------+
137| Input Parameters:                                                         |
138\*-------------------------------------------------------------------------*/
139 rtems_libi2c_bus_t *bh                  /* bus specifier structure        */
140)
141/*-------------------------------------------------------------------------*\
142| Return Value:                                                             |
143|    o = ok or error code                                                   |
144\*=========================================================================*/
145{
146#if defined(DEBUG)
147  printk("bsp_spi_send_stop called... ");
148#endif
149#if defined(MPC8349EAMDS)
150  /*
151   * deselect given device
152   * GPIO1[0] is nSEL_SPI for M25P40
153   * set it to be inactive/high
154   */
155  mpc83xx.gpio[0].gpdat |=  (1 << (31- 0));
156#endif
157#if defined(HSC_CM01)
158  /*
159   * deselect device
160   * GPIO1[27] is high-active strobe
161   */
162  mpc83xx.gpio[0].gpdat &= ~(1 << (31- 27));
163#endif
164#if defined(DEBUG)
165  printk("... exit OK\r\n");
166#endif
167  return 0;
168}
169
170/*=========================================================================*\
171| list of handlers                                                          |
172\*=========================================================================*/
173
174rtems_libi2c_bus_ops_t bsp_spi_ops = {
175  init:             mpc83xx_spi_init,
176  send_start:       bsp_spi_send_start_dummy,
177  send_stop:        bsp_spi_send_stop,
178  send_addr:        bsp_spi_sel_addr,
179  read_bytes:       mpc83xx_spi_read_bytes,
180  write_bytes:      mpc83xx_spi_write_bytes,
181  ioctl:            mpc83xx_spi_ioctl
182};
183
184static mpc83xx_spi_desc_t bsp_spi_bus_desc = {
185  {/* public fields */
186    ops:        &bsp_spi_ops,
187    size:       sizeof(bsp_spi_bus_desc),
188  },
189  { /* our private fields */
190    reg_ptr:     &mpc83xx.spi,
191    initialized: FALSE,
192    irq_number:  BSP_IPIC_IRQ_SPI
193  }
194};
195
196/*=========================================================================*\
197| initialization                                                            |
198\*=========================================================================*/
199
200/*=========================================================================*\
201| Function:                                                                 |
202\*-------------------------------------------------------------------------*/
203rtems_status_code bsp_register_spi
204(
205/*-------------------------------------------------------------------------*\
206| Purpose:                                                                  |
207|   register SPI bus and devices                                            |
208+---------------------------------------------------------------------------+
209| Input Parameters:                                                         |
210\*-------------------------------------------------------------------------*/
211 void                                    /* <none>                         */
212)
213/*-------------------------------------------------------------------------*\
214| Return Value:                                                             |
215|    0 or error code                                                        |
216\*=========================================================================*/
217{
218  int ret_code;
219  int spi_busno;
220
221  /*
222   * init I2C library (if not already done)
223   */
224  rtems_libi2c_initialize ();
225
226  /*
227   * init port pins used to address/select SPI devices
228   */
229#if defined(MPC8349EAMDS)
230  /*
231   * GPIO1[0] is nSEL_SPI for M25P40
232   * set it to be output, high
233   */
234  mpc83xx.gpio[0].gpdat |=  (1 << (31- 0));
235  mpc83xx.gpio[0].gpdir |=  (1 << (31- 0));
236  mpc83xx.gpio[0].gpdr  &= ~(1 << (31- 0));
237#endif
238#if defined(HSC_CM01)
239  /*
240   * GPIO1[24] is SPI_A0
241   * GPIO1[25] is SPI_A1
242   * GPIO1[26] is SPI_A2
243   * GPIO1[27] is high-active strobe
244   * set pins to be output, low
245   */
246  mpc83xx.gpio[0].gpdat &= ~(0xf << (31-27));
247  mpc83xx.gpio[0].gpdir |=  (0xf << (31-27));
248  mpc83xx.gpio[0].gpdr  &= ~(0xf << (31-27));
249#endif
250  /*
251   * register SPI bus
252   */
253  ret_code = rtems_libi2c_register_bus("/dev/spi",
254                                       &(bsp_spi_bus_desc.bus_desc));
255  if (ret_code < 0) {
256    return -ret_code;
257  }
258  spi_busno = ret_code;
259  /*
260   * register M25P40 Flash, when available
261   */
262#if defined(MPC8349EAMDS)
263  ret_code = rtems_libi2c_register_drv(RTEMS_BSP_SPI_FLASH_DEVICE_NAME,
264                                       spi_flash_m25p40_rw_driver_descriptor,
265                                       spi_busno,0x00);
266  if (ret_code < 0) {
267    return -ret_code;
268  }
269#endif
270  /*
271   * FIXME: further drivers, when available
272   */
273  return 0;
274}
Note: See TracBrowser for help on using the repository browser.