cmd_ide.c 50 KB

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