 Good afternoon My name is Marek Behun and I'm going to talk about what I have been working on for the last few months in at work And that is about Porting you would to a modular device a device which into which different modules can be plugged like Usb or something like that, but it doesn't work like usb. So a different approach had to be chosen Here we have the contents about the presentation So first about me I was born in Slovakia 1991 using Linux from 14 currently I am studying computer science in Prague and For the last two years I have been working at CZ nick on a project to this and The problem which this presentation is about Was for solved for tourist mocks So What is device three I don't know how people here are proficient in the you would so Let's hope this is not too much for you The device three The operating system kernel has to know how different computer Parts components are on a board are connected to each other and for this device three was The standard for device three was created device three is a The name suggests is a graph in which each note represents one device as properties about What what type of device it is which driver should be used and Which interrupt lines can are connected to the to the device and so on Actually, it's not a three in graph theory because The edges can form a directed cycle sometimes so so it should be maybe called the device graph Well, what if some components are pluggable For example USB is pluggable into into each device which supports USB if you have a router or Raspberry Pi or You can also plug PCI cards PCI express cards in computers and Nowadays SD IO can be used So we can plug a Wi-Fi card into the slot for SD cards But what if the components which we are talking about aren't Plucked with via such a bus that that supports Such a change Then device three has to be changed if you have USB devices you do not have to tell the operating system Which USB device is connected because so USB supports or self identification so the operating system can can Read from the USB device what type of device it is but And also from from SD IO and PCI express, but if you have for example Raspberry Pi with the if you bury shield or If you have There are many many kinds of shield now now. I have read about TV head for Raspberry Pi If there's something like that, then you have to change the device tree which with which You boot boots the boots Linux So and our device exposes several Interfaces here like USB PCI and ethernet via one connector And There are several different modules which can be connected to the device and they can also be connected in Some of them can be connected multiple times for example switch module can be connected more times and then you have multiple ports Ethernet ports The topology of how the Topology of how these modules are connected can be read via SPI via a SPI shift register And the problem I was solving was how to let the kernel know in an elegant way Which modules are connected Normally for this kind of thing there are device tree overlays device tree overlays are Special kind of device trees device tree binaries which the bootloader can Overlay over normal device tree And It will somehow change the the original device tree binary It will for example, it will add Some devices or Connects create some edges in the device tree so that the kernel will know that something is connected additionally Here we here is a screenshot of listing of all the different Device tree overlays supported by raspberry pi. So if you if you look into do Slash boot directory slash slash overlays in on your raspberry pi You can find all these overlays and and more here are only 14 listed So the question is can we use? device tree overlays for each module on our board and the answer is no because the parameters in the for the device tree for each module are Depend on not only on which module this is but also where in the topology disconnected so if it's if it is on the second place after some different module then The parameters have to change for for this module for example if I have module for the fourth one for SFP cage It's dependent on if it is on for example on the third place or if it is directly connected to the CPU module because There are GPIOs on the SFP cage and the GPIOs can be read via the SPI line via which the The topology is also read so So we can't use device tree overlays for for this problem really At first I actually use device tree overlays, but But as you can see the number of different device trees device tree overlays grows rapidly because of each configuration has to have its own Overlay so for example if I connect three three switches of With chipset peri dot then I have to have different device tree overlay as when I connect only two and This this will grow more rapidly because the SFP Module can also Will also multiply the number by by two so So we could use device tree overlays, but it's not very nice and elegant fortunately you would has this feature of That each board specific code can use so for different boards in the you would source you have a Special code for booting them and one of the function one of the functions you can write for for your board is FT board setup which will change Which will pass you the pointer to the device tree loaded by the user or by the script booting the operating system and In this function you can change the device tree Fix it or something Remove nodes create nodes and so on So at first I tried to Create the whole whole nodes for each module In in this function, so the main device tree in the kernel For for the kernel didn't contain nodes for for the modules only for the CPU module not for the pluggable modules and the my code in in FT board setup or Created all the know all the nodes for all the connected modules, but the result was almost 500 1500 lines of ugly C code mmm ugly If if I wanted to check for every possible error, then it was ugly, so And many times it was the same thing, but something was different so you couldn't write a specific a special function for for for everything It couldn't be squashed in seven by by using functions so I Asked myself how to do it more So so the code how to do it so that the code will look more nice and I Have decided that the right metric was minimizing number of the changes that the code in you would has to do or in the device three So that it can boot correctly the device so I Decided that I will write nodes for every module inside the main device tree in Linux But I I will use status disabled for for those nodes so that kernel will ignore them on default and on boot time You put in this FT board setup function will just look at the SP SP I line Discover which modules are connected and it will just change Statues to okay on on those nodes which are connected and also Sometimes it has to change something more because of what I talked about Some slides ago because different some some modules need special configuration about Because of the position where where they are so the result from this was Only under 250 lines of additional C code I have a screenshot here It looks in my opinion. What's nicer than the original 1500 lines We also add 600 lines of device three code in the main device three in kernel and The Status of this is that the patches some of them are already Reviewed on kernel mailing lists, so it looks like it will be accepted into kernel as well And it is it was already reviewed in you would Here in this screen so you can see that I Have to change the name of the node which Which Exposes GPIOs for the SFP module so The advantages and disadvantages of this solution Well in my opinion the advantages are that there is more there is less C code new C code It is also more readable I am I am Actually had the 1500 lines of code Few months ago, but I didn't know that I will be making this presentation and I removed it But if I had it you you would see that the code was ugly and Another advantage in my opinion is that when some developer makes a change in In the in a driver in the kernel for example for the Switch in the DSA subsystem and this change would have to also change Device trees somehow Then if the whole device tree is in the kernel in the kernel Repository, then they will also change it and I I won't have to change On my own The disadvantages Well where we are not using overlays and they are meant for this kind of thing, but They couldn't be used in Efficient ways of so why I this I decided not to use them a bonus slide One problem which I had to solve which I Needed to find solution for on our board Serdes lines The driver for any initialization of Serdes lines is Loaded before SPI driver this means that Serdes lines are Initialized before and and When I initialize the Serdes lines I Have to know the speed for each line for example if SFP module is connected then the corresponding Serdes line has to be For one gigabit, but when When switch module is connected the corresponding line has to be at 2.5 gigabits per second and when The Serdes initialization is done before the SPI and I cannot know which module is connected so There were two solutions for this one solution is was to write a proper driver for Serdes initially, sorry initialization By proper I mean that the SFP node in you would Should in the in the device 3 should have a pointer to the Serdes line and the driver then will would have Known which speed to use if it if it is connected to the SFP line on in the device 3 It would have known that it should enable the Serdes line on 1 gigabit and if The switch node is connected then it would have known it should enable it on 2.5, but Writing Completely the writing the Serdes initialization code was too much work for my time so I decided that I will write For I will solve this problem by By writing my own tiny implementation implementation of SPI communication it is Only 35 lines of code I write to some registers directly on the CPU and Then read some registers and I have read The first ten bytes from the SPI so that then I know which modules are connected it is not This solution is not Very Elegant because Because I have to There is a proper implementation of SPI communication in a wood and I had to write My own so it is called the duplication, but this can be removed when when we have Correct Serdes initialization driver. Thank you. Okay, that's all from me any questions Yes Our device does not support hot plug-in only booting I I have actually talked today with a colleague If it was possible if it were possible The problem is that our device if you connect something the voltage will drop for a little time and the CPU will reset but we have been thinking about if we could use we could do hot plug ink and Kernel supports device 3 Changing online, so it would have to work on it We would I would have to write drivers for for this, but it could be working But our device does not support it No, I'll devote her would have to be changed. Maybe maybe next version. Okay. Thank you. Welcome Questions