cmd_ide.c 50 KB

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