cmd_ide.c 52 KB

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