ide.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2011
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <ata.h>
  8. #include <blk.h>
  9. #include <dm.h>
  10. #include <ide.h>
  11. #include <log.h>
  12. #include <part.h>
  13. #include <watchdog.h>
  14. #include <asm/io.h>
  15. #include <linux/delay.h>
  16. #ifdef __PPC__
  17. # define EIEIO __asm__ volatile ("eieio")
  18. # define SYNC __asm__ volatile ("sync")
  19. #else
  20. # define EIEIO /* nothing */
  21. # define SYNC /* nothing */
  22. #endif
  23. /* Current offset for IDE0 / IDE1 bus access */
  24. ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
  25. #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
  26. CONFIG_SYS_ATA_IDE0_OFFSET,
  27. #endif
  28. #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
  29. CONFIG_SYS_ATA_IDE1_OFFSET,
  30. #endif
  31. };
  32. static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
  33. struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
  34. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  35. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  36. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  37. #ifndef CONFIG_SYS_ATA_PORT_ADDR
  38. #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
  39. #endif
  40. #ifdef CONFIG_IDE_RESET
  41. extern void ide_set_reset(int idereset);
  42. static void ide_reset(void)
  43. {
  44. int i;
  45. for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
  46. ide_bus_ok[i] = 0;
  47. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
  48. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  49. ide_set_reset(1); /* assert reset */
  50. /* the reset signal shall be asserted for et least 25 us */
  51. udelay(25);
  52. WATCHDOG_RESET();
  53. /* de-assert RESET signal */
  54. ide_set_reset(0);
  55. /* wait 250 ms */
  56. for (i = 0; i < 250; ++i)
  57. udelay(1000);
  58. }
  59. #else
  60. #define ide_reset() /* dummy */
  61. #endif /* CONFIG_IDE_RESET */
  62. /*
  63. * Wait until Busy bit is off, or timeout (in ms)
  64. * Return last status
  65. */
  66. static uchar ide_wait(int dev, ulong t)
  67. {
  68. ulong delay = 10 * t; /* poll every 100 us */
  69. uchar c;
  70. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  71. udelay(100);
  72. if (delay-- == 0)
  73. break;
  74. }
  75. return c;
  76. }
  77. /*
  78. * copy src to dest, skipping leading and trailing blanks and null
  79. * terminate the string
  80. * "len" is the size of available memory including the terminating '\0'
  81. */
  82. static void ident_cpy(unsigned char *dst, unsigned char *src,
  83. unsigned int len)
  84. {
  85. unsigned char *end, *last;
  86. last = dst;
  87. end = src + len - 1;
  88. /* reserve space for '\0' */
  89. if (len < 2)
  90. goto OUT;
  91. /* skip leading white space */
  92. while ((*src) && (src < end) && (*src == ' '))
  93. ++src;
  94. /* copy string, omitting trailing white space */
  95. while ((*src) && (src < end)) {
  96. *dst++ = *src;
  97. if (*src++ != ' ')
  98. last = dst;
  99. }
  100. OUT:
  101. *last = '\0';
  102. }
  103. #ifdef CONFIG_ATAPI
  104. /****************************************************************************
  105. * ATAPI Support
  106. */
  107. #if defined(CONFIG_IDE_SWAP_IO)
  108. /* since ATAPI may use commands with not 4 bytes alligned length
  109. * we have our own transfer functions, 2 bytes alligned */
  110. __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  111. {
  112. ushort *dbuf;
  113. volatile ushort *pbuf;
  114. pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
  115. dbuf = (ushort *)sect_buf;
  116. debug("in output data shorts base for read is %lx\n",
  117. (unsigned long) pbuf);
  118. while (shorts--) {
  119. EIEIO;
  120. *pbuf = *dbuf++;
  121. }
  122. }
  123. __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  124. {
  125. ushort *dbuf;
  126. volatile ushort *pbuf;
  127. pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
  128. dbuf = (ushort *)sect_buf;
  129. debug("in input data shorts base for read is %lx\n",
  130. (unsigned long) pbuf);
  131. while (shorts--) {
  132. EIEIO;
  133. *dbuf++ = *pbuf;
  134. }
  135. }
  136. #else /* ! CONFIG_IDE_SWAP_IO */
  137. __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  138. {
  139. outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
  140. }
  141. __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  142. {
  143. insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
  144. }
  145. #endif /* CONFIG_IDE_SWAP_IO */
  146. /*
  147. * Wait until (Status & mask) == res, or timeout (in ms)
  148. * Return last status
  149. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  150. * and then they set their DRQ Bit
  151. */
  152. static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
  153. {
  154. ulong delay = 10 * t; /* poll every 100 us */
  155. uchar c;
  156. /* prevents to read the status before valid */
  157. c = ide_inb(dev, ATA_DEV_CTL);
  158. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  159. /* break if error occurs (doesn't make sense to wait more) */
  160. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
  161. break;
  162. udelay(100);
  163. if (delay-- == 0)
  164. break;
  165. }
  166. return c;
  167. }
  168. /*
  169. * issue an atapi command
  170. */
  171. unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
  172. unsigned char *buffer, int buflen)
  173. {
  174. unsigned char c, err, mask, res;
  175. int n;
  176. /* Select device
  177. */
  178. mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
  179. res = 0;
  180. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  181. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  182. if ((c & mask) != res) {
  183. printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
  184. c);
  185. err = 0xFF;
  186. goto AI_OUT;
  187. }
  188. /* write taskfile */
  189. ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  190. ide_outb(device, ATA_SECT_CNT, 0);
  191. ide_outb(device, ATA_SECT_NUM, 0);
  192. ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
  193. ide_outb(device, ATA_CYL_HIGH,
  194. (unsigned char) ((buflen >> 8) & 0xFF));
  195. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  196. ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
  197. udelay(50);
  198. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  199. res = ATA_STAT_DRQ;
  200. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  201. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  202. printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
  203. device, c);
  204. err = 0xFF;
  205. goto AI_OUT;
  206. }
  207. /* write command block */
  208. ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
  209. /* ATAPI Command written wait for completition */
  210. udelay(5000); /* device must set bsy */
  211. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  212. /*
  213. * if no data wait for DRQ = 0 BSY = 0
  214. * if data wait for DRQ = 1 BSY = 0
  215. */
  216. res = 0;
  217. if (buflen)
  218. res = ATA_STAT_DRQ;
  219. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  220. if ((c & mask) != res) {
  221. if (c & ATA_STAT_ERR) {
  222. err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
  223. debug("atapi_issue 1 returned sense key %X status %02X\n",
  224. err, c);
  225. } else {
  226. printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
  227. ccb[0], c);
  228. err = 0xFF;
  229. }
  230. goto AI_OUT;
  231. }
  232. n = ide_inb(device, ATA_CYL_HIGH);
  233. n <<= 8;
  234. n += ide_inb(device, ATA_CYL_LOW);
  235. if (n > buflen) {
  236. printf("ERROR, transfer bytes %d requested only %d\n", n,
  237. buflen);
  238. err = 0xff;
  239. goto AI_OUT;
  240. }
  241. if ((n == 0) && (buflen < 0)) {
  242. printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
  243. err = 0xff;
  244. goto AI_OUT;
  245. }
  246. if (n != buflen) {
  247. debug("WARNING, transfer bytes %d not equal with requested %d\n",
  248. n, buflen);
  249. }
  250. if (n != 0) { /* data transfer */
  251. debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
  252. /* we transfer shorts */
  253. n >>= 1;
  254. /* ok now decide if it is an in or output */
  255. if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
  256. debug("Write to device\n");
  257. ide_output_data_shorts(device, (unsigned short *)buffer,
  258. n);
  259. } else {
  260. debug("Read from device @ %p shorts %d\n", buffer, n);
  261. ide_input_data_shorts(device, (unsigned short *)buffer,
  262. n);
  263. }
  264. }
  265. udelay(5000); /* seems that some CD ROMs need this... */
  266. mask = ATA_STAT_BUSY | ATA_STAT_ERR;
  267. res = 0;
  268. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  269. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  270. err = (ide_inb(device, ATA_ERROR_REG) >> 4);
  271. debug("atapi_issue 2 returned sense key %X status %X\n", err,
  272. c);
  273. } else {
  274. err = 0;
  275. }
  276. AI_OUT:
  277. return err;
  278. }
  279. /*
  280. * sending the command to atapi_issue. If an status other than good
  281. * returns, an request_sense will be issued
  282. */
  283. #define ATAPI_DRIVE_NOT_READY 100
  284. #define ATAPI_UNIT_ATTN 10
  285. unsigned char atapi_issue_autoreq(int device,
  286. unsigned char *ccb,
  287. int ccblen,
  288. unsigned char *buffer, int buflen)
  289. {
  290. unsigned char sense_data[18], sense_ccb[12];
  291. unsigned char res, key, asc, ascq;
  292. int notready, unitattn;
  293. unitattn = ATAPI_UNIT_ATTN;
  294. notready = ATAPI_DRIVE_NOT_READY;
  295. retry:
  296. res = atapi_issue(device, ccb, ccblen, buffer, buflen);
  297. if (res == 0)
  298. return 0; /* Ok */
  299. if (res == 0xFF)
  300. return 0xFF; /* error */
  301. debug("(auto_req)atapi_issue returned sense key %X\n", res);
  302. memset(sense_ccb, 0, sizeof(sense_ccb));
  303. memset(sense_data, 0, sizeof(sense_data));
  304. sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
  305. sense_ccb[4] = 18; /* allocation Length */
  306. res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
  307. key = (sense_data[2] & 0xF);
  308. asc = (sense_data[12]);
  309. ascq = (sense_data[13]);
  310. debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
  311. debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  312. sense_data[0], key, asc, ascq);
  313. if ((key == 0))
  314. return 0; /* ok device ready */
  315. if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
  316. if (unitattn-- > 0) {
  317. udelay(200 * 1000);
  318. goto retry;
  319. }
  320. printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
  321. goto error;
  322. }
  323. if ((asc == 0x4) && (ascq == 0x1)) {
  324. /* not ready, but will be ready soon */
  325. if (notready-- > 0) {
  326. udelay(200 * 1000);
  327. goto retry;
  328. }
  329. printf("Drive not ready, tried %d times\n",
  330. ATAPI_DRIVE_NOT_READY);
  331. goto error;
  332. }
  333. if (asc == 0x3a) {
  334. debug("Media not present\n");
  335. goto error;
  336. }
  337. printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
  338. ascq);
  339. error:
  340. debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
  341. return 0xFF;
  342. }
  343. /*
  344. * atapi_read:
  345. * we transfer only one block per command, since the multiple DRQ per
  346. * command is not yet implemented
  347. */
  348. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  349. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  350. #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
  351. ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
  352. void *buffer)
  353. {
  354. int device = block_dev->devnum;
  355. ulong n = 0;
  356. unsigned char ccb[12]; /* Command descriptor block */
  357. ulong cnt;
  358. debug("atapi_read dev %d start " LBAF " blocks " LBAF
  359. " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
  360. do {
  361. if (blkcnt > ATAPI_READ_MAX_BLOCK)
  362. cnt = ATAPI_READ_MAX_BLOCK;
  363. else
  364. cnt = blkcnt;
  365. ccb[0] = ATAPI_CMD_READ_12;
  366. ccb[1] = 0; /* reserved */
  367. ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
  368. ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
  369. ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
  370. ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
  371. ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
  372. ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
  373. ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
  374. ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
  375. ccb[10] = 0; /* reserved */
  376. ccb[11] = 0; /* reserved */
  377. if (atapi_issue_autoreq(device, ccb, 12,
  378. (unsigned char *)buffer,
  379. cnt * ATAPI_READ_BLOCK_SIZE)
  380. == 0xFF) {
  381. return n;
  382. }
  383. n += cnt;
  384. blkcnt -= cnt;
  385. blknr += cnt;
  386. buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
  387. } while (blkcnt > 0);
  388. return n;
  389. }
  390. static void atapi_inquiry(struct blk_desc *dev_desc)
  391. {
  392. unsigned char ccb[12]; /* Command descriptor block */
  393. unsigned char iobuf[64]; /* temp buf */
  394. unsigned char c;
  395. int device;
  396. device = dev_desc->devnum;
  397. dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
  398. #ifndef CONFIG_BLK
  399. dev_desc->block_read = atapi_read;
  400. #endif
  401. memset(ccb, 0, sizeof(ccb));
  402. memset(iobuf, 0, sizeof(iobuf));
  403. ccb[0] = ATAPI_CMD_INQUIRY;
  404. ccb[4] = 40; /* allocation Legnth */
  405. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
  406. debug("ATAPI_CMD_INQUIRY returned %x\n", c);
  407. if (c != 0)
  408. return;
  409. /* copy device ident strings */
  410. ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
  411. ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
  412. ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
  413. dev_desc->lun = 0;
  414. dev_desc->lba = 0;
  415. dev_desc->blksz = 0;
  416. dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
  417. dev_desc->type = iobuf[0] & 0x1f;
  418. if ((iobuf[1] & 0x80) == 0x80)
  419. dev_desc->removable = 1;
  420. else
  421. dev_desc->removable = 0;
  422. memset(ccb, 0, sizeof(ccb));
  423. memset(iobuf, 0, sizeof(iobuf));
  424. ccb[0] = ATAPI_CMD_START_STOP;
  425. ccb[4] = 0x03; /* start */
  426. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
  427. debug("ATAPI_CMD_START_STOP returned %x\n", c);
  428. if (c != 0)
  429. return;
  430. memset(ccb, 0, sizeof(ccb));
  431. memset(iobuf, 0, sizeof(iobuf));
  432. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
  433. debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
  434. if (c != 0)
  435. return;
  436. memset(ccb, 0, sizeof(ccb));
  437. memset(iobuf, 0, sizeof(iobuf));
  438. ccb[0] = ATAPI_CMD_READ_CAP;
  439. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
  440. debug("ATAPI_CMD_READ_CAP returned %x\n", c);
  441. if (c != 0)
  442. return;
  443. debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  444. iobuf[0], iobuf[1], iobuf[2], iobuf[3],
  445. iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
  446. dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
  447. ((unsigned long) iobuf[1] << 16) +
  448. ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
  449. dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
  450. ((unsigned long) iobuf[5] << 16) +
  451. ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
  452. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  453. #ifdef CONFIG_LBA48
  454. /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
  455. dev_desc->lba48 = 0;
  456. #endif
  457. return;
  458. }
  459. #endif /* CONFIG_ATAPI */
  460. static void ide_ident(struct blk_desc *dev_desc)
  461. {
  462. unsigned char c;
  463. hd_driveid_t iop;
  464. #ifdef CONFIG_ATAPI
  465. int retries = 0;
  466. #endif
  467. int device;
  468. device = dev_desc->devnum;
  469. printf(" Device %d: ", device);
  470. /* Select device
  471. */
  472. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  473. dev_desc->if_type = IF_TYPE_IDE;
  474. #ifdef CONFIG_ATAPI
  475. retries = 0;
  476. /* Warning: This will be tricky to read */
  477. while (retries <= 1) {
  478. /* check signature */
  479. if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
  480. (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
  481. (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
  482. (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
  483. /* ATAPI Signature found */
  484. dev_desc->if_type = IF_TYPE_ATAPI;
  485. /*
  486. * Start Ident Command
  487. */
  488. ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
  489. /*
  490. * Wait for completion - ATAPI devices need more time
  491. * to become ready
  492. */
  493. c = ide_wait(device, ATAPI_TIME_OUT);
  494. } else
  495. #endif
  496. {
  497. /*
  498. * Start Ident Command
  499. */
  500. ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
  501. /*
  502. * Wait for completion
  503. */
  504. c = ide_wait(device, IDE_TIME_OUT);
  505. }
  506. if (((c & ATA_STAT_DRQ) == 0) ||
  507. ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
  508. #ifdef CONFIG_ATAPI
  509. {
  510. /*
  511. * Need to soft reset the device
  512. * in case it's an ATAPI...
  513. */
  514. debug("Retrying...\n");
  515. ide_outb(device, ATA_DEV_HD,
  516. ATA_LBA | ATA_DEVICE(device));
  517. udelay(100000);
  518. ide_outb(device, ATA_COMMAND, 0x08);
  519. udelay(500000); /* 500 ms */
  520. }
  521. /*
  522. * Select device
  523. */
  524. ide_outb(device, ATA_DEV_HD,
  525. ATA_LBA | ATA_DEVICE(device));
  526. retries++;
  527. #else
  528. return;
  529. #endif
  530. }
  531. #ifdef CONFIG_ATAPI
  532. else
  533. break;
  534. } /* see above - ugly to read */
  535. if (retries == 2) /* Not found */
  536. return;
  537. #endif
  538. ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
  539. ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
  540. sizeof(dev_desc->revision));
  541. ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
  542. sizeof(dev_desc->vendor));
  543. ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
  544. sizeof(dev_desc->product));
  545. #ifdef __LITTLE_ENDIAN
  546. /*
  547. * firmware revision, model, and serial number have Big Endian Byte
  548. * order in Word. Convert all three to little endian.
  549. *
  550. * See CF+ and CompactFlash Specification Revision 2.0:
  551. * 6.2.1.6: Identify Drive, Table 39 for more details
  552. */
  553. strswab(dev_desc->revision);
  554. strswab(dev_desc->vendor);
  555. strswab(dev_desc->product);
  556. #endif /* __LITTLE_ENDIAN */
  557. if ((iop.config & 0x0080) == 0x0080)
  558. dev_desc->removable = 1;
  559. else
  560. dev_desc->removable = 0;
  561. #ifdef CONFIG_ATAPI
  562. if (dev_desc->if_type == IF_TYPE_ATAPI) {
  563. atapi_inquiry(dev_desc);
  564. return;
  565. }
  566. #endif /* CONFIG_ATAPI */
  567. #ifdef __BIG_ENDIAN
  568. /* swap shorts */
  569. dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
  570. #else /* ! __BIG_ENDIAN */
  571. /*
  572. * do not swap shorts on little endian
  573. *
  574. * See CF+ and CompactFlash Specification Revision 2.0:
  575. * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
  576. */
  577. dev_desc->lba = iop.lba_capacity;
  578. #endif /* __BIG_ENDIAN */
  579. #ifdef CONFIG_LBA48
  580. if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
  581. dev_desc->lba48 = 1;
  582. dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
  583. ((unsigned long long) iop.lba48_capacity[1] << 16) |
  584. ((unsigned long long) iop.lba48_capacity[2] << 32) |
  585. ((unsigned long long) iop.lba48_capacity[3] << 48);
  586. } else {
  587. dev_desc->lba48 = 0;
  588. }
  589. #endif /* CONFIG_LBA48 */
  590. /* assuming HD */
  591. dev_desc->type = DEV_TYPE_HARDDISK;
  592. dev_desc->blksz = ATA_BLOCKSIZE;
  593. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  594. dev_desc->lun = 0; /* just to fill something in... */
  595. #if 0 /* only used to test the powersaving mode,
  596. * if enabled, the drive goes after 5 sec
  597. * in standby mode */
  598. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  599. c = ide_wait(device, IDE_TIME_OUT);
  600. ide_outb(device, ATA_SECT_CNT, 1);
  601. ide_outb(device, ATA_LBA_LOW, 0);
  602. ide_outb(device, ATA_LBA_MID, 0);
  603. ide_outb(device, ATA_LBA_HIGH, 0);
  604. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  605. ide_outb(device, ATA_COMMAND, 0xe3);
  606. udelay(50);
  607. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  608. #endif
  609. }
  610. __weak void ide_outb(int dev, int port, unsigned char val)
  611. {
  612. debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
  613. dev, port, val,
  614. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  615. #if defined(CONFIG_IDE_AHB)
  616. if (port) {
  617. /* write command */
  618. ide_write_register(dev, port, val);
  619. } else {
  620. /* write data */
  621. outb(val, (ATA_CURR_BASE(dev)));
  622. }
  623. #else
  624. outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  625. #endif
  626. }
  627. __weak unsigned char ide_inb(int dev, int port)
  628. {
  629. uchar val;
  630. #if defined(CONFIG_IDE_AHB)
  631. val = ide_read_register(dev, port);
  632. #else
  633. val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  634. #endif
  635. debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
  636. dev, port,
  637. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
  638. return val;
  639. }
  640. void ide_init(void)
  641. {
  642. unsigned char c;
  643. int i, bus;
  644. #ifdef CONFIG_IDE_PREINIT
  645. WATCHDOG_RESET();
  646. if (ide_preinit()) {
  647. puts("ide_preinit failed\n");
  648. return;
  649. }
  650. #endif /* CONFIG_IDE_PREINIT */
  651. WATCHDOG_RESET();
  652. /* ATAPI Drives seems to need a proper IDE Reset */
  653. ide_reset();
  654. /*
  655. * Wait for IDE to get ready.
  656. * According to spec, this can take up to 31 seconds!
  657. */
  658. for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
  659. int dev =
  660. bus * (CONFIG_SYS_IDE_MAXDEVICE /
  661. CONFIG_SYS_IDE_MAXBUS);
  662. printf("Bus %d: ", bus);
  663. ide_bus_ok[bus] = 0;
  664. /* Select device
  665. */
  666. udelay(100000); /* 100 ms */
  667. ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  668. udelay(100000); /* 100 ms */
  669. i = 0;
  670. do {
  671. udelay(10000); /* 10 ms */
  672. c = ide_inb(dev, ATA_STATUS);
  673. i++;
  674. if (i > (ATA_RESET_TIME * 100)) {
  675. puts("** Timeout **\n");
  676. return;
  677. }
  678. if ((i >= 100) && ((i % 100) == 0))
  679. putc('.');
  680. } while (c & ATA_STAT_BUSY);
  681. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  682. puts("not available ");
  683. debug("Status = 0x%02X ", c);
  684. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  685. } else if ((c & ATA_STAT_READY) == 0) {
  686. puts("not available ");
  687. debug("Status = 0x%02X ", c);
  688. #endif
  689. } else {
  690. puts("OK ");
  691. ide_bus_ok[bus] = 1;
  692. }
  693. WATCHDOG_RESET();
  694. }
  695. putc('\n');
  696. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
  697. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  698. ide_dev_desc[i].if_type = IF_TYPE_IDE;
  699. ide_dev_desc[i].devnum = i;
  700. ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
  701. ide_dev_desc[i].blksz = 0;
  702. ide_dev_desc[i].log2blksz =
  703. LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
  704. ide_dev_desc[i].lba = 0;
  705. #ifndef CONFIG_BLK
  706. ide_dev_desc[i].block_read = ide_read;
  707. ide_dev_desc[i].block_write = ide_write;
  708. #endif
  709. if (!ide_bus_ok[IDE_BUS(i)])
  710. continue;
  711. ide_ident(&ide_dev_desc[i]);
  712. dev_print(&ide_dev_desc[i]);
  713. #ifndef CONFIG_BLK
  714. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  715. /* initialize partition type */
  716. part_init(&ide_dev_desc[i]);
  717. }
  718. #endif
  719. }
  720. WATCHDOG_RESET();
  721. #ifdef CONFIG_BLK
  722. struct udevice *dev;
  723. uclass_first_device(UCLASS_IDE, &dev);
  724. #endif
  725. }
  726. /* We only need to swap data if we are running on a big endian cpu. */
  727. #if defined(__LITTLE_ENDIAN)
  728. __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
  729. {
  730. ide_input_data(dev, sect_buf, words);
  731. }
  732. #else
  733. __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
  734. {
  735. volatile ushort *pbuf =
  736. (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
  737. ushort *dbuf = (ushort *)sect_buf;
  738. debug("in input swap data base for read is %lx\n",
  739. (unsigned long) pbuf);
  740. while (words--) {
  741. #ifdef __MIPS__
  742. *dbuf++ = swab16p((u16 *)pbuf);
  743. *dbuf++ = swab16p((u16 *)pbuf);
  744. #else
  745. *dbuf++ = ld_le16(pbuf);
  746. *dbuf++ = ld_le16(pbuf);
  747. #endif /* !MIPS */
  748. }
  749. }
  750. #endif /* __LITTLE_ENDIAN */
  751. #if defined(CONFIG_IDE_SWAP_IO)
  752. __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
  753. {
  754. ushort *dbuf;
  755. volatile ushort *pbuf;
  756. pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
  757. dbuf = (ushort *)sect_buf;
  758. while (words--) {
  759. EIEIO;
  760. *pbuf = *dbuf++;
  761. EIEIO;
  762. *pbuf = *dbuf++;
  763. }
  764. }
  765. #else /* ! CONFIG_IDE_SWAP_IO */
  766. __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
  767. {
  768. #if defined(CONFIG_IDE_AHB)
  769. ide_write_data(dev, sect_buf, words);
  770. #else
  771. outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
  772. #endif
  773. }
  774. #endif /* CONFIG_IDE_SWAP_IO */
  775. #if defined(CONFIG_IDE_SWAP_IO)
  776. __weak void ide_input_data(int dev, ulong *sect_buf, int words)
  777. {
  778. ushort *dbuf;
  779. volatile ushort *pbuf;
  780. pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
  781. dbuf = (ushort *)sect_buf;
  782. debug("in input data base for read is %lx\n", (unsigned long) pbuf);
  783. while (words--) {
  784. EIEIO;
  785. *dbuf++ = *pbuf;
  786. EIEIO;
  787. *dbuf++ = *pbuf;
  788. }
  789. }
  790. #else /* ! CONFIG_IDE_SWAP_IO */
  791. __weak void ide_input_data(int dev, ulong *sect_buf, int words)
  792. {
  793. #if defined(CONFIG_IDE_AHB)
  794. ide_read_data(dev, sect_buf, words);
  795. #else
  796. insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
  797. #endif
  798. }
  799. #endif /* CONFIG_IDE_SWAP_IO */
  800. #ifdef CONFIG_BLK
  801. ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  802. void *buffer)
  803. #else
  804. ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
  805. void *buffer)
  806. #endif
  807. {
  808. #ifdef CONFIG_BLK
  809. struct blk_desc *block_dev = dev_get_uclass_plat(dev);
  810. #endif
  811. int device = block_dev->devnum;
  812. ulong n = 0;
  813. unsigned char c;
  814. unsigned char pwrsave = 0; /* power save */
  815. #ifdef CONFIG_LBA48
  816. unsigned char lba48 = 0;
  817. if (blknr & 0x0000fffff0000000ULL) {
  818. /* more than 28 bits used, use 48bit mode */
  819. lba48 = 1;
  820. }
  821. #endif
  822. debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
  823. device, blknr, blkcnt, (ulong) buffer);
  824. /* Select device
  825. */
  826. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  827. c = ide_wait(device, IDE_TIME_OUT);
  828. if (c & ATA_STAT_BUSY) {
  829. printf("IDE read: device %d not ready\n", device);
  830. goto IDE_READ_E;
  831. }
  832. /* first check if the drive is in Powersaving mode, if yes,
  833. * increase the timeout value */
  834. ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
  835. udelay(50);
  836. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  837. if (c & ATA_STAT_BUSY) {
  838. printf("IDE read: device %d not ready\n", device);
  839. goto IDE_READ_E;
  840. }
  841. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  842. printf("No Powersaving mode %X\n", c);
  843. } else {
  844. c = ide_inb(device, ATA_SECT_CNT);
  845. debug("Powersaving %02X\n", c);
  846. if (c == 0)
  847. pwrsave = 1;
  848. }
  849. while (blkcnt-- > 0) {
  850. c = ide_wait(device, IDE_TIME_OUT);
  851. if (c & ATA_STAT_BUSY) {
  852. printf("IDE read: device %d not ready\n", device);
  853. break;
  854. }
  855. #ifdef CONFIG_LBA48
  856. if (lba48) {
  857. /* write high bits */
  858. ide_outb(device, ATA_SECT_CNT, 0);
  859. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  860. #ifdef CONFIG_SYS_64BIT_LBA
  861. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  862. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  863. #else
  864. ide_outb(device, ATA_LBA_MID, 0);
  865. ide_outb(device, ATA_LBA_HIGH, 0);
  866. #endif
  867. }
  868. #endif
  869. ide_outb(device, ATA_SECT_CNT, 1);
  870. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  871. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  872. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  873. #ifdef CONFIG_LBA48
  874. if (lba48) {
  875. ide_outb(device, ATA_DEV_HD,
  876. ATA_LBA | ATA_DEVICE(device));
  877. ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
  878. } else
  879. #endif
  880. {
  881. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  882. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  883. ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
  884. }
  885. udelay(50);
  886. if (pwrsave) {
  887. /* may take up to 4 sec */
  888. c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
  889. pwrsave = 0;
  890. } else {
  891. /* can't take over 500 ms */
  892. c = ide_wait(device, IDE_TIME_OUT);
  893. }
  894. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  895. ATA_STAT_DRQ) {
  896. printf("Error (no IRQ) dev %d blk " LBAF
  897. ": status %#02x\n", device, blknr, c);
  898. break;
  899. }
  900. ide_input_data(device, buffer, ATA_SECTORWORDS);
  901. (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
  902. ++n;
  903. ++blknr;
  904. buffer += ATA_BLOCKSIZE;
  905. }
  906. IDE_READ_E:
  907. return n;
  908. }
  909. #ifdef CONFIG_BLK
  910. ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  911. const void *buffer)
  912. #else
  913. ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
  914. const void *buffer)
  915. #endif
  916. {
  917. #ifdef CONFIG_BLK
  918. struct blk_desc *block_dev = dev_get_uclass_plat(dev);
  919. #endif
  920. int device = block_dev->devnum;
  921. ulong n = 0;
  922. unsigned char c;
  923. #ifdef CONFIG_LBA48
  924. unsigned char lba48 = 0;
  925. if (blknr & 0x0000fffff0000000ULL) {
  926. /* more than 28 bits used, use 48bit mode */
  927. lba48 = 1;
  928. }
  929. #endif
  930. /* Select device
  931. */
  932. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  933. while (blkcnt-- > 0) {
  934. c = ide_wait(device, IDE_TIME_OUT);
  935. if (c & ATA_STAT_BUSY) {
  936. printf("IDE read: device %d not ready\n", device);
  937. goto WR_OUT;
  938. }
  939. #ifdef CONFIG_LBA48
  940. if (lba48) {
  941. /* write high bits */
  942. ide_outb(device, ATA_SECT_CNT, 0);
  943. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  944. #ifdef CONFIG_SYS_64BIT_LBA
  945. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  946. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  947. #else
  948. ide_outb(device, ATA_LBA_MID, 0);
  949. ide_outb(device, ATA_LBA_HIGH, 0);
  950. #endif
  951. }
  952. #endif
  953. ide_outb(device, ATA_SECT_CNT, 1);
  954. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  955. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  956. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  957. #ifdef CONFIG_LBA48
  958. if (lba48) {
  959. ide_outb(device, ATA_DEV_HD,
  960. ATA_LBA | ATA_DEVICE(device));
  961. ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
  962. } else
  963. #endif
  964. {
  965. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  966. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  967. ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
  968. }
  969. udelay(50);
  970. /* can't take over 500 ms */
  971. c = ide_wait(device, IDE_TIME_OUT);
  972. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  973. ATA_STAT_DRQ) {
  974. printf("Error (no IRQ) dev %d blk " LBAF
  975. ": status %#02x\n", device, blknr, c);
  976. goto WR_OUT;
  977. }
  978. ide_output_data(device, buffer, ATA_SECTORWORDS);
  979. c = ide_inb(device, ATA_STATUS); /* clear IRQ */
  980. ++n;
  981. ++blknr;
  982. buffer += ATA_BLOCKSIZE;
  983. }
  984. WR_OUT:
  985. return n;
  986. }
  987. #if defined(CONFIG_OF_IDE_FIXUP)
  988. int ide_device_present(int dev)
  989. {
  990. if (dev >= CONFIG_SYS_IDE_MAXBUS)
  991. return 0;
  992. return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
  993. }
  994. #endif
  995. #ifdef CONFIG_BLK
  996. static int ide_blk_probe(struct udevice *udev)
  997. {
  998. struct blk_desc *desc = dev_get_uclass_plat(udev);
  999. /* fill in device vendor/product/rev strings */
  1000. strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
  1001. BLK_VEN_SIZE);
  1002. desc->vendor[BLK_VEN_SIZE] = '\0';
  1003. strncpy(desc->product, ide_dev_desc[desc->devnum].product,
  1004. BLK_PRD_SIZE);
  1005. desc->product[BLK_PRD_SIZE] = '\0';
  1006. strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
  1007. BLK_REV_SIZE);
  1008. desc->revision[BLK_REV_SIZE] = '\0';
  1009. return 0;
  1010. }
  1011. static const struct blk_ops ide_blk_ops = {
  1012. .read = ide_read,
  1013. .write = ide_write,
  1014. };
  1015. U_BOOT_DRIVER(ide_blk) = {
  1016. .name = "ide_blk",
  1017. .id = UCLASS_BLK,
  1018. .ops = &ide_blk_ops,
  1019. .probe = ide_blk_probe,
  1020. };
  1021. static int ide_probe(struct udevice *udev)
  1022. {
  1023. struct udevice *blk_dev;
  1024. char name[20];
  1025. int blksz;
  1026. lbaint_t size;
  1027. int i;
  1028. int ret;
  1029. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
  1030. if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
  1031. sprintf(name, "blk#%d", i);
  1032. blksz = ide_dev_desc[i].blksz;
  1033. size = blksz * ide_dev_desc[i].lba;
  1034. /*
  1035. * With CDROM, if there is no CD inserted, blksz will
  1036. * be zero, don't bother to create IDE block device.
  1037. */
  1038. if (!blksz)
  1039. continue;
  1040. ret = blk_create_devicef(udev, "ide_blk", name,
  1041. IF_TYPE_IDE, i,
  1042. blksz, size, &blk_dev);
  1043. if (ret)
  1044. return ret;
  1045. }
  1046. }
  1047. return 0;
  1048. }
  1049. U_BOOT_DRIVER(ide) = {
  1050. .name = "ide",
  1051. .id = UCLASS_IDE,
  1052. .probe = ide_probe,
  1053. };
  1054. struct pci_device_id ide_supported[] = {
  1055. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
  1056. { }
  1057. };
  1058. U_BOOT_PCI_DEVICE(ide, ide_supported);
  1059. UCLASS_DRIVER(ide) = {
  1060. .name = "ide",
  1061. .id = UCLASS_IDE,
  1062. };
  1063. #else
  1064. U_BOOT_LEGACY_BLK(ide) = {
  1065. .if_typename = "ide",
  1066. .if_type = IF_TYPE_IDE,
  1067. .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
  1068. .desc = ide_dev_desc,
  1069. };
  1070. #endif