hw_ide.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* This file is part of the program psim.
  2. Copyright (C) 1996, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _HW_IDE_C_
  15. #define _HW_IDE_C_
  16. #include "device_table.h"
  17. /* DEVICE
  18. ide - Integrated Disk Electronics
  19. DESCRIPTION
  20. This device models the primary/secondary <<ide>> controller
  21. described in the [CHRPIO] document.
  22. The controller has separate independant interrupt outputs for each
  23. <<ide>> bus.
  24. PROPERTIES
  25. reg = ... (required)
  26. The <<reg>> property is described in the document [CHRPIO].
  27. ready-delay = <integer> (optional)
  28. If present, this specifies the time that the <<ide>> device takes
  29. to complete an I/O operation.
  30. disk@?/ide-byte-count = <integer> (optional)
  31. disk@?/ide-sector-count = <integer> (optional)
  32. disk@?/ide-head-count = <integer> (optional)
  33. The <<ide>> device checks each child (disk device) node to see if
  34. it has the above properties. If present, these values will be used
  35. to compute the <<LBA>> address in <<CHS>> addressing mode.
  36. EXAMPLES
  37. Enable tracing:
  38. | -t ide-device \
  39. Attach the <<ide>> device to the <<pci>> bus at slot one. Specify
  40. legacy I/O addresses:
  41. | -o '/phb/ide@1/assigned-addresses \
  42. | ni0,0,10,1f0 8 \
  43. | ni0,0,14,3f8 8 \
  44. | ni0,0,18,170 8 \
  45. | ni0,0,1c,378 8 \
  46. | ni0,0,20,200 8' \
  47. | -o '/phb@0x80000000/ide@1/reg \
  48. | 1 0 \
  49. | i0,0,10,0 8 \
  50. | i0,0,18,0 8 \
  51. | i0,0,14,6 1 \
  52. | i0,0,1c,6 1 \
  53. | i0,0,20,0 8' \
  54. Note: the fouth and fifth reg entries specify that the register is
  55. at an offset into the address specified by the base register
  56. (<<assigned-addresses>>); Apart from restrictions placed by the
  57. <<pci>> specification, no restrictions are placed on the number of
  58. base registers specified by the <<assigned-addresses>> property.
  59. Attach a <<disk>> to the primary and a <<cdrom>> to the secondary
  60. <<ide>> controller.
  61. | -o '/phb@0x80000000/ide@1/disk@0/file "zero' \
  62. | -o '/phb@0x80000000/ide@1/cdrom@2/file "/dev/cdrom"' \
  63. Connect the two interrupt outputs (a and b) to a <<glue>> device to
  64. allow testing of the interrupt port. In a real simulation they
  65. would be wired to the interrupt controller.
  66. | -o '/phb@0x80000000/glue@2/reg 2 0 ni0,0,0,0 8' \
  67. | -o '/phb@0x80000000/ide@1 > a 0 /phb@0x80000000/glue@2' \
  68. | -o '/phb@0x80000000/ide@1 > b 1 /phb@0x80000000/glue@2'
  69. BUGS
  70. While the DMA registers are present, DMA support has not yet been
  71. implemented.
  72. The number of supported commands is very limited.
  73. The standards documents appear to be vague on how to specify the
  74. <<unit-address>> of disk devices devices being attached to the
  75. <<ide>> controller. I've chosen to use integers with devices zero
  76. and one going to the primary controller while two and three are
  77. connected to the secondary controller.
  78. REFERENCES
  79. [CHRPIO] PowerPC(tm) Microprocessor Common Hardware Reference
  80. Platform: I/O Device Reference. http://chrp.apple.com/???.
  81. [SCHMIDT] The SCSI Bus and IDE Interface - Protocols, Applications
  82. and Programming. Friedhelm Schmidt (translated by Michael
  83. Schultz). ISBN 0-201-42284-0. Addison-Wesley Publishing Company.
  84. */
  85. typedef enum _io_direction {
  86. is_read,
  87. is_write,
  88. } io_direction;
  89. enum {
  90. nr_ide_controllers = 2,
  91. nr_ide_drives_per_controller = 2,
  92. nr_fifo_entries = 8192,
  93. };
  94. enum {
  95. /* command register block - read */
  96. ide_data_reg,
  97. ide_error_reg, /*ide_feature_reg*/
  98. ide_sector_count_reg,
  99. ide_sector_number_reg,
  100. ide_cylinder_reg0,
  101. ide_cylinder_reg1,
  102. ide_drive_head_reg,
  103. ide_status_reg, /*ide_command_reg*/
  104. /* command register block - write */
  105. ide_feature_reg, /*ide_error_reg*/
  106. ide_command_reg, /*ide_status_reg*/
  107. /* control register block - read */
  108. ide_alternate_status_reg, /*ide_control_reg*/
  109. ide_control_reg, /*ide_alternate_status_reg*/
  110. /* dma register block */
  111. ide_dma_command_reg,
  112. ide_dma_unused_1_reg,
  113. ide_dma_status_reg,
  114. ide_dma_unused_3_reg,
  115. ide_dma_prd_table_address_reg0,
  116. ide_dma_prd_table_address_reg1,
  117. ide_dma_prd_table_address_reg2,
  118. ide_dma_prd_table_address_reg3,
  119. nr_ide_registers,
  120. };
  121. typedef enum _ide_states {
  122. idle_state,
  123. busy_loaded_state,
  124. busy_drained_state,
  125. busy_dma_state,
  126. busy_command_state,
  127. loading_state,
  128. draining_state,
  129. } ide_states;
  130. static const char *
  131. ide_state_name(ide_states state)
  132. {
  133. switch (state) {
  134. case idle_state: return "idle";
  135. case busy_loaded_state: return "busy_loaded_state";
  136. case busy_drained_state: return "busy_drained_state";
  137. case busy_dma_state: return "busy_dma_state";
  138. case busy_command_state: return "busy_command_state";
  139. case loading_state: return "loading_state";
  140. case draining_state: return "draining_state";
  141. default: return "illegal-state";
  142. }
  143. }
  144. typedef struct _ide_geometry {
  145. int head;
  146. int sector;
  147. int byte;
  148. } ide_geometry;
  149. typedef struct _ide_drive {
  150. int nr;
  151. device *device;
  152. ide_geometry geometry;
  153. ide_geometry default_geometry;
  154. } ide_drive;
  155. typedef struct _ide_controller {
  156. int nr;
  157. ide_states state;
  158. uint8_t reg[nr_ide_registers];
  159. uint8_t fifo[nr_fifo_entries];
  160. int fifo_pos;
  161. int fifo_size;
  162. ide_drive *current_drive;
  163. int current_byte;
  164. int current_transfer;
  165. ide_drive drive[nr_ide_drives_per_controller];
  166. device *me;
  167. event_entry_tag event_tag;
  168. int is_interrupting;
  169. int64_t ready_delay;
  170. } ide_controller;
  171. static void
  172. set_interrupt(device *me,
  173. ide_controller *controller)
  174. {
  175. if ((controller->reg[ide_control_reg] & 0x2) == 0) {
  176. DTRACE(ide, ("controller %d - interrupt set\n", controller->nr));
  177. device_interrupt_event(me, controller->nr, 1, NULL, 0);
  178. controller->is_interrupting = 1;
  179. }
  180. }
  181. static void
  182. clear_interrupt(device *me,
  183. ide_controller *controller)
  184. {
  185. if (controller->is_interrupting) {
  186. DTRACE(ide, ("controller %d - interrupt clear\n", controller->nr));
  187. device_interrupt_event(me, controller->nr, 0, NULL, 0);
  188. controller->is_interrupting = 0;
  189. }
  190. }
  191. static void
  192. do_event(void *data)
  193. {
  194. ide_controller *controller = data;
  195. device *me = controller->me;
  196. controller->event_tag = 0;
  197. switch (controller->state) {
  198. case busy_loaded_state:
  199. case busy_drained_state:
  200. if (controller->current_transfer > 0) {
  201. controller->state = (controller->state == busy_loaded_state
  202. ? loading_state : draining_state);
  203. }
  204. else {
  205. controller->state = idle_state;
  206. }
  207. set_interrupt(me, controller);
  208. break;
  209. default:
  210. device_error(me, "controller %d - unexpected event", controller->nr);
  211. break;
  212. }
  213. }
  214. static void
  215. schedule_ready_event(device *me,
  216. ide_controller *controller)
  217. {
  218. if (controller->event_tag != 0)
  219. device_error(me, "controller %d - attempting to schedule multiple events",
  220. controller->nr);
  221. controller->event_tag =
  222. device_event_queue_schedule(me, controller->ready_delay,
  223. do_event, controller);
  224. }
  225. static void
  226. do_fifo_read(device *me,
  227. ide_controller *controller,
  228. void *dest,
  229. int nr_bytes)
  230. {
  231. if (controller->state != draining_state)
  232. device_error(me, "controller %d - reading fifo when not ready (%s)",
  233. controller->nr,
  234. ide_state_name(controller->state));
  235. if (controller->fifo_pos + nr_bytes > controller->fifo_size)
  236. device_error(me, "controller %d - fifo underflow", controller->nr);
  237. if (nr_bytes > 0) {
  238. memcpy(dest, &controller->fifo[controller->fifo_pos], nr_bytes);
  239. controller->fifo_pos += nr_bytes;
  240. }
  241. if (controller->fifo_pos == controller->fifo_size) {
  242. controller->current_transfer -= 1;
  243. if (controller->current_transfer > 0
  244. && controller->current_drive != NULL) {
  245. DTRACE(ide, ("controller %d:%d - reading %d byte block at 0x%x\n",
  246. controller->nr,
  247. controller->current_drive->nr,
  248. controller->fifo_size,
  249. controller->current_byte));
  250. if (device_io_read_buffer(controller->current_drive->device,
  251. controller->fifo,
  252. 0, controller->current_byte,
  253. controller->fifo_size,
  254. NULL, 0)
  255. != controller->fifo_size)
  256. device_error(me, "controller %d - disk %s io read error",
  257. controller->nr,
  258. device_path(controller->current_drive->device));
  259. }
  260. controller->state = busy_drained_state;
  261. controller->fifo_pos = 0;
  262. controller->current_byte += controller->fifo_size;
  263. schedule_ready_event(me, controller);
  264. }
  265. }
  266. static void
  267. do_fifo_write(device *me,
  268. ide_controller *controller,
  269. const void *source,
  270. int nr_bytes)
  271. {
  272. if (controller->state != loading_state)
  273. device_error(me, "controller %d - writing fifo when not ready (%s)",
  274. controller->nr,
  275. ide_state_name(controller->state));
  276. if (controller->fifo_pos + nr_bytes > controller->fifo_size)
  277. device_error(me, "controller %d - fifo overflow", controller->nr);
  278. if (nr_bytes > 0) {
  279. memcpy(&controller->fifo[controller->fifo_pos], source, nr_bytes);
  280. controller->fifo_pos += nr_bytes;
  281. }
  282. if (controller->fifo_pos == controller->fifo_size) {
  283. if (controller->current_transfer > 0
  284. && controller->current_drive != NULL) {
  285. DTRACE(ide, ("controller %d:%d - writing %d byte block at 0x%x\n",
  286. controller->nr,
  287. controller->current_drive->nr,
  288. controller->fifo_size,
  289. controller->current_byte));
  290. if (device_io_write_buffer(controller->current_drive->device,
  291. controller->fifo,
  292. 0, controller->current_byte,
  293. controller->fifo_size,
  294. NULL, 0)
  295. != controller->fifo_size)
  296. device_error(me, "controller %d - disk %s io write error",
  297. controller->nr,
  298. device_path(controller->current_drive->device));
  299. }
  300. controller->current_transfer -= 1;
  301. controller->fifo_pos = 0;
  302. controller->current_byte += controller->fifo_size;
  303. controller->state = busy_loaded_state;
  304. schedule_ready_event(me, controller);
  305. }
  306. }
  307. static void
  308. setup_fifo(device *me,
  309. ide_controller *controller,
  310. int is_simple,
  311. int is_with_disk,
  312. io_direction direction)
  313. {
  314. /* find the disk */
  315. if (is_with_disk) {
  316. int drive_nr = (controller->reg[ide_drive_head_reg] & 0x10) != 0;
  317. controller->current_drive = &controller->drive[drive_nr];
  318. }
  319. else {
  320. controller->current_drive = NULL;
  321. }
  322. /* number of transfers */
  323. if (is_simple)
  324. controller->current_transfer = 1;
  325. else {
  326. int sector_count = controller->reg[ide_sector_count_reg];
  327. if (sector_count == 0)
  328. controller->current_transfer = 256;
  329. else
  330. controller->current_transfer = sector_count;
  331. }
  332. /* the transfer size */
  333. if (controller->current_drive == NULL)
  334. controller->fifo_size = 512;
  335. else
  336. controller->fifo_size = controller->current_drive->geometry.byte;
  337. /* empty the fifo */
  338. controller->fifo_pos = 0;
  339. /* the starting address */
  340. if (controller->current_drive == NULL)
  341. controller->current_byte = 0;
  342. else if (controller->reg[ide_drive_head_reg] & 0x40) {
  343. /* LBA addressing mode */
  344. controller->current_byte = controller->fifo_size
  345. * (((controller->reg[ide_drive_head_reg] & 0xf) << 24)
  346. | (controller->reg[ide_cylinder_reg1] << 16)
  347. | (controller->reg[ide_cylinder_reg0] << 8)
  348. | (controller->reg[ide_sector_number_reg]));
  349. }
  350. else if (controller->current_drive->geometry.head != 0
  351. && controller->current_drive->geometry.sector != 0) {
  352. /* CHS addressing mode */
  353. int head_nr = controller->reg[ide_drive_head_reg] & 0xf;
  354. int cylinder_nr = ((controller->reg[ide_cylinder_reg1] << 8)
  355. | controller->reg[ide_cylinder_reg0]);
  356. int sector_nr = controller->reg[ide_sector_number_reg];
  357. controller->current_byte = controller->fifo_size
  358. * ((cylinder_nr * controller->current_drive->geometry.head + head_nr)
  359. * controller->current_drive->geometry.sector + sector_nr - 1);
  360. }
  361. else
  362. device_error(me, "controller %d:%d - CHS addressing disabled",
  363. controller->nr, controller->current_drive->nr);
  364. DTRACE(ide, ("controller %ld:%ld - transfer (%s) %ld blocks of %ld bytes from 0x%lx\n",
  365. (long)controller->nr,
  366. controller->current_drive == NULL ? -1L : (long)controller->current_drive->nr,
  367. direction == is_read ? "read" : "write",
  368. (long)controller->current_transfer,
  369. (long)controller->fifo_size,
  370. (unsigned long)controller->current_byte));
  371. switch (direction) {
  372. case is_read:
  373. /* force a primeing read */
  374. controller->current_transfer += 1;
  375. controller->state = draining_state;
  376. controller->fifo_pos = controller->fifo_size;
  377. do_fifo_read(me, controller, NULL, 0);
  378. break;
  379. case is_write:
  380. controller->state = loading_state;
  381. break;
  382. }
  383. }
  384. static void
  385. do_command(device *me,
  386. ide_controller *controller,
  387. int command)
  388. {
  389. if (controller->state != idle_state)
  390. device_error(me, "controller %d - command when not idle", controller->nr);
  391. switch (command) {
  392. case 0x20: case 0x21: /* read-sectors */
  393. setup_fifo(me, controller, 0/*is_simple*/, 1/*is_with_disk*/, is_read);
  394. break;
  395. case 0x30: case 0x31: /* write */
  396. setup_fifo(me, controller, 0/*is_simple*/, 1/*is_with_disk*/, is_write);
  397. break;
  398. }
  399. }
  400. static uint8_t
  401. get_status(device *me,
  402. ide_controller *controller)
  403. {
  404. switch (controller->state) {
  405. case loading_state:
  406. case draining_state:
  407. return 0x08; /* data req */
  408. case busy_loaded_state:
  409. case busy_drained_state:
  410. return 0x80; /* busy */
  411. case idle_state:
  412. return 0x40; /* drive ready */
  413. default:
  414. device_error(me, "internal error");
  415. return 0;
  416. }
  417. }
  418. /* The address presented to the IDE controler is decoded and then
  419. mapped onto a controller:reg pair */
  420. enum {
  421. nr_address_blocks = 6,
  422. };
  423. typedef struct _address_block {
  424. int space;
  425. unsigned_word base_addr;
  426. unsigned_word bound_addr;
  427. int controller;
  428. int base_reg;
  429. } address_block;
  430. typedef struct _address_decoder {
  431. address_block block[nr_address_blocks];
  432. } address_decoder;
  433. static void
  434. decode_address(device *me,
  435. address_decoder *decoder,
  436. int space,
  437. unsigned_word address,
  438. int *controller,
  439. int *reg,
  440. io_direction direction)
  441. {
  442. int i;
  443. for (i = 0; i < nr_address_blocks; i++) {
  444. if (space == decoder->block[i].space
  445. && address >= decoder->block[i].base_addr
  446. && address <= decoder->block[i].bound_addr) {
  447. *controller = decoder->block[i].controller;
  448. *reg = (address
  449. - decoder->block[i].base_addr
  450. + decoder->block[i].base_reg);
  451. if (direction == is_write) {
  452. switch (*reg) {
  453. case ide_error_reg: *reg = ide_feature_reg; break;
  454. case ide_status_reg: *reg = ide_command_reg; break;
  455. case ide_alternate_status_reg: *reg = ide_control_reg; break;
  456. default: break;
  457. }
  458. }
  459. return;
  460. }
  461. }
  462. device_error(me, "address %d:0x%lx invalid",
  463. space, (unsigned long)address);
  464. }
  465. static void
  466. build_address_decoder(device *me,
  467. address_decoder *decoder)
  468. {
  469. int reg;
  470. for (reg = 1; reg < 6; reg++) {
  471. reg_property_spec unit;
  472. int space;
  473. unsigned_word address;
  474. unsigned size;
  475. /* find and decode the reg property */
  476. if (!device_find_reg_array_property(me, "reg", reg, &unit))
  477. device_error(me, "missing or invalid reg entry %d", reg);
  478. device_address_to_attach_address(device_parent(me), &unit.address,
  479. &space, &address, me);
  480. device_size_to_attach_size(device_parent(me), &unit.size, &size, me);
  481. /* insert it into the address decoder */
  482. switch (reg) {
  483. case 1:
  484. case 2:
  485. /* command register block */
  486. if (size != 8)
  487. device_error(me, "reg entry %d must have a size of 8", reg);
  488. decoder->block[reg-1].space = space;
  489. decoder->block[reg-1].base_addr = address;
  490. decoder->block[reg-1].bound_addr = address + size - 1;
  491. decoder->block[reg-1].controller = (reg + 1) % nr_ide_controllers;
  492. decoder->block[reg-1].base_reg = ide_data_reg;
  493. DTRACE(ide, ("controller %d command register block at %d:0x%lx..0x%lx\n",
  494. decoder->block[reg-1].controller,
  495. decoder->block[reg-1].space,
  496. (unsigned long)decoder->block[reg-1].base_addr,
  497. (unsigned long)decoder->block[reg-1].bound_addr));
  498. break;
  499. case 3:
  500. case 4:
  501. /* control register block */
  502. if (size != 1)
  503. device_error(me, "reg entry %d must have a size of 1", reg);
  504. decoder->block[reg-1].space = space;
  505. decoder->block[reg-1].base_addr = address;
  506. decoder->block[reg-1].bound_addr = address + size - 1;
  507. decoder->block[reg-1].controller = (reg + 1) % nr_ide_controllers;
  508. decoder->block[reg-1].base_reg = ide_alternate_status_reg;
  509. DTRACE(ide, ("controller %d control register block at %d:0x%lx..0x%lx\n",
  510. decoder->block[reg-1].controller,
  511. decoder->block[reg-1].space,
  512. (unsigned long)decoder->block[reg-1].base_addr,
  513. (unsigned long)decoder->block[reg-1].bound_addr));
  514. break;
  515. case 5:
  516. /* dma register block */
  517. if (size != 8)
  518. device_error(me, "reg entry %d must have a size of 8", reg);
  519. decoder->block[reg-1].space = space;
  520. decoder->block[reg-1].base_addr = address;
  521. decoder->block[reg-1].bound_addr = address + 4 - 1;
  522. decoder->block[reg-1].base_reg = ide_dma_command_reg;
  523. decoder->block[reg-1].controller = 0;
  524. DTRACE(ide, ("controller %d dma register block at %d:0x%lx..0x%lx\n",
  525. decoder->block[reg-1].controller,
  526. decoder->block[reg-1].space,
  527. (unsigned long)decoder->block[reg-1].base_addr,
  528. (unsigned long)decoder->block[reg-1].bound_addr));
  529. decoder->block[reg].space = space;
  530. decoder->block[reg].base_addr = address + 4;
  531. decoder->block[reg].bound_addr = address + 8 - 1;
  532. decoder->block[reg].controller = 1;
  533. decoder->block[reg].base_reg = ide_dma_command_reg;
  534. DTRACE(ide, ("controller %d dma register block at %d:0x%lx..0x%lx\n",
  535. decoder->block[reg].controller,
  536. decoder->block[reg-1].space,
  537. (unsigned long)decoder->block[reg].base_addr,
  538. (unsigned long)decoder->block[reg].bound_addr));
  539. break;
  540. default:
  541. device_error(me, "internal error - bad switch");
  542. break;
  543. }
  544. }
  545. }
  546. typedef struct _hw_ide_device {
  547. ide_controller controller[nr_ide_controllers];
  548. address_decoder decoder;
  549. } hw_ide_device;
  550. static void
  551. hw_ide_init_address(device *me)
  552. {
  553. hw_ide_device *ide = device_data(me);
  554. int controller;
  555. int drive;
  556. /* zero some things */
  557. for (controller = 0; controller < nr_ide_controllers; controller++) {
  558. memset(&ide->controller[controller], 0, sizeof(ide_controller));
  559. for (drive = 0; drive < nr_ide_drives_per_controller; drive++) {
  560. ide->controller[controller].drive[drive].nr = drive;
  561. }
  562. ide->controller[controller].me = me;
  563. if (device_find_property(me, "ready-delay") != NULL)
  564. ide->controller[controller].ready_delay =
  565. device_find_integer_property(me, "ready-delay");
  566. }
  567. /* attach this device to its parent */
  568. generic_device_init_address(me);
  569. /* determine our own address map */
  570. build_address_decoder(me, &ide->decoder);
  571. }
  572. static void
  573. hw_ide_attach_address(device *me,
  574. attach_type type,
  575. int space,
  576. unsigned_word addr,
  577. unsigned nr_bytes,
  578. access_type access,
  579. device *client) /*callback/default*/
  580. {
  581. hw_ide_device *ide = (hw_ide_device*)device_data(me);
  582. int controller_nr = addr / nr_ide_drives_per_controller;
  583. int drive_nr = addr % nr_ide_drives_per_controller;
  584. ide_controller *controller;
  585. ide_drive *drive;
  586. if (controller_nr >= nr_ide_controllers)
  587. device_error(me, "no controller for disk %s",
  588. device_path(client));
  589. controller = &ide->controller[controller_nr];
  590. drive = &controller->drive[drive_nr];
  591. drive->device = client;
  592. if (device_find_property(client, "ide-byte-count") != NULL)
  593. drive->geometry.byte = device_find_integer_property(client, "ide-byte-count");
  594. else
  595. drive->geometry.byte = 512;
  596. if (device_find_property(client, "ide-sector-count") != NULL)
  597. drive->geometry.sector = device_find_integer_property(client, "ide-sector-count");
  598. if (device_find_property(client, "ide-head-count") != NULL)
  599. drive->geometry.head = device_find_integer_property(client, "ide-head-count");
  600. drive->default_geometry = drive->geometry;
  601. DTRACE(ide, ("controller %d:%d %s byte-count %d, sector-count %d, head-count %d\n",
  602. controller_nr,
  603. drive->nr,
  604. device_path(client),
  605. drive->geometry.byte,
  606. drive->geometry.sector,
  607. drive->geometry.head));
  608. }
  609. static unsigned
  610. hw_ide_io_read_buffer(device *me,
  611. void *dest,
  612. int space,
  613. unsigned_word addr,
  614. unsigned nr_bytes,
  615. cpu *processor,
  616. unsigned_word cia)
  617. {
  618. hw_ide_device *ide = (hw_ide_device *)device_data(me);
  619. int control_nr;
  620. int reg;
  621. ide_controller *controller;
  622. /* find the interface */
  623. decode_address(me, &ide->decoder, space, addr, &control_nr, &reg, is_read);
  624. controller = & ide->controller[control_nr];
  625. /* process the transfer */
  626. memset(dest, 0, nr_bytes);
  627. switch (reg) {
  628. case ide_data_reg:
  629. do_fifo_read(me, controller, dest, nr_bytes);
  630. break;
  631. case ide_status_reg:
  632. *(uint8_t*)dest = get_status(me, controller);
  633. clear_interrupt(me, controller);
  634. break;
  635. case ide_alternate_status_reg:
  636. *(uint8_t*)dest = get_status(me, controller);
  637. break;
  638. case ide_error_reg:
  639. case ide_sector_count_reg:
  640. case ide_sector_number_reg:
  641. case ide_cylinder_reg0:
  642. case ide_cylinder_reg1:
  643. case ide_drive_head_reg:
  644. case ide_control_reg:
  645. case ide_dma_command_reg:
  646. case ide_dma_status_reg:
  647. case ide_dma_prd_table_address_reg0:
  648. case ide_dma_prd_table_address_reg1:
  649. case ide_dma_prd_table_address_reg2:
  650. case ide_dma_prd_table_address_reg3:
  651. *(uint8_t*)dest = controller->reg[reg];
  652. break;
  653. default:
  654. device_error(me, "bus-error at address 0x%lx", (unsigned long)addr);
  655. break;
  656. }
  657. return nr_bytes;
  658. }
  659. static unsigned
  660. hw_ide_io_write_buffer(device *me,
  661. const void *source,
  662. int space,
  663. unsigned_word addr,
  664. unsigned nr_bytes,
  665. cpu *processor,
  666. unsigned_word cia)
  667. {
  668. hw_ide_device *ide = (hw_ide_device *)device_data(me);
  669. int control_nr;
  670. int reg;
  671. ide_controller *controller;
  672. /* find the interface */
  673. decode_address(me, &ide->decoder, space, addr, &control_nr, &reg, is_write);
  674. controller = &ide->controller[control_nr];
  675. /* process the access */
  676. switch (reg) {
  677. case ide_data_reg:
  678. do_fifo_write(me, controller, source, nr_bytes);
  679. break;
  680. case ide_command_reg:
  681. do_command(me, controller, *(uint8_t*)source);
  682. break;
  683. case ide_control_reg:
  684. controller->reg[reg] = *(uint8_t*)source;
  685. /* possibly cancel interrupts */
  686. if ((controller->reg[reg] & 0x02) == 0x02)
  687. clear_interrupt(me, controller);
  688. break;
  689. case ide_feature_reg:
  690. case ide_sector_count_reg:
  691. case ide_sector_number_reg:
  692. case ide_cylinder_reg0:
  693. case ide_cylinder_reg1:
  694. case ide_drive_head_reg:
  695. case ide_dma_command_reg:
  696. case ide_dma_status_reg:
  697. case ide_dma_prd_table_address_reg0:
  698. case ide_dma_prd_table_address_reg1:
  699. case ide_dma_prd_table_address_reg2:
  700. case ide_dma_prd_table_address_reg3:
  701. controller->reg[reg] = *(uint8_t*)source;
  702. break;
  703. default:
  704. device_error(me, "bus-error at 0x%lx", (unsigned long)addr);
  705. break;
  706. }
  707. return nr_bytes;
  708. }
  709. static const device_interrupt_port_descriptor hw_ide_interrupt_ports[] = {
  710. { "a", 0, 0 },
  711. { "b", 1, 0 },
  712. { "c", 2, 0 },
  713. { "d", 3, 0 },
  714. { NULL }
  715. };
  716. static device_callbacks const hw_ide_callbacks = {
  717. { hw_ide_init_address, },
  718. { hw_ide_attach_address, }, /* attach */
  719. { hw_ide_io_read_buffer, hw_ide_io_write_buffer, },
  720. { NULL, }, /* DMA */
  721. { NULL, NULL, hw_ide_interrupt_ports }, /* interrupt */
  722. { generic_device_unit_decode,
  723. generic_device_unit_encode,
  724. generic_device_address_to_attach_address,
  725. generic_device_size_to_attach_size },
  726. };
  727. static void *
  728. hw_ide_create(const char *name,
  729. const device_unit *unit_address,
  730. const char *args)
  731. {
  732. hw_ide_device *ide = ZALLOC(hw_ide_device);
  733. return ide;
  734. }
  735. const device_descriptor hw_ide_device_descriptor[] = {
  736. { "ide", hw_ide_create, &hw_ide_callbacks },
  737. { NULL, },
  738. };
  739. #endif /* _HW_IDE_ */