cmd_ide.c 45 KB

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