cmd_ide.c 50 KB

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