6cea1a51f0093fad43fcc3ef85c6f2c50f7737dc
[openwrt.git] / target / linux / adm5120 / files / drivers / block / rb1xx / bdev.c
1 /* CF-mips driver
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.
5    
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>
8
9    This work is redistributed under the terms of the GNU General Public License.
10 */
11
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>
17 #include <linux/fs.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>
23
24 #include <asm/uaccess.h>
25 #include <asm/io.h>
26 #include <asm/mach-adm5120/adm5120_cf.h>
27
28 #ifdef DEBUG
29 #define DEBUGP printk
30 #define DLEVEL 1
31 #else
32 #define DEBUGP(format, args...)
33 #define DLEVEL 0
34 #endif
35
36 #define CF_MIPS_MAJOR 13
37 #define MAJOR_NR        CF_MIPS_MAJOR
38 #define CF_MAX_PART     16              /* max 15 partitions */
39
40 #include "ata.h"
41
42 //extern struct block_device_operations cf_bdops;
43
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];
49
50 // static spinlock_t lock = SPIN_LOCK_UNLOCKED;
51
52 // volatile int cf_busy = 0;
53
54 static struct request *active_req = NULL;
55
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);
59
60 static void cf_request(request_queue_t * q);
61 static int cf_transfer(const struct request *req);
62
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 *);
68
69 static struct block_device_operations cf_bdops = {
70       .owner = THIS_MODULE,
71       .open = cf_open,
72       .release = cf_release,
73       .ioctl = cf_ioctl,
74       .media_changed = NULL,
75       .unlocked_ioctl = NULL,
76       .revalidate_disk = NULL,
77       .compat_ioctl = NULL,
78       .direct_access = NULL
79 };
80
81
82 int cf_mips_probe(struct platform_device *pdev)
83 {
84         struct gendisk* cf_gendisk=NULL;
85         struct cf_device *cdev = (struct cf_device *) pdev->dev.platform_data;
86         struct cf_mips_dev *dev;
87         struct resource *r;
88         int reg_result;
89
90         reg_result = register_blkdev(MAJOR_NR, "cf-mips");
91         if (reg_result < 0) {
92                 printk(KERN_WARNING "cf-mips: can't get major %d\n", MAJOR_NR);
93                 return reg_result;
94         }
95
96         dev = (struct cf_mips_dev *)kzalloc(sizeof(struct cf_mips_dev),GFP_KERNEL);
97         if (!dev)
98                 goto out_err;
99
100         cdev->dev = dev;
101         
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;
106         
107         if (cf_init(dev))
108                 goto out_err;
109         printk(KERN_INFO "cf-mips: init done\n");
110         
111         spin_lock_init(&dev->lock);
112         dev->queue = blk_init_queue(cf_request,&dev->lock);
113         if (!dev->queue){
114                 printk(KERN_ERR "cf-mips: no mem for queue\n");
115                 goto out_err;
116         }
117         blk_queue_max_sectors(dev->queue,ATA_MAX_SECT_PER_CMD);
118
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);
124         }*/
125
126         dev->gd = alloc_disk(CF_MAX_PART);
127         cf_gendisk = dev->gd;
128         cdev->gd = dev->gd;
129
130         if (!cf_gendisk)
131                 goto out_err; /* Last of these goto's */
132         
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;
141         
142         set_capacity(cf_gendisk,dev->sectors * CF_KERNEL_MUL);
143         
144         /* Let the disk go live */
145         add_disk(cf_gendisk);
146 #if 0
147         result = cf_init();
148         
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;
156         }
157
158         /* setup info for whole disk (partition 0) */
159         cf_part_sizes[0] = cf_sectors / 2;
160         cf_parts[0].nr_sects = cf_sectors;
161
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) */
167
168         blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
169
170         add_gendisk(&cf_gendisk);
171 #endif
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);
175         return 0;
176
177 out_err:
178         if (dev->queue){
179                 blk_cleanup_queue(dev->queue);
180         }
181         if (reg_result) {
182                 unregister_blkdev(MAJOR_NR, "cf-mips");
183                 return reg_result;
184         }
185         if (dev){
186                 cf_cleanup(dev);
187                 kfree(dev);
188         }
189         return 1;
190 }
191
192 static int
193 cf_mips_remove(struct platform_device *pdev)
194 {
195         struct cf_device *cdev = (struct cf_device *) pdev->dev.platform_data;
196         struct cf_mips_dev *dev = (struct cf_mips_dev *) cdev->dev;
197         
198         unregister_blkdev(MAJOR_NR, "cf-mips");
199         blk_cleanup_queue(dev->queue);
200
201         del_gendisk(dev->gd);
202         cf_cleanup(dev);
203         return 0;
204 }
205
206
207 static struct platform_driver cf_driver = {
208         .driver.name = "rb153-cf",
209         .probe = cf_mips_probe,
210         .remove = cf_mips_remove,
211 };
212
213 static int __init cf_mips_init(void)
214 {
215         printk(KERN_INFO "cf-mips module loaded\n");
216         return platform_driver_register(&cf_driver);
217 }
218
219 static void cf_mips_cleanup(void)
220 {
221         platform_driver_unregister(&cf_driver);
222         printk(KERN_INFO "cf-mips module removed\n");
223 }
224
225 module_init(cf_mips_init);
226 module_exit(cf_mips_cleanup);
227
228 MODULE_LICENSE("GPL");
229 MODULE_ALIAS_BLOCKDEV_MAJOR(CF_MIPS_MAJOR);
230
231
232 static int cf_open(struct inode *inode, struct file *filp)
233 {
234         struct cf_mips_dev  *dev=inode->i_bdev->bd_disk->private_data;
235         int minor = MINOR(inode->i_rdev);
236         
237         if (minor >= CF_MAX_PART)
238                 return -ENODEV;
239         //DEBUGP(KERN_INFO "cf-mips module opened, minor %d\n", minor);
240         spin_lock(&dev->lock);
241         dev->users++;
242         spin_unlock(&dev->lock);
243         filp->private_data=dev;
244         
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 */
249 }
250
251 static int cf_release(struct inode *inode, struct file *filp)
252 {
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);
256         dev->users--;
257         spin_unlock(&dev->lock);
258         return 0;
259 }
260
261 static int cf_ioctl(struct inode *inode, struct file *filp,
262          unsigned int cmd, unsigned long arg)
263 {
264         unsigned minor = MINOR(inode->i_rdev);
265         struct cf_mips_dev  *dev=inode->i_bdev->bd_disk->private_data;
266
267         DEBUGP(KERN_INFO "cf_ioctl cmd %u\n", cmd);
268         switch (cmd) {
269         case BLKRRPART: /* re-read partition table */
270                 if (!capable(CAP_SYS_ADMIN))
271                         return -EACCES;
272                 printk(KERN_INFO "cf-mips partition check: \n");
273                 register_disk(dev->gd);
274                 return 0;
275
276         case HDIO_GETGEO:
277                 {
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)))
284                                 return -EFAULT;
285                 }
286                 return 0;
287         }
288
289         return -EINVAL;         /* unknown command */
290 }
291
292 static void cf_request(request_queue_t * q)
293 {
294         struct cf_mips_dev* dev;
295         
296         struct request * req;
297         int status;
298
299         /* We could have q->queuedata = dev , but haven't yet. */
300         if (active_req)
301                 return;
302
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){
307                         active_req=req;
308                         return;
309                 }
310                 end_request(req,status);
311         }
312 }
313
314 static int cf_transfer(const struct request *req)
315 {
316         struct cf_mips_dev* dev=req->rq_disk->private_data;
317
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;
322         }
323         
324         return cf_do_transfer(dev,req->sector,req->current_nr_sectors,req->buffer,rq_data_dir(req));
325 }
326
327 void cf_async_trans_done(struct cf_mips_dev * dev,int result)
328 {
329         struct request *req;
330         
331         spin_lock(&dev->lock);
332         req=active_req;
333         active_req=NULL;
334         end_request(req,result);
335         spin_unlock(&dev->lock);
336
337         spin_lock(&dev->lock);
338         cf_request(dev->queue);
339         spin_unlock(&dev->lock);
340 }
341