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/spi-flash-m25p40.h> |
---|
25 | #endif |
---|
26 | #if defined(HSC_CM01) |
---|
27 | #include <libchip/spi-fram-fm25l256.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | /*=========================================================================*\ |
---|
31 | | Board-specific adaptation functions | |
---|
32 | \*=========================================================================*/ |
---|
33 | |
---|
34 | /*=========================================================================*\ |
---|
35 | | Function: | |
---|
36 | \*-------------------------------------------------------------------------*/ |
---|
37 | static rtems_status_code bsp_spi_sel_addr |
---|
38 | ( |
---|
39 | /*-------------------------------------------------------------------------*\ |
---|
40 | | Purpose: | |
---|
41 | | address a slave device on the bus | |
---|
42 | +---------------------------------------------------------------------------+ |
---|
43 | | Input Parameters: | |
---|
44 | \*-------------------------------------------------------------------------*/ |
---|
45 | rtems_libi2c_bus_t *bh, /* bus specifier structure */ |
---|
46 | uint32_t addr, /* address to send on bus */ |
---|
47 | int rw /* 0=write,1=read */ |
---|
48 | ) |
---|
49 | /*-------------------------------------------------------------------------*\ |
---|
50 | | Return Value: | |
---|
51 | | o = ok or error code | |
---|
52 | \*=========================================================================*/ |
---|
53 | { |
---|
54 | #if defined(MPC8349EAMDS) |
---|
55 | /* |
---|
56 | * check device address for valid range |
---|
57 | */ |
---|
58 | if (addr > 0) { |
---|
59 | return RTEMS_INVALID_NUMBER; |
---|
60 | } |
---|
61 | /* |
---|
62 | * select given device |
---|
63 | * GPIO1[0] is nSEL_SPI for M25P40 |
---|
64 | * set it to be active/low |
---|
65 | */ |
---|
66 | mpc83xx.gpio[0].gpdat &= ~(1 << (31- 0)); |
---|
67 | #endif |
---|
68 | #if defined(HSC_CM01) |
---|
69 | /* |
---|
70 | * check device address for valid range |
---|
71 | */ |
---|
72 | if (addr > 7) { |
---|
73 | return RTEMS_INVALID_NUMBER; |
---|
74 | } |
---|
75 | /* |
---|
76 | * select given device |
---|
77 | */ |
---|
78 | /* |
---|
79 | * GPIO1[24] is SPI_A0 |
---|
80 | * GPIO1[25] is SPI_A1 |
---|
81 | * GPIO1[26] is SPI_A2 |
---|
82 | * set pins to address |
---|
83 | */ |
---|
84 | mpc83xx.gpio[0].gpdat = |
---|
85 | (mpc83xx.gpio[0].gpdat & ~(0x7 << (31-26))) |
---|
86 | | (addr << (31-26)); |
---|
87 | /* |
---|
88 | * GPIO1[27] is high-active strobe |
---|
89 | */ |
---|
90 | mpc83xx.gpio[0].gpdat |= (1 << (31- 27)); |
---|
91 | #endif |
---|
92 | return RTEMS_SUCCESSFUL; |
---|
93 | } |
---|
94 | |
---|
95 | /*=========================================================================*\ |
---|
96 | | Function: | |
---|
97 | \*-------------------------------------------------------------------------*/ |
---|
98 | static rtems_status_code bsp_spi_send_start_dummy |
---|
99 | ( |
---|
100 | /*-------------------------------------------------------------------------*\ |
---|
101 | | Purpose: | |
---|
102 | | dummy function, SPI has no start condition | |
---|
103 | +---------------------------------------------------------------------------+ |
---|
104 | | Input Parameters: | |
---|
105 | \*-------------------------------------------------------------------------*/ |
---|
106 | rtems_libi2c_bus_t *bh /* bus specifier structure */ |
---|
107 | ) |
---|
108 | /*-------------------------------------------------------------------------*\ |
---|
109 | | Return Value: | |
---|
110 | | o = ok or error code | |
---|
111 | \*=========================================================================*/ |
---|
112 | { |
---|
113 | #if defined(MPC8349EAMDS) |
---|
114 | /* |
---|
115 | * GPIO1[0] is nSEL_SPI for M25P40 |
---|
116 | * set it to inactive/high |
---|
117 | */ |
---|
118 | mpc83xx.gpio[0].gpdat |= (1 << (31- 0)); |
---|
119 | #endif |
---|
120 | #if defined(HSC_CM01) |
---|
121 | /* |
---|
122 | * GPIO1[27] is high-active strobe |
---|
123 | * set it to inactive/ low |
---|
124 | */ |
---|
125 | mpc83xx.gpio[0].gpdat &= ~(0x1 << (31-27)); |
---|
126 | #endif |
---|
127 | return 0; |
---|
128 | } |
---|
129 | |
---|
130 | /*=========================================================================*\ |
---|
131 | | Function: | |
---|
132 | \*-------------------------------------------------------------------------*/ |
---|
133 | static rtems_status_code bsp_spi_send_stop |
---|
134 | ( |
---|
135 | /*-------------------------------------------------------------------------*\ |
---|
136 | | Purpose: | |
---|
137 | | deselect SPI | |
---|
138 | +---------------------------------------------------------------------------+ |
---|
139 | | Input Parameters: | |
---|
140 | \*-------------------------------------------------------------------------*/ |
---|
141 | rtems_libi2c_bus_t *bh /* bus specifier structure */ |
---|
142 | ) |
---|
143 | /*-------------------------------------------------------------------------*\ |
---|
144 | | Return Value: | |
---|
145 | | o = ok or error code | |
---|
146 | \*=========================================================================*/ |
---|
147 | { |
---|
148 | #if defined(DEBUG) |
---|
149 | printk("bsp_spi_send_stop called... "); |
---|
150 | #endif |
---|
151 | #if defined(MPC8349EAMDS) |
---|
152 | /* |
---|
153 | * deselect given device |
---|
154 | * GPIO1[0] is nSEL_SPI for M25P40 |
---|
155 | * set it to be inactive/high |
---|
156 | */ |
---|
157 | mpc83xx.gpio[0].gpdat |= (1 << (31- 0)); |
---|
158 | #endif |
---|
159 | #if defined(HSC_CM01) |
---|
160 | /* |
---|
161 | * deselect device |
---|
162 | * GPIO1[27] is high-active strobe |
---|
163 | */ |
---|
164 | mpc83xx.gpio[0].gpdat &= ~(1 << (31- 27)); |
---|
165 | #endif |
---|
166 | #if defined(DEBUG) |
---|
167 | printk("... exit OK\r\n"); |
---|
168 | #endif |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | /*=========================================================================*\ |
---|
173 | | list of handlers | |
---|
174 | \*=========================================================================*/ |
---|
175 | |
---|
176 | rtems_libi2c_bus_ops_t bsp_spi_ops = { |
---|
177 | init: mpc83xx_spi_init, |
---|
178 | send_start: bsp_spi_send_start_dummy, |
---|
179 | send_stop: bsp_spi_send_stop, |
---|
180 | send_addr: bsp_spi_sel_addr, |
---|
181 | read_bytes: mpc83xx_spi_read_bytes, |
---|
182 | write_bytes: mpc83xx_spi_write_bytes, |
---|
183 | ioctl: mpc83xx_spi_ioctl |
---|
184 | }; |
---|
185 | |
---|
186 | static mpc83xx_spi_desc_t bsp_spi_bus_desc = { |
---|
187 | {/* public fields */ |
---|
188 | ops: &bsp_spi_ops, |
---|
189 | size: sizeof(bsp_spi_bus_desc), |
---|
190 | }, |
---|
191 | { /* our private fields */ |
---|
192 | reg_ptr: &mpc83xx.spi, |
---|
193 | initialized: FALSE, |
---|
194 | irq_number: BSP_IPIC_IRQ_SPI, |
---|
195 | base_frq : 0 /* filled in during init */ |
---|
196 | } |
---|
197 | }; |
---|
198 | |
---|
199 | /*=========================================================================*\ |
---|
200 | | initialization | |
---|
201 | \*=========================================================================*/ |
---|
202 | |
---|
203 | /*=========================================================================*\ |
---|
204 | | Function: | |
---|
205 | \*-------------------------------------------------------------------------*/ |
---|
206 | rtems_status_code bsp_register_spi |
---|
207 | ( |
---|
208 | /*-------------------------------------------------------------------------*\ |
---|
209 | | Purpose: | |
---|
210 | | register SPI bus and devices | |
---|
211 | +---------------------------------------------------------------------------+ |
---|
212 | | Input Parameters: | |
---|
213 | \*-------------------------------------------------------------------------*/ |
---|
214 | void /* <none> */ |
---|
215 | ) |
---|
216 | /*-------------------------------------------------------------------------*\ |
---|
217 | | Return Value: | |
---|
218 | | 0 or error code | |
---|
219 | \*=========================================================================*/ |
---|
220 | { |
---|
221 | int ret_code; |
---|
222 | int spi_busno; |
---|
223 | |
---|
224 | /* |
---|
225 | * init I2C library (if not already done) |
---|
226 | */ |
---|
227 | rtems_libi2c_initialize (); |
---|
228 | |
---|
229 | /* |
---|
230 | * init port pins used to address/select SPI devices |
---|
231 | */ |
---|
232 | #if defined(MPC8349EAMDS) |
---|
233 | /* |
---|
234 | * GPIO1[0] is nSEL_SPI for M25P40 |
---|
235 | * set it to be output, high |
---|
236 | */ |
---|
237 | mpc83xx.gpio[0].gpdat |= (1 << (31- 0)); |
---|
238 | mpc83xx.gpio[0].gpdir |= (1 << (31- 0)); |
---|
239 | mpc83xx.gpio[0].gpdr &= ~(1 << (31- 0)); |
---|
240 | #endif |
---|
241 | #if defined(HSC_CM01) |
---|
242 | /* |
---|
243 | * GPIO1[24] is SPI_A0 |
---|
244 | * GPIO1[25] is SPI_A1 |
---|
245 | * GPIO1[26] is SPI_A2 |
---|
246 | * GPIO1[27] is high-active strobe |
---|
247 | * set pins to be output, low |
---|
248 | */ |
---|
249 | mpc83xx.gpio[0].gpdat &= ~(0xf << (31-27)); |
---|
250 | mpc83xx.gpio[0].gpdir |= (0xf << (31-27)); |
---|
251 | mpc83xx.gpio[0].gpdr &= ~(0xf << (31-27)); |
---|
252 | #endif |
---|
253 | /* |
---|
254 | * update base frequency in spi descriptor |
---|
255 | */ |
---|
256 | bsp_spi_bus_desc.softc.base_frq = BSP_bus_frequency; |
---|
257 | |
---|
258 | /* |
---|
259 | * register SPI bus |
---|
260 | */ |
---|
261 | ret_code = rtems_libi2c_register_bus("/dev/spi", |
---|
262 | &(bsp_spi_bus_desc.bus_desc)); |
---|
263 | if (ret_code < 0) { |
---|
264 | return -ret_code; |
---|
265 | } |
---|
266 | spi_busno = ret_code; |
---|
267 | #if defined(MPC8349EAMDS) |
---|
268 | /* |
---|
269 | * register M25P40 Flash |
---|
270 | */ |
---|
271 | ret_code = rtems_libi2c_register_drv(RTEMS_BSP_SPI_FLASH_DEVICE_NAME, |
---|
272 | spi_flash_m25p40_rw_driver_descriptor, |
---|
273 | spi_busno,0x00); |
---|
274 | if (ret_code < 0) { |
---|
275 | return -ret_code; |
---|
276 | } |
---|
277 | #endif |
---|
278 | #if defined(HSC_CM01) |
---|
279 | /* |
---|
280 | * register FM25L256 FRAM |
---|
281 | */ |
---|
282 | ret_code = rtems_libi2c_register_drv(RTEMS_BSP_SPI_FRAM_DEVICE_NAME, |
---|
283 | spi_fram_fm25l256_rw_driver_descriptor, |
---|
284 | spi_busno,0x02); |
---|
285 | if (ret_code < 0) { |
---|
286 | return -ret_code; |
---|
287 | } |
---|
288 | #endif |
---|
289 | /* |
---|
290 | * FIXME: further drivers, when available |
---|
291 | */ |
---|
292 | return 0; |
---|
293 | } |
---|