25 #include <linux/types.h>
26 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/reboot.h>
33 #include <linux/pci.h>
34 #include <linux/adb.h>
35 #include <linux/pmu.h>
37 #include <linux/slab.h>
43 #include <asm/pci-bridge.h>
44 #include <asm/machdep.h>
46 #include <asm/sections.h>
50 #define DRV_NAME "ide-pmac"
54 #define DMA_WAIT_TIMEOUT 50
88 static const char* model_name[] = {
101 #define IDE_TIMING_CONFIG 0x200
102 #define IDE_INTERRUPT 0x300
105 #define IDE_KAUAI_PIO_CONFIG 0x200
106 #define IDE_KAUAI_ULTRA_CONFIG 0x210
107 #define IDE_KAUAI_POLL_CONFIG 0x220
114 #define SYSCLK_TICKS(t) (((t) + IDE_SYSCLK_NS - 1) / IDE_SYSCLK_NS)
115 #define SYSCLK_TICKS_66(t) (((t) + IDE_SYSCLK_66_NS - 1) / IDE_SYSCLK_66_NS)
116 #define IDE_SYSCLK_NS 30
117 #define IDE_SYSCLK_66_NS 15
123 #define TR_133_PIOREG_PIO_MASK 0xff000fff
124 #define TR_133_PIOREG_MDMA_MASK 0x00fff800
125 #define TR_133_UDMAREG_UDMA_MASK 0x0003ffff
126 #define TR_133_UDMAREG_UDMA_EN 0x00000001
144 #define TR_100_PIOREG_PIO_MASK 0xff000fff
145 #define TR_100_PIOREG_MDMA_MASK 0x00fff000
146 #define TR_100_UDMAREG_UDMA_MASK 0x0000ffff
147 #define TR_100_UDMAREG_UDMA_EN 0x00000001
166 #define TR_66_UDMA_MASK 0xfff00000
167 #define TR_66_UDMA_EN 0x00100000
168 #define TR_66_UDMA_ADDRSETUP_MASK 0xe0000000
169 #define TR_66_UDMA_ADDRSETUP_SHIFT 29
170 #define TR_66_UDMA_RDY2PAUS_MASK 0x1e000000
171 #define TR_66_UDMA_RDY2PAUS_SHIFT 25
172 #define TR_66_UDMA_WRDATASETUP_MASK 0x01e00000
173 #define TR_66_UDMA_WRDATASETUP_SHIFT 21
174 #define TR_66_MDMA_MASK 0x000ffc00
175 #define TR_66_MDMA_RECOVERY_MASK 0x000f8000
176 #define TR_66_MDMA_RECOVERY_SHIFT 15
177 #define TR_66_MDMA_ACCESS_MASK 0x00007c00
178 #define TR_66_MDMA_ACCESS_SHIFT 10
179 #define TR_66_PIO_MASK 0x000003ff
180 #define TR_66_PIO_RECOVERY_MASK 0x000003e0
181 #define TR_66_PIO_RECOVERY_SHIFT 5
182 #define TR_66_PIO_ACCESS_MASK 0x0000001f
183 #define TR_66_PIO_ACCESS_SHIFT 0
196 #define TR_33_MDMA_MASK 0x003ff800
197 #define TR_33_MDMA_RECOVERY_MASK 0x001f0000
198 #define TR_33_MDMA_RECOVERY_SHIFT 16
199 #define TR_33_MDMA_ACCESS_MASK 0x0000f800
200 #define TR_33_MDMA_ACCESS_SHIFT 11
201 #define TR_33_MDMA_HALFTICK 0x00200000
202 #define TR_33_PIO_MASK 0x000007ff
203 #define TR_33_PIO_E 0x00000400
204 #define TR_33_PIO_RECOVERY_MASK 0x000003e0
205 #define TR_33_PIO_RECOVERY_SHIFT 5
206 #define TR_33_PIO_ACCESS_MASK 0x0000001f
207 #define TR_33_PIO_ACCESS_SHIFT 0
212 #define IDE_INTR_DMA 0x80000000
213 #define IDE_INTR_DEVICE 0x40000000
218 #define KAUAI_FCR_UATA_MAGIC 0x00000004
219 #define KAUAI_FCR_UATA_RESET_N 0x00000002
220 #define KAUAI_FCR_UATA_ENABLE 0x00000001
295 { 930 , 0x08000fff },
296 { 600 , 0x08000a92 },
297 { 383 , 0x0800060f },
298 { 360 , 0x08000492 },
299 { 330 , 0x0800048f },
300 { 300 , 0x080003cf },
301 { 270 , 0x080003cc },
302 { 240 , 0x0800038b },
303 { 239 , 0x0800030c },
304 { 180 , 0x05000249 },
305 { 120 , 0x04000148 },
311 { 1260 , 0x00fff000 },
312 { 480 , 0x00618000 },
313 { 360 , 0x00492000 },
314 { 270 , 0x0038e000 },
315 { 240 , 0x0030c000 },
316 { 210 , 0x002cb000 },
317 { 180 , 0x00249000 },
318 { 150 , 0x00209000 },
319 { 120 , 0x00148000 },
325 { 120 , 0x000070c0 },
336 { 930 , 0x08000fff },
337 { 600 , 0x0A000c97 },
338 { 383 , 0x07000712 },
339 { 360 , 0x040003cd },
340 { 330 , 0x040003cd },
341 { 300 , 0x040003cd },
342 { 270 , 0x040003cd },
343 { 240 , 0x040003cd },
344 { 239 , 0x040003cd },
345 { 180 , 0x0400028b },
346 { 120 , 0x0400010a },
352 { 1260 , 0x00fff000 },
353 { 480 , 0x00820800 },
354 { 360 , 0x00820800 },
355 { 270 , 0x00820800 },
356 { 240 , 0x00820800 },
357 { 210 , 0x00820800 },
358 { 180 , 0x00820800 },
359 { 150 , 0x0028b000 },
360 { 120 , 0x001ca000 },
366 { 120 , 0x00035901, },
367 { 90 , 0x000348b1, },
368 { 60 , 0x00033881, },
369 { 45 , 0x00033861, },
370 { 30 , 0x00033841, },
371 { 20 , 0x00033031, },
372 { 15 , 0x00033021, },
383 if (cycle_time > table[i+1].cycle_time)
390 #define MAX_DCMDS 256
404 #define IDE_WAKEUP_DELAY (1*HZ)
408 #define PMAC_IDE_REG(x) \
409 ((void __iomem *)((drive)->hwif->io_ports.data_addr + (x)))
416 static void pmac_ide_apply_timings(
ide_drive_t *drive)
434 static void pmac_ide_kauai_apply_timings(
ide_drive_t *drive)
463 pmac_ide_kauai_apply_timings(drive);
465 pmac_ide_apply_timings(drive);
470 pmac_ide_apply_timings(drive);
473 (
void __iomem *)drive->
hwif->io_ports.device_addr);
476 static void pmac_kauai_dev_select(
ide_drive_t *drive)
478 pmac_ide_kauai_apply_timings(drive);
481 (
void __iomem *)drive->
hwif->io_ports.device_addr);
508 unsigned accessTicks, recTicks;
510 unsigned int cycle_time;
518 switch (pmif->
kind) {
521 u32 tr = kauai_lookup_timing(shasta_pio_timings, cycle_time);
528 u32 tr = kauai_lookup_timing(kauai_pio_timings, cycle_time);
535 recTime =
max(recTime, 150
U);
537 accessTime =
max(accessTime, 150
U);
539 accessTicks =
min(accessTicks, 0x1fU);
541 recTicks =
min(recTicks, 0x1fU);
550 recTime =
max(recTime, 150
U);
552 accessTime =
max(accessTime, 150
U);
554 accessTicks =
min(accessTicks, 0x1fU);
555 accessTicks =
max(accessTicks, 4
U);
557 recTicks =
min(recTicks, 0x1fU);
558 recTicks =
max(recTicks, 5
U) - 4;
572 #ifdef IDE_PMAC_DEBUG
574 drive->
name, pio, *timings);
578 pmac_ide_do_update_timings(drive);
585 set_timings_udma_ata4(
u32 *timings,
u8 speed)
587 unsigned rdyToPauseTicks, wrDataSetupTicks, addrTicks;
601 #ifdef IDE_PMAC_DEBUG
602 printk(
KERN_ERR "ide_pmac: Set UDMA timing for mode %d, reg: 0x%08x\n",
603 speed & 0xf, *timings);
613 set_timings_udma_ata6(
u32 *pio_timings,
u32 *ultra_timings,
u8 speed)
620 tr = kauai_lookup_timing(kauai_udma_timings, (
int)t->
udma);
631 set_timings_udma_shasta(
u32 *pio_timings,
u32 *ultra_timings,
u8 speed)
638 tr = kauai_lookup_timing(shasta_udma133_timings, (
int)t->
udma);
653 int cycleTime, accessTime = 0, recTime = 0;
654 unsigned accessTicks, recTicks;
659 switch(speed & 0xf) {
660 case 0: cycleTime = 480;
break;
661 case 1: cycleTime = 150;
break;
662 case 2: cycleTime = 120;
break;
670 cycleTime =
max_t(
int,
id[ATA_ID_EIDE_DMA_TIME], cycleTime);
695 if (tm[i+1].cycleTime < cycleTime)
703 #ifdef IDE_PMAC_DEBUG
704 printk(
KERN_ERR "%s: MDMA, cycleTime: %d, accessTime: %d, recTime: %d\n",
705 drive->
name, cycleTime, accessTime, recTime);
711 u32 tr = kauai_lookup_timing(shasta_mdma_timings, cycleTime);
718 u32 tr = kauai_lookup_timing(kauai_mdma_timings, cycleTime);
726 accessTicks =
min(accessTicks, 0x1fU);
727 accessTicks =
max(accessTicks, 0x1U);
729 recTicks =
min(recTicks, 0x1fU);
730 recTicks =
max(recTicks, 0x3U);
739 accessTicks =
max(accessTicks, 1
U);
740 accessTicks =
min(accessTicks, 0x1fU);
743 recTicks =
max(recTicks, 1
U);
744 recTicks =
min(recTicks, 0x1fU);
753 int origRecTime = recTime;
756 accessTicks =
max(accessTicks, 1
U);
757 accessTicks =
min(accessTicks, 0x1fU);
760 recTicks =
max(recTicks, 2
U) - 1;
761 recTicks =
min(recTicks, 0x1fU);
762 recTime = (recTicks + 1) * IDE_SYSCLK_NS;
763 if ((accessTicks > 1) &&
764 ((accessTime - IDE_SYSCLK_NS/2) >= origAccessTime) &&
765 ((recTime - IDE_SYSCLK_NS/2) >= origRecTime)) {
776 #ifdef IDE_PMAC_DEBUG
778 drive->
name, speed & 0xf, *timings);
787 u32 *timings, *timings2, tl[2];
792 timings2 = &pmif->
timings[unit+2];
800 ret = set_timings_udma_ata4(&tl[0], speed);
803 ret = set_timings_udma_ata6(&tl[0], &tl[1], speed);
805 ret = set_timings_udma_shasta(&tl[0], &tl[1], speed);
809 set_timings_mdma(drive, pmif->
kind, &tl[0], &tl[1], speed);
818 pmac_ide_do_update_timings(drive);
828 unsigned int value, value2 = 0;
873 if (on_media_bay(pmif))
896 if (!on_media_bay(pmif)) {
913 sanitize_timings(pmif);
930 if (cable && !
strncmp(cable,
"80-", 3)) {
933 if (!
strncmp(model,
"PowerBook", 9))
957 if (on_media_bay(pmif)) {
966 static const struct ide_tp_ops pmac_tp_ops = {
967 .exec_command = pmac_exec_command,
970 .write_devctl = pmac_write_devctl,
972 .dev_select = pmac_dev_select,
980 static const struct ide_tp_ops pmac_ata6_tp_ops = {
981 .exec_command = pmac_exec_command,
984 .write_devctl = pmac_write_devctl,
986 .dev_select = pmac_kauai_dev_select,
994 static const struct ide_port_ops pmac_ide_ata4_port_ops = {
995 .init_dev = pmac_ide_init_dev,
996 .set_pio_mode = pmac_ide_set_pio_mode,
997 .set_dma_mode = pmac_ide_set_dma_mode,
998 .cable_detect = pmac_ide_cable_detect,
1002 .init_dev = pmac_ide_init_dev,
1003 .set_pio_mode = pmac_ide_set_pio_mode,
1004 .set_dma_mode = pmac_ide_set_dma_mode,
1011 .init_dma = pmac_ide_init_dma,
1013 .tp_ops = &pmac_tp_ops,
1014 .port_ops = &pmac_ide_port_ops,
1015 .dma_ops = &pmac_dma_ops,
1035 struct ide_hw *hws[] = { hw };
1042 d.
tp_ops = &pmac_ata6_tp_ops;
1043 d.
port_ops = &pmac_ide_ata4_port_ops;
1047 d.
tp_ops = &pmac_ata6_tp_ops;
1048 d.
port_ops = &pmac_ide_ata4_port_ops;
1052 d.
tp_ops = &pmac_ata6_tp_ops;
1053 d.
port_ops = &pmac_ide_ata4_port_ops;
1058 d.
port_ops = &pmac_ide_ata4_port_ops;
1079 sanitize_timings(pmif);
1092 if (on_media_bay(pmif)) {
1101 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, 0, 1);
1112 "bus ID %d%s, irq %d\n", model_name[pmif->
kind],
1114 on_media_bay(pmif) ?
" (mediabay)" :
"", hw->
irq);
1129 static void __devinit pmac_ide_init_ports(
struct ide_hw *hw,
unsigned long base)
1133 for (i = 0; i < 8; ++
i)
1136 hw->
io_ports.ctl_addr = base + 0x160;
1146 unsigned long regbase;
1155 if (macio_resource_count(mdev) == 0) {
1157 mdev->ofdev.dev.of_node->full_name);
1165 "%s!\n", mdev->ofdev.dev.of_node->full_name);
1175 if (macio_irq_count(mdev) == 0) {
1177 "13\n", mdev->ofdev.dev.of_node->full_name);
1180 irq = macio_irq(mdev, 0);
1182 base =
ioremap(macio_resource_start(mdev, 0), 0x400);
1183 regbase = (
unsigned long) base;
1186 pmif->node = mdev->ofdev.dev.of_node;
1187 pmif->regbase = regbase;
1189 pmif->kauai_fcr =
NULL;
1191 if (macio_resource_count(mdev) >= 2) {
1194 "resource for %s!\n",
1195 mdev->ofdev.dev.of_node->full_name);
1197 pmif->dma_regs =
ioremap(macio_resource_start(mdev, 1), 0x1000);
1199 pmif->dma_regs =
NULL;
1203 memset(&hw, 0,
sizeof(hw));
1204 pmac_ide_init_ports(&hw, pmif->regbase);
1206 hw.
dev = &mdev->bus->pdev->dev;
1207 hw.
parent = &mdev->ofdev.dev;
1209 rc = pmac_ide_setup_device(pmif, &hw);
1214 if (pmif->dma_regs) {
1230 pmac_ide_macio_suspend(
struct macio_dev *mdev,
pm_message_t mesg)
1236 if (mesg.
event != mdev->ofdev.dev.power.power_state.event
1238 rc = pmac_ide_do_suspend(pmif);
1240 mdev->ofdev.dev.power.power_state = mesg;
1247 pmac_ide_macio_resume(
struct macio_dev *mdev)
1253 if (mdev->ofdev.dev.power.power_state.event !=
PM_EVENT_ON) {
1254 rc = pmac_ide_do_resume(pmif);
1256 mdev->ofdev.dev.power.power_state =
PMSG_ON;
1271 unsigned long rbase, rlen;
1275 np = pci_device_to_OF_node(pdev);
1277 printk(
KERN_ERR "ide-pmac: cannot find MacIO node for Kauai ATA interface\n");
1287 "%s\n", np->full_name);
1295 "%s\n", np->full_name);
1312 pci_set_drvdata(pdev, pmif);
1314 memset(&hw, 0,
sizeof(hw));
1315 pmac_ide_init_ports(&hw, pmif->
regbase);
1319 rc = pmac_ide_setup_device(pmif, &hw);
1322 pci_set_drvdata(pdev,
NULL);
1341 if (mesg.
event != pdev->
dev.power.power_state.event
1343 rc = pmac_ide_do_suspend(pmif);
1345 pdev->
dev.power.power_state = mesg;
1352 pmac_ide_pci_resume(
struct pci_dev *pdev)
1358 rc = pmac_ide_do_resume(pmif);
1366 #ifdef CONFIG_PMAC_MEDIABAY
1367 static void pmac_ide_macio_mb_event(
struct macio_dev* mdev,
int mb_state)
1402 static struct macio_driver pmac_ide_macio_driver =
1407 .of_match_table = pmac_ide_macio_match,
1409 .probe = pmac_ide_macio_attach,
1410 .suspend = pmac_ide_macio_suspend,
1411 .resume = pmac_ide_macio_resume,
1412 #ifdef CONFIG_PMAC_MEDIABAY
1413 .mediabay_event = pmac_ide_macio_mb_event,
1426 static struct pci_driver pmac_ide_pci_driver = {
1428 .id_table = pmac_ide_pci_match,
1429 .probe = pmac_ide_pci_attach,
1430 .suspend = pmac_ide_pci_suspend,
1431 .resume = pmac_ide_pci_resume,
1439 if (!machine_is(powermac))
1442 #ifdef CONFIG_BLK_DEV_IDE_PMAC_ATA100FIRST
1443 error = pci_register_driver(&pmac_ide_pci_driver);
1455 error = pci_register_driver(&pmac_ide_pci_driver);
1474 struct dbdma_cmd *
table;
1500 "switching to PIO on Ohare chipset\n", drive->
name);
1506 unsigned int tc = (cur_len < 0xfe00)? cur_len: 0xfe00;
1513 st_le16(&table->command, wr? OUTPUT_MORE: INPUT_MORE);
1514 st_le16(&table->req_count, tc);
1515 st_le32(&table->phy_addr, cur_addr);
1517 table->xfer_status = 0;
1518 table->res_count = 0;
1529 st_le16(&table[-1].
command, wr? OUTPUT_LAST: INPUT_LAST);
1531 memset(table, 0,
sizeof(
struct dbdma_cmd));
1532 st_le16(&table->command, DBDMA_STOP);
1555 if (pmac_ide_build_dmatable(drive, cmd) == 0)
1599 dstat =
readl(&dma->status);
1639 status =
readl(&dma->status);
1654 status =
readl(&dma->status);
1655 if ((status &
FLUSH) == 0)
1657 if (++timeout > 100) {
1666 static void pmac_ide_dma_host_set(
ide_drive_t *drive,
int on)
1677 unsigned long status =
readl(&dma->status);
1679 printk(
KERN_ERR "ide-pmac lost interrupt, dma status: %lx\n", status);
1683 .dma_host_set = pmac_ide_dma_host_set,
1684 .dma_setup = pmac_ide_dma_setup,
1685 .dma_start = pmac_ide_dma_start,
1686 .dma_end = pmac_ide_dma_end,
1687 .dma_test_irq = pmac_ide_dma_test_irq,
1688 .dma_lost_irq = pmac_ide_dma_lost_irq,
1714 (
MAX_DCMDS + 2) *
sizeof(
struct dbdma_cmd),