1 #include <linux/delay.h>
2 #include <linux/timer.h>
4 #include <linux/module.h>
6 #include <linux/init.h>
8 #include <linux/blkpg.h>
9 #include <linux/hdreg.h>
10 #include <linux/major.h>
11 #include <asm/uaccess.h>
14 #define DEVICE_NAME "mmc"
15 #define DEVICE_NR(device) (MINOR(device))
16 #define DEVICE_ON(device)
17 #define DEVICE_OFF(device)
20 #include <linux/blk.h>
22 MODULE_AUTHOR("Madsuk/Rohde");
23 MODULE_DESCRIPTION("Driver MMC/SD-Cards");
24 MODULE_SUPPORTED_DEVICE("WRT54G");
25 MODULE_LICENSE("GPL");
32 /* we have only one device */
33 static int hd_sizes
[1<<6];
34 static int hd_blocksizes
[1<<6];
35 static int hd_hardsectsizes
[1<<6];
36 static int hd_maxsect
[1<<6];
37 static struct hd_struct hd
[1<<6];
39 static struct timer_list mmc_timer
;
40 static int mmc_media_detect
= 0;
41 static int mmc_media_changed
= 1;
43 typedef unsigned int uint32
;
45 static unsigned char port_state
= 0x00;
46 static volatile uint32
*gpioaddr_input
= (uint32
*)0xb8000060;
47 static volatile uint32
*gpioaddr_output
= (uint32
*)0xb8000064;
48 static volatile uint32
*gpioaddr_enable
= (uint32
*)0xb8000068;
49 static volatile uint32
*gpioaddr_control
= (uint32
*)0xb800006c;
51 static void mmc_spi_cs_low(void)
53 port_state
&= ~(SD_CS
);
54 *gpioaddr_output
= port_state
;
57 static void mmc_spi_cs_high(void)
60 *gpioaddr_output
= port_state
;
63 static unsigned char mmc_spi_io(unsigned char data_out
)
66 unsigned char result
= 0, tmp_data
= 0;
69 if(data_out
& (0x01 << (7-i
)))
74 *gpioaddr_output
= port_state
;
76 *gpioaddr_output
= port_state
;
78 tmp_data
= *gpioaddr_input
;
80 port_state
&= ~SD_CLK
;
81 *gpioaddr_output
= port_state
;
92 static int mmc_write_block(unsigned int dest_addr
, unsigned char *data
)
96 unsigned char ab0
, ab1
, ab2
, ab3
;
101 ab3
= 0xff & (address
>> 24);
102 ab2
= 0xff & (address
>> 16);
103 ab1
= 0xff & (address
>> 8);
104 ab0
= 0xff & address
;
106 for (i
= 0; i
< 4; i
++) mmc_spi_io(0xff);
108 mmc_spi_io(ab3
); /* msb */
111 mmc_spi_io(ab0
); /* lsb */
113 for (i
= 0; i
< 8; i
++)
115 r
= mmc_spi_io(0xff);
116 if (r
== 0x00) break;
126 for (i
= 0; i
< 512; i
++) mmc_spi_io(data
[i
]);
127 for (i
= 0; i
< 2; i
++) mmc_spi_io(0xff);
129 for (i
= 0; i
< 1000000; i
++)
131 r
= mmc_spi_io(0xff);
132 if (r
== 0xff) break;
145 static int mmc_read_block(unsigned char *data
, unsigned int src_addr
)
147 unsigned int address
;
149 unsigned char ab0
, ab1
, ab2
, ab3
;
154 ab3
= 0xff & (address
>> 24);
155 ab2
= 0xff & (address
>> 16);
156 ab1
= 0xff & (address
>> 8);
157 ab0
= 0xff & address
;
160 for (i
= 0; i
< 4; i
++) mmc_spi_io(0xff);
162 mmc_spi_io(ab3
); /* msb */
165 mmc_spi_io(ab0
); /* lsb */
168 for (i
= 0; i
< 8; i
++)
170 r
= mmc_spi_io(0xff);
171 if (r
== 0x00) break;
179 for (i
= 0; i
< 100000; i
++)
181 r
= mmc_spi_io(0xff);
182 if (r
== 0xfe) break;
190 for (i
= 0; i
< 512; i
++)
192 r
= mmc_spi_io(0xff);
195 for (i
= 0; i
< 2; i
++)
197 r
= mmc_spi_io(0xff);
205 static void mmc_request(request_queue_t
*q
)
207 unsigned int mmc_address
;
208 unsigned char *buffer_address
;
217 code
= 1; // Default is success
219 mmc_address
= (CURRENT
->sector
+ hd
[MINOR(CURRENT
->rq_dev
)].start_sect
) * hd_hardsectsizes
[0];
220 buffer_address
= CURRENT
->buffer
;
221 nr_sectors
= CURRENT
->current_nr_sectors
;
223 if (((CURRENT
->sector
+ CURRENT
->current_nr_sectors
+ hd
[MINOR(CURRENT
->rq_dev
)].start_sect
) > hd
[0].nr_sects
) || (mmc_media_detect
== 0))
227 else if (cmd
== READ
)
229 spin_unlock_irq(&io_request_lock
);
230 for (i
= 0; i
< nr_sectors
; i
++)
232 rc
= mmc_read_block(buffer_address
, mmc_address
);
235 printk("mmc: error in mmc_read_block (%d)\n", rc
);
241 mmc_address
+= hd_hardsectsizes
[0];
242 buffer_address
+= hd_hardsectsizes
[0];
245 spin_lock_irq(&io_request_lock
);
247 else if (cmd
== WRITE
)
249 spin_unlock_irq(&io_request_lock
);
250 for (i
= 0; i
< nr_sectors
; i
++)
252 rc
= mmc_write_block(mmc_address
, buffer_address
);
255 printk("mmc: error in mmc_write_block (%d)\n", rc
);
261 mmc_address
+= hd_hardsectsizes
[0];
262 buffer_address
+= hd_hardsectsizes
[0];
265 spin_lock_irq(&io_request_lock
);
276 static int mmc_open(struct inode
*inode
, struct file
*filp
)
281 if (mmc_media_detect
== 0) return -ENODEV
;
289 static int mmc_release(struct inode
*inode
, struct file
*filp
)
292 fsync_dev(inode
->i_rdev
);
293 invalidate_buffers(inode
->i_rdev
);
301 extern struct gendisk hd_gendisk
;
302 static int mmc_revalidate(kdev_t dev
)
304 int target
, max_p
, start
, i
;
305 if (mmc_media_detect
== 0) return -ENODEV
;
307 target
= DEVICE_NR(dev
);
309 max_p
= hd_gendisk
.max_p
;
311 for (i
= max_p
- 1; i
>= 0; i
--) {
312 int minor
= start
+ i
;
313 invalidate_device(MKDEV(MAJOR_NR
, minor
), 1);
314 hd_gendisk
.part
[minor
].start_sect
= 0;
315 hd_gendisk
.part
[minor
].nr_sects
= 0;
318 grok_partitions(&hd_gendisk
, target
, 1 << 6,
324 static int mmc_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
, unsigned long arg
)
326 if (!inode
|| !inode
->i_rdev
)
331 return put_user(hd
[MINOR(inode
->i_rdev
)].nr_sects
, (unsigned long *)arg
);
333 return put_user((u64
)hd
[MINOR(inode
->i_rdev
)].
334 nr_sects
, (u64
*) arg
);
336 if (!capable(CAP_SYS_ADMIN
))
339 return mmc_revalidate(inode
->i_rdev
);
342 struct hd_geometry
*loc
, g
;
343 loc
= (struct hd_geometry
*) arg
;
348 g
.cylinders
= hd
[0].nr_sects
/ (4 * 16);
349 g
.start
= hd
[MINOR(inode
->i_rdev
)].start_sect
;
350 return copy_to_user(loc
, &g
, sizeof(g
)) ? -EFAULT
: 0;
353 return blk_ioctl(inode
->i_rdev
, cmd
, arg
);
357 static int mmc_card_init(void)
366 printk("mmc Card init\n");
368 for (i
= 0; i
< 20; i
++) mmc_spi_io(0xff);
373 for (i
= 0; i
< 4; i
++) mmc_spi_io(0x00);
375 for (i
= 0; i
< 8; i
++)
377 r
= mmc_spi_io(0xff);
378 if (r
== 0x01) break;
384 restore_flags(flags
);
388 printk("mmc Card init *1*\n");
389 for (j
= 0; j
< 10000; j
++)
394 for (i
= 0; i
< 4; i
++) mmc_spi_io(0x00);
396 for (i
= 0; i
< 8; i
++)
398 r
= mmc_spi_io(0xff);
399 if (r
== 0x00) break;
405 restore_flags(flags
);
406 printk("mmc Card init *2*\n");
410 restore_flags(flags
);
415 static int mmc_card_config(void)
419 unsigned char csd
[32];
421 unsigned int c_size_mult
;
423 unsigned int read_bl_len
;
424 unsigned int blocknr
= 0;
425 unsigned int block_len
= 0;
426 unsigned int size
= 0;
429 for (i
= 0; i
< 4; i
++) mmc_spi_io(0xff);
431 for (i
= 0; i
< 4; i
++) mmc_spi_io(0x00);
433 for (i
= 0; i
< 8; i
++)
435 r
= mmc_spi_io(0xff);
436 if (r
== 0x00) break;
444 for (i
= 0; i
< 8; i
++)
446 r
= mmc_spi_io(0xff);
447 if (r
== 0xfe) break;
455 for (i
= 0; i
< 16; i
++)
457 r
= mmc_spi_io(0xff);
460 for (i
= 0; i
< 2; i
++)
462 r
= mmc_spi_io(0xff);
466 if (r
== 0x00) return(3);
468 c_size
= csd
[8] + csd
[7] * 256 + (csd
[6] & 0x03) * 256 * 256;
470 c_size_mult
= csd
[10] + (csd
[9] & 0x03) * 256;
472 read_bl_len
= csd
[5] & 0x0f;
474 mult
<<= c_size_mult
+ 2;
475 blocknr
= (c_size
+ 1) * mult
;
477 block_len
<<= read_bl_len
;
478 size
= block_len
* blocknr
;
481 for(i
=0; i
<(1<<6); i
++) {
482 hd_blocksizes
[i
] = 1024;
483 hd_hardsectsizes
[i
] = block_len
;
487 hd
[0].nr_sects
= blocknr
;
490 printk("Size = %d, hardsectsize = %d, sectors = %d\n",
491 size
, block_len
, blocknr
);
496 static int mmc_hardware_init(void)
498 unsigned char gpio_outen
;
500 // Set inputs/outputs here
501 printk("mmc Hardware init\n");
502 gpio_outen
= *gpioaddr_enable
;
504 gpio_outen
= (gpio_outen
| SD_DI
| SD_CLK
| SD_CS
) & ~SD_DO
;
505 *gpioaddr_enable
= gpio_outen
;
507 port_state
= *gpioaddr_input
;
510 port_state
&= ~(SD_CLK
| SD_DI
| SD_CS
);
511 *gpioaddr_output
= port_state
;
516 static int mmc_check_media_change(kdev_t dev
)
519 if (mmc_media_changed
== 1)
521 mmc_media_changed
= 0;
527 static struct block_device_operations mmc_bdops
=
530 release
: mmc_release
,
533 check_media_change
: mmc_check_media_change
,
534 revalidate
: mmc_revalidate
,
538 static struct gendisk hd_gendisk
= {
540 major_name
: DEVICE_NAME
,
548 static int mmc_init(void)
552 rc
= mmc_hardware_init();
556 printk("mmc: error in mmc_hardware_init (%d)\n", rc
);
560 rc
= mmc_card_init();
563 // Give it an extra shot
564 rc
= mmc_card_init();
567 printk("mmc: error in mmc_card_init (%d)\n", rc
);
572 memset(hd_sizes
, 0, sizeof(hd_sizes
));
573 rc
= mmc_card_config();
576 printk("mmc: error in mmc_card_config (%d)\n", rc
);
581 blk_size
[MAJOR_NR
] = hd_sizes
;
583 memset(hd
, 0, sizeof(hd
));
584 hd
[0].nr_sects
= hd_sizes
[0]*2;
586 blksize_size
[MAJOR_NR
] = hd_blocksizes
;
587 hardsect_size
[MAJOR_NR
] = hd_hardsectsizes
;
588 max_sectors
[MAJOR_NR
] = hd_maxsect
;
590 hd_gendisk
.nr_real
= 1;
592 register_disk(&hd_gendisk
, MKDEV(MAJOR_NR
,0), 1<<6,
593 &mmc_bdops
, hd_sizes
[0]*2);
598 static void mmc_exit(void)
600 blk_size
[MAJOR_NR
] = NULL
;
601 blksize_size
[MAJOR_NR
] = NULL
;
602 hardsect_size
[MAJOR_NR
] = NULL
;
603 max_sectors
[MAJOR_NR
] = NULL
;
607 static void mmc_check_media(void)
612 old_state
= mmc_media_detect
;
614 // TODO: Add card detection here
615 mmc_media_detect
= 1;
616 if (old_state
!= mmc_media_detect
)
618 mmc_media_changed
= 1;
619 if (mmc_media_detect
== 1)
622 if (rc
!= 0) printk("mmc: error in mmc_init (%d)\n", rc
);
630 /* del_timer(&mmc_timer);
631 mmc_timer.expires = jiffies + 10*HZ;
632 add_timer(&mmc_timer); */
635 static int __init
mmc_driver_init(void)
639 rc
= devfs_register_blkdev(MAJOR_NR
, DEVICE_NAME
, &mmc_bdops
);
642 printk(KERN_WARNING
"mmc: can't get major %d\n", MAJOR_NR
);
646 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
), mmc_request
);
648 read_ahead
[MAJOR_NR
] = 8;
649 add_gendisk(&hd_gendisk
);
653 /*init_timer(&mmc_timer);
654 mmc_timer.expires = jiffies + HZ;
655 mmc_timer.function = (void *)mmc_check_media;
656 add_timer(&mmc_timer);*/
661 static void __exit
mmc_driver_exit(void)
664 del_timer(&mmc_timer
);
666 for (i
= 0; i
< (1 << 6); i
++)
667 fsync_dev(MKDEV(MAJOR_NR
, i
));
669 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
));
670 del_gendisk(&hd_gendisk
);
671 devfs_unregister_blkdev(MAJOR_NR
, DEVICE_NAME
);
675 module_init(mmc_driver_init
);
676 module_exit(mmc_driver_exit
);