source: rtems/c/src/lib/libbsp/arm/gp32/smc/smc.c @ e9e422e

4.104.114.95
Last change on this file since e9e422e was e9e422e, checked in by Chris Johns <chrisj@…>, on 07/29/08 at 02:56:20

2008-07-29 Chris Johns <chrisj@…>

  • smc/smc.c: Updated to the libblock changes.
  • Property mode set to 100644
File size: 13.7 KB
Line 
1/* smc.c -- s3c2400 smc disk block device implementation
2
3 Squidge's SMC Low-level access routines. 
4 Inspired and derived from routines provided by Samsung Electronics M/M R&D Center & FireFly.
5
6*/
7
8#include <rtems.h>
9#include <rtems/libio.h>
10#include <errno.h>
11#include <stdlib.h>
12#include <stdio.h>
13#include <string.h>
14#include <inttypes.h>
15
16#include "rtems/blkdev.h"
17#include "rtems/diskdevs.h"
18#include "smc.h"
19#include <rtems/bspIo.h>
20#include <s3c24xx.h>
21
22#define SMC_DEVICE_NAME "/dev/smc"
23#define SMC_SAMSUNG_ID          0xEC
24#define SMC_TOSHIBA_ID          0x98
25
26#define SMC_16MB        0x73
27#define SMC_32MB        0x75
28#define SMC_64MB        0x76
29#define SMC_128MB       0x79
30
31#define LBA_UNUSED              0x80000000
32#define LBA_RESERVED            0x80000001
33
34#define BLOCK_UNUSED            0x80000000
35#define BLOCK_RESERVED          0x80000001
36
37/* SmartMedia Command */
38#define SEQ_DATA_INPUT_CMD      0x80
39#define READ1_CMD               0x00
40#define READ1_1_CMD             0x01
41#define READ2_CMD               0x50
42#define READ_ID_CMD             0x90
43#define RESET_CMD               0xFF
44#define PAGE_PROGRAM_CMD        0x10
45#define BLOCK_ERASE_CMD         0x60
46#define BLOCK_ERASE_CFM_CMD     0xD0
47#define READ_STATUS_CMD         0x70
48#define RESET_PTR_CMD           0x00
49
50
51/* Internal SMC disk descriptor */
52struct SMC_INFO
53{
54  uint8_t  id[3];
55  uint32_t bytes_per_page;
56  uint32_t pages_per_block;
57  uint32_t blocks;
58  uint32_t mb;
59};
60
61#ifdef CPU_S3C2410      //different regester map
62#define rPBDAT rGPBDAT
63#define rPBCON rGPBCON
64#define rPDDAT rGPDDAT
65#define rPEDAT rGPEDAT
66#endif
67
68
69static struct SMC_INFO smc_info;
70
71uint32_t smc_l2p[0x2000];
72uint32_t smc_p2l[0x2000];
73
74#define sm_busy() while (!(rPDDAT & 0x200))
75#define sm_chip_en() rPDDAT &= (~0x80)
76#define sm_chip_dis() rPDDAT |= 0x80
77#define sm_cle_en() rPEDAT |= 0x20
78#define sm_cle_dis() rPEDAT &= (~0x20)
79#define sm_ale_en() rPEDAT |= 0x10
80#define sm_ale_dis() rPEDAT &= (~0x10)
81#define sm_wp_en() rPDDAT &= (~0x40)
82#define sm_wp_dis() rPDDAT |= 0x40
83#define sm_read_en() rPBCON &= 0xFFFF0000
84#define sm_read_dis() rPBCON = (rPBCON & 0xFFFF0000) | 0x5555
85#define sm_write_en() sm_read_dis()
86#define sm_write_dis() sm_read_en()
87
88static void sm_write( uint8_t data)
89{
90  rPBDAT = (rPBDAT & 0xFF00) | data;
91  rPEDAT &= (~0x08);
92  rPEDAT |= 0x08;
93}
94
95static uint8_t sm_read()
96{
97  uint8_t data;
98 
99  rPDDAT &= (~0x100);
100  data = rPBDAT & 0xFF;
101  rPDDAT |= 0x100;
102  return data;
103}
104
105
106/* assumes chip enabled
107   bit 7: write protected = 0, write enabled = 1
108   bit 6: busy = 0, ready = 1
109   bit 0: success = 0, failed = 1
110
111   returns 1 on success, 0 on fail
112*/
113#if UNUSED
114static uint8_t sm_status()
115{
116  uint8_t status;
117
118  sm_cle_en();
119  sm_write_en();
120  sm_write(READ_STATUS_CMD);
121  sm_write_dis();
122  sm_cle_dis();
123
124  sm_read_en();
125  status = sm_read();
126  sm_read_dis();
127
128  if (status == 0xC0)
129    return 1;
130  else
131    return 0;
132}
133#endif
134
135void smc_read_id( uint8_t* buf, uint32_t length)
136{
137 
138  uint32_t i;
139
140  sm_chip_en();
141
142  sm_cle_en();
143  sm_write_en();
144  sm_write(READ_ID_CMD);
145  sm_write_dis();
146  sm_cle_dis();
147
148  sm_ale_en();
149  sm_write_en();
150  sm_write( 0);
151  sm_write_dis();
152  sm_ale_dis();
153
154  sm_read_en();
155  for (i=0;i<length;i++) *(buf+i) = sm_read();
156  sm_read_dis();
157
158  sm_chip_dis();
159}
160
161/* read an entire logical page of 512 bytes.*/
162uint8_t smc_read_page (uint32_t lpage, uint8_t* buf)
163{
164        uint32_t block, page, i;
165       
166        /* convert logical block to physical block
167           and then convert into page suitable for read1 command...
168        */
169        block = lpage >> 5;
170        if (smc_l2p[block] < LBA_UNUSED) {
171                page = smc_l2p[block] << 5;
172                page += (lpage & 0x1F);
173        }
174        else
175                return 0;
176               
177        sm_chip_en();
178       
179        sm_cle_en();
180        sm_write_en();
181        sm_write(READ1_CMD);
182        sm_write_dis();
183        sm_cle_dis();
184
185                        sm_ale_en();
186                        sm_write_en();
187                        sm_write( 0x00);
188                        sm_write( (uint8_t)(page >> 0));
189                        sm_write( (uint8_t)(page >> 8));
190                        if (smc_info.mb >= 64) sm_write( (uint8_t)(page >> 16));
191                        sm_write_dis();
192                        sm_ale_dis();
193       
194                        sm_busy();
195               
196                        sm_read_en();
197                        for (i = 0; i < 512; i++)
198                        {
199                                *buf = sm_read();
200                                buf++;
201                        }
202                        sm_read_dis();
203                        sm_chip_dis();
204                       
205                        sm_busy();
206        return 1;       
207}
208
209void smc_read_spare( uint32_t page, uint8_t* buf, uint8_t length)
210{
211  uint32_t i;
212
213
214  sm_chip_en();
215
216  sm_cle_en();
217  sm_read_dis();
218  sm_write(READ2_CMD);
219  sm_read_en();
220  sm_cle_dis();
221
222  sm_ale_en();
223  sm_read_dis();
224  sm_write( 0x00);
225  sm_write( (uint8_t)(page >> 0));
226  sm_write( (uint8_t)(page >> 8));
227  if (smc_info.mb >= 64) sm_write( (uint8_t)(page >> 16));
228  sm_read_en();
229  sm_ale_dis();
230
231  sm_busy();
232
233  sm_read_en();
234  for (i=0;i<length;i++) *(buf+i) = sm_read();
235  sm_read_dis();
236
237  sm_chip_dis();
238
239}
240
241void smc_make_l2p()
242{
243  uint32_t pblock, i, j, lblock, zone, count, cnt1, cnt2, cnt3;
244  uint8_t data[512];
245
246  cnt1 = 0;
247  cnt2 = 0;
248  cnt3 = 0;
249
250  for (i=0;i<0x2000;i++)
251  {
252    smc_l2p[i] = LBA_RESERVED;
253    smc_p2l[i] = BLOCK_RESERVED;
254  }
255  for (pblock=0;pblock<smc_info.blocks;pblock++)
256  {
257    /* read physical block - first page */
258    smc_read_spare( pblock*smc_info.pages_per_block, (uint8_t*)&data, 16);
259   
260    zone = pblock >> 10; /* divide by 1024 to get zone */
261    if ((data[5] == 0xFF) && ((data[6]&0xF8) == 0x10))
262    {
263      lblock = ((((data[6]<<8)|(data[7]<<0)) >> 1) & 0x03FF) + (zone * 1000);
264      smc_l2p[lblock] = pblock;
265      smc_p2l[pblock] = lblock;
266      cnt1++;
267    }
268    else
269    {
270      count = 0;
271      for (j=0;j<16;j++)
272      {
273        if (data[j] == 0xFF) count++;
274      }
275      if (count == 16)
276      {
277        smc_p2l[pblock] = BLOCK_UNUSED;
278        cnt2++;
279      }
280      else
281      {
282        smc_p2l[pblock] = BLOCK_RESERVED;
283        cnt3++;
284      }
285    }
286  }
287}
288
289
290void smc_detect( uint8_t id1, uint8_t id2, uint8_t id3)
291{
292  smc_info.id[0] = id1;
293  smc_info.id[1] = id2;
294  smc_info.id[2] = id3;
295  smc_info.mb    = 0;
296  smc_info.bytes_per_page  = 0;
297  smc_info.pages_per_block = 0;
298  smc_info.blocks          = 0;
299
300  switch (id1)
301  {
302    case SMC_SAMSUNG_ID:
303    case SMC_TOSHIBA_ID:
304    {
305      switch (id2)
306      {
307        case SMC_16MB  : smc_info.mb = 16; break;
308        case SMC_32MB  : smc_info.mb = 32; break;
309        case SMC_64MB  : smc_info.mb = 64; break;
310        case SMC_128MB : smc_info.mb = 128; break;
311      }
312      break;
313    }
314  }
315
316  switch (smc_info.mb)
317  {
318    case 16  : smc_info.bytes_per_page = 512; smc_info.pages_per_block = 32; smc_info.blocks = 0x0400; break;
319    case 32  : smc_info.bytes_per_page = 512; smc_info.pages_per_block = 32; smc_info.blocks = 0x0800; break;
320    case 64  : smc_info.bytes_per_page = 512; smc_info.pages_per_block = 32; smc_info.blocks = 0x1000; break;
321    case 128 : smc_info.bytes_per_page = 512; smc_info.pages_per_block = 32; smc_info.blocks = 0x2000; break;
322  }
323}
324
325void smc_init( void)
326{
327        unsigned char buf[32];
328        int i;
329       
330        /* reset smc */
331        sm_chip_en();
332        sm_cle_en();
333        sm_write_en();
334        sm_write(0xFF);
335        sm_write_dis();
336        sm_cle_dis();
337        for(i=0;i<10;i++);
338        sm_busy();
339        sm_chip_dis();
340       
341        smc_read_id (buf, 4);
342        smc_detect (buf[0], buf[1], buf[2]);
343        printk ("SMC: [%02X-%02X-%02X-%02X]\n", buf[0], buf[1], buf[2], buf[3]);
344        printk ("SMC size: %dMB detected\n",smc_info.mb);
345        smc_make_l2p();
346}       
347
348/**********
349 * Function: sm_ECCEncode (completely ripped, unaltered, from the samsung routines)
350 * Remark:
351 *      - adopted from "ECC Algorithm for SmartMedia V3.0"
352 *              by Memory Product & Technology, Samsung Electronics Co. (ecc30.pdf)
353 **********/
354int sm_ECCEncode(const uint8_t * p_buf, uint8_t * p_ecc)
355{
356        uint32_t i, j;
357        uint8_t paritr[256], tmp = 0, tmp2 = 0;
358        uint8_t data_table0[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
359        uint8_t data_table1[16] = { 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 };
360        uint8_t sum = 0, paritc = 0;
361        uint8_t parit0c = 0, parit1c = 0, parit2c = 0, parit3c = 0;
362        uint8_t parit4c = 0, parit5c = 0, parit6c = 0, parit7c = 0;
363        uint8_t parit1_1, parit1_2, parit2_1, parit2_2, parit4_1, parit4_2;
364        uint8_t parit8_1 = 0, parit8_2 = 0, parit16_1 = 0, parit16_2 = 0, parit32_1 = 0, parit32_2 = 0;
365        uint8_t parit64_1 = 0, parit64_2 = 0, parit128_1 = 0, parit128_2 = 0, parit256_1 = 0, parit256_2 = 0;
366        uint8_t parit512_1 = 0, parit512_2 = 0, parit1024_1 = 0, parit1024_2 = 0;
367        uint8_t* paritr_ptr;
368
369        paritr_ptr = paritr;
370        for (i = 0; i < 256; ++i, ++paritr_ptr, ++p_buf)
371        {
372                paritc ^= *p_buf;
373                tmp = (*p_buf & 0xf0) >> 4;
374                tmp2 = *p_buf & 0x0f;
375
376                switch (tmp)
377                {
378                        case 0:
379                        case 3:
380                        case 5:
381                        case 6:
382                        case 9:
383                        case 10:
384                        case 12:
385                        case 15:
386                                *paritr_ptr = *(data_table0 + tmp2);
387                                break;
388
389                        case 1:
390                        case 2:
391                        case 4:
392                        case 7:
393                        case 8:
394                        case 11:
395                        case 13:
396                        case 14:
397                                *paritr_ptr = *(data_table1 + tmp2);
398                                break;
399                }
400        }
401
402        parit0c = (paritc & 0x01) ? 1 : 0;
403        parit1c = (paritc & 0x02) ? 1 : 0;
404        parit2c = (paritc & 0x04) ? 1 : 0;
405        parit3c = (paritc & 0x08) ? 1 : 0;
406        parit4c = (paritc & 0x10) ? 1 : 0;
407        parit5c = (paritc & 0x20) ? 1 : 0;
408        parit6c = (paritc & 0x40) ? 1 : 0;
409        parit7c = (paritc & 0x80) ? 1 : 0;
410        parit1_2 = parit6c ^ parit4c ^ parit2c ^ parit0c;
411        parit1_1 = parit7c ^ parit5c ^ parit3c ^ parit1c;
412        parit2_2 = parit5c ^ parit4c ^ parit1c ^ parit0c;
413        parit2_1 = parit7c ^ parit6c ^ parit3c ^ parit2c;
414        parit4_2 = parit3c ^ parit2c ^ parit1c ^ parit0c;
415        parit4_1 = parit7c ^ parit6c ^ parit5c ^ parit4c;
416
417        paritr_ptr = paritr;
418        for (i = 0; i < 256; ++i, ++paritr_ptr)
419        {
420                sum ^= *paritr_ptr;
421        }
422
423        paritr_ptr = paritr;
424        for (i = 0; i < 256; i += 2, paritr_ptr += 2)
425        {
426                parit8_2 ^= *paritr_ptr;
427        }
428
429        paritr_ptr = paritr;
430        for (i = 0; i < 256; i += 4, paritr_ptr += 4)
431        {
432                parit16_2 ^= *paritr_ptr;
433                parit16_2 ^= *(paritr_ptr + 1);
434        }
435
436        paritr_ptr = paritr;
437        for (i = 0; i < 256; i += 8, paritr_ptr += 8)
438        {
439                for (j = 0; j <= 3; ++j)
440                {
441                        parit32_2 ^= *(paritr_ptr + j);
442                }
443        }
444
445        paritr_ptr = paritr;
446        for (i = 0; i < 256; i += 16, paritr_ptr += 16)
447        {
448                for (j = 0; j <= 7; ++j)
449                {
450                        parit64_2 ^= *(paritr_ptr + j);
451                }
452        }
453
454        paritr_ptr = paritr;
455        for (i = 0; i < 256; i += 32, paritr_ptr += 32)
456        {
457                for (j = 0; j <= 15; ++j)
458                {
459                        parit128_2 ^= *(paritr_ptr + j);
460                }
461        }
462
463        paritr_ptr = paritr;
464        for (i = 0; i < 256; i += 64, paritr_ptr += 64)
465        {
466                for (j = 0; j <= 31; ++j)
467                {
468                        parit256_2 ^= *(paritr_ptr + j);
469                }
470        }
471
472        paritr_ptr = paritr;
473        for (i = 0; i < 256; i += 128, paritr_ptr += 128)
474        {
475                for (j = 0; j <= 63; ++j)
476                {
477                        parit512_2 ^= *(paritr_ptr + j);
478                }
479        }
480
481        paritr_ptr = paritr;
482        for (i = 0; i < 256; i += 256, paritr_ptr += 256)
483        {
484                for (j = 0; j <= 127; ++j)
485                {
486                        parit1024_2 ^= *(paritr_ptr + j);
487                }
488        }
489
490        if (sum==0)
491        {
492                parit1024_1 = parit1024_2;
493                parit512_1 = parit512_2;
494                parit256_1 = parit256_2;
495                parit128_1 = parit128_2;
496                parit64_1 = parit64_2;
497                parit32_1 = parit32_2;
498                parit16_1 = parit16_2;
499                parit8_1 = parit8_2;
500        }
501        else
502        {
503                parit1024_1 = parit1024_2 ? 0 : 1;
504                parit512_1 = parit512_2 ? 0 : 1;
505                parit256_1 = parit256_2 ? 0 : 1;
506                parit128_1 = parit128_2 ? 0 : 1;
507                parit64_1 = parit64_2 ? 0 : 1;
508                parit32_1 = parit32_2 ? 0 : 1;
509                parit16_1 = parit16_2 ? 0 : 1;
510                parit8_1 = parit8_2 ? 0 : 1;
511        }
512
513        parit1_2 <<= 2;
514        parit1_1 <<= 3;
515        parit2_2 <<= 4;
516        parit2_1 <<= 5;
517        parit4_2 <<= 6;
518        parit4_1 <<= 7;
519        parit128_1 <<= 1;
520        parit256_2 <<= 2;
521        parit256_1 <<= 3;
522        parit512_2 <<= 4;
523        parit512_1 <<= 5;
524        parit1024_2 <<= 6;
525        parit1024_1 <<= 7;
526        parit8_1 <<= 1;
527        parit16_2 <<= 2;
528        parit16_1 <<= 3;
529        parit32_2 <<= 4;
530        parit32_1 <<= 5;
531        parit64_2 <<= 6;
532        parit64_1 <<= 7;
533
534        p_ecc[0] = ~(parit64_1 | parit64_2 | parit32_1 | parit32_2 | parit16_1 | parit16_2 | parit8_1 | parit8_2);
535        p_ecc[1] = ~(parit1024_1 |parit1024_2 | parit512_1 | parit512_2 | parit256_1 | parit256_2 | parit128_1 | parit128_2);
536        p_ecc[2] = ~(parit4_1 | parit4_2 | parit2_1 | parit2_2 | parit1_1 | parit1_2);
537
538        return 0;
539}
540
541/* smc_write --
542 * write stub
543*/
544static int smc_write(rtems_blkdev_request *req)
545{
546        req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0);
547        return 0;
548}
549
550
551/* smc_read --
552 * PARAMETERS:
553 *     req - pointer to the READ block device request info
554 *
555 * RETURNS:
556 *     ioctl return value
557 */
558static int
559smc_read(rtems_blkdev_request *req)
560{
561    uint32_t   i;
562    rtems_blkdev_sg_buffer *sg;
563    uint32_t   remains;
564
565    remains = smc_info.bytes_per_page * req->count;
566    sg = req->bufs;
567    for (i = 0; (remains > 0) && (i < req->bufnum); i++, sg++)
568    {
569        int count = sg->length;
570        if (count > remains)
571            count = remains;
572        smc_read_page(sg->block,sg->buffer);
573        remains -= count;
574    }
575    req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0);
576    return 0;
577}
578
579/* smc_ioctl --
580 *     IOCTL handler for SMC device.
581 *
582 * PARAMETERS:
583 *      dev  - device number (major, minor number)
584 *      req  - IOCTL request code
585 *      argp - IOCTL argument
586 *
587 * RETURNS:
588 *     IOCTL return value
589 */
590static int
591smc_ioctl(dev_t dev, uint32_t req, void *argp)
592{
593    switch (req)
594    {
595        case RTEMS_BLKIO_REQUEST:
596        {
597            rtems_blkdev_request *r = argp;
598            switch (r->req)
599            {
600                case RTEMS_BLKDEV_REQ_READ:
601                    return smc_read(r);
602                case RTEMS_BLKDEV_REQ_WRITE:
603                    return smc_write(r);
604                default:
605                    errno = EBADRQC;
606                    return -1;
607            }
608            break;
609        }
610
611        default:
612            errno = EBADRQC;
613            return -1;
614    }
615}
616
617/* smc_initialize --
618 *     RAM disk device driver initialization. Run through RAM disk
619 *     configuration information and configure appropriate RAM disks.
620 *
621 * PARAMETERS:
622 *     major - RAM disk major device number
623 *     minor - minor device number, not applicable
624 *     arg   - initialization argument, not applicable
625 *
626 * RETURNS:
627 *     none
628 */
629rtems_device_driver
630smc_initialize(
631    rtems_device_major_number major,
632    rtems_device_minor_number minor,
633    void *arg)
634{
635    rtems_status_code rc;
636    dev_t dev;
637    uint32_t block_num;
638
639    rc = rtems_disk_io_initialize();
640    if (rc != RTEMS_SUCCESSFUL)
641        return rc;
642
643    smc_init();
644    block_num = smc_info.blocks << 5;
645
646    dev = rtems_filesystem_make_dev_t(major, 0);
647    rc = rtems_disk_create_phys(dev, 512, block_num,
648                                    smc_ioctl, SMC_DEVICE_NAME);
649
650    return RTEMS_SUCCESSFUL;
651}
Note: See TracBrowser for help on using the repository browser.