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