cmd_ide.c 51 KB

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