cmd_ide.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  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. __inline__ unsigned ld_le16(const volatile unsigned short *addr)
  678. {
  679. unsigned val;
  680. __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r"(val) : "r"(addr), "m"(*addr));
  681. return val;
  682. }
  683. #ifdef CONFIG_AMIGAONEG3SE
  684. static void
  685. output_data_short(int dev, ulong *sect_buf, int words)
  686. {
  687. ushort *dbuf;
  688. volatile ushort *pbuf;
  689. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  690. dbuf = (ushort *)sect_buf;
  691. while (words--) {
  692. __asm__ volatile ("eieio");
  693. *pbuf = *dbuf++;
  694. __asm__ volatile ("eieio");
  695. }
  696. if (words&1)
  697. *pbuf = 0;
  698. }
  699. #endif
  700. static void
  701. input_swap_data(int dev, ulong *sect_buf, int words)
  702. {
  703. volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  704. ushort *dbuf = (ushort *)sect_buf;
  705. while (words--) {
  706. *dbuf++ = ld_le16(pbuf);
  707. *dbuf++ = ld_le16(pbuf);
  708. }
  709. }
  710. #else /* ! __PPC__ */
  711. #define input_swap_data(x,y,z) input_data(x,y,z)
  712. #endif /* __PPC__ */
  713. #ifdef __PPC__
  714. static void
  715. output_data(int dev, ulong *sect_buf, int words)
  716. {
  717. ushort *dbuf;
  718. volatile ushort *pbuf;
  719. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  720. dbuf = (ushort *)sect_buf;
  721. while (words--) {
  722. __asm__ volatile ("eieio");
  723. *pbuf = *dbuf++;
  724. __asm__ volatile ("eieio");
  725. *pbuf = *dbuf++;
  726. }
  727. }
  728. #else /* ! __PPC__ */
  729. static void
  730. output_data(int dev, ulong *sect_buf, int words)
  731. {
  732. outsw(ATA_DATA_REG, sect_buf, words<<1);
  733. }
  734. #endif /* __PPC__ */
  735. #ifdef __PPC__
  736. static void
  737. input_data(int dev, ulong *sect_buf, int words)
  738. {
  739. ushort *dbuf;
  740. volatile ushort *pbuf;
  741. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  742. dbuf = (ushort *)sect_buf;
  743. while (words--) {
  744. __asm__ volatile ("eieio");
  745. *dbuf++ = *pbuf;
  746. __asm__ volatile ("eieio");
  747. *dbuf++ = *pbuf;
  748. }
  749. }
  750. #else /* ! __PPC__ */
  751. static void
  752. input_data(int dev, ulong *sect_buf, int words)
  753. {
  754. insw(ATA_DATA_REG, sect_buf, words << 1);
  755. }
  756. #endif /* __PPC__ */
  757. #ifdef CONFIG_AMIGAONEG3SE
  758. static void
  759. input_data_short(int dev, ulong *sect_buf, int words)
  760. {
  761. ushort *dbuf;
  762. volatile ushort *pbuf;
  763. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  764. dbuf = (ushort *)sect_buf;
  765. while (words--) {
  766. __asm__ volatile ("eieio");
  767. *dbuf++ = *pbuf;
  768. __asm__ volatile ("eieio");
  769. }
  770. if (words&1)
  771. {
  772. ushort dummy;
  773. dummy = *pbuf;
  774. }
  775. }
  776. #endif
  777. /* -------------------------------------------------------------------------
  778. */
  779. static void ide_ident (block_dev_desc_t *dev_desc)
  780. {
  781. ulong iobuf[ATA_SECTORWORDS];
  782. unsigned char c;
  783. hd_driveid_t *iop = (hd_driveid_t *)iobuf;
  784. #ifdef CONFIG_AMIGAONEG3SE
  785. int max_bus_scan;
  786. int retries = 0;
  787. char *s;
  788. int do_retry = 0;
  789. #endif
  790. #if 0
  791. int mode, cycle_time;
  792. #endif
  793. int device;
  794. device=dev_desc->dev;
  795. printf (" Device %d: ", device);
  796. #ifdef CONFIG_AMIGAONEG3SE
  797. s = getenv("ide_maxbus");
  798. if (s) {
  799. max_bus_scan = simple_strtol(s, NULL, 10);
  800. } else {
  801. max_bus_scan = CFG_IDE_MAXBUS;
  802. }
  803. if (device >= max_bus_scan*2) {
  804. dev_desc->type=DEV_TYPE_UNKNOWN;
  805. return;
  806. }
  807. #endif
  808. ide_led (DEVICE_LED(device), 1); /* LED on */
  809. /* Select device
  810. */
  811. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  812. dev_desc->if_type=IF_TYPE_IDE;
  813. #ifdef CONFIG_ATAPI
  814. #ifdef CONFIG_AMIGAONEG3SE
  815. do_retry = 0;
  816. retries = 0;
  817. /* Warning: This will be tricky to read */
  818. while (retries <= 1)
  819. {
  820. #endif /* CONFIG_AMIGAONEG3SE */
  821. /* check signature */
  822. if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
  823. (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
  824. (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
  825. (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
  826. /* ATAPI Signature found */
  827. dev_desc->if_type=IF_TYPE_ATAPI;
  828. /* Start Ident Command
  829. */
  830. ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
  831. /*
  832. * Wait for completion - ATAPI devices need more time
  833. * to become ready
  834. */
  835. c = ide_wait (device, ATAPI_TIME_OUT);
  836. }
  837. else
  838. #endif
  839. {
  840. /* Start Ident Command
  841. */
  842. ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
  843. /* Wait for completion
  844. */
  845. c = ide_wait (device, IDE_TIME_OUT);
  846. }
  847. ide_led (DEVICE_LED(device), 0); /* LED off */
  848. if (((c & ATA_STAT_DRQ) == 0) ||
  849. ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
  850. #ifdef CONFIG_AMIGAONEG3SE
  851. if (retries == 0) {
  852. do_retry = 1;
  853. } else {
  854. dev_desc->type=DEV_TYPE_UNKNOWN;
  855. return;
  856. }
  857. #else
  858. dev_desc->type=DEV_TYPE_UNKNOWN;
  859. return;
  860. #endif /* CONFIG_AMIGAONEG3SE */
  861. }
  862. #ifdef CONFIG_AMIGAONEG3SE
  863. s = getenv("ide_doreset");
  864. if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
  865. /* Need to soft reset the device in case it's an ATAPI... */
  866. PRINTF("Retrying...\n");
  867. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  868. udelay(100000);
  869. ide_outb (device, ATA_COMMAND, 0x08);
  870. udelay (100000); /* 100 ms */
  871. retries++;
  872. } else {
  873. retries = 100;
  874. }
  875. } /* see above - ugly to read */
  876. #endif /* CONFIG_AMIGAONEG3SE */
  877. input_swap_data (device, iobuf, ATA_SECTORWORDS);
  878. ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
  879. ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
  880. ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
  881. if ((iop->config & 0x0080)==0x0080)
  882. dev_desc->removable = 1;
  883. else
  884. dev_desc->removable = 0;
  885. #if 0
  886. /*
  887. * Drive PIO mode autoselection
  888. */
  889. mode = iop->tPIO;
  890. printf ("tPIO = 0x%02x = %d\n",mode, mode);
  891. if (mode > 2) { /* 2 is maximum allowed tPIO value */
  892. mode = 2;
  893. PRINTF ("Override tPIO -> 2\n");
  894. }
  895. if (iop->field_valid & 2) { /* drive implements ATA2? */
  896. PRINTF ("Drive implements ATA2\n");
  897. if (iop->capability & 8) { /* drive supports use_iordy? */
  898. cycle_time = iop->eide_pio_iordy;
  899. } else {
  900. cycle_time = iop->eide_pio;
  901. }
  902. PRINTF ("cycle time = %d\n", cycle_time);
  903. mode = 4;
  904. if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
  905. if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
  906. if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
  907. if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
  908. }
  909. printf ("PIO mode to use: PIO %d\n", mode);
  910. #endif /* 0 */
  911. #ifdef CONFIG_ATAPI
  912. if (dev_desc->if_type==IF_TYPE_ATAPI) {
  913. atapi_inquiry(dev_desc);
  914. return;
  915. }
  916. #endif /* CONFIG_ATAPI */
  917. /* swap shorts */
  918. dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
  919. /* assuming HD */
  920. dev_desc->type=DEV_TYPE_HARDDISK;
  921. dev_desc->blksz=ATA_BLOCKSIZE;
  922. dev_desc->lun=0; /* just to fill something in... */
  923. #if 0 /* only used to test the powersaving mode,
  924. * if enabled, the drive goes after 5 sec
  925. * in standby mode */
  926. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  927. c = ide_wait (device, IDE_TIME_OUT);
  928. ide_outb (device, ATA_SECT_CNT, 1);
  929. ide_outb (device, ATA_LBA_LOW, 0);
  930. ide_outb (device, ATA_LBA_MID, 0);
  931. ide_outb (device, ATA_LBA_HIGH, 0);
  932. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  933. ATA_DEVICE(device));
  934. ide_outb (device, ATA_COMMAND, 0xe3);
  935. udelay (50);
  936. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  937. #endif
  938. }
  939. /* ------------------------------------------------------------------------- */
  940. ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  941. {
  942. ulong n = 0;
  943. unsigned char c;
  944. unsigned char pwrsave=0; /* power save */
  945. PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
  946. device, blknr, blkcnt, (ulong)buffer);
  947. ide_led (DEVICE_LED(device), 1); /* LED on */
  948. /* Select device
  949. */
  950. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  951. c = ide_wait (device, IDE_TIME_OUT);
  952. if (c & ATA_STAT_BUSY) {
  953. printf ("IDE read: device %d not ready\n", device);
  954. goto IDE_READ_E;
  955. }
  956. /* first check if the drive is in Powersaving mode, if yes,
  957. * increase the timeout value */
  958. ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  959. udelay (50);
  960. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  961. if (c & ATA_STAT_BUSY) {
  962. printf ("IDE read: device %d not ready\n", device);
  963. goto IDE_READ_E;
  964. }
  965. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  966. printf ("No Powersaving mode %X\n", c);
  967. } else {
  968. c = ide_inb(device,ATA_SECT_CNT);
  969. PRINTF("Powersaving %02X\n",c);
  970. if(c==0)
  971. pwrsave=1;
  972. }
  973. while (blkcnt-- > 0) {
  974. c = ide_wait (device, IDE_TIME_OUT);
  975. if (c & ATA_STAT_BUSY) {
  976. printf ("IDE read: device %d not ready\n", device);
  977. break;
  978. }
  979. ide_outb (device, ATA_SECT_CNT, 1);
  980. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  981. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  982. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  983. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  984. ATA_DEVICE(device) |
  985. ((blknr >> 24) & 0xF) );
  986. ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
  987. udelay (50);
  988. if(pwrsave) {
  989. c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
  990. pwrsave=0;
  991. } else {
  992. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  993. }
  994. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  995. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  996. device, blknr, c);
  997. break;
  998. }
  999. input_data (device, buffer, ATA_SECTORWORDS);
  1000. (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
  1001. ++n;
  1002. ++blknr;
  1003. buffer += ATA_SECTORWORDS;
  1004. }
  1005. IDE_READ_E:
  1006. ide_led (DEVICE_LED(device), 0); /* LED off */
  1007. return (n);
  1008. }
  1009. /* ------------------------------------------------------------------------- */
  1010. ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  1011. {
  1012. ulong n = 0;
  1013. unsigned char c;
  1014. ide_led (DEVICE_LED(device), 1); /* LED on */
  1015. /* Select device
  1016. */
  1017. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1018. while (blkcnt-- > 0) {
  1019. c = ide_wait (device, IDE_TIME_OUT);
  1020. if (c & ATA_STAT_BUSY) {
  1021. printf ("IDE read: device %d not ready\n", device);
  1022. goto WR_OUT;
  1023. }
  1024. ide_outb (device, ATA_SECT_CNT, 1);
  1025. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  1026. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  1027. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  1028. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  1029. ATA_DEVICE(device) |
  1030. ((blknr >> 24) & 0xF) );
  1031. ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
  1032. udelay (50);
  1033. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1034. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  1035. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  1036. device, blknr, c);
  1037. goto WR_OUT;
  1038. }
  1039. output_data (device, buffer, ATA_SECTORWORDS);
  1040. c = ide_inb (device, ATA_STATUS); /* clear IRQ */
  1041. ++n;
  1042. ++blknr;
  1043. buffer += ATA_SECTORWORDS;
  1044. }
  1045. WR_OUT:
  1046. ide_led (DEVICE_LED(device), 0); /* LED off */
  1047. return (n);
  1048. }
  1049. /* ------------------------------------------------------------------------- */
  1050. /*
  1051. * copy src to dest, skipping leading and trailing blanks and null
  1052. * terminate the string
  1053. */
  1054. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
  1055. {
  1056. int start,end;
  1057. start=0;
  1058. while (start<len) {
  1059. if (src[start]!=' ')
  1060. break;
  1061. start++;
  1062. }
  1063. end=len-1;
  1064. while (end>start) {
  1065. if (src[end]!=' ')
  1066. break;
  1067. end--;
  1068. }
  1069. for ( ; start<=end; start++) {
  1070. *dest++=src[start];
  1071. }
  1072. *dest='\0';
  1073. }
  1074. /* ------------------------------------------------------------------------- */
  1075. /*
  1076. * Wait until Busy bit is off, or timeout (in ms)
  1077. * Return last status
  1078. */
  1079. static uchar ide_wait (int dev, ulong t)
  1080. {
  1081. ulong delay = 10 * t; /* poll every 100 us */
  1082. uchar c;
  1083. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  1084. udelay (100);
  1085. if (delay-- == 0) {
  1086. break;
  1087. }
  1088. }
  1089. return (c);
  1090. }
  1091. /* ------------------------------------------------------------------------- */
  1092. #ifdef CONFIG_IDE_RESET
  1093. extern void ide_set_reset(int idereset);
  1094. static void ide_reset (void)
  1095. {
  1096. #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
  1097. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  1098. #endif
  1099. int i;
  1100. curr_device = -1;
  1101. for (i=0; i<CFG_IDE_MAXBUS; ++i)
  1102. ide_bus_ok[i] = 0;
  1103. for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
  1104. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  1105. ide_set_reset (1); /* assert reset */
  1106. WATCHDOG_RESET();
  1107. #ifdef CFG_PB_12V_ENABLE
  1108. immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
  1109. immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
  1110. immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
  1111. immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
  1112. /* wait 500 ms for the voltage to stabilize
  1113. */
  1114. for (i=0; i<500; ++i) {
  1115. udelay (1000);
  1116. }
  1117. immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
  1118. #endif /* CFG_PB_12V_ENABLE */
  1119. #ifdef CFG_PB_IDE_MOTOR
  1120. /* configure IDE Motor voltage monitor pin as input */
  1121. immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
  1122. immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
  1123. immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
  1124. /* wait up to 1 s for the motor voltage to stabilize
  1125. */
  1126. for (i=0; i<1000; ++i) {
  1127. if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
  1128. break;
  1129. }
  1130. udelay (1000);
  1131. }
  1132. if (i == 1000) { /* Timeout */
  1133. printf ("\nWarning: 5V for IDE Motor missing\n");
  1134. # ifdef CONFIG_STATUS_LED
  1135. # ifdef STATUS_LED_YELLOW
  1136. status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
  1137. # endif
  1138. # ifdef STATUS_LED_GREEN
  1139. status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
  1140. # endif
  1141. # endif /* CONFIG_STATUS_LED */
  1142. }
  1143. #endif /* CFG_PB_IDE_MOTOR */
  1144. WATCHDOG_RESET();
  1145. /* de-assert RESET signal */
  1146. ide_set_reset(0);
  1147. /* wait 250 ms */
  1148. for (i=0; i<250; ++i) {
  1149. udelay (1000);
  1150. }
  1151. }
  1152. #endif /* CONFIG_IDE_RESET */
  1153. /* ------------------------------------------------------------------------- */
  1154. #if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K)
  1155. static uchar led_buffer = 0; /* Buffer for current LED status */
  1156. static void ide_led (uchar led, uchar status)
  1157. {
  1158. uchar *led_port = LED_PORT;
  1159. if (status) { /* switch LED on */
  1160. led_buffer |= led;
  1161. } else { /* switch LED off */
  1162. led_buffer &= ~led;
  1163. }
  1164. *led_port = led_buffer;
  1165. }
  1166. #endif /* CONFIG_IDE_LED */
  1167. /* ------------------------------------------------------------------------- */
  1168. #ifdef CONFIG_ATAPI
  1169. /****************************************************************************
  1170. * ATAPI Support
  1171. */
  1172. #undef ATAPI_DEBUG
  1173. #ifdef ATAPI_DEBUG
  1174. #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
  1175. #else
  1176. #define AT_PRINTF(fmt,args...)
  1177. #endif
  1178. #ifdef __PPC__
  1179. /* since ATAPI may use commands with not 4 bytes alligned length
  1180. * we have our own transfer functions, 2 bytes alligned */
  1181. static void
  1182. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1183. {
  1184. ushort *dbuf;
  1185. volatile ushort *pbuf;
  1186. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1187. dbuf = (ushort *)sect_buf;
  1188. while (shorts--) {
  1189. __asm__ volatile ("eieio");
  1190. *pbuf = *dbuf++;
  1191. }
  1192. }
  1193. static void
  1194. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1195. {
  1196. ushort *dbuf;
  1197. volatile ushort *pbuf;
  1198. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1199. dbuf = (ushort *)sect_buf;
  1200. while (shorts--) {
  1201. __asm__ volatile ("eieio");
  1202. *dbuf++ = *pbuf;
  1203. }
  1204. }
  1205. #else /* ! __PPC__ */
  1206. static void
  1207. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1208. {
  1209. outsw(ATA_DATA_REG, sect_buf, shorts);
  1210. }
  1211. static void
  1212. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1213. {
  1214. insw(ATA_DATA_REG, sect_buf, shorts);
  1215. }
  1216. #endif /* __PPC__ */
  1217. /*
  1218. * Wait until (Status & mask) == res, or timeout (in ms)
  1219. * Return last status
  1220. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1221. * and then they set their DRQ Bit
  1222. */
  1223. static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
  1224. {
  1225. ulong delay = 10 * t; /* poll every 100 us */
  1226. uchar c;
  1227. c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
  1228. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  1229. /* break if error occurs (doesn't make sense to wait more) */
  1230. if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
  1231. break;
  1232. udelay (100);
  1233. if (delay-- == 0) {
  1234. break;
  1235. }
  1236. }
  1237. return (c);
  1238. }
  1239. /*
  1240. * issue an atapi command
  1241. */
  1242. unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
  1243. {
  1244. unsigned char c,err,mask,res;
  1245. int n;
  1246. ide_led (DEVICE_LED(device), 1); /* LED on */
  1247. /* Select device
  1248. */
  1249. mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
  1250. res = 0;
  1251. #ifdef CONFIG_AMIGAONEG3SE
  1252. # warning THF: Removed LBA mode ???
  1253. #endif
  1254. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1255. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1256. if ((c & mask) != res) {
  1257. printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
  1258. err=0xFF;
  1259. goto AI_OUT;
  1260. }
  1261. /* write taskfile */
  1262. ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1263. ide_outb (device, ATA_SECT_CNT, 0);
  1264. ide_outb (device, ATA_SECT_NUM, 0);
  1265. ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
  1266. ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
  1267. #ifdef CONFIG_AMIGAONEG3SE
  1268. # warning THF: Removed LBA mode ???
  1269. #endif
  1270. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1271. ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1272. udelay (50);
  1273. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1274. res = ATA_STAT_DRQ;
  1275. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1276. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1277. printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
  1278. err=0xFF;
  1279. goto AI_OUT;
  1280. }
  1281. output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
  1282. /* ATAPI Command written wait for completition */
  1283. udelay (5000); /* device must set bsy */
  1284. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1285. /* if no data wait for DRQ = 0 BSY = 0
  1286. * if data wait for DRQ = 1 BSY = 0 */
  1287. res=0;
  1288. if(buflen)
  1289. res = ATA_STAT_DRQ;
  1290. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1291. if ((c & mask) != res ) {
  1292. if (c & ATA_STAT_ERR) {
  1293. err=(ide_inb(device,ATA_ERROR_REG))>>4;
  1294. AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
  1295. } else {
  1296. printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
  1297. err=0xFF;
  1298. }
  1299. goto AI_OUT;
  1300. }
  1301. n=ide_inb(device, ATA_CYL_HIGH);
  1302. n<<=8;
  1303. n+=ide_inb(device, ATA_CYL_LOW);
  1304. if(n>buflen) {
  1305. printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
  1306. err=0xff;
  1307. goto AI_OUT;
  1308. }
  1309. if((n==0)&&(buflen<0)) {
  1310. printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
  1311. err=0xff;
  1312. goto AI_OUT;
  1313. }
  1314. if(n!=buflen) {
  1315. AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
  1316. }
  1317. if(n!=0) { /* data transfer */
  1318. AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
  1319. /* we transfer shorts */
  1320. n>>=1;
  1321. /* ok now decide if it is an in or output */
  1322. if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
  1323. AT_PRINTF("Write to device\n");
  1324. output_data_shorts(device,(unsigned short *)buffer,n);
  1325. } else {
  1326. AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
  1327. input_data_shorts(device,(unsigned short *)buffer,n);
  1328. }
  1329. }
  1330. udelay(5000); /* seems that some CD ROMs need this... */
  1331. mask = ATA_STAT_BUSY|ATA_STAT_ERR;
  1332. res=0;
  1333. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1334. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1335. err=(ide_inb(device,ATA_ERROR_REG) >> 4);
  1336. AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
  1337. } else {
  1338. err = 0;
  1339. }
  1340. AI_OUT:
  1341. ide_led (DEVICE_LED(device), 0); /* LED off */
  1342. return (err);
  1343. }
  1344. /*
  1345. * sending the command to atapi_issue. If an status other than good
  1346. * returns, an request_sense will be issued
  1347. */
  1348. #define ATAPI_DRIVE_NOT_READY 100
  1349. #define ATAPI_UNIT_ATTN 10
  1350. unsigned char atapi_issue_autoreq (int device,
  1351. unsigned char* ccb,
  1352. int ccblen,
  1353. unsigned char *buffer,
  1354. int buflen)
  1355. {
  1356. unsigned char sense_data[18],sense_ccb[12];
  1357. unsigned char res,key,asc,ascq;
  1358. int notready,unitattn;
  1359. #ifdef CONFIG_AMIGAONEG3SE
  1360. char *s;
  1361. unsigned int timeout, retrycnt;
  1362. s = getenv("ide_cd_timeout");
  1363. timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
  1364. retrycnt = 0;
  1365. #endif
  1366. unitattn=ATAPI_UNIT_ATTN;
  1367. notready=ATAPI_DRIVE_NOT_READY;
  1368. retry:
  1369. res= atapi_issue(device,ccb,ccblen,buffer,buflen);
  1370. if (res==0)
  1371. return (0); /* Ok */
  1372. if (res==0xFF)
  1373. return (0xFF); /* error */
  1374. AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
  1375. memset(sense_ccb,0,sizeof(sense_ccb));
  1376. memset(sense_data,0,sizeof(sense_data));
  1377. sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
  1378. sense_ccb[4]=18; /* allocation Length */
  1379. res=atapi_issue(device,sense_ccb,12,sense_data,18);
  1380. key=(sense_data[2]&0xF);
  1381. asc=(sense_data[12]);
  1382. ascq=(sense_data[13]);
  1383. AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
  1384. AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1385. sense_data[0],
  1386. key,
  1387. asc,
  1388. ascq);
  1389. if((key==0))
  1390. return 0; /* ok device ready */
  1391. if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
  1392. if(unitattn-->0) {
  1393. udelay(200*1000);
  1394. goto retry;
  1395. }
  1396. printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
  1397. goto error;
  1398. }
  1399. if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
  1400. if (notready-->0) {
  1401. udelay(200*1000);
  1402. goto retry;
  1403. }
  1404. printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
  1405. goto error;
  1406. }
  1407. if(asc==0x3a) {
  1408. AT_PRINTF("Media not present\n");
  1409. goto error;
  1410. }
  1411. #ifdef CONFIG_AMIGAONEG3SE
  1412. if ((sense_data[2]&0xF)==0x0B) {
  1413. AT_PRINTF("ABORTED COMMAND...retry\n");
  1414. if (retrycnt++ < 4)
  1415. goto retry;
  1416. return (0xFF);
  1417. }
  1418. if ((sense_data[2]&0xf) == 0x02 &&
  1419. sense_data[12] == 0x04 &&
  1420. sense_data[13] == 0x01 ) {
  1421. AT_PRINTF("Waiting for unit to become active\n");
  1422. udelay(timeout);
  1423. if (retrycnt++ < 4)
  1424. goto retry;
  1425. return 0xFF;
  1426. }
  1427. #endif /* CONFIG_AMIGAONEG3SE */
  1428. printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1429. error:
  1430. AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1431. return (0xFF);
  1432. }
  1433. static void atapi_inquiry(block_dev_desc_t * dev_desc)
  1434. {
  1435. unsigned char ccb[12]; /* Command descriptor block */
  1436. unsigned char iobuf[64]; /* temp buf */
  1437. unsigned char c;
  1438. int device;
  1439. device=dev_desc->dev;
  1440. dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
  1441. dev_desc->block_read=atapi_read;
  1442. memset(ccb,0,sizeof(ccb));
  1443. memset(iobuf,0,sizeof(iobuf));
  1444. ccb[0]=ATAPI_CMD_INQUIRY;
  1445. ccb[4]=40; /* allocation Legnth */
  1446. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
  1447. AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
  1448. if (c!=0)
  1449. return;
  1450. /* copy device ident strings */
  1451. ident_cpy(dev_desc->vendor,&iobuf[8],8);
  1452. ident_cpy(dev_desc->product,&iobuf[16],16);
  1453. ident_cpy(dev_desc->revision,&iobuf[32],5);
  1454. dev_desc->lun=0;
  1455. dev_desc->lba=0;
  1456. dev_desc->blksz=0;
  1457. dev_desc->type=iobuf[0] & 0x1f;
  1458. if ((iobuf[1]&0x80)==0x80)
  1459. dev_desc->removable = 1;
  1460. else
  1461. dev_desc->removable = 0;
  1462. memset(ccb,0,sizeof(ccb));
  1463. memset(iobuf,0,sizeof(iobuf));
  1464. ccb[0]=ATAPI_CMD_START_STOP;
  1465. ccb[4]=0x03; /* start */
  1466. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1467. AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
  1468. if (c!=0)
  1469. return;
  1470. memset(ccb,0,sizeof(ccb));
  1471. memset(iobuf,0,sizeof(iobuf));
  1472. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1473. AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
  1474. if (c!=0)
  1475. return;
  1476. memset(ccb,0,sizeof(ccb));
  1477. memset(iobuf,0,sizeof(iobuf));
  1478. ccb[0]=ATAPI_CMD_READ_CAP;
  1479. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
  1480. AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
  1481. if (c!=0)
  1482. return;
  1483. AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1484. iobuf[0],iobuf[1],iobuf[2],iobuf[3],
  1485. iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
  1486. dev_desc->lba =((unsigned long)iobuf[0]<<24) +
  1487. ((unsigned long)iobuf[1]<<16) +
  1488. ((unsigned long)iobuf[2]<< 8) +
  1489. ((unsigned long)iobuf[3]);
  1490. dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
  1491. ((unsigned long)iobuf[5]<<16) +
  1492. ((unsigned long)iobuf[6]<< 8) +
  1493. ((unsigned long)iobuf[7]);
  1494. return;
  1495. }
  1496. /*
  1497. * atapi_read:
  1498. * we transfer only one block per command, since the multiple DRQ per
  1499. * command is not yet implemented
  1500. */
  1501. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1502. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1503. #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
  1504. ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  1505. {
  1506. ulong n = 0;
  1507. unsigned char ccb[12]; /* Command descriptor block */
  1508. ulong cnt;
  1509. AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1510. device, blknr, blkcnt, (ulong)buffer);
  1511. do {
  1512. if (blkcnt>ATAPI_READ_MAX_BLOCK) {
  1513. cnt=ATAPI_READ_MAX_BLOCK;
  1514. } else {
  1515. cnt=blkcnt;
  1516. }
  1517. ccb[0]=ATAPI_CMD_READ_12;
  1518. ccb[1]=0; /* reserved */
  1519. ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
  1520. ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
  1521. ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
  1522. ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
  1523. ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
  1524. ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
  1525. ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
  1526. ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
  1527. ccb[10]=0; /* reserved */
  1528. ccb[11]=0; /* reserved */
  1529. if (atapi_issue_autoreq(device,ccb,12,
  1530. (unsigned char *)buffer,
  1531. cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
  1532. return (n);
  1533. }
  1534. n+=cnt;
  1535. blkcnt-=cnt;
  1536. blknr+=cnt;
  1537. buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
  1538. } while (blkcnt > 0);
  1539. return (n);
  1540. }
  1541. /* ------------------------------------------------------------------------- */
  1542. #endif /* CONFIG_ATAPI */
  1543. #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */