cmd_ide.c 50 KB

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