cmd_ide.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * (C) Copyright 2000-2011
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * IDE support
  26. */
  27. #include <common.h>
  28. #include <config.h>
  29. #include <watchdog.h>
  30. #include <command.h>
  31. #include <image.h>
  32. #include <asm/byteorder.h>
  33. #include <asm/io.h>
  34. #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
  35. # include <pcmcia.h>
  36. #endif
  37. #include <ide.h>
  38. #include <ata.h>
  39. #ifdef CONFIG_STATUS_LED
  40. # include <status_led.h>
  41. #endif
  42. #ifdef __PPC__
  43. # define EIEIO __asm__ volatile ("eieio")
  44. # define SYNC __asm__ volatile ("sync")
  45. #else
  46. # define EIEIO /* nothing */
  47. # define SYNC /* nothing */
  48. #endif
  49. /* ------------------------------------------------------------------------- */
  50. /* Current I/O Device */
  51. static int curr_device = -1;
  52. /* Current offset for IDE0 / IDE1 bus access */
  53. ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
  54. #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
  55. CONFIG_SYS_ATA_IDE0_OFFSET,
  56. #endif
  57. #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
  58. CONFIG_SYS_ATA_IDE1_OFFSET,
  59. #endif
  60. };
  61. static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
  62. block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
  63. /* ------------------------------------------------------------------------- */
  64. #ifdef CONFIG_IDE_RESET
  65. static void ide_reset (void);
  66. #else
  67. #define ide_reset() /* dummy */
  68. #endif
  69. static void ide_ident (block_dev_desc_t *dev_desc);
  70. static uchar ide_wait (int dev, ulong t);
  71. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  72. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  73. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  74. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  75. #ifndef CONFIG_SYS_ATA_PORT_ADDR
  76. #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
  77. #endif
  78. #ifdef CONFIG_ATAPI
  79. static void atapi_inquiry(block_dev_desc_t *dev_desc);
  80. ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
  81. #endif
  82. /* ------------------------------------------------------------------------- */
  83. int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  84. {
  85. int rcode = 0;
  86. switch (argc) {
  87. case 0:
  88. case 1:
  89. return CMD_RET_USAGE;
  90. case 2:
  91. if (strncmp(argv[1], "res", 3) == 0) {
  92. puts("\nReset IDE"
  93. #ifdef CONFIG_IDE_8xx_DIRECT
  94. " on PCMCIA " PCMCIA_SLOT_MSG
  95. #endif
  96. ": ");
  97. ide_init();
  98. return 0;
  99. } else if (strncmp(argv[1], "inf", 3) == 0) {
  100. int i;
  101. putc('\n');
  102. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
  103. if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
  104. continue; /* list only known devices */
  105. printf("IDE device %d: ", i);
  106. dev_print(&ide_dev_desc[i]);
  107. }
  108. return 0;
  109. } else if (strncmp(argv[1], "dev", 3) == 0) {
  110. if ((curr_device < 0)
  111. || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
  112. puts("\nno IDE devices available\n");
  113. return 1;
  114. }
  115. printf("\nIDE device %d: ", curr_device);
  116. dev_print(&ide_dev_desc[curr_device]);
  117. return 0;
  118. } else if (strncmp(argv[1], "part", 4) == 0) {
  119. int dev, ok;
  120. for (ok = 0, dev = 0;
  121. dev < CONFIG_SYS_IDE_MAXDEVICE;
  122. ++dev) {
  123. if (ide_dev_desc[dev].part_type !=
  124. PART_TYPE_UNKNOWN) {
  125. ++ok;
  126. if (dev)
  127. putc('\n');
  128. print_part(&ide_dev_desc[dev]);
  129. }
  130. }
  131. if (!ok) {
  132. puts("\nno IDE devices available\n");
  133. rcode++;
  134. }
  135. return rcode;
  136. }
  137. return CMD_RET_USAGE;
  138. case 3:
  139. if (strncmp(argv[1], "dev", 3) == 0) {
  140. int dev = (int) simple_strtoul(argv[2], NULL, 10);
  141. printf("\nIDE device %d: ", dev);
  142. if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
  143. puts("unknown device\n");
  144. return 1;
  145. }
  146. dev_print(&ide_dev_desc[dev]);
  147. /*ide_print (dev); */
  148. if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
  149. return 1;
  150. curr_device = dev;
  151. puts("... is now current device\n");
  152. return 0;
  153. } else if (strncmp(argv[1], "part", 4) == 0) {
  154. int dev = (int) simple_strtoul(argv[2], NULL, 10);
  155. if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  156. print_part(&ide_dev_desc[dev]);
  157. } else {
  158. printf("\nIDE device %d not available\n",
  159. dev);
  160. rcode = 1;
  161. }
  162. return rcode;
  163. }
  164. return CMD_RET_USAGE;
  165. default:
  166. /* at least 4 args */
  167. if (strcmp(argv[1], "read") == 0) {
  168. ulong addr = simple_strtoul(argv[2], NULL, 16);
  169. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  170. ulong n;
  171. #ifdef CONFIG_SYS_64BIT_LBA
  172. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  173. printf("\nIDE read: device %d block # %lld, count %ld ... ",
  174. curr_device, blk, cnt);
  175. #else
  176. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  177. printf("\nIDE read: device %d block # %ld, count %ld ... ",
  178. curr_device, blk, cnt);
  179. #endif
  180. n = ide_dev_desc[curr_device].block_read(curr_device,
  181. blk, cnt,
  182. (ulong *)addr);
  183. /* flush cache after read */
  184. flush_cache(addr,
  185. cnt * ide_dev_desc[curr_device].blksz);
  186. printf("%ld blocks read: %s\n",
  187. n, (n == cnt) ? "OK" : "ERROR");
  188. if (n == cnt)
  189. return 0;
  190. else
  191. return 1;
  192. } else if (strcmp(argv[1], "write") == 0) {
  193. ulong addr = simple_strtoul(argv[2], NULL, 16);
  194. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  195. ulong n;
  196. #ifdef CONFIG_SYS_64BIT_LBA
  197. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  198. printf("\nIDE write: device %d block # %lld, count %ld ... ",
  199. curr_device, blk, cnt);
  200. #else
  201. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  202. printf("\nIDE write: device %d block # %ld, count %ld ... ",
  203. curr_device, blk, cnt);
  204. #endif
  205. n = ide_write(curr_device, blk, cnt, (ulong *) addr);
  206. printf("%ld blocks written: %s\n",
  207. n, (n == cnt) ? "OK" : "ERROR");
  208. if (n == cnt)
  209. return 0;
  210. else
  211. return 1;
  212. } else {
  213. return CMD_RET_USAGE;
  214. }
  215. return rcode;
  216. }
  217. }
  218. int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  219. {
  220. return common_diskboot(cmdtp, "ide", argc, argv);
  221. }
  222. /* ------------------------------------------------------------------------- */
  223. void __ide_led(uchar led, uchar status)
  224. {
  225. #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
  226. static uchar led_buffer; /* Buffer for current LED status */
  227. uchar *led_port = LED_PORT;
  228. if (status) /* switch LED on */
  229. led_buffer |= led;
  230. else /* switch LED off */
  231. led_buffer &= ~led;
  232. *led_port = led_buffer;
  233. #endif
  234. }
  235. void ide_led(uchar led, uchar status)
  236. __attribute__ ((weak, alias("__ide_led")));
  237. #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
  238. # define DEVICE_LED(x) 0
  239. # define LED_IDE1 1
  240. # define LED_IDE2 2
  241. #endif
  242. /* ------------------------------------------------------------------------- */
  243. inline void __ide_outb(int dev, int port, unsigned char val)
  244. {
  245. debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
  246. dev, port, val,
  247. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  248. #if defined(CONFIG_IDE_AHB)
  249. if (port) {
  250. /* write command */
  251. ide_write_register(dev, port, val);
  252. } else {
  253. /* write data */
  254. outb(val, (ATA_CURR_BASE(dev)));
  255. }
  256. #else
  257. outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  258. #endif
  259. }
  260. void ide_outb(int dev, int port, unsigned char val)
  261. __attribute__ ((weak, alias("__ide_outb")));
  262. inline unsigned char __ide_inb(int dev, int port)
  263. {
  264. uchar val;
  265. #if defined(CONFIG_IDE_AHB)
  266. val = ide_read_register(dev, port);
  267. #else
  268. val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  269. #endif
  270. debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
  271. dev, port,
  272. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
  273. return val;
  274. }
  275. unsigned char ide_inb(int dev, int port)
  276. __attribute__ ((weak, alias("__ide_inb")));
  277. #ifdef CONFIG_TUNE_PIO
  278. inline int __ide_set_piomode(int pio_mode)
  279. {
  280. return 0;
  281. }
  282. inline int ide_set_piomode(int pio_mode)
  283. __attribute__ ((weak, alias("__ide_set_piomode")));
  284. #endif
  285. void ide_init(void)
  286. {
  287. unsigned char c;
  288. int i, bus;
  289. #ifdef CONFIG_IDE_8xx_PCCARD
  290. extern int ide_devices_found; /* Initialized in check_ide_device() */
  291. #endif /* CONFIG_IDE_8xx_PCCARD */
  292. #ifdef CONFIG_IDE_PREINIT
  293. WATCHDOG_RESET();
  294. if (ide_preinit()) {
  295. puts("ide_preinit failed\n");
  296. return;
  297. }
  298. #endif /* CONFIG_IDE_PREINIT */
  299. WATCHDOG_RESET();
  300. /*
  301. * Reset the IDE just to be sure.
  302. * Light LED's to show
  303. */
  304. ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  305. /* ATAPI Drives seems to need a proper IDE Reset */
  306. ide_reset();
  307. #ifdef CONFIG_IDE_INIT_POSTRESET
  308. WATCHDOG_RESET();
  309. if (ide_init_postreset()) {
  310. puts("ide_preinit_postreset failed\n");
  311. return;
  312. }
  313. #endif /* CONFIG_IDE_INIT_POSTRESET */
  314. /*
  315. * Wait for IDE to get ready.
  316. * According to spec, this can take up to 31 seconds!
  317. */
  318. for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
  319. int dev =
  320. bus * (CONFIG_SYS_IDE_MAXDEVICE /
  321. CONFIG_SYS_IDE_MAXBUS);
  322. #ifdef CONFIG_IDE_8xx_PCCARD
  323. /* Skip non-ide devices from probing */
  324. if ((ide_devices_found & (1 << bus)) == 0) {
  325. ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  326. continue;
  327. }
  328. #endif
  329. printf("Bus %d: ", bus);
  330. ide_bus_ok[bus] = 0;
  331. /* Select device
  332. */
  333. udelay(100000); /* 100 ms */
  334. ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  335. udelay(100000); /* 100 ms */
  336. i = 0;
  337. do {
  338. udelay(10000); /* 10 ms */
  339. c = ide_inb(dev, ATA_STATUS);
  340. i++;
  341. if (i > (ATA_RESET_TIME * 100)) {
  342. puts("** Timeout **\n");
  343. /* LED's off */
  344. ide_led((LED_IDE1 | LED_IDE2), 0);
  345. return;
  346. }
  347. if ((i >= 100) && ((i % 100) == 0))
  348. putc('.');
  349. } while (c & ATA_STAT_BUSY);
  350. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  351. puts("not available ");
  352. debug("Status = 0x%02X ", c);
  353. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  354. } else if ((c & ATA_STAT_READY) == 0) {
  355. puts("not available ");
  356. debug("Status = 0x%02X ", c);
  357. #endif
  358. } else {
  359. puts("OK ");
  360. ide_bus_ok[bus] = 1;
  361. }
  362. WATCHDOG_RESET();
  363. }
  364. putc('\n');
  365. ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  366. curr_device = -1;
  367. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
  368. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  369. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  370. ide_dev_desc[i].if_type = IF_TYPE_IDE;
  371. ide_dev_desc[i].dev = i;
  372. ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
  373. ide_dev_desc[i].blksz = 0;
  374. ide_dev_desc[i].lba = 0;
  375. ide_dev_desc[i].block_read = ide_read;
  376. ide_dev_desc[i].block_write = ide_write;
  377. if (!ide_bus_ok[IDE_BUS(i)])
  378. continue;
  379. ide_led(led, 1); /* LED on */
  380. ide_ident(&ide_dev_desc[i]);
  381. ide_led(led, 0); /* LED off */
  382. dev_print(&ide_dev_desc[i]);
  383. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  384. /* initialize partition type */
  385. init_part(&ide_dev_desc[i]);
  386. if (curr_device < 0)
  387. curr_device = i;
  388. }
  389. }
  390. WATCHDOG_RESET();
  391. }
  392. /* ------------------------------------------------------------------------- */
  393. #ifdef CONFIG_PARTITIONS
  394. block_dev_desc_t *ide_get_dev(int dev)
  395. {
  396. return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
  397. }
  398. #endif
  399. /* ------------------------------------------------------------------------- */
  400. void ide_input_swap_data(int dev, ulong *sect_buf, int words)
  401. __attribute__ ((weak, alias("__ide_input_swap_data")));
  402. void ide_input_data(int dev, ulong *sect_buf, int words)
  403. __attribute__ ((weak, alias("__ide_input_data")));
  404. void ide_output_data(int dev, const ulong *sect_buf, int words)
  405. __attribute__ ((weak, alias("__ide_output_data")));
  406. /* We only need to swap data if we are running on a big endian cpu. */
  407. #if defined(__LITTLE_ENDIAN)
  408. void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
  409. {
  410. ide_input_data(dev, sect_buf, words);
  411. }
  412. #else
  413. void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
  414. {
  415. volatile ushort *pbuf =
  416. (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  417. ushort *dbuf = (ushort *) sect_buf;
  418. debug("in input swap data base for read is %lx\n",
  419. (unsigned long) pbuf);
  420. while (words--) {
  421. #ifdef __MIPS__
  422. *dbuf++ = swab16p((u16 *) pbuf);
  423. *dbuf++ = swab16p((u16 *) pbuf);
  424. #else
  425. *dbuf++ = ld_le16(pbuf);
  426. *dbuf++ = ld_le16(pbuf);
  427. #endif /* !MIPS */
  428. }
  429. }
  430. #endif /* __LITTLE_ENDIAN */
  431. #if defined(CONFIG_IDE_SWAP_IO)
  432. void __ide_output_data(int dev, const ulong *sect_buf, int words)
  433. {
  434. ushort *dbuf;
  435. volatile ushort *pbuf;
  436. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  437. dbuf = (ushort *) sect_buf;
  438. while (words--) {
  439. EIEIO;
  440. *pbuf = *dbuf++;
  441. EIEIO;
  442. *pbuf = *dbuf++;
  443. }
  444. }
  445. #else /* ! CONFIG_IDE_SWAP_IO */
  446. void __ide_output_data(int dev, const ulong *sect_buf, int words)
  447. {
  448. #if defined(CONFIG_IDE_AHB)
  449. ide_write_data(dev, sect_buf, words);
  450. #else
  451. outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
  452. #endif
  453. }
  454. #endif /* CONFIG_IDE_SWAP_IO */
  455. #if defined(CONFIG_IDE_SWAP_IO)
  456. void __ide_input_data(int dev, ulong *sect_buf, int words)
  457. {
  458. ushort *dbuf;
  459. volatile ushort *pbuf;
  460. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  461. dbuf = (ushort *) sect_buf;
  462. debug("in input data base for read is %lx\n", (unsigned long) pbuf);
  463. while (words--) {
  464. EIEIO;
  465. *dbuf++ = *pbuf;
  466. EIEIO;
  467. *dbuf++ = *pbuf;
  468. }
  469. }
  470. #else /* ! CONFIG_IDE_SWAP_IO */
  471. void __ide_input_data(int dev, ulong *sect_buf, int words)
  472. {
  473. #if defined(CONFIG_IDE_AHB)
  474. ide_read_data(dev, sect_buf, words);
  475. #else
  476. insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
  477. #endif
  478. }
  479. #endif /* CONFIG_IDE_SWAP_IO */
  480. /* -------------------------------------------------------------------------
  481. */
  482. static void ide_ident(block_dev_desc_t *dev_desc)
  483. {
  484. unsigned char c;
  485. hd_driveid_t iop;
  486. #ifdef CONFIG_ATAPI
  487. int retries = 0;
  488. #endif
  489. #ifdef CONFIG_TUNE_PIO
  490. int pio_mode;
  491. #endif
  492. #if 0
  493. int mode, cycle_time;
  494. #endif
  495. int device;
  496. device = dev_desc->dev;
  497. printf(" Device %d: ", device);
  498. ide_led(DEVICE_LED(device), 1); /* LED on */
  499. /* Select device
  500. */
  501. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  502. dev_desc->if_type = IF_TYPE_IDE;
  503. #ifdef CONFIG_ATAPI
  504. retries = 0;
  505. /* Warning: This will be tricky to read */
  506. while (retries <= 1) {
  507. /* check signature */
  508. if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
  509. (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
  510. (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
  511. (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
  512. /* ATAPI Signature found */
  513. dev_desc->if_type = IF_TYPE_ATAPI;
  514. /*
  515. * Start Ident Command
  516. */
  517. ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
  518. /*
  519. * Wait for completion - ATAPI devices need more time
  520. * to become ready
  521. */
  522. c = ide_wait(device, ATAPI_TIME_OUT);
  523. } else
  524. #endif
  525. {
  526. /*
  527. * Start Ident Command
  528. */
  529. ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
  530. /*
  531. * Wait for completion
  532. */
  533. c = ide_wait(device, IDE_TIME_OUT);
  534. }
  535. ide_led(DEVICE_LED(device), 0); /* LED off */
  536. if (((c & ATA_STAT_DRQ) == 0) ||
  537. ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
  538. #ifdef CONFIG_ATAPI
  539. {
  540. /*
  541. * Need to soft reset the device
  542. * in case it's an ATAPI...
  543. */
  544. debug("Retrying...\n");
  545. ide_outb(device, ATA_DEV_HD,
  546. ATA_LBA | ATA_DEVICE(device));
  547. udelay(100000);
  548. ide_outb(device, ATA_COMMAND, 0x08);
  549. udelay(500000); /* 500 ms */
  550. }
  551. /*
  552. * Select device
  553. */
  554. ide_outb(device, ATA_DEV_HD,
  555. ATA_LBA | ATA_DEVICE(device));
  556. retries++;
  557. #else
  558. return;
  559. #endif
  560. }
  561. #ifdef CONFIG_ATAPI
  562. else
  563. break;
  564. } /* see above - ugly to read */
  565. if (retries == 2) /* Not found */
  566. return;
  567. #endif
  568. ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
  569. ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
  570. sizeof(dev_desc->revision));
  571. ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
  572. sizeof(dev_desc->vendor));
  573. ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
  574. sizeof(dev_desc->product));
  575. #ifdef __LITTLE_ENDIAN
  576. /*
  577. * firmware revision, model, and serial number have Big Endian Byte
  578. * order in Word. Convert all three to little endian.
  579. *
  580. * See CF+ and CompactFlash Specification Revision 2.0:
  581. * 6.2.1.6: Identify Drive, Table 39 for more details
  582. */
  583. strswab(dev_desc->revision);
  584. strswab(dev_desc->vendor);
  585. strswab(dev_desc->product);
  586. #endif /* __LITTLE_ENDIAN */
  587. if ((iop.config & 0x0080) == 0x0080)
  588. dev_desc->removable = 1;
  589. else
  590. dev_desc->removable = 0;
  591. #ifdef CONFIG_TUNE_PIO
  592. /* Mode 0 - 2 only, are directly determined by word 51. */
  593. pio_mode = iop.tPIO;
  594. if (pio_mode > 2) {
  595. printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
  596. /* Force it to dead slow, and hope for the best... */
  597. pio_mode = 0;
  598. }
  599. /* Any CompactFlash Storage Card that supports PIO mode 3 or above
  600. * shall set bit 1 of word 53 to one and support the fields contained
  601. * in words 64 through 70.
  602. */
  603. if (iop.field_valid & 0x02) {
  604. /*
  605. * Mode 3 and above are possible. Check in order from slow
  606. * to fast, so we wind up with the highest mode allowed.
  607. */
  608. if (iop.eide_pio_modes & 0x01)
  609. pio_mode = 3;
  610. if (iop.eide_pio_modes & 0x02)
  611. pio_mode = 4;
  612. if (ata_id_is_cfa((u16 *)&iop)) {
  613. if ((iop.cf_advanced_caps & 0x07) == 0x01)
  614. pio_mode = 5;
  615. if ((iop.cf_advanced_caps & 0x07) == 0x02)
  616. pio_mode = 6;
  617. }
  618. }
  619. /* System-specific, depends on bus speeds, etc. */
  620. ide_set_piomode(pio_mode);
  621. #endif /* CONFIG_TUNE_PIO */
  622. #if 0
  623. /*
  624. * Drive PIO mode autoselection
  625. */
  626. mode = iop.tPIO;
  627. printf("tPIO = 0x%02x = %d\n", mode, mode);
  628. if (mode > 2) { /* 2 is maximum allowed tPIO value */
  629. mode = 2;
  630. debug("Override tPIO -> 2\n");
  631. }
  632. if (iop.field_valid & 2) { /* drive implements ATA2? */
  633. debug("Drive implements ATA2\n");
  634. if (iop.capability & 8) { /* drive supports use_iordy? */
  635. cycle_time = iop.eide_pio_iordy;
  636. } else {
  637. cycle_time = iop.eide_pio;
  638. }
  639. debug("cycle time = %d\n", cycle_time);
  640. mode = 4;
  641. if (cycle_time > 120)
  642. mode = 3; /* 120 ns for PIO mode 4 */
  643. if (cycle_time > 180)
  644. mode = 2; /* 180 ns for PIO mode 3 */
  645. if (cycle_time > 240)
  646. mode = 1; /* 240 ns for PIO mode 4 */
  647. if (cycle_time > 383)
  648. mode = 0; /* 383 ns for PIO mode 4 */
  649. }
  650. printf("PIO mode to use: PIO %d\n", mode);
  651. #endif /* 0 */
  652. #ifdef CONFIG_ATAPI
  653. if (dev_desc->if_type == IF_TYPE_ATAPI) {
  654. atapi_inquiry(dev_desc);
  655. return;
  656. }
  657. #endif /* CONFIG_ATAPI */
  658. #ifdef __BIG_ENDIAN
  659. /* swap shorts */
  660. dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
  661. #else /* ! __BIG_ENDIAN */
  662. /*
  663. * do not swap shorts on little endian
  664. *
  665. * See CF+ and CompactFlash Specification Revision 2.0:
  666. * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
  667. */
  668. dev_desc->lba = iop.lba_capacity;
  669. #endif /* __BIG_ENDIAN */
  670. #ifdef CONFIG_LBA48
  671. if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
  672. dev_desc->lba48 = 1;
  673. dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
  674. ((unsigned long long) iop.lba48_capacity[1] << 16) |
  675. ((unsigned long long) iop.lba48_capacity[2] << 32) |
  676. ((unsigned long long) iop.lba48_capacity[3] << 48);
  677. } else {
  678. dev_desc->lba48 = 0;
  679. }
  680. #endif /* CONFIG_LBA48 */
  681. /* assuming HD */
  682. dev_desc->type = DEV_TYPE_HARDDISK;
  683. dev_desc->blksz = ATA_BLOCKSIZE;
  684. dev_desc->lun = 0; /* just to fill something in... */
  685. #if 0 /* only used to test the powersaving mode,
  686. * if enabled, the drive goes after 5 sec
  687. * in standby mode */
  688. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  689. c = ide_wait(device, IDE_TIME_OUT);
  690. ide_outb(device, ATA_SECT_CNT, 1);
  691. ide_outb(device, ATA_LBA_LOW, 0);
  692. ide_outb(device, ATA_LBA_MID, 0);
  693. ide_outb(device, ATA_LBA_HIGH, 0);
  694. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  695. ide_outb(device, ATA_COMMAND, 0xe3);
  696. udelay(50);
  697. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  698. #endif
  699. }
  700. /* ------------------------------------------------------------------------- */
  701. ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  702. {
  703. ulong n = 0;
  704. unsigned char c;
  705. unsigned char pwrsave = 0; /* power save */
  706. #ifdef CONFIG_LBA48
  707. unsigned char lba48 = 0;
  708. if (blknr & 0x0000fffff0000000ULL) {
  709. /* more than 28 bits used, use 48bit mode */
  710. lba48 = 1;
  711. }
  712. #endif
  713. debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
  714. device, blknr, blkcnt, (ulong) buffer);
  715. ide_led(DEVICE_LED(device), 1); /* LED on */
  716. /* Select device
  717. */
  718. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  719. c = ide_wait(device, IDE_TIME_OUT);
  720. if (c & ATA_STAT_BUSY) {
  721. printf("IDE read: device %d not ready\n", device);
  722. goto IDE_READ_E;
  723. }
  724. /* first check if the drive is in Powersaving mode, if yes,
  725. * increase the timeout value */
  726. ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  727. udelay(50);
  728. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  729. if (c & ATA_STAT_BUSY) {
  730. printf("IDE read: device %d not ready\n", device);
  731. goto IDE_READ_E;
  732. }
  733. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  734. printf("No Powersaving mode %X\n", c);
  735. } else {
  736. c = ide_inb(device, ATA_SECT_CNT);
  737. debug("Powersaving %02X\n", c);
  738. if (c == 0)
  739. pwrsave = 1;
  740. }
  741. while (blkcnt-- > 0) {
  742. c = ide_wait(device, IDE_TIME_OUT);
  743. if (c & ATA_STAT_BUSY) {
  744. printf("IDE read: device %d not ready\n", device);
  745. break;
  746. }
  747. #ifdef CONFIG_LBA48
  748. if (lba48) {
  749. /* write high bits */
  750. ide_outb(device, ATA_SECT_CNT, 0);
  751. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  752. #ifdef CONFIG_SYS_64BIT_LBA
  753. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  754. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  755. #else
  756. ide_outb(device, ATA_LBA_MID, 0);
  757. ide_outb(device, ATA_LBA_HIGH, 0);
  758. #endif
  759. }
  760. #endif
  761. ide_outb(device, ATA_SECT_CNT, 1);
  762. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  763. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  764. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  765. #ifdef CONFIG_LBA48
  766. if (lba48) {
  767. ide_outb(device, ATA_DEV_HD,
  768. ATA_LBA | ATA_DEVICE(device));
  769. ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
  770. } else
  771. #endif
  772. {
  773. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  774. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  775. ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
  776. }
  777. udelay(50);
  778. if (pwrsave) {
  779. /* may take up to 4 sec */
  780. c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
  781. pwrsave = 0;
  782. } else {
  783. /* can't take over 500 ms */
  784. c = ide_wait(device, IDE_TIME_OUT);
  785. }
  786. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  787. ATA_STAT_DRQ) {
  788. #if defined(CONFIG_SYS_64BIT_LBA)
  789. printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
  790. device, blknr, c);
  791. #else
  792. printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  793. device, (ulong) blknr, c);
  794. #endif
  795. break;
  796. }
  797. ide_input_data(device, buffer, ATA_SECTORWORDS);
  798. (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
  799. ++n;
  800. ++blknr;
  801. buffer += ATA_BLOCKSIZE;
  802. }
  803. IDE_READ_E:
  804. ide_led(DEVICE_LED(device), 0); /* LED off */
  805. return (n);
  806. }
  807. /* ------------------------------------------------------------------------- */
  808. ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
  809. {
  810. ulong n = 0;
  811. unsigned char c;
  812. #ifdef CONFIG_LBA48
  813. unsigned char lba48 = 0;
  814. if (blknr & 0x0000fffff0000000ULL) {
  815. /* more than 28 bits used, use 48bit mode */
  816. lba48 = 1;
  817. }
  818. #endif
  819. ide_led(DEVICE_LED(device), 1); /* LED on */
  820. /* Select device
  821. */
  822. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  823. while (blkcnt-- > 0) {
  824. c = ide_wait(device, IDE_TIME_OUT);
  825. if (c & ATA_STAT_BUSY) {
  826. printf("IDE read: device %d not ready\n", device);
  827. goto WR_OUT;
  828. }
  829. #ifdef CONFIG_LBA48
  830. if (lba48) {
  831. /* write high bits */
  832. ide_outb(device, ATA_SECT_CNT, 0);
  833. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  834. #ifdef CONFIG_SYS_64BIT_LBA
  835. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  836. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  837. #else
  838. ide_outb(device, ATA_LBA_MID, 0);
  839. ide_outb(device, ATA_LBA_HIGH, 0);
  840. #endif
  841. }
  842. #endif
  843. ide_outb(device, ATA_SECT_CNT, 1);
  844. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  845. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  846. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  847. #ifdef CONFIG_LBA48
  848. if (lba48) {
  849. ide_outb(device, ATA_DEV_HD,
  850. ATA_LBA | ATA_DEVICE(device));
  851. ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
  852. } else
  853. #endif
  854. {
  855. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  856. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  857. ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
  858. }
  859. udelay(50);
  860. /* can't take over 500 ms */
  861. c = ide_wait(device, IDE_TIME_OUT);
  862. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  863. ATA_STAT_DRQ) {
  864. #if defined(CONFIG_SYS_64BIT_LBA)
  865. printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
  866. device, blknr, c);
  867. #else
  868. printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  869. device, (ulong) blknr, c);
  870. #endif
  871. goto WR_OUT;
  872. }
  873. ide_output_data(device, buffer, ATA_SECTORWORDS);
  874. c = ide_inb(device, ATA_STATUS); /* clear IRQ */
  875. ++n;
  876. ++blknr;
  877. buffer += ATA_BLOCKSIZE;
  878. }
  879. WR_OUT:
  880. ide_led(DEVICE_LED(device), 0); /* LED off */
  881. return (n);
  882. }
  883. /* ------------------------------------------------------------------------- */
  884. /*
  885. * copy src to dest, skipping leading and trailing blanks and null
  886. * terminate the string
  887. * "len" is the size of available memory including the terminating '\0'
  888. */
  889. static void ident_cpy(unsigned char *dst, unsigned char *src,
  890. unsigned int len)
  891. {
  892. unsigned char *end, *last;
  893. last = dst;
  894. end = src + len - 1;
  895. /* reserve space for '\0' */
  896. if (len < 2)
  897. goto OUT;
  898. /* skip leading white space */
  899. while ((*src) && (src < end) && (*src == ' '))
  900. ++src;
  901. /* copy string, omitting trailing white space */
  902. while ((*src) && (src < end)) {
  903. *dst++ = *src;
  904. if (*src++ != ' ')
  905. last = dst;
  906. }
  907. OUT:
  908. *last = '\0';
  909. }
  910. /* ------------------------------------------------------------------------- */
  911. /*
  912. * Wait until Busy bit is off, or timeout (in ms)
  913. * Return last status
  914. */
  915. static uchar ide_wait(int dev, ulong t)
  916. {
  917. ulong delay = 10 * t; /* poll every 100 us */
  918. uchar c;
  919. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  920. udelay(100);
  921. if (delay-- == 0)
  922. break;
  923. }
  924. return (c);
  925. }
  926. /* ------------------------------------------------------------------------- */
  927. #ifdef CONFIG_IDE_RESET
  928. extern void ide_set_reset(int idereset);
  929. static void ide_reset(void)
  930. {
  931. int i;
  932. curr_device = -1;
  933. for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
  934. ide_bus_ok[i] = 0;
  935. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
  936. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  937. ide_set_reset(1); /* assert reset */
  938. /* the reset signal shall be asserted for et least 25 us */
  939. udelay(25);
  940. WATCHDOG_RESET();
  941. /* de-assert RESET signal */
  942. ide_set_reset(0);
  943. /* wait 250 ms */
  944. for (i = 0; i < 250; ++i)
  945. udelay(1000);
  946. }
  947. #endif /* CONFIG_IDE_RESET */
  948. /* ------------------------------------------------------------------------- */
  949. #if defined(CONFIG_OF_IDE_FIXUP)
  950. int ide_device_present(int dev)
  951. {
  952. if (dev >= CONFIG_SYS_IDE_MAXBUS)
  953. return 0;
  954. return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
  955. }
  956. #endif
  957. /* ------------------------------------------------------------------------- */
  958. #ifdef CONFIG_ATAPI
  959. /****************************************************************************
  960. * ATAPI Support
  961. */
  962. void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  963. __attribute__ ((weak, alias("__ide_input_data_shorts")));
  964. void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  965. __attribute__ ((weak, alias("__ide_output_data_shorts")));
  966. #if defined(CONFIG_IDE_SWAP_IO)
  967. /* since ATAPI may use commands with not 4 bytes alligned length
  968. * we have our own transfer functions, 2 bytes alligned */
  969. void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  970. {
  971. ushort *dbuf;
  972. volatile ushort *pbuf;
  973. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  974. dbuf = (ushort *) sect_buf;
  975. debug("in output data shorts base for read is %lx\n",
  976. (unsigned long) pbuf);
  977. while (shorts--) {
  978. EIEIO;
  979. *pbuf = *dbuf++;
  980. }
  981. }
  982. void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  983. {
  984. ushort *dbuf;
  985. volatile ushort *pbuf;
  986. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  987. dbuf = (ushort *) sect_buf;
  988. debug("in input data shorts base for read is %lx\n",
  989. (unsigned long) pbuf);
  990. while (shorts--) {
  991. EIEIO;
  992. *dbuf++ = *pbuf;
  993. }
  994. }
  995. #else /* ! CONFIG_IDE_SWAP_IO */
  996. void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  997. {
  998. outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
  999. }
  1000. void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1001. {
  1002. insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
  1003. }
  1004. #endif /* CONFIG_IDE_SWAP_IO */
  1005. /*
  1006. * Wait until (Status & mask) == res, or timeout (in ms)
  1007. * Return last status
  1008. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1009. * and then they set their DRQ Bit
  1010. */
  1011. static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
  1012. {
  1013. ulong delay = 10 * t; /* poll every 100 us */
  1014. uchar c;
  1015. /* prevents to read the status before valid */
  1016. c = ide_inb(dev, ATA_DEV_CTL);
  1017. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  1018. /* break if error occurs (doesn't make sense to wait more) */
  1019. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
  1020. break;
  1021. udelay(100);
  1022. if (delay-- == 0)
  1023. break;
  1024. }
  1025. return (c);
  1026. }
  1027. /*
  1028. * issue an atapi command
  1029. */
  1030. unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
  1031. unsigned char *buffer, int buflen)
  1032. {
  1033. unsigned char c, err, mask, res;
  1034. int n;
  1035. ide_led(DEVICE_LED(device), 1); /* LED on */
  1036. /* Select device
  1037. */
  1038. mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
  1039. res = 0;
  1040. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1041. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1042. if ((c & mask) != res) {
  1043. printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
  1044. c);
  1045. err = 0xFF;
  1046. goto AI_OUT;
  1047. }
  1048. /* write taskfile */
  1049. ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1050. ide_outb(device, ATA_SECT_CNT, 0);
  1051. ide_outb(device, ATA_SECT_NUM, 0);
  1052. ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
  1053. ide_outb(device, ATA_CYL_HIGH,
  1054. (unsigned char) ((buflen >> 8) & 0xFF));
  1055. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1056. ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1057. udelay(50);
  1058. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  1059. res = ATA_STAT_DRQ;
  1060. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1061. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1062. printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
  1063. device, c);
  1064. err = 0xFF;
  1065. goto AI_OUT;
  1066. }
  1067. /* write command block */
  1068. ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
  1069. /* ATAPI Command written wait for completition */
  1070. udelay(5000); /* device must set bsy */
  1071. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  1072. /*
  1073. * if no data wait for DRQ = 0 BSY = 0
  1074. * if data wait for DRQ = 1 BSY = 0
  1075. */
  1076. res = 0;
  1077. if (buflen)
  1078. res = ATA_STAT_DRQ;
  1079. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1080. if ((c & mask) != res) {
  1081. if (c & ATA_STAT_ERR) {
  1082. err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
  1083. debug("atapi_issue 1 returned sense key %X status %02X\n",
  1084. err, c);
  1085. } else {
  1086. printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
  1087. ccb[0], c);
  1088. err = 0xFF;
  1089. }
  1090. goto AI_OUT;
  1091. }
  1092. n = ide_inb(device, ATA_CYL_HIGH);
  1093. n <<= 8;
  1094. n += ide_inb(device, ATA_CYL_LOW);
  1095. if (n > buflen) {
  1096. printf("ERROR, transfer bytes %d requested only %d\n", n,
  1097. buflen);
  1098. err = 0xff;
  1099. goto AI_OUT;
  1100. }
  1101. if ((n == 0) && (buflen < 0)) {
  1102. printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
  1103. err = 0xff;
  1104. goto AI_OUT;
  1105. }
  1106. if (n != buflen) {
  1107. debug("WARNING, transfer bytes %d not equal with requested %d\n",
  1108. n, buflen);
  1109. }
  1110. if (n != 0) { /* data transfer */
  1111. debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
  1112. /* we transfer shorts */
  1113. n >>= 1;
  1114. /* ok now decide if it is an in or output */
  1115. if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
  1116. debug("Write to device\n");
  1117. ide_output_data_shorts(device,
  1118. (unsigned short *) buffer, n);
  1119. } else {
  1120. debug("Read from device @ %p shorts %d\n", buffer, n);
  1121. ide_input_data_shorts(device,
  1122. (unsigned short *) buffer, n);
  1123. }
  1124. }
  1125. udelay(5000); /* seems that some CD ROMs need this... */
  1126. mask = ATA_STAT_BUSY | ATA_STAT_ERR;
  1127. res = 0;
  1128. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1129. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1130. err = (ide_inb(device, ATA_ERROR_REG) >> 4);
  1131. debug("atapi_issue 2 returned sense key %X status %X\n", err,
  1132. c);
  1133. } else {
  1134. err = 0;
  1135. }
  1136. AI_OUT:
  1137. ide_led(DEVICE_LED(device), 0); /* LED off */
  1138. return (err);
  1139. }
  1140. /*
  1141. * sending the command to atapi_issue. If an status other than good
  1142. * returns, an request_sense will be issued
  1143. */
  1144. #define ATAPI_DRIVE_NOT_READY 100
  1145. #define ATAPI_UNIT_ATTN 10
  1146. unsigned char atapi_issue_autoreq(int device,
  1147. unsigned char *ccb,
  1148. int ccblen,
  1149. unsigned char *buffer, int buflen)
  1150. {
  1151. unsigned char sense_data[18], sense_ccb[12];
  1152. unsigned char res, key, asc, ascq;
  1153. int notready, unitattn;
  1154. unitattn = ATAPI_UNIT_ATTN;
  1155. notready = ATAPI_DRIVE_NOT_READY;
  1156. retry:
  1157. res = atapi_issue(device, ccb, ccblen, buffer, buflen);
  1158. if (res == 0)
  1159. return 0; /* Ok */
  1160. if (res == 0xFF)
  1161. return 0xFF; /* error */
  1162. debug("(auto_req)atapi_issue returned sense key %X\n", res);
  1163. memset(sense_ccb, 0, sizeof(sense_ccb));
  1164. memset(sense_data, 0, sizeof(sense_data));
  1165. sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
  1166. sense_ccb[4] = 18; /* allocation Length */
  1167. res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
  1168. key = (sense_data[2] & 0xF);
  1169. asc = (sense_data[12]);
  1170. ascq = (sense_data[13]);
  1171. debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
  1172. debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1173. sense_data[0], key, asc, ascq);
  1174. if ((key == 0))
  1175. return 0; /* ok device ready */
  1176. if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
  1177. if (unitattn-- > 0) {
  1178. udelay(200 * 1000);
  1179. goto retry;
  1180. }
  1181. printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
  1182. goto error;
  1183. }
  1184. if ((asc == 0x4) && (ascq == 0x1)) {
  1185. /* not ready, but will be ready soon */
  1186. if (notready-- > 0) {
  1187. udelay(200 * 1000);
  1188. goto retry;
  1189. }
  1190. printf("Drive not ready, tried %d times\n",
  1191. ATAPI_DRIVE_NOT_READY);
  1192. goto error;
  1193. }
  1194. if (asc == 0x3a) {
  1195. debug("Media not present\n");
  1196. goto error;
  1197. }
  1198. printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
  1199. ascq);
  1200. error:
  1201. debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
  1202. return (0xFF);
  1203. }
  1204. static void atapi_inquiry(block_dev_desc_t *dev_desc)
  1205. {
  1206. unsigned char ccb[12]; /* Command descriptor block */
  1207. unsigned char iobuf[64]; /* temp buf */
  1208. unsigned char c;
  1209. int device;
  1210. device = dev_desc->dev;
  1211. dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
  1212. dev_desc->block_read = atapi_read;
  1213. memset(ccb, 0, sizeof(ccb));
  1214. memset(iobuf, 0, sizeof(iobuf));
  1215. ccb[0] = ATAPI_CMD_INQUIRY;
  1216. ccb[4] = 40; /* allocation Legnth */
  1217. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
  1218. debug("ATAPI_CMD_INQUIRY returned %x\n", c);
  1219. if (c != 0)
  1220. return;
  1221. /* copy device ident strings */
  1222. ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
  1223. ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
  1224. ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
  1225. dev_desc->lun = 0;
  1226. dev_desc->lba = 0;
  1227. dev_desc->blksz = 0;
  1228. dev_desc->type = iobuf[0] & 0x1f;
  1229. if ((iobuf[1] & 0x80) == 0x80)
  1230. dev_desc->removable = 1;
  1231. else
  1232. dev_desc->removable = 0;
  1233. memset(ccb, 0, sizeof(ccb));
  1234. memset(iobuf, 0, sizeof(iobuf));
  1235. ccb[0] = ATAPI_CMD_START_STOP;
  1236. ccb[4] = 0x03; /* start */
  1237. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
  1238. debug("ATAPI_CMD_START_STOP returned %x\n", c);
  1239. if (c != 0)
  1240. return;
  1241. memset(ccb, 0, sizeof(ccb));
  1242. memset(iobuf, 0, sizeof(iobuf));
  1243. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
  1244. debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
  1245. if (c != 0)
  1246. return;
  1247. memset(ccb, 0, sizeof(ccb));
  1248. memset(iobuf, 0, sizeof(iobuf));
  1249. ccb[0] = ATAPI_CMD_READ_CAP;
  1250. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
  1251. debug("ATAPI_CMD_READ_CAP returned %x\n", c);
  1252. if (c != 0)
  1253. return;
  1254. debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1255. iobuf[0], iobuf[1], iobuf[2], iobuf[3],
  1256. iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
  1257. dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
  1258. ((unsigned long) iobuf[1] << 16) +
  1259. ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
  1260. dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
  1261. ((unsigned long) iobuf[5] << 16) +
  1262. ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
  1263. #ifdef CONFIG_LBA48
  1264. /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
  1265. dev_desc->lba48 = 0;
  1266. #endif
  1267. return;
  1268. }
  1269. /*
  1270. * atapi_read:
  1271. * we transfer only one block per command, since the multiple DRQ per
  1272. * command is not yet implemented
  1273. */
  1274. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1275. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1276. #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
  1277. ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1278. {
  1279. ulong n = 0;
  1280. unsigned char ccb[12]; /* Command descriptor block */
  1281. ulong cnt;
  1282. debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1283. device, blknr, blkcnt, (ulong) buffer);
  1284. do {
  1285. if (blkcnt > ATAPI_READ_MAX_BLOCK)
  1286. cnt = ATAPI_READ_MAX_BLOCK;
  1287. else
  1288. cnt = blkcnt;
  1289. ccb[0] = ATAPI_CMD_READ_12;
  1290. ccb[1] = 0; /* reserved */
  1291. ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
  1292. ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
  1293. ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
  1294. ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
  1295. ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
  1296. ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
  1297. ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
  1298. ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
  1299. ccb[10] = 0; /* reserved */
  1300. ccb[11] = 0; /* reserved */
  1301. if (atapi_issue_autoreq(device, ccb, 12,
  1302. (unsigned char *) buffer,
  1303. cnt * ATAPI_READ_BLOCK_SIZE)
  1304. == 0xFF) {
  1305. return (n);
  1306. }
  1307. n += cnt;
  1308. blkcnt -= cnt;
  1309. blknr += cnt;
  1310. buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
  1311. } while (blkcnt > 0);
  1312. return (n);
  1313. }
  1314. /* ------------------------------------------------------------------------- */
  1315. #endif /* CONFIG_ATAPI */
  1316. U_BOOT_CMD(ide, 5, 1, do_ide,
  1317. "IDE sub-system",
  1318. "reset - reset IDE controller\n"
  1319. "ide info - show available IDE devices\n"
  1320. "ide device [dev] - show or set current device\n"
  1321. "ide part [dev] - print partition table of one or all IDE devices\n"
  1322. "ide read addr blk# cnt\n"
  1323. "ide write addr blk# cnt - read/write `cnt'"
  1324. " blocks starting at block `blk#'\n"
  1325. " to/from memory address `addr'");
  1326. U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
  1327. "boot from IDE device", "loadAddr dev:part");