cmd_ide.c 51 KB

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