mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 142bdbe | 2007-05-15 23:38:05 +0200 | [diff] [blame] | 2 | * Copyright (C) Procsys. All rights reserved. |
| 3 | * Author: Mushtaq Khan <mushtaq_k@procsys.com> |
| 4 | * <mushtaqk_921@yahoo.co.in> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | * MA 02111-1307 USA |
| 20 | * |
| 21 | * with the reference to ata_piix driver in kernel 2.4.32 |
| 22 | */ |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 23 | |
| 24 | /* |
Wolfgang Denk | 142bdbe | 2007-05-15 23:38:05 +0200 | [diff] [blame] | 25 | * This file contains SATA controller and SATA drive initialization functions |
| 26 | */ |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 27 | |
| 28 | #include <common.h> |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 29 | #include <asm/io.h> |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 30 | #include <pci.h> |
| 31 | #include <command.h> |
| 32 | #include <config.h> |
| 33 | #include <asm/byteorder.h> |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 34 | #include <part.h> |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 35 | #include <ide.h> |
| 36 | #include <ata.h> |
Pavel Herrmann | 9e9f628 | 2012-09-27 23:18:04 +0000 | [diff] [blame] | 37 | #include <sata.h> |
Dave Liu | a566ebd | 2008-03-26 22:50:45 +0800 | [diff] [blame] | 38 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 39 | #define DEBUG_SATA 0 /* For debug prints set DEBUG_SATA to 1 */ |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 40 | |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 41 | #define SATA_DECL |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 42 | #define DRV_DECL /* For file specific declarations */ |
Dave Liu | c230272 | 2008-03-26 22:48:18 +0800 | [diff] [blame] | 43 | #include "ata_piix.h" |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 44 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 45 | /* Macros realted to PCI */ |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 46 | #define PCI_SATA_BUS 0x00 |
| 47 | #define PCI_SATA_DEV 0x1f |
| 48 | #define PCI_SATA_FUNC 0x02 |
| 49 | |
| 50 | #define PCI_SATA_BASE1 0x10 |
| 51 | #define PCI_SATA_BASE2 0x14 |
| 52 | #define PCI_SATA_BASE3 0x18 |
| 53 | #define PCI_SATA_BASE4 0x1c |
| 54 | #define PCI_SATA_BASE5 0x20 |
| 55 | #define PCI_PMR 0x90 |
| 56 | #define PCI_PI 0x09 |
| 57 | #define PCI_PCS 0x92 |
| 58 | #define PCI_DMA_CTL 0x48 |
| 59 | |
| 60 | #define PORT_PRESENT (1<<0) |
| 61 | #define PORT_ENABLED (1<<4) |
| 62 | |
| 63 | u32 bdf; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 64 | u32 iobase1; /* Primary cmd block */ |
| 65 | u32 iobase2; /* Primary ctl block */ |
| 66 | u32 iobase3; /* Sec cmd block */ |
| 67 | u32 iobase4; /* sec ctl block */ |
| 68 | u32 iobase5; /* BMDMA*/ |
| 69 | |
| 70 | int pci_sata_init(void) |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 71 | { |
| 72 | u32 bus = PCI_SATA_BUS; |
| 73 | u32 dev = PCI_SATA_DEV; |
| 74 | u32 fun = PCI_SATA_FUNC; |
| 75 | u16 cmd = 0; |
| 76 | u8 lat = 0, pcibios_max_latency = 0xff; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 77 | u8 pmr; /* Port mapping reg */ |
| 78 | u8 pi; /* Prgming Interface reg */ |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 79 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 80 | bdf = PCI_BDF(bus, dev, fun); |
| 81 | pci_read_config_dword(bdf, PCI_SATA_BASE1, &iobase1); |
| 82 | pci_read_config_dword(bdf, PCI_SATA_BASE2, &iobase2); |
| 83 | pci_read_config_dword(bdf, PCI_SATA_BASE3, &iobase3); |
| 84 | pci_read_config_dword(bdf, PCI_SATA_BASE4, &iobase4); |
| 85 | pci_read_config_dword(bdf, PCI_SATA_BASE5, &iobase5); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 86 | |
| 87 | if ((iobase1 == 0xFFFFFFFF) || (iobase2 == 0xFFFFFFFF) || |
| 88 | (iobase3 == 0xFFFFFFFF) || (iobase4 == 0xFFFFFFFF) || |
| 89 | (iobase5 == 0xFFFFFFFF)) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 90 | /* ERROR */ |
| 91 | printf("error no base addr for SATA controller\n"); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 92 | return 1; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 93 | } |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 94 | |
| 95 | iobase1 &= 0xFFFFFFFE; |
| 96 | iobase2 &= 0xFFFFFFFE; |
| 97 | iobase3 &= 0xFFFFFFFE; |
| 98 | iobase4 &= 0xFFFFFFFE; |
| 99 | iobase5 &= 0xFFFFFFFE; |
| 100 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 101 | /* check for mode */ |
| 102 | pci_read_config_byte(bdf, PCI_PMR, &pmr); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 103 | if (pmr > 1) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 104 | puts("combined mode not supported\n"); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 105 | return 1; |
| 106 | } |
| 107 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 108 | pci_read_config_byte(bdf, PCI_PI, &pi); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 109 | if ((pi & 0x05) != 0x05) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 110 | puts("Sata is in Legacy mode\n"); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 111 | return 1; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 112 | } else |
| 113 | puts("sata is in Native mode\n"); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 114 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 115 | /* MASTER CFG AND IO CFG */ |
| 116 | pci_read_config_word(bdf, PCI_COMMAND, &cmd); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 117 | cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 118 | pci_write_config_word(bdf, PCI_COMMAND, cmd); |
| 119 | pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 120 | |
| 121 | if (lat < 16) |
| 122 | lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency; |
| 123 | else if (lat > pcibios_max_latency) |
| 124 | lat = pcibios_max_latency; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 125 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 130 | int sata_bus_probe(int port_no) |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 131 | { |
| 132 | int orig_mask, mask; |
| 133 | u16 pcs; |
| 134 | |
| 135 | mask = (PORT_PRESENT << port_no); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 136 | pci_read_config_word(bdf, PCI_PCS, &pcs); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 137 | orig_mask = (int) pcs & 0xff; |
| 138 | if ((orig_mask & mask) != mask) |
| 139 | return 0; |
| 140 | else |
| 141 | return 1; |
| 142 | } |
| 143 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 144 | int init_sata(int dev) |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 145 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 146 | static int done; |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 147 | u8 i, rv = 0; |
| 148 | |
Dave Liu | a566ebd | 2008-03-26 22:50:45 +0800 | [diff] [blame] | 149 | if (!done) |
| 150 | done = 1; |
| 151 | else |
| 152 | return 0; |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 153 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 154 | rv = pci_sata_init(); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 155 | if (rv == 1) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 156 | puts("pci initialization failed\n"); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | port[0].port_no = 0; |
| 161 | port[0].ioaddr.cmd_addr = iobase1; |
| 162 | port[0].ioaddr.altstatus_addr = port[0].ioaddr.ctl_addr = |
| 163 | iobase2 | ATA_PCI_CTL_OFS; |
| 164 | port[0].ioaddr.bmdma_addr = iobase5; |
| 165 | |
| 166 | port[1].port_no = 1; |
| 167 | port[1].ioaddr.cmd_addr = iobase3; |
| 168 | port[1].ioaddr.altstatus_addr = port[1].ioaddr.ctl_addr = |
| 169 | iobase4 | ATA_PCI_CTL_OFS; |
| 170 | port[1].ioaddr.bmdma_addr = iobase5 + 0x8; |
| 171 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 172 | for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 173 | sata_port(&port[i].ioaddr); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 174 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 175 | for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 176 | if (!(sata_bus_probe(i))) { |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 177 | port[i].port_state = 0; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 178 | printf("SATA#%d port is not present\n", i); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 179 | } else { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 180 | printf("SATA#%d port is present\n", i); |
| 181 | if (sata_bus_softreset(i)) |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 182 | port[i].port_state = 0; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 183 | else |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 184 | port[i].port_state = 1; |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 188 | for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) { |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 189 | u8 j, devno; |
| 190 | |
| 191 | if (port[i].port_state == 0) |
| 192 | continue; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 193 | for (j = 0; j < CONFIG_SYS_SATA_DEVS_PER_BUS; j++) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 194 | sata_identify(i, j); |
| 195 | set_Feature_cmd(i, j); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 196 | devno = i * CONFIG_SYS_SATA_DEVS_PER_BUS + j; |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 197 | if ((sata_dev_desc[devno].lba > 0) && |
| 198 | (sata_dev_desc[devno].blksz > 0)) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 199 | dev_print(&sata_dev_desc[devno]); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 200 | /* initialize partition type */ |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 201 | init_part(&sata_dev_desc[devno]); |
mushtaq khan | 5651b34 | 2007-04-20 14:23:02 +0530 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | return 0; |
| 206 | } |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 207 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 208 | static inline u8 sata_inb(unsigned long ioaddr) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 209 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 210 | return inb(ioaddr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 211 | } |
| 212 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 213 | static inline void sata_outb(unsigned char val, unsigned long ioaddr) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 214 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 215 | outb(val, ioaddr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 216 | } |
| 217 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 218 | static void output_data(struct sata_ioports *ioaddr, ulong * sect_buf, |
| 219 | int words) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 220 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 221 | outsw(ioaddr->data_addr, sect_buf, words << 1); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 224 | static int input_data(struct sata_ioports *ioaddr, ulong * sect_buf, int words) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 225 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 226 | insw(ioaddr->data_addr, sect_buf, words << 1); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 230 | static void sata_cpy(unsigned char *dst, unsigned char *src, unsigned int len) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 231 | { |
| 232 | unsigned char *end, *last; |
| 233 | |
| 234 | last = dst; |
| 235 | end = src + len - 1; |
| 236 | |
| 237 | /* reserve space for '\0' */ |
| 238 | if (len < 2) |
| 239 | goto OUT; |
| 240 | |
| 241 | /* skip leading white space */ |
| 242 | while ((*src) && (src < end) && (*src == ' ')) |
| 243 | ++src; |
| 244 | |
| 245 | /* copy string, omitting trailing white space */ |
| 246 | while ((*src) && (src < end)) { |
| 247 | *dst++ = *src; |
| 248 | if (*src++ != ' ') |
| 249 | last = dst; |
| 250 | } |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 251 | OUT: |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 252 | *last = '\0'; |
| 253 | } |
| 254 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 255 | int sata_bus_softreset(int num) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 256 | { |
| 257 | u8 dev = 0, status = 0, i; |
| 258 | |
| 259 | port[num].dev_mask = 0; |
| 260 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 261 | for (i = 0; i < CONFIG_SYS_SATA_DEVS_PER_BUS; i++) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 262 | if (!(sata_devchk(&port[num].ioaddr, i))) { |
| 263 | debug("dev_chk failed for dev#%d\n", i); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 264 | } else { |
| 265 | port[num].dev_mask |= (1 << i); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 266 | debug("dev_chk passed for dev#%d\n", i); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | if (!(port[num].dev_mask)) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 271 | printf("no devices on port%d\n", num); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 272 | return 1; |
| 273 | } |
| 274 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 275 | dev_select(&port[num].ioaddr, dev); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 276 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 277 | port[num].ctl_reg = 0x08; /* Default value of control reg */ |
| 278 | sata_outb(port[num].ctl_reg, port[num].ioaddr.ctl_addr); |
| 279 | udelay(10); |
| 280 | sata_outb(port[num].ctl_reg | ATA_SRST, port[num].ioaddr.ctl_addr); |
| 281 | udelay(10); |
| 282 | sata_outb(port[num].ctl_reg, port[num].ioaddr.ctl_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 283 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 284 | /* |
| 285 | * spec mandates ">= 2ms" before checking status. |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 286 | * We wait 150ms, because that was the magic delay used for |
| 287 | * ATAPI devices in Hale Landis's ATADRVR, for the period of time |
| 288 | * between when the ATA command register is written, and then |
| 289 | * status is checked. Because waiting for "a while" before |
| 290 | * checking status is fine, post SRST, we perform this magic |
| 291 | * delay here as well. |
| 292 | */ |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 293 | mdelay(150); |
| 294 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 300); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 295 | while ((status & ATA_BUSY)) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 296 | mdelay(100); |
| 297 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 3); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (status & ATA_BUSY) |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 301 | printf("ata%u is slow to respond,plz be patient\n", num); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 302 | |
| 303 | while ((status & ATA_BUSY)) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 304 | mdelay(100); |
| 305 | status = sata_chk_status(&port[num].ioaddr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (status & ATA_BUSY) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 309 | printf("ata%u failed to respond : bus reset failed\n", num); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 310 | return 1; |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 315 | void sata_identify(int num, int dev) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 316 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 317 | u8 cmd = 0, status = 0; |
| 318 | u8 devno = num * CONFIG_SYS_SATA_DEVS_PER_BUS + dev; |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 319 | u16 iobuf[ATA_SECT_SIZE]; |
| 320 | u64 n_sectors = 0; |
| 321 | u8 mask = 0; |
| 322 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 323 | memset(iobuf, 0, sizeof(iobuf)); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 324 | hd_driveid_t *iop = (hd_driveid_t *) iobuf; |
| 325 | |
| 326 | if (dev == 0) |
| 327 | mask = 0x01; |
| 328 | else |
| 329 | mask = 0x02; |
| 330 | |
| 331 | if (!(port[num].dev_mask & mask)) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 332 | printf("dev%d is not present on port#%d\n", dev, num); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 336 | printf("port=%d dev=%d\n", num, dev); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 337 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 338 | dev_select(&port[num].ioaddr, dev); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 339 | |
| 340 | status = 0; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 341 | cmd = ATA_CMD_IDENT; /* Device Identify Command */ |
| 342 | sata_outb(cmd, port[num].ioaddr.command_addr); |
| 343 | sata_inb(port[num].ioaddr.altstatus_addr); |
| 344 | udelay(10); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 345 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 346 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 1000); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 347 | if (status & ATA_ERR) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 348 | puts("\ndevice not responding\n"); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 349 | port[num].dev_mask &= ~mask; |
| 350 | return; |
| 351 | } |
| 352 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 353 | input_data(&port[num].ioaddr, (ulong *) iobuf, ATA_SECTORWORDS); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 354 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 355 | debug("\nata%u: dev %u cfg 49:%04x 82:%04x 83:%04x 84:%04x85:%04x" |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 356 | "86:%04x" "87:%04x 88:%04x\n", num, dev, iobuf[49], |
| 357 | iobuf[82], iobuf[83], iobuf[84], iobuf[85], iobuf[86], |
| 358 | iobuf[87], iobuf[88]); |
| 359 | |
| 360 | /* we require LBA and DMA support (bits 8 & 9 of word 49) */ |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 361 | if (!ata_id_has_dma(iobuf) || !ata_id_has_lba(iobuf)) |
| 362 | debug("ata%u: no dma/lba\n", num); |
| 363 | ata_dump_id(iobuf); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 364 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 365 | if (ata_id_has_lba48(iobuf)) |
| 366 | n_sectors = ata_id_u64(iobuf, 100); |
| 367 | else |
| 368 | n_sectors = ata_id_u32(iobuf, 60); |
| 369 | debug("no. of sectors %u\n", ata_id_u64(iobuf, 100)); |
| 370 | debug("no. of sectors %u\n", ata_id_u32(iobuf, 60)); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 371 | |
| 372 | if (n_sectors == 0) { |
| 373 | port[num].dev_mask &= ~mask; |
| 374 | return; |
| 375 | } |
| 376 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 377 | sata_cpy((unsigned char *)sata_dev_desc[devno].revision, iop->fw_rev, |
| 378 | sizeof(sata_dev_desc[devno].revision)); |
| 379 | sata_cpy((unsigned char *)sata_dev_desc[devno].vendor, iop->model, |
| 380 | sizeof(sata_dev_desc[devno].vendor)); |
| 381 | sata_cpy((unsigned char *)sata_dev_desc[devno].product, iop->serial_no, |
| 382 | sizeof(sata_dev_desc[devno].product)); |
| 383 | strswab(sata_dev_desc[devno].revision); |
| 384 | strswab(sata_dev_desc[devno].vendor); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 385 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 386 | if ((iop->config & 0x0080) == 0x0080) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 387 | sata_dev_desc[devno].removable = 1; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 388 | else |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 389 | sata_dev_desc[devno].removable = 0; |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 390 | |
| 391 | sata_dev_desc[devno].lba = iop->lba_capacity; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 392 | debug("lba=0x%x", sata_dev_desc[devno].lba); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 393 | |
| 394 | #ifdef CONFIG_LBA48 |
| 395 | if (iop->command_set_2 & 0x0400) { |
| 396 | sata_dev_desc[devno].lba48 = 1; |
| 397 | lba = (unsigned long long) iop->lba48_capacity[0] | |
| 398 | ((unsigned long long) iop->lba48_capacity[1] << 16) | |
| 399 | ((unsigned long long) iop->lba48_capacity[2] << 32) | |
| 400 | ((unsigned long long) iop->lba48_capacity[3] << 48); |
| 401 | } else { |
| 402 | sata_dev_desc[devno].lba48 = 0; |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | /* assuming HD */ |
| 407 | sata_dev_desc[devno].type = DEV_TYPE_HARDDISK; |
| 408 | sata_dev_desc[devno].blksz = ATA_BLOCKSIZE; |
Egbert Eich | 2eec2ab | 2013-04-09 21:11:56 +0000 | [diff] [blame^] | 409 | sata_dev_desc[devno].log2blksz = LOG2(sata_dev_desc[devno].blksz); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 410 | sata_dev_desc[devno].lun = 0; /* just to fill something in... */ |
| 411 | } |
| 412 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 413 | void set_Feature_cmd(int num, int dev) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 414 | { |
| 415 | u8 mask = 0x00, status = 0; |
| 416 | |
| 417 | if (dev == 0) |
| 418 | mask = 0x01; |
| 419 | else |
| 420 | mask = 0x02; |
| 421 | |
| 422 | if (!(port[num].dev_mask & mask)) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 423 | debug("dev%d is not present on port#%d\n", dev, num); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 427 | dev_select(&port[num].ioaddr, dev); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 428 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 429 | sata_outb(SETFEATURES_XFER, port[num].ioaddr.feature_addr); |
| 430 | sata_outb(XFER_PIO_4, port[num].ioaddr.nsect_addr); |
| 431 | sata_outb(0, port[num].ioaddr.lbal_addr); |
| 432 | sata_outb(0, port[num].ioaddr.lbam_addr); |
| 433 | sata_outb(0, port[num].ioaddr.lbah_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 434 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 435 | sata_outb(ATA_DEVICE_OBS, port[num].ioaddr.device_addr); |
| 436 | sata_outb(ATA_CMD_SETF, port[num].ioaddr.command_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 437 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 438 | udelay(50); |
| 439 | mdelay(150); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 440 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 441 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 5000); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 442 | if ((status & (ATA_STAT_BUSY | ATA_STAT_ERR))) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 443 | printf("Error : status 0x%02x\n", status); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 444 | port[num].dev_mask &= ~mask; |
| 445 | } |
| 446 | } |
| 447 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 448 | void sata_port(struct sata_ioports *ioport) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 449 | { |
| 450 | ioport->data_addr = ioport->cmd_addr + ATA_REG_DATA; |
| 451 | ioport->error_addr = ioport->cmd_addr + ATA_REG_ERR; |
| 452 | ioport->feature_addr = ioport->cmd_addr + ATA_REG_FEATURE; |
| 453 | ioport->nsect_addr = ioport->cmd_addr + ATA_REG_NSECT; |
| 454 | ioport->lbal_addr = ioport->cmd_addr + ATA_REG_LBAL; |
| 455 | ioport->lbam_addr = ioport->cmd_addr + ATA_REG_LBAM; |
| 456 | ioport->lbah_addr = ioport->cmd_addr + ATA_REG_LBAH; |
| 457 | ioport->device_addr = ioport->cmd_addr + ATA_REG_DEVICE; |
| 458 | ioport->status_addr = ioport->cmd_addr + ATA_REG_STATUS; |
| 459 | ioport->command_addr = ioport->cmd_addr + ATA_REG_CMD; |
| 460 | } |
| 461 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 462 | int sata_devchk(struct sata_ioports *ioaddr, int dev) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 463 | { |
| 464 | u8 nsect, lbal; |
| 465 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 466 | dev_select(ioaddr, dev); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 467 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 468 | sata_outb(0x55, ioaddr->nsect_addr); |
| 469 | sata_outb(0xaa, ioaddr->lbal_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 470 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 471 | sata_outb(0xaa, ioaddr->nsect_addr); |
| 472 | sata_outb(0x55, ioaddr->lbal_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 473 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 474 | sata_outb(0x55, ioaddr->nsect_addr); |
| 475 | sata_outb(0xaa, ioaddr->lbal_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 476 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 477 | nsect = sata_inb(ioaddr->nsect_addr); |
| 478 | lbal = sata_inb(ioaddr->lbal_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 479 | |
| 480 | if ((nsect == 0x55) && (lbal == 0xaa)) |
| 481 | return 1; /* we found a device */ |
| 482 | else |
| 483 | return 0; /* nothing found */ |
| 484 | } |
| 485 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 486 | void dev_select(struct sata_ioports *ioaddr, int dev) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 487 | { |
| 488 | u8 tmp = 0; |
| 489 | |
| 490 | if (dev == 0) |
| 491 | tmp = ATA_DEVICE_OBS; |
| 492 | else |
| 493 | tmp = ATA_DEVICE_OBS | ATA_DEV1; |
| 494 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 495 | sata_outb(tmp, ioaddr->device_addr); |
| 496 | sata_inb(ioaddr->altstatus_addr); |
| 497 | udelay(5); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 498 | } |
| 499 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 500 | u8 sata_busy_wait(struct sata_ioports *ioaddr, int bits, unsigned int max) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 501 | { |
| 502 | u8 status; |
| 503 | |
| 504 | do { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 505 | udelay(1000); |
| 506 | status = sata_chk_status(ioaddr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 507 | max--; |
| 508 | } while ((status & bits) && (max > 0)); |
| 509 | |
| 510 | return status; |
| 511 | } |
| 512 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 513 | u8 sata_chk_status(struct sata_ioports *ioaddr) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 514 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 515 | return sata_inb(ioaddr->status_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 516 | } |
| 517 | |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 518 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 519 | ulong sata_read(int device, ulong blknr, lbaint_t blkcnt, void *buff) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 520 | { |
| 521 | ulong n = 0, *buffer = (ulong *)buff; |
| 522 | u8 dev = 0, num = 0, mask = 0, status = 0; |
| 523 | |
| 524 | #ifdef CONFIG_LBA48 |
| 525 | unsigned char lba48 = 0; |
| 526 | |
| 527 | if (blknr & 0x0000fffff0000000) { |
| 528 | if (!sata_dev_desc[devno].lba48) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 529 | printf("Drive doesn't support 48-bit addressing\n"); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | /* more than 28 bits used, use 48bit mode */ |
| 533 | lba48 = 1; |
| 534 | } |
| 535 | #endif |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 536 | /* Port Number */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 537 | num = device / CONFIG_SYS_SATA_DEVS_PER_BUS; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 538 | /* dev on the port */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 539 | if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS) |
| 540 | dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS; |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 541 | else |
| 542 | dev = device; |
| 543 | |
| 544 | if (dev == 0) |
| 545 | mask = 0x01; |
| 546 | else |
| 547 | mask = 0x02; |
| 548 | |
| 549 | if (!(port[num].dev_mask & mask)) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 550 | printf("dev%d is not present on port#%d\n", dev, num); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | /* Select device */ |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 555 | dev_select(&port[num].ioaddr, dev); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 556 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 557 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 558 | if (status & ATA_BUSY) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 559 | printf("ata%u failed to respond\n", port[num].port_no); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 560 | return n; |
| 561 | } |
| 562 | while (blkcnt-- > 0) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 563 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 564 | if (status & ATA_BUSY) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 565 | printf("ata%u failed to respond\n", 0); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 566 | return n; |
| 567 | } |
| 568 | #ifdef CONFIG_LBA48 |
| 569 | if (lba48) { |
| 570 | /* write high bits */ |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 571 | sata_outb(0, port[num].ioaddr.nsect_addr); |
| 572 | sata_outb((blknr >> 24) & 0xFF, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 573 | port[num].ioaddr.lbal_addr); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 574 | sata_outb((blknr >> 32) & 0xFF, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 575 | port[num].ioaddr.lbam_addr); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 576 | sata_outb((blknr >> 40) & 0xFF, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 577 | port[num].ioaddr.lbah_addr); |
| 578 | } |
| 579 | #endif |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 580 | sata_outb(1, port[num].ioaddr.nsect_addr); |
| 581 | sata_outb(((blknr) >> 0) & 0xFF, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 582 | port[num].ioaddr.lbal_addr); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 583 | sata_outb((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr); |
| 584 | sata_outb((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 585 | |
| 586 | #ifdef CONFIG_LBA48 |
| 587 | if (lba48) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 588 | sata_outb(ATA_LBA, port[num].ioaddr.device_addr); |
| 589 | sata_outb(ATA_CMD_READ_EXT, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 590 | port[num].ioaddr.command_addr); |
| 591 | } else |
| 592 | #endif |
| 593 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 594 | sata_outb(ATA_LBA | ((blknr >> 24) & 0xF), |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 595 | port[num].ioaddr.device_addr); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 596 | sata_outb(ATA_CMD_READ, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 597 | port[num].ioaddr.command_addr); |
| 598 | } |
| 599 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 600 | mdelay(50); |
| 601 | /* may take up to 4 sec */ |
| 602 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 4000); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 603 | |
| 604 | if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) |
| 605 | != ATA_STAT_DRQ) { |
| 606 | u8 err = 0; |
| 607 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 608 | printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n", |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 609 | device, (ulong) blknr, status); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 610 | err = sata_inb(port[num].ioaddr.error_addr); |
| 611 | printf("Error reg = 0x%x\n", err); |
| 612 | return n; |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 613 | } |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 614 | input_data(&port[num].ioaddr, buffer, ATA_SECTORWORDS); |
| 615 | sata_inb(port[num].ioaddr.altstatus_addr); |
| 616 | udelay(50); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 617 | |
| 618 | ++n; |
| 619 | ++blknr; |
| 620 | buffer += ATA_SECTORWORDS; |
| 621 | } |
| 622 | return n; |
| 623 | } |
| 624 | |
Tom Rini | d4a6a5b | 2012-09-29 07:52:48 -0700 | [diff] [blame] | 625 | ulong sata_write(int device, ulong blknr, lbaint_t blkcnt, const void *buff) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 626 | { |
| 627 | ulong n = 0, *buffer = (ulong *)buff; |
| 628 | unsigned char status = 0, num = 0, dev = 0, mask = 0; |
| 629 | |
| 630 | #ifdef CONFIG_LBA48 |
| 631 | unsigned char lba48 = 0; |
| 632 | |
| 633 | if (blknr & 0x0000fffff0000000) { |
| 634 | if (!sata_dev_desc[devno].lba48) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 635 | printf("Drive doesn't support 48-bit addressing\n"); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | /* more than 28 bits used, use 48bit mode */ |
| 639 | lba48 = 1; |
| 640 | } |
| 641 | #endif |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 642 | /* Port Number */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 643 | num = device / CONFIG_SYS_SATA_DEVS_PER_BUS; |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 644 | /* dev on the Port */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 645 | if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS) |
| 646 | dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS; |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 647 | else |
| 648 | dev = device; |
| 649 | |
| 650 | if (dev == 0) |
| 651 | mask = 0x01; |
| 652 | else |
| 653 | mask = 0x02; |
| 654 | |
| 655 | /* Select device */ |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 656 | dev_select(&port[num].ioaddr, dev); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 657 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 658 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 659 | if (status & ATA_BUSY) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 660 | printf("ata%u failed to respond\n", port[num].port_no); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 661 | return n; |
| 662 | } |
| 663 | |
| 664 | while (blkcnt-- > 0) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 665 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 666 | if (status & ATA_BUSY) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 667 | printf("ata%u failed to respond\n", |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 668 | port[num].port_no); |
| 669 | return n; |
| 670 | } |
| 671 | #ifdef CONFIG_LBA48 |
| 672 | if (lba48) { |
| 673 | /* write high bits */ |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 674 | sata_outb(0, port[num].ioaddr.nsect_addr); |
| 675 | sata_outb((blknr >> 24) & 0xFF, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 676 | port[num].ioaddr.lbal_addr); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 677 | sata_outb((blknr >> 32) & 0xFF, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 678 | port[num].ioaddr.lbam_addr); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 679 | sata_outb((blknr >> 40) & 0xFF, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 680 | port[num].ioaddr.lbah_addr); |
| 681 | } |
| 682 | #endif |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 683 | sata_outb(1, port[num].ioaddr.nsect_addr); |
| 684 | sata_outb((blknr >> 0) & 0xFF, port[num].ioaddr.lbal_addr); |
| 685 | sata_outb((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr); |
| 686 | sata_outb((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 687 | #ifdef CONFIG_LBA48 |
| 688 | if (lba48) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 689 | sata_outb(ATA_LBA, port[num].ioaddr.device_addr); |
| 690 | sata_outb(ATA_CMD_WRITE_EXT, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 691 | port[num].ioaddr.command_addr); |
| 692 | } else |
| 693 | #endif |
| 694 | { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 695 | sata_outb(ATA_LBA | ((blknr >> 24) & 0xF), |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 696 | port[num].ioaddr.device_addr); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 697 | sata_outb(ATA_CMD_WRITE, |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 698 | port[num].ioaddr.command_addr); |
| 699 | } |
| 700 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 701 | mdelay(50); |
| 702 | /* may take up to 4 sec */ |
| 703 | status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 4000); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 704 | if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) |
| 705 | != ATA_STAT_DRQ) { |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 706 | printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n", |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 707 | device, (ulong) blknr, status); |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 708 | return n; |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 709 | } |
| 710 | |
Tom Rini | 953f90f | 2012-09-29 07:35:12 -0700 | [diff] [blame] | 711 | output_data(&port[num].ioaddr, buffer, ATA_SECTORWORDS); |
| 712 | sata_inb(port[num].ioaddr.altstatus_addr); |
| 713 | udelay(50); |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 714 | |
| 715 | ++n; |
| 716 | ++blknr; |
| 717 | buffer += ATA_SECTORWORDS; |
| 718 | } |
| 719 | return n; |
| 720 | } |
| 721 | |
Dave Liu | a566ebd | 2008-03-26 22:50:45 +0800 | [diff] [blame] | 722 | int scan_sata(int dev) |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 723 | { |
Dave Liu | a566ebd | 2008-03-26 22:50:45 +0800 | [diff] [blame] | 724 | return 0; |
Dave Liu | 8b3eaa1 | 2008-03-26 22:47:06 +0800 | [diff] [blame] | 725 | } |