cmd_ide.c 43 KB

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