PCIe to I2C Bridge Windows Driver

Hi Peter,

Thanks for the detailed and quick explanation. Really appreciate that :slight_smile: I have few more doubts.

Because of your specific use case, you simply would write an ordinary PCI device driver that controls your I2C controller at the bottom edge, and that takes whatever Reads, Writes, and a Ioctls that you wish to define from your user app at the top edge.

There is another use case to the one I mentioned in my description. There could be a need wherein, instead of the application directly sending command to the controller to control the peripheral beneath, there could be a KMDF or UMDF peripheral driver present for that peripheral device in the OS. So in this case, the application will not directly talk to controller driver. but talks to the peripheral driver.
This could be achieved using the SPB framework if we are static and using the ACPI table to define the devices and the connections. But unfortunately we are not.
Is there anyway to use the SPB framework and escape from using the ACPI table for Controller and peripheral binding? From the documentation I could see that, there are too much dependencies on the ACPI table. For example, to get the target connection settings, the controller driver needs to call SpbTargetGetConnectionParameters. This API is inturn reading through the ACPI table to obtain the device’s settings.

I am thinking if I can somehow achieve the I2C/SPI controller to device binding without ACPI and through some other means, I can reuse the SPB framework and achieve both the use cases.

IS there a way out here??

Thanks,
Ganesh

There could be a need wherein, instead of the application directly sending command to the controller to control the peripheral beneath, there could be a KMDF or UMDF peripheral driver present for that peripheral device in the OS.

Well, that’s almost 100% different from your first use case… And it changes the design COMPLETELY. This is a MUCH harder ask.

If your goal is to allow “standard” WDF drivers to be recognized and loaded for devices that are attached to your controller, I don’t see any reasonable way of avoiding having the device described in the ACPI BIOS. The Client Driver is going to try to open a Remote I/O Target connection to the Controller Driver via the Resource Hub (ACPIEX). ACPIEX is going to try to determine which Controller the Client device is attached to by looking through the ACPI BIOS tables.

If you wanted to be really fancy and complex, I suppose you could:

  • Write a bus driver that enumerates the device(s) connected to your controller. Because there’s no standard way to dynamically enumerate the I2C bus, you’d have to create some method that users specify the device(s) connected to your I2C Controller’s bus (as well as the Client IDs associated with each of those devices). You’ll also need to be clever enough to “fake” a Connection ID for the Client driver to use when it enumerates its resources and attempt to open the connection to the Resource Hub.

  • Write a filter driver that intercepts Open requests that are sent to the Resource Hub, and redirect the open (via STATUS_REPARSE) to your Controller Driver.

  • Ensure your Controller Driver takes into account (that is, effectively “emulates”) the behavior that the Client Driver would expect if it were talking to a Controller Driver via the SPBCx. So, you’ll need to support the “standard” IOCTLs.

I think you’ll be able to avoid actual use of the SPB Controller Extensions entirely if you do the above. They don’t really provide any advantage or help in your case… and I suspect that once the Client Driver gets his Connection ID and has opened a Remote I/O Target to your Controller Driver, it’ll start sending you those standard IOCTLs.

The above ignores the case where the Client device has some other resources other than the I2C Connection. For example, this doesn’t allow you support for connections from the I2C device to a GPIO controller for interrupts (which is pretty common). I really don’t see how you could support interrupts on the Client devices you enumerate… GPIO controller isn’t under your control, after all.

ANYhow… having to load the standard driver (and not just allow an app to do write and read) really turns this from a simple project into what is basically a research project. You’re waaaay out of the standard ways of doing things.

Peter

I really don’t see how you could support interrupts on the Client devices

And here comes the PCIe interface. PCIe is very good with interrupts.
PCI(e) is also more reliable than USB (TL;DR) so it can be preferred for unattended systems (servers, industrial…) even if cost is higher.

–pa

PCIe is very good with interrupts.

From an I2C device that expects to be connected via a GPIO controller?

So, now you’re telling me the OP has also neglected to tell us about the other 50% of his project in which his PCIe card has a GPIO controller on it, and he needs to write control that as well?

Peter

I suspect that the OP still holds some interesting details up his sleeve…
Same logic on the PCIe device that drives I2C can also receive interrupt requests. Not any "GPIO controller. As you and Mr. Roberts wrote previously it can be a simple PCI device, or a custom bus parent . It now depends on the class of the I2C devices - should they use in-box drivers or can be custom as well.

Let my bits go on the wire!
– pa

Not any "GPIO controller.”

The OPs board supports “ordinary” I2C Client devices, which connect to an I2C bus and (perhaps, depending on the device) one or more interrupts. On the interrupt side, Client device expects to talk to a GPIO controller. It’s simple for you to write “ Same logic on the PCIe device that drives I2C can also receive interrupt requests” — but it’s not so simple in the hardware. You need to match logic levels and timing. You need to accommodate the potential for multiple interrupts in the same way you’d accommodate multiple GPIOs… if you’re going to pipe these to multiple MSIs there needs to be some logic involved, right?

I really don’t think it’s as simple as you seem to think. You can’t just wire one or more lines that are designed to pull high or low (depending on the device) to generate an interrupt to your PCIe interface chip. You have an FPGA out there to do all this matching and mapping?

@“Peter_Viscarola_(OSR)” said:

There is no I2C device class, so there is no I2C abstraction API

Except in the land of Windows IoT, where (a) I2C is much more likely to be used as Mr. Roberts alludes, and (b) there’s a very complete abstraction available in C# (at least).

Peter

Exactly right Peter ! In fact as I recall the entire spbclx is now a part of OneCore, not just for Windows IoT. Therefore a PCIe card with a proprietary ic2 host controller could be nicely supported by a proper driver into a well formed i2c class device on Windows 10. Moverover, shall one such exist I may even be interested to order it for future designs.

Mr. Roberts, i2c is a bit slow, yes, but still very important, in some areas in fact is the key for the instrument, such as Tektronix analyzer. One of our systems has five(5) i2c host controllers, since there are many components to communicate with. Yes on Windows 10.

Thanks,
Sergey

@Tim_Roberts said:

Right, because there IS no pre-defined device class for I2C adapters. Any driver you write for this will be a custom device with a custom API.

i2c host controller is not a custom device in Windows 10. i2c host controller is a supported type of device.
SPB Framework Extension (SpbCx)
For example Intel implemented i2c host controller connected to PCIe on Core-i5 based board.

00:15.0 Signal processing controller: Intel Corporation Sunrise Point-LP Serial IO I2C Controller #0 (rev 21)
Subsystem: Intel Corporation Device 7270
Control: I/O- Mem- BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx+
Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- SERR- <PERR- INTx-
Interrupt: pin A routed to IRQ 16

Therefore a PCIe card with a proprietary ic2 host controller could be nicely supported by a proper driver into a well formed i2c class device on Windows 10

Yes, well… that’s true. I2C controllers, in general, are backplane bus type devices (supported by PCI.SYS).

However, the issue is that… regardless of how the Controller is interfaced, the Client devices still need to be listed in the ACPI BIOS, because the I2C bus is not dynamically enumerable.

And, as I said earlier… it’s not typically a matter of JUST I2C. I2C (and SPI) connected devices are often designed to connect to one or more GPIOs to allow them to signal conditions.

Anyhow… your project is turning into something that’s feeling far too complicated to reasonably design via forum posts.

Good luck with your project. It’s an interesting one, for sure.

Peter

Hi Peter and others,

Thanks for spending time in helping me out here. Looks like I triggered a ruckus here ;). I am still a fair bit away from understanding the entire Windows driver architecture. I will go through the suggestions and comments from everyone here and try to progress. I will keep you guys updated in case I break through this.

Thanks again for your time :slight_smile:

Ganesh.

HI All,

I am back :slight_smile:

We had a discussion with marketing and finally we agreed that our I2C/SPI drivers need to be compliant to SPB framework and thus it was also agreed to have the ACPI table of the system to be updated to add our device.

Now that this is fixed and done, I have another query. We have a SPI controller on one of the PCIe endpoint as I pointed out earlier. There are 2 instances of the controller in the same PCIe endpoint. So logically it should be 2 SPB controller driver instances with 2 separate set of registers. So I thought of calling the WDFDeviceCreate and SPB related functions twice in the OnDeviceAdd callback function to create 2 separate instances of SPI controller. But the 2nd WDFDeviceCreate is failing since the FxDeviceInit object is not free.

Since this is a PCIe device, we hit the OnDeviceAdd only once upon the device detection but the requirement is to have 2 SPI controller instances registered to the SPB framework.

Is there a simple way to achieve this?

Thanks in advance,
Ganesh

Hmmmm… so, you have a PCIe device, that provides TWO SPI controllers, with separate sets of registers… but ONE PCIe function? Who designed THAT device? Cuz it’s not PCIe compliant. Does the device do anything ELSE?

Is there a simple way to achieve this?

Simple? No. Can it be done? Yes, and it’s not TOO difficult.

You need to write a bus driver that enumerates each of the SPI controllers, and creates one PDO for each. As a result of reporting these PDO during normal PnP processing, the function driver for the controller will be instantiated on each PDO. Hence EvtDriverDeciceAdd in the function driver will be called twice, once for each controller.

With all due respect, if your company’s major goal is for all this to work properly on Windows, I strongly suggest you hire a consultant who understands how all these pieces fit together… before you design and build hardware and try to write drivers that make life difficult. You’ll likely save yourself a ton of time and effort. You don’t know what you don’t know.

Peter

Something around mf.sys with a resource map? Is this still supported on win10?
https://docs.microsoft.com/en-us/windows-hardware/drivers/multifunction/creating-varying-resource-maps

What I don’t understand at all… if SPB devices must have ACPI descriptors - who enumerates them: ACPI or PCI? Or PCI with ACPI as upper filter?

– pa

It’s not clear to me how it is possible to have an SPB device on a PCI bus. SPB is supposed to include I2C, SPI, MIPI, and UART devices, none of which have any PnP capabilities at all, so ACPI is the only way to expose those devices. I’m not convinced that the PCI bus driver knows how to expose an SPB-like device.

@“Peter_Viscarola_(OSR)” said:
Hmmmm… so, you have a PCIe device, that provides TWO SPI controllers, with separate sets of registers… but ONE PCIe function? Who designed THAT device? Cuz it’s not PCIe compliant. Does the device do anything ELSE?

I am not sure if this is PCIe compliant or not but, I have seen similar posts w.r.t many UARTs being shared in the same PCIe function. Isn’t this similar to that?
Yes this device also has a I2C and UART in separate endpoints. So as a whole this is a PCIe multifunction device considering UART, I2C, SPI being present in the same endpoint but having their own config spaces.

You need to write a bus driver that enumerates each of the SPI controllers, and creates one PDO for each. As a result of reporting these PDO during normal PnP processing, the function driver for the controller will be instantiated on each PDO. Hence EvtDriverDeciceAdd in the function driver will be called twice, once for each controller.

So you mean to say its similar to using the mf.sys? Can I use mf.sys here to take care of creating the PDOs for me?

With all due respect, if your company’s major goal is for all this to work properly on Windows, I strongly suggest you hire a consultant who understands how all these pieces fit together… before you design and build hardware and try to write drivers that make life difficult. You’ll likely save yourself a ton of time and effort. You don’t know what you don’t know.

I can certainly convey this message but i think the ship sailed beyond that point.

@Pavel_A said:
Something around mf.sys with a resource map? Is this still supported on win10?
https://docs.microsoft.com/en-us/windows-hardware/drivers/multifunction/creating-varying-resource-maps

So you are saying that I can use the default mf.sys itself for my device?
The documentation regarding the resource map is not that clear. This is my understanding till now:

  1. Provide an inf for mf.sys which contains the actual PCIe HW IDs, MSI interrupts.
  2. Allocate resources to each of the child some resources of the parent in the parent inf. Like BAR0 - 0 - 200 → SPI1, BAR0 200 - 400 SPI2.
  3. Create two child infs for each of the SPI controller. I am not sure what to put in these INFs apart from the pointers to .sys and cat files.

here comes my doubt, we will have only one .sys file, should i create 2 INFs for 2 SPI controllers?
Is my understanding correct?

What I don’t understand at all… if SPB devices must have ACPI descriptors - who enumerates them: ACPI or PCI? Or PCI with ACPI as upper filter?

My understanding is that the enumeration will be done by the PCI driver and the logical connections between SPB controller and SPB peripherals will be maintained by the ACPI.

Thanks,
Ganesh.

I’m not convinced that the PCI bus driver knows how to expose an SPB-like device.

Well, it doesn’t. The OP said in his most recent post that his card has SPB controllers which are typically just standard PCIE backplane bus architecture devices.

if SPB devices must have ACPI descriptors - who enumerates them: ACPI or PCI?

Here, we’re talking about the client devices I assume. The system developer manually creates entries… one per device… is the SSDT. The entries are enumerated by ACPI — ACPI is the enumerator and bus driver for all things SPB. Because devices on the buses are not dynamically discoverable.

you are saying that I can use the default mf.sys itself

Forget about it. Just write a bus driver using KMDF. Enumerate each of the devices on your card. Then the function driver will load for each of those devices. If the I2C and SPI controllers are “standard” controllers, you could use the in-box drivers for those. Which would be good, because writing drivers for those won’t be easy.

This design still isn’t a “done deal.” I’m not sure how you’re going to describe the controllers in the ACPI SSDT. Remember, they need to be described, too.

I’m really not sure how or if this whole project will work… at all. I’m sure there’s a lot that you haven’t told us. If what you’re trying to do is build an add-in card to allow random users to attach I2C or SPI devices to a Windows machine… forget it: That’s not supported by the OS at this point (it should be, but it’s not). I you’re trying to create an add-in card that OEMs would use, say, in a laptop or an embedded system to attach I2C and SPI devices, you might have a chance. But, as I said, I don’t know how you make that work in ACPI — you’ll need someone who knows a lot more about ACPI than I do, that’s for sure, to tell you how to do this.

Peter

@“Peter_Viscarola_(OSR)” said:

you are saying that I can use the default mf.sys itself

Forget about it. Just write a bus driver using KMDF. Enumerate each of the devices on your card. Then the function driver will load for each of those devices. If the I2C and SPI controllers are “standard” controllers, you could use the in-box drivers for those. Which would be good, because writing drivers for those won’t be easy.

Ok. I am yet to go through Windows documentation for writing a bus driver. Any pointers?

This design still isn’t a “done deal.” I’m not sure how you’re going to describe the controllers in the ACPI SSDT. Remember, they need to be described, too.

Yes. I thought of doing it this way:
_DefinitionBlock (“”, “SSDT”, 2, “OEMID”, "SPBSSDT ", 0x00)
{
//PCI port below which our PCIe controller resides
Scope (_SB.PCI0)
{
Device (SPI0)
{
//0x00010003 → device, function number of the PCIe
Name (ADR, 0x00010003)
}
}
}

Won’t this work? This was before this multiple instance issue came up. Will this require a change if we introduce a bus driver in between?

I’m really not sure how or if this whole project will work… at all. I’m sure there’s a lot that you haven’t told us. If what you’re trying to do is build an add-in card to allow random users to attach I2C or SPI devices to a Windows machine… forget it: That’s not supported by the OS at this point (it should be, but it’s not). I you’re trying to create an add-in card that OEMs would use, say, in a laptop or an embedded system to attach I2C and SPI devices, you might have a chance. But, as I said, I don’t know how you make that work in ACPI — you’ll need someone who knows a lot more about ACPI than I do, that’s for sure, to tell you how to do this.

Sorry I forgot to add one more point. we spoke to Marketing team and they conveyed that this PCIe device cannot be a pluggable module. Its present by default in a system upon bootup. So that’s one less problem. Let me give a gist of the product without revealing too much. Its a PCIe device that has one multi-function endpoint where we have UART,SPI,I2C and GPIO controllers. Each of these is an independent function. i.e. they have their own config space. Here in case of SPI alone, there are 2 SPI controller instances shared in the same function.

Also there are cases where we need user apps to directly interact with our controller drivers to control the peripherals beneath I2C/SPI without a peripheral driver. I went through this link which says its feasible through a RHProxy driver:
https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/enable-usermode-access

Understood that even this needs ACPI entry beneath a RHProxy node. Anyway we are fine to go with editing the ACPI table.

What do you think?

Ganesh.

this PCIe device cannot be a pluggable module.

Well, that makes all the difference, actually. That makes this “just another I2C and SPI controller”… which is no big thing.

PCIe device that has one multi-function endpoint where we have UART,SPI,I2C and GPIO controllers.

Wait. One multifunction endpoint? That means it surfaces separate devices for each of the controllers, and this will be automatically handled by the PCI bus driver.

You therefore have no work to do.

Peter

As someone who’s done a fair amount of hooking up strange devices to PC’s, its looking like you’re starting to drift into the weeds a bit (actually your first post was the most clear). I would take a step back and determine

a) what is the target hardware (commercial off the shelf, like a laptop) or customizable (you’ll order something from Wistron or WiPro)
b) what exactly are you trying to accomplish … you’ve mentioned a lot of things here: on the I2C network are you trying to be a master, a slave or a monitor? Same on SPI; master, slave or monitor? GPIO are you trying to read from a GPIO expander (which would mean I2C) or are you really going to have some wires that you can toggle? For UART is this RS232, RS485 or something else and are you going to be a master, a slave or a monitor?
c) what is the end user going to be … is this targeted for a system integrator (who will likely want to interact with this at the device level) or at a user level (something similar to ReadWriteEverything, or the SIV64 display?
d) what is the timeframe that you’re looking at, hours/ days/ weeks/ months?

Generally, I have found that letting a PC be a PC, Windows be Windows and keeping everything at arms length works best. You’ve got several technologies here (I2C, SPI and GPIO) which really aren’t found on regular hardware and you’re asking Windows to do some things that it’s really not designed to do (such as directly accessing hardware) … that all spells trouble, especially when a new version of Windows comes out every six months and you get to retest everything

If I had a similar request (I2C/ SPI/ GPIO/ UART on the back end) I would spec out a PCIe card which would be plugged into an m2 connector (which most motherboards and laptops have) and have two FPGA’s on that: one FPGA for the PCIe IP and one FPGA to handle all of the strange stuff. Write a single KMDF PCIe function driver which can interact with that card and you’re done … that way all of the I2C/ SPI/ GPIO/ UART stuff is handled by the back end FPGA (which is programmed by the front end FPGA, so you can customize it as you wish), the front end FPGA handles the PCIe stuff and all you need to do is expose some BAR’s and interrupts for the KMDF driver to handle as needed …

But again, a clear idea of what you’re wanting to do, what it’s running on and who’s going to be using it will help you immeasurably …

1 Like

You therefore have no work to do.
But he still has two SPIs in one PCIe function. However if ACPI can describe SPB devices as children of given PCI bus/device/function then the ACPI patch is all what is remaining. No tinkering with mf.sys.