Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Michael Sevakis | 2541302 | 2008-03-31 04:53:03 +0000 | [diff] [blame] | 8 | * $Id$ |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 9 | * |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 10 | * Copyright (C) 2006,2007 by Marcoen Hirschberg |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
| 22 | #include "cpu.h" |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 23 | #include "kernel.h" |
Michael Sevakis | a05bb26 | 2007-04-30 12:54:05 +0000 | [diff] [blame] | 24 | #include "thread.h" |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 25 | #include "system.h" |
| 26 | #include "power.h" |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 27 | #include "panic.h" |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 28 | #include "pcf50606.h" |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 29 | #include "ata-target.h" |
Steve Gotthardt | c07814f | 2007-01-07 06:38:57 +0000 | [diff] [blame] | 30 | #include "backlight-target.h" |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 31 | |
Karl Kurbjun | 60fed9a | 2007-04-11 05:30:15 +0000 | [diff] [blame] | 32 | /* ARESET on C7C68300 and RESET on ATA interface (Active Low) */ |
| 33 | #define ATA_RESET_ENABLE GPGDAT &= ~(1 << 10) |
| 34 | #define ATA_RESET_DISABLE GPGDAT |= (1 << 10) |
| 35 | |
| 36 | /* ATA_EN on C7C68300 */ |
| 37 | #define USB_ATA_ENABLE GPBDAT |= (1 << 5) |
| 38 | #define USB_ATA_DISABLE GPBDAT &= ~(1 << 5) |
| 39 | |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 40 | void ata_reset(void) |
| 41 | { |
Karl Kurbjun | 60fed9a | 2007-04-11 05:30:15 +0000 | [diff] [blame] | 42 | ATA_RESET_ENABLE; |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 43 | sleep(1); /* > 25us */ |
Karl Kurbjun | 60fed9a | 2007-04-11 05:30:15 +0000 | [diff] [blame] | 44 | ATA_RESET_DISABLE; |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 45 | sleep(1); /* > 2ms */ |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Karl Kurbjun | 60fed9a | 2007-04-11 05:30:15 +0000 | [diff] [blame] | 48 | /* This function is called before enabling the USB bus */ |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 49 | void ata_enable(bool on) |
| 50 | { |
Karl Kurbjun | 173b611 | 2007-05-07 19:34:34 +0000 | [diff] [blame] | 51 | GPBCON=( GPBCON&~(1<<11) ) | (1<<10); /* Make the pin an output */ |
| 52 | GPBUP|=1<<5; /* Disable pullup in SOC as we are now driving */ |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 53 | if(on) |
Karl Kurbjun | 60fed9a | 2007-04-11 05:30:15 +0000 | [diff] [blame] | 54 | USB_ATA_DISABLE; |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 55 | else |
Karl Kurbjun | 60fed9a | 2007-04-11 05:30:15 +0000 | [diff] [blame] | 56 | USB_ATA_ENABLE; |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool ata_is_coldstart(void) |
| 60 | { |
Karl Kurbjun | 60fed9a | 2007-04-11 05:30:15 +0000 | [diff] [blame] | 61 | /* Check the pin configuration - return true when pin is unconfigured */ |
| 62 | return (GPGCON & 0x00300000) == 0; |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void ata_device_init(void) |
| 66 | { |
Karl Kurbjun | 60fed9a | 2007-04-11 05:30:15 +0000 | [diff] [blame] | 67 | GPGCON=( GPGCON&~(1<<21) ) | (1<<20); /* Make the pin an output */ |
| 68 | GPGUP |= 1<<10; /* Disable pullup in SOC as we are now driving */ |
Karl Kurbjun | 173b611 | 2007-05-07 19:34:34 +0000 | [diff] [blame] | 69 | /* ATA reset */ |
| 70 | ATA_RESET_DISABLE; /* Set the pin to disable an active low reset */ |
Marcoen Hirschberg | dd75488 | 2006-08-12 08:01:54 +0000 | [diff] [blame] | 71 | } |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 72 | |
Greg White | 355be50 | 2007-01-13 02:24:15 +0000 | [diff] [blame] | 73 | #if !defined(BOOTLOADER) |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 74 | void copy_read_sectors(unsigned char* buf, int wordcount) |
| 75 | { |
Steve Gotthardt | 93b047f | 2007-01-08 08:53:36 +0000 | [diff] [blame] | 76 | __buttonlight_trigger(); |
Greg White | 355be50 | 2007-01-13 02:24:15 +0000 | [diff] [blame] | 77 | |
Greg White | ebcd762 | 2007-01-06 01:26:36 +0000 | [diff] [blame] | 78 | /* Unaligned transfer - slow copy */ |
| 79 | if ( (unsigned long)buf & 1) |
| 80 | { /* not 16-bit aligned, copy byte by byte */ |
| 81 | unsigned short tmp = 0; |
| 82 | unsigned char* bufend = buf + wordcount*2; |
| 83 | do |
| 84 | { |
| 85 | tmp = ATA_DATA; |
| 86 | *buf++ = tmp & 0xff; /* I assume big endian */ |
| 87 | *buf++ = tmp >> 8; /* and don't use the SWAB16 macro */ |
| 88 | } while (buf < bufend); /* tail loop is faster */ |
| 89 | return; |
| 90 | } |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 91 | /* This should never happen, but worth watching for */ |
| 92 | if(wordcount > (1 << 18)) |
| 93 | panicf("atd-meg-fx.c: copy_read_sectors: too many sectors per read!"); |
Greg White | 5668505 | 2007-01-05 04:17:25 +0000 | [diff] [blame] | 94 | |
| 95 | //#define GIGABEAT_DEBUG_ATA |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 96 | #ifdef GIGABEAT_DEBUG_ATA |
Greg White | ebcd762 | 2007-01-06 01:26:36 +0000 | [diff] [blame] | 97 | static int line = 0; |
| 98 | static char str[256]; |
| 99 | snprintf(str, sizeof(str), "ODD DMA to %08x, %d", buf, wordcount); |
| 100 | lcd_puts(10, line, str); |
| 101 | line = (line+1) % 32; |
| 102 | lcd_update(); |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 103 | #endif |
| 104 | /* Reset the channel */ |
| 105 | DMASKTRIG0 |= 4; |
| 106 | /* Wait for DMA controller to be ready */ |
| 107 | while(DMASKTRIG0 & 0x2) |
| 108 | ; |
| 109 | while(DSTAT0 & (1 << 20)) |
| 110 | ; |
| 111 | /* Source is ATA_DATA, on AHB Bus, Fixed */ |
| 112 | DISRC0 = (int) 0x18000000; |
| 113 | DISRCC0 = 0x1; |
| 114 | /* Dest mapped to physical address, on AHB bus, increment */ |
Greg White | 355be50 | 2007-01-13 02:24:15 +0000 | [diff] [blame] | 115 | DIDST0 = (int) buf; |
| 116 | if(DIDST0 < 0x30000000) |
| 117 | DIDST0 += 0x30000000; |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 118 | DIDSTC0 = 0; |
| 119 | |
Michael Sevakis | a05bb26 | 2007-04-30 12:54:05 +0000 | [diff] [blame] | 120 | /* DACK/DREQ Sync to AHB, Whole service, No reload, 16-bit transfers */ |
| 121 | DCON0 = ((1 << 30) | (1<<27) | (1<<22) | (1<<20)) | wordcount; |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 122 | |
| 123 | /* Activate the channel */ |
| 124 | DMASKTRIG0 = 0x2; |
| 125 | |
Greg White | ebcd762 | 2007-01-06 01:26:36 +0000 | [diff] [blame] | 126 | invalidate_dcache_range((void *)buf, wordcount*2); |
Greg White | 6eb81e2 | 2007-01-04 11:35:09 +0000 | [diff] [blame] | 127 | |
| 128 | /* Start DMA */ |
| 129 | DMASKTRIG0 |= 0x1; |
| 130 | |
| 131 | /* Wait for transfer to complete */ |
| 132 | while((DSTAT0 & 0x000fffff)) |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 133 | yield(); |
Greg White | ebcd762 | 2007-01-06 01:26:36 +0000 | [diff] [blame] | 134 | /* Dump cache for the buffer */ |
Greg White | 5668505 | 2007-01-05 04:17:25 +0000 | [diff] [blame] | 135 | } |
Greg White | 355be50 | 2007-01-13 02:24:15 +0000 | [diff] [blame] | 136 | #endif |