1 #include <linux/slab.h>
9 static u8 _Check_D_DevCode(u8);
11 static u8 IsSSFDCCompliance;
12 static u8 IsXDCompliance;
14 struct keucr_media_info Ssfdc;
15 struct keucr_media_address Media;
16 struct keucr_media_area CisArea;
20 #define EVEN 0 /* Even Page for 256byte/page */
21 #define ODD 1 /* Odd Page for 256byte/page */
24 /* SmartMedia Redundant buffer data Control Subroutine
25 *----- Check_D_DataBlank() --------------------------------------------
27 int Check_D_DataBlank(u8 *redundant)
31 for (i = 0; i < REDTSIZE; i++)
32 if (*redundant++ != 0xFF)
38 /* ----- Check_D_FailBlock() -------------------------------------------- */
39 int Check_D_FailBlock(u8 *redundant)
41 redundant += REDT_BLOCK;
43 if (*redundant == 0xFF)
47 if (hweight8(*redundant) < 7)
53 /* ----- Check_D_DataStatus() ------------------------------------------- */
54 int Check_D_DataStatus(u8 *redundant)
56 redundant += REDT_DATA;
58 if (*redundant == 0xFF)
61 ErrXDCode = ERR_DataStatus;
66 if (hweight8(*redundant) < 5)
72 /* ----- Load_D_LogBlockAddr() ------------------------------------------ */
73 int Load_D_LogBlockAddr(u8 *redundant)
77 addr1 = (u16)*(redundant + REDT_ADDR1H)*0x0100 +
78 (u16)*(redundant + REDT_ADDR1L);
79 addr2 = (u16)*(redundant + REDT_ADDR2H)*0x0100 +
80 (u16)*(redundant + REDT_ADDR2L);
83 if ((addr1 & 0xF000) == 0x1000) {
84 Media.LogBlock = (addr1 & 0x0FFF) / 2;
88 if (hweight16((u16)(addr1^addr2)) != 0x01)
91 if ((addr1 & 0xF000) == 0x1000)
92 if (!(hweight16(addr1) & 0x01)) {
93 Media.LogBlock = (addr1 & 0x0FFF) / 2;
97 if ((addr2 & 0xF000) == 0x1000)
98 if (!(hweight16(addr2) & 0x01)) {
99 Media.LogBlock = (addr2 & 0x0FFF) / 2;
106 /* ----- Clr_D_RedundantData() ------------------------------------------ */
107 void Clr_D_RedundantData(u8 *redundant)
111 for (i = 0; i < REDTSIZE; i++)
112 *(redundant + i) = 0xFF;
115 /* ----- Set_D_LogBlockAddr() ------------------------------------------- */
116 void Set_D_LogBlockAddr(u8 *redundant)
120 *(redundant + REDT_BLOCK) = 0xFF;
121 *(redundant + REDT_DATA) = 0xFF;
122 addr = Media.LogBlock*2 + 0x1000;
124 if ((hweight16(addr) % 2))
127 *(redundant + REDT_ADDR1H) = *(redundant + REDT_ADDR2H) =
129 *(redundant + REDT_ADDR1L) = *(redundant + REDT_ADDR2L) = (u8)addr;
132 /*----- Set_D_FailBlock() ---------------------------------------------- */
133 void Set_D_FailBlock(u8 *redundant)
136 for (i = 0; i < REDTSIZE; i++)
137 *redundant++ = (u8)((i == REDT_BLOCK) ? 0xF0 : 0xFF);
140 /* ----- Set_D_DataStaus() ---------------------------------------------- */
141 void Set_D_DataStaus(u8 *redundant)
143 redundant += REDT_DATA;
147 /* SmartMedia Function Command Subroutine
150 /* ----- Ssfdc_D_Reset() ------------------------------------------------ */
151 void Ssfdc_D_Reset(struct us_data *us)
156 /* ----- Ssfdc_D_ReadCisSect() ------------------------------------------ */
157 int Ssfdc_D_ReadCisSect(struct us_data *us, u8 *buf, u8 *redundant)
162 zone = Media.Zone; block = Media.PhyBlock; sector = Media.Sector;
164 Media.PhyBlock = CisArea.PhyBlock;
165 Media.Sector = CisArea.Sector;
167 if (Ssfdc_D_ReadSect(us, buf, redundant)) {
169 Media.PhyBlock = block;
170 Media.Sector = sector;
174 Media.Zone = zone; Media.PhyBlock = block; Media.Sector = sector;
179 /* ----- Ssfdc_D_ReadSect() --------------------------------------------- */
180 int Ssfdc_D_ReadSect(struct us_data *us, u8 *buf, u8 *redundant)
182 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
186 result = ENE_LoadBinCode(us, SM_RW_PATTERN);
187 if (result != USB_STOR_XFER_GOOD) {
188 dev_err(&us->pusb_dev->dev,
189 "Failed to load SmartMedia read/write code\n");
190 return USB_STOR_TRANSPORT_ERROR;
193 addr = (u16)Media.Zone*Ssfdc.MaxBlocks + Media.PhyBlock;
194 addr = addr*(u16)Ssfdc.MaxSectors + Media.Sector;
197 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
198 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
199 bcb->DataTransferLength = 0x200;
203 bcb->CDB[4] = (u8)addr;
204 bcb->CDB[3] = (u8)(addr / 0x0100);
205 bcb->CDB[2] = Media.Zone / 2;
207 result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
208 if (result != USB_STOR_XFER_GOOD)
209 return USB_STOR_TRANSPORT_ERROR;
212 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
213 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
214 bcb->DataTransferLength = 0x10;
218 bcb->CDB[4] = (u8)addr;
219 bcb->CDB[3] = (u8)(addr / 0x0100);
220 bcb->CDB[2] = Media.Zone / 2;
224 result = ENE_SendScsiCmd(us, FDIR_READ, redundant, 0);
225 if (result != USB_STOR_XFER_GOOD)
226 return USB_STOR_TRANSPORT_ERROR;
228 return USB_STOR_TRANSPORT_GOOD;
231 /* ----- Ssfdc_D_ReadBlock() --------------------------------------------- */
232 int Ssfdc_D_ReadBlock(struct us_data *us, u16 count, u8 *buf,
235 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
239 result = ENE_LoadBinCode(us, SM_RW_PATTERN);
240 if (result != USB_STOR_XFER_GOOD) {
241 dev_err(&us->pusb_dev->dev,
242 "Failed to load SmartMedia read/write code\n");
243 return USB_STOR_TRANSPORT_ERROR;
246 addr = (u16)Media.Zone*Ssfdc.MaxBlocks + Media.PhyBlock;
247 addr = addr*(u16)Ssfdc.MaxSectors + Media.Sector;
250 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
251 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
252 bcb->DataTransferLength = 0x200*count;
256 bcb->CDB[4] = (u8)addr;
257 bcb->CDB[3] = (u8)(addr / 0x0100);
258 bcb->CDB[2] = Media.Zone / 2;
260 result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
261 if (result != USB_STOR_XFER_GOOD)
262 return USB_STOR_TRANSPORT_ERROR;
265 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
266 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
267 bcb->DataTransferLength = 0x10;
271 bcb->CDB[4] = (u8)addr;
272 bcb->CDB[3] = (u8)(addr / 0x0100);
273 bcb->CDB[2] = Media.Zone / 2;
277 result = ENE_SendScsiCmd(us, FDIR_READ, redundant, 0);
278 if (result != USB_STOR_XFER_GOOD)
279 return USB_STOR_TRANSPORT_ERROR;
281 return USB_STOR_TRANSPORT_GOOD;
285 /* ----- Ssfdc_D_CopyBlock() -------------------------------------------- */
286 int Ssfdc_D_CopyBlock(struct us_data *us, u16 count, u8 *buf,
289 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
291 u16 ReadAddr, WriteAddr;
293 result = ENE_LoadBinCode(us, SM_RW_PATTERN);
294 if (result != USB_STOR_XFER_GOOD) {
295 dev_err(&us->pusb_dev->dev,
296 "Failed to load SmartMedia read/write code\n");
297 return USB_STOR_TRANSPORT_ERROR;
300 ReadAddr = (u16)Media.Zone*Ssfdc.MaxBlocks + ReadBlock;
301 ReadAddr = ReadAddr*(u16)Ssfdc.MaxSectors;
302 WriteAddr = (u16)Media.Zone*Ssfdc.MaxBlocks + WriteBlock;
303 WriteAddr = WriteAddr*(u16)Ssfdc.MaxSectors;
305 /* Write sect data */
306 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
307 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
308 bcb->DataTransferLength = 0x200*count;
312 bcb->CDB[7] = (u8)WriteAddr;
313 bcb->CDB[6] = (u8)(WriteAddr / 0x0100);
314 bcb->CDB[5] = Media.Zone / 2;
315 bcb->CDB[8] = *(redundant + REDT_ADDR1H);
316 bcb->CDB[9] = *(redundant + REDT_ADDR1L);
317 bcb->CDB[10] = Media.Sector;
319 if (ReadBlock != NO_ASSIGN) {
320 bcb->CDB[4] = (u8)ReadAddr;
321 bcb->CDB[3] = (u8)(ReadAddr / 0x0100);
322 bcb->CDB[2] = Media.Zone / 2;
326 result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
327 if (result != USB_STOR_XFER_GOOD)
328 return USB_STOR_TRANSPORT_ERROR;
330 return USB_STOR_TRANSPORT_GOOD;
333 /* ----- Ssfdc_D_WriteSectForCopy() ------------------------------------- */
334 int Ssfdc_D_WriteSectForCopy(struct us_data *us, u8 *buf, u8 *redundant)
336 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
340 result = ENE_LoadBinCode(us, SM_RW_PATTERN);
341 if (result != USB_STOR_XFER_GOOD) {
342 dev_err(&us->pusb_dev->dev,
343 "Failed to load SmartMedia read/write code\n");
344 return USB_STOR_TRANSPORT_ERROR;
348 addr = (u16)Media.Zone*Ssfdc.MaxBlocks + Media.PhyBlock;
349 addr = addr*(u16)Ssfdc.MaxSectors + Media.Sector;
351 /* Write sect data */
352 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
353 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
354 bcb->DataTransferLength = 0x200;
358 bcb->CDB[7] = (u8)addr;
359 bcb->CDB[6] = (u8)(addr / 0x0100);
360 bcb->CDB[5] = Media.Zone / 2;
361 bcb->CDB[8] = *(redundant + REDT_ADDR1H);
362 bcb->CDB[9] = *(redundant + REDT_ADDR1L);
364 result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
365 if (result != USB_STOR_XFER_GOOD)
366 return USB_STOR_TRANSPORT_ERROR;
368 return USB_STOR_TRANSPORT_GOOD;
372 /* ----- Ssfdc_D_EraseBlock() ------------------------------------------- */
373 int Ssfdc_D_EraseBlock(struct us_data *us)
375 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
379 result = ENE_LoadBinCode(us, SM_RW_PATTERN);
380 if (result != USB_STOR_XFER_GOOD) {
381 dev_err(&us->pusb_dev->dev,
382 "Failed to load SmartMedia read/write code\n");
383 return USB_STOR_TRANSPORT_ERROR;
386 addr = (u16)Media.Zone*Ssfdc.MaxBlocks + Media.PhyBlock;
387 addr = addr*(u16)Ssfdc.MaxSectors;
389 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
390 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
391 bcb->DataTransferLength = 0x200;
395 bcb->CDB[7] = (u8)addr;
396 bcb->CDB[6] = (u8)(addr / 0x0100);
397 bcb->CDB[5] = Media.Zone / 2;
399 result = ENE_SendScsiCmd(us, FDIR_READ, NULL, 0);
400 if (result != USB_STOR_XFER_GOOD)
401 return USB_STOR_TRANSPORT_ERROR;
403 return USB_STOR_TRANSPORT_GOOD;
407 /*----- Ssfdc_D_ReadRedtData() ----------------------------------------- */
408 int Ssfdc_D_ReadRedtData(struct us_data *us, u8 *redundant)
410 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
415 result = ENE_LoadBinCode(us, SM_RW_PATTERN);
416 if (result != USB_STOR_XFER_GOOD) {
417 dev_err(&us->pusb_dev->dev,
418 "Failed to load SmartMedia read/write code\n");
419 return USB_STOR_TRANSPORT_ERROR;
422 addr = (u16)Media.Zone*Ssfdc.MaxBlocks + Media.PhyBlock;
423 addr = addr*(u16)Ssfdc.MaxSectors + Media.Sector;
425 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
426 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
427 bcb->DataTransferLength = 0x10;
431 bcb->CDB[4] = (u8)addr;
432 bcb->CDB[3] = (u8)(addr / 0x0100);
433 bcb->CDB[2] = Media.Zone / 2;
437 buf = kmalloc(0x10, GFP_KERNEL);
438 result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
439 memcpy(redundant, buf, 0x10);
441 if (result != USB_STOR_XFER_GOOD)
442 return USB_STOR_TRANSPORT_ERROR;
444 return USB_STOR_TRANSPORT_GOOD;
448 /* ----- Ssfdc_D_WriteRedtData() ---------------------------------------- */
449 int Ssfdc_D_WriteRedtData(struct us_data *us, u8 *redundant)
451 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
455 result = ENE_LoadBinCode(us, SM_RW_PATTERN);
456 if (result != USB_STOR_XFER_GOOD) {
457 dev_err(&us->pusb_dev->dev,
458 "Failed to load SmartMedia read/write code\n");
459 return USB_STOR_TRANSPORT_ERROR;
462 addr = (u16)Media.Zone*Ssfdc.MaxBlocks + Media.PhyBlock;
463 addr = addr*(u16)Ssfdc.MaxSectors + Media.Sector;
465 memset(bcb, 0, sizeof(struct bulk_cb_wrap));
466 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
467 bcb->DataTransferLength = 0x10;
471 bcb->CDB[7] = (u8)addr;
472 bcb->CDB[6] = (u8)(addr / 0x0100);
473 bcb->CDB[5] = Media.Zone / 2;
474 bcb->CDB[8] = *(redundant + REDT_ADDR1H);
475 bcb->CDB[9] = *(redundant + REDT_ADDR1L);
477 result = ENE_SendScsiCmd(us, FDIR_READ, NULL, 0);
478 if (result != USB_STOR_XFER_GOOD)
479 return USB_STOR_TRANSPORT_ERROR;
481 return USB_STOR_TRANSPORT_GOOD;
484 /* ----- Ssfdc_D_CheckStatus() ------------------------------------------ */
485 int Ssfdc_D_CheckStatus(void)
492 /* SmartMedia ID Code Check & Mode Set Subroutine
493 * ----- Set_D_SsfdcModel() ---------------------------------------------
495 int Set_D_SsfdcModel(u8 dcode)
497 switch (_Check_D_DevCode(dcode)) {
499 Ssfdc.Model = SSFDC1MB;
500 Ssfdc.Attribute = FLASH | AD3CYC | BS16 | PS256;
502 Ssfdc.MaxBlocks = 256;
503 Ssfdc.MaxLogBlocks = 250;
504 Ssfdc.MaxSectors = 8;
507 Ssfdc.Model = SSFDC2MB;
508 Ssfdc.Attribute = FLASH | AD3CYC | BS16 | PS256;
510 Ssfdc.MaxBlocks = 512;
511 Ssfdc.MaxLogBlocks = 500;
512 Ssfdc.MaxSectors = 8;
515 Ssfdc.Model = SSFDC4MB;
516 Ssfdc.Attribute = FLASH | AD3CYC | BS16 | PS512;
518 Ssfdc.MaxBlocks = 512;
519 Ssfdc.MaxLogBlocks = 500;
520 Ssfdc.MaxSectors = 16;
523 Ssfdc.Model = SSFDC8MB;
524 Ssfdc.Attribute = FLASH | AD3CYC | BS16 | PS512;
526 Ssfdc.MaxBlocks = 1024;
527 Ssfdc.MaxLogBlocks = 1000;
528 Ssfdc.MaxSectors = 16;
531 Ssfdc.Model = SSFDC16MB;
532 Ssfdc.Attribute = FLASH | AD3CYC | BS32 | PS512;
534 Ssfdc.MaxBlocks = 1024;
535 Ssfdc.MaxLogBlocks = 1000;
536 Ssfdc.MaxSectors = 32;
539 Ssfdc.Model = SSFDC32MB;
540 Ssfdc.Attribute = FLASH | AD3CYC | BS32 | PS512;
542 Ssfdc.MaxBlocks = 1024;
543 Ssfdc.MaxLogBlocks = 1000;
544 Ssfdc.MaxSectors = 32;
547 Ssfdc.Model = SSFDC64MB;
548 Ssfdc.Attribute = FLASH | AD4CYC | BS32 | PS512;
550 Ssfdc.MaxBlocks = 1024;
551 Ssfdc.MaxLogBlocks = 1000;
552 Ssfdc.MaxSectors = 32;
555 Ssfdc.Model = SSFDC128MB;
556 Ssfdc.Attribute = FLASH | AD4CYC | BS32 | PS512;
558 Ssfdc.MaxBlocks = 1024;
559 Ssfdc.MaxLogBlocks = 1000;
560 Ssfdc.MaxSectors = 32;
563 Ssfdc.Model = SSFDC256MB;
564 Ssfdc.Attribute = FLASH | AD4CYC | BS32 | PS512;
566 Ssfdc.MaxBlocks = 1024;
567 Ssfdc.MaxLogBlocks = 1000;
568 Ssfdc.MaxSectors = 32;
571 Ssfdc.Model = SSFDC512MB;
572 Ssfdc.Attribute = FLASH | AD4CYC | BS32 | PS512;
574 Ssfdc.MaxBlocks = 1024;
575 Ssfdc.MaxLogBlocks = 1000;
576 Ssfdc.MaxSectors = 32;
579 Ssfdc.Model = SSFDC1GB;
580 Ssfdc.Attribute = FLASH | AD4CYC | BS32 | PS512;
582 Ssfdc.MaxBlocks = 1024;
583 Ssfdc.MaxLogBlocks = 1000;
584 Ssfdc.MaxSectors = 32;
587 Ssfdc.Model = SSFDC2GB;
588 Ssfdc.Attribute = FLASH | AD4CYC | BS32 | PS512;
589 Ssfdc.MaxZones = 128;
590 Ssfdc.MaxBlocks = 1024;
591 Ssfdc.MaxLogBlocks = 1000;
592 Ssfdc.MaxSectors = 32;
595 Ssfdc.Model = NOSSFDC;
602 /* ----- _Check_D_DevCode() --------------------------------------------- */
603 static u8 _Check_D_DevCode(u8 dcode)
608 case 0xEC: return SSFDC1MB; /* 8Mbit (1M) NAND */
610 case 0xEA: return SSFDC2MB; /* 16Mbit (2M) NAND */
613 case 0xE5: return SSFDC4MB; /* 32Mbit (4M) NAND */
614 case 0xE6: return SSFDC8MB; /* 64Mbit (8M) NAND */
615 case 0x73: return SSFDC16MB; /* 128Mbit (16M)NAND */
616 case 0x75: return SSFDC32MB; /* 256Mbit (32M)NAND */
617 case 0x76: return SSFDC64MB; /* 512Mbit (64M)NAND */
618 case 0x79: return SSFDC128MB; /* 1Gbit(128M)NAND */
619 case 0x71: return SSFDC256MB;
620 case 0xDC: return SSFDC512MB;
621 case 0xD3: return SSFDC1GB;
622 case 0xD5: return SSFDC2GB;
623 default: return NOSSFDC;
630 /* SmartMedia ECC Control Subroutine
631 * ----- Check_D_ReadError() ----------------------------------------------
633 int Check_D_ReadError(u8 *redundant)
638 /* ----- Check_D_Correct() ---------------------------------------------- */
639 int Check_D_Correct(u8 *buf, u8 *redundant)
644 /* ----- Check_D_CISdata() ---------------------------------------------- */
645 int Check_D_CISdata(u8 *buf, u8 *redundant)
647 u8 cis[] = {0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02,
650 int cis_len = sizeof(cis);
652 if (!IsSSFDCCompliance && !IsXDCompliance)
655 if (!memcmp(redundant + 0x0D, EccBuf, 3))
656 return memcmp(buf, cis, cis_len);
658 if (!_Correct_D_SwECC(buf, redundant + 0x0D, EccBuf))
659 return memcmp(buf, cis, cis_len);
662 if (!memcmp(redundant + 0x08, EccBuf + 0x03, 3))
663 return memcmp(buf, cis, cis_len);
665 if (!_Correct_D_SwECC(buf, redundant + 0x08, EccBuf + 0x03))
666 return memcmp(buf, cis, cis_len);
671 /* ----- Set_D_RightECC() ---------------------------------------------- */
672 void Set_D_RightECC(u8 *redundant)
674 /* Driver ECC Check */