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> /* printk() */
13 #include <linux/module.h> /* module to be loadable */
14 #include <linux/delay.h>
15 #include <linux/sched.h>
16 #include <linux/pci.h>
17 #include <linux/ioport.h> /* request_mem_region() */
20 #include <asm/unaligned.h> /* ioremap() */
21 #include <asm/io.h> /* ioremap() */
22 #include <asm/rc32434/rb.h>
26 #define REQUEST_MEM_REGION 0
32 #define DEBUGP(format, args...)
35 #define SECS 1000000 /* unit for wait_not_busy() is 1us */
40 unsigned cf_sectors
= 0;
41 static unsigned cf_block_size
= 1;
42 static void *baddr
= 0;
44 #define DBUF32 ((volatile u32 *)((unsigned long)dev->baddr | ATA_DBUF_OFFSET))
47 static void cf_do_tasklet(unsigned long dev_l
);
50 static inline void wareg(u8 val
, unsigned reg
, struct cf_mips_dev
* dev
)
52 writeb(val
, dev
->baddr
+ ATA_REG_OFFSET
+ reg
);
55 static inline u8
rareg(unsigned reg
, struct cf_mips_dev
* dev
)
57 return readb(dev
->baddr
+ ATA_REG_OFFSET
+ reg
);
60 static inline int cfrdy(struct cf_mips_dev
*dev
)
62 return gpio_get_value(dev
->pin
);
65 static inline void prepare_cf_irq(struct cf_mips_dev
*dev
)
67 rb500_gpio_set_int_level(1, dev
->pin
); /* interrupt on cf ready (not busy) */
68 rb500_gpio_set_int_status(0, dev
->pin
); /* clear interrupt status */
71 static inline int cf_present(struct cf_mips_dev
* dev
)
73 /* TODO: read and configure CIS into memory mapped mode
74 * TODO: parse CISTPL_CONFIG on CF+ cards to get base address (0x200)
75 * TODO: maybe adjust power saving setting for Hitachi Microdrive
79 /* setup CFRDY GPIO as input */
80 rb500_gpio_set_func(dev
->pin
, 0);
81 gpio_direction_input(dev
->pin
);
83 for (i
= 0; i
< 0x10; ++i
) {
84 if (rareg(i
,dev
) != 0xff)
90 static inline int is_busy(struct cf_mips_dev
*dev
)
95 static int wait_not_busy(int to_us
, int wait_for_busy
,struct cf_mips_dev
*dev
)
98 if (wait_for_busy
&& !is_busy(dev
)) {
99 /* busy must appear within 400ns,
100 * but it may dissapear before we see it
101 * => must not wait for busy in a loop
108 udelay(1); /* never reached in async mode */
110 if (us_passed
> 1 * SECS
) {
111 printk(KERN_WARNING
"cf-mips: not busy ok (after %dus)"
112 ", status 0x%02x\n", us_passed
, (unsigned) rareg(ATA_REG_ST
,dev
));
116 if (us_passed
== 1 * SECS
) {
117 printk(KERN_WARNING
"cf-mips: wait not busy %dus..\n", to_us
);
119 if (dev
->async_mode
) {
120 dev
->to_timer
.expires
= jiffies
+ (to_us
* HZ
/ SECS
);
121 dev
->irq_enable_time
= jiffies
;
124 add_timer(&dev
->to_timer
);
125 enable_irq(dev
->irq
);
126 return CF_TRANS_IN_PROGRESS
;
131 } while (us_passed
< to_us
);
133 printk(KERN_ERR
"cf-mips: wait not busy timeout (%dus)"
134 ", status 0x%02x, state %d\n",
135 to_us
, (unsigned) rareg(ATA_REG_ST
,dev
), dev
->tstate
);
136 return CF_TRANS_FAILED
;
139 static irqreturn_t
cf_irq_handler(int irq
, void *dev_id
)
141 /* While tasklet has not disabled irq, irq will be retried all the time
142 * because of ILEVEL matching GPIO pin status => deadlock.
143 * To avoid this, we change ILEVEL to 0.
145 struct cf_mips_dev
*dev
=dev_id
;
147 rb500_gpio_set_int_level(0, dev
->pin
);
148 rb500_gpio_set_int_status(0, dev
->pin
);
150 del_timer(&dev
->to_timer
);
151 tasklet_schedule(&dev
->tasklet
);
155 static int do_reset(struct cf_mips_dev
*dev
)
157 printk(KERN_INFO
"cf-mips: resetting..\n");
159 wareg(ATA_REG_DC_SRST
, ATA_REG_DC
,dev
);
160 udelay(1); /* FIXME: how long should we wait here? */
161 wareg(0, ATA_REG_DC
,dev
);
163 return wait_not_busy(30 * SECS
, 1,dev
);
166 static int set_multiple(struct cf_mips_dev
*dev
)
168 if (dev
->block_size
<= 1)
171 wareg(dev
->block_size
, ATA_REG_SC
,dev
);
172 wareg(ATA_REG_DH_BASE
| ATA_REG_DH_LBA
, ATA_REG_DH
,dev
);
173 wareg(ATA_CMD_SET_MULTIPLE
, ATA_REG_CMD
,dev
);
175 return wait_not_busy(10 * SECS
, 1,dev
);
178 static int set_cmd(struct cf_mips_dev
*dev
)
180 //DEBUGP(KERN_INFO "cf-mips: ata cmd 0x%02x\n", dev->tcmd);
181 // sector_count should be <=24 bits..
182 BUG_ON(dev
->tsect_start
>=0x10000000);
183 // This way, it addresses 2^24 * 512 = 128G
185 if (dev
->tsector_count
) {
186 wareg(dev
->tsector_count
& 0xff, ATA_REG_SC
,dev
);
187 wareg(dev
->tsect_start
& 0xff, ATA_REG_SN
,dev
);
188 wareg((dev
->tsect_start
>> 8) & 0xff, ATA_REG_CL
,dev
);
189 wareg((dev
->tsect_start
>> 16) & 0xff, ATA_REG_CH
,dev
);
191 wareg(((dev
->tsect_start
>> 24) & 0x0f) | ATA_REG_DH_BASE
| ATA_REG_DH_LBA
,
192 ATA_REG_DH
,dev
); /* select drive on all commands */
193 wareg(dev
->tcmd
, ATA_REG_CMD
,dev
);
194 return wait_not_busy(10 * SECS
, 1,dev
);
197 static int do_trans(struct cf_mips_dev
*dev
)
203 //printk("do_trans: %d sectors left\n",dev->tsectors_left);
204 while (dev
->tsectors_left
) {
207 st
= rareg(ATA_REG_ST
,dev
);
208 if (!(st
& ATA_REG_ST_DRQ
)) {
209 printk(KERN_ERR
"cf-mips: do_trans without DRQ (status 0x%x)!\n", st
);
210 if (st
& ATA_REG_ST_ERR
) {
211 int errId
= rareg(ATA_REG_ERR
,dev
);
212 printk(KERN_ERR
"cf-mips: %s error, status 0x%x, errid 0x%x\n",
213 (dev
->tread
? "read" : "write"), st
, errId
);
215 return CF_TRANS_FAILED
;
217 do { /* Fill/read the buffer one block */
219 qbuf
= (u32
*)dev
->tbuf
;
220 qend
= qbuf
+ CF_SECT_SIZE
/ sizeof(u32
);
223 put_unaligned(*DBUF32
,qbuf
++);
224 //*(qbuf++) = *DBUF32;
228 *DBUF32
= get_unaligned(qbuf
++);
231 dev
->tsectors_left
--;
232 dev
->tbuf
+= CF_SECT_SIZE
;
233 dev
->tbuf_size
-= CF_SECT_SIZE
;
235 } while (transfered
!= dev
->block_size
&& dev
->tsectors_left
> 0);
237 res
= wait_not_busy(10 * SECS
, 1,dev
);
238 if (res
!= CF_TRANS_OK
)
242 st
= rareg(ATA_REG_ST
,dev
);
243 if (st
& (ATA_REG_ST_DRQ
| ATA_REG_ST_DWF
| ATA_REG_ST_ERR
)) {
244 if (st
& ATA_REG_ST_DRQ
) {
245 printk(KERN_ERR
"cf-mips: DRQ after all %d sectors are %s"
246 ", status 0x%x\n", dev
->tsector_count
, (dev
->tread
? "read" : "written"), st
);
247 } else if (st
& ATA_REG_ST_DWF
) {
248 printk(KERN_ERR
"cf-mips: write fault, status 0x%x\n", st
);
250 int errId
= rareg(ATA_REG_ERR
,dev
);
251 printk(KERN_ERR
"cf-mips: %s error, status 0x%x, errid 0x%x\n",
252 (dev
->tread
? "read" : "write"), st
, errId
);
254 return CF_TRANS_FAILED
;
259 static int cf_do_state(struct cf_mips_dev
*dev
)
262 switch (dev
->tstate
) { /* fall through everywhere */
264 dev
->tstate
= TS_READY
;
266 dev
->tstate
= TS_AFTER_RESET
;
268 if (res
!= CF_TRANS_OK
)
272 if (dev
->tstate
== TS_AFTER_RESET
) {
273 dev
->tstate
= TS_READY
;
274 res
= set_multiple(dev
);
275 if (res
!= CF_TRANS_OK
)
279 dev
->tstate
= TS_CMD
;
281 if (res
!= CF_TRANS_OK
)
284 dev
->tstate
= TS_TRANS
;
289 printk(KERN_ERR
"cf-mips: BUG: unknown tstate %d\n", dev
->tstate
);
290 return CF_TRANS_FAILED
;
292 if (res
!= CF_TRANS_IN_PROGRESS
)
293 dev
->tstate
= TS_IDLE
;
297 static void cf_do_tasklet(unsigned long dev_l
)
299 struct cf_mips_dev
* dev
=(struct cf_mips_dev
*) dev_l
;
302 disable_irq(dev
->irq
);
304 if (dev
->tstate
== TS_IDLE
)
305 return; /* can happen when irq is first registered */
308 DEBUGP(KERN_WARNING
"cf-mips: not busy ok (tasklet) status 0x%02x\n",
309 (unsigned) rareg(ATA_REG_ST
,dev
));
312 res
= cf_do_state(dev
);
313 if (res
== CF_TRANS_IN_PROGRESS
)
315 cf_async_trans_done(dev
,res
);
318 static void cf_async_timeout(unsigned long dev_l
)
320 struct cf_mips_dev
* dev
=(struct cf_mips_dev
*) dev_l
;
321 disable_irq(dev
->irq
);
322 /* Perhaps send abort to the device? */
323 printk(KERN_ERR
"cf-mips: wait not busy timeout (%lus)"
324 ", status 0x%02x, state %d\n",
325 jiffies
- dev
->irq_enable_time
, (unsigned) rareg(ATA_REG_ST
,dev
), dev
->tstate
);
326 dev
->tstate
= TS_IDLE
;
327 cf_async_trans_done(dev
,CF_TRANS_FAILED
);
330 int cf_do_transfer(struct cf_mips_dev
* dev
,sector_t sector
, unsigned long nsect
,
331 char* buffer
, int is_write
)
333 BUG_ON(dev
->tstate
!=TS_IDLE
);
334 if (nsect
> ATA_MAX_SECT_PER_CMD
) {
335 printk(KERN_WARNING
"cf-mips: sector count %lu out of range\n",nsect
);
336 return CF_TRANS_FAILED
;
338 if (sector
+ nsect
> dev
->sectors
) {
339 printk(KERN_WARNING
"cf-mips: sector %lu out of range\n",sector
);
340 return CF_TRANS_FAILED
;
343 dev
->tbuf_size
= nsect
*512;
344 dev
->tsect_start
= sector
;
345 dev
->tsector_count
= nsect
;
346 dev
->tsectors_left
= dev
->tsector_count
;
347 dev
->tread
= (is_write
)?0:1;
349 dev
->tcmd
= (dev
->block_size
== 1 ?
350 (is_write
? ATA_CMD_WRITE_SECTORS
: ATA_CMD_READ_SECTORS
) :
351 (is_write
? ATA_CMD_WRITE_MULTIPLE
: ATA_CMD_READ_MULTIPLE
));
353 return cf_do_state(dev
);
356 static int do_identify(struct cf_mips_dev
*dev
)
358 u16 sbuf
[CF_SECT_SIZE
>> 1];
360 char tstr
[17]; //serial
363 BUG_ON(dev
->tstate
!=TS_IDLE
);
364 dev
->tbuf
= (char *) sbuf
;
365 dev
->tbuf_size
= CF_SECT_SIZE
;
366 dev
->tsect_start
= 0;
367 dev
->tsector_count
= 0;
368 dev
->tsectors_left
= 1;
370 dev
->tcmd
= ATA_CMD_IDENTIFY_DRIVE
;
372 DEBUGP(KERN_INFO
"cf-mips: identify drive..\n");
373 res
= cf_do_state(dev
);
374 if (res
== CF_TRANS_IN_PROGRESS
) {
375 printk(KERN_ERR
"cf-mips: BUG: async identify cmd\n");
376 return CF_TRANS_FAILED
;
378 if (res
!= CF_TRANS_OK
)
384 dev
->sectors
= ((unsigned long) sbuf
[7] << 16) | sbuf
[8];
386 memcpy(tstr
, &sbuf
[12], 16);
389 /* Byte-swap the serial number */
390 for (i
= 0; i
<8; i
++) {
392 tstr
[i
* 2] = tstr
[i
* 2 +1];
393 tstr
[i
* 2 + 1] = tmp
;
396 printk(KERN_INFO
"cf-mips: %s detected, C/H/S=%d/%d/%d sectors=%u (%uMB) Serial=%s\n",
397 (sbuf
[0] == 0x848A ? "CF card" : "ATA drive"), dev
->cyl
, dev
->head
,
398 dev
->spt
, dev
->sectors
, dev
->sectors
>> 11, tstr
);
402 static void init_multiple(struct cf_mips_dev
* dev
)
405 DEBUGP(KERN_INFO
"cf-mips: detecting block size\n");
407 dev
->block_size
= 128; /* max block size = 128 sectors (64KB) */
409 wareg(dev
->block_size
, ATA_REG_SC
,dev
);
410 wareg(ATA_REG_DH_BASE
| ATA_REG_DH_LBA
, ATA_REG_DH
,dev
);
411 wareg(ATA_CMD_SET_MULTIPLE
, ATA_REG_CMD
,dev
);
413 res
= wait_not_busy(10 * SECS
, 1,dev
);
414 if (res
!= CF_TRANS_OK
) {
415 printk(KERN_ERR
"cf-mips: failed to detect block size: busy!\n");
419 if ((rareg(ATA_REG_ST
,dev
) & ATA_REG_ST_ERR
) == 0)
421 dev
->block_size
/= 2;
422 } while (dev
->block_size
> 1);
424 printk(KERN_INFO
"cf-mips: multiple sectors = %d\n", dev
->block_size
);
427 int cf_init(struct cf_mips_dev
*dev
)
429 tasklet_init(&dev
->tasklet
,cf_do_tasklet
,(unsigned long)dev
);
430 dev
->baddr
= ioremap_nocache((unsigned long)dev
->base
, CFDEV_BUF_SIZE
);
432 printk(KERN_ERR
"cf-mips: cf_init: ioremap for (%lx,%x) failed\n",
433 (unsigned long) dev
->base
, CFDEV_BUF_SIZE
);
437 if (!cf_present(dev
)) {
438 printk(KERN_WARNING
"cf-mips: cf card not present\n");
443 if (do_reset(dev
) != CF_TRANS_OK
) {
444 printk(KERN_ERR
"cf-mips: cf reset failed\n");
449 if (!do_identify(dev
)) {
450 printk(KERN_ERR
"cf-mips: cf identify failed\n");
455 /* set_apm_level(ATA_APM_WITH_STANDBY); */
458 init_timer(&dev
->to_timer
);
459 dev
->to_timer
.function
= cf_async_timeout
;
460 dev
->to_timer
.data
= (unsigned long)dev
;
463 if (request_irq(dev
->irq
, cf_irq_handler
, 0, "CF Mips", dev
)) {
464 printk(KERN_ERR
"cf-mips: failed to get irq\n");
468 /* Disable below would be odd, because request will enable, and the tasklet
469 will disable it itself */
470 //disable_irq(dev->irq);
477 void cf_cleanup(struct cf_mips_dev
*dev
)
480 free_irq(dev
->irq
, NULL
);
481 #if REQUEST_MEM_REGION
482 release_mem_region((unsigned long)dev
->base
, CFDEV_BUF_SIZE
);
This page took 0.075838 seconds and 5 git commands to generate.