]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/disk/ide/v2_0/src/ide_disk.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / devs / disk / ide / v2_0 / src / ide_disk.c
1 //==========================================================================
2 //
3 //      ide_disk.c
4 //
5 //      IDE polled mode disk driver 
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Red Hat, Inc.
12 // Copyright (C) 2004, 2006 eCosCentric, Ltd.
13 //
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 //
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
33 //
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 //
37 // -------------------------------------------
38 //####ECOSGPLCOPYRIGHTEND####
39 //==========================================================================
40 //#####DESCRIPTIONBEGIN####
41 //
42 // Author(s):    iz
43 // Contributors: 
44 // Date:         2004-10-16
45 //
46 //####DESCRIPTIONEND####
47 //
48 //==========================================================================
49
50 #include <pkgconf/devs_disk_ide.h>
51
52 #include <cyg/infra/cyg_type.h>
53 #include <cyg/infra/cyg_ass.h>
54 #include <cyg/infra/diag.h>
55 #include <cyg/hal/hal_arch.h>
56 #include <cyg/hal/hal_io.h>
57 #include <cyg/hal/hal_if.h>             // delays
58 #include <cyg/hal/drv_api.h>
59 #include <cyg/io/io.h>
60 #include <cyg/io/devtab.h>
61 #include <cyg/io/disk.h>
62
63 #include "ide_disk.h"
64
65 // ----------------------------------------------------------------------------
66
67 //#define DEBUG 1
68
69 #ifdef DEBUG
70 # define D(fmt,args...) diag_printf(fmt, ## args)
71 #else
72 # define D(fmt,args...)
73 #endif
74
75 // ----------------------------------------------------------------------------
76
77 // No h/w controller structure is needed in this driver, but the address of the
78 // second argument is taken anyway.
79 DISK_CONTROLLER(ide_disk_controller, ide_disk_controller);
80
81 #ifdef CYGVAR_DEVS_DISK_IDE_DISK0
82 IDE_DISK_INSTANCE(0, 0, 0, true, CYGDAT_IO_DISK_IDE_DISK0_NAME);
83 #endif
84
85 #ifdef CYGVAR_DEVS_DISK_IDE_DISK1
86 IDE_DISK_INSTANCE(1, 0, 1, true, CYGDAT_IO_DISK_IDE_DISK1_NAME);
87 #endif
88
89 #ifdef CYGVAR_DEVS_DISK_IDE_DISK2
90 IDE_DISK_INSTANCE(2, 1, 0, true, CYGDAT_IO_DISK_IDE_DISK2_NAME);
91 #endif
92
93 #ifdef CYGVAR_DEVS_DISK_IDE_DISK3
94 IDE_DISK_INSTANCE(3, 1, 1, true, CYGDAT_IO_DISK_IDE_DISK3_NAME);
95 #endif
96
97 // ----------------------------------------------------------------------------
98
99 static void
100 id_strcpy(char *dest, cyg_uint16 *src, cyg_uint16 size)
101 {
102     int i;
103
104     for (i = 0; i < size; i+=2)
105     {
106         *dest++ = (char)(*src >> 8);
107         *dest++ = (char)(*src & 0x00FF);
108         src++;
109     }
110     *dest = '\0';
111 }
112
113 // ----------------------------------------------------------------------------
114
115 static inline void
116 __wait_for_ready(int ctlr)
117 {
118     cyg_uint8 status;
119     do {
120          HAL_IDE_READ_UINT8(ctlr, IDE_REG_STATUS, status);
121     } while (status & (IDE_STAT_BSY | IDE_STAT_DRQ));
122 }
123
124 // Wait while the device is busy with the last command
125 static inline int
126 __wait_busy(int ctlr)
127 {
128     cyg_uint8 status;
129     cyg_ucount32 tries;
130     
131     for (tries=0; tries < 1000000; tries++) {
132          CYGACC_CALL_IF_DELAY_US(10);
133          HAL_IDE_READ_UINT8(ctlr, IDE_REG_STATUS, status);
134          if ((status & IDE_STAT_BSY) == 0)
135               return 1;
136     }
137     return 0;   
138 }
139
140 static inline int
141 __wait_for_drq(int ctlr)
142 {
143     cyg_uint8 status;
144     cyg_ucount32 tries;
145
146     for (tries=0; tries<1000000; tries++) {
147         CYGACC_CALL_IF_DELAY_US(10);
148         HAL_IDE_READ_UINT8(ctlr, IDE_REG_STATUS, status);
149         if (!(status & IDE_STAT_BSY)) {
150             if (status & IDE_STAT_DRQ)
151                 return 1;
152             else
153                 return 0;
154         }
155     }
156     return 0;
157 }
158
159 // Return true if any devices attached to controller
160 static int
161 ide_presence_detect(int ctlr)
162 {
163     cyg_uint8 sel, val;
164     int i;
165
166     for (i = 0; i < 2; i++) {
167         sel = (i << 4) | 0xA0;
168         CYGACC_CALL_IF_DELAY_US((cyg_uint32)50000);
169         HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_DEVICE, sel);
170         CYGACC_CALL_IF_DELAY_US((cyg_uint32)50000);
171         HAL_IDE_READ_UINT8(ctlr, IDE_REG_DEVICE, val);
172         if (val == sel) {
173 #ifndef CYGSEM_DEVS_DISK_IDE_VMWARE
174             if (i)
175                 HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_DEVICE, 0);
176 #endif
177             return 1;
178         }
179     }
180     return 0;
181 }
182
183 static int
184 ide_reset(int ctlr)
185 {
186     cyg_uint8 status;
187     int delay;
188 //
189 // VMware note:
190 // VMware virtual IDE device handler obviously expects that
191 // the reset and setup functions were already done
192 // by it's bios and complains if one uses reset here...
193 //
194 #ifndef CYGSEM_DEVS_DISK_IDE_VMWARE
195     HAL_IDE_WRITE_CONTROL(ctlr, 6);    // polled mode, reset asserted
196     CYGACC_CALL_IF_DELAY_US(5000);
197     HAL_IDE_WRITE_CONTROL(ctlr, 2);   // polled mode, reset cleared
198     CYGACC_CALL_IF_DELAY_US((cyg_uint32)50000);
199 #endif
200
201     // wait 30 seconds max for not busy and drive ready
202     for (delay = 0; delay < 300; ++delay) {
203         CYGACC_CALL_IF_DELAY_US((cyg_uint32)100000);
204         HAL_IDE_READ_UINT8(ctlr, IDE_REG_STATUS, status);
205             if (!(status & IDE_STAT_BSY)) {
206                 if (status & IDE_STAT_DRDY) {
207                     return 1;
208                 }
209             }
210     }
211     return 0;
212 }
213
214 static int
215 ide_ident(int ctlr, int dev, cyg_uint16 *buf)
216 {
217     int i;
218
219     if (!__wait_busy(ctlr)) {
220          return 0;
221     }
222     
223     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_DEVICE, dev << 4);
224     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_COMMAND, 0xEC);
225     CYGACC_CALL_IF_DELAY_US((cyg_uint32)50000);
226
227     if (!__wait_for_drq(ctlr)) {
228          return 0;
229     }
230     
231     for (i = 0; i < (CYGDAT_DEVS_DISK_IDE_SECTOR_SIZE / sizeof(cyg_uint16));
232          i++, buf++)
233         HAL_IDE_READ_UINT16(ctlr, IDE_REG_DATA, *buf);
234
235     return 1;
236 }
237
238 static int
239 ide_read_sector(int ctlr, int dev, cyg_uint32 start, 
240                 cyg_uint8 *buf, cyg_uint32 len)
241 {
242     int j, c;
243     cyg_uint16 p;
244     cyg_uint8 * b=buf;
245
246     if(!__wait_busy(ctlr)) {
247          return 0;
248     }
249     
250     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_COUNT, 1);    // count =1
251     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_LBALOW, start & 0xff);
252     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_LBAMID, (start >>  8) & 0xff);
253     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_LBAHI,  (start >> 16) & 0xff);
254     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_DEVICE,
255           ((start >> 24) & 0xf) | (dev << 4) | 0x40);
256     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_COMMAND, 0x20);
257
258     if (!__wait_for_drq(ctlr))
259         return 0;
260     // 
261     // It would be fine if all buffers were word aligned,
262     // but who knows
263     //
264     for (j = 0, c=0 ; j < (CYGDAT_DEVS_DISK_IDE_SECTOR_SIZE / sizeof(cyg_uint16));
265          j++) {
266         HAL_IDE_READ_UINT16(ctlr, IDE_REG_DATA, p);
267         if (c++<(len*512)) *b++=p&0xff;
268         if (c++<(len*512)) *b++=(p>>8)&0xff;
269     }
270     return 1;
271 }
272
273 static int
274 ide_write_sector(int ctlr, int dev, cyg_uint32 start, 
275                 cyg_uint8 *buf, cyg_uint32 len)
276 {
277     int j, c;
278     cyg_uint16 p;
279     cyg_uint8 * b=buf;
280
281     if(!__wait_busy(ctlr)) {
282          return 0;
283     }
284     
285     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_COUNT, 1);    // count =1
286     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_LBALOW, start & 0xff);
287     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_LBAMID, (start >>  8) & 0xff);
288     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_LBAHI,  (start >> 16) & 0xff);
289     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_DEVICE,
290           ((start >> 24) & 0xf) | (dev << 4) | 0x40);
291     HAL_IDE_WRITE_UINT8(ctlr, IDE_REG_COMMAND, 0x30);
292
293     if (!__wait_for_drq(ctlr))
294         return 0;
295     // 
296     // It would be fine if all buffers were word aligned,
297     // but who knows
298     //
299     for (j = 0, c=0 ; j < (CYGDAT_DEVS_DISK_IDE_SECTOR_SIZE / sizeof(cyg_uint16));
300          j++) {
301         p = (c++<(len*512)) ? *b++ : 0;
302         p |= (c++<(len*512)) ? (*b++<<8) : 0; 
303         HAL_IDE_WRITE_UINT16(ctlr, IDE_REG_DATA, p);
304     }
305     return 1;
306 }
307
308 // ----------------------------------------------------------------------------
309
310 static cyg_bool 
311 ide_disk_init(struct cyg_devtab_entry *tab)
312 {
313     disk_channel *chan = (disk_channel *) tab->priv;
314     ide_disk_info_t *info = (ide_disk_info_t *) chan->dev_priv;
315     cyg_uint32 id_buf[CYGDAT_DEVS_DISK_IDE_SECTOR_SIZE/sizeof(cyg_uint32)];
316     static int num_controllers;
317     static int ide_present[4], ide_reset_done[4];
318     cyg_disk_identify_t ident;
319     ide_identify_data_t *ide_idData=(ide_identify_data_t*)id_buf;
320     
321     if (chan->init) 
322         return true;
323
324     D("IDE(%d:%d) hw init\n", info->port, info->chan);
325     
326     if (!num_controllers) num_controllers=HAL_IDE_INIT();
327     if (info->chan>=num_controllers) {
328         D("No IDE controller for channel %d:%d\n", info->port, info->chan);
329         return false;
330     }
331     if (!ide_present[info->port]) {
332         ide_present[info->port]=ide_presence_detect(info->port);
333         if (!ide_present[info->port]) {
334             diag_printf("No devices on IDE controller #%d\n",info->port);
335             return false;
336         }
337     }
338     if (!ide_reset_done[info->port]) {
339         ide_reset_done[info->port]=ide_reset(info->port);
340         if (!ide_reset_done[info->port]) {
341             D("Controller #%d reset failure\n",info->port);
342             return false;
343         }
344     }
345     
346     D("IDE %d:%d identify drive\n", info->port, info->chan);
347     
348     if (!ide_ident(info->port, info->chan, (cyg_uint16 *)id_buf)) {
349         diag_printf("IDE %d:%d ident DRQ error\n", info->port, info->chan);
350         return false;
351     }
352
353     id_strcpy(ident.serial, ide_idData->serial,       20);
354     id_strcpy(ident.firmware_rev, ide_idData->firmware_rev, 8);
355     id_strcpy(ident.model_num, ide_idData->model_num,    40);
356     
357     ident.cylinders_num  = ide_idData->num_cylinders;
358     ident.heads_num = ide_idData->num_heads;
359     ident.sectors_num = ide_idData->num_sectors;
360     ident.lba_sectors_num = ide_idData->lba_total_sectors[1] << 16 | 
361                             ide_idData->lba_total_sectors[0];
362     ident.phys_block_size = 1;
363     ident.max_transfer = 512;
364     
365     D("\tSerial : %s\n", ident.serial);
366     D("\tFirmware rev. : %s\n", ident.firmware_rev);
367     D("\tModel : %s\n", ident.model_num);
368     D("\tC/H/S : %d/%d/%d\n", ident.cylinders_num, 
369                               ident.heads_num, ident.sectors_num);
370     D("\tKind : %x\n", (ide_idData->general_conf>>8)&0x1f);
371
372     if (((ide_idData->general_conf>>8)&0x1f)!=2) {
373         diag_printf("IDE device %d:%d is not a hard disk!\n",
374                     info->port, info->chan);
375         return false;
376     }
377     if (!(chan->callbacks->disk_init)(tab))
378         return false;
379
380     if (ENOERR != (chan->callbacks->disk_connected)(tab, &ident))
381         return false;
382
383     return true;
384 }
385
386 // ----------------------------------------------------------------------------
387
388 static Cyg_ErrNo 
389 ide_disk_lookup(struct cyg_devtab_entry **tab, 
390                 struct cyg_devtab_entry  *sub_tab,
391                 const char *name)
392 {
393     disk_channel *chan = (disk_channel *) (*tab)->priv;
394     return (chan->callbacks->disk_lookup)(tab, sub_tab, name);
395 }
396
397 // ----------------------------------------------------------------------------
398
399 static Cyg_ErrNo 
400 ide_disk_read(disk_channel *chan, 
401               void         *buf,
402               cyg_uint32    len, 
403               cyg_uint32    block_num)
404 {
405     ide_disk_info_t *info = (ide_disk_info_t *)chan->dev_priv;
406
407     D("IDE %d:%d read block %d\n", info->port, info->chan, block_num);
408
409     if (!ide_read_sector(info->port, info->chan, block_num, 
410                          (cyg_uint8 *)buf, len)) {
411         diag_printf("IDE %d:%d read DRQ error\n", info->port, info->chan);
412         return -EIO; 
413     }
414
415     return ENOERR;
416 }
417
418 // ----------------------------------------------------------------------------
419
420 static Cyg_ErrNo 
421 ide_disk_write(disk_channel *chan, 
422                const void   *buf,
423                cyg_uint32    len, 
424                cyg_uint32    block_num)
425 {
426     ide_disk_info_t *info = (ide_disk_info_t *)chan->dev_priv;
427
428     D("IDE %d:%d write block %d\n", info->port, info->chan, block_num);
429
430     if (!ide_write_sector(info->port, info->chan, block_num, 
431                          (cyg_uint8 *)buf, len)) {
432         diag_printf("IDE %d:%d read DRQ error\n", info->port, info->chan);
433         return -EIO; 
434     }
435         
436     return ENOERR;
437 }
438
439 // ----------------------------------------------------------------------------
440
441 static Cyg_ErrNo
442 ide_disk_get_config(disk_channel *chan, 
443                     cyg_uint32    key,
444                     const void   *xbuf, 
445                     cyg_uint32   *len)
446 {
447     return -EINVAL;
448 }
449
450 // ----------------------------------------------------------------------------
451
452 static Cyg_ErrNo
453 ide_disk_set_config(disk_channel *chan, 
454                     cyg_uint32    key,
455                     const void   *xbuf, 
456                     cyg_uint32   *len)
457 {
458     return -EINVAL;
459 }
460
461 //EOF ide_disk.c