cmd_ide.c 43 KB

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