设备树学习之(十)spi flash

来源:互联网 发布:reveal.js echarts 编辑:程序博客网 时间:2024/06/10 11:39

转载自:http://blog.csdn.net/lizuobin2/article/details/54565214

开发板:tiny4412SDK + S702 + 4GB Flash
要移植的内核版本:Linux-4.4.0 (支持device tree)
u-boot版本:友善之臂自带的 U-Boot 2010.12
busybox版本:busybox 1.25

目标:
驱动外接的8M的 spi flash,注册为块设备。

设备树:

&spi_0 {        status = "okay";        cs-gpios = <&gpb 1 GPIO_ACTIVE_HIGH>;        spi_flash@0 {                compatible = "tiny4412,spi_flash";                spi-max-frequency = <10000000>;                reg = <0>;                controller-data {                        samsung,spi-feedback-delay = <0>;                };        };};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这里指定的 reg = <0> 表示的该 spi 设备引用第一个 cs-gpios
spi-max-frequency = <10000000>;表示最大速率

代码:

#include <linux/clk.h>#include <linux/delay.h>#include <linux/firmware.h>#include <linux/gpio.h>#include <linux/i2c.h>#include <linux/init.h>#include <linux/media.h>#include <linux/module.h>#include <linux/of_gpio.h>#include <linux/regulator/consumer.h>#include <linux/sizes.h>#include <linux/slab.h>#include <linux/spi/spi.h>#include <linux/videodev2.h>#include <media/media-entity.h>#include <media/v4l2-ctrls.h>#include <media/v4l2-device.h>#include <media/v4l2-subdev.h>#include <media/v4l2-mediabus.h>#include <media/s5c73m3.h>#include <media/v4l2-of.h>#include <linux/mtd/mtd.h>/* 参考: * drivers\mtd\devices\mtdram.c * drivers/mtd/devices/m25p80.c */static struct spi_device *spi_flash;void SPIFlashReadID(int *pMID, int *pDID){    unsigned char tx_buf[4];    unsigned char rx_buf[2];    //printk("%s\n",__func__);    tx_buf[0] = 0x90;    tx_buf[1] = 0;    tx_buf[2] = 0;    tx_buf[3] = 0;    spi_write_then_read(spi_flash, tx_buf, 4, rx_buf, 2);    *pMID = rx_buf[0];    *pDID = rx_buf[1];}static void SPIFlashWriteEnable(int enable){    unsigned char val = enable ? 0x06 : 0x04;    //printk("%s\n",__func__);    spi_write(spi_flash, &val, 1);}static unsigned char SPIFlashReadStatusReg1(void){    unsigned char val;    unsigned char cmd = 0x05;    //printk("%s\n",__func__);    spi_write_then_read(spi_flash, &cmd, 1, &val, 1);    return val;}static unsigned char SPIFlashReadStatusReg2(void){    unsigned char val;    unsigned char cmd = 0x35;    //printk("%s\n",__func__);    spi_write_then_read(spi_flash, &cmd, 1, &val, 1);    return val;}static void SPIFlashWaitWhenBusy(void){    //printk("%s\n",__func__);    while (SPIFlashReadStatusReg1() & 1)    {        /* 休眠一段时间 */        /* Sector erase time : 60ms         * Page program time : 0.7ms         * Write status reg time : 10ms         */        set_current_state(TASK_INTERRUPTIBLE);        schedule_timeout(HZ / 100); /* 休眠10MS后再次判断 */    }}static void SPIFlashWriteStatusReg(unsigned char reg1, unsigned char reg2){    unsigned char tx_buf[4];    //printk("%s\n",__func__);    SPIFlashWriteEnable(1);    tx_buf[0] = 0x01;    tx_buf[1] = reg1;    tx_buf[2] = reg2;    spi_write(spi_flash, tx_buf, 3);    SPIFlashWaitWhenBusy();}static void SPIFlashClearProtectForStatusReg(void){    unsigned char reg1, reg2;    //printk("%s\n",__func__);    reg1 = SPIFlashReadStatusReg1();    reg2 = SPIFlashReadStatusReg2();    reg1 &= ~(1 << 7);    reg2 &= ~(1 << 0);    SPIFlashWriteStatusReg(reg1, reg2);}static void SPIFlashClearProtectForData(void){    /* cmp=0,bp2,1,0=0b000 */    unsigned char reg1, reg2;    //printk("%s\n",__func__);    reg1 = SPIFlashReadStatusReg1();    reg2 = SPIFlashReadStatusReg2();    reg1 &= ~(7 << 2);    reg2 &= ~(1 << 6);    SPIFlashWriteStatusReg(reg1, reg2);}/* erase 4K */void SPIFlashEraseSector(unsigned int addr){    unsigned char tx_buf[4];    //printk("%s\n",__func__);    tx_buf[0] = 0x20;    tx_buf[1] = addr >> 16;    tx_buf[2] = addr >> 8;    tx_buf[3] = addr & 0xff;    SPIFlashWriteEnable(1);    spi_write(spi_flash, tx_buf, 4);    SPIFlashWaitWhenBusy();}/* program */void SPIFlashProgram(unsigned int addr, unsigned char *buf, int len){    unsigned char tx_buf[4];    struct spi_transfer t[] =    {        {            .tx_buf     = tx_buf,            .len        = 4,        },        {            .tx_buf     = buf,            .len        = len,        },    };    struct spi_message  m;    //printk("%s\n",__func__);    tx_buf[0] = 0x02;    tx_buf[1] = addr >> 16;    tx_buf[2] = addr >> 8;    tx_buf[3] = addr & 0xff;    SPIFlashWriteEnable(1);    spi_message_init(&m);    spi_message_add_tail(&t[0], &m);    spi_message_add_tail(&t[1], &m);    spi_sync(spi_flash, &m);    SPIFlashWaitWhenBusy();}void SPIFlashRead(unsigned int addr, unsigned char *buf, int len){    /* spi_write_then_read规定了tx_cnt+rx_cnt < 32     * 所以对于大量数据的读取,不能使用该函数     */    unsigned char tx_buf[4];    struct spi_transfer t[] =    {        {            .tx_buf     = tx_buf,            .len        = 4,        },        {            .rx_buf     = buf,            .len        = len,        },    };    struct spi_message  m;    //printk("%s\n",__func__);    tx_buf[0] = 0x03;    tx_buf[1] = addr >> 16;    tx_buf[2] = addr >> 8;    tx_buf[3] = addr & 0xff;    spi_message_init(&m);    spi_message_add_tail(&t[0], &m);    spi_message_add_tail(&t[1], &m);    spi_sync(spi_flash, &m);}static void SPIFlashInit(void){    SPIFlashClearProtectForStatusReg();    SPIFlashClearProtectForData();}/* 构造注册一个mtd_info * mtd_device_register(master, parts, nr_parts) * *//* 首先: 构造注册spi_driver * 然后: 在spi_driver的probe函数里构造注册mtd_info */static struct mtd_info spi_flash_dev;static int spi_flash_erase(struct mtd_info *mtd, struct erase_info *instr){    unsigned int addr = instr->addr;    unsigned int len  = 0;    //判断参数     if ((addr & (spi_flash_dev.erasesize - 1)) || (instr->len & (spi_flash_dev.erasesize - 1)))    {        printk("spi_flash_erase addr/len is not aligned %x %x\n",(unsigned int)instr->addr,(unsigned int)instr->len);        return -EINVAL;    }    for (len = 0; len < instr->len; len += 4096)    {        SPIFlashEraseSector(addr);        addr += 4096;    }    instr->state = MTD_ERASE_DONE;    mtd_erase_callback(instr);    return 0;}static int spi_flash_read(struct mtd_info *mtd, loff_t from, size_t len,                          size_t *retlen, u_char *buf){    SPIFlashRead(from, buf, len);    *retlen = len;    return 0;}static int spi_flash_write(struct mtd_info *mtd, loff_t to, size_t len,                           size_t *retlen, const u_char *buf){    unsigned int addr = to;    unsigned int wlen  = 0;    unsigned int i;    //判断参数    if ((to & (spi_flash_dev.erasesize - 1)))    {        printk("spi_flash_write addr/len is not aligned %x %x\n",(unsigned int)to,(unsigned int)len);        return -EINVAL;    }    if (len <= 256)    {        SPIFlashProgram(addr, (unsigned char *)buf, len);    }    else    {        for (i = 0; i < len / 256; i ++)        {            SPIFlashProgram(addr, (unsigned char *)buf, 256);            addr += 256;            buf += 256;        }        if (len % 256 != 0)        {            SPIFlashProgram(addr, (unsigned char *)buf, len % 256);        }    }    *retlen = len;    return 0;}static int spi_flash_probe(struct spi_device *spi){    int mid, did, ret;    spi_flash = spi;    spi->mode = SPI_MODE_0;    ret = spi_setup(spi);    if (ret < 0)    {        printk("spi_setup error\n");    }    printk("%s\n",__func__);    //s3c2410_gpio_cfgpin(spi->chip_select, S3C2410_GPIO_OUTPUT);    SPIFlashInit();    SPIFlashReadID(&mid, &did);    printk("SPI Flash ID: %02x %02x\n", mid, did);    memset(&spi_flash_dev, 0, sizeof(spi_flash_dev));    //Setup the MTD structure    spi_flash_dev.name = "spi_flash";    spi_flash_dev.type = MTD_NORFLASH;    spi_flash_dev.flags = MTD_CAP_NORFLASH;    spi_flash_dev.size = 0x800000;     spi_flash_dev.writesize = 1;    spi_flash_dev.writebufsize = 4096;     spi_flash_dev.erasesize = 4096;     spi_flash_dev.owner = THIS_MODULE;    spi_flash_dev._erase = spi_flash_erase;    spi_flash_dev._read  = spi_flash_read;    spi_flash_dev._write = spi_flash_write;    mtd_device_register(&spi_flash_dev, NULL, 0);    return 0;}static int spi_flash_remove(struct spi_device *spi){    mtd_device_unregister(&spi_flash_dev);    return 0;}static const struct of_device_id spi_flash_ids[] = {    { .compatible = "tiny4412,spi_flash" },    { }};static struct spi_driver spi_flash_drv ={    .driver = {        .name   = "spi_flash",    },    .probe      = spi_flash_probe,    .remove     = spi_flash_remove,};static int spi_flash_init(void){    printk("init\n");    return spi_register_driver(&spi_flash_drv);}static void spi_flash_exit(void){    spi_unregister_driver(&spi_flash_drv);}module_init(spi_flash_init);module_exit(spi_flash_exit);MODULE_LICENSE("GPL");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
原创粉丝点击