Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:04

0001 /* Trivial i2c driver for reading "2-byte eeproms".
0002  * On 'open' the read-pointer is reset to 0, subsequent
0003  * read operations slurp data from there...
0004  */
0005 
0006 /*
0007  * Authorship
0008  * ----------
0009  * This software was created by
0010  *     Till Straumann <strauman@slac.stanford.edu>, 2005,
0011  *     Stanford Linear Accelerator Center, Stanford University.
0012  *
0013  * Acknowledgement of sponsorship
0014  * ------------------------------
0015  * This software was produced by
0016  *     the Stanford Linear Accelerator Center, Stanford University,
0017  *     under Contract DE-AC03-76SFO0515 with the Department of Energy.
0018  *
0019  * Government disclaimer of liability
0020  * ----------------------------------
0021  * Neither the United States nor the United States Department of Energy,
0022  * nor any of their employees, makes any warranty, express or implied, or
0023  * assumes any legal liability or responsibility for the accuracy,
0024  * completeness, or usefulness of any data, apparatus, product, or process
0025  * disclosed, or represents that its use would not infringe privately owned
0026  * rights.
0027  *
0028  * Stanford disclaimer of liability
0029  * --------------------------------
0030  * Stanford University makes no representations or warranties, express or
0031  * implied, nor assumes any liability for the use of this software.
0032  *
0033  * Stanford disclaimer of copyright
0034  * --------------------------------
0035  * Stanford University, owner of the copyright, hereby disclaims its
0036  * copyright and all other rights in this software.  Hence, anyone may
0037  * freely use it for any purpose without restriction.
0038  *
0039  * Maintenance of notices
0040  * ----------------------
0041  * In the interest of clarity regarding the origin and status of this
0042  * SLAC software, this and all the preceding Stanford University notices
0043  * are to remain affixed to any copy or derivative of this software made
0044  * or distributed by the recipient and are to be affixed to any copy of
0045  * software made or distributed by the recipient that contains a copy or
0046  * derivative of this software.
0047  *
0048  * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
0049  */
0050 
0051 
0052 #include <rtems.h>
0053 #include <rtems/libi2c.h>
0054 
0055 #include <libchip/i2c-2b-eeprom.h>
0056 #include <rtems/libio.h>
0057 
0058 #define EEPROM_PG_SZ    32
0059 #define ALGN(x) (((uint32_t)(x) + EEPROM_PG_SZ) & ~(EEPROM_PG_SZ-1))
0060 
0061 static rtems_status_code
0062 send_file_ptr (rtems_device_minor_number minor, unsigned pos, int tout)
0063 {
0064   int sc;
0065   unsigned char bytes[2];
0066 
0067   bytes[0] = (pos >> 8) & 0xff;
0068   bytes[1] = (pos) & 0xff;
0069 
0070   /* poll addressing the next page; if 'tout' is <=0 we only try once
0071    * and return the status. If 'tout' is positive, we try 'tout' times
0072    * and return RTEMS_TIMEOUT if it didnt work
0073    */
0074   while ((sc = rtems_libi2c_start_write_bytes (minor, bytes, 2)) < 0) {
0075     if (--tout <= 0)
0076       return tout ? -sc : RTEMS_TIMEOUT;
0077     rtems_task_wake_after (1);
0078   }
0079   return RTEMS_SUCCESSFUL;
0080 }
0081 
0082 static rtems_status_code
0083 i2c_2b_eeprom_write (rtems_device_major_number major,
0084                      rtems_device_minor_number minor, void *arg)
0085 {
0086   rtems_libio_rw_args_t *rwargs = arg;
0087   unsigned off = rwargs->offset;
0088   int cnt = rwargs->count;
0089   unsigned char *buf = (unsigned char *)rwargs->buffer;
0090   int sc;
0091   unsigned end;
0092   int l;
0093 
0094   if (cnt <= 0)
0095     return RTEMS_SUCCESSFUL;
0096 
0097   if ((sc = send_file_ptr (minor, off, 0)))
0098     return sc;
0099 
0100   do {
0101     /* write up to next page boundary */
0102     end = ALGN (off);
0103     l = end - off;
0104     if (l > cnt)
0105       l = cnt;
0106 
0107     sc = rtems_libi2c_write_bytes (minor, buf, l);
0108     if (sc < 0)
0109       return -sc;
0110 
0111     sc = rtems_libi2c_send_stop (minor);
0112     if (sc)
0113       return sc;
0114 
0115     rwargs->bytes_moved += l;
0116 
0117     buf += l;
0118     cnt -= l;
0119     off += l;
0120 
0121     /* poll addressing the next page */
0122     if ((sc = send_file_ptr (minor, off, 100)))
0123       return sc;
0124 
0125   } while (cnt > 0);
0126 
0127   return rtems_libi2c_send_stop (minor);
0128 }
0129 
0130 static rtems_status_code
0131 i2c_2b_eeprom_read (rtems_device_major_number major,
0132                     rtems_device_minor_number minor, void *arg)
0133 {
0134   int sc;
0135   rtems_libio_rw_args_t *rwargs = arg;
0136 
0137   if (RTEMS_SUCCESSFUL != (sc = send_file_ptr (minor, rwargs->offset, 0)))
0138     return -sc;
0139 
0140   sc = rtems_libi2c_start_read_bytes(
0141     minor,
0142     (unsigned char *)rwargs->buffer,
0143     rwargs->count
0144   );
0145 
0146   if (sc < 0) {
0147     rwargs->bytes_moved = 0;
0148     return -sc;
0149   }
0150   rwargs->bytes_moved = sc;
0151 
0152   return rtems_libi2c_send_stop (minor);
0153 }
0154 
0155 static rtems_driver_address_table myops = {
0156   .read_entry =  i2c_2b_eeprom_read,
0157   .write_entry = i2c_2b_eeprom_write,
0158 };
0159 
0160 static rtems_libi2c_drv_t my_drv_tbl = {
0161   .ops =         &myops,
0162   .size =        sizeof (my_drv_tbl),
0163 };
0164 
0165 /* provide a second table for R/O access */
0166 static rtems_driver_address_table my_ro_ops = {
0167   .read_entry =  i2c_2b_eeprom_read,
0168 };
0169 
0170 static rtems_libi2c_drv_t my_ro_drv_tbl = {
0171   .ops =         &my_ro_ops,
0172   .size =        sizeof (my_ro_drv_tbl),
0173 };
0174 
0175 
0176 rtems_libi2c_drv_t *i2c_2b_eeprom_driver_descriptor = &my_drv_tbl;
0177 rtems_libi2c_drv_t *i2c_2b_eeprom_ro_driver_descriptor = &my_ro_drv_tbl;