18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for OHCI 1394 controllers
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/bitops.h>
98c2ecf20Sopenharmony_ci#include <linux/bug.h>
108c2ecf20Sopenharmony_ci#include <linux/compiler.h>
118c2ecf20Sopenharmony_ci#include <linux/delay.h>
128c2ecf20Sopenharmony_ci#include <linux/device.h>
138c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
148c2ecf20Sopenharmony_ci#include <linux/firewire.h>
158c2ecf20Sopenharmony_ci#include <linux/firewire-constants.h>
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
188c2ecf20Sopenharmony_ci#include <linux/io.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/list.h>
218c2ecf20Sopenharmony_ci#include <linux/mm.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
248c2ecf20Sopenharmony_ci#include <linux/mutex.h>
258c2ecf20Sopenharmony_ci#include <linux/pci.h>
268c2ecf20Sopenharmony_ci#include <linux/pci_ids.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
298c2ecf20Sopenharmony_ci#include <linux/string.h>
308c2ecf20Sopenharmony_ci#include <linux/time.h>
318c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
328c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
358c2ecf20Sopenharmony_ci#include <asm/page.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_PMAC
388c2ecf20Sopenharmony_ci#include <asm/pmac_feature.h>
398c2ecf20Sopenharmony_ci#endif
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include "core.h"
428c2ecf20Sopenharmony_ci#include "ohci.h"
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define ohci_info(ohci, f, args...)	dev_info(ohci->card.device, f, ##args)
458c2ecf20Sopenharmony_ci#define ohci_notice(ohci, f, args...)	dev_notice(ohci->card.device, f, ##args)
468c2ecf20Sopenharmony_ci#define ohci_err(ohci, f, args...)	dev_err(ohci->card.device, f, ##args)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define DESCRIPTOR_OUTPUT_MORE		0
498c2ecf20Sopenharmony_ci#define DESCRIPTOR_OUTPUT_LAST		(1 << 12)
508c2ecf20Sopenharmony_ci#define DESCRIPTOR_INPUT_MORE		(2 << 12)
518c2ecf20Sopenharmony_ci#define DESCRIPTOR_INPUT_LAST		(3 << 12)
528c2ecf20Sopenharmony_ci#define DESCRIPTOR_STATUS		(1 << 11)
538c2ecf20Sopenharmony_ci#define DESCRIPTOR_KEY_IMMEDIATE	(2 << 8)
548c2ecf20Sopenharmony_ci#define DESCRIPTOR_PING			(1 << 7)
558c2ecf20Sopenharmony_ci#define DESCRIPTOR_YY			(1 << 6)
568c2ecf20Sopenharmony_ci#define DESCRIPTOR_NO_IRQ		(0 << 4)
578c2ecf20Sopenharmony_ci#define DESCRIPTOR_IRQ_ERROR		(1 << 4)
588c2ecf20Sopenharmony_ci#define DESCRIPTOR_IRQ_ALWAYS		(3 << 4)
598c2ecf20Sopenharmony_ci#define DESCRIPTOR_BRANCH_ALWAYS	(3 << 2)
608c2ecf20Sopenharmony_ci#define DESCRIPTOR_WAIT			(3 << 0)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define DESCRIPTOR_CMD			(0xf << 12)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct descriptor {
658c2ecf20Sopenharmony_ci	__le16 req_count;
668c2ecf20Sopenharmony_ci	__le16 control;
678c2ecf20Sopenharmony_ci	__le32 data_address;
688c2ecf20Sopenharmony_ci	__le32 branch_address;
698c2ecf20Sopenharmony_ci	__le16 res_count;
708c2ecf20Sopenharmony_ci	__le16 transfer_status;
718c2ecf20Sopenharmony_ci} __attribute__((aligned(16)));
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define CONTROL_SET(regs)	(regs)
748c2ecf20Sopenharmony_ci#define CONTROL_CLEAR(regs)	((regs) + 4)
758c2ecf20Sopenharmony_ci#define COMMAND_PTR(regs)	((regs) + 12)
768c2ecf20Sopenharmony_ci#define CONTEXT_MATCH(regs)	((regs) + 16)
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#define AR_BUFFER_SIZE	(32*1024)
798c2ecf20Sopenharmony_ci#define AR_BUFFERS_MIN	DIV_ROUND_UP(AR_BUFFER_SIZE, PAGE_SIZE)
808c2ecf20Sopenharmony_ci/* we need at least two pages for proper list management */
818c2ecf20Sopenharmony_ci#define AR_BUFFERS	(AR_BUFFERS_MIN >= 2 ? AR_BUFFERS_MIN : 2)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define MAX_ASYNC_PAYLOAD	4096
848c2ecf20Sopenharmony_ci#define MAX_AR_PACKET_SIZE	(16 + MAX_ASYNC_PAYLOAD + 4)
858c2ecf20Sopenharmony_ci#define AR_WRAPAROUND_PAGES	DIV_ROUND_UP(MAX_AR_PACKET_SIZE, PAGE_SIZE)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistruct ar_context {
888c2ecf20Sopenharmony_ci	struct fw_ohci *ohci;
898c2ecf20Sopenharmony_ci	struct page *pages[AR_BUFFERS];
908c2ecf20Sopenharmony_ci	void *buffer;
918c2ecf20Sopenharmony_ci	struct descriptor *descriptors;
928c2ecf20Sopenharmony_ci	dma_addr_t descriptors_bus;
938c2ecf20Sopenharmony_ci	void *pointer;
948c2ecf20Sopenharmony_ci	unsigned int last_buffer_index;
958c2ecf20Sopenharmony_ci	u32 regs;
968c2ecf20Sopenharmony_ci	struct tasklet_struct tasklet;
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistruct context;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_citypedef int (*descriptor_callback_t)(struct context *ctx,
1028c2ecf20Sopenharmony_ci				     struct descriptor *d,
1038c2ecf20Sopenharmony_ci				     struct descriptor *last);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/*
1068c2ecf20Sopenharmony_ci * A buffer that contains a block of DMA-able coherent memory used for
1078c2ecf20Sopenharmony_ci * storing a portion of a DMA descriptor program.
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_cistruct descriptor_buffer {
1108c2ecf20Sopenharmony_ci	struct list_head list;
1118c2ecf20Sopenharmony_ci	dma_addr_t buffer_bus;
1128c2ecf20Sopenharmony_ci	size_t buffer_size;
1138c2ecf20Sopenharmony_ci	size_t used;
1148c2ecf20Sopenharmony_ci	struct descriptor buffer[];
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistruct context {
1188c2ecf20Sopenharmony_ci	struct fw_ohci *ohci;
1198c2ecf20Sopenharmony_ci	u32 regs;
1208c2ecf20Sopenharmony_ci	int total_allocation;
1218c2ecf20Sopenharmony_ci	u32 current_bus;
1228c2ecf20Sopenharmony_ci	bool running;
1238c2ecf20Sopenharmony_ci	bool flushing;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/*
1268c2ecf20Sopenharmony_ci	 * List of page-sized buffers for storing DMA descriptors.
1278c2ecf20Sopenharmony_ci	 * Head of list contains buffers in use and tail of list contains
1288c2ecf20Sopenharmony_ci	 * free buffers.
1298c2ecf20Sopenharmony_ci	 */
1308c2ecf20Sopenharmony_ci	struct list_head buffer_list;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	/*
1338c2ecf20Sopenharmony_ci	 * Pointer to a buffer inside buffer_list that contains the tail
1348c2ecf20Sopenharmony_ci	 * end of the current DMA program.
1358c2ecf20Sopenharmony_ci	 */
1368c2ecf20Sopenharmony_ci	struct descriptor_buffer *buffer_tail;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	/*
1398c2ecf20Sopenharmony_ci	 * The descriptor containing the branch address of the first
1408c2ecf20Sopenharmony_ci	 * descriptor that has not yet been filled by the device.
1418c2ecf20Sopenharmony_ci	 */
1428c2ecf20Sopenharmony_ci	struct descriptor *last;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/*
1458c2ecf20Sopenharmony_ci	 * The last descriptor block in the DMA program. It contains the branch
1468c2ecf20Sopenharmony_ci	 * address that must be updated upon appending a new descriptor.
1478c2ecf20Sopenharmony_ci	 */
1488c2ecf20Sopenharmony_ci	struct descriptor *prev;
1498c2ecf20Sopenharmony_ci	int prev_z;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	descriptor_callback_t callback;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	struct tasklet_struct tasklet;
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci#define IT_HEADER_SY(v)          ((v) <<  0)
1578c2ecf20Sopenharmony_ci#define IT_HEADER_TCODE(v)       ((v) <<  4)
1588c2ecf20Sopenharmony_ci#define IT_HEADER_CHANNEL(v)     ((v) <<  8)
1598c2ecf20Sopenharmony_ci#define IT_HEADER_TAG(v)         ((v) << 14)
1608c2ecf20Sopenharmony_ci#define IT_HEADER_SPEED(v)       ((v) << 16)
1618c2ecf20Sopenharmony_ci#define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistruct iso_context {
1648c2ecf20Sopenharmony_ci	struct fw_iso_context base;
1658c2ecf20Sopenharmony_ci	struct context context;
1668c2ecf20Sopenharmony_ci	void *header;
1678c2ecf20Sopenharmony_ci	size_t header_length;
1688c2ecf20Sopenharmony_ci	unsigned long flushing_completions;
1698c2ecf20Sopenharmony_ci	u32 mc_buffer_bus;
1708c2ecf20Sopenharmony_ci	u16 mc_completed;
1718c2ecf20Sopenharmony_ci	u16 last_timestamp;
1728c2ecf20Sopenharmony_ci	u8 sync;
1738c2ecf20Sopenharmony_ci	u8 tags;
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci#define CONFIG_ROM_SIZE 1024
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistruct fw_ohci {
1798c2ecf20Sopenharmony_ci	struct fw_card card;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	__iomem char *registers;
1828c2ecf20Sopenharmony_ci	int node_id;
1838c2ecf20Sopenharmony_ci	int generation;
1848c2ecf20Sopenharmony_ci	int request_generation;	/* for timestamping incoming requests */
1858c2ecf20Sopenharmony_ci	unsigned quirks;
1868c2ecf20Sopenharmony_ci	unsigned int pri_req_max;
1878c2ecf20Sopenharmony_ci	u32 bus_time;
1888c2ecf20Sopenharmony_ci	bool bus_time_running;
1898c2ecf20Sopenharmony_ci	bool is_root;
1908c2ecf20Sopenharmony_ci	bool csr_state_setclear_abdicate;
1918c2ecf20Sopenharmony_ci	int n_ir;
1928c2ecf20Sopenharmony_ci	int n_it;
1938c2ecf20Sopenharmony_ci	/*
1948c2ecf20Sopenharmony_ci	 * Spinlock for accessing fw_ohci data.  Never call out of
1958c2ecf20Sopenharmony_ci	 * this driver with this lock held.
1968c2ecf20Sopenharmony_ci	 */
1978c2ecf20Sopenharmony_ci	spinlock_t lock;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	struct mutex phy_reg_mutex;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	void *misc_buffer;
2028c2ecf20Sopenharmony_ci	dma_addr_t misc_buffer_bus;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	struct ar_context ar_request_ctx;
2058c2ecf20Sopenharmony_ci	struct ar_context ar_response_ctx;
2068c2ecf20Sopenharmony_ci	struct context at_request_ctx;
2078c2ecf20Sopenharmony_ci	struct context at_response_ctx;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	u32 it_context_support;
2108c2ecf20Sopenharmony_ci	u32 it_context_mask;     /* unoccupied IT contexts */
2118c2ecf20Sopenharmony_ci	struct iso_context *it_context_list;
2128c2ecf20Sopenharmony_ci	u64 ir_context_channels; /* unoccupied channels */
2138c2ecf20Sopenharmony_ci	u32 ir_context_support;
2148c2ecf20Sopenharmony_ci	u32 ir_context_mask;     /* unoccupied IR contexts */
2158c2ecf20Sopenharmony_ci	struct iso_context *ir_context_list;
2168c2ecf20Sopenharmony_ci	u64 mc_channels; /* channels in use by the multichannel IR context */
2178c2ecf20Sopenharmony_ci	bool mc_allocated;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	__be32    *config_rom;
2208c2ecf20Sopenharmony_ci	dma_addr_t config_rom_bus;
2218c2ecf20Sopenharmony_ci	__be32    *next_config_rom;
2228c2ecf20Sopenharmony_ci	dma_addr_t next_config_rom_bus;
2238c2ecf20Sopenharmony_ci	__be32     next_header;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	__le32    *self_id;
2268c2ecf20Sopenharmony_ci	dma_addr_t self_id_bus;
2278c2ecf20Sopenharmony_ci	struct work_struct bus_reset_work;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	u32 self_id_buffer[512];
2308c2ecf20Sopenharmony_ci};
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic struct workqueue_struct *selfid_workqueue;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic inline struct fw_ohci *fw_ohci(struct fw_card *card)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	return container_of(card, struct fw_ohci, card);
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci#define IT_CONTEXT_CYCLE_MATCH_ENABLE	0x80000000
2408c2ecf20Sopenharmony_ci#define IR_CONTEXT_BUFFER_FILL		0x80000000
2418c2ecf20Sopenharmony_ci#define IR_CONTEXT_ISOCH_HEADER		0x40000000
2428c2ecf20Sopenharmony_ci#define IR_CONTEXT_CYCLE_MATCH_ENABLE	0x20000000
2438c2ecf20Sopenharmony_ci#define IR_CONTEXT_MULTI_CHANNEL_MODE	0x10000000
2448c2ecf20Sopenharmony_ci#define IR_CONTEXT_DUAL_BUFFER_MODE	0x08000000
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci#define CONTEXT_RUN	0x8000
2478c2ecf20Sopenharmony_ci#define CONTEXT_WAKE	0x1000
2488c2ecf20Sopenharmony_ci#define CONTEXT_DEAD	0x0800
2498c2ecf20Sopenharmony_ci#define CONTEXT_ACTIVE	0x0400
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci#define OHCI1394_MAX_AT_REQ_RETRIES	0xf
2528c2ecf20Sopenharmony_ci#define OHCI1394_MAX_AT_RESP_RETRIES	0x2
2538c2ecf20Sopenharmony_ci#define OHCI1394_MAX_PHYS_RESP_RETRIES	0x8
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci#define OHCI1394_REGISTER_SIZE		0x800
2568c2ecf20Sopenharmony_ci#define OHCI1394_PCI_HCI_Control	0x40
2578c2ecf20Sopenharmony_ci#define SELF_ID_BUF_SIZE		0x800
2588c2ecf20Sopenharmony_ci#define OHCI_TCODE_PHY_PACKET		0x0e
2598c2ecf20Sopenharmony_ci#define OHCI_VERSION_1_1		0x010010
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic char ohci_driver_name[] = KBUILD_MODNAME;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci#define PCI_VENDOR_ID_PINNACLE_SYSTEMS	0x11bd
2648c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_AGERE_FW643	0x5901
2658c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_CREATIVE_SB1394	0x4001
2668c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_JMICRON_JMB38X_FW	0x2380
2678c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_TI_TSB12LV22	0x8009
2688c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_TI_TSB12LV26	0x8020
2698c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_TI_TSB82AA2	0x8025
2708c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_VIA_VT630X	0x3044
2718c2ecf20Sopenharmony_ci#define PCI_REV_ID_VIA_VT6306		0x46
2728c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_VIA_VT6315	0x3403
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci#define QUIRK_CYCLE_TIMER		0x1
2758c2ecf20Sopenharmony_ci#define QUIRK_RESET_PACKET		0x2
2768c2ecf20Sopenharmony_ci#define QUIRK_BE_HEADERS		0x4
2778c2ecf20Sopenharmony_ci#define QUIRK_NO_1394A			0x8
2788c2ecf20Sopenharmony_ci#define QUIRK_NO_MSI			0x10
2798c2ecf20Sopenharmony_ci#define QUIRK_TI_SLLZ059		0x20
2808c2ecf20Sopenharmony_ci#define QUIRK_IR_WAKE			0x40
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci// On PCI Express Root Complex in any type of AMD Ryzen machine, VIA VT6306/6307/6308 with Asmedia
2838c2ecf20Sopenharmony_ci// ASM1083/1085 brings an inconvenience that the read accesses to 'Isochronous Cycle Timer' register
2848c2ecf20Sopenharmony_ci// (at offset 0xf0 in PCI I/O space) often causes unexpected system reboot. The mechanism is not
2858c2ecf20Sopenharmony_ci// clear, since the read access to the other registers is enough safe; e.g. 'Node ID' register,
2868c2ecf20Sopenharmony_ci// while it is probable due to detection of any type of PCIe error.
2878c2ecf20Sopenharmony_ci#define QUIRK_REBOOT_BY_CYCLE_TIMER_READ	0x80000000
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_X86)
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic bool has_reboot_by_cycle_timer_read_quirk(const struct fw_ohci *ohci)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	return !!(ohci->quirks & QUIRK_REBOOT_BY_CYCLE_TIMER_READ);
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_ASMEDIA_ASM108X	0x1080
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic bool detect_vt630x_with_asm1083_on_amd_ryzen_machine(const struct pci_dev *pdev)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	const struct pci_dev *pcie_to_pci_bridge;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	// Detect any type of AMD Ryzen machine.
3038c2ecf20Sopenharmony_ci	if (!static_cpu_has(X86_FEATURE_ZEN))
3048c2ecf20Sopenharmony_ci		return false;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	// Detect VIA VT6306/6307/6308.
3078c2ecf20Sopenharmony_ci	if (pdev->vendor != PCI_VENDOR_ID_VIA)
3088c2ecf20Sopenharmony_ci		return false;
3098c2ecf20Sopenharmony_ci	if (pdev->device != PCI_DEVICE_ID_VIA_VT630X)
3108c2ecf20Sopenharmony_ci		return false;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	// Detect Asmedia ASM1083/1085.
3138c2ecf20Sopenharmony_ci	pcie_to_pci_bridge = pdev->bus->self;
3148c2ecf20Sopenharmony_ci	if (pcie_to_pci_bridge->vendor != PCI_VENDOR_ID_ASMEDIA)
3158c2ecf20Sopenharmony_ci		return false;
3168c2ecf20Sopenharmony_ci	if (pcie_to_pci_bridge->device != PCI_DEVICE_ID_ASMEDIA_ASM108X)
3178c2ecf20Sopenharmony_ci		return false;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	return true;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci#else
3238c2ecf20Sopenharmony_ci#define has_reboot_by_cycle_timer_read_quirk(ohci) false
3248c2ecf20Sopenharmony_ci#define detect_vt630x_with_asm1083_on_amd_ryzen_machine(pdev)	false
3258c2ecf20Sopenharmony_ci#endif
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci/* In case of multiple matches in ohci_quirks[], only the first one is used. */
3288c2ecf20Sopenharmony_cistatic const struct {
3298c2ecf20Sopenharmony_ci	unsigned short vendor, device, revision, flags;
3308c2ecf20Sopenharmony_ci} ohci_quirks[] = {
3318c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_AL, PCI_ANY_ID, PCI_ANY_ID,
3328c2ecf20Sopenharmony_ci		QUIRK_CYCLE_TIMER},
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_FW, PCI_ANY_ID,
3358c2ecf20Sopenharmony_ci		QUIRK_BE_HEADERS},
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_AGERE_FW643, 6,
3388c2ecf20Sopenharmony_ci		QUIRK_NO_MSI},
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_SB1394, PCI_ANY_ID,
3418c2ecf20Sopenharmony_ci		QUIRK_RESET_PACKET},
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB38X_FW, PCI_ANY_ID,
3448c2ecf20Sopenharmony_ci		QUIRK_NO_MSI},
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_NEC, PCI_ANY_ID, PCI_ANY_ID,
3478c2ecf20Sopenharmony_ci		QUIRK_CYCLE_TIMER},
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_O2, PCI_ANY_ID, PCI_ANY_ID,
3508c2ecf20Sopenharmony_ci		QUIRK_NO_MSI},
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_RICOH, PCI_ANY_ID, PCI_ANY_ID,
3538c2ecf20Sopenharmony_ci		QUIRK_CYCLE_TIMER | QUIRK_NO_MSI},
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV22, PCI_ANY_ID,
3568c2ecf20Sopenharmony_ci		QUIRK_CYCLE_TIMER | QUIRK_RESET_PACKET | QUIRK_NO_1394A},
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV26, PCI_ANY_ID,
3598c2ecf20Sopenharmony_ci		QUIRK_RESET_PACKET | QUIRK_TI_SLLZ059},
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB82AA2, PCI_ANY_ID,
3628c2ecf20Sopenharmony_ci		QUIRK_RESET_PACKET | QUIRK_TI_SLLZ059},
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_TI, PCI_ANY_ID, PCI_ANY_ID,
3658c2ecf20Sopenharmony_ci		QUIRK_RESET_PACKET},
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT630X, PCI_REV_ID_VIA_VT6306,
3688c2ecf20Sopenharmony_ci		QUIRK_CYCLE_TIMER | QUIRK_IR_WAKE},
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT6315, 0,
3718c2ecf20Sopenharmony_ci		QUIRK_CYCLE_TIMER /* FIXME: necessary? */ | QUIRK_NO_MSI},
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT6315, PCI_ANY_ID,
3748c2ecf20Sopenharmony_ci		QUIRK_NO_MSI},
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	{PCI_VENDOR_ID_VIA, PCI_ANY_ID, PCI_ANY_ID,
3778c2ecf20Sopenharmony_ci		QUIRK_CYCLE_TIMER | QUIRK_NO_MSI},
3788c2ecf20Sopenharmony_ci};
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci/* This overrides anything that was found in ohci_quirks[]. */
3818c2ecf20Sopenharmony_cistatic int param_quirks;
3828c2ecf20Sopenharmony_cimodule_param_named(quirks, param_quirks, int, 0644);
3838c2ecf20Sopenharmony_ciMODULE_PARM_DESC(quirks, "Chip quirks (default = 0"
3848c2ecf20Sopenharmony_ci	", nonatomic cycle timer = "	__stringify(QUIRK_CYCLE_TIMER)
3858c2ecf20Sopenharmony_ci	", reset packet generation = "	__stringify(QUIRK_RESET_PACKET)
3868c2ecf20Sopenharmony_ci	", AR/selfID endianness = "	__stringify(QUIRK_BE_HEADERS)
3878c2ecf20Sopenharmony_ci	", no 1394a enhancements = "	__stringify(QUIRK_NO_1394A)
3888c2ecf20Sopenharmony_ci	", disable MSI = "		__stringify(QUIRK_NO_MSI)
3898c2ecf20Sopenharmony_ci	", TI SLLZ059 erratum = "	__stringify(QUIRK_TI_SLLZ059)
3908c2ecf20Sopenharmony_ci	", IR wake unreliable = "	__stringify(QUIRK_IR_WAKE)
3918c2ecf20Sopenharmony_ci	")");
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci#define OHCI_PARAM_DEBUG_AT_AR		1
3948c2ecf20Sopenharmony_ci#define OHCI_PARAM_DEBUG_SELFIDS	2
3958c2ecf20Sopenharmony_ci#define OHCI_PARAM_DEBUG_IRQS		4
3968c2ecf20Sopenharmony_ci#define OHCI_PARAM_DEBUG_BUSRESETS	8 /* only effective before chip init */
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic int param_debug;
3998c2ecf20Sopenharmony_cimodule_param_named(debug, param_debug, int, 0644);
4008c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Verbose logging (default = 0"
4018c2ecf20Sopenharmony_ci	", AT/AR events = "	__stringify(OHCI_PARAM_DEBUG_AT_AR)
4028c2ecf20Sopenharmony_ci	", self-IDs = "		__stringify(OHCI_PARAM_DEBUG_SELFIDS)
4038c2ecf20Sopenharmony_ci	", IRQs = "		__stringify(OHCI_PARAM_DEBUG_IRQS)
4048c2ecf20Sopenharmony_ci	", busReset events = "	__stringify(OHCI_PARAM_DEBUG_BUSRESETS)
4058c2ecf20Sopenharmony_ci	", or a combination, or all = -1)");
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic bool param_remote_dma;
4088c2ecf20Sopenharmony_cimodule_param_named(remote_dma, param_remote_dma, bool, 0444);
4098c2ecf20Sopenharmony_ciMODULE_PARM_DESC(remote_dma, "Enable unfiltered remote DMA (default = N)");
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic void log_irqs(struct fw_ohci *ohci, u32 evt)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	if (likely(!(param_debug &
4148c2ecf20Sopenharmony_ci			(OHCI_PARAM_DEBUG_IRQS | OHCI_PARAM_DEBUG_BUSRESETS))))
4158c2ecf20Sopenharmony_ci		return;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (!(param_debug & OHCI_PARAM_DEBUG_IRQS) &&
4188c2ecf20Sopenharmony_ci	    !(evt & OHCI1394_busReset))
4198c2ecf20Sopenharmony_ci		return;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	ohci_notice(ohci, "IRQ %08x%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", evt,
4228c2ecf20Sopenharmony_ci	    evt & OHCI1394_selfIDComplete	? " selfID"		: "",
4238c2ecf20Sopenharmony_ci	    evt & OHCI1394_RQPkt		? " AR_req"		: "",
4248c2ecf20Sopenharmony_ci	    evt & OHCI1394_RSPkt		? " AR_resp"		: "",
4258c2ecf20Sopenharmony_ci	    evt & OHCI1394_reqTxComplete	? " AT_req"		: "",
4268c2ecf20Sopenharmony_ci	    evt & OHCI1394_respTxComplete	? " AT_resp"		: "",
4278c2ecf20Sopenharmony_ci	    evt & OHCI1394_isochRx		? " IR"			: "",
4288c2ecf20Sopenharmony_ci	    evt & OHCI1394_isochTx		? " IT"			: "",
4298c2ecf20Sopenharmony_ci	    evt & OHCI1394_postedWriteErr	? " postedWriteErr"	: "",
4308c2ecf20Sopenharmony_ci	    evt & OHCI1394_cycleTooLong		? " cycleTooLong"	: "",
4318c2ecf20Sopenharmony_ci	    evt & OHCI1394_cycle64Seconds	? " cycle64Seconds"	: "",
4328c2ecf20Sopenharmony_ci	    evt & OHCI1394_cycleInconsistent	? " cycleInconsistent"	: "",
4338c2ecf20Sopenharmony_ci	    evt & OHCI1394_regAccessFail	? " regAccessFail"	: "",
4348c2ecf20Sopenharmony_ci	    evt & OHCI1394_unrecoverableError	? " unrecoverableError"	: "",
4358c2ecf20Sopenharmony_ci	    evt & OHCI1394_busReset		? " busReset"		: "",
4368c2ecf20Sopenharmony_ci	    evt & ~(OHCI1394_selfIDComplete | OHCI1394_RQPkt |
4378c2ecf20Sopenharmony_ci		    OHCI1394_RSPkt | OHCI1394_reqTxComplete |
4388c2ecf20Sopenharmony_ci		    OHCI1394_respTxComplete | OHCI1394_isochRx |
4398c2ecf20Sopenharmony_ci		    OHCI1394_isochTx | OHCI1394_postedWriteErr |
4408c2ecf20Sopenharmony_ci		    OHCI1394_cycleTooLong | OHCI1394_cycle64Seconds |
4418c2ecf20Sopenharmony_ci		    OHCI1394_cycleInconsistent |
4428c2ecf20Sopenharmony_ci		    OHCI1394_regAccessFail | OHCI1394_busReset)
4438c2ecf20Sopenharmony_ci						? " ?"			: "");
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic const char *speed[] = {
4478c2ecf20Sopenharmony_ci	[0] = "S100", [1] = "S200", [2] = "S400",    [3] = "beta",
4488c2ecf20Sopenharmony_ci};
4498c2ecf20Sopenharmony_cistatic const char *power[] = {
4508c2ecf20Sopenharmony_ci	[0] = "+0W",  [1] = "+15W", [2] = "+30W",    [3] = "+45W",
4518c2ecf20Sopenharmony_ci	[4] = "-3W",  [5] = " ?W",  [6] = "-3..-6W", [7] = "-3..-10W",
4528c2ecf20Sopenharmony_ci};
4538c2ecf20Sopenharmony_cistatic const char port[] = { '.', '-', 'p', 'c', };
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic char _p(u32 *s, int shift)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	return port[*s >> shift & 3];
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cistatic void log_selfids(struct fw_ohci *ohci, int generation, int self_id_count)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	u32 *s;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	if (likely(!(param_debug & OHCI_PARAM_DEBUG_SELFIDS)))
4658c2ecf20Sopenharmony_ci		return;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	ohci_notice(ohci, "%d selfIDs, generation %d, local node ID %04x\n",
4688c2ecf20Sopenharmony_ci		    self_id_count, generation, ohci->node_id);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	for (s = ohci->self_id_buffer; self_id_count--; ++s)
4718c2ecf20Sopenharmony_ci		if ((*s & 1 << 23) == 0)
4728c2ecf20Sopenharmony_ci			ohci_notice(ohci,
4738c2ecf20Sopenharmony_ci			    "selfID 0: %08x, phy %d [%c%c%c] %s gc=%d %s %s%s%s\n",
4748c2ecf20Sopenharmony_ci			    *s, *s >> 24 & 63, _p(s, 6), _p(s, 4), _p(s, 2),
4758c2ecf20Sopenharmony_ci			    speed[*s >> 14 & 3], *s >> 16 & 63,
4768c2ecf20Sopenharmony_ci			    power[*s >> 8 & 7], *s >> 22 & 1 ? "L" : "",
4778c2ecf20Sopenharmony_ci			    *s >> 11 & 1 ? "c" : "", *s & 2 ? "i" : "");
4788c2ecf20Sopenharmony_ci		else
4798c2ecf20Sopenharmony_ci			ohci_notice(ohci,
4808c2ecf20Sopenharmony_ci			    "selfID n: %08x, phy %d [%c%c%c%c%c%c%c%c]\n",
4818c2ecf20Sopenharmony_ci			    *s, *s >> 24 & 63,
4828c2ecf20Sopenharmony_ci			    _p(s, 16), _p(s, 14), _p(s, 12), _p(s, 10),
4838c2ecf20Sopenharmony_ci			    _p(s,  8), _p(s,  6), _p(s,  4), _p(s,  2));
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic const char *evts[] = {
4878c2ecf20Sopenharmony_ci	[0x00] = "evt_no_status",	[0x01] = "-reserved-",
4888c2ecf20Sopenharmony_ci	[0x02] = "evt_long_packet",	[0x03] = "evt_missing_ack",
4898c2ecf20Sopenharmony_ci	[0x04] = "evt_underrun",	[0x05] = "evt_overrun",
4908c2ecf20Sopenharmony_ci	[0x06] = "evt_descriptor_read",	[0x07] = "evt_data_read",
4918c2ecf20Sopenharmony_ci	[0x08] = "evt_data_write",	[0x09] = "evt_bus_reset",
4928c2ecf20Sopenharmony_ci	[0x0a] = "evt_timeout",		[0x0b] = "evt_tcode_err",
4938c2ecf20Sopenharmony_ci	[0x0c] = "-reserved-",		[0x0d] = "-reserved-",
4948c2ecf20Sopenharmony_ci	[0x0e] = "evt_unknown",		[0x0f] = "evt_flushed",
4958c2ecf20Sopenharmony_ci	[0x10] = "-reserved-",		[0x11] = "ack_complete",
4968c2ecf20Sopenharmony_ci	[0x12] = "ack_pending ",	[0x13] = "-reserved-",
4978c2ecf20Sopenharmony_ci	[0x14] = "ack_busy_X",		[0x15] = "ack_busy_A",
4988c2ecf20Sopenharmony_ci	[0x16] = "ack_busy_B",		[0x17] = "-reserved-",
4998c2ecf20Sopenharmony_ci	[0x18] = "-reserved-",		[0x19] = "-reserved-",
5008c2ecf20Sopenharmony_ci	[0x1a] = "-reserved-",		[0x1b] = "ack_tardy",
5018c2ecf20Sopenharmony_ci	[0x1c] = "-reserved-",		[0x1d] = "ack_data_error",
5028c2ecf20Sopenharmony_ci	[0x1e] = "ack_type_error",	[0x1f] = "-reserved-",
5038c2ecf20Sopenharmony_ci	[0x20] = "pending/cancelled",
5048c2ecf20Sopenharmony_ci};
5058c2ecf20Sopenharmony_cistatic const char *tcodes[] = {
5068c2ecf20Sopenharmony_ci	[0x0] = "QW req",		[0x1] = "BW req",
5078c2ecf20Sopenharmony_ci	[0x2] = "W resp",		[0x3] = "-reserved-",
5088c2ecf20Sopenharmony_ci	[0x4] = "QR req",		[0x5] = "BR req",
5098c2ecf20Sopenharmony_ci	[0x6] = "QR resp",		[0x7] = "BR resp",
5108c2ecf20Sopenharmony_ci	[0x8] = "cycle start",		[0x9] = "Lk req",
5118c2ecf20Sopenharmony_ci	[0xa] = "async stream packet",	[0xb] = "Lk resp",
5128c2ecf20Sopenharmony_ci	[0xc] = "-reserved-",		[0xd] = "-reserved-",
5138c2ecf20Sopenharmony_ci	[0xe] = "link internal",	[0xf] = "-reserved-",
5148c2ecf20Sopenharmony_ci};
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_cistatic void log_ar_at_event(struct fw_ohci *ohci,
5178c2ecf20Sopenharmony_ci			    char dir, int speed, u32 *header, int evt)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci	int tcode = header[0] >> 4 & 0xf;
5208c2ecf20Sopenharmony_ci	char specific[12];
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	if (likely(!(param_debug & OHCI_PARAM_DEBUG_AT_AR)))
5238c2ecf20Sopenharmony_ci		return;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	if (unlikely(evt >= ARRAY_SIZE(evts)))
5268c2ecf20Sopenharmony_ci			evt = 0x1f;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	if (evt == OHCI1394_evt_bus_reset) {
5298c2ecf20Sopenharmony_ci		ohci_notice(ohci, "A%c evt_bus_reset, generation %d\n",
5308c2ecf20Sopenharmony_ci			    dir, (header[2] >> 16) & 0xff);
5318c2ecf20Sopenharmony_ci		return;
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	switch (tcode) {
5358c2ecf20Sopenharmony_ci	case 0x0: case 0x6: case 0x8:
5368c2ecf20Sopenharmony_ci		snprintf(specific, sizeof(specific), " = %08x",
5378c2ecf20Sopenharmony_ci			 be32_to_cpu((__force __be32)header[3]));
5388c2ecf20Sopenharmony_ci		break;
5398c2ecf20Sopenharmony_ci	case 0x1: case 0x5: case 0x7: case 0x9: case 0xb:
5408c2ecf20Sopenharmony_ci		snprintf(specific, sizeof(specific), " %x,%x",
5418c2ecf20Sopenharmony_ci			 header[3] >> 16, header[3] & 0xffff);
5428c2ecf20Sopenharmony_ci		break;
5438c2ecf20Sopenharmony_ci	default:
5448c2ecf20Sopenharmony_ci		specific[0] = '\0';
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	switch (tcode) {
5488c2ecf20Sopenharmony_ci	case 0xa:
5498c2ecf20Sopenharmony_ci		ohci_notice(ohci, "A%c %s, %s\n",
5508c2ecf20Sopenharmony_ci			    dir, evts[evt], tcodes[tcode]);
5518c2ecf20Sopenharmony_ci		break;
5528c2ecf20Sopenharmony_ci	case 0xe:
5538c2ecf20Sopenharmony_ci		ohci_notice(ohci, "A%c %s, PHY %08x %08x\n",
5548c2ecf20Sopenharmony_ci			    dir, evts[evt], header[1], header[2]);
5558c2ecf20Sopenharmony_ci		break;
5568c2ecf20Sopenharmony_ci	case 0x0: case 0x1: case 0x4: case 0x5: case 0x9:
5578c2ecf20Sopenharmony_ci		ohci_notice(ohci,
5588c2ecf20Sopenharmony_ci			    "A%c spd %x tl %02x, %04x -> %04x, %s, %s, %04x%08x%s\n",
5598c2ecf20Sopenharmony_ci			    dir, speed, header[0] >> 10 & 0x3f,
5608c2ecf20Sopenharmony_ci			    header[1] >> 16, header[0] >> 16, evts[evt],
5618c2ecf20Sopenharmony_ci			    tcodes[tcode], header[1] & 0xffff, header[2], specific);
5628c2ecf20Sopenharmony_ci		break;
5638c2ecf20Sopenharmony_ci	default:
5648c2ecf20Sopenharmony_ci		ohci_notice(ohci,
5658c2ecf20Sopenharmony_ci			    "A%c spd %x tl %02x, %04x -> %04x, %s, %s%s\n",
5668c2ecf20Sopenharmony_ci			    dir, speed, header[0] >> 10 & 0x3f,
5678c2ecf20Sopenharmony_ci			    header[1] >> 16, header[0] >> 16, evts[evt],
5688c2ecf20Sopenharmony_ci			    tcodes[tcode], specific);
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	writel(data, ohci->registers + offset);
5758c2ecf20Sopenharmony_ci}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_cistatic inline u32 reg_read(const struct fw_ohci *ohci, int offset)
5788c2ecf20Sopenharmony_ci{
5798c2ecf20Sopenharmony_ci	return readl(ohci->registers + offset);
5808c2ecf20Sopenharmony_ci}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_cistatic inline void flush_writes(const struct fw_ohci *ohci)
5838c2ecf20Sopenharmony_ci{
5848c2ecf20Sopenharmony_ci	/* Do a dummy read to flush writes. */
5858c2ecf20Sopenharmony_ci	reg_read(ohci, OHCI1394_Version);
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci/*
5898c2ecf20Sopenharmony_ci * Beware!  read_phy_reg(), write_phy_reg(), update_phy_reg(), and
5908c2ecf20Sopenharmony_ci * read_paged_phy_reg() require the caller to hold ohci->phy_reg_mutex.
5918c2ecf20Sopenharmony_ci * In other words, only use ohci_read_phy_reg() and ohci_update_phy_reg()
5928c2ecf20Sopenharmony_ci * directly.  Exceptions are intrinsically serialized contexts like pci_probe.
5938c2ecf20Sopenharmony_ci */
5948c2ecf20Sopenharmony_cistatic int read_phy_reg(struct fw_ohci *ohci, int addr)
5958c2ecf20Sopenharmony_ci{
5968c2ecf20Sopenharmony_ci	u32 val;
5978c2ecf20Sopenharmony_ci	int i;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
6008c2ecf20Sopenharmony_ci	for (i = 0; i < 3 + 100; i++) {
6018c2ecf20Sopenharmony_ci		val = reg_read(ohci, OHCI1394_PhyControl);
6028c2ecf20Sopenharmony_ci		if (!~val)
6038c2ecf20Sopenharmony_ci			return -ENODEV; /* Card was ejected. */
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci		if (val & OHCI1394_PhyControl_ReadDone)
6068c2ecf20Sopenharmony_ci			return OHCI1394_PhyControl_ReadData(val);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		/*
6098c2ecf20Sopenharmony_ci		 * Try a few times without waiting.  Sleeping is necessary
6108c2ecf20Sopenharmony_ci		 * only when the link/PHY interface is busy.
6118c2ecf20Sopenharmony_ci		 */
6128c2ecf20Sopenharmony_ci		if (i >= 3)
6138c2ecf20Sopenharmony_ci			msleep(1);
6148c2ecf20Sopenharmony_ci	}
6158c2ecf20Sopenharmony_ci	ohci_err(ohci, "failed to read phy reg %d\n", addr);
6168c2ecf20Sopenharmony_ci	dump_stack();
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	return -EBUSY;
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_cistatic int write_phy_reg(const struct fw_ohci *ohci, int addr, u32 val)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	int i;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_PhyControl,
6268c2ecf20Sopenharmony_ci		  OHCI1394_PhyControl_Write(addr, val));
6278c2ecf20Sopenharmony_ci	for (i = 0; i < 3 + 100; i++) {
6288c2ecf20Sopenharmony_ci		val = reg_read(ohci, OHCI1394_PhyControl);
6298c2ecf20Sopenharmony_ci		if (!~val)
6308c2ecf20Sopenharmony_ci			return -ENODEV; /* Card was ejected. */
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci		if (!(val & OHCI1394_PhyControl_WritePending))
6338c2ecf20Sopenharmony_ci			return 0;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci		if (i >= 3)
6368c2ecf20Sopenharmony_ci			msleep(1);
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci	ohci_err(ohci, "failed to write phy reg %d, val %u\n", addr, val);
6398c2ecf20Sopenharmony_ci	dump_stack();
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	return -EBUSY;
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_cistatic int update_phy_reg(struct fw_ohci *ohci, int addr,
6458c2ecf20Sopenharmony_ci			  int clear_bits, int set_bits)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	int ret = read_phy_reg(ohci, addr);
6488c2ecf20Sopenharmony_ci	if (ret < 0)
6498c2ecf20Sopenharmony_ci		return ret;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	/*
6528c2ecf20Sopenharmony_ci	 * The interrupt status bits are cleared by writing a one bit.
6538c2ecf20Sopenharmony_ci	 * Avoid clearing them unless explicitly requested in set_bits.
6548c2ecf20Sopenharmony_ci	 */
6558c2ecf20Sopenharmony_ci	if (addr == 5)
6568c2ecf20Sopenharmony_ci		clear_bits |= PHY_INT_STATUS_BITS;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	return write_phy_reg(ohci, addr, (ret & ~clear_bits) | set_bits);
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic int read_paged_phy_reg(struct fw_ohci *ohci, int page, int addr)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	int ret;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	ret = update_phy_reg(ohci, 7, PHY_PAGE_SELECT, page << 5);
6668c2ecf20Sopenharmony_ci	if (ret < 0)
6678c2ecf20Sopenharmony_ci		return ret;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	return read_phy_reg(ohci, addr);
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic int ohci_read_phy_reg(struct fw_card *card, int addr)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
6758c2ecf20Sopenharmony_ci	int ret;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	mutex_lock(&ohci->phy_reg_mutex);
6788c2ecf20Sopenharmony_ci	ret = read_phy_reg(ohci, addr);
6798c2ecf20Sopenharmony_ci	mutex_unlock(&ohci->phy_reg_mutex);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	return ret;
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_cistatic int ohci_update_phy_reg(struct fw_card *card, int addr,
6858c2ecf20Sopenharmony_ci			       int clear_bits, int set_bits)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
6888c2ecf20Sopenharmony_ci	int ret;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	mutex_lock(&ohci->phy_reg_mutex);
6918c2ecf20Sopenharmony_ci	ret = update_phy_reg(ohci, addr, clear_bits, set_bits);
6928c2ecf20Sopenharmony_ci	mutex_unlock(&ohci->phy_reg_mutex);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	return ret;
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic inline dma_addr_t ar_buffer_bus(struct ar_context *ctx, unsigned int i)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	return page_private(ctx->pages[i]);
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic void ar_context_link_page(struct ar_context *ctx, unsigned int index)
7038c2ecf20Sopenharmony_ci{
7048c2ecf20Sopenharmony_ci	struct descriptor *d;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	d = &ctx->descriptors[index];
7078c2ecf20Sopenharmony_ci	d->branch_address  &= cpu_to_le32(~0xf);
7088c2ecf20Sopenharmony_ci	d->res_count       =  cpu_to_le16(PAGE_SIZE);
7098c2ecf20Sopenharmony_ci	d->transfer_status =  0;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	wmb(); /* finish init of new descriptors before branch_address update */
7128c2ecf20Sopenharmony_ci	d = &ctx->descriptors[ctx->last_buffer_index];
7138c2ecf20Sopenharmony_ci	d->branch_address  |= cpu_to_le32(1);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	ctx->last_buffer_index = index;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic void ar_context_release(struct ar_context *ctx)
7218c2ecf20Sopenharmony_ci{
7228c2ecf20Sopenharmony_ci	struct device *dev = ctx->ohci->card.device;
7238c2ecf20Sopenharmony_ci	unsigned int i;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	vunmap(ctx->buffer);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	for (i = 0; i < AR_BUFFERS; i++) {
7288c2ecf20Sopenharmony_ci		if (ctx->pages[i])
7298c2ecf20Sopenharmony_ci			dma_free_pages(dev, PAGE_SIZE, ctx->pages[i],
7308c2ecf20Sopenharmony_ci				       ar_buffer_bus(ctx, i), DMA_FROM_DEVICE);
7318c2ecf20Sopenharmony_ci	}
7328c2ecf20Sopenharmony_ci}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_cistatic void ar_context_abort(struct ar_context *ctx, const char *error_msg)
7358c2ecf20Sopenharmony_ci{
7368c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = ctx->ohci;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	if (reg_read(ohci, CONTROL_CLEAR(ctx->regs)) & CONTEXT_RUN) {
7398c2ecf20Sopenharmony_ci		reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
7408c2ecf20Sopenharmony_ci		flush_writes(ohci);
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci		ohci_err(ohci, "AR error: %s; DMA stopped\n", error_msg);
7438c2ecf20Sopenharmony_ci	}
7448c2ecf20Sopenharmony_ci	/* FIXME: restart? */
7458c2ecf20Sopenharmony_ci}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_cistatic inline unsigned int ar_next_buffer_index(unsigned int index)
7488c2ecf20Sopenharmony_ci{
7498c2ecf20Sopenharmony_ci	return (index + 1) % AR_BUFFERS;
7508c2ecf20Sopenharmony_ci}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_cistatic inline unsigned int ar_first_buffer_index(struct ar_context *ctx)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	return ar_next_buffer_index(ctx->last_buffer_index);
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci/*
7588c2ecf20Sopenharmony_ci * We search for the buffer that contains the last AR packet DMA data written
7598c2ecf20Sopenharmony_ci * by the controller.
7608c2ecf20Sopenharmony_ci */
7618c2ecf20Sopenharmony_cistatic unsigned int ar_search_last_active_buffer(struct ar_context *ctx,
7628c2ecf20Sopenharmony_ci						 unsigned int *buffer_offset)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	unsigned int i, next_i, last = ctx->last_buffer_index;
7658c2ecf20Sopenharmony_ci	__le16 res_count, next_res_count;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	i = ar_first_buffer_index(ctx);
7688c2ecf20Sopenharmony_ci	res_count = READ_ONCE(ctx->descriptors[i].res_count);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	/* A buffer that is not yet completely filled must be the last one. */
7718c2ecf20Sopenharmony_ci	while (i != last && res_count == 0) {
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci		/* Peek at the next descriptor. */
7748c2ecf20Sopenharmony_ci		next_i = ar_next_buffer_index(i);
7758c2ecf20Sopenharmony_ci		rmb(); /* read descriptors in order */
7768c2ecf20Sopenharmony_ci		next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count);
7778c2ecf20Sopenharmony_ci		/*
7788c2ecf20Sopenharmony_ci		 * If the next descriptor is still empty, we must stop at this
7798c2ecf20Sopenharmony_ci		 * descriptor.
7808c2ecf20Sopenharmony_ci		 */
7818c2ecf20Sopenharmony_ci		if (next_res_count == cpu_to_le16(PAGE_SIZE)) {
7828c2ecf20Sopenharmony_ci			/*
7838c2ecf20Sopenharmony_ci			 * The exception is when the DMA data for one packet is
7848c2ecf20Sopenharmony_ci			 * split over three buffers; in this case, the middle
7858c2ecf20Sopenharmony_ci			 * buffer's descriptor might be never updated by the
7868c2ecf20Sopenharmony_ci			 * controller and look still empty, and we have to peek
7878c2ecf20Sopenharmony_ci			 * at the third one.
7888c2ecf20Sopenharmony_ci			 */
7898c2ecf20Sopenharmony_ci			if (MAX_AR_PACKET_SIZE > PAGE_SIZE && i != last) {
7908c2ecf20Sopenharmony_ci				next_i = ar_next_buffer_index(next_i);
7918c2ecf20Sopenharmony_ci				rmb();
7928c2ecf20Sopenharmony_ci				next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count);
7938c2ecf20Sopenharmony_ci				if (next_res_count != cpu_to_le16(PAGE_SIZE))
7948c2ecf20Sopenharmony_ci					goto next_buffer_is_active;
7958c2ecf20Sopenharmony_ci			}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci			break;
7988c2ecf20Sopenharmony_ci		}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_cinext_buffer_is_active:
8018c2ecf20Sopenharmony_ci		i = next_i;
8028c2ecf20Sopenharmony_ci		res_count = next_res_count;
8038c2ecf20Sopenharmony_ci	}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	rmb(); /* read res_count before the DMA data */
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	*buffer_offset = PAGE_SIZE - le16_to_cpu(res_count);
8088c2ecf20Sopenharmony_ci	if (*buffer_offset > PAGE_SIZE) {
8098c2ecf20Sopenharmony_ci		*buffer_offset = 0;
8108c2ecf20Sopenharmony_ci		ar_context_abort(ctx, "corrupted descriptor");
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	return i;
8148c2ecf20Sopenharmony_ci}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_cistatic void ar_sync_buffers_for_cpu(struct ar_context *ctx,
8178c2ecf20Sopenharmony_ci				    unsigned int end_buffer_index,
8188c2ecf20Sopenharmony_ci				    unsigned int end_buffer_offset)
8198c2ecf20Sopenharmony_ci{
8208c2ecf20Sopenharmony_ci	unsigned int i;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	i = ar_first_buffer_index(ctx);
8238c2ecf20Sopenharmony_ci	while (i != end_buffer_index) {
8248c2ecf20Sopenharmony_ci		dma_sync_single_for_cpu(ctx->ohci->card.device,
8258c2ecf20Sopenharmony_ci					ar_buffer_bus(ctx, i),
8268c2ecf20Sopenharmony_ci					PAGE_SIZE, DMA_FROM_DEVICE);
8278c2ecf20Sopenharmony_ci		i = ar_next_buffer_index(i);
8288c2ecf20Sopenharmony_ci	}
8298c2ecf20Sopenharmony_ci	if (end_buffer_offset > 0)
8308c2ecf20Sopenharmony_ci		dma_sync_single_for_cpu(ctx->ohci->card.device,
8318c2ecf20Sopenharmony_ci					ar_buffer_bus(ctx, i),
8328c2ecf20Sopenharmony_ci					end_buffer_offset, DMA_FROM_DEVICE);
8338c2ecf20Sopenharmony_ci}
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
8368c2ecf20Sopenharmony_ci#define cond_le32_to_cpu(v) \
8378c2ecf20Sopenharmony_ci	(ohci->quirks & QUIRK_BE_HEADERS ? (__force __u32)(v) : le32_to_cpu(v))
8388c2ecf20Sopenharmony_ci#else
8398c2ecf20Sopenharmony_ci#define cond_le32_to_cpu(v) le32_to_cpu(v)
8408c2ecf20Sopenharmony_ci#endif
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_cistatic __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
8438c2ecf20Sopenharmony_ci{
8448c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = ctx->ohci;
8458c2ecf20Sopenharmony_ci	struct fw_packet p;
8468c2ecf20Sopenharmony_ci	u32 status, length, tcode;
8478c2ecf20Sopenharmony_ci	int evt;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	p.header[0] = cond_le32_to_cpu(buffer[0]);
8508c2ecf20Sopenharmony_ci	p.header[1] = cond_le32_to_cpu(buffer[1]);
8518c2ecf20Sopenharmony_ci	p.header[2] = cond_le32_to_cpu(buffer[2]);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	tcode = (p.header[0] >> 4) & 0x0f;
8548c2ecf20Sopenharmony_ci	switch (tcode) {
8558c2ecf20Sopenharmony_ci	case TCODE_WRITE_QUADLET_REQUEST:
8568c2ecf20Sopenharmony_ci	case TCODE_READ_QUADLET_RESPONSE:
8578c2ecf20Sopenharmony_ci		p.header[3] = (__force __u32) buffer[3];
8588c2ecf20Sopenharmony_ci		p.header_length = 16;
8598c2ecf20Sopenharmony_ci		p.payload_length = 0;
8608c2ecf20Sopenharmony_ci		break;
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	case TCODE_READ_BLOCK_REQUEST :
8638c2ecf20Sopenharmony_ci		p.header[3] = cond_le32_to_cpu(buffer[3]);
8648c2ecf20Sopenharmony_ci		p.header_length = 16;
8658c2ecf20Sopenharmony_ci		p.payload_length = 0;
8668c2ecf20Sopenharmony_ci		break;
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	case TCODE_WRITE_BLOCK_REQUEST:
8698c2ecf20Sopenharmony_ci	case TCODE_READ_BLOCK_RESPONSE:
8708c2ecf20Sopenharmony_ci	case TCODE_LOCK_REQUEST:
8718c2ecf20Sopenharmony_ci	case TCODE_LOCK_RESPONSE:
8728c2ecf20Sopenharmony_ci		p.header[3] = cond_le32_to_cpu(buffer[3]);
8738c2ecf20Sopenharmony_ci		p.header_length = 16;
8748c2ecf20Sopenharmony_ci		p.payload_length = p.header[3] >> 16;
8758c2ecf20Sopenharmony_ci		if (p.payload_length > MAX_ASYNC_PAYLOAD) {
8768c2ecf20Sopenharmony_ci			ar_context_abort(ctx, "invalid packet length");
8778c2ecf20Sopenharmony_ci			return NULL;
8788c2ecf20Sopenharmony_ci		}
8798c2ecf20Sopenharmony_ci		break;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	case TCODE_WRITE_RESPONSE:
8828c2ecf20Sopenharmony_ci	case TCODE_READ_QUADLET_REQUEST:
8838c2ecf20Sopenharmony_ci	case OHCI_TCODE_PHY_PACKET:
8848c2ecf20Sopenharmony_ci		p.header_length = 12;
8858c2ecf20Sopenharmony_ci		p.payload_length = 0;
8868c2ecf20Sopenharmony_ci		break;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	default:
8898c2ecf20Sopenharmony_ci		ar_context_abort(ctx, "invalid tcode");
8908c2ecf20Sopenharmony_ci		return NULL;
8918c2ecf20Sopenharmony_ci	}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	p.payload = (void *) buffer + p.header_length;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* FIXME: What to do about evt_* errors? */
8968c2ecf20Sopenharmony_ci	length = (p.header_length + p.payload_length + 3) / 4;
8978c2ecf20Sopenharmony_ci	status = cond_le32_to_cpu(buffer[length]);
8988c2ecf20Sopenharmony_ci	evt    = (status >> 16) & 0x1f;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	p.ack        = evt - 16;
9018c2ecf20Sopenharmony_ci	p.speed      = (status >> 21) & 0x7;
9028c2ecf20Sopenharmony_ci	p.timestamp  = status & 0xffff;
9038c2ecf20Sopenharmony_ci	p.generation = ohci->request_generation;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	log_ar_at_event(ohci, 'R', p.speed, p.header, evt);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	/*
9088c2ecf20Sopenharmony_ci	 * Several controllers, notably from NEC and VIA, forget to
9098c2ecf20Sopenharmony_ci	 * write ack_complete status at PHY packet reception.
9108c2ecf20Sopenharmony_ci	 */
9118c2ecf20Sopenharmony_ci	if (evt == OHCI1394_evt_no_status &&
9128c2ecf20Sopenharmony_ci	    (p.header[0] & 0xff) == (OHCI1394_phy_tcode << 4))
9138c2ecf20Sopenharmony_ci		p.ack = ACK_COMPLETE;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	/*
9168c2ecf20Sopenharmony_ci	 * The OHCI bus reset handler synthesizes a PHY packet with
9178c2ecf20Sopenharmony_ci	 * the new generation number when a bus reset happens (see
9188c2ecf20Sopenharmony_ci	 * section 8.4.2.3).  This helps us determine when a request
9198c2ecf20Sopenharmony_ci	 * was received and make sure we send the response in the same
9208c2ecf20Sopenharmony_ci	 * generation.  We only need this for requests; for responses
9218c2ecf20Sopenharmony_ci	 * we use the unique tlabel for finding the matching
9228c2ecf20Sopenharmony_ci	 * request.
9238c2ecf20Sopenharmony_ci	 *
9248c2ecf20Sopenharmony_ci	 * Alas some chips sometimes emit bus reset packets with a
9258c2ecf20Sopenharmony_ci	 * wrong generation.  We set the correct generation for these
9268c2ecf20Sopenharmony_ci	 * at a slightly incorrect time (in bus_reset_work).
9278c2ecf20Sopenharmony_ci	 */
9288c2ecf20Sopenharmony_ci	if (evt == OHCI1394_evt_bus_reset) {
9298c2ecf20Sopenharmony_ci		if (!(ohci->quirks & QUIRK_RESET_PACKET))
9308c2ecf20Sopenharmony_ci			ohci->request_generation = (p.header[2] >> 16) & 0xff;
9318c2ecf20Sopenharmony_ci	} else if (ctx == &ohci->ar_request_ctx) {
9328c2ecf20Sopenharmony_ci		fw_core_handle_request(&ohci->card, &p);
9338c2ecf20Sopenharmony_ci	} else {
9348c2ecf20Sopenharmony_ci		fw_core_handle_response(&ohci->card, &p);
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	return buffer + length + 1;
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cistatic void *handle_ar_packets(struct ar_context *ctx, void *p, void *end)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	void *next;
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	while (p < end) {
9458c2ecf20Sopenharmony_ci		next = handle_ar_packet(ctx, p);
9468c2ecf20Sopenharmony_ci		if (!next)
9478c2ecf20Sopenharmony_ci			return p;
9488c2ecf20Sopenharmony_ci		p = next;
9498c2ecf20Sopenharmony_ci	}
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	return p;
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_cistatic void ar_recycle_buffers(struct ar_context *ctx, unsigned int end_buffer)
9558c2ecf20Sopenharmony_ci{
9568c2ecf20Sopenharmony_ci	unsigned int i;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	i = ar_first_buffer_index(ctx);
9598c2ecf20Sopenharmony_ci	while (i != end_buffer) {
9608c2ecf20Sopenharmony_ci		dma_sync_single_for_device(ctx->ohci->card.device,
9618c2ecf20Sopenharmony_ci					   ar_buffer_bus(ctx, i),
9628c2ecf20Sopenharmony_ci					   PAGE_SIZE, DMA_FROM_DEVICE);
9638c2ecf20Sopenharmony_ci		ar_context_link_page(ctx, i);
9648c2ecf20Sopenharmony_ci		i = ar_next_buffer_index(i);
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_cistatic void ar_context_tasklet(unsigned long data)
9698c2ecf20Sopenharmony_ci{
9708c2ecf20Sopenharmony_ci	struct ar_context *ctx = (struct ar_context *)data;
9718c2ecf20Sopenharmony_ci	unsigned int end_buffer_index, end_buffer_offset;
9728c2ecf20Sopenharmony_ci	void *p, *end;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	p = ctx->pointer;
9758c2ecf20Sopenharmony_ci	if (!p)
9768c2ecf20Sopenharmony_ci		return;
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	end_buffer_index = ar_search_last_active_buffer(ctx,
9798c2ecf20Sopenharmony_ci							&end_buffer_offset);
9808c2ecf20Sopenharmony_ci	ar_sync_buffers_for_cpu(ctx, end_buffer_index, end_buffer_offset);
9818c2ecf20Sopenharmony_ci	end = ctx->buffer + end_buffer_index * PAGE_SIZE + end_buffer_offset;
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	if (end_buffer_index < ar_first_buffer_index(ctx)) {
9848c2ecf20Sopenharmony_ci		/*
9858c2ecf20Sopenharmony_ci		 * The filled part of the overall buffer wraps around; handle
9868c2ecf20Sopenharmony_ci		 * all packets up to the buffer end here.  If the last packet
9878c2ecf20Sopenharmony_ci		 * wraps around, its tail will be visible after the buffer end
9888c2ecf20Sopenharmony_ci		 * because the buffer start pages are mapped there again.
9898c2ecf20Sopenharmony_ci		 */
9908c2ecf20Sopenharmony_ci		void *buffer_end = ctx->buffer + AR_BUFFERS * PAGE_SIZE;
9918c2ecf20Sopenharmony_ci		p = handle_ar_packets(ctx, p, buffer_end);
9928c2ecf20Sopenharmony_ci		if (p < buffer_end)
9938c2ecf20Sopenharmony_ci			goto error;
9948c2ecf20Sopenharmony_ci		/* adjust p to point back into the actual buffer */
9958c2ecf20Sopenharmony_ci		p -= AR_BUFFERS * PAGE_SIZE;
9968c2ecf20Sopenharmony_ci	}
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	p = handle_ar_packets(ctx, p, end);
9998c2ecf20Sopenharmony_ci	if (p != end) {
10008c2ecf20Sopenharmony_ci		if (p > end)
10018c2ecf20Sopenharmony_ci			ar_context_abort(ctx, "inconsistent descriptor");
10028c2ecf20Sopenharmony_ci		goto error;
10038c2ecf20Sopenharmony_ci	}
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	ctx->pointer = p;
10068c2ecf20Sopenharmony_ci	ar_recycle_buffers(ctx, end_buffer_index);
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	return;
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_cierror:
10118c2ecf20Sopenharmony_ci	ctx->pointer = NULL;
10128c2ecf20Sopenharmony_ci}
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_cistatic int ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci,
10158c2ecf20Sopenharmony_ci			   unsigned int descriptors_offset, u32 regs)
10168c2ecf20Sopenharmony_ci{
10178c2ecf20Sopenharmony_ci	struct device *dev = ohci->card.device;
10188c2ecf20Sopenharmony_ci	unsigned int i;
10198c2ecf20Sopenharmony_ci	dma_addr_t dma_addr;
10208c2ecf20Sopenharmony_ci	struct page *pages[AR_BUFFERS + AR_WRAPAROUND_PAGES];
10218c2ecf20Sopenharmony_ci	struct descriptor *d;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	ctx->regs        = regs;
10248c2ecf20Sopenharmony_ci	ctx->ohci        = ohci;
10258c2ecf20Sopenharmony_ci	tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	for (i = 0; i < AR_BUFFERS; i++) {
10288c2ecf20Sopenharmony_ci		ctx->pages[i] = dma_alloc_pages(dev, PAGE_SIZE, &dma_addr,
10298c2ecf20Sopenharmony_ci						DMA_FROM_DEVICE, GFP_KERNEL);
10308c2ecf20Sopenharmony_ci		if (!ctx->pages[i])
10318c2ecf20Sopenharmony_ci			goto out_of_memory;
10328c2ecf20Sopenharmony_ci		set_page_private(ctx->pages[i], dma_addr);
10338c2ecf20Sopenharmony_ci		dma_sync_single_for_device(dev, dma_addr, PAGE_SIZE,
10348c2ecf20Sopenharmony_ci					   DMA_FROM_DEVICE);
10358c2ecf20Sopenharmony_ci	}
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	for (i = 0; i < AR_BUFFERS; i++)
10388c2ecf20Sopenharmony_ci		pages[i]              = ctx->pages[i];
10398c2ecf20Sopenharmony_ci	for (i = 0; i < AR_WRAPAROUND_PAGES; i++)
10408c2ecf20Sopenharmony_ci		pages[AR_BUFFERS + i] = ctx->pages[i];
10418c2ecf20Sopenharmony_ci	ctx->buffer = vmap(pages, ARRAY_SIZE(pages), VM_MAP, PAGE_KERNEL);
10428c2ecf20Sopenharmony_ci	if (!ctx->buffer)
10438c2ecf20Sopenharmony_ci		goto out_of_memory;
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	ctx->descriptors     = ohci->misc_buffer     + descriptors_offset;
10468c2ecf20Sopenharmony_ci	ctx->descriptors_bus = ohci->misc_buffer_bus + descriptors_offset;
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	for (i = 0; i < AR_BUFFERS; i++) {
10498c2ecf20Sopenharmony_ci		d = &ctx->descriptors[i];
10508c2ecf20Sopenharmony_ci		d->req_count      = cpu_to_le16(PAGE_SIZE);
10518c2ecf20Sopenharmony_ci		d->control        = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
10528c2ecf20Sopenharmony_ci						DESCRIPTOR_STATUS |
10538c2ecf20Sopenharmony_ci						DESCRIPTOR_BRANCH_ALWAYS);
10548c2ecf20Sopenharmony_ci		d->data_address   = cpu_to_le32(ar_buffer_bus(ctx, i));
10558c2ecf20Sopenharmony_ci		d->branch_address = cpu_to_le32(ctx->descriptors_bus +
10568c2ecf20Sopenharmony_ci			ar_next_buffer_index(i) * sizeof(struct descriptor));
10578c2ecf20Sopenharmony_ci	}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	return 0;
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ciout_of_memory:
10628c2ecf20Sopenharmony_ci	ar_context_release(ctx);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	return -ENOMEM;
10658c2ecf20Sopenharmony_ci}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_cistatic void ar_context_run(struct ar_context *ctx)
10688c2ecf20Sopenharmony_ci{
10698c2ecf20Sopenharmony_ci	unsigned int i;
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	for (i = 0; i < AR_BUFFERS; i++)
10728c2ecf20Sopenharmony_ci		ar_context_link_page(ctx, i);
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	ctx->pointer = ctx->buffer;
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ctx->descriptors_bus | 1);
10778c2ecf20Sopenharmony_ci	reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
10788c2ecf20Sopenharmony_ci}
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_cistatic struct descriptor *find_branch_descriptor(struct descriptor *d, int z)
10818c2ecf20Sopenharmony_ci{
10828c2ecf20Sopenharmony_ci	__le16 branch;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	branch = d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS);
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	/* figure out which descriptor the branch address goes in */
10878c2ecf20Sopenharmony_ci	if (z == 2 && branch == cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
10888c2ecf20Sopenharmony_ci		return d;
10898c2ecf20Sopenharmony_ci	else
10908c2ecf20Sopenharmony_ci		return d + z - 1;
10918c2ecf20Sopenharmony_ci}
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_cistatic void context_tasklet(unsigned long data)
10948c2ecf20Sopenharmony_ci{
10958c2ecf20Sopenharmony_ci	struct context *ctx = (struct context *) data;
10968c2ecf20Sopenharmony_ci	struct descriptor *d, *last;
10978c2ecf20Sopenharmony_ci	u32 address;
10988c2ecf20Sopenharmony_ci	int z;
10998c2ecf20Sopenharmony_ci	struct descriptor_buffer *desc;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	desc = list_entry(ctx->buffer_list.next,
11028c2ecf20Sopenharmony_ci			struct descriptor_buffer, list);
11038c2ecf20Sopenharmony_ci	last = ctx->last;
11048c2ecf20Sopenharmony_ci	while (last->branch_address != 0) {
11058c2ecf20Sopenharmony_ci		struct descriptor_buffer *old_desc = desc;
11068c2ecf20Sopenharmony_ci		address = le32_to_cpu(last->branch_address);
11078c2ecf20Sopenharmony_ci		z = address & 0xf;
11088c2ecf20Sopenharmony_ci		address &= ~0xf;
11098c2ecf20Sopenharmony_ci		ctx->current_bus = address;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci		/* If the branch address points to a buffer outside of the
11128c2ecf20Sopenharmony_ci		 * current buffer, advance to the next buffer. */
11138c2ecf20Sopenharmony_ci		if (address < desc->buffer_bus ||
11148c2ecf20Sopenharmony_ci				address >= desc->buffer_bus + desc->used)
11158c2ecf20Sopenharmony_ci			desc = list_entry(desc->list.next,
11168c2ecf20Sopenharmony_ci					struct descriptor_buffer, list);
11178c2ecf20Sopenharmony_ci		d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
11188c2ecf20Sopenharmony_ci		last = find_branch_descriptor(d, z);
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci		if (!ctx->callback(ctx, d, last))
11218c2ecf20Sopenharmony_ci			break;
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci		if (old_desc != desc) {
11248c2ecf20Sopenharmony_ci			/* If we've advanced to the next buffer, move the
11258c2ecf20Sopenharmony_ci			 * previous buffer to the free list. */
11268c2ecf20Sopenharmony_ci			unsigned long flags;
11278c2ecf20Sopenharmony_ci			old_desc->used = 0;
11288c2ecf20Sopenharmony_ci			spin_lock_irqsave(&ctx->ohci->lock, flags);
11298c2ecf20Sopenharmony_ci			list_move_tail(&old_desc->list, &ctx->buffer_list);
11308c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&ctx->ohci->lock, flags);
11318c2ecf20Sopenharmony_ci		}
11328c2ecf20Sopenharmony_ci		ctx->last = last;
11338c2ecf20Sopenharmony_ci	}
11348c2ecf20Sopenharmony_ci}
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci/*
11378c2ecf20Sopenharmony_ci * Allocate a new buffer and add it to the list of free buffers for this
11388c2ecf20Sopenharmony_ci * context.  Must be called with ohci->lock held.
11398c2ecf20Sopenharmony_ci */
11408c2ecf20Sopenharmony_cistatic int context_add_buffer(struct context *ctx)
11418c2ecf20Sopenharmony_ci{
11428c2ecf20Sopenharmony_ci	struct descriptor_buffer *desc;
11438c2ecf20Sopenharmony_ci	dma_addr_t bus_addr;
11448c2ecf20Sopenharmony_ci	int offset;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	/*
11478c2ecf20Sopenharmony_ci	 * 16MB of descriptors should be far more than enough for any DMA
11488c2ecf20Sopenharmony_ci	 * program.  This will catch run-away userspace or DoS attacks.
11498c2ecf20Sopenharmony_ci	 */
11508c2ecf20Sopenharmony_ci	if (ctx->total_allocation >= 16*1024*1024)
11518c2ecf20Sopenharmony_ci		return -ENOMEM;
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	desc = dma_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE,
11548c2ecf20Sopenharmony_ci			&bus_addr, GFP_ATOMIC);
11558c2ecf20Sopenharmony_ci	if (!desc)
11568c2ecf20Sopenharmony_ci		return -ENOMEM;
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	offset = (void *)&desc->buffer - (void *)desc;
11598c2ecf20Sopenharmony_ci	/*
11608c2ecf20Sopenharmony_ci	 * Some controllers, like JMicron ones, always issue 0x20-byte DMA reads
11618c2ecf20Sopenharmony_ci	 * for descriptors, even 0x10-byte ones. This can cause page faults when
11628c2ecf20Sopenharmony_ci	 * an IOMMU is in use and the oversized read crosses a page boundary.
11638c2ecf20Sopenharmony_ci	 * Work around this by always leaving at least 0x10 bytes of padding.
11648c2ecf20Sopenharmony_ci	 */
11658c2ecf20Sopenharmony_ci	desc->buffer_size = PAGE_SIZE - offset - 0x10;
11668c2ecf20Sopenharmony_ci	desc->buffer_bus = bus_addr + offset;
11678c2ecf20Sopenharmony_ci	desc->used = 0;
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci	list_add_tail(&desc->list, &ctx->buffer_list);
11708c2ecf20Sopenharmony_ci	ctx->total_allocation += PAGE_SIZE;
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	return 0;
11738c2ecf20Sopenharmony_ci}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_cistatic int context_init(struct context *ctx, struct fw_ohci *ohci,
11768c2ecf20Sopenharmony_ci			u32 regs, descriptor_callback_t callback)
11778c2ecf20Sopenharmony_ci{
11788c2ecf20Sopenharmony_ci	ctx->ohci = ohci;
11798c2ecf20Sopenharmony_ci	ctx->regs = regs;
11808c2ecf20Sopenharmony_ci	ctx->total_allocation = 0;
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ctx->buffer_list);
11838c2ecf20Sopenharmony_ci	if (context_add_buffer(ctx) < 0)
11848c2ecf20Sopenharmony_ci		return -ENOMEM;
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	ctx->buffer_tail = list_entry(ctx->buffer_list.next,
11878c2ecf20Sopenharmony_ci			struct descriptor_buffer, list);
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
11908c2ecf20Sopenharmony_ci	ctx->callback = callback;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	/*
11938c2ecf20Sopenharmony_ci	 * We put a dummy descriptor in the buffer that has a NULL
11948c2ecf20Sopenharmony_ci	 * branch address and looks like it's been sent.  That way we
11958c2ecf20Sopenharmony_ci	 * have a descriptor to append DMA programs to.
11968c2ecf20Sopenharmony_ci	 */
11978c2ecf20Sopenharmony_ci	memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
11988c2ecf20Sopenharmony_ci	ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
11998c2ecf20Sopenharmony_ci	ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
12008c2ecf20Sopenharmony_ci	ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
12018c2ecf20Sopenharmony_ci	ctx->last = ctx->buffer_tail->buffer;
12028c2ecf20Sopenharmony_ci	ctx->prev = ctx->buffer_tail->buffer;
12038c2ecf20Sopenharmony_ci	ctx->prev_z = 1;
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	return 0;
12068c2ecf20Sopenharmony_ci}
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_cistatic void context_release(struct context *ctx)
12098c2ecf20Sopenharmony_ci{
12108c2ecf20Sopenharmony_ci	struct fw_card *card = &ctx->ohci->card;
12118c2ecf20Sopenharmony_ci	struct descriptor_buffer *desc, *tmp;
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list)
12148c2ecf20Sopenharmony_ci		dma_free_coherent(card->device, PAGE_SIZE, desc,
12158c2ecf20Sopenharmony_ci			desc->buffer_bus -
12168c2ecf20Sopenharmony_ci			((void *)&desc->buffer - (void *)desc));
12178c2ecf20Sopenharmony_ci}
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci/* Must be called with ohci->lock held */
12208c2ecf20Sopenharmony_cistatic struct descriptor *context_get_descriptors(struct context *ctx,
12218c2ecf20Sopenharmony_ci						  int z, dma_addr_t *d_bus)
12228c2ecf20Sopenharmony_ci{
12238c2ecf20Sopenharmony_ci	struct descriptor *d = NULL;
12248c2ecf20Sopenharmony_ci	struct descriptor_buffer *desc = ctx->buffer_tail;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	if (z * sizeof(*d) > desc->buffer_size)
12278c2ecf20Sopenharmony_ci		return NULL;
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	if (z * sizeof(*d) > desc->buffer_size - desc->used) {
12308c2ecf20Sopenharmony_ci		/* No room for the descriptor in this buffer, so advance to the
12318c2ecf20Sopenharmony_ci		 * next one. */
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci		if (desc->list.next == &ctx->buffer_list) {
12348c2ecf20Sopenharmony_ci			/* If there is no free buffer next in the list,
12358c2ecf20Sopenharmony_ci			 * allocate one. */
12368c2ecf20Sopenharmony_ci			if (context_add_buffer(ctx) < 0)
12378c2ecf20Sopenharmony_ci				return NULL;
12388c2ecf20Sopenharmony_ci		}
12398c2ecf20Sopenharmony_ci		desc = list_entry(desc->list.next,
12408c2ecf20Sopenharmony_ci				struct descriptor_buffer, list);
12418c2ecf20Sopenharmony_ci		ctx->buffer_tail = desc;
12428c2ecf20Sopenharmony_ci	}
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	d = desc->buffer + desc->used / sizeof(*d);
12458c2ecf20Sopenharmony_ci	memset(d, 0, z * sizeof(*d));
12468c2ecf20Sopenharmony_ci	*d_bus = desc->buffer_bus + desc->used;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	return d;
12498c2ecf20Sopenharmony_ci}
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_cistatic void context_run(struct context *ctx, u32 extra)
12528c2ecf20Sopenharmony_ci{
12538c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = ctx->ohci;
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	reg_write(ohci, COMMAND_PTR(ctx->regs),
12568c2ecf20Sopenharmony_ci		  le32_to_cpu(ctx->last->branch_address));
12578c2ecf20Sopenharmony_ci	reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
12588c2ecf20Sopenharmony_ci	reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
12598c2ecf20Sopenharmony_ci	ctx->running = true;
12608c2ecf20Sopenharmony_ci	flush_writes(ohci);
12618c2ecf20Sopenharmony_ci}
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_cistatic void context_append(struct context *ctx,
12648c2ecf20Sopenharmony_ci			   struct descriptor *d, int z, int extra)
12658c2ecf20Sopenharmony_ci{
12668c2ecf20Sopenharmony_ci	dma_addr_t d_bus;
12678c2ecf20Sopenharmony_ci	struct descriptor_buffer *desc = ctx->buffer_tail;
12688c2ecf20Sopenharmony_ci	struct descriptor *d_branch;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d);
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	desc->used += (z + extra) * sizeof(*d);
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	wmb(); /* finish init of new descriptors before branch_address update */
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	d_branch = find_branch_descriptor(ctx->prev, ctx->prev_z);
12778c2ecf20Sopenharmony_ci	d_branch->branch_address = cpu_to_le32(d_bus | z);
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	/*
12808c2ecf20Sopenharmony_ci	 * VT6306 incorrectly checks only the single descriptor at the
12818c2ecf20Sopenharmony_ci	 * CommandPtr when the wake bit is written, so if it's a
12828c2ecf20Sopenharmony_ci	 * multi-descriptor block starting with an INPUT_MORE, put a copy of
12838c2ecf20Sopenharmony_ci	 * the branch address in the first descriptor.
12848c2ecf20Sopenharmony_ci	 *
12858c2ecf20Sopenharmony_ci	 * Not doing this for transmit contexts since not sure how it interacts
12868c2ecf20Sopenharmony_ci	 * with skip addresses.
12878c2ecf20Sopenharmony_ci	 */
12888c2ecf20Sopenharmony_ci	if (unlikely(ctx->ohci->quirks & QUIRK_IR_WAKE) &&
12898c2ecf20Sopenharmony_ci	    d_branch != ctx->prev &&
12908c2ecf20Sopenharmony_ci	    (ctx->prev->control & cpu_to_le16(DESCRIPTOR_CMD)) ==
12918c2ecf20Sopenharmony_ci	     cpu_to_le16(DESCRIPTOR_INPUT_MORE)) {
12928c2ecf20Sopenharmony_ci		ctx->prev->branch_address = cpu_to_le32(d_bus | z);
12938c2ecf20Sopenharmony_ci	}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	ctx->prev = d;
12968c2ecf20Sopenharmony_ci	ctx->prev_z = z;
12978c2ecf20Sopenharmony_ci}
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_cistatic void context_stop(struct context *ctx)
13008c2ecf20Sopenharmony_ci{
13018c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = ctx->ohci;
13028c2ecf20Sopenharmony_ci	u32 reg;
13038c2ecf20Sopenharmony_ci	int i;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
13068c2ecf20Sopenharmony_ci	ctx->running = false;
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	for (i = 0; i < 1000; i++) {
13098c2ecf20Sopenharmony_ci		reg = reg_read(ohci, CONTROL_SET(ctx->regs));
13108c2ecf20Sopenharmony_ci		if ((reg & CONTEXT_ACTIVE) == 0)
13118c2ecf20Sopenharmony_ci			return;
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci		if (i)
13148c2ecf20Sopenharmony_ci			udelay(10);
13158c2ecf20Sopenharmony_ci	}
13168c2ecf20Sopenharmony_ci	ohci_err(ohci, "DMA context still active (0x%08x)\n", reg);
13178c2ecf20Sopenharmony_ci}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_cistruct driver_data {
13208c2ecf20Sopenharmony_ci	u8 inline_data[8];
13218c2ecf20Sopenharmony_ci	struct fw_packet *packet;
13228c2ecf20Sopenharmony_ci};
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci/*
13258c2ecf20Sopenharmony_ci * This function apppends a packet to the DMA queue for transmission.
13268c2ecf20Sopenharmony_ci * Must always be called with the ochi->lock held to ensure proper
13278c2ecf20Sopenharmony_ci * generation handling and locking around packet queue manipulation.
13288c2ecf20Sopenharmony_ci */
13298c2ecf20Sopenharmony_cistatic int at_context_queue_packet(struct context *ctx,
13308c2ecf20Sopenharmony_ci				   struct fw_packet *packet)
13318c2ecf20Sopenharmony_ci{
13328c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = ctx->ohci;
13338c2ecf20Sopenharmony_ci	dma_addr_t d_bus, payload_bus;
13348c2ecf20Sopenharmony_ci	struct driver_data *driver_data;
13358c2ecf20Sopenharmony_ci	struct descriptor *d, *last;
13368c2ecf20Sopenharmony_ci	__le32 *header;
13378c2ecf20Sopenharmony_ci	int z, tcode;
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	d = context_get_descriptors(ctx, 4, &d_bus);
13408c2ecf20Sopenharmony_ci	if (d == NULL) {
13418c2ecf20Sopenharmony_ci		packet->ack = RCODE_SEND_ERROR;
13428c2ecf20Sopenharmony_ci		return -1;
13438c2ecf20Sopenharmony_ci	}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	d[0].control   = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
13468c2ecf20Sopenharmony_ci	d[0].res_count = cpu_to_le16(packet->timestamp);
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	/*
13498c2ecf20Sopenharmony_ci	 * The DMA format for asynchronous link packets is different
13508c2ecf20Sopenharmony_ci	 * from the IEEE1394 layout, so shift the fields around
13518c2ecf20Sopenharmony_ci	 * accordingly.
13528c2ecf20Sopenharmony_ci	 */
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci	tcode = (packet->header[0] >> 4) & 0x0f;
13558c2ecf20Sopenharmony_ci	header = (__le32 *) &d[1];
13568c2ecf20Sopenharmony_ci	switch (tcode) {
13578c2ecf20Sopenharmony_ci	case TCODE_WRITE_QUADLET_REQUEST:
13588c2ecf20Sopenharmony_ci	case TCODE_WRITE_BLOCK_REQUEST:
13598c2ecf20Sopenharmony_ci	case TCODE_WRITE_RESPONSE:
13608c2ecf20Sopenharmony_ci	case TCODE_READ_QUADLET_REQUEST:
13618c2ecf20Sopenharmony_ci	case TCODE_READ_BLOCK_REQUEST:
13628c2ecf20Sopenharmony_ci	case TCODE_READ_QUADLET_RESPONSE:
13638c2ecf20Sopenharmony_ci	case TCODE_READ_BLOCK_RESPONSE:
13648c2ecf20Sopenharmony_ci	case TCODE_LOCK_REQUEST:
13658c2ecf20Sopenharmony_ci	case TCODE_LOCK_RESPONSE:
13668c2ecf20Sopenharmony_ci		header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
13678c2ecf20Sopenharmony_ci					(packet->speed << 16));
13688c2ecf20Sopenharmony_ci		header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
13698c2ecf20Sopenharmony_ci					(packet->header[0] & 0xffff0000));
13708c2ecf20Sopenharmony_ci		header[2] = cpu_to_le32(packet->header[2]);
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci		if (TCODE_IS_BLOCK_PACKET(tcode))
13738c2ecf20Sopenharmony_ci			header[3] = cpu_to_le32(packet->header[3]);
13748c2ecf20Sopenharmony_ci		else
13758c2ecf20Sopenharmony_ci			header[3] = (__force __le32) packet->header[3];
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci		d[0].req_count = cpu_to_le16(packet->header_length);
13788c2ecf20Sopenharmony_ci		break;
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci	case TCODE_LINK_INTERNAL:
13818c2ecf20Sopenharmony_ci		header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
13828c2ecf20Sopenharmony_ci					(packet->speed << 16));
13838c2ecf20Sopenharmony_ci		header[1] = cpu_to_le32(packet->header[1]);
13848c2ecf20Sopenharmony_ci		header[2] = cpu_to_le32(packet->header[2]);
13858c2ecf20Sopenharmony_ci		d[0].req_count = cpu_to_le16(12);
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci		if (is_ping_packet(&packet->header[1]))
13888c2ecf20Sopenharmony_ci			d[0].control |= cpu_to_le16(DESCRIPTOR_PING);
13898c2ecf20Sopenharmony_ci		break;
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	case TCODE_STREAM_DATA:
13928c2ecf20Sopenharmony_ci		header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
13938c2ecf20Sopenharmony_ci					(packet->speed << 16));
13948c2ecf20Sopenharmony_ci		header[1] = cpu_to_le32(packet->header[0] & 0xffff0000);
13958c2ecf20Sopenharmony_ci		d[0].req_count = cpu_to_le16(8);
13968c2ecf20Sopenharmony_ci		break;
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	default:
13998c2ecf20Sopenharmony_ci		/* BUG(); */
14008c2ecf20Sopenharmony_ci		packet->ack = RCODE_SEND_ERROR;
14018c2ecf20Sopenharmony_ci		return -1;
14028c2ecf20Sopenharmony_ci	}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct driver_data) > sizeof(struct descriptor));
14058c2ecf20Sopenharmony_ci	driver_data = (struct driver_data *) &d[3];
14068c2ecf20Sopenharmony_ci	driver_data->packet = packet;
14078c2ecf20Sopenharmony_ci	packet->driver_data = driver_data;
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci	if (packet->payload_length > 0) {
14108c2ecf20Sopenharmony_ci		if (packet->payload_length > sizeof(driver_data->inline_data)) {
14118c2ecf20Sopenharmony_ci			payload_bus = dma_map_single(ohci->card.device,
14128c2ecf20Sopenharmony_ci						     packet->payload,
14138c2ecf20Sopenharmony_ci						     packet->payload_length,
14148c2ecf20Sopenharmony_ci						     DMA_TO_DEVICE);
14158c2ecf20Sopenharmony_ci			if (dma_mapping_error(ohci->card.device, payload_bus)) {
14168c2ecf20Sopenharmony_ci				packet->ack = RCODE_SEND_ERROR;
14178c2ecf20Sopenharmony_ci				return -1;
14188c2ecf20Sopenharmony_ci			}
14198c2ecf20Sopenharmony_ci			packet->payload_bus	= payload_bus;
14208c2ecf20Sopenharmony_ci			packet->payload_mapped	= true;
14218c2ecf20Sopenharmony_ci		} else {
14228c2ecf20Sopenharmony_ci			memcpy(driver_data->inline_data, packet->payload,
14238c2ecf20Sopenharmony_ci			       packet->payload_length);
14248c2ecf20Sopenharmony_ci			payload_bus = d_bus + 3 * sizeof(*d);
14258c2ecf20Sopenharmony_ci		}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci		d[2].req_count    = cpu_to_le16(packet->payload_length);
14288c2ecf20Sopenharmony_ci		d[2].data_address = cpu_to_le32(payload_bus);
14298c2ecf20Sopenharmony_ci		last = &d[2];
14308c2ecf20Sopenharmony_ci		z = 3;
14318c2ecf20Sopenharmony_ci	} else {
14328c2ecf20Sopenharmony_ci		last = &d[0];
14338c2ecf20Sopenharmony_ci		z = 2;
14348c2ecf20Sopenharmony_ci	}
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci	last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
14378c2ecf20Sopenharmony_ci				     DESCRIPTOR_IRQ_ALWAYS |
14388c2ecf20Sopenharmony_ci				     DESCRIPTOR_BRANCH_ALWAYS);
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	/* FIXME: Document how the locking works. */
14418c2ecf20Sopenharmony_ci	if (ohci->generation != packet->generation) {
14428c2ecf20Sopenharmony_ci		if (packet->payload_mapped)
14438c2ecf20Sopenharmony_ci			dma_unmap_single(ohci->card.device, payload_bus,
14448c2ecf20Sopenharmony_ci					 packet->payload_length, DMA_TO_DEVICE);
14458c2ecf20Sopenharmony_ci		packet->ack = RCODE_GENERATION;
14468c2ecf20Sopenharmony_ci		return -1;
14478c2ecf20Sopenharmony_ci	}
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	context_append(ctx, d, z, 4 - z);
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	if (ctx->running)
14528c2ecf20Sopenharmony_ci		reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
14538c2ecf20Sopenharmony_ci	else
14548c2ecf20Sopenharmony_ci		context_run(ctx, 0);
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci	return 0;
14578c2ecf20Sopenharmony_ci}
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_cistatic void at_context_flush(struct context *ctx)
14608c2ecf20Sopenharmony_ci{
14618c2ecf20Sopenharmony_ci	tasklet_disable(&ctx->tasklet);
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	ctx->flushing = true;
14648c2ecf20Sopenharmony_ci	context_tasklet((unsigned long)ctx);
14658c2ecf20Sopenharmony_ci	ctx->flushing = false;
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	tasklet_enable(&ctx->tasklet);
14688c2ecf20Sopenharmony_ci}
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_cistatic int handle_at_packet(struct context *context,
14718c2ecf20Sopenharmony_ci			    struct descriptor *d,
14728c2ecf20Sopenharmony_ci			    struct descriptor *last)
14738c2ecf20Sopenharmony_ci{
14748c2ecf20Sopenharmony_ci	struct driver_data *driver_data;
14758c2ecf20Sopenharmony_ci	struct fw_packet *packet;
14768c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = context->ohci;
14778c2ecf20Sopenharmony_ci	int evt;
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	if (last->transfer_status == 0 && !context->flushing)
14808c2ecf20Sopenharmony_ci		/* This descriptor isn't done yet, stop iteration. */
14818c2ecf20Sopenharmony_ci		return 0;
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci	driver_data = (struct driver_data *) &d[3];
14848c2ecf20Sopenharmony_ci	packet = driver_data->packet;
14858c2ecf20Sopenharmony_ci	if (packet == NULL)
14868c2ecf20Sopenharmony_ci		/* This packet was cancelled, just continue. */
14878c2ecf20Sopenharmony_ci		return 1;
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	if (packet->payload_mapped)
14908c2ecf20Sopenharmony_ci		dma_unmap_single(ohci->card.device, packet->payload_bus,
14918c2ecf20Sopenharmony_ci				 packet->payload_length, DMA_TO_DEVICE);
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci	evt = le16_to_cpu(last->transfer_status) & 0x1f;
14948c2ecf20Sopenharmony_ci	packet->timestamp = le16_to_cpu(last->res_count);
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_ci	log_ar_at_event(ohci, 'T', packet->speed, packet->header, evt);
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci	switch (evt) {
14998c2ecf20Sopenharmony_ci	case OHCI1394_evt_timeout:
15008c2ecf20Sopenharmony_ci		/* Async response transmit timed out. */
15018c2ecf20Sopenharmony_ci		packet->ack = RCODE_CANCELLED;
15028c2ecf20Sopenharmony_ci		break;
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci	case OHCI1394_evt_flushed:
15058c2ecf20Sopenharmony_ci		/*
15068c2ecf20Sopenharmony_ci		 * The packet was flushed should give same error as
15078c2ecf20Sopenharmony_ci		 * when we try to use a stale generation count.
15088c2ecf20Sopenharmony_ci		 */
15098c2ecf20Sopenharmony_ci		packet->ack = RCODE_GENERATION;
15108c2ecf20Sopenharmony_ci		break;
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	case OHCI1394_evt_missing_ack:
15138c2ecf20Sopenharmony_ci		if (context->flushing)
15148c2ecf20Sopenharmony_ci			packet->ack = RCODE_GENERATION;
15158c2ecf20Sopenharmony_ci		else {
15168c2ecf20Sopenharmony_ci			/*
15178c2ecf20Sopenharmony_ci			 * Using a valid (current) generation count, but the
15188c2ecf20Sopenharmony_ci			 * node is not on the bus or not sending acks.
15198c2ecf20Sopenharmony_ci			 */
15208c2ecf20Sopenharmony_ci			packet->ack = RCODE_NO_ACK;
15218c2ecf20Sopenharmony_ci		}
15228c2ecf20Sopenharmony_ci		break;
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	case ACK_COMPLETE + 0x10:
15258c2ecf20Sopenharmony_ci	case ACK_PENDING + 0x10:
15268c2ecf20Sopenharmony_ci	case ACK_BUSY_X + 0x10:
15278c2ecf20Sopenharmony_ci	case ACK_BUSY_A + 0x10:
15288c2ecf20Sopenharmony_ci	case ACK_BUSY_B + 0x10:
15298c2ecf20Sopenharmony_ci	case ACK_DATA_ERROR + 0x10:
15308c2ecf20Sopenharmony_ci	case ACK_TYPE_ERROR + 0x10:
15318c2ecf20Sopenharmony_ci		packet->ack = evt - 0x10;
15328c2ecf20Sopenharmony_ci		break;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci	case OHCI1394_evt_no_status:
15358c2ecf20Sopenharmony_ci		if (context->flushing) {
15368c2ecf20Sopenharmony_ci			packet->ack = RCODE_GENERATION;
15378c2ecf20Sopenharmony_ci			break;
15388c2ecf20Sopenharmony_ci		}
15398c2ecf20Sopenharmony_ci		fallthrough;
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	default:
15428c2ecf20Sopenharmony_ci		packet->ack = RCODE_SEND_ERROR;
15438c2ecf20Sopenharmony_ci		break;
15448c2ecf20Sopenharmony_ci	}
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	packet->callback(packet, &ohci->card, packet->ack);
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	return 1;
15498c2ecf20Sopenharmony_ci}
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci#define HEADER_GET_DESTINATION(q)	(((q) >> 16) & 0xffff)
15528c2ecf20Sopenharmony_ci#define HEADER_GET_TCODE(q)		(((q) >> 4) & 0x0f)
15538c2ecf20Sopenharmony_ci#define HEADER_GET_OFFSET_HIGH(q)	(((q) >> 0) & 0xffff)
15548c2ecf20Sopenharmony_ci#define HEADER_GET_DATA_LENGTH(q)	(((q) >> 16) & 0xffff)
15558c2ecf20Sopenharmony_ci#define HEADER_GET_EXTENDED_TCODE(q)	(((q) >> 0) & 0xffff)
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_cistatic void handle_local_rom(struct fw_ohci *ohci,
15588c2ecf20Sopenharmony_ci			     struct fw_packet *packet, u32 csr)
15598c2ecf20Sopenharmony_ci{
15608c2ecf20Sopenharmony_ci	struct fw_packet response;
15618c2ecf20Sopenharmony_ci	int tcode, length, i;
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	tcode = HEADER_GET_TCODE(packet->header[0]);
15648c2ecf20Sopenharmony_ci	if (TCODE_IS_BLOCK_PACKET(tcode))
15658c2ecf20Sopenharmony_ci		length = HEADER_GET_DATA_LENGTH(packet->header[3]);
15668c2ecf20Sopenharmony_ci	else
15678c2ecf20Sopenharmony_ci		length = 4;
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci	i = csr - CSR_CONFIG_ROM;
15708c2ecf20Sopenharmony_ci	if (i + length > CONFIG_ROM_SIZE) {
15718c2ecf20Sopenharmony_ci		fw_fill_response(&response, packet->header,
15728c2ecf20Sopenharmony_ci				 RCODE_ADDRESS_ERROR, NULL, 0);
15738c2ecf20Sopenharmony_ci	} else if (!TCODE_IS_READ_REQUEST(tcode)) {
15748c2ecf20Sopenharmony_ci		fw_fill_response(&response, packet->header,
15758c2ecf20Sopenharmony_ci				 RCODE_TYPE_ERROR, NULL, 0);
15768c2ecf20Sopenharmony_ci	} else {
15778c2ecf20Sopenharmony_ci		fw_fill_response(&response, packet->header, RCODE_COMPLETE,
15788c2ecf20Sopenharmony_ci				 (void *) ohci->config_rom + i, length);
15798c2ecf20Sopenharmony_ci	}
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci	fw_core_handle_response(&ohci->card, &response);
15828c2ecf20Sopenharmony_ci}
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_cistatic void handle_local_lock(struct fw_ohci *ohci,
15858c2ecf20Sopenharmony_ci			      struct fw_packet *packet, u32 csr)
15868c2ecf20Sopenharmony_ci{
15878c2ecf20Sopenharmony_ci	struct fw_packet response;
15888c2ecf20Sopenharmony_ci	int tcode, length, ext_tcode, sel, try;
15898c2ecf20Sopenharmony_ci	__be32 *payload, lock_old;
15908c2ecf20Sopenharmony_ci	u32 lock_arg, lock_data;
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci	tcode = HEADER_GET_TCODE(packet->header[0]);
15938c2ecf20Sopenharmony_ci	length = HEADER_GET_DATA_LENGTH(packet->header[3]);
15948c2ecf20Sopenharmony_ci	payload = packet->payload;
15958c2ecf20Sopenharmony_ci	ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci	if (tcode == TCODE_LOCK_REQUEST &&
15988c2ecf20Sopenharmony_ci	    ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
15998c2ecf20Sopenharmony_ci		lock_arg = be32_to_cpu(payload[0]);
16008c2ecf20Sopenharmony_ci		lock_data = be32_to_cpu(payload[1]);
16018c2ecf20Sopenharmony_ci	} else if (tcode == TCODE_READ_QUADLET_REQUEST) {
16028c2ecf20Sopenharmony_ci		lock_arg = 0;
16038c2ecf20Sopenharmony_ci		lock_data = 0;
16048c2ecf20Sopenharmony_ci	} else {
16058c2ecf20Sopenharmony_ci		fw_fill_response(&response, packet->header,
16068c2ecf20Sopenharmony_ci				 RCODE_TYPE_ERROR, NULL, 0);
16078c2ecf20Sopenharmony_ci		goto out;
16088c2ecf20Sopenharmony_ci	}
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci	sel = (csr - CSR_BUS_MANAGER_ID) / 4;
16118c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_CSRData, lock_data);
16128c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
16138c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_CSRControl, sel);
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_ci	for (try = 0; try < 20; try++)
16168c2ecf20Sopenharmony_ci		if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000) {
16178c2ecf20Sopenharmony_ci			lock_old = cpu_to_be32(reg_read(ohci,
16188c2ecf20Sopenharmony_ci							OHCI1394_CSRData));
16198c2ecf20Sopenharmony_ci			fw_fill_response(&response, packet->header,
16208c2ecf20Sopenharmony_ci					 RCODE_COMPLETE,
16218c2ecf20Sopenharmony_ci					 &lock_old, sizeof(lock_old));
16228c2ecf20Sopenharmony_ci			goto out;
16238c2ecf20Sopenharmony_ci		}
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	ohci_err(ohci, "swap not done (CSR lock timeout)\n");
16268c2ecf20Sopenharmony_ci	fw_fill_response(&response, packet->header, RCODE_BUSY, NULL, 0);
16278c2ecf20Sopenharmony_ci
16288c2ecf20Sopenharmony_ci out:
16298c2ecf20Sopenharmony_ci	fw_core_handle_response(&ohci->card, &response);
16308c2ecf20Sopenharmony_ci}
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_cistatic void handle_local_request(struct context *ctx, struct fw_packet *packet)
16338c2ecf20Sopenharmony_ci{
16348c2ecf20Sopenharmony_ci	u64 offset, csr;
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci	if (ctx == &ctx->ohci->at_request_ctx) {
16378c2ecf20Sopenharmony_ci		packet->ack = ACK_PENDING;
16388c2ecf20Sopenharmony_ci		packet->callback(packet, &ctx->ohci->card, packet->ack);
16398c2ecf20Sopenharmony_ci	}
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	offset =
16428c2ecf20Sopenharmony_ci		((unsigned long long)
16438c2ecf20Sopenharmony_ci		 HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
16448c2ecf20Sopenharmony_ci		packet->header[2];
16458c2ecf20Sopenharmony_ci	csr = offset - CSR_REGISTER_BASE;
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	/* Handle config rom reads. */
16488c2ecf20Sopenharmony_ci	if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
16498c2ecf20Sopenharmony_ci		handle_local_rom(ctx->ohci, packet, csr);
16508c2ecf20Sopenharmony_ci	else switch (csr) {
16518c2ecf20Sopenharmony_ci	case CSR_BUS_MANAGER_ID:
16528c2ecf20Sopenharmony_ci	case CSR_BANDWIDTH_AVAILABLE:
16538c2ecf20Sopenharmony_ci	case CSR_CHANNELS_AVAILABLE_HI:
16548c2ecf20Sopenharmony_ci	case CSR_CHANNELS_AVAILABLE_LO:
16558c2ecf20Sopenharmony_ci		handle_local_lock(ctx->ohci, packet, csr);
16568c2ecf20Sopenharmony_ci		break;
16578c2ecf20Sopenharmony_ci	default:
16588c2ecf20Sopenharmony_ci		if (ctx == &ctx->ohci->at_request_ctx)
16598c2ecf20Sopenharmony_ci			fw_core_handle_request(&ctx->ohci->card, packet);
16608c2ecf20Sopenharmony_ci		else
16618c2ecf20Sopenharmony_ci			fw_core_handle_response(&ctx->ohci->card, packet);
16628c2ecf20Sopenharmony_ci		break;
16638c2ecf20Sopenharmony_ci	}
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_ci	if (ctx == &ctx->ohci->at_response_ctx) {
16668c2ecf20Sopenharmony_ci		packet->ack = ACK_COMPLETE;
16678c2ecf20Sopenharmony_ci		packet->callback(packet, &ctx->ohci->card, packet->ack);
16688c2ecf20Sopenharmony_ci	}
16698c2ecf20Sopenharmony_ci}
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_cistatic void at_context_transmit(struct context *ctx, struct fw_packet *packet)
16728c2ecf20Sopenharmony_ci{
16738c2ecf20Sopenharmony_ci	unsigned long flags;
16748c2ecf20Sopenharmony_ci	int ret;
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->ohci->lock, flags);
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci	if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
16798c2ecf20Sopenharmony_ci	    ctx->ohci->generation == packet->generation) {
16808c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ctx->ohci->lock, flags);
16818c2ecf20Sopenharmony_ci		handle_local_request(ctx, packet);
16828c2ecf20Sopenharmony_ci		return;
16838c2ecf20Sopenharmony_ci	}
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_ci	ret = at_context_queue_packet(ctx, packet);
16868c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->ohci->lock, flags);
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	if (ret < 0)
16898c2ecf20Sopenharmony_ci		packet->callback(packet, &ctx->ohci->card, packet->ack);
16908c2ecf20Sopenharmony_ci
16918c2ecf20Sopenharmony_ci}
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_cistatic void detect_dead_context(struct fw_ohci *ohci,
16948c2ecf20Sopenharmony_ci				const char *name, unsigned int regs)
16958c2ecf20Sopenharmony_ci{
16968c2ecf20Sopenharmony_ci	u32 ctl;
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_ci	ctl = reg_read(ohci, CONTROL_SET(regs));
16998c2ecf20Sopenharmony_ci	if (ctl & CONTEXT_DEAD)
17008c2ecf20Sopenharmony_ci		ohci_err(ohci, "DMA context %s has stopped, error code: %s\n",
17018c2ecf20Sopenharmony_ci			name, evts[ctl & 0x1f]);
17028c2ecf20Sopenharmony_ci}
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_cistatic void handle_dead_contexts(struct fw_ohci *ohci)
17058c2ecf20Sopenharmony_ci{
17068c2ecf20Sopenharmony_ci	unsigned int i;
17078c2ecf20Sopenharmony_ci	char name[8];
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	detect_dead_context(ohci, "ATReq", OHCI1394_AsReqTrContextBase);
17108c2ecf20Sopenharmony_ci	detect_dead_context(ohci, "ATRsp", OHCI1394_AsRspTrContextBase);
17118c2ecf20Sopenharmony_ci	detect_dead_context(ohci, "ARReq", OHCI1394_AsReqRcvContextBase);
17128c2ecf20Sopenharmony_ci	detect_dead_context(ohci, "ARRsp", OHCI1394_AsRspRcvContextBase);
17138c2ecf20Sopenharmony_ci	for (i = 0; i < 32; ++i) {
17148c2ecf20Sopenharmony_ci		if (!(ohci->it_context_support & (1 << i)))
17158c2ecf20Sopenharmony_ci			continue;
17168c2ecf20Sopenharmony_ci		sprintf(name, "IT%u", i);
17178c2ecf20Sopenharmony_ci		detect_dead_context(ohci, name, OHCI1394_IsoXmitContextBase(i));
17188c2ecf20Sopenharmony_ci	}
17198c2ecf20Sopenharmony_ci	for (i = 0; i < 32; ++i) {
17208c2ecf20Sopenharmony_ci		if (!(ohci->ir_context_support & (1 << i)))
17218c2ecf20Sopenharmony_ci			continue;
17228c2ecf20Sopenharmony_ci		sprintf(name, "IR%u", i);
17238c2ecf20Sopenharmony_ci		detect_dead_context(ohci, name, OHCI1394_IsoRcvContextBase(i));
17248c2ecf20Sopenharmony_ci	}
17258c2ecf20Sopenharmony_ci	/* TODO: maybe try to flush and restart the dead contexts */
17268c2ecf20Sopenharmony_ci}
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_cistatic u32 cycle_timer_ticks(u32 cycle_timer)
17298c2ecf20Sopenharmony_ci{
17308c2ecf20Sopenharmony_ci	u32 ticks;
17318c2ecf20Sopenharmony_ci
17328c2ecf20Sopenharmony_ci	ticks = cycle_timer & 0xfff;
17338c2ecf20Sopenharmony_ci	ticks += 3072 * ((cycle_timer >> 12) & 0x1fff);
17348c2ecf20Sopenharmony_ci	ticks += (3072 * 8000) * (cycle_timer >> 25);
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_ci	return ticks;
17378c2ecf20Sopenharmony_ci}
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci/*
17408c2ecf20Sopenharmony_ci * Some controllers exhibit one or more of the following bugs when updating the
17418c2ecf20Sopenharmony_ci * iso cycle timer register:
17428c2ecf20Sopenharmony_ci *  - When the lowest six bits are wrapping around to zero, a read that happens
17438c2ecf20Sopenharmony_ci *    at the same time will return garbage in the lowest ten bits.
17448c2ecf20Sopenharmony_ci *  - When the cycleOffset field wraps around to zero, the cycleCount field is
17458c2ecf20Sopenharmony_ci *    not incremented for about 60 ns.
17468c2ecf20Sopenharmony_ci *  - Occasionally, the entire register reads zero.
17478c2ecf20Sopenharmony_ci *
17488c2ecf20Sopenharmony_ci * To catch these, we read the register three times and ensure that the
17498c2ecf20Sopenharmony_ci * difference between each two consecutive reads is approximately the same, i.e.
17508c2ecf20Sopenharmony_ci * less than twice the other.  Furthermore, any negative difference indicates an
17518c2ecf20Sopenharmony_ci * error.  (A PCI read should take at least 20 ticks of the 24.576 MHz timer to
17528c2ecf20Sopenharmony_ci * execute, so we have enough precision to compute the ratio of the differences.)
17538c2ecf20Sopenharmony_ci */
17548c2ecf20Sopenharmony_cistatic u32 get_cycle_time(struct fw_ohci *ohci)
17558c2ecf20Sopenharmony_ci{
17568c2ecf20Sopenharmony_ci	u32 c0, c1, c2;
17578c2ecf20Sopenharmony_ci	u32 t0, t1, t2;
17588c2ecf20Sopenharmony_ci	s32 diff01, diff12;
17598c2ecf20Sopenharmony_ci	int i;
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_ci	if (has_reboot_by_cycle_timer_read_quirk(ohci))
17628c2ecf20Sopenharmony_ci		return 0;
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci	c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
17658c2ecf20Sopenharmony_ci
17668c2ecf20Sopenharmony_ci	if (ohci->quirks & QUIRK_CYCLE_TIMER) {
17678c2ecf20Sopenharmony_ci		i = 0;
17688c2ecf20Sopenharmony_ci		c1 = c2;
17698c2ecf20Sopenharmony_ci		c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
17708c2ecf20Sopenharmony_ci		do {
17718c2ecf20Sopenharmony_ci			c0 = c1;
17728c2ecf20Sopenharmony_ci			c1 = c2;
17738c2ecf20Sopenharmony_ci			c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
17748c2ecf20Sopenharmony_ci			t0 = cycle_timer_ticks(c0);
17758c2ecf20Sopenharmony_ci			t1 = cycle_timer_ticks(c1);
17768c2ecf20Sopenharmony_ci			t2 = cycle_timer_ticks(c2);
17778c2ecf20Sopenharmony_ci			diff01 = t1 - t0;
17788c2ecf20Sopenharmony_ci			diff12 = t2 - t1;
17798c2ecf20Sopenharmony_ci		} while ((diff01 <= 0 || diff12 <= 0 ||
17808c2ecf20Sopenharmony_ci			  diff01 / diff12 >= 2 || diff12 / diff01 >= 2)
17818c2ecf20Sopenharmony_ci			 && i++ < 20);
17828c2ecf20Sopenharmony_ci	}
17838c2ecf20Sopenharmony_ci
17848c2ecf20Sopenharmony_ci	return c2;
17858c2ecf20Sopenharmony_ci}
17868c2ecf20Sopenharmony_ci
17878c2ecf20Sopenharmony_ci/*
17888c2ecf20Sopenharmony_ci * This function has to be called at least every 64 seconds.  The bus_time
17898c2ecf20Sopenharmony_ci * field stores not only the upper 25 bits of the BUS_TIME register but also
17908c2ecf20Sopenharmony_ci * the most significant bit of the cycle timer in bit 6 so that we can detect
17918c2ecf20Sopenharmony_ci * changes in this bit.
17928c2ecf20Sopenharmony_ci */
17938c2ecf20Sopenharmony_cistatic u32 update_bus_time(struct fw_ohci *ohci)
17948c2ecf20Sopenharmony_ci{
17958c2ecf20Sopenharmony_ci	u32 cycle_time_seconds = get_cycle_time(ohci) >> 25;
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_ci	if (unlikely(!ohci->bus_time_running)) {
17988c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_cycle64Seconds);
17998c2ecf20Sopenharmony_ci		ohci->bus_time = (lower_32_bits(ktime_get_seconds()) & ~0x7f) |
18008c2ecf20Sopenharmony_ci		                 (cycle_time_seconds & 0x40);
18018c2ecf20Sopenharmony_ci		ohci->bus_time_running = true;
18028c2ecf20Sopenharmony_ci	}
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci	if ((ohci->bus_time & 0x40) != (cycle_time_seconds & 0x40))
18058c2ecf20Sopenharmony_ci		ohci->bus_time += 0x40;
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_ci	return ohci->bus_time | cycle_time_seconds;
18088c2ecf20Sopenharmony_ci}
18098c2ecf20Sopenharmony_ci
18108c2ecf20Sopenharmony_cistatic int get_status_for_port(struct fw_ohci *ohci, int port_index)
18118c2ecf20Sopenharmony_ci{
18128c2ecf20Sopenharmony_ci	int reg;
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_ci	mutex_lock(&ohci->phy_reg_mutex);
18158c2ecf20Sopenharmony_ci	reg = write_phy_reg(ohci, 7, port_index);
18168c2ecf20Sopenharmony_ci	if (reg >= 0)
18178c2ecf20Sopenharmony_ci		reg = read_phy_reg(ohci, 8);
18188c2ecf20Sopenharmony_ci	mutex_unlock(&ohci->phy_reg_mutex);
18198c2ecf20Sopenharmony_ci	if (reg < 0)
18208c2ecf20Sopenharmony_ci		return reg;
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_ci	switch (reg & 0x0f) {
18238c2ecf20Sopenharmony_ci	case 0x06:
18248c2ecf20Sopenharmony_ci		return 2;	/* is child node (connected to parent node) */
18258c2ecf20Sopenharmony_ci	case 0x0e:
18268c2ecf20Sopenharmony_ci		return 3;	/* is parent node (connected to child node) */
18278c2ecf20Sopenharmony_ci	}
18288c2ecf20Sopenharmony_ci	return 1;		/* not connected */
18298c2ecf20Sopenharmony_ci}
18308c2ecf20Sopenharmony_ci
18318c2ecf20Sopenharmony_cistatic int get_self_id_pos(struct fw_ohci *ohci, u32 self_id,
18328c2ecf20Sopenharmony_ci	int self_id_count)
18338c2ecf20Sopenharmony_ci{
18348c2ecf20Sopenharmony_ci	int i;
18358c2ecf20Sopenharmony_ci	u32 entry;
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_ci	for (i = 0; i < self_id_count; i++) {
18388c2ecf20Sopenharmony_ci		entry = ohci->self_id_buffer[i];
18398c2ecf20Sopenharmony_ci		if ((self_id & 0xff000000) == (entry & 0xff000000))
18408c2ecf20Sopenharmony_ci			return -1;
18418c2ecf20Sopenharmony_ci		if ((self_id & 0xff000000) < (entry & 0xff000000))
18428c2ecf20Sopenharmony_ci			return i;
18438c2ecf20Sopenharmony_ci	}
18448c2ecf20Sopenharmony_ci	return i;
18458c2ecf20Sopenharmony_ci}
18468c2ecf20Sopenharmony_ci
18478c2ecf20Sopenharmony_cistatic int initiated_reset(struct fw_ohci *ohci)
18488c2ecf20Sopenharmony_ci{
18498c2ecf20Sopenharmony_ci	int reg;
18508c2ecf20Sopenharmony_ci	int ret = 0;
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_ci	mutex_lock(&ohci->phy_reg_mutex);
18538c2ecf20Sopenharmony_ci	reg = write_phy_reg(ohci, 7, 0xe0); /* Select page 7 */
18548c2ecf20Sopenharmony_ci	if (reg >= 0) {
18558c2ecf20Sopenharmony_ci		reg = read_phy_reg(ohci, 8);
18568c2ecf20Sopenharmony_ci		reg |= 0x40;
18578c2ecf20Sopenharmony_ci		reg = write_phy_reg(ohci, 8, reg); /* set PMODE bit */
18588c2ecf20Sopenharmony_ci		if (reg >= 0) {
18598c2ecf20Sopenharmony_ci			reg = read_phy_reg(ohci, 12); /* read register 12 */
18608c2ecf20Sopenharmony_ci			if (reg >= 0) {
18618c2ecf20Sopenharmony_ci				if ((reg & 0x08) == 0x08) {
18628c2ecf20Sopenharmony_ci					/* bit 3 indicates "initiated reset" */
18638c2ecf20Sopenharmony_ci					ret = 0x2;
18648c2ecf20Sopenharmony_ci				}
18658c2ecf20Sopenharmony_ci			}
18668c2ecf20Sopenharmony_ci		}
18678c2ecf20Sopenharmony_ci	}
18688c2ecf20Sopenharmony_ci	mutex_unlock(&ohci->phy_reg_mutex);
18698c2ecf20Sopenharmony_ci	return ret;
18708c2ecf20Sopenharmony_ci}
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_ci/*
18738c2ecf20Sopenharmony_ci * TI TSB82AA2B and TSB12LV26 do not receive the selfID of a locally
18748c2ecf20Sopenharmony_ci * attached TSB41BA3D phy; see http://www.ti.com/litv/pdf/sllz059.
18758c2ecf20Sopenharmony_ci * Construct the selfID from phy register contents.
18768c2ecf20Sopenharmony_ci */
18778c2ecf20Sopenharmony_cistatic int find_and_insert_self_id(struct fw_ohci *ohci, int self_id_count)
18788c2ecf20Sopenharmony_ci{
18798c2ecf20Sopenharmony_ci	int reg, i, pos, status;
18808c2ecf20Sopenharmony_ci	/* link active 1, speed 3, bridge 0, contender 1, more packets 0 */
18818c2ecf20Sopenharmony_ci	u32 self_id = 0x8040c800;
18828c2ecf20Sopenharmony_ci
18838c2ecf20Sopenharmony_ci	reg = reg_read(ohci, OHCI1394_NodeID);
18848c2ecf20Sopenharmony_ci	if (!(reg & OHCI1394_NodeID_idValid)) {
18858c2ecf20Sopenharmony_ci		ohci_notice(ohci,
18868c2ecf20Sopenharmony_ci			    "node ID not valid, new bus reset in progress\n");
18878c2ecf20Sopenharmony_ci		return -EBUSY;
18888c2ecf20Sopenharmony_ci	}
18898c2ecf20Sopenharmony_ci	self_id |= ((reg & 0x3f) << 24); /* phy ID */
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_ci	reg = ohci_read_phy_reg(&ohci->card, 4);
18928c2ecf20Sopenharmony_ci	if (reg < 0)
18938c2ecf20Sopenharmony_ci		return reg;
18948c2ecf20Sopenharmony_ci	self_id |= ((reg & 0x07) << 8); /* power class */
18958c2ecf20Sopenharmony_ci
18968c2ecf20Sopenharmony_ci	reg = ohci_read_phy_reg(&ohci->card, 1);
18978c2ecf20Sopenharmony_ci	if (reg < 0)
18988c2ecf20Sopenharmony_ci		return reg;
18998c2ecf20Sopenharmony_ci	self_id |= ((reg & 0x3f) << 16); /* gap count */
19008c2ecf20Sopenharmony_ci
19018c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++) {
19028c2ecf20Sopenharmony_ci		status = get_status_for_port(ohci, i);
19038c2ecf20Sopenharmony_ci		if (status < 0)
19048c2ecf20Sopenharmony_ci			return status;
19058c2ecf20Sopenharmony_ci		self_id |= ((status & 0x3) << (6 - (i * 2)));
19068c2ecf20Sopenharmony_ci	}
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci	self_id |= initiated_reset(ohci);
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci	pos = get_self_id_pos(ohci, self_id, self_id_count);
19118c2ecf20Sopenharmony_ci	if (pos >= 0) {
19128c2ecf20Sopenharmony_ci		memmove(&(ohci->self_id_buffer[pos+1]),
19138c2ecf20Sopenharmony_ci			&(ohci->self_id_buffer[pos]),
19148c2ecf20Sopenharmony_ci			(self_id_count - pos) * sizeof(*ohci->self_id_buffer));
19158c2ecf20Sopenharmony_ci		ohci->self_id_buffer[pos] = self_id;
19168c2ecf20Sopenharmony_ci		self_id_count++;
19178c2ecf20Sopenharmony_ci	}
19188c2ecf20Sopenharmony_ci	return self_id_count;
19198c2ecf20Sopenharmony_ci}
19208c2ecf20Sopenharmony_ci
19218c2ecf20Sopenharmony_cistatic void bus_reset_work(struct work_struct *work)
19228c2ecf20Sopenharmony_ci{
19238c2ecf20Sopenharmony_ci	struct fw_ohci *ohci =
19248c2ecf20Sopenharmony_ci		container_of(work, struct fw_ohci, bus_reset_work);
19258c2ecf20Sopenharmony_ci	int self_id_count, generation, new_generation, i, j;
19268c2ecf20Sopenharmony_ci	u32 reg;
19278c2ecf20Sopenharmony_ci	void *free_rom = NULL;
19288c2ecf20Sopenharmony_ci	dma_addr_t free_rom_bus = 0;
19298c2ecf20Sopenharmony_ci	bool is_new_root;
19308c2ecf20Sopenharmony_ci
19318c2ecf20Sopenharmony_ci	reg = reg_read(ohci, OHCI1394_NodeID);
19328c2ecf20Sopenharmony_ci	if (!(reg & OHCI1394_NodeID_idValid)) {
19338c2ecf20Sopenharmony_ci		ohci_notice(ohci,
19348c2ecf20Sopenharmony_ci			    "node ID not valid, new bus reset in progress\n");
19358c2ecf20Sopenharmony_ci		return;
19368c2ecf20Sopenharmony_ci	}
19378c2ecf20Sopenharmony_ci	if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
19388c2ecf20Sopenharmony_ci		ohci_notice(ohci, "malconfigured bus\n");
19398c2ecf20Sopenharmony_ci		return;
19408c2ecf20Sopenharmony_ci	}
19418c2ecf20Sopenharmony_ci	ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
19428c2ecf20Sopenharmony_ci			       OHCI1394_NodeID_nodeNumber);
19438c2ecf20Sopenharmony_ci
19448c2ecf20Sopenharmony_ci	is_new_root = (reg & OHCI1394_NodeID_root) != 0;
19458c2ecf20Sopenharmony_ci	if (!(ohci->is_root && is_new_root))
19468c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_LinkControlSet,
19478c2ecf20Sopenharmony_ci			  OHCI1394_LinkControl_cycleMaster);
19488c2ecf20Sopenharmony_ci	ohci->is_root = is_new_root;
19498c2ecf20Sopenharmony_ci
19508c2ecf20Sopenharmony_ci	reg = reg_read(ohci, OHCI1394_SelfIDCount);
19518c2ecf20Sopenharmony_ci	if (reg & OHCI1394_SelfIDCount_selfIDError) {
19528c2ecf20Sopenharmony_ci		ohci_notice(ohci, "self ID receive error\n");
19538c2ecf20Sopenharmony_ci		return;
19548c2ecf20Sopenharmony_ci	}
19558c2ecf20Sopenharmony_ci	/*
19568c2ecf20Sopenharmony_ci	 * The count in the SelfIDCount register is the number of
19578c2ecf20Sopenharmony_ci	 * bytes in the self ID receive buffer.  Since we also receive
19588c2ecf20Sopenharmony_ci	 * the inverted quadlets and a header quadlet, we shift one
19598c2ecf20Sopenharmony_ci	 * bit extra to get the actual number of self IDs.
19608c2ecf20Sopenharmony_ci	 */
19618c2ecf20Sopenharmony_ci	self_id_count = (reg >> 3) & 0xff;
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	if (self_id_count > 252) {
19648c2ecf20Sopenharmony_ci		ohci_notice(ohci, "bad selfIDSize (%08x)\n", reg);
19658c2ecf20Sopenharmony_ci		return;
19668c2ecf20Sopenharmony_ci	}
19678c2ecf20Sopenharmony_ci
19688c2ecf20Sopenharmony_ci	generation = (cond_le32_to_cpu(ohci->self_id[0]) >> 16) & 0xff;
19698c2ecf20Sopenharmony_ci	rmb();
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_ci	for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
19728c2ecf20Sopenharmony_ci		u32 id  = cond_le32_to_cpu(ohci->self_id[i]);
19738c2ecf20Sopenharmony_ci		u32 id2 = cond_le32_to_cpu(ohci->self_id[i + 1]);
19748c2ecf20Sopenharmony_ci
19758c2ecf20Sopenharmony_ci		if (id != ~id2) {
19768c2ecf20Sopenharmony_ci			/*
19778c2ecf20Sopenharmony_ci			 * If the invalid data looks like a cycle start packet,
19788c2ecf20Sopenharmony_ci			 * it's likely to be the result of the cycle master
19798c2ecf20Sopenharmony_ci			 * having a wrong gap count.  In this case, the self IDs
19808c2ecf20Sopenharmony_ci			 * so far are valid and should be processed so that the
19818c2ecf20Sopenharmony_ci			 * bus manager can then correct the gap count.
19828c2ecf20Sopenharmony_ci			 */
19838c2ecf20Sopenharmony_ci			if (id == 0xffff008f) {
19848c2ecf20Sopenharmony_ci				ohci_notice(ohci, "ignoring spurious self IDs\n");
19858c2ecf20Sopenharmony_ci				self_id_count = j;
19868c2ecf20Sopenharmony_ci				break;
19878c2ecf20Sopenharmony_ci			}
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_ci			ohci_notice(ohci, "bad self ID %d/%d (%08x != ~%08x)\n",
19908c2ecf20Sopenharmony_ci				    j, self_id_count, id, id2);
19918c2ecf20Sopenharmony_ci			return;
19928c2ecf20Sopenharmony_ci		}
19938c2ecf20Sopenharmony_ci		ohci->self_id_buffer[j] = id;
19948c2ecf20Sopenharmony_ci	}
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	if (ohci->quirks & QUIRK_TI_SLLZ059) {
19978c2ecf20Sopenharmony_ci		self_id_count = find_and_insert_self_id(ohci, self_id_count);
19988c2ecf20Sopenharmony_ci		if (self_id_count < 0) {
19998c2ecf20Sopenharmony_ci			ohci_notice(ohci,
20008c2ecf20Sopenharmony_ci				    "could not construct local self ID\n");
20018c2ecf20Sopenharmony_ci			return;
20028c2ecf20Sopenharmony_ci		}
20038c2ecf20Sopenharmony_ci	}
20048c2ecf20Sopenharmony_ci
20058c2ecf20Sopenharmony_ci	if (self_id_count == 0) {
20068c2ecf20Sopenharmony_ci		ohci_notice(ohci, "no self IDs\n");
20078c2ecf20Sopenharmony_ci		return;
20088c2ecf20Sopenharmony_ci	}
20098c2ecf20Sopenharmony_ci	rmb();
20108c2ecf20Sopenharmony_ci
20118c2ecf20Sopenharmony_ci	/*
20128c2ecf20Sopenharmony_ci	 * Check the consistency of the self IDs we just read.  The
20138c2ecf20Sopenharmony_ci	 * problem we face is that a new bus reset can start while we
20148c2ecf20Sopenharmony_ci	 * read out the self IDs from the DMA buffer. If this happens,
20158c2ecf20Sopenharmony_ci	 * the DMA buffer will be overwritten with new self IDs and we
20168c2ecf20Sopenharmony_ci	 * will read out inconsistent data.  The OHCI specification
20178c2ecf20Sopenharmony_ci	 * (section 11.2) recommends a technique similar to
20188c2ecf20Sopenharmony_ci	 * linux/seqlock.h, where we remember the generation of the
20198c2ecf20Sopenharmony_ci	 * self IDs in the buffer before reading them out and compare
20208c2ecf20Sopenharmony_ci	 * it to the current generation after reading them out.  If
20218c2ecf20Sopenharmony_ci	 * the two generations match we know we have a consistent set
20228c2ecf20Sopenharmony_ci	 * of self IDs.
20238c2ecf20Sopenharmony_ci	 */
20248c2ecf20Sopenharmony_ci
20258c2ecf20Sopenharmony_ci	new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
20268c2ecf20Sopenharmony_ci	if (new_generation != generation) {
20278c2ecf20Sopenharmony_ci		ohci_notice(ohci, "new bus reset, discarding self ids\n");
20288c2ecf20Sopenharmony_ci		return;
20298c2ecf20Sopenharmony_ci	}
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci	/* FIXME: Document how the locking works. */
20328c2ecf20Sopenharmony_ci	spin_lock_irq(&ohci->lock);
20338c2ecf20Sopenharmony_ci
20348c2ecf20Sopenharmony_ci	ohci->generation = -1; /* prevent AT packet queueing */
20358c2ecf20Sopenharmony_ci	context_stop(&ohci->at_request_ctx);
20368c2ecf20Sopenharmony_ci	context_stop(&ohci->at_response_ctx);
20378c2ecf20Sopenharmony_ci
20388c2ecf20Sopenharmony_ci	spin_unlock_irq(&ohci->lock);
20398c2ecf20Sopenharmony_ci
20408c2ecf20Sopenharmony_ci	/*
20418c2ecf20Sopenharmony_ci	 * Per OHCI 1.2 draft, clause 7.2.3.3, hardware may leave unsent
20428c2ecf20Sopenharmony_ci	 * packets in the AT queues and software needs to drain them.
20438c2ecf20Sopenharmony_ci	 * Some OHCI 1.1 controllers (JMicron) apparently require this too.
20448c2ecf20Sopenharmony_ci	 */
20458c2ecf20Sopenharmony_ci	at_context_flush(&ohci->at_request_ctx);
20468c2ecf20Sopenharmony_ci	at_context_flush(&ohci->at_response_ctx);
20478c2ecf20Sopenharmony_ci
20488c2ecf20Sopenharmony_ci	spin_lock_irq(&ohci->lock);
20498c2ecf20Sopenharmony_ci
20508c2ecf20Sopenharmony_ci	ohci->generation = generation;
20518c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
20528c2ecf20Sopenharmony_ci
20538c2ecf20Sopenharmony_ci	if (ohci->quirks & QUIRK_RESET_PACKET)
20548c2ecf20Sopenharmony_ci		ohci->request_generation = generation;
20558c2ecf20Sopenharmony_ci
20568c2ecf20Sopenharmony_ci	/*
20578c2ecf20Sopenharmony_ci	 * This next bit is unrelated to the AT context stuff but we
20588c2ecf20Sopenharmony_ci	 * have to do it under the spinlock also.  If a new config rom
20598c2ecf20Sopenharmony_ci	 * was set up before this reset, the old one is now no longer
20608c2ecf20Sopenharmony_ci	 * in use and we can free it. Update the config rom pointers
20618c2ecf20Sopenharmony_ci	 * to point to the current config rom and clear the
20628c2ecf20Sopenharmony_ci	 * next_config_rom pointer so a new update can take place.
20638c2ecf20Sopenharmony_ci	 */
20648c2ecf20Sopenharmony_ci
20658c2ecf20Sopenharmony_ci	if (ohci->next_config_rom != NULL) {
20668c2ecf20Sopenharmony_ci		if (ohci->next_config_rom != ohci->config_rom) {
20678c2ecf20Sopenharmony_ci			free_rom      = ohci->config_rom;
20688c2ecf20Sopenharmony_ci			free_rom_bus  = ohci->config_rom_bus;
20698c2ecf20Sopenharmony_ci		}
20708c2ecf20Sopenharmony_ci		ohci->config_rom      = ohci->next_config_rom;
20718c2ecf20Sopenharmony_ci		ohci->config_rom_bus  = ohci->next_config_rom_bus;
20728c2ecf20Sopenharmony_ci		ohci->next_config_rom = NULL;
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci		/*
20758c2ecf20Sopenharmony_ci		 * Restore config_rom image and manually update
20768c2ecf20Sopenharmony_ci		 * config_rom registers.  Writing the header quadlet
20778c2ecf20Sopenharmony_ci		 * will indicate that the config rom is ready, so we
20788c2ecf20Sopenharmony_ci		 * do that last.
20798c2ecf20Sopenharmony_ci		 */
20808c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_BusOptions,
20818c2ecf20Sopenharmony_ci			  be32_to_cpu(ohci->config_rom[2]));
20828c2ecf20Sopenharmony_ci		ohci->config_rom[0] = ohci->next_header;
20838c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_ConfigROMhdr,
20848c2ecf20Sopenharmony_ci			  be32_to_cpu(ohci->next_header));
20858c2ecf20Sopenharmony_ci	}
20868c2ecf20Sopenharmony_ci
20878c2ecf20Sopenharmony_ci	if (param_remote_dma) {
20888c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0);
20898c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0);
20908c2ecf20Sopenharmony_ci	}
20918c2ecf20Sopenharmony_ci
20928c2ecf20Sopenharmony_ci	spin_unlock_irq(&ohci->lock);
20938c2ecf20Sopenharmony_ci
20948c2ecf20Sopenharmony_ci	if (free_rom)
20958c2ecf20Sopenharmony_ci		dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
20968c2ecf20Sopenharmony_ci				  free_rom, free_rom_bus);
20978c2ecf20Sopenharmony_ci
20988c2ecf20Sopenharmony_ci	log_selfids(ohci, generation, self_id_count);
20998c2ecf20Sopenharmony_ci
21008c2ecf20Sopenharmony_ci	fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
21018c2ecf20Sopenharmony_ci				 self_id_count, ohci->self_id_buffer,
21028c2ecf20Sopenharmony_ci				 ohci->csr_state_setclear_abdicate);
21038c2ecf20Sopenharmony_ci	ohci->csr_state_setclear_abdicate = false;
21048c2ecf20Sopenharmony_ci}
21058c2ecf20Sopenharmony_ci
21068c2ecf20Sopenharmony_cistatic irqreturn_t irq_handler(int irq, void *data)
21078c2ecf20Sopenharmony_ci{
21088c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = data;
21098c2ecf20Sopenharmony_ci	u32 event, iso_event;
21108c2ecf20Sopenharmony_ci	int i;
21118c2ecf20Sopenharmony_ci
21128c2ecf20Sopenharmony_ci	event = reg_read(ohci, OHCI1394_IntEventClear);
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	if (!event || !~event)
21158c2ecf20Sopenharmony_ci		return IRQ_NONE;
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_ci	/*
21188c2ecf20Sopenharmony_ci	 * busReset and postedWriteErr must not be cleared yet
21198c2ecf20Sopenharmony_ci	 * (OHCI 1.1 clauses 7.2.3.2 and 13.2.8.1)
21208c2ecf20Sopenharmony_ci	 */
21218c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IntEventClear,
21228c2ecf20Sopenharmony_ci		  event & ~(OHCI1394_busReset | OHCI1394_postedWriteErr));
21238c2ecf20Sopenharmony_ci	log_irqs(ohci, event);
21248c2ecf20Sopenharmony_ci
21258c2ecf20Sopenharmony_ci	if (event & OHCI1394_selfIDComplete)
21268c2ecf20Sopenharmony_ci		queue_work(selfid_workqueue, &ohci->bus_reset_work);
21278c2ecf20Sopenharmony_ci
21288c2ecf20Sopenharmony_ci	if (event & OHCI1394_RQPkt)
21298c2ecf20Sopenharmony_ci		tasklet_schedule(&ohci->ar_request_ctx.tasklet);
21308c2ecf20Sopenharmony_ci
21318c2ecf20Sopenharmony_ci	if (event & OHCI1394_RSPkt)
21328c2ecf20Sopenharmony_ci		tasklet_schedule(&ohci->ar_response_ctx.tasklet);
21338c2ecf20Sopenharmony_ci
21348c2ecf20Sopenharmony_ci	if (event & OHCI1394_reqTxComplete)
21358c2ecf20Sopenharmony_ci		tasklet_schedule(&ohci->at_request_ctx.tasklet);
21368c2ecf20Sopenharmony_ci
21378c2ecf20Sopenharmony_ci	if (event & OHCI1394_respTxComplete)
21388c2ecf20Sopenharmony_ci		tasklet_schedule(&ohci->at_response_ctx.tasklet);
21398c2ecf20Sopenharmony_ci
21408c2ecf20Sopenharmony_ci	if (event & OHCI1394_isochRx) {
21418c2ecf20Sopenharmony_ci		iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
21428c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
21438c2ecf20Sopenharmony_ci
21448c2ecf20Sopenharmony_ci		while (iso_event) {
21458c2ecf20Sopenharmony_ci			i = ffs(iso_event) - 1;
21468c2ecf20Sopenharmony_ci			tasklet_schedule(
21478c2ecf20Sopenharmony_ci				&ohci->ir_context_list[i].context.tasklet);
21488c2ecf20Sopenharmony_ci			iso_event &= ~(1 << i);
21498c2ecf20Sopenharmony_ci		}
21508c2ecf20Sopenharmony_ci	}
21518c2ecf20Sopenharmony_ci
21528c2ecf20Sopenharmony_ci	if (event & OHCI1394_isochTx) {
21538c2ecf20Sopenharmony_ci		iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
21548c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
21558c2ecf20Sopenharmony_ci
21568c2ecf20Sopenharmony_ci		while (iso_event) {
21578c2ecf20Sopenharmony_ci			i = ffs(iso_event) - 1;
21588c2ecf20Sopenharmony_ci			tasklet_schedule(
21598c2ecf20Sopenharmony_ci				&ohci->it_context_list[i].context.tasklet);
21608c2ecf20Sopenharmony_ci			iso_event &= ~(1 << i);
21618c2ecf20Sopenharmony_ci		}
21628c2ecf20Sopenharmony_ci	}
21638c2ecf20Sopenharmony_ci
21648c2ecf20Sopenharmony_ci	if (unlikely(event & OHCI1394_regAccessFail))
21658c2ecf20Sopenharmony_ci		ohci_err(ohci, "register access failure\n");
21668c2ecf20Sopenharmony_ci
21678c2ecf20Sopenharmony_ci	if (unlikely(event & OHCI1394_postedWriteErr)) {
21688c2ecf20Sopenharmony_ci		reg_read(ohci, OHCI1394_PostedWriteAddressHi);
21698c2ecf20Sopenharmony_ci		reg_read(ohci, OHCI1394_PostedWriteAddressLo);
21708c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IntEventClear,
21718c2ecf20Sopenharmony_ci			  OHCI1394_postedWriteErr);
21728c2ecf20Sopenharmony_ci		if (printk_ratelimit())
21738c2ecf20Sopenharmony_ci			ohci_err(ohci, "PCI posted write error\n");
21748c2ecf20Sopenharmony_ci	}
21758c2ecf20Sopenharmony_ci
21768c2ecf20Sopenharmony_ci	if (unlikely(event & OHCI1394_cycleTooLong)) {
21778c2ecf20Sopenharmony_ci		if (printk_ratelimit())
21788c2ecf20Sopenharmony_ci			ohci_notice(ohci, "isochronous cycle too long\n");
21798c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_LinkControlSet,
21808c2ecf20Sopenharmony_ci			  OHCI1394_LinkControl_cycleMaster);
21818c2ecf20Sopenharmony_ci	}
21828c2ecf20Sopenharmony_ci
21838c2ecf20Sopenharmony_ci	if (unlikely(event & OHCI1394_cycleInconsistent)) {
21848c2ecf20Sopenharmony_ci		/*
21858c2ecf20Sopenharmony_ci		 * We need to clear this event bit in order to make
21868c2ecf20Sopenharmony_ci		 * cycleMatch isochronous I/O work.  In theory we should
21878c2ecf20Sopenharmony_ci		 * stop active cycleMatch iso contexts now and restart
21888c2ecf20Sopenharmony_ci		 * them at least two cycles later.  (FIXME?)
21898c2ecf20Sopenharmony_ci		 */
21908c2ecf20Sopenharmony_ci		if (printk_ratelimit())
21918c2ecf20Sopenharmony_ci			ohci_notice(ohci, "isochronous cycle inconsistent\n");
21928c2ecf20Sopenharmony_ci	}
21938c2ecf20Sopenharmony_ci
21948c2ecf20Sopenharmony_ci	if (unlikely(event & OHCI1394_unrecoverableError))
21958c2ecf20Sopenharmony_ci		handle_dead_contexts(ohci);
21968c2ecf20Sopenharmony_ci
21978c2ecf20Sopenharmony_ci	if (event & OHCI1394_cycle64Seconds) {
21988c2ecf20Sopenharmony_ci		spin_lock(&ohci->lock);
21998c2ecf20Sopenharmony_ci		update_bus_time(ohci);
22008c2ecf20Sopenharmony_ci		spin_unlock(&ohci->lock);
22018c2ecf20Sopenharmony_ci	} else
22028c2ecf20Sopenharmony_ci		flush_writes(ohci);
22038c2ecf20Sopenharmony_ci
22048c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
22058c2ecf20Sopenharmony_ci}
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_cistatic int software_reset(struct fw_ohci *ohci)
22088c2ecf20Sopenharmony_ci{
22098c2ecf20Sopenharmony_ci	u32 val;
22108c2ecf20Sopenharmony_ci	int i;
22118c2ecf20Sopenharmony_ci
22128c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
22138c2ecf20Sopenharmony_ci	for (i = 0; i < 500; i++) {
22148c2ecf20Sopenharmony_ci		val = reg_read(ohci, OHCI1394_HCControlSet);
22158c2ecf20Sopenharmony_ci		if (!~val)
22168c2ecf20Sopenharmony_ci			return -ENODEV; /* Card was ejected. */
22178c2ecf20Sopenharmony_ci
22188c2ecf20Sopenharmony_ci		if (!(val & OHCI1394_HCControl_softReset))
22198c2ecf20Sopenharmony_ci			return 0;
22208c2ecf20Sopenharmony_ci
22218c2ecf20Sopenharmony_ci		msleep(1);
22228c2ecf20Sopenharmony_ci	}
22238c2ecf20Sopenharmony_ci
22248c2ecf20Sopenharmony_ci	return -EBUSY;
22258c2ecf20Sopenharmony_ci}
22268c2ecf20Sopenharmony_ci
22278c2ecf20Sopenharmony_cistatic void copy_config_rom(__be32 *dest, const __be32 *src, size_t length)
22288c2ecf20Sopenharmony_ci{
22298c2ecf20Sopenharmony_ci	size_t size = length * 4;
22308c2ecf20Sopenharmony_ci
22318c2ecf20Sopenharmony_ci	memcpy(dest, src, size);
22328c2ecf20Sopenharmony_ci	if (size < CONFIG_ROM_SIZE)
22338c2ecf20Sopenharmony_ci		memset(&dest[length], 0, CONFIG_ROM_SIZE - size);
22348c2ecf20Sopenharmony_ci}
22358c2ecf20Sopenharmony_ci
22368c2ecf20Sopenharmony_cistatic int configure_1394a_enhancements(struct fw_ohci *ohci)
22378c2ecf20Sopenharmony_ci{
22388c2ecf20Sopenharmony_ci	bool enable_1394a;
22398c2ecf20Sopenharmony_ci	int ret, clear, set, offset;
22408c2ecf20Sopenharmony_ci
22418c2ecf20Sopenharmony_ci	/* Check if the driver should configure link and PHY. */
22428c2ecf20Sopenharmony_ci	if (!(reg_read(ohci, OHCI1394_HCControlSet) &
22438c2ecf20Sopenharmony_ci	      OHCI1394_HCControl_programPhyEnable))
22448c2ecf20Sopenharmony_ci		return 0;
22458c2ecf20Sopenharmony_ci
22468c2ecf20Sopenharmony_ci	/* Paranoia: check whether the PHY supports 1394a, too. */
22478c2ecf20Sopenharmony_ci	enable_1394a = false;
22488c2ecf20Sopenharmony_ci	ret = read_phy_reg(ohci, 2);
22498c2ecf20Sopenharmony_ci	if (ret < 0)
22508c2ecf20Sopenharmony_ci		return ret;
22518c2ecf20Sopenharmony_ci	if ((ret & PHY_EXTENDED_REGISTERS) == PHY_EXTENDED_REGISTERS) {
22528c2ecf20Sopenharmony_ci		ret = read_paged_phy_reg(ohci, 1, 8);
22538c2ecf20Sopenharmony_ci		if (ret < 0)
22548c2ecf20Sopenharmony_ci			return ret;
22558c2ecf20Sopenharmony_ci		if (ret >= 1)
22568c2ecf20Sopenharmony_ci			enable_1394a = true;
22578c2ecf20Sopenharmony_ci	}
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ci	if (ohci->quirks & QUIRK_NO_1394A)
22608c2ecf20Sopenharmony_ci		enable_1394a = false;
22618c2ecf20Sopenharmony_ci
22628c2ecf20Sopenharmony_ci	/* Configure PHY and link consistently. */
22638c2ecf20Sopenharmony_ci	if (enable_1394a) {
22648c2ecf20Sopenharmony_ci		clear = 0;
22658c2ecf20Sopenharmony_ci		set = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
22668c2ecf20Sopenharmony_ci	} else {
22678c2ecf20Sopenharmony_ci		clear = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
22688c2ecf20Sopenharmony_ci		set = 0;
22698c2ecf20Sopenharmony_ci	}
22708c2ecf20Sopenharmony_ci	ret = update_phy_reg(ohci, 5, clear, set);
22718c2ecf20Sopenharmony_ci	if (ret < 0)
22728c2ecf20Sopenharmony_ci		return ret;
22738c2ecf20Sopenharmony_ci
22748c2ecf20Sopenharmony_ci	if (enable_1394a)
22758c2ecf20Sopenharmony_ci		offset = OHCI1394_HCControlSet;
22768c2ecf20Sopenharmony_ci	else
22778c2ecf20Sopenharmony_ci		offset = OHCI1394_HCControlClear;
22788c2ecf20Sopenharmony_ci	reg_write(ohci, offset, OHCI1394_HCControl_aPhyEnhanceEnable);
22798c2ecf20Sopenharmony_ci
22808c2ecf20Sopenharmony_ci	/* Clean up: configuration has been taken care of. */
22818c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_HCControlClear,
22828c2ecf20Sopenharmony_ci		  OHCI1394_HCControl_programPhyEnable);
22838c2ecf20Sopenharmony_ci
22848c2ecf20Sopenharmony_ci	return 0;
22858c2ecf20Sopenharmony_ci}
22868c2ecf20Sopenharmony_ci
22878c2ecf20Sopenharmony_cistatic int probe_tsb41ba3d(struct fw_ohci *ohci)
22888c2ecf20Sopenharmony_ci{
22898c2ecf20Sopenharmony_ci	/* TI vendor ID = 0x080028, TSB41BA3D product ID = 0x833005 (sic) */
22908c2ecf20Sopenharmony_ci	static const u8 id[] = { 0x08, 0x00, 0x28, 0x83, 0x30, 0x05, };
22918c2ecf20Sopenharmony_ci	int reg, i;
22928c2ecf20Sopenharmony_ci
22938c2ecf20Sopenharmony_ci	reg = read_phy_reg(ohci, 2);
22948c2ecf20Sopenharmony_ci	if (reg < 0)
22958c2ecf20Sopenharmony_ci		return reg;
22968c2ecf20Sopenharmony_ci	if ((reg & PHY_EXTENDED_REGISTERS) != PHY_EXTENDED_REGISTERS)
22978c2ecf20Sopenharmony_ci		return 0;
22988c2ecf20Sopenharmony_ci
22998c2ecf20Sopenharmony_ci	for (i = ARRAY_SIZE(id) - 1; i >= 0; i--) {
23008c2ecf20Sopenharmony_ci		reg = read_paged_phy_reg(ohci, 1, i + 10);
23018c2ecf20Sopenharmony_ci		if (reg < 0)
23028c2ecf20Sopenharmony_ci			return reg;
23038c2ecf20Sopenharmony_ci		if (reg != id[i])
23048c2ecf20Sopenharmony_ci			return 0;
23058c2ecf20Sopenharmony_ci	}
23068c2ecf20Sopenharmony_ci	return 1;
23078c2ecf20Sopenharmony_ci}
23088c2ecf20Sopenharmony_ci
23098c2ecf20Sopenharmony_cistatic int ohci_enable(struct fw_card *card,
23108c2ecf20Sopenharmony_ci		       const __be32 *config_rom, size_t length)
23118c2ecf20Sopenharmony_ci{
23128c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
23138c2ecf20Sopenharmony_ci	u32 lps, version, irqs;
23148c2ecf20Sopenharmony_ci	int i, ret;
23158c2ecf20Sopenharmony_ci
23168c2ecf20Sopenharmony_ci	ret = software_reset(ohci);
23178c2ecf20Sopenharmony_ci	if (ret < 0) {
23188c2ecf20Sopenharmony_ci		ohci_err(ohci, "failed to reset ohci card\n");
23198c2ecf20Sopenharmony_ci		return ret;
23208c2ecf20Sopenharmony_ci	}
23218c2ecf20Sopenharmony_ci
23228c2ecf20Sopenharmony_ci	/*
23238c2ecf20Sopenharmony_ci	 * Now enable LPS, which we need in order to start accessing
23248c2ecf20Sopenharmony_ci	 * most of the registers.  In fact, on some cards (ALI M5251),
23258c2ecf20Sopenharmony_ci	 * accessing registers in the SClk domain without LPS enabled
23268c2ecf20Sopenharmony_ci	 * will lock up the machine.  Wait 50msec to make sure we have
23278c2ecf20Sopenharmony_ci	 * full link enabled.  However, with some cards (well, at least
23288c2ecf20Sopenharmony_ci	 * a JMicron PCIe card), we have to try again sometimes.
23298c2ecf20Sopenharmony_ci	 *
23308c2ecf20Sopenharmony_ci	 * TI TSB82AA2 + TSB81BA3(A) cards signal LPS enabled early but
23318c2ecf20Sopenharmony_ci	 * cannot actually use the phy at that time.  These need tens of
23328c2ecf20Sopenharmony_ci	 * millisecods pause between LPS write and first phy access too.
23338c2ecf20Sopenharmony_ci	 */
23348c2ecf20Sopenharmony_ci
23358c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_HCControlSet,
23368c2ecf20Sopenharmony_ci		  OHCI1394_HCControl_LPS |
23378c2ecf20Sopenharmony_ci		  OHCI1394_HCControl_postedWriteEnable);
23388c2ecf20Sopenharmony_ci	flush_writes(ohci);
23398c2ecf20Sopenharmony_ci
23408c2ecf20Sopenharmony_ci	for (lps = 0, i = 0; !lps && i < 3; i++) {
23418c2ecf20Sopenharmony_ci		msleep(50);
23428c2ecf20Sopenharmony_ci		lps = reg_read(ohci, OHCI1394_HCControlSet) &
23438c2ecf20Sopenharmony_ci		      OHCI1394_HCControl_LPS;
23448c2ecf20Sopenharmony_ci	}
23458c2ecf20Sopenharmony_ci
23468c2ecf20Sopenharmony_ci	if (!lps) {
23478c2ecf20Sopenharmony_ci		ohci_err(ohci, "failed to set Link Power Status\n");
23488c2ecf20Sopenharmony_ci		return -EIO;
23498c2ecf20Sopenharmony_ci	}
23508c2ecf20Sopenharmony_ci
23518c2ecf20Sopenharmony_ci	if (ohci->quirks & QUIRK_TI_SLLZ059) {
23528c2ecf20Sopenharmony_ci		ret = probe_tsb41ba3d(ohci);
23538c2ecf20Sopenharmony_ci		if (ret < 0)
23548c2ecf20Sopenharmony_ci			return ret;
23558c2ecf20Sopenharmony_ci		if (ret)
23568c2ecf20Sopenharmony_ci			ohci_notice(ohci, "local TSB41BA3D phy\n");
23578c2ecf20Sopenharmony_ci		else
23588c2ecf20Sopenharmony_ci			ohci->quirks &= ~QUIRK_TI_SLLZ059;
23598c2ecf20Sopenharmony_ci	}
23608c2ecf20Sopenharmony_ci
23618c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_HCControlClear,
23628c2ecf20Sopenharmony_ci		  OHCI1394_HCControl_noByteSwapData);
23638c2ecf20Sopenharmony_ci
23648c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
23658c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_LinkControlSet,
23668c2ecf20Sopenharmony_ci		  OHCI1394_LinkControl_cycleTimerEnable |
23678c2ecf20Sopenharmony_ci		  OHCI1394_LinkControl_cycleMaster);
23688c2ecf20Sopenharmony_ci
23698c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_ATRetries,
23708c2ecf20Sopenharmony_ci		  OHCI1394_MAX_AT_REQ_RETRIES |
23718c2ecf20Sopenharmony_ci		  (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
23728c2ecf20Sopenharmony_ci		  (OHCI1394_MAX_PHYS_RESP_RETRIES << 8) |
23738c2ecf20Sopenharmony_ci		  (200 << 16));
23748c2ecf20Sopenharmony_ci
23758c2ecf20Sopenharmony_ci	ohci->bus_time_running = false;
23768c2ecf20Sopenharmony_ci
23778c2ecf20Sopenharmony_ci	for (i = 0; i < 32; i++)
23788c2ecf20Sopenharmony_ci		if (ohci->ir_context_support & (1 << i))
23798c2ecf20Sopenharmony_ci			reg_write(ohci, OHCI1394_IsoRcvContextControlClear(i),
23808c2ecf20Sopenharmony_ci				  IR_CONTEXT_MULTI_CHANNEL_MODE);
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_ci	version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
23838c2ecf20Sopenharmony_ci	if (version >= OHCI_VERSION_1_1) {
23848c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_InitialChannelsAvailableHi,
23858c2ecf20Sopenharmony_ci			  0xfffffffe);
23868c2ecf20Sopenharmony_ci		card->broadcast_channel_auto_allocated = true;
23878c2ecf20Sopenharmony_ci	}
23888c2ecf20Sopenharmony_ci
23898c2ecf20Sopenharmony_ci	/* Get implemented bits of the priority arbitration request counter. */
23908c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_FairnessControl, 0x3f);
23918c2ecf20Sopenharmony_ci	ohci->pri_req_max = reg_read(ohci, OHCI1394_FairnessControl) & 0x3f;
23928c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_FairnessControl, 0);
23938c2ecf20Sopenharmony_ci	card->priority_budget_implemented = ohci->pri_req_max != 0;
23948c2ecf20Sopenharmony_ci
23958c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_PhyUpperBound, FW_MAX_PHYSICAL_RANGE >> 16);
23968c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IntEventClear, ~0);
23978c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IntMaskClear, ~0);
23988c2ecf20Sopenharmony_ci
23998c2ecf20Sopenharmony_ci	ret = configure_1394a_enhancements(ohci);
24008c2ecf20Sopenharmony_ci	if (ret < 0)
24018c2ecf20Sopenharmony_ci		return ret;
24028c2ecf20Sopenharmony_ci
24038c2ecf20Sopenharmony_ci	/* Activate link_on bit and contender bit in our self ID packets.*/
24048c2ecf20Sopenharmony_ci	ret = ohci_update_phy_reg(card, 4, 0, PHY_LINK_ACTIVE | PHY_CONTENDER);
24058c2ecf20Sopenharmony_ci	if (ret < 0)
24068c2ecf20Sopenharmony_ci		return ret;
24078c2ecf20Sopenharmony_ci
24088c2ecf20Sopenharmony_ci	/*
24098c2ecf20Sopenharmony_ci	 * When the link is not yet enabled, the atomic config rom
24108c2ecf20Sopenharmony_ci	 * update mechanism described below in ohci_set_config_rom()
24118c2ecf20Sopenharmony_ci	 * is not active.  We have to update ConfigRomHeader and
24128c2ecf20Sopenharmony_ci	 * BusOptions manually, and the write to ConfigROMmap takes
24138c2ecf20Sopenharmony_ci	 * effect immediately.  We tie this to the enabling of the
24148c2ecf20Sopenharmony_ci	 * link, so we have a valid config rom before enabling - the
24158c2ecf20Sopenharmony_ci	 * OHCI requires that ConfigROMhdr and BusOptions have valid
24168c2ecf20Sopenharmony_ci	 * values before enabling.
24178c2ecf20Sopenharmony_ci	 *
24188c2ecf20Sopenharmony_ci	 * However, when the ConfigROMmap is written, some controllers
24198c2ecf20Sopenharmony_ci	 * always read back quadlets 0 and 2 from the config rom to
24208c2ecf20Sopenharmony_ci	 * the ConfigRomHeader and BusOptions registers on bus reset.
24218c2ecf20Sopenharmony_ci	 * They shouldn't do that in this initial case where the link
24228c2ecf20Sopenharmony_ci	 * isn't enabled.  This means we have to use the same
24238c2ecf20Sopenharmony_ci	 * workaround here, setting the bus header to 0 and then write
24248c2ecf20Sopenharmony_ci	 * the right values in the bus reset tasklet.
24258c2ecf20Sopenharmony_ci	 */
24268c2ecf20Sopenharmony_ci
24278c2ecf20Sopenharmony_ci	if (config_rom) {
24288c2ecf20Sopenharmony_ci		ohci->next_config_rom =
24298c2ecf20Sopenharmony_ci			dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
24308c2ecf20Sopenharmony_ci					   &ohci->next_config_rom_bus,
24318c2ecf20Sopenharmony_ci					   GFP_KERNEL);
24328c2ecf20Sopenharmony_ci		if (ohci->next_config_rom == NULL)
24338c2ecf20Sopenharmony_ci			return -ENOMEM;
24348c2ecf20Sopenharmony_ci
24358c2ecf20Sopenharmony_ci		copy_config_rom(ohci->next_config_rom, config_rom, length);
24368c2ecf20Sopenharmony_ci	} else {
24378c2ecf20Sopenharmony_ci		/*
24388c2ecf20Sopenharmony_ci		 * In the suspend case, config_rom is NULL, which
24398c2ecf20Sopenharmony_ci		 * means that we just reuse the old config rom.
24408c2ecf20Sopenharmony_ci		 */
24418c2ecf20Sopenharmony_ci		ohci->next_config_rom = ohci->config_rom;
24428c2ecf20Sopenharmony_ci		ohci->next_config_rom_bus = ohci->config_rom_bus;
24438c2ecf20Sopenharmony_ci	}
24448c2ecf20Sopenharmony_ci
24458c2ecf20Sopenharmony_ci	ohci->next_header = ohci->next_config_rom[0];
24468c2ecf20Sopenharmony_ci	ohci->next_config_rom[0] = 0;
24478c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
24488c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_BusOptions,
24498c2ecf20Sopenharmony_ci		  be32_to_cpu(ohci->next_config_rom[2]));
24508c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
24518c2ecf20Sopenharmony_ci
24528c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
24538c2ecf20Sopenharmony_ci
24548c2ecf20Sopenharmony_ci	irqs =	OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
24558c2ecf20Sopenharmony_ci		OHCI1394_RQPkt | OHCI1394_RSPkt |
24568c2ecf20Sopenharmony_ci		OHCI1394_isochTx | OHCI1394_isochRx |
24578c2ecf20Sopenharmony_ci		OHCI1394_postedWriteErr |
24588c2ecf20Sopenharmony_ci		OHCI1394_selfIDComplete |
24598c2ecf20Sopenharmony_ci		OHCI1394_regAccessFail |
24608c2ecf20Sopenharmony_ci		OHCI1394_cycleInconsistent |
24618c2ecf20Sopenharmony_ci		OHCI1394_unrecoverableError |
24628c2ecf20Sopenharmony_ci		OHCI1394_cycleTooLong |
24638c2ecf20Sopenharmony_ci		OHCI1394_masterIntEnable;
24648c2ecf20Sopenharmony_ci	if (param_debug & OHCI_PARAM_DEBUG_BUSRESETS)
24658c2ecf20Sopenharmony_ci		irqs |= OHCI1394_busReset;
24668c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IntMaskSet, irqs);
24678c2ecf20Sopenharmony_ci
24688c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_HCControlSet,
24698c2ecf20Sopenharmony_ci		  OHCI1394_HCControl_linkEnable |
24708c2ecf20Sopenharmony_ci		  OHCI1394_HCControl_BIBimageValid);
24718c2ecf20Sopenharmony_ci
24728c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_LinkControlSet,
24738c2ecf20Sopenharmony_ci		  OHCI1394_LinkControl_rcvSelfID |
24748c2ecf20Sopenharmony_ci		  OHCI1394_LinkControl_rcvPhyPkt);
24758c2ecf20Sopenharmony_ci
24768c2ecf20Sopenharmony_ci	ar_context_run(&ohci->ar_request_ctx);
24778c2ecf20Sopenharmony_ci	ar_context_run(&ohci->ar_response_ctx);
24788c2ecf20Sopenharmony_ci
24798c2ecf20Sopenharmony_ci	flush_writes(ohci);
24808c2ecf20Sopenharmony_ci
24818c2ecf20Sopenharmony_ci	/* We are ready to go, reset bus to finish initialization. */
24828c2ecf20Sopenharmony_ci	fw_schedule_bus_reset(&ohci->card, false, true);
24838c2ecf20Sopenharmony_ci
24848c2ecf20Sopenharmony_ci	return 0;
24858c2ecf20Sopenharmony_ci}
24868c2ecf20Sopenharmony_ci
24878c2ecf20Sopenharmony_cistatic int ohci_set_config_rom(struct fw_card *card,
24888c2ecf20Sopenharmony_ci			       const __be32 *config_rom, size_t length)
24898c2ecf20Sopenharmony_ci{
24908c2ecf20Sopenharmony_ci	struct fw_ohci *ohci;
24918c2ecf20Sopenharmony_ci	__be32 *next_config_rom;
24928c2ecf20Sopenharmony_ci	dma_addr_t next_config_rom_bus;
24938c2ecf20Sopenharmony_ci
24948c2ecf20Sopenharmony_ci	ohci = fw_ohci(card);
24958c2ecf20Sopenharmony_ci
24968c2ecf20Sopenharmony_ci	/*
24978c2ecf20Sopenharmony_ci	 * When the OHCI controller is enabled, the config rom update
24988c2ecf20Sopenharmony_ci	 * mechanism is a bit tricky, but easy enough to use.  See
24998c2ecf20Sopenharmony_ci	 * section 5.5.6 in the OHCI specification.
25008c2ecf20Sopenharmony_ci	 *
25018c2ecf20Sopenharmony_ci	 * The OHCI controller caches the new config rom address in a
25028c2ecf20Sopenharmony_ci	 * shadow register (ConfigROMmapNext) and needs a bus reset
25038c2ecf20Sopenharmony_ci	 * for the changes to take place.  When the bus reset is
25048c2ecf20Sopenharmony_ci	 * detected, the controller loads the new values for the
25058c2ecf20Sopenharmony_ci	 * ConfigRomHeader and BusOptions registers from the specified
25068c2ecf20Sopenharmony_ci	 * config rom and loads ConfigROMmap from the ConfigROMmapNext
25078c2ecf20Sopenharmony_ci	 * shadow register. All automatically and atomically.
25088c2ecf20Sopenharmony_ci	 *
25098c2ecf20Sopenharmony_ci	 * Now, there's a twist to this story.  The automatic load of
25108c2ecf20Sopenharmony_ci	 * ConfigRomHeader and BusOptions doesn't honor the
25118c2ecf20Sopenharmony_ci	 * noByteSwapData bit, so with a be32 config rom, the
25128c2ecf20Sopenharmony_ci	 * controller will load be32 values in to these registers
25138c2ecf20Sopenharmony_ci	 * during the atomic update, even on litte endian
25148c2ecf20Sopenharmony_ci	 * architectures.  The workaround we use is to put a 0 in the
25158c2ecf20Sopenharmony_ci	 * header quadlet; 0 is endian agnostic and means that the
25168c2ecf20Sopenharmony_ci	 * config rom isn't ready yet.  In the bus reset tasklet we
25178c2ecf20Sopenharmony_ci	 * then set up the real values for the two registers.
25188c2ecf20Sopenharmony_ci	 *
25198c2ecf20Sopenharmony_ci	 * We use ohci->lock to avoid racing with the code that sets
25208c2ecf20Sopenharmony_ci	 * ohci->next_config_rom to NULL (see bus_reset_work).
25218c2ecf20Sopenharmony_ci	 */
25228c2ecf20Sopenharmony_ci
25238c2ecf20Sopenharmony_ci	next_config_rom =
25248c2ecf20Sopenharmony_ci		dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
25258c2ecf20Sopenharmony_ci				   &next_config_rom_bus, GFP_KERNEL);
25268c2ecf20Sopenharmony_ci	if (next_config_rom == NULL)
25278c2ecf20Sopenharmony_ci		return -ENOMEM;
25288c2ecf20Sopenharmony_ci
25298c2ecf20Sopenharmony_ci	spin_lock_irq(&ohci->lock);
25308c2ecf20Sopenharmony_ci
25318c2ecf20Sopenharmony_ci	/*
25328c2ecf20Sopenharmony_ci	 * If there is not an already pending config_rom update,
25338c2ecf20Sopenharmony_ci	 * push our new allocation into the ohci->next_config_rom
25348c2ecf20Sopenharmony_ci	 * and then mark the local variable as null so that we
25358c2ecf20Sopenharmony_ci	 * won't deallocate the new buffer.
25368c2ecf20Sopenharmony_ci	 *
25378c2ecf20Sopenharmony_ci	 * OTOH, if there is a pending config_rom update, just
25388c2ecf20Sopenharmony_ci	 * use that buffer with the new config_rom data, and
25398c2ecf20Sopenharmony_ci	 * let this routine free the unused DMA allocation.
25408c2ecf20Sopenharmony_ci	 */
25418c2ecf20Sopenharmony_ci
25428c2ecf20Sopenharmony_ci	if (ohci->next_config_rom == NULL) {
25438c2ecf20Sopenharmony_ci		ohci->next_config_rom = next_config_rom;
25448c2ecf20Sopenharmony_ci		ohci->next_config_rom_bus = next_config_rom_bus;
25458c2ecf20Sopenharmony_ci		next_config_rom = NULL;
25468c2ecf20Sopenharmony_ci	}
25478c2ecf20Sopenharmony_ci
25488c2ecf20Sopenharmony_ci	copy_config_rom(ohci->next_config_rom, config_rom, length);
25498c2ecf20Sopenharmony_ci
25508c2ecf20Sopenharmony_ci	ohci->next_header = config_rom[0];
25518c2ecf20Sopenharmony_ci	ohci->next_config_rom[0] = 0;
25528c2ecf20Sopenharmony_ci
25538c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
25548c2ecf20Sopenharmony_ci
25558c2ecf20Sopenharmony_ci	spin_unlock_irq(&ohci->lock);
25568c2ecf20Sopenharmony_ci
25578c2ecf20Sopenharmony_ci	/* If we didn't use the DMA allocation, delete it. */
25588c2ecf20Sopenharmony_ci	if (next_config_rom != NULL)
25598c2ecf20Sopenharmony_ci		dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
25608c2ecf20Sopenharmony_ci				  next_config_rom, next_config_rom_bus);
25618c2ecf20Sopenharmony_ci
25628c2ecf20Sopenharmony_ci	/*
25638c2ecf20Sopenharmony_ci	 * Now initiate a bus reset to have the changes take
25648c2ecf20Sopenharmony_ci	 * effect. We clean up the old config rom memory and DMA
25658c2ecf20Sopenharmony_ci	 * mappings in the bus reset tasklet, since the OHCI
25668c2ecf20Sopenharmony_ci	 * controller could need to access it before the bus reset
25678c2ecf20Sopenharmony_ci	 * takes effect.
25688c2ecf20Sopenharmony_ci	 */
25698c2ecf20Sopenharmony_ci
25708c2ecf20Sopenharmony_ci	fw_schedule_bus_reset(&ohci->card, true, true);
25718c2ecf20Sopenharmony_ci
25728c2ecf20Sopenharmony_ci	return 0;
25738c2ecf20Sopenharmony_ci}
25748c2ecf20Sopenharmony_ci
25758c2ecf20Sopenharmony_cistatic void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
25768c2ecf20Sopenharmony_ci{
25778c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
25788c2ecf20Sopenharmony_ci
25798c2ecf20Sopenharmony_ci	at_context_transmit(&ohci->at_request_ctx, packet);
25808c2ecf20Sopenharmony_ci}
25818c2ecf20Sopenharmony_ci
25828c2ecf20Sopenharmony_cistatic void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
25838c2ecf20Sopenharmony_ci{
25848c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
25858c2ecf20Sopenharmony_ci
25868c2ecf20Sopenharmony_ci	at_context_transmit(&ohci->at_response_ctx, packet);
25878c2ecf20Sopenharmony_ci}
25888c2ecf20Sopenharmony_ci
25898c2ecf20Sopenharmony_cistatic int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
25908c2ecf20Sopenharmony_ci{
25918c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
25928c2ecf20Sopenharmony_ci	struct context *ctx = &ohci->at_request_ctx;
25938c2ecf20Sopenharmony_ci	struct driver_data *driver_data = packet->driver_data;
25948c2ecf20Sopenharmony_ci	int ret = -ENOENT;
25958c2ecf20Sopenharmony_ci
25968c2ecf20Sopenharmony_ci	tasklet_disable(&ctx->tasklet);
25978c2ecf20Sopenharmony_ci
25988c2ecf20Sopenharmony_ci	if (packet->ack != 0)
25998c2ecf20Sopenharmony_ci		goto out;
26008c2ecf20Sopenharmony_ci
26018c2ecf20Sopenharmony_ci	if (packet->payload_mapped)
26028c2ecf20Sopenharmony_ci		dma_unmap_single(ohci->card.device, packet->payload_bus,
26038c2ecf20Sopenharmony_ci				 packet->payload_length, DMA_TO_DEVICE);
26048c2ecf20Sopenharmony_ci
26058c2ecf20Sopenharmony_ci	log_ar_at_event(ohci, 'T', packet->speed, packet->header, 0x20);
26068c2ecf20Sopenharmony_ci	driver_data->packet = NULL;
26078c2ecf20Sopenharmony_ci	packet->ack = RCODE_CANCELLED;
26088c2ecf20Sopenharmony_ci	packet->callback(packet, &ohci->card, packet->ack);
26098c2ecf20Sopenharmony_ci	ret = 0;
26108c2ecf20Sopenharmony_ci out:
26118c2ecf20Sopenharmony_ci	tasklet_enable(&ctx->tasklet);
26128c2ecf20Sopenharmony_ci
26138c2ecf20Sopenharmony_ci	return ret;
26148c2ecf20Sopenharmony_ci}
26158c2ecf20Sopenharmony_ci
26168c2ecf20Sopenharmony_cistatic int ohci_enable_phys_dma(struct fw_card *card,
26178c2ecf20Sopenharmony_ci				int node_id, int generation)
26188c2ecf20Sopenharmony_ci{
26198c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
26208c2ecf20Sopenharmony_ci	unsigned long flags;
26218c2ecf20Sopenharmony_ci	int n, ret = 0;
26228c2ecf20Sopenharmony_ci
26238c2ecf20Sopenharmony_ci	if (param_remote_dma)
26248c2ecf20Sopenharmony_ci		return 0;
26258c2ecf20Sopenharmony_ci
26268c2ecf20Sopenharmony_ci	/*
26278c2ecf20Sopenharmony_ci	 * FIXME:  Make sure this bitmask is cleared when we clear the busReset
26288c2ecf20Sopenharmony_ci	 * interrupt bit.  Clear physReqResourceAllBuses on bus reset.
26298c2ecf20Sopenharmony_ci	 */
26308c2ecf20Sopenharmony_ci
26318c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ohci->lock, flags);
26328c2ecf20Sopenharmony_ci
26338c2ecf20Sopenharmony_ci	if (ohci->generation != generation) {
26348c2ecf20Sopenharmony_ci		ret = -ESTALE;
26358c2ecf20Sopenharmony_ci		goto out;
26368c2ecf20Sopenharmony_ci	}
26378c2ecf20Sopenharmony_ci
26388c2ecf20Sopenharmony_ci	/*
26398c2ecf20Sopenharmony_ci	 * Note, if the node ID contains a non-local bus ID, physical DMA is
26408c2ecf20Sopenharmony_ci	 * enabled for _all_ nodes on remote buses.
26418c2ecf20Sopenharmony_ci	 */
26428c2ecf20Sopenharmony_ci
26438c2ecf20Sopenharmony_ci	n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
26448c2ecf20Sopenharmony_ci	if (n < 32)
26458c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
26468c2ecf20Sopenharmony_ci	else
26478c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
26488c2ecf20Sopenharmony_ci
26498c2ecf20Sopenharmony_ci	flush_writes(ohci);
26508c2ecf20Sopenharmony_ci out:
26518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ohci->lock, flags);
26528c2ecf20Sopenharmony_ci
26538c2ecf20Sopenharmony_ci	return ret;
26548c2ecf20Sopenharmony_ci}
26558c2ecf20Sopenharmony_ci
26568c2ecf20Sopenharmony_cistatic u32 ohci_read_csr(struct fw_card *card, int csr_offset)
26578c2ecf20Sopenharmony_ci{
26588c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
26598c2ecf20Sopenharmony_ci	unsigned long flags;
26608c2ecf20Sopenharmony_ci	u32 value;
26618c2ecf20Sopenharmony_ci
26628c2ecf20Sopenharmony_ci	switch (csr_offset) {
26638c2ecf20Sopenharmony_ci	case CSR_STATE_CLEAR:
26648c2ecf20Sopenharmony_ci	case CSR_STATE_SET:
26658c2ecf20Sopenharmony_ci		if (ohci->is_root &&
26668c2ecf20Sopenharmony_ci		    (reg_read(ohci, OHCI1394_LinkControlSet) &
26678c2ecf20Sopenharmony_ci		     OHCI1394_LinkControl_cycleMaster))
26688c2ecf20Sopenharmony_ci			value = CSR_STATE_BIT_CMSTR;
26698c2ecf20Sopenharmony_ci		else
26708c2ecf20Sopenharmony_ci			value = 0;
26718c2ecf20Sopenharmony_ci		if (ohci->csr_state_setclear_abdicate)
26728c2ecf20Sopenharmony_ci			value |= CSR_STATE_BIT_ABDICATE;
26738c2ecf20Sopenharmony_ci
26748c2ecf20Sopenharmony_ci		return value;
26758c2ecf20Sopenharmony_ci
26768c2ecf20Sopenharmony_ci	case CSR_NODE_IDS:
26778c2ecf20Sopenharmony_ci		return reg_read(ohci, OHCI1394_NodeID) << 16;
26788c2ecf20Sopenharmony_ci
26798c2ecf20Sopenharmony_ci	case CSR_CYCLE_TIME:
26808c2ecf20Sopenharmony_ci		return get_cycle_time(ohci);
26818c2ecf20Sopenharmony_ci
26828c2ecf20Sopenharmony_ci	case CSR_BUS_TIME:
26838c2ecf20Sopenharmony_ci		/*
26848c2ecf20Sopenharmony_ci		 * We might be called just after the cycle timer has wrapped
26858c2ecf20Sopenharmony_ci		 * around but just before the cycle64Seconds handler, so we
26868c2ecf20Sopenharmony_ci		 * better check here, too, if the bus time needs to be updated.
26878c2ecf20Sopenharmony_ci		 */
26888c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ohci->lock, flags);
26898c2ecf20Sopenharmony_ci		value = update_bus_time(ohci);
26908c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ohci->lock, flags);
26918c2ecf20Sopenharmony_ci		return value;
26928c2ecf20Sopenharmony_ci
26938c2ecf20Sopenharmony_ci	case CSR_BUSY_TIMEOUT:
26948c2ecf20Sopenharmony_ci		value = reg_read(ohci, OHCI1394_ATRetries);
26958c2ecf20Sopenharmony_ci		return (value >> 4) & 0x0ffff00f;
26968c2ecf20Sopenharmony_ci
26978c2ecf20Sopenharmony_ci	case CSR_PRIORITY_BUDGET:
26988c2ecf20Sopenharmony_ci		return (reg_read(ohci, OHCI1394_FairnessControl) & 0x3f) |
26998c2ecf20Sopenharmony_ci			(ohci->pri_req_max << 8);
27008c2ecf20Sopenharmony_ci
27018c2ecf20Sopenharmony_ci	default:
27028c2ecf20Sopenharmony_ci		WARN_ON(1);
27038c2ecf20Sopenharmony_ci		return 0;
27048c2ecf20Sopenharmony_ci	}
27058c2ecf20Sopenharmony_ci}
27068c2ecf20Sopenharmony_ci
27078c2ecf20Sopenharmony_cistatic void ohci_write_csr(struct fw_card *card, int csr_offset, u32 value)
27088c2ecf20Sopenharmony_ci{
27098c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
27108c2ecf20Sopenharmony_ci	unsigned long flags;
27118c2ecf20Sopenharmony_ci
27128c2ecf20Sopenharmony_ci	switch (csr_offset) {
27138c2ecf20Sopenharmony_ci	case CSR_STATE_CLEAR:
27148c2ecf20Sopenharmony_ci		if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
27158c2ecf20Sopenharmony_ci			reg_write(ohci, OHCI1394_LinkControlClear,
27168c2ecf20Sopenharmony_ci				  OHCI1394_LinkControl_cycleMaster);
27178c2ecf20Sopenharmony_ci			flush_writes(ohci);
27188c2ecf20Sopenharmony_ci		}
27198c2ecf20Sopenharmony_ci		if (value & CSR_STATE_BIT_ABDICATE)
27208c2ecf20Sopenharmony_ci			ohci->csr_state_setclear_abdicate = false;
27218c2ecf20Sopenharmony_ci		break;
27228c2ecf20Sopenharmony_ci
27238c2ecf20Sopenharmony_ci	case CSR_STATE_SET:
27248c2ecf20Sopenharmony_ci		if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
27258c2ecf20Sopenharmony_ci			reg_write(ohci, OHCI1394_LinkControlSet,
27268c2ecf20Sopenharmony_ci				  OHCI1394_LinkControl_cycleMaster);
27278c2ecf20Sopenharmony_ci			flush_writes(ohci);
27288c2ecf20Sopenharmony_ci		}
27298c2ecf20Sopenharmony_ci		if (value & CSR_STATE_BIT_ABDICATE)
27308c2ecf20Sopenharmony_ci			ohci->csr_state_setclear_abdicate = true;
27318c2ecf20Sopenharmony_ci		break;
27328c2ecf20Sopenharmony_ci
27338c2ecf20Sopenharmony_ci	case CSR_NODE_IDS:
27348c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_NodeID, value >> 16);
27358c2ecf20Sopenharmony_ci		flush_writes(ohci);
27368c2ecf20Sopenharmony_ci		break;
27378c2ecf20Sopenharmony_ci
27388c2ecf20Sopenharmony_ci	case CSR_CYCLE_TIME:
27398c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsochronousCycleTimer, value);
27408c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IntEventSet,
27418c2ecf20Sopenharmony_ci			  OHCI1394_cycleInconsistent);
27428c2ecf20Sopenharmony_ci		flush_writes(ohci);
27438c2ecf20Sopenharmony_ci		break;
27448c2ecf20Sopenharmony_ci
27458c2ecf20Sopenharmony_ci	case CSR_BUS_TIME:
27468c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ohci->lock, flags);
27478c2ecf20Sopenharmony_ci		ohci->bus_time = (update_bus_time(ohci) & 0x40) |
27488c2ecf20Sopenharmony_ci		                 (value & ~0x7f);
27498c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ohci->lock, flags);
27508c2ecf20Sopenharmony_ci		break;
27518c2ecf20Sopenharmony_ci
27528c2ecf20Sopenharmony_ci	case CSR_BUSY_TIMEOUT:
27538c2ecf20Sopenharmony_ci		value = (value & 0xf) | ((value & 0xf) << 4) |
27548c2ecf20Sopenharmony_ci			((value & 0xf) << 8) | ((value & 0x0ffff000) << 4);
27558c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_ATRetries, value);
27568c2ecf20Sopenharmony_ci		flush_writes(ohci);
27578c2ecf20Sopenharmony_ci		break;
27588c2ecf20Sopenharmony_ci
27598c2ecf20Sopenharmony_ci	case CSR_PRIORITY_BUDGET:
27608c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_FairnessControl, value & 0x3f);
27618c2ecf20Sopenharmony_ci		flush_writes(ohci);
27628c2ecf20Sopenharmony_ci		break;
27638c2ecf20Sopenharmony_ci
27648c2ecf20Sopenharmony_ci	default:
27658c2ecf20Sopenharmony_ci		WARN_ON(1);
27668c2ecf20Sopenharmony_ci		break;
27678c2ecf20Sopenharmony_ci	}
27688c2ecf20Sopenharmony_ci}
27698c2ecf20Sopenharmony_ci
27708c2ecf20Sopenharmony_cistatic void flush_iso_completions(struct iso_context *ctx)
27718c2ecf20Sopenharmony_ci{
27728c2ecf20Sopenharmony_ci	ctx->base.callback.sc(&ctx->base, ctx->last_timestamp,
27738c2ecf20Sopenharmony_ci			      ctx->header_length, ctx->header,
27748c2ecf20Sopenharmony_ci			      ctx->base.callback_data);
27758c2ecf20Sopenharmony_ci	ctx->header_length = 0;
27768c2ecf20Sopenharmony_ci}
27778c2ecf20Sopenharmony_ci
27788c2ecf20Sopenharmony_cistatic void copy_iso_headers(struct iso_context *ctx, const u32 *dma_hdr)
27798c2ecf20Sopenharmony_ci{
27808c2ecf20Sopenharmony_ci	u32 *ctx_hdr;
27818c2ecf20Sopenharmony_ci
27828c2ecf20Sopenharmony_ci	if (ctx->header_length + ctx->base.header_size > PAGE_SIZE) {
27838c2ecf20Sopenharmony_ci		if (ctx->base.drop_overflow_headers)
27848c2ecf20Sopenharmony_ci			return;
27858c2ecf20Sopenharmony_ci		flush_iso_completions(ctx);
27868c2ecf20Sopenharmony_ci	}
27878c2ecf20Sopenharmony_ci
27888c2ecf20Sopenharmony_ci	ctx_hdr = ctx->header + ctx->header_length;
27898c2ecf20Sopenharmony_ci	ctx->last_timestamp = (u16)le32_to_cpu((__force __le32)dma_hdr[0]);
27908c2ecf20Sopenharmony_ci
27918c2ecf20Sopenharmony_ci	/*
27928c2ecf20Sopenharmony_ci	 * The two iso header quadlets are byteswapped to little
27938c2ecf20Sopenharmony_ci	 * endian by the controller, but we want to present them
27948c2ecf20Sopenharmony_ci	 * as big endian for consistency with the bus endianness.
27958c2ecf20Sopenharmony_ci	 */
27968c2ecf20Sopenharmony_ci	if (ctx->base.header_size > 0)
27978c2ecf20Sopenharmony_ci		ctx_hdr[0] = swab32(dma_hdr[1]); /* iso packet header */
27988c2ecf20Sopenharmony_ci	if (ctx->base.header_size > 4)
27998c2ecf20Sopenharmony_ci		ctx_hdr[1] = swab32(dma_hdr[0]); /* timestamp */
28008c2ecf20Sopenharmony_ci	if (ctx->base.header_size > 8)
28018c2ecf20Sopenharmony_ci		memcpy(&ctx_hdr[2], &dma_hdr[2], ctx->base.header_size - 8);
28028c2ecf20Sopenharmony_ci	ctx->header_length += ctx->base.header_size;
28038c2ecf20Sopenharmony_ci}
28048c2ecf20Sopenharmony_ci
28058c2ecf20Sopenharmony_cistatic int handle_ir_packet_per_buffer(struct context *context,
28068c2ecf20Sopenharmony_ci				       struct descriptor *d,
28078c2ecf20Sopenharmony_ci				       struct descriptor *last)
28088c2ecf20Sopenharmony_ci{
28098c2ecf20Sopenharmony_ci	struct iso_context *ctx =
28108c2ecf20Sopenharmony_ci		container_of(context, struct iso_context, context);
28118c2ecf20Sopenharmony_ci	struct descriptor *pd;
28128c2ecf20Sopenharmony_ci	u32 buffer_dma;
28138c2ecf20Sopenharmony_ci
28148c2ecf20Sopenharmony_ci	for (pd = d; pd <= last; pd++)
28158c2ecf20Sopenharmony_ci		if (pd->transfer_status)
28168c2ecf20Sopenharmony_ci			break;
28178c2ecf20Sopenharmony_ci	if (pd > last)
28188c2ecf20Sopenharmony_ci		/* Descriptor(s) not done yet, stop iteration */
28198c2ecf20Sopenharmony_ci		return 0;
28208c2ecf20Sopenharmony_ci
28218c2ecf20Sopenharmony_ci	while (!(d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))) {
28228c2ecf20Sopenharmony_ci		d++;
28238c2ecf20Sopenharmony_ci		buffer_dma = le32_to_cpu(d->data_address);
28248c2ecf20Sopenharmony_ci		dma_sync_single_range_for_cpu(context->ohci->card.device,
28258c2ecf20Sopenharmony_ci					      buffer_dma & PAGE_MASK,
28268c2ecf20Sopenharmony_ci					      buffer_dma & ~PAGE_MASK,
28278c2ecf20Sopenharmony_ci					      le16_to_cpu(d->req_count),
28288c2ecf20Sopenharmony_ci					      DMA_FROM_DEVICE);
28298c2ecf20Sopenharmony_ci	}
28308c2ecf20Sopenharmony_ci
28318c2ecf20Sopenharmony_ci	copy_iso_headers(ctx, (u32 *) (last + 1));
28328c2ecf20Sopenharmony_ci
28338c2ecf20Sopenharmony_ci	if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
28348c2ecf20Sopenharmony_ci		flush_iso_completions(ctx);
28358c2ecf20Sopenharmony_ci
28368c2ecf20Sopenharmony_ci	return 1;
28378c2ecf20Sopenharmony_ci}
28388c2ecf20Sopenharmony_ci
28398c2ecf20Sopenharmony_ci/* d == last because each descriptor block is only a single descriptor. */
28408c2ecf20Sopenharmony_cistatic int handle_ir_buffer_fill(struct context *context,
28418c2ecf20Sopenharmony_ci				 struct descriptor *d,
28428c2ecf20Sopenharmony_ci				 struct descriptor *last)
28438c2ecf20Sopenharmony_ci{
28448c2ecf20Sopenharmony_ci	struct iso_context *ctx =
28458c2ecf20Sopenharmony_ci		container_of(context, struct iso_context, context);
28468c2ecf20Sopenharmony_ci	unsigned int req_count, res_count, completed;
28478c2ecf20Sopenharmony_ci	u32 buffer_dma;
28488c2ecf20Sopenharmony_ci
28498c2ecf20Sopenharmony_ci	req_count = le16_to_cpu(last->req_count);
28508c2ecf20Sopenharmony_ci	res_count = le16_to_cpu(READ_ONCE(last->res_count));
28518c2ecf20Sopenharmony_ci	completed = req_count - res_count;
28528c2ecf20Sopenharmony_ci	buffer_dma = le32_to_cpu(last->data_address);
28538c2ecf20Sopenharmony_ci
28548c2ecf20Sopenharmony_ci	if (completed > 0) {
28558c2ecf20Sopenharmony_ci		ctx->mc_buffer_bus = buffer_dma;
28568c2ecf20Sopenharmony_ci		ctx->mc_completed = completed;
28578c2ecf20Sopenharmony_ci	}
28588c2ecf20Sopenharmony_ci
28598c2ecf20Sopenharmony_ci	if (res_count != 0)
28608c2ecf20Sopenharmony_ci		/* Descriptor(s) not done yet, stop iteration */
28618c2ecf20Sopenharmony_ci		return 0;
28628c2ecf20Sopenharmony_ci
28638c2ecf20Sopenharmony_ci	dma_sync_single_range_for_cpu(context->ohci->card.device,
28648c2ecf20Sopenharmony_ci				      buffer_dma & PAGE_MASK,
28658c2ecf20Sopenharmony_ci				      buffer_dma & ~PAGE_MASK,
28668c2ecf20Sopenharmony_ci				      completed, DMA_FROM_DEVICE);
28678c2ecf20Sopenharmony_ci
28688c2ecf20Sopenharmony_ci	if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) {
28698c2ecf20Sopenharmony_ci		ctx->base.callback.mc(&ctx->base,
28708c2ecf20Sopenharmony_ci				      buffer_dma + completed,
28718c2ecf20Sopenharmony_ci				      ctx->base.callback_data);
28728c2ecf20Sopenharmony_ci		ctx->mc_completed = 0;
28738c2ecf20Sopenharmony_ci	}
28748c2ecf20Sopenharmony_ci
28758c2ecf20Sopenharmony_ci	return 1;
28768c2ecf20Sopenharmony_ci}
28778c2ecf20Sopenharmony_ci
28788c2ecf20Sopenharmony_cistatic void flush_ir_buffer_fill(struct iso_context *ctx)
28798c2ecf20Sopenharmony_ci{
28808c2ecf20Sopenharmony_ci	dma_sync_single_range_for_cpu(ctx->context.ohci->card.device,
28818c2ecf20Sopenharmony_ci				      ctx->mc_buffer_bus & PAGE_MASK,
28828c2ecf20Sopenharmony_ci				      ctx->mc_buffer_bus & ~PAGE_MASK,
28838c2ecf20Sopenharmony_ci				      ctx->mc_completed, DMA_FROM_DEVICE);
28848c2ecf20Sopenharmony_ci
28858c2ecf20Sopenharmony_ci	ctx->base.callback.mc(&ctx->base,
28868c2ecf20Sopenharmony_ci			      ctx->mc_buffer_bus + ctx->mc_completed,
28878c2ecf20Sopenharmony_ci			      ctx->base.callback_data);
28888c2ecf20Sopenharmony_ci	ctx->mc_completed = 0;
28898c2ecf20Sopenharmony_ci}
28908c2ecf20Sopenharmony_ci
28918c2ecf20Sopenharmony_cistatic inline void sync_it_packet_for_cpu(struct context *context,
28928c2ecf20Sopenharmony_ci					  struct descriptor *pd)
28938c2ecf20Sopenharmony_ci{
28948c2ecf20Sopenharmony_ci	__le16 control;
28958c2ecf20Sopenharmony_ci	u32 buffer_dma;
28968c2ecf20Sopenharmony_ci
28978c2ecf20Sopenharmony_ci	/* only packets beginning with OUTPUT_MORE* have data buffers */
28988c2ecf20Sopenharmony_ci	if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
28998c2ecf20Sopenharmony_ci		return;
29008c2ecf20Sopenharmony_ci
29018c2ecf20Sopenharmony_ci	/* skip over the OUTPUT_MORE_IMMEDIATE descriptor */
29028c2ecf20Sopenharmony_ci	pd += 2;
29038c2ecf20Sopenharmony_ci
29048c2ecf20Sopenharmony_ci	/*
29058c2ecf20Sopenharmony_ci	 * If the packet has a header, the first OUTPUT_MORE/LAST descriptor's
29068c2ecf20Sopenharmony_ci	 * data buffer is in the context program's coherent page and must not
29078c2ecf20Sopenharmony_ci	 * be synced.
29088c2ecf20Sopenharmony_ci	 */
29098c2ecf20Sopenharmony_ci	if ((le32_to_cpu(pd->data_address) & PAGE_MASK) ==
29108c2ecf20Sopenharmony_ci	    (context->current_bus          & PAGE_MASK)) {
29118c2ecf20Sopenharmony_ci		if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
29128c2ecf20Sopenharmony_ci			return;
29138c2ecf20Sopenharmony_ci		pd++;
29148c2ecf20Sopenharmony_ci	}
29158c2ecf20Sopenharmony_ci
29168c2ecf20Sopenharmony_ci	do {
29178c2ecf20Sopenharmony_ci		buffer_dma = le32_to_cpu(pd->data_address);
29188c2ecf20Sopenharmony_ci		dma_sync_single_range_for_cpu(context->ohci->card.device,
29198c2ecf20Sopenharmony_ci					      buffer_dma & PAGE_MASK,
29208c2ecf20Sopenharmony_ci					      buffer_dma & ~PAGE_MASK,
29218c2ecf20Sopenharmony_ci					      le16_to_cpu(pd->req_count),
29228c2ecf20Sopenharmony_ci					      DMA_TO_DEVICE);
29238c2ecf20Sopenharmony_ci		control = pd->control;
29248c2ecf20Sopenharmony_ci		pd++;
29258c2ecf20Sopenharmony_ci	} while (!(control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)));
29268c2ecf20Sopenharmony_ci}
29278c2ecf20Sopenharmony_ci
29288c2ecf20Sopenharmony_cistatic int handle_it_packet(struct context *context,
29298c2ecf20Sopenharmony_ci			    struct descriptor *d,
29308c2ecf20Sopenharmony_ci			    struct descriptor *last)
29318c2ecf20Sopenharmony_ci{
29328c2ecf20Sopenharmony_ci	struct iso_context *ctx =
29338c2ecf20Sopenharmony_ci		container_of(context, struct iso_context, context);
29348c2ecf20Sopenharmony_ci	struct descriptor *pd;
29358c2ecf20Sopenharmony_ci	__be32 *ctx_hdr;
29368c2ecf20Sopenharmony_ci
29378c2ecf20Sopenharmony_ci	for (pd = d; pd <= last; pd++)
29388c2ecf20Sopenharmony_ci		if (pd->transfer_status)
29398c2ecf20Sopenharmony_ci			break;
29408c2ecf20Sopenharmony_ci	if (pd > last)
29418c2ecf20Sopenharmony_ci		/* Descriptor(s) not done yet, stop iteration */
29428c2ecf20Sopenharmony_ci		return 0;
29438c2ecf20Sopenharmony_ci
29448c2ecf20Sopenharmony_ci	sync_it_packet_for_cpu(context, d);
29458c2ecf20Sopenharmony_ci
29468c2ecf20Sopenharmony_ci	if (ctx->header_length + 4 > PAGE_SIZE) {
29478c2ecf20Sopenharmony_ci		if (ctx->base.drop_overflow_headers)
29488c2ecf20Sopenharmony_ci			return 1;
29498c2ecf20Sopenharmony_ci		flush_iso_completions(ctx);
29508c2ecf20Sopenharmony_ci	}
29518c2ecf20Sopenharmony_ci
29528c2ecf20Sopenharmony_ci	ctx_hdr = ctx->header + ctx->header_length;
29538c2ecf20Sopenharmony_ci	ctx->last_timestamp = le16_to_cpu(last->res_count);
29548c2ecf20Sopenharmony_ci	/* Present this value as big-endian to match the receive code */
29558c2ecf20Sopenharmony_ci	*ctx_hdr = cpu_to_be32((le16_to_cpu(pd->transfer_status) << 16) |
29568c2ecf20Sopenharmony_ci			       le16_to_cpu(pd->res_count));
29578c2ecf20Sopenharmony_ci	ctx->header_length += 4;
29588c2ecf20Sopenharmony_ci
29598c2ecf20Sopenharmony_ci	if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
29608c2ecf20Sopenharmony_ci		flush_iso_completions(ctx);
29618c2ecf20Sopenharmony_ci
29628c2ecf20Sopenharmony_ci	return 1;
29638c2ecf20Sopenharmony_ci}
29648c2ecf20Sopenharmony_ci
29658c2ecf20Sopenharmony_cistatic void set_multichannel_mask(struct fw_ohci *ohci, u64 channels)
29668c2ecf20Sopenharmony_ci{
29678c2ecf20Sopenharmony_ci	u32 hi = channels >> 32, lo = channels;
29688c2ecf20Sopenharmony_ci
29698c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IRMultiChanMaskHiClear, ~hi);
29708c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IRMultiChanMaskLoClear, ~lo);
29718c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IRMultiChanMaskHiSet, hi);
29728c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IRMultiChanMaskLoSet, lo);
29738c2ecf20Sopenharmony_ci	ohci->mc_channels = channels;
29748c2ecf20Sopenharmony_ci}
29758c2ecf20Sopenharmony_ci
29768c2ecf20Sopenharmony_cistatic struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
29778c2ecf20Sopenharmony_ci				int type, int channel, size_t header_size)
29788c2ecf20Sopenharmony_ci{
29798c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(card);
29808c2ecf20Sopenharmony_ci	struct iso_context *ctx;
29818c2ecf20Sopenharmony_ci	descriptor_callback_t callback;
29828c2ecf20Sopenharmony_ci	u64 *channels;
29838c2ecf20Sopenharmony_ci	u32 *mask, regs;
29848c2ecf20Sopenharmony_ci	int index, ret = -EBUSY;
29858c2ecf20Sopenharmony_ci
29868c2ecf20Sopenharmony_ci	spin_lock_irq(&ohci->lock);
29878c2ecf20Sopenharmony_ci
29888c2ecf20Sopenharmony_ci	switch (type) {
29898c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_TRANSMIT:
29908c2ecf20Sopenharmony_ci		mask     = &ohci->it_context_mask;
29918c2ecf20Sopenharmony_ci		callback = handle_it_packet;
29928c2ecf20Sopenharmony_ci		index    = ffs(*mask) - 1;
29938c2ecf20Sopenharmony_ci		if (index >= 0) {
29948c2ecf20Sopenharmony_ci			*mask &= ~(1 << index);
29958c2ecf20Sopenharmony_ci			regs = OHCI1394_IsoXmitContextBase(index);
29968c2ecf20Sopenharmony_ci			ctx  = &ohci->it_context_list[index];
29978c2ecf20Sopenharmony_ci		}
29988c2ecf20Sopenharmony_ci		break;
29998c2ecf20Sopenharmony_ci
30008c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE:
30018c2ecf20Sopenharmony_ci		channels = &ohci->ir_context_channels;
30028c2ecf20Sopenharmony_ci		mask     = &ohci->ir_context_mask;
30038c2ecf20Sopenharmony_ci		callback = handle_ir_packet_per_buffer;
30048c2ecf20Sopenharmony_ci		index    = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1;
30058c2ecf20Sopenharmony_ci		if (index >= 0) {
30068c2ecf20Sopenharmony_ci			*channels &= ~(1ULL << channel);
30078c2ecf20Sopenharmony_ci			*mask     &= ~(1 << index);
30088c2ecf20Sopenharmony_ci			regs = OHCI1394_IsoRcvContextBase(index);
30098c2ecf20Sopenharmony_ci			ctx  = &ohci->ir_context_list[index];
30108c2ecf20Sopenharmony_ci		}
30118c2ecf20Sopenharmony_ci		break;
30128c2ecf20Sopenharmony_ci
30138c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
30148c2ecf20Sopenharmony_ci		mask     = &ohci->ir_context_mask;
30158c2ecf20Sopenharmony_ci		callback = handle_ir_buffer_fill;
30168c2ecf20Sopenharmony_ci		index    = !ohci->mc_allocated ? ffs(*mask) - 1 : -1;
30178c2ecf20Sopenharmony_ci		if (index >= 0) {
30188c2ecf20Sopenharmony_ci			ohci->mc_allocated = true;
30198c2ecf20Sopenharmony_ci			*mask &= ~(1 << index);
30208c2ecf20Sopenharmony_ci			regs = OHCI1394_IsoRcvContextBase(index);
30218c2ecf20Sopenharmony_ci			ctx  = &ohci->ir_context_list[index];
30228c2ecf20Sopenharmony_ci		}
30238c2ecf20Sopenharmony_ci		break;
30248c2ecf20Sopenharmony_ci
30258c2ecf20Sopenharmony_ci	default:
30268c2ecf20Sopenharmony_ci		index = -1;
30278c2ecf20Sopenharmony_ci		ret = -ENOSYS;
30288c2ecf20Sopenharmony_ci	}
30298c2ecf20Sopenharmony_ci
30308c2ecf20Sopenharmony_ci	spin_unlock_irq(&ohci->lock);
30318c2ecf20Sopenharmony_ci
30328c2ecf20Sopenharmony_ci	if (index < 0)
30338c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
30348c2ecf20Sopenharmony_ci
30358c2ecf20Sopenharmony_ci	memset(ctx, 0, sizeof(*ctx));
30368c2ecf20Sopenharmony_ci	ctx->header_length = 0;
30378c2ecf20Sopenharmony_ci	ctx->header = (void *) __get_free_page(GFP_KERNEL);
30388c2ecf20Sopenharmony_ci	if (ctx->header == NULL) {
30398c2ecf20Sopenharmony_ci		ret = -ENOMEM;
30408c2ecf20Sopenharmony_ci		goto out;
30418c2ecf20Sopenharmony_ci	}
30428c2ecf20Sopenharmony_ci	ret = context_init(&ctx->context, ohci, regs, callback);
30438c2ecf20Sopenharmony_ci	if (ret < 0)
30448c2ecf20Sopenharmony_ci		goto out_with_header;
30458c2ecf20Sopenharmony_ci
30468c2ecf20Sopenharmony_ci	if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL) {
30478c2ecf20Sopenharmony_ci		set_multichannel_mask(ohci, 0);
30488c2ecf20Sopenharmony_ci		ctx->mc_completed = 0;
30498c2ecf20Sopenharmony_ci	}
30508c2ecf20Sopenharmony_ci
30518c2ecf20Sopenharmony_ci	return &ctx->base;
30528c2ecf20Sopenharmony_ci
30538c2ecf20Sopenharmony_ci out_with_header:
30548c2ecf20Sopenharmony_ci	free_page((unsigned long)ctx->header);
30558c2ecf20Sopenharmony_ci out:
30568c2ecf20Sopenharmony_ci	spin_lock_irq(&ohci->lock);
30578c2ecf20Sopenharmony_ci
30588c2ecf20Sopenharmony_ci	switch (type) {
30598c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE:
30608c2ecf20Sopenharmony_ci		*channels |= 1ULL << channel;
30618c2ecf20Sopenharmony_ci		break;
30628c2ecf20Sopenharmony_ci
30638c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
30648c2ecf20Sopenharmony_ci		ohci->mc_allocated = false;
30658c2ecf20Sopenharmony_ci		break;
30668c2ecf20Sopenharmony_ci	}
30678c2ecf20Sopenharmony_ci	*mask |= 1 << index;
30688c2ecf20Sopenharmony_ci
30698c2ecf20Sopenharmony_ci	spin_unlock_irq(&ohci->lock);
30708c2ecf20Sopenharmony_ci
30718c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
30728c2ecf20Sopenharmony_ci}
30738c2ecf20Sopenharmony_ci
30748c2ecf20Sopenharmony_cistatic int ohci_start_iso(struct fw_iso_context *base,
30758c2ecf20Sopenharmony_ci			  s32 cycle, u32 sync, u32 tags)
30768c2ecf20Sopenharmony_ci{
30778c2ecf20Sopenharmony_ci	struct iso_context *ctx = container_of(base, struct iso_context, base);
30788c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = ctx->context.ohci;
30798c2ecf20Sopenharmony_ci	u32 control = IR_CONTEXT_ISOCH_HEADER, match;
30808c2ecf20Sopenharmony_ci	int index;
30818c2ecf20Sopenharmony_ci
30828c2ecf20Sopenharmony_ci	/* the controller cannot start without any queued packets */
30838c2ecf20Sopenharmony_ci	if (ctx->context.last->branch_address == 0)
30848c2ecf20Sopenharmony_ci		return -ENODATA;
30858c2ecf20Sopenharmony_ci
30868c2ecf20Sopenharmony_ci	switch (ctx->base.type) {
30878c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_TRANSMIT:
30888c2ecf20Sopenharmony_ci		index = ctx - ohci->it_context_list;
30898c2ecf20Sopenharmony_ci		match = 0;
30908c2ecf20Sopenharmony_ci		if (cycle >= 0)
30918c2ecf20Sopenharmony_ci			match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
30928c2ecf20Sopenharmony_ci				(cycle & 0x7fff) << 16;
30938c2ecf20Sopenharmony_ci
30948c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
30958c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
30968c2ecf20Sopenharmony_ci		context_run(&ctx->context, match);
30978c2ecf20Sopenharmony_ci		break;
30988c2ecf20Sopenharmony_ci
30998c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
31008c2ecf20Sopenharmony_ci		control |= IR_CONTEXT_BUFFER_FILL|IR_CONTEXT_MULTI_CHANNEL_MODE;
31018c2ecf20Sopenharmony_ci		fallthrough;
31028c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE:
31038c2ecf20Sopenharmony_ci		index = ctx - ohci->ir_context_list;
31048c2ecf20Sopenharmony_ci		match = (tags << 28) | (sync << 8) | ctx->base.channel;
31058c2ecf20Sopenharmony_ci		if (cycle >= 0) {
31068c2ecf20Sopenharmony_ci			match |= (cycle & 0x07fff) << 12;
31078c2ecf20Sopenharmony_ci			control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
31088c2ecf20Sopenharmony_ci		}
31098c2ecf20Sopenharmony_ci
31108c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
31118c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
31128c2ecf20Sopenharmony_ci		reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
31138c2ecf20Sopenharmony_ci		context_run(&ctx->context, control);
31148c2ecf20Sopenharmony_ci
31158c2ecf20Sopenharmony_ci		ctx->sync = sync;
31168c2ecf20Sopenharmony_ci		ctx->tags = tags;
31178c2ecf20Sopenharmony_ci
31188c2ecf20Sopenharmony_ci		break;
31198c2ecf20Sopenharmony_ci	}
31208c2ecf20Sopenharmony_ci
31218c2ecf20Sopenharmony_ci	return 0;
31228c2ecf20Sopenharmony_ci}
31238c2ecf20Sopenharmony_ci
31248c2ecf20Sopenharmony_cistatic int ohci_stop_iso(struct fw_iso_context *base)
31258c2ecf20Sopenharmony_ci{
31268c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(base->card);
31278c2ecf20Sopenharmony_ci	struct iso_context *ctx = container_of(base, struct iso_context, base);
31288c2ecf20Sopenharmony_ci	int index;
31298c2ecf20Sopenharmony_ci
31308c2ecf20Sopenharmony_ci	switch (ctx->base.type) {
31318c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_TRANSMIT:
31328c2ecf20Sopenharmony_ci		index = ctx - ohci->it_context_list;
31338c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
31348c2ecf20Sopenharmony_ci		break;
31358c2ecf20Sopenharmony_ci
31368c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE:
31378c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
31388c2ecf20Sopenharmony_ci		index = ctx - ohci->ir_context_list;
31398c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
31408c2ecf20Sopenharmony_ci		break;
31418c2ecf20Sopenharmony_ci	}
31428c2ecf20Sopenharmony_ci	flush_writes(ohci);
31438c2ecf20Sopenharmony_ci	context_stop(&ctx->context);
31448c2ecf20Sopenharmony_ci	tasklet_kill(&ctx->context.tasklet);
31458c2ecf20Sopenharmony_ci
31468c2ecf20Sopenharmony_ci	return 0;
31478c2ecf20Sopenharmony_ci}
31488c2ecf20Sopenharmony_ci
31498c2ecf20Sopenharmony_cistatic void ohci_free_iso_context(struct fw_iso_context *base)
31508c2ecf20Sopenharmony_ci{
31518c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(base->card);
31528c2ecf20Sopenharmony_ci	struct iso_context *ctx = container_of(base, struct iso_context, base);
31538c2ecf20Sopenharmony_ci	unsigned long flags;
31548c2ecf20Sopenharmony_ci	int index;
31558c2ecf20Sopenharmony_ci
31568c2ecf20Sopenharmony_ci	ohci_stop_iso(base);
31578c2ecf20Sopenharmony_ci	context_release(&ctx->context);
31588c2ecf20Sopenharmony_ci	free_page((unsigned long)ctx->header);
31598c2ecf20Sopenharmony_ci
31608c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ohci->lock, flags);
31618c2ecf20Sopenharmony_ci
31628c2ecf20Sopenharmony_ci	switch (base->type) {
31638c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_TRANSMIT:
31648c2ecf20Sopenharmony_ci		index = ctx - ohci->it_context_list;
31658c2ecf20Sopenharmony_ci		ohci->it_context_mask |= 1 << index;
31668c2ecf20Sopenharmony_ci		break;
31678c2ecf20Sopenharmony_ci
31688c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE:
31698c2ecf20Sopenharmony_ci		index = ctx - ohci->ir_context_list;
31708c2ecf20Sopenharmony_ci		ohci->ir_context_mask |= 1 << index;
31718c2ecf20Sopenharmony_ci		ohci->ir_context_channels |= 1ULL << base->channel;
31728c2ecf20Sopenharmony_ci		break;
31738c2ecf20Sopenharmony_ci
31748c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
31758c2ecf20Sopenharmony_ci		index = ctx - ohci->ir_context_list;
31768c2ecf20Sopenharmony_ci		ohci->ir_context_mask |= 1 << index;
31778c2ecf20Sopenharmony_ci		ohci->ir_context_channels |= ohci->mc_channels;
31788c2ecf20Sopenharmony_ci		ohci->mc_channels = 0;
31798c2ecf20Sopenharmony_ci		ohci->mc_allocated = false;
31808c2ecf20Sopenharmony_ci		break;
31818c2ecf20Sopenharmony_ci	}
31828c2ecf20Sopenharmony_ci
31838c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ohci->lock, flags);
31848c2ecf20Sopenharmony_ci}
31858c2ecf20Sopenharmony_ci
31868c2ecf20Sopenharmony_cistatic int ohci_set_iso_channels(struct fw_iso_context *base, u64 *channels)
31878c2ecf20Sopenharmony_ci{
31888c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = fw_ohci(base->card);
31898c2ecf20Sopenharmony_ci	unsigned long flags;
31908c2ecf20Sopenharmony_ci	int ret;
31918c2ecf20Sopenharmony_ci
31928c2ecf20Sopenharmony_ci	switch (base->type) {
31938c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
31948c2ecf20Sopenharmony_ci
31958c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ohci->lock, flags);
31968c2ecf20Sopenharmony_ci
31978c2ecf20Sopenharmony_ci		/* Don't allow multichannel to grab other contexts' channels. */
31988c2ecf20Sopenharmony_ci		if (~ohci->ir_context_channels & ~ohci->mc_channels & *channels) {
31998c2ecf20Sopenharmony_ci			*channels = ohci->ir_context_channels;
32008c2ecf20Sopenharmony_ci			ret = -EBUSY;
32018c2ecf20Sopenharmony_ci		} else {
32028c2ecf20Sopenharmony_ci			set_multichannel_mask(ohci, *channels);
32038c2ecf20Sopenharmony_ci			ret = 0;
32048c2ecf20Sopenharmony_ci		}
32058c2ecf20Sopenharmony_ci
32068c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ohci->lock, flags);
32078c2ecf20Sopenharmony_ci
32088c2ecf20Sopenharmony_ci		break;
32098c2ecf20Sopenharmony_ci	default:
32108c2ecf20Sopenharmony_ci		ret = -EINVAL;
32118c2ecf20Sopenharmony_ci	}
32128c2ecf20Sopenharmony_ci
32138c2ecf20Sopenharmony_ci	return ret;
32148c2ecf20Sopenharmony_ci}
32158c2ecf20Sopenharmony_ci
32168c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
32178c2ecf20Sopenharmony_cistatic void ohci_resume_iso_dma(struct fw_ohci *ohci)
32188c2ecf20Sopenharmony_ci{
32198c2ecf20Sopenharmony_ci	int i;
32208c2ecf20Sopenharmony_ci	struct iso_context *ctx;
32218c2ecf20Sopenharmony_ci
32228c2ecf20Sopenharmony_ci	for (i = 0 ; i < ohci->n_ir ; i++) {
32238c2ecf20Sopenharmony_ci		ctx = &ohci->ir_context_list[i];
32248c2ecf20Sopenharmony_ci		if (ctx->context.running)
32258c2ecf20Sopenharmony_ci			ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags);
32268c2ecf20Sopenharmony_ci	}
32278c2ecf20Sopenharmony_ci
32288c2ecf20Sopenharmony_ci	for (i = 0 ; i < ohci->n_it ; i++) {
32298c2ecf20Sopenharmony_ci		ctx = &ohci->it_context_list[i];
32308c2ecf20Sopenharmony_ci		if (ctx->context.running)
32318c2ecf20Sopenharmony_ci			ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags);
32328c2ecf20Sopenharmony_ci	}
32338c2ecf20Sopenharmony_ci}
32348c2ecf20Sopenharmony_ci#endif
32358c2ecf20Sopenharmony_ci
32368c2ecf20Sopenharmony_cistatic int queue_iso_transmit(struct iso_context *ctx,
32378c2ecf20Sopenharmony_ci			      struct fw_iso_packet *packet,
32388c2ecf20Sopenharmony_ci			      struct fw_iso_buffer *buffer,
32398c2ecf20Sopenharmony_ci			      unsigned long payload)
32408c2ecf20Sopenharmony_ci{
32418c2ecf20Sopenharmony_ci	struct descriptor *d, *last, *pd;
32428c2ecf20Sopenharmony_ci	struct fw_iso_packet *p;
32438c2ecf20Sopenharmony_ci	__le32 *header;
32448c2ecf20Sopenharmony_ci	dma_addr_t d_bus, page_bus;
32458c2ecf20Sopenharmony_ci	u32 z, header_z, payload_z, irq;
32468c2ecf20Sopenharmony_ci	u32 payload_index, payload_end_index, next_page_index;
32478c2ecf20Sopenharmony_ci	int page, end_page, i, length, offset;
32488c2ecf20Sopenharmony_ci
32498c2ecf20Sopenharmony_ci	p = packet;
32508c2ecf20Sopenharmony_ci	payload_index = payload;
32518c2ecf20Sopenharmony_ci
32528c2ecf20Sopenharmony_ci	if (p->skip)
32538c2ecf20Sopenharmony_ci		z = 1;
32548c2ecf20Sopenharmony_ci	else
32558c2ecf20Sopenharmony_ci		z = 2;
32568c2ecf20Sopenharmony_ci	if (p->header_length > 0)
32578c2ecf20Sopenharmony_ci		z++;
32588c2ecf20Sopenharmony_ci
32598c2ecf20Sopenharmony_ci	/* Determine the first page the payload isn't contained in. */
32608c2ecf20Sopenharmony_ci	end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
32618c2ecf20Sopenharmony_ci	if (p->payload_length > 0)
32628c2ecf20Sopenharmony_ci		payload_z = end_page - (payload_index >> PAGE_SHIFT);
32638c2ecf20Sopenharmony_ci	else
32648c2ecf20Sopenharmony_ci		payload_z = 0;
32658c2ecf20Sopenharmony_ci
32668c2ecf20Sopenharmony_ci	z += payload_z;
32678c2ecf20Sopenharmony_ci
32688c2ecf20Sopenharmony_ci	/* Get header size in number of descriptors. */
32698c2ecf20Sopenharmony_ci	header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
32708c2ecf20Sopenharmony_ci
32718c2ecf20Sopenharmony_ci	d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
32728c2ecf20Sopenharmony_ci	if (d == NULL)
32738c2ecf20Sopenharmony_ci		return -ENOMEM;
32748c2ecf20Sopenharmony_ci
32758c2ecf20Sopenharmony_ci	if (!p->skip) {
32768c2ecf20Sopenharmony_ci		d[0].control   = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
32778c2ecf20Sopenharmony_ci		d[0].req_count = cpu_to_le16(8);
32788c2ecf20Sopenharmony_ci		/*
32798c2ecf20Sopenharmony_ci		 * Link the skip address to this descriptor itself.  This causes
32808c2ecf20Sopenharmony_ci		 * a context to skip a cycle whenever lost cycles or FIFO
32818c2ecf20Sopenharmony_ci		 * overruns occur, without dropping the data.  The application
32828c2ecf20Sopenharmony_ci		 * should then decide whether this is an error condition or not.
32838c2ecf20Sopenharmony_ci		 * FIXME:  Make the context's cycle-lost behaviour configurable?
32848c2ecf20Sopenharmony_ci		 */
32858c2ecf20Sopenharmony_ci		d[0].branch_address = cpu_to_le32(d_bus | z);
32868c2ecf20Sopenharmony_ci
32878c2ecf20Sopenharmony_ci		header = (__le32 *) &d[1];
32888c2ecf20Sopenharmony_ci		header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
32898c2ecf20Sopenharmony_ci					IT_HEADER_TAG(p->tag) |
32908c2ecf20Sopenharmony_ci					IT_HEADER_TCODE(TCODE_STREAM_DATA) |
32918c2ecf20Sopenharmony_ci					IT_HEADER_CHANNEL(ctx->base.channel) |
32928c2ecf20Sopenharmony_ci					IT_HEADER_SPEED(ctx->base.speed));
32938c2ecf20Sopenharmony_ci		header[1] =
32948c2ecf20Sopenharmony_ci			cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
32958c2ecf20Sopenharmony_ci							  p->payload_length));
32968c2ecf20Sopenharmony_ci	}
32978c2ecf20Sopenharmony_ci
32988c2ecf20Sopenharmony_ci	if (p->header_length > 0) {
32998c2ecf20Sopenharmony_ci		d[2].req_count    = cpu_to_le16(p->header_length);
33008c2ecf20Sopenharmony_ci		d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
33018c2ecf20Sopenharmony_ci		memcpy(&d[z], p->header, p->header_length);
33028c2ecf20Sopenharmony_ci	}
33038c2ecf20Sopenharmony_ci
33048c2ecf20Sopenharmony_ci	pd = d + z - payload_z;
33058c2ecf20Sopenharmony_ci	payload_end_index = payload_index + p->payload_length;
33068c2ecf20Sopenharmony_ci	for (i = 0; i < payload_z; i++) {
33078c2ecf20Sopenharmony_ci		page               = payload_index >> PAGE_SHIFT;
33088c2ecf20Sopenharmony_ci		offset             = payload_index & ~PAGE_MASK;
33098c2ecf20Sopenharmony_ci		next_page_index    = (page + 1) << PAGE_SHIFT;
33108c2ecf20Sopenharmony_ci		length             =
33118c2ecf20Sopenharmony_ci			min(next_page_index, payload_end_index) - payload_index;
33128c2ecf20Sopenharmony_ci		pd[i].req_count    = cpu_to_le16(length);
33138c2ecf20Sopenharmony_ci
33148c2ecf20Sopenharmony_ci		page_bus = page_private(buffer->pages[page]);
33158c2ecf20Sopenharmony_ci		pd[i].data_address = cpu_to_le32(page_bus + offset);
33168c2ecf20Sopenharmony_ci
33178c2ecf20Sopenharmony_ci		dma_sync_single_range_for_device(ctx->context.ohci->card.device,
33188c2ecf20Sopenharmony_ci						 page_bus, offset, length,
33198c2ecf20Sopenharmony_ci						 DMA_TO_DEVICE);
33208c2ecf20Sopenharmony_ci
33218c2ecf20Sopenharmony_ci		payload_index += length;
33228c2ecf20Sopenharmony_ci	}
33238c2ecf20Sopenharmony_ci
33248c2ecf20Sopenharmony_ci	if (p->interrupt)
33258c2ecf20Sopenharmony_ci		irq = DESCRIPTOR_IRQ_ALWAYS;
33268c2ecf20Sopenharmony_ci	else
33278c2ecf20Sopenharmony_ci		irq = DESCRIPTOR_NO_IRQ;
33288c2ecf20Sopenharmony_ci
33298c2ecf20Sopenharmony_ci	last = z == 2 ? d : d + z - 1;
33308c2ecf20Sopenharmony_ci	last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
33318c2ecf20Sopenharmony_ci				     DESCRIPTOR_STATUS |
33328c2ecf20Sopenharmony_ci				     DESCRIPTOR_BRANCH_ALWAYS |
33338c2ecf20Sopenharmony_ci				     irq);
33348c2ecf20Sopenharmony_ci
33358c2ecf20Sopenharmony_ci	context_append(&ctx->context, d, z, header_z);
33368c2ecf20Sopenharmony_ci
33378c2ecf20Sopenharmony_ci	return 0;
33388c2ecf20Sopenharmony_ci}
33398c2ecf20Sopenharmony_ci
33408c2ecf20Sopenharmony_cistatic int queue_iso_packet_per_buffer(struct iso_context *ctx,
33418c2ecf20Sopenharmony_ci				       struct fw_iso_packet *packet,
33428c2ecf20Sopenharmony_ci				       struct fw_iso_buffer *buffer,
33438c2ecf20Sopenharmony_ci				       unsigned long payload)
33448c2ecf20Sopenharmony_ci{
33458c2ecf20Sopenharmony_ci	struct device *device = ctx->context.ohci->card.device;
33468c2ecf20Sopenharmony_ci	struct descriptor *d, *pd;
33478c2ecf20Sopenharmony_ci	dma_addr_t d_bus, page_bus;
33488c2ecf20Sopenharmony_ci	u32 z, header_z, rest;
33498c2ecf20Sopenharmony_ci	int i, j, length;
33508c2ecf20Sopenharmony_ci	int page, offset, packet_count, header_size, payload_per_buffer;
33518c2ecf20Sopenharmony_ci
33528c2ecf20Sopenharmony_ci	/*
33538c2ecf20Sopenharmony_ci	 * The OHCI controller puts the isochronous header and trailer in the
33548c2ecf20Sopenharmony_ci	 * buffer, so we need at least 8 bytes.
33558c2ecf20Sopenharmony_ci	 */
33568c2ecf20Sopenharmony_ci	packet_count = packet->header_length / ctx->base.header_size;
33578c2ecf20Sopenharmony_ci	header_size  = max(ctx->base.header_size, (size_t)8);
33588c2ecf20Sopenharmony_ci
33598c2ecf20Sopenharmony_ci	/* Get header size in number of descriptors. */
33608c2ecf20Sopenharmony_ci	header_z = DIV_ROUND_UP(header_size, sizeof(*d));
33618c2ecf20Sopenharmony_ci	page     = payload >> PAGE_SHIFT;
33628c2ecf20Sopenharmony_ci	offset   = payload & ~PAGE_MASK;
33638c2ecf20Sopenharmony_ci	payload_per_buffer = packet->payload_length / packet_count;
33648c2ecf20Sopenharmony_ci
33658c2ecf20Sopenharmony_ci	for (i = 0; i < packet_count; i++) {
33668c2ecf20Sopenharmony_ci		/* d points to the header descriptor */
33678c2ecf20Sopenharmony_ci		z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
33688c2ecf20Sopenharmony_ci		d = context_get_descriptors(&ctx->context,
33698c2ecf20Sopenharmony_ci				z + header_z, &d_bus);
33708c2ecf20Sopenharmony_ci		if (d == NULL)
33718c2ecf20Sopenharmony_ci			return -ENOMEM;
33728c2ecf20Sopenharmony_ci
33738c2ecf20Sopenharmony_ci		d->control      = cpu_to_le16(DESCRIPTOR_STATUS |
33748c2ecf20Sopenharmony_ci					      DESCRIPTOR_INPUT_MORE);
33758c2ecf20Sopenharmony_ci		if (packet->skip && i == 0)
33768c2ecf20Sopenharmony_ci			d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
33778c2ecf20Sopenharmony_ci		d->req_count    = cpu_to_le16(header_size);
33788c2ecf20Sopenharmony_ci		d->res_count    = d->req_count;
33798c2ecf20Sopenharmony_ci		d->transfer_status = 0;
33808c2ecf20Sopenharmony_ci		d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
33818c2ecf20Sopenharmony_ci
33828c2ecf20Sopenharmony_ci		rest = payload_per_buffer;
33838c2ecf20Sopenharmony_ci		pd = d;
33848c2ecf20Sopenharmony_ci		for (j = 1; j < z; j++) {
33858c2ecf20Sopenharmony_ci			pd++;
33868c2ecf20Sopenharmony_ci			pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
33878c2ecf20Sopenharmony_ci						  DESCRIPTOR_INPUT_MORE);
33888c2ecf20Sopenharmony_ci
33898c2ecf20Sopenharmony_ci			if (offset + rest < PAGE_SIZE)
33908c2ecf20Sopenharmony_ci				length = rest;
33918c2ecf20Sopenharmony_ci			else
33928c2ecf20Sopenharmony_ci				length = PAGE_SIZE - offset;
33938c2ecf20Sopenharmony_ci			pd->req_count = cpu_to_le16(length);
33948c2ecf20Sopenharmony_ci			pd->res_count = pd->req_count;
33958c2ecf20Sopenharmony_ci			pd->transfer_status = 0;
33968c2ecf20Sopenharmony_ci
33978c2ecf20Sopenharmony_ci			page_bus = page_private(buffer->pages[page]);
33988c2ecf20Sopenharmony_ci			pd->data_address = cpu_to_le32(page_bus + offset);
33998c2ecf20Sopenharmony_ci
34008c2ecf20Sopenharmony_ci			dma_sync_single_range_for_device(device, page_bus,
34018c2ecf20Sopenharmony_ci							 offset, length,
34028c2ecf20Sopenharmony_ci							 DMA_FROM_DEVICE);
34038c2ecf20Sopenharmony_ci
34048c2ecf20Sopenharmony_ci			offset = (offset + length) & ~PAGE_MASK;
34058c2ecf20Sopenharmony_ci			rest -= length;
34068c2ecf20Sopenharmony_ci			if (offset == 0)
34078c2ecf20Sopenharmony_ci				page++;
34088c2ecf20Sopenharmony_ci		}
34098c2ecf20Sopenharmony_ci		pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
34108c2ecf20Sopenharmony_ci					  DESCRIPTOR_INPUT_LAST |
34118c2ecf20Sopenharmony_ci					  DESCRIPTOR_BRANCH_ALWAYS);
34128c2ecf20Sopenharmony_ci		if (packet->interrupt && i == packet_count - 1)
34138c2ecf20Sopenharmony_ci			pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
34148c2ecf20Sopenharmony_ci
34158c2ecf20Sopenharmony_ci		context_append(&ctx->context, d, z, header_z);
34168c2ecf20Sopenharmony_ci	}
34178c2ecf20Sopenharmony_ci
34188c2ecf20Sopenharmony_ci	return 0;
34198c2ecf20Sopenharmony_ci}
34208c2ecf20Sopenharmony_ci
34218c2ecf20Sopenharmony_cistatic int queue_iso_buffer_fill(struct iso_context *ctx,
34228c2ecf20Sopenharmony_ci				 struct fw_iso_packet *packet,
34238c2ecf20Sopenharmony_ci				 struct fw_iso_buffer *buffer,
34248c2ecf20Sopenharmony_ci				 unsigned long payload)
34258c2ecf20Sopenharmony_ci{
34268c2ecf20Sopenharmony_ci	struct descriptor *d;
34278c2ecf20Sopenharmony_ci	dma_addr_t d_bus, page_bus;
34288c2ecf20Sopenharmony_ci	int page, offset, rest, z, i, length;
34298c2ecf20Sopenharmony_ci
34308c2ecf20Sopenharmony_ci	page   = payload >> PAGE_SHIFT;
34318c2ecf20Sopenharmony_ci	offset = payload & ~PAGE_MASK;
34328c2ecf20Sopenharmony_ci	rest   = packet->payload_length;
34338c2ecf20Sopenharmony_ci
34348c2ecf20Sopenharmony_ci	/* We need one descriptor for each page in the buffer. */
34358c2ecf20Sopenharmony_ci	z = DIV_ROUND_UP(offset + rest, PAGE_SIZE);
34368c2ecf20Sopenharmony_ci
34378c2ecf20Sopenharmony_ci	if (WARN_ON(offset & 3 || rest & 3 || page + z > buffer->page_count))
34388c2ecf20Sopenharmony_ci		return -EFAULT;
34398c2ecf20Sopenharmony_ci
34408c2ecf20Sopenharmony_ci	for (i = 0; i < z; i++) {
34418c2ecf20Sopenharmony_ci		d = context_get_descriptors(&ctx->context, 1, &d_bus);
34428c2ecf20Sopenharmony_ci		if (d == NULL)
34438c2ecf20Sopenharmony_ci			return -ENOMEM;
34448c2ecf20Sopenharmony_ci
34458c2ecf20Sopenharmony_ci		d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
34468c2ecf20Sopenharmony_ci					 DESCRIPTOR_BRANCH_ALWAYS);
34478c2ecf20Sopenharmony_ci		if (packet->skip && i == 0)
34488c2ecf20Sopenharmony_ci			d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
34498c2ecf20Sopenharmony_ci		if (packet->interrupt && i == z - 1)
34508c2ecf20Sopenharmony_ci			d->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
34518c2ecf20Sopenharmony_ci
34528c2ecf20Sopenharmony_ci		if (offset + rest < PAGE_SIZE)
34538c2ecf20Sopenharmony_ci			length = rest;
34548c2ecf20Sopenharmony_ci		else
34558c2ecf20Sopenharmony_ci			length = PAGE_SIZE - offset;
34568c2ecf20Sopenharmony_ci		d->req_count = cpu_to_le16(length);
34578c2ecf20Sopenharmony_ci		d->res_count = d->req_count;
34588c2ecf20Sopenharmony_ci		d->transfer_status = 0;
34598c2ecf20Sopenharmony_ci
34608c2ecf20Sopenharmony_ci		page_bus = page_private(buffer->pages[page]);
34618c2ecf20Sopenharmony_ci		d->data_address = cpu_to_le32(page_bus + offset);
34628c2ecf20Sopenharmony_ci
34638c2ecf20Sopenharmony_ci		dma_sync_single_range_for_device(ctx->context.ohci->card.device,
34648c2ecf20Sopenharmony_ci						 page_bus, offset, length,
34658c2ecf20Sopenharmony_ci						 DMA_FROM_DEVICE);
34668c2ecf20Sopenharmony_ci
34678c2ecf20Sopenharmony_ci		rest -= length;
34688c2ecf20Sopenharmony_ci		offset = 0;
34698c2ecf20Sopenharmony_ci		page++;
34708c2ecf20Sopenharmony_ci
34718c2ecf20Sopenharmony_ci		context_append(&ctx->context, d, 1, 0);
34728c2ecf20Sopenharmony_ci	}
34738c2ecf20Sopenharmony_ci
34748c2ecf20Sopenharmony_ci	return 0;
34758c2ecf20Sopenharmony_ci}
34768c2ecf20Sopenharmony_ci
34778c2ecf20Sopenharmony_cistatic int ohci_queue_iso(struct fw_iso_context *base,
34788c2ecf20Sopenharmony_ci			  struct fw_iso_packet *packet,
34798c2ecf20Sopenharmony_ci			  struct fw_iso_buffer *buffer,
34808c2ecf20Sopenharmony_ci			  unsigned long payload)
34818c2ecf20Sopenharmony_ci{
34828c2ecf20Sopenharmony_ci	struct iso_context *ctx = container_of(base, struct iso_context, base);
34838c2ecf20Sopenharmony_ci	unsigned long flags;
34848c2ecf20Sopenharmony_ci	int ret = -ENOSYS;
34858c2ecf20Sopenharmony_ci
34868c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->context.ohci->lock, flags);
34878c2ecf20Sopenharmony_ci	switch (base->type) {
34888c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_TRANSMIT:
34898c2ecf20Sopenharmony_ci		ret = queue_iso_transmit(ctx, packet, buffer, payload);
34908c2ecf20Sopenharmony_ci		break;
34918c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE:
34928c2ecf20Sopenharmony_ci		ret = queue_iso_packet_per_buffer(ctx, packet, buffer, payload);
34938c2ecf20Sopenharmony_ci		break;
34948c2ecf20Sopenharmony_ci	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
34958c2ecf20Sopenharmony_ci		ret = queue_iso_buffer_fill(ctx, packet, buffer, payload);
34968c2ecf20Sopenharmony_ci		break;
34978c2ecf20Sopenharmony_ci	}
34988c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->context.ohci->lock, flags);
34998c2ecf20Sopenharmony_ci
35008c2ecf20Sopenharmony_ci	return ret;
35018c2ecf20Sopenharmony_ci}
35028c2ecf20Sopenharmony_ci
35038c2ecf20Sopenharmony_cistatic void ohci_flush_queue_iso(struct fw_iso_context *base)
35048c2ecf20Sopenharmony_ci{
35058c2ecf20Sopenharmony_ci	struct context *ctx =
35068c2ecf20Sopenharmony_ci			&container_of(base, struct iso_context, base)->context;
35078c2ecf20Sopenharmony_ci
35088c2ecf20Sopenharmony_ci	reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
35098c2ecf20Sopenharmony_ci}
35108c2ecf20Sopenharmony_ci
35118c2ecf20Sopenharmony_cistatic int ohci_flush_iso_completions(struct fw_iso_context *base)
35128c2ecf20Sopenharmony_ci{
35138c2ecf20Sopenharmony_ci	struct iso_context *ctx = container_of(base, struct iso_context, base);
35148c2ecf20Sopenharmony_ci	int ret = 0;
35158c2ecf20Sopenharmony_ci
35168c2ecf20Sopenharmony_ci	tasklet_disable(&ctx->context.tasklet);
35178c2ecf20Sopenharmony_ci
35188c2ecf20Sopenharmony_ci	if (!test_and_set_bit_lock(0, &ctx->flushing_completions)) {
35198c2ecf20Sopenharmony_ci		context_tasklet((unsigned long)&ctx->context);
35208c2ecf20Sopenharmony_ci
35218c2ecf20Sopenharmony_ci		switch (base->type) {
35228c2ecf20Sopenharmony_ci		case FW_ISO_CONTEXT_TRANSMIT:
35238c2ecf20Sopenharmony_ci		case FW_ISO_CONTEXT_RECEIVE:
35248c2ecf20Sopenharmony_ci			if (ctx->header_length != 0)
35258c2ecf20Sopenharmony_ci				flush_iso_completions(ctx);
35268c2ecf20Sopenharmony_ci			break;
35278c2ecf20Sopenharmony_ci		case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
35288c2ecf20Sopenharmony_ci			if (ctx->mc_completed != 0)
35298c2ecf20Sopenharmony_ci				flush_ir_buffer_fill(ctx);
35308c2ecf20Sopenharmony_ci			break;
35318c2ecf20Sopenharmony_ci		default:
35328c2ecf20Sopenharmony_ci			ret = -ENOSYS;
35338c2ecf20Sopenharmony_ci		}
35348c2ecf20Sopenharmony_ci
35358c2ecf20Sopenharmony_ci		clear_bit_unlock(0, &ctx->flushing_completions);
35368c2ecf20Sopenharmony_ci		smp_mb__after_atomic();
35378c2ecf20Sopenharmony_ci	}
35388c2ecf20Sopenharmony_ci
35398c2ecf20Sopenharmony_ci	tasklet_enable(&ctx->context.tasklet);
35408c2ecf20Sopenharmony_ci
35418c2ecf20Sopenharmony_ci	return ret;
35428c2ecf20Sopenharmony_ci}
35438c2ecf20Sopenharmony_ci
35448c2ecf20Sopenharmony_cistatic const struct fw_card_driver ohci_driver = {
35458c2ecf20Sopenharmony_ci	.enable			= ohci_enable,
35468c2ecf20Sopenharmony_ci	.read_phy_reg		= ohci_read_phy_reg,
35478c2ecf20Sopenharmony_ci	.update_phy_reg		= ohci_update_phy_reg,
35488c2ecf20Sopenharmony_ci	.set_config_rom		= ohci_set_config_rom,
35498c2ecf20Sopenharmony_ci	.send_request		= ohci_send_request,
35508c2ecf20Sopenharmony_ci	.send_response		= ohci_send_response,
35518c2ecf20Sopenharmony_ci	.cancel_packet		= ohci_cancel_packet,
35528c2ecf20Sopenharmony_ci	.enable_phys_dma	= ohci_enable_phys_dma,
35538c2ecf20Sopenharmony_ci	.read_csr		= ohci_read_csr,
35548c2ecf20Sopenharmony_ci	.write_csr		= ohci_write_csr,
35558c2ecf20Sopenharmony_ci
35568c2ecf20Sopenharmony_ci	.allocate_iso_context	= ohci_allocate_iso_context,
35578c2ecf20Sopenharmony_ci	.free_iso_context	= ohci_free_iso_context,
35588c2ecf20Sopenharmony_ci	.set_iso_channels	= ohci_set_iso_channels,
35598c2ecf20Sopenharmony_ci	.queue_iso		= ohci_queue_iso,
35608c2ecf20Sopenharmony_ci	.flush_queue_iso	= ohci_flush_queue_iso,
35618c2ecf20Sopenharmony_ci	.flush_iso_completions	= ohci_flush_iso_completions,
35628c2ecf20Sopenharmony_ci	.start_iso		= ohci_start_iso,
35638c2ecf20Sopenharmony_ci	.stop_iso		= ohci_stop_iso,
35648c2ecf20Sopenharmony_ci};
35658c2ecf20Sopenharmony_ci
35668c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_PMAC
35678c2ecf20Sopenharmony_cistatic void pmac_ohci_on(struct pci_dev *dev)
35688c2ecf20Sopenharmony_ci{
35698c2ecf20Sopenharmony_ci	if (machine_is(powermac)) {
35708c2ecf20Sopenharmony_ci		struct device_node *ofn = pci_device_to_OF_node(dev);
35718c2ecf20Sopenharmony_ci
35728c2ecf20Sopenharmony_ci		if (ofn) {
35738c2ecf20Sopenharmony_ci			pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1);
35748c2ecf20Sopenharmony_ci			pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
35758c2ecf20Sopenharmony_ci		}
35768c2ecf20Sopenharmony_ci	}
35778c2ecf20Sopenharmony_ci}
35788c2ecf20Sopenharmony_ci
35798c2ecf20Sopenharmony_cistatic void pmac_ohci_off(struct pci_dev *dev)
35808c2ecf20Sopenharmony_ci{
35818c2ecf20Sopenharmony_ci	if (machine_is(powermac)) {
35828c2ecf20Sopenharmony_ci		struct device_node *ofn = pci_device_to_OF_node(dev);
35838c2ecf20Sopenharmony_ci
35848c2ecf20Sopenharmony_ci		if (ofn) {
35858c2ecf20Sopenharmony_ci			pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
35868c2ecf20Sopenharmony_ci			pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0);
35878c2ecf20Sopenharmony_ci		}
35888c2ecf20Sopenharmony_ci	}
35898c2ecf20Sopenharmony_ci}
35908c2ecf20Sopenharmony_ci#else
35918c2ecf20Sopenharmony_cistatic inline void pmac_ohci_on(struct pci_dev *dev) {}
35928c2ecf20Sopenharmony_cistatic inline void pmac_ohci_off(struct pci_dev *dev) {}
35938c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_PMAC */
35948c2ecf20Sopenharmony_ci
35958c2ecf20Sopenharmony_cistatic int pci_probe(struct pci_dev *dev,
35968c2ecf20Sopenharmony_ci			       const struct pci_device_id *ent)
35978c2ecf20Sopenharmony_ci{
35988c2ecf20Sopenharmony_ci	struct fw_ohci *ohci;
35998c2ecf20Sopenharmony_ci	u32 bus_options, max_receive, link_speed, version;
36008c2ecf20Sopenharmony_ci	u64 guid;
36018c2ecf20Sopenharmony_ci	int i, err;
36028c2ecf20Sopenharmony_ci	size_t size;
36038c2ecf20Sopenharmony_ci
36048c2ecf20Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_PINNACLE_SYSTEMS) {
36058c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "Pinnacle MovieBoard is not yet supported\n");
36068c2ecf20Sopenharmony_ci		return -ENOSYS;
36078c2ecf20Sopenharmony_ci	}
36088c2ecf20Sopenharmony_ci
36098c2ecf20Sopenharmony_ci	ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
36108c2ecf20Sopenharmony_ci	if (ohci == NULL) {
36118c2ecf20Sopenharmony_ci		err = -ENOMEM;
36128c2ecf20Sopenharmony_ci		goto fail;
36138c2ecf20Sopenharmony_ci	}
36148c2ecf20Sopenharmony_ci
36158c2ecf20Sopenharmony_ci	fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
36168c2ecf20Sopenharmony_ci
36178c2ecf20Sopenharmony_ci	pmac_ohci_on(dev);
36188c2ecf20Sopenharmony_ci
36198c2ecf20Sopenharmony_ci	err = pci_enable_device(dev);
36208c2ecf20Sopenharmony_ci	if (err) {
36218c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "failed to enable OHCI hardware\n");
36228c2ecf20Sopenharmony_ci		goto fail_free;
36238c2ecf20Sopenharmony_ci	}
36248c2ecf20Sopenharmony_ci
36258c2ecf20Sopenharmony_ci	pci_set_master(dev);
36268c2ecf20Sopenharmony_ci	pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
36278c2ecf20Sopenharmony_ci	pci_set_drvdata(dev, ohci);
36288c2ecf20Sopenharmony_ci
36298c2ecf20Sopenharmony_ci	spin_lock_init(&ohci->lock);
36308c2ecf20Sopenharmony_ci	mutex_init(&ohci->phy_reg_mutex);
36318c2ecf20Sopenharmony_ci
36328c2ecf20Sopenharmony_ci	INIT_WORK(&ohci->bus_reset_work, bus_reset_work);
36338c2ecf20Sopenharmony_ci
36348c2ecf20Sopenharmony_ci	if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM) ||
36358c2ecf20Sopenharmony_ci	    pci_resource_len(dev, 0) < OHCI1394_REGISTER_SIZE) {
36368c2ecf20Sopenharmony_ci		ohci_err(ohci, "invalid MMIO resource\n");
36378c2ecf20Sopenharmony_ci		err = -ENXIO;
36388c2ecf20Sopenharmony_ci		goto fail_disable;
36398c2ecf20Sopenharmony_ci	}
36408c2ecf20Sopenharmony_ci
36418c2ecf20Sopenharmony_ci	err = pci_request_region(dev, 0, ohci_driver_name);
36428c2ecf20Sopenharmony_ci	if (err) {
36438c2ecf20Sopenharmony_ci		ohci_err(ohci, "MMIO resource unavailable\n");
36448c2ecf20Sopenharmony_ci		goto fail_disable;
36458c2ecf20Sopenharmony_ci	}
36468c2ecf20Sopenharmony_ci
36478c2ecf20Sopenharmony_ci	ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
36488c2ecf20Sopenharmony_ci	if (ohci->registers == NULL) {
36498c2ecf20Sopenharmony_ci		ohci_err(ohci, "failed to remap registers\n");
36508c2ecf20Sopenharmony_ci		err = -ENXIO;
36518c2ecf20Sopenharmony_ci		goto fail_iomem;
36528c2ecf20Sopenharmony_ci	}
36538c2ecf20Sopenharmony_ci
36548c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ohci_quirks); i++)
36558c2ecf20Sopenharmony_ci		if ((ohci_quirks[i].vendor == dev->vendor) &&
36568c2ecf20Sopenharmony_ci		    (ohci_quirks[i].device == (unsigned short)PCI_ANY_ID ||
36578c2ecf20Sopenharmony_ci		     ohci_quirks[i].device == dev->device) &&
36588c2ecf20Sopenharmony_ci		    (ohci_quirks[i].revision == (unsigned short)PCI_ANY_ID ||
36598c2ecf20Sopenharmony_ci		     ohci_quirks[i].revision >= dev->revision)) {
36608c2ecf20Sopenharmony_ci			ohci->quirks = ohci_quirks[i].flags;
36618c2ecf20Sopenharmony_ci			break;
36628c2ecf20Sopenharmony_ci		}
36638c2ecf20Sopenharmony_ci	if (param_quirks)
36648c2ecf20Sopenharmony_ci		ohci->quirks = param_quirks;
36658c2ecf20Sopenharmony_ci
36668c2ecf20Sopenharmony_ci	if (detect_vt630x_with_asm1083_on_amd_ryzen_machine(dev))
36678c2ecf20Sopenharmony_ci		ohci->quirks |= QUIRK_REBOOT_BY_CYCLE_TIMER_READ;
36688c2ecf20Sopenharmony_ci
36698c2ecf20Sopenharmony_ci	/*
36708c2ecf20Sopenharmony_ci	 * Because dma_alloc_coherent() allocates at least one page,
36718c2ecf20Sopenharmony_ci	 * we save space by using a common buffer for the AR request/
36728c2ecf20Sopenharmony_ci	 * response descriptors and the self IDs buffer.
36738c2ecf20Sopenharmony_ci	 */
36748c2ecf20Sopenharmony_ci	BUILD_BUG_ON(AR_BUFFERS * sizeof(struct descriptor) > PAGE_SIZE/4);
36758c2ecf20Sopenharmony_ci	BUILD_BUG_ON(SELF_ID_BUF_SIZE > PAGE_SIZE/2);
36768c2ecf20Sopenharmony_ci	ohci->misc_buffer = dma_alloc_coherent(ohci->card.device,
36778c2ecf20Sopenharmony_ci					       PAGE_SIZE,
36788c2ecf20Sopenharmony_ci					       &ohci->misc_buffer_bus,
36798c2ecf20Sopenharmony_ci					       GFP_KERNEL);
36808c2ecf20Sopenharmony_ci	if (!ohci->misc_buffer) {
36818c2ecf20Sopenharmony_ci		err = -ENOMEM;
36828c2ecf20Sopenharmony_ci		goto fail_iounmap;
36838c2ecf20Sopenharmony_ci	}
36848c2ecf20Sopenharmony_ci
36858c2ecf20Sopenharmony_ci	err = ar_context_init(&ohci->ar_request_ctx, ohci, 0,
36868c2ecf20Sopenharmony_ci			      OHCI1394_AsReqRcvContextControlSet);
36878c2ecf20Sopenharmony_ci	if (err < 0)
36888c2ecf20Sopenharmony_ci		goto fail_misc_buf;
36898c2ecf20Sopenharmony_ci
36908c2ecf20Sopenharmony_ci	err = ar_context_init(&ohci->ar_response_ctx, ohci, PAGE_SIZE/4,
36918c2ecf20Sopenharmony_ci			      OHCI1394_AsRspRcvContextControlSet);
36928c2ecf20Sopenharmony_ci	if (err < 0)
36938c2ecf20Sopenharmony_ci		goto fail_arreq_ctx;
36948c2ecf20Sopenharmony_ci
36958c2ecf20Sopenharmony_ci	err = context_init(&ohci->at_request_ctx, ohci,
36968c2ecf20Sopenharmony_ci			   OHCI1394_AsReqTrContextControlSet, handle_at_packet);
36978c2ecf20Sopenharmony_ci	if (err < 0)
36988c2ecf20Sopenharmony_ci		goto fail_arrsp_ctx;
36998c2ecf20Sopenharmony_ci
37008c2ecf20Sopenharmony_ci	err = context_init(&ohci->at_response_ctx, ohci,
37018c2ecf20Sopenharmony_ci			   OHCI1394_AsRspTrContextControlSet, handle_at_packet);
37028c2ecf20Sopenharmony_ci	if (err < 0)
37038c2ecf20Sopenharmony_ci		goto fail_atreq_ctx;
37048c2ecf20Sopenharmony_ci
37058c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
37068c2ecf20Sopenharmony_ci	ohci->ir_context_channels = ~0ULL;
37078c2ecf20Sopenharmony_ci	ohci->ir_context_support = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
37088c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
37098c2ecf20Sopenharmony_ci	ohci->ir_context_mask = ohci->ir_context_support;
37108c2ecf20Sopenharmony_ci	ohci->n_ir = hweight32(ohci->ir_context_mask);
37118c2ecf20Sopenharmony_ci	size = sizeof(struct iso_context) * ohci->n_ir;
37128c2ecf20Sopenharmony_ci	ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
37138c2ecf20Sopenharmony_ci
37148c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
37158c2ecf20Sopenharmony_ci	ohci->it_context_support = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
37168c2ecf20Sopenharmony_ci	/* JMicron JMB38x often shows 0 at first read, just ignore it */
37178c2ecf20Sopenharmony_ci	if (!ohci->it_context_support) {
37188c2ecf20Sopenharmony_ci		ohci_notice(ohci, "overriding IsoXmitIntMask\n");
37198c2ecf20Sopenharmony_ci		ohci->it_context_support = 0xf;
37208c2ecf20Sopenharmony_ci	}
37218c2ecf20Sopenharmony_ci	reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
37228c2ecf20Sopenharmony_ci	ohci->it_context_mask = ohci->it_context_support;
37238c2ecf20Sopenharmony_ci	ohci->n_it = hweight32(ohci->it_context_mask);
37248c2ecf20Sopenharmony_ci	size = sizeof(struct iso_context) * ohci->n_it;
37258c2ecf20Sopenharmony_ci	ohci->it_context_list = kzalloc(size, GFP_KERNEL);
37268c2ecf20Sopenharmony_ci
37278c2ecf20Sopenharmony_ci	if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
37288c2ecf20Sopenharmony_ci		err = -ENOMEM;
37298c2ecf20Sopenharmony_ci		goto fail_contexts;
37308c2ecf20Sopenharmony_ci	}
37318c2ecf20Sopenharmony_ci
37328c2ecf20Sopenharmony_ci	ohci->self_id     = ohci->misc_buffer     + PAGE_SIZE/2;
37338c2ecf20Sopenharmony_ci	ohci->self_id_bus = ohci->misc_buffer_bus + PAGE_SIZE/2;
37348c2ecf20Sopenharmony_ci
37358c2ecf20Sopenharmony_ci	bus_options = reg_read(ohci, OHCI1394_BusOptions);
37368c2ecf20Sopenharmony_ci	max_receive = (bus_options >> 12) & 0xf;
37378c2ecf20Sopenharmony_ci	link_speed = bus_options & 0x7;
37388c2ecf20Sopenharmony_ci	guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
37398c2ecf20Sopenharmony_ci		reg_read(ohci, OHCI1394_GUIDLo);
37408c2ecf20Sopenharmony_ci
37418c2ecf20Sopenharmony_ci	if (!(ohci->quirks & QUIRK_NO_MSI))
37428c2ecf20Sopenharmony_ci		pci_enable_msi(dev);
37438c2ecf20Sopenharmony_ci	if (request_irq(dev->irq, irq_handler,
37448c2ecf20Sopenharmony_ci			pci_dev_msi_enabled(dev) ? 0 : IRQF_SHARED,
37458c2ecf20Sopenharmony_ci			ohci_driver_name, ohci)) {
37468c2ecf20Sopenharmony_ci		ohci_err(ohci, "failed to allocate interrupt %d\n", dev->irq);
37478c2ecf20Sopenharmony_ci		err = -EIO;
37488c2ecf20Sopenharmony_ci		goto fail_msi;
37498c2ecf20Sopenharmony_ci	}
37508c2ecf20Sopenharmony_ci
37518c2ecf20Sopenharmony_ci	err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
37528c2ecf20Sopenharmony_ci	if (err)
37538c2ecf20Sopenharmony_ci		goto fail_irq;
37548c2ecf20Sopenharmony_ci
37558c2ecf20Sopenharmony_ci	version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
37568c2ecf20Sopenharmony_ci	ohci_notice(ohci,
37578c2ecf20Sopenharmony_ci		    "added OHCI v%x.%x device as card %d, "
37588c2ecf20Sopenharmony_ci		    "%d IR + %d IT contexts, quirks 0x%x%s\n",
37598c2ecf20Sopenharmony_ci		    version >> 16, version & 0xff, ohci->card.index,
37608c2ecf20Sopenharmony_ci		    ohci->n_ir, ohci->n_it, ohci->quirks,
37618c2ecf20Sopenharmony_ci		    reg_read(ohci, OHCI1394_PhyUpperBound) ?
37628c2ecf20Sopenharmony_ci			", physUB" : "");
37638c2ecf20Sopenharmony_ci
37648c2ecf20Sopenharmony_ci	return 0;
37658c2ecf20Sopenharmony_ci
37668c2ecf20Sopenharmony_ci fail_irq:
37678c2ecf20Sopenharmony_ci	free_irq(dev->irq, ohci);
37688c2ecf20Sopenharmony_ci fail_msi:
37698c2ecf20Sopenharmony_ci	pci_disable_msi(dev);
37708c2ecf20Sopenharmony_ci fail_contexts:
37718c2ecf20Sopenharmony_ci	kfree(ohci->ir_context_list);
37728c2ecf20Sopenharmony_ci	kfree(ohci->it_context_list);
37738c2ecf20Sopenharmony_ci	context_release(&ohci->at_response_ctx);
37748c2ecf20Sopenharmony_ci fail_atreq_ctx:
37758c2ecf20Sopenharmony_ci	context_release(&ohci->at_request_ctx);
37768c2ecf20Sopenharmony_ci fail_arrsp_ctx:
37778c2ecf20Sopenharmony_ci	ar_context_release(&ohci->ar_response_ctx);
37788c2ecf20Sopenharmony_ci fail_arreq_ctx:
37798c2ecf20Sopenharmony_ci	ar_context_release(&ohci->ar_request_ctx);
37808c2ecf20Sopenharmony_ci fail_misc_buf:
37818c2ecf20Sopenharmony_ci	dma_free_coherent(ohci->card.device, PAGE_SIZE,
37828c2ecf20Sopenharmony_ci			  ohci->misc_buffer, ohci->misc_buffer_bus);
37838c2ecf20Sopenharmony_ci fail_iounmap:
37848c2ecf20Sopenharmony_ci	pci_iounmap(dev, ohci->registers);
37858c2ecf20Sopenharmony_ci fail_iomem:
37868c2ecf20Sopenharmony_ci	pci_release_region(dev, 0);
37878c2ecf20Sopenharmony_ci fail_disable:
37888c2ecf20Sopenharmony_ci	pci_disable_device(dev);
37898c2ecf20Sopenharmony_ci fail_free:
37908c2ecf20Sopenharmony_ci	kfree(ohci);
37918c2ecf20Sopenharmony_ci	pmac_ohci_off(dev);
37928c2ecf20Sopenharmony_ci fail:
37938c2ecf20Sopenharmony_ci	return err;
37948c2ecf20Sopenharmony_ci}
37958c2ecf20Sopenharmony_ci
37968c2ecf20Sopenharmony_cistatic void pci_remove(struct pci_dev *dev)
37978c2ecf20Sopenharmony_ci{
37988c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = pci_get_drvdata(dev);
37998c2ecf20Sopenharmony_ci
38008c2ecf20Sopenharmony_ci	/*
38018c2ecf20Sopenharmony_ci	 * If the removal is happening from the suspend state, LPS won't be
38028c2ecf20Sopenharmony_ci	 * enabled and host registers (eg., IntMaskClear) won't be accessible.
38038c2ecf20Sopenharmony_ci	 */
38048c2ecf20Sopenharmony_ci	if (reg_read(ohci, OHCI1394_HCControlSet) & OHCI1394_HCControl_LPS) {
38058c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_IntMaskClear, ~0);
38068c2ecf20Sopenharmony_ci		flush_writes(ohci);
38078c2ecf20Sopenharmony_ci	}
38088c2ecf20Sopenharmony_ci	cancel_work_sync(&ohci->bus_reset_work);
38098c2ecf20Sopenharmony_ci	fw_core_remove_card(&ohci->card);
38108c2ecf20Sopenharmony_ci
38118c2ecf20Sopenharmony_ci	/*
38128c2ecf20Sopenharmony_ci	 * FIXME: Fail all pending packets here, now that the upper
38138c2ecf20Sopenharmony_ci	 * layers can't queue any more.
38148c2ecf20Sopenharmony_ci	 */
38158c2ecf20Sopenharmony_ci
38168c2ecf20Sopenharmony_ci	software_reset(ohci);
38178c2ecf20Sopenharmony_ci	free_irq(dev->irq, ohci);
38188c2ecf20Sopenharmony_ci
38198c2ecf20Sopenharmony_ci	if (ohci->next_config_rom && ohci->next_config_rom != ohci->config_rom)
38208c2ecf20Sopenharmony_ci		dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
38218c2ecf20Sopenharmony_ci				  ohci->next_config_rom, ohci->next_config_rom_bus);
38228c2ecf20Sopenharmony_ci	if (ohci->config_rom)
38238c2ecf20Sopenharmony_ci		dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
38248c2ecf20Sopenharmony_ci				  ohci->config_rom, ohci->config_rom_bus);
38258c2ecf20Sopenharmony_ci	ar_context_release(&ohci->ar_request_ctx);
38268c2ecf20Sopenharmony_ci	ar_context_release(&ohci->ar_response_ctx);
38278c2ecf20Sopenharmony_ci	dma_free_coherent(ohci->card.device, PAGE_SIZE,
38288c2ecf20Sopenharmony_ci			  ohci->misc_buffer, ohci->misc_buffer_bus);
38298c2ecf20Sopenharmony_ci	context_release(&ohci->at_request_ctx);
38308c2ecf20Sopenharmony_ci	context_release(&ohci->at_response_ctx);
38318c2ecf20Sopenharmony_ci	kfree(ohci->it_context_list);
38328c2ecf20Sopenharmony_ci	kfree(ohci->ir_context_list);
38338c2ecf20Sopenharmony_ci	pci_disable_msi(dev);
38348c2ecf20Sopenharmony_ci	pci_iounmap(dev, ohci->registers);
38358c2ecf20Sopenharmony_ci	pci_release_region(dev, 0);
38368c2ecf20Sopenharmony_ci	pci_disable_device(dev);
38378c2ecf20Sopenharmony_ci	kfree(ohci);
38388c2ecf20Sopenharmony_ci	pmac_ohci_off(dev);
38398c2ecf20Sopenharmony_ci
38408c2ecf20Sopenharmony_ci	dev_notice(&dev->dev, "removed fw-ohci device\n");
38418c2ecf20Sopenharmony_ci}
38428c2ecf20Sopenharmony_ci
38438c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
38448c2ecf20Sopenharmony_cistatic int pci_suspend(struct pci_dev *dev, pm_message_t state)
38458c2ecf20Sopenharmony_ci{
38468c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = pci_get_drvdata(dev);
38478c2ecf20Sopenharmony_ci	int err;
38488c2ecf20Sopenharmony_ci
38498c2ecf20Sopenharmony_ci	software_reset(ohci);
38508c2ecf20Sopenharmony_ci	err = pci_save_state(dev);
38518c2ecf20Sopenharmony_ci	if (err) {
38528c2ecf20Sopenharmony_ci		ohci_err(ohci, "pci_save_state failed\n");
38538c2ecf20Sopenharmony_ci		return err;
38548c2ecf20Sopenharmony_ci	}
38558c2ecf20Sopenharmony_ci	err = pci_set_power_state(dev, pci_choose_state(dev, state));
38568c2ecf20Sopenharmony_ci	if (err)
38578c2ecf20Sopenharmony_ci		ohci_err(ohci, "pci_set_power_state failed with %d\n", err);
38588c2ecf20Sopenharmony_ci	pmac_ohci_off(dev);
38598c2ecf20Sopenharmony_ci
38608c2ecf20Sopenharmony_ci	return 0;
38618c2ecf20Sopenharmony_ci}
38628c2ecf20Sopenharmony_ci
38638c2ecf20Sopenharmony_cistatic int pci_resume(struct pci_dev *dev)
38648c2ecf20Sopenharmony_ci{
38658c2ecf20Sopenharmony_ci	struct fw_ohci *ohci = pci_get_drvdata(dev);
38668c2ecf20Sopenharmony_ci	int err;
38678c2ecf20Sopenharmony_ci
38688c2ecf20Sopenharmony_ci	pmac_ohci_on(dev);
38698c2ecf20Sopenharmony_ci	pci_set_power_state(dev, PCI_D0);
38708c2ecf20Sopenharmony_ci	pci_restore_state(dev);
38718c2ecf20Sopenharmony_ci	err = pci_enable_device(dev);
38728c2ecf20Sopenharmony_ci	if (err) {
38738c2ecf20Sopenharmony_ci		ohci_err(ohci, "pci_enable_device failed\n");
38748c2ecf20Sopenharmony_ci		return err;
38758c2ecf20Sopenharmony_ci	}
38768c2ecf20Sopenharmony_ci
38778c2ecf20Sopenharmony_ci	/* Some systems don't setup GUID register on resume from ram  */
38788c2ecf20Sopenharmony_ci	if (!reg_read(ohci, OHCI1394_GUIDLo) &&
38798c2ecf20Sopenharmony_ci					!reg_read(ohci, OHCI1394_GUIDHi)) {
38808c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_GUIDLo, (u32)ohci->card.guid);
38818c2ecf20Sopenharmony_ci		reg_write(ohci, OHCI1394_GUIDHi, (u32)(ohci->card.guid >> 32));
38828c2ecf20Sopenharmony_ci	}
38838c2ecf20Sopenharmony_ci
38848c2ecf20Sopenharmony_ci	err = ohci_enable(&ohci->card, NULL, 0);
38858c2ecf20Sopenharmony_ci	if (err)
38868c2ecf20Sopenharmony_ci		return err;
38878c2ecf20Sopenharmony_ci
38888c2ecf20Sopenharmony_ci	ohci_resume_iso_dma(ohci);
38898c2ecf20Sopenharmony_ci
38908c2ecf20Sopenharmony_ci	return 0;
38918c2ecf20Sopenharmony_ci}
38928c2ecf20Sopenharmony_ci#endif
38938c2ecf20Sopenharmony_ci
38948c2ecf20Sopenharmony_cistatic const struct pci_device_id pci_table[] = {
38958c2ecf20Sopenharmony_ci	{ PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
38968c2ecf20Sopenharmony_ci	{ }
38978c2ecf20Sopenharmony_ci};
38988c2ecf20Sopenharmony_ci
38998c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, pci_table);
39008c2ecf20Sopenharmony_ci
39018c2ecf20Sopenharmony_cistatic struct pci_driver fw_ohci_pci_driver = {
39028c2ecf20Sopenharmony_ci	.name		= ohci_driver_name,
39038c2ecf20Sopenharmony_ci	.id_table	= pci_table,
39048c2ecf20Sopenharmony_ci	.probe		= pci_probe,
39058c2ecf20Sopenharmony_ci	.remove		= pci_remove,
39068c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
39078c2ecf20Sopenharmony_ci	.resume		= pci_resume,
39088c2ecf20Sopenharmony_ci	.suspend	= pci_suspend,
39098c2ecf20Sopenharmony_ci#endif
39108c2ecf20Sopenharmony_ci};
39118c2ecf20Sopenharmony_ci
39128c2ecf20Sopenharmony_cistatic int __init fw_ohci_init(void)
39138c2ecf20Sopenharmony_ci{
39148c2ecf20Sopenharmony_ci	selfid_workqueue = alloc_workqueue(KBUILD_MODNAME, WQ_MEM_RECLAIM, 0);
39158c2ecf20Sopenharmony_ci	if (!selfid_workqueue)
39168c2ecf20Sopenharmony_ci		return -ENOMEM;
39178c2ecf20Sopenharmony_ci
39188c2ecf20Sopenharmony_ci	return pci_register_driver(&fw_ohci_pci_driver);
39198c2ecf20Sopenharmony_ci}
39208c2ecf20Sopenharmony_ci
39218c2ecf20Sopenharmony_cistatic void __exit fw_ohci_cleanup(void)
39228c2ecf20Sopenharmony_ci{
39238c2ecf20Sopenharmony_ci	pci_unregister_driver(&fw_ohci_pci_driver);
39248c2ecf20Sopenharmony_ci	destroy_workqueue(selfid_workqueue);
39258c2ecf20Sopenharmony_ci}
39268c2ecf20Sopenharmony_ci
39278c2ecf20Sopenharmony_cimodule_init(fw_ohci_init);
39288c2ecf20Sopenharmony_cimodule_exit(fw_ohci_cleanup);
39298c2ecf20Sopenharmony_ci
39308c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
39318c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
39328c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
39338c2ecf20Sopenharmony_ci
39348c2ecf20Sopenharmony_ci/* Provide a module alias so root-on-sbp2 initrds don't break. */
39358c2ecf20Sopenharmony_ciMODULE_ALIAS("ohci1394");
3936