1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/config.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/pci.h>
33#include <linux/init.h>
34#include <linux/blkdev.h>
35#include <linux/delay.h>
36#include <linux/interrupt.h>
37#include "scsi.h"
38#include "hosts.h"
39#include <linux/libata.h>
40
41#ifdef CONFIG_ALL_PPC
42#include <asm/prom.h>
43#include <asm/pci-bridge.h>
44#endif
45
46#define DRV_NAME "ata_k2"
47#define DRV_VERSION "1.03"
48
49
50static u32 k2_sata_scr_read (struct ata_port *ap, unsigned int sc_reg)
51{
52 if (sc_reg > SCR_CONTROL)
53 return 0xffffffffU;
54 return readl((void *) ap->ioaddr.scr_addr + (sc_reg * 4));
55}
56
57
58static void k2_sata_scr_write (struct ata_port *ap, unsigned int sc_reg,
59 u32 val)
60{
61 if (sc_reg > SCR_CONTROL)
62 return;
63 writel(val, (void *) ap->ioaddr.scr_addr + (sc_reg * 4));
64}
65
66
67static void k2_sata_tf_load(struct ata_port *ap, struct ata_taskfile *tf)
68{
69 struct ata_ioports *ioaddr = &ap->ioaddr;
70 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
71
72 if (tf->ctl != ap->last_ctl) {
73 writeb(tf->ctl, ioaddr->ctl_addr);
74 ap->last_ctl = tf->ctl;
75 ata_wait_idle(ap);
76 }
77 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
78 writew(tf->feature | (((u16)tf->hob_feature) << 8), ioaddr->error_addr);
79 writew(tf->nsect | (((u16)tf->hob_nsect) << 8), ioaddr->nsect_addr);
80 writew(tf->lbal | (((u16)tf->hob_lbal) << 8), ioaddr->lbal_addr);
81 writew(tf->lbam | (((u16)tf->hob_lbam) << 8), ioaddr->lbam_addr);
82 writew(tf->lbah | (((u16)tf->hob_lbah) << 8), ioaddr->lbah_addr);
83 } else if (is_addr) {
84 writew(tf->feature, ioaddr->error_addr);
85 writew(tf->nsect, ioaddr->nsect_addr);
86 writew(tf->lbal, ioaddr->lbal_addr);
87 writew(tf->lbam, ioaddr->lbam_addr);
88 writew(tf->lbah, ioaddr->lbah_addr);
89 }
90
91 if (tf->flags & ATA_TFLAG_DEVICE)
92 writeb(tf->device, ioaddr->device_addr);
93
94 ata_wait_idle(ap);
95}
96
97
98static void k2_sata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
99{
100 struct ata_ioports *ioaddr = &ap->ioaddr;
101 u16 nsect, lbal, lbam, lbah;
102
103 nsect = tf->nsect = readw(ioaddr->nsect_addr);
104 lbal = tf->lbal = readw(ioaddr->lbal_addr);
105 lbam = tf->lbam = readw(ioaddr->lbam_addr);
106 lbah = tf->lbah = readw(ioaddr->lbah_addr);
107 tf->device = readw(ioaddr->device_addr);
108
109 if (tf->flags & ATA_TFLAG_LBA48) {
110 tf->hob_feature = readw(ioaddr->error_addr) >> 8;
111 tf->hob_nsect = nsect >> 8;
112 tf->hob_lbal = lbal >> 8;
113 tf->hob_lbam = lbam >> 8;
114 tf->hob_lbah = lbah >> 8;
115 }
116}
117
118
119static u8 k2_stat_check_status(struct ata_port *ap)
120{
121 return readl((void *) ap->ioaddr.cmdstat_addr);
122}
123
124static void k2_sata_set_piomode (struct ata_port *ap, struct ata_device *adev,
125 unsigned int pio)
126{
127
128
129
130}
131
132
133static void k2_sata_set_udmamode (struct ata_port *ap, struct ata_device *adev,
134 unsigned int udma)
135{
136
137
138
139}
140
141
142#ifdef CONFIG_ALL_PPC
143
144
145
146
147
148
149
150
151
152
153
154static int k2_sata_proc_info(char *page, char **start, off_t offset, int count,
155 int hostno, int inout)
156{
157 struct Scsi_Host *hpnt;
158 struct ata_port *ap;
159 struct device_node *np;
160 int len, index;
161
162
163
164
165
166 hpnt = scsi_hostlist;
167 while (hpnt) {
168 if (hostno == hpnt->host_no)
169 break;
170 hpnt = hpnt->next;
171 }
172 if (!hpnt)
173 return 0;
174
175
176 ap = (struct ata_port *) &hpnt->hostdata[0];
177 if (ap == NULL)
178 return 0;
179
180
181 np = pci_device_to_OF_node(ap->host_set->pdev);
182 if (np == NULL)
183 return 0;
184
185
186 index = (ap == ap->host_set->ports[0]) ? 0 : 1;
187 for (np = np->child; np != NULL; np = np->sibling) {
188 u32 *reg = (u32 *)get_property(np, "reg", NULL);
189 if (!reg)
190 continue;
191 if (index == *reg)
192 break;
193 }
194 if (np == NULL)
195 return 0;
196
197 len = sprintf(page, "devspec: %s\n", np->full_name);
198
199 return len;
200}
201#endif
202
203
204static Scsi_Host_Template k2_sata_sht = {
205 .module = THIS_MODULE,
206 .name = DRV_NAME,
207 .queuecommand = ata_scsi_queuecmd,
208 .eh_strategy_handler = ata_scsi_error,
209 .can_queue = ATA_DEF_QUEUE,
210 .this_id = ATA_SHT_THIS_ID,
211 .sg_tablesize = ATA_MAX_PRD,
212 .max_sectors = ATA_MAX_SECTORS,
213 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
214 .emulated = ATA_SHT_EMULATED,
215 .use_clustering = ATA_SHT_USE_CLUSTERING,
216 .proc_name = DRV_NAME,
217 .dma_boundary = ATA_DMA_BOUNDARY,
218 .slave_configure = ata_scsi_slave_config,
219#ifdef CONFIG_ALL_PPC
220 .proc_info = k2_sata_proc_info
221#endif
222};
223
224
225static struct ata_port_operations k2_sata_ops = {
226 .port_disable = ata_port_disable,
227 .set_piomode = k2_sata_set_piomode,
228 .set_udmamode = k2_sata_set_udmamode,
229 .tf_load = k2_sata_tf_load,
230 .tf_read = k2_sata_tf_read,
231 .check_status = k2_stat_check_status,
232 .exec_command = ata_exec_command_mmio,
233 .phy_reset = sata_phy_reset,
234 .phy_config = pata_phy_config,
235 .bmdma_start = ata_bmdma_start_mmio,
236 .fill_sg = ata_fill_sg,
237 .eng_timeout = ata_eng_timeout,
238 .irq_handler = ata_interrupt,
239 .scr_read = k2_sata_scr_read,
240 .scr_write = k2_sata_scr_write,
241 .port_start = ata_port_start,
242 .port_stop = ata_port_stop,
243};
244
245
246static void k2_sata_setup_port(struct ata_ioports *port, unsigned long base)
247{
248 port->cmd_addr = base;
249 port->data_addr = base;
250 port->error_addr = base + 0x4;
251 port->nsect_addr = base + 0x8;
252 port->lbal_addr = base + 0xc;
253 port->lbam_addr = base + 0x10;
254 port->lbah_addr = base + 0x14;
255 port->device_addr = base + 0x18;
256 port->cmdstat_addr = base + 0x1c;
257 port->ctl_addr = base + 0x20;
258 port->bmdma_addr = base + 0x30;
259 port->scr_addr = base + 0x40;
260}
261
262
263static int k2_sata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
264{
265 static int printed_version;
266 struct ata_probe_ent *probe_ent = NULL;
267 unsigned long base;
268 void *mmio_base;
269 int rc;
270
271 if (!printed_version++)
272 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
273
274
275
276
277
278 rc = pci_enable_device(pdev);
279 if (rc)
280 return rc;
281
282 rc = pci_request_regions(pdev, DRV_NAME);
283 if (rc)
284 goto err_out;
285
286 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
287 if (rc)
288 goto err_out_regions;
289
290 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
291 if (probe_ent == NULL) {
292 rc = -ENOMEM;
293 goto err_out_regions;
294 }
295
296 memset(probe_ent, 0, sizeof(*probe_ent));
297 probe_ent->pdev = pdev;
298 INIT_LIST_HEAD(&probe_ent->node);
299
300 mmio_base = ioremap(pci_resource_start(pdev, 5),
301 pci_resource_len(pdev, 5));
302 if (mmio_base == NULL) {
303 rc = -ENOMEM;
304 goto err_out_free_ent;
305 }
306 base = (unsigned long) mmio_base;
307
308
309
310
311
312 if (readl(mmio_base + 0x40) == 0xffffffffUL &&
313 readl(mmio_base + 0x140) == 0xffffffffUL) {
314 rc = -ENODEV;
315 goto err_out_unmap;
316 }
317
318
319
320
321
322 writel(readl(mmio_base + 0x80) & ~0x00040000, mmio_base + 0x80);
323
324
325 writel(0xffffffff, mmio_base + 0x44);
326 writel(0x0, mmio_base + 0x88);
327
328 probe_ent->sht = &k2_sata_sht;
329 probe_ent->host_flags = ATA_FLAG_SATA | ATA_FLAG_SATA_RESET |
330 ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
331 probe_ent->port_ops = &k2_sata_ops;
332 probe_ent->n_ports = 2;
333 probe_ent->irq = pdev->irq;
334 probe_ent->irq_flags = SA_SHIRQ;
335 probe_ent->mmio_base = mmio_base;
336
337
338
339
340
341 probe_ent->pio_mask = 0x1f;
342 probe_ent->udma_mask = 0x7f;
343
344 k2_sata_setup_port(&probe_ent->port[0], base);
345 k2_sata_setup_port(&probe_ent->port[1], base + 0x100);
346
347 pci_set_master(pdev);
348
349
350 ata_device_add(probe_ent);
351 kfree(probe_ent);
352
353 return 0;
354
355err_out_unmap:
356 iounmap((void *)base);
357err_out_free_ent:
358 kfree(probe_ent);
359err_out_regions:
360 pci_release_regions(pdev);
361err_out:
362 pci_disable_device(pdev);
363 return rc;
364}
365
366
367static struct pci_device_id k2_sata_pci_tbl[] = {
368 { 0x1166, 0x0240, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
369 { }
370};
371
372
373static struct pci_driver k2_sata_pci_driver = {
374 .name = DRV_NAME,
375 .id_table = k2_sata_pci_tbl,
376 .probe = k2_sata_init_one,
377 .remove = ata_pci_remove_one,
378};
379
380
381static int __init k2_sata_init(void)
382{
383 int rc;
384
385 rc = pci_module_init(&k2_sata_pci_driver);
386 if (rc)
387 return rc;
388
389 return 0;
390}
391
392
393static void __exit k2_sata_exit(void)
394{
395 pci_unregister_driver(&k2_sata_pci_driver);
396}
397
398
399MODULE_AUTHOR("Benjamin Herrenschmidt");
400MODULE_DESCRIPTION("low-level driver for K2 SATA controller");
401MODULE_LICENSE("GPL");
402MODULE_DEVICE_TABLE(pci, k2_sata_pci_tbl);
403
404module_init(k2_sata_init);
405module_exit(k2_sata_exit);
406