cmd_ide.c 50 KB

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