cmd_ide.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  1. /*
  2. * (C) Copyright 2000-2002
  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. #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
  34. # include <pcmcia.h>
  35. #endif
  36. #ifdef CONFIG_8xx
  37. # include <mpc8xx.h>
  38. #endif
  39. #include <ide.h>
  40. #include <ata.h>
  41. #include <cmd_ide.h>
  42. #include <cmd_disk.h>
  43. #ifdef CONFIG_STATUS_LED
  44. # include <status_led.h>
  45. #endif
  46. #ifdef __I386__
  47. #include <asm/io.h>
  48. #endif
  49. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  50. # include <status_led.h>
  51. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  52. #else
  53. # define SHOW_BOOT_PROGRESS(arg)
  54. #endif
  55. #undef IDE_DEBUG
  56. #ifdef IDE_DEBUG
  57. #define PRINTF(fmt,args...) printf (fmt ,##args)
  58. #else
  59. #define PRINTF(fmt,args...)
  60. #endif
  61. #if (CONFIG_COMMANDS & CFG_CMD_IDE)
  62. /* Timings for IDE Interface
  63. *
  64. * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
  65. * 70 165 30 PIO-Mode 0, [ns]
  66. * 4 9 2 [Cycles]
  67. * 50 125 20 PIO-Mode 1, [ns]
  68. * 3 7 2 [Cycles]
  69. * 30 100 15 PIO-Mode 2, [ns]
  70. * 2 6 1 [Cycles]
  71. * 30 80 10 PIO-Mode 3, [ns]
  72. * 2 5 1 [Cycles]
  73. * 25 70 10 PIO-Mode 4, [ns]
  74. * 2 4 1 [Cycles]
  75. */
  76. const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
  77. {
  78. /* Setup Length Hold */
  79. { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
  80. { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
  81. { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
  82. { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
  83. { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
  84. };
  85. static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
  86. #ifndef CFG_PIO_MODE
  87. #define CFG_PIO_MODE 0 /* use a relaxed default */
  88. #endif
  89. static int pio_mode = CFG_PIO_MODE;
  90. /* Make clock cycles and always round up */
  91. #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
  92. /* ------------------------------------------------------------------------- */
  93. /* Current I/O Device */
  94. static int curr_device = -1;
  95. /* Current offset for IDE0 / IDE1 bus access */
  96. ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
  97. #if defined(CFG_ATA_IDE0_OFFSET)
  98. CFG_ATA_IDE0_OFFSET,
  99. #endif
  100. #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
  101. CFG_ATA_IDE1_OFFSET,
  102. #endif
  103. };
  104. #ifdef __PPC__
  105. #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
  106. #endif
  107. static int ide_bus_ok[CFG_IDE_MAXBUS];
  108. static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
  109. /* ------------------------------------------------------------------------- */
  110. #ifdef CONFIG_IDE_LED
  111. static void ide_led (uchar led, uchar status);
  112. #else
  113. #define ide_led(a,b) /* dummy */
  114. #endif
  115. #ifdef CONFIG_IDE_RESET
  116. static void ide_reset (void);
  117. #else
  118. #define ide_reset() /* dummy */
  119. #endif
  120. static void ide_ident (block_dev_desc_t *dev_desc);
  121. static uchar ide_wait (int dev, ulong t);
  122. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  123. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  124. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  125. static void __inline__ ide_outb(int dev, int port, unsigned char val);
  126. static unsigned char __inline__ ide_inb(int dev, int port);
  127. #ifdef __PPC__
  128. static void input_swap_data(int dev, ulong *sect_buf, int words);
  129. #endif
  130. static void input_data(int dev, ulong *sect_buf, int words);
  131. static void output_data(int dev, ulong *sect_buf, int words);
  132. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  133. #ifdef CONFIG_ATAPI
  134. static void atapi_inquiry(block_dev_desc_t *dev_desc);
  135. ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
  136. #endif
  137. #ifdef CONFIG_IDE_8xx_DIRECT
  138. static void set_pcmcia_timing (int pmode);
  139. #else
  140. #define set_pcmcia_timing(a) /* dummy */
  141. #endif
  142. /* ------------------------------------------------------------------------- */
  143. int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  144. {
  145. int rcode = 0;
  146. switch (argc) {
  147. case 0:
  148. case 1:
  149. printf ("Usage:\n%s\n", cmdtp->usage);
  150. return 1;
  151. case 2:
  152. if (strncmp(argv[1],"res",3) == 0) {
  153. puts ("\nReset IDE"
  154. #ifdef CONFIG_IDE_8xx_DIRECT
  155. " on PCMCIA " PCMCIA_SLOT_MSG
  156. #endif
  157. ": ");
  158. ide_init ();
  159. return 0;
  160. } else if (strncmp(argv[1],"inf",3) == 0) {
  161. int i;
  162. putc ('\n');
  163. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  164. if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
  165. continue; /* list only known devices */
  166. printf ("IDE device %d: ", i);
  167. dev_print(&ide_dev_desc[i]);
  168. }
  169. return 0;
  170. } else if (strncmp(argv[1],"dev",3) == 0) {
  171. if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
  172. puts ("\nno IDE devices available\n");
  173. return 1;
  174. }
  175. printf ("\nIDE device %d: ", curr_device);
  176. dev_print(&ide_dev_desc[curr_device]);
  177. return 0;
  178. } else if (strncmp(argv[1],"part",4) == 0) {
  179. int dev, ok;
  180. for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
  181. if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
  182. ++ok;
  183. if (dev)
  184. putc ('\n');
  185. print_part(&ide_dev_desc[dev]);
  186. }
  187. }
  188. if (!ok) {
  189. puts ("\nno IDE devices available\n");
  190. rcode ++;
  191. }
  192. return rcode;
  193. }
  194. printf ("Usage:\n%s\n", cmdtp->usage);
  195. return 1;
  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 >= CFG_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. }
  209. curr_device = dev;
  210. puts ("... is now current device\n");
  211. return 0;
  212. } else if (strncmp(argv[1],"part",4) == 0) {
  213. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  214. if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
  215. print_part(&ide_dev_desc[dev]);
  216. } else {
  217. printf ("\nIDE device %d not available\n", dev);
  218. rcode = 1;
  219. }
  220. return rcode;
  221. #if 0
  222. } else if (strncmp(argv[1],"pio",4) == 0) {
  223. int mode = (int)simple_strtoul(argv[2], NULL, 10);
  224. if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
  225. puts ("\nSetting ");
  226. pio_mode = mode;
  227. ide_init ();
  228. } else {
  229. printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
  230. mode, IDE_MAX_PIO_MODE);
  231. }
  232. return;
  233. #endif
  234. }
  235. printf ("Usage:\n%s\n", cmdtp->usage);
  236. return 1;
  237. default:
  238. /* at least 4 args */
  239. if (strcmp(argv[1],"read") == 0) {
  240. ulong addr = simple_strtoul(argv[2], NULL, 16);
  241. ulong blk = simple_strtoul(argv[3], NULL, 16);
  242. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  243. ulong n;
  244. printf ("\nIDE read: device %d block # %ld, count %ld ... ",
  245. curr_device, blk, cnt);
  246. n = ide_dev_desc[curr_device].block_read (curr_device,
  247. blk, cnt,
  248. (ulong *)addr);
  249. /* flush cache after read */
  250. flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
  251. printf ("%ld blocks read: %s\n",
  252. n, (n==cnt) ? "OK" : "ERROR");
  253. if (n==cnt) {
  254. return 0;
  255. } else {
  256. return 1;
  257. }
  258. } else if (strcmp(argv[1],"write") == 0) {
  259. ulong addr = simple_strtoul(argv[2], NULL, 16);
  260. ulong blk = simple_strtoul(argv[3], NULL, 16);
  261. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  262. ulong n;
  263. printf ("\nIDE write: device %d block # %ld, count %ld ... ",
  264. curr_device, blk, cnt);
  265. n = ide_write (curr_device, blk, cnt, (ulong *)addr);
  266. printf ("%ld blocks written: %s\n",
  267. n, (n==cnt) ? "OK" : "ERROR");
  268. if (n==cnt) {
  269. return 0;
  270. } else {
  271. return 1;
  272. }
  273. } else {
  274. printf ("Usage:\n%s\n", cmdtp->usage);
  275. rcode = 1;
  276. }
  277. return rcode;
  278. }
  279. }
  280. int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  281. {
  282. char *boot_device = NULL;
  283. char *ep;
  284. int dev, part = 0;
  285. ulong cnt;
  286. ulong addr;
  287. disk_partition_t info;
  288. image_header_t *hdr;
  289. int rcode = 0;
  290. switch (argc) {
  291. case 1:
  292. addr = CFG_LOAD_ADDR;
  293. boot_device = getenv ("bootdevice");
  294. break;
  295. case 2:
  296. addr = simple_strtoul(argv[1], NULL, 16);
  297. boot_device = getenv ("bootdevice");
  298. break;
  299. case 3:
  300. addr = simple_strtoul(argv[1], NULL, 16);
  301. boot_device = argv[2];
  302. break;
  303. default:
  304. printf ("Usage:\n%s\n", cmdtp->usage);
  305. SHOW_BOOT_PROGRESS (-1);
  306. return 1;
  307. }
  308. if (!boot_device) {
  309. puts ("\n** No boot device **\n");
  310. SHOW_BOOT_PROGRESS (-1);
  311. return 1;
  312. }
  313. dev = simple_strtoul(boot_device, &ep, 16);
  314. if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
  315. printf ("\n** Device %d not available\n", dev);
  316. SHOW_BOOT_PROGRESS (-1);
  317. return 1;
  318. }
  319. if (*ep) {
  320. if (*ep != ':') {
  321. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  322. SHOW_BOOT_PROGRESS (-1);
  323. return 1;
  324. }
  325. part = simple_strtoul(++ep, NULL, 16);
  326. }
  327. if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
  328. SHOW_BOOT_PROGRESS (-1);
  329. return 1;
  330. }
  331. if (strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
  332. printf ("\n** Invalid partition type \"%.32s\""
  333. " (expect \"" BOOT_PART_TYPE "\")\n",
  334. info.type);
  335. SHOW_BOOT_PROGRESS (-1);
  336. return 1;
  337. }
  338. printf ("\nLoading from IDE device %d, partition %d: "
  339. "Name: %.32s Type: %.32s\n",
  340. dev, part, info.name, info.type);
  341. PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
  342. info.start, info.size, info.blksz);
  343. if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
  344. printf ("** Read error on %d:%d\n", dev, part);
  345. SHOW_BOOT_PROGRESS (-1);
  346. return 1;
  347. }
  348. hdr = (image_header_t *)addr;
  349. if (ntohl(hdr->ih_magic) == IH_MAGIC) {
  350. print_image_hdr (hdr);
  351. cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
  352. cnt += info.blksz - 1;
  353. cnt /= info.blksz;
  354. cnt -= 1;
  355. } else {
  356. printf("\n** Bad Magic Number **\n");
  357. SHOW_BOOT_PROGRESS (-1);
  358. return 1;
  359. }
  360. if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
  361. (ulong *)(addr+info.blksz)) != cnt) {
  362. printf ("** Read error on %d:%d\n", dev, part);
  363. SHOW_BOOT_PROGRESS (-1);
  364. return 1;
  365. }
  366. /* Loading ok, update default load address */
  367. load_addr = addr;
  368. /* Check if we should attempt an auto-start */
  369. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  370. char *local_args[2];
  371. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  372. local_args[0] = argv[0];
  373. local_args[1] = NULL;
  374. printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
  375. do_bootm (cmdtp, 0, 1, local_args);
  376. rcode = 1;
  377. }
  378. return rcode;
  379. }
  380. /* ------------------------------------------------------------------------- */
  381. void ide_init (void)
  382. {
  383. DECLARE_GLOBAL_DATA_PTR;
  384. #ifdef CONFIG_IDE_8xx_DIRECT
  385. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  386. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  387. #endif
  388. unsigned char c;
  389. int i, bus;
  390. #ifdef CONFIG_IDE_8xx_PCCARD
  391. extern int pcmcia_on (void);
  392. WATCHDOG_RESET();
  393. /* initialize the PCMCIA IDE adapter card */
  394. if (pcmcia_on())
  395. return;
  396. udelay (1000000); /* 1 s */
  397. #endif /* CONFIG_IDE_8xx_PCCARD */
  398. WATCHDOG_RESET();
  399. /* Initialize PIO timing tables */
  400. for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
  401. pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
  402. gd->bus_clk);
  403. pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
  404. gd->bus_clk);
  405. pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
  406. gd->bus_clk);
  407. PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
  408. " len=%3d ns/%d clk"
  409. " hold=%2d ns/%d clk\n",
  410. i,
  411. pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
  412. pio_config_ns[i].t_length, pio_config_clk[i].t_length,
  413. pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
  414. }
  415. /* Reset the IDE just to be sure.
  416. * Light LED's to show
  417. */
  418. ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  419. ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
  420. #ifdef CONFIG_IDE_8xx_DIRECT
  421. /* PCMCIA / IDE initialization for common mem space */
  422. pcmp->pcmc_pgcrb = 0;
  423. #endif
  424. /* start in PIO mode 0 - most relaxed timings */
  425. pio_mode = 0;
  426. set_pcmcia_timing (pio_mode);
  427. /*
  428. * Wait for IDE to get ready.
  429. * According to spec, this can take up to 31 seconds!
  430. */
  431. for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
  432. int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
  433. printf ("Bus %d: ", bus);
  434. ide_bus_ok[bus] = 0;
  435. /* Select device
  436. */
  437. udelay (100000); /* 100 ms */
  438. ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  439. udelay (100000); /* 100 ms */
  440. i = 0;
  441. do {
  442. udelay (10000); /* 10 ms */
  443. c = ide_inb (dev, ATA_STATUS);
  444. i++;
  445. if (i > (ATA_RESET_TIME * 100)) {
  446. puts ("** Timeout **\n");
  447. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  448. return;
  449. }
  450. if ((i >= 100) && ((i%100)==0)) {
  451. putc ('.');
  452. }
  453. } while (c & ATA_STAT_BUSY);
  454. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  455. puts ("not available ");
  456. PRINTF ("Status = 0x%02X ", c);
  457. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  458. } else if ((c & ATA_STAT_READY) == 0) {
  459. puts ("not available ");
  460. PRINTF ("Status = 0x%02X ", c);
  461. #endif
  462. } else {
  463. puts ("OK ");
  464. ide_bus_ok[bus] = 1;
  465. }
  466. WATCHDOG_RESET();
  467. }
  468. putc ('\n');
  469. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  470. curr_device = -1;
  471. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  472. #ifdef CONFIG_IDE_LED
  473. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  474. #endif
  475. ide_dev_desc[i].if_type=IF_TYPE_IDE;
  476. ide_dev_desc[i].dev=i;
  477. ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
  478. ide_dev_desc[i].blksz=0;
  479. ide_dev_desc[i].lba=0;
  480. ide_dev_desc[i].block_read=ide_read;
  481. if (!ide_bus_ok[IDE_BUS(i)])
  482. continue;
  483. ide_led (led, 1); /* LED on */
  484. ide_ident(&ide_dev_desc[i]);
  485. ide_led (led, 0); /* LED off */
  486. dev_print(&ide_dev_desc[i]);
  487. /* ide_print (i); */
  488. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  489. init_part (&ide_dev_desc[i]); /* initialize partition type */
  490. if (curr_device < 0)
  491. curr_device = i;
  492. }
  493. }
  494. WATCHDOG_RESET();
  495. }
  496. /* ------------------------------------------------------------------------- */
  497. block_dev_desc_t * ide_get_dev(int dev)
  498. {
  499. return ((block_dev_desc_t *)&ide_dev_desc[dev]);
  500. }
  501. #ifdef CONFIG_IDE_8xx_DIRECT
  502. static void
  503. set_pcmcia_timing (int pmode)
  504. {
  505. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  506. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  507. ulong timings;
  508. PRINTF ("Set timing for PIO Mode %d\n", pmode);
  509. timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
  510. | PCMCIA_SST(pio_config_clk[pmode].t_setup)
  511. | PCMCIA_SL (pio_config_clk[pmode].t_length)
  512. ;
  513. /* IDE 0
  514. */
  515. pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
  516. pcmp->pcmc_por0 = CFG_PCMCIA_POR0
  517. #if (CFG_PCMCIA_POR0 != 0)
  518. | timings
  519. #endif
  520. ;
  521. PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
  522. pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
  523. pcmp->pcmc_por1 = CFG_PCMCIA_POR1
  524. #if (CFG_PCMCIA_POR1 != 0)
  525. | timings
  526. #endif
  527. ;
  528. PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
  529. pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
  530. pcmp->pcmc_por2 = CFG_PCMCIA_POR2
  531. #if (CFG_PCMCIA_POR2 != 0)
  532. | timings
  533. #endif
  534. ;
  535. PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
  536. pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
  537. pcmp->pcmc_por3 = CFG_PCMCIA_POR3
  538. #if (CFG_PCMCIA_POR3 != 0)
  539. | timings
  540. #endif
  541. ;
  542. PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
  543. /* IDE 1
  544. */
  545. pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
  546. pcmp->pcmc_por4 = CFG_PCMCIA_POR4
  547. #if (CFG_PCMCIA_POR4 != 0)
  548. | timings
  549. #endif
  550. ;
  551. PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
  552. pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
  553. pcmp->pcmc_por5 = CFG_PCMCIA_POR5
  554. #if (CFG_PCMCIA_POR5 != 0)
  555. | timings
  556. #endif
  557. ;
  558. PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
  559. pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
  560. pcmp->pcmc_por6 = CFG_PCMCIA_POR6
  561. #if (CFG_PCMCIA_POR6 != 0)
  562. | timings
  563. #endif
  564. ;
  565. PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
  566. pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
  567. pcmp->pcmc_por7 = CFG_PCMCIA_POR7
  568. #if (CFG_PCMCIA_POR7 != 0)
  569. | timings
  570. #endif
  571. ;
  572. PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
  573. }
  574. #endif /* CONFIG_IDE_8xx_DIRECT */
  575. /* ------------------------------------------------------------------------- */
  576. #ifdef __PPC__
  577. static void __inline__
  578. ide_outb(int dev, int port, unsigned char val)
  579. {
  580. /* Ensure I/O operations complete */
  581. __asm__ volatile("eieio");
  582. *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
  583. #if 0
  584. printf ("ide_outb: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
  585. #endif
  586. }
  587. #else /* ! __PPC__ */
  588. static void __inline__
  589. ide_outb(int dev, int port, unsigned char val)
  590. {
  591. outb(val, port);
  592. }
  593. #endif /* __PPC__ */
  594. #ifdef __PPC__
  595. static unsigned char __inline__
  596. ide_inb(int dev, int port)
  597. {
  598. uchar val;
  599. /* Ensure I/O operations complete */
  600. __asm__ volatile("eieio");
  601. val = *((uchar *)(ATA_CURR_BASE(dev)+port));
  602. #if 0
  603. printf ("ide_inb: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
  604. #endif
  605. return (val);
  606. }
  607. #else /* ! __PPC__ */
  608. static unsigned char __inline__
  609. ide_inb(int dev, int port)
  610. {
  611. return inb(port);
  612. }
  613. #endif /* __PPC__ */
  614. #ifdef __PPC__
  615. __inline__ unsigned ld_le16(const volatile unsigned short *addr)
  616. {
  617. unsigned val;
  618. __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r"(val) : "r"(addr), "m"(*addr));
  619. return val;
  620. }
  621. static void
  622. input_swap_data(int dev, ulong *sect_buf, int words)
  623. {
  624. volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  625. ushort *dbuf = (ushort *)sect_buf;
  626. while (words--) {
  627. *dbuf++ = ld_le16(pbuf);
  628. *dbuf++ = ld_le16(pbuf);
  629. }
  630. }
  631. #else /* ! __PPC__ */
  632. #define input_swap_data(x,y,z) input_data(x,y,z)
  633. #endif /* __PPC__ */
  634. #ifdef __PPC__
  635. static void
  636. output_data(int dev, ulong *sect_buf, int words)
  637. {
  638. ushort *dbuf;
  639. volatile ushort *pbuf;
  640. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  641. dbuf = (ushort *)sect_buf;
  642. while (words--) {
  643. __asm__ volatile ("eieio");
  644. *pbuf = *dbuf++;
  645. __asm__ volatile ("eieio");
  646. *pbuf = *dbuf++;
  647. }
  648. }
  649. #else /* ! __PPC__ */
  650. static void
  651. output_data(int dev, ulong *sect_buf, int words)
  652. {
  653. outsw(ATA_DATA_REG, sect_buf, words<<1);
  654. }
  655. #endif /* __PPC__ */
  656. #ifdef __PPC__
  657. static void
  658. input_data(int dev, ulong *sect_buf, int words)
  659. {
  660. ushort *dbuf;
  661. volatile ushort *pbuf;
  662. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  663. dbuf = (ushort *)sect_buf;
  664. while (words--) {
  665. __asm__ volatile ("eieio");
  666. *dbuf++ = *pbuf;
  667. __asm__ volatile ("eieio");
  668. *dbuf++ = *pbuf;
  669. }
  670. }
  671. #else /* ! __PPC__ */
  672. static void
  673. input_data(int dev, ulong *sect_buf, int words)
  674. {
  675. insw(ATA_DATA_REG, sect_buf, words << 1);
  676. }
  677. #endif /* __PPC__ */
  678. /* -------------------------------------------------------------------------
  679. */
  680. static void ide_ident (block_dev_desc_t *dev_desc)
  681. {
  682. ulong iobuf[ATA_SECTORWORDS];
  683. unsigned char c;
  684. hd_driveid_t *iop = (hd_driveid_t *)iobuf;
  685. #if 0
  686. int mode, cycle_time;
  687. #endif
  688. int device;
  689. device=dev_desc->dev;
  690. printf (" Device %d: ", device);
  691. ide_led (DEVICE_LED(device), 1); /* LED on */
  692. /* Select device
  693. */
  694. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  695. dev_desc->if_type=IF_TYPE_IDE;
  696. #ifdef CONFIG_ATAPI
  697. /* check signature */
  698. if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
  699. (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
  700. (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
  701. (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
  702. /* ATAPI Signature found */
  703. dev_desc->if_type=IF_TYPE_ATAPI;
  704. /* Start Ident Command
  705. */
  706. ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
  707. /*
  708. * Wait for completion - ATAPI devices need more time
  709. * to become ready
  710. */
  711. c = ide_wait (device, ATAPI_TIME_OUT);
  712. }
  713. else
  714. #endif
  715. {
  716. /* Start Ident Command
  717. */
  718. ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
  719. /* Wait for completion
  720. */
  721. c = ide_wait (device, IDE_TIME_OUT);
  722. }
  723. ide_led (DEVICE_LED(device), 0); /* LED off */
  724. if (((c & ATA_STAT_DRQ) == 0) ||
  725. ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
  726. dev_desc->type=DEV_TYPE_UNKNOWN;
  727. return;
  728. }
  729. input_swap_data (device, iobuf, ATA_SECTORWORDS);
  730. ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
  731. ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
  732. ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
  733. if ((iop->config & 0x0080)==0x0080)
  734. dev_desc->removable = 1;
  735. else
  736. dev_desc->removable = 0;
  737. #if 0
  738. /*
  739. * Drive PIO mode autoselection
  740. */
  741. mode = iop->tPIO;
  742. printf ("tPIO = 0x%02x = %d\n",mode, mode);
  743. if (mode > 2) { /* 2 is maximum allowed tPIO value */
  744. mode = 2;
  745. PRINTF ("Override tPIO -> 2\n");
  746. }
  747. if (iop->field_valid & 2) { /* drive implements ATA2? */
  748. PRINTF ("Drive implements ATA2\n");
  749. if (iop->capability & 8) { /* drive supports use_iordy? */
  750. cycle_time = iop->eide_pio_iordy;
  751. } else {
  752. cycle_time = iop->eide_pio;
  753. }
  754. PRINTF ("cycle time = %d\n", cycle_time);
  755. mode = 4;
  756. if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
  757. if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
  758. if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
  759. if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
  760. }
  761. printf ("PIO mode to use: PIO %d\n", mode);
  762. #endif /* 0 */
  763. #ifdef CONFIG_ATAPI
  764. if (dev_desc->if_type==IF_TYPE_ATAPI) {
  765. atapi_inquiry(dev_desc);
  766. return;
  767. }
  768. #endif /* CONFIG_ATAPI */
  769. /* swap shorts */
  770. dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
  771. /* assuming HD */
  772. dev_desc->type=DEV_TYPE_HARDDISK;
  773. dev_desc->blksz=ATA_BLOCKSIZE;
  774. dev_desc->lun=0; /* just to fill something in... */
  775. #if 0 /* only used to test the powersaving mode,
  776. * if enabled, the drive goes after 5 sec
  777. * in standby mode */
  778. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  779. c = ide_wait (device, IDE_TIME_OUT);
  780. ide_outb (device, ATA_SECT_CNT, 1);
  781. ide_outb (device, ATA_LBA_LOW, 0);
  782. ide_outb (device, ATA_LBA_MID, 0);
  783. ide_outb (device, ATA_LBA_HIGH, 0);
  784. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  785. ATA_DEVICE(device));
  786. ide_outb (device, ATA_COMMAND, 0xe3);
  787. udelay (50);
  788. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  789. #endif
  790. }
  791. /* ------------------------------------------------------------------------- */
  792. ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  793. {
  794. ulong n = 0;
  795. unsigned char c;
  796. unsigned char pwrsave=0; /* power save */
  797. PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
  798. device, blknr, blkcnt, (ulong)buffer);
  799. ide_led (DEVICE_LED(device), 1); /* LED on */
  800. /* Select device
  801. */
  802. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  803. c = ide_wait (device, IDE_TIME_OUT);
  804. if (c & ATA_STAT_BUSY) {
  805. printf ("IDE read: device %d not ready\n", device);
  806. goto IDE_READ_E;
  807. }
  808. /* first check if the drive is in Powersaving mode, if yes,
  809. * increase the timeout value */
  810. ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  811. udelay (50);
  812. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  813. if (c & ATA_STAT_BUSY) {
  814. printf ("IDE read: device %d not ready\n", device);
  815. goto IDE_READ_E;
  816. }
  817. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  818. printf ("No Powersaving mode %X\n", c);
  819. } else {
  820. c = ide_inb(device,ATA_SECT_CNT);
  821. PRINTF("Powersaving %02X\n",c);
  822. if(c==0)
  823. pwrsave=1;
  824. }
  825. while (blkcnt-- > 0) {
  826. c = ide_wait (device, IDE_TIME_OUT);
  827. if (c & ATA_STAT_BUSY) {
  828. printf ("IDE read: device %d not ready\n", device);
  829. break;
  830. }
  831. ide_outb (device, ATA_SECT_CNT, 1);
  832. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  833. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  834. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  835. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  836. ATA_DEVICE(device) |
  837. ((blknr >> 24) & 0xF) );
  838. ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
  839. udelay (50);
  840. if(pwrsave) {
  841. c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
  842. pwrsave=0;
  843. } else {
  844. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  845. }
  846. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  847. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  848. device, blknr, c);
  849. break;
  850. }
  851. input_data (device, buffer, ATA_SECTORWORDS);
  852. (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
  853. ++n;
  854. ++blknr;
  855. buffer += ATA_SECTORWORDS;
  856. }
  857. IDE_READ_E:
  858. ide_led (DEVICE_LED(device), 0); /* LED off */
  859. return (n);
  860. }
  861. /* ------------------------------------------------------------------------- */
  862. ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  863. {
  864. ulong n = 0;
  865. unsigned char c;
  866. ide_led (DEVICE_LED(device), 1); /* LED on */
  867. /* Select device
  868. */
  869. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  870. while (blkcnt-- > 0) {
  871. c = ide_wait (device, IDE_TIME_OUT);
  872. if (c & ATA_STAT_BUSY) {
  873. printf ("IDE read: device %d not ready\n", device);
  874. goto WR_OUT;
  875. }
  876. ide_outb (device, ATA_SECT_CNT, 1);
  877. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  878. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  879. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  880. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  881. ATA_DEVICE(device) |
  882. ((blknr >> 24) & 0xF) );
  883. ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
  884. udelay (50);
  885. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  886. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  887. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  888. device, blknr, c);
  889. goto WR_OUT;
  890. }
  891. output_data (device, buffer, ATA_SECTORWORDS);
  892. c = ide_inb (device, ATA_STATUS); /* clear IRQ */
  893. ++n;
  894. ++blknr;
  895. buffer += ATA_SECTORWORDS;
  896. }
  897. WR_OUT:
  898. ide_led (DEVICE_LED(device), 0); /* LED off */
  899. return (n);
  900. }
  901. /* ------------------------------------------------------------------------- */
  902. /*
  903. * copy src to dest, skipping leading and trailing blanks and null
  904. * terminate the string
  905. */
  906. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
  907. {
  908. int start,end;
  909. start=0;
  910. while (start<len) {
  911. if (src[start]!=' ')
  912. break;
  913. start++;
  914. }
  915. end=len-1;
  916. while (end>start) {
  917. if (src[end]!=' ')
  918. break;
  919. end--;
  920. }
  921. for ( ; start<=end; start++) {
  922. *dest++=src[start];
  923. }
  924. *dest='\0';
  925. }
  926. /* ------------------------------------------------------------------------- */
  927. /*
  928. * Wait until Busy bit is off, or timeout (in ms)
  929. * Return last status
  930. */
  931. static uchar ide_wait (int dev, ulong t)
  932. {
  933. ulong delay = 10 * t; /* poll every 100 us */
  934. uchar c;
  935. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  936. udelay (100);
  937. if (delay-- == 0) {
  938. break;
  939. }
  940. }
  941. return (c);
  942. }
  943. /* ------------------------------------------------------------------------- */
  944. #ifdef CONFIG_IDE_RESET
  945. extern void ide_set_reset(int idereset);
  946. static void ide_reset (void)
  947. {
  948. #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
  949. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  950. #endif
  951. int i;
  952. curr_device = -1;
  953. for (i=0; i<CFG_IDE_MAXBUS; ++i)
  954. ide_bus_ok[i] = 0;
  955. for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
  956. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  957. ide_set_reset (1); /* assert reset */
  958. WATCHDOG_RESET();
  959. #ifdef CFG_PB_12V_ENABLE
  960. immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
  961. immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
  962. immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
  963. immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
  964. /* wait 500 ms for the voltage to stabilize
  965. */
  966. for (i=0; i<500; ++i) {
  967. udelay (1000);
  968. }
  969. immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
  970. #endif /* CFG_PB_12V_ENABLE */
  971. #ifdef CFG_PB_IDE_MOTOR
  972. /* configure IDE Motor voltage monitor pin as input */
  973. immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
  974. immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
  975. immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
  976. /* wait up to 1 s for the motor voltage to stabilize
  977. */
  978. for (i=0; i<1000; ++i) {
  979. if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
  980. break;
  981. }
  982. udelay (1000);
  983. }
  984. if (i == 1000) { /* Timeout */
  985. printf ("\nWarning: 5V for IDE Motor missing\n");
  986. # ifdef CONFIG_STATUS_LED
  987. # ifdef STATUS_LED_YELLOW
  988. status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
  989. # endif
  990. # ifdef STATUS_LED_GREEN
  991. status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
  992. # endif
  993. # endif /* CONFIG_STATUS_LED */
  994. }
  995. #endif /* CFG_PB_IDE_MOTOR */
  996. WATCHDOG_RESET();
  997. /* de-assert RESET signal */
  998. ide_set_reset(0);
  999. /* wait 250 ms */
  1000. for (i=0; i<250; ++i) {
  1001. udelay (1000);
  1002. }
  1003. }
  1004. #endif /* CONFIG_IDE_RESET */
  1005. /* ------------------------------------------------------------------------- */
  1006. #ifdef CONFIG_IDE_LED
  1007. static uchar led_buffer = 0; /* Buffer for current LED status */
  1008. static void ide_led (uchar led, uchar status)
  1009. {
  1010. uchar *led_port = LED_PORT;
  1011. if (status) { /* switch LED on */
  1012. led_buffer |= led;
  1013. } else { /* switch LED off */
  1014. led_buffer &= ~led;
  1015. }
  1016. *led_port = led_buffer;
  1017. }
  1018. #endif /* CONFIG_IDE_LED */
  1019. /* ------------------------------------------------------------------------- */
  1020. #ifdef CONFIG_ATAPI
  1021. /****************************************************************************
  1022. * ATAPI Support
  1023. */
  1024. #undef ATAPI_DEBUG
  1025. #ifdef ATAPI_DEBUG
  1026. #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
  1027. #else
  1028. #define AT_PRINTF(fmt,args...)
  1029. #endif
  1030. #ifdef __PPC__
  1031. /* since ATAPI may use commands with not 4 bytes alligned length
  1032. * we have our own transfer functions, 2 bytes alligned */
  1033. static void
  1034. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1035. {
  1036. ushort *dbuf;
  1037. volatile ushort *pbuf;
  1038. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1039. dbuf = (ushort *)sect_buf;
  1040. while (shorts--) {
  1041. __asm__ volatile ("eieio");
  1042. *pbuf = *dbuf++;
  1043. }
  1044. }
  1045. static void
  1046. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1047. {
  1048. ushort *dbuf;
  1049. volatile ushort *pbuf;
  1050. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1051. dbuf = (ushort *)sect_buf;
  1052. while (shorts--) {
  1053. __asm__ volatile ("eieio");
  1054. *dbuf++ = *pbuf;
  1055. }
  1056. }
  1057. #else /* ! __PPC__ */
  1058. static void
  1059. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1060. {
  1061. outsw(ATA_DATA_REG, sect_buf, shorts);
  1062. }
  1063. static void
  1064. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1065. {
  1066. insw(ATA_DATA_REG, sect_buf, shorts);
  1067. }
  1068. #endif /* __PPC__ */
  1069. /*
  1070. * Wait until (Status & mask) == res, or timeout (in ms)
  1071. * Return last status
  1072. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1073. * and then they set their DRQ Bit
  1074. */
  1075. static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
  1076. {
  1077. ulong delay = 10 * t; /* poll every 100 us */
  1078. uchar c;
  1079. c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
  1080. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  1081. /* break if error occurs (doesn't make sense to wait more) */
  1082. if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
  1083. break;
  1084. udelay (100);
  1085. if (delay-- == 0) {
  1086. break;
  1087. }
  1088. }
  1089. return (c);
  1090. }
  1091. /*
  1092. * issue an atapi command
  1093. */
  1094. unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
  1095. {
  1096. unsigned char c,err,mask,res;
  1097. int n;
  1098. ide_led (DEVICE_LED(device), 1); /* LED on */
  1099. /* Select device
  1100. */
  1101. mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
  1102. res = 0;
  1103. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1104. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1105. if ((c & mask) != res) {
  1106. printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
  1107. err=0xFF;
  1108. goto AI_OUT;
  1109. }
  1110. /* write taskfile */
  1111. ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1112. ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
  1113. ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen<<8) & 0xFF));
  1114. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1115. ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1116. udelay (50);
  1117. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1118. res = ATA_STAT_DRQ;
  1119. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1120. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1121. printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
  1122. err=0xFF;
  1123. goto AI_OUT;
  1124. }
  1125. output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
  1126. /* ATAPI Command written wait for completition */
  1127. udelay (5000); /* device must set bsy */
  1128. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1129. /* if no data wait for DRQ = 0 BSY = 0
  1130. * if data wait for DRQ = 1 BSY = 0 */
  1131. res=0;
  1132. if(buflen)
  1133. res = ATA_STAT_DRQ;
  1134. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1135. if ((c & mask) != res ) {
  1136. if (c & ATA_STAT_ERR) {
  1137. err=(ide_inb(device,ATA_ERROR_REG))>>4;
  1138. AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
  1139. } else {
  1140. printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
  1141. err=0xFF;
  1142. }
  1143. goto AI_OUT;
  1144. }
  1145. n=ide_inb(device, ATA_CYL_HIGH);
  1146. n<<=8;
  1147. n+=ide_inb(device, ATA_CYL_LOW);
  1148. if(n>buflen) {
  1149. printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
  1150. err=0xff;
  1151. goto AI_OUT;
  1152. }
  1153. if((n==0)&&(buflen<0)) {
  1154. printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
  1155. err=0xff;
  1156. goto AI_OUT;
  1157. }
  1158. if(n!=buflen) {
  1159. AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
  1160. }
  1161. if(n!=0) { /* data transfer */
  1162. AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
  1163. /* we transfer shorts */
  1164. n>>=1;
  1165. /* ok now decide if it is an in or output */
  1166. if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
  1167. AT_PRINTF("Write to device\n");
  1168. output_data_shorts(device,(unsigned short *)buffer,n);
  1169. } else {
  1170. AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
  1171. input_data_shorts(device,(unsigned short *)buffer,n);
  1172. }
  1173. }
  1174. udelay(5000); /* seems that some CD ROMs need this... */
  1175. mask = ATA_STAT_BUSY|ATA_STAT_ERR;
  1176. res=0;
  1177. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1178. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1179. err=(ide_inb(device,ATA_ERROR_REG) >> 4);
  1180. AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
  1181. } else {
  1182. err = 0;
  1183. }
  1184. AI_OUT:
  1185. ide_led (DEVICE_LED(device), 0); /* LED off */
  1186. return (err);
  1187. }
  1188. /*
  1189. * sending the command to atapi_issue. If an status other than good
  1190. * returns, an request_sense will be issued
  1191. */
  1192. #define ATAPI_DRIVE_NOT_READY 100
  1193. #define ATAPI_UNIT_ATTN 10
  1194. unsigned char atapi_issue_autoreq (int device,
  1195. unsigned char* ccb,
  1196. int ccblen,
  1197. unsigned char *buffer,
  1198. int buflen)
  1199. {
  1200. unsigned char sense_data[18],sense_ccb[12];
  1201. unsigned char res,key,asc,ascq;
  1202. int notready,unitattn;
  1203. unitattn=ATAPI_UNIT_ATTN;
  1204. notready=ATAPI_DRIVE_NOT_READY;
  1205. retry:
  1206. res= atapi_issue(device,ccb,ccblen,buffer,buflen);
  1207. if (res==0)
  1208. return (0); /* Ok */
  1209. if (res==0xFF)
  1210. return (0xFF); /* error */
  1211. AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
  1212. memset(sense_ccb,0,sizeof(sense_ccb));
  1213. memset(sense_data,0,sizeof(sense_data));
  1214. sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
  1215. sense_ccb[4]=18; /* allocation Legnth */
  1216. res=atapi_issue(device,sense_ccb,12,sense_data,18);
  1217. key=(sense_data[2]&0xF);
  1218. asc=(sense_data[12]);
  1219. ascq=(sense_data[13]);
  1220. AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
  1221. AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1222. sense_data[0],
  1223. key,
  1224. asc,
  1225. ascq);
  1226. if((key==0))
  1227. return 0; /* ok device ready */
  1228. if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
  1229. if(unitattn-->0) {
  1230. udelay(200*1000);
  1231. goto retry;
  1232. }
  1233. printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
  1234. goto error;
  1235. }
  1236. if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
  1237. if (notready-->0) {
  1238. udelay(200*1000);
  1239. goto retry;
  1240. }
  1241. printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
  1242. goto error;
  1243. }
  1244. if(asc==0x3a) {
  1245. AT_PRINTF("Media not present\n");
  1246. goto error;
  1247. }
  1248. printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1249. error:
  1250. AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1251. return (0xFF);
  1252. }
  1253. static void atapi_inquiry(block_dev_desc_t * dev_desc)
  1254. {
  1255. unsigned char ccb[12]; /* Command descriptor block */
  1256. unsigned char iobuf[64]; /* temp buf */
  1257. unsigned char c;
  1258. int device;
  1259. device=dev_desc->dev;
  1260. dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
  1261. dev_desc->block_read=atapi_read;
  1262. memset(ccb,0,sizeof(ccb));
  1263. memset(iobuf,0,sizeof(iobuf));
  1264. ccb[0]=ATAPI_CMD_INQUIRY;
  1265. ccb[4]=40; /* allocation Legnth */
  1266. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
  1267. AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
  1268. if (c!=0)
  1269. return;
  1270. /* copy device ident strings */
  1271. ident_cpy(dev_desc->vendor,&iobuf[8],8);
  1272. ident_cpy(dev_desc->product,&iobuf[16],16);
  1273. ident_cpy(dev_desc->revision,&iobuf[32],5);
  1274. dev_desc->lun=0;
  1275. dev_desc->lba=0;
  1276. dev_desc->blksz=0;
  1277. dev_desc->type=iobuf[0] & 0x1f;
  1278. if ((iobuf[1]&0x80)==0x80)
  1279. dev_desc->removable = 1;
  1280. else
  1281. dev_desc->removable = 0;
  1282. memset(ccb,0,sizeof(ccb));
  1283. memset(iobuf,0,sizeof(iobuf));
  1284. ccb[0]=ATAPI_CMD_START_STOP;
  1285. ccb[4]=0x03; /* start */
  1286. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1287. AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
  1288. if (c!=0)
  1289. return;
  1290. memset(ccb,0,sizeof(ccb));
  1291. memset(iobuf,0,sizeof(iobuf));
  1292. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1293. AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
  1294. if (c!=0)
  1295. return;
  1296. memset(ccb,0,sizeof(ccb));
  1297. memset(iobuf,0,sizeof(iobuf));
  1298. ccb[0]=ATAPI_CMD_READ_CAP;
  1299. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
  1300. AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
  1301. if (c!=0)
  1302. return;
  1303. AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1304. iobuf[0],iobuf[1],iobuf[2],iobuf[3],
  1305. iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
  1306. dev_desc->lba =((unsigned long)iobuf[0]<<24) +
  1307. ((unsigned long)iobuf[1]<<16) +
  1308. ((unsigned long)iobuf[2]<< 8) +
  1309. ((unsigned long)iobuf[3]);
  1310. dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
  1311. ((unsigned long)iobuf[5]<<16) +
  1312. ((unsigned long)iobuf[6]<< 8) +
  1313. ((unsigned long)iobuf[7]);
  1314. return;
  1315. }
  1316. /*
  1317. * atapi_read:
  1318. * we transfer only one block per command, since the multiple DRQ per
  1319. * command is not yet implemented
  1320. */
  1321. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1322. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1323. #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
  1324. ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  1325. {
  1326. ulong n = 0;
  1327. unsigned char ccb[12]; /* Command descriptor block */
  1328. ulong cnt;
  1329. AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1330. device, blknr, blkcnt, (ulong)buffer);
  1331. do {
  1332. if (blkcnt>ATAPI_READ_MAX_BLOCK) {
  1333. cnt=ATAPI_READ_MAX_BLOCK;
  1334. } else {
  1335. cnt=blkcnt;
  1336. }
  1337. ccb[0]=ATAPI_CMD_READ_12;
  1338. ccb[1]=0; /* reserved */
  1339. ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
  1340. ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
  1341. ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
  1342. ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
  1343. ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
  1344. ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
  1345. ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
  1346. ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
  1347. ccb[10]=0; /* reserved */
  1348. ccb[11]=0; /* reserved */
  1349. if (atapi_issue_autoreq(device,ccb,12,
  1350. (unsigned char *)buffer,
  1351. cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
  1352. return (n);
  1353. }
  1354. n+=cnt;
  1355. blkcnt-=cnt;
  1356. blknr+=cnt;
  1357. buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
  1358. } while (blkcnt > 0);
  1359. return (n);
  1360. }
  1361. /* ------------------------------------------------------------------------- */
  1362. #endif /* CONFIG_ATAPI */
  1363. #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */