blob: 30426842cc40e31be5e0a2ef3e8dac931463eef9 [file] [log] [blame]
mushtaq khan66d9dbe2007-04-20 14:23:02 +05301/*
Wolfgang Denk3162eb82007-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denk3162eb82007-05-15 23:38:05 +02007 *
8 * with the reference to ata_piix driver in kernel 2.4.32
9 */
mushtaq khan66d9dbe2007-04-20 14:23:02 +053010
11/*
Wolfgang Denk3162eb82007-05-15 23:38:05 +020012 * This file contains SATA controller and SATA drive initialization functions
13 */
mushtaq khan66d9dbe2007-04-20 14:23:02 +053014
15#include <common.h>
Dave Liu9eef6282008-03-26 22:47:06 +080016#include <asm/io.h>
mushtaq khan66d9dbe2007-04-20 14:23:02 +053017#include <pci.h>
18#include <command.h>
19#include <config.h>
20#include <asm/byteorder.h>
Dave Liu9eef6282008-03-26 22:47:06 +080021#include <part.h>
mushtaq khan66d9dbe2007-04-20 14:23:02 +053022#include <ide.h>
23#include <ata.h>
Pavel Herrmanne46a4352012-09-27 23:18:04 +000024#include <sata.h>
Dave Liu8e9bb432008-03-26 22:50:45 +080025
Tom Rini879a57a2012-09-29 07:35:12 -070026#define DEBUG_SATA 0 /* For debug prints set DEBUG_SATA to 1 */
mushtaq khan66d9dbe2007-04-20 14:23:02 +053027
Dave Liu9eef6282008-03-26 22:47:06 +080028#define SATA_DECL
Tom Rini879a57a2012-09-29 07:35:12 -070029#define DRV_DECL /* For file specific declarations */
Dave Liu83c7f472008-03-26 22:48:18 +080030#include "ata_piix.h"
mushtaq khan66d9dbe2007-04-20 14:23:02 +053031
Tom Rini879a57a2012-09-29 07:35:12 -070032/* Macros realted to PCI */
mushtaq khan66d9dbe2007-04-20 14:23:02 +053033#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
50u32 bdf;
Tom Rini879a57a2012-09-29 07:35:12 -070051u32 iobase1; /* Primary cmd block */
52u32 iobase2; /* Primary ctl block */
53u32 iobase3; /* Sec cmd block */
54u32 iobase4; /* sec ctl block */
55u32 iobase5; /* BMDMA*/
56
57int pci_sata_init(void)
mushtaq khan66d9dbe2007-04-20 14:23:02 +053058{
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 Rini879a57a2012-09-29 07:35:12 -070064 u8 pmr; /* Port mapping reg */
65 u8 pi; /* Prgming Interface reg */
mushtaq khan66d9dbe2007-04-20 14:23:02 +053066
Tom Rini879a57a2012-09-29 07:35:12 -070067 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 khan66d9dbe2007-04-20 14:23:02 +053073
74 if ((iobase1 == 0xFFFFFFFF) || (iobase2 == 0xFFFFFFFF) ||
75 (iobase3 == 0xFFFFFFFF) || (iobase4 == 0xFFFFFFFF) ||
76 (iobase5 == 0xFFFFFFFF)) {
Tom Rini879a57a2012-09-29 07:35:12 -070077 /* ERROR */
78 printf("error no base addr for SATA controller\n");
mushtaq khan66d9dbe2007-04-20 14:23:02 +053079 return 1;
Tom Rini879a57a2012-09-29 07:35:12 -070080 }
mushtaq khan66d9dbe2007-04-20 14:23:02 +053081
82 iobase1 &= 0xFFFFFFFE;
83 iobase2 &= 0xFFFFFFFE;
84 iobase3 &= 0xFFFFFFFE;
85 iobase4 &= 0xFFFFFFFE;
86 iobase5 &= 0xFFFFFFFE;
87
Tom Rini879a57a2012-09-29 07:35:12 -070088 /* check for mode */
89 pci_read_config_byte(bdf, PCI_PMR, &pmr);
mushtaq khan66d9dbe2007-04-20 14:23:02 +053090 if (pmr > 1) {
Tom Rini879a57a2012-09-29 07:35:12 -070091 puts("combined mode not supported\n");
mushtaq khan66d9dbe2007-04-20 14:23:02 +053092 return 1;
93 }
94
Tom Rini879a57a2012-09-29 07:35:12 -070095 pci_read_config_byte(bdf, PCI_PI, &pi);
mushtaq khan66d9dbe2007-04-20 14:23:02 +053096 if ((pi & 0x05) != 0x05) {
Tom Rini879a57a2012-09-29 07:35:12 -070097 puts("Sata is in Legacy mode\n");
mushtaq khan66d9dbe2007-04-20 14:23:02 +053098 return 1;
Tom Rini879a57a2012-09-29 07:35:12 -070099 } else
100 puts("sata is in Native mode\n");
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530101
Tom Rini879a57a2012-09-29 07:35:12 -0700102 /* MASTER CFG AND IO CFG */
103 pci_read_config_word(bdf, PCI_COMMAND, &cmd);
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530104 cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO;
Tom Rini879a57a2012-09-29 07:35:12 -0700105 pci_write_config_word(bdf, PCI_COMMAND, cmd);
106 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530107
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 Rini879a57a2012-09-29 07:35:12 -0700112 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530113
114 return 0;
115}
116
Tom Rini879a57a2012-09-29 07:35:12 -0700117int sata_bus_probe(int port_no)
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530118{
119 int orig_mask, mask;
120 u16 pcs;
121
122 mask = (PORT_PRESENT << port_no);
Tom Rini879a57a2012-09-29 07:35:12 -0700123 pci_read_config_word(bdf, PCI_PCS, &pcs);
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530124 orig_mask = (int) pcs & 0xff;
125 if ((orig_mask & mask) != mask)
126 return 0;
127 else
128 return 1;
129}
130
Tom Rini879a57a2012-09-29 07:35:12 -0700131int init_sata(int dev)
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530132{
Tom Rini879a57a2012-09-29 07:35:12 -0700133 static int done;
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530134 u8 i, rv = 0;
135
Dave Liu8e9bb432008-03-26 22:50:45 +0800136 if (!done)
137 done = 1;
138 else
139 return 0;
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530140
Tom Rini879a57a2012-09-29 07:35:12 -0700141 rv = pci_sata_init();
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530142 if (rv == 1) {
Tom Rini879a57a2012-09-29 07:35:12 -0700143 puts("pci initialization failed\n");
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530144 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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200159 for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++)
Tom Rini879a57a2012-09-29 07:35:12 -0700160 sata_port(&port[i].ioaddr);
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530161
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200162 for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) {
Tom Rini879a57a2012-09-29 07:35:12 -0700163 if (!(sata_bus_probe(i))) {
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530164 port[i].port_state = 0;
Tom Rini879a57a2012-09-29 07:35:12 -0700165 printf("SATA#%d port is not present\n", i);
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530166 } else {
Tom Rini879a57a2012-09-29 07:35:12 -0700167 printf("SATA#%d port is present\n", i);
168 if (sata_bus_softreset(i))
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530169 port[i].port_state = 0;
Tom Rini879a57a2012-09-29 07:35:12 -0700170 else
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530171 port[i].port_state = 1;
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530172 }
173 }
174
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200175 for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) {
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530176 u8 j, devno;
177
178 if (port[i].port_state == 0)
179 continue;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200180 for (j = 0; j < CONFIG_SYS_SATA_DEVS_PER_BUS; j++) {
Tom Rini879a57a2012-09-29 07:35:12 -0700181 sata_identify(i, j);
182 set_Feature_cmd(i, j);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200183 devno = i * CONFIG_SYS_SATA_DEVS_PER_BUS + j;
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530184 if ((sata_dev_desc[devno].lba > 0) &&
185 (sata_dev_desc[devno].blksz > 0)) {
Tom Rini879a57a2012-09-29 07:35:12 -0700186 dev_print(&sata_dev_desc[devno]);
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530187 /* initialize partition type */
Tom Rini879a57a2012-09-29 07:35:12 -0700188 init_part(&sata_dev_desc[devno]);
mushtaq khan66d9dbe2007-04-20 14:23:02 +0530189 }
190 }
191 }
192 return 0;
193}
Dave Liu9eef6282008-03-26 22:47:06 +0800194
Nikita Kiryanov10ee8ec2014-11-21 12:47:23 +0200195int reset_sata(int dev)
196{
197 return 0;
198}
199
Tom Rini879a57a2012-09-29 07:35:12 -0700200static inline u8 sata_inb(unsigned long ioaddr)
Dave Liu9eef6282008-03-26 22:47:06 +0800201{
Tom Rini879a57a2012-09-29 07:35:12 -0700202 return inb(ioaddr);
Dave Liu9eef6282008-03-26 22:47:06 +0800203}
204
Tom Rini879a57a2012-09-29 07:35:12 -0700205static inline void sata_outb(unsigned char val, unsigned long ioaddr)
Dave Liu9eef6282008-03-26 22:47:06 +0800206{
Tom Rini879a57a2012-09-29 07:35:12 -0700207 outb(val, ioaddr);
Dave Liu9eef6282008-03-26 22:47:06 +0800208}
209
Tom Rini879a57a2012-09-29 07:35:12 -0700210static void output_data(struct sata_ioports *ioaddr, ulong * sect_buf,
211 int words)
Dave Liu9eef6282008-03-26 22:47:06 +0800212{
Tom Rini879a57a2012-09-29 07:35:12 -0700213 outsw(ioaddr->data_addr, sect_buf, words << 1);
Dave Liu9eef6282008-03-26 22:47:06 +0800214}
215
Tom Rini879a57a2012-09-29 07:35:12 -0700216static int input_data(struct sata_ioports *ioaddr, ulong * sect_buf, int words)
Dave Liu9eef6282008-03-26 22:47:06 +0800217{
Tom Rini879a57a2012-09-29 07:35:12 -0700218 insw(ioaddr->data_addr, sect_buf, words << 1);
Dave Liu9eef6282008-03-26 22:47:06 +0800219 return 0;
220}
221
Tom Rini879a57a2012-09-29 07:35:12 -0700222static void sata_cpy(unsigned char *dst, unsigned char *src, unsigned int len)
Dave Liu9eef6282008-03-26 22:47:06 +0800223{
224 unsigned char *end, *last;
225
226 last = dst;
227 end = src + len - 1;
228
229 /* reserve space for '\0' */
230 if (len < 2)
231 goto OUT;
232
233 /* skip leading white space */
234 while ((*src) && (src < end) && (*src == ' '))
235 ++src;
236
237 /* copy string, omitting trailing white space */
238 while ((*src) && (src < end)) {
239 *dst++ = *src;
240 if (*src++ != ' ')
241 last = dst;
242 }
Tom Rini879a57a2012-09-29 07:35:12 -0700243OUT:
Dave Liu9eef6282008-03-26 22:47:06 +0800244 *last = '\0';
245}
246
Tom Rini879a57a2012-09-29 07:35:12 -0700247int sata_bus_softreset(int num)
Dave Liu9eef6282008-03-26 22:47:06 +0800248{
249 u8 dev = 0, status = 0, i;
250
251 port[num].dev_mask = 0;
252
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200253 for (i = 0; i < CONFIG_SYS_SATA_DEVS_PER_BUS; i++) {
Tom Rini879a57a2012-09-29 07:35:12 -0700254 if (!(sata_devchk(&port[num].ioaddr, i))) {
255 debug("dev_chk failed for dev#%d\n", i);
Dave Liu9eef6282008-03-26 22:47:06 +0800256 } else {
257 port[num].dev_mask |= (1 << i);
Tom Rini879a57a2012-09-29 07:35:12 -0700258 debug("dev_chk passed for dev#%d\n", i);
Dave Liu9eef6282008-03-26 22:47:06 +0800259 }
260 }
261
262 if (!(port[num].dev_mask)) {
Tom Rini879a57a2012-09-29 07:35:12 -0700263 printf("no devices on port%d\n", num);
Dave Liu9eef6282008-03-26 22:47:06 +0800264 return 1;
265 }
266
Tom Rini879a57a2012-09-29 07:35:12 -0700267 dev_select(&port[num].ioaddr, dev);
Dave Liu9eef6282008-03-26 22:47:06 +0800268
Tom Rini879a57a2012-09-29 07:35:12 -0700269 port[num].ctl_reg = 0x08; /* Default value of control reg */
270 sata_outb(port[num].ctl_reg, port[num].ioaddr.ctl_addr);
271 udelay(10);
272 sata_outb(port[num].ctl_reg | ATA_SRST, port[num].ioaddr.ctl_addr);
273 udelay(10);
274 sata_outb(port[num].ctl_reg, port[num].ioaddr.ctl_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800275
Tom Rini879a57a2012-09-29 07:35:12 -0700276 /*
277 * spec mandates ">= 2ms" before checking status.
Dave Liu9eef6282008-03-26 22:47:06 +0800278 * We wait 150ms, because that was the magic delay used for
279 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
280 * between when the ATA command register is written, and then
281 * status is checked. Because waiting for "a while" before
282 * checking status is fine, post SRST, we perform this magic
283 * delay here as well.
284 */
Tom Rini879a57a2012-09-29 07:35:12 -0700285 mdelay(150);
286 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 300);
Dave Liu9eef6282008-03-26 22:47:06 +0800287 while ((status & ATA_BUSY)) {
Tom Rini879a57a2012-09-29 07:35:12 -0700288 mdelay(100);
289 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 3);
Dave Liu9eef6282008-03-26 22:47:06 +0800290 }
291
292 if (status & ATA_BUSY)
Tom Rini879a57a2012-09-29 07:35:12 -0700293 printf("ata%u is slow to respond,plz be patient\n", num);
Dave Liu9eef6282008-03-26 22:47:06 +0800294
295 while ((status & ATA_BUSY)) {
Tom Rini879a57a2012-09-29 07:35:12 -0700296 mdelay(100);
297 status = sata_chk_status(&port[num].ioaddr);
Dave Liu9eef6282008-03-26 22:47:06 +0800298 }
299
300 if (status & ATA_BUSY) {
Tom Rini879a57a2012-09-29 07:35:12 -0700301 printf("ata%u failed to respond : bus reset failed\n", num);
Dave Liu9eef6282008-03-26 22:47:06 +0800302 return 1;
303 }
304 return 0;
305}
306
Tom Rini879a57a2012-09-29 07:35:12 -0700307void sata_identify(int num, int dev)
Dave Liu9eef6282008-03-26 22:47:06 +0800308{
Tom Rini879a57a2012-09-29 07:35:12 -0700309 u8 cmd = 0, status = 0;
310 u8 devno = num * CONFIG_SYS_SATA_DEVS_PER_BUS + dev;
Dave Liu9eef6282008-03-26 22:47:06 +0800311 u16 iobuf[ATA_SECT_SIZE];
312 u64 n_sectors = 0;
313 u8 mask = 0;
314
Tom Rini879a57a2012-09-29 07:35:12 -0700315 memset(iobuf, 0, sizeof(iobuf));
Dave Liu9eef6282008-03-26 22:47:06 +0800316 hd_driveid_t *iop = (hd_driveid_t *) iobuf;
317
318 if (dev == 0)
319 mask = 0x01;
320 else
321 mask = 0x02;
322
323 if (!(port[num].dev_mask & mask)) {
Tom Rini879a57a2012-09-29 07:35:12 -0700324 printf("dev%d is not present on port#%d\n", dev, num);
Dave Liu9eef6282008-03-26 22:47:06 +0800325 return;
326 }
327
Tom Rini879a57a2012-09-29 07:35:12 -0700328 printf("port=%d dev=%d\n", num, dev);
Dave Liu9eef6282008-03-26 22:47:06 +0800329
Tom Rini879a57a2012-09-29 07:35:12 -0700330 dev_select(&port[num].ioaddr, dev);
Dave Liu9eef6282008-03-26 22:47:06 +0800331
332 status = 0;
Tom Rini879a57a2012-09-29 07:35:12 -0700333 cmd = ATA_CMD_IDENT; /* Device Identify Command */
334 sata_outb(cmd, port[num].ioaddr.command_addr);
335 sata_inb(port[num].ioaddr.altstatus_addr);
336 udelay(10);
Dave Liu9eef6282008-03-26 22:47:06 +0800337
Tom Rini879a57a2012-09-29 07:35:12 -0700338 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 1000);
Dave Liu9eef6282008-03-26 22:47:06 +0800339 if (status & ATA_ERR) {
Tom Rini879a57a2012-09-29 07:35:12 -0700340 puts("\ndevice not responding\n");
Dave Liu9eef6282008-03-26 22:47:06 +0800341 port[num].dev_mask &= ~mask;
342 return;
343 }
344
Tom Rini879a57a2012-09-29 07:35:12 -0700345 input_data(&port[num].ioaddr, (ulong *) iobuf, ATA_SECTORWORDS);
Dave Liu9eef6282008-03-26 22:47:06 +0800346
Tom Rini879a57a2012-09-29 07:35:12 -0700347 debug("\nata%u: dev %u cfg 49:%04x 82:%04x 83:%04x 84:%04x85:%04x"
Dave Liu9eef6282008-03-26 22:47:06 +0800348 "86:%04x" "87:%04x 88:%04x\n", num, dev, iobuf[49],
349 iobuf[82], iobuf[83], iobuf[84], iobuf[85], iobuf[86],
350 iobuf[87], iobuf[88]);
351
352 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
Tom Rini879a57a2012-09-29 07:35:12 -0700353 if (!ata_id_has_dma(iobuf) || !ata_id_has_lba(iobuf))
354 debug("ata%u: no dma/lba\n", num);
355 ata_dump_id(iobuf);
Dave Liu9eef6282008-03-26 22:47:06 +0800356
Tom Rini879a57a2012-09-29 07:35:12 -0700357 if (ata_id_has_lba48(iobuf))
358 n_sectors = ata_id_u64(iobuf, 100);
359 else
360 n_sectors = ata_id_u32(iobuf, 60);
361 debug("no. of sectors %u\n", ata_id_u64(iobuf, 100));
362 debug("no. of sectors %u\n", ata_id_u32(iobuf, 60));
Dave Liu9eef6282008-03-26 22:47:06 +0800363
364 if (n_sectors == 0) {
365 port[num].dev_mask &= ~mask;
366 return;
367 }
368
Tom Rini879a57a2012-09-29 07:35:12 -0700369 sata_cpy((unsigned char *)sata_dev_desc[devno].revision, iop->fw_rev,
370 sizeof(sata_dev_desc[devno].revision));
371 sata_cpy((unsigned char *)sata_dev_desc[devno].vendor, iop->model,
372 sizeof(sata_dev_desc[devno].vendor));
373 sata_cpy((unsigned char *)sata_dev_desc[devno].product, iop->serial_no,
374 sizeof(sata_dev_desc[devno].product));
375 strswab(sata_dev_desc[devno].revision);
376 strswab(sata_dev_desc[devno].vendor);
Dave Liu9eef6282008-03-26 22:47:06 +0800377
Tom Rini879a57a2012-09-29 07:35:12 -0700378 if ((iop->config & 0x0080) == 0x0080)
Dave Liu9eef6282008-03-26 22:47:06 +0800379 sata_dev_desc[devno].removable = 1;
Tom Rini879a57a2012-09-29 07:35:12 -0700380 else
Dave Liu9eef6282008-03-26 22:47:06 +0800381 sata_dev_desc[devno].removable = 0;
Dave Liu9eef6282008-03-26 22:47:06 +0800382
383 sata_dev_desc[devno].lba = iop->lba_capacity;
Tom Rini879a57a2012-09-29 07:35:12 -0700384 debug("lba=0x%x", sata_dev_desc[devno].lba);
Dave Liu9eef6282008-03-26 22:47:06 +0800385
386#ifdef CONFIG_LBA48
387 if (iop->command_set_2 & 0x0400) {
388 sata_dev_desc[devno].lba48 = 1;
389 lba = (unsigned long long) iop->lba48_capacity[0] |
390 ((unsigned long long) iop->lba48_capacity[1] << 16) |
391 ((unsigned long long) iop->lba48_capacity[2] << 32) |
392 ((unsigned long long) iop->lba48_capacity[3] << 48);
393 } else {
394 sata_dev_desc[devno].lba48 = 0;
395 }
396#endif
397
398 /* assuming HD */
399 sata_dev_desc[devno].type = DEV_TYPE_HARDDISK;
400 sata_dev_desc[devno].blksz = ATA_BLOCKSIZE;
Egbert Eich0472fbf2013-04-09 21:11:56 +0000401 sata_dev_desc[devno].log2blksz = LOG2(sata_dev_desc[devno].blksz);
Dave Liu9eef6282008-03-26 22:47:06 +0800402 sata_dev_desc[devno].lun = 0; /* just to fill something in... */
403}
404
Tom Rini879a57a2012-09-29 07:35:12 -0700405void set_Feature_cmd(int num, int dev)
Dave Liu9eef6282008-03-26 22:47:06 +0800406{
407 u8 mask = 0x00, status = 0;
408
409 if (dev == 0)
410 mask = 0x01;
411 else
412 mask = 0x02;
413
414 if (!(port[num].dev_mask & mask)) {
Tom Rini879a57a2012-09-29 07:35:12 -0700415 debug("dev%d is not present on port#%d\n", dev, num);
Dave Liu9eef6282008-03-26 22:47:06 +0800416 return;
417 }
418
Tom Rini879a57a2012-09-29 07:35:12 -0700419 dev_select(&port[num].ioaddr, dev);
Dave Liu9eef6282008-03-26 22:47:06 +0800420
Tom Rini879a57a2012-09-29 07:35:12 -0700421 sata_outb(SETFEATURES_XFER, port[num].ioaddr.feature_addr);
422 sata_outb(XFER_PIO_4, port[num].ioaddr.nsect_addr);
423 sata_outb(0, port[num].ioaddr.lbal_addr);
424 sata_outb(0, port[num].ioaddr.lbam_addr);
425 sata_outb(0, port[num].ioaddr.lbah_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800426
Tom Rini879a57a2012-09-29 07:35:12 -0700427 sata_outb(ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
428 sata_outb(ATA_CMD_SETF, port[num].ioaddr.command_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800429
Tom Rini879a57a2012-09-29 07:35:12 -0700430 udelay(50);
431 mdelay(150);
Dave Liu9eef6282008-03-26 22:47:06 +0800432
Tom Rini879a57a2012-09-29 07:35:12 -0700433 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 5000);
Dave Liu9eef6282008-03-26 22:47:06 +0800434 if ((status & (ATA_STAT_BUSY | ATA_STAT_ERR))) {
Tom Rini879a57a2012-09-29 07:35:12 -0700435 printf("Error : status 0x%02x\n", status);
Dave Liu9eef6282008-03-26 22:47:06 +0800436 port[num].dev_mask &= ~mask;
437 }
438}
439
Tom Rini879a57a2012-09-29 07:35:12 -0700440void sata_port(struct sata_ioports *ioport)
Dave Liu9eef6282008-03-26 22:47:06 +0800441{
442 ioport->data_addr = ioport->cmd_addr + ATA_REG_DATA;
443 ioport->error_addr = ioport->cmd_addr + ATA_REG_ERR;
444 ioport->feature_addr = ioport->cmd_addr + ATA_REG_FEATURE;
445 ioport->nsect_addr = ioport->cmd_addr + ATA_REG_NSECT;
446 ioport->lbal_addr = ioport->cmd_addr + ATA_REG_LBAL;
447 ioport->lbam_addr = ioport->cmd_addr + ATA_REG_LBAM;
448 ioport->lbah_addr = ioport->cmd_addr + ATA_REG_LBAH;
449 ioport->device_addr = ioport->cmd_addr + ATA_REG_DEVICE;
450 ioport->status_addr = ioport->cmd_addr + ATA_REG_STATUS;
451 ioport->command_addr = ioport->cmd_addr + ATA_REG_CMD;
452}
453
Tom Rini879a57a2012-09-29 07:35:12 -0700454int sata_devchk(struct sata_ioports *ioaddr, int dev)
Dave Liu9eef6282008-03-26 22:47:06 +0800455{
456 u8 nsect, lbal;
457
Tom Rini879a57a2012-09-29 07:35:12 -0700458 dev_select(ioaddr, dev);
Dave Liu9eef6282008-03-26 22:47:06 +0800459
Tom Rini879a57a2012-09-29 07:35:12 -0700460 sata_outb(0x55, ioaddr->nsect_addr);
461 sata_outb(0xaa, ioaddr->lbal_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800462
Tom Rini879a57a2012-09-29 07:35:12 -0700463 sata_outb(0xaa, ioaddr->nsect_addr);
464 sata_outb(0x55, ioaddr->lbal_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800465
Tom Rini879a57a2012-09-29 07:35:12 -0700466 sata_outb(0x55, ioaddr->nsect_addr);
467 sata_outb(0xaa, ioaddr->lbal_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800468
Tom Rini879a57a2012-09-29 07:35:12 -0700469 nsect = sata_inb(ioaddr->nsect_addr);
470 lbal = sata_inb(ioaddr->lbal_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800471
472 if ((nsect == 0x55) && (lbal == 0xaa))
473 return 1; /* we found a device */
474 else
475 return 0; /* nothing found */
476}
477
Tom Rini879a57a2012-09-29 07:35:12 -0700478void dev_select(struct sata_ioports *ioaddr, int dev)
Dave Liu9eef6282008-03-26 22:47:06 +0800479{
480 u8 tmp = 0;
481
482 if (dev == 0)
483 tmp = ATA_DEVICE_OBS;
484 else
485 tmp = ATA_DEVICE_OBS | ATA_DEV1;
486
Tom Rini879a57a2012-09-29 07:35:12 -0700487 sata_outb(tmp, ioaddr->device_addr);
488 sata_inb(ioaddr->altstatus_addr);
489 udelay(5);
Dave Liu9eef6282008-03-26 22:47:06 +0800490}
491
Tom Rini879a57a2012-09-29 07:35:12 -0700492u8 sata_busy_wait(struct sata_ioports *ioaddr, int bits, unsigned int max)
Dave Liu9eef6282008-03-26 22:47:06 +0800493{
494 u8 status;
495
496 do {
Tom Rini879a57a2012-09-29 07:35:12 -0700497 udelay(1000);
498 status = sata_chk_status(ioaddr);
Dave Liu9eef6282008-03-26 22:47:06 +0800499 max--;
500 } while ((status & bits) && (max > 0));
501
502 return status;
503}
504
Tom Rini879a57a2012-09-29 07:35:12 -0700505u8 sata_chk_status(struct sata_ioports *ioaddr)
Dave Liu9eef6282008-03-26 22:47:06 +0800506{
Tom Rini879a57a2012-09-29 07:35:12 -0700507 return sata_inb(ioaddr->status_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800508}
509
Dave Liu9eef6282008-03-26 22:47:06 +0800510
Tom Rini879a57a2012-09-29 07:35:12 -0700511ulong sata_read(int device, ulong blknr, lbaint_t blkcnt, void *buff)
Dave Liu9eef6282008-03-26 22:47:06 +0800512{
513 ulong n = 0, *buffer = (ulong *)buff;
514 u8 dev = 0, num = 0, mask = 0, status = 0;
515
516#ifdef CONFIG_LBA48
517 unsigned char lba48 = 0;
518
519 if (blknr & 0x0000fffff0000000) {
520 if (!sata_dev_desc[devno].lba48) {
Tom Rini879a57a2012-09-29 07:35:12 -0700521 printf("Drive doesn't support 48-bit addressing\n");
Dave Liu9eef6282008-03-26 22:47:06 +0800522 return 0;
523 }
524 /* more than 28 bits used, use 48bit mode */
525 lba48 = 1;
526 }
527#endif
Tom Rini879a57a2012-09-29 07:35:12 -0700528 /* Port Number */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200529 num = device / CONFIG_SYS_SATA_DEVS_PER_BUS;
Tom Rini879a57a2012-09-29 07:35:12 -0700530 /* dev on the port */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200531 if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS)
532 dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS;
Dave Liu9eef6282008-03-26 22:47:06 +0800533 else
534 dev = device;
535
536 if (dev == 0)
537 mask = 0x01;
538 else
539 mask = 0x02;
540
541 if (!(port[num].dev_mask & mask)) {
Tom Rini879a57a2012-09-29 07:35:12 -0700542 printf("dev%d is not present on port#%d\n", dev, num);
Dave Liu9eef6282008-03-26 22:47:06 +0800543 return 0;
544 }
545
546 /* Select device */
Tom Rini879a57a2012-09-29 07:35:12 -0700547 dev_select(&port[num].ioaddr, dev);
Dave Liu9eef6282008-03-26 22:47:06 +0800548
Tom Rini879a57a2012-09-29 07:35:12 -0700549 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
Dave Liu9eef6282008-03-26 22:47:06 +0800550 if (status & ATA_BUSY) {
Tom Rini879a57a2012-09-29 07:35:12 -0700551 printf("ata%u failed to respond\n", port[num].port_no);
Dave Liu9eef6282008-03-26 22:47:06 +0800552 return n;
553 }
554 while (blkcnt-- > 0) {
Tom Rini879a57a2012-09-29 07:35:12 -0700555 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
Dave Liu9eef6282008-03-26 22:47:06 +0800556 if (status & ATA_BUSY) {
Tom Rini879a57a2012-09-29 07:35:12 -0700557 printf("ata%u failed to respond\n", 0);
Dave Liu9eef6282008-03-26 22:47:06 +0800558 return n;
559 }
560#ifdef CONFIG_LBA48
561 if (lba48) {
562 /* write high bits */
Tom Rini879a57a2012-09-29 07:35:12 -0700563 sata_outb(0, port[num].ioaddr.nsect_addr);
564 sata_outb((blknr >> 24) & 0xFF,
Dave Liu9eef6282008-03-26 22:47:06 +0800565 port[num].ioaddr.lbal_addr);
Tom Rini879a57a2012-09-29 07:35:12 -0700566 sata_outb((blknr >> 32) & 0xFF,
Dave Liu9eef6282008-03-26 22:47:06 +0800567 port[num].ioaddr.lbam_addr);
Tom Rini879a57a2012-09-29 07:35:12 -0700568 sata_outb((blknr >> 40) & 0xFF,
Dave Liu9eef6282008-03-26 22:47:06 +0800569 port[num].ioaddr.lbah_addr);
570 }
571#endif
Tom Rini879a57a2012-09-29 07:35:12 -0700572 sata_outb(1, port[num].ioaddr.nsect_addr);
573 sata_outb(((blknr) >> 0) & 0xFF,
Dave Liu9eef6282008-03-26 22:47:06 +0800574 port[num].ioaddr.lbal_addr);
Tom Rini879a57a2012-09-29 07:35:12 -0700575 sata_outb((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
576 sata_outb((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800577
578#ifdef CONFIG_LBA48
579 if (lba48) {
Tom Rini879a57a2012-09-29 07:35:12 -0700580 sata_outb(ATA_LBA, port[num].ioaddr.device_addr);
581 sata_outb(ATA_CMD_READ_EXT,
Dave Liu9eef6282008-03-26 22:47:06 +0800582 port[num].ioaddr.command_addr);
583 } else
584#endif
585 {
Tom Rini879a57a2012-09-29 07:35:12 -0700586 sata_outb(ATA_LBA | ((blknr >> 24) & 0xF),
Dave Liu9eef6282008-03-26 22:47:06 +0800587 port[num].ioaddr.device_addr);
Tom Rini879a57a2012-09-29 07:35:12 -0700588 sata_outb(ATA_CMD_READ,
Dave Liu9eef6282008-03-26 22:47:06 +0800589 port[num].ioaddr.command_addr);
590 }
591
Tom Rini879a57a2012-09-29 07:35:12 -0700592 mdelay(50);
593 /* may take up to 4 sec */
594 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 4000);
Dave Liu9eef6282008-03-26 22:47:06 +0800595
596 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
597 != ATA_STAT_DRQ) {
598 u8 err = 0;
599
Tom Rini879a57a2012-09-29 07:35:12 -0700600 printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
Dave Liu9eef6282008-03-26 22:47:06 +0800601 device, (ulong) blknr, status);
Tom Rini879a57a2012-09-29 07:35:12 -0700602 err = sata_inb(port[num].ioaddr.error_addr);
603 printf("Error reg = 0x%x\n", err);
604 return n;
Dave Liu9eef6282008-03-26 22:47:06 +0800605 }
Tom Rini879a57a2012-09-29 07:35:12 -0700606 input_data(&port[num].ioaddr, buffer, ATA_SECTORWORDS);
607 sata_inb(port[num].ioaddr.altstatus_addr);
608 udelay(50);
Dave Liu9eef6282008-03-26 22:47:06 +0800609
610 ++n;
611 ++blknr;
612 buffer += ATA_SECTORWORDS;
613 }
614 return n;
615}
616
Tom Rini5048a672012-09-29 07:52:48 -0700617ulong sata_write(int device, ulong blknr, lbaint_t blkcnt, const void *buff)
Dave Liu9eef6282008-03-26 22:47:06 +0800618{
619 ulong n = 0, *buffer = (ulong *)buff;
620 unsigned char status = 0, num = 0, dev = 0, mask = 0;
621
622#ifdef CONFIG_LBA48
623 unsigned char lba48 = 0;
624
625 if (blknr & 0x0000fffff0000000) {
626 if (!sata_dev_desc[devno].lba48) {
Tom Rini879a57a2012-09-29 07:35:12 -0700627 printf("Drive doesn't support 48-bit addressing\n");
Dave Liu9eef6282008-03-26 22:47:06 +0800628 return 0;
629 }
630 /* more than 28 bits used, use 48bit mode */
631 lba48 = 1;
632 }
633#endif
Tom Rini879a57a2012-09-29 07:35:12 -0700634 /* Port Number */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200635 num = device / CONFIG_SYS_SATA_DEVS_PER_BUS;
Tom Rini879a57a2012-09-29 07:35:12 -0700636 /* dev on the Port */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200637 if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS)
638 dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS;
Dave Liu9eef6282008-03-26 22:47:06 +0800639 else
640 dev = device;
641
642 if (dev == 0)
643 mask = 0x01;
644 else
645 mask = 0x02;
646
647 /* Select device */
Tom Rini879a57a2012-09-29 07:35:12 -0700648 dev_select(&port[num].ioaddr, dev);
Dave Liu9eef6282008-03-26 22:47:06 +0800649
Tom Rini879a57a2012-09-29 07:35:12 -0700650 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
Dave Liu9eef6282008-03-26 22:47:06 +0800651 if (status & ATA_BUSY) {
Tom Rini879a57a2012-09-29 07:35:12 -0700652 printf("ata%u failed to respond\n", port[num].port_no);
Dave Liu9eef6282008-03-26 22:47:06 +0800653 return n;
654 }
655
656 while (blkcnt-- > 0) {
Tom Rini879a57a2012-09-29 07:35:12 -0700657 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
Dave Liu9eef6282008-03-26 22:47:06 +0800658 if (status & ATA_BUSY) {
Tom Rini879a57a2012-09-29 07:35:12 -0700659 printf("ata%u failed to respond\n",
Dave Liu9eef6282008-03-26 22:47:06 +0800660 port[num].port_no);
661 return n;
662 }
663#ifdef CONFIG_LBA48
664 if (lba48) {
665 /* write high bits */
Tom Rini879a57a2012-09-29 07:35:12 -0700666 sata_outb(0, port[num].ioaddr.nsect_addr);
667 sata_outb((blknr >> 24) & 0xFF,
Dave Liu9eef6282008-03-26 22:47:06 +0800668 port[num].ioaddr.lbal_addr);
Tom Rini879a57a2012-09-29 07:35:12 -0700669 sata_outb((blknr >> 32) & 0xFF,
Dave Liu9eef6282008-03-26 22:47:06 +0800670 port[num].ioaddr.lbam_addr);
Tom Rini879a57a2012-09-29 07:35:12 -0700671 sata_outb((blknr >> 40) & 0xFF,
Dave Liu9eef6282008-03-26 22:47:06 +0800672 port[num].ioaddr.lbah_addr);
673 }
674#endif
Tom Rini879a57a2012-09-29 07:35:12 -0700675 sata_outb(1, port[num].ioaddr.nsect_addr);
676 sata_outb((blknr >> 0) & 0xFF, port[num].ioaddr.lbal_addr);
677 sata_outb((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
678 sata_outb((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
Dave Liu9eef6282008-03-26 22:47:06 +0800679#ifdef CONFIG_LBA48
680 if (lba48) {
Tom Rini879a57a2012-09-29 07:35:12 -0700681 sata_outb(ATA_LBA, port[num].ioaddr.device_addr);
682 sata_outb(ATA_CMD_WRITE_EXT,
Dave Liu9eef6282008-03-26 22:47:06 +0800683 port[num].ioaddr.command_addr);
684 } else
685#endif
686 {
Tom Rini879a57a2012-09-29 07:35:12 -0700687 sata_outb(ATA_LBA | ((blknr >> 24) & 0xF),
Dave Liu9eef6282008-03-26 22:47:06 +0800688 port[num].ioaddr.device_addr);
Tom Rini879a57a2012-09-29 07:35:12 -0700689 sata_outb(ATA_CMD_WRITE,
Dave Liu9eef6282008-03-26 22:47:06 +0800690 port[num].ioaddr.command_addr);
691 }
692
Tom Rini879a57a2012-09-29 07:35:12 -0700693 mdelay(50);
694 /* may take up to 4 sec */
695 status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 4000);
Dave Liu9eef6282008-03-26 22:47:06 +0800696 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
697 != ATA_STAT_DRQ) {
Tom Rini879a57a2012-09-29 07:35:12 -0700698 printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
Dave Liu9eef6282008-03-26 22:47:06 +0800699 device, (ulong) blknr, status);
Tom Rini879a57a2012-09-29 07:35:12 -0700700 return n;
Dave Liu9eef6282008-03-26 22:47:06 +0800701 }
702
Tom Rini879a57a2012-09-29 07:35:12 -0700703 output_data(&port[num].ioaddr, buffer, ATA_SECTORWORDS);
704 sata_inb(port[num].ioaddr.altstatus_addr);
705 udelay(50);
Dave Liu9eef6282008-03-26 22:47:06 +0800706
707 ++n;
708 ++blknr;
709 buffer += ATA_SECTORWORDS;
710 }
711 return n;
712}
713
Dave Liu8e9bb432008-03-26 22:50:45 +0800714int scan_sata(int dev)
Dave Liu9eef6282008-03-26 22:47:06 +0800715{
Dave Liu8e9bb432008-03-26 22:50:45 +0800716 return 0;
Dave Liu9eef6282008-03-26 22:47:06 +0800717}