Your SlideShare is downloading. ×
0
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Embedded Device Bus Drivers
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Embedded Device Bus Drivers

18,374

Published on

Published in: Technology, Business
3 Comments
14 Likes
Statistics
Notes
No Downloads
Views
Total Views
18,374
On Slideshare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
0
Comments
3
Likes
14
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. © 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Embedded Device Bus Drivers
  • 2. 2© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? Overview of Serial Subsystem Overview of UART Drivers Overview of TTY Drivers I2 C Subsystem Coding Details of an I2 C Device Driver Browsing Sample Code SPI Subsystem Coding Details of an SPI Device Driver Browsing Sample Code PCI Subsystem Coding Details of a PCI Device Driver Browsing Sample Code
  • 3. 3© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Serial Drivers
  • 4. 4© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Serial Prologue Typically known as RS232 protocol But, it covers many more. Namely, Infrared (back bone is RS232) RS485 (industry enhanced RS232) Serial Modem Console … from Linux kernel's perspective Serial Drivers provides a generic abstract view for all these, to the user
  • 5. 5© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Serial Subsystem N_TTY (n_tty.c) N_IRDA (irtty_sir.c) TTY Core (tty_io.c) serial_core.c usb-serial.c (Core Module) 8250.c USB-Serial Converter Driver User Applications (System Call Interface) Line Discipline Layer TTY Layer Low-level Driver Layer Physical Devices like Infrared, UART, USB-Serial Converter
  • 6. 6© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Low-level (UART) Driver related Data Structures & APIs Header: <linux/serial_core.h> Data Structures struct uart_driver struct uart_port struct uart_ops APIs int uart_register_driver(struct uart_driver *); int uart_unregister_driver(struct uart_driver *); int uart_add_one_port(struct uart_driver *, struct uart_port); int uart_remove_one_port(struct uart_driver *, struct uart_port); Typically implemented as a platform driver
  • 7. 7© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. TTY Driver related Data Structures & APIs Header: <linux/tty.h> Data Structures struct tty_struct struct tty_flip_buffer struct tty_driver APIs int tty_register_driver(struct tty_driver *); int tty_unregister_driver(struct tty_driver *); struct device *tty_register_device(struct tty_driver *, unsigned, struct device); void tty_unregister_device(struct tty_driver *, unsigned);
  • 8. 8© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Inter Integrated Circuit (I2 C) Drivers
  • 9. 9© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. I2 C Prologue Originally developed by Phillips (SMBus by Intel) Suitable for small slow devices I2 C is a 2-wire protocol One Serial Clock One Data Line Popular in Desktops & Embedded Systems Used for accessing EEPROMs RTCs ...
  • 10. 10© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. I2 C Subsystem User Space Kernel Space I2 C Device I2 C Host Controller I2 C Bus Hardware Space /sys, /dev User Applications I2 C User Mode Device Driver I2 C Core i2c-dev I2 C Adapter / Algo Driver Vertical: Character/SysFS I2 C Client Driver Horizontal: I2 C
  • 11. 11© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. I2 C Subsystem Details i2c-adapter / i2-algo Controller-specific I2 C host controller / adapter Also called as the I2 C bus drivers i2c-core Hides the adapter details from the layers above By providing the generic I2 C APIs i2c-dev Provides device access in user space through /sys Enables implementation of User Mode Drivers i2c-client Driver specific to an I2 C device Implemented using i2c-core APIs
  • 12. 12© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. SMBus Subset of I2 C Using only SMBus commands makes it compatible with both adapters Part of I2 C Core itself APIs for Device Access (Header: <linux/i2c.h>) int ioctl(smbus_fp, cmd, arg); s32 i2c_smbus_write_byte(client, val); s32 i2c_smbus_read_byte(client); s32 i2c_smbus_write_*_data(client, cmd, val); s32 i2c_smbus_read_*_data(client, cmd); client – Pointer to struct i2c_client created by the probe function
  • 13. 13© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. I2 C Client Driver Typically a character driver vertical or /sys exposed But actually depends on the device category And the I2 C driver horizontal Registers with I2 C Core (in the init function) Unregisters from I2 C Core (in the cleanup function) And uses the transfer function from I2 C Core for actual data transactions int i2c_transfer (struct i2c_adapter *adap, struct i2c_msg *msgs, int num); adap is the client->adapter
  • 14. 14© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. I2 C Client Driver's init & cleanup Registering to the I2 C Core using int i2c_add_driver(struct i2c_driver *); struct i2c_driver contains probe function – called on device detection remove function – called on device shutdown id_table – Table of device identifiers Unregistering from the I2 C Core using void i2c_del_driver(struct i2c_driver *); Common bare-bone of init & cleanup Just use module_i2c_driver(struct i2c_driver);
  • 15. 15© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Checking Adapter Capabilities I2 C client driver's probe would typically check for HC capabilities Header: <linux/i2c.h> APIs u32 i2c_get_functionality (struct *i2c_adapter); int i2c_check_functionality (struct *i2c_adapter, u32 func);
  • 16. 16© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. I2 C Driver Examples Path: <kernel_source>/drivers/ I2 C EEPROM: AT24 misc/eeprom/at24.c I2 C Audio: Beagle Audio Codec cum Pwr Mgmt mfd/twl4030-audio.c -> mfd/twl-core.c(plat driver) I2 C RTC: DS1307 rtc/rtc-twl.c -> mfd/twl-core.c; rtc/rtc-ds1307.c Browse & Discuss any
  • 17. 17© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Serial Peripheral Interface (SPI) Drivers
  • 18. 18© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. SPI Prologue Suitable for small slow devices SPI is a 4-wire protocol unlike I2 C Apart from Serial Clock, Data Lines are 2 And a additional Chip Select However, at s/w level, it is similar to I2 C Hence, let's get straight to the client driver details
  • 19. 19© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. SPI Client Driver Register probe() & remove() with SPI Core Optionally, register suspend() & resume() Header: <linux/spi/spi.h> API int spi_register_driver(struct spi_driver *); void spi_unregister_driver(struct spi_driver *); Device Access APIs spi_sync(struct spi_device *, struct spi_message *); spi_async(struct spi_device *, struct spi_message *);
  • 20. 20© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. SPI Device Access /* struct spi_device *spi – obtained through probe */ struct spi_transfer xfer; struct spi_message sm; u8 *cmd_buf; int len; … /* Ready the cmd_buf & its len */ ... spi_message_init(&sm); xfer.tx_buf = cmd_buf; xfer.len = len; spi_message_add_tail(&xfer, &sm); spi_sync(spi, &sm); /* Blocking transfer request */ spi_transfer_del(&xfer);
  • 21. 21© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. SPI Driver Example Driver: SPI EEPROM or SPI Sensor Path: <kernel_source>/drivers/spi Browse & Discuss
  • 22. 22© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Peripheral Component Interconnect (PCI) Drivers
  • 23. 23© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. PCI Prologue Developed by Intel as a replacement for ISA More of a processor (specific) bus for x86 architectures Replacing Features Plug n Play Platform independent Fast Hardware Addressing Through South Bridge (PCI Controller) Buses (upto 256) → Devices (upto 32) → Functions (upto 8)
  • 24. 24© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel Aspect Organized as PCI Core (driver of the PCI Controller) PCI Drivers (device drivers) Kernel Windows for PCI /proc/bus/pci/devices (Try cat) /sys/bus/pci/devices (Try tree) Command: lspci
  • 25. 25© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Dynamic Device Configuration What Configuration? Device Base Address(es) Register Space(s) Memory(s) Interrupt Vectors ... Done during Bootup by BIOS, Or PCI Core (Bootloader or Kernel) Configured by the time, driver comes up But, where is this configuration stored?
  • 26. 26© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. PCI Device Configuration Space Vendor ID BIST Header Type Lat Timer Cache Line Class Code Rev ID Status Register Command Register Device ID 0x0 0xF Base Address 0 CardBus CIS pointerBase Address 5Base Address 4 Base Address 3Base Address 2Base Address 1 Subsystem Vendor ID Subsystem Device ID Expansion ROM Base Address IRQ Line IRQ Pin Min Gnt Max Lat 0x00 0x30 0x20 0x10 Reserved
  • 27. 27© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. PCI Device Config Space Access Header: <linux/pci.h> APIs int pci_read_config_byte(struct pci_dev *dev, int where, u8 *val); int pci_read_config_word(struct pci_dev *dev, int where, u16 *val); int pci_read_config_dword(struct pci_dev *dev, int where, u32 *val); int pci_write_config_byte(struct pci_dev *dev, int where, u8 *val); int pci_write_config_word(struct pci_dev *dev, int where, u16 *val); int pci_write_config_dword(struct pci_dev *dev, int where, u32 *val); NB struct pci_dev *dev would be available as parameter to probe()
  • 28. 28© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Standard PCI Device Access API Upto 6 Memory or I/O regions Header: <linux/pci.h> APIs unsigned long pci_resource_start(dev, bar); unsigned long pci_resource_len(dev, bar); unsigned long pci_resource_flags(dev, bar); Flags IORESOURCE_IO IORESOURCE_MEM IORESOURCE_PREFETCH
  • 29. 29© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. PCI Driver Registration int pci_register_driver(struct pci_driver *drv); int pci_unregister_driver(struct pci_driver *drv); struct pci_driver const char *name const struct pci_dev_id *id_table; PCI_DEVICE(vendor, device); PCI_DEVICE_CLASS(dev_class, dev_class_mask); int (*probe)(pci_dev, id_table); void (*remove)(pci_dev); Header: <linux/pci.h>
  • 30. 30© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Typical 'probe' Function int probe(struct pci_dev *dev, struct pci_dev_id *id) { /* Enable the PCI Device */ pci_enable_device(dev); ... /* Acquire PCI Device's regions */ pci_request_regions(dev, “label”); ... /* Possibly map I/Os */ ... pci_set_drvdata(dev, <pvt_data_ptr>); // Optional ... /* Register the vertical */ ... return 0; /* Claimed. Negative for not Claimed */ }
  • 31. 31© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Typical 'remove' Function void remove(struct pci_dev *dev) { /* Unregister the vertical */ ... pci_set_drvdata(dev, NULL); // Optional ... /* Possibly unmap I/Os */ ... /* Release PCI Device's regions */ pci_release_regions(dev); ... /* Disable the PCI Device */ pci_disable_device(dev); }
  • 32. 32© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Old-style PCI Probing & Getting struct pci_dev *pci_find_*(vendor, device, from); struct pci_dev *pci_get_device(v, d, from); struct pci_dev *pci_get_subsys(v, d, ssv, ssd, f); struct pci_dev *pci_get_slot(bus, devfn); pci_dev_put(pci_dev);
  • 33. © 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. DMA Mapping APIs dma_addr_t pci_map_single(struct pci_dev *, void *, size_t, int dir); void pci_unmap_single(struct pci_dev *, dma_addr_t, size_t, int dir); Directions PCI_DMA_BIDIRECTIONAL PCI_DMA_TODEVICE PCI_DMA_FROMDEVICE Header: <linux/pci.h> To Device PM VM PA VA From Device
  • 34. © 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. DMA Allocation PM VM APIs void *pci_alloc_consistent(struct pci_dev *, size_t, dma_addr_t *); void pci_free_consistent(struct pci_dev *, size_t, void *, dma_addr_t); int pci_set_dma_mask(struct pci_dev *, u64 mask); Header: <linux/pci.h> PA VA
  • 35. 35© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved.
  • 36. 36© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? Overview of Serial Subsystem Overview of UART Drivers Overview of TTY Drivers I2 C Subsystem Coding Details of an I2 C Device Driver Browsing Sample Code SPI Subsystem Coding Details of an SPI Device Driver Browsing Sample Code PCI Subsystem Coding Details of a PCI Device Driver Browsing Sample Code
  • 37. 37© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?

×