summaryrefslogtreecommitdiffstats
path: root/Vlv2TbltDevicePkg/Include/Protocol/I2cMasterMcg.h
blob: f239b5ec2fd6898f4dcf839bcc2bba73a0d21e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*++

  Copyright (c) 2004  - 2014, Intel Corporation. All rights reserved.<BR>
                                                                                   

  This program and the accompanying materials are licensed and made available under

  the terms and conditions of the BSD License that accompanies this distribution.  

  The full text of the license may be found at                                     

  http://opensource.org/licenses/bsd-license.php.                                  

                                                                                   

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,            

  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.    

                                                                                   



  \section I2cDriverStack       I2C Driver Stack

  The following is a representation of the I<sup>2</sup>C (I2C)
  driver stack and an I2C bus layout.

  <code><pre>
              +-----------------+
              |   Application   |
              +-----------------+
                       |
                       | Third Party or UEFI
                       |
                       V
 +--------+   +-----------------+
 | Slave  |   |   Third Party   |
 | Driver |   |   I2C Device    |
 |        |   |     Driver      |
 +--------+   +-----------------+
      |                |
      |          BUS   |
      |                |
      |                V
      |       +-----------------+
      |       | I2C Bus Driver  |------------------.
      |       +-----------------+                  |
      |                |                           |
      |         HOST   |          BUS              |
      |                |          CONFIGURATION    |
SLAVE |                V          MANAGEMENT       | ACPI
      |       +-----------------+                  |
      |       | I2C Host Driver |----------.       |
      |       +-----------------+          |       |
      |                |                   |       |
      |        MASTER  |                   V       V
      |                |               +-------=-------------+
      |                V               | I2C Platform Driver |
      |       +-----------------+      +---------------------+
      `------>| I2C Port Driver |               |      |
              +-----------------+               |      |
                       |                        |      |
            Software   |                        |      |
            --------------------------------------------------
            Hardware   |                        |      |
                       |                        |      |
                       V                        |      |
              +-----------------+               |      |
              | I2C Controller  |               |      |
              +-----------------+               |      |
                       |                        |      |
            -----------------------             |      |
            I2C Bus    |                        |      |
                       |    +------------+      |      |
                       +----| High speed |      |      |
                       |    | I2C device |      |      |
                       |    |    0x01    |      |      |
                       |    +------------+      |      |
                       |                        |      |
                  +---------+  0                |      |
                  | Switch  |<------------------`      |
                  +---------+  1                       |
                       |                               |
                       |    +------------+             |
                       +----| Fast speed |             |
                       |    | I2C device |             |
                       |    |    0x02    |             |
                       |    +------------+             |
                       |                               |
                +-------------+                        |
                | Multiplexer |<-----------------------`
                +-------------+
                 0 |       | 1
                   |       |
                   |       |
                   |       |    +-------------+
                   |       +----| Third Party |
                   |       |    | I2C Device  |
                   |       |    |  0x03, 0x04 |
                   |       |    +-------------+
                   |       |
                   |
                   |            +-------------+
                   +------------| Third Party |
                   |            | I2C Device  |
                   |            |  0x03, 0x04 |
                   |            +-------------+
                   |
  </pre></code>

  The platform hardware designer chooses the bus layout based upon
  the platform, I2C chip and software requirements.  The design uses
  switches to truncate the bus to enable higher speed operation for a
  subset of devices which are placed closer to the controller.  When the
  switch is on, the extended bus must operate at a lower speed.  The
  design uses multiplexer to create separate address spaces enabling
  the use of multiple devices which would otherwise have conflicting
  addresses. See the
  <a href="http://www.nxp.com/documents/user_manual/UM10204.pdf">I<sup>2</sup>C
  Specification</a> for more details.

  N.B. Some operating systems may prohibit the changing of switches
  and multiplexers in the I2C bus.  In this case the platform hardware
  and software designers must select a single I2C bus configuration
  consisting of constant input values for the switches and multiplexers.
  The platform software designer must then ensure that this I2C bus
  configuration is enabled prior to passing control to the operating
  system.

  The platform hardware designer needs to provide the platform software
  designer the following data for each I2C bus:

  1.  Which controller controls this bus

  2.  A list of logic blocks contained in one or more I2C devices:

      a.  I2C device which contains this logic block

      b.  Logic block slave address

      c.  Logic block name

  3.  For each configuration of the switches and multiplexer

      a.  What is the maximum speed of operation

      b.  What devices are accessible

  4.  The settings for the switches and multiplexers when control is
      given to the operating system.

  \section ThirdPartyI2cDrivers   Third Party I2C Drivers

  This layer is I2C chip specific but platform and host controller
  independent.

  Third party I2C driver writers, typically silicon vendors, need
  to provide:

  1.  The device path node data that is used to select their
      driver.

  2.  The order for the blocks of logic that get referenced
      by the entries in the slave address array.

  3.  The hardware version of the I2C device, this value is passed
      to the third party I2C driver to enable it to perform work
      arounds for the specific hardware version.  This value should
      match the value in the ACPI _HRV tag.

  The third party I2C driver uses relative addressing to abstract
  the platform specific details of the I2C device.  Using an
  example I2C device containing an accelerometer and a magnetometer
  which consumes two slave addresses, one for each logic block.  The
  third party I2C driver writer may choose to write two drivers, one
  for each block of logic, in which case each driver refers to the
  single I2C slave address using the relative value of zero (0).
  However if the third party I2C driver writer chooses to write a
  single driver which consumes multiple slave addresses then the
  third party I2C driver writer needs to convey the order of the
  I2C slave address entries in the slave address array to the
  platform software designer.  For the example:

      0: Accelerometer

      1: Magnetometer

  The platform hardware designer picks the actual slave addresses
  from the I2C device's data sheet and provides this information
  to the platform software designer.  The platform software designer
  then places the slave addresses into the slave address array in the
  order specified by the third party I2C driver writer.  The third
  party driver I2C writer then indirectly references this array by
  specifying the index value as the relative slave address.  The
  relative value always starts at zero (0) and its maximum value is
  the number of entries in slave address array minus one.

  The slave address is specified as a 32-bit integer to allow room
  for future slave address expansion.  Only the port driver knows
  the maximum slave address value.  All other drivers and
  applications must look for the EFI_NOT_FOUND status for the
  indication that the maximum slave address was exceeded.

  \section I2cBusDriver         I2C Bus Driver

  This layer is platform, host controller, and I2C chip independent.

  The I2C bus driver creates a handle for each of the I2C devices
  described within the platform driver.  The I2C controller's device
  path is extended with the device path node provided by the platform
  driver and attached to the handle.  The third party I2C device driver
  uses the device path to determine if it may connect.  For ACPI nodes,
  the third party I2C driver should use the CID or CidString value.

  The I2C bus driver validates the relative address for the I2C device
  and then converts the relative value to an actual I2C slave address.
  The request is then passed to the I2C host driver.

  \section I2cHostDriver        I2C Host Driver

  This layer is platform, host controller, and I2C chip independent.

  N.B. For proper operation of the I2C bus, only the I2C bus driver
  and the I2C test application should connect to the I2C host driver
  via the EFI_I2C_HOST_DRIVER_PROTOCOL.

  The I2C host driver may access any device on the I2C bus.  The I2C
  host driver has the following responsibilities:

  1.  Limits the number of requests to the I2C port driver to one.
      The I2C host driver holds on to additional requests until the
      I2C port driver is available to process the request.  The I2C
      requests are issued in FIFO order to the I2C port driver.

  2.  Enable the proper I2C bus configuration before starting the
      I2C request on the I2C port driver

  I2C devices are addressed as the tuple: BusConfiguration:SlaveAddress.
  I2C bus configuration zero (0) is the portion of the I2C bus that
  connects to the host controller.  The bus configuration specifies
  the control values for the switches and multiplexers in the I2C bus.
  After the switches and multiplexers are properly configured, the I2C
  controller uses the slave address to access the requested I2C device.

  Since the I2C driver stack supports asynchronous operations this
  layer maintains a queue of I2C requests until the I2C controller
  is available them.  When a request reaches the head of the queue
  the necessary bus configuration is enabled and then the request
  is sent to the I2C port driver.

  \section I2cPortDriver        I2C Port Driver

  This layer is I2C controller specific but platform independent.

  This layer manipulates the I2C controller to perform an operation
  on the I2C bus.  This layer does not configure the I2C bus so it
  is up to the caller to ensure that the I2C bus is in the proper
  configuration before issuing the I2C request.

  This layer typically needs the following information:

  1.  Host controller address
  2.  Controller's input clock frequency

  Depending upon the I2C controller, more data may be necessary.
  This layer may use any method to get these values: hard coded
  values, PCD values, or may choose to communicate with the platform
  layer using an undefined mechanism to get these values.

  If the I2C port driver requires data from the platform driver then
  the I2C port driver writer needs to provide the platform interface
  details to the platform software designer.

  \section I2cPlatformDriver    I2C Platform Driver

  When enabling access to I2C devices within UEFI, this driver
  installs the EFI_I2C_ACPI_PROTOCOL to provide the I2C device
  descriptions to the I2C bus driver using the EFI_I2C_DEVICE
  structure.  These descriptions include the bus configuration
  number required for the I2C device, the slave address array
  and the device path.

  The EFI_I2C_BUS_CONFIGURATION_MANAGEMENT protocol is optional.
  This protocol needs to be specified under the following conditions:

  1.  The I2C bus must operate at a frequency greater than 100 KHz
  2.  The I2C bus contains switches or multiplexers.

  The EFI_I2C_BUS_CONFIGURATION_MANAGEMENT protocol enables the
  I2C host driver to call into the I2C platform driver to enable
  a specific I2C bus configuration and set its maximum clock speed.

  The platform software designer collects the data requirements
  from third party I2C driver writers, the I2C controller
  driver writer, the EFI_I2C_ACPI_PROTOCOL and
  EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL.  The platform
  software designer gets the necessary data from the platform
  hardware designer.  The platform software designer then builds
  the data structures and implements the necessary routines to
  construct the I2C platform driver.

  \section I2cSwitches          Switches and Multiplexers

  There are some I2C switches and I2C multiplexers where the control
  is done via I2C commands.  When the control inputs come via the
  same I2C bus that is being configured then the platform driver must
  use the EFI_I2C_MASTER_PROTOCOL that is passed to the platform
  driver.  While the I2C host driver makes the call to the I2C
  platform driver to configure the bus, the host driver keeps the
  I2C port driver idle, to allow the I2C platform driver preform
  the necessary configuration operations.

  If however the configuration control is done via and I2C device
  connected to a different I2C bus (host controller), then it is
  possible for the platform software designer may choose between
  the following:

  1.  Call into a third party I2C driver to manipulate the I2C
      bus control device.
  2.  Call into the EFI_I2C_BUS_PROTOCOL if no third party I2C
      driver exists for the I2C bus control device
  3.  Call into the EFI_I2C_HOST_PROTOCOL if the platform does
      not expose the I2C bus control device.

**/

#ifndef __I2C_MASTER_H__
#define __I2C_MASTER_H__

/**
  Declare the forward references

**/
typedef struct _EFI_I2C_MASTER_PROTOCOL   EFI_I2C_MASTER_PROTOCOL;  ///<  I2C master protocol

///
/// I2C device operation
///
/// This structure provides the information necessary for an operation
/// on an I2C device
///
typedef struct {
  ///
  /// Number of bytes to send to the I2C device
  ///
  UINT32 WriteBytes;

  ///
  /// Number of bytes to read, set to zero for write only operations
  ///
  UINT32 ReadBytes;

  ///
  /// Address of the buffer containing the data to send to the I2C device.
  /// The WriteBuffer must be at least WriteBytes in length.
  ///
  UINT8 *WriteBuffer;

  ///
  /// Address of the buffer to receive data from the I2C device. Use NULL
  /// for write only operations.  The ReadBuffer must be at least ReadBytes
  /// in length.
  ///
  UINT8 *ReadBuffer;

  ///
  /// Timeout for the I2C operation in 100 ns units
  ///
  UINT32 Timeout;
} EFI_I2C_REQUEST_PACKET;


/**
  Set the I2C controller bus clock frequency.

  This routine must be called at or below TPL_NOTIFY.

  The software and controller do a best case effort of using the specified
  frequency for the I2C bus.  If the frequency does not match exactly then
  the controller will use a slightly lower frequency to avoid
  exceeding the operating conditions for any of the I2C devices on the bus.
  For example if 400 KHz was specified and the controller's divide network
  only supports 402 KHz or 398 KHz then the controller would be set to 398
  KHz.  However if the desired frequency is 400 KHz and the controller only
  supports 1 MHz and 100 KHz then this routine would return EFI_UNSUPPORTED.

  @param[in] This           Address of an EFI_I2C_MASTER_PROTOCOL
                            structure
  @param[in] BusClockHertz  New I2C bus clock frequency in Hertz

  @retval EFI_SUCCESS       The bus frequency was set successfully.
  @retval EFI_UNSUPPORTED   The controller does not support this frequency.

**/
typedef
EFI_STATUS
(EFIAPI *EFI_I2C_MASTER_BUS_FREQUENCY_SET) (
  IN CONST EFI_I2C_MASTER_PROTOCOL *This,
  IN UINTN BusClockHertz
  );

/**
  Reset the I2C controller and configure it for use

  This routine must be called at or below TPL_NOTIFY.

  The I2C controller is reset and the I2C bus frequency is set to 100 KHz.

  @param[in]     This       Address of an EFI_I2C_MASTER_PROTOCOL
                            structure

**/
typedef
VOID
(EFIAPI *EFI_I2C_MASTER_RESET) (
  IN CONST EFI_I2C_MASTER_PROTOCOL *This
  );

/**
  Start an I2C operation on the host controller

  This routine must be called at or below TPL_NOTIFY.  For synchronous
  requests this routine must be called at or below TPL_CALLBACK.

  This function initiates an I2C operation on the controller.

  The operation is performed by selecting the I2C device with its slave
  address and then sending all write data to the I2C device.  If read data
  is requested, a restart is sent followed by the slave address and then
  the read data is clocked into the I2C controller and placed in the read
  buffer.  When the operation completes, the status value is returned and
  then the event is set.

  N.B. The typical consumer of this API is the I2C host driver.
  Extreme care must be taken by other consumers of this API to
  prevent confusing the third party I2C drivers due to a state
  change at the I2C device which the third party I2C drivers did
  not initiate.  I2C platform drivers may use this API within
  these guidelines.

  N.B. This API supports only one operation, no queuing support
  exists at this layer.  This API assumes that the I2C bus is in
  the correct configuration for the I2C request.

  @param[in] This           Address of an EFI_I2C_MASTER_PROTOCOL
                            structure
  @param[in] SlaveAddress   Address of the device on the I2C bus.
  @param[in] Event          Event to set for asynchronous operations,
                            NULL for synchronous operations
  @param[in] RequestPacket  Address of an EFI_I2C_REQUEST_PACKET
                            structure describing the I2C operation
  @param[out] I2cStatus     Optional buffer to receive the I2C operation
                            completion status

  @retval EFI_SUCCESS           The operation completed successfully.
  @retval EFI_ABORTED           The request did not complete because the driver
                                was shutdown.
  @retval EFI_BAD_BUFFER_SIZE   The WriteBytes or ReadBytes buffer size is too large.
  @retval EFI_DEVICE_ERROR      There was an I2C error (NACK) during the operation.
                                This could indicate the slave device is not present.
  @retval EFI_INVALID_PARAMETER RequestPacket is NULL
  @retval EFI_INVALID_PARAMETER TPL is too high
  @retval EFI_NOT_FOUND         SlaveAddress exceeds maximum address
  @retval EFI_NOT_READY         I2C bus is busy or operation pending, wait for
                                the event and then read status pointed to by
                                the request packet.
  @retval EFI_NO_RESPONSE       The I2C device is not responding to the
                                slave address.  EFI_DEVICE_ERROR may also be
                                returned if the controller cannot distinguish
                                when the NACK occurred.
  @retval EFI_OUT_OF_RESOURCES  Insufficient memory for I2C operation
  @retval EFI_TIMEOUT           The transaction did not complete within an internally
                                specified timeout period.

**/
typedef
EFI_STATUS
(EFIAPI *EFI_I2C_MASTER_START_REQUEST) (
  IN CONST EFI_I2C_MASTER_PROTOCOL *This,
  IN UINTN SlaveAddress,
  IN EFI_EVENT Event OPTIONAL,
  IN CONST EFI_I2C_REQUEST_PACKET *RequestPacket,
  OUT EFI_STATUS *I2cStatus OPTIONAL
  );

///
/// I2C master mode protocol
///
/// This protocol manipulates the I2C host controller to perform transactions as a
/// master on the I2C bus using the current state of any switches or multiplexers
/// in the I2C bus.
///
struct _EFI_I2C_MASTER_PROTOCOL {
  ///
  /// Set the clock frequency for the I2C bus
  ///
  EFI_I2C_MASTER_BUS_FREQUENCY_SET BusFrequencySet;

  ///
  /// Reset the I2C host controller
  ///
  EFI_I2C_MASTER_RESET Reset;

  ///
  /// Start an I2C transaction in master mode on the host controller
  ///
  EFI_I2C_MASTER_START_REQUEST StartRequest;

  ///
  /// The maximum number of bytes the I2C host controller
  /// is able to receive from the I2C bus.
  ///
  UINT32 MaximumReceiveBytes;

  ///
  /// The maximum number of bytes the I2C host controller
  /// is able to send on the I2C bus.
  ///
  UINT32 MaximumTransmitBytes;

  ///
  /// The maximum number of bytes in the I2C bus transaction.
  ///
  UINT32 MaximumTotalBytes;
};

///
/// GUID for the EFI_I2C_MASTER_PROTOCOL
///
extern EFI_GUID gEfiI2cMasterProtocolGuid;

#endif  //  __I2C_MASTER_H__