cmd_ide.c 44 KB

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