cmd_ide.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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. #ifdef CONFIG_MPC5xxx
  40. #include <mpc5xxx.h>
  41. #endif
  42. #include <ide.h>
  43. #include <ata.h>
  44. #ifdef CONFIG_STATUS_LED
  45. # include <status_led.h>
  46. #endif
  47. #ifndef __PPC__
  48. #include <asm/io.h>
  49. #ifdef __MIPS__
  50. /* Macros depend on this variable */
  51. static unsigned long mips_io_port_base = 0;
  52. #endif
  53. #endif
  54. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  55. # include <status_led.h>
  56. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  57. #else
  58. # define SHOW_BOOT_PROGRESS(arg)
  59. #endif
  60. #undef IDE_DEBUG
  61. #ifdef IDE_DEBUG
  62. #define PRINTF(fmt,args...) printf (fmt ,##args)
  63. #else
  64. #define PRINTF(fmt,args...)
  65. #endif
  66. #if (CONFIG_COMMANDS & CFG_CMD_IDE)
  67. #ifdef CONFIG_IDE_8xx_DIRECT
  68. /* Timings for IDE Interface
  69. *
  70. * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
  71. * 70 165 30 PIO-Mode 0, [ns]
  72. * 4 9 2 [Cycles]
  73. * 50 125 20 PIO-Mode 1, [ns]
  74. * 3 7 2 [Cycles]
  75. * 30 100 15 PIO-Mode 2, [ns]
  76. * 2 6 1 [Cycles]
  77. * 30 80 10 PIO-Mode 3, [ns]
  78. * 2 5 1 [Cycles]
  79. * 25 70 10 PIO-Mode 4, [ns]
  80. * 2 4 1 [Cycles]
  81. */
  82. const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
  83. {
  84. /* Setup Length Hold */
  85. { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
  86. { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
  87. { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
  88. { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
  89. { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
  90. };
  91. static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
  92. #ifndef CFG_PIO_MODE
  93. #define CFG_PIO_MODE 0 /* use a relaxed default */
  94. #endif
  95. static int pio_mode = CFG_PIO_MODE;
  96. /* Make clock cycles and always round up */
  97. #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
  98. #endif /* CONFIG_IDE_8xx_DIRECT */
  99. /* ------------------------------------------------------------------------- */
  100. /* Current I/O Device */
  101. static int curr_device = -1;
  102. /* Current offset for IDE0 / IDE1 bus access */
  103. ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
  104. #if defined(CFG_ATA_IDE0_OFFSET)
  105. CFG_ATA_IDE0_OFFSET,
  106. #endif
  107. #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
  108. CFG_ATA_IDE1_OFFSET,
  109. #endif
  110. };
  111. #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
  112. #ifndef CONFIG_AMIGAONEG3SE
  113. static int ide_bus_ok[CFG_IDE_MAXBUS];
  114. #else
  115. static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
  116. #endif
  117. static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
  118. /* ------------------------------------------------------------------------- */
  119. #ifdef CONFIG_IDE_LED
  120. #if !defined(CONFIG_KUP4K) && !defined(CONFIG_BMS2003)
  121. static void ide_led (uchar led, uchar status);
  122. #else
  123. extern void ide_led (uchar led, uchar status);
  124. #endif
  125. #else
  126. #ifndef CONFIG_AMIGAONEG3SE
  127. #define ide_led(a,b) /* dummy */
  128. #else
  129. extern void ide_led(uchar led, uchar status);
  130. #define LED_IDE1 1
  131. #define LED_IDE2 2
  132. #define CONFIG_IDE_LED 1
  133. #define DEVICE_LED(x) 1
  134. #endif
  135. #endif
  136. #ifdef CONFIG_IDE_RESET
  137. static void ide_reset (void);
  138. #else
  139. #define ide_reset() /* dummy */
  140. #endif
  141. static void ide_ident (block_dev_desc_t *dev_desc);
  142. static uchar ide_wait (int dev, ulong t);
  143. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  144. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  145. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  146. static void __inline__ ide_outb(int dev, int port, unsigned char val);
  147. static unsigned char __inline__ ide_inb(int dev, int port);
  148. static void input_data(int dev, ulong *sect_buf, int words);
  149. static void output_data(int dev, ulong *sect_buf, int words);
  150. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  151. #ifdef CONFIG_ATAPI
  152. static void atapi_inquiry(block_dev_desc_t *dev_desc);
  153. ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
  154. #endif
  155. #ifdef CONFIG_IDE_8xx_DIRECT
  156. static void set_pcmcia_timing (int pmode);
  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. #ifdef CONFIG_IDE_8xx_DIRECT
  401. DECLARE_GLOBAL_DATA_PTR;
  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. #endif /* CONFIG_IDE_8xx_PCCARD */
  416. #ifdef CONFIG_IDE_PREINIT
  417. WATCHDOG_RESET();
  418. if (ide_preinit ()) {
  419. puts ("ide_preinit failed\n");
  420. return;
  421. }
  422. #endif /* CONFIG_IDE_PREINIT */
  423. #ifdef CONFIG_IDE_8xx_PCCARD
  424. extern int pcmcia_on (void);
  425. extern int ide_devices_found; /* Initialized in check_ide_device() */
  426. WATCHDOG_RESET();
  427. ide_devices_found = 0;
  428. /* initialize the PCMCIA IDE adapter card */
  429. pcmcia_on();
  430. if (!ide_devices_found)
  431. return;
  432. udelay (1000000); /* 1 s */
  433. #endif /* CONFIG_IDE_8xx_PCCARD */
  434. WATCHDOG_RESET();
  435. #ifdef CONFIG_IDE_8xx_DIRECT
  436. /* Initialize PIO timing tables */
  437. for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
  438. pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
  439. gd->bus_clk);
  440. pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
  441. gd->bus_clk);
  442. pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
  443. gd->bus_clk);
  444. PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
  445. " len=%3d ns/%d clk"
  446. " hold=%2d ns/%d clk\n",
  447. i,
  448. pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
  449. pio_config_ns[i].t_length, pio_config_clk[i].t_length,
  450. pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
  451. }
  452. #endif /* CONFIG_IDE_8xx_DIRECT */
  453. /* Reset the IDE just to be sure.
  454. * Light LED's to show
  455. */
  456. ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  457. ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
  458. #ifdef CONFIG_IDE_8xx_DIRECT
  459. /* PCMCIA / IDE initialization for common mem space */
  460. pcmp->pcmc_pgcrb = 0;
  461. /* start in PIO mode 0 - most relaxed timings */
  462. pio_mode = 0;
  463. set_pcmcia_timing (pio_mode);
  464. #endif /* CONFIG_IDE_8xx_DIRECT */
  465. /*
  466. * Wait for IDE to get ready.
  467. * According to spec, this can take up to 31 seconds!
  468. */
  469. #ifndef CONFIG_AMIGAONEG3SE
  470. for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
  471. int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
  472. #else
  473. s = getenv("ide_maxbus");
  474. if (s)
  475. max_bus_scan = simple_strtol(s, NULL, 10);
  476. else
  477. max_bus_scan = CFG_IDE_MAXBUS;
  478. for (bus=0; bus<max_bus_scan; ++bus) {
  479. int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
  480. #endif
  481. #ifdef CONFIG_IDE_8xx_PCCARD
  482. /* Skip non-ide devices from probing */
  483. if ((ide_devices_found & (1 << bus)) == 0) {
  484. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  485. continue;
  486. }
  487. #endif
  488. printf ("Bus %d: ", bus);
  489. ide_bus_ok[bus] = 0;
  490. /* Select device
  491. */
  492. udelay (100000); /* 100 ms */
  493. ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  494. udelay (100000); /* 100 ms */
  495. #ifdef CONFIG_AMIGAONEG3SE
  496. ata_reset_time = ATA_RESET_TIME;
  497. s = getenv("ide_reset_timeout");
  498. if (s) ata_reset_time = 2*simple_strtol(s, NULL, 10);
  499. #endif
  500. i = 0;
  501. do {
  502. udelay (10000); /* 10 ms */
  503. c = ide_inb (dev, ATA_STATUS);
  504. i++;
  505. #ifdef CONFIG_AMIGAONEG3SE
  506. if (i > (ata_reset_time * 100)) {
  507. #else
  508. if (i > (ATA_RESET_TIME * 100)) {
  509. #endif
  510. puts ("** Timeout **\n");
  511. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  512. #ifdef CONFIG_AMIGAONEG3SE
  513. /* If this is the second bus, the first one was OK */
  514. if (bus != 0)
  515. {
  516. ide_bus_ok[bus] = 0;
  517. goto skip_bus;
  518. }
  519. #endif
  520. return;
  521. }
  522. if ((i >= 100) && ((i%100)==0)) {
  523. putc ('.');
  524. }
  525. } while (c & ATA_STAT_BUSY);
  526. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  527. puts ("not available ");
  528. PRINTF ("Status = 0x%02X ", c);
  529. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  530. } else if ((c & ATA_STAT_READY) == 0) {
  531. puts ("not available ");
  532. PRINTF ("Status = 0x%02X ", c);
  533. #endif
  534. } else {
  535. puts ("OK ");
  536. ide_bus_ok[bus] = 1;
  537. }
  538. WATCHDOG_RESET();
  539. }
  540. #ifdef CONFIG_AMIGAONEG3SE
  541. skip_bus:
  542. #endif
  543. putc ('\n');
  544. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  545. curr_device = -1;
  546. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  547. #ifdef CONFIG_IDE_LED
  548. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  549. #endif
  550. ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
  551. ide_dev_desc[i].if_type=IF_TYPE_IDE;
  552. ide_dev_desc[i].dev=i;
  553. ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
  554. ide_dev_desc[i].blksz=0;
  555. ide_dev_desc[i].lba=0;
  556. ide_dev_desc[i].block_read=ide_read;
  557. if (!ide_bus_ok[IDE_BUS(i)])
  558. continue;
  559. ide_led (led, 1); /* LED on */
  560. ide_ident(&ide_dev_desc[i]);
  561. ide_led (led, 0); /* LED off */
  562. dev_print(&ide_dev_desc[i]);
  563. /* ide_print (i); */
  564. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  565. init_part (&ide_dev_desc[i]); /* initialize partition type */
  566. if (curr_device < 0)
  567. curr_device = i;
  568. }
  569. }
  570. WATCHDOG_RESET();
  571. }
  572. /* ------------------------------------------------------------------------- */
  573. block_dev_desc_t * ide_get_dev(int dev)
  574. {
  575. return ((block_dev_desc_t *)&ide_dev_desc[dev]);
  576. }
  577. #ifdef CONFIG_IDE_8xx_DIRECT
  578. static void
  579. set_pcmcia_timing (int pmode)
  580. {
  581. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  582. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  583. ulong timings;
  584. PRINTF ("Set timing for PIO Mode %d\n", pmode);
  585. timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
  586. | PCMCIA_SST(pio_config_clk[pmode].t_setup)
  587. | PCMCIA_SL (pio_config_clk[pmode].t_length)
  588. ;
  589. /* IDE 0
  590. */
  591. pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
  592. pcmp->pcmc_por0 = CFG_PCMCIA_POR0
  593. #if (CFG_PCMCIA_POR0 != 0)
  594. | timings
  595. #endif
  596. ;
  597. PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
  598. pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
  599. pcmp->pcmc_por1 = CFG_PCMCIA_POR1
  600. #if (CFG_PCMCIA_POR1 != 0)
  601. | timings
  602. #endif
  603. ;
  604. PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
  605. pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
  606. pcmp->pcmc_por2 = CFG_PCMCIA_POR2
  607. #if (CFG_PCMCIA_POR2 != 0)
  608. | timings
  609. #endif
  610. ;
  611. PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
  612. pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
  613. pcmp->pcmc_por3 = CFG_PCMCIA_POR3
  614. #if (CFG_PCMCIA_POR3 != 0)
  615. | timings
  616. #endif
  617. ;
  618. PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
  619. /* IDE 1
  620. */
  621. pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
  622. pcmp->pcmc_por4 = CFG_PCMCIA_POR4
  623. #if (CFG_PCMCIA_POR4 != 0)
  624. | timings
  625. #endif
  626. ;
  627. PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
  628. pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
  629. pcmp->pcmc_por5 = CFG_PCMCIA_POR5
  630. #if (CFG_PCMCIA_POR5 != 0)
  631. | timings
  632. #endif
  633. ;
  634. PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
  635. pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
  636. pcmp->pcmc_por6 = CFG_PCMCIA_POR6
  637. #if (CFG_PCMCIA_POR6 != 0)
  638. | timings
  639. #endif
  640. ;
  641. PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
  642. pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
  643. pcmp->pcmc_por7 = CFG_PCMCIA_POR7
  644. #if (CFG_PCMCIA_POR7 != 0)
  645. | timings
  646. #endif
  647. ;
  648. PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
  649. }
  650. #endif /* CONFIG_IDE_8xx_DIRECT */
  651. /* ------------------------------------------------------------------------- */
  652. #ifdef __PPC__
  653. static void __inline__
  654. ide_outb(int dev, int port, unsigned char val)
  655. {
  656. PRINTF ("ide_outb (dev= %d, port= %d, val= 0x%02x) : @ 0x%08lx\n",
  657. dev, port, val, (ATA_CURR_BASE(dev)+port));
  658. /* Ensure I/O operations complete */
  659. __asm__ volatile("eieio");
  660. *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
  661. }
  662. #else /* ! __PPC__ */
  663. static void __inline__
  664. ide_outb(int dev, int port, unsigned char val)
  665. {
  666. outb(val, ATA_CURR_BASE(dev)+port);
  667. }
  668. #endif /* __PPC__ */
  669. #ifdef __PPC__
  670. static unsigned char __inline__
  671. ide_inb(int dev, int port)
  672. {
  673. uchar val;
  674. /* Ensure I/O operations complete */
  675. __asm__ volatile("eieio");
  676. val = *((uchar *)(ATA_CURR_BASE(dev)+port));
  677. PRINTF ("ide_inb (dev= %d, port= %d) : @ 0x%08lx -> 0x%02x\n",
  678. dev, port, (ATA_CURR_BASE(dev)+port), val);
  679. return (val);
  680. }
  681. #else /* ! __PPC__ */
  682. static unsigned char __inline__
  683. ide_inb(int dev, int port)
  684. {
  685. return inb(ATA_CURR_BASE(dev)+port);
  686. }
  687. #endif /* __PPC__ */
  688. #ifdef __PPC__
  689. # ifdef CONFIG_AMIGAONEG3SE
  690. static void
  691. output_data_short(int dev, ulong *sect_buf, int words)
  692. {
  693. ushort *dbuf;
  694. volatile ushort *pbuf;
  695. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  696. dbuf = (ushort *)sect_buf;
  697. while (words--) {
  698. __asm__ volatile ("eieio");
  699. *pbuf = *dbuf++;
  700. __asm__ volatile ("eieio");
  701. }
  702. if (words&1)
  703. *pbuf = 0;
  704. }
  705. # endif /* CONFIG_AMIGAONEG3SE */
  706. #endif /* __PPC_ */
  707. /* We only need to swap data if we are running on a big endian cpu. */
  708. /* But Au1x00 cpu:s already swaps data in big endian mode! */
  709. #if defined(__LITTLE_ENDIAN) || defined(CONFIG_AU1X00)
  710. #define input_swap_data(x,y,z) input_data(x,y,z)
  711. #else
  712. static void
  713. input_swap_data(int dev, ulong *sect_buf, int words)
  714. {
  715. #ifndef CONFIG_BMS2003
  716. volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  717. ushort *dbuf = (ushort *)sect_buf;
  718. while (words--) {
  719. *dbuf++ = ld_le16(pbuf);
  720. *dbuf++ = ld_le16(pbuf);
  721. }
  722. #else /* CONFIG_BMS2003 */
  723. uchar i;
  724. volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  725. volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  726. ushort *dbuf = (ushort *)sect_buf;
  727. while (words--) {
  728. for (i=0; i<2; i++) {
  729. *(((uchar *)(dbuf)) + 1) = *pbuf_even;
  730. *(uchar *)dbuf = *pbuf_odd;
  731. dbuf+=1;
  732. }
  733. }
  734. #endif /* CONFIG_BMS2003 */
  735. }
  736. #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
  737. #ifdef __PPC__
  738. static void
  739. output_data(int dev, ulong *sect_buf, int words)
  740. {
  741. #ifndef CONFIG_BMS2003
  742. ushort *dbuf;
  743. volatile ushort *pbuf;
  744. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  745. dbuf = (ushort *)sect_buf;
  746. while (words--) {
  747. __asm__ volatile ("eieio");
  748. *pbuf = *dbuf++;
  749. __asm__ volatile ("eieio");
  750. *pbuf = *dbuf++;
  751. }
  752. #else /* CONFIG_BMS2003 */
  753. uchar *dbuf;
  754. volatile uchar *pbuf_even;
  755. volatile uchar *pbuf_odd;
  756. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  757. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  758. dbuf = (uchar *)sect_buf;
  759. while (words--) {
  760. __asm__ volatile ("eieio");
  761. *pbuf_even = *dbuf++;
  762. __asm__ volatile ("eieio");
  763. *pbuf_odd = *dbuf++;
  764. __asm__ volatile ("eieio");
  765. *pbuf_even = *dbuf++;
  766. __asm__ volatile ("eieio");
  767. *pbuf_odd = *dbuf++;
  768. }
  769. #endif /* CONFIG_BMS2003 */
  770. }
  771. #else /* ! __PPC__ */
  772. static void
  773. output_data(int dev, ulong *sect_buf, int words)
  774. {
  775. outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
  776. }
  777. #endif /* __PPC__ */
  778. #ifdef __PPC__
  779. static void
  780. input_data(int dev, ulong *sect_buf, int words)
  781. {
  782. #ifndef CONFIG_BMS2003
  783. ushort *dbuf;
  784. volatile ushort *pbuf;
  785. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  786. dbuf = (ushort *)sect_buf;
  787. while (words--) {
  788. __asm__ volatile ("eieio");
  789. *dbuf++ = *pbuf;
  790. __asm__ volatile ("eieio");
  791. *dbuf++ = *pbuf;
  792. }
  793. #else /* CONFIG_BMS2003 */
  794. uchar *dbuf;
  795. volatile uchar *pbuf_even;
  796. volatile uchar *pbuf_odd;
  797. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  798. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  799. dbuf = (uchar *)sect_buf;
  800. while (words--) {
  801. __asm__ volatile ("eieio");
  802. *dbuf++ = *pbuf_even;
  803. __asm__ volatile ("eieio");
  804. *dbuf++ = *pbuf_odd;
  805. __asm__ volatile ("eieio");
  806. *dbuf++ = *pbuf_even;
  807. __asm__ volatile ("eieio");
  808. *dbuf++ = *pbuf_odd;
  809. }
  810. #endif /* CONFIG_BMS2003 */
  811. }
  812. #else /* ! __PPC__ */
  813. static void
  814. input_data(int dev, ulong *sect_buf, int words)
  815. {
  816. insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
  817. }
  818. #endif /* __PPC__ */
  819. #ifdef CONFIG_AMIGAONEG3SE
  820. static void
  821. input_data_short(int dev, ulong *sect_buf, int words)
  822. {
  823. ushort *dbuf;
  824. volatile ushort *pbuf;
  825. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  826. dbuf = (ushort *)sect_buf;
  827. while (words--) {
  828. __asm__ volatile ("eieio");
  829. *dbuf++ = *pbuf;
  830. __asm__ volatile ("eieio");
  831. }
  832. if (words&1)
  833. {
  834. ushort dummy;
  835. dummy = *pbuf;
  836. }
  837. }
  838. #endif
  839. /* -------------------------------------------------------------------------
  840. */
  841. static void ide_ident (block_dev_desc_t *dev_desc)
  842. {
  843. ulong iobuf[ATA_SECTORWORDS];
  844. unsigned char c;
  845. hd_driveid_t *iop = (hd_driveid_t *)iobuf;
  846. #ifdef CONFIG_AMIGAONEG3SE
  847. int max_bus_scan;
  848. int retries = 0;
  849. char *s;
  850. int do_retry = 0;
  851. #endif
  852. #if 0
  853. int mode, cycle_time;
  854. #endif
  855. int device;
  856. device=dev_desc->dev;
  857. printf (" Device %d: ", device);
  858. #ifdef CONFIG_AMIGAONEG3SE
  859. s = getenv("ide_maxbus");
  860. if (s) {
  861. max_bus_scan = simple_strtol(s, NULL, 10);
  862. } else {
  863. max_bus_scan = CFG_IDE_MAXBUS;
  864. }
  865. if (device >= max_bus_scan*2) {
  866. dev_desc->type=DEV_TYPE_UNKNOWN;
  867. return;
  868. }
  869. #endif
  870. ide_led (DEVICE_LED(device), 1); /* LED on */
  871. /* Select device
  872. */
  873. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  874. dev_desc->if_type=IF_TYPE_IDE;
  875. #ifdef CONFIG_ATAPI
  876. #ifdef CONFIG_AMIGAONEG3SE
  877. do_retry = 0;
  878. retries = 0;
  879. /* Warning: This will be tricky to read */
  880. while (retries <= 1)
  881. {
  882. #endif /* CONFIG_AMIGAONEG3SE */
  883. /* check signature */
  884. if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
  885. (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
  886. (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
  887. (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
  888. /* ATAPI Signature found */
  889. dev_desc->if_type=IF_TYPE_ATAPI;
  890. /* Start Ident Command
  891. */
  892. ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
  893. /*
  894. * Wait for completion - ATAPI devices need more time
  895. * to become ready
  896. */
  897. c = ide_wait (device, ATAPI_TIME_OUT);
  898. }
  899. else
  900. #endif
  901. {
  902. /* Start Ident Command
  903. */
  904. ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
  905. /* Wait for completion
  906. */
  907. c = ide_wait (device, IDE_TIME_OUT);
  908. }
  909. ide_led (DEVICE_LED(device), 0); /* LED off */
  910. if (((c & ATA_STAT_DRQ) == 0) ||
  911. ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
  912. #ifdef CONFIG_AMIGAONEG3SE
  913. if (retries == 0) {
  914. do_retry = 1;
  915. } else {
  916. return;
  917. }
  918. #else
  919. return;
  920. #endif /* CONFIG_AMIGAONEG3SE */
  921. }
  922. #ifdef CONFIG_AMIGAONEG3SE
  923. s = getenv("ide_doreset");
  924. if (s && strcmp(s, "on") == 0 && 1 == do_retry) {
  925. /* Need to soft reset the device in case it's an ATAPI... */
  926. PRINTF("Retrying...\n");
  927. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  928. udelay(100000);
  929. ide_outb (device, ATA_COMMAND, 0x08);
  930. udelay (100000); /* 100 ms */
  931. retries++;
  932. } else {
  933. retries = 100;
  934. }
  935. } /* see above - ugly to read */
  936. #endif /* CONFIG_AMIGAONEG3SE */
  937. input_swap_data (device, iobuf, ATA_SECTORWORDS);
  938. ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
  939. ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
  940. ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
  941. if ((iop->config & 0x0080)==0x0080)
  942. dev_desc->removable = 1;
  943. else
  944. dev_desc->removable = 0;
  945. #if 0
  946. /*
  947. * Drive PIO mode autoselection
  948. */
  949. mode = iop->tPIO;
  950. printf ("tPIO = 0x%02x = %d\n",mode, mode);
  951. if (mode > 2) { /* 2 is maximum allowed tPIO value */
  952. mode = 2;
  953. PRINTF ("Override tPIO -> 2\n");
  954. }
  955. if (iop->field_valid & 2) { /* drive implements ATA2? */
  956. PRINTF ("Drive implements ATA2\n");
  957. if (iop->capability & 8) { /* drive supports use_iordy? */
  958. cycle_time = iop->eide_pio_iordy;
  959. } else {
  960. cycle_time = iop->eide_pio;
  961. }
  962. PRINTF ("cycle time = %d\n", cycle_time);
  963. mode = 4;
  964. if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
  965. if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
  966. if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
  967. if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
  968. }
  969. printf ("PIO mode to use: PIO %d\n", mode);
  970. #endif /* 0 */
  971. #ifdef CONFIG_ATAPI
  972. if (dev_desc->if_type==IF_TYPE_ATAPI) {
  973. atapi_inquiry(dev_desc);
  974. return;
  975. }
  976. #endif /* CONFIG_ATAPI */
  977. /* swap shorts */
  978. dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
  979. /* assuming HD */
  980. dev_desc->type=DEV_TYPE_HARDDISK;
  981. dev_desc->blksz=ATA_BLOCKSIZE;
  982. dev_desc->lun=0; /* just to fill something in... */
  983. #if 0 /* only used to test the powersaving mode,
  984. * if enabled, the drive goes after 5 sec
  985. * in standby mode */
  986. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  987. c = ide_wait (device, IDE_TIME_OUT);
  988. ide_outb (device, ATA_SECT_CNT, 1);
  989. ide_outb (device, ATA_LBA_LOW, 0);
  990. ide_outb (device, ATA_LBA_MID, 0);
  991. ide_outb (device, ATA_LBA_HIGH, 0);
  992. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  993. ATA_DEVICE(device));
  994. ide_outb (device, ATA_COMMAND, 0xe3);
  995. udelay (50);
  996. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  997. #endif
  998. }
  999. /* ------------------------------------------------------------------------- */
  1000. ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  1001. {
  1002. ulong n = 0;
  1003. unsigned char c;
  1004. unsigned char pwrsave=0; /* power save */
  1005. PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1006. device, blknr, blkcnt, (ulong)buffer);
  1007. ide_led (DEVICE_LED(device), 1); /* LED on */
  1008. /* Select device
  1009. */
  1010. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1011. c = ide_wait (device, IDE_TIME_OUT);
  1012. if (c & ATA_STAT_BUSY) {
  1013. printf ("IDE read: device %d not ready\n", device);
  1014. goto IDE_READ_E;
  1015. }
  1016. /* first check if the drive is in Powersaving mode, if yes,
  1017. * increase the timeout value */
  1018. ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  1019. udelay (50);
  1020. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1021. if (c & ATA_STAT_BUSY) {
  1022. printf ("IDE read: device %d not ready\n", device);
  1023. goto IDE_READ_E;
  1024. }
  1025. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1026. printf ("No Powersaving mode %X\n", c);
  1027. } else {
  1028. c = ide_inb(device,ATA_SECT_CNT);
  1029. PRINTF("Powersaving %02X\n",c);
  1030. if(c==0)
  1031. pwrsave=1;
  1032. }
  1033. while (blkcnt-- > 0) {
  1034. c = ide_wait (device, IDE_TIME_OUT);
  1035. if (c & ATA_STAT_BUSY) {
  1036. printf ("IDE read: device %d not ready\n", device);
  1037. break;
  1038. }
  1039. ide_outb (device, ATA_SECT_CNT, 1);
  1040. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  1041. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  1042. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  1043. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  1044. ATA_DEVICE(device) |
  1045. ((blknr >> 24) & 0xF) );
  1046. ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
  1047. udelay (50);
  1048. if(pwrsave) {
  1049. c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
  1050. pwrsave=0;
  1051. } else {
  1052. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1053. }
  1054. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  1055. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  1056. device, blknr, c);
  1057. break;
  1058. }
  1059. input_data (device, buffer, ATA_SECTORWORDS);
  1060. (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
  1061. ++n;
  1062. ++blknr;
  1063. buffer += ATA_SECTORWORDS;
  1064. }
  1065. IDE_READ_E:
  1066. ide_led (DEVICE_LED(device), 0); /* LED off */
  1067. return (n);
  1068. }
  1069. /* ------------------------------------------------------------------------- */
  1070. ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  1071. {
  1072. ulong n = 0;
  1073. unsigned char c;
  1074. ide_led (DEVICE_LED(device), 1); /* LED on */
  1075. /* Select device
  1076. */
  1077. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1078. while (blkcnt-- > 0) {
  1079. c = ide_wait (device, IDE_TIME_OUT);
  1080. if (c & ATA_STAT_BUSY) {
  1081. printf ("IDE read: device %d not ready\n", device);
  1082. goto WR_OUT;
  1083. }
  1084. ide_outb (device, ATA_SECT_CNT, 1);
  1085. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  1086. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  1087. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  1088. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  1089. ATA_DEVICE(device) |
  1090. ((blknr >> 24) & 0xF) );
  1091. ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
  1092. udelay (50);
  1093. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1094. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  1095. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  1096. device, blknr, c);
  1097. goto WR_OUT;
  1098. }
  1099. output_data (device, buffer, ATA_SECTORWORDS);
  1100. c = ide_inb (device, ATA_STATUS); /* clear IRQ */
  1101. ++n;
  1102. ++blknr;
  1103. buffer += ATA_SECTORWORDS;
  1104. }
  1105. WR_OUT:
  1106. ide_led (DEVICE_LED(device), 0); /* LED off */
  1107. return (n);
  1108. }
  1109. /* ------------------------------------------------------------------------- */
  1110. /*
  1111. * copy src to dest, skipping leading and trailing blanks and null
  1112. * terminate the string
  1113. */
  1114. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
  1115. {
  1116. int start,end;
  1117. start=0;
  1118. while (start<len) {
  1119. if (src[start]!=' ')
  1120. break;
  1121. start++;
  1122. }
  1123. end=len-1;
  1124. while (end>start) {
  1125. if (src[end]!=' ')
  1126. break;
  1127. end--;
  1128. }
  1129. for ( ; start<=end; start++) {
  1130. *dest++=src[start];
  1131. }
  1132. *dest='\0';
  1133. }
  1134. /* ------------------------------------------------------------------------- */
  1135. /*
  1136. * Wait until Busy bit is off, or timeout (in ms)
  1137. * Return last status
  1138. */
  1139. static uchar ide_wait (int dev, ulong t)
  1140. {
  1141. ulong delay = 10 * t; /* poll every 100 us */
  1142. uchar c;
  1143. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  1144. udelay (100);
  1145. if (delay-- == 0) {
  1146. break;
  1147. }
  1148. }
  1149. return (c);
  1150. }
  1151. /* ------------------------------------------------------------------------- */
  1152. #ifdef CONFIG_IDE_RESET
  1153. extern void ide_set_reset(int idereset);
  1154. static void ide_reset (void)
  1155. {
  1156. #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
  1157. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  1158. #endif
  1159. int i;
  1160. curr_device = -1;
  1161. for (i=0; i<CFG_IDE_MAXBUS; ++i)
  1162. ide_bus_ok[i] = 0;
  1163. for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
  1164. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  1165. ide_set_reset (1); /* assert reset */
  1166. WATCHDOG_RESET();
  1167. #ifdef CFG_PB_12V_ENABLE
  1168. immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
  1169. immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
  1170. immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
  1171. immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
  1172. /* wait 500 ms for the voltage to stabilize
  1173. */
  1174. for (i=0; i<500; ++i) {
  1175. udelay (1000);
  1176. }
  1177. immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
  1178. #endif /* CFG_PB_12V_ENABLE */
  1179. #ifdef CFG_PB_IDE_MOTOR
  1180. /* configure IDE Motor voltage monitor pin as input */
  1181. immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
  1182. immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
  1183. immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
  1184. /* wait up to 1 s for the motor voltage to stabilize
  1185. */
  1186. for (i=0; i<1000; ++i) {
  1187. if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
  1188. break;
  1189. }
  1190. udelay (1000);
  1191. }
  1192. if (i == 1000) { /* Timeout */
  1193. printf ("\nWarning: 5V for IDE Motor missing\n");
  1194. # ifdef CONFIG_STATUS_LED
  1195. # ifdef STATUS_LED_YELLOW
  1196. status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
  1197. # endif
  1198. # ifdef STATUS_LED_GREEN
  1199. status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
  1200. # endif
  1201. # endif /* CONFIG_STATUS_LED */
  1202. }
  1203. #endif /* CFG_PB_IDE_MOTOR */
  1204. WATCHDOG_RESET();
  1205. /* de-assert RESET signal */
  1206. ide_set_reset(0);
  1207. /* wait 250 ms */
  1208. for (i=0; i<250; ++i) {
  1209. udelay (1000);
  1210. }
  1211. }
  1212. #endif /* CONFIG_IDE_RESET */
  1213. /* ------------------------------------------------------------------------- */
  1214. #if defined(CONFIG_IDE_LED) && !defined(CONFIG_AMIGAONEG3SE) && !defined(CONFIG_KUP4K) && !defined(CONFIG_BMS2003)
  1215. static uchar led_buffer = 0; /* Buffer for current LED status */
  1216. static void ide_led (uchar led, uchar status)
  1217. {
  1218. uchar *led_port = LED_PORT;
  1219. if (status) { /* switch LED on */
  1220. led_buffer |= led;
  1221. } else { /* switch LED off */
  1222. led_buffer &= ~led;
  1223. }
  1224. *led_port = led_buffer;
  1225. }
  1226. #endif /* CONFIG_IDE_LED */
  1227. /* ------------------------------------------------------------------------- */
  1228. #ifdef CONFIG_ATAPI
  1229. /****************************************************************************
  1230. * ATAPI Support
  1231. */
  1232. #undef ATAPI_DEBUG
  1233. #ifdef ATAPI_DEBUG
  1234. #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
  1235. #else
  1236. #define AT_PRINTF(fmt,args...)
  1237. #endif
  1238. #ifdef __PPC__
  1239. /* since ATAPI may use commands with not 4 bytes alligned length
  1240. * we have our own transfer functions, 2 bytes alligned */
  1241. static void
  1242. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1243. {
  1244. #ifndef CONFIG_BMS2003
  1245. ushort *dbuf;
  1246. volatile ushort *pbuf;
  1247. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1248. dbuf = (ushort *)sect_buf;
  1249. while (shorts--) {
  1250. __asm__ volatile ("eieio");
  1251. *pbuf = *dbuf++;
  1252. }
  1253. #else /* CONFIG_BMS2003 */
  1254. uchar *dbuf;
  1255. volatile uchar *pbuf_even;
  1256. volatile uchar *pbuf_odd;
  1257. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  1258. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  1259. while (shorts--) {
  1260. __asm__ volatile ("eieio");
  1261. *pbuf_even = *dbuf++;
  1262. __asm__ volatile ("eieio");
  1263. *pbuf_odd = *dbuf++;
  1264. }
  1265. #endif /* CONFIG_BMS2003 */
  1266. }
  1267. static void
  1268. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1269. {
  1270. #ifndef CONFIG_BMS2003
  1271. ushort *dbuf;
  1272. volatile ushort *pbuf;
  1273. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1274. dbuf = (ushort *)sect_buf;
  1275. while (shorts--) {
  1276. __asm__ volatile ("eieio");
  1277. *dbuf++ = *pbuf;
  1278. }
  1279. #else /* CONFIG_BMS2003 */
  1280. uchar *dbuf;
  1281. volatile uchar *pbuf_even;
  1282. volatile uchar *pbuf_odd;
  1283. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  1284. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  1285. while (shorts--) {
  1286. __asm__ volatile ("eieio");
  1287. *dbuf++ = *pbuf_even;
  1288. __asm__ volatile ("eieio");
  1289. *dbuf++ = *pbuf_odd;
  1290. }
  1291. #endif /* CONFIG_BMS2003 */
  1292. }
  1293. #else /* ! __PPC__ */
  1294. static void
  1295. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1296. {
  1297. outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
  1298. }
  1299. static void
  1300. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1301. {
  1302. insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
  1303. }
  1304. #endif /* __PPC__ */
  1305. /*
  1306. * Wait until (Status & mask) == res, or timeout (in ms)
  1307. * Return last status
  1308. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1309. * and then they set their DRQ Bit
  1310. */
  1311. static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
  1312. {
  1313. ulong delay = 10 * t; /* poll every 100 us */
  1314. uchar c;
  1315. c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
  1316. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  1317. /* break if error occurs (doesn't make sense to wait more) */
  1318. if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
  1319. break;
  1320. udelay (100);
  1321. if (delay-- == 0) {
  1322. break;
  1323. }
  1324. }
  1325. return (c);
  1326. }
  1327. /*
  1328. * issue an atapi command
  1329. */
  1330. unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
  1331. {
  1332. unsigned char c,err,mask,res;
  1333. int n;
  1334. ide_led (DEVICE_LED(device), 1); /* LED on */
  1335. /* Select device
  1336. */
  1337. mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
  1338. res = 0;
  1339. #ifdef CONFIG_AMIGAONEG3SE
  1340. # warning THF: Removed LBA mode ???
  1341. #endif
  1342. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1343. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1344. if ((c & mask) != res) {
  1345. printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
  1346. err=0xFF;
  1347. goto AI_OUT;
  1348. }
  1349. /* write taskfile */
  1350. ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1351. ide_outb (device, ATA_SECT_CNT, 0);
  1352. ide_outb (device, ATA_SECT_NUM, 0);
  1353. ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
  1354. ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
  1355. #ifdef CONFIG_AMIGAONEG3SE
  1356. # warning THF: Removed LBA mode ???
  1357. #endif
  1358. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1359. ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1360. udelay (50);
  1361. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1362. res = ATA_STAT_DRQ;
  1363. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1364. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1365. printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
  1366. err=0xFF;
  1367. goto AI_OUT;
  1368. }
  1369. output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
  1370. /* ATAPI Command written wait for completition */
  1371. udelay (5000); /* device must set bsy */
  1372. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1373. /* if no data wait for DRQ = 0 BSY = 0
  1374. * if data wait for DRQ = 1 BSY = 0 */
  1375. res=0;
  1376. if(buflen)
  1377. res = ATA_STAT_DRQ;
  1378. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1379. if ((c & mask) != res ) {
  1380. if (c & ATA_STAT_ERR) {
  1381. err=(ide_inb(device,ATA_ERROR_REG))>>4;
  1382. AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
  1383. } else {
  1384. printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
  1385. err=0xFF;
  1386. }
  1387. goto AI_OUT;
  1388. }
  1389. n=ide_inb(device, ATA_CYL_HIGH);
  1390. n<<=8;
  1391. n+=ide_inb(device, ATA_CYL_LOW);
  1392. if(n>buflen) {
  1393. printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
  1394. err=0xff;
  1395. goto AI_OUT;
  1396. }
  1397. if((n==0)&&(buflen<0)) {
  1398. printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
  1399. err=0xff;
  1400. goto AI_OUT;
  1401. }
  1402. if(n!=buflen) {
  1403. AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
  1404. }
  1405. if(n!=0) { /* data transfer */
  1406. AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
  1407. /* we transfer shorts */
  1408. n>>=1;
  1409. /* ok now decide if it is an in or output */
  1410. if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
  1411. AT_PRINTF("Write to device\n");
  1412. output_data_shorts(device,(unsigned short *)buffer,n);
  1413. } else {
  1414. AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
  1415. input_data_shorts(device,(unsigned short *)buffer,n);
  1416. }
  1417. }
  1418. udelay(5000); /* seems that some CD ROMs need this... */
  1419. mask = ATA_STAT_BUSY|ATA_STAT_ERR;
  1420. res=0;
  1421. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1422. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1423. err=(ide_inb(device,ATA_ERROR_REG) >> 4);
  1424. AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
  1425. } else {
  1426. err = 0;
  1427. }
  1428. AI_OUT:
  1429. ide_led (DEVICE_LED(device), 0); /* LED off */
  1430. return (err);
  1431. }
  1432. /*
  1433. * sending the command to atapi_issue. If an status other than good
  1434. * returns, an request_sense will be issued
  1435. */
  1436. #define ATAPI_DRIVE_NOT_READY 100
  1437. #define ATAPI_UNIT_ATTN 10
  1438. unsigned char atapi_issue_autoreq (int device,
  1439. unsigned char* ccb,
  1440. int ccblen,
  1441. unsigned char *buffer,
  1442. int buflen)
  1443. {
  1444. unsigned char sense_data[18],sense_ccb[12];
  1445. unsigned char res,key,asc,ascq;
  1446. int notready,unitattn;
  1447. #ifdef CONFIG_AMIGAONEG3SE
  1448. char *s;
  1449. unsigned int timeout, retrycnt;
  1450. s = getenv("ide_cd_timeout");
  1451. timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
  1452. retrycnt = 0;
  1453. #endif
  1454. unitattn=ATAPI_UNIT_ATTN;
  1455. notready=ATAPI_DRIVE_NOT_READY;
  1456. retry:
  1457. res= atapi_issue(device,ccb,ccblen,buffer,buflen);
  1458. if (res==0)
  1459. return (0); /* Ok */
  1460. if (res==0xFF)
  1461. return (0xFF); /* error */
  1462. AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
  1463. memset(sense_ccb,0,sizeof(sense_ccb));
  1464. memset(sense_data,0,sizeof(sense_data));
  1465. sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
  1466. sense_ccb[4]=18; /* allocation Length */
  1467. res=atapi_issue(device,sense_ccb,12,sense_data,18);
  1468. key=(sense_data[2]&0xF);
  1469. asc=(sense_data[12]);
  1470. ascq=(sense_data[13]);
  1471. AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
  1472. AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1473. sense_data[0],
  1474. key,
  1475. asc,
  1476. ascq);
  1477. if((key==0))
  1478. return 0; /* ok device ready */
  1479. if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
  1480. if(unitattn-->0) {
  1481. udelay(200*1000);
  1482. goto retry;
  1483. }
  1484. printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
  1485. goto error;
  1486. }
  1487. if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
  1488. if (notready-->0) {
  1489. udelay(200*1000);
  1490. goto retry;
  1491. }
  1492. printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
  1493. goto error;
  1494. }
  1495. if(asc==0x3a) {
  1496. AT_PRINTF("Media not present\n");
  1497. goto error;
  1498. }
  1499. #ifdef CONFIG_AMIGAONEG3SE
  1500. if ((sense_data[2]&0xF)==0x0B) {
  1501. AT_PRINTF("ABORTED COMMAND...retry\n");
  1502. if (retrycnt++ < 4)
  1503. goto retry;
  1504. return (0xFF);
  1505. }
  1506. if ((sense_data[2]&0xf) == 0x02 &&
  1507. sense_data[12] == 0x04 &&
  1508. sense_data[13] == 0x01 ) {
  1509. AT_PRINTF("Waiting for unit to become active\n");
  1510. udelay(timeout);
  1511. if (retrycnt++ < 4)
  1512. goto retry;
  1513. return 0xFF;
  1514. }
  1515. #endif /* CONFIG_AMIGAONEG3SE */
  1516. printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1517. error:
  1518. AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1519. return (0xFF);
  1520. }
  1521. static void atapi_inquiry(block_dev_desc_t * dev_desc)
  1522. {
  1523. unsigned char ccb[12]; /* Command descriptor block */
  1524. unsigned char iobuf[64]; /* temp buf */
  1525. unsigned char c;
  1526. int device;
  1527. device=dev_desc->dev;
  1528. dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
  1529. dev_desc->block_read=atapi_read;
  1530. memset(ccb,0,sizeof(ccb));
  1531. memset(iobuf,0,sizeof(iobuf));
  1532. ccb[0]=ATAPI_CMD_INQUIRY;
  1533. ccb[4]=40; /* allocation Legnth */
  1534. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
  1535. AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
  1536. if (c!=0)
  1537. return;
  1538. /* copy device ident strings */
  1539. ident_cpy(dev_desc->vendor,&iobuf[8],8);
  1540. ident_cpy(dev_desc->product,&iobuf[16],16);
  1541. ident_cpy(dev_desc->revision,&iobuf[32],5);
  1542. dev_desc->lun=0;
  1543. dev_desc->lba=0;
  1544. dev_desc->blksz=0;
  1545. dev_desc->type=iobuf[0] & 0x1f;
  1546. if ((iobuf[1]&0x80)==0x80)
  1547. dev_desc->removable = 1;
  1548. else
  1549. dev_desc->removable = 0;
  1550. memset(ccb,0,sizeof(ccb));
  1551. memset(iobuf,0,sizeof(iobuf));
  1552. ccb[0]=ATAPI_CMD_START_STOP;
  1553. ccb[4]=0x03; /* start */
  1554. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1555. AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
  1556. if (c!=0)
  1557. return;
  1558. memset(ccb,0,sizeof(ccb));
  1559. memset(iobuf,0,sizeof(iobuf));
  1560. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1561. AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
  1562. if (c!=0)
  1563. return;
  1564. memset(ccb,0,sizeof(ccb));
  1565. memset(iobuf,0,sizeof(iobuf));
  1566. ccb[0]=ATAPI_CMD_READ_CAP;
  1567. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
  1568. AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
  1569. if (c!=0)
  1570. return;
  1571. AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1572. iobuf[0],iobuf[1],iobuf[2],iobuf[3],
  1573. iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
  1574. dev_desc->lba =((unsigned long)iobuf[0]<<24) +
  1575. ((unsigned long)iobuf[1]<<16) +
  1576. ((unsigned long)iobuf[2]<< 8) +
  1577. ((unsigned long)iobuf[3]);
  1578. dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
  1579. ((unsigned long)iobuf[5]<<16) +
  1580. ((unsigned long)iobuf[6]<< 8) +
  1581. ((unsigned long)iobuf[7]);
  1582. return;
  1583. }
  1584. /*
  1585. * atapi_read:
  1586. * we transfer only one block per command, since the multiple DRQ per
  1587. * command is not yet implemented
  1588. */
  1589. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1590. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1591. #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
  1592. ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  1593. {
  1594. ulong n = 0;
  1595. unsigned char ccb[12]; /* Command descriptor block */
  1596. ulong cnt;
  1597. AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1598. device, blknr, blkcnt, (ulong)buffer);
  1599. do {
  1600. if (blkcnt>ATAPI_READ_MAX_BLOCK) {
  1601. cnt=ATAPI_READ_MAX_BLOCK;
  1602. } else {
  1603. cnt=blkcnt;
  1604. }
  1605. ccb[0]=ATAPI_CMD_READ_12;
  1606. ccb[1]=0; /* reserved */
  1607. ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
  1608. ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
  1609. ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
  1610. ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
  1611. ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
  1612. ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
  1613. ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
  1614. ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
  1615. ccb[10]=0; /* reserved */
  1616. ccb[11]=0; /* reserved */
  1617. if (atapi_issue_autoreq(device,ccb,12,
  1618. (unsigned char *)buffer,
  1619. cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
  1620. return (n);
  1621. }
  1622. n+=cnt;
  1623. blkcnt-=cnt;
  1624. blknr+=cnt;
  1625. buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
  1626. } while (blkcnt > 0);
  1627. return (n);
  1628. }
  1629. /* ------------------------------------------------------------------------- */
  1630. #endif /* CONFIG_ATAPI */
  1631. U_BOOT_CMD(
  1632. ide, 5, 1, do_ide,
  1633. "ide - IDE sub-system\n",
  1634. "reset - reset IDE controller\n"
  1635. "ide info - show available IDE devices\n"
  1636. "ide device [dev] - show or set current device\n"
  1637. "ide part [dev] - print partition table of one or all IDE devices\n"
  1638. "ide read addr blk# cnt\n"
  1639. "ide write addr blk# cnt - read/write `cnt'"
  1640. " blocks starting at block `blk#'\n"
  1641. " to/from memory address `addr'\n"
  1642. );
  1643. U_BOOT_CMD(
  1644. diskboot, 3, 1, do_diskboot,
  1645. "diskboot- boot from IDE device\n",
  1646. "loadAddr dev:part\n"
  1647. );
  1648. #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */