summaryrefslogtreecommitdiffstats
path: root/shared/opensource/spi/bcmSpiRes.c
blob: ed16575e2d578c86707185c997269b23240202be (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
    Copyright 2000-2010 Broadcom Corporation

    Unless you and Broadcom execute a separate written software license
    agreement governing use of this software, this software is licensed
    to you under the terms of the GNU General Public License version 2
    (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
    with the following added to such license:

        As a special exception, the copyright holders of this software give
        you permission to link this software with independent modules, and to
        copy and distribute the resulting executable under terms of your
        choice, provided that you also meet, for each linked independent
        module, the terms and conditions of the license of that module. 
        An independent module is a module which is not derived from this
        software.  The special exception does not apply to any modifications
        of the software.

    Notwithstanding the above, under no circumstances may you combine this
    software in any way with any other Broadcom software provided under a
    license other than the GPL, without Broadcom's express prior written
    consent.
*/                       

#ifdef _CFE_
#include "lib_types.h"
#include "lib_printf.h"
#include "lib_string.h"
#include "bcm_map.h"  
#define  printk  printf
#else
#include <linux/version.h>
#include <linux/autoconf.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29)
#include <linux/semaphore.h>
#endif
#include <linux/spi/spi.h>
#include <linux/autoconf.h>

#include <bcm_map_part.h>
#endif
#include "bcmSpiRes.h"

extern int BcmLegSpiRead(unsigned char * msg_buf, int prependcnt, int nbytes, int devId, int freqHz);
extern int BcmLegSpiWrite(unsigned char * msg_buf, int nbytes, int devId, int freqHz);
extern int BcmHsSpiRead(unsigned char * msg_buf, int prependcnt, int nbytes, int devId, int freqHz);
extern int BcmHsSpiWrite(unsigned char * msg_buf, int nbytes, int devId, int freqHz);

#ifndef _CFE_
#ifdef SPI
/* the BCM legacy controller supports up to 8 devices */
static struct spi_board_info bcmLegSpiDevInfo[8] =
{
    {
        .modalias      = "bcm_LegSpiDev0",
        .chip_select   = 0,
        .max_speed_hz  = 781000,
        .bus_num       = LEG_SPI_BUS_NUM,
        .mode          = SPI_MODE_3,
    },
    {
        .modalias      = "bcm_LegSpiDev1",
        .chip_select   = 1,
        .max_speed_hz  = 781000,
        .bus_num       = LEG_SPI_BUS_NUM,
        .mode          = SPI_MODE_3,
    },
    {
        .modalias      = "bcm_LegSpiDev2",
        .chip_select   = 2,
        .max_speed_hz  = 781000,
        .bus_num       = LEG_SPI_BUS_NUM,
        .mode          = SPI_MODE_3,
    },
    {
        .modalias      = "bcm_LegSpiDev3",
        .chip_select   = 3,
        .max_speed_hz  = 781000,
        .bus_num       = LEG_SPI_BUS_NUM,
        .mode          = SPI_MODE_3,
    },
    {
        .modalias      = "bcm_LegSpiDev4",
        .chip_select   = 4,
        .max_speed_hz  = 781000,
        .bus_num       = LEG_SPI_BUS_NUM,
        .mode          = SPI_MODE_3,
    },
    {
        .modalias      = "bcm_LegSpiDev5",
        .chip_select   = 5,
        .max_speed_hz  = 781000,
        .bus_num       = LEG_SPI_BUS_NUM,
        .mode          = SPI_MODE_3,
    },
    {
        .modalias      = "bcm_LegSpiDev6",
        .chip_select   = 6,
        .max_speed_hz  = 781000,
        .bus_num       = LEG_SPI_BUS_NUM,
        .mode          = SPI_MODE_3,
    },
    {
        .modalias      = "bcm_LegSpiDev7",
        .chip_select   = 7,
        .max_speed_hz  = 781000,
        .bus_num       = LEG_SPI_BUS_NUM,
        .mode          = SPI_MODE_3,
    },
};

static struct spi_driver bcmLegSpiDevDrv[8] = 
{
    {
        .driver = 
            {
            .name     = "bcm_LegSpiDev0",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_LegSpiDev1",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_LegSpiDev2",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_LegSpiDev3",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_LegSpiDev4",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    }, 
    {
        .driver = 
            {
            .name     = "bcm_LegSpiDev5",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_LegSpiDev6",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_LegSpiDev7",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
};

static struct spi_device * bcmLegSpiDevices[8];
#endif

#ifdef HS_SPI
/* the BCM HS controller supports up to 8 devices */
static struct spi_board_info bcmHSSpiDevInfo[8] =
{
    {
        .modalias        = "bcm_HSSpiDev0",
        .controller_data = (void *)SPI_CONTROLLER_STATE_DEFAULT,
        .chip_select     = 0,
        .max_speed_hz    = 781000,
        .bus_num         = HS_SPI_BUS_NUM,
        .mode            = SPI_MODE_DEFAULT,
    },
    {
        .modalias        = "bcm_HSSpiDev1",
        .controller_data = (void *)SPI_CONTROLLER_STATE_DEFAULT,
        .chip_select     = 1,
        .max_speed_hz    = 781000,
        .bus_num         = HS_SPI_BUS_NUM,
        .mode            = SPI_MODE_DEFAULT,
    },
    {
        .modalias        = "bcm_HSSpiDev2",
        .controller_data = (void *)SPI_CONTROLLER_STATE_DEFAULT,
        .chip_select     = 2,
        .max_speed_hz    = 781000,
        .bus_num         = HS_SPI_BUS_NUM,
        .mode            = SPI_MODE_DEFAULT,
    },
    {
        .modalias        = "bcm_HSSpiDev3",
        .controller_data = (void *)SPI_CONTROLLER_STATE_DEFAULT,
        .chip_select     = 3,
        .max_speed_hz    = 781000,
        .bus_num         = HS_SPI_BUS_NUM,
        .mode            = SPI_MODE_DEFAULT,
    },
    {
        .modalias        = "bcm_HSSpiDev4",
        .controller_data = (void *)SPI_CONTROLLER_STATE_DEFAULT,
        .chip_select     = 4,
        .max_speed_hz    = 781000,
        .bus_num         = HS_SPI_BUS_NUM,
        .mode            = SPI_MODE_DEFAULT,
    },
    {
        .modalias        = "bcm_HSSpiDev5",
        .controller_data = (void *)SPI_CONTROLLER_STATE_DEFAULT,
        .chip_select     = 5,
        .max_speed_hz    = 781000,
        .bus_num         = HS_SPI_BUS_NUM,
        .mode            = SPI_MODE_DEFAULT,
    },
    {
        .modalias        = "bcm_HSSpiDev6",
        .controller_data = (void *)SPI_CONTROLLER_STATE_DEFAULT,
        .chip_select     = 6,
        .max_speed_hz    = 781000,
        .bus_num         = HS_SPI_BUS_NUM,
        .mode            = SPI_MODE_DEFAULT,
    },
    {
        .modalias        = "bcm_HSSpiDev7",
        .controller_data = (void *)SPI_CONTROLLER_STATE_DEFAULT,
        .chip_select     = 7,
        .max_speed_hz    = 781000,
        .bus_num         = HS_SPI_BUS_NUM,
        .mode            = SPI_MODE_DEFAULT,
    },
};

static struct spi_driver bcmHSSpiDevDrv[8] = 
{
    {
        .driver = 
            {
            .name     = "bcm_HSSpiDev0",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_HSSpiDev1",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_HSSpiDev2",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_HSSpiDev3",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_HSSpiDev4",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    }, 
    {
        .driver = 
            {
            .name     = "bcm_HSSpiDev5",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_HSSpiDev6",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
    {
        .driver = 
            {
            .name     = "bcm_HSSpiDev7",
            .bus      = &spi_bus_type,
            .owner    = THIS_MODULE,
            },
    },
};

static struct spi_device * bcmHSSpiDevices[8];
#endif


int BcmSpiReserveSlave2(int busNum, int slaveId, int maxFreq, int spiMode, int ctrlState)
{
    struct spi_master * pSpiMaster;
    struct spi_driver * pSpiDriver;

    if ( slaveId > 7 )
    {
        return SPI_STATUS_ERR;
    }

    if ( LEG_SPI_BUS_NUM == busNum )
    {
#ifndef SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL != bcmLegSpiDevices[slaveId] )
        {
            printk(KERN_ERR "BcmSpiReserveSlave - slaveId %d, already registerd\n", slaveId);
            return( SPI_STATUS_ERR );
        }

        bcmLegSpiDevInfo[slaveId].max_speed_hz    = maxFreq;
        bcmLegSpiDevInfo[slaveId].controller_data = (void *)ctrlState;
        bcmLegSpiDevInfo[slaveId].mode            = spiMode;
        
        pSpiMaster                = spi_busnum_to_master( busNum );
        bcmLegSpiDevices[slaveId] = spi_new_device(pSpiMaster, &bcmLegSpiDevInfo[slaveId]);
        pSpiDriver                = &bcmLegSpiDevDrv[slaveId];
#endif
    }
    else if ( HS_SPI_BUS_NUM == busNum )
    {
#ifndef HS_SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL != bcmHSSpiDevices[slaveId] )
        {
            printk(KERN_ERR "BcmSpiReserveSlave - slaveId %d, already registerd\n", slaveId);
            return( SPI_STATUS_ERR );
        }

        bcmHSSpiDevInfo[slaveId].max_speed_hz    = maxFreq;
        bcmHSSpiDevInfo[slaveId].controller_data = (void *)ctrlState;
        bcmHSSpiDevInfo[slaveId].mode            = spiMode;
        
        pSpiMaster               = spi_busnum_to_master( busNum );
        bcmHSSpiDevices[slaveId] = spi_new_device(pSpiMaster, &bcmHSSpiDevInfo[slaveId]);
        pSpiDriver               = &bcmHSSpiDevDrv[slaveId];
#endif        
    }
    else
        return( SPI_STATUS_ERR );

    /* register the SPI driver */
    spi_register_driver(pSpiDriver);

    return 0;
    
}
EXPORT_SYMBOL(BcmSpiReserveSlave2);

int BcmSpiReserveSlave(int busNum, int slaveId, int maxFreq)
{
   return( BcmSpiReserveSlave2(busNum, slaveId, maxFreq, SPI_MODE_DEFAULT, SPI_CONTROLLER_STATE_DEFAULT) );
}
EXPORT_SYMBOL(BcmSpiReserveSlave);

int BcmSpiReleaseSlave(int busNum, int slaveId)
{
    if ( slaveId > 7 )
    {
        return SPI_STATUS_ERR;
    }

    if ( LEG_SPI_BUS_NUM == busNum )
    {
#ifndef SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL == bcmLegSpiDevices[slaveId] )
        {
            printk(KERN_ERR "BcmSpiReleaseSlave - slaveId %d, already released\n", slaveId);
            return( SPI_STATUS_ERR );
        }

        bcmLegSpiDevInfo[slaveId].max_speed_hz = 781000;
        spi_unregister_driver(&bcmLegSpiDevDrv[slaveId]);
        spi_unregister_device(bcmLegSpiDevices[slaveId]);
        bcmLegSpiDevices[slaveId] = 0;
#endif
    }
    else if ( HS_SPI_BUS_NUM == busNum )
    {
#ifndef HS_SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL == bcmHSSpiDevices[slaveId] )
        {
            printk(KERN_ERR "BcmSpiReleaseSlave - slaveId %d, already released\n", slaveId);
            return( SPI_STATUS_ERR );
        }

        bcmHSSpiDevInfo[slaveId].max_speed_hz = 781000;
        spi_unregister_driver(&bcmHSSpiDevDrv[slaveId]);
        spi_unregister_device(bcmHSSpiDevices[slaveId]);
        bcmHSSpiDevices[slaveId] = 0;
#endif        
    }
    else
        return( SPI_STATUS_ERR );

    return 0;
    
}
EXPORT_SYMBOL(BcmSpiReleaseSlave);


int BcmSpiSyncTrans(unsigned char *txBuf, unsigned char *rxBuf, int prependcnt, int nbytes, int busNum, int slaveId)
{
    struct spi_message  msg;
    struct spi_transfer xfer[2];
    int                 status;
    int                 maxLength;
    struct spi_device  *pSpiDevice;

    maxLength = BcmSpi_GetMaxRWSize(busNum);
    if ( (nbytes > maxLength) || (prependcnt > maxLength) )
    {
        printk(KERN_ERR "ERROR BcmSpiSyncTrans: invalid length len %d, pre %d, max %d\n", nbytes, prependcnt, maxLength);
        return SPI_STATUS_ERR;
    }
    
    if ( slaveId > 7 )
    {
        printk(KERN_ERR "ERROR BcmSpiSyncTrans: invalid slave id %d\n", slaveId);
        return SPI_STATUS_ERR;
    }

    if ( LEG_SPI_BUS_NUM == busNum )
    {
#ifndef SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL == bcmLegSpiDevices[slaveId] )
        {
            printk(KERN_ERR "ERROR BcmSpiSyncTrans: device not registered\n");
            return SPI_STATUS_ERR;
        } 
        pSpiDevice = bcmLegSpiDevices[slaveId];
#endif
    }
    else if ( HS_SPI_BUS_NUM == busNum )
    {
#ifndef HS_SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL == bcmHSSpiDevices[slaveId] )
        {
            printk(KERN_ERR "ERROR BcmSpiSyncTrans: device not registered\n");
            return SPI_STATUS_ERR;
        } 
        pSpiDevice = bcmHSSpiDevices[slaveId];
#endif
    }
    else
        return( SPI_STATUS_ERR );

    spi_message_init(&msg);
    memset(xfer, 0, (sizeof xfer));

    if ( prependcnt )
    {
        xfer[0].len      = prependcnt;
        xfer[0].speed_hz = pSpiDevice->max_speed_hz;
        if ( txBuf )
        {
            xfer[0].tx_buf = txBuf;
        }
        else
        {
            xfer[0].tx_buf = rxBuf;
        }
        spi_message_add_tail(&xfer[0], &msg);
    }

    xfer[1].len      = nbytes;
    xfer[1].speed_hz = pSpiDevice->max_speed_hz;
    xfer[1].rx_buf   = rxBuf;

    /* for the controller to use the prepend count correctly the first operation must be a read and the second a write 
       make sure tx is NULL for second transaction */
    if ( 0 == prependcnt )
    {
        xfer[1].tx_buf = txBuf;
    }
    spi_message_add_tail(&xfer[1], &msg);

    status = spi_sync(pSpiDevice, &msg);
    if (status >= 0)
    {
        status = SPI_STATUS_OK;
    } 
    else
    {
        status = SPI_STATUS_ERR;
    }

    return( status );

}
EXPORT_SYMBOL(BcmSpiSyncTrans);
#endif

int BcmSpi_SetFlashCtrl( int opCode, int addrBytes, int dummyBytes, int busNum, int devId )
{
    if ( HS_SPI_BUS_NUM == busNum )
    {
#ifndef HS_SPI
        return SPI_STATUS_ERR;
#else
        int clock;

        clock = HS_SPI_PLL_FREQ/HS_SPI_CLOCK_DEF;
        if (HS_SPI_PLL_FREQ%HS_SPI_CLOCK_DEF)
            clock++;

        clock = 2048/clock;
        if (2048%(clock))
            clock++;

        HS_SPI_PROFILES[0].clk_ctrl = 1<<HS_SPI_ACCUM_RST_ON_LOOP | 0<<HS_SPI_SPI_CLK_2X_SEL | clock<<HS_SPI_FREQ_CTRL_WORD;
        HS_SPI->hs_spiFlashCtrl = devId<<HS_SPI_FCTRL_SS_NUM | 0<<HS_SPI_FCTRL_PROFILE_NUM | dummyBytes<<HS_SPI_FCTRL_DUMMY_BYTES | 
                                  addrBytes<<HS_SPI_FCTRL_ADDR_BYTES | opCode<<HS_SPI_FCTRL_READ_OPCODE;
#endif
    }
    else if ( LEG_SPI_BUS_NUM == busNum )
    {
#ifndef SPI
        return SPI_STATUS_ERR;
#endif 
    }
    else
        return SPI_STATUS_ERR;
            
    return SPI_STATUS_OK;
    
}


int BcmSpi_GetMaxRWSize( int busNum )
{
    int maxRWSize = 0;

    if ( HS_SPI_BUS_NUM == busNum )
    {
#ifdef HS_SPI
        maxRWSize = HS_SPI_BUFFER_LEN;
#endif
    }
    else if ( LEG_SPI_BUS_NUM == busNum )
    {
#ifdef SPI
        maxRWSize = sizeof(SPI->spiMsgData);
#endif
    }

    maxRWSize &= ~0x3;

    return(maxRWSize);

}


/* The interface bcmSpi_Read and bcmSpi_Write provide direct access to the SPI controller.
   these interfaces should only be called by CFE and early spi flash code */
int BcmSpi_Read( unsigned char *msg_buf, int prependcnt, int nbytes, int busNum, int devId, int freqHz )
{
    if ( LEG_SPI_BUS_NUM == busNum )
    {
#ifndef SPI
        return SPI_STATUS_ERR;
#else
        return BcmLegSpiRead( msg_buf, prependcnt, nbytes, devId, freqHz );
#endif
    }
    else if ( HS_SPI_BUS_NUM == busNum )
    {
#ifndef HS_SPI
        return SPI_STATUS_ERR;
#else
        return BcmHsSpiRead( msg_buf, prependcnt, nbytes, devId, freqHz );
#endif
    }
    else
    {
        return SPI_STATUS_ERR;
    }

}

int BcmSpi_Write( unsigned char *msg_buf, int nbytes, int busNum, int devId, int freqHz )
{
    if ( LEG_SPI_BUS_NUM == busNum )
    {
#ifndef SPI
        return SPI_STATUS_ERR;
#else
        return BcmLegSpiWrite( msg_buf, nbytes, devId, freqHz );
#endif
    }
    else if ( HS_SPI_BUS_NUM == busNum )
    {
#ifndef HS_SPI
        return SPI_STATUS_ERR;
#else
        return BcmHsSpiWrite( msg_buf, nbytes, devId, freqHz );
#endif
    }
    else
    {
        return SPI_STATUS_ERR;
    }
}

#ifndef _CFE_
EXPORT_SYMBOL(BcmSpi_SetFlashCtrl);
EXPORT_SYMBOL(BcmSpi_GetMaxRWSize);
#endif