2 This is a block driver for the direct (mmaped) interface to the CF-slot,
3 found in Routerboard.com's RB532 board
4 See SDK provided from routerboard.com.
6 Module adapted By P.Christeas <p_christeas@yahoo.com>, 2005-6.
7 Cleaned up and adapted to platform_device by Felix Fietkau <nbd@openwrt.org>
9 This work is redistributed under the terms of the GNU General Public License.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/time.h>
16 #include <linux/wait.h>
18 #include <linux/genhd.h>
19 #include <linux/blkdev.h>
20 #include <linux/blkpg.h>
21 #include <linux/hdreg.h>
22 #include <linux/platform_device.h>
24 #include <asm/uaccess.h>
26 #include <asm/mach-adm5120/adm5120_cf.h>
32 #define DEBUGP(format, args...)
36 #define CF_MIPS_MAJOR 13
37 #define MAJOR_NR CF_MIPS_MAJOR
38 #define CF_MAX_PART 16 /* max 15 partitions */
42 //extern struct block_device_operations cf_bdops;
44 // static struct hd_struct cf_parts[CF_MAX_PART];
45 // static int cf_part_sizes[CF_MAX_PART];
46 // static int cf_hsect_sizes[CF_MAX_PART];
47 // static int cf_max_sectors[CF_MAX_PART];
48 // static int cf_blksize_sizes[CF_MAX_PART];
50 // static spinlock_t lock = SPIN_LOCK_UNLOCKED;
52 // volatile int cf_busy = 0;
54 static struct request *active_req = NULL;
56 static int cf_open (struct inode *, struct file *);
57 static int cf_release (struct inode *, struct file *);
58 static int cf_ioctl (struct inode *, struct file *, unsigned, unsigned long);
60 static void cf_request(request_queue_t * q);
61 static int cf_transfer(const struct request *req);
63 /*long (*unlocked_ioctl) (struct file *, unsigned, unsigned long);
64 long (*compat_ioctl) (struct file *, unsigned, unsigned long);*/
65 // int (*direct_access) (struct block_device *, sector_t, unsigned long *);
66 // int (*media_changed) (struct gendisk *);
67 // int (*revalidate_disk) (struct gendisk *);
69 static struct block_device_operations cf_bdops = {
72 .release = cf_release,
74 .media_changed = NULL,
75 .unlocked_ioctl = NULL,
76 .revalidate_disk = NULL,
82 int cf_mips_probe(struct platform_device *pdev)
84 struct gendisk* cf_gendisk=NULL;
85 struct cf_device *cdev = (struct cf_device *) pdev->dev.platform_data;
86 struct cf_mips_dev *dev;
90 reg_result = register_blkdev(MAJOR_NR, "cf-mips");
92 printk(KERN_WARNING "cf-mips: can't get major %d\n", MAJOR_NR);
96 dev = (struct cf_mips_dev *)kzalloc(sizeof(struct cf_mips_dev),GFP_KERNEL);
102 dev->pin = cdev->gpio_pin;
103 dev->irq = platform_get_irq_byname(pdev, "cf_irq");
104 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cf_membase");
105 dev->base = (void *) r->start;
109 printk(KERN_INFO "cf-mips: init done");
111 spin_lock_init(&dev->lock);
112 dev->queue = blk_init_queue(cf_request,&dev->lock);
114 printk(KERN_ERR "cf-mips: no mem for queue\n");
117 blk_queue_max_sectors(dev->queue,ATA_MAX_SECT_PER_CMD);
119 /* For memory devices, it is always better to avoid crossing segments
120 inside the same request. */
121 /* if (dev->dtype==0x848A){
122 printk(KERN_INFO "Setting boundary for cf to 0x%x",(dev->block_size*512)-1);
123 blk_queue_segment_boundary(dev->queue, (dev->block_size*512)-1);
126 dev->gd = alloc_disk(CF_MAX_PART);
127 cf_gendisk = dev->gd;
131 goto out_err; /* Last of these goto's */
133 cf_gendisk->major = MAJOR_NR;
134 cf_gendisk->first_minor = 0;
135 cf_gendisk->queue=dev->queue;
136 BUG_ON(cf_gendisk->minors != CF_MAX_PART);
137 strcpy(cf_gendisk->disk_name,"cfa");
138 cf_gendisk->fops = &cf_bdops;
139 cf_gendisk->flags = 0 ; /* is not yet GENHD_FL_REMOVABLE */
140 cf_gendisk->private_data=dev;
142 set_capacity(cf_gendisk,dev->sectors * CF_KERNEL_MUL);
144 /* Let the disk go live */
145 add_disk(cf_gendisk);
149 /* default cfg for all partitions */
150 memset(cf_parts, 0, sizeof (cf_parts[0]) * CF_MAX_PART);
151 memset(cf_part_sizes, 0, sizeof (cf_part_sizes[0]) * CF_MAX_PART);
152 for (i = 0; i < CF_MAX_PART; ++i) {
153 cf_hsect_sizes[i] = CF_SECT_SIZE;
154 cf_max_sectors[i] = ATA_MAX_SECT_PER_CMD;
155 cf_blksize_sizes[i] = BLOCK_SIZE;
158 /* setup info for whole disk (partition 0) */
159 cf_part_sizes[0] = cf_sectors / 2;
160 cf_parts[0].nr_sects = cf_sectors;
162 blk_size[MAJOR_NR] = cf_part_sizes;
163 blksize_size[MAJOR_NR] = cf_blksize_sizes;
164 max_sectors[MAJOR_NR] = cf_max_sectors;
165 hardsect_size[MAJOR_NR] = cf_hsect_sizes;
166 read_ahead[MAJOR_NR] = 8; /* (4kB) */
168 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
170 add_gendisk(&cf_gendisk);
172 // printk(KERN_INFO "cf-mips partition check: \n");
173 // register_disk(cf_gendisk, MKDEV(MAJOR_NR, 0), CF_MAX_PART,
174 // &cf_bdops, dev->sectors);
179 blk_cleanup_queue(dev->queue);
182 unregister_blkdev(MAJOR_NR, "cf-mips");
193 cf_mips_remove(struct platform_device *pdev)
195 struct cf_device *cdev = (struct cf_device *) pdev->dev.platform_data;
196 struct cf_mips_dev *dev = (struct cf_mips_dev *) cdev->dev;
198 unregister_blkdev(MAJOR_NR, "cf-mips");
199 blk_cleanup_queue(dev->queue);
201 del_gendisk(dev->gd);
207 static struct platform_driver cf_driver = {
208 .driver.name = "rb153-cf",
209 .probe = cf_mips_probe,
210 .remove = cf_mips_remove,
213 static int __init cf_mips_init(void)
215 printk(KERN_INFO "cf-mips module loaded\n");
216 return platform_driver_register(&cf_driver);
219 static void cf_mips_cleanup(void)
221 platform_driver_unregister(&cf_driver);
222 printk(KERN_INFO "cf-mips module removed\n");
225 module_init(cf_mips_init);
226 module_exit(cf_mips_cleanup);
228 MODULE_LICENSE("GPL");
229 MODULE_ALIAS_BLOCKDEV_MAJOR(CF_MIPS_MAJOR);
232 static int cf_open(struct inode *inode, struct file *filp)
234 struct cf_mips_dev *dev=inode->i_bdev->bd_disk->private_data;
235 int minor = MINOR(inode->i_rdev);
237 if (minor >= CF_MAX_PART)
239 //DEBUGP(KERN_INFO "cf-mips module opened, minor %d\n", minor);
240 spin_lock(&dev->lock);
242 spin_unlock(&dev->lock);
243 filp->private_data=dev;
245 /* dirty workaround to set CFRDY GPIO as an input when some other
246 program sets it as an output */
247 //gpio_set(CFG, (1 << dev->pin), 0);
248 return 0; /* success */
251 static int cf_release(struct inode *inode, struct file *filp)
253 int minor = MINOR(inode->i_rdev);
254 struct cf_mips_dev *dev=inode->i_bdev->bd_disk->private_data;
255 spin_lock(&dev->lock);
257 spin_unlock(&dev->lock);
261 static int cf_ioctl(struct inode *inode, struct file *filp,
262 unsigned int cmd, unsigned long arg)
264 unsigned minor = MINOR(inode->i_rdev);
265 struct cf_mips_dev *dev=inode->i_bdev->bd_disk->private_data;
267 DEBUGP(KERN_INFO "cf_ioctl cmd %u\n", cmd);
269 case BLKRRPART: /* re-read partition table */
270 if (!capable(CAP_SYS_ADMIN))
272 printk(KERN_INFO "cf-mips partition check: \n");
273 register_disk(dev->gd);
278 struct hd_geometry geo;
279 geo.cylinders = dev->cyl;
280 geo.heads = dev->head;
281 geo.sectors = dev->spt;
282 geo.start = (*dev->gd->part)[minor].start_sect;
283 if (copy_to_user((void *) arg, &geo, sizeof (geo)))
289 return -EINVAL; /* unknown command */
292 static void cf_request(request_queue_t * q)
294 struct cf_mips_dev* dev;
296 struct request * req;
299 /* We could have q->queuedata = dev , but haven't yet. */
303 while ((req=elv_next_request(q))!=NULL){
304 dev=req->rq_disk->private_data;
305 status=cf_transfer(req);
306 if (status==CF_TRANS_IN_PROGRESS){
310 end_request(req,status);
314 static int cf_transfer(const struct request *req)
316 struct cf_mips_dev* dev=req->rq_disk->private_data;
318 if (!blk_fs_request(req)){
319 if (printk_ratelimit())
320 printk(KERN_WARNING "cf-mips: skipping non-fs request 0x%x\n",req->cmd[0]);
321 return CF_TRANS_FAILED;
324 return cf_do_transfer(dev,req->sector,req->current_nr_sectors,req->buffer,rq_data_dir(req));
327 void cf_async_trans_done(struct cf_mips_dev * dev,int result)
331 spin_lock(&dev->lock);
334 end_request(req,result);
335 spin_unlock(&dev->lock);
337 spin_lock(&dev->lock);
338 cf_request(dev->queue);
339 spin_unlock(&dev->lock);