cmd_ide.c 40 KB

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