cmd_ide.c 47 KB

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