cmd_ide.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. /*
  2. * (C) Copyright 2000-2011
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * IDE support
  26. */
  27. #include <common.h>
  28. #include <config.h>
  29. #include <watchdog.h>
  30. #include <command.h>
  31. #include <image.h>
  32. #include <asm/byteorder.h>
  33. #include <asm/io.h>
  34. #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
  35. # include <pcmcia.h>
  36. #endif
  37. #ifdef CONFIG_8xx
  38. # include <mpc8xx.h>
  39. #endif
  40. #ifdef CONFIG_MPC5xxx
  41. #include <mpc5xxx.h>
  42. #endif
  43. #include <ide.h>
  44. #include <ata.h>
  45. #ifdef CONFIG_STATUS_LED
  46. # include <status_led.h>
  47. #endif
  48. #ifdef __PPC__
  49. # define EIEIO __asm__ volatile ("eieio")
  50. # define SYNC __asm__ volatile ("sync")
  51. #else
  52. # define EIEIO /* nothing */
  53. # define SYNC /* nothing */
  54. #endif
  55. /* ------------------------------------------------------------------------- */
  56. /* Current I/O Device */
  57. static int curr_device = -1;
  58. /* Current offset for IDE0 / IDE1 bus access */
  59. ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
  60. #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
  61. CONFIG_SYS_ATA_IDE0_OFFSET,
  62. #endif
  63. #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
  64. CONFIG_SYS_ATA_IDE1_OFFSET,
  65. #endif
  66. };
  67. static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
  68. block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
  69. /* ------------------------------------------------------------------------- */
  70. #ifdef CONFIG_IDE_LED
  71. # if !defined(CONFIG_BMS2003) && \
  72. !defined(CONFIG_CPC45) && \
  73. !defined(CONFIG_KUP4K) && \
  74. !defined(CONFIG_KUP4X)
  75. static void ide_led (uchar led, uchar status);
  76. #else
  77. extern void ide_led (uchar led, uchar status);
  78. #endif
  79. #else
  80. #define ide_led(a,b) /* dummy */
  81. #endif
  82. #ifdef CONFIG_IDE_RESET
  83. static void ide_reset (void);
  84. #else
  85. #define ide_reset() /* dummy */
  86. #endif
  87. static void ide_ident (block_dev_desc_t *dev_desc);
  88. static uchar ide_wait (int dev, ulong t);
  89. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  90. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  91. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  92. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  93. #ifndef CONFIG_SYS_ATA_PORT_ADDR
  94. #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
  95. #endif
  96. #ifdef CONFIG_ATAPI
  97. static void atapi_inquiry(block_dev_desc_t *dev_desc);
  98. ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
  99. #endif
  100. /* ------------------------------------------------------------------------- */
  101. int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  102. {
  103. int rcode = 0;
  104. switch (argc) {
  105. case 0:
  106. case 1:
  107. return CMD_RET_USAGE;
  108. case 2:
  109. if (strncmp(argv[1], "res", 3) == 0) {
  110. puts("\nReset IDE"
  111. #ifdef CONFIG_IDE_8xx_DIRECT
  112. " on PCMCIA " PCMCIA_SLOT_MSG
  113. #endif
  114. ": ");
  115. ide_init();
  116. return 0;
  117. } else if (strncmp(argv[1], "inf", 3) == 0) {
  118. int i;
  119. putc('\n');
  120. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
  121. if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
  122. continue; /* list only known devices */
  123. printf("IDE device %d: ", i);
  124. dev_print(&ide_dev_desc[i]);
  125. }
  126. return 0;
  127. } else if (strncmp(argv[1], "dev", 3) == 0) {
  128. if ((curr_device < 0)
  129. || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
  130. puts("\nno IDE devices available\n");
  131. return 1;
  132. }
  133. printf("\nIDE device %d: ", curr_device);
  134. dev_print(&ide_dev_desc[curr_device]);
  135. return 0;
  136. } else if (strncmp(argv[1], "part", 4) == 0) {
  137. int dev, ok;
  138. for (ok = 0, dev = 0;
  139. dev < CONFIG_SYS_IDE_MAXDEVICE;
  140. ++dev) {
  141. if (ide_dev_desc[dev].part_type !=
  142. PART_TYPE_UNKNOWN) {
  143. ++ok;
  144. if (dev)
  145. putc('\n');
  146. print_part(&ide_dev_desc[dev]);
  147. }
  148. }
  149. if (!ok) {
  150. puts("\nno IDE devices available\n");
  151. rcode++;
  152. }
  153. return rcode;
  154. }
  155. return CMD_RET_USAGE;
  156. case 3:
  157. if (strncmp(argv[1], "dev", 3) == 0) {
  158. int dev = (int) simple_strtoul(argv[2], NULL, 10);
  159. printf("\nIDE device %d: ", dev);
  160. if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
  161. puts("unknown device\n");
  162. return 1;
  163. }
  164. dev_print(&ide_dev_desc[dev]);
  165. /*ide_print (dev); */
  166. if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
  167. return 1;
  168. curr_device = dev;
  169. puts("... is now current device\n");
  170. return 0;
  171. } else if (strncmp(argv[1], "part", 4) == 0) {
  172. int dev = (int) simple_strtoul(argv[2], NULL, 10);
  173. if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  174. print_part(&ide_dev_desc[dev]);
  175. } else {
  176. printf("\nIDE device %d not available\n",
  177. dev);
  178. rcode = 1;
  179. }
  180. return rcode;
  181. }
  182. return CMD_RET_USAGE;
  183. default:
  184. /* at least 4 args */
  185. if (strcmp(argv[1], "read") == 0) {
  186. ulong addr = simple_strtoul(argv[2], NULL, 16);
  187. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  188. ulong n;
  189. #ifdef CONFIG_SYS_64BIT_LBA
  190. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  191. printf("\nIDE read: device %d block # %lld, count %ld ... ",
  192. curr_device, blk, cnt);
  193. #else
  194. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  195. printf("\nIDE read: device %d block # %ld, count %ld ... ",
  196. curr_device, blk, cnt);
  197. #endif
  198. n = ide_dev_desc[curr_device].block_read(curr_device,
  199. blk, cnt,
  200. (ulong *)addr);
  201. /* flush cache after read */
  202. flush_cache(addr,
  203. cnt * ide_dev_desc[curr_device].blksz);
  204. printf("%ld blocks read: %s\n",
  205. n, (n == cnt) ? "OK" : "ERROR");
  206. if (n == cnt)
  207. return 0;
  208. else
  209. return 1;
  210. } else if (strcmp(argv[1], "write") == 0) {
  211. ulong addr = simple_strtoul(argv[2], NULL, 16);
  212. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  213. ulong n;
  214. #ifdef CONFIG_SYS_64BIT_LBA
  215. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  216. printf("\nIDE write: device %d block # %lld, count %ld ... ",
  217. curr_device, blk, cnt);
  218. #else
  219. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  220. printf("\nIDE write: device %d block # %ld, count %ld ... ",
  221. curr_device, blk, cnt);
  222. #endif
  223. n = ide_write(curr_device, blk, cnt, (ulong *) addr);
  224. printf("%ld blocks written: %s\n",
  225. n, (n == cnt) ? "OK" : "ERROR");
  226. if (n == cnt)
  227. return 0;
  228. else
  229. return 1;
  230. } else {
  231. return CMD_RET_USAGE;
  232. }
  233. return rcode;
  234. }
  235. }
  236. int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  237. {
  238. return common_diskboot(cmdtp, "ide", argc, argv);
  239. }
  240. /* ------------------------------------------------------------------------- */
  241. inline void __ide_outb(int dev, int port, unsigned char val)
  242. {
  243. debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
  244. dev, port, val,
  245. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  246. #if defined(CONFIG_IDE_AHB)
  247. if (port) {
  248. /* write command */
  249. ide_write_register(dev, port, val);
  250. } else {
  251. /* write data */
  252. outb(val, (ATA_CURR_BASE(dev)));
  253. }
  254. #else
  255. outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  256. #endif
  257. }
  258. void ide_outb(int dev, int port, unsigned char val)
  259. __attribute__ ((weak, alias("__ide_outb")));
  260. inline unsigned char __ide_inb(int dev, int port)
  261. {
  262. uchar val;
  263. #if defined(CONFIG_IDE_AHB)
  264. val = ide_read_register(dev, port);
  265. #else
  266. val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  267. #endif
  268. debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
  269. dev, port,
  270. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
  271. return val;
  272. }
  273. unsigned char ide_inb(int dev, int port)
  274. __attribute__ ((weak, alias("__ide_inb")));
  275. #ifdef CONFIG_TUNE_PIO
  276. inline int __ide_set_piomode(int pio_mode)
  277. {
  278. return 0;
  279. }
  280. inline int ide_set_piomode(int pio_mode)
  281. __attribute__ ((weak, alias("__ide_set_piomode")));
  282. #endif
  283. void ide_init(void)
  284. {
  285. unsigned char c;
  286. int i, bus;
  287. #ifdef CONFIG_IDE_8xx_PCCARD
  288. extern int ide_devices_found; /* Initialized in check_ide_device() */
  289. #endif /* CONFIG_IDE_8xx_PCCARD */
  290. #ifdef CONFIG_IDE_PREINIT
  291. WATCHDOG_RESET();
  292. if (ide_preinit()) {
  293. puts("ide_preinit failed\n");
  294. return;
  295. }
  296. #endif /* CONFIG_IDE_PREINIT */
  297. WATCHDOG_RESET();
  298. /*
  299. * Reset the IDE just to be sure.
  300. * Light LED's to show
  301. */
  302. ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  303. /* ATAPI Drives seems to need a proper IDE Reset */
  304. ide_reset();
  305. #ifdef CONFIG_IDE_INIT_POSTRESET
  306. WATCHDOG_RESET();
  307. if (ide_init_postreset()) {
  308. puts("ide_preinit_postreset failed\n");
  309. return;
  310. }
  311. #endif /* CONFIG_IDE_INIT_POSTRESET */
  312. /*
  313. * Wait for IDE to get ready.
  314. * According to spec, this can take up to 31 seconds!
  315. */
  316. for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
  317. int dev =
  318. bus * (CONFIG_SYS_IDE_MAXDEVICE /
  319. CONFIG_SYS_IDE_MAXBUS);
  320. #ifdef CONFIG_IDE_8xx_PCCARD
  321. /* Skip non-ide devices from probing */
  322. if ((ide_devices_found & (1 << bus)) == 0) {
  323. ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  324. continue;
  325. }
  326. #endif
  327. printf("Bus %d: ", bus);
  328. ide_bus_ok[bus] = 0;
  329. /* Select device
  330. */
  331. udelay(100000); /* 100 ms */
  332. ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  333. udelay(100000); /* 100 ms */
  334. i = 0;
  335. do {
  336. udelay(10000); /* 10 ms */
  337. c = ide_inb(dev, ATA_STATUS);
  338. i++;
  339. if (i > (ATA_RESET_TIME * 100)) {
  340. puts("** Timeout **\n");
  341. /* LED's off */
  342. ide_led((LED_IDE1 | LED_IDE2), 0);
  343. return;
  344. }
  345. if ((i >= 100) && ((i % 100) == 0))
  346. putc('.');
  347. } while (c & ATA_STAT_BUSY);
  348. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  349. puts("not available ");
  350. debug("Status = 0x%02X ", c);
  351. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  352. } else if ((c & ATA_STAT_READY) == 0) {
  353. puts("not available ");
  354. debug("Status = 0x%02X ", c);
  355. #endif
  356. } else {
  357. puts("OK ");
  358. ide_bus_ok[bus] = 1;
  359. }
  360. WATCHDOG_RESET();
  361. }
  362. putc('\n');
  363. ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  364. curr_device = -1;
  365. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
  366. #ifdef CONFIG_IDE_LED
  367. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  368. #endif
  369. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  370. ide_dev_desc[i].if_type = IF_TYPE_IDE;
  371. ide_dev_desc[i].dev = i;
  372. ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
  373. ide_dev_desc[i].blksz = 0;
  374. ide_dev_desc[i].lba = 0;
  375. ide_dev_desc[i].block_read = ide_read;
  376. ide_dev_desc[i].block_write = ide_write;
  377. if (!ide_bus_ok[IDE_BUS(i)])
  378. continue;
  379. ide_led(led, 1); /* LED on */
  380. ide_ident(&ide_dev_desc[i]);
  381. ide_led(led, 0); /* LED off */
  382. dev_print(&ide_dev_desc[i]);
  383. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  384. /* initialize partition type */
  385. init_part(&ide_dev_desc[i]);
  386. if (curr_device < 0)
  387. curr_device = i;
  388. }
  389. }
  390. WATCHDOG_RESET();
  391. }
  392. /* ------------------------------------------------------------------------- */
  393. #ifdef CONFIG_PARTITIONS
  394. block_dev_desc_t *ide_get_dev(int dev)
  395. {
  396. return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
  397. }
  398. #endif
  399. /* ------------------------------------------------------------------------- */
  400. void ide_input_swap_data(int dev, ulong *sect_buf, int words)
  401. __attribute__ ((weak, alias("__ide_input_swap_data")));
  402. void ide_input_data(int dev, ulong *sect_buf, int words)
  403. __attribute__ ((weak, alias("__ide_input_data")));
  404. void ide_output_data(int dev, const ulong *sect_buf, int words)
  405. __attribute__ ((weak, alias("__ide_output_data")));
  406. /* We only need to swap data if we are running on a big endian cpu. */
  407. /* But Au1x00 cpu:s already swaps data in big endian mode! */
  408. #if defined(__LITTLE_ENDIAN) || defined(CONFIG_SOC_AU1X00)
  409. void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
  410. {
  411. ide_input_data(dev, sect_buf, words);
  412. }
  413. #else
  414. void __ide_input_swap_data(int dev, ulong *sect_buf, int words)
  415. {
  416. #if defined(CONFIG_CPC45)
  417. uchar i;
  418. volatile uchar *pbuf_even =
  419. (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
  420. volatile uchar *pbuf_odd =
  421. (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
  422. ushort *dbuf = (ushort *) sect_buf;
  423. while (words--) {
  424. for (i = 0; i < 2; i++) {
  425. *(((uchar *) (dbuf)) + 1) = *pbuf_even;
  426. *(uchar *) dbuf = *pbuf_odd;
  427. dbuf += 1;
  428. }
  429. }
  430. #else
  431. volatile ushort *pbuf =
  432. (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  433. ushort *dbuf = (ushort *) sect_buf;
  434. debug("in input swap data base for read is %lx\n",
  435. (unsigned long) pbuf);
  436. while (words--) {
  437. #ifdef __MIPS__
  438. *dbuf++ = swab16p((u16 *) pbuf);
  439. *dbuf++ = swab16p((u16 *) pbuf);
  440. #elif defined(CONFIG_PCS440EP)
  441. *dbuf++ = *pbuf;
  442. *dbuf++ = *pbuf;
  443. #else
  444. *dbuf++ = ld_le16(pbuf);
  445. *dbuf++ = ld_le16(pbuf);
  446. #endif /* !MIPS */
  447. }
  448. #endif
  449. }
  450. #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
  451. #if defined(CONFIG_IDE_SWAP_IO)
  452. void __ide_output_data(int dev, const ulong *sect_buf, int words)
  453. {
  454. #if defined(CONFIG_CPC45)
  455. uchar *dbuf;
  456. volatile uchar *pbuf_even;
  457. volatile uchar *pbuf_odd;
  458. pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
  459. pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
  460. dbuf = (uchar *) sect_buf;
  461. while (words--) {
  462. EIEIO;
  463. *pbuf_even = *dbuf++;
  464. EIEIO;
  465. *pbuf_odd = *dbuf++;
  466. EIEIO;
  467. *pbuf_even = *dbuf++;
  468. EIEIO;
  469. *pbuf_odd = *dbuf++;
  470. }
  471. #else
  472. ushort *dbuf;
  473. volatile ushort *pbuf;
  474. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  475. dbuf = (ushort *) sect_buf;
  476. while (words--) {
  477. #if defined(CONFIG_PCS440EP)
  478. /* not tested, because CF was write protected */
  479. EIEIO;
  480. *pbuf = ld_le16(dbuf++);
  481. EIEIO;
  482. *pbuf = ld_le16(dbuf++);
  483. #else
  484. EIEIO;
  485. *pbuf = *dbuf++;
  486. EIEIO;
  487. *pbuf = *dbuf++;
  488. #endif
  489. }
  490. #endif
  491. }
  492. #else /* ! CONFIG_IDE_SWAP_IO */
  493. void __ide_output_data(int dev, const ulong *sect_buf, int words)
  494. {
  495. #if defined(CONFIG_IDE_AHB)
  496. ide_write_data(dev, sect_buf, words);
  497. #else
  498. outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
  499. #endif
  500. }
  501. #endif /* CONFIG_IDE_SWAP_IO */
  502. #if defined(CONFIG_IDE_SWAP_IO)
  503. void __ide_input_data(int dev, ulong *sect_buf, int words)
  504. {
  505. #if defined(CONFIG_CPC45)
  506. uchar *dbuf;
  507. volatile uchar *pbuf_even;
  508. volatile uchar *pbuf_odd;
  509. pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
  510. pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
  511. dbuf = (uchar *) sect_buf;
  512. while (words--) {
  513. *dbuf++ = *pbuf_even;
  514. EIEIO;
  515. SYNC;
  516. *dbuf++ = *pbuf_odd;
  517. EIEIO;
  518. SYNC;
  519. *dbuf++ = *pbuf_even;
  520. EIEIO;
  521. SYNC;
  522. *dbuf++ = *pbuf_odd;
  523. EIEIO;
  524. SYNC;
  525. }
  526. #else
  527. ushort *dbuf;
  528. volatile ushort *pbuf;
  529. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  530. dbuf = (ushort *) sect_buf;
  531. debug("in input data base for read is %lx\n", (unsigned long) pbuf);
  532. while (words--) {
  533. #if defined(CONFIG_PCS440EP)
  534. EIEIO;
  535. *dbuf++ = ld_le16(pbuf);
  536. EIEIO;
  537. *dbuf++ = ld_le16(pbuf);
  538. #else
  539. EIEIO;
  540. *dbuf++ = *pbuf;
  541. EIEIO;
  542. *dbuf++ = *pbuf;
  543. #endif
  544. }
  545. #endif
  546. }
  547. #else /* ! CONFIG_IDE_SWAP_IO */
  548. void __ide_input_data(int dev, ulong *sect_buf, int words)
  549. {
  550. #if defined(CONFIG_IDE_AHB)
  551. ide_read_data(dev, sect_buf, words);
  552. #else
  553. insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
  554. #endif
  555. }
  556. #endif /* CONFIG_IDE_SWAP_IO */
  557. /* -------------------------------------------------------------------------
  558. */
  559. static void ide_ident(block_dev_desc_t *dev_desc)
  560. {
  561. unsigned char c;
  562. hd_driveid_t iop;
  563. #ifdef CONFIG_ATAPI
  564. int retries = 0;
  565. #endif
  566. #ifdef CONFIG_TUNE_PIO
  567. int pio_mode;
  568. #endif
  569. #if 0
  570. int mode, cycle_time;
  571. #endif
  572. int device;
  573. device = dev_desc->dev;
  574. printf(" Device %d: ", device);
  575. ide_led(DEVICE_LED(device), 1); /* LED on */
  576. /* Select device
  577. */
  578. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  579. dev_desc->if_type = IF_TYPE_IDE;
  580. #ifdef CONFIG_ATAPI
  581. retries = 0;
  582. /* Warning: This will be tricky to read */
  583. while (retries <= 1) {
  584. /* check signature */
  585. if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
  586. (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
  587. (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
  588. (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
  589. /* ATAPI Signature found */
  590. dev_desc->if_type = IF_TYPE_ATAPI;
  591. /*
  592. * Start Ident Command
  593. */
  594. ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
  595. /*
  596. * Wait for completion - ATAPI devices need more time
  597. * to become ready
  598. */
  599. c = ide_wait(device, ATAPI_TIME_OUT);
  600. } else
  601. #endif
  602. {
  603. /*
  604. * Start Ident Command
  605. */
  606. ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
  607. /*
  608. * Wait for completion
  609. */
  610. c = ide_wait(device, IDE_TIME_OUT);
  611. }
  612. ide_led(DEVICE_LED(device), 0); /* LED off */
  613. if (((c & ATA_STAT_DRQ) == 0) ||
  614. ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
  615. #ifdef CONFIG_ATAPI
  616. {
  617. /*
  618. * Need to soft reset the device
  619. * in case it's an ATAPI...
  620. */
  621. debug("Retrying...\n");
  622. ide_outb(device, ATA_DEV_HD,
  623. ATA_LBA | ATA_DEVICE(device));
  624. udelay(100000);
  625. ide_outb(device, ATA_COMMAND, 0x08);
  626. udelay(500000); /* 500 ms */
  627. }
  628. /*
  629. * Select device
  630. */
  631. ide_outb(device, ATA_DEV_HD,
  632. ATA_LBA | ATA_DEVICE(device));
  633. retries++;
  634. #else
  635. return;
  636. #endif
  637. }
  638. #ifdef CONFIG_ATAPI
  639. else
  640. break;
  641. } /* see above - ugly to read */
  642. if (retries == 2) /* Not found */
  643. return;
  644. #endif
  645. ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
  646. ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
  647. sizeof(dev_desc->revision));
  648. ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
  649. sizeof(dev_desc->vendor));
  650. ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
  651. sizeof(dev_desc->product));
  652. #ifdef __LITTLE_ENDIAN
  653. /*
  654. * firmware revision, model, and serial number have Big Endian Byte
  655. * order in Word. Convert all three to little endian.
  656. *
  657. * See CF+ and CompactFlash Specification Revision 2.0:
  658. * 6.2.1.6: Identify Drive, Table 39 for more details
  659. */
  660. strswab(dev_desc->revision);
  661. strswab(dev_desc->vendor);
  662. strswab(dev_desc->product);
  663. #endif /* __LITTLE_ENDIAN */
  664. if ((iop.config & 0x0080) == 0x0080)
  665. dev_desc->removable = 1;
  666. else
  667. dev_desc->removable = 0;
  668. #ifdef CONFIG_TUNE_PIO
  669. /* Mode 0 - 2 only, are directly determined by word 51. */
  670. pio_mode = iop.tPIO;
  671. if (pio_mode > 2) {
  672. printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
  673. /* Force it to dead slow, and hope for the best... */
  674. pio_mode = 0;
  675. }
  676. /* Any CompactFlash Storage Card that supports PIO mode 3 or above
  677. * shall set bit 1 of word 53 to one and support the fields contained
  678. * in words 64 through 70.
  679. */
  680. if (iop.field_valid & 0x02) {
  681. /*
  682. * Mode 3 and above are possible. Check in order from slow
  683. * to fast, so we wind up with the highest mode allowed.
  684. */
  685. if (iop.eide_pio_modes & 0x01)
  686. pio_mode = 3;
  687. if (iop.eide_pio_modes & 0x02)
  688. pio_mode = 4;
  689. if (ata_id_is_cfa((u16 *)&iop)) {
  690. if ((iop.cf_advanced_caps & 0x07) == 0x01)
  691. pio_mode = 5;
  692. if ((iop.cf_advanced_caps & 0x07) == 0x02)
  693. pio_mode = 6;
  694. }
  695. }
  696. /* System-specific, depends on bus speeds, etc. */
  697. ide_set_piomode(pio_mode);
  698. #endif /* CONFIG_TUNE_PIO */
  699. #if 0
  700. /*
  701. * Drive PIO mode autoselection
  702. */
  703. mode = iop.tPIO;
  704. printf("tPIO = 0x%02x = %d\n", mode, mode);
  705. if (mode > 2) { /* 2 is maximum allowed tPIO value */
  706. mode = 2;
  707. debug("Override tPIO -> 2\n");
  708. }
  709. if (iop.field_valid & 2) { /* drive implements ATA2? */
  710. debug("Drive implements ATA2\n");
  711. if (iop.capability & 8) { /* drive supports use_iordy? */
  712. cycle_time = iop.eide_pio_iordy;
  713. } else {
  714. cycle_time = iop.eide_pio;
  715. }
  716. debug("cycle time = %d\n", cycle_time);
  717. mode = 4;
  718. if (cycle_time > 120)
  719. mode = 3; /* 120 ns for PIO mode 4 */
  720. if (cycle_time > 180)
  721. mode = 2; /* 180 ns for PIO mode 3 */
  722. if (cycle_time > 240)
  723. mode = 1; /* 240 ns for PIO mode 4 */
  724. if (cycle_time > 383)
  725. mode = 0; /* 383 ns for PIO mode 4 */
  726. }
  727. printf("PIO mode to use: PIO %d\n", mode);
  728. #endif /* 0 */
  729. #ifdef CONFIG_ATAPI
  730. if (dev_desc->if_type == IF_TYPE_ATAPI) {
  731. atapi_inquiry(dev_desc);
  732. return;
  733. }
  734. #endif /* CONFIG_ATAPI */
  735. #ifdef __BIG_ENDIAN
  736. /* swap shorts */
  737. dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
  738. #else /* ! __BIG_ENDIAN */
  739. /*
  740. * do not swap shorts on little endian
  741. *
  742. * See CF+ and CompactFlash Specification Revision 2.0:
  743. * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
  744. */
  745. dev_desc->lba = iop.lba_capacity;
  746. #endif /* __BIG_ENDIAN */
  747. #ifdef CONFIG_LBA48
  748. if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
  749. dev_desc->lba48 = 1;
  750. dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
  751. ((unsigned long long) iop.lba48_capacity[1] << 16) |
  752. ((unsigned long long) iop.lba48_capacity[2] << 32) |
  753. ((unsigned long long) iop.lba48_capacity[3] << 48);
  754. } else {
  755. dev_desc->lba48 = 0;
  756. }
  757. #endif /* CONFIG_LBA48 */
  758. /* assuming HD */
  759. dev_desc->type = DEV_TYPE_HARDDISK;
  760. dev_desc->blksz = ATA_BLOCKSIZE;
  761. dev_desc->lun = 0; /* just to fill something in... */
  762. #if 0 /* only used to test the powersaving mode,
  763. * if enabled, the drive goes after 5 sec
  764. * in standby mode */
  765. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  766. c = ide_wait(device, IDE_TIME_OUT);
  767. ide_outb(device, ATA_SECT_CNT, 1);
  768. ide_outb(device, ATA_LBA_LOW, 0);
  769. ide_outb(device, ATA_LBA_MID, 0);
  770. ide_outb(device, ATA_LBA_HIGH, 0);
  771. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  772. ide_outb(device, ATA_COMMAND, 0xe3);
  773. udelay(50);
  774. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  775. #endif
  776. }
  777. /* ------------------------------------------------------------------------- */
  778. ulong ide_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  779. {
  780. ulong n = 0;
  781. unsigned char c;
  782. unsigned char pwrsave = 0; /* power save */
  783. #ifdef CONFIG_LBA48
  784. unsigned char lba48 = 0;
  785. if (blknr & 0x0000fffff0000000ULL) {
  786. /* more than 28 bits used, use 48bit mode */
  787. lba48 = 1;
  788. }
  789. #endif
  790. debug("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
  791. device, blknr, blkcnt, (ulong) buffer);
  792. ide_led(DEVICE_LED(device), 1); /* LED on */
  793. /* Select device
  794. */
  795. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  796. c = ide_wait(device, IDE_TIME_OUT);
  797. if (c & ATA_STAT_BUSY) {
  798. printf("IDE read: device %d not ready\n", device);
  799. goto IDE_READ_E;
  800. }
  801. /* first check if the drive is in Powersaving mode, if yes,
  802. * increase the timeout value */
  803. ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  804. udelay(50);
  805. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  806. if (c & ATA_STAT_BUSY) {
  807. printf("IDE read: device %d not ready\n", device);
  808. goto IDE_READ_E;
  809. }
  810. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  811. printf("No Powersaving mode %X\n", c);
  812. } else {
  813. c = ide_inb(device, ATA_SECT_CNT);
  814. debug("Powersaving %02X\n", c);
  815. if (c == 0)
  816. pwrsave = 1;
  817. }
  818. while (blkcnt-- > 0) {
  819. c = ide_wait(device, IDE_TIME_OUT);
  820. if (c & ATA_STAT_BUSY) {
  821. printf("IDE read: device %d not ready\n", device);
  822. break;
  823. }
  824. #ifdef CONFIG_LBA48
  825. if (lba48) {
  826. /* write high bits */
  827. ide_outb(device, ATA_SECT_CNT, 0);
  828. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  829. #ifdef CONFIG_SYS_64BIT_LBA
  830. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  831. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  832. #else
  833. ide_outb(device, ATA_LBA_MID, 0);
  834. ide_outb(device, ATA_LBA_HIGH, 0);
  835. #endif
  836. }
  837. #endif
  838. ide_outb(device, ATA_SECT_CNT, 1);
  839. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  840. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  841. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  842. #ifdef CONFIG_LBA48
  843. if (lba48) {
  844. ide_outb(device, ATA_DEV_HD,
  845. ATA_LBA | ATA_DEVICE(device));
  846. ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
  847. } else
  848. #endif
  849. {
  850. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  851. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  852. ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
  853. }
  854. udelay(50);
  855. if (pwrsave) {
  856. /* may take up to 4 sec */
  857. c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
  858. pwrsave = 0;
  859. } else {
  860. /* can't take over 500 ms */
  861. c = ide_wait(device, IDE_TIME_OUT);
  862. }
  863. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  864. ATA_STAT_DRQ) {
  865. #if defined(CONFIG_SYS_64BIT_LBA)
  866. printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
  867. device, blknr, c);
  868. #else
  869. printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  870. device, (ulong) blknr, c);
  871. #endif
  872. break;
  873. }
  874. ide_input_data(device, buffer, ATA_SECTORWORDS);
  875. (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
  876. ++n;
  877. ++blknr;
  878. buffer += ATA_BLOCKSIZE;
  879. }
  880. IDE_READ_E:
  881. ide_led(DEVICE_LED(device), 0); /* LED off */
  882. return (n);
  883. }
  884. /* ------------------------------------------------------------------------- */
  885. ulong ide_write(int device, lbaint_t blknr, ulong blkcnt, const void *buffer)
  886. {
  887. ulong n = 0;
  888. unsigned char c;
  889. #ifdef CONFIG_LBA48
  890. unsigned char lba48 = 0;
  891. if (blknr & 0x0000fffff0000000ULL) {
  892. /* more than 28 bits used, use 48bit mode */
  893. lba48 = 1;
  894. }
  895. #endif
  896. ide_led(DEVICE_LED(device), 1); /* LED on */
  897. /* Select device
  898. */
  899. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  900. while (blkcnt-- > 0) {
  901. c = ide_wait(device, IDE_TIME_OUT);
  902. if (c & ATA_STAT_BUSY) {
  903. printf("IDE read: device %d not ready\n", device);
  904. goto WR_OUT;
  905. }
  906. #ifdef CONFIG_LBA48
  907. if (lba48) {
  908. /* write high bits */
  909. ide_outb(device, ATA_SECT_CNT, 0);
  910. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  911. #ifdef CONFIG_SYS_64BIT_LBA
  912. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  913. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  914. #else
  915. ide_outb(device, ATA_LBA_MID, 0);
  916. ide_outb(device, ATA_LBA_HIGH, 0);
  917. #endif
  918. }
  919. #endif
  920. ide_outb(device, ATA_SECT_CNT, 1);
  921. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  922. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  923. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  924. #ifdef CONFIG_LBA48
  925. if (lba48) {
  926. ide_outb(device, ATA_DEV_HD,
  927. ATA_LBA | ATA_DEVICE(device));
  928. ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
  929. } else
  930. #endif
  931. {
  932. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  933. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  934. ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
  935. }
  936. udelay(50);
  937. /* can't take over 500 ms */
  938. c = ide_wait(device, IDE_TIME_OUT);
  939. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  940. ATA_STAT_DRQ) {
  941. #if defined(CONFIG_SYS_64BIT_LBA)
  942. printf("Error (no IRQ) dev %d blk %lld: status 0x%02x\n",
  943. device, blknr, c);
  944. #else
  945. printf("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  946. device, (ulong) blknr, c);
  947. #endif
  948. goto WR_OUT;
  949. }
  950. ide_output_data(device, buffer, ATA_SECTORWORDS);
  951. c = ide_inb(device, ATA_STATUS); /* clear IRQ */
  952. ++n;
  953. ++blknr;
  954. buffer += ATA_BLOCKSIZE;
  955. }
  956. WR_OUT:
  957. ide_led(DEVICE_LED(device), 0); /* LED off */
  958. return (n);
  959. }
  960. /* ------------------------------------------------------------------------- */
  961. /*
  962. * copy src to dest, skipping leading and trailing blanks and null
  963. * terminate the string
  964. * "len" is the size of available memory including the terminating '\0'
  965. */
  966. static void ident_cpy(unsigned char *dst, unsigned char *src,
  967. unsigned int len)
  968. {
  969. unsigned char *end, *last;
  970. last = dst;
  971. end = src + len - 1;
  972. /* reserve space for '\0' */
  973. if (len < 2)
  974. goto OUT;
  975. /* skip leading white space */
  976. while ((*src) && (src < end) && (*src == ' '))
  977. ++src;
  978. /* copy string, omitting trailing white space */
  979. while ((*src) && (src < end)) {
  980. *dst++ = *src;
  981. if (*src++ != ' ')
  982. last = dst;
  983. }
  984. OUT:
  985. *last = '\0';
  986. }
  987. /* ------------------------------------------------------------------------- */
  988. /*
  989. * Wait until Busy bit is off, or timeout (in ms)
  990. * Return last status
  991. */
  992. static uchar ide_wait(int dev, ulong t)
  993. {
  994. ulong delay = 10 * t; /* poll every 100 us */
  995. uchar c;
  996. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  997. udelay(100);
  998. if (delay-- == 0)
  999. break;
  1000. }
  1001. return (c);
  1002. }
  1003. /* ------------------------------------------------------------------------- */
  1004. #ifdef CONFIG_IDE_RESET
  1005. extern void ide_set_reset(int idereset);
  1006. static void ide_reset(void)
  1007. {
  1008. int i;
  1009. curr_device = -1;
  1010. for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
  1011. ide_bus_ok[i] = 0;
  1012. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
  1013. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  1014. ide_set_reset(1); /* assert reset */
  1015. /* the reset signal shall be asserted for et least 25 us */
  1016. udelay(25);
  1017. WATCHDOG_RESET();
  1018. /* de-assert RESET signal */
  1019. ide_set_reset(0);
  1020. /* wait 250 ms */
  1021. for (i = 0; i < 250; ++i)
  1022. udelay(1000);
  1023. }
  1024. #endif /* CONFIG_IDE_RESET */
  1025. /* ------------------------------------------------------------------------- */
  1026. #if defined(CONFIG_IDE_LED) && \
  1027. !defined(CONFIG_CPC45) && \
  1028. !defined(CONFIG_KUP4K) && \
  1029. !defined(CONFIG_KUP4X)
  1030. static uchar led_buffer; /* Buffer for current LED status */
  1031. static void ide_led(uchar led, uchar status)
  1032. {
  1033. uchar *led_port = LED_PORT;
  1034. if (status) /* switch LED on */
  1035. led_buffer |= led;
  1036. else /* switch LED off */
  1037. led_buffer &= ~led;
  1038. *led_port = led_buffer;
  1039. }
  1040. #endif /* CONFIG_IDE_LED */
  1041. #if defined(CONFIG_OF_IDE_FIXUP)
  1042. int ide_device_present(int dev)
  1043. {
  1044. if (dev >= CONFIG_SYS_IDE_MAXBUS)
  1045. return 0;
  1046. return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
  1047. }
  1048. #endif
  1049. /* ------------------------------------------------------------------------- */
  1050. #ifdef CONFIG_ATAPI
  1051. /****************************************************************************
  1052. * ATAPI Support
  1053. */
  1054. void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1055. __attribute__ ((weak, alias("__ide_input_data_shorts")));
  1056. void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1057. __attribute__ ((weak, alias("__ide_output_data_shorts")));
  1058. #if defined(CONFIG_IDE_SWAP_IO)
  1059. /* since ATAPI may use commands with not 4 bytes alligned length
  1060. * we have our own transfer functions, 2 bytes alligned */
  1061. void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1062. {
  1063. #if defined(CONFIG_CPC45)
  1064. uchar *dbuf;
  1065. volatile uchar *pbuf_even;
  1066. volatile uchar *pbuf_odd;
  1067. pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
  1068. pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
  1069. while (shorts--) {
  1070. EIEIO;
  1071. *pbuf_even = *dbuf++;
  1072. EIEIO;
  1073. *pbuf_odd = *dbuf++;
  1074. }
  1075. #else
  1076. ushort *dbuf;
  1077. volatile ushort *pbuf;
  1078. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  1079. dbuf = (ushort *) sect_buf;
  1080. debug("in output data shorts base for read is %lx\n",
  1081. (unsigned long) pbuf);
  1082. while (shorts--) {
  1083. EIEIO;
  1084. *pbuf = *dbuf++;
  1085. }
  1086. #endif
  1087. }
  1088. void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1089. {
  1090. #if defined(CONFIG_CPC45)
  1091. uchar *dbuf;
  1092. volatile uchar *pbuf_even;
  1093. volatile uchar *pbuf_odd;
  1094. pbuf_even = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_EVEN);
  1095. pbuf_odd = (uchar *) (ATA_CURR_BASE(dev) + ATA_DATA_ODD);
  1096. while (shorts--) {
  1097. EIEIO;
  1098. *dbuf++ = *pbuf_even;
  1099. EIEIO;
  1100. *dbuf++ = *pbuf_odd;
  1101. }
  1102. #else
  1103. ushort *dbuf;
  1104. volatile ushort *pbuf;
  1105. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  1106. dbuf = (ushort *) sect_buf;
  1107. debug("in input data shorts base for read is %lx\n",
  1108. (unsigned long) pbuf);
  1109. while (shorts--) {
  1110. EIEIO;
  1111. *dbuf++ = *pbuf;
  1112. }
  1113. #endif
  1114. }
  1115. #else /* ! CONFIG_IDE_SWAP_IO */
  1116. void __ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1117. {
  1118. outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
  1119. }
  1120. void __ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1121. {
  1122. insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
  1123. }
  1124. #endif /* CONFIG_IDE_SWAP_IO */
  1125. /*
  1126. * Wait until (Status & mask) == res, or timeout (in ms)
  1127. * Return last status
  1128. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1129. * and then they set their DRQ Bit
  1130. */
  1131. static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
  1132. {
  1133. ulong delay = 10 * t; /* poll every 100 us */
  1134. uchar c;
  1135. /* prevents to read the status before valid */
  1136. c = ide_inb(dev, ATA_DEV_CTL);
  1137. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  1138. /* break if error occurs (doesn't make sense to wait more) */
  1139. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
  1140. break;
  1141. udelay(100);
  1142. if (delay-- == 0)
  1143. break;
  1144. }
  1145. return (c);
  1146. }
  1147. /*
  1148. * issue an atapi command
  1149. */
  1150. unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
  1151. unsigned char *buffer, int buflen)
  1152. {
  1153. unsigned char c, err, mask, res;
  1154. int n;
  1155. ide_led(DEVICE_LED(device), 1); /* LED on */
  1156. /* Select device
  1157. */
  1158. mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
  1159. res = 0;
  1160. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1161. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1162. if ((c & mask) != res) {
  1163. printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
  1164. c);
  1165. err = 0xFF;
  1166. goto AI_OUT;
  1167. }
  1168. /* write taskfile */
  1169. ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1170. ide_outb(device, ATA_SECT_CNT, 0);
  1171. ide_outb(device, ATA_SECT_NUM, 0);
  1172. ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
  1173. ide_outb(device, ATA_CYL_HIGH,
  1174. (unsigned char) ((buflen >> 8) & 0xFF));
  1175. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1176. ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1177. udelay(50);
  1178. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  1179. res = ATA_STAT_DRQ;
  1180. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1181. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1182. printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
  1183. device, c);
  1184. err = 0xFF;
  1185. goto AI_OUT;
  1186. }
  1187. /* write command block */
  1188. ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
  1189. /* ATAPI Command written wait for completition */
  1190. udelay(5000); /* device must set bsy */
  1191. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  1192. /*
  1193. * if no data wait for DRQ = 0 BSY = 0
  1194. * if data wait for DRQ = 1 BSY = 0
  1195. */
  1196. res = 0;
  1197. if (buflen)
  1198. res = ATA_STAT_DRQ;
  1199. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1200. if ((c & mask) != res) {
  1201. if (c & ATA_STAT_ERR) {
  1202. err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
  1203. debug("atapi_issue 1 returned sense key %X status %02X\n",
  1204. err, c);
  1205. } else {
  1206. printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
  1207. ccb[0], c);
  1208. err = 0xFF;
  1209. }
  1210. goto AI_OUT;
  1211. }
  1212. n = ide_inb(device, ATA_CYL_HIGH);
  1213. n <<= 8;
  1214. n += ide_inb(device, ATA_CYL_LOW);
  1215. if (n > buflen) {
  1216. printf("ERROR, transfer bytes %d requested only %d\n", n,
  1217. buflen);
  1218. err = 0xff;
  1219. goto AI_OUT;
  1220. }
  1221. if ((n == 0) && (buflen < 0)) {
  1222. printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
  1223. err = 0xff;
  1224. goto AI_OUT;
  1225. }
  1226. if (n != buflen) {
  1227. debug("WARNING, transfer bytes %d not equal with requested %d\n",
  1228. n, buflen);
  1229. }
  1230. if (n != 0) { /* data transfer */
  1231. debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
  1232. /* we transfer shorts */
  1233. n >>= 1;
  1234. /* ok now decide if it is an in or output */
  1235. if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
  1236. debug("Write to device\n");
  1237. ide_output_data_shorts(device,
  1238. (unsigned short *) buffer, n);
  1239. } else {
  1240. debug("Read from device @ %p shorts %d\n", buffer, n);
  1241. ide_input_data_shorts(device,
  1242. (unsigned short *) buffer, n);
  1243. }
  1244. }
  1245. udelay(5000); /* seems that some CD ROMs need this... */
  1246. mask = ATA_STAT_BUSY | ATA_STAT_ERR;
  1247. res = 0;
  1248. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1249. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1250. err = (ide_inb(device, ATA_ERROR_REG) >> 4);
  1251. debug("atapi_issue 2 returned sense key %X status %X\n", err,
  1252. c);
  1253. } else {
  1254. err = 0;
  1255. }
  1256. AI_OUT:
  1257. ide_led(DEVICE_LED(device), 0); /* LED off */
  1258. return (err);
  1259. }
  1260. /*
  1261. * sending the command to atapi_issue. If an status other than good
  1262. * returns, an request_sense will be issued
  1263. */
  1264. #define ATAPI_DRIVE_NOT_READY 100
  1265. #define ATAPI_UNIT_ATTN 10
  1266. unsigned char atapi_issue_autoreq(int device,
  1267. unsigned char *ccb,
  1268. int ccblen,
  1269. unsigned char *buffer, int buflen)
  1270. {
  1271. unsigned char sense_data[18], sense_ccb[12];
  1272. unsigned char res, key, asc, ascq;
  1273. int notready, unitattn;
  1274. unitattn = ATAPI_UNIT_ATTN;
  1275. notready = ATAPI_DRIVE_NOT_READY;
  1276. retry:
  1277. res = atapi_issue(device, ccb, ccblen, buffer, buflen);
  1278. if (res == 0)
  1279. return 0; /* Ok */
  1280. if (res == 0xFF)
  1281. return 0xFF; /* error */
  1282. debug("(auto_req)atapi_issue returned sense key %X\n", res);
  1283. memset(sense_ccb, 0, sizeof(sense_ccb));
  1284. memset(sense_data, 0, sizeof(sense_data));
  1285. sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
  1286. sense_ccb[4] = 18; /* allocation Length */
  1287. res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
  1288. key = (sense_data[2] & 0xF);
  1289. asc = (sense_data[12]);
  1290. ascq = (sense_data[13]);
  1291. debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
  1292. debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1293. sense_data[0], key, asc, ascq);
  1294. if ((key == 0))
  1295. return 0; /* ok device ready */
  1296. if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
  1297. if (unitattn-- > 0) {
  1298. udelay(200 * 1000);
  1299. goto retry;
  1300. }
  1301. printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
  1302. goto error;
  1303. }
  1304. if ((asc == 0x4) && (ascq == 0x1)) {
  1305. /* not ready, but will be ready soon */
  1306. if (notready-- > 0) {
  1307. udelay(200 * 1000);
  1308. goto retry;
  1309. }
  1310. printf("Drive not ready, tried %d times\n",
  1311. ATAPI_DRIVE_NOT_READY);
  1312. goto error;
  1313. }
  1314. if (asc == 0x3a) {
  1315. debug("Media not present\n");
  1316. goto error;
  1317. }
  1318. printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
  1319. ascq);
  1320. error:
  1321. debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
  1322. return (0xFF);
  1323. }
  1324. static void atapi_inquiry(block_dev_desc_t *dev_desc)
  1325. {
  1326. unsigned char ccb[12]; /* Command descriptor block */
  1327. unsigned char iobuf[64]; /* temp buf */
  1328. unsigned char c;
  1329. int device;
  1330. device = dev_desc->dev;
  1331. dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
  1332. dev_desc->block_read = atapi_read;
  1333. memset(ccb, 0, sizeof(ccb));
  1334. memset(iobuf, 0, sizeof(iobuf));
  1335. ccb[0] = ATAPI_CMD_INQUIRY;
  1336. ccb[4] = 40; /* allocation Legnth */
  1337. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
  1338. debug("ATAPI_CMD_INQUIRY returned %x\n", c);
  1339. if (c != 0)
  1340. return;
  1341. /* copy device ident strings */
  1342. ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
  1343. ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
  1344. ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
  1345. dev_desc->lun = 0;
  1346. dev_desc->lba = 0;
  1347. dev_desc->blksz = 0;
  1348. dev_desc->type = iobuf[0] & 0x1f;
  1349. if ((iobuf[1] & 0x80) == 0x80)
  1350. dev_desc->removable = 1;
  1351. else
  1352. dev_desc->removable = 0;
  1353. memset(ccb, 0, sizeof(ccb));
  1354. memset(iobuf, 0, sizeof(iobuf));
  1355. ccb[0] = ATAPI_CMD_START_STOP;
  1356. ccb[4] = 0x03; /* start */
  1357. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
  1358. debug("ATAPI_CMD_START_STOP returned %x\n", c);
  1359. if (c != 0)
  1360. return;
  1361. memset(ccb, 0, sizeof(ccb));
  1362. memset(iobuf, 0, sizeof(iobuf));
  1363. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
  1364. debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
  1365. if (c != 0)
  1366. return;
  1367. memset(ccb, 0, sizeof(ccb));
  1368. memset(iobuf, 0, sizeof(iobuf));
  1369. ccb[0] = ATAPI_CMD_READ_CAP;
  1370. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
  1371. debug("ATAPI_CMD_READ_CAP returned %x\n", c);
  1372. if (c != 0)
  1373. return;
  1374. debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1375. iobuf[0], iobuf[1], iobuf[2], iobuf[3],
  1376. iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
  1377. dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
  1378. ((unsigned long) iobuf[1] << 16) +
  1379. ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
  1380. dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
  1381. ((unsigned long) iobuf[5] << 16) +
  1382. ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
  1383. #ifdef CONFIG_LBA48
  1384. /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
  1385. dev_desc->lba48 = 0;
  1386. #endif
  1387. return;
  1388. }
  1389. /*
  1390. * atapi_read:
  1391. * we transfer only one block per command, since the multiple DRQ per
  1392. * command is not yet implemented
  1393. */
  1394. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1395. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1396. #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
  1397. ulong atapi_read(int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1398. {
  1399. ulong n = 0;
  1400. unsigned char ccb[12]; /* Command descriptor block */
  1401. ulong cnt;
  1402. debug("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1403. device, blknr, blkcnt, (ulong) buffer);
  1404. do {
  1405. if (blkcnt > ATAPI_READ_MAX_BLOCK)
  1406. cnt = ATAPI_READ_MAX_BLOCK;
  1407. else
  1408. cnt = blkcnt;
  1409. ccb[0] = ATAPI_CMD_READ_12;
  1410. ccb[1] = 0; /* reserved */
  1411. ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
  1412. ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
  1413. ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
  1414. ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
  1415. ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
  1416. ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
  1417. ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
  1418. ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
  1419. ccb[10] = 0; /* reserved */
  1420. ccb[11] = 0; /* reserved */
  1421. if (atapi_issue_autoreq(device, ccb, 12,
  1422. (unsigned char *) buffer,
  1423. cnt * ATAPI_READ_BLOCK_SIZE)
  1424. == 0xFF) {
  1425. return (n);
  1426. }
  1427. n += cnt;
  1428. blkcnt -= cnt;
  1429. blknr += cnt;
  1430. buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
  1431. } while (blkcnt > 0);
  1432. return (n);
  1433. }
  1434. /* ------------------------------------------------------------------------- */
  1435. #endif /* CONFIG_ATAPI */
  1436. U_BOOT_CMD(ide, 5, 1, do_ide,
  1437. "IDE sub-system",
  1438. "reset - reset IDE controller\n"
  1439. "ide info - show available IDE devices\n"
  1440. "ide device [dev] - show or set current device\n"
  1441. "ide part [dev] - print partition table of one or all IDE devices\n"
  1442. "ide read addr blk# cnt\n"
  1443. "ide write addr blk# cnt - read/write `cnt'"
  1444. " blocks starting at block `blk#'\n"
  1445. " to/from memory address `addr'");
  1446. U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
  1447. "boot from IDE device", "loadAddr dev:part");