來自:http://blog.csdn.net/woshixingaaa/article/details/6574220
這篇來分析spi子系統的建立過程。
嵌入式微處理器訪問SPI設備有兩種方式:使用GPIO模擬SPI接口的工作時序或者使用SPI控制器。使用GPIO模擬SPI接口的工作時序是非常容易實現的,但是會導致大量的時間耗費在模擬SPI接口的時序上,訪問效率比較低,容易成爲系統瓶頸。這裏主要分析使用SPI控制器的情況。
這個是由sys文件系統導出的spi子系統在內核中的視圖了。
首先了解一下Linux內核中的幾個文件:spi.c也就是spi子系統的核心了,spi_s3c24xx.c是s3c24xx系列芯片的SPI
controller驅動,它向更上層的SPI核心層(spi.c)提供接口用來控制芯片的SPI controller,是一個被其他驅動使用的驅動。而spidev.c是在覈心層基礎之上將SPI controller模擬成一個字符型的驅動,向文件系統提供標準的文件系統接口,用來操作對應的SPI controller。
下面我們來看看spi子系統是怎麼註冊進內核的:
- static int __init spi_init(void)
- {
- int status;
- buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
- if (!buf) {
- status = -ENOMEM;
- goto err0;
- }
- status = bus_register(&spi_bus_type);
- if (status < 0)
- goto err1;
- status = class_register(&spi_master_class);
- if (status < 0)
- goto err2;
- return 0;
- err2:
- bus_unregister(&spi_bus_type);
- err1:
- kfree(buf);
- buf = NULL;
- err0:
- return status;
- }
- postcore_initcall(spi_init);
下面來分析SPI controller驅動的註冊與初始化過程,首先執行的是s3c24xx_spi_init。
- static int __init s3c24xx_spi_init(void)
- {
- return platform_driver_probe(&s3c24xx_spi_driver, s3c24xx_spi_probe);
- }
- <span style="font-size:18px;">static int __init s3c24xx_spi_probe(struct platform_device *pdev)
- {
- struct s3c2410_spi_info *pdata;
- struct s3c24xx_spi *hw;
- struct spi_master *master;
- struct resource *res;
- int err = 0;
- /*分配struct spi_master+struct s3c24xx_spi大小的數據,把s3c24xx_spi設爲spi_master的私有數據*/
- master = spi_alloc_master(&pdev->dev, sizeof(struct s3c24xx_spi));
- if (master == NULL) {
- dev_err(&pdev->dev, "No memory for spi_master\n");
- err = -ENOMEM;
- goto err_nomem;
- }
- /*從master中獲得s3c24xx_spi*/
- hw = spi_master_get_devdata(master);
- memset(hw, 0, sizeof(struct s3c24xx_spi));
- hw->master = spi_master_get(master);
- /*驅動移植的時候需要實現的重要結構,初始化爲&s3c2410_spi0_platdata*/
- hw->pdata = pdata = pdev->dev.platform_data;
- hw->dev = &pdev->dev;
- if (pdata == NULL) {
- dev_err(&pdev->dev, "No platform data supplied\n");
- err = -ENOENT;
- goto err_no_pdata;
- }
- /*設置平臺的私有數據爲s3c24xx_spi*/
- platform_set_drvdata(pdev, hw);
- init_completion(&hw->done);
- /* setup the master state. */
- /*該總線上的設備數*/
- master->num_chipselect = hw->pdata->num_cs;
- /*總線號*/
- master->bus_num = pdata->bus_num;
- /* setup the state for the bitbang driver */
- /*spi_bitbang專門負責數據的傳輸*/
- hw->bitbang.master = hw->master;
- hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;
- hw->bitbang.chipselect = s3c24xx_spi_chipsel;
- hw->bitbang.txrx_bufs = s3c24xx_spi_txrx;
- hw->bitbang.master->setup = s3c24xx_spi_setup;
- dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
- 。。。。。。。。。。。。。。。。。。。。。。。。
- /*初始化設置寄存器,包括對SPIMOSI,SPIMISO,SPICLK引腳的設置*/
- s3c24xx_spi_initialsetup(hw);
- /* register our spi controller */
- err = spi_bitbang_start(&hw->bitbang);
- 。。。。。。。。。。。。。。。。。。。。。
- }
- spi controller的register在spi_bitbang_start函數中實現:
- int spi_bitbang_start(struct spi_bitbang *bitbang)
- {
- int status;
- if (!bitbang->master || !bitbang->chipselect)
- return -EINVAL;
- /*動態創建一個work_struct結構,它的處理函數是bitbang_work*/
- INIT_WORK(&bitbang->work, bitbang_work);
- spin_lock_init(&bitbang->lock);
- INIT_LIST_HEAD(&bitbang->queue);
- /*spi的數據傳輸就是用這個方法*/
- if (!bitbang->master->transfer)
- bitbang->master->transfer = spi_bitbang_transfer;
- if (!bitbang->txrx_bufs) {
- bitbang->use_dma = 0;
- /*spi_s3c24xx.c中有spi_bitbang_bufs方法,在bitbang_work中被調用*/
- bitbang->txrx_bufs = spi_bitbang_bufs;
- if (!bitbang->master->setup) {
- if (!bitbang->setup_transfer)
- bitbang->setup_transfer =
- spi_bitbang_setup_transfer;
- /*在spi_s3c24xx.c中有setup的處理方法,在spi_new_device中被調用*/
- bitbang->master->setup = spi_bitbang_setup;
- bitbang->master->cleanup = spi_bitbang_cleanup;
- }
- } else if (!bitbang->master->setup)
- return -EINVAL;
- /* this task is the only thing to touch the SPI bits */
- bitbang->busy = 0;
- /調用create_singlethread_workqueue創建單個工作線程/
- bitbang->workqueue = create_singlethread_workqueue(
- dev_name(bitbang->master->dev.parent));
- if (bitbang->workqueue == NULL) {
- status = -EBUSY;
- goto err1;
- }
- status = spi_register_master(bitbang->master);
- if (status < 0)
- goto err2;
- return status;
- err2:
- destroy_workqueue(bitbang->workqueue);
- err1:
- return status;
- }</span>
然後看這裏是怎樣註冊spi主機控制器驅動的:
- int spi_register_master(struct spi_master *master)
- {
- 。。。。。。。。。。。。。。。。
- /*將spi添加到內核,這也是sys/class/Spi_master下產生Spi0,Spi1的原因*/
- dev_set_name(&master->dev, "spi%u", master->bus_num);
- status = device_add(&master->dev);
- scan_boardinfo(master);
- }
這裏跟蹤scan_boardinfo函數:
- static void scan_boardinfo(struct spi_master *master)
- {
- struct boardinfo *bi;
- mutex_lock(&board_lock);
- /*遍歷所有掛在board_list上的struct boardinfo*/
- list_for_each_entry(bi, &board_list, list) {
- struct spi_board_info *chip = bi->board_info;
- unsigned n;
- /*遍歷每個boardinfo管理的spi_board_info,如果設備的總線號與控制器的總線好相等,則創建新設備*/
- for (n = bi->n_board_info; n > 0; n--, chip++) {
- if (chip->bus_num != master->bus_num)
- continue;
- (void) spi_new_device(master, chip);
- }
- }
- mutex_unlock(&board_lock);
- }
這個函數完成了將spi_board_info交由boardinfo管理,並把boardinfo掛載到board_list鏈表上。也就是說在系統初始化的時候將spi_device交由到掛在board_list上的boardinfo管理,在spi controller的driver註冊的時候不但註冊這個主機控制器的驅動,還要遍歷這個主機控制器的總線上的spi_device,將總線上的spi_device全部註冊進內核。當註冊進內核並且spi_driver已經註冊的時候,如果總線match成功,則會調用spi_driver的probe函數,這個將在後邊進行分析。
- <span style="font-size:18px;">int __init
- spi_register_board_info(struct spi_board_info const *info, unsigned n)
- {
- struct boardinfo *bi;
- bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);
- if (!bi)
- return -ENOMEM;
- bi->n_board_info = n;
- memcpy(bi->board_info, info, n * sizeof *info);
- mutex_lock(&board_lock);
- list_add_tail(&bi->list, &board_list);
- mutex_unlock(&board_lock);
- return 0;
- }</span>
- <span style="font-size:18px;">struct spi_device *spi_new_device(struct spi_master *master,
- struct spi_board_info *chip)
- {
- struct spi_device *proxy;
- int status;
- proxy = spi_alloc_device(master);
- if (!proxy)
- return NULL;
- WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
- /*初始化spi_device的各個字段*/
- proxy->chip_select = chip->chip_select;
- proxy->max_speed_hz = chip->max_speed_hz;
- proxy->mode = chip->mode;
- proxy->irq = chip->irq;
- /*這裏獲得了spi_device的名字,這個modalias也是在我們移植時在mach-smdk2440.c中的s3c2410_spi0_board中設定的*/
- strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
- proxy->dev.platform_data = (void *) chip->platform_data;
- proxy->controller_data = chip->controller_data;
- proxy->controller_state = NULL;
- /*主要完成將spi_device添加到內核*/
- status = spi_add_device(proxy);
- if (status < 0) {
- spi_dev_put(proxy);
- return NULL;
- }
- return proxy;
- }</span>
- struct spi_device *spi_alloc_device(struct spi_master *master)
- {
- struct spi_device *spi;
- struct device *dev = master->dev.parent;
- if (!spi_master_get(master))
- return NULL;
- spi = kzalloc(sizeof *spi, GFP_KERNEL);
- if (!spi) {
- dev_err(dev, "cannot alloc spi_device\n");
- spi_master_put(master);
- return NULL;
- }
- spi->master = master;
- spi->dev.parent = dev;
- /*設置總線是spi_bus_type,下面會講到spi_device與spi_driver是怎樣match上的*/
- spi->dev.bus = &spi_bus_type;
- spi->dev.release = spidev_release;
- device_initialize(&spi->dev);
- return spi;
- }
- int spi_add_device(struct spi_device *spi)
- {
- static DEFINE_MUTEX(spi_add_lock);
- struct device *dev = spi->master->dev.parent;
- int status;
- /*spi_device的片選號不能大於spi控制器的片選數*/
- if (spi->chip_select >= spi->master->num_chipselect) {
- dev_err(dev, "cs%d >= max %d\n",
- spi->chip_select,
- spi->master->num_chipselect);
- return -EINVAL;
- }
- /*這裏設置是spi_device在Linux設備驅動模型中的name,也就是圖中的spi0.0,而在/dev/下設備節點的名字是proxy->modalias中的名字*/
- dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
- spi->chip_select);
- mutex_lock(&spi_add_lock);
- /*如果總線上掛的設備已經有這個名字,則設置狀態忙碌,並退出*/
- if (bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev))
- != NULL) {
- dev_err(dev, "chipselect %d already in use\n",
- spi->chip_select);
- status = -EBUSY;
- goto done;
- }
- /對spi_device的時鐘等進行設置/
- status = spi->master->setup(spi);
- if (status < 0) {
- dev_err(dev, "can't %s %s, status %d\n",
- "setup", dev_name(&spi->dev), status);
- goto done;
- }
- /*添加到內核*/
- status = device_add(&spi->dev);
- if (status < 0)
- dev_err(dev, "can't %s %s, status %d\n",
- "add", dev_name(&spi->dev), status);
- else
- dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
- done:
- mutex_unlock(&spi_add_lock);
- return status;
- }
- static int s3c24xx_spi_setup(struct spi_device *spi)
- {
- 。。。。。。。。。。。。。。
- ret = s3c24xx_spi_setupxfer(spi, NULL);
- 。。。。。。。。。。。。。。
- }
- static int s3c24xx_spi_setupxfer(struct spi_device *spi,
- struct spi_transfer *t)
- {
- struct s3c24xx_spi *hw = to_hw(spi);
- unsigned int bpw;
- unsigned int hz;
- unsigned int div;
- /*設置了每字長的位數,發送速度*/
- bpw = t ? t->bits_per_word : spi->bits_per_word;
- hz = t ? t->speed_hz : spi->max_speed_hz;
- if (bpw != 8) {
- dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
- return -EINVAL;
- }
- /*色黃志分頻值*/
- div = clk_get_rate(hw->clk) / hz;
- /* is clk = pclk / (2 * (pre+1)), or is it
- * clk = (pclk * 2) / ( pre + 1) */
- div /= 2;
- if (div > 0)
- div -= 1;
- if (div > 255)
- div = 255;
- dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", div, hz);
- writeb(div, hw->regs + S3C2410_SPPRE);
- spin_lock(&hw->bitbang.lock);
- if (!hw->bitbang.busy) {
- hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
- /* need to ndelay for 0.5 clocktick ? */
- }
- spin_unlock(&hw->bitbang.lock);
- return 0;
- }
在spidev.c中:
- static int __init spidev_init(void)
- {
- int status;
- BUILD_BUG_ON(N_SPI_MINORS > 256);
- status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
- if (status < 0)
- return status;
- spidev_class = class_create(THIS_MODULE, "spidev");
- if (IS_ERR(spidev_class)) {
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
- return PTR_ERR(spidev_class);
- }
- status = spi_register_driver(&spidev_spi);
- if (status < 0) {
- class_destroy(spidev_class);
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
- }
- return status;
- }
- static struct spi_driver spidev_spi = {
- .driver = {
- .name = "spidev",
- .owner = THIS_MODULE,
- },
- .probe = spidev_probe,
- .remove = __devexit_p(spidev_remove),
- };
- static struct spi_driver spidev_spi = {
- .driver = {
- .name = "spidev",
- .owner = THIS_MODULE,
- },
- .probe = spidev_probe,
- .remove = __devexit_p(spidev_remove),
- };
- static int __driver_attach(struct device *dev, void *data)
- {
- struct device_driver *drv = data;
- if (!driver_match_device(drv, dev))
- return 0;
- if (dev->parent) /* Needed for USB */
- down(&dev->parent->sem);
- down(&dev->sem);
- if (!dev->driver)
- driver_probe_device(drv, dev);
- up(&dev->sem);
- if (dev->parent)
- up(&dev->parent->sem);
- return 0;
- }
- struct bus_type spi_bus_type = {
- .name = "spi",
- .dev_attrs = spi_dev_attrs,
- .match = spi_match_device,
- .uevent = spi_uevent,
- .suspend = spi_suspend,
- .resume = spi_resume,
- };
- static int spi_match_device(struct device *dev, struct device_driver *drv)
- {
- const struct spi_device *spi = to_spi_device(dev);
- return strcmp(spi->modalias, drv->name) == 0;
- }
- static int spi_drv_probe(struct device *dev)
- {
- const struct spi_driver *sdrv = to_spi_driver(dev->driver);
- return sdrv->probe(to_spi_device(dev));
- }
- static int spidev_remove(struct spi_device *spi)
- {
- struct spidev_data *spidev = spi_get_drvdata(spi);
- /* make sure ops on existing fds can abort cleanly */
- spin_lock_irq(&spidev->spi_lock);
- spidev->spi = NULL;
- spi_set_drvdata(spi, NULL);
- spin_unlock_irq(&spidev->spi_lock);
- /* prevent new opens */
- mutex_lock(&device_list_lock);
- list_del(&spidev->device_entry);
- device_destroy(spidev_class, spidev->devt);
- clear_bit(MINOR(spidev->devt), minors);
- if (spidev->users == 0)
- kfree(spidev);
- mutex_unlock(&device_list_lock);
- return 0;
- }
- static struct file_operations spidev_fops = {
- .owner = THIS_MODULE,
- /* REVISIT switch to aio primitives, so that userspace
- * gets more complete API coverage. It'll simplify things
- * too, except for the locking.
- */
- .write = spidev_write,
- .read = spidev_read,
- .unlocked_ioctl = spidev_ioctl,
- .open = spidev_open,
- .release = spidev_release,
- };