1 | /*===============================================================*\ |
---|
2 | | Project: RTEMS generic MPC5200 BSP | |
---|
3 | +-----------------------------------------------------------------+ |
---|
4 | | Copyright (c) 2005 | |
---|
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.org/license/LICENSE. | |
---|
15 | | | |
---|
16 | +-----------------------------------------------------------------+ |
---|
17 | | MBUS module (I2C bus) driver | |
---|
18 | +-----------------------------------------------------------------+ |
---|
19 | | This file has been adapted from an existing source code file, | |
---|
20 | | see the original file header below for reference | |
---|
21 | \*===============================================================*/ |
---|
22 | |
---|
23 | /* MCF5206e MBUS module (I2C bus) driver |
---|
24 | * |
---|
25 | * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia |
---|
26 | * Author: Victor V. Vengerov <vvv@oktet.ru> |
---|
27 | * |
---|
28 | * The license and distribution terms for this file may be |
---|
29 | * found in the file LICENSE in this distribution or at |
---|
30 | * |
---|
31 | * http://www.rtems.org/license/LICENSE. |
---|
32 | */ |
---|
33 | #include "../i2c/mpc5200mbus.h" |
---|
34 | #include <bsp/mpc5200.h> |
---|
35 | #include <bsp/irq.h> |
---|
36 | #include <bsp/i2c.h> |
---|
37 | |
---|
38 | /* Events of I2C machine */ |
---|
39 | typedef enum i2c_event { |
---|
40 | EVENT_NONE, /* Spurious event */ |
---|
41 | EVENT_TRANSFER, /* Start new transfer */ |
---|
42 | EVENT_NEXTMSG, /* Start processing of next message in transfer */ |
---|
43 | EVENT_ACK, /* Sending finished with ACK */ |
---|
44 | EVENT_NACK, /* Sending finished with NACK */ |
---|
45 | EVENT_TIMEOUT, /* Timeout occured */ |
---|
46 | EVENT_DATA_RECV, /* Data received */ |
---|
47 | EVENT_ARB_LOST, /* Arbitration lost */ |
---|
48 | EVENT_SLAVE /* Addressed as a slave */ |
---|
49 | } i2c_event; |
---|
50 | |
---|
51 | /*** Auxillary primitives ***/ |
---|
52 | |
---|
53 | /* Change state of finite state machine */ |
---|
54 | #define next_state(bus,new_state) \ |
---|
55 | do { \ |
---|
56 | (bus)->state = (new_state); \ |
---|
57 | } while (0) |
---|
58 | |
---|
59 | /* Initiate start condition on the I2C bus */ |
---|
60 | #define mpc5200mbus_start(bus) \ |
---|
61 | do { \ |
---|
62 | mpc5200.i2c_regs[bus->bus_idx].mcr |= MPC5200_I2C_MCR_MSTA; \ |
---|
63 | } while (0) |
---|
64 | |
---|
65 | /* Initiate stop condition on the I2C bus */ |
---|
66 | #define mpc5200mbus_stop(bus) \ |
---|
67 | do { \ |
---|
68 | mpc5200.i2c_regs[bus->bus_idx].mcr &= ~MPC5200_I2C_MCR_MSTA; \ |
---|
69 | } while (0) |
---|
70 | |
---|
71 | /* Initiate repeat start condition on the I2C bus */ |
---|
72 | #define mpc5200mbus_rstart(bus) \ |
---|
73 | do { \ |
---|
74 | mpc5200.i2c_regs[bus->bus_idx].mcr |= MPC5200_I2C_MCR_RSTA; \ |
---|
75 | } while (0) |
---|
76 | |
---|
77 | /* Send byte to the bus */ |
---|
78 | #define mpc5200mbus_send(bus,byte) \ |
---|
79 | do { \ |
---|
80 | mpc5200.i2c_regs[bus->bus_idx].mdr = (byte); \ |
---|
81 | } while (0) |
---|
82 | |
---|
83 | /* Set transmit mode */ |
---|
84 | #define mpc5200mbus_tx_mode(bus) \ |
---|
85 | do { \ |
---|
86 | mpc5200.i2c_regs[bus->bus_idx].mcr |= MPC5200_I2C_MCR_MTX; \ |
---|
87 | } while (0) |
---|
88 | |
---|
89 | /* Set receive mode */ |
---|
90 | #define mpc5200mbus_rx_mode(bus) \ |
---|
91 | do { \ |
---|
92 | mpc5200.i2c_regs[bus->bus_idx].mcr &= ~MPC5200_I2C_MCR_MTX; \ |
---|
93 | (void)mpc5200.i2c_regs[bus->bus_idx].mdr; \ |
---|
94 | } while (0) |
---|
95 | |
---|
96 | |
---|
97 | /* Transmit acknowledge when byte received */ |
---|
98 | #define mpc5200mbus_send_ack(bus) \ |
---|
99 | do { \ |
---|
100 | mpc5200.i2c_regs[bus->bus_idx].mcr &= ~MPC5200_I2C_MCR_TXAK; \ |
---|
101 | } while (0) |
---|
102 | |
---|
103 | /* DO NOT transmit acknowledge when byte received */ |
---|
104 | #define mpc5200mbus_send_nack(bus) \ |
---|
105 | do { \ |
---|
106 | mpc5200.i2c_regs[bus->bus_idx].mcr |= MPC5200_I2C_MCR_TXAK; \ |
---|
107 | } while (0) |
---|
108 | |
---|
109 | #define mpc5200mbus_error(bus,err_status) \ |
---|
110 | do { \ |
---|
111 | do { \ |
---|
112 | (bus)->cmsg->status = (err_status); \ |
---|
113 | (bus)->cmsg++; \ |
---|
114 | } while (((bus)->cmsg - (bus)->msg < (bus)->nmsg) && \ |
---|
115 | ((bus)->cmsg->flags & I2C_MSG_ERRSKIP)); \ |
---|
116 | bus->cmsg--; \ |
---|
117 | } while (0) |
---|
118 | |
---|
119 | /* mpc5200mbus_get_event -- |
---|
120 | * Read MBUS module status register, determine interrupt reason and |
---|
121 | * return appropriate event. |
---|
122 | * |
---|
123 | * PARAMETERS: |
---|
124 | * bus - pointer to MBUS module descriptor structure |
---|
125 | * |
---|
126 | * RETURNS: |
---|
127 | * event code |
---|
128 | */ |
---|
129 | static i2c_event |
---|
130 | mpc5200mbus_get_event(mpc5200mbus *bus) |
---|
131 | { |
---|
132 | i2c_event event; |
---|
133 | uint8_t status, control; |
---|
134 | rtems_interrupt_level level; |
---|
135 | rtems_interrupt_disable(level); |
---|
136 | status = mpc5200.i2c_regs[bus->bus_idx].msr; |
---|
137 | control = mpc5200.i2c_regs[bus->bus_idx].mcr; |
---|
138 | if (status & MPC5200_I2C_MSR_MIF) /* Interrupt occured */ |
---|
139 | { |
---|
140 | if (status & MPC5200_I2C_MSR_MAAS) |
---|
141 | { |
---|
142 | event = EVENT_SLAVE; |
---|
143 | mpc5200.i2c_regs[bus->bus_idx].mcr = control; /* To clear Addressed As Slave |
---|
144 | condition */ |
---|
145 | } |
---|
146 | else if (status & MPC5200_I2C_MSR_MAL) /* Arbitration lost */ |
---|
147 | { |
---|
148 | mpc5200.i2c_regs[bus->bus_idx].msr = status & ~MPC5200_I2C_MSR_MAL; |
---|
149 | event = EVENT_ARB_LOST; |
---|
150 | } |
---|
151 | else if (control & MPC5200_I2C_MCR_MTX) /* Trasmit mode */ |
---|
152 | { |
---|
153 | if (status & MPC5200_I2C_MSR_RXAK) |
---|
154 | event = EVENT_NACK; |
---|
155 | else |
---|
156 | event = EVENT_ACK; |
---|
157 | } |
---|
158 | else /* Received */ |
---|
159 | { |
---|
160 | event = EVENT_DATA_RECV; |
---|
161 | } |
---|
162 | |
---|
163 | /* Clear interrupt condition */ |
---|
164 | mpc5200.i2c_regs[bus->bus_idx].msr &= ~MPC5200_I2C_MSR_MIF; |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | event = EVENT_NONE; |
---|
169 | } |
---|
170 | rtems_interrupt_enable(level); |
---|
171 | return event; |
---|
172 | } |
---|
173 | |
---|
174 | static void |
---|
175 | mpc5200mbus_machine_error(mpc5200mbus *bus, i2c_event event) |
---|
176 | { |
---|
177 | return; |
---|
178 | } |
---|
179 | |
---|
180 | /* mpc5200mbus_machine -- |
---|
181 | * finite state machine for I2C bus protocol |
---|
182 | * |
---|
183 | * PARAMETERS: |
---|
184 | * bus - pointer to ColdFire MBUS descriptor structure |
---|
185 | * event - I2C event |
---|
186 | * |
---|
187 | * RETURNS: |
---|
188 | * none |
---|
189 | */ |
---|
190 | static void |
---|
191 | mpc5200mbus_machine(mpc5200mbus *bus, i2c_event event) |
---|
192 | { |
---|
193 | uint8_t b; |
---|
194 | switch (bus->state) |
---|
195 | { |
---|
196 | |
---|
197 | case STATE_UNINITIALIZED: |
---|
198 | /* this should never happen. */ |
---|
199 | mpc5200mbus_machine_error(bus, event); |
---|
200 | break; |
---|
201 | case STATE_IDLE: |
---|
202 | switch (event) |
---|
203 | { |
---|
204 | case EVENT_NEXTMSG: /* Start new message processing */ |
---|
205 | bus->cmsg++; |
---|
206 | /* FALLTHRU */ |
---|
207 | |
---|
208 | case EVENT_TRANSFER: /* Initiate new transfer */ |
---|
209 | if (bus->cmsg - bus->msg >= bus->nmsg) |
---|
210 | { |
---|
211 | mpc5200mbus_stop(bus); |
---|
212 | next_state(bus, STATE_IDLE); |
---|
213 | bus->msg = bus->cmsg = NULL; |
---|
214 | bus->nmsg = bus->byte = 0; |
---|
215 | bus->done(bus->done_arg_ptr); |
---|
216 | break; |
---|
217 | } |
---|
218 | |
---|
219 | /* Initiate START or REPEATED START condition on the bus */ |
---|
220 | if (event == EVENT_TRANSFER) |
---|
221 | { |
---|
222 | mpc5200mbus_start(bus); |
---|
223 | } |
---|
224 | else /* (event == EVENT_NEXTMSG) */ |
---|
225 | { |
---|
226 | mpc5200mbus_rstart(bus); |
---|
227 | } |
---|
228 | |
---|
229 | bus->byte = 0; |
---|
230 | mpc5200mbus_tx_mode(bus); |
---|
231 | |
---|
232 | /* Initiate slave address sending */ |
---|
233 | if (bus->cmsg->flags & I2C_MSG_ADDR_10) |
---|
234 | { |
---|
235 | i2c_address a = bus->cmsg->addr; |
---|
236 | b = 0xf0 | (((a >> 8) & 0x03) << 1); |
---|
237 | if (bus->cmsg->flags & I2C_MSG_WR) |
---|
238 | { |
---|
239 | mpc5200mbus_send(bus, b); |
---|
240 | next_state(bus, STATE_ADDR_1_W); |
---|
241 | } |
---|
242 | else |
---|
243 | { |
---|
244 | mpc5200mbus_send(bus, b | 1); |
---|
245 | next_state(bus, STATE_ADDR_1_R); |
---|
246 | } |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | b = (bus->cmsg->addr & ~0x01); |
---|
251 | |
---|
252 | if (bus->cmsg->flags & I2C_MSG_WR) |
---|
253 | { |
---|
254 | next_state(bus, STATE_SENDING); |
---|
255 | } |
---|
256 | else |
---|
257 | { |
---|
258 | next_state(bus, STATE_ADDR_7); |
---|
259 | b |= 1; |
---|
260 | } |
---|
261 | |
---|
262 | mpc5200mbus_send(bus, b); |
---|
263 | } |
---|
264 | break; |
---|
265 | |
---|
266 | default: |
---|
267 | mpc5200mbus_machine_error(bus, event); |
---|
268 | break; |
---|
269 | } |
---|
270 | break; |
---|
271 | |
---|
272 | case STATE_ADDR_7: |
---|
273 | switch (event) |
---|
274 | { |
---|
275 | case EVENT_ACK: |
---|
276 | mpc5200mbus_rx_mode(bus); |
---|
277 | if (bus->cmsg->len <= 1) |
---|
278 | mpc5200mbus_send_nack(bus); |
---|
279 | else |
---|
280 | mpc5200mbus_send_ack(bus); |
---|
281 | next_state(bus, STATE_RECEIVING); |
---|
282 | break; |
---|
283 | |
---|
284 | case EVENT_NACK: |
---|
285 | mpc5200mbus_error(bus, I2C_NO_DEVICE); |
---|
286 | next_state(bus, STATE_IDLE); |
---|
287 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
288 | break; |
---|
289 | |
---|
290 | case EVENT_ARB_LOST: |
---|
291 | mpc5200mbus_error(bus, I2C_ARBITRATION_LOST); |
---|
292 | next_state(bus, STATE_IDLE); |
---|
293 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
294 | break; |
---|
295 | |
---|
296 | default: |
---|
297 | mpc5200mbus_machine_error(bus, event); |
---|
298 | break; |
---|
299 | } |
---|
300 | break; |
---|
301 | |
---|
302 | case STATE_ADDR_1_R: |
---|
303 | case STATE_ADDR_1_W: |
---|
304 | switch (event) |
---|
305 | { |
---|
306 | case EVENT_ACK: |
---|
307 | { |
---|
308 | uint8_t b = (bus->cmsg->addr & 0xff); |
---|
309 | mpc5200mbus_send(bus, b); |
---|
310 | if (bus->state == STATE_ADDR_1_W) |
---|
311 | { |
---|
312 | next_state(bus, STATE_SENDING); |
---|
313 | } |
---|
314 | else |
---|
315 | { |
---|
316 | i2c_address a; |
---|
317 | mpc5200mbus_rstart(bus); |
---|
318 | mpc5200mbus_tx_mode(bus); |
---|
319 | a = bus->cmsg->addr; |
---|
320 | b = 0xf0 | (((a >> 8) & 0x03) << 1) | 1; |
---|
321 | mpc5200mbus_send(bus, b); |
---|
322 | next_state(bus, STATE_ADDR_7); |
---|
323 | } |
---|
324 | break; |
---|
325 | } |
---|
326 | |
---|
327 | case EVENT_NACK: |
---|
328 | mpc5200mbus_error(bus, I2C_NO_DEVICE); |
---|
329 | next_state(bus, STATE_IDLE); |
---|
330 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
331 | break; |
---|
332 | |
---|
333 | case EVENT_ARB_LOST: |
---|
334 | mpc5200mbus_error(bus, I2C_ARBITRATION_LOST); |
---|
335 | next_state(bus, STATE_IDLE); |
---|
336 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
337 | break; |
---|
338 | |
---|
339 | default: |
---|
340 | mpc5200mbus_machine_error(bus, event); |
---|
341 | break; |
---|
342 | } |
---|
343 | break; |
---|
344 | |
---|
345 | case STATE_SENDING: |
---|
346 | switch (event) |
---|
347 | { |
---|
348 | case EVENT_ACK: |
---|
349 | if (bus->byte == bus->cmsg->len) |
---|
350 | { |
---|
351 | next_state(bus, STATE_IDLE); |
---|
352 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
353 | } |
---|
354 | else |
---|
355 | { |
---|
356 | mpc5200mbus_send(bus, bus->cmsg->buf[bus->byte++]); |
---|
357 | next_state(bus, STATE_SENDING); |
---|
358 | } |
---|
359 | break; |
---|
360 | |
---|
361 | case EVENT_NACK: |
---|
362 | if (bus->byte == 0) |
---|
363 | { |
---|
364 | mpc5200mbus_error(bus, I2C_NO_DEVICE); |
---|
365 | } |
---|
366 | else |
---|
367 | { |
---|
368 | mpc5200mbus_error(bus, I2C_NO_ACKNOWLEDGE); |
---|
369 | } |
---|
370 | next_state(bus, STATE_IDLE); |
---|
371 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
372 | break; |
---|
373 | |
---|
374 | case EVENT_ARB_LOST: |
---|
375 | mpc5200mbus_error(bus, I2C_ARBITRATION_LOST); |
---|
376 | next_state(bus, STATE_IDLE); |
---|
377 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
378 | break; |
---|
379 | |
---|
380 | default: |
---|
381 | mpc5200mbus_machine_error(bus, event); |
---|
382 | break; |
---|
383 | |
---|
384 | } |
---|
385 | break; |
---|
386 | |
---|
387 | case STATE_RECEIVING: |
---|
388 | switch (event) |
---|
389 | { |
---|
390 | case EVENT_DATA_RECV: |
---|
391 | if (bus->cmsg->len - bus->byte <= 2) |
---|
392 | { |
---|
393 | mpc5200mbus_send_nack(bus); |
---|
394 | if (bus->cmsg->len - bus->byte <= 1) |
---|
395 | { |
---|
396 | if (bus->cmsg - bus->msg + 1 == bus->nmsg) |
---|
397 | mpc5200mbus_stop(bus); |
---|
398 | else |
---|
399 | mpc5200mbus_rstart(bus); |
---|
400 | } |
---|
401 | } |
---|
402 | else |
---|
403 | { |
---|
404 | mpc5200mbus_send_ack(bus); |
---|
405 | } |
---|
406 | bus->cmsg->buf[bus->byte++] = mpc5200.i2c_regs[bus->bus_idx].mdr; |
---|
407 | if (bus->cmsg->len == bus->byte) |
---|
408 | { |
---|
409 | next_state(bus,STATE_IDLE); |
---|
410 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
411 | } |
---|
412 | else |
---|
413 | { |
---|
414 | next_state(bus,STATE_RECEIVING); |
---|
415 | } |
---|
416 | break; |
---|
417 | |
---|
418 | case EVENT_ARB_LOST: |
---|
419 | mpc5200mbus_error(bus, I2C_ARBITRATION_LOST); |
---|
420 | next_state(bus, STATE_IDLE); |
---|
421 | mpc5200mbus_machine(bus, EVENT_NEXTMSG); |
---|
422 | break; |
---|
423 | |
---|
424 | default: |
---|
425 | mpc5200mbus_machine_error(bus, event); |
---|
426 | break; |
---|
427 | } |
---|
428 | break; |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | /* mpc5200mbus_interrupt_handler -- |
---|
433 | * MBUS module interrupt handler routine |
---|
434 | * |
---|
435 | * PARAMETERS: |
---|
436 | * handle: pointer to mbus structure |
---|
437 | * |
---|
438 | * RETURNS: |
---|
439 | * none |
---|
440 | */ |
---|
441 | static void mpc5200mbus_interrupt_handler(rtems_irq_hdl_param handle) |
---|
442 | { |
---|
443 | i2c_event event; |
---|
444 | mpc5200mbus *bus = handle; |
---|
445 | |
---|
446 | event = mpc5200mbus_get_event(bus); |
---|
447 | /* |
---|
448 | * clear interrupt bit |
---|
449 | */ |
---|
450 | mpc5200.i2c_regs[bus->bus_idx].msr &= ~MPC5200_I2C_MSR_MIF; |
---|
451 | |
---|
452 | mpc5200mbus_machine(bus, event); |
---|
453 | } |
---|
454 | |
---|
455 | /* |
---|
456 | * mpc5200_mbus_irq_enable |
---|
457 | * enable irq for mbus |
---|
458 | */ |
---|
459 | static void mpc5200mbus_irq_enable(const rtems_irq_connect_data* ptr) |
---|
460 | { |
---|
461 | int minor = ((mpc5200mbus*)(ptr->handle))->bus_idx; |
---|
462 | |
---|
463 | mpc5200.i2c_regs[minor].mcr |= MPC5200_I2C_MCR_MIEN; |
---|
464 | } |
---|
465 | |
---|
466 | /* |
---|
467 | * mpc5200_mbus_irq_disable |
---|
468 | * enable irq for mbus |
---|
469 | */ |
---|
470 | static void mpc5200mbus_irq_disable(const rtems_irq_connect_data* ptr) |
---|
471 | { |
---|
472 | int minor = ((mpc5200mbus*)(ptr->handle))->bus_idx; |
---|
473 | |
---|
474 | mpc5200.i2c_regs[minor].mcr &= ~MPC5200_I2C_MCR_MIEN; |
---|
475 | } |
---|
476 | |
---|
477 | /* |
---|
478 | * mpc5200_mbus_isOn |
---|
479 | * check, whether irq is enabled |
---|
480 | */ |
---|
481 | static int mpc5200mbus_irq_isOn(const rtems_irq_connect_data* ptr) |
---|
482 | { |
---|
483 | int minor = ((mpc5200mbus*)(ptr->handle))->bus_idx; |
---|
484 | |
---|
485 | return (0 != (mpc5200.i2c_regs[minor].mcr & MPC5200_I2C_MCR_MIEN)); |
---|
486 | } |
---|
487 | |
---|
488 | /* mpc5200mbus_poll -- |
---|
489 | * MBUS module poll routine; used to poll events when I2C driver |
---|
490 | * operates in poll-driven mode. |
---|
491 | * |
---|
492 | * PARAMETERS: |
---|
493 | * none |
---|
494 | * |
---|
495 | * RETURNS: |
---|
496 | * none |
---|
497 | */ |
---|
498 | void |
---|
499 | mpc5200mbus_poll(mpc5200mbus *bus) |
---|
500 | { |
---|
501 | i2c_event event; |
---|
502 | event = mpc5200mbus_get_event(bus); |
---|
503 | if (event != EVENT_NONE) |
---|
504 | mpc5200mbus_machine(bus, event); |
---|
505 | } |
---|
506 | |
---|
507 | /* mpc5200mbus_select_clock_divider -- |
---|
508 | * Select divider for system clock which is used for I2C bus clock |
---|
509 | * generation. Not each divider can be selected for I2C bus; this |
---|
510 | * function select nearest larger or equal divider. |
---|
511 | * |
---|
512 | * PARAMETERS: |
---|
513 | * i2c_bus - pointer to the bus descriptor structure |
---|
514 | * divider - system frequency divider for I2C serial clock. |
---|
515 | * RETURNS: |
---|
516 | * RTEMS_SUCCESSFUL, if operation performed successfully, or |
---|
517 | * RTEMS error code when failed. |
---|
518 | */ |
---|
519 | rtems_status_code |
---|
520 | mpc5200mbus_select_clock_divider(mpc5200mbus *bus, int divider) |
---|
521 | { |
---|
522 | int i; |
---|
523 | int mbc; |
---|
524 | struct { |
---|
525 | int divider; |
---|
526 | int mbc; |
---|
527 | } dividers[] ={ |
---|
528 | { 20, 0x20 }, { 22, 0x21 }, { 24, 0x22 }, { 26, 0x23 }, |
---|
529 | { 28, 0x00 }, { 30, 0x01 }, { 32, 0x25 }, { 34, 0x02 }, |
---|
530 | { 36, 0x26 }, { 40, 0x03 }, { 44, 0x04 }, { 48, 0x05 }, |
---|
531 | { 56, 0x06 }, { 64, 0x2a }, { 68, 0x07 }, { 72, 0x2B }, |
---|
532 | { 80, 0x08 }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A }, |
---|
533 | { 112, 0x2E }, { 128, 0x0B }, { 144, 0x0C }, { 160, 0x0D }, |
---|
534 | { 192, 0x0E }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 }, |
---|
535 | { 288, 0x10 }, { 320, 0x11 }, { 384, 0x12 }, { 448, 0x36 }, |
---|
536 | { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x15 }, |
---|
537 | { 768, 0x16 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B }, |
---|
538 | { 1152, 0x18 }, { 1280, 0x19 }, { 1536, 0x1A }, { 1792, 0x3E }, |
---|
539 | { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D }, |
---|
540 | { 3072, 0x1E }, { 3840, 0x1F } |
---|
541 | }; |
---|
542 | |
---|
543 | if (bus == NULL) |
---|
544 | return RTEMS_INVALID_ADDRESS; |
---|
545 | |
---|
546 | for (i = 0, mbc = -1; i < sizeof(dividers)/sizeof(dividers[0]); i++) |
---|
547 | { |
---|
548 | mbc = dividers[i].mbc; |
---|
549 | if (dividers[i].divider >= divider) |
---|
550 | { |
---|
551 | break; |
---|
552 | } |
---|
553 | } |
---|
554 | mpc5200.i2c_regs[bus->bus_idx].mfdr = mbc; |
---|
555 | return RTEMS_SUCCESSFUL; |
---|
556 | } |
---|
557 | |
---|
558 | /* mpc5200mbus_initialize -- |
---|
559 | * Initialize MPC5200 MBUS I2C bus controller. |
---|
560 | * |
---|
561 | * PARAMETERS: |
---|
562 | * i2c_bus - pointer to the bus descriptor structure |
---|
563 | * |
---|
564 | * RETURNS: |
---|
565 | * RTEMS_SUCCESSFUL, or RTEMS error code when initialization failed. |
---|
566 | */ |
---|
567 | rtems_status_code |
---|
568 | mpc5200mbus_initialize(mpc5200mbus *i2c_bus) |
---|
569 | { |
---|
570 | rtems_interrupt_level level; |
---|
571 | rtems_status_code sc; |
---|
572 | rtems_irq_connect_data mbusIrqData; |
---|
573 | |
---|
574 | if (i2c_bus == NULL) |
---|
575 | return RTEMS_INVALID_ADDRESS; |
---|
576 | |
---|
577 | if (i2c_bus->state != STATE_UNINITIALIZED) /* Check if already initialized */ |
---|
578 | return RTEMS_RESOURCE_IN_USE; |
---|
579 | |
---|
580 | i2c_bus->state = STATE_IDLE; |
---|
581 | i2c_bus->msg = NULL; |
---|
582 | i2c_bus->cmsg = NULL; |
---|
583 | i2c_bus->nmsg = 0; |
---|
584 | i2c_bus->byte = 0; |
---|
585 | |
---|
586 | /* |
---|
587 | * install interrupt handler |
---|
588 | */ |
---|
589 | mbusIrqData.on = mpc5200mbus_irq_enable; |
---|
590 | mbusIrqData.off = mpc5200mbus_irq_disable; |
---|
591 | mbusIrqData.isOn = mpc5200mbus_irq_isOn; |
---|
592 | mbusIrqData.handle = (rtems_irq_hdl_param)i2c_bus; |
---|
593 | mbusIrqData.hdl = mpc5200mbus_interrupt_handler; |
---|
594 | switch(i2c_bus->bus_idx) { |
---|
595 | case 0: |
---|
596 | mbusIrqData.name = BSP_SIU_IRQ_I2C1; |
---|
597 | break; |
---|
598 | case 1: |
---|
599 | mbusIrqData.name = BSP_SIU_IRQ_I2C2; |
---|
600 | break; |
---|
601 | } |
---|
602 | if (!BSP_install_rtems_irq_handler (&mbusIrqData)) { |
---|
603 | sc = RTEMS_UNSATISFIED; |
---|
604 | return sc; |
---|
605 | } |
---|
606 | |
---|
607 | rtems_interrupt_disable(level); |
---|
608 | |
---|
609 | mpc5200.i2c_regs[i2c_bus->bus_idx].mcr &= ~MPC5200_I2C_MCR_MEN; |
---|
610 | mpc5200.i2c_regs[i2c_bus->bus_idx].msr = 0; |
---|
611 | mpc5200.i2c_regs[i2c_bus->bus_idx].mdr = 0x1F; /* Maximum possible divider is 3840 */ |
---|
612 | mpc5200.i2c_regs[i2c_bus->bus_idx].mcr |= MPC5200_I2C_MCR_MEN; |
---|
613 | |
---|
614 | rtems_interrupt_enable(level); |
---|
615 | |
---|
616 | return RTEMS_SUCCESSFUL; |
---|
617 | } |
---|
618 | |
---|
619 | /* mpc5200mbus_i2c_transfer -- |
---|
620 | * Initiate multiple-messages transfer over I2C bus via ColdFire MBUS |
---|
621 | * controller. |
---|
622 | * |
---|
623 | * PARAMETERS: |
---|
624 | * bus - pointer to MBUS controller descriptor |
---|
625 | * nmsg - number of messages |
---|
626 | * msg - pointer to messages array |
---|
627 | * done - function which is called when transfer is finished |
---|
628 | * done_arg_ptr - arbitrary argument pointer passed to done funciton |
---|
629 | * |
---|
630 | * RETURNS: |
---|
631 | * RTEMS_SUCCESSFUL if transfer initiated successfully, or error |
---|
632 | * code when failed. |
---|
633 | */ |
---|
634 | rtems_status_code |
---|
635 | mpc5200mbus_i2c_transfer(mpc5200mbus *bus, int nmsg, i2c_message *msg, |
---|
636 | i2c_transfer_done done, void *done_arg_ptr) |
---|
637 | { |
---|
638 | if (bus->state == STATE_UNINITIALIZED) |
---|
639 | return RTEMS_NOT_CONFIGURED; |
---|
640 | |
---|
641 | bus->done = done; |
---|
642 | bus->done_arg_ptr = done_arg_ptr; |
---|
643 | bus->cmsg = bus->msg = msg; |
---|
644 | bus->nmsg = nmsg; |
---|
645 | bus->byte = 0; |
---|
646 | bus->state = STATE_IDLE; |
---|
647 | mpc5200mbus_machine(bus, EVENT_TRANSFER); |
---|
648 | return RTEMS_SUCCESSFUL; |
---|
649 | } |
---|
650 | |
---|
651 | |
---|
652 | /* mpc5200mbus_i2c_done -- |
---|
653 | * Close ColdFire MBUS I2C bus controller and release all resources. |
---|
654 | * |
---|
655 | * PARAMETERS: |
---|
656 | * bus - pointer to MBUS controller descriptor |
---|
657 | * |
---|
658 | * RETURNS: |
---|
659 | * RTEMS_SUCCESSFUL, if transfer initiated successfully, or error |
---|
660 | * code when failed. |
---|
661 | */ |
---|
662 | rtems_status_code |
---|
663 | mpc5200mbus_i2c_done(mpc5200mbus *i2c_bus) |
---|
664 | { |
---|
665 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
666 | |
---|
667 | if (i2c_bus->state == STATE_UNINITIALIZED) |
---|
668 | return RTEMS_NOT_CONFIGURED; |
---|
669 | |
---|
670 | mpc5200.i2c_regs[i2c_bus->bus_idx].mcr = 0; |
---|
671 | |
---|
672 | return sc; |
---|
673 | } |
---|