aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ata
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-02-24 20:32:15 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-02-24 20:32:15 -0500
commitd9978ec5680059d727b39d6c706777c6973587f2 (patch)
tree8fad9cf8cf9599af58757e4acbf27c602df9ed79 /drivers/ata
parenta883b70d8e0a88278c0a1f80753b4dc99962b541 (diff)
parent53637e0760d56274177be7e6d630490bc49766c1 (diff)
Merge tag 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzik/libata-dev
Pull libata updates from Jeff Garzik: 1) apply, and then revert, the sysfs export of ATA host controller number. Discussion was continuing after patch application, trying to figure out how to best mesh exported data with the installers, boot-time agents and other parties that want this info. 2) Merge Zero-Power Optical Device Driver (ZPODD) support, bringing the wonderfulness of sane power management to your CD/DVD device. Includes one SCSI-subsystem patch (with appropriate ACKs), adding runtime PM support to 'sr' driver. That is the ZPODD interaction bits. Patchset went through some 13 revisions before it got here; kudos to Intel for persistence. 3) pata_samsung_cf: use devm_clk_get() 4) more ata_piix, ahci PCI IDs 5) Add SATA driver for R-Car SoC 6) Convert libata to use devm_ioremap_resource (Note: I think Greg sent this to you, also) 7) Set proper Sense Key (SK) in the SCSI simulator when ATA passthrough indicates check condition. Google and specification hawks everywhere shall rejoice. * tag 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzik/libata-dev: (22 commits) [libata] fix smatch warning for zpodd_wake_dev [libata] Set proper SK when CK_COND is set. [libata] Convert to devm_ioremap_resource() libata: add R-Car SATA driver ahci: Add Device IDs for Intel Wellsburg PCH ata_piix: Add Device IDs for Intel Wellsburg PCH [SCSI] remove can_power_off flag from scsi_device [libata] scsi: no poll when ODD is powered off [SCSI] sr: support runtime pm ahci: AHCI-mode SATA patch for Intel Avoton DeviceIDs ata_piix: IDE-mode SATA patch for Intel Avoton DeviceIDs [libata] PM code cleanup for ata port [libata] pm: differentiate system and runtime pm for ata port Revert "libata: export host controller number thru /sys" libata: do not suspend port if normal ODD is attached libata: expose pm qos flags for ata device libata: handle power transition of ODD libata: check zero power ready status for ZPODD libata: move acpi notification code to zpodd libata: identify and init ZPODD devices ...
Diffstat (limited to 'drivers/ata')
-rw-r--r--drivers/ata/Kconfig21
-rw-r--r--drivers/ata/Makefile2
-rw-r--r--drivers/ata/ahci.c24
-rw-r--r--drivers/ata/ata_piix.c17
-rw-r--r--drivers/ata/libata-acpi.c177
-rw-r--r--drivers/ata/libata-core.c66
-rw-r--r--drivers/ata/libata-eh.c27
-rw-r--r--drivers/ata/libata-scsi.c20
-rw-r--r--drivers/ata/libata-zpodd.c299
-rw-r--r--drivers/ata/libata.h27
-rw-r--r--drivers/ata/pata_samsung_cf.c4
-rw-r--r--drivers/ata/sata_rcar.c910
12 files changed, 1460 insertions, 134 deletions
diff --git a/drivers/ata/Kconfig b/drivers/ata/Kconfig
index 4cfb7200260d..3e751b74615e 100644
--- a/drivers/ata/Kconfig
+++ b/drivers/ata/Kconfig
@@ -58,6 +58,19 @@ config ATA_ACPI
58 You can disable this at kernel boot time by using the 58 You can disable this at kernel boot time by using the
59 option libata.noacpi=1 59 option libata.noacpi=1
60 60
61config SATA_ZPODD
62 bool "SATA Zero Power ODD Support"
63 depends on ATA_ACPI
64 default n
65 help
66 This option adds support for SATA ZPODD. It requires both
67 ODD and the platform support, and if enabled, will automatically
68 power on/off the ODD when certain condition is satisfied. This
69 does not impact user's experience of the ODD, only power is saved
70 when ODD is not in use(i.e. no disc inside).
71
72 If unsure, say N.
73
61config SATA_PMP 74config SATA_PMP
62 bool "SATA Port Multiplier support" 75 bool "SATA Port Multiplier support"
63 default y 76 default y
@@ -247,6 +260,14 @@ config SATA_PROMISE
247 260
248 If unsure, say N. 261 If unsure, say N.
249 262
263config SATA_RCAR
264 tristate "Renesas R-Car SATA support"
265 depends on ARCH_SHMOBILE && ARCH_R8A7779
266 help
267 This option enables support for Renesas R-Car Serial ATA.
268
269 If unsure, say N.
270
250config SATA_SIL 271config SATA_SIL
251 tristate "Silicon Image SATA support" 272 tristate "Silicon Image SATA support"
252 depends on PCI 273 depends on PCI
diff --git a/drivers/ata/Makefile b/drivers/ata/Makefile
index 9329dafba91b..c04d0fd038a3 100644
--- a/drivers/ata/Makefile
+++ b/drivers/ata/Makefile
@@ -23,6 +23,7 @@ obj-$(CONFIG_ATA_PIIX) += ata_piix.o
23obj-$(CONFIG_SATA_MV) += sata_mv.o 23obj-$(CONFIG_SATA_MV) += sata_mv.o
24obj-$(CONFIG_SATA_NV) += sata_nv.o 24obj-$(CONFIG_SATA_NV) += sata_nv.o
25obj-$(CONFIG_SATA_PROMISE) += sata_promise.o 25obj-$(CONFIG_SATA_PROMISE) += sata_promise.o
26obj-$(CONFIG_SATA_RCAR) += sata_rcar.o
26obj-$(CONFIG_SATA_SIL) += sata_sil.o 27obj-$(CONFIG_SATA_SIL) += sata_sil.o
27obj-$(CONFIG_SATA_SIS) += sata_sis.o 28obj-$(CONFIG_SATA_SIS) += sata_sis.o
28obj-$(CONFIG_SATA_SVW) += sata_svw.o 29obj-$(CONFIG_SATA_SVW) += sata_svw.o
@@ -107,3 +108,4 @@ libata-y := libata-core.o libata-scsi.o libata-eh.o libata-transport.o
107libata-$(CONFIG_ATA_SFF) += libata-sff.o 108libata-$(CONFIG_ATA_SFF) += libata-sff.o
108libata-$(CONFIG_SATA_PMP) += libata-pmp.o 109libata-$(CONFIG_SATA_PMP) += libata-pmp.o
109libata-$(CONFIG_ATA_ACPI) += libata-acpi.o 110libata-$(CONFIG_ATA_ACPI) += libata-acpi.o
111libata-$(CONFIG_SATA_ZPODD) += libata-zpodd.o
diff --git a/drivers/ata/ahci.c b/drivers/ata/ahci.c
index 495aeed26779..a99112cfd8b1 100644
--- a/drivers/ata/ahci.c
+++ b/drivers/ata/ahci.c
@@ -265,6 +265,30 @@ static const struct pci_device_id ahci_pci_tbl[] = {
265 { PCI_VDEVICE(INTEL, 0x9c07), board_ahci }, /* Lynx Point-LP RAID */ 265 { PCI_VDEVICE(INTEL, 0x9c07), board_ahci }, /* Lynx Point-LP RAID */
266 { PCI_VDEVICE(INTEL, 0x9c0e), board_ahci }, /* Lynx Point-LP RAID */ 266 { PCI_VDEVICE(INTEL, 0x9c0e), board_ahci }, /* Lynx Point-LP RAID */
267 { PCI_VDEVICE(INTEL, 0x9c0f), board_ahci }, /* Lynx Point-LP RAID */ 267 { PCI_VDEVICE(INTEL, 0x9c0f), board_ahci }, /* Lynx Point-LP RAID */
268 { PCI_VDEVICE(INTEL, 0x1f22), board_ahci }, /* Avoton AHCI */
269 { PCI_VDEVICE(INTEL, 0x1f23), board_ahci }, /* Avoton AHCI */
270 { PCI_VDEVICE(INTEL, 0x1f24), board_ahci }, /* Avoton RAID */
271 { PCI_VDEVICE(INTEL, 0x1f25), board_ahci }, /* Avoton RAID */
272 { PCI_VDEVICE(INTEL, 0x1f26), board_ahci }, /* Avoton RAID */
273 { PCI_VDEVICE(INTEL, 0x1f27), board_ahci }, /* Avoton RAID */
274 { PCI_VDEVICE(INTEL, 0x1f2e), board_ahci }, /* Avoton RAID */
275 { PCI_VDEVICE(INTEL, 0x1f2f), board_ahci }, /* Avoton RAID */
276 { PCI_VDEVICE(INTEL, 0x1f32), board_ahci }, /* Avoton AHCI */
277 { PCI_VDEVICE(INTEL, 0x1f33), board_ahci }, /* Avoton AHCI */
278 { PCI_VDEVICE(INTEL, 0x1f34), board_ahci }, /* Avoton RAID */
279 { PCI_VDEVICE(INTEL, 0x1f35), board_ahci }, /* Avoton RAID */
280 { PCI_VDEVICE(INTEL, 0x1f36), board_ahci }, /* Avoton RAID */
281 { PCI_VDEVICE(INTEL, 0x1f37), board_ahci }, /* Avoton RAID */
282 { PCI_VDEVICE(INTEL, 0x1f3e), board_ahci }, /* Avoton RAID */
283 { PCI_VDEVICE(INTEL, 0x1f3f), board_ahci }, /* Avoton RAID */
284 { PCI_VDEVICE(INTEL, 0x8d02), board_ahci }, /* Wellsburg AHCI */
285 { PCI_VDEVICE(INTEL, 0x8d04), board_ahci }, /* Wellsburg RAID */
286 { PCI_VDEVICE(INTEL, 0x8d06), board_ahci }, /* Wellsburg RAID */
287 { PCI_VDEVICE(INTEL, 0x8d0e), board_ahci }, /* Wellsburg RAID */
288 { PCI_VDEVICE(INTEL, 0x8d62), board_ahci }, /* Wellsburg AHCI */
289 { PCI_VDEVICE(INTEL, 0x8d64), board_ahci }, /* Wellsburg RAID */
290 { PCI_VDEVICE(INTEL, 0x8d66), board_ahci }, /* Wellsburg RAID */
291 { PCI_VDEVICE(INTEL, 0x8d6e), board_ahci }, /* Wellsburg RAID */
268 292
269 /* JMicron 360/1/3/5/6, match class to avoid IDE function */ 293 /* JMicron 360/1/3/5/6, match class to avoid IDE function */
270 { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 294 { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
diff --git a/drivers/ata/ata_piix.c b/drivers/ata/ata_piix.c
index 174eca609b42..d2ba439cfe54 100644
--- a/drivers/ata/ata_piix.c
+++ b/drivers/ata/ata_piix.c
@@ -317,6 +317,23 @@ static const struct pci_device_id piix_pci_tbl[] = {
317 { 0x8086, 0x9c09, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata }, 317 { 0x8086, 0x9c09, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
318 /* SATA Controller IDE (DH89xxCC) */ 318 /* SATA Controller IDE (DH89xxCC) */
319 { 0x8086, 0x2326, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata }, 319 { 0x8086, 0x2326, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
320 /* SATA Controller IDE (Avoton) */
321 { 0x8086, 0x1f20, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
322 /* SATA Controller IDE (Avoton) */
323 { 0x8086, 0x1f21, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
324 /* SATA Controller IDE (Avoton) */
325 { 0x8086, 0x1f30, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
326 /* SATA Controller IDE (Avoton) */
327 { 0x8086, 0x1f31, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
328 /* SATA Controller IDE (Wellsburg) */
329 { 0x8086, 0x8d00, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
330 /* SATA Controller IDE (Wellsburg) */
331 { 0x8086, 0x8d08, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
332 /* SATA Controller IDE (Wellsburg) */
333 { 0x8086, 0x8d60, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_sata_snb },
334 /* SATA Controller IDE (Wellsburg) */
335 { 0x8086, 0x8d68, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich8_2port_sata },
336
320 { } /* terminate list */ 337 { } /* terminate list */
321}; 338};
322 339
diff --git a/drivers/ata/libata-acpi.c b/drivers/ata/libata-acpi.c
index 6fc67f7efb22..0ea1018280bd 100644
--- a/drivers/ata/libata-acpi.c
+++ b/drivers/ata/libata-acpi.c
@@ -17,6 +17,7 @@
17#include <linux/pci.h> 17#include <linux/pci.h>
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/pm_runtime.h> 19#include <linux/pm_runtime.h>
20#include <linux/pm_qos.h>
20#include <scsi/scsi_device.h> 21#include <scsi/scsi_device.h>
21#include "libata.h" 22#include "libata.h"
22 23
@@ -835,50 +836,95 @@ void ata_acpi_on_resume(struct ata_port *ap)
835 } 836 }
836} 837}
837 838
838/** 839static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
839 * ata_acpi_set_state - set the port power state 840{
840 * @ap: target ATA port 841 int d_max_in = ACPI_STATE_D3_COLD;
841 * @state: state, on/off 842 if (!runtime)
842 * 843 goto out;
843 * This function executes the _PS0/_PS3 ACPI method to set the power state. 844
844 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off 845 /*
845 */ 846 * For ATAPI, runtime D3 cold is only allowed
846void ata_acpi_set_state(struct ata_port *ap, pm_message_t state) 847 * for ZPODD in zero power ready state
848 */
849 if (dev->class == ATA_DEV_ATAPI &&
850 !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
851 d_max_in = ACPI_STATE_D3_HOT;
852
853out:
854 return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev,
855 NULL, d_max_in);
856}
857
858static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
847{ 859{
860 bool runtime = PMSG_IS_AUTO(state);
848 struct ata_device *dev; 861 struct ata_device *dev;
849 acpi_handle handle; 862 acpi_handle handle;
850 int acpi_state; 863 int acpi_state;
851 864
852 /* channel first and then drives for power on and vica versa
853 for power off */
854 handle = ata_ap_acpi_handle(ap);
855 if (handle && state.event == PM_EVENT_ON)
856 acpi_bus_set_power(handle, ACPI_STATE_D0);
857
858 ata_for_each_dev(dev, &ap->link, ENABLED) { 865 ata_for_each_dev(dev, &ap->link, ENABLED) {
859 handle = ata_dev_acpi_handle(dev); 866 handle = ata_dev_acpi_handle(dev);
860 if (!handle) 867 if (!handle)
861 continue; 868 continue;
862 869
863 if (state.event != PM_EVENT_ON) { 870 if (!(state.event & PM_EVENT_RESUME)) {
864 acpi_state = acpi_pm_device_sleep_state( 871 acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
865 &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3); 872 if (acpi_state == ACPI_STATE_D0)
866 if (acpi_state > 0) 873 continue;
867 acpi_bus_set_power(handle, acpi_state); 874 if (runtime && zpodd_dev_enabled(dev) &&
868 /* TBD: need to check if it's runtime pm request */ 875 acpi_state == ACPI_STATE_D3_COLD)
869 acpi_pm_device_run_wake( 876 zpodd_enable_run_wake(dev);
870 &dev->sdev->sdev_gendev, true); 877 acpi_bus_set_power(handle, acpi_state);
871 } else { 878 } else {
872 /* Ditto */ 879 if (runtime && zpodd_dev_enabled(dev))
873 acpi_pm_device_run_wake( 880 zpodd_disable_run_wake(dev);
874 &dev->sdev->sdev_gendev, false);
875 acpi_bus_set_power(handle, ACPI_STATE_D0); 881 acpi_bus_set_power(handle, ACPI_STATE_D0);
876 } 882 }
877 } 883 }
884}
878 885
879 handle = ata_ap_acpi_handle(ap); 886/* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
880 if (handle && state.event != PM_EVENT_ON) 887static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
881 acpi_bus_set_power(handle, ACPI_STATE_D3); 888{
889 struct ata_device *dev;
890 acpi_handle port_handle;
891
892 port_handle = ata_ap_acpi_handle(ap);
893 if (!port_handle)
894 return;
895
896 /* channel first and then drives for power on and vica versa
897 for power off */
898 if (state.event & PM_EVENT_RESUME)
899 acpi_bus_set_power(port_handle, ACPI_STATE_D0);
900
901 ata_for_each_dev(dev, &ap->link, ENABLED) {
902 acpi_handle dev_handle = ata_dev_acpi_handle(dev);
903 if (!dev_handle)
904 continue;
905
906 acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
907 ACPI_STATE_D0 : ACPI_STATE_D3);
908 }
909
910 if (!(state.event & PM_EVENT_RESUME))
911 acpi_bus_set_power(port_handle, ACPI_STATE_D3);
912}
913
914/**
915 * ata_acpi_set_state - set the port power state
916 * @ap: target ATA port
917 * @state: state, on/off
918 *
919 * This function sets a proper ACPI D state for the device on
920 * system and runtime PM operations.
921 */
922void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
923{
924 if (ap->flags & ATA_FLAG_ACPI_SATA)
925 sata_acpi_set_state(ap, state);
926 else
927 pata_acpi_set_state(ap, state);
882} 928}
883 929
884/** 930/**
@@ -974,57 +1020,6 @@ void ata_acpi_on_disable(struct ata_device *dev)
974 ata_acpi_clear_gtf(dev); 1020 ata_acpi_clear_gtf(dev);
975} 1021}
976 1022
977static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
978{
979 struct ata_device *ata_dev = context;
980
981 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
982 pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
983 scsi_autopm_get_device(ata_dev->sdev);
984}
985
986static void ata_acpi_add_pm_notifier(struct ata_device *dev)
987{
988 struct acpi_device *acpi_dev;
989 acpi_handle handle;
990 acpi_status status;
991
992 handle = ata_dev_acpi_handle(dev);
993 if (!handle)
994 return;
995
996 status = acpi_bus_get_device(handle, &acpi_dev);
997 if (ACPI_FAILURE(status))
998 return;
999
1000 if (dev->sdev->can_power_off) {
1001 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1002 ata_acpi_wake_dev, dev);
1003 device_set_run_wake(&dev->sdev->sdev_gendev, true);
1004 }
1005}
1006
1007static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
1008{
1009 struct acpi_device *acpi_dev;
1010 acpi_handle handle;
1011 acpi_status status;
1012
1013 handle = ata_dev_acpi_handle(dev);
1014 if (!handle)
1015 return;
1016
1017 status = acpi_bus_get_device(handle, &acpi_dev);
1018 if (ACPI_FAILURE(status))
1019 return;
1020
1021 if (dev->sdev->can_power_off) {
1022 device_set_run_wake(&dev->sdev->sdev_gendev, false);
1023 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1024 ata_acpi_wake_dev);
1025 }
1026}
1027
1028static void ata_acpi_register_power_resource(struct ata_device *dev) 1023static void ata_acpi_register_power_resource(struct ata_device *dev)
1029{ 1024{
1030 struct scsi_device *sdev = dev->sdev; 1025 struct scsi_device *sdev = dev->sdev;
@@ -1047,13 +1042,13 @@ static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1047 1042
1048void ata_acpi_bind(struct ata_device *dev) 1043void ata_acpi_bind(struct ata_device *dev)
1049{ 1044{
1050 ata_acpi_add_pm_notifier(dev);
1051 ata_acpi_register_power_resource(dev); 1045 ata_acpi_register_power_resource(dev);
1046 if (zpodd_dev_enabled(dev))
1047 dev_pm_qos_expose_flags(&dev->sdev->sdev_gendev, 0);
1052} 1048}
1053 1049
1054void ata_acpi_unbind(struct ata_device *dev) 1050void ata_acpi_unbind(struct ata_device *dev)
1055{ 1051{
1056 ata_acpi_remove_pm_notifier(dev);
1057 ata_acpi_unregister_power_resource(dev); 1052 ata_acpi_unregister_power_resource(dev);
1058} 1053}
1059 1054
@@ -1095,9 +1090,6 @@ static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1095 acpi_handle *handle) 1090 acpi_handle *handle)
1096{ 1091{
1097 struct ata_device *ata_dev; 1092 struct ata_device *ata_dev;
1098 acpi_status status;
1099 struct acpi_device *acpi_dev;
1100 struct acpi_device_power_state *states;
1101 1093
1102 if (ap->flags & ATA_FLAG_ACPI_SATA) { 1094 if (ap->flags & ATA_FLAG_ACPI_SATA) {
1103 if (!sata_pmp_attached(ap)) 1095 if (!sata_pmp_attached(ap))
@@ -1114,21 +1106,6 @@ static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1114 if (!*handle) 1106 if (!*handle)
1115 return -ENODEV; 1107 return -ENODEV;
1116 1108
1117 status = acpi_bus_get_device(*handle, &acpi_dev);
1118 if (ACPI_FAILURE(status))
1119 return 0;
1120
1121 /*
1122 * If firmware has _PS3 or _PR3 for this device,
1123 * and this ata ODD device support device attention,
1124 * it means this device can be powered off
1125 */
1126 states = acpi_dev->power.states;
1127 if ((states[ACPI_STATE_D3_HOT].flags.valid ||
1128 states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
1129 ata_dev->flags & ATA_DFLAG_DA)
1130 sdev->can_power_off = 1;
1131
1132 return 0; 1109 return 0;
1133} 1110}
1134 1111
diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index 46cd3f4c6aaa..497adea1f0d6 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -2400,8 +2400,10 @@ int ata_dev_configure(struct ata_device *dev)
2400 dma_dir_string = ", DMADIR"; 2400 dma_dir_string = ", DMADIR";
2401 } 2401 }
2402 2402
2403 if (ata_id_has_da(dev->id)) 2403 if (ata_id_has_da(dev->id)) {
2404 dev->flags |= ATA_DFLAG_DA; 2404 dev->flags |= ATA_DFLAG_DA;
2405 zpodd_init(dev);
2406 }
2405 2407
2406 /* print device info to dmesg */ 2408 /* print device info to dmesg */
2407 if (ata_msg_drv(ap) && print_info) 2409 if (ata_msg_drv(ap) && print_info)
@@ -5331,9 +5333,6 @@ static int ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5331 5333
5332static int __ata_port_suspend_common(struct ata_port *ap, pm_message_t mesg, int *async) 5334static int __ata_port_suspend_common(struct ata_port *ap, pm_message_t mesg, int *async)
5333{ 5335{
5334 unsigned int ehi_flags = ATA_EHI_QUIET;
5335 int rc;
5336
5337 /* 5336 /*
5338 * On some hardware, device fails to respond after spun down 5337 * On some hardware, device fails to respond after spun down
5339 * for suspend. As the device won't be used before being 5338 * for suspend. As the device won't be used before being
@@ -5342,11 +5341,9 @@ static int __ata_port_suspend_common(struct ata_port *ap, pm_message_t mesg, int
5342 * 5341 *
5343 * http://thread.gmane.org/gmane.linux.ide/46764 5342 * http://thread.gmane.org/gmane.linux.ide/46764
5344 */ 5343 */
5345 if (mesg.event == PM_EVENT_SUSPEND) 5344 unsigned int ehi_flags = ATA_EHI_QUIET | ATA_EHI_NO_AUTOPSY |
5346 ehi_flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_NO_RECOVERY; 5345 ATA_EHI_NO_RECOVERY;
5347 5346 return ata_port_request_pm(ap, mesg, 0, ehi_flags, async);
5348 rc = ata_port_request_pm(ap, mesg, 0, ehi_flags, async);
5349 return rc;
5350} 5347}
5351 5348
5352static int ata_port_suspend_common(struct device *dev, pm_message_t mesg) 5349static int ata_port_suspend_common(struct device *dev, pm_message_t mesg)
@@ -5367,40 +5364,38 @@ static int ata_port_suspend(struct device *dev)
5367static int ata_port_do_freeze(struct device *dev) 5364static int ata_port_do_freeze(struct device *dev)
5368{ 5365{
5369 if (pm_runtime_suspended(dev)) 5366 if (pm_runtime_suspended(dev))
5370 pm_runtime_resume(dev); 5367 return 0;
5371 5368
5372 return ata_port_suspend_common(dev, PMSG_FREEZE); 5369 return ata_port_suspend_common(dev, PMSG_FREEZE);
5373} 5370}
5374 5371
5375static int ata_port_poweroff(struct device *dev) 5372static int ata_port_poweroff(struct device *dev)
5376{ 5373{
5377 if (pm_runtime_suspended(dev))
5378 return 0;
5379
5380 return ata_port_suspend_common(dev, PMSG_HIBERNATE); 5374 return ata_port_suspend_common(dev, PMSG_HIBERNATE);
5381} 5375}
5382 5376
5383static int __ata_port_resume_common(struct ata_port *ap, int *async) 5377static int __ata_port_resume_common(struct ata_port *ap, pm_message_t mesg,
5378 int *async)
5384{ 5379{
5385 int rc; 5380 int rc;
5386 5381
5387 rc = ata_port_request_pm(ap, PMSG_ON, ATA_EH_RESET, 5382 rc = ata_port_request_pm(ap, mesg, ATA_EH_RESET,
5388 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, async); 5383 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, async);
5389 return rc; 5384 return rc;
5390} 5385}
5391 5386
5392static int ata_port_resume_common(struct device *dev) 5387static int ata_port_resume_common(struct device *dev, pm_message_t mesg)
5393{ 5388{
5394 struct ata_port *ap = to_ata_port(dev); 5389 struct ata_port *ap = to_ata_port(dev);
5395 5390
5396 return __ata_port_resume_common(ap, NULL); 5391 return __ata_port_resume_common(ap, mesg, NULL);
5397} 5392}
5398 5393
5399static int ata_port_resume(struct device *dev) 5394static int ata_port_resume(struct device *dev)
5400{ 5395{
5401 int rc; 5396 int rc;
5402 5397
5403 rc = ata_port_resume_common(dev); 5398 rc = ata_port_resume_common(dev, PMSG_RESUME);
5404 if (!rc) { 5399 if (!rc) {
5405 pm_runtime_disable(dev); 5400 pm_runtime_disable(dev);
5406 pm_runtime_set_active(dev); 5401 pm_runtime_set_active(dev);
@@ -5410,11 +5405,40 @@ static int ata_port_resume(struct device *dev)
5410 return rc; 5405 return rc;
5411} 5406}
5412 5407
5408/*
5409 * For ODDs, the upper layer will poll for media change every few seconds,
5410 * which will make it enter and leave suspend state every few seconds. And
5411 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5412 * is very little and the ODD may malfunction after constantly being reset.
5413 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5414 * ODD is attached to the port.
5415 */
5413static int ata_port_runtime_idle(struct device *dev) 5416static int ata_port_runtime_idle(struct device *dev)
5414{ 5417{
5418 struct ata_port *ap = to_ata_port(dev);
5419 struct ata_link *link;
5420 struct ata_device *adev;
5421
5422 ata_for_each_link(link, ap, HOST_FIRST) {
5423 ata_for_each_dev(adev, link, ENABLED)
5424 if (adev->class == ATA_DEV_ATAPI &&
5425 !zpodd_dev_enabled(adev))
5426 return -EBUSY;
5427 }
5428
5415 return pm_runtime_suspend(dev); 5429 return pm_runtime_suspend(dev);
5416} 5430}
5417 5431
5432static int ata_port_runtime_suspend(struct device *dev)
5433{
5434 return ata_port_suspend_common(dev, PMSG_AUTO_SUSPEND);
5435}
5436
5437static int ata_port_runtime_resume(struct device *dev)
5438{
5439 return ata_port_resume_common(dev, PMSG_AUTO_RESUME);
5440}
5441
5418static const struct dev_pm_ops ata_port_pm_ops = { 5442static const struct dev_pm_ops ata_port_pm_ops = {
5419 .suspend = ata_port_suspend, 5443 .suspend = ata_port_suspend,
5420 .resume = ata_port_resume, 5444 .resume = ata_port_resume,
@@ -5423,8 +5447,8 @@ static const struct dev_pm_ops ata_port_pm_ops = {
5423 .poweroff = ata_port_poweroff, 5447 .poweroff = ata_port_poweroff,
5424 .restore = ata_port_resume, 5448 .restore = ata_port_resume,
5425 5449
5426 .runtime_suspend = ata_port_suspend, 5450 .runtime_suspend = ata_port_runtime_suspend,
5427 .runtime_resume = ata_port_resume_common, 5451 .runtime_resume = ata_port_runtime_resume,
5428 .runtime_idle = ata_port_runtime_idle, 5452 .runtime_idle = ata_port_runtime_idle,
5429}; 5453};
5430 5454
@@ -5441,7 +5465,7 @@ EXPORT_SYMBOL_GPL(ata_sas_port_async_suspend);
5441 5465
5442int ata_sas_port_async_resume(struct ata_port *ap, int *async) 5466int ata_sas_port_async_resume(struct ata_port *ap, int *async)
5443{ 5467{
5444 return __ata_port_resume_common(ap, async); 5468 return __ata_port_resume_common(ap, PMSG_RESUME, async);
5445} 5469}
5446EXPORT_SYMBOL_GPL(ata_sas_port_async_resume); 5470EXPORT_SYMBOL_GPL(ata_sas_port_async_resume);
5447 5471
diff --git a/drivers/ata/libata-eh.c b/drivers/ata/libata-eh.c
index bcf4437214f5..f9476fb3ac43 100644
--- a/drivers/ata/libata-eh.c
+++ b/drivers/ata/libata-eh.c
@@ -1591,7 +1591,7 @@ static int ata_eh_read_log_10h(struct ata_device *dev,
1591 * RETURNS: 1591 * RETURNS:
1592 * 0 on success, AC_ERR_* mask on failure. 1592 * 0 on success, AC_ERR_* mask on failure.
1593 */ 1593 */
1594static unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key) 1594unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1595{ 1595{
1596 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 }; 1596 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1597 struct ata_taskfile tf; 1597 struct ata_taskfile tf;
@@ -1624,7 +1624,7 @@ static unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1624 * RETURNS: 1624 * RETURNS:
1625 * 0 on success, AC_ERR_* mask on failure 1625 * 0 on success, AC_ERR_* mask on failure
1626 */ 1626 */
1627static unsigned int atapi_eh_request_sense(struct ata_device *dev, 1627unsigned int atapi_eh_request_sense(struct ata_device *dev,
1628 u8 *sense_buf, u8 dfl_sense_key) 1628 u8 *sense_buf, u8 dfl_sense_key)
1629{ 1629{
1630 u8 cdb[ATAPI_CDB_LEN] = 1630 u8 cdb[ATAPI_CDB_LEN] =
@@ -3857,6 +3857,8 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3857 rc = atapi_eh_clear_ua(dev); 3857 rc = atapi_eh_clear_ua(dev);
3858 if (rc) 3858 if (rc)
3859 goto rest_fail; 3859 goto rest_fail;
3860 if (zpodd_dev_enabled(dev))
3861 zpodd_post_poweron(dev);
3860 } 3862 }
3861 } 3863 }
3862 3864
@@ -4022,11 +4024,12 @@ static void ata_eh_handle_port_suspend(struct ata_port *ap)
4022{ 4024{
4023 unsigned long flags; 4025 unsigned long flags;
4024 int rc = 0; 4026 int rc = 0;
4027 struct ata_device *dev;
4025 4028
4026 /* are we suspending? */ 4029 /* are we suspending? */
4027 spin_lock_irqsave(ap->lock, flags); 4030 spin_lock_irqsave(ap->lock, flags);
4028 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 4031 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4029 ap->pm_mesg.event == PM_EVENT_ON) { 4032 ap->pm_mesg.event & PM_EVENT_RESUME) {
4030 spin_unlock_irqrestore(ap->lock, flags); 4033 spin_unlock_irqrestore(ap->lock, flags);
4031 return; 4034 return;
4032 } 4035 }
@@ -4034,6 +4037,18 @@ static void ata_eh_handle_port_suspend(struct ata_port *ap)
4034 4037
4035 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED); 4038 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
4036 4039
4040 /*
4041 * If we have a ZPODD attached, check its zero
4042 * power ready status before the port is frozen.
4043 * Only needed for runtime suspend.
4044 */
4045 if (PMSG_IS_AUTO(ap->pm_mesg)) {
4046 ata_for_each_dev(dev, &ap->link, ENABLED) {
4047 if (zpodd_dev_enabled(dev))
4048 zpodd_on_suspend(dev);
4049 }
4050 }
4051
4037 /* tell ACPI we're suspending */ 4052 /* tell ACPI we're suspending */
4038 rc = ata_acpi_on_suspend(ap); 4053 rc = ata_acpi_on_suspend(ap);
4039 if (rc) 4054 if (rc)
@@ -4045,7 +4060,7 @@ static void ata_eh_handle_port_suspend(struct ata_port *ap)
4045 if (ap->ops->port_suspend) 4060 if (ap->ops->port_suspend)
4046 rc = ap->ops->port_suspend(ap, ap->pm_mesg); 4061 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
4047 4062
4048 ata_acpi_set_state(ap, PMSG_SUSPEND); 4063 ata_acpi_set_state(ap, ap->pm_mesg);
4049 out: 4064 out:
4050 /* report result */ 4065 /* report result */
4051 spin_lock_irqsave(ap->lock, flags); 4066 spin_lock_irqsave(ap->lock, flags);
@@ -4085,7 +4100,7 @@ static void ata_eh_handle_port_resume(struct ata_port *ap)
4085 /* are we resuming? */ 4100 /* are we resuming? */
4086 spin_lock_irqsave(ap->lock, flags); 4101 spin_lock_irqsave(ap->lock, flags);
4087 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 4102 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4088 ap->pm_mesg.event != PM_EVENT_ON) { 4103 !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
4089 spin_unlock_irqrestore(ap->lock, flags); 4104 spin_unlock_irqrestore(ap->lock, flags);
4090 return; 4105 return;
4091 } 4106 }
@@ -4104,7 +4119,7 @@ static void ata_eh_handle_port_resume(struct ata_port *ap)
4104 ata_for_each_dev(dev, link, ALL) 4119 ata_for_each_dev(dev, link, ALL)
4105 ata_ering_clear(&dev->ering); 4120 ata_ering_clear(&dev->ering);
4106 4121
4107 ata_acpi_set_state(ap, PMSG_ON); 4122 ata_acpi_set_state(ap, ap->pm_mesg);
4108 4123
4109 if (ap->ops->port_resume) 4124 if (ap->ops->port_resume)
4110 rc = ap->ops->port_resume(ap); 4125 rc = ap->ops->port_resume(ap);
diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
index 7c337e754dab..318b41358187 100644
--- a/drivers/ata/libata-scsi.c
+++ b/drivers/ata/libata-scsi.c
@@ -933,7 +933,11 @@ static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
933 * block specified for the ATA pass through commands. Regardless 933 * block specified for the ATA pass through commands. Regardless
934 * of whether the command errored or not, return a sense 934 * of whether the command errored or not, return a sense
935 * block. Copy all controller registers into the sense 935 * block. Copy all controller registers into the sense
936 * block. Clear sense key, ASC & ASCQ if there is no error. 936 * block. If there was no error, we get the request from an ATA
937 * passthrough command, so we use the following sense data:
938 * sk = RECOVERED ERROR
939 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
940 *
937 * 941 *
938 * LOCKING: 942 * LOCKING:
939 * None. 943 * None.
@@ -959,6 +963,10 @@ static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
959 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature, 963 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
960 &sb[1], &sb[2], &sb[3], verbose); 964 &sb[1], &sb[2], &sb[3], verbose);
961 sb[1] &= 0x0f; 965 sb[1] &= 0x0f;
966 } else {
967 sb[1] = RECOVERED_ERROR;
968 sb[2] = 0;
969 sb[3] = 0x1D;
962 } 970 }
963 971
964 /* 972 /*
@@ -1733,10 +1741,12 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1733 1741
1734 /* For ATA pass thru (SAT) commands, generate a sense block if 1742 /* For ATA pass thru (SAT) commands, generate a sense block if
1735 * user mandated it or if there's an error. Note that if we 1743 * user mandated it or if there's an error. Note that if we
1736 * generate because the user forced us to, a check condition 1744 * generate because the user forced us to [CK_COND =1], a check
1737 * is generated and the ATA register values are returned 1745 * condition is generated and the ATA register values are returned
1738 * whether the command completed successfully or not. If there 1746 * whether the command completed successfully or not. If there
1739 * was no error, SK, ASC and ASCQ will all be zero. 1747 * was no error, we use the following sense data:
1748 * sk = RECOVERED ERROR
1749 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1740 */ 1750 */
1741 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) && 1751 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1742 ((cdb[2] & 0x20) || need_sense)) { 1752 ((cdb[2] & 0x20) || need_sense)) {
@@ -3755,6 +3765,8 @@ static void ata_scsi_remove_dev(struct ata_device *dev)
3755 mutex_lock(&ap->scsi_host->scan_mutex); 3765 mutex_lock(&ap->scsi_host->scan_mutex);
3756 spin_lock_irqsave(ap->lock, flags); 3766 spin_lock_irqsave(ap->lock, flags);
3757 3767
3768 if (zpodd_dev_enabled(dev))
3769 zpodd_exit(dev);
3758 ata_acpi_unbind(dev); 3770 ata_acpi_unbind(dev);
3759 3771
3760 /* clearing dev->sdev is protected by host lock */ 3772 /* clearing dev->sdev is protected by host lock */
diff --git a/drivers/ata/libata-zpodd.c b/drivers/ata/libata-zpodd.c
new file mode 100644
index 000000000000..90b159b740b3
--- /dev/null
+++ b/drivers/ata/libata-zpodd.c
@@ -0,0 +1,299 @@
1#include <linux/libata.h>
2#include <linux/cdrom.h>
3#include <linux/pm_runtime.h>
4#include <linux/module.h>
5#include <scsi/scsi_device.h>
6
7#include "libata.h"
8
9static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */
10module_param(zpodd_poweroff_delay, int, 0644);
11MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds");
12
13enum odd_mech_type {
14 ODD_MECH_TYPE_SLOT,
15 ODD_MECH_TYPE_DRAWER,
16 ODD_MECH_TYPE_UNSUPPORTED,
17};
18
19struct zpodd {
20 enum odd_mech_type mech_type; /* init during probe, RO afterwards */
21 struct ata_device *dev;
22
23 /* The following fields are synchronized by PM core. */
24 bool from_notify; /* resumed as a result of
25 * acpi wake notification */
26 bool zp_ready; /* ZP ready state */
27 unsigned long last_ready; /* last ZP ready timestamp */
28 bool zp_sampled; /* ZP ready state sampled */
29 bool powered_off; /* ODD is powered off
30 * during suspend */
31};
32
33static int eject_tray(struct ata_device *dev)
34{
35 struct ata_taskfile tf = {};
36 const char cdb[] = { GPCMD_START_STOP_UNIT,
37 0, 0, 0,
38 0x02, /* LoEj */
39 0, 0, 0, 0, 0, 0, 0,
40 };
41
42 tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
43 tf.command = ATA_CMD_PACKET;
44 tf.protocol = ATAPI_PROT_NODATA;
45
46 return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
47}
48
49/* Per the spec, only slot type and drawer type ODD can be supported */
50static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev)
51{
52 char buf[16];
53 unsigned int ret;
54 struct rm_feature_desc *desc = (void *)(buf + 8);
55 struct ata_taskfile tf = {};
56
57 char cdb[] = { GPCMD_GET_CONFIGURATION,
58 2, /* only 1 feature descriptor requested */
59 0, 3, /* 3, removable medium feature */
60 0, 0, 0,/* reserved */
61 0, sizeof(buf),
62 0, 0, 0,
63 };
64
65 tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
66 tf.command = ATA_CMD_PACKET;
67 tf.protocol = ATAPI_PROT_PIO;
68 tf.lbam = sizeof(buf);
69
70 ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
71 buf, sizeof(buf), 0);
72 if (ret)
73 return ODD_MECH_TYPE_UNSUPPORTED;
74
75 if (be16_to_cpu(desc->feature_code) != 3)
76 return ODD_MECH_TYPE_UNSUPPORTED;
77
78 if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1)
79 return ODD_MECH_TYPE_SLOT;
80 else if (desc->mech_type == 1 && desc->load == 0 && desc->eject == 1)
81 return ODD_MECH_TYPE_DRAWER;
82 else
83 return ODD_MECH_TYPE_UNSUPPORTED;
84}
85
86static bool odd_can_poweroff(struct ata_device *ata_dev)
87{
88 acpi_handle handle;
89 acpi_status status;
90 struct acpi_device *acpi_dev;
91
92 handle = ata_dev_acpi_handle(ata_dev);
93 if (!handle)
94 return false;
95
96 status = acpi_bus_get_device(handle, &acpi_dev);
97 if (ACPI_FAILURE(status))
98 return false;
99
100 return acpi_device_can_poweroff(acpi_dev);
101}
102
103/* Test if ODD is zero power ready by sense code */
104static bool zpready(struct ata_device *dev)
105{
106 u8 sense_key, *sense_buf;
107 unsigned int ret, asc, ascq, add_len;
108 struct zpodd *zpodd = dev->zpodd;
109
110 ret = atapi_eh_tur(dev, &sense_key);
111
112 if (!ret || sense_key != NOT_READY)
113 return false;
114
115 sense_buf = dev->link->ap->sector_buf;
116 ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
117 if (ret)
118 return false;
119
120 /* sense valid */
121 if ((sense_buf[0] & 0x7f) != 0x70)
122 return false;
123
124 add_len = sense_buf[7];
125 /* has asc and ascq */
126 if (add_len < 6)
127 return false;
128
129 asc = sense_buf[12];
130 ascq = sense_buf[13];
131
132 if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
133 /* no media inside */
134 return asc == 0x3a;
135 else
136 /* no media inside and door closed */
137 return asc == 0x3a && ascq == 0x01;
138}
139
140/*
141 * Update the zpodd->zp_ready field. This field will only be set
142 * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
143 * time, and will be used to decide if power off is allowed. If it
144 * is set, it will be cleared during resume from powered off state.
145 */
146void zpodd_on_suspend(struct ata_device *dev)
147{
148 struct zpodd *zpodd = dev->zpodd;
149 unsigned long expires;
150
151 if (!zpready(dev)) {
152 zpodd->zp_sampled = false;
153 zpodd->zp_ready = false;
154 return;
155 }
156
157 if (!zpodd->zp_sampled) {
158 zpodd->zp_sampled = true;
159 zpodd->last_ready = jiffies;
160 return;
161 }
162
163 expires = zpodd->last_ready +
164 msecs_to_jiffies(zpodd_poweroff_delay * 1000);
165 if (time_before(jiffies, expires))
166 return;
167
168 zpodd->zp_ready = true;
169}
170
171bool zpodd_zpready(struct ata_device *dev)
172{
173 struct zpodd *zpodd = dev->zpodd;
174 return zpodd->zp_ready;
175}
176
177/*
178 * Enable runtime wake capability through ACPI and set the powered_off flag,
179 * this flag will be used during resume to decide what operations are needed
180 * to take.
181 *
182 * Also, media poll needs to be silenced, so that it doesn't bring the ODD
183 * back to full power state every few seconds.
184 */
185void zpodd_enable_run_wake(struct ata_device *dev)
186{
187 struct zpodd *zpodd = dev->zpodd;
188
189 sdev_disable_disk_events(dev->sdev);
190
191 zpodd->powered_off = true;
192 device_set_run_wake(&dev->sdev->sdev_gendev, true);
193 acpi_pm_device_run_wake(&dev->sdev->sdev_gendev, true);
194}
195
196/* Disable runtime wake capability if it is enabled */
197void zpodd_disable_run_wake(struct ata_device *dev)
198{
199 struct zpodd *zpodd = dev->zpodd;
200
201 if (zpodd->powered_off) {
202 acpi_pm_device_run_wake(&dev->sdev->sdev_gendev, false);
203 device_set_run_wake(&dev->sdev->sdev_gendev, false);
204 }
205}
206
207/*
208 * Post power on processing after the ODD has been recovered. If the
209 * ODD wasn't powered off during suspend, it doesn't do anything.
210 *
211 * For drawer type ODD, if it is powered on due to user pressed the
212 * eject button, the tray needs to be ejected. This can only be done
213 * after the ODD has been recovered, i.e. link is initialized and
214 * device is able to process NON_DATA PIO command, as eject needs to
215 * send command for the ODD to process.
216 *
217 * The from_notify flag set in wake notification handler function
218 * zpodd_wake_dev represents if power on is due to user's action.
219 *
220 * For both types of ODD, several fields need to be reset.
221 */
222void zpodd_post_poweron(struct ata_device *dev)
223{
224 struct zpodd *zpodd = dev->zpodd;
225
226 if (!zpodd->powered_off)
227 return;
228
229 zpodd->powered_off = false;
230
231 if (zpodd->from_notify) {
232 zpodd->from_notify = false;
233 if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
234 eject_tray(dev);
235 }
236
237 zpodd->zp_sampled = false;
238 zpodd->zp_ready = false;
239
240 sdev_enable_disk_events(dev->sdev);
241}
242
243static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
244{
245 struct ata_device *ata_dev = context;
246 struct zpodd *zpodd = ata_dev->zpodd;
247 struct device *dev = &ata_dev->sdev->sdev_gendev;
248
249 if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) {
250 zpodd->from_notify = true;
251 pm_runtime_resume(dev);
252 }
253}
254
255static void ata_acpi_add_pm_notifier(struct ata_device *dev)
256{
257 acpi_handle handle = ata_dev_acpi_handle(dev);
258 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
259 zpodd_wake_dev, dev);
260}
261
262static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
263{
264 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->sdev->sdev_gendev);
265 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
266}
267
268void zpodd_init(struct ata_device *dev)
269{
270 enum odd_mech_type mech_type;
271 struct zpodd *zpodd;
272
273 if (dev->zpodd)
274 return;
275
276 if (!odd_can_poweroff(dev))
277 return;
278
279 mech_type = zpodd_get_mech_type(dev);
280 if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
281 return;
282
283 zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
284 if (!zpodd)
285 return;
286
287 zpodd->mech_type = mech_type;
288
289 ata_acpi_add_pm_notifier(dev);
290 zpodd->dev = dev;
291 dev->zpodd = zpodd;
292}
293
294void zpodd_exit(struct ata_device *dev)
295{
296 ata_acpi_remove_pm_notifier(dev);
297 kfree(dev->zpodd);
298 dev->zpodd = NULL;
299}
diff --git a/drivers/ata/libata.h b/drivers/ata/libata.h
index 7148a58020b9..c949dd311b2e 100644
--- a/drivers/ata/libata.h
+++ b/drivers/ata/libata.h
@@ -182,6 +182,9 @@ extern void ata_eh_finish(struct ata_port *ap);
182extern int ata_ering_map(struct ata_ering *ering, 182extern int ata_ering_map(struct ata_ering *ering,
183 int (*map_fn)(struct ata_ering_entry *, void *), 183 int (*map_fn)(struct ata_ering_entry *, void *),
184 void *arg); 184 void *arg);
185extern unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key);
186extern unsigned int atapi_eh_request_sense(struct ata_device *dev,
187 u8 *sense_buf, u8 dfl_sense_key);
185 188
186/* libata-pmp.c */ 189/* libata-pmp.c */
187#ifdef CONFIG_SATA_PMP 190#ifdef CONFIG_SATA_PMP
@@ -230,4 +233,28 @@ static inline void ata_sff_exit(void)
230{ } 233{ }
231#endif /* CONFIG_ATA_SFF */ 234#endif /* CONFIG_ATA_SFF */
232 235
236/* libata-zpodd.c */
237#ifdef CONFIG_SATA_ZPODD
238void zpodd_init(struct ata_device *dev);
239void zpodd_exit(struct ata_device *dev);
240static inline bool zpodd_dev_enabled(struct ata_device *dev)
241{
242 return dev->zpodd != NULL;
243}
244void zpodd_on_suspend(struct ata_device *dev);
245bool zpodd_zpready(struct ata_device *dev);
246void zpodd_enable_run_wake(struct ata_device *dev);
247void zpodd_disable_run_wake(struct ata_device *dev);
248void zpodd_post_poweron(struct ata_device *dev);
249#else /* CONFIG_SATA_ZPODD */
250static inline void zpodd_init(struct ata_device *dev) {}
251static inline void zpodd_exit(struct ata_device *dev) {}
252static inline bool zpodd_dev_enabled(struct ata_device *dev) { return false; }
253static inline void zpodd_on_suspend(struct ata_device *dev) {}
254static inline bool zpodd_zpready(struct ata_device *dev) { return false; }
255static inline void zpodd_enable_run_wake(struct ata_device *dev) {}
256static inline void zpodd_disable_run_wake(struct ata_device *dev) {}
257static inline void zpodd_post_poweron(struct ata_device *dev) {}
258#endif /* CONFIG_SATA_ZPODD */
259
233#endif /* __LIBATA_H__ */ 260#endif /* __LIBATA_H__ */
diff --git a/drivers/ata/pata_samsung_cf.c b/drivers/ata/pata_samsung_cf.c
index 63ffb002ec67..70b0e01372b3 100644
--- a/drivers/ata/pata_samsung_cf.c
+++ b/drivers/ata/pata_samsung_cf.c
@@ -512,7 +512,7 @@ static int __init pata_s3c_probe(struct platform_device *pdev)
512 return -ENOMEM; 512 return -ENOMEM;
513 } 513 }
514 514
515 info->clk = clk_get(&pdev->dev, "cfcon"); 515 info->clk = devm_clk_get(&pdev->dev, "cfcon");
516 if (IS_ERR(info->clk)) { 516 if (IS_ERR(info->clk)) {
517 dev_err(dev, "failed to get access to cf controller clock\n"); 517 dev_err(dev, "failed to get access to cf controller clock\n");
518 ret = PTR_ERR(info->clk); 518 ret = PTR_ERR(info->clk);
@@ -589,7 +589,6 @@ static int __init pata_s3c_probe(struct platform_device *pdev)
589 589
590stop_clk: 590stop_clk:
591 clk_disable(info->clk); 591 clk_disable(info->clk);
592 clk_put(info->clk);
593 return ret; 592 return ret;
594} 593}
595 594
@@ -601,7 +600,6 @@ static int __exit pata_s3c_remove(struct platform_device *pdev)
601 ata_host_detach(host); 600 ata_host_detach(host);
602 601
603 clk_disable(info->clk); 602 clk_disable(info->clk);
604 clk_put(info->clk);
605 603
606 return 0; 604 return 0;
607} 605}
diff --git a/drivers/ata/sata_rcar.c b/drivers/ata/sata_rcar.c
new file mode 100644
index 000000000000..caf33f620c35
--- /dev/null
+++ b/drivers/ata/sata_rcar.c
@@ -0,0 +1,910 @@
1/*
2 * Renesas R-Car SATA driver
3 *
4 * Author: Vladimir Barinov <source@cogentembedded.com>
5 * Copyright (C) 2013 Cogent Embedded, Inc.
6 * Copyright (C) 2013 Renesas Solutions Corp.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/ata.h>
17#include <linux/libata.h>
18#include <linux/platform_device.h>
19#include <linux/clk.h>
20
21#define DRV_NAME "sata_rcar"
22
23/* SH-Navi2G/ATAPI-ATA compatible task registers */
24#define DATA_REG 0x100
25#define SDEVCON_REG 0x138
26
27/* SH-Navi2G/ATAPI module compatible control registers */
28#define ATAPI_CONTROL1_REG 0x180
29#define ATAPI_STATUS_REG 0x184
30#define ATAPI_INT_ENABLE_REG 0x188
31#define ATAPI_DTB_ADR_REG 0x198
32#define ATAPI_DMA_START_ADR_REG 0x19C
33#define ATAPI_DMA_TRANS_CNT_REG 0x1A0
34#define ATAPI_CONTROL2_REG 0x1A4
35#define ATAPI_SIG_ST_REG 0x1B0
36#define ATAPI_BYTE_SWAP_REG 0x1BC
37
38/* ATAPI control 1 register (ATAPI_CONTROL1) bits */
39#define ATAPI_CONTROL1_ISM BIT(16)
40#define ATAPI_CONTROL1_DTA32M BIT(11)
41#define ATAPI_CONTROL1_RESET BIT(7)
42#define ATAPI_CONTROL1_DESE BIT(3)
43#define ATAPI_CONTROL1_RW BIT(2)
44#define ATAPI_CONTROL1_STOP BIT(1)
45#define ATAPI_CONTROL1_START BIT(0)
46
47/* ATAPI status register (ATAPI_STATUS) bits */
48#define ATAPI_STATUS_SATAINT BIT(11)
49#define ATAPI_STATUS_DNEND BIT(6)
50#define ATAPI_STATUS_DEVTRM BIT(5)
51#define ATAPI_STATUS_DEVINT BIT(4)
52#define ATAPI_STATUS_ERR BIT(2)
53#define ATAPI_STATUS_NEND BIT(1)
54#define ATAPI_STATUS_ACT BIT(0)
55
56/* Interrupt enable register (ATAPI_INT_ENABLE) bits */
57#define ATAPI_INT_ENABLE_SATAINT BIT(11)
58#define ATAPI_INT_ENABLE_DNEND BIT(6)
59#define ATAPI_INT_ENABLE_DEVTRM BIT(5)
60#define ATAPI_INT_ENABLE_DEVINT BIT(4)
61#define ATAPI_INT_ENABLE_ERR BIT(2)
62#define ATAPI_INT_ENABLE_NEND BIT(1)
63#define ATAPI_INT_ENABLE_ACT BIT(0)
64
65/* Access control registers for physical layer control register */
66#define SATAPHYADDR_REG 0x200
67#define SATAPHYWDATA_REG 0x204
68#define SATAPHYACCEN_REG 0x208
69#define SATAPHYRESET_REG 0x20C
70#define SATAPHYRDATA_REG 0x210
71#define SATAPHYACK_REG 0x214
72
73/* Physical layer control address command register (SATAPHYADDR) bits */
74#define SATAPHYADDR_PHYRATEMODE BIT(10)
75#define SATAPHYADDR_PHYCMD_READ BIT(9)
76#define SATAPHYADDR_PHYCMD_WRITE BIT(8)
77
78/* Physical layer control enable register (SATAPHYACCEN) bits */
79#define SATAPHYACCEN_PHYLANE BIT(0)
80
81/* Physical layer control reset register (SATAPHYRESET) bits */
82#define SATAPHYRESET_PHYRST BIT(1)
83#define SATAPHYRESET_PHYSRES BIT(0)
84
85/* Physical layer control acknowledge register (SATAPHYACK) bits */
86#define SATAPHYACK_PHYACK BIT(0)
87
88/* Serial-ATA HOST control registers */
89#define BISTCONF_REG 0x102C
90#define SDATA_REG 0x1100
91#define SSDEVCON_REG 0x1204
92
93#define SCRSSTS_REG 0x1400
94#define SCRSERR_REG 0x1404
95#define SCRSCON_REG 0x1408
96#define SCRSACT_REG 0x140C
97
98#define SATAINTSTAT_REG 0x1508
99#define SATAINTMASK_REG 0x150C
100
101/* SATA INT status register (SATAINTSTAT) bits */
102#define SATAINTSTAT_SERR BIT(3)
103#define SATAINTSTAT_ATA BIT(0)
104
105/* SATA INT mask register (SATAINTSTAT) bits */
106#define SATAINTMASK_SERRMSK BIT(3)
107#define SATAINTMASK_ERRMSK BIT(2)
108#define SATAINTMASK_ERRCRTMSK BIT(1)
109#define SATAINTMASK_ATAMSK BIT(0)
110
111#define SATA_RCAR_INT_MASK (SATAINTMASK_SERRMSK | \
112 SATAINTMASK_ATAMSK)
113
114/* Physical Layer Control Registers */
115#define SATAPCTLR1_REG 0x43
116#define SATAPCTLR2_REG 0x52
117#define SATAPCTLR3_REG 0x5A
118#define SATAPCTLR4_REG 0x60
119
120/* Descriptor table word 0 bit (when DTA32M = 1) */
121#define SATA_RCAR_DTEND BIT(0)
122
123struct sata_rcar_priv {
124 void __iomem *base;
125 struct clk *clk;
126};
127
128static void sata_rcar_phy_initialize(struct sata_rcar_priv *priv)
129{
130 /* idle state */
131 iowrite32(0, priv->base + SATAPHYADDR_REG);
132 /* reset */
133 iowrite32(SATAPHYRESET_PHYRST, priv->base + SATAPHYRESET_REG);
134 udelay(10);
135 /* deassert reset */
136 iowrite32(0, priv->base + SATAPHYRESET_REG);
137}
138
139static void sata_rcar_phy_write(struct sata_rcar_priv *priv, u16 reg, u32 val,
140 int group)
141{
142 int timeout;
143
144 /* deassert reset */
145 iowrite32(0, priv->base + SATAPHYRESET_REG);
146 /* lane 1 */
147 iowrite32(SATAPHYACCEN_PHYLANE, priv->base + SATAPHYACCEN_REG);
148 /* write phy register value */
149 iowrite32(val, priv->base + SATAPHYWDATA_REG);
150 /* set register group */
151 if (group)
152 reg |= SATAPHYADDR_PHYRATEMODE;
153 /* write command */
154 iowrite32(SATAPHYADDR_PHYCMD_WRITE | reg, priv->base + SATAPHYADDR_REG);
155 /* wait for ack */
156 for (timeout = 0; timeout < 100; timeout++) {
157 val = ioread32(priv->base + SATAPHYACK_REG);
158 if (val & SATAPHYACK_PHYACK)
159 break;
160 }
161 if (timeout >= 100)
162 pr_err("%s timeout\n", __func__);
163 /* idle state */
164 iowrite32(0, priv->base + SATAPHYADDR_REG);
165}
166
167static void sata_rcar_freeze(struct ata_port *ap)
168{
169 struct sata_rcar_priv *priv = ap->host->private_data;
170
171 /* mask */
172 iowrite32(0x7ff, priv->base + SATAINTMASK_REG);
173
174 ata_sff_freeze(ap);
175}
176
177static void sata_rcar_thaw(struct ata_port *ap)
178{
179 struct sata_rcar_priv *priv = ap->host->private_data;
180
181 /* ack */
182 iowrite32(~SATA_RCAR_INT_MASK, priv->base + SATAINTSTAT_REG);
183
184 ata_sff_thaw(ap);
185
186 /* unmask */
187 iowrite32(0x7ff & ~SATA_RCAR_INT_MASK, priv->base + SATAINTMASK_REG);
188}
189
190static void sata_rcar_ioread16_rep(void __iomem *reg, void *buffer, int count)
191{
192 u16 *ptr = buffer;
193
194 while (count--) {
195 u16 data = ioread32(reg);
196
197 *ptr++ = data;
198 }
199}
200
201static void sata_rcar_iowrite16_rep(void __iomem *reg, void *buffer, int count)
202{
203 const u16 *ptr = buffer;
204
205 while (count--)
206 iowrite32(*ptr++, reg);
207}
208
209static u8 sata_rcar_check_status(struct ata_port *ap)
210{
211 return ioread32(ap->ioaddr.status_addr);
212}
213
214static u8 sata_rcar_check_altstatus(struct ata_port *ap)
215{
216 return ioread32(ap->ioaddr.altstatus_addr);
217}
218
219static void sata_rcar_set_devctl(struct ata_port *ap, u8 ctl)
220{
221 iowrite32(ctl, ap->ioaddr.ctl_addr);
222}
223
224static void sata_rcar_dev_select(struct ata_port *ap, unsigned int device)
225{
226 iowrite32(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
227 ata_sff_pause(ap); /* needed; also flushes, for mmio */
228}
229
230static unsigned int sata_rcar_ata_devchk(struct ata_port *ap,
231 unsigned int device)
232{
233 struct ata_ioports *ioaddr = &ap->ioaddr;
234 u8 nsect, lbal;
235
236 sata_rcar_dev_select(ap, device);
237
238 iowrite32(0x55, ioaddr->nsect_addr);
239 iowrite32(0xaa, ioaddr->lbal_addr);
240
241 iowrite32(0xaa, ioaddr->nsect_addr);
242 iowrite32(0x55, ioaddr->lbal_addr);
243
244 iowrite32(0x55, ioaddr->nsect_addr);
245 iowrite32(0xaa, ioaddr->lbal_addr);
246
247 nsect = ioread32(ioaddr->nsect_addr);
248 lbal = ioread32(ioaddr->lbal_addr);
249
250 if (nsect == 0x55 && lbal == 0xaa)
251 return 1; /* found a device */
252
253 return 0; /* nothing found */
254}
255
256static int sata_rcar_wait_after_reset(struct ata_link *link,
257 unsigned long deadline)
258{
259 struct ata_port *ap = link->ap;
260
261 ata_msleep(ap, ATA_WAIT_AFTER_RESET);
262
263 return ata_sff_wait_ready(link, deadline);
264}
265
266static int sata_rcar_bus_softreset(struct ata_port *ap, unsigned long deadline)
267{
268 struct ata_ioports *ioaddr = &ap->ioaddr;
269
270 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
271
272 /* software reset. causes dev0 to be selected */
273 iowrite32(ap->ctl, ioaddr->ctl_addr);
274 udelay(20);
275 iowrite32(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
276 udelay(20);
277 iowrite32(ap->ctl, ioaddr->ctl_addr);
278 ap->last_ctl = ap->ctl;
279
280 /* wait the port to become ready */
281 return sata_rcar_wait_after_reset(&ap->link, deadline);
282}
283
284static int sata_rcar_softreset(struct ata_link *link, unsigned int *classes,
285 unsigned long deadline)
286{
287 struct ata_port *ap = link->ap;
288 unsigned int devmask = 0;
289 int rc;
290 u8 err;
291
292 /* determine if device 0 is present */
293 if (sata_rcar_ata_devchk(ap, 0))
294 devmask |= 1 << 0;
295
296 /* issue bus reset */
297 DPRINTK("about to softreset, devmask=%x\n", devmask);
298 rc = sata_rcar_bus_softreset(ap, deadline);
299 /* if link is occupied, -ENODEV too is an error */
300 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
301 ata_link_err(link, "SRST failed (errno=%d)\n", rc);
302 return rc;
303 }
304
305 /* determine by signature whether we have ATA or ATAPI devices */
306 classes[0] = ata_sff_dev_classify(&link->device[0], devmask, &err);
307
308 DPRINTK("classes[0]=%u\n", classes[0]);
309 return 0;
310}
311
312static void sata_rcar_tf_load(struct ata_port *ap,
313 const struct ata_taskfile *tf)
314{
315 struct ata_ioports *ioaddr = &ap->ioaddr;
316 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
317
318 if (tf->ctl != ap->last_ctl) {
319 iowrite32(tf->ctl, ioaddr->ctl_addr);
320 ap->last_ctl = tf->ctl;
321 ata_wait_idle(ap);
322 }
323
324 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
325 iowrite32(tf->hob_feature, ioaddr->feature_addr);
326 iowrite32(tf->hob_nsect, ioaddr->nsect_addr);
327 iowrite32(tf->hob_lbal, ioaddr->lbal_addr);
328 iowrite32(tf->hob_lbam, ioaddr->lbam_addr);
329 iowrite32(tf->hob_lbah, ioaddr->lbah_addr);
330 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
331 tf->hob_feature,
332 tf->hob_nsect,
333 tf->hob_lbal,
334 tf->hob_lbam,
335 tf->hob_lbah);
336 }
337
338 if (is_addr) {
339 iowrite32(tf->feature, ioaddr->feature_addr);
340 iowrite32(tf->nsect, ioaddr->nsect_addr);
341 iowrite32(tf->lbal, ioaddr->lbal_addr);
342 iowrite32(tf->lbam, ioaddr->lbam_addr);
343 iowrite32(tf->lbah, ioaddr->lbah_addr);
344 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
345 tf->feature,
346 tf->nsect,
347 tf->lbal,
348 tf->lbam,
349 tf->lbah);
350 }
351
352 if (tf->flags & ATA_TFLAG_DEVICE) {
353 iowrite32(tf->device, ioaddr->device_addr);
354 VPRINTK("device 0x%X\n", tf->device);
355 }
356
357 ata_wait_idle(ap);
358}
359
360static void sata_rcar_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
361{
362 struct ata_ioports *ioaddr = &ap->ioaddr;
363
364 tf->command = sata_rcar_check_status(ap);
365 tf->feature = ioread32(ioaddr->error_addr);
366 tf->nsect = ioread32(ioaddr->nsect_addr);
367 tf->lbal = ioread32(ioaddr->lbal_addr);
368 tf->lbam = ioread32(ioaddr->lbam_addr);
369 tf->lbah = ioread32(ioaddr->lbah_addr);
370 tf->device = ioread32(ioaddr->device_addr);
371
372 if (tf->flags & ATA_TFLAG_LBA48) {
373 iowrite32(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
374 tf->hob_feature = ioread32(ioaddr->error_addr);
375 tf->hob_nsect = ioread32(ioaddr->nsect_addr);
376 tf->hob_lbal = ioread32(ioaddr->lbal_addr);
377 tf->hob_lbam = ioread32(ioaddr->lbam_addr);
378 tf->hob_lbah = ioread32(ioaddr->lbah_addr);
379 iowrite32(tf->ctl, ioaddr->ctl_addr);
380 ap->last_ctl = tf->ctl;
381 }
382}
383
384static void sata_rcar_exec_command(struct ata_port *ap,
385 const struct ata_taskfile *tf)
386{
387 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
388
389 iowrite32(tf->command, ap->ioaddr.command_addr);
390 ata_sff_pause(ap);
391}
392
393static unsigned int sata_rcar_data_xfer(struct ata_device *dev,
394 unsigned char *buf,
395 unsigned int buflen, int rw)
396{
397 struct ata_port *ap = dev->link->ap;
398 void __iomem *data_addr = ap->ioaddr.data_addr;
399 unsigned int words = buflen >> 1;
400
401 /* Transfer multiple of 2 bytes */
402 if (rw == READ)
403 sata_rcar_ioread16_rep(data_addr, buf, words);
404 else
405 sata_rcar_iowrite16_rep(data_addr, buf, words);
406
407 /* Transfer trailing byte, if any. */
408 if (unlikely(buflen & 0x01)) {
409 unsigned char pad[2] = { };
410
411 /* Point buf to the tail of buffer */
412 buf += buflen - 1;
413
414 /*
415 * Use io*16_rep() accessors here as well to avoid pointlessly
416 * swapping bytes to and from on the big endian machines...
417 */
418 if (rw == READ) {
419 sata_rcar_ioread16_rep(data_addr, pad, 1);
420 *buf = pad[0];
421 } else {
422 pad[0] = *buf;
423 sata_rcar_iowrite16_rep(data_addr, pad, 1);
424 }
425 words++;
426 }
427
428 return words << 1;
429}
430
431static void sata_rcar_drain_fifo(struct ata_queued_cmd *qc)
432{
433 int count;
434 struct ata_port *ap;
435
436 /* We only need to flush incoming data when a command was running */
437 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
438 return;
439
440 ap = qc->ap;
441 /* Drain up to 64K of data before we give up this recovery method */
442 for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ) &&
443 count < 65536; count += 2)
444 ioread32(ap->ioaddr.data_addr);
445
446 /* Can become DEBUG later */
447 if (count)
448 ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
449}
450
451static int sata_rcar_scr_read(struct ata_link *link, unsigned int sc_reg,
452 u32 *val)
453{
454 if (sc_reg > SCR_ACTIVE)
455 return -EINVAL;
456
457 *val = ioread32(link->ap->ioaddr.scr_addr + (sc_reg << 2));
458 return 0;
459}
460
461static int sata_rcar_scr_write(struct ata_link *link, unsigned int sc_reg,
462 u32 val)
463{
464 if (sc_reg > SCR_ACTIVE)
465 return -EINVAL;
466
467 iowrite32(val, link->ap->ioaddr.scr_addr + (sc_reg << 2));
468 return 0;
469}
470
471static void sata_rcar_bmdma_fill_sg(struct ata_queued_cmd *qc)
472{
473 struct ata_port *ap = qc->ap;
474 struct ata_bmdma_prd *prd = ap->bmdma_prd;
475 struct scatterlist *sg;
476 unsigned int si, pi;
477
478 pi = 0;
479 for_each_sg(qc->sg, sg, qc->n_elem, si) {
480 u32 addr, sg_len, len;
481
482 /*
483 * Note: h/w doesn't support 64-bit, so we unconditionally
484 * truncate dma_addr_t to u32.
485 */
486 addr = (u32)sg_dma_address(sg);
487 sg_len = sg_dma_len(sg);
488
489 /* H/w transfer count is only 29 bits long, let's be careful */
490 while (sg_len) {
491 len = sg_len;
492 if (len > 0x1ffffffe)
493 len = 0x1ffffffe;
494
495 prd[pi].addr = cpu_to_le32(addr);
496 prd[pi].flags_len = cpu_to_le32(len);
497 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
498
499 pi++;
500 sg_len -= len;
501 addr += len;
502 }
503 }
504
505 /* end-of-table flag */
506 prd[pi - 1].addr |= cpu_to_le32(SATA_RCAR_DTEND);
507}
508
509static void sata_rcar_qc_prep(struct ata_queued_cmd *qc)
510{
511 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
512 return;
513
514 sata_rcar_bmdma_fill_sg(qc);
515}
516
517static void sata_rcar_bmdma_setup(struct ata_queued_cmd *qc)
518{
519 struct ata_port *ap = qc->ap;
520 unsigned int rw = qc->tf.flags & ATA_TFLAG_WRITE;
521 u32 dmactl;
522 struct sata_rcar_priv *priv = ap->host->private_data;
523
524 /* load PRD table addr. */
525 mb(); /* make sure PRD table writes are visible to controller */
526 iowrite32(ap->bmdma_prd_dma, priv->base + ATAPI_DTB_ADR_REG);
527
528 /* specify data direction, triple-check start bit is clear */
529 dmactl = ioread32(priv->base + ATAPI_CONTROL1_REG);
530 dmactl &= ~(ATAPI_CONTROL1_RW | ATAPI_CONTROL1_STOP);
531 if (dmactl & ATAPI_CONTROL1_START) {
532 dmactl &= ~ATAPI_CONTROL1_START;
533 dmactl |= ATAPI_CONTROL1_STOP;
534 }
535 if (!rw)
536 dmactl |= ATAPI_CONTROL1_RW;
537 iowrite32(dmactl, priv->base + ATAPI_CONTROL1_REG);
538
539 /* issue r/w command */
540 ap->ops->sff_exec_command(ap, &qc->tf);
541}
542
543static void sata_rcar_bmdma_start(struct ata_queued_cmd *qc)
544{
545 struct ata_port *ap = qc->ap;
546 u32 dmactl;
547 struct sata_rcar_priv *priv = ap->host->private_data;
548
549 /* start host DMA transaction */
550 dmactl = ioread32(priv->base + ATAPI_CONTROL1_REG);
551 dmactl |= ATAPI_CONTROL1_START;
552 iowrite32(dmactl, priv->base + ATAPI_CONTROL1_REG);
553}
554
555static void sata_rcar_bmdma_stop(struct ata_queued_cmd *qc)
556{
557 struct ata_port *ap = qc->ap;
558 struct sata_rcar_priv *priv = ap->host->private_data;
559 u32 dmactl;
560
561 /* force termination of DMA transfer if active */
562 dmactl = ioread32(priv->base + ATAPI_CONTROL1_REG);
563 if (dmactl & ATAPI_CONTROL1_START) {
564 dmactl &= ~ATAPI_CONTROL1_START;
565 dmactl |= ATAPI_CONTROL1_STOP;
566 iowrite32(dmactl, priv->base + ATAPI_CONTROL1_REG);
567 }
568
569 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
570 ata_sff_dma_pause(ap);
571}
572
573static u8 sata_rcar_bmdma_status(struct ata_port *ap)
574{
575 struct sata_rcar_priv *priv = ap->host->private_data;
576 u32 status;
577 u8 host_stat = 0;
578
579 status = ioread32(priv->base + ATAPI_STATUS_REG);
580 if (status & ATAPI_STATUS_DEVINT)
581 host_stat |= ATA_DMA_INTR;
582 if (status & ATAPI_STATUS_ACT)
583 host_stat |= ATA_DMA_ACTIVE;
584
585 return host_stat;
586}
587
588static struct scsi_host_template sata_rcar_sht = {
589 ATA_BMDMA_SHT(DRV_NAME),
590};
591
592static struct ata_port_operations sata_rcar_port_ops = {
593 .inherits = &ata_bmdma_port_ops,
594
595 .freeze = sata_rcar_freeze,
596 .thaw = sata_rcar_thaw,
597 .softreset = sata_rcar_softreset,
598
599 .scr_read = sata_rcar_scr_read,
600 .scr_write = sata_rcar_scr_write,
601
602 .sff_dev_select = sata_rcar_dev_select,
603 .sff_set_devctl = sata_rcar_set_devctl,
604 .sff_check_status = sata_rcar_check_status,
605 .sff_check_altstatus = sata_rcar_check_altstatus,
606 .sff_tf_load = sata_rcar_tf_load,
607 .sff_tf_read = sata_rcar_tf_read,
608 .sff_exec_command = sata_rcar_exec_command,
609 .sff_data_xfer = sata_rcar_data_xfer,
610 .sff_drain_fifo = sata_rcar_drain_fifo,
611
612 .qc_prep = sata_rcar_qc_prep,
613
614 .bmdma_setup = sata_rcar_bmdma_setup,
615 .bmdma_start = sata_rcar_bmdma_start,
616 .bmdma_stop = sata_rcar_bmdma_stop,
617 .bmdma_status = sata_rcar_bmdma_status,
618};
619
620static int sata_rcar_serr_interrupt(struct ata_port *ap)
621{
622 struct sata_rcar_priv *priv = ap->host->private_data;
623 struct ata_eh_info *ehi = &ap->link.eh_info;
624 int freeze = 0;
625 int handled = 0;
626 u32 serror;
627
628 serror = ioread32(priv->base + SCRSERR_REG);
629 if (!serror)
630 return 0;
631
632 DPRINTK("SError @host_intr: 0x%x\n", serror);
633
634 /* first, analyze and record host port events */
635 ata_ehi_clear_desc(ehi);
636
637 if (serror & (SERR_DEV_XCHG | SERR_PHYRDY_CHG)) {
638 /* Setup a soft-reset EH action */
639 ata_ehi_hotplugged(ehi);
640 ata_ehi_push_desc(ehi, "%s", "hotplug");
641
642 freeze = serror & SERR_COMM_WAKE ? 0 : 1;
643 handled = 1;
644 }
645
646 /* freeze or abort */
647 if (freeze)
648 ata_port_freeze(ap);
649 else
650 ata_port_abort(ap);
651
652 return handled;
653}
654
655static int sata_rcar_ata_interrupt(struct ata_port *ap)
656{
657 struct ata_queued_cmd *qc;
658 int handled = 0;
659
660 qc = ata_qc_from_tag(ap, ap->link.active_tag);
661 if (qc)
662 handled |= ata_bmdma_port_intr(ap, qc);
663
664 return handled;
665}
666
667static irqreturn_t sata_rcar_interrupt(int irq, void *dev_instance)
668{
669 struct ata_host *host = dev_instance;
670 struct sata_rcar_priv *priv = host->private_data;
671 struct ata_port *ap;
672 unsigned int handled = 0;
673 u32 sataintstat;
674 unsigned long flags;
675
676 spin_lock_irqsave(&host->lock, flags);
677
678 sataintstat = ioread32(priv->base + SATAINTSTAT_REG);
679 if (!sataintstat)
680 goto done;
681 /* ack */
682 iowrite32(sataintstat & ~SATA_RCAR_INT_MASK,
683 priv->base + SATAINTSTAT_REG);
684
685 ap = host->ports[0];
686
687 if (sataintstat & SATAINTSTAT_ATA)
688 handled |= sata_rcar_ata_interrupt(ap);
689
690 if (sataintstat & SATAINTSTAT_SERR)
691 handled |= sata_rcar_serr_interrupt(ap);
692
693done:
694 spin_unlock_irqrestore(&host->lock, flags);
695
696 return IRQ_RETVAL(handled);
697}
698
699static void sata_rcar_setup_port(struct ata_host *host)
700{
701 struct ata_port *ap = host->ports[0];
702 struct ata_ioports *ioaddr = &ap->ioaddr;
703 struct sata_rcar_priv *priv = host->private_data;
704
705 ap->ops = &sata_rcar_port_ops;
706 ap->pio_mask = ATA_PIO4;
707 ap->udma_mask = ATA_UDMA6;
708 ap->flags |= ATA_FLAG_SATA;
709
710 ioaddr->cmd_addr = priv->base + SDATA_REG;
711 ioaddr->ctl_addr = priv->base + SSDEVCON_REG;
712 ioaddr->scr_addr = priv->base + SCRSSTS_REG;
713 ioaddr->altstatus_addr = ioaddr->ctl_addr;
714
715 ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
716 ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
717 ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
718 ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
719 ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
720 ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
721 ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
722 ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
723 ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
724 ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
725}
726
727static void sata_rcar_init_controller(struct ata_host *host)
728{
729 struct sata_rcar_priv *priv = host->private_data;
730 u32 val;
731
732 /* reset and setup phy */
733 sata_rcar_phy_initialize(priv);
734 sata_rcar_phy_write(priv, SATAPCTLR1_REG, 0x00200188, 0);
735 sata_rcar_phy_write(priv, SATAPCTLR1_REG, 0x00200188, 1);
736 sata_rcar_phy_write(priv, SATAPCTLR3_REG, 0x0000A061, 0);
737 sata_rcar_phy_write(priv, SATAPCTLR2_REG, 0x20000000, 0);
738 sata_rcar_phy_write(priv, SATAPCTLR2_REG, 0x20000000, 1);
739 sata_rcar_phy_write(priv, SATAPCTLR4_REG, 0x28E80000, 0);
740
741 /* SATA-IP reset state */
742 val = ioread32(priv->base + ATAPI_CONTROL1_REG);
743 val |= ATAPI_CONTROL1_RESET;
744 iowrite32(val, priv->base + ATAPI_CONTROL1_REG);
745
746 /* ISM mode, PRD mode, DTEND flag at bit 0 */
747 val = ioread32(priv->base + ATAPI_CONTROL1_REG);
748 val |= ATAPI_CONTROL1_ISM;
749 val |= ATAPI_CONTROL1_DESE;
750 val |= ATAPI_CONTROL1_DTA32M;
751 iowrite32(val, priv->base + ATAPI_CONTROL1_REG);
752
753 /* Release the SATA-IP from the reset state */
754 val = ioread32(priv->base + ATAPI_CONTROL1_REG);
755 val &= ~ATAPI_CONTROL1_RESET;
756 iowrite32(val, priv->base + ATAPI_CONTROL1_REG);
757
758 /* ack and mask */
759 iowrite32(0, priv->base + SATAINTSTAT_REG);
760 iowrite32(0x7ff, priv->base + SATAINTMASK_REG);
761 /* enable interrupts */
762 iowrite32(ATAPI_INT_ENABLE_SATAINT, priv->base + ATAPI_INT_ENABLE_REG);
763}
764
765static int sata_rcar_probe(struct platform_device *pdev)
766{
767 struct ata_host *host;
768 struct sata_rcar_priv *priv;
769 struct resource *mem;
770 int irq;
771 int ret = 0;
772
773 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
774 if (mem == NULL)
775 return -EINVAL;
776
777 irq = platform_get_irq(pdev, 0);
778 if (irq <= 0)
779 return -EINVAL;
780
781 priv = devm_kzalloc(&pdev->dev, sizeof(struct sata_rcar_priv),
782 GFP_KERNEL);
783 if (!priv)
784 return -ENOMEM;
785
786 priv->clk = devm_clk_get(&pdev->dev, NULL);
787 if (IS_ERR(priv->clk)) {
788 dev_err(&pdev->dev, "failed to get access to sata clock\n");
789 return PTR_ERR(priv->clk);
790 }
791 clk_enable(priv->clk);
792
793 host = ata_host_alloc(&pdev->dev, 1);
794 if (!host) {
795 dev_err(&pdev->dev, "ata_host_alloc failed\n");
796 ret = -ENOMEM;
797 goto cleanup;
798 }
799
800 host->private_data = priv;
801
802 priv->base = devm_request_and_ioremap(&pdev->dev, mem);
803 if (!priv->base) {
804 ret = -EADDRNOTAVAIL;
805 goto cleanup;
806 }
807
808 /* setup port */
809 sata_rcar_setup_port(host);
810
811 /* initialize host controller */
812 sata_rcar_init_controller(host);
813
814 ret = ata_host_activate(host, irq, sata_rcar_interrupt, 0,
815 &sata_rcar_sht);
816 if (!ret)
817 return 0;
818
819cleanup:
820 clk_disable(priv->clk);
821
822 return ret;
823}
824
825static int sata_rcar_remove(struct platform_device *pdev)
826{
827 struct ata_host *host = dev_get_drvdata(&pdev->dev);
828 struct sata_rcar_priv *priv = host->private_data;
829
830 ata_host_detach(host);
831
832 /* disable interrupts */
833 iowrite32(0, priv->base + ATAPI_INT_ENABLE_REG);
834 /* ack and mask */
835 iowrite32(0, priv->base + SATAINTSTAT_REG);
836 iowrite32(0x7ff, priv->base + SATAINTMASK_REG);
837
838 clk_disable(priv->clk);
839
840 return 0;
841}
842
843#ifdef CONFIG_PM
844static int sata_rcar_suspend(struct device *dev)
845{
846 struct ata_host *host = dev_get_drvdata(dev);
847 struct sata_rcar_priv *priv = host->private_data;
848 int ret;
849
850 ret = ata_host_suspend(host, PMSG_SUSPEND);
851 if (!ret) {
852 /* disable interrupts */
853 iowrite32(0, priv->base + ATAPI_INT_ENABLE_REG);
854 /* mask */
855 iowrite32(0x7ff, priv->base + SATAINTMASK_REG);
856
857 clk_disable(priv->clk);
858 }
859
860 return ret;
861}
862
863static int sata_rcar_resume(struct device *dev)
864{
865 struct ata_host *host = dev_get_drvdata(dev);
866 struct sata_rcar_priv *priv = host->private_data;
867
868 clk_enable(priv->clk);
869
870 /* ack and mask */
871 iowrite32(0, priv->base + SATAINTSTAT_REG);
872 iowrite32(0x7ff, priv->base + SATAINTMASK_REG);
873 /* enable interrupts */
874 iowrite32(ATAPI_INT_ENABLE_SATAINT, priv->base + ATAPI_INT_ENABLE_REG);
875
876 ata_host_resume(host);
877
878 return 0;
879}
880
881static const struct dev_pm_ops sata_rcar_pm_ops = {
882 .suspend = sata_rcar_suspend,
883 .resume = sata_rcar_resume,
884};
885#endif
886
887static struct of_device_id sata_rcar_match[] = {
888 { .compatible = "renesas,rcar-sata", },
889 {},
890};
891MODULE_DEVICE_TABLE(of, sata_rcar_match);
892
893static struct platform_driver sata_rcar_driver = {
894 .probe = sata_rcar_probe,
895 .remove = sata_rcar_remove,
896 .driver = {
897 .name = DRV_NAME,
898 .owner = THIS_MODULE,
899 .of_match_table = sata_rcar_match,
900#ifdef CONFIG_PM
901 .pm = &sata_rcar_pm_ops,
902#endif
903 },
904};
905
906module_platform_driver(sata_rcar_driver);
907
908MODULE_LICENSE("GPL");
909MODULE_AUTHOR("Vladimir Barinov");
910MODULE_DESCRIPTION("Renesas R-Car SATA controller low level driver");