随后初始化了completion,这个东东将用于实现同步I/O,详见下文。之后,为master定义了setup方法,为bitbang定义了3个方法。
接着获取了一系列的资源,同时注册了中断服务程序。接着调用s3c24xx_spi_initialsetup初始化控制器。我们来看下该函数。
该函数位于下列函数位于deivers/spi/s3c24xx.c。
- static void s3c24xx_spi_initialsetup(struct s3c24xx_spi *hw)
- {
- /* for the moment, permanently enable the clock */
- clk_enable(hw->clk); /*使能时钟*/
- /* program defaults into the registers */
- writeb(0xff, hw->regs + S3C2410_SPPRE); /*设置预分频系数,baudrate=pclk/2/(prescaler value+1)*/
- writeb(SPPIN_DEFAULT, hw->regs + S3C2410_SPPIN);/*使能master out keep*/
- writeb(SPCON_DEFAULT, hw->regs + S3C2410_SPCON);/*master, interrupt mode*/
- if (hw->pdata) {
- if (hw->set_cs == s3c24xx_spi_gpiocs) /*set_cs 在probe方法中设置为s3c24xx_spi_gpiocs*/
- gpio_direction_output(hw->pdata->pin_cs, 1); /*设置该管脚为输出,貌似多次一举,在probe已经调用过该函数*/
- */
- if (hw->pdata->gpio_setup)
- hw->pdata->gpio_setup(hw->pdata, 1);
- }
- }
注意,这里设置了SPI0主控制器工作在master方式,使用中断模式。
最后调用了spi_bitbang_start函数,该函数非常重要,在下一小节中单独讲解。
4.2.2 spi_bitbang_start函数
下列函数位于drivers/spi/spi_bitbang.c
- /**
- * spi_bitbang_start - start up a polled/bitbanging SPI master driver
- * @bitbang: driver handle
- *
- * Caller should have zero-initialized all parts of the structure, and then
- * provided callbacks for chip selection and I/O loops. If the master has
- * a transfer method, its final step should call spi_bitbang_transfer; or,
- * that's the default if the transfer routine is not initialized. It should
- * also set up the bus number and number of chipselects.
- *
- * For i/o loops, provide callbacks either per-word (for bitbanging, or for
- * hardware that basically exposes a shift register) or per-spi_transfer
- * (which takes better advantage of hardware like fifos or DMA engines).
- *
- * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
- * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
- * master methods. Those methods are the defaults if the bitbang->txrx_bufs
- * routine isn't initialized.
- *
- * This routine registers the spi_master, which will process requests in a
- * dedicated task, keeping IRQs unblocked most of the time. To stop
- * processing those requests, call spi_bitbang_stop().
- */
- int spi_bitbang_start(struct spi_bitbang *bitbang)
- {
- int status;
- if (!bitbang->master || !bitbang->chipselect)
- return -EINVAL;
- INIT_WORK(&bitbang->work, bitbang_work); /*初始化工作,工作为bitbang_work*/
- spin_lock_init(&bitbang->lock); /*初始化自旋锁*/
- INIT_LIST_HEAD(&bitbang->queue); /*初始化链表头,链表为双向循环链表*/
- if (!bitbang->master->transfer) /*master的transfer方法没有定义过*/
- bitbang->master->transfer = spi_bitbang_transfer; /*使用spi_bitbang_transfe方法*/
- if (!bitbang->txrx_bufs) { /*如果bitbang没有txrx_bufs方法,在probe函数中定义过��方法*/
- bitbang->use_dma = 0;
- bitbang->txrx_bufs = spi_bitbang_bufs;
- if (!bitbang->master->setup) {
- if (!bitbang->setup_transfer)
- bitbang->setup_transfer =
- spi_bitbang_setup_transfer;
- bitbang->master->setup = spi_bitbang_setup;
- bitbang->master->cleanup = spi_bitbang_cleanup;
- }
- } else if (!bitbang->master->setup) /*setup方法在probe函数中有定义*/
- return -EINVAL;
- /* this task is the only thing to touch the SPI bits */
- bitbang->busy = 0;
- bitbang->workqueue = create_singlethread_workqueue( /*创建工作队列*/
- dev_name(bitbang->master->dev.parent));
- if (bitbang->workqueue == NULL) {
- status = -EBUSY;
- goto err1;
- }
- /* driver may get busy before register() returns, especially
- * if someone registered boardinfo for devices
- */
- status = spi_register_master(bitbang->master); /*注册spi控制器*/
- if (status < 0)
- goto err2;
- return status;
- err2:
- destroy_workqueue(bitbang->workqueue);
- err1:
- return status;
- }
- EXPORT_SYMBOL_GPL(spi_bitbang_start);
在这里,定义了控制器的transfer方法为spi_bitbang_transfer。创建了一个工作队列和一个工作bitbang_work,同时创建了一个链表。这些东东的作用将在后面介绍。
