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