Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:56

0001 /* Register i2c bus driver & devices */
0002 
0003 /*
0004  * Authorship
0005  * ----------
0006  * This software ('mvme3100' RTEMS BSP) was created by
0007  *
0008  *     Till Straumann <strauman@slac.stanford.edu>, 2005-2007,
0009  *     Stanford Linear Accelerator Center, Stanford University.
0010  *
0011  * Acknowledgement of sponsorship
0012  * ------------------------------
0013  * The 'mvme3100' BSP was produced by
0014  *     the Stanford Linear Accelerator Center, Stanford University,
0015  *     under Contract DE-AC03-76SFO0515 with the Department of Energy.
0016  *
0017  * Government disclaimer of liability
0018  * ----------------------------------
0019  * Neither the United States nor the United States Department of Energy,
0020  * nor any of their employees, makes any warranty, express or implied, or
0021  * assumes any legal liability or responsibility for the accuracy,
0022  * completeness, or usefulness of any data, apparatus, product, or process
0023  * disclosed, or represents that its use would not infringe privately owned
0024  * rights.
0025  *
0026  * Stanford disclaimer of liability
0027  * --------------------------------
0028  * Stanford University makes no representations or warranties, express or
0029  * implied, nor assumes any liability for the use of this software.
0030  *
0031  * Stanford disclaimer of copyright
0032  * --------------------------------
0033  * Stanford University, owner of the copyright, hereby disclaims its
0034  * copyright and all other rights in this software.  Hence, anyone may
0035  * freely use it for any purpose without restriction.
0036  *
0037  * Maintenance of notices
0038  * ----------------------
0039  * In the interest of clarity regarding the origin and status of this
0040  * SLAC software, this and all the preceding Stanford University notices
0041  * are to remain affixed to any copy or derivative of this software made
0042  * or distributed by the recipient and are to be affixed to any copy of
0043  * software made or distributed by the recipient that contains a copy or
0044  * derivative of this software.
0045  *
0046  * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
0047  */
0048 
0049 #include <rtems.h>
0050 #include <bsp.h>
0051 #include <rtems/bspIo.h>
0052 #include <rtems/libi2c.h>
0053 #include <libchip/i2c-2b-eeprom.h>
0054 #include <libchip/i2c-ds1621.h>
0055 #include <bsp/mpc8540_i2c_busdrv.h>
0056 #include <rtems/libio.h>
0057 #include <rtems/score/sysstate.h>
0058 
0059 #include <stdio.h>
0060 #include <sys/stat.h>
0061 #include <errno.h>
0062 #include <stdarg.h>
0063 
0064 static void
0065 safe_printf (const char *fmt, ...)
0066 {
0067     va_list ap;
0068 
0069     va_start(ap, fmt);
0070     if ( _System_state_Is_up( _System_state_Get() ) )
0071         vfprintf( stderr, fmt, ap );
0072     else
0073         vprintk( fmt, ap );
0074     va_end(ap);
0075 }
0076 
0077 static void
0078 safe_perror(const char *s)
0079 {
0080     safe_printf("%s :%s\n", s, strerror(errno));
0081 }
0082 
0083 
0084 int
0085 BSP_i2c_initialize(void)
0086 {
0087 int busno, succ = 0;
0088 
0089     /* Initialize the library */
0090     if ( rtems_libi2c_initialize() ) {
0091         safe_printf("Initializing I2C library failed\n");
0092         return -1;
0093     }
0094 
0095     /* Register our bus driver */
0096     if ( (busno=rtems_libi2c_register_bus(
0097                     BSP_I2C_BUS0_NAME,
0098                     BSP_I2C_BUS_DESCRIPTOR) ) < 0 ) {
0099         safe_perror("Registering mpc8540 i2c bus driver");
0100         return -1;
0101     }
0102 
0103     /* Now register higher level drivers; note that
0104      * the i2c address in the manual is actually left-shifted
0105      * by one bit, i.e., as it would go on the bus.
0106      */
0107 
0108     /* Use read-only driver for VPD */
0109     if ( rtems_libi2c_register_drv(
0110                 BSP_I2C_VPD_EEPROM_NAME,
0111                 i2c_2b_eeprom_ro_driver_descriptor,
0112                 busno,
0113                 BSP_VPD_I2C_ADDR) < 0 ) {
0114         safe_perror("Registering i2c VPD eeprom driver failed");
0115     } else {
0116         succ++;
0117     }
0118 
0119     /* Use read-write driver for user eeprom -- you still might
0120      * have to disable HW write-protection on your board.
0121      */
0122     if ( rtems_libi2c_register_drv(
0123                 BSP_I2C_USR_EEPROM_NAME,
0124                 i2c_2b_eeprom_driver_descriptor,
0125                 busno,
0126                 BSP_USR0_I2C_ADDR) < 0 ) {
0127         safe_perror("Registering i2c 1st USR eeprom driver failed");
0128     } else {
0129         succ++;
0130     }
0131 
0132     /* Use read-write driver for user eeprom -- you still might
0133      * have to disable HW write-protection on your board.
0134      */
0135     if ( rtems_libi2c_register_drv(
0136                 BSP_I2C_USR1_EEPROM_NAME,
0137                 i2c_2b_eeprom_driver_descriptor,
0138                 busno,
0139                 BSP_USR1_I2C_ADDR) < 0 ) {
0140         safe_perror("Registering i2c 2nd USR eeprom driver failed");
0141     } else {
0142         succ++;
0143     }
0144 
0145     /* The thermostat */
0146     if ( rtems_libi2c_register_drv(
0147                 BSP_I2C_DS1621_NAME,
0148                 i2c_ds1621_driver_descriptor,
0149                 busno,
0150                 BSP_THM_I2C_ADDR) < 0 ) {
0151         safe_perror("Registering i2c ds1621 temp sensor. driver failed");
0152     } else {
0153         succ++;
0154     }
0155 
0156     /* Finally, as an example, register raw access to the
0157      * ds1621. The driver above just reads the 8 msb of the
0158      * temperature but doesn't support anything else. Using
0159      * the raw device node you can write/read individual
0160      * control bytes yourself and e.g., program the thermostat...
0161      */
0162 
0163     if ( mknod(
0164             BSP_I2C_DS1621_RAW_DEV_NAME,
0165             0666 | S_IFCHR,
0166             rtems_filesystem_make_dev_t(rtems_libi2c_major,
0167                   RTEMS_LIBI2C_MAKE_MINOR(busno,BSP_THM_I2C_ADDR))) ) {
0168         safe_perror("Creating device node for raw ds1621 (temp. sensor) access failed");
0169     } else {
0170         succ++;
0171     }
0172 
0173     /* Raw access to RTC */
0174     if ( mknod(
0175             BSP_I2C_DS1375_RAW_DEV_NAME,
0176             0666 | S_IFCHR,
0177             rtems_filesystem_make_dev_t(rtems_libi2c_major,
0178                   RTEMS_LIBI2C_MAKE_MINOR(busno,BSP_RTC_I2C_ADDR))) ) {
0179         safe_perror("Creating device node for raw ds1375 (rtc) access failed");
0180     } else {
0181         succ++;
0182     }
0183 
0184     safe_printf("%i I2C devices registered\n", succ);
0185     return 0;
0186 }