blob: 1e33a66c45078acc66640346d17323babd2e2579 [file] [log] [blame]
mushtaq khan5651b342007-04-20 14:23:02 +05301/*
Wolfgang Denk142bdbe2007-05-15 23:38:05 +02002 * 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 khan5651b342007-04-20 14:23:02 +053023
24/*
Wolfgang Denk142bdbe2007-05-15 23:38:05 +020025 * This file contains SATA controller and SATA drive initialization functions
26 */
mushtaq khan5651b342007-04-20 14:23:02 +053027
28#include <common.h>
Dave Liu8b3eaa12008-03-26 22:47:06 +080029#include <asm/io.h>
mushtaq khan5651b342007-04-20 14:23:02 +053030#include <pci.h>
31#include <command.h>
32#include <config.h>
33#include <asm/byteorder.h>
Dave Liu8b3eaa12008-03-26 22:47:06 +080034#include <part.h>
mushtaq khan5651b342007-04-20 14:23:02 +053035#include <ide.h>
36#include <ata.h>
Pavel Herrmann9e9f6282012-09-27 23:18:04 +000037#include <sata.h>
Dave Liua566ebd2008-03-26 22:50:45 +080038
Tom Rini953f90f2012-09-29 07:35:12 -070039#define DEBUG_SATA 0 /* For debug prints set DEBUG_SATA to 1 */
mushtaq khan5651b342007-04-20 14:23:02 +053040
Dave Liu8b3eaa12008-03-26 22:47:06 +080041#define SATA_DECL
Tom Rini953f90f2012-09-29 07:35:12 -070042#define DRV_DECL /* For file specific declarations */
Dave Liuc2302722008-03-26 22:48:18 +080043#include "ata_piix.h"
mushtaq khan5651b342007-04-20 14:23:02 +053044
Tom Rini953f90f2012-09-29 07:35:12 -070045/* Macros realted to PCI */
mushtaq khan5651b342007-04-20 14:23:02 +053046#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
63u32 bdf;
Tom Rini953f90f2012-09-29 07:35:12 -070064u32 iobase1; /* Primary cmd block */
65u32 iobase2; /* Primary ctl block */
66u32 iobase3; /* Sec cmd block */
67u32 iobase4; /* sec ctl block */
68u32 iobase5; /* BMDMA*/
69
70int pci_sata_init(void)
mushtaq khan5651b342007-04-20 14:23:02 +053071{
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 Rini953f90f2012-09-29 07:35:12 -070077 u8 pmr; /* Port mapping reg */
78 u8 pi; /* Prgming Interface reg */
mushtaq khan5651b342007-04-20 14:23:02 +053079
Tom Rini953f90f2012-09-29 07:35:12 -070080 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 khan5651b342007-04-20 14:23:02 +053086
87 if ((iobase1 == 0xFFFFFFFF) || (iobase2 == 0xFFFFFFFF) ||
88 (iobase3 == 0xFFFFFFFF) || (iobase4 == 0xFFFFFFFF) ||
89 (iobase5 == 0xFFFFFFFF)) {
Tom Rini953f90f2012-09-29 07:35:12 -070090 /* ERROR */
91 printf("error no base addr for SATA controller\n");
mushtaq khan5651b342007-04-20 14:23:02 +053092 return 1;
Tom Rini953f90f2012-09-29 07:35:12 -070093 }
mushtaq khan5651b342007-04-20 14:23:02 +053094
95 iobase1 &= 0xFFFFFFFE;
96 iobase2 &= 0xFFFFFFFE;
97 iobase3 &= 0xFFFFFFFE;
98 iobase4 &= 0xFFFFFFFE;
99 iobase5 &= 0xFFFFFFFE;
100
Tom Rini953f90f2012-09-29 07:35:12 -0700101 /* check for mode */
102 pci_read_config_byte(bdf, PCI_PMR, &pmr);
mushtaq khan5651b342007-04-20 14:23:02 +0530103 if (pmr > 1) {
Tom Rini953f90f2012-09-29 07:35:12 -0700104 puts("combined mode not supported\n");
mushtaq khan5651b342007-04-20 14:23:02 +0530105 return 1;
106 }
107
Tom Rini953f90f2012-09-29 07:35:12 -0700108 pci_read_config_byte(bdf, PCI_PI, &pi);
mushtaq khan5651b342007-04-20 14:23:02 +0530109 if ((pi & 0x05) != 0x05) {
Tom Rini953f90f2012-09-29 07:35:12 -0700110 puts("Sata is in Legacy mode\n");
mushtaq khan5651b342007-04-20 14:23:02 +0530111 return 1;
Tom Rini953f90f2012-09-29 07:35:12 -0700112 } else
113 puts("sata is in Native mode\n");
mushtaq khan5651b342007-04-20 14:23:02 +0530114
Tom Rini953f90f2012-09-29 07:35:12 -0700115 /* MASTER CFG AND IO CFG */
116 pci_read_config_word(bdf, PCI_COMMAND, &cmd);
mushtaq khan5651b342007-04-20 14:23:02 +0530117 cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO;
Tom Rini953f90f2012-09-29 07:35:12 -0700118 pci_write_config_word(bdf, PCI_COMMAND, cmd);
119 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
mushtaq khan5651b342007-04-20 14:23:02 +0530120
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 Rini953f90f2012-09-29 07:35:12 -0700125 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
mushtaq khan5651b342007-04-20 14:23:02 +0530126
127 return 0;
128}
129
Tom Rini953f90f2012-09-29 07:35:12 -0700130int sata_bus_probe(int port_no)
mushtaq khan5651b342007-04-20 14:23:02 +0530131{
132 int orig_mask, mask;
133 u16 pcs;
134
135 mask = (PORT_PRESENT << port_no);
Tom Rini953f90f2012-09-29 07:35:12 -0700136 pci_read_config_word(bdf, PCI_PCS, &pcs);
mushtaq khan5651b342007-04-20 14:23:02 +0530137 orig_mask = (int) pcs & 0xff;
138 if ((orig_mask & mask) != mask)
139 return 0;
140 else
141 return 1;
142}
143
Tom Rini953f90f2012-09-29 07:35:12 -0700144int init_sata(int dev)
mushtaq khan5651b342007-04-20 14:23:02 +0530145{
Tom Rini953f90f2012-09-29 07:35:12 -0700146 static int done;
mushtaq khan5651b342007-04-20 14:23:02 +0530147 u8 i, rv = 0;
148
Dave Liua566ebd2008-03-26 22:50:45 +0800149 if (!done)
150 done = 1;
151 else
152 return 0;
mushtaq khan5651b342007-04-20 14:23:02 +0530153
Tom Rini953f90f2012-09-29 07:35:12 -0700154 rv = pci_sata_init();
mushtaq khan5651b342007-04-20 14:23:02 +0530155 if (rv == 1) {
Tom Rini953f90f2012-09-29 07:35:12 -0700156 puts("pci initialization failed\n");
mushtaq khan5651b342007-04-20 14:23:02 +0530157 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-VILLARD03836942008-10-16 15:01:15 +0200172 for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++)
Tom Rini953f90f2012-09-29 07:35:12 -0700173 sata_port(&port[i].ioaddr);
mushtaq khan5651b342007-04-20 14:23:02 +0530174
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200175 for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) {
Tom Rini953f90f2012-09-29 07:35:12 -0700176 if (!(sata_bus_probe(i))) {
mushtaq khan5651b342007-04-20 14:23:02 +0530177 port[i].port_state = 0;
Tom Rini953f90f2012-09-29 07:35:12 -0700178 printf("SATA#%d port is not present\n", i);
mushtaq khan5651b342007-04-20 14:23:02 +0530179 } else {
Tom Rini953f90f2012-09-29 07:35:12 -0700180 printf("SATA#%d port is present\n", i);
181 if (sata_bus_softreset(i))
mushtaq khan5651b342007-04-20 14:23:02 +0530182 port[i].port_state = 0;
Tom Rini953f90f2012-09-29 07:35:12 -0700183 else
mushtaq khan5651b342007-04-20 14:23:02 +0530184 port[i].port_state = 1;
mushtaq khan5651b342007-04-20 14:23:02 +0530185 }
186 }
187
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200188 for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) {
mushtaq khan5651b342007-04-20 14:23:02 +0530189 u8 j, devno;
190
191 if (port[i].port_state == 0)
192 continue;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200193 for (j = 0; j < CONFIG_SYS_SATA_DEVS_PER_BUS; j++) {
Tom Rini953f90f2012-09-29 07:35:12 -0700194 sata_identify(i, j);
195 set_Feature_cmd(i, j);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200196 devno = i * CONFIG_SYS_SATA_DEVS_PER_BUS + j;
mushtaq khan5651b342007-04-20 14:23:02 +0530197 if ((sata_dev_desc[devno].lba > 0) &&
198 (sata_dev_desc[devno].blksz > 0)) {
Tom Rini953f90f2012-09-29 07:35:12 -0700199 dev_print(&sata_dev_desc[devno]);
mushtaq khan5651b342007-04-20 14:23:02 +0530200 /* initialize partition type */
Tom Rini953f90f2012-09-29 07:35:12 -0700201 init_part(&sata_dev_desc[devno]);
mushtaq khan5651b342007-04-20 14:23:02 +0530202 }
203 }
204 }
205 return 0;
206}
Dave Liu8b3eaa12008-03-26 22:47:06 +0800207
Tom Rini953f90f2012-09-29 07:35:12 -0700208static inline u8 sata_inb(unsigned long ioaddr)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800209{
Tom Rini953f90f2012-09-29 07:35:12 -0700210 return inb(ioaddr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800211}
212
Tom Rini953f90f2012-09-29 07:35:12 -0700213static inline void sata_outb(unsigned char val, unsigned long ioaddr)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800214{
Tom Rini953f90f2012-09-29 07:35:12 -0700215 outb(val, ioaddr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800216}
217
Tom Rini953f90f2012-09-29 07:35:12 -0700218static void output_data(struct sata_ioports *ioaddr, ulong * sect_buf,
219 int words)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800220{
Tom Rini953f90f2012-09-29 07:35:12 -0700221 outsw(ioaddr->data_addr, sect_buf, words << 1);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800222}
223
Tom Rini953f90f2012-09-29 07:35:12 -0700224static int input_data(struct sata_ioports *ioaddr, ulong * sect_buf, int words)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800225{
Tom Rini953f90f2012-09-29 07:35:12 -0700226 insw(ioaddr->data_addr, sect_buf, words << 1);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800227 return 0;
228}
229
Tom Rini953f90f2012-09-29 07:35:12 -0700230static void sata_cpy(unsigned char *dst, unsigned char *src, unsigned int len)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800231{
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 Rini953f90f2012-09-29 07:35:12 -0700251OUT:
Dave Liu8b3eaa12008-03-26 22:47:06 +0800252 *last = '\0';
253}
254
Tom Rini953f90f2012-09-29 07:35:12 -0700255int sata_bus_softreset(int num)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800256{
257 u8 dev = 0, status = 0, i;
258
259 port[num].dev_mask = 0;
260
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200261 for (i = 0; i < CONFIG_SYS_SATA_DEVS_PER_BUS; i++) {
Tom Rini953f90f2012-09-29 07:35:12 -0700262 if (!(sata_devchk(&port[num].ioaddr, i))) {
263 debug("dev_chk failed for dev#%d\n", i);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800264 } else {
265 port[num].dev_mask |= (1 << i);
Tom Rini953f90f2012-09-29 07:35:12 -0700266 debug("dev_chk passed for dev#%d\n", i);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800267 }
268 }
269
270 if (!(port[num].dev_mask)) {
Tom Rini953f90f2012-09-29 07:35:12 -0700271 printf("no devices on port%d\n", num);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800272 return 1;
273 }
274
Tom Rini953f90f2012-09-29 07:35:12 -0700275 dev_select(&port[num].ioaddr, dev);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800276
Tom Rini953f90f2012-09-29 07:35:12 -0700277 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 Liu8b3eaa12008-03-26 22:47:06 +0800283
Tom Rini953f90f2012-09-29 07:35:12 -0700284 /*
285 * spec mandates ">= 2ms" before checking status.
Dave Liu8b3eaa12008-03-26 22:47:06 +0800286 * 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 Rini953f90f2012-09-29 07:35:12 -0700293 mdelay(150);
294 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 300);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800295 while ((status & ATA_BUSY)) {
Tom Rini953f90f2012-09-29 07:35:12 -0700296 mdelay(100);
297 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 3);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800298 }
299
300 if (status & ATA_BUSY)
Tom Rini953f90f2012-09-29 07:35:12 -0700301 printf("ata%u is slow to respond,plz be patient\n", num);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800302
303 while ((status & ATA_BUSY)) {
Tom Rini953f90f2012-09-29 07:35:12 -0700304 mdelay(100);
305 status = sata_chk_status(&port[num].ioaddr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800306 }
307
308 if (status & ATA_BUSY) {
Tom Rini953f90f2012-09-29 07:35:12 -0700309 printf("ata%u failed to respond : bus reset failed\n", num);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800310 return 1;
311 }
312 return 0;
313}
314
Tom Rini953f90f2012-09-29 07:35:12 -0700315void sata_identify(int num, int dev)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800316{
Tom Rini953f90f2012-09-29 07:35:12 -0700317 u8 cmd = 0, status = 0;
318 u8 devno = num * CONFIG_SYS_SATA_DEVS_PER_BUS + dev;
Dave Liu8b3eaa12008-03-26 22:47:06 +0800319 u16 iobuf[ATA_SECT_SIZE];
320 u64 n_sectors = 0;
321 u8 mask = 0;
322
Tom Rini953f90f2012-09-29 07:35:12 -0700323 memset(iobuf, 0, sizeof(iobuf));
Dave Liu8b3eaa12008-03-26 22:47:06 +0800324 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 Rini953f90f2012-09-29 07:35:12 -0700332 printf("dev%d is not present on port#%d\n", dev, num);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800333 return;
334 }
335
Tom Rini953f90f2012-09-29 07:35:12 -0700336 printf("port=%d dev=%d\n", num, dev);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800337
Tom Rini953f90f2012-09-29 07:35:12 -0700338 dev_select(&port[num].ioaddr, dev);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800339
340 status = 0;
Tom Rini953f90f2012-09-29 07:35:12 -0700341 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 Liu8b3eaa12008-03-26 22:47:06 +0800345
Tom Rini953f90f2012-09-29 07:35:12 -0700346 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 1000);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800347 if (status & ATA_ERR) {
Tom Rini953f90f2012-09-29 07:35:12 -0700348 puts("\ndevice not responding\n");
Dave Liu8b3eaa12008-03-26 22:47:06 +0800349 port[num].dev_mask &= ~mask;
350 return;
351 }
352
Tom Rini953f90f2012-09-29 07:35:12 -0700353 input_data(&port[num].ioaddr, (ulong *) iobuf, ATA_SECTORWORDS);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800354
Tom Rini953f90f2012-09-29 07:35:12 -0700355 debug("\nata%u: dev %u cfg 49:%04x 82:%04x 83:%04x 84:%04x85:%04x"
Dave Liu8b3eaa12008-03-26 22:47:06 +0800356 "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 Rini953f90f2012-09-29 07:35:12 -0700361 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 Liu8b3eaa12008-03-26 22:47:06 +0800364
Tom Rini953f90f2012-09-29 07:35:12 -0700365 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 Liu8b3eaa12008-03-26 22:47:06 +0800371
372 if (n_sectors == 0) {
373 port[num].dev_mask &= ~mask;
374 return;
375 }
376
Tom Rini953f90f2012-09-29 07:35:12 -0700377 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 Liu8b3eaa12008-03-26 22:47:06 +0800385
Tom Rini953f90f2012-09-29 07:35:12 -0700386 if ((iop->config & 0x0080) == 0x0080)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800387 sata_dev_desc[devno].removable = 1;
Tom Rini953f90f2012-09-29 07:35:12 -0700388 else
Dave Liu8b3eaa12008-03-26 22:47:06 +0800389 sata_dev_desc[devno].removable = 0;
Dave Liu8b3eaa12008-03-26 22:47:06 +0800390
391 sata_dev_desc[devno].lba = iop->lba_capacity;
Tom Rini953f90f2012-09-29 07:35:12 -0700392 debug("lba=0x%x", sata_dev_desc[devno].lba);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800393
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;
409 sata_dev_desc[devno].lun = 0; /* just to fill something in... */
410}
411
Tom Rini953f90f2012-09-29 07:35:12 -0700412void set_Feature_cmd(int num, int dev)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800413{
414 u8 mask = 0x00, status = 0;
415
416 if (dev == 0)
417 mask = 0x01;
418 else
419 mask = 0x02;
420
421 if (!(port[num].dev_mask & mask)) {
Tom Rini953f90f2012-09-29 07:35:12 -0700422 debug("dev%d is not present on port#%d\n", dev, num);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800423 return;
424 }
425
Tom Rini953f90f2012-09-29 07:35:12 -0700426 dev_select(&port[num].ioaddr, dev);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800427
Tom Rini953f90f2012-09-29 07:35:12 -0700428 sata_outb(SETFEATURES_XFER, port[num].ioaddr.feature_addr);
429 sata_outb(XFER_PIO_4, port[num].ioaddr.nsect_addr);
430 sata_outb(0, port[num].ioaddr.lbal_addr);
431 sata_outb(0, port[num].ioaddr.lbam_addr);
432 sata_outb(0, port[num].ioaddr.lbah_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800433
Tom Rini953f90f2012-09-29 07:35:12 -0700434 sata_outb(ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
435 sata_outb(ATA_CMD_SETF, port[num].ioaddr.command_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800436
Tom Rini953f90f2012-09-29 07:35:12 -0700437 udelay(50);
438 mdelay(150);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800439
Tom Rini953f90f2012-09-29 07:35:12 -0700440 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 5000);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800441 if ((status & (ATA_STAT_BUSY | ATA_STAT_ERR))) {
Tom Rini953f90f2012-09-29 07:35:12 -0700442 printf("Error : status 0x%02x\n", status);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800443 port[num].dev_mask &= ~mask;
444 }
445}
446
Tom Rini953f90f2012-09-29 07:35:12 -0700447void sata_port(struct sata_ioports *ioport)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800448{
449 ioport->data_addr = ioport->cmd_addr + ATA_REG_DATA;
450 ioport->error_addr = ioport->cmd_addr + ATA_REG_ERR;
451 ioport->feature_addr = ioport->cmd_addr + ATA_REG_FEATURE;
452 ioport->nsect_addr = ioport->cmd_addr + ATA_REG_NSECT;
453 ioport->lbal_addr = ioport->cmd_addr + ATA_REG_LBAL;
454 ioport->lbam_addr = ioport->cmd_addr + ATA_REG_LBAM;
455 ioport->lbah_addr = ioport->cmd_addr + ATA_REG_LBAH;
456 ioport->device_addr = ioport->cmd_addr + ATA_REG_DEVICE;
457 ioport->status_addr = ioport->cmd_addr + ATA_REG_STATUS;
458 ioport->command_addr = ioport->cmd_addr + ATA_REG_CMD;
459}
460
Tom Rini953f90f2012-09-29 07:35:12 -0700461int sata_devchk(struct sata_ioports *ioaddr, int dev)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800462{
463 u8 nsect, lbal;
464
Tom Rini953f90f2012-09-29 07:35:12 -0700465 dev_select(ioaddr, dev);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800466
Tom Rini953f90f2012-09-29 07:35:12 -0700467 sata_outb(0x55, ioaddr->nsect_addr);
468 sata_outb(0xaa, ioaddr->lbal_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800469
Tom Rini953f90f2012-09-29 07:35:12 -0700470 sata_outb(0xaa, ioaddr->nsect_addr);
471 sata_outb(0x55, ioaddr->lbal_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800472
Tom Rini953f90f2012-09-29 07:35:12 -0700473 sata_outb(0x55, ioaddr->nsect_addr);
474 sata_outb(0xaa, ioaddr->lbal_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800475
Tom Rini953f90f2012-09-29 07:35:12 -0700476 nsect = sata_inb(ioaddr->nsect_addr);
477 lbal = sata_inb(ioaddr->lbal_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800478
479 if ((nsect == 0x55) && (lbal == 0xaa))
480 return 1; /* we found a device */
481 else
482 return 0; /* nothing found */
483}
484
Tom Rini953f90f2012-09-29 07:35:12 -0700485void dev_select(struct sata_ioports *ioaddr, int dev)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800486{
487 u8 tmp = 0;
488
489 if (dev == 0)
490 tmp = ATA_DEVICE_OBS;
491 else
492 tmp = ATA_DEVICE_OBS | ATA_DEV1;
493
Tom Rini953f90f2012-09-29 07:35:12 -0700494 sata_outb(tmp, ioaddr->device_addr);
495 sata_inb(ioaddr->altstatus_addr);
496 udelay(5);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800497}
498
Tom Rini953f90f2012-09-29 07:35:12 -0700499u8 sata_busy_wait(struct sata_ioports *ioaddr, int bits, unsigned int max)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800500{
501 u8 status;
502
503 do {
Tom Rini953f90f2012-09-29 07:35:12 -0700504 udelay(1000);
505 status = sata_chk_status(ioaddr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800506 max--;
507 } while ((status & bits) && (max > 0));
508
509 return status;
510}
511
Tom Rini953f90f2012-09-29 07:35:12 -0700512u8 sata_chk_status(struct sata_ioports *ioaddr)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800513{
Tom Rini953f90f2012-09-29 07:35:12 -0700514 return sata_inb(ioaddr->status_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800515}
516
Dave Liu8b3eaa12008-03-26 22:47:06 +0800517
Tom Rini953f90f2012-09-29 07:35:12 -0700518ulong sata_read(int device, ulong blknr, lbaint_t blkcnt, void *buff)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800519{
520 ulong n = 0, *buffer = (ulong *)buff;
521 u8 dev = 0, num = 0, mask = 0, status = 0;
522
523#ifdef CONFIG_LBA48
524 unsigned char lba48 = 0;
525
526 if (blknr & 0x0000fffff0000000) {
527 if (!sata_dev_desc[devno].lba48) {
Tom Rini953f90f2012-09-29 07:35:12 -0700528 printf("Drive doesn't support 48-bit addressing\n");
Dave Liu8b3eaa12008-03-26 22:47:06 +0800529 return 0;
530 }
531 /* more than 28 bits used, use 48bit mode */
532 lba48 = 1;
533 }
534#endif
Tom Rini953f90f2012-09-29 07:35:12 -0700535 /* Port Number */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200536 num = device / CONFIG_SYS_SATA_DEVS_PER_BUS;
Tom Rini953f90f2012-09-29 07:35:12 -0700537 /* dev on the port */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200538 if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS)
539 dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS;
Dave Liu8b3eaa12008-03-26 22:47:06 +0800540 else
541 dev = device;
542
543 if (dev == 0)
544 mask = 0x01;
545 else
546 mask = 0x02;
547
548 if (!(port[num].dev_mask & mask)) {
Tom Rini953f90f2012-09-29 07:35:12 -0700549 printf("dev%d is not present on port#%d\n", dev, num);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800550 return 0;
551 }
552
553 /* Select device */
Tom Rini953f90f2012-09-29 07:35:12 -0700554 dev_select(&port[num].ioaddr, dev);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800555
Tom Rini953f90f2012-09-29 07:35:12 -0700556 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800557 if (status & ATA_BUSY) {
Tom Rini953f90f2012-09-29 07:35:12 -0700558 printf("ata%u failed to respond\n", port[num].port_no);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800559 return n;
560 }
561 while (blkcnt-- > 0) {
Tom Rini953f90f2012-09-29 07:35:12 -0700562 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800563 if (status & ATA_BUSY) {
Tom Rini953f90f2012-09-29 07:35:12 -0700564 printf("ata%u failed to respond\n", 0);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800565 return n;
566 }
567#ifdef CONFIG_LBA48
568 if (lba48) {
569 /* write high bits */
Tom Rini953f90f2012-09-29 07:35:12 -0700570 sata_outb(0, port[num].ioaddr.nsect_addr);
571 sata_outb((blknr >> 24) & 0xFF,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800572 port[num].ioaddr.lbal_addr);
Tom Rini953f90f2012-09-29 07:35:12 -0700573 sata_outb((blknr >> 32) & 0xFF,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800574 port[num].ioaddr.lbam_addr);
Tom Rini953f90f2012-09-29 07:35:12 -0700575 sata_outb((blknr >> 40) & 0xFF,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800576 port[num].ioaddr.lbah_addr);
577 }
578#endif
Tom Rini953f90f2012-09-29 07:35:12 -0700579 sata_outb(1, port[num].ioaddr.nsect_addr);
580 sata_outb(((blknr) >> 0) & 0xFF,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800581 port[num].ioaddr.lbal_addr);
Tom Rini953f90f2012-09-29 07:35:12 -0700582 sata_outb((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
583 sata_outb((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800584
585#ifdef CONFIG_LBA48
586 if (lba48) {
Tom Rini953f90f2012-09-29 07:35:12 -0700587 sata_outb(ATA_LBA, port[num].ioaddr.device_addr);
588 sata_outb(ATA_CMD_READ_EXT,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800589 port[num].ioaddr.command_addr);
590 } else
591#endif
592 {
Tom Rini953f90f2012-09-29 07:35:12 -0700593 sata_outb(ATA_LBA | ((blknr >> 24) & 0xF),
Dave Liu8b3eaa12008-03-26 22:47:06 +0800594 port[num].ioaddr.device_addr);
Tom Rini953f90f2012-09-29 07:35:12 -0700595 sata_outb(ATA_CMD_READ,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800596 port[num].ioaddr.command_addr);
597 }
598
Tom Rini953f90f2012-09-29 07:35:12 -0700599 mdelay(50);
600 /* may take up to 4 sec */
601 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 4000);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800602
603 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
604 != ATA_STAT_DRQ) {
605 u8 err = 0;
606
Tom Rini953f90f2012-09-29 07:35:12 -0700607 printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
Dave Liu8b3eaa12008-03-26 22:47:06 +0800608 device, (ulong) blknr, status);
Tom Rini953f90f2012-09-29 07:35:12 -0700609 err = sata_inb(port[num].ioaddr.error_addr);
610 printf("Error reg = 0x%x\n", err);
611 return n;
Dave Liu8b3eaa12008-03-26 22:47:06 +0800612 }
Tom Rini953f90f2012-09-29 07:35:12 -0700613 input_data(&port[num].ioaddr, buffer, ATA_SECTORWORDS);
614 sata_inb(port[num].ioaddr.altstatus_addr);
615 udelay(50);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800616
617 ++n;
618 ++blknr;
619 buffer += ATA_SECTORWORDS;
620 }
621 return n;
622}
623
Tom Rinid4a6a5b2012-09-29 07:52:48 -0700624ulong sata_write(int device, ulong blknr, lbaint_t blkcnt, const void *buff)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800625{
626 ulong n = 0, *buffer = (ulong *)buff;
627 unsigned char status = 0, num = 0, dev = 0, mask = 0;
628
629#ifdef CONFIG_LBA48
630 unsigned char lba48 = 0;
631
632 if (blknr & 0x0000fffff0000000) {
633 if (!sata_dev_desc[devno].lba48) {
Tom Rini953f90f2012-09-29 07:35:12 -0700634 printf("Drive doesn't support 48-bit addressing\n");
Dave Liu8b3eaa12008-03-26 22:47:06 +0800635 return 0;
636 }
637 /* more than 28 bits used, use 48bit mode */
638 lba48 = 1;
639 }
640#endif
Tom Rini953f90f2012-09-29 07:35:12 -0700641 /* Port Number */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200642 num = device / CONFIG_SYS_SATA_DEVS_PER_BUS;
Tom Rini953f90f2012-09-29 07:35:12 -0700643 /* dev on the Port */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200644 if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS)
645 dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS;
Dave Liu8b3eaa12008-03-26 22:47:06 +0800646 else
647 dev = device;
648
649 if (dev == 0)
650 mask = 0x01;
651 else
652 mask = 0x02;
653
654 /* Select device */
Tom Rini953f90f2012-09-29 07:35:12 -0700655 dev_select(&port[num].ioaddr, dev);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800656
Tom Rini953f90f2012-09-29 07:35:12 -0700657 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800658 if (status & ATA_BUSY) {
Tom Rini953f90f2012-09-29 07:35:12 -0700659 printf("ata%u failed to respond\n", port[num].port_no);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800660 return n;
661 }
662
663 while (blkcnt-- > 0) {
Tom Rini953f90f2012-09-29 07:35:12 -0700664 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800665 if (status & ATA_BUSY) {
Tom Rini953f90f2012-09-29 07:35:12 -0700666 printf("ata%u failed to respond\n",
Dave Liu8b3eaa12008-03-26 22:47:06 +0800667 port[num].port_no);
668 return n;
669 }
670#ifdef CONFIG_LBA48
671 if (lba48) {
672 /* write high bits */
Tom Rini953f90f2012-09-29 07:35:12 -0700673 sata_outb(0, port[num].ioaddr.nsect_addr);
674 sata_outb((blknr >> 24) & 0xFF,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800675 port[num].ioaddr.lbal_addr);
Tom Rini953f90f2012-09-29 07:35:12 -0700676 sata_outb((blknr >> 32) & 0xFF,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800677 port[num].ioaddr.lbam_addr);
Tom Rini953f90f2012-09-29 07:35:12 -0700678 sata_outb((blknr >> 40) & 0xFF,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800679 port[num].ioaddr.lbah_addr);
680 }
681#endif
Tom Rini953f90f2012-09-29 07:35:12 -0700682 sata_outb(1, port[num].ioaddr.nsect_addr);
683 sata_outb((blknr >> 0) & 0xFF, port[num].ioaddr.lbal_addr);
684 sata_outb((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
685 sata_outb((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800686#ifdef CONFIG_LBA48
687 if (lba48) {
Tom Rini953f90f2012-09-29 07:35:12 -0700688 sata_outb(ATA_LBA, port[num].ioaddr.device_addr);
689 sata_outb(ATA_CMD_WRITE_EXT,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800690 port[num].ioaddr.command_addr);
691 } else
692#endif
693 {
Tom Rini953f90f2012-09-29 07:35:12 -0700694 sata_outb(ATA_LBA | ((blknr >> 24) & 0xF),
Dave Liu8b3eaa12008-03-26 22:47:06 +0800695 port[num].ioaddr.device_addr);
Tom Rini953f90f2012-09-29 07:35:12 -0700696 sata_outb(ATA_CMD_WRITE,
Dave Liu8b3eaa12008-03-26 22:47:06 +0800697 port[num].ioaddr.command_addr);
698 }
699
Tom Rini953f90f2012-09-29 07:35:12 -0700700 mdelay(50);
701 /* may take up to 4 sec */
702 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 4000);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800703 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
704 != ATA_STAT_DRQ) {
Tom Rini953f90f2012-09-29 07:35:12 -0700705 printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
Dave Liu8b3eaa12008-03-26 22:47:06 +0800706 device, (ulong) blknr, status);
Tom Rini953f90f2012-09-29 07:35:12 -0700707 return n;
Dave Liu8b3eaa12008-03-26 22:47:06 +0800708 }
709
Tom Rini953f90f2012-09-29 07:35:12 -0700710 output_data(&port[num].ioaddr, buffer, ATA_SECTORWORDS);
711 sata_inb(port[num].ioaddr.altstatus_addr);
712 udelay(50);
Dave Liu8b3eaa12008-03-26 22:47:06 +0800713
714 ++n;
715 ++blknr;
716 buffer += ATA_SECTORWORDS;
717 }
718 return n;
719}
720
Dave Liua566ebd2008-03-26 22:50:45 +0800721int scan_sata(int dev)
Dave Liu8b3eaa12008-03-26 22:47:06 +0800722{
Dave Liua566ebd2008-03-26 22:50:45 +0800723 return 0;
Dave Liu8b3eaa12008-03-26 22:47:06 +0800724}