cmd_ide.c 50 KB

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