Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ide_disk.c 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. #include "etherboot.h"
  2. #include "timer.h"
  3. #include "pci.h"
  4. #include "isa.h"
  5. #include "disk.h"
  6. #define BSY_SET_DURING_SPINUP 1
  7. /*
  8. * UBL, The Universal Talkware Boot Loader
  9. * Copyright (C) 2000 Universal Talkware Inc.
  10. * Copyright (C) 2002 Eric Biederman
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. *
  27. */
  28. struct controller {
  29. uint16_t cmd_base;
  30. uint16_t ctrl_base;
  31. };
  32. struct harddisk_info {
  33. struct controller *ctrl;
  34. uint16_t heads;
  35. uint16_t cylinders;
  36. uint16_t sectors_per_track;
  37. uint8_t model_number[41];
  38. uint8_t slave;
  39. sector_t sectors;
  40. int address_mode; /* am i lba (0x40) or chs (0x00) */
  41. #define ADDRESS_MODE_CHS 0
  42. #define ADDRESS_MODE_LBA 1
  43. #define ADDRESS_MODE_LBA48 2
  44. int drive_exists;
  45. int slave_absent;
  46. int basedrive;
  47. };
  48. #define IDE_SECTOR_SIZE 0x200
  49. #define IDE_BASE0 (0x1F0u) /* primary controller */
  50. #define IDE_BASE1 (0x170u) /* secondary */
  51. #define IDE_BASE2 (0x0F0u) /* third */
  52. #define IDE_BASE3 (0x070u) /* fourth */
  53. #define IDE_REG_EXTENDED_OFFSET (0x204u)
  54. #define IDE_REG_DATA(base) ((ctrl)->cmd_base + 0u) /* word register */
  55. #define IDE_REG_ERROR(base) ((ctrl)->cmd_base + 1u)
  56. #define IDE_REG_PRECOMP(base) ((ctrl)->cmd_base + 1u)
  57. #define IDE_REG_FEATURE(base) ((ctrl)->cmd_base + 1u)
  58. #define IDE_REG_SECTOR_COUNT(base) ((ctrl)->cmd_base + 2u)
  59. #define IDE_REG_SECTOR_NUMBER(base) ((ctrl)->cmd_base + 3u)
  60. #define IDE_REG_LBA_LOW(base) ((ctrl)->cmd_base + 3u)
  61. #define IDE_REG_CYLINDER_LSB(base) ((ctrl)->cmd_base + 4u)
  62. #define IDE_REG_LBA_MID(base) ((ctrl)->cmd_base + 4u)
  63. #define IDE_REG_CYLINDER_MSB(base) ((ctrl)->cmd_base + 5u)
  64. #define IDE_REG_LBA_HIGH(base) ((ctrl)->cmd_base + 5u)
  65. #define IDE_REG_DRIVEHEAD(base) ((ctrl)->cmd_base + 6u)
  66. #define IDE_REG_DEVICE(base) ((ctrl)->cmd_base + 6u)
  67. #define IDE_REG_STATUS(base) ((ctrl)->cmd_base + 7u)
  68. #define IDE_REG_COMMAND(base) ((ctrl)->cmd_base + 7u)
  69. #define IDE_REG_ALTSTATUS(base) ((ctrl)->ctrl_base + 2u)
  70. #define IDE_REG_DEVICE_CONTROL(base) ((ctrl)->ctrl_base + 2u)
  71. struct ide_pio_command
  72. {
  73. uint8_t feature;
  74. uint8_t sector_count;
  75. uint8_t lba_low;
  76. uint8_t lba_mid;
  77. uint8_t lba_high;
  78. uint8_t device;
  79. # define IDE_DH_DEFAULT (0xA0)
  80. # define IDE_DH_HEAD(x) ((x) & 0x0F)
  81. # define IDE_DH_MASTER (0x00)
  82. # define IDE_DH_SLAVE (0x10)
  83. # define IDE_DH_LBA (0x40)
  84. # define IDE_DH_CHS (0x00)
  85. uint8_t command;
  86. uint8_t sector_count2;
  87. uint8_t lba_low2;
  88. uint8_t lba_mid2;
  89. uint8_t lba_high2;
  90. };
  91. #define IDE_DEFAULT_COMMAND { 0xFFu, 0x01, 0x00, 0x0000, IDE_DH_DEFAULT }
  92. #define IDE_ERR_ICRC 0x80 /* ATA Ultra DMA bad CRC */
  93. #define IDE_ERR_BBK 0x80 /* ATA bad block */
  94. #define IDE_ERR_UNC 0x40 /* ATA uncorrected error */
  95. #define IDE_ERR_MC 0x20 /* ATA media change */
  96. #define IDE_ERR_IDNF 0x10 /* ATA id not found */
  97. #define IDE_ERR_MCR 0x08 /* ATA media change request */
  98. #define IDE_ERR_ABRT 0x04 /* ATA command aborted */
  99. #define IDE_ERR_NTK0 0x02 /* ATA track 0 not found */
  100. #define IDE_ERR_NDAM 0x01 /* ATA address mark not found */
  101. #define IDE_STATUS_BSY 0x80 /* busy */
  102. #define IDE_STATUS_RDY 0x40 /* ready */
  103. #define IDE_STATUS_DF 0x20 /* device fault */
  104. #define IDE_STATUS_WFT 0x20 /* write fault (old name) */
  105. #define IDE_STATUS_SKC 0x10 /* seek complete */
  106. #define IDE_STATUS_DRQ 0x08 /* data request */
  107. #define IDE_STATUS_CORR 0x04 /* corrected */
  108. #define IDE_STATUS_IDX 0x02 /* index */
  109. #define IDE_STATUS_ERR 0x01 /* error (ATA) */
  110. #define IDE_STATUS_CHK 0x01 /* check (ATAPI) */
  111. #define IDE_CTRL_HD15 0x08 /* bit should always be set to one */
  112. #define IDE_CTRL_SRST 0x04 /* soft reset */
  113. #define IDE_CTRL_NIEN 0x02 /* disable interrupts */
  114. /* Most mandtory and optional ATA commands (from ATA-3), */
  115. #define IDE_CMD_CFA_ERASE_SECTORS 0xC0
  116. #define IDE_CMD_CFA_REQUEST_EXT_ERR_CODE 0x03
  117. #define IDE_CMD_CFA_TRANSLATE_SECTOR 0x87
  118. #define IDE_CMD_CFA_WRITE_MULTIPLE_WO_ERASE 0xCD
  119. #define IDE_CMD_CFA_WRITE_SECTORS_WO_ERASE 0x38
  120. #define IDE_CMD_CHECK_POWER_MODE1 0xE5
  121. #define IDE_CMD_CHECK_POWER_MODE2 0x98
  122. #define IDE_CMD_DEVICE_RESET 0x08
  123. #define IDE_CMD_EXECUTE_DEVICE_DIAGNOSTIC 0x90
  124. #define IDE_CMD_FLUSH_CACHE 0xE7
  125. #define IDE_CMD_FORMAT_TRACK 0x50
  126. #define IDE_CMD_IDENTIFY_DEVICE 0xEC
  127. #define IDE_CMD_IDENTIFY_DEVICE_PACKET 0xA1
  128. #define IDE_CMD_IDENTIFY_PACKET_DEVICE 0xA1
  129. #define IDE_CMD_IDLE1 0xE3
  130. #define IDE_CMD_IDLE2 0x97
  131. #define IDE_CMD_IDLE_IMMEDIATE1 0xE1
  132. #define IDE_CMD_IDLE_IMMEDIATE2 0x95
  133. #define IDE_CMD_INITIALIZE_DRIVE_PARAMETERS 0x91
  134. #define IDE_CMD_INITIALIZE_DEVICE_PARAMETERS 0x91
  135. #define IDE_CMD_NOP 0x00
  136. #define IDE_CMD_PACKET 0xA0
  137. #define IDE_CMD_READ_BUFFER 0xE4
  138. #define IDE_CMD_READ_DMA 0xC8
  139. #define IDE_CMD_READ_DMA_QUEUED 0xC7
  140. #define IDE_CMD_READ_MULTIPLE 0xC4
  141. #define IDE_CMD_READ_SECTORS 0x20
  142. #define IDE_CMD_READ_SECTORS_EXT 0x24
  143. #define IDE_CMD_READ_VERIFY_SECTORS 0x40
  144. #define IDE_CMD_RECALIBRATE 0x10
  145. #define IDE_CMD_SEEK 0x70
  146. #define IDE_CMD_SET_FEATURES 0xEF
  147. #define IDE_CMD_SET_MAX_ADDR_EXT 0x24
  148. #define IDE_CMD_SET_MULTIPLE_MODE 0xC6
  149. #define IDE_CMD_SLEEP1 0xE6
  150. #define IDE_CMD_SLEEP2 0x99
  151. #define IDE_CMD_STANDBY1 0xE2
  152. #define IDE_CMD_STANDBY2 0x96
  153. #define IDE_CMD_STANDBY_IMMEDIATE1 0xE0
  154. #define IDE_CMD_STANDBY_IMMEDIATE2 0x94
  155. #define IDE_CMD_WRITE_BUFFER 0xE8
  156. #define IDE_CMD_WRITE_DMA 0xCA
  157. #define IDE_CMD_WRITE_DMA_QUEUED 0xCC
  158. #define IDE_CMD_WRITE_MULTIPLE 0xC5
  159. #define IDE_CMD_WRITE_SECTORS 0x30
  160. #define IDE_CMD_WRITE_VERIFY 0x3C
  161. /* IDE_CMD_SET_FEATURE sub commands */
  162. #define IDE_FEATURE_CFA_ENABLE_8BIT_PIO 0x01
  163. #define IDE_FEATURE_ENABLE_WRITE_CACHE 0x02
  164. #define IDE_FEATURE_SET_TRANSFER_MODE 0x03
  165. #define IDE_FEATURE_ENABLE_POWER_MANAGEMENT 0x05
  166. #define IDE_FEATURE_ENABLE_POWERUP_IN_STANDBY 0x06
  167. #define IDE_FEATURE_STANDBY_SPINUP_DRIVE 0x07
  168. #define IDE_FEATURE_CFA_ENABLE_POWER_MODE1 0x0A
  169. #define IDE_FEATURE_DISABLE_MEDIA_STATUS_NOTIFICATION 0x31
  170. #define IDE_FEATURE_ENABLE_AUTOMATIC_ACOUSTIC_MANAGEMENT 0x42
  171. #define IDE_FEATURE_SET_MAXIMUM_HOST_INTERFACE_SECTOR_TIMES 0x43
  172. #define IDE_FEATURE_DISABLE_READ_LOOKAHEAD 0x55
  173. #define IDE_FEATURE_ENABLE_RELEASE_INTERRUPT 0x5D
  174. #define IDE_FEATURE_ENABLE_SERVICE_INTERRUPT 0x5E
  175. #define IDE_FEATURE_DISABLE_REVERTING_TO_POWERON_DEFAULTS 0x66
  176. #define IDE_FEATURE_CFA_DISABLE_8BIT_PIO 0x81
  177. #define IDE_FEATURE_DISABLE_WRITE_CACHE 0x82
  178. #define IDE_FEATURE_DISABLE_POWER_MANAGEMENT 0x85
  179. #define IDE_FEATURE_DISABLE_POWERUP_IN_STANDBY 0x86
  180. #define IDE_FEATURE_CFA_DISABLE_POWER_MODE1 0x8A
  181. #define IDE_FEATURE_ENABLE_MEDIA_STATUS_NOTIFICATION 0x95
  182. #define IDE_FEATURE_ENABLE_READ_LOOKAHEAD 0xAA
  183. #define IDE_FEATURE_DISABLE_AUTOMATIC_ACOUSTIC_MANAGEMENT 0xC2
  184. #define IDE_FEATURE_ENABLE_REVERTING_TO_POWERON_DEFAULTS 0xCC
  185. #define IDE_FEATURE_DISABLE_SERVICE_INTERRUPT 0xDE
  186. struct controller controller;
  187. struct harddisk_info harddisk_info[2];
  188. static int await_ide(int (*done)(struct controller *ctrl),
  189. struct controller *ctrl, unsigned long timeout)
  190. {
  191. int result;
  192. for(;;) {
  193. result = done(ctrl);
  194. if (result) {
  195. return 0;
  196. }
  197. poll_interruptions();
  198. if ((timeout == 0) || (currticks() > timeout)) {
  199. break;
  200. }
  201. }
  202. return -1;
  203. }
  204. /* The maximum time any IDE command can last 31 seconds,
  205. * So if any IDE commands takes this long we know we have problems.
  206. */
  207. #define IDE_TIMEOUT (32*TICKS_PER_SEC)
  208. static int not_bsy(struct controller *ctrl)
  209. {
  210. return !(inb(IDE_REG_STATUS(ctrl)) & IDE_STATUS_BSY);
  211. }
  212. #if !BSY_SET_DURING_SPINUP
  213. static int timeout(struct controller *ctrl)
  214. {
  215. return 0;
  216. }
  217. #endif
  218. static int ide_software_reset(struct controller *ctrl)
  219. {
  220. /* Wait a little bit in case this is immediately after
  221. * hardware reset.
  222. */
  223. mdelay(2);
  224. /* A software reset should not be delivered while the bsy bit
  225. * is set. If the bsy bit does not clear in a reasonable
  226. * amount of time give up.
  227. */
  228. if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
  229. return -1;
  230. }
  231. /* Disable Interrupts and reset the ide bus */
  232. outb(IDE_CTRL_HD15 | IDE_CTRL_SRST | IDE_CTRL_NIEN,
  233. IDE_REG_DEVICE_CONTROL(ctrl));
  234. udelay(5);
  235. outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
  236. mdelay(2);
  237. if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
  238. return -1;
  239. }
  240. return 0;
  241. }
  242. static void pio_set_registers(
  243. struct controller *ctrl, const struct ide_pio_command *cmd)
  244. {
  245. uint8_t device;
  246. /* Disable Interrupts */
  247. outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
  248. /* Possibly switch selected device */
  249. device = inb(IDE_REG_DEVICE(ctrl));
  250. outb(cmd->device, IDE_REG_DEVICE(ctrl));
  251. if ((device & (1UL << 4)) != (cmd->device & (1UL << 4))) {
  252. /* Allow time for the selected drive to switch,
  253. * The linux ide code suggests 50ms is the right
  254. * amount of time to use here.
  255. */
  256. mdelay(50);
  257. }
  258. outb(cmd->feature, IDE_REG_FEATURE(ctrl));
  259. outb(cmd->sector_count2, IDE_REG_SECTOR_COUNT(ctrl));
  260. outb(cmd->sector_count, IDE_REG_SECTOR_COUNT(ctrl));
  261. outb(cmd->lba_low2, IDE_REG_LBA_LOW(ctrl));
  262. outb(cmd->lba_low, IDE_REG_LBA_LOW(ctrl));
  263. outb(cmd->lba_mid2, IDE_REG_LBA_MID(ctrl));
  264. outb(cmd->lba_mid, IDE_REG_LBA_MID(ctrl));
  265. outb(cmd->lba_high2, IDE_REG_LBA_HIGH(ctrl));
  266. outb(cmd->lba_high, IDE_REG_LBA_HIGH(ctrl));
  267. outb(cmd->command, IDE_REG_COMMAND(ctrl));
  268. }
  269. static int pio_non_data(struct controller *ctrl, const struct ide_pio_command *cmd)
  270. {
  271. /* Wait until the busy bit is clear */
  272. if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
  273. return -1;
  274. }
  275. pio_set_registers(ctrl, cmd);
  276. if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
  277. return -1;
  278. }
  279. /* FIXME is there more error checking I could do here? */
  280. return 0;
  281. }
  282. static int pio_data_in(struct controller *ctrl, const struct ide_pio_command *cmd,
  283. void *buffer, size_t bytes)
  284. {
  285. unsigned int status;
  286. /* FIXME handle commands with multiple blocks */
  287. /* Wait until the busy bit is clear */
  288. if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
  289. return -1;
  290. }
  291. /* How do I tell if INTRQ is asserted? */
  292. pio_set_registers(ctrl, cmd);
  293. if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
  294. return -1;
  295. }
  296. status = inb(IDE_REG_STATUS(ctrl));
  297. if (!(status & IDE_STATUS_DRQ)) {
  298. return -1;
  299. }
  300. insw(IDE_REG_DATA(ctrl), buffer, bytes/2);
  301. status = inb(IDE_REG_STATUS(ctrl));
  302. if (status & IDE_STATUS_DRQ) {
  303. return -1;
  304. }
  305. return 0;
  306. }
  307. #if 0
  308. static int pio_packet(struct controller *ctrl, int in,
  309. const void *packet, int packet_len,
  310. void *buffer, int buffer_len)
  311. {
  312. const uint8_t *pbuf;
  313. unsigned int status;
  314. struct ide_pio_command cmd;
  315. memset(&cmd, 0, sizeof(cmd));
  316. /* Wait until the busy bit is clear */
  317. if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
  318. return -1;
  319. }
  320. pio_set_registers(ctrl, cmd);
  321. ndelay(400);
  322. if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
  323. return -1;
  324. }
  325. status = inb(IDE_REG_STATUS(ctrl));
  326. if (!(status & IDE_STATUS_DRQ)) {
  327. return -1;
  328. }
  329. while(packet_len > 1) {
  330. outb(*pbuf, IDE_REG_DATA(ctrl));
  331. pbuf++;
  332. packet_len -= 1;
  333. }
  334. inb(IDE_REG_ALTSTATUS(ctrl));
  335. if (await_ide){}
  336. /*FIXME finish this function */
  337. }
  338. #endif
  339. static inline int ide_read_sector_chs(
  340. struct harddisk_info *info, void *buffer, unsigned long sector)
  341. {
  342. struct ide_pio_command cmd;
  343. unsigned int track;
  344. unsigned int offset;
  345. unsigned int cylinder;
  346. memset(&cmd, 0, sizeof(cmd));
  347. cmd.sector_count = 1;
  348. track = sector / info->sectors_per_track;
  349. /* Sector number */
  350. offset = 1 + (sector % info->sectors_per_track);
  351. cylinder = track / info->heads;
  352. cmd.lba_low = offset;
  353. cmd.lba_mid = cylinder & 0xff;
  354. cmd.lba_high = (cylinder >> 8) & 0xff;
  355. cmd.device = IDE_DH_DEFAULT |
  356. IDE_DH_HEAD(track % info->heads) |
  357. info->slave |
  358. IDE_DH_CHS;
  359. cmd.command = IDE_CMD_READ_SECTORS;
  360. return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
  361. }
  362. static inline int ide_read_sector_lba(
  363. struct harddisk_info *info, void *buffer, unsigned long sector)
  364. {
  365. struct ide_pio_command cmd;
  366. memset(&cmd, 0, sizeof(cmd));
  367. cmd.sector_count = 1;
  368. cmd.lba_low = sector & 0xff;
  369. cmd.lba_mid = (sector >> 8) & 0xff;
  370. cmd.lba_high = (sector >> 16) & 0xff;
  371. cmd.device = IDE_DH_DEFAULT |
  372. ((sector >> 24) & 0x0f) |
  373. info->slave |
  374. IDE_DH_LBA;
  375. cmd.command = IDE_CMD_READ_SECTORS;
  376. return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
  377. }
  378. static inline int ide_read_sector_lba48(
  379. struct harddisk_info *info, void *buffer, sector_t sector)
  380. {
  381. struct ide_pio_command cmd;
  382. memset(&cmd, 0, sizeof(cmd));
  383. cmd.sector_count = 1;
  384. cmd.lba_low = sector & 0xff;
  385. cmd.lba_mid = (sector >> 8) & 0xff;
  386. cmd.lba_high = (sector >> 16) & 0xff;
  387. cmd.lba_low2 = (sector >> 24) & 0xff;
  388. cmd.lba_mid2 = (sector >> 32) & 0xff;
  389. cmd.lba_high2 = (sector >> 40) & 0xff;
  390. cmd.device = info->slave | IDE_DH_LBA;
  391. cmd.command = IDE_CMD_READ_SECTORS_EXT;
  392. return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
  393. }
  394. static int ide_read(struct disk *disk, sector_t sector)
  395. {
  396. struct harddisk_info *info = disk->priv;
  397. int result;
  398. /* Report the buffer is empty */
  399. disk->sector = 0;
  400. disk->bytes = 0;
  401. if (sector > info->sectors) {
  402. return -1;
  403. }
  404. if (info->address_mode == ADDRESS_MODE_CHS) {
  405. result = ide_read_sector_chs(info, disk->buffer, sector);
  406. }
  407. else if (info->address_mode == ADDRESS_MODE_LBA) {
  408. result = ide_read_sector_lba(info, disk->buffer, sector);
  409. }
  410. else if (info->address_mode == ADDRESS_MODE_LBA48) {
  411. result = ide_read_sector_lba48(info, disk->buffer, sector);
  412. }
  413. else {
  414. result = -1;
  415. }
  416. /* On success report the buffer has data */
  417. if (result != -1) {
  418. disk->bytes = IDE_SECTOR_SIZE;
  419. disk->sector = sector;
  420. }
  421. return result;
  422. }
  423. static int init_drive(struct harddisk_info *info, struct controller *ctrl, int slave,
  424. int basedrive, unsigned char *buffer)
  425. {
  426. uint16_t* drive_info;
  427. struct ide_pio_command cmd;
  428. int i;
  429. info->ctrl = ctrl;
  430. info->heads = 0u;
  431. info->cylinders = 0u;
  432. info->sectors_per_track = 0u;
  433. info->address_mode = IDE_DH_CHS;
  434. info->sectors = 0ul;
  435. info->drive_exists = 0;
  436. info->slave_absent = 0;
  437. info->slave = slave?IDE_DH_SLAVE: IDE_DH_MASTER;
  438. info->basedrive = basedrive;
  439. #if 0
  440. printf("Testing for disk %d\n", info->basedrive);
  441. #endif
  442. /* Select the drive that we are testing */
  443. outb(IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave,
  444. IDE_REG_DEVICE(ctrl));
  445. mdelay(50);
  446. /* Test to see if the drive registers exist,
  447. * In many cases this quickly rules out a missing drive.
  448. */
  449. for(i = 0; i < 4; i++) {
  450. outb(0xaa + i, (ctrl->cmd_base) + 2 + i);
  451. }
  452. for(i = 0; i < 4; i++) {
  453. if (inb((ctrl->cmd_base) + 2 + i) != 0xaa + i) {
  454. return 1;
  455. }
  456. }
  457. for(i = 0; i < 4; i++) {
  458. outb(0x55 + i, (ctrl->cmd_base) + 2 + i);
  459. }
  460. for(i = 0; i < 4; i++) {
  461. if (inb((ctrl->cmd_base) + 2 + i) != 0x55 + i) {
  462. return 1;
  463. }
  464. }
  465. #if 0
  466. printf("Probing for disk %d\n", info->basedrive);
  467. #endif
  468. memset(&cmd, 0, sizeof(cmd));
  469. cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
  470. cmd.command = IDE_CMD_IDENTIFY_DEVICE;
  471. if (pio_data_in(ctrl, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
  472. /* Well, if that command didn't work, we probably don't have drive. */
  473. return 1;
  474. }
  475. /* Now suck the data out */
  476. drive_info = (uint16_t *)buffer;
  477. if (drive_info[2] == 0x37C8) {
  478. /* If the response is incomplete spin up the drive... */
  479. memset(&cmd, 0, sizeof(cmd));
  480. cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
  481. info->slave;
  482. cmd.feature = IDE_FEATURE_STANDBY_SPINUP_DRIVE;
  483. if (pio_non_data(ctrl, &cmd) < 0) {
  484. /* If the command doesn't work give up on the drive */
  485. return 1;
  486. }
  487. }
  488. if ((drive_info[2] == 0x37C8) || (drive_info[2] == 0x8C73)) {
  489. /* The response is incomplete retry the drive info command */
  490. memset(&cmd, 0, sizeof(cmd));
  491. cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
  492. info->slave;
  493. cmd.command = IDE_CMD_IDENTIFY_DEVICE;
  494. if(pio_data_in(ctrl, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
  495. /* If the command didn't work give up on the drive. */
  496. return 1;
  497. }
  498. }
  499. if ((drive_info[2] != 0x37C8) &&
  500. (drive_info[2] != 0x738C) &&
  501. (drive_info[2] != 0x8C73) &&
  502. (drive_info[2] != 0xC837) &&
  503. (drive_info[2] != 0x0000)) {
  504. printf("Invalid IDE Configuration: %hx\n", drive_info[2]);
  505. return 1;
  506. }
  507. for(i = 27; i < 47; i++) {
  508. info->model_number[((i-27)<< 1)] = (drive_info[i] >> 8) & 0xff;
  509. info->model_number[((i-27)<< 1)+1] = drive_info[i] & 0xff;
  510. }
  511. info->model_number[40] = '\0';
  512. info->drive_exists = 1;
  513. /* See if LBA is supported */
  514. if (drive_info[49] & (1 << 9)) {
  515. info->address_mode = ADDRESS_MODE_LBA;
  516. info->sectors = (drive_info[61] << 16) | (drive_info[60]);
  517. /* Enable LBA48 mode if it is present */
  518. if (drive_info[83] & (1 <<10)) {
  519. /* Should LBA48 depend on LBA? */
  520. printf("LBA48 mode\n");
  521. info->address_mode = ADDRESS_MODE_LBA48;
  522. info->sectors =
  523. (((sector_t)drive_info[103]) << 48) |
  524. (((sector_t)drive_info[102]) << 32) |
  525. (((sector_t)drive_info[101]) << 16) |
  526. (((sector_t)drive_info[100]) << 0);
  527. }
  528. } else {
  529. info->address_mode = ADDRESS_MODE_CHS;
  530. info->heads = drive_info[3];
  531. info->cylinders = drive_info[1];
  532. info->sectors_per_track = drive_info[6];
  533. info->sectors =
  534. info->sectors_per_track *
  535. info->heads *
  536. info->cylinders;
  537. printf( "%s sectors_per_track=[%d], heads=[%d], cylinders=[%d]\n",
  538. __FUNCTION__,
  539. info->sectors_per_track,
  540. info->heads,
  541. info->cylinders);
  542. }
  543. /* See if we have a slave */
  544. if (!info->slave && (((drive_info[93] >> 14) & 3) == 1)) {
  545. info->slave_absent = !(drive_info[93] & (1 << 5));
  546. }
  547. /* See if we need to put the device in CFA power mode 1 */
  548. if ((drive_info[160] & ((1 << 15) | (1 << 13)| (1 << 12))) ==
  549. ((1 << 15) | (1 << 13)| (1 << 12))) {
  550. memset(&cmd, 0, sizeof(cmd));
  551. cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
  552. cmd.feature = IDE_FEATURE_CFA_ENABLE_POWER_MODE1;
  553. if (pio_non_data(ctrl, &cmd) < 0) {
  554. /* If I need to power up the drive, and I can't
  555. * give up.
  556. */
  557. printf("Cannot power up CFA device\n");
  558. return 1;
  559. }
  560. }
  561. printf("disk%d %dk cap: %hx\n",
  562. info->basedrive,
  563. (unsigned long)(info->sectors >> 1),
  564. drive_info[49]);
  565. return 0;
  566. }
  567. static int init_controller(struct controller *ctrl, int basedrive, unsigned char *buffer)
  568. {
  569. struct harddisk_info *info;
  570. /* Intialize the harddisk_info structures */
  571. memset(harddisk_info, 0, sizeof(harddisk_info));
  572. /* Put the drives ide channel in a know state and wait
  573. * for the drives to spinup.
  574. *
  575. * In practice IDE disks tend not to respond to commands until
  576. * they have spun up. This makes IDE hard to deal with
  577. * immediately after power up, as the delays can be quite
  578. * long, so we must be very careful here.
  579. *
  580. * There are two pathological cases that must be dealt with:
  581. *
  582. * - The BSY bit not being set while the IDE drives spin up.
  583. * In this cases only a hard coded delay will work. As
  584. * I have not reproduced it, and this is out of spec for
  585. * IDE drives the work around can be enabled by setting
  586. * BSY_SET_DURING_SPINUP to 0.
  587. *
  588. * - The BSY bit floats high when no drives are plugged in.
  589. * This case will not be detected except by timing out but
  590. * we avoid the problems by only probing devices we are
  591. * supposed to boot from. If we don't do the probe we
  592. * will not experience the problem.
  593. *
  594. * So speed wise I am only slow if the BSY bit is not set
  595. * or not reported by the IDE controller during spinup, which
  596. * is quite rare.
  597. *
  598. */
  599. #if !BSY_SET_DURING_SPINUP
  600. if (await_ide(timeout, ctrl, IDE_TIMEOUT) < 0) {
  601. return -1;
  602. }
  603. #endif
  604. if (ide_software_reset(ctrl) < 0) {
  605. return -1;
  606. }
  607. /* Note: I have just done a software reset. It may be
  608. * reasonable to just read the boot time signatures
  609. * off of the drives to see if they are present.
  610. *
  611. * For now I will go with just sending commands to the drives
  612. * and assuming filtering out missing drives by detecting registers
  613. * that won't set and commands that fail to execute properly.
  614. */
  615. /* Now initialize the individual drives */
  616. info = &harddisk_info[0];
  617. init_drive(info, ctrl, 0, basedrive, buffer);
  618. if (info->drive_exists && !info->slave_absent) {
  619. basedrive++;
  620. info++;
  621. init_drive(info, ctrl, 1, basedrive, buffer);
  622. }
  623. return 0;
  624. }
  625. static void ide_disable(struct dev *dev)
  626. {
  627. struct disk *disk = (struct disk *)dev;
  628. struct harddisk_info *info = disk->priv;
  629. ide_software_reset(info->ctrl);
  630. }
  631. #ifdef CONFIG_PCI
  632. static int ide_pci_probe(struct dev *dev, struct pci_device *pci)
  633. {
  634. struct disk *disk = (struct disk *)dev;
  635. struct harddisk_info *info;
  636. int index;
  637. adjust_pci_device(pci);
  638. index = dev->index + 1;
  639. if (dev->how_probe == PROBE_NEXT) {
  640. index++;
  641. }
  642. for(; index < 4; index++) {
  643. unsigned mask;
  644. mask = (index < 2)? (1 << 0) : (1 << 2);
  645. if ((pci->class & mask) == 0) {
  646. /* IDE special pci mode */
  647. uint16_t base;
  648. base = (index < 2)?IDE_BASE0:IDE_BASE1;
  649. controller.cmd_base = base;
  650. controller.ctrl_base = base + IDE_REG_EXTENDED_OFFSET;
  651. } else {
  652. /* IDE normal pci mode */
  653. unsigned cmd_reg, ctrl_reg;
  654. uint32_t cmd_base, ctrl_base;
  655. if (index < 2) {
  656. cmd_reg = PCI_BASE_ADDRESS_0;
  657. ctrl_reg = PCI_BASE_ADDRESS_1;
  658. } else {
  659. cmd_reg = PCI_BASE_ADDRESS_2;
  660. ctrl_reg = PCI_BASE_ADDRESS_3;
  661. }
  662. pcibios_read_config_dword(pci->bus, pci->devfn, cmd_reg, &cmd_base);
  663. pcibios_read_config_dword(pci->bus, pci->devfn, ctrl_reg, &ctrl_base);
  664. controller.cmd_base = cmd_base & ~3;
  665. controller.ctrl_base = ctrl_base & ~3;
  666. }
  667. if (((index & 1) == 0) || (dev->how_probe == PROBE_AWAKE)) {
  668. if (init_controller(&controller, disk->drive, disk->buffer) < 0) {
  669. /* nothing behind the controller */
  670. continue;
  671. }
  672. }
  673. info = &harddisk_info[index & 1];
  674. if (!info->drive_exists) {
  675. /* unknown drive */
  676. continue;
  677. }
  678. disk->hw_sector_size = IDE_SECTOR_SIZE;
  679. disk->sectors_per_read = 1;
  680. disk->sectors = info->sectors;
  681. dev->index = index;
  682. dev->disable = ide_disable;
  683. disk->read = ide_read;
  684. disk->priv = info;
  685. return 1;
  686. }
  687. /* past all of the drives */
  688. dev->index = 0;
  689. return 0;
  690. }
  691. #define PCI_DEVICE_ID_INTEL_82801CA_11 0x248b
  692. static struct pci_id ide_controllers[] = {
  693. PCI_ROM(0x0000, 0x0000, "ide_disk", "Generic IDE disk support"),
  694. /* { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_11, "PIIX4" },*/
  695. #if 0 /* Currently I don't need any entries in this table so ignore it */
  696. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371FB_0, "PIIX" },
  697. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371FB_1, "PIIX" },
  698. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371MX, "MPIIX" },
  699. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1, "PIIX3" },
  700. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB, "PIIX4" },
  701. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_1, "PIIX4" },
  702. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_1, "PIIX4" },
  703. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_1, "PIIX4" },
  704. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82372FB_1, "PIIX4" },
  705. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82451NX, "PIIX4" },
  706. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_9, "PIIX4" },
  707. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_8, "PIIX4" },
  708. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_10, "PIIX4" },
  709. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C561, "VIA_IDE" },
  710. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C576_1, "VP_IDE" },
  711. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_1, "VP_IDE" },
  712. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20246, "PDC20246" },
  713. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20262, "PDC20262" },
  714. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20265, "PDC20265" },
  715. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20267, "PDC20267" },
  716. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20268, "PDC20268" },
  717. { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20268R, "PDC20268" },
  718. { PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000, "RZ1000" },
  719. { PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1001, "RZ1001" },
  720. { PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_SAMURAI_IDE, "SAMURAI" },
  721. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_640, "CMD640" },
  722. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_643, "CMD646" },
  723. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_646, "CMD648" },
  724. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_648, "CMD643" },
  725. { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_649, "CMD649" },
  726. { PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5513, "SIS5513" },
  727. { PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C621, "OPTI621" },
  728. { PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C558, "OPTI621V" },
  729. { PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C825, "OPTI621X" },
  730. { PCI_VENDOR_ID_TEKRAM, PCI_DEVICE_ID_TEKRAM_DC290, "TRM290" },
  731. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, "NS87410" },
  732. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87415, "NS87415" },
  733. { PCI_VENDOR_ID_HOLTEK2, PCI_DEVICE_ID_HOLTEK2_6565, "HT6565" },
  734. { PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_ATP850UF, "AEC6210" },
  735. { PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_ATP860, "AEC6260" },
  736. { PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_ATP860R, "AEC6260R" },
  737. { PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105, "W82C105" },
  738. { PCI_VENDOR_ID_UMC, PCI_DEVICE_ID_UMC_UM8673F, "UM8673F" },
  739. { PCI_VENDOR_ID_UMC, PCI_DEVICE_ID_UMC_UM8886A, "UM8886A" },
  740. { PCI_VENDOR_ID_UMC, PCI_DEVICE_ID_UMC_UM8886BF, "UM8886BF" },
  741. { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT343, "HPT34X" },
  742. { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT366, "HPT366" },
  743. { PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5229, "ALI15X3" },
  744. { PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, "CY82C693" },
  745. { 0x3388, 0x8013, "HINT_IDE" },
  746. { PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5530_IDE, "CS5530" },
  747. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_COBRA_7401, "AMD7401" },
  748. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7409, "AMD7409" },
  749. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7411, "AMD7411" },
  750. { PCI_VENDOR_ID_PDC, PCI_DEVICE_ID_PDC_1841, "PDCADMA" },
  751. { PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_1, "SLC90E66" },
  752. { PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4IDE, "OSB4" },
  753. { PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5IDE, "OSB5" },
  754. { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_IT8172G, "ITE8172G" },
  755. #endif
  756. };
  757. static struct pci_driver ide_driver __pci_driver = {
  758. .type = DISK_DRIVER,
  759. .name = "IDE",
  760. .probe = ide_pci_probe,
  761. .ids = ide_controllers,
  762. .id_count = sizeof(ide_controllers)/sizeof(ide_controllers),
  763. .class = PCI_CLASS_STORAGE_IDE,
  764. };
  765. #endif
  766. /* The isa driver works but it causes disks to show up twice.
  767. * comment it out for now.
  768. */
  769. #if 0 && defined(CONFIG_ISA)
  770. static int ide_isa_probe(struct dev * dev, unsigned short *probe_addrs)
  771. {
  772. struct disk *disk = (struct disk *)dev;
  773. int index;
  774. unsigned short addr;
  775. struct harddisk_info *info;
  776. index = dev->index +1;
  777. if (dev->how_probe == PROBE_AWAKE) {
  778. index--;
  779. }
  780. for(; (index >= 0) && (addr = probe_addrs[index >> 1]); index += 2) {
  781. if ((index & 1) == 0) {
  782. controller.cmd_base = addr;
  783. controller.ctrl_base = addr + IDE_REG_EXTENDED_OFFSET;
  784. if (init_controller(&controller, disk->drive, disk->buffer) < 0) {
  785. /* nothing behind the controller */
  786. continue;
  787. }
  788. }
  789. info = &harddisk_info[index & 1];
  790. if (!info->drive_exists) {
  791. /* unknown drive */
  792. return 0;
  793. }
  794. disk->sectors_per_read = 1;
  795. disk->sectors = info->sectors;
  796. dev->index = index;
  797. dev->disable = ide_disable;
  798. disk->read = ide_read;
  799. disk->priv = info;
  800. return 1;
  801. }
  802. /* past all of the drives */
  803. dev->index = -1;
  804. return 0;
  805. }
  806. static unsigned short ide_base[] = {
  807. IDE_BASE0,
  808. IDE_BASE1,
  809. IDE_BASE2,
  810. IDE_BASE3,
  811. 0
  812. };
  813. static struct isa_driver ide_isa_driver __isa_driver = {
  814. .type = DISK_DRIVER,
  815. .name = "IDE/ISA",
  816. .probe = ide_isa_probe,
  817. .ioaddrs = ide_base,
  818. };
  819. #endif