1baa293e9SMauro Carvalho Chehab================= 2baa293e9SMauro Carvalho ChehabMEN Chameleon Bus 3baa293e9SMauro Carvalho Chehab================= 4baa293e9SMauro Carvalho Chehab 5baa293e9SMauro Carvalho Chehab.. Table of Contents 6baa293e9SMauro Carvalho Chehab ================= 7baa293e9SMauro Carvalho Chehab 1 Introduction 8baa293e9SMauro Carvalho Chehab 1.1 Scope of this Document 9baa293e9SMauro Carvalho Chehab 1.2 Limitations of the current implementation 10baa293e9SMauro Carvalho Chehab 2 Architecture 11baa293e9SMauro Carvalho Chehab 2.1 MEN Chameleon Bus 12baa293e9SMauro Carvalho Chehab 2.2 Carrier Devices 13baa293e9SMauro Carvalho Chehab 2.3 Parser 14baa293e9SMauro Carvalho Chehab 3 Resource handling 15baa293e9SMauro Carvalho Chehab 3.1 Memory Resources 16baa293e9SMauro Carvalho Chehab 3.2 IRQs 17baa293e9SMauro Carvalho Chehab 4 Writing an MCB driver 18baa293e9SMauro Carvalho Chehab 4.1 The driver structure 19baa293e9SMauro Carvalho Chehab 4.2 Probing and attaching 20baa293e9SMauro Carvalho Chehab 4.3 Initializing the driver 21*7594bb08SJohannes Thumshirn 4.4 Using DMA 22baa293e9SMauro Carvalho Chehab 23baa293e9SMauro Carvalho Chehab 24baa293e9SMauro Carvalho ChehabIntroduction 25baa293e9SMauro Carvalho Chehab============ 26baa293e9SMauro Carvalho Chehab 27baa293e9SMauro Carvalho ChehabThis document describes the architecture and implementation of the MEN 28baa293e9SMauro Carvalho ChehabChameleon Bus (called MCB throughout this document). 29baa293e9SMauro Carvalho Chehab 30baa293e9SMauro Carvalho ChehabScope of this Document 31baa293e9SMauro Carvalho Chehab---------------------- 32baa293e9SMauro Carvalho Chehab 33baa293e9SMauro Carvalho ChehabThis document is intended to be a short overview of the current 34baa293e9SMauro Carvalho Chehabimplementation and does by no means describe the complete possibilities of MCB 35baa293e9SMauro Carvalho Chehabbased devices. 36baa293e9SMauro Carvalho Chehab 37baa293e9SMauro Carvalho ChehabLimitations of the current implementation 38baa293e9SMauro Carvalho Chehab----------------------------------------- 39baa293e9SMauro Carvalho Chehab 40baa293e9SMauro Carvalho ChehabThe current implementation is limited to PCI and PCIe based carrier devices 41baa293e9SMauro Carvalho Chehabthat only use a single memory resource and share the PCI legacy IRQ. Not 42baa293e9SMauro Carvalho Chehabimplemented are: 43baa293e9SMauro Carvalho Chehab 44baa293e9SMauro Carvalho Chehab- Multi-resource MCB devices like the VME Controller or M-Module carrier. 45baa293e9SMauro Carvalho Chehab- MCB devices that need another MCB device, like SRAM for a DMA Controller's 46baa293e9SMauro Carvalho Chehab buffer descriptors or a video controller's video memory. 47baa293e9SMauro Carvalho Chehab- A per-carrier IRQ domain for carrier devices that have one (or more) IRQs 48baa293e9SMauro Carvalho Chehab per MCB device like PCIe based carriers with MSI or MSI-X support. 49baa293e9SMauro Carvalho Chehab 50baa293e9SMauro Carvalho ChehabArchitecture 51baa293e9SMauro Carvalho Chehab============ 52baa293e9SMauro Carvalho Chehab 53baa293e9SMauro Carvalho ChehabMCB is divided into 3 functional blocks: 54baa293e9SMauro Carvalho Chehab 55baa293e9SMauro Carvalho Chehab- The MEN Chameleon Bus itself, 56baa293e9SMauro Carvalho Chehab- drivers for MCB Carrier Devices and 57baa293e9SMauro Carvalho Chehab- the parser for the Chameleon table. 58baa293e9SMauro Carvalho Chehab 59baa293e9SMauro Carvalho ChehabMEN Chameleon Bus 60baa293e9SMauro Carvalho Chehab----------------- 61baa293e9SMauro Carvalho Chehab 62baa293e9SMauro Carvalho ChehabThe MEN Chameleon Bus is an artificial bus system that attaches to a so 63baa293e9SMauro Carvalho Chehabcalled Chameleon FPGA device found on some hardware produced my MEN Mikro 64baa293e9SMauro Carvalho ChehabElektronik GmbH. These devices are multi-function devices implemented in a 65baa293e9SMauro Carvalho Chehabsingle FPGA and usually attached via some sort of PCI or PCIe link. Each 66baa293e9SMauro Carvalho ChehabFPGA contains a header section describing the content of the FPGA. The 67baa293e9SMauro Carvalho Chehabheader lists the device id, PCI BAR, offset from the beginning of the PCI 68baa293e9SMauro Carvalho ChehabBAR, size in the FPGA, interrupt number and some other properties currently 69baa293e9SMauro Carvalho Chehabnot handled by the MCB implementation. 70baa293e9SMauro Carvalho Chehab 71baa293e9SMauro Carvalho ChehabCarrier Devices 72baa293e9SMauro Carvalho Chehab--------------- 73baa293e9SMauro Carvalho Chehab 74baa293e9SMauro Carvalho ChehabA carrier device is just an abstraction for the real world physical bus the 75baa293e9SMauro Carvalho ChehabChameleon FPGA is attached to. Some IP Core drivers may need to interact with 76baa293e9SMauro Carvalho Chehabproperties of the carrier device (like querying the IRQ number of a PCI 77baa293e9SMauro Carvalho Chehabdevice). To provide abstraction from the real hardware bus, an MCB carrier 78baa293e9SMauro Carvalho Chehabdevice provides callback methods to translate the driver's MCB function calls 79baa293e9SMauro Carvalho Chehabto hardware related function calls. For example a carrier device may 80baa293e9SMauro Carvalho Chehabimplement the get_irq() method which can be translated into a hardware bus 81baa293e9SMauro Carvalho Chehabquery for the IRQ number the device should use. 82baa293e9SMauro Carvalho Chehab 83baa293e9SMauro Carvalho ChehabParser 84baa293e9SMauro Carvalho Chehab------ 85baa293e9SMauro Carvalho Chehab 86baa293e9SMauro Carvalho ChehabThe parser reads the first 512 bytes of a Chameleon device and parses the 87baa293e9SMauro Carvalho ChehabChameleon table. Currently the parser only supports the Chameleon v2 variant 88baa293e9SMauro Carvalho Chehabof the Chameleon table but can easily be adopted to support an older or 89baa293e9SMauro Carvalho Chehabpossible future variant. While parsing the table's entries new MCB devices 90baa293e9SMauro Carvalho Chehabare allocated and their resources are assigned according to the resource 91baa293e9SMauro Carvalho Chehabassignment in the Chameleon table. After resource assignment is finished, the 92baa293e9SMauro Carvalho ChehabMCB devices are registered at the MCB and thus at the driver core of the 93baa293e9SMauro Carvalho ChehabLinux kernel. 94baa293e9SMauro Carvalho Chehab 95baa293e9SMauro Carvalho ChehabResource handling 96baa293e9SMauro Carvalho Chehab================= 97baa293e9SMauro Carvalho Chehab 98baa293e9SMauro Carvalho ChehabThe current implementation assigns exactly one memory and one IRQ resource 99baa293e9SMauro Carvalho Chehabper MCB device. But this is likely going to change in the future. 100baa293e9SMauro Carvalho Chehab 101baa293e9SMauro Carvalho ChehabMemory Resources 102baa293e9SMauro Carvalho Chehab---------------- 103baa293e9SMauro Carvalho Chehab 104baa293e9SMauro Carvalho ChehabEach MCB device has exactly one memory resource, which can be requested from 105baa293e9SMauro Carvalho Chehabthe MCB bus. This memory resource is the physical address of the MCB device 106baa293e9SMauro Carvalho Chehabinside the carrier and is intended to be passed to ioremap() and friends. It 107baa293e9SMauro Carvalho Chehabis already requested from the kernel by calling request_mem_region(). 108baa293e9SMauro Carvalho Chehab 109baa293e9SMauro Carvalho ChehabIRQs 110baa293e9SMauro Carvalho Chehab---- 111baa293e9SMauro Carvalho Chehab 112baa293e9SMauro Carvalho ChehabEach MCB device has exactly one IRQ resource, which can be requested from the 113baa293e9SMauro Carvalho ChehabMCB bus. If a carrier device driver implements the ->get_irq() callback 114baa293e9SMauro Carvalho Chehabmethod, the IRQ number assigned by the carrier device will be returned, 115baa293e9SMauro Carvalho Chehabotherwise the IRQ number inside the Chameleon table will be returned. This 116baa293e9SMauro Carvalho Chehabnumber is suitable to be passed to request_irq(). 117baa293e9SMauro Carvalho Chehab 118baa293e9SMauro Carvalho ChehabWriting an MCB driver 119baa293e9SMauro Carvalho Chehab===================== 120baa293e9SMauro Carvalho Chehab 121baa293e9SMauro Carvalho ChehabThe driver structure 122baa293e9SMauro Carvalho Chehab-------------------- 123baa293e9SMauro Carvalho Chehab 124baa293e9SMauro Carvalho ChehabEach MCB driver has a structure to identify the device driver as well as 125baa293e9SMauro Carvalho Chehabdevice ids which identify the IP Core inside the FPGA. The driver structure 126baa293e9SMauro Carvalho Chehabalso contains callback methods which get executed on driver probe and 127baa293e9SMauro Carvalho Chehabremoval from the system:: 128baa293e9SMauro Carvalho Chehab 129baa293e9SMauro Carvalho Chehab static const struct mcb_device_id foo_ids[] = { 130baa293e9SMauro Carvalho Chehab { .device = 0x123 }, 131baa293e9SMauro Carvalho Chehab { } 132baa293e9SMauro Carvalho Chehab }; 133baa293e9SMauro Carvalho Chehab MODULE_DEVICE_TABLE(mcb, foo_ids); 134baa293e9SMauro Carvalho Chehab 135baa293e9SMauro Carvalho Chehab static struct mcb_driver foo_driver = { 136baa293e9SMauro Carvalho Chehab driver = { 137baa293e9SMauro Carvalho Chehab .name = "foo-bar", 138baa293e9SMauro Carvalho Chehab .owner = THIS_MODULE, 139baa293e9SMauro Carvalho Chehab }, 140baa293e9SMauro Carvalho Chehab .probe = foo_probe, 141baa293e9SMauro Carvalho Chehab .remove = foo_remove, 142baa293e9SMauro Carvalho Chehab .id_table = foo_ids, 143baa293e9SMauro Carvalho Chehab }; 144baa293e9SMauro Carvalho Chehab 145baa293e9SMauro Carvalho ChehabProbing and attaching 146baa293e9SMauro Carvalho Chehab--------------------- 147baa293e9SMauro Carvalho Chehab 148baa293e9SMauro Carvalho ChehabWhen a driver is loaded and the MCB devices it services are found, the MCB 149baa293e9SMauro Carvalho Chehabcore will call the driver's probe callback method. When the driver is removed 150baa293e9SMauro Carvalho Chehabfrom the system, the MCB core will call the driver's remove callback method:: 151baa293e9SMauro Carvalho Chehab 152baa293e9SMauro Carvalho Chehab static init foo_probe(struct mcb_device *mdev, const struct mcb_device_id *id); 153baa293e9SMauro Carvalho Chehab static void foo_remove(struct mcb_device *mdev); 154baa293e9SMauro Carvalho Chehab 155baa293e9SMauro Carvalho ChehabInitializing the driver 156baa293e9SMauro Carvalho Chehab----------------------- 157baa293e9SMauro Carvalho Chehab 158baa293e9SMauro Carvalho ChehabWhen the kernel is booted or your foo driver module is inserted, you have to 159baa293e9SMauro Carvalho Chehabperform driver initialization. Usually it is enough to register your driver 160baa293e9SMauro Carvalho Chehabmodule at the MCB core:: 161baa293e9SMauro Carvalho Chehab 162baa293e9SMauro Carvalho Chehab static int __init foo_init(void) 163baa293e9SMauro Carvalho Chehab { 164baa293e9SMauro Carvalho Chehab return mcb_register_driver(&foo_driver); 165baa293e9SMauro Carvalho Chehab } 166baa293e9SMauro Carvalho Chehab module_init(foo_init); 167baa293e9SMauro Carvalho Chehab 168baa293e9SMauro Carvalho Chehab static void __exit foo_exit(void) 169baa293e9SMauro Carvalho Chehab { 170baa293e9SMauro Carvalho Chehab mcb_unregister_driver(&foo_driver); 171baa293e9SMauro Carvalho Chehab } 172baa293e9SMauro Carvalho Chehab module_exit(foo_exit); 173baa293e9SMauro Carvalho Chehab 174baa293e9SMauro Carvalho ChehabThe module_mcb_driver() macro can be used to reduce the above code:: 175baa293e9SMauro Carvalho Chehab 176baa293e9SMauro Carvalho Chehab module_mcb_driver(foo_driver); 177*7594bb08SJohannes Thumshirn 178*7594bb08SJohannes ThumshirnUsing DMA 179*7594bb08SJohannes Thumshirn--------- 180*7594bb08SJohannes Thumshirn 181*7594bb08SJohannes ThumshirnTo make use of the kernel's DMA-API's function, you will need to use the 182*7594bb08SJohannes Thumshirncarrier device's 'struct device'. Fortunately 'struct mcb_device' embeds a 183*7594bb08SJohannes Thumshirnpointer (->dma_dev) to the carrier's device for DMA purposes:: 184*7594bb08SJohannes Thumshirn 185*7594bb08SJohannes Thumshirn ret = dma_set_mask_and_coherent(&mdev->dma_dev, DMA_BIT_MASK(dma_bits)); 186*7594bb08SJohannes Thumshirn if (rc) 187*7594bb08SJohannes Thumshirn /* Handle errors */ 188