diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2018-04-23 09:45:28 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2018-04-23 15:18:43 +0200 |
commit | a2dad96ab736f66ed54421cad53caf31f250e181 (patch) | |
tree | 1935320b5b52276ed2e52741cdae71637f209f77 /c/src/lib/libbsp/powerpc | |
parent | bsps/arm: Remove unused stm32f* files (diff) | |
download | rtems-a2dad96ab736f66ed54421cad53caf31f250e181.tar.bz2 |
bsps: Move I2C drivers to bsps
This patch is a part of the BSP source reorganization.
Update #3285.
Diffstat (limited to 'c/src/lib/libbsp/powerpc')
-rw-r--r-- | c/src/lib/libbsp/powerpc/gen5200/Makefile.am | 6 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/gen5200/i2c/i2c.c | 320 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/gen5200/i2c/i2cdrv.c | 287 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/gen5200/i2c/mpc5200mbus.c | 673 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/gen5200/i2c/mpc5200mbus.h | 150 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/gen83xx/Makefile.am | 2 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/gen83xx/i2c/i2c_init.c | 152 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/mpc55xxevb/Makefile.am | 2 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/mpc55xxevb/i2c/i2c_init.c | 75 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/mvme3100/Makefile.am | 4 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/mvme3100/i2c/i2c_init.c | 186 | ||||
-rw-r--r-- | c/src/lib/libbsp/powerpc/mvme3100/i2c/mpc8540_i2c.c | 453 |
12 files changed, 7 insertions, 2303 deletions
diff --git a/c/src/lib/libbsp/powerpc/gen5200/Makefile.am b/c/src/lib/libbsp/powerpc/gen5200/Makefile.am index 17d49bb96a..afa2c53b00 100644 --- a/c/src/lib/libbsp/powerpc/gen5200/Makefile.am +++ b/c/src/lib/libbsp/powerpc/gen5200/Makefile.am @@ -58,9 +58,9 @@ librtemsbsp_a_SOURCES +=../../../../../../bsps/powerpc/shared/clock/clock.c # console librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/gen5200/console/console.c # i2c -librtemsbsp_a_SOURCES += i2c/i2c.c -librtemsbsp_a_SOURCES += i2c/i2cdrv.c -librtemsbsp_a_SOURCES += i2c/mpc5200mbus.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/gen5200/i2c/i2c.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/gen5200/i2c/i2cdrv.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/gen5200/i2c/mpc5200mbus.c # ide librtemsbsp_a_SOURCES += ide/idecfg.c librtemsbsp_a_SOURCES += ide/pcmcia_ide.c diff --git a/c/src/lib/libbsp/powerpc/gen5200/i2c/i2c.c b/c/src/lib/libbsp/powerpc/gen5200/i2c/i2c.c deleted file mode 100644 index 0b5adeb2a9..0000000000 --- a/c/src/lib/libbsp/powerpc/gen5200/i2c/i2c.c +++ /dev/null @@ -1,320 +0,0 @@ -/* I2C bus common (driver-independent) primitives implementation. - * - * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia - * Author: Victor V. Vengerov <vvv@oktet.ru> - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * - * http://www.rtems.org/license/LICENSE. - */ - -#include <bsp.h> -#include <bsp/i2c.h> -#include <rtems/score/sysstate.h> - -/* i2c_transfer_sema_done_func -- - * This function called from I2C driver layer to signal that I2C - * transfer is finished. This function resumes of task execution which - * has invoked blocking I2C primitive. - * - * PARAMETERS: - * arg - done function argument; it is RTEMS semaphore ID. - */ -static void -i2c_transfer_sema_done_func(void * arg) -{ - rtems_id sema = *(rtems_id *)arg; - rtems_semaphore_release(sema); -} - -/* i2c_transfer_poll_done_func -- - * This function called from I2C driver layer to signal that I2C - * transfer is finished. This function set the flag polled by waiting - * function. - * - * PARAMETERS: - * arg - done function argument; address of poll_done_flag - */ -static void -i2c_transfer_poll_done_func(void *arg) -{ - bool *poll_done_flag = (bool *)arg; - *poll_done_flag = true; -} - -/* i2c_transfer_wait_sema -- - * Initiate I2C bus transfer and block on temporary created semaphore - * until this transfer will be finished. - * - * PARAMETERS: - * bus - I2C bus number - * msg - pointer to transfer messages array - * nmsg - number of messages in transfer - * - * RETURNS: - * RTEMS_SUCCESSFUL, if tranfer finished successfully, - * or RTEMS status code if semaphore operations has failed. - */ -static i2c_message_status -i2c_transfer_wait_sema(i2c_bus_number bus, i2c_message *msg, int nmsg) -{ - rtems_status_code sc; - rtems_id sema; - sc = rtems_semaphore_create( - rtems_build_name('I', '2', 'C', 'S'), - 0, - RTEMS_COUNTING_SEMAPHORE | RTEMS_NO_INHERIT_PRIORITY | - RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL, - 0, - &sema - ); - if (sc != RTEMS_SUCCESSFUL) - return I2C_RESOURCE_NOT_AVAILABLE; - sc = i2c_transfer(bus, nmsg, msg, - i2c_transfer_sema_done_func, &sema); - if (sc != RTEMS_SUCCESSFUL) - { - rtems_semaphore_delete(sema); - return sc; - } - rtems_semaphore_obtain(sema, RTEMS_WAIT, RTEMS_NO_TIMEOUT); - sc = rtems_semaphore_delete(sema); - return sc; -} - -/* i2c_transfer_wait_poll -- - * Initiate I2C bus transfer and wait by poll transaction done flag until - * this transfer will be finished. - * - * PARAMETERS: - * bus - I2C bus number - * msg - pointer to transfer messages array - * nmsg - number of messages in transfer - * - * RETURNS: - * RTEMS_SUCCESSFUL - */ -static rtems_status_code -i2c_transfer_wait_poll(i2c_bus_number bus, i2c_message *msg, int nmsg) -{ - /* - * this looks nasty, but is correct: - * we wait in this function, until the poll_done_flag is - * set deep inside the i2c_poll() function - */ - volatile bool poll_done_flag; - rtems_status_code sc; - poll_done_flag = false; - sc = i2c_transfer(bus, nmsg, msg, - i2c_transfer_poll_done_func,(void *)&poll_done_flag); - if (sc != RTEMS_SUCCESSFUL) - return sc; - while (poll_done_flag == false) - { - i2c_poll(bus); - } - return RTEMS_SUCCESSFUL; -} - -/* i2c_transfer_wait -- - * Initiate I2C bus transfer and block until this transfer will be - * finished. This function wait the semaphore if system in - * SYSTEM_STATE_UP state, or poll done flag in other states. - * - * PARAMETERS: - * bus - I2C bus number - * msg - pointer to transfer messages array - * nmsg - number of messages in transfer - * - * RETURNS: - * I2C_SUCCESSFUL, if tranfer finished successfully, - * I2C_RESOURCE_NOT_AVAILABLE, if semaphore operations has failed, - * value of status field of first error-finished message in transfer, - * if something wrong. - */ -i2c_message_status -i2c_transfer_wait(i2c_bus_number bus, i2c_message *msg, int nmsg) -{ - rtems_status_code sc; - int i; - if (_System_state_Is_up(_System_state_Get())) - { - sc = i2c_transfer_wait_sema(bus, msg, nmsg); - } - else - { - sc = i2c_transfer_wait_poll(bus, msg, nmsg); - } - - if (sc != RTEMS_SUCCESSFUL) - return I2C_RESOURCE_NOT_AVAILABLE; - - for (i = 0; i < nmsg; i++) - { - if (msg[i].status != I2C_SUCCESSFUL) - { - return msg[i].status; - } - } - return I2C_SUCCESSFUL; -} - -/* i2c_write -- - * Send single message over specified I2C bus to addressed device and - * wait while transfer is finished. - * - * PARAMETERS: - * bus - I2C bus number - * addr - address of I2C device - * buf - data to be sent to device - * size - data buffer size - * - * RETURNS: - * transfer status - */ -i2c_message_status -i2c_write(i2c_bus_number bus, i2c_address addr, void *buf, int size) -{ - i2c_message msg; - msg.addr = addr; - msg.flags = I2C_MSG_WR; - if (addr > 0xff) - msg.flags |= I2C_MSG_ADDR_10; - msg.status = 0; - msg.len = size; - msg.buf = buf; - return i2c_transfer_wait(bus, &msg, 1); -} - -/* i2c_wrbyte -- - * Send single one-byte long message over specified I2C bus to - * addressed device and wait while transfer is finished. - * - * PARAMETERS: - * bus - I2C bus number - * addr - address of I2C device - * cmd - byte message to be sent to device - * - * RETURNS: - * transfer status - */ -i2c_message_status -i2c_wrbyte(i2c_bus_number bus, i2c_address addr, uint8_t cmd) -{ - i2c_message msg; - uint8_t data = cmd; - msg.addr = addr; - msg.flags = I2C_MSG_WR; - if (addr > 0xff) - msg.flags |= I2C_MSG_ADDR_10; - msg.status = 0; - msg.len = sizeof(data); - msg.buf = &data; - return i2c_transfer_wait(bus, &msg, 1); -} - -/* i2c_read -- - * receive single message over specified I2C bus from addressed device. - * This call will wait while transfer is finished. - * - * PARAMETERS: - * bus - I2C bus number - * addr - address of I2C device - * buf - buffer for received message - * size - receive buffer size - * - * RETURNS: - * transfer status - */ -i2c_message_status -i2c_read(i2c_bus_number bus, i2c_address addr, void *buf, int size) -{ - i2c_message msg; - msg.addr = addr; - msg.flags = 0; - if (addr > 0xff) - msg.flags |= I2C_MSG_ADDR_10; - msg.status = 0; - msg.len = size; - msg.buf = buf; - return i2c_transfer_wait(bus, &msg, 1); -} - -/* i2c_wrrd -- - * Send message over I2C bus to specified device and receive message - * from the same device during single transfer. - * - * PARAMETERS: - * bus - I2C bus number - * addr - address of I2C device - * bufw - data to be sent to device - * sizew - send data buffer size - * bufr - buffer for received message - * sizer - receive buffer size - * - * RETURNS: - * transfer status - */ -i2c_message_status -i2c_wrrd(i2c_bus_number bus, i2c_address addr, void *bufw, int sizew, - void *bufr, int sizer) -{ - i2c_message msg[2]; - msg[0].addr = addr; - msg[0].flags = I2C_MSG_WR | I2C_MSG_ERRSKIP; - if (addr > 0xff) - msg[0].flags |= I2C_MSG_ADDR_10; - msg[0].status = 0; - msg[0].len = sizew; - msg[0].buf = bufw; - - msg[1].addr = addr; - msg[1].flags = 0; - if (addr > 0xff) - msg[1].flags |= I2C_MSG_ADDR_10; - msg[1].status = 0; - msg[1].len = sizer; - msg[1].buf = bufr; - - return i2c_transfer_wait(bus, msg, 2); -} - -/* i2c_wbrd -- - * Send one-byte message over I2C bus to specified device and receive - * message from the same device during single transfer. - * - * PARAMETERS: - * bus - I2C bus number - * addr - address of I2C device - * cmd - one-byte message to be sent over I2C bus - * bufr - buffer for received message - * sizer - receive buffer size - * - * RETURNS: - * transfer status - */ -i2c_message_status -i2c_wbrd(i2c_bus_number bus, i2c_address addr, uint8_t cmd, - void *bufr, int sizer) -{ - i2c_message msg[2]; - uint8_t bufw = cmd; - msg[0].addr = addr; - msg[0].flags = I2C_MSG_WR | I2C_MSG_ERRSKIP; - if (addr > 0xff) - msg[0].flags |= I2C_MSG_ADDR_10; - msg[0].status = 0; - msg[0].len = sizeof(bufw); - msg[0].buf = &bufw; - - msg[1].addr = addr; - msg[1].flags = I2C_MSG_ERRSKIP; - if (addr > 0xff) - msg[1].flags |= I2C_MSG_ADDR_10; - msg[1].status = 0; - msg[1].len = sizer; - msg[1].buf = bufr; - - return i2c_transfer_wait(bus, msg, 2); -} diff --git a/c/src/lib/libbsp/powerpc/gen5200/i2c/i2cdrv.c b/c/src/lib/libbsp/powerpc/gen5200/i2c/i2cdrv.c deleted file mode 100644 index 2c20b3dbdc..0000000000 --- a/c/src/lib/libbsp/powerpc/gen5200/i2c/i2cdrv.c +++ /dev/null @@ -1,287 +0,0 @@ -/*===============================================================*\ -| Project: RTEMS generic MPC5200 BSP | -+-----------------------------------------------------------------+ -| Copyright (c) 2005 | -| Embedded Brains GmbH | -| Obere Lagerstr. 30 | -| D-82178 Puchheim | -| Germany | -| rtems@embedded-brains.de | -+-----------------------------------------------------------------+ -| The license and distribution terms for this file may be | -| found in the file LICENSE in this distribution or at | -| | -| http://www.rtems.org/license/LICENSE. | -| | -+-----------------------------------------------------------------+ -| I2C driver for MPC5200 | -+-----------------------------------------------------------------+ -| This file has been adapted from an existing source code file, | -| see the original file header below for reference | -\*===============================================================*/ - -/* I2C driver for MCF5206eLITE board. I2C bus accessed through on-chip - * MCF5206e MBUS controller. - * - * The purpose of this module is to perform I2C driver initialization - * and serialize I2C transfers. - * - * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia - * Author: Victor V. Vengerov <vvv@oktet.ru> - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * - * http://www.rtems.org/license/LICENSE. - */ - -#include <bsp.h> -#include <bsp/i2c.h> -#include <bsp/i2cdrv.h> -#include <stdlib.h> -#include <string.h> - -#include "mpc5200mbus.h" - -#ifndef I2C_NUMBER_OF_BUSES -#define I2C_NUMBER_OF_BUSES (2) -#endif - -#ifndef I2C_SELECT_BUS -#define I2C_SELECT_BUS(bus) -#endif - -/* - * Few I2C transfers may be posted simultaneously, but MBUS driver is able - * to process it one-by-one. To serialize transfers, function i2c_transfer - * put transfer information to the queue and initiate new transfers if MBUS - * driver is not busy. When driver is busy, next transfer is dequeued - * when current active transfer is finished. - */ - -/* - * i2c_qel - I2C transfers queue element; contain information about - * delayed transfer - */ -typedef struct i2c_qel { - i2c_bus_number bus; /* I2C bus number */ - i2c_message *msg; /* pointer to the transfer' messages array */ - int nmsg; /* number of messages in transfer */ - i2c_transfer_done done; /* transfer done callback function */ - void * done_arg_ptr; /* arbitrary arg pointer to done callback */ -} i2c_qel; - -/* Memory for I2C transfer queue. This queue represented like a ring buffer */ -static i2c_qel *tqueue; - -/* Maximum number of elements in transfer queue */ -static int tqueue_size; - -/* Position of next free element in a ring buffer */ -static volatile int tqueue_head; - -/* Position of the first element in transfer queue */ -static volatile int tqueue_tail; - -/* MBus I2C bus controller busy flag */ -static volatile bool mbus_busy; - -/* MBus I2C bus controller descriptor */ -static mpc5200mbus mbus[I2C_NUMBER_OF_BUSES]; - -/* Clock rate selected for each of bus */ -static int i2cdrv_bus_clock_div[I2C_NUMBER_OF_BUSES]; - -/* Currently selected I2C bus clock rate */ -static int i2cdrv_bus_clock_div_current; - -/* Forward function declaration */ -static void i2cdrv_unload(void); - -/* i2cdrv_done -- - * Callback function which is called from MBus low-level driver when - * transfer is finished. - */ -static void -i2cdrv_done(void * arg_ptr) -{ - rtems_interrupt_level level; - i2c_qel *qel = tqueue + tqueue_tail; - qel->done(qel->done_arg_ptr); - rtems_interrupt_disable(level); - tqueue_tail = (tqueue_tail + 1) % tqueue_size; - mbus_busy = false; - rtems_interrupt_enable(level); - i2cdrv_unload(); -} - -/* i2cdrv_unload -- - * If MBUS controller is not busy and transfer waiting in a queue, - * initiate processing of the next transfer in queue. - */ -static void -i2cdrv_unload(void) -{ - rtems_interrupt_level level; - i2c_qel *qel; - rtems_status_code sc; - rtems_interrupt_disable(level); - if (!mbus_busy && (tqueue_head != tqueue_tail)) - { - mbus_busy = true; - rtems_interrupt_enable(level); - qel = tqueue + tqueue_tail; - - I2C_SELECT_BUS(qel->bus); - if (i2cdrv_bus_clock_div[qel->bus] != i2cdrv_bus_clock_div_current) - { - i2cdrv_bus_clock_div_current = i2cdrv_bus_clock_div[qel->bus]; - mpc5200mbus_select_clock_divider(&mbus[qel->bus], i2cdrv_bus_clock_div_current); - } - sc = mpc5200mbus_i2c_transfer(&mbus[qel->bus], qel->nmsg, qel->msg, - i2cdrv_done,qel); - if (sc != RTEMS_SUCCESSFUL) - { - int i; - for (i = 0; i < qel->nmsg; i++) - { - qel->msg[i].status = I2C_RESOURCE_NOT_AVAILABLE; - } - i2cdrv_done(qel); - } - } - else - { - rtems_interrupt_enable(level); - } -} - -/* i2c_transfer -- - * Initiate multiple-messages transfer over specified I2C bus or - * put request into queue if bus or some other resource is busy. (This - * is non-blocking function). - * - * PARAMETERS: - * bus - I2C bus number - * nmsg - number of messages - * msg - pointer to messages array - * done - function which is called when transfer is finished - * done_arg_ptr - arbitrary argument pointer passed to done funciton - * - * RETURNS: - * RTEMS_SUCCESSFUL if transfer initiated successfully, or error - * code if something failed. - */ -rtems_status_code -i2c_transfer(i2c_bus_number bus, int nmsg, i2c_message *msg, - i2c_transfer_done done, void * done_arg_ptr) -{ - i2c_qel qel; - rtems_interrupt_level level; - - if (bus >= I2C_NUMBER_OF_BUSES) - { - return RTEMS_INVALID_NUMBER; - } - - if (msg == NULL) - { - return RTEMS_INVALID_ADDRESS; - } - - qel.bus = bus; - qel.msg = msg; - qel.nmsg = nmsg; - qel.done = done; - qel.done_arg_ptr = done_arg_ptr; - rtems_interrupt_disable(level); - if ((tqueue_head + 1) % tqueue_size == tqueue_tail) - { - rtems_interrupt_enable(level); - return RTEMS_TOO_MANY; - } - memcpy(tqueue + tqueue_head, &qel, sizeof(qel)); - tqueue_head = (tqueue_head + 1) % tqueue_size; - rtems_interrupt_enable(level); - i2cdrv_unload(); - return RTEMS_SUCCESSFUL; -} - -/* i2cdrv_initialize -- - * I2C driver initialization (rtems I/O driver primitive) - */ -rtems_device_driver -i2cdrv_initialize(rtems_device_major_number major, - rtems_device_minor_number minor, - void *arg) -{ - int i; - rtems_status_code sc; - mbus_busy = false; - tqueue_tail = tqueue_head = 0; - tqueue_size = 32; - tqueue = calloc(tqueue_size, sizeof(i2c_qel)); - - for (i = 0; i < I2C_NUMBER_OF_BUSES; i++) - { - mbus[i].bus_idx = i; - sc = mpc5200mbus_initialize(&mbus[i]); - if (sc != RTEMS_SUCCESSFUL) - return sc; - } - - for (i = 0; i < I2C_NUMBER_OF_BUSES; i++) - { - sc = i2c_select_clock_rate(i, 100000); - if (sc != RTEMS_SUCCESSFUL) - return sc; - } - i2cdrv_bus_clock_div_current = -1; - return RTEMS_SUCCESSFUL; -} - -/* i2c_select_clock_rate -- - * select I2C bus clock rate for specified bus. Some bus controller do not - * allow to select arbitrary clock rate; in this case nearest possible - * slower clock rate is selected. - * - * PARAMETERS: - * bus - I2C bus number - * bps - data transfer rate for this bytes in bits per second - * - * RETURNS: - * RTEMS_SUCCESSFUL, if operation performed successfully, - * RTEMS_INVALID_NUMBER, if wrong bus number is specified, - * RTEMS_UNSATISFIED, if bus do not support data transfer rate selection - * or specified data transfer rate could not be used. - */ -rtems_status_code -i2c_select_clock_rate(i2c_bus_number bus, int bps) -{ - int div; - if (bus >= I2C_NUMBER_OF_BUSES) - return RTEMS_INVALID_NUMBER; - - if (bps == 0) - return RTEMS_UNSATISFIED; - - div = IPB_CLOCK / bps; - i2cdrv_bus_clock_div[bus] = div; - return RTEMS_SUCCESSFUL; -} - -/* i2c_poll -- - * Poll I2C bus controller for events and hanle it. This function is - * used when I2C driver operates in poll-driven mode. - * - * PARAMETERS: - * bus - bus number to be polled - * - * RETURNS: - * none - */ -void -i2c_poll(i2c_bus_number bus) -{ - mpc5200mbus_poll(&mbus[bus]); -} diff --git a/c/src/lib/libbsp/powerpc/gen5200/i2c/mpc5200mbus.c b/c/src/lib/libbsp/powerpc/gen5200/i2c/mpc5200mbus.c deleted file mode 100644 index aa926bc8d9..0000000000 --- a/c/src/lib/libbsp/powerpc/gen5200/i2c/mpc5200mbus.c +++ /dev/null @@ -1,673 +0,0 @@ -/*===============================================================*\ -| Project: RTEMS generic MPC5200 BSP | -+-----------------------------------------------------------------+ -| Copyright (c) 2005 | -| Embedded Brains GmbH | -| Obere Lagerstr. 30 | -| D-82178 Puchheim | -| Germany | -| rtems@embedded-brains.de | -+-----------------------------------------------------------------+ -| The license and distribution terms for this file may be | -| found in the file LICENSE in this distribution or at | -| | -| http://www.rtems.org/license/LICENSE. | -| | -+-----------------------------------------------------------------+ -| MBUS module (I2C bus) driver | -+-----------------------------------------------------------------+ -| This file has been adapted from an existing source code file, | -| see the original file header below for reference | -\*===============================================================*/ - -/* MCF5206e MBUS module (I2C bus) driver - * - * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia - * Author: Victor V. Vengerov <vvv@oktet.ru> - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * - * http://www.rtems.org/license/LICENSE. - */ -#include "../i2c/mpc5200mbus.h" -#include <bsp/mpc5200.h> -#include <bsp/irq.h> -#include <bsp/i2c.h> - -/* Events of I2C machine */ -typedef enum i2c_event { - EVENT_NONE, /* Spurious event */ - EVENT_TRANSFER, /* Start new transfer */ - EVENT_NEXTMSG, /* Start processing of next message in transfer */ - EVENT_ACK, /* Sending finished with ACK */ - EVENT_NACK, /* Sending finished with NACK */ - EVENT_TIMEOUT, /* Timeout occured */ - EVENT_DATA_RECV, /* Data received */ - EVENT_ARB_LOST, /* Arbitration lost */ - EVENT_SLAVE /* Addressed as a slave */ -} i2c_event; - -/*** Auxillary primitives ***/ - -/* Change state of finite state machine */ -#define next_state(bus,new_state) \ - do { \ - (bus)->state = (new_state); \ - } while (0) - -/* Initiate start condition on the I2C bus */ -#define mpc5200mbus_start(bus) \ - do { \ - mpc5200.i2c_regs[bus->bus_idx].mcr |= MPC5200_I2C_MCR_MSTA; \ - } while (0) - -/* Initiate stop condition on the I2C bus */ -#define mpc5200mbus_stop(bus) \ - do { \ - mpc5200.i2c_regs[bus->bus_idx].mcr &= ~MPC5200_I2C_MCR_MSTA; \ - } while (0) - -/* Initiate repeat start condition on the I2C bus */ -#define mpc5200mbus_rstart(bus) \ - do { \ - mpc5200.i2c_regs[bus->bus_idx].mcr |= MPC5200_I2C_MCR_RSTA; \ - } while (0) - -/* Send byte to the bus */ -#define mpc5200mbus_send(bus,byte) \ - do { \ - mpc5200.i2c_regs[bus->bus_idx].mdr = (byte); \ - } while (0) - -/* Set transmit mode */ -#define mpc5200mbus_tx_mode(bus) \ - do { \ - mpc5200.i2c_regs[bus->bus_idx].mcr |= MPC5200_I2C_MCR_MTX; \ - } while (0) - -/* Set receive mode */ -#define mpc5200mbus_rx_mode(bus) \ - do { \ - mpc5200.i2c_regs[bus->bus_idx].mcr &= ~MPC5200_I2C_MCR_MTX; \ - (void)mpc5200.i2c_regs[bus->bus_idx].mdr; \ - } while (0) - - -/* Transmit acknowledge when byte received */ -#define mpc5200mbus_send_ack(bus) \ - do { \ - mpc5200.i2c_regs[bus->bus_idx].mcr &= ~MPC5200_I2C_MCR_TXAK; \ - } while (0) - -/* DO NOT transmit acknowledge when byte received */ -#define mpc5200mbus_send_nack(bus) \ - do { \ - mpc5200.i2c_regs[bus->bus_idx].mcr |= MPC5200_I2C_MCR_TXAK; \ - } while (0) - -#define mpc5200mbus_error(bus,err_status) \ - do { \ - do { \ - (bus)->cmsg->status = (err_status); \ - (bus)->cmsg++; \ - } while (((bus)->cmsg - (bus)->msg < (bus)->nmsg) && \ - ((bus)->cmsg->flags & I2C_MSG_ERRSKIP)); \ - bus->cmsg--; \ - } while (0) - -/* mpc5200mbus_get_event -- - * Read MBUS module status register, determine interrupt reason and - * return appropriate event. - * - * PARAMETERS: - * bus - pointer to MBUS module descriptor structure - * - * RETURNS: - * event code - */ -static i2c_event -mpc5200mbus_get_event(mpc5200mbus *bus) -{ - i2c_event event; - uint8_t status, control; - rtems_interrupt_level level; - rtems_interrupt_disable(level); - status = mpc5200.i2c_regs[bus->bus_idx].msr; - control = mpc5200.i2c_regs[bus->bus_idx].mcr; - if (status & MPC5200_I2C_MSR_MIF) /* Interrupt occured */ - { - if (status & MPC5200_I2C_MSR_MAAS) - { - event = EVENT_SLAVE; - mpc5200.i2c_regs[bus->bus_idx].mcr = control; /* To clear Addressed As Slave - condition */ - } - else if (status & MPC5200_I2C_MSR_MAL) /* Arbitration lost */ - { - mpc5200.i2c_regs[bus->bus_idx].msr = status & ~MPC5200_I2C_MSR_MAL; - event = EVENT_ARB_LOST; - } - else if (control & MPC5200_I2C_MCR_MTX) /* Trasmit mode */ - { - if (status & MPC5200_I2C_MSR_RXAK) - event = EVENT_NACK; - else - event = EVENT_ACK; - } - else /* Received */ - { - event = EVENT_DATA_RECV; - } - - /* Clear interrupt condition */ - mpc5200.i2c_regs[bus->bus_idx].msr &= ~MPC5200_I2C_MSR_MIF; - } - else - { - event = EVENT_NONE; - } - rtems_interrupt_enable(level); - return event; -} - -static void -mpc5200mbus_machine_error(mpc5200mbus *bus, i2c_event event) -{ - return; -} - -/* mpc5200mbus_machine -- - * finite state machine for I2C bus protocol - * - * PARAMETERS: - * bus - pointer to ColdFire MBUS descriptor structure - * event - I2C event - * - * RETURNS: - * none - */ -static void -mpc5200mbus_machine(mpc5200mbus *bus, i2c_event event) -{ - uint8_t b; - switch (bus->state) - { - - case STATE_UNINITIALIZED: - /* this should never happen. */ - mpc5200mbus_machine_error(bus, event); - break; - case STATE_IDLE: - switch (event) - { - case EVENT_NEXTMSG: /* Start new message processing */ - bus->cmsg++; - /* FALLTHRU */ - - case EVENT_TRANSFER: /* Initiate new transfer */ - if (bus->cmsg - bus->msg >= bus->nmsg) - { - mpc5200mbus_stop(bus); - next_state(bus, STATE_IDLE); - bus->msg = bus->cmsg = NULL; - bus->nmsg = bus->byte = 0; - bus->done(bus->done_arg_ptr); - break; - } - - /* Initiate START or REPEATED START condition on the bus */ - if (event == EVENT_TRANSFER) - { - mpc5200mbus_start(bus); - } - else /* (event == EVENT_NEXTMSG) */ - { - mpc5200mbus_rstart(bus); - } - - bus->byte = 0; - mpc5200mbus_tx_mode(bus); - - /* Initiate slave address sending */ - if (bus->cmsg->flags & I2C_MSG_ADDR_10) - { - i2c_address a = bus->cmsg->addr; - b = 0xf0 | (((a >> 8) & 0x03) << 1); - if (bus->cmsg->flags & I2C_MSG_WR) - { - mpc5200mbus_send(bus, b); - next_state(bus, STATE_ADDR_1_W); - } - else - { - mpc5200mbus_send(bus, b | 1); - next_state(bus, STATE_ADDR_1_R); - } - } - else - { - b = (bus->cmsg->addr & ~0x01); - - if (bus->cmsg->flags & I2C_MSG_WR) - { - next_state(bus, STATE_SENDING); - } - else - { - next_state(bus, STATE_ADDR_7); - b |= 1; - } - - mpc5200mbus_send(bus, b); - } - break; - - default: - mpc5200mbus_machine_error(bus, event); - break; - } - break; - - case STATE_ADDR_7: - switch (event) - { - case EVENT_ACK: - mpc5200mbus_rx_mode(bus); - if (bus->cmsg->len <= 1) - mpc5200mbus_send_nack(bus); - else - mpc5200mbus_send_ack(bus); - next_state(bus, STATE_RECEIVING); - break; - - case EVENT_NACK: - mpc5200mbus_error(bus, I2C_NO_DEVICE); - next_state(bus, STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - break; - - case EVENT_ARB_LOST: - mpc5200mbus_error(bus, I2C_ARBITRATION_LOST); - next_state(bus, STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - break; - - default: - mpc5200mbus_machine_error(bus, event); - break; - } - break; - - case STATE_ADDR_1_R: - case STATE_ADDR_1_W: - switch (event) - { - case EVENT_ACK: - { - uint8_t b = (bus->cmsg->addr & 0xff); - mpc5200mbus_send(bus, b); - if (bus->state == STATE_ADDR_1_W) - { - next_state(bus, STATE_SENDING); - } - else - { - i2c_address a; - mpc5200mbus_rstart(bus); - mpc5200mbus_tx_mode(bus); - a = bus->cmsg->addr; - b = 0xf0 | (((a >> 8) & 0x03) << 1) | 1; - mpc5200mbus_send(bus, b); - next_state(bus, STATE_ADDR_7); - } - break; - } - - case EVENT_NACK: - mpc5200mbus_error(bus, I2C_NO_DEVICE); - next_state(bus, STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - break; - - case EVENT_ARB_LOST: - mpc5200mbus_error(bus, I2C_ARBITRATION_LOST); - next_state(bus, STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - break; - - default: - mpc5200mbus_machine_error(bus, event); - break; - } - break; - - case STATE_SENDING: - switch (event) - { - case EVENT_ACK: - if (bus->byte == bus->cmsg->len) - { - next_state(bus, STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - } - else - { - mpc5200mbus_send(bus, bus->cmsg->buf[bus->byte++]); - next_state(bus, STATE_SENDING); - } - break; - - case EVENT_NACK: - if (bus->byte == 0) - { - mpc5200mbus_error(bus, I2C_NO_DEVICE); - } - else - { - mpc5200mbus_error(bus, I2C_NO_ACKNOWLEDGE); - } - next_state(bus, STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - break; - - case EVENT_ARB_LOST: - mpc5200mbus_error(bus, I2C_ARBITRATION_LOST); - next_state(bus, STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - break; - - default: - mpc5200mbus_machine_error(bus, event); - break; - - } - break; - - case STATE_RECEIVING: - switch (event) - { - case EVENT_DATA_RECV: - if (bus->cmsg->len - bus->byte <= 2) - { - mpc5200mbus_send_nack(bus); - if (bus->cmsg->len - bus->byte <= 1) - { - if (bus->cmsg - bus->msg + 1 == bus->nmsg) - mpc5200mbus_stop(bus); - else - mpc5200mbus_rstart(bus); - } - } - else - { - mpc5200mbus_send_ack(bus); - } - bus->cmsg->buf[bus->byte++] = mpc5200.i2c_regs[bus->bus_idx].mdr; - if (bus->cmsg->len == bus->byte) - { - next_state(bus,STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - } - else - { - next_state(bus,STATE_RECEIVING); - } - break; - - case EVENT_ARB_LOST: - mpc5200mbus_error(bus, I2C_ARBITRATION_LOST); - next_state(bus, STATE_IDLE); - mpc5200mbus_machine(bus, EVENT_NEXTMSG); - break; - - default: - mpc5200mbus_machine_error(bus, event); - break; - } - break; - } -} - -/* mpc5200mbus_interrupt_handler -- - * MBUS module interrupt handler routine - * - * PARAMETERS: - * handle: pointer to mbus structure - * - * RETURNS: - * none - */ -static void mpc5200mbus_interrupt_handler(rtems_irq_hdl_param handle) -{ - i2c_event event; - mpc5200mbus *bus = handle; - - event = mpc5200mbus_get_event(bus); - /* - * clear interrupt bit - */ - mpc5200.i2c_regs[bus->bus_idx].msr &= ~MPC5200_I2C_MSR_MIF; - - mpc5200mbus_machine(bus, event); -} - -/* - * mpc5200_mbus_irq_enable - * enable irq for mbus - */ -static void mpc5200mbus_irq_enable(const rtems_irq_connect_data* ptr) -{ - int minor = ((mpc5200mbus*)(ptr->handle))->bus_idx; - - mpc5200.i2c_regs[minor].mcr |= MPC5200_I2C_MCR_MIEN; -} - -/* - * mpc5200_mbus_irq_disable - * enable irq for mbus - */ -static void mpc5200mbus_irq_disable(const rtems_irq_connect_data* ptr) -{ - int minor = ((mpc5200mbus*)(ptr->handle))->bus_idx; - - mpc5200.i2c_regs[minor].mcr &= ~MPC5200_I2C_MCR_MIEN; -} - -/* - * mpc5200_mbus_isOn - * check, whether irq is enabled - */ -static int mpc5200mbus_irq_isOn(const rtems_irq_connect_data* ptr) -{ - int minor = ((mpc5200mbus*)(ptr->handle))->bus_idx; - - return (0 != (mpc5200.i2c_regs[minor].mcr & MPC5200_I2C_MCR_MIEN)); -} - -/* mpc5200mbus_poll -- - * MBUS module poll routine; used to poll events when I2C driver - * operates in poll-driven mode. - * - * PARAMETERS: - * none - * - * RETURNS: - * none - */ -void -mpc5200mbus_poll(mpc5200mbus *bus) -{ - i2c_event event; - event = mpc5200mbus_get_event(bus); - if (event != EVENT_NONE) - mpc5200mbus_machine(bus, event); -} - -/* mpc5200mbus_select_clock_divider -- - * Select divider for system clock which is used for I2C bus clock - * generation. Not each divider can be selected for I2C bus; this - * function select nearest larger or equal divider. - * - * PARAMETERS: - * i2c_bus - pointer to the bus descriptor structure - * divider - system frequency divider for I2C serial clock. - * RETURNS: - * RTEMS_SUCCESSFUL, if operation performed successfully, or - * RTEMS error code when failed. - */ -rtems_status_code -mpc5200mbus_select_clock_divider(mpc5200mbus *bus, int divider) -{ - int i; - int mbc; - struct { - int divider; - int mbc; - } dividers[] ={ - { 20, 0x20 }, { 22, 0x21 }, { 24, 0x22 }, { 26, 0x23 }, - { 28, 0x00 }, { 30, 0x01 }, { 32, 0x25 }, { 34, 0x02 }, - { 36, 0x26 }, { 40, 0x03 }, { 44, 0x04 }, { 48, 0x05 }, - { 56, 0x06 }, { 64, 0x2a }, { 68, 0x07 }, { 72, 0x2B }, - { 80, 0x08 }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A }, - { 112, 0x2E }, { 128, 0x0B }, { 144, 0x0C }, { 160, 0x0D }, - { 192, 0x0E }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 }, - { 288, 0x10 }, { 320, 0x11 }, { 384, 0x12 }, { 448, 0x36 }, - { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x15 }, - { 768, 0x16 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B }, - { 1152, 0x18 }, { 1280, 0x19 }, { 1536, 0x1A }, { 1792, 0x3E }, - { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D }, - { 3072, 0x1E }, { 3840, 0x1F } - }; - - if (bus == NULL) - return RTEMS_INVALID_ADDRESS; - - for (i = 0, mbc = -1; i < sizeof(dividers)/sizeof(dividers[0]); i++) - { - mbc = dividers[i].mbc; - if (dividers[i].divider >= divider) - { - break; - } - } - mpc5200.i2c_regs[bus->bus_idx].mfdr = mbc; - return RTEMS_SUCCESSFUL; -} - -/* mpc5200mbus_initialize -- - * Initialize MPC5200 MBUS I2C bus controller. - * - * PARAMETERS: - * i2c_bus - pointer to the bus descriptor structure - * - * RETURNS: - * RTEMS_SUCCESSFUL, or RTEMS error code when initialization failed. - */ -rtems_status_code -mpc5200mbus_initialize(mpc5200mbus *i2c_bus) -{ - rtems_interrupt_level level; - rtems_status_code sc; - rtems_irq_connect_data mbusIrqData; - - if (i2c_bus == NULL) - return RTEMS_INVALID_ADDRESS; - - if (i2c_bus->state != STATE_UNINITIALIZED) /* Check if already initialized */ - return RTEMS_RESOURCE_IN_USE; - - i2c_bus->state = STATE_IDLE; - i2c_bus->msg = NULL; - i2c_bus->cmsg = NULL; - i2c_bus->nmsg = 0; - i2c_bus->byte = 0; - - /* - * install interrupt handler - */ - mbusIrqData.on = mpc5200mbus_irq_enable; - mbusIrqData.off = mpc5200mbus_irq_disable; - mbusIrqData.isOn = mpc5200mbus_irq_isOn; - mbusIrqData.handle = (rtems_irq_hdl_param)i2c_bus; - mbusIrqData.hdl = mpc5200mbus_interrupt_handler; - switch(i2c_bus->bus_idx) { - case 0: - mbusIrqData.name = BSP_SIU_IRQ_I2C1; - break; - case 1: - mbusIrqData.name = BSP_SIU_IRQ_I2C2; - break; - } - if (!BSP_install_rtems_irq_handler (&mbusIrqData)) { - sc = RTEMS_UNSATISFIED; - return sc; - } - - rtems_interrupt_disable(level); - - mpc5200.i2c_regs[i2c_bus->bus_idx].mcr &= ~MPC5200_I2C_MCR_MEN; - mpc5200.i2c_regs[i2c_bus->bus_idx].msr = 0; - mpc5200.i2c_regs[i2c_bus->bus_idx].mdr = 0x1F; /* Maximum possible divider is 3840 */ - mpc5200.i2c_regs[i2c_bus->bus_idx].mcr |= MPC5200_I2C_MCR_MEN; - - rtems_interrupt_enable(level); - - return RTEMS_SUCCESSFUL; -} - -/* mpc5200mbus_i2c_transfer -- - * Initiate multiple-messages transfer over I2C bus via ColdFire MBUS - * controller. - * - * PARAMETERS: - * bus - pointer to MBUS controller descriptor - * nmsg - number of messages - * msg - pointer to messages array - * done - function which is called when transfer is finished - * done_arg_ptr - arbitrary argument pointer passed to done funciton - * - * RETURNS: - * RTEMS_SUCCESSFUL if transfer initiated successfully, or error - * code when failed. - */ -rtems_status_code -mpc5200mbus_i2c_transfer(mpc5200mbus *bus, int nmsg, i2c_message *msg, - i2c_transfer_done done, void *done_arg_ptr) -{ - if (bus->state == STATE_UNINITIALIZED) - return RTEMS_NOT_CONFIGURED; - - bus->done = done; - bus->done_arg_ptr = done_arg_ptr; - bus->cmsg = bus->msg = msg; - bus->nmsg = nmsg; - bus->byte = 0; - bus->state = STATE_IDLE; - mpc5200mbus_machine(bus, EVENT_TRANSFER); - return RTEMS_SUCCESSFUL; -} - - -/* mpc5200mbus_i2c_done -- - * Close ColdFire MBUS I2C bus controller and release all resources. - * - * PARAMETERS: - * bus - pointer to MBUS controller descriptor - * - * RETURNS: - * RTEMS_SUCCESSFUL, if transfer initiated successfully, or error - * code when failed. - */ -rtems_status_code -mpc5200mbus_i2c_done(mpc5200mbus *i2c_bus) -{ - rtems_status_code sc = RTEMS_SUCCESSFUL; - - if (i2c_bus->state == STATE_UNINITIALIZED) - return RTEMS_NOT_CONFIGURED; - - mpc5200.i2c_regs[i2c_bus->bus_idx].mcr = 0; - - return sc; -} diff --git a/c/src/lib/libbsp/powerpc/gen5200/i2c/mpc5200mbus.h b/c/src/lib/libbsp/powerpc/gen5200/i2c/mpc5200mbus.h deleted file mode 100644 index e68b564e2f..0000000000 --- a/c/src/lib/libbsp/powerpc/gen5200/i2c/mpc5200mbus.h +++ /dev/null @@ -1,150 +0,0 @@ -/*===============================================================*\ -| Project: RTEMS generic MPC5200 BSP | -+-----------------------------------------------------------------+ -| Copyright (c) 2005 | -| Embedded Brains GmbH | -| Obere Lagerstr. 30 | -| D-82178 Puchheim | -| Germany | -| rtems@embedded-brains.de | -+-----------------------------------------------------------------+ -| The license and distribution terms for this file may be | -| found in the file LICENSE in this distribution or at | -| | -| http://www.rtems.org/license/LICENSE. | -| | -+-----------------------------------------------------------------+ -| MBUS module (I2C bus) definitions | -+-----------------------------------------------------------------+ -| This file has been adapted from an existing source code file, | -| see the original file header below for reference | -\*===============================================================*/ -/* - * MCF5206e MBUS module (I2C bus) driver header file - * - * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia - * Author: Victor V. Vengerov <vvv@oktet.ru> - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * - * http://www.rtems.org/license/LICENSE. - */ - -#ifndef __MPC5200BSP_MPC5200MBUS_H__ -#define __MPC5200BSP_MPC5200MBUS_H__ - -#include <bsp/mpc5200.h> -#include <bsp/i2c.h> - - -/* States of I2C machine */ -typedef enum mpc5200mbus_i2c_state { - STATE_UNINITIALIZED, - STATE_IDLE, - STATE_ADDR_7, - STATE_ADDR_1_W, - STATE_ADDR_1_R, - STATE_SENDING, - STATE_RECEIVING -} mpc5200mbus_i2c_state; - -typedef struct mpc5200_mbus { - enum mpc5200mbus_i2c_state state;/* State of I2C machine */ - i2c_message *msg; /* Pointer to the first message in transfer */ - int nmsg; /* Number of messages in transfer */ - i2c_message *cmsg; /* Current message */ - int byte; /* Byte number in current message */ - rtems_id sema; /* MBUS semaphore */ - i2c_transfer_done done; /* Transfer done function */ - void * done_arg_ptr; /* Done function argument pointer */ - int bus_idx; /* bus index: 0 or 1 */ -} mpc5200mbus; - -/* mpc5200mbus_initialize -- - * Initialize ColdFire MBUS I2C bus controller. - * - * PARAMETERS: - * i2c_bus - pointer to the bus descriptor structure - * - * RETURNS: - * RTEMS_SUCCESSFUL, or RTEMS error code when initialization failed. - */ -rtems_status_code -mpc5200mbus_initialize(mpc5200mbus *i2c_bus); - -/* mpc5200mbus_select_clock_divider -- - * Select divider for system clock which is used for I2C bus clock - * generation. Not each divider can be selected for I2C bus; this - * function select nearest larger or equal divider, or maximum - * possible divider, if passed value greater. - * - * PARAMETERS: - * i2c_bus - pointer to the bus descriptor structure - * divider - system frequency divider for I2C serial clock. - * - * RETURNS: - * RTEMS_SUCCESSFUL, if operation performed successfully, or - * RTEMS error code when failed. - */ -rtems_status_code -mpc5200mbus_select_clock_divider(mpc5200mbus *i2c_bus, int divider); - -/* mpc5200mbus_i2c_transfer -- - * Initiate multiple-messages transfer over I2C bus via ColdFire MBUS - * controller. - * - * PARAMETERS: - * bus - pointer to MBUS controller descriptor - * nmsg - number of messages - * msg - pointer to messages array - * done - function which is called when transfer is finished - * done_arg_ptr - arbitrary argument ptr passed to done funciton - * - * RETURNS: - * RTEMS_SUCCESSFUL if transfer initiated successfully, or error - * code when failed. - */ -rtems_status_code -mpc5200mbus_i2c_transfer(mpc5200mbus *bus, int nmsg, i2c_message *msg, - i2c_transfer_done done, void * done_arg_ptr); - -/* mpc5200mbus_i2c_done -- - * Close ColdFire MBUS I2C bus controller and release all resources. - * - * PARAMETERS: - * bus - pointer to MBUS controller descriptor - * - * RETURNS: - * RTEMS_SUCCESSFUL, if transfer initiated successfully, or error - * code when failed. - */ -rtems_status_code -mpc5200mbus_i2c_done(mpc5200mbus *i2c_bus); - -/* mpc5200mbus_i2c_interrupt_handler -- - * ColdFire MBUS I2C bus controller interrupt handler. This function - * called from real interrupt handler, and pointer to MBUS descriptor - * structure passed to this function. - * - * PARAMETERS: - * bus - pointert to the bus descriptor structure - * - * RETURNS: - * none - */ -void mpc5200mbus_i2c_interrupt_handler(mpc5200mbus *bus); - -/* mpc5200mbus_poll -- - * MBUS module poll routine; used to poll events when I2C driver - * operates in poll-driven mode. - * - * PARAMETERS: - * none - * - * RETURNS: - * none - */ -void mpc5200mbus_poll(mpc5200mbus *bus); - -#endif /* __MPC5200BSP_MPC5200MBUS_H__ */ diff --git a/c/src/lib/libbsp/powerpc/gen83xx/Makefile.am b/c/src/lib/libbsp/powerpc/gen83xx/Makefile.am index 1f99f2c30c..716a0061c2 100644 --- a/c/src/lib/libbsp/powerpc/gen83xx/Makefile.am +++ b/c/src/lib/libbsp/powerpc/gen83xx/Makefile.am @@ -56,7 +56,7 @@ librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/dev/serial/console-termio librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/gen83xx/console/console-config.c # bsp_i2c -librtemsbsp_a_SOURCES += i2c/i2c_init.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/gen83xx/i2c/i2c_init.c # bsp_spi librtemsbsp_a_SOURCES += spi/spi_init.c diff --git a/c/src/lib/libbsp/powerpc/gen83xx/i2c/i2c_init.c b/c/src/lib/libbsp/powerpc/gen83xx/i2c/i2c_init.c deleted file mode 100644 index 7fdd6bbd4c..0000000000 --- a/c/src/lib/libbsp/powerpc/gen83xx/i2c/i2c_init.c +++ /dev/null @@ -1,152 +0,0 @@ -/*===============================================================*\ -| Project: RTEMS support for MPC83xx | -+-----------------------------------------------------------------+ -| Copyright (c) 2007 | -| Embedded Brains GmbH | -| Obere Lagerstr. 30 | -| D-82178 Puchheim | -| Germany | -| rtems@embedded-brains.de | -+-----------------------------------------------------------------+ -| The license and distribution terms for this file may be | -| found in the file LICENSE in this distribution or at | -| | -| http://www.rtems.org/license/LICENSE. | -| | -+-----------------------------------------------------------------+ -| this file contains the low level MPC83xx I2C driver parameters | -\*===============================================================*/ - - -#include <mpc83xx/mpc83xx_i2cdrv.h> -#include <libchip/i2c-2b-eeprom.h> -#include <libchip/i2c-sc620.h> -#include <bsp/irq.h> -#include <bsp.h> - -static void i2c1_probe(mpc83xx_i2c_softc_t *self) -{ -#if MPC83XX_CHIP_TYPE != 8309 - if (((mpc83xx.clk.sccr >> (31-1)) & 0x03) > 0) { - self->base_frq = - (BSP_bus_frequency - /((mpc83xx.clk.sccr >> (31-1)) & 0x03)); - } -#else /* MPC83XX_CHIP_TYPE != 8309 */ - self->base_frq = BSP_bus_frequency; -#endif /* MPC83XX_CHIP_TYPE != 8309 */ -} - -#ifndef MPC83XX_BOARD_BR_UID -static void i2c2_probe(mpc83xx_i2c_softc_t *self) -{ - self->base_frq = BSP_bus_frequency; -} -#endif /* MPC83XX_BOARD_BR_UID */ - -static mpc83xx_i2c_desc_t mpc83xx_i2c_bus_tbl[] = { - /* first channel */ - { - {/* public fields */ - .ops = &mpc83xx_i2c_ops, - .size = sizeof(mpc83xx_i2c_bus_tbl[0]), - }, - { /* our private fields */ - .reg_ptr = &mpc83xx.i2c[0], - .initialized = FALSE, - .irq_number = BSP_IPIC_IRQ_I2C1, - .base_frq = 0, /* will be set during probe */ - .probe = i2c1_probe - } - } -#ifndef MPC83XX_BOARD_BR_UID - /* second channel */ - , { - { /* public fields */ - .ops = &mpc83xx_i2c_ops, - .size = sizeof(mpc83xx_i2c_bus_tbl[0]), - }, - { /* our private fields */ - .reg_ptr = &mpc83xx.i2c[1], - .initialized = FALSE, - .irq_number = BSP_IPIC_IRQ_I2C2, - .base_frq = 0, /* will be set during probe */ - .probe = i2c2_probe - } - } -#endif /* MPC83XX_BOARD_BR_UID */ -}; - -/*=========================================================================*\ -| Function: | -\*-------------------------------------------------------------------------*/ -rtems_status_code bsp_register_i2c -( -/*-------------------------------------------------------------------------*\ -| Purpose: | -| register I2C busses and devices | -+---------------------------------------------------------------------------+ -| Input Parameters: | -\*-------------------------------------------------------------------------*/ - void /* <none> */ -) -/*-------------------------------------------------------------------------*\ -| Return Value: | -| 0 or error code | -\*=========================================================================*/ - -{ - char device_path[] = "/dev/i2c?"; - size_t n = RTEMS_ARRAY_SIZE(mpc83xx_i2c_bus_tbl); - size_t i; - int i2c_busno[n]; - - /* - * init I2C library (if not already done) - */ - rtems_libi2c_initialize (); - - /* - * init I2C buses - */ - for (i = 0; i < n; ++i) { - mpc83xx_i2c_desc_t *desc = &mpc83xx_i2c_bus_tbl[i]; - - (*desc->softc.probe)(&desc->softc); - device_path[sizeof(device_path) - 2] = (char) ('1' + i); - i2c_busno[i] = rtems_libi2c_register_bus(device_path, &desc->bus_desc); - } - -#if defined(RTEMS_BSP_I2C_EEPROM_DEVICE_NAME) - if (n > 0) { - /* - * register EEPROM to bus 1, Address 0x50 - */ - rtems_libi2c_register_drv(RTEMS_BSP_I2C_EEPROM_DEVICE_NAME, - i2c_2b_eeprom_driver_descriptor, - i2c_busno[0],0x50); - } -#elif defined(MPC83XX_BOARD_BR_UID) - if (n > 0) { - rtems_libi2c_register_drv( - "sc620", - &i2c_sc620_driver, - i2c_busno[0], - 0x70 - ); - } -#else - - /* - * We have no i2c configuration for this variant but need to mark - * i2c_busno as used. - */ - (void) i2c_busno[0]; /* avoid set but not used warning */ -#endif - - /* - * FIXME: register RTC driver, when available - */ - - return RTEMS_SUCCESSFUL; -} diff --git a/c/src/lib/libbsp/powerpc/mpc55xxevb/Makefile.am b/c/src/lib/libbsp/powerpc/mpc55xxevb/Makefile.am index 74ec50343f..3838063aef 100644 --- a/c/src/lib/libbsp/powerpc/mpc55xxevb/Makefile.am +++ b/c/src/lib/libbsp/powerpc/mpc55xxevb/Makefile.am @@ -84,7 +84,7 @@ librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/irq/irq-default-handler.c librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/mpc55xxevb/start/sd-card-init.c # I2C -librtemsbsp_a_SOURCES += i2c/i2c_init.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/mpc55xxevb/i2c/i2c_init.c # Network if HAS_NETWORKING diff --git a/c/src/lib/libbsp/powerpc/mpc55xxevb/i2c/i2c_init.c b/c/src/lib/libbsp/powerpc/mpc55xxevb/i2c/i2c_init.c deleted file mode 100644 index 49fd8cad9e..0000000000 --- a/c/src/lib/libbsp/powerpc/mpc55xxevb/i2c/i2c_init.c +++ /dev/null @@ -1,75 +0,0 @@ -/*===============================================================*\ -| Project: RTEMS support for GWLCFM | -+-----------------------------------------------------------------+ -| Copyright (c) 2010 | -| Embedded Brains GmbH | -| Obere Lagerstr. 30 | -| D-82178 Puchheim | -| Germany | -| rtems@embedded-brains.de | -+-----------------------------------------------------------------+ -| The license and distribution terms for this file may be | -| found in the file LICENSE in this distribution or at | -| | -| http://www.rtems.org/license/LICENSE. | -| | -+-----------------------------------------------------------------+ -| this file contains the low level MPC5516 I2C driver parameters | -\*===============================================================*/ - -#include <libchip/i2c-2b-eeprom.h> - -#include <bsp.h> -#include <bsp/irq.h> -#include <mpc83xx/mpc83xx_i2cdrv.h> - -#if MPC55XX_CHIP_FAMILY == 551 - static void i2c_probe(mpc83xx_i2c_softc_t *self) - { - self->base_frq = bsp_clock_speed; - } - - static mpc83xx_i2c_desc_t mpc55xx_i2c_bus = { - .bus_desc = { - .ops = &mpc83xx_i2c_ops, - .size = sizeof(mpc55xx_i2c_bus), - }, - .softc = { - .reg_ptr = (m83xxI2CRegisters_t *) 0xfff88000, - .initialized = FALSE, - .irq_number = MPC55XX_IRQ_I2C(0), - .base_frq = 0, - .probe = i2c_probe - } - }; - - rtems_status_code bsp_register_i2c(void) - { - int rv = 0; - int busno = 0; - - rtems_libi2c_initialize (); - - busno = rtems_libi2c_register_bus( - "/dev/i2c1", - &mpc55xx_i2c_bus.bus_desc - ); - if (busno < 0) { - return RTEMS_IO_ERROR; - } - - #ifdef RTEMS_BSP_I2C_EEPROM_DEVICE_NAME - rv = rtems_libi2c_register_drv( - RTEMS_BSP_I2C_EEPROM_DEVICE_NAME, - i2c_2b_eeprom_driver_descriptor, - busno, - 0x51 - ); - if (rv < 0) { - return RTEMS_IO_ERROR; - } - #endif - - return RTEMS_SUCCESSFUL; - } -#endif diff --git a/c/src/lib/libbsp/powerpc/mvme3100/Makefile.am b/c/src/lib/libbsp/powerpc/mvme3100/Makefile.am index f0fdf86cf8..bd453ee559 100644 --- a/c/src/lib/libbsp/powerpc/mvme3100/Makefile.am +++ b/c/src/lib/libbsp/powerpc/mvme3100/Makefile.am @@ -74,8 +74,8 @@ librtemsbsp_a_SOURCES += ../shared/flash/spansionFlash.c librtemsbsp_a_SOURCES += flash/flashcfg.c # i2c -librtemsbsp_a_SOURCES += i2c/mpc8540_i2c.c -librtemsbsp_a_SOURCES += i2c/i2c_init.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/mvme3100/i2c/mpc8540_i2c.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/powerpc/mvme3100/i2c/i2c_init.c # vpd librtemsbsp_a_SOURCES += ../shared/motorola/vpd.c diff --git a/c/src/lib/libbsp/powerpc/mvme3100/i2c/i2c_init.c b/c/src/lib/libbsp/powerpc/mvme3100/i2c/i2c_init.c deleted file mode 100644 index 9e242baf20..0000000000 --- a/c/src/lib/libbsp/powerpc/mvme3100/i2c/i2c_init.c +++ /dev/null @@ -1,186 +0,0 @@ -/* Register i2c bus driver & devices */ - -/* - * Authorship - * ---------- - * This software ('mvme3100' RTEMS BSP) was created by - * - * Till Straumann <strauman@slac.stanford.edu>, 2005-2007, - * Stanford Linear Accelerator Center, Stanford University. - * - * Acknowledgement of sponsorship - * ------------------------------ - * The 'mvme3100' BSP was produced by - * the Stanford Linear Accelerator Center, Stanford University, - * under Contract DE-AC03-76SFO0515 with the Department of Energy. - * - * Government disclaimer of liability - * ---------------------------------- - * Neither the United States nor the United States Department of Energy, - * nor any of their employees, makes any warranty, express or implied, or - * assumes any legal liability or responsibility for the accuracy, - * completeness, or usefulness of any data, apparatus, product, or process - * disclosed, or represents that its use would not infringe privately owned - * rights. - * - * Stanford disclaimer of liability - * -------------------------------- - * Stanford University makes no representations or warranties, express or - * implied, nor assumes any liability for the use of this software. - * - * Stanford disclaimer of copyright - * -------------------------------- - * Stanford University, owner of the copyright, hereby disclaims its - * copyright and all other rights in this software. Hence, anyone may - * freely use it for any purpose without restriction. - * - * Maintenance of notices - * ---------------------- - * In the interest of clarity regarding the origin and status of this - * SLAC software, this and all the preceding Stanford University notices - * are to remain affixed to any copy or derivative of this software made - * or distributed by the recipient and are to be affixed to any copy of - * software made or distributed by the recipient that contains a copy or - * derivative of this software. - * - * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 - */ - -#include <rtems.h> -#include <bsp.h> -#include <rtems/bspIo.h> -#include <rtems/libi2c.h> -#include <libchip/i2c-2b-eeprom.h> -#include <libchip/i2c-ds1621.h> -#include <bsp/mpc8540_i2c_busdrv.h> -#include <rtems/libio.h> -#include <rtems/score/sysstate.h> - -#include <stdio.h> -#include <sys/stat.h> -#include <errno.h> -#include <stdarg.h> - -static void -safe_printf (const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - if ( _System_state_Is_up( _System_state_Get() ) ) - vfprintf( stderr, fmt, ap ); - else - vprintk( fmt, ap ); - va_end(ap); -} - -static void -safe_perror(const char *s) -{ - safe_printf("%s :%s\n", s, strerror(errno)); -} - - -int -BSP_i2c_initialize(void) -{ -int busno, succ = 0; - - /* Initialize the library */ - if ( rtems_libi2c_initialize() ) { - safe_printf("Initializing I2C library failed\n"); - return -1; - } - - /* Register our bus driver */ - if ( (busno=rtems_libi2c_register_bus( - BSP_I2C_BUS0_NAME, - BSP_I2C_BUS_DESCRIPTOR) ) < 0 ) { - safe_perror("Registering mpc8540 i2c bus driver"); - return -1; - } - - /* Now register higher level drivers; note that - * the i2c address in the manual is actually left-shifted - * by one bit, i.e., as it would go on the bus. - */ - - /* Use read-only driver for VPD */ - if ( rtems_libi2c_register_drv( - BSP_I2C_VPD_EEPROM_NAME, - i2c_2b_eeprom_ro_driver_descriptor, - busno, - BSP_VPD_I2C_ADDR) < 0 ) { - safe_perror("Registering i2c VPD eeprom driver failed"); - } else { - succ++; - } - - /* Use read-write driver for user eeprom -- you still might - * have to disable HW write-protection on your board. - */ - if ( rtems_libi2c_register_drv( - BSP_I2C_USR_EEPROM_NAME, - i2c_2b_eeprom_driver_descriptor, - busno, - BSP_USR0_I2C_ADDR) < 0 ) { - safe_perror("Registering i2c 1st USR eeprom driver failed"); - } else { - succ++; - } - - /* Use read-write driver for user eeprom -- you still might - * have to disable HW write-protection on your board. - */ - if ( rtems_libi2c_register_drv( - BSP_I2C_USR1_EEPROM_NAME, - i2c_2b_eeprom_driver_descriptor, - busno, - BSP_USR1_I2C_ADDR) < 0 ) { - safe_perror("Registering i2c 2nd USR eeprom driver failed"); - } else { - succ++; - } - - /* The thermostat */ - if ( rtems_libi2c_register_drv( - BSP_I2C_DS1621_NAME, - i2c_ds1621_driver_descriptor, - busno, - BSP_THM_I2C_ADDR) < 0 ) { - safe_perror("Registering i2c ds1621 temp sensor. driver failed"); - } else { - succ++; - } - - /* Finally, as an example, register raw access to the - * ds1621. The driver above just reads the 8 msb of the - * temperature but doesn't support anything else. Using - * the raw device node you can write/read individual - * control bytes yourself and e.g., program the thermostat... - */ - - if ( mknod( - BSP_I2C_DS1621_RAW_DEV_NAME, - 0666 | S_IFCHR, - rtems_filesystem_make_dev_t(rtems_libi2c_major, - RTEMS_LIBI2C_MAKE_MINOR(busno,BSP_THM_I2C_ADDR))) ) { - safe_perror("Creating device node for raw ds1621 (temp. sensor) access failed"); - } else { - succ++; - } - - /* Raw access to RTC */ - if ( mknod( - BSP_I2C_DS1375_RAW_DEV_NAME, - 0666 | S_IFCHR, - rtems_filesystem_make_dev_t(rtems_libi2c_major, - RTEMS_LIBI2C_MAKE_MINOR(busno,BSP_RTC_I2C_ADDR))) ) { - safe_perror("Creating device node for raw ds1375 (rtc) access failed"); - } else { - succ++; - } - - safe_printf("%i I2C devices registered\n", succ); - return 0; -} diff --git a/c/src/lib/libbsp/powerpc/mvme3100/i2c/mpc8540_i2c.c b/c/src/lib/libbsp/powerpc/mvme3100/i2c/mpc8540_i2c.c deleted file mode 100644 index b32b69255e..0000000000 --- a/c/src/lib/libbsp/powerpc/mvme3100/i2c/mpc8540_i2c.c +++ /dev/null @@ -1,453 +0,0 @@ -/* I2C bus driver for mpc8540-based boards */ - -/* - * Authorship - * ---------- - * This software ('mvme3100' RTEMS BSP) was created by - * - * Till Straumann <strauman@slac.stanford.edu>, 2005-2007, - * Stanford Linear Accelerator Center, Stanford University. - * - * Acknowledgement of sponsorship - * ------------------------------ - * The 'mvme3100' BSP was produced by - * the Stanford Linear Accelerator Center, Stanford University, - * under Contract DE-AC03-76SFO0515 with the Department of Energy. - * - * Government disclaimer of liability - * ---------------------------------- - * Neither the United States nor the United States Department of Energy, - * nor any of their employees, makes any warranty, express or implied, or - * assumes any legal liability or responsibility for the accuracy, - * completeness, or usefulness of any data, apparatus, product, or process - * disclosed, or represents that its use would not infringe privately owned - * rights. - * - * Stanford disclaimer of liability - * -------------------------------- - * Stanford University makes no representations or warranties, express or - * implied, nor assumes any liability for the use of this software. - * - * Stanford disclaimer of copyright - * -------------------------------- - * Stanford University, owner of the copyright, hereby disclaims its - * copyright and all other rights in this software. Hence, anyone may - * freely use it for any purpose without restriction. - * - * Maintenance of notices - * ---------------------- - * In the interest of clarity regarding the origin and status of this - * SLAC software, this and all the preceding Stanford University notices - * are to remain affixed to any copy or derivative of this software made - * or distributed by the recipient and are to be affixed to any copy of - * software made or distributed by the recipient that contains a copy or - * derivative of this software. - * - * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 - */ - -/* Note: We maintain base address, IRQ etc. statically and - * globally. We don't bother creating driver-specific - * data or using the bus handle but simply assume - * this is the only 8540/i2c bus in the system. - * Proper support for multiple instances would not - * be very hard to add but I don't see the point... - */ - -#include <rtems.h> -#include <bsp.h> -#include <rtems/libi2c.h> -#include <bsp/irq.h> -#include <libcpu/spr.h> -#include <libcpu/io.h> -#include <rtems/bspIo.h> -#include <rtems/score/sysstate.h> - -#include <bsp/mpc8540_i2c_busdrv.h> - -#define STATIC static - -/* I2C controller register definitions */ -#define I2CADR 0x3000 -#define I2CFDR 0x3004 -#define I2CCR 0x3008 -#define I2CCR_MEN (1<<(7-0)) -#define I2CCR_MIEN (1<<(7-1)) -#define I2CCR_MSTA (1<<(7-2)) -#define I2CCR_MTX (1<<(7-3)) -#define I2CCR_TXAK (1<<(7-4)) -#define I2CCR_RSTA (1<<(7-5)) -#define I2CCR_BCST (1<<(7-7)) -#define I2CSR 0x300c -#define I2CSR_MCF (1<<(7-0)) -#define I2CSR_MAAS (1<<(7-1)) -#define I2CSR_MBB (1<<(7-2)) -#define I2CSR_MAL (1<<(7-3)) -#define I2CSR_BCSTM (1<<(7-4)) -#define I2CSR_SRW (1<<(7-5)) -#define I2CSR_MIF (1<<(7-6)) -#define I2CSR_RXAK (1<<(7-7)) -#define I2CDR 0x3010 -#define I2CDFSRR 0x3014 - -SPR_RO(TBRL) - -/********* Global Variables **********/ - -/* - * Semaphore for synchronizing accessing task - * with the (slow) hardware operation. - * Task takes semaphore and blocks, ISR releases. - */ -static rtems_id syncsem = 0; - -static inline int ok_to_block(void) -{ - return syncsem && _System_state_Is_up( _System_state_Get() ); -} - -/* - * Wild guess for 0.2 s; this timeout is effective - * in polling mode; during early init we don't know - * the system clock rate yet - it's one of the things - * we have to read from VPD -- via i2c. - */ - -static uint32_t poll_timeout = 333333333/8/5; - -/********* Primitives ****************/ - -static inline uint8_t -i2c_rd(unsigned reg) -{ - return in_8( (volatile uint8_t *)(BSP_8540_CCSR_BASE + reg) ); -} - -static inline void -i2c_wr(unsigned reg, uint8_t val) -{ - out_8( (volatile uint8_t *)(BSP_8540_CCSR_BASE + reg), val ); -} - -static inline void -i2c_set(unsigned reg, uint8_t val) -{ - i2c_wr( reg, i2c_rd( reg ) | val ); -} - -static inline void -i2c_clr(unsigned reg, uint8_t val) -{ - i2c_wr( reg, i2c_rd( reg ) & ~val ); -} - -/********* Helper Routines ***********/ - -/* Synchronize (wait) for a condition on the - * i2c bus. Wait for START or STOP to be complete - * or wait for a byte-transfer. - * The latter is much slower (9 bit times vs. 1/2 - * in the former cases). - * - * If the system is up (and we may block) then - * this routine attempts to block the current - * task rather than busy-waiting. - * - * NOTE: waiting for START/STOP always requires - * polling. - */ - -/* wait until i2c status reg AND mask == cond */ -static rtems_status_code -i2c_wait( uint8_t msk, uint8_t cond ) -{ -uint32_t then; -rtems_status_code sc; -static int warn = 0; - - if ( I2CSR_MIF == msk && ok_to_block() ) { - /* block on semaphore only if system is up and sema initialized */ - sc = rtems_semaphore_obtain( syncsem, RTEMS_WAIT, 100 ); - if ( RTEMS_SUCCESSFUL != sc ) - return sc; - } else { - /* system not up (no SEMA yet ) or waiting on something other - * than MIF - */ - if ( I2CSR_MIF == msk && _System_state_Is_up( _System_state_Get() ) ) { - if ( warn < 8 || ! (warn & 0x1f) ) - printk("WARNING: i2c bus driver running in polled mode -- should initialize properly!\n"); - warn++; - } - - then = _read_TBRL(); - do { - /* poll for .2 seconds */ - if ( (_read_TBRL() - then) > poll_timeout ) - return RTEMS_TIMEOUT; - } while ( (msk & i2c_rd( I2CSR )) != cond ); - } - - return RTEMS_SUCCESSFUL; -} - -/* - * multi-byte transfer - * - set transfer direction (master read or master write) - * - transfer byte - * - wait/synchronize - * - check for ACK - * - * RETURNS: number of bytes transferred or negative error code. - */ - -STATIC int -i2c_xfer(int rw, uint8_t *buf, int len) -{ -int i; -rtems_status_code sc; - - if ( rw ) { - i2c_clr( I2CCR, I2CCR_MTX ); - } else { - i2c_set( I2CCR, I2CCR_MTX ); - } - - for ( i = 0; i< len; i++ ) { - i2c_clr( I2CSR, I2CSR_MIF ); - /* Enable interrupts if necessary */ - if ( ok_to_block() ) - i2c_set( I2CCR, I2CCR_MIEN ); - if ( rw ) { - buf[i] = i2c_rd( I2CDR ); - } else { - i2c_wr( I2CDR, buf[i] ); - } - if ( RTEMS_SUCCESSFUL != (sc = i2c_wait( I2CSR_MIF, I2CSR_MIF )) ) - return -sc; - if ( (I2CSR_RXAK & i2c_rd( I2CSR )) ) { - /* NO ACK */ - return -RTEMS_IO_ERROR; - } - } - - return i; -} - -/* - * This bus controller gives us lagging data, i.e., - * when we read a byte from the data reg then that - * issues a read cycle on the bus and gives us the - * byte from the *previous* read cycle :-( - * - * This makes it impossible to properly terminate - * a read transaction w/o knowing ahead of time - * how many bytes are going to be read (API decouples - * 'START'/'STOP' from 'READ') since we would have to - * set TXAK when reading the next-to-last byte - * (i.e., when the last byte is read on the i2c bus). - * - * Hence, (if we are reading) we must do a dummy - * read-cycle here -- hopefully - * that has no side-effects! (i.e., EEPROM drivers should - * reposition file pointers after issuing STOP) - * - */ - -static void -rd1byte_noack(void) -{ -uint8_t dum; -uint8_t ccr; - - /* If we are in reading state then read one more - * byte w/o acknowledge - */ - - ccr = i2c_rd (I2CCR ); - - if ( ! ( I2CCR_MTX & ccr ) ) { - i2c_wr( I2CCR, ccr | I2CCR_TXAK ); - i2c_xfer(1, &dum, 1); - /* restore original TXAK bit setting */ - i2c_clr( I2CCR, (I2CCR_TXAK & ccr) ); - } -} - - -/********* ISR ***********************/ - -static void i2c_isr(rtems_irq_hdl_param arg) -{ - /* disable irq */ - i2c_clr( I2CCR, I2CCR_MIEN ); - /* release task */ - rtems_semaphore_release( syncsem ); -} - -/********* IIC Bus Driver Ops ********/ - -STATIC rtems_status_code -i2c_init(rtems_libi2c_bus_t *bh) -{ -rtems_status_code sc; - - /* compute more accurate timeout */ - if ( BSP_bus_frequency && BSP_time_base_divisor ) - poll_timeout = BSP_bus_frequency/BSP_time_base_divisor*1000/5; - - i2c_clr( I2CCR, I2CCR_MEN ); - i2c_set( I2CCR, I2CCR_MEN ); - - i2c_wr( I2CADR, 0 ); - - /* leave motload settings for divisor and filter registers */ - - if ( SYSTEM_STATE_BEFORE_MULTITASKING <= _System_state_Get() && !syncsem ) { - sc = rtems_semaphore_create( - rtems_build_name('i','2','c','b'), - 0, - RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_LOCAL, - 0, - &syncsem); - if ( RTEMS_SUCCESSFUL == sc ) { - rtems_irq_connect_data xxx; - xxx.name = BSP_I2C_IRQ; - xxx.on = 0; - xxx.off = 0; - xxx.isOn = 0; - xxx.hdl = i2c_isr; - xxx.handle = 0; - if ( ! BSP_install_rtems_irq_handler( &xxx ) ) { - printk("Unable to install i2c ISR -- falling back to polling mode\n"); - rtems_semaphore_delete( syncsem ); - /* fall back to polling mode */ - syncsem = 0; - } - } else { - syncsem = 0; - } - } - - return RTEMS_SUCCESSFUL; -} - -STATIC rtems_status_code -i2c_start(rtems_libi2c_bus_t *bh) -{ -uint8_t v; -rtems_status_code sc = RTEMS_SUCCESSFUL; - - v = i2c_rd( I2CCR ); - if ( I2CCR_MSTA & v ) { - /* RESTART */ - rd1byte_noack(); - v |= I2CCR_RSTA; - } else { - v |= I2CCR_MSTA; - } - i2c_wr( I2CCR, v ); - - /* On MBB we can only poll-wait (no IRQ is generated) - * and this is also much faster than reading a byte - * (1/2-bit time) so the overhead of an IRQ may not - * be justified. - * OTOH, we can put this off into the 'send_addr' routine - * - - sc = i2c_wait( I2CSR_MBB, I2CSR_MBB ); - */ - - return sc; -} - -STATIC rtems_status_code -i2c_stop(rtems_libi2c_bus_t *bh) -{ - rd1byte_noack(); - - /* STOP */ - i2c_clr( I2CCR, I2CCR_TXAK | I2CCR_MSTA ); - - /* FIXME: should we really spend 1/2 bit-time polling - * or should we just go ahead and hope noone - * else will get a chance to do something to - * the bus until the STOP completes? - */ - return i2c_wait( I2CSR_MBB, 0 ); -} - -STATIC rtems_status_code -i2c_send_addr(rtems_libi2c_bus_t *bh, uint32_t addr, int rw) -{ -uint8_t buf[2]; -int l = 0; -uint8_t read_mask = rw ? 1 : 0; -rtems_status_code sc; - - /* Make sure we are started; (i2c_start() didn't bother to wait - * so we do it here - some time already has expired. - */ - sc = i2c_wait( I2CSR_MBB, I2CSR_MBB ); - - if ( RTEMS_SUCCESSFUL != sc ) - return sc; - - if ( addr > 0x7f ) { - /* 10-bit request; 1st address byte is 0b11110<b9><b8><r/w> */ - buf[l] = 0xf0 | ((addr >> 7) & 0x06) | read_mask; - read_mask = 0; - l++; - buf[l] = addr & 0xff; - } else { - buf[l] = (addr << 1) | read_mask; - l++; - } - - /* - * After sending a an address for reading we must - * read a dummy byte (this actually clocks the first real - * byte on the i2c bus and makes it available in the - * data register so that the first 'read_bytes' operation - * obtains the byte we clock in here [and starts clocking - * the second byte]) to overcome the pipeline - * delay in the hardware (I don't like this design) :-(. - */ - sc = i2c_xfer( 0, buf, l ); - if ( rw && l == sc ) { - sc = i2c_xfer( 1, buf, 1 ); - } - return sc >=0 ? RTEMS_SUCCESSFUL : -sc; -} - -STATIC int -i2c_read_bytes(rtems_libi2c_bus_t *bh, unsigned char *buf, int len) -{ - return i2c_xfer( 1, buf, len ); -} - -STATIC int -i2c_write_bytes(rtems_libi2c_bus_t *bh, unsigned char *buf, int len) -{ - return i2c_xfer( 0, buf, len ); -} - -/********* Driver Glue Vars **********/ - -static rtems_libi2c_bus_ops_t myops = { - init: i2c_init, - send_start: i2c_start, - send_stop: i2c_stop, - send_addr: i2c_send_addr, - read_bytes: i2c_read_bytes, - write_bytes: i2c_write_bytes, -}; - -static rtems_libi2c_bus_t my_bus_tbl = { - ops: &myops, - size: sizeof(my_bus_tbl), -}; - -/********* Global Driver Handle ******/ - -rtems_libi2c_bus_t *mpc8540_i2c_bus_descriptor = &my_bus_tbl; |