Chinaunix首页 | 论坛 | 博客
  • 博客访问: 648772
  • 博文数量: 113
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 4176
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-15 20:22
个人简介

最大化我的市场价值

文章分类

全部博文(113)

文章存档

2013年(113)

分类: LINUX

2013-03-07 13:50:59

 linux spi驱动开发学习(四)-----spi驱动程序完整流程分析

所有的应用程序使用dev/目录下创建的设备,这些字符设备的操作函数集在文件spidev.c中实现。

点击(此处)折叠或打开

  1. static const struct file_operations spidev_fops = {
  2.     .owner =    THIS_MODULE,
  3.     /* REVISIT switch to aio primitives, so that userspace
  4.      * gets more complete API coverage. It'll simplify things
  5.      * too, except for the locking.
  6.      */
  7.     .write =    spidev_write,
  8.     .read =        spidev_read,
  9.     .unlocked_ioctl = spidev_ioctl,
  10.     .open =        spidev_open,
  11.     .release =    spidev_release,
  12. };
以上是所有应用程序所能够做的所有操作,由此开始追踪spi 驱动程序的完整执行流程
其中,最重要的就是ioctl, 从这里开始先重点剖析inctl 

点击(此处)折叠或打开

  1. spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  2. {
  3.     int err = 0;
  4.     int retval = 0;
  5.     struct spidev_data *spidev;
  6.     struct spi_device *spi;
  7.     u32 tmp;
  8.     unsigned n_ioc;
  9.     struct spi_ioc_transfer *ioc;
  10.     //ioctl cmd 检查
  11.     /* Check type and command number */
  12.     if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
  13.         return -ENOTTY;
  14.     
  15.     /* Check access direction once here; don't repeat below.
  16.      * IOC_DIR is from the user perspective, while access_ok is
  17.      * from the kernel perspective; so they look reversed.
  18.      */
  19.     if (_IOC_DIR(cmd) & _IOC_READ)
  20.         err = !access_ok(VERIFY_WRITE,
  21.                 (void __user *)arg, _IOC_SIZE(cmd));
  22.     if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
  23.         err = !access_ok(VERIFY_READ,
  24.                 (void __user *)arg, _IOC_SIZE(cmd));
  25.     if (err)
  26.         return -EFAULT;

  27.     /* guard against device removal before, or while,
  28.      * we issue this ioctl.
  29.      */
  30.     //通过以下方式获取spi_device-----spi(是之后操作的基础)
  31.     spidev = filp->private_data;
  32.     spin_lock_irq(&spidev->spi_lock);
  33.     spi = spi_dev_get(spidev->spi);
  34.     spin_unlock_irq(&spidev->spi_lock);

  35.     if (spi == NULL)
  36.         return -ESHUTDOWN;

  37.     /* use the buffer lock here for triple duty:
  38.      * - prevent I/O (from us) so calling spi_setup() is safe;
  39.      * - prevent concurrent SPI_IOC_WR_* from morphing
  40.      * data fields while SPI_IOC_RD_* reads them;
  41.      * - SPI_IOC_MESSAGE needs the buffer locked "normally".
  42.      */
  43.     mutex_lock(&spidev->buf_lock);
  44.     //以上是进行check,检查命令有效性,以及进行初始化数据,这里不在多做说明
  45.     switch (cmd) {
  46.     /* read requests */
  47.     case SPI_IOC_RD_MODE://获取模式信息,将信息发送给用户
  48.         retval = __put_user(spi->mode & SPI_MODE_MASK,
  49.                     (__u8 __user *)arg);
  50.         break;
  51.     case SPI_IOC_RD_LSB_FIRST://获取spi最低有效位
  52.         retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
  53.                     (__u8 __user *)arg);
  54.         break;
  55.     case SPI_IOC_RD_BITS_PER_WORD:
  56.         retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
  57.         break;
  58.     case SPI_IOC_RD_MAX_SPEED_HZ:
  59.         retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
  60.         break;

  61.     /* write requests */
  62.     case SPI_IOC_WR_MODE://设置数据传输模式,这里只是把设置的数据保存在spi 中,但并没有对spi device做任何操作,对spi device的操作一并在最后进行
  63.         retval = __get_user(tmp, (u8 __user *)arg);
  64.         if (retval == 0) {
  65.             u8 save = spi->mode;

  66.             if (tmp & ~SPI_MODE_MASK) {
  67.                 retval = -EINVAL;
  68.                 break;
  69.             }

  70.             tmp |= spi->mode & ~SPI_MODE_MASK;
  71.             spi->mode = (u8)tmp;
  72.             retval = spi_setup(spi);
  73.             if (retval < 0)
  74.                 spi->mode = save;
  75.             else
  76.                 dev_dbg(&spi->dev, "spi mode %02xn", tmp);
  77.         }
  78.         break;
  79.     case SPI_IOC_WR_LSB_FIRST://设置设置spi写最低有效位,同上
  80.         retval = __get_user(tmp, (__u8 __user *)arg);
  81.         if (retval == 0) {
  82.             u8 save = spi->mode;

  83.             if (tmp)
  84.                 spi->mode |= SPI_LSB_FIRST;
  85.             else
  86.                 spi->mode &= ~SPI_LSB_FIRST;
  87.             retval = spi_setup(spi);
  88.             if (retval < 0)
  89.                 spi->mode = save;
  90.             else
  91.                 dev_dbg(&spi->dev, "%csb firstn",
  92.                         tmp ? 'l' : 'm');
  93.         }
  94.         break;
  95.     case SPI_IOC_WR_BITS_PER_WORD://设置spi写每个字含多个个位,同上
  96.         retval = __get_user(tmp, (__u8 __user *)arg);
  97.         if (retval == 0) {
  98.             u8 save = spi->bits_per_word;

  99.             spi->bits_per_word = tmp;
  100.             retval = spi_setup(spi);
  101.             if (retval < 0)
  102.                 spi->bits_per_word = save;
  103.             else
  104.                 dev_dbg(&spi->dev, "%d bits per wordn", tmp);
  105.         }
  106.         break;
  107.     case SPI_IOC_WR_MAX_SPEED_HZ://设置spi写最大速率,同上
  108.         retval = __get_user(tmp, (__u32 __user *)arg);
  109.         if (retval == 0) {
  110.             u32 save = spi->max_speed_hz;

  111.             spi->max_speed_hz = tmp;
  112.             retval = spi_setup(spi);
  113.             if (retval < 0)
  114.                 spi->max_speed_hz = save;
  115.             else
  116.                 dev_dbg(&spi->dev, "%d Hz (max)n", tmp);
  117.         }
  118.         break;

  119.     default:
  120.         /* segmented and/or full-duplex I/O request */
  121.         if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))//查看是否为数据write命令
  122.                 || _IOC_DIR(cmd) != _IOC_WRITE) {
  123.             retval = -ENOTTY;
  124.             break;
  125.         }
  126.         //pay more time on understanding below method
  127.         tmp = _IOC_SIZE(cmd);//从命令参数中解析出用户数据大小
  128.         if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {//数据大小必须是struct spi_ioc_transfer的整数倍
  129.             retval = -EINVAL;
  130.             break;
  131.         }
  132.         n_ioc = tmp / sizeof(struct spi_ioc_transfer);//将要传输的数据分成n个传输数据段
  133.         if (n_ioc == 0)
  134.             break;

  135.         /* copy into scratch area */
  136.         ioc = kmalloc(tmp, GFP_KERNEL);//获取n个数据段的数据管理结构体的内存空间
  137.         if (!ioc) {
  138.             retval = -ENOMEM;
  139.             break;
  140.         }
  141.         if (__copy_from_user(ioc, (void __user *)arg, tmp)) {//从用户空间获取数据管理结构体的初始化值
  142.             kfree(ioc);
  143.             retval = -EFAULT;
  144.             break;
  145.         }

  146.         /* translate to spi_message, execute */
  147.         retval = spidev_message(spidev, ioc, n_ioc);//数据传输,这是整个流程中的核心
  148.         kfree(ioc);
  149.         break;
  150.     }

  151.     mutex_unlock(&spidev->buf_lock);
  152.     spi_dev_put(spi);
  153.     return retval;
  154. }

****************************************************************************************华丽分割线****************************************************************************************

(1)

//通过调用函数spi->master->setup()来设置SPI模式。

static inline int
spi_setup(struct spi_device *spi)
{
 return spi->master->setup(spi);
}

master->setup成员被初始化成函数s3c24xx_spi_setup()。这一工作是在函数

s3c24xx_spi_probe()中进行的

hw->bitbang.master->setup  = s3c24xx_spi_setup;

函数s3c24xx_spi_setup()是在文件linux/drivers/spi/spi_s3c24xx.c中实现的。


点击(此处)折叠或打开

  1. static int s3c24xx_spi_setup(struct spi_device *spi)
  2. {
  3.     struct s3c24xx_spi_devstate *cs = spi->controller_state;
  4.     struct s3c24xx_spi *hw = to_hw(spi);
  5.     int ret;

  6.     /* allocate settings on the first call */
  7.     if (!cs) {
  8.         cs = kzalloc(sizeof(struct s3c24xx_spi_devstate), GFP_KERNEL);
  9.         if (!cs) {
  10.             dev_err(&spi->dev, "no memory for controller staten");
  11.             return -ENOMEM;
  12.         }

  13.         cs->spcon = SPCON_DEFAULT;
  14.         cs->hz = -1;
  15.         spi->controller_state = cs;
  16.     }

  17.     /* initialise the state from the device */
  18.     ret = s3c24xx_spi_update_state(spi, NULL);
  19.     if (ret)
  20.         return ret;

  21.     spin_lock(&hw->bitbang.lock);
  22.     //bitbang包含了数据传输函数,在数据传输时忙碌标识bitbang.busy置1,也就是在数据传输过程中不能改变数据传输模式。
  23.     if (!hw->bitbang.busy) {
  24.         hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
  25.         /* need to ndelay for 0.5 clocktick ? */
  26.     }
  27.     spin_unlock(&hw->bitbang.lock);

  28.     return 0;
  29. }

点击(此处)折叠或打开

  1. static int s3c24xx_spi_update_state(struct spi_device *spi,
  2.                  struct spi_transfer *t)
  3. {
  4.     struct s3c24xx_spi *hw = to_hw(spi);
  5.     struct s3c24xx_spi_devstate *cs = spi->controller_state;
  6.     unsigned int bpw;
  7.     unsigned int hz;
  8.     unsigned int div;
  9.     unsigned long clk;

  10.     bpw = t ? t->bits_per_word : spi->bits_per_word;
  11.     hz = t ? t->speed_hz : spi->max_speed_hz;

  12.     if (!bpw)
  13.         bpw = 8;

  14.     if (!hz)
  15.         hz = spi->max_speed_hz;

  16.     if (bpw != 8) {
  17.         dev_err(&spi->dev, "invalid bits-per-word (%d)n", bpw);
  18.         return -EINVAL;
  19.     }

  20.     if (spi->mode != cs->mode) {
  21.         u8 spcon = SPCON_DEFAULT;

  22.         if (spi->mode & SPI_CPHA)
  23.             spcon |= S3C2410_SPCON_CPHA_FMTB;

  24.         if (spi->mode & SPI_CPOL)
  25.             spcon |= S3C2410_SPCON_CPOL_HIGH;

  26.         cs->mode = spi->mode;
  27.         cs->spcon = spcon;
  28.     }

  29.     if (cs->hz != hz) {
  30.         clk = clk_get_rate(hw->clk);
  31.         div = DIV_ROUND_UP(clk, hz * 2) - 1;

  32.         if (div > 255)
  33.             div = 255;

  34.         dev_dbg(&spi->dev, "pre-scaler=%d (wanted %d, got %ld)n",
  35.             div, hz, clk / (2 * (div + 1)));

  36.         cs->hz = hz;
  37.         cs->sppre = div;
  38.     }

  39.     return 0;
  40. }
s3c24xx_spi_update_state 方法对设置项做了以下初始化和检查,并未做任何的有关于硬件的任何操作,
所有对硬件的设置是通过s3c24xx_spi_probe中填充好的s3c24xx_spi_chipsel方法实现的

点击(此处)折叠或打开

  1. /* setup the state for the bitbang driver */

  2.     hw->bitbang.master = hw->master;
  3.     hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;
  4.     hw->bitbang.chipselect = s3c24xx_spi_chipsel;
  5.     hw->bitbang.txrx_bufs = s3c24xx_spi_txrx;

  6.     hw->master->setup = s3c24xx_spi_setup;
  7.     hw->master->cleanup = s3c24xx_spi_cleanup;
下面看一下这个方法的具体实现过程:

点击(此处)折叠或打开

  1. static void s3c24xx_spi_chipsel(struct spi_device *spi, int value)
  2. {
  3.     struct s3c24xx_spi_devstate *cs = spi->controller_state;
  4.     struct s3c24xx_spi *hw = to_hw(spi);
  5.     unsigned int cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;

  6.     /* change the chipselect state and the state of the spi engine clock */

  7.     switch (value) {
  8.     case BITBANG_CS_INACTIVE:
  9.         hw->set_cs(hw->pdata, spi->chip_select, cspol^1);
  10.         writeb(cs->spcon, hw->regs + S3C2410_SPCON);
  11.         break;

  12.     case BITBANG_CS_ACTIVE:
  13.         writeb(cs->spcon | S3C2410_SPCON_ENSCK,
  14.         hw->regs + S3C2410_SPCON);
  15.         hw->set_cs(hw->pdata, spi->chip_select, cspol);
  16.         break;
  17.     }
  18. }

//上面对本函数的调用传递的参数是BITBANG_CS_INACTIVE。可以看出上层函数对数据传输模式的设置,

//只能改变spi->mode字段,而不能通过函数spi->master->setup(spi);的调用而将要设置的传输模式写入硬件。

//这或许是linux中的SPI子系统还不够完善吧。但spi->mode的值最终是会被写入SPI的控制寄存器中的,

// hw->bitbang.chipselect(spi, BITBANG_CS_ACTIVE);  的时候。在后面会遇到,这里是在s3c24xx_spi_probe 方法中填充的


**************************************************************************************华丽分割线************************************************************************************
(2)
在iotcl中, 除了(1) 中讲到的设置项,以下便是重点中的重点
retval = spidev_message(spidev, ioc, n_ioc);//数据传输

点击(此处)折叠或打开

  1. static int spidev_message(struct spidev_data *spidev,
  2.         struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
  3. {
  4.     struct spi_message    msg;
  5.     struct spi_transfer    *k_xfers;
  6.     struct spi_transfer    *k_tmp;
  7.     struct spi_ioc_transfer *u_tmp;
  8.     unsigned        n, total;
  9.     u8            *buf;
  10.     int            status = -EFAULT;

  11.     spi_message_init(&msg);
  12.     k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);//每个 spi_transfer代表一段要传输的数据
  13.     if (k_xfers == NULL)
  14.         return -ENOMEM;

  15.     /* Construct spi_message, copying any tx data to bounce buffer.
  16.      * We walk the array of user-provided transfers, using each one
  17.      * to initialize a kernel version of the same transfer.
  18.      */
  19.     buf = spidev->buffer;
  20.     total = 0;
  21.     for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
  22.             n;
  23.             n--, k_tmp++, u_tmp++) {
  24.         k_tmp->len = u_tmp->len;
  25.         //将要传输的数据分成n个数据段每个数据段用一个spi_transfer管理,u_xfers为用户空间传来的数据段
  26.         total += k_tmp->len;//要传输的数据总量
  27.         if (total > bufsiz) {
  28.             status = -EMSGSIZE;
  29.             goto done;
  30.         }

  31.         if (u_tmp->rx_buf) {//需要接收则分给一段用于接收数据的内存
  32.             k_tmp->rx_buf = buf;
  33.             if (!access_ok(VERIFY_WRITE, (u8 __user *)
  34.                         (uintptr_t) u_tmp->rx_buf,
  35.                         u_tmp->len))
  36.                 goto done;
  37.         }
  38.         if (u_tmp->tx_buf) {
  39.             k_tmp->tx_buf = buf;
  40.             if (copy_from_user(buf, (const u8 __user *)
  41.                         (uintptr_t) u_tmp->tx_buf,
  42.                     u_tmp->len))
  43.                 goto done;
  44.         }
  45.         buf += k_tmp->len;//指向下一段内存

  46.         k_tmp->cs_change = !!u_tmp->cs_change;//双非操作取其逻辑值
  47.         k_tmp->bits_per_word = u_tmp->bits_per_word;
  48.         k_tmp->delay_usecs = u_tmp->delay_usecs;//一段数据的完全传输需要一定时间的等待
  49.         k_tmp->speed_hz = u_tmp->speed_hz;
  50. #ifdef VERBOSE
  51.         dev_dbg(&spi->dev,
  52.             " xfer len %zd %s%s%s%dbits %u usec %uHzn",
  53.             u_tmp->len,
  54.             u_tmp->rx_buf ? "rx " : "",
  55.             u_tmp->tx_buf ? "tx " : "",
  56.             u_tmp->cs_change ? "cs " : "",
  57.             u_tmp->bits_per_word ? : spi->bits_per_word,
  58.             u_tmp->delay_usecs,
  59.             u_tmp->speed_hz ? : spi->max_speed_hz);
  60. #endif
  61.         spi_message_add_tail(k_tmp, &msg);//将用于数据传输的数据段挂在msg上
  62.     }

  63.     status = spidev_sync(spidev, &msg);//调用底层函数进行数据传输
  64.     if (status < 0)
  65.         goto done;

  66.     /* copy any rx data out of bounce buffer */
  67.     buf = spidev->buffer;
  68.     for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
  69.         if (u_tmp->rx_buf) {
  70.             if (__copy_to_user((u8 __user *)
  71.                     (uintptr_t) u_tmp->rx_buf, buf,
  72.                     u_tmp->len)) {//分段向用户空间传输数据
  73.                 status = -EFAULT;
  74.                 goto done;
  75.             }
  76.         }
  77.         buf += u_tmp->len;
  78.     }
  79.     status = total;

  80. done:
  81.     kfree(k_xfers);
  82.     return status;
  83. }

点击(此处)折叠或打开

  1. static ssize_t
  2. spidev_sync(struct spidev_data *spidev, struct spi_message *message)
  3. {
  4.     DECLARE_COMPLETION_ONSTACK(done);
  5.     int status;

  6.     message->complete = spidev_complete;
  7.     message->context = &done;//在底层的数据传输函数中会调用函数spidev_complete来通知数据传输完成,在此留一标记

  8.     spin_lock_irq(&spidev->spi_lock);
  9.     if (spidev->spi == NULL)
  10.         status = -ESHUTDOWN;
  11.     else
  12.         status = spi_async(spidev->spi, message);
  13.     spin_unlock_irq(&spidev->spi_lock);

  14.     if (status == 0) {
  15.         wait_for_completion(&done); //等待数据完成
  16.         status = message->status;
  17.         if (status == 0)
  18.             status = message->actual_length;
  19.     }
  20.     return status;
  21. }

点击(此处)折叠或打开

  1. int spi_async(struct spi_device *spi, struct spi_message *message)
  2. {
  3.     struct spi_master *master = spi->master;

  4.     /* Half-duplex links include original MicroWire, and ones with
  5.      * only one data pin like SPI_3WIRE (switches direction) or where
  6.      * either MOSI or MISO is missing. They can also be caused by
  7.      * software limitations.
  8.      */
  9.     if ((master->flags & SPI_MASTER_HALF_DUPLEX)
  10.             || (spi->mode & SPI_3WIRE)) {
  11.         struct spi_transfer *xfer;
  12.         unsigned flags = master->flags;

  13.         list_for_each_entry(xfer, &message->transfers, transfer_list) {
  14.             if (xfer->rx_buf && xfer->tx_buf)
  15.                 return -EINVAL;
  16.             if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
  17.                 return -EINVAL;
  18.             if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
  19.                 return -EINVAL;
  20.         }
  21.     }

  22.     message->spi = spi;
  23.     message->status = -EINPROGRESS;
  24.     return master->transfer(spi, message);
  25. }
  26. EXPORT_SYMBOL_GPL(spi_async);

函数spi->master->transfer()在函数spi_bitbang_start()中被初始化为函数spi_bitbang_transfer()如下

 if (!bitbang->master->transfer)
    bitbang->master->transfer = spi_bitbang_transfer;

函数spi_bitbang_transfer()又在函数s3c24xx_spi_probe()中被调用。

函数spi_bitbang_transfer()在文件spi_bitbang.c中实现。


点击(此处)折叠或打开

  1. int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
  2. {
  3.     struct spi_bitbang    *bitbang;
  4.     unsigned long        flags;
  5.     int            status = 0;

  6.     m->actual_length = 0;
  7.     m->status = -EINPROGRESS;

  8.     bitbang = spi_master_get_devdata(spi->master);

  9.     spin_lock_irqsave(&bitbang->lock, flags);
  10.     if (!spi->max_speed_hz)
  11.         status = -ENETDOWN;
  12.     else {  
  13.         //将携带数据的结构体spi_message挂到bitbang->queue上。每一次数据传输都将要传输的数据包装成结构体spi_message传递
  14.         list_add_tail(&m->queue, &bitbang->queue);
  15.         //将该传输任务添加到工作队列头bitbang->workqueue。接下来将调用任务处理函数进一步数据处理。
  16.         queue_work(bitbang->workqueue, &bitbang->work);
  17.     }
  18.     spin_unlock_irqrestore(&bitbang->lock, flags);

  19.     return status;
  20. }
  21. EXPORT_SYMBOL_GPL(spi_bitbang_transfer);

在此不得不讨论一下结构体,这是一个完成数据传输的重要结构体。

struct spi_bitbang {
 struct workqueue_struct *workqueue; //工作队列头。
 struct work_struct work; //每一次数据传输都传递下来一个spi_message,都向工作队列头添加一个任务。

。。。。。。

//挂接spi_message,如果上一次的spi_message还没处理完接下来的spi_message就挂接在 queue上等待处理。
 struct list_head queue; 
 u8   busy;  //忙碌标识。

。。。。。。

 struct spi_master *master;

 // 以下三个函数都是在函数s3c24xx_spi_probe()中被初始化的。

 int (*setup_transfer)(struct spi_device *spi,struct spi_transfer *t);  //设置数据传输波特率

 void (*chipselect)(struct spi_device *spi, int is_on);  //将数据传输模式写入控制寄存器。

 //向SPTDAT中写入要传输的第一个数据,数据传输是通过中断方式完成的,只要进行一次中断触发,以后向SPTDAT中写数据

 //的工作就在中断处理函数中进行。

 int (*txrx_bufs)(struct spi_device *spi, struct spi_transfer *t); 
。。。。。。
};


数据传输是SPI接口的任务,结构体master代表了一个接口,当一个spi_message从上层函数传递下来时,

master的成员函数bitbang->master->transfer将该数据传输任务添加到工作队列头。

queue_work(bitbang->workqueue, &bitbang->work);

函数bitbang->master->transfer()在上面已经讲解。

工作队列struct workqueue_struct *workqueue;的创建和 struct work_struct work的初始化都是在函数

spi_bitbang_start()中进行的。

INIT_WORK(&bitbang->work, bitbang_work); //bitbang_work是任务处理函数

bitbang->workqueue = create_singlethread_workqueue(dev_name(bitbang->master->dev.parent));

任务处理函数如下


点击(此处)折叠或打开

  1. static void bitbang_work(struct work_struct *work)
  2. {
  3.     struct spi_bitbang    *bitbang =
  4.         container_of(work, struct spi_bitbang, work);
  5.     unsigned long        flags;
  6.     int            do_setup = -1;
  7.     int            (*setup_transfer)(struct spi_device *,
  8.                     struct spi_transfer *);

  9.     setup_transfer = bitbang->setup_transfer;

  10.     spin_lock_irqsave(&bitbang->lock, flags);
  11.     bitbang->busy = 1;
  12.     while (!list_empty(&bitbang->queue)) {
  13.         struct spi_message    *m;
  14.         struct spi_device    *spi;
  15.         unsigned        nsecs;
  16.         struct spi_transfer    *t = NULL;
  17.         unsigned        tmp;
  18.         unsigned        cs_change;
  19.         int            status;

  20.         m = container_of(bitbang->queue.next, struct spi_message,
  21.                 queue);
  22.         list_del_init(&m->queue);
  23.         spin_unlock_irqrestore(&bitbang->lock, flags);

  24.         /* FIXME this is made-up ... the correct value is known to
  25.          * word-at-a-time bitbang code, and presumably chipselect()
  26.          * should enforce these requirements too?
  27.          */
  28.         nsecs = 100;

  29.         spi = m->spi;
  30.         tmp = 0;
  31.         cs_change = 1;
  32.         status = 0;

  33.         list_for_each_entry (t, &m->transfers, transfer_list) {

  34.             /* override speed or wordsize? */
  35.             if (t->speed_hz || t->bits_per_word)
  36.                 do_setup = 1;

  37.             /* init (-1) or override (1) transfer params */
  38.             if (do_setup != 0) {
  39.                 if (!setup_transfer) {
  40.                     status = -ENOPROTOOPT;
  41.                     break;
  42.                 }
  43.                 status = setup_transfer(spi, t);
  44.                 if (status < 0)
  45.                     break;
  46.             }

  47.             /* set up default clock polarity, and activate chip;
  48.              * this implicitly updates clock and spi modes as
  49.              * previously recorded for this device via setup().
  50.              * (and also deselects any other chip that might be
  51.              * selected ...)
  52.              */
  53.             if (cs_change) {
  54.                 //多段数据传输时只需第一段数据传输时调用以下模式设置,将spi->mode
  55.                             //写入SPI的模式控制寄存器。并调用函数hw->set_cs,片选设置
  56.                             //在函数讲解函数spidev_ioctl()中的模式设置时讲到过。在那里  调用的函数chipselect(spi, BITBANG_CS_INACTIVE);
  57.                             //传递的传输是BITBANG_CS_INACTIVE是不能将数据传输模式spi->mode写入SPI控制寄存器的,不过在那里设置了spi->mode的值。
  58.                 bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
  59.                 ndelay(nsecs);
  60.             }
  61.             cs_change = t->cs_change;
  62.             if (!t->tx_buf && !t->rx_buf && t->len) {
  63.                 status = -EINVAL;
  64.                 break;
  65.             }

  66.             /* transfer data. the lower level code handles any
  67.              * new dma mappings it needs. our caller always gave
  68.              * us dma-safe buffers.
  69.              */
  70.             if (t->len) {
  71.                 /* REVISIT dma API still needs a designated
  72.                  * DMA_ADDR_INVALID; ~0 might be better.
  73.                  */
  74.                 if (!m->is_dma_mapped)
  75.                     t->rx_dma = t->tx_dma = 0;
  76.                 status = bitbang->txrx_bufs(spi, t);
  77.             }
  78.             if (status > 0)
  79.                 m->actual_length += status;
  80.             if (status != t->len) {
  81.                 /* always report some kind of error */
  82.                 if (status >= 0)
  83.                     status = -EREMOTEIO;
  84.                 break;
  85.             }
  86.             status = 0;

  87.             /* protocol tweaks before next transfer */
  88.             if (t->delay_usecs)//延时等待一段数据传输完,因为一段数据要经过多次中断传输
  89.                 udelay(t->delay_usecs);

  90.             if (!cs_change)//多段数据传输时t->cs_change为0表示下面还有未传输数据否者判断遍历是否结束
  91.                 continue;
  92.             if (t->transfer_list.next == &m->transfers)
  93.                 break;

  94.             /* sometimes a short mid-message deselect of the chip
  95.              * may be needed to terminate a mode or command
  96.              */
  97.             ndelay(nsecs);
  98.             bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  99.             ndelay(nsecs);
  100.         }

  101.         m->status = status;
  102.         m->complete(m->context);// 通知一次数据传输完

  103.         /* restore speed and wordsize if it was overridden */
  104.         if (do_setup == 1)
  105.             setup_transfer(spi, NULL);
  106.         do_setup = 0;

  107.         /* normally deactivate chipselect ... unless no error and
  108.          * cs_change has hinted that the next message will probably
  109.          * be for this chip too.
  110.          */
  111.         if (!(status == 0 && cs_change)) {
  112.             ndelay(nsecs);
  113.             bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
  114.             ndelay(nsecs);
  115.         }

  116.         spin_lock_irqsave(&bitbang->lock, flags);
  117.     }
  118.     bitbang->busy = 0;
  119.     spin_unlock_irqrestore(&bitbang->lock, flags);
  120. }


点击(此处)折叠或打开

  1. static int s3c24xx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  2. {
  3.     struct s3c24xx_spi *hw = to_hw(spi);

  4.     dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %dn",
  5.         t->tx_buf, t->rx_buf, t->len);

  6.     hw->tx = t->tx_buf;
  7.     hw->rx = t->rx_buf;
  8.     hw->len = t->len;
  9.     hw->count = 0;

  10.     init_completion(&hw->done);

  11.     /* send the first byte */
  12.     writeb(hw_txbyte(hw, 0), hw->regs + S3C2410_SPTDAT);

  13.     wait_for_completion(&hw->done);

  14.     return hw->count;
  15. }
********************************************************************************************************
函数hw_txbyte()的实现如下
static inline unsigned int hw_txbyte(struct s3c24xx_spi *hw, int count)
{
return hw->tx ? hw->tx[count] : 0;
}
将要传输的数据段的第一个数据写入SPI数据寄存器S3C2410_SPTDAT。即便是数据接收也得向数据寄存器写入数据
才能触发一次数据的传输。只需将该数据段的第一个数据写入数据寄存器就可以触发数据传输结束中断,以后的数据
就在中断处理函数中写入数据寄存器。
********************************************************************************************************

数据传输结束中断

点击(此处)折叠或打开

  1. static irqreturn_t s3c24xx_spi_irq(int irq, void *dev)
  2. {
  3.     struct s3c24xx_spi *hw = dev;
  4.     unsigned int spsta = readb(hw->regs + S3C2410_SPSTA);
  5.     unsigned int count = hw->count;

  6.     if (spsta & S3C2410_SPSTA_DCOL) {
  7.         dev_dbg(hw->dev, "data-collisionn");
  8.         complete(&hw->done);
  9.         goto irq_done;
  10.     }

  11.     if (!(spsta & S3C2410_SPSTA_READY)) {//数据准备好
  12.         dev_dbg(hw->dev, "spi not ready for tx?n");
  13.         complete(&hw->done);
  14.         goto irq_done;
  15.     }

  16.     hw->count++;

  17.     if (hw->rx)
  18.         hw->rx[count] = readb(hw->regs + S3C2410_SPRDAT);//接收数据

  19.     count++;

  20.     if (count < hw->len)
  21.         writeb(hw_txbyte(hw, count), hw->regs + S3C2410_SPTDAT);//写入数据
  22.     else
  23.         complete(&hw->done);

  24.  irq_done:
  25.     return IRQ_HANDLED;
  26. }
以上为记录运行流程,细节待修正补充
好了,稍微总结一下:
spi的读写请求通过:
spi_transfer->spi_message->spi_bitbang添加都bitbang->queue中,被bitbang->work反方向提取出来执行(后面会提到)。

通过queue_work(bitbang->workqueue, &bitbang->work)把bitbang-work加入bitbang->workqueue后,在某个合适的时间, bitbang->work将被调度运行,bitbang_work函数将被调用

阅读(5112) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~