xref: /OK3568_Linux_fs/kernel/drivers/firewire/ohci.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Driver for OHCI 1394 controllers
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/bitops.h>
9*4882a593Smuzhiyun #include <linux/bug.h>
10*4882a593Smuzhiyun #include <linux/compiler.h>
11*4882a593Smuzhiyun #include <linux/delay.h>
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <linux/dma-mapping.h>
14*4882a593Smuzhiyun #include <linux/firewire.h>
15*4882a593Smuzhiyun #include <linux/firewire-constants.h>
16*4882a593Smuzhiyun #include <linux/init.h>
17*4882a593Smuzhiyun #include <linux/interrupt.h>
18*4882a593Smuzhiyun #include <linux/io.h>
19*4882a593Smuzhiyun #include <linux/kernel.h>
20*4882a593Smuzhiyun #include <linux/list.h>
21*4882a593Smuzhiyun #include <linux/mm.h>
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun #include <linux/moduleparam.h>
24*4882a593Smuzhiyun #include <linux/mutex.h>
25*4882a593Smuzhiyun #include <linux/pci.h>
26*4882a593Smuzhiyun #include <linux/pci_ids.h>
27*4882a593Smuzhiyun #include <linux/slab.h>
28*4882a593Smuzhiyun #include <linux/spinlock.h>
29*4882a593Smuzhiyun #include <linux/string.h>
30*4882a593Smuzhiyun #include <linux/time.h>
31*4882a593Smuzhiyun #include <linux/vmalloc.h>
32*4882a593Smuzhiyun #include <linux/workqueue.h>
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <asm/byteorder.h>
35*4882a593Smuzhiyun #include <asm/page.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #ifdef CONFIG_PPC_PMAC
38*4882a593Smuzhiyun #include <asm/pmac_feature.h>
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #include "core.h"
42*4882a593Smuzhiyun #include "ohci.h"
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #define ohci_info(ohci, f, args...)	dev_info(ohci->card.device, f, ##args)
45*4882a593Smuzhiyun #define ohci_notice(ohci, f, args...)	dev_notice(ohci->card.device, f, ##args)
46*4882a593Smuzhiyun #define ohci_err(ohci, f, args...)	dev_err(ohci->card.device, f, ##args)
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define DESCRIPTOR_OUTPUT_MORE		0
49*4882a593Smuzhiyun #define DESCRIPTOR_OUTPUT_LAST		(1 << 12)
50*4882a593Smuzhiyun #define DESCRIPTOR_INPUT_MORE		(2 << 12)
51*4882a593Smuzhiyun #define DESCRIPTOR_INPUT_LAST		(3 << 12)
52*4882a593Smuzhiyun #define DESCRIPTOR_STATUS		(1 << 11)
53*4882a593Smuzhiyun #define DESCRIPTOR_KEY_IMMEDIATE	(2 << 8)
54*4882a593Smuzhiyun #define DESCRIPTOR_PING			(1 << 7)
55*4882a593Smuzhiyun #define DESCRIPTOR_YY			(1 << 6)
56*4882a593Smuzhiyun #define DESCRIPTOR_NO_IRQ		(0 << 4)
57*4882a593Smuzhiyun #define DESCRIPTOR_IRQ_ERROR		(1 << 4)
58*4882a593Smuzhiyun #define DESCRIPTOR_IRQ_ALWAYS		(3 << 4)
59*4882a593Smuzhiyun #define DESCRIPTOR_BRANCH_ALWAYS	(3 << 2)
60*4882a593Smuzhiyun #define DESCRIPTOR_WAIT			(3 << 0)
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #define DESCRIPTOR_CMD			(0xf << 12)
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun struct descriptor {
65*4882a593Smuzhiyun 	__le16 req_count;
66*4882a593Smuzhiyun 	__le16 control;
67*4882a593Smuzhiyun 	__le32 data_address;
68*4882a593Smuzhiyun 	__le32 branch_address;
69*4882a593Smuzhiyun 	__le16 res_count;
70*4882a593Smuzhiyun 	__le16 transfer_status;
71*4882a593Smuzhiyun } __attribute__((aligned(16)));
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #define CONTROL_SET(regs)	(regs)
74*4882a593Smuzhiyun #define CONTROL_CLEAR(regs)	((regs) + 4)
75*4882a593Smuzhiyun #define COMMAND_PTR(regs)	((regs) + 12)
76*4882a593Smuzhiyun #define CONTEXT_MATCH(regs)	((regs) + 16)
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #define AR_BUFFER_SIZE	(32*1024)
79*4882a593Smuzhiyun #define AR_BUFFERS_MIN	DIV_ROUND_UP(AR_BUFFER_SIZE, PAGE_SIZE)
80*4882a593Smuzhiyun /* we need at least two pages for proper list management */
81*4882a593Smuzhiyun #define AR_BUFFERS	(AR_BUFFERS_MIN >= 2 ? AR_BUFFERS_MIN : 2)
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #define MAX_ASYNC_PAYLOAD	4096
84*4882a593Smuzhiyun #define MAX_AR_PACKET_SIZE	(16 + MAX_ASYNC_PAYLOAD + 4)
85*4882a593Smuzhiyun #define AR_WRAPAROUND_PAGES	DIV_ROUND_UP(MAX_AR_PACKET_SIZE, PAGE_SIZE)
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun struct ar_context {
88*4882a593Smuzhiyun 	struct fw_ohci *ohci;
89*4882a593Smuzhiyun 	struct page *pages[AR_BUFFERS];
90*4882a593Smuzhiyun 	void *buffer;
91*4882a593Smuzhiyun 	struct descriptor *descriptors;
92*4882a593Smuzhiyun 	dma_addr_t descriptors_bus;
93*4882a593Smuzhiyun 	void *pointer;
94*4882a593Smuzhiyun 	unsigned int last_buffer_index;
95*4882a593Smuzhiyun 	u32 regs;
96*4882a593Smuzhiyun 	struct tasklet_struct tasklet;
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun struct context;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun typedef int (*descriptor_callback_t)(struct context *ctx,
102*4882a593Smuzhiyun 				     struct descriptor *d,
103*4882a593Smuzhiyun 				     struct descriptor *last);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  * A buffer that contains a block of DMA-able coherent memory used for
107*4882a593Smuzhiyun  * storing a portion of a DMA descriptor program.
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun struct descriptor_buffer {
110*4882a593Smuzhiyun 	struct list_head list;
111*4882a593Smuzhiyun 	dma_addr_t buffer_bus;
112*4882a593Smuzhiyun 	size_t buffer_size;
113*4882a593Smuzhiyun 	size_t used;
114*4882a593Smuzhiyun 	struct descriptor buffer[];
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun struct context {
118*4882a593Smuzhiyun 	struct fw_ohci *ohci;
119*4882a593Smuzhiyun 	u32 regs;
120*4882a593Smuzhiyun 	int total_allocation;
121*4882a593Smuzhiyun 	u32 current_bus;
122*4882a593Smuzhiyun 	bool running;
123*4882a593Smuzhiyun 	bool flushing;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/*
126*4882a593Smuzhiyun 	 * List of page-sized buffers for storing DMA descriptors.
127*4882a593Smuzhiyun 	 * Head of list contains buffers in use and tail of list contains
128*4882a593Smuzhiyun 	 * free buffers.
129*4882a593Smuzhiyun 	 */
130*4882a593Smuzhiyun 	struct list_head buffer_list;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/*
133*4882a593Smuzhiyun 	 * Pointer to a buffer inside buffer_list that contains the tail
134*4882a593Smuzhiyun 	 * end of the current DMA program.
135*4882a593Smuzhiyun 	 */
136*4882a593Smuzhiyun 	struct descriptor_buffer *buffer_tail;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/*
139*4882a593Smuzhiyun 	 * The descriptor containing the branch address of the first
140*4882a593Smuzhiyun 	 * descriptor that has not yet been filled by the device.
141*4882a593Smuzhiyun 	 */
142*4882a593Smuzhiyun 	struct descriptor *last;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/*
145*4882a593Smuzhiyun 	 * The last descriptor block in the DMA program. It contains the branch
146*4882a593Smuzhiyun 	 * address that must be updated upon appending a new descriptor.
147*4882a593Smuzhiyun 	 */
148*4882a593Smuzhiyun 	struct descriptor *prev;
149*4882a593Smuzhiyun 	int prev_z;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	descriptor_callback_t callback;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	struct tasklet_struct tasklet;
154*4882a593Smuzhiyun };
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun #define IT_HEADER_SY(v)          ((v) <<  0)
157*4882a593Smuzhiyun #define IT_HEADER_TCODE(v)       ((v) <<  4)
158*4882a593Smuzhiyun #define IT_HEADER_CHANNEL(v)     ((v) <<  8)
159*4882a593Smuzhiyun #define IT_HEADER_TAG(v)         ((v) << 14)
160*4882a593Smuzhiyun #define IT_HEADER_SPEED(v)       ((v) << 16)
161*4882a593Smuzhiyun #define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun struct iso_context {
164*4882a593Smuzhiyun 	struct fw_iso_context base;
165*4882a593Smuzhiyun 	struct context context;
166*4882a593Smuzhiyun 	void *header;
167*4882a593Smuzhiyun 	size_t header_length;
168*4882a593Smuzhiyun 	unsigned long flushing_completions;
169*4882a593Smuzhiyun 	u32 mc_buffer_bus;
170*4882a593Smuzhiyun 	u16 mc_completed;
171*4882a593Smuzhiyun 	u16 last_timestamp;
172*4882a593Smuzhiyun 	u8 sync;
173*4882a593Smuzhiyun 	u8 tags;
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun #define CONFIG_ROM_SIZE 1024
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun struct fw_ohci {
179*4882a593Smuzhiyun 	struct fw_card card;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	__iomem char *registers;
182*4882a593Smuzhiyun 	int node_id;
183*4882a593Smuzhiyun 	int generation;
184*4882a593Smuzhiyun 	int request_generation;	/* for timestamping incoming requests */
185*4882a593Smuzhiyun 	unsigned quirks;
186*4882a593Smuzhiyun 	unsigned int pri_req_max;
187*4882a593Smuzhiyun 	u32 bus_time;
188*4882a593Smuzhiyun 	bool bus_time_running;
189*4882a593Smuzhiyun 	bool is_root;
190*4882a593Smuzhiyun 	bool csr_state_setclear_abdicate;
191*4882a593Smuzhiyun 	int n_ir;
192*4882a593Smuzhiyun 	int n_it;
193*4882a593Smuzhiyun 	/*
194*4882a593Smuzhiyun 	 * Spinlock for accessing fw_ohci data.  Never call out of
195*4882a593Smuzhiyun 	 * this driver with this lock held.
196*4882a593Smuzhiyun 	 */
197*4882a593Smuzhiyun 	spinlock_t lock;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	struct mutex phy_reg_mutex;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	void *misc_buffer;
202*4882a593Smuzhiyun 	dma_addr_t misc_buffer_bus;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	struct ar_context ar_request_ctx;
205*4882a593Smuzhiyun 	struct ar_context ar_response_ctx;
206*4882a593Smuzhiyun 	struct context at_request_ctx;
207*4882a593Smuzhiyun 	struct context at_response_ctx;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	u32 it_context_support;
210*4882a593Smuzhiyun 	u32 it_context_mask;     /* unoccupied IT contexts */
211*4882a593Smuzhiyun 	struct iso_context *it_context_list;
212*4882a593Smuzhiyun 	u64 ir_context_channels; /* unoccupied channels */
213*4882a593Smuzhiyun 	u32 ir_context_support;
214*4882a593Smuzhiyun 	u32 ir_context_mask;     /* unoccupied IR contexts */
215*4882a593Smuzhiyun 	struct iso_context *ir_context_list;
216*4882a593Smuzhiyun 	u64 mc_channels; /* channels in use by the multichannel IR context */
217*4882a593Smuzhiyun 	bool mc_allocated;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	__be32    *config_rom;
220*4882a593Smuzhiyun 	dma_addr_t config_rom_bus;
221*4882a593Smuzhiyun 	__be32    *next_config_rom;
222*4882a593Smuzhiyun 	dma_addr_t next_config_rom_bus;
223*4882a593Smuzhiyun 	__be32     next_header;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	__le32    *self_id;
226*4882a593Smuzhiyun 	dma_addr_t self_id_bus;
227*4882a593Smuzhiyun 	struct work_struct bus_reset_work;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	u32 self_id_buffer[512];
230*4882a593Smuzhiyun };
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static struct workqueue_struct *selfid_workqueue;
233*4882a593Smuzhiyun 
fw_ohci(struct fw_card * card)234*4882a593Smuzhiyun static inline struct fw_ohci *fw_ohci(struct fw_card *card)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	return container_of(card, struct fw_ohci, card);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun #define IT_CONTEXT_CYCLE_MATCH_ENABLE	0x80000000
240*4882a593Smuzhiyun #define IR_CONTEXT_BUFFER_FILL		0x80000000
241*4882a593Smuzhiyun #define IR_CONTEXT_ISOCH_HEADER		0x40000000
242*4882a593Smuzhiyun #define IR_CONTEXT_CYCLE_MATCH_ENABLE	0x20000000
243*4882a593Smuzhiyun #define IR_CONTEXT_MULTI_CHANNEL_MODE	0x10000000
244*4882a593Smuzhiyun #define IR_CONTEXT_DUAL_BUFFER_MODE	0x08000000
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun #define CONTEXT_RUN	0x8000
247*4882a593Smuzhiyun #define CONTEXT_WAKE	0x1000
248*4882a593Smuzhiyun #define CONTEXT_DEAD	0x0800
249*4882a593Smuzhiyun #define CONTEXT_ACTIVE	0x0400
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun #define OHCI1394_MAX_AT_REQ_RETRIES	0xf
252*4882a593Smuzhiyun #define OHCI1394_MAX_AT_RESP_RETRIES	0x2
253*4882a593Smuzhiyun #define OHCI1394_MAX_PHYS_RESP_RETRIES	0x8
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun #define OHCI1394_REGISTER_SIZE		0x800
256*4882a593Smuzhiyun #define OHCI1394_PCI_HCI_Control	0x40
257*4882a593Smuzhiyun #define SELF_ID_BUF_SIZE		0x800
258*4882a593Smuzhiyun #define OHCI_TCODE_PHY_PACKET		0x0e
259*4882a593Smuzhiyun #define OHCI_VERSION_1_1		0x010010
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun static char ohci_driver_name[] = KBUILD_MODNAME;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun #define PCI_VENDOR_ID_PINNACLE_SYSTEMS	0x11bd
264*4882a593Smuzhiyun #define PCI_DEVICE_ID_AGERE_FW643	0x5901
265*4882a593Smuzhiyun #define PCI_DEVICE_ID_CREATIVE_SB1394	0x4001
266*4882a593Smuzhiyun #define PCI_DEVICE_ID_JMICRON_JMB38X_FW	0x2380
267*4882a593Smuzhiyun #define PCI_DEVICE_ID_TI_TSB12LV22	0x8009
268*4882a593Smuzhiyun #define PCI_DEVICE_ID_TI_TSB12LV26	0x8020
269*4882a593Smuzhiyun #define PCI_DEVICE_ID_TI_TSB82AA2	0x8025
270*4882a593Smuzhiyun #define PCI_DEVICE_ID_VIA_VT630X	0x3044
271*4882a593Smuzhiyun #define PCI_REV_ID_VIA_VT6306		0x46
272*4882a593Smuzhiyun #define PCI_DEVICE_ID_VIA_VT6315	0x3403
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun #define QUIRK_CYCLE_TIMER		0x1
275*4882a593Smuzhiyun #define QUIRK_RESET_PACKET		0x2
276*4882a593Smuzhiyun #define QUIRK_BE_HEADERS		0x4
277*4882a593Smuzhiyun #define QUIRK_NO_1394A			0x8
278*4882a593Smuzhiyun #define QUIRK_NO_MSI			0x10
279*4882a593Smuzhiyun #define QUIRK_TI_SLLZ059		0x20
280*4882a593Smuzhiyun #define QUIRK_IR_WAKE			0x40
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun /* In case of multiple matches in ohci_quirks[], only the first one is used. */
283*4882a593Smuzhiyun static const struct {
284*4882a593Smuzhiyun 	unsigned short vendor, device, revision, flags;
285*4882a593Smuzhiyun } ohci_quirks[] = {
286*4882a593Smuzhiyun 	{PCI_VENDOR_ID_AL, PCI_ANY_ID, PCI_ANY_ID,
287*4882a593Smuzhiyun 		QUIRK_CYCLE_TIMER},
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	{PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_FW, PCI_ANY_ID,
290*4882a593Smuzhiyun 		QUIRK_BE_HEADERS},
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	{PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_AGERE_FW643, 6,
293*4882a593Smuzhiyun 		QUIRK_NO_MSI},
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	{PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_SB1394, PCI_ANY_ID,
296*4882a593Smuzhiyun 		QUIRK_RESET_PACKET},
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	{PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB38X_FW, PCI_ANY_ID,
299*4882a593Smuzhiyun 		QUIRK_NO_MSI},
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	{PCI_VENDOR_ID_NEC, PCI_ANY_ID, PCI_ANY_ID,
302*4882a593Smuzhiyun 		QUIRK_CYCLE_TIMER},
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	{PCI_VENDOR_ID_O2, PCI_ANY_ID, PCI_ANY_ID,
305*4882a593Smuzhiyun 		QUIRK_NO_MSI},
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	{PCI_VENDOR_ID_RICOH, PCI_ANY_ID, PCI_ANY_ID,
308*4882a593Smuzhiyun 		QUIRK_CYCLE_TIMER | QUIRK_NO_MSI},
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	{PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV22, PCI_ANY_ID,
311*4882a593Smuzhiyun 		QUIRK_CYCLE_TIMER | QUIRK_RESET_PACKET | QUIRK_NO_1394A},
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	{PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB12LV26, PCI_ANY_ID,
314*4882a593Smuzhiyun 		QUIRK_RESET_PACKET | QUIRK_TI_SLLZ059},
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	{PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_TSB82AA2, PCI_ANY_ID,
317*4882a593Smuzhiyun 		QUIRK_RESET_PACKET | QUIRK_TI_SLLZ059},
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	{PCI_VENDOR_ID_TI, PCI_ANY_ID, PCI_ANY_ID,
320*4882a593Smuzhiyun 		QUIRK_RESET_PACKET},
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	{PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT630X, PCI_REV_ID_VIA_VT6306,
323*4882a593Smuzhiyun 		QUIRK_CYCLE_TIMER | QUIRK_IR_WAKE},
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	{PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT6315, 0,
326*4882a593Smuzhiyun 		QUIRK_CYCLE_TIMER /* FIXME: necessary? */ | QUIRK_NO_MSI},
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	{PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VT6315, PCI_ANY_ID,
329*4882a593Smuzhiyun 		QUIRK_NO_MSI},
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	{PCI_VENDOR_ID_VIA, PCI_ANY_ID, PCI_ANY_ID,
332*4882a593Smuzhiyun 		QUIRK_CYCLE_TIMER | QUIRK_NO_MSI},
333*4882a593Smuzhiyun };
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun /* This overrides anything that was found in ohci_quirks[]. */
336*4882a593Smuzhiyun static int param_quirks;
337*4882a593Smuzhiyun module_param_named(quirks, param_quirks, int, 0644);
338*4882a593Smuzhiyun MODULE_PARM_DESC(quirks, "Chip quirks (default = 0"
339*4882a593Smuzhiyun 	", nonatomic cycle timer = "	__stringify(QUIRK_CYCLE_TIMER)
340*4882a593Smuzhiyun 	", reset packet generation = "	__stringify(QUIRK_RESET_PACKET)
341*4882a593Smuzhiyun 	", AR/selfID endianness = "	__stringify(QUIRK_BE_HEADERS)
342*4882a593Smuzhiyun 	", no 1394a enhancements = "	__stringify(QUIRK_NO_1394A)
343*4882a593Smuzhiyun 	", disable MSI = "		__stringify(QUIRK_NO_MSI)
344*4882a593Smuzhiyun 	", TI SLLZ059 erratum = "	__stringify(QUIRK_TI_SLLZ059)
345*4882a593Smuzhiyun 	", IR wake unreliable = "	__stringify(QUIRK_IR_WAKE)
346*4882a593Smuzhiyun 	")");
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun #define OHCI_PARAM_DEBUG_AT_AR		1
349*4882a593Smuzhiyun #define OHCI_PARAM_DEBUG_SELFIDS	2
350*4882a593Smuzhiyun #define OHCI_PARAM_DEBUG_IRQS		4
351*4882a593Smuzhiyun #define OHCI_PARAM_DEBUG_BUSRESETS	8 /* only effective before chip init */
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun static int param_debug;
354*4882a593Smuzhiyun module_param_named(debug, param_debug, int, 0644);
355*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Verbose logging (default = 0"
356*4882a593Smuzhiyun 	", AT/AR events = "	__stringify(OHCI_PARAM_DEBUG_AT_AR)
357*4882a593Smuzhiyun 	", self-IDs = "		__stringify(OHCI_PARAM_DEBUG_SELFIDS)
358*4882a593Smuzhiyun 	", IRQs = "		__stringify(OHCI_PARAM_DEBUG_IRQS)
359*4882a593Smuzhiyun 	", busReset events = "	__stringify(OHCI_PARAM_DEBUG_BUSRESETS)
360*4882a593Smuzhiyun 	", or a combination, or all = -1)");
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun static bool param_remote_dma;
363*4882a593Smuzhiyun module_param_named(remote_dma, param_remote_dma, bool, 0444);
364*4882a593Smuzhiyun MODULE_PARM_DESC(remote_dma, "Enable unfiltered remote DMA (default = N)");
365*4882a593Smuzhiyun 
log_irqs(struct fw_ohci * ohci,u32 evt)366*4882a593Smuzhiyun static void log_irqs(struct fw_ohci *ohci, u32 evt)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	if (likely(!(param_debug &
369*4882a593Smuzhiyun 			(OHCI_PARAM_DEBUG_IRQS | OHCI_PARAM_DEBUG_BUSRESETS))))
370*4882a593Smuzhiyun 		return;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if (!(param_debug & OHCI_PARAM_DEBUG_IRQS) &&
373*4882a593Smuzhiyun 	    !(evt & OHCI1394_busReset))
374*4882a593Smuzhiyun 		return;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	ohci_notice(ohci, "IRQ %08x%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", evt,
377*4882a593Smuzhiyun 	    evt & OHCI1394_selfIDComplete	? " selfID"		: "",
378*4882a593Smuzhiyun 	    evt & OHCI1394_RQPkt		? " AR_req"		: "",
379*4882a593Smuzhiyun 	    evt & OHCI1394_RSPkt		? " AR_resp"		: "",
380*4882a593Smuzhiyun 	    evt & OHCI1394_reqTxComplete	? " AT_req"		: "",
381*4882a593Smuzhiyun 	    evt & OHCI1394_respTxComplete	? " AT_resp"		: "",
382*4882a593Smuzhiyun 	    evt & OHCI1394_isochRx		? " IR"			: "",
383*4882a593Smuzhiyun 	    evt & OHCI1394_isochTx		? " IT"			: "",
384*4882a593Smuzhiyun 	    evt & OHCI1394_postedWriteErr	? " postedWriteErr"	: "",
385*4882a593Smuzhiyun 	    evt & OHCI1394_cycleTooLong		? " cycleTooLong"	: "",
386*4882a593Smuzhiyun 	    evt & OHCI1394_cycle64Seconds	? " cycle64Seconds"	: "",
387*4882a593Smuzhiyun 	    evt & OHCI1394_cycleInconsistent	? " cycleInconsistent"	: "",
388*4882a593Smuzhiyun 	    evt & OHCI1394_regAccessFail	? " regAccessFail"	: "",
389*4882a593Smuzhiyun 	    evt & OHCI1394_unrecoverableError	? " unrecoverableError"	: "",
390*4882a593Smuzhiyun 	    evt & OHCI1394_busReset		? " busReset"		: "",
391*4882a593Smuzhiyun 	    evt & ~(OHCI1394_selfIDComplete | OHCI1394_RQPkt |
392*4882a593Smuzhiyun 		    OHCI1394_RSPkt | OHCI1394_reqTxComplete |
393*4882a593Smuzhiyun 		    OHCI1394_respTxComplete | OHCI1394_isochRx |
394*4882a593Smuzhiyun 		    OHCI1394_isochTx | OHCI1394_postedWriteErr |
395*4882a593Smuzhiyun 		    OHCI1394_cycleTooLong | OHCI1394_cycle64Seconds |
396*4882a593Smuzhiyun 		    OHCI1394_cycleInconsistent |
397*4882a593Smuzhiyun 		    OHCI1394_regAccessFail | OHCI1394_busReset)
398*4882a593Smuzhiyun 						? " ?"			: "");
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun static const char *speed[] = {
402*4882a593Smuzhiyun 	[0] = "S100", [1] = "S200", [2] = "S400",    [3] = "beta",
403*4882a593Smuzhiyun };
404*4882a593Smuzhiyun static const char *power[] = {
405*4882a593Smuzhiyun 	[0] = "+0W",  [1] = "+15W", [2] = "+30W",    [3] = "+45W",
406*4882a593Smuzhiyun 	[4] = "-3W",  [5] = " ?W",  [6] = "-3..-6W", [7] = "-3..-10W",
407*4882a593Smuzhiyun };
408*4882a593Smuzhiyun static const char port[] = { '.', '-', 'p', 'c', };
409*4882a593Smuzhiyun 
_p(u32 * s,int shift)410*4882a593Smuzhiyun static char _p(u32 *s, int shift)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	return port[*s >> shift & 3];
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
log_selfids(struct fw_ohci * ohci,int generation,int self_id_count)415*4882a593Smuzhiyun static void log_selfids(struct fw_ohci *ohci, int generation, int self_id_count)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	u32 *s;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	if (likely(!(param_debug & OHCI_PARAM_DEBUG_SELFIDS)))
420*4882a593Smuzhiyun 		return;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	ohci_notice(ohci, "%d selfIDs, generation %d, local node ID %04x\n",
423*4882a593Smuzhiyun 		    self_id_count, generation, ohci->node_id);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	for (s = ohci->self_id_buffer; self_id_count--; ++s)
426*4882a593Smuzhiyun 		if ((*s & 1 << 23) == 0)
427*4882a593Smuzhiyun 			ohci_notice(ohci,
428*4882a593Smuzhiyun 			    "selfID 0: %08x, phy %d [%c%c%c] %s gc=%d %s %s%s%s\n",
429*4882a593Smuzhiyun 			    *s, *s >> 24 & 63, _p(s, 6), _p(s, 4), _p(s, 2),
430*4882a593Smuzhiyun 			    speed[*s >> 14 & 3], *s >> 16 & 63,
431*4882a593Smuzhiyun 			    power[*s >> 8 & 7], *s >> 22 & 1 ? "L" : "",
432*4882a593Smuzhiyun 			    *s >> 11 & 1 ? "c" : "", *s & 2 ? "i" : "");
433*4882a593Smuzhiyun 		else
434*4882a593Smuzhiyun 			ohci_notice(ohci,
435*4882a593Smuzhiyun 			    "selfID n: %08x, phy %d [%c%c%c%c%c%c%c%c]\n",
436*4882a593Smuzhiyun 			    *s, *s >> 24 & 63,
437*4882a593Smuzhiyun 			    _p(s, 16), _p(s, 14), _p(s, 12), _p(s, 10),
438*4882a593Smuzhiyun 			    _p(s,  8), _p(s,  6), _p(s,  4), _p(s,  2));
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun static const char *evts[] = {
442*4882a593Smuzhiyun 	[0x00] = "evt_no_status",	[0x01] = "-reserved-",
443*4882a593Smuzhiyun 	[0x02] = "evt_long_packet",	[0x03] = "evt_missing_ack",
444*4882a593Smuzhiyun 	[0x04] = "evt_underrun",	[0x05] = "evt_overrun",
445*4882a593Smuzhiyun 	[0x06] = "evt_descriptor_read",	[0x07] = "evt_data_read",
446*4882a593Smuzhiyun 	[0x08] = "evt_data_write",	[0x09] = "evt_bus_reset",
447*4882a593Smuzhiyun 	[0x0a] = "evt_timeout",		[0x0b] = "evt_tcode_err",
448*4882a593Smuzhiyun 	[0x0c] = "-reserved-",		[0x0d] = "-reserved-",
449*4882a593Smuzhiyun 	[0x0e] = "evt_unknown",		[0x0f] = "evt_flushed",
450*4882a593Smuzhiyun 	[0x10] = "-reserved-",		[0x11] = "ack_complete",
451*4882a593Smuzhiyun 	[0x12] = "ack_pending ",	[0x13] = "-reserved-",
452*4882a593Smuzhiyun 	[0x14] = "ack_busy_X",		[0x15] = "ack_busy_A",
453*4882a593Smuzhiyun 	[0x16] = "ack_busy_B",		[0x17] = "-reserved-",
454*4882a593Smuzhiyun 	[0x18] = "-reserved-",		[0x19] = "-reserved-",
455*4882a593Smuzhiyun 	[0x1a] = "-reserved-",		[0x1b] = "ack_tardy",
456*4882a593Smuzhiyun 	[0x1c] = "-reserved-",		[0x1d] = "ack_data_error",
457*4882a593Smuzhiyun 	[0x1e] = "ack_type_error",	[0x1f] = "-reserved-",
458*4882a593Smuzhiyun 	[0x20] = "pending/cancelled",
459*4882a593Smuzhiyun };
460*4882a593Smuzhiyun static const char *tcodes[] = {
461*4882a593Smuzhiyun 	[0x0] = "QW req",		[0x1] = "BW req",
462*4882a593Smuzhiyun 	[0x2] = "W resp",		[0x3] = "-reserved-",
463*4882a593Smuzhiyun 	[0x4] = "QR req",		[0x5] = "BR req",
464*4882a593Smuzhiyun 	[0x6] = "QR resp",		[0x7] = "BR resp",
465*4882a593Smuzhiyun 	[0x8] = "cycle start",		[0x9] = "Lk req",
466*4882a593Smuzhiyun 	[0xa] = "async stream packet",	[0xb] = "Lk resp",
467*4882a593Smuzhiyun 	[0xc] = "-reserved-",		[0xd] = "-reserved-",
468*4882a593Smuzhiyun 	[0xe] = "link internal",	[0xf] = "-reserved-",
469*4882a593Smuzhiyun };
470*4882a593Smuzhiyun 
log_ar_at_event(struct fw_ohci * ohci,char dir,int speed,u32 * header,int evt)471*4882a593Smuzhiyun static void log_ar_at_event(struct fw_ohci *ohci,
472*4882a593Smuzhiyun 			    char dir, int speed, u32 *header, int evt)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun 	int tcode = header[0] >> 4 & 0xf;
475*4882a593Smuzhiyun 	char specific[12];
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	if (likely(!(param_debug & OHCI_PARAM_DEBUG_AT_AR)))
478*4882a593Smuzhiyun 		return;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	if (unlikely(evt >= ARRAY_SIZE(evts)))
481*4882a593Smuzhiyun 			evt = 0x1f;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	if (evt == OHCI1394_evt_bus_reset) {
484*4882a593Smuzhiyun 		ohci_notice(ohci, "A%c evt_bus_reset, generation %d\n",
485*4882a593Smuzhiyun 			    dir, (header[2] >> 16) & 0xff);
486*4882a593Smuzhiyun 		return;
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	switch (tcode) {
490*4882a593Smuzhiyun 	case 0x0: case 0x6: case 0x8:
491*4882a593Smuzhiyun 		snprintf(specific, sizeof(specific), " = %08x",
492*4882a593Smuzhiyun 			 be32_to_cpu((__force __be32)header[3]));
493*4882a593Smuzhiyun 		break;
494*4882a593Smuzhiyun 	case 0x1: case 0x5: case 0x7: case 0x9: case 0xb:
495*4882a593Smuzhiyun 		snprintf(specific, sizeof(specific), " %x,%x",
496*4882a593Smuzhiyun 			 header[3] >> 16, header[3] & 0xffff);
497*4882a593Smuzhiyun 		break;
498*4882a593Smuzhiyun 	default:
499*4882a593Smuzhiyun 		specific[0] = '\0';
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	switch (tcode) {
503*4882a593Smuzhiyun 	case 0xa:
504*4882a593Smuzhiyun 		ohci_notice(ohci, "A%c %s, %s\n",
505*4882a593Smuzhiyun 			    dir, evts[evt], tcodes[tcode]);
506*4882a593Smuzhiyun 		break;
507*4882a593Smuzhiyun 	case 0xe:
508*4882a593Smuzhiyun 		ohci_notice(ohci, "A%c %s, PHY %08x %08x\n",
509*4882a593Smuzhiyun 			    dir, evts[evt], header[1], header[2]);
510*4882a593Smuzhiyun 		break;
511*4882a593Smuzhiyun 	case 0x0: case 0x1: case 0x4: case 0x5: case 0x9:
512*4882a593Smuzhiyun 		ohci_notice(ohci,
513*4882a593Smuzhiyun 			    "A%c spd %x tl %02x, %04x -> %04x, %s, %s, %04x%08x%s\n",
514*4882a593Smuzhiyun 			    dir, speed, header[0] >> 10 & 0x3f,
515*4882a593Smuzhiyun 			    header[1] >> 16, header[0] >> 16, evts[evt],
516*4882a593Smuzhiyun 			    tcodes[tcode], header[1] & 0xffff, header[2], specific);
517*4882a593Smuzhiyun 		break;
518*4882a593Smuzhiyun 	default:
519*4882a593Smuzhiyun 		ohci_notice(ohci,
520*4882a593Smuzhiyun 			    "A%c spd %x tl %02x, %04x -> %04x, %s, %s%s\n",
521*4882a593Smuzhiyun 			    dir, speed, header[0] >> 10 & 0x3f,
522*4882a593Smuzhiyun 			    header[1] >> 16, header[0] >> 16, evts[evt],
523*4882a593Smuzhiyun 			    tcodes[tcode], specific);
524*4882a593Smuzhiyun 	}
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun 
reg_write(const struct fw_ohci * ohci,int offset,u32 data)527*4882a593Smuzhiyun static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	writel(data, ohci->registers + offset);
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun 
reg_read(const struct fw_ohci * ohci,int offset)532*4882a593Smuzhiyun static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	return readl(ohci->registers + offset);
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun 
flush_writes(const struct fw_ohci * ohci)537*4882a593Smuzhiyun static inline void flush_writes(const struct fw_ohci *ohci)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	/* Do a dummy read to flush writes. */
540*4882a593Smuzhiyun 	reg_read(ohci, OHCI1394_Version);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun  * Beware!  read_phy_reg(), write_phy_reg(), update_phy_reg(), and
545*4882a593Smuzhiyun  * read_paged_phy_reg() require the caller to hold ohci->phy_reg_mutex.
546*4882a593Smuzhiyun  * In other words, only use ohci_read_phy_reg() and ohci_update_phy_reg()
547*4882a593Smuzhiyun  * directly.  Exceptions are intrinsically serialized contexts like pci_probe.
548*4882a593Smuzhiyun  */
read_phy_reg(struct fw_ohci * ohci,int addr)549*4882a593Smuzhiyun static int read_phy_reg(struct fw_ohci *ohci, int addr)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun 	u32 val;
552*4882a593Smuzhiyun 	int i;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
555*4882a593Smuzhiyun 	for (i = 0; i < 3 + 100; i++) {
556*4882a593Smuzhiyun 		val = reg_read(ohci, OHCI1394_PhyControl);
557*4882a593Smuzhiyun 		if (!~val)
558*4882a593Smuzhiyun 			return -ENODEV; /* Card was ejected. */
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 		if (val & OHCI1394_PhyControl_ReadDone)
561*4882a593Smuzhiyun 			return OHCI1394_PhyControl_ReadData(val);
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 		/*
564*4882a593Smuzhiyun 		 * Try a few times without waiting.  Sleeping is necessary
565*4882a593Smuzhiyun 		 * only when the link/PHY interface is busy.
566*4882a593Smuzhiyun 		 */
567*4882a593Smuzhiyun 		if (i >= 3)
568*4882a593Smuzhiyun 			msleep(1);
569*4882a593Smuzhiyun 	}
570*4882a593Smuzhiyun 	ohci_err(ohci, "failed to read phy reg %d\n", addr);
571*4882a593Smuzhiyun 	dump_stack();
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	return -EBUSY;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
write_phy_reg(const struct fw_ohci * ohci,int addr,u32 val)576*4882a593Smuzhiyun static int write_phy_reg(const struct fw_ohci *ohci, int addr, u32 val)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun 	int i;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_PhyControl,
581*4882a593Smuzhiyun 		  OHCI1394_PhyControl_Write(addr, val));
582*4882a593Smuzhiyun 	for (i = 0; i < 3 + 100; i++) {
583*4882a593Smuzhiyun 		val = reg_read(ohci, OHCI1394_PhyControl);
584*4882a593Smuzhiyun 		if (!~val)
585*4882a593Smuzhiyun 			return -ENODEV; /* Card was ejected. */
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 		if (!(val & OHCI1394_PhyControl_WritePending))
588*4882a593Smuzhiyun 			return 0;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 		if (i >= 3)
591*4882a593Smuzhiyun 			msleep(1);
592*4882a593Smuzhiyun 	}
593*4882a593Smuzhiyun 	ohci_err(ohci, "failed to write phy reg %d, val %u\n", addr, val);
594*4882a593Smuzhiyun 	dump_stack();
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	return -EBUSY;
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun 
update_phy_reg(struct fw_ohci * ohci,int addr,int clear_bits,int set_bits)599*4882a593Smuzhiyun static int update_phy_reg(struct fw_ohci *ohci, int addr,
600*4882a593Smuzhiyun 			  int clear_bits, int set_bits)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun 	int ret = read_phy_reg(ohci, addr);
603*4882a593Smuzhiyun 	if (ret < 0)
604*4882a593Smuzhiyun 		return ret;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	/*
607*4882a593Smuzhiyun 	 * The interrupt status bits are cleared by writing a one bit.
608*4882a593Smuzhiyun 	 * Avoid clearing them unless explicitly requested in set_bits.
609*4882a593Smuzhiyun 	 */
610*4882a593Smuzhiyun 	if (addr == 5)
611*4882a593Smuzhiyun 		clear_bits |= PHY_INT_STATUS_BITS;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	return write_phy_reg(ohci, addr, (ret & ~clear_bits) | set_bits);
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun 
read_paged_phy_reg(struct fw_ohci * ohci,int page,int addr)616*4882a593Smuzhiyun static int read_paged_phy_reg(struct fw_ohci *ohci, int page, int addr)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	int ret;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	ret = update_phy_reg(ohci, 7, PHY_PAGE_SELECT, page << 5);
621*4882a593Smuzhiyun 	if (ret < 0)
622*4882a593Smuzhiyun 		return ret;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	return read_phy_reg(ohci, addr);
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun 
ohci_read_phy_reg(struct fw_card * card,int addr)627*4882a593Smuzhiyun static int ohci_read_phy_reg(struct fw_card *card, int addr)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
630*4882a593Smuzhiyun 	int ret;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	mutex_lock(&ohci->phy_reg_mutex);
633*4882a593Smuzhiyun 	ret = read_phy_reg(ohci, addr);
634*4882a593Smuzhiyun 	mutex_unlock(&ohci->phy_reg_mutex);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	return ret;
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun 
ohci_update_phy_reg(struct fw_card * card,int addr,int clear_bits,int set_bits)639*4882a593Smuzhiyun static int ohci_update_phy_reg(struct fw_card *card, int addr,
640*4882a593Smuzhiyun 			       int clear_bits, int set_bits)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
643*4882a593Smuzhiyun 	int ret;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	mutex_lock(&ohci->phy_reg_mutex);
646*4882a593Smuzhiyun 	ret = update_phy_reg(ohci, addr, clear_bits, set_bits);
647*4882a593Smuzhiyun 	mutex_unlock(&ohci->phy_reg_mutex);
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	return ret;
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun 
ar_buffer_bus(struct ar_context * ctx,unsigned int i)652*4882a593Smuzhiyun static inline dma_addr_t ar_buffer_bus(struct ar_context *ctx, unsigned int i)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun 	return page_private(ctx->pages[i]);
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun 
ar_context_link_page(struct ar_context * ctx,unsigned int index)657*4882a593Smuzhiyun static void ar_context_link_page(struct ar_context *ctx, unsigned int index)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun 	struct descriptor *d;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	d = &ctx->descriptors[index];
662*4882a593Smuzhiyun 	d->branch_address  &= cpu_to_le32(~0xf);
663*4882a593Smuzhiyun 	d->res_count       =  cpu_to_le16(PAGE_SIZE);
664*4882a593Smuzhiyun 	d->transfer_status =  0;
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	wmb(); /* finish init of new descriptors before branch_address update */
667*4882a593Smuzhiyun 	d = &ctx->descriptors[ctx->last_buffer_index];
668*4882a593Smuzhiyun 	d->branch_address  |= cpu_to_le32(1);
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	ctx->last_buffer_index = index;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun 
ar_context_release(struct ar_context * ctx)675*4882a593Smuzhiyun static void ar_context_release(struct ar_context *ctx)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun 	struct device *dev = ctx->ohci->card.device;
678*4882a593Smuzhiyun 	unsigned int i;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	vunmap(ctx->buffer);
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	for (i = 0; i < AR_BUFFERS; i++) {
683*4882a593Smuzhiyun 		if (ctx->pages[i])
684*4882a593Smuzhiyun 			dma_free_pages(dev, PAGE_SIZE, ctx->pages[i],
685*4882a593Smuzhiyun 				       ar_buffer_bus(ctx, i), DMA_FROM_DEVICE);
686*4882a593Smuzhiyun 	}
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun 
ar_context_abort(struct ar_context * ctx,const char * error_msg)689*4882a593Smuzhiyun static void ar_context_abort(struct ar_context *ctx, const char *error_msg)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun 	struct fw_ohci *ohci = ctx->ohci;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	if (reg_read(ohci, CONTROL_CLEAR(ctx->regs)) & CONTEXT_RUN) {
694*4882a593Smuzhiyun 		reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
695*4882a593Smuzhiyun 		flush_writes(ohci);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 		ohci_err(ohci, "AR error: %s; DMA stopped\n", error_msg);
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 	/* FIXME: restart? */
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun 
ar_next_buffer_index(unsigned int index)702*4882a593Smuzhiyun static inline unsigned int ar_next_buffer_index(unsigned int index)
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun 	return (index + 1) % AR_BUFFERS;
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun 
ar_first_buffer_index(struct ar_context * ctx)707*4882a593Smuzhiyun static inline unsigned int ar_first_buffer_index(struct ar_context *ctx)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	return ar_next_buffer_index(ctx->last_buffer_index);
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun /*
713*4882a593Smuzhiyun  * We search for the buffer that contains the last AR packet DMA data written
714*4882a593Smuzhiyun  * by the controller.
715*4882a593Smuzhiyun  */
ar_search_last_active_buffer(struct ar_context * ctx,unsigned int * buffer_offset)716*4882a593Smuzhiyun static unsigned int ar_search_last_active_buffer(struct ar_context *ctx,
717*4882a593Smuzhiyun 						 unsigned int *buffer_offset)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	unsigned int i, next_i, last = ctx->last_buffer_index;
720*4882a593Smuzhiyun 	__le16 res_count, next_res_count;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	i = ar_first_buffer_index(ctx);
723*4882a593Smuzhiyun 	res_count = READ_ONCE(ctx->descriptors[i].res_count);
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	/* A buffer that is not yet completely filled must be the last one. */
726*4882a593Smuzhiyun 	while (i != last && res_count == 0) {
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 		/* Peek at the next descriptor. */
729*4882a593Smuzhiyun 		next_i = ar_next_buffer_index(i);
730*4882a593Smuzhiyun 		rmb(); /* read descriptors in order */
731*4882a593Smuzhiyun 		next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count);
732*4882a593Smuzhiyun 		/*
733*4882a593Smuzhiyun 		 * If the next descriptor is still empty, we must stop at this
734*4882a593Smuzhiyun 		 * descriptor.
735*4882a593Smuzhiyun 		 */
736*4882a593Smuzhiyun 		if (next_res_count == cpu_to_le16(PAGE_SIZE)) {
737*4882a593Smuzhiyun 			/*
738*4882a593Smuzhiyun 			 * The exception is when the DMA data for one packet is
739*4882a593Smuzhiyun 			 * split over three buffers; in this case, the middle
740*4882a593Smuzhiyun 			 * buffer's descriptor might be never updated by the
741*4882a593Smuzhiyun 			 * controller and look still empty, and we have to peek
742*4882a593Smuzhiyun 			 * at the third one.
743*4882a593Smuzhiyun 			 */
744*4882a593Smuzhiyun 			if (MAX_AR_PACKET_SIZE > PAGE_SIZE && i != last) {
745*4882a593Smuzhiyun 				next_i = ar_next_buffer_index(next_i);
746*4882a593Smuzhiyun 				rmb();
747*4882a593Smuzhiyun 				next_res_count = READ_ONCE(ctx->descriptors[next_i].res_count);
748*4882a593Smuzhiyun 				if (next_res_count != cpu_to_le16(PAGE_SIZE))
749*4882a593Smuzhiyun 					goto next_buffer_is_active;
750*4882a593Smuzhiyun 			}
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 			break;
753*4882a593Smuzhiyun 		}
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun next_buffer_is_active:
756*4882a593Smuzhiyun 		i = next_i;
757*4882a593Smuzhiyun 		res_count = next_res_count;
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	rmb(); /* read res_count before the DMA data */
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	*buffer_offset = PAGE_SIZE - le16_to_cpu(res_count);
763*4882a593Smuzhiyun 	if (*buffer_offset > PAGE_SIZE) {
764*4882a593Smuzhiyun 		*buffer_offset = 0;
765*4882a593Smuzhiyun 		ar_context_abort(ctx, "corrupted descriptor");
766*4882a593Smuzhiyun 	}
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	return i;
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun 
ar_sync_buffers_for_cpu(struct ar_context * ctx,unsigned int end_buffer_index,unsigned int end_buffer_offset)771*4882a593Smuzhiyun static void ar_sync_buffers_for_cpu(struct ar_context *ctx,
772*4882a593Smuzhiyun 				    unsigned int end_buffer_index,
773*4882a593Smuzhiyun 				    unsigned int end_buffer_offset)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun 	unsigned int i;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	i = ar_first_buffer_index(ctx);
778*4882a593Smuzhiyun 	while (i != end_buffer_index) {
779*4882a593Smuzhiyun 		dma_sync_single_for_cpu(ctx->ohci->card.device,
780*4882a593Smuzhiyun 					ar_buffer_bus(ctx, i),
781*4882a593Smuzhiyun 					PAGE_SIZE, DMA_FROM_DEVICE);
782*4882a593Smuzhiyun 		i = ar_next_buffer_index(i);
783*4882a593Smuzhiyun 	}
784*4882a593Smuzhiyun 	if (end_buffer_offset > 0)
785*4882a593Smuzhiyun 		dma_sync_single_for_cpu(ctx->ohci->card.device,
786*4882a593Smuzhiyun 					ar_buffer_bus(ctx, i),
787*4882a593Smuzhiyun 					end_buffer_offset, DMA_FROM_DEVICE);
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
791*4882a593Smuzhiyun #define cond_le32_to_cpu(v) \
792*4882a593Smuzhiyun 	(ohci->quirks & QUIRK_BE_HEADERS ? (__force __u32)(v) : le32_to_cpu(v))
793*4882a593Smuzhiyun #else
794*4882a593Smuzhiyun #define cond_le32_to_cpu(v) le32_to_cpu(v)
795*4882a593Smuzhiyun #endif
796*4882a593Smuzhiyun 
handle_ar_packet(struct ar_context * ctx,__le32 * buffer)797*4882a593Smuzhiyun static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
798*4882a593Smuzhiyun {
799*4882a593Smuzhiyun 	struct fw_ohci *ohci = ctx->ohci;
800*4882a593Smuzhiyun 	struct fw_packet p;
801*4882a593Smuzhiyun 	u32 status, length, tcode;
802*4882a593Smuzhiyun 	int evt;
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	p.header[0] = cond_le32_to_cpu(buffer[0]);
805*4882a593Smuzhiyun 	p.header[1] = cond_le32_to_cpu(buffer[1]);
806*4882a593Smuzhiyun 	p.header[2] = cond_le32_to_cpu(buffer[2]);
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	tcode = (p.header[0] >> 4) & 0x0f;
809*4882a593Smuzhiyun 	switch (tcode) {
810*4882a593Smuzhiyun 	case TCODE_WRITE_QUADLET_REQUEST:
811*4882a593Smuzhiyun 	case TCODE_READ_QUADLET_RESPONSE:
812*4882a593Smuzhiyun 		p.header[3] = (__force __u32) buffer[3];
813*4882a593Smuzhiyun 		p.header_length = 16;
814*4882a593Smuzhiyun 		p.payload_length = 0;
815*4882a593Smuzhiyun 		break;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	case TCODE_READ_BLOCK_REQUEST :
818*4882a593Smuzhiyun 		p.header[3] = cond_le32_to_cpu(buffer[3]);
819*4882a593Smuzhiyun 		p.header_length = 16;
820*4882a593Smuzhiyun 		p.payload_length = 0;
821*4882a593Smuzhiyun 		break;
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	case TCODE_WRITE_BLOCK_REQUEST:
824*4882a593Smuzhiyun 	case TCODE_READ_BLOCK_RESPONSE:
825*4882a593Smuzhiyun 	case TCODE_LOCK_REQUEST:
826*4882a593Smuzhiyun 	case TCODE_LOCK_RESPONSE:
827*4882a593Smuzhiyun 		p.header[3] = cond_le32_to_cpu(buffer[3]);
828*4882a593Smuzhiyun 		p.header_length = 16;
829*4882a593Smuzhiyun 		p.payload_length = p.header[3] >> 16;
830*4882a593Smuzhiyun 		if (p.payload_length > MAX_ASYNC_PAYLOAD) {
831*4882a593Smuzhiyun 			ar_context_abort(ctx, "invalid packet length");
832*4882a593Smuzhiyun 			return NULL;
833*4882a593Smuzhiyun 		}
834*4882a593Smuzhiyun 		break;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	case TCODE_WRITE_RESPONSE:
837*4882a593Smuzhiyun 	case TCODE_READ_QUADLET_REQUEST:
838*4882a593Smuzhiyun 	case OHCI_TCODE_PHY_PACKET:
839*4882a593Smuzhiyun 		p.header_length = 12;
840*4882a593Smuzhiyun 		p.payload_length = 0;
841*4882a593Smuzhiyun 		break;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	default:
844*4882a593Smuzhiyun 		ar_context_abort(ctx, "invalid tcode");
845*4882a593Smuzhiyun 		return NULL;
846*4882a593Smuzhiyun 	}
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	p.payload = (void *) buffer + p.header_length;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	/* FIXME: What to do about evt_* errors? */
851*4882a593Smuzhiyun 	length = (p.header_length + p.payload_length + 3) / 4;
852*4882a593Smuzhiyun 	status = cond_le32_to_cpu(buffer[length]);
853*4882a593Smuzhiyun 	evt    = (status >> 16) & 0x1f;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	p.ack        = evt - 16;
856*4882a593Smuzhiyun 	p.speed      = (status >> 21) & 0x7;
857*4882a593Smuzhiyun 	p.timestamp  = status & 0xffff;
858*4882a593Smuzhiyun 	p.generation = ohci->request_generation;
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	log_ar_at_event(ohci, 'R', p.speed, p.header, evt);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	/*
863*4882a593Smuzhiyun 	 * Several controllers, notably from NEC and VIA, forget to
864*4882a593Smuzhiyun 	 * write ack_complete status at PHY packet reception.
865*4882a593Smuzhiyun 	 */
866*4882a593Smuzhiyun 	if (evt == OHCI1394_evt_no_status &&
867*4882a593Smuzhiyun 	    (p.header[0] & 0xff) == (OHCI1394_phy_tcode << 4))
868*4882a593Smuzhiyun 		p.ack = ACK_COMPLETE;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	/*
871*4882a593Smuzhiyun 	 * The OHCI bus reset handler synthesizes a PHY packet with
872*4882a593Smuzhiyun 	 * the new generation number when a bus reset happens (see
873*4882a593Smuzhiyun 	 * section 8.4.2.3).  This helps us determine when a request
874*4882a593Smuzhiyun 	 * was received and make sure we send the response in the same
875*4882a593Smuzhiyun 	 * generation.  We only need this for requests; for responses
876*4882a593Smuzhiyun 	 * we use the unique tlabel for finding the matching
877*4882a593Smuzhiyun 	 * request.
878*4882a593Smuzhiyun 	 *
879*4882a593Smuzhiyun 	 * Alas some chips sometimes emit bus reset packets with a
880*4882a593Smuzhiyun 	 * wrong generation.  We set the correct generation for these
881*4882a593Smuzhiyun 	 * at a slightly incorrect time (in bus_reset_work).
882*4882a593Smuzhiyun 	 */
883*4882a593Smuzhiyun 	if (evt == OHCI1394_evt_bus_reset) {
884*4882a593Smuzhiyun 		if (!(ohci->quirks & QUIRK_RESET_PACKET))
885*4882a593Smuzhiyun 			ohci->request_generation = (p.header[2] >> 16) & 0xff;
886*4882a593Smuzhiyun 	} else if (ctx == &ohci->ar_request_ctx) {
887*4882a593Smuzhiyun 		fw_core_handle_request(&ohci->card, &p);
888*4882a593Smuzhiyun 	} else {
889*4882a593Smuzhiyun 		fw_core_handle_response(&ohci->card, &p);
890*4882a593Smuzhiyun 	}
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	return buffer + length + 1;
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun 
handle_ar_packets(struct ar_context * ctx,void * p,void * end)895*4882a593Smuzhiyun static void *handle_ar_packets(struct ar_context *ctx, void *p, void *end)
896*4882a593Smuzhiyun {
897*4882a593Smuzhiyun 	void *next;
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	while (p < end) {
900*4882a593Smuzhiyun 		next = handle_ar_packet(ctx, p);
901*4882a593Smuzhiyun 		if (!next)
902*4882a593Smuzhiyun 			return p;
903*4882a593Smuzhiyun 		p = next;
904*4882a593Smuzhiyun 	}
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 	return p;
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun 
ar_recycle_buffers(struct ar_context * ctx,unsigned int end_buffer)909*4882a593Smuzhiyun static void ar_recycle_buffers(struct ar_context *ctx, unsigned int end_buffer)
910*4882a593Smuzhiyun {
911*4882a593Smuzhiyun 	unsigned int i;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	i = ar_first_buffer_index(ctx);
914*4882a593Smuzhiyun 	while (i != end_buffer) {
915*4882a593Smuzhiyun 		dma_sync_single_for_device(ctx->ohci->card.device,
916*4882a593Smuzhiyun 					   ar_buffer_bus(ctx, i),
917*4882a593Smuzhiyun 					   PAGE_SIZE, DMA_FROM_DEVICE);
918*4882a593Smuzhiyun 		ar_context_link_page(ctx, i);
919*4882a593Smuzhiyun 		i = ar_next_buffer_index(i);
920*4882a593Smuzhiyun 	}
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun 
ar_context_tasklet(unsigned long data)923*4882a593Smuzhiyun static void ar_context_tasklet(unsigned long data)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun 	struct ar_context *ctx = (struct ar_context *)data;
926*4882a593Smuzhiyun 	unsigned int end_buffer_index, end_buffer_offset;
927*4882a593Smuzhiyun 	void *p, *end;
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	p = ctx->pointer;
930*4882a593Smuzhiyun 	if (!p)
931*4882a593Smuzhiyun 		return;
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	end_buffer_index = ar_search_last_active_buffer(ctx,
934*4882a593Smuzhiyun 							&end_buffer_offset);
935*4882a593Smuzhiyun 	ar_sync_buffers_for_cpu(ctx, end_buffer_index, end_buffer_offset);
936*4882a593Smuzhiyun 	end = ctx->buffer + end_buffer_index * PAGE_SIZE + end_buffer_offset;
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	if (end_buffer_index < ar_first_buffer_index(ctx)) {
939*4882a593Smuzhiyun 		/*
940*4882a593Smuzhiyun 		 * The filled part of the overall buffer wraps around; handle
941*4882a593Smuzhiyun 		 * all packets up to the buffer end here.  If the last packet
942*4882a593Smuzhiyun 		 * wraps around, its tail will be visible after the buffer end
943*4882a593Smuzhiyun 		 * because the buffer start pages are mapped there again.
944*4882a593Smuzhiyun 		 */
945*4882a593Smuzhiyun 		void *buffer_end = ctx->buffer + AR_BUFFERS * PAGE_SIZE;
946*4882a593Smuzhiyun 		p = handle_ar_packets(ctx, p, buffer_end);
947*4882a593Smuzhiyun 		if (p < buffer_end)
948*4882a593Smuzhiyun 			goto error;
949*4882a593Smuzhiyun 		/* adjust p to point back into the actual buffer */
950*4882a593Smuzhiyun 		p -= AR_BUFFERS * PAGE_SIZE;
951*4882a593Smuzhiyun 	}
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	p = handle_ar_packets(ctx, p, end);
954*4882a593Smuzhiyun 	if (p != end) {
955*4882a593Smuzhiyun 		if (p > end)
956*4882a593Smuzhiyun 			ar_context_abort(ctx, "inconsistent descriptor");
957*4882a593Smuzhiyun 		goto error;
958*4882a593Smuzhiyun 	}
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	ctx->pointer = p;
961*4882a593Smuzhiyun 	ar_recycle_buffers(ctx, end_buffer_index);
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	return;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun error:
966*4882a593Smuzhiyun 	ctx->pointer = NULL;
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun 
ar_context_init(struct ar_context * ctx,struct fw_ohci * ohci,unsigned int descriptors_offset,u32 regs)969*4882a593Smuzhiyun static int ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci,
970*4882a593Smuzhiyun 			   unsigned int descriptors_offset, u32 regs)
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun 	struct device *dev = ohci->card.device;
973*4882a593Smuzhiyun 	unsigned int i;
974*4882a593Smuzhiyun 	dma_addr_t dma_addr;
975*4882a593Smuzhiyun 	struct page *pages[AR_BUFFERS + AR_WRAPAROUND_PAGES];
976*4882a593Smuzhiyun 	struct descriptor *d;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	ctx->regs        = regs;
979*4882a593Smuzhiyun 	ctx->ohci        = ohci;
980*4882a593Smuzhiyun 	tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	for (i = 0; i < AR_BUFFERS; i++) {
983*4882a593Smuzhiyun 		ctx->pages[i] = dma_alloc_pages(dev, PAGE_SIZE, &dma_addr,
984*4882a593Smuzhiyun 						DMA_FROM_DEVICE, GFP_KERNEL);
985*4882a593Smuzhiyun 		if (!ctx->pages[i])
986*4882a593Smuzhiyun 			goto out_of_memory;
987*4882a593Smuzhiyun 		set_page_private(ctx->pages[i], dma_addr);
988*4882a593Smuzhiyun 		dma_sync_single_for_device(dev, dma_addr, PAGE_SIZE,
989*4882a593Smuzhiyun 					   DMA_FROM_DEVICE);
990*4882a593Smuzhiyun 	}
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	for (i = 0; i < AR_BUFFERS; i++)
993*4882a593Smuzhiyun 		pages[i]              = ctx->pages[i];
994*4882a593Smuzhiyun 	for (i = 0; i < AR_WRAPAROUND_PAGES; i++)
995*4882a593Smuzhiyun 		pages[AR_BUFFERS + i] = ctx->pages[i];
996*4882a593Smuzhiyun 	ctx->buffer = vmap(pages, ARRAY_SIZE(pages), VM_MAP, PAGE_KERNEL);
997*4882a593Smuzhiyun 	if (!ctx->buffer)
998*4882a593Smuzhiyun 		goto out_of_memory;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	ctx->descriptors     = ohci->misc_buffer     + descriptors_offset;
1001*4882a593Smuzhiyun 	ctx->descriptors_bus = ohci->misc_buffer_bus + descriptors_offset;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	for (i = 0; i < AR_BUFFERS; i++) {
1004*4882a593Smuzhiyun 		d = &ctx->descriptors[i];
1005*4882a593Smuzhiyun 		d->req_count      = cpu_to_le16(PAGE_SIZE);
1006*4882a593Smuzhiyun 		d->control        = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
1007*4882a593Smuzhiyun 						DESCRIPTOR_STATUS |
1008*4882a593Smuzhiyun 						DESCRIPTOR_BRANCH_ALWAYS);
1009*4882a593Smuzhiyun 		d->data_address   = cpu_to_le32(ar_buffer_bus(ctx, i));
1010*4882a593Smuzhiyun 		d->branch_address = cpu_to_le32(ctx->descriptors_bus +
1011*4882a593Smuzhiyun 			ar_next_buffer_index(i) * sizeof(struct descriptor));
1012*4882a593Smuzhiyun 	}
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	return 0;
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun out_of_memory:
1017*4882a593Smuzhiyun 	ar_context_release(ctx);
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	return -ENOMEM;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun 
ar_context_run(struct ar_context * ctx)1022*4882a593Smuzhiyun static void ar_context_run(struct ar_context *ctx)
1023*4882a593Smuzhiyun {
1024*4882a593Smuzhiyun 	unsigned int i;
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun 	for (i = 0; i < AR_BUFFERS; i++)
1027*4882a593Smuzhiyun 		ar_context_link_page(ctx, i);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	ctx->pointer = ctx->buffer;
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ctx->descriptors_bus | 1);
1032*4882a593Smuzhiyun 	reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
1033*4882a593Smuzhiyun }
1034*4882a593Smuzhiyun 
find_branch_descriptor(struct descriptor * d,int z)1035*4882a593Smuzhiyun static struct descriptor *find_branch_descriptor(struct descriptor *d, int z)
1036*4882a593Smuzhiyun {
1037*4882a593Smuzhiyun 	__le16 branch;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	branch = d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS);
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	/* figure out which descriptor the branch address goes in */
1042*4882a593Smuzhiyun 	if (z == 2 && branch == cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
1043*4882a593Smuzhiyun 		return d;
1044*4882a593Smuzhiyun 	else
1045*4882a593Smuzhiyun 		return d + z - 1;
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun 
context_tasklet(unsigned long data)1048*4882a593Smuzhiyun static void context_tasklet(unsigned long data)
1049*4882a593Smuzhiyun {
1050*4882a593Smuzhiyun 	struct context *ctx = (struct context *) data;
1051*4882a593Smuzhiyun 	struct descriptor *d, *last;
1052*4882a593Smuzhiyun 	u32 address;
1053*4882a593Smuzhiyun 	int z;
1054*4882a593Smuzhiyun 	struct descriptor_buffer *desc;
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	desc = list_entry(ctx->buffer_list.next,
1057*4882a593Smuzhiyun 			struct descriptor_buffer, list);
1058*4882a593Smuzhiyun 	last = ctx->last;
1059*4882a593Smuzhiyun 	while (last->branch_address != 0) {
1060*4882a593Smuzhiyun 		struct descriptor_buffer *old_desc = desc;
1061*4882a593Smuzhiyun 		address = le32_to_cpu(last->branch_address);
1062*4882a593Smuzhiyun 		z = address & 0xf;
1063*4882a593Smuzhiyun 		address &= ~0xf;
1064*4882a593Smuzhiyun 		ctx->current_bus = address;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 		/* If the branch address points to a buffer outside of the
1067*4882a593Smuzhiyun 		 * current buffer, advance to the next buffer. */
1068*4882a593Smuzhiyun 		if (address < desc->buffer_bus ||
1069*4882a593Smuzhiyun 				address >= desc->buffer_bus + desc->used)
1070*4882a593Smuzhiyun 			desc = list_entry(desc->list.next,
1071*4882a593Smuzhiyun 					struct descriptor_buffer, list);
1072*4882a593Smuzhiyun 		d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
1073*4882a593Smuzhiyun 		last = find_branch_descriptor(d, z);
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 		if (!ctx->callback(ctx, d, last))
1076*4882a593Smuzhiyun 			break;
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 		if (old_desc != desc) {
1079*4882a593Smuzhiyun 			/* If we've advanced to the next buffer, move the
1080*4882a593Smuzhiyun 			 * previous buffer to the free list. */
1081*4882a593Smuzhiyun 			unsigned long flags;
1082*4882a593Smuzhiyun 			old_desc->used = 0;
1083*4882a593Smuzhiyun 			spin_lock_irqsave(&ctx->ohci->lock, flags);
1084*4882a593Smuzhiyun 			list_move_tail(&old_desc->list, &ctx->buffer_list);
1085*4882a593Smuzhiyun 			spin_unlock_irqrestore(&ctx->ohci->lock, flags);
1086*4882a593Smuzhiyun 		}
1087*4882a593Smuzhiyun 		ctx->last = last;
1088*4882a593Smuzhiyun 	}
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun /*
1092*4882a593Smuzhiyun  * Allocate a new buffer and add it to the list of free buffers for this
1093*4882a593Smuzhiyun  * context.  Must be called with ohci->lock held.
1094*4882a593Smuzhiyun  */
context_add_buffer(struct context * ctx)1095*4882a593Smuzhiyun static int context_add_buffer(struct context *ctx)
1096*4882a593Smuzhiyun {
1097*4882a593Smuzhiyun 	struct descriptor_buffer *desc;
1098*4882a593Smuzhiyun 	dma_addr_t bus_addr;
1099*4882a593Smuzhiyun 	int offset;
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	/*
1102*4882a593Smuzhiyun 	 * 16MB of descriptors should be far more than enough for any DMA
1103*4882a593Smuzhiyun 	 * program.  This will catch run-away userspace or DoS attacks.
1104*4882a593Smuzhiyun 	 */
1105*4882a593Smuzhiyun 	if (ctx->total_allocation >= 16*1024*1024)
1106*4882a593Smuzhiyun 		return -ENOMEM;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	desc = dma_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE,
1109*4882a593Smuzhiyun 			&bus_addr, GFP_ATOMIC);
1110*4882a593Smuzhiyun 	if (!desc)
1111*4882a593Smuzhiyun 		return -ENOMEM;
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	offset = (void *)&desc->buffer - (void *)desc;
1114*4882a593Smuzhiyun 	/*
1115*4882a593Smuzhiyun 	 * Some controllers, like JMicron ones, always issue 0x20-byte DMA reads
1116*4882a593Smuzhiyun 	 * for descriptors, even 0x10-byte ones. This can cause page faults when
1117*4882a593Smuzhiyun 	 * an IOMMU is in use and the oversized read crosses a page boundary.
1118*4882a593Smuzhiyun 	 * Work around this by always leaving at least 0x10 bytes of padding.
1119*4882a593Smuzhiyun 	 */
1120*4882a593Smuzhiyun 	desc->buffer_size = PAGE_SIZE - offset - 0x10;
1121*4882a593Smuzhiyun 	desc->buffer_bus = bus_addr + offset;
1122*4882a593Smuzhiyun 	desc->used = 0;
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	list_add_tail(&desc->list, &ctx->buffer_list);
1125*4882a593Smuzhiyun 	ctx->total_allocation += PAGE_SIZE;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	return 0;
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun 
context_init(struct context * ctx,struct fw_ohci * ohci,u32 regs,descriptor_callback_t callback)1130*4882a593Smuzhiyun static int context_init(struct context *ctx, struct fw_ohci *ohci,
1131*4882a593Smuzhiyun 			u32 regs, descriptor_callback_t callback)
1132*4882a593Smuzhiyun {
1133*4882a593Smuzhiyun 	ctx->ohci = ohci;
1134*4882a593Smuzhiyun 	ctx->regs = regs;
1135*4882a593Smuzhiyun 	ctx->total_allocation = 0;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	INIT_LIST_HEAD(&ctx->buffer_list);
1138*4882a593Smuzhiyun 	if (context_add_buffer(ctx) < 0)
1139*4882a593Smuzhiyun 		return -ENOMEM;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	ctx->buffer_tail = list_entry(ctx->buffer_list.next,
1142*4882a593Smuzhiyun 			struct descriptor_buffer, list);
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
1145*4882a593Smuzhiyun 	ctx->callback = callback;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 	/*
1148*4882a593Smuzhiyun 	 * We put a dummy descriptor in the buffer that has a NULL
1149*4882a593Smuzhiyun 	 * branch address and looks like it's been sent.  That way we
1150*4882a593Smuzhiyun 	 * have a descriptor to append DMA programs to.
1151*4882a593Smuzhiyun 	 */
1152*4882a593Smuzhiyun 	memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
1153*4882a593Smuzhiyun 	ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
1154*4882a593Smuzhiyun 	ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
1155*4882a593Smuzhiyun 	ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
1156*4882a593Smuzhiyun 	ctx->last = ctx->buffer_tail->buffer;
1157*4882a593Smuzhiyun 	ctx->prev = ctx->buffer_tail->buffer;
1158*4882a593Smuzhiyun 	ctx->prev_z = 1;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	return 0;
1161*4882a593Smuzhiyun }
1162*4882a593Smuzhiyun 
context_release(struct context * ctx)1163*4882a593Smuzhiyun static void context_release(struct context *ctx)
1164*4882a593Smuzhiyun {
1165*4882a593Smuzhiyun 	struct fw_card *card = &ctx->ohci->card;
1166*4882a593Smuzhiyun 	struct descriptor_buffer *desc, *tmp;
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list)
1169*4882a593Smuzhiyun 		dma_free_coherent(card->device, PAGE_SIZE, desc,
1170*4882a593Smuzhiyun 			desc->buffer_bus -
1171*4882a593Smuzhiyun 			((void *)&desc->buffer - (void *)desc));
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun /* Must be called with ohci->lock held */
context_get_descriptors(struct context * ctx,int z,dma_addr_t * d_bus)1175*4882a593Smuzhiyun static struct descriptor *context_get_descriptors(struct context *ctx,
1176*4882a593Smuzhiyun 						  int z, dma_addr_t *d_bus)
1177*4882a593Smuzhiyun {
1178*4882a593Smuzhiyun 	struct descriptor *d = NULL;
1179*4882a593Smuzhiyun 	struct descriptor_buffer *desc = ctx->buffer_tail;
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 	if (z * sizeof(*d) > desc->buffer_size)
1182*4882a593Smuzhiyun 		return NULL;
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	if (z * sizeof(*d) > desc->buffer_size - desc->used) {
1185*4882a593Smuzhiyun 		/* No room for the descriptor in this buffer, so advance to the
1186*4882a593Smuzhiyun 		 * next one. */
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 		if (desc->list.next == &ctx->buffer_list) {
1189*4882a593Smuzhiyun 			/* If there is no free buffer next in the list,
1190*4882a593Smuzhiyun 			 * allocate one. */
1191*4882a593Smuzhiyun 			if (context_add_buffer(ctx) < 0)
1192*4882a593Smuzhiyun 				return NULL;
1193*4882a593Smuzhiyun 		}
1194*4882a593Smuzhiyun 		desc = list_entry(desc->list.next,
1195*4882a593Smuzhiyun 				struct descriptor_buffer, list);
1196*4882a593Smuzhiyun 		ctx->buffer_tail = desc;
1197*4882a593Smuzhiyun 	}
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	d = desc->buffer + desc->used / sizeof(*d);
1200*4882a593Smuzhiyun 	memset(d, 0, z * sizeof(*d));
1201*4882a593Smuzhiyun 	*d_bus = desc->buffer_bus + desc->used;
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	return d;
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun 
context_run(struct context * ctx,u32 extra)1206*4882a593Smuzhiyun static void context_run(struct context *ctx, u32 extra)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun 	struct fw_ohci *ohci = ctx->ohci;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	reg_write(ohci, COMMAND_PTR(ctx->regs),
1211*4882a593Smuzhiyun 		  le32_to_cpu(ctx->last->branch_address));
1212*4882a593Smuzhiyun 	reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
1213*4882a593Smuzhiyun 	reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
1214*4882a593Smuzhiyun 	ctx->running = true;
1215*4882a593Smuzhiyun 	flush_writes(ohci);
1216*4882a593Smuzhiyun }
1217*4882a593Smuzhiyun 
context_append(struct context * ctx,struct descriptor * d,int z,int extra)1218*4882a593Smuzhiyun static void context_append(struct context *ctx,
1219*4882a593Smuzhiyun 			   struct descriptor *d, int z, int extra)
1220*4882a593Smuzhiyun {
1221*4882a593Smuzhiyun 	dma_addr_t d_bus;
1222*4882a593Smuzhiyun 	struct descriptor_buffer *desc = ctx->buffer_tail;
1223*4882a593Smuzhiyun 	struct descriptor *d_branch;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d);
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun 	desc->used += (z + extra) * sizeof(*d);
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 	wmb(); /* finish init of new descriptors before branch_address update */
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 	d_branch = find_branch_descriptor(ctx->prev, ctx->prev_z);
1232*4882a593Smuzhiyun 	d_branch->branch_address = cpu_to_le32(d_bus | z);
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	/*
1235*4882a593Smuzhiyun 	 * VT6306 incorrectly checks only the single descriptor at the
1236*4882a593Smuzhiyun 	 * CommandPtr when the wake bit is written, so if it's a
1237*4882a593Smuzhiyun 	 * multi-descriptor block starting with an INPUT_MORE, put a copy of
1238*4882a593Smuzhiyun 	 * the branch address in the first descriptor.
1239*4882a593Smuzhiyun 	 *
1240*4882a593Smuzhiyun 	 * Not doing this for transmit contexts since not sure how it interacts
1241*4882a593Smuzhiyun 	 * with skip addresses.
1242*4882a593Smuzhiyun 	 */
1243*4882a593Smuzhiyun 	if (unlikely(ctx->ohci->quirks & QUIRK_IR_WAKE) &&
1244*4882a593Smuzhiyun 	    d_branch != ctx->prev &&
1245*4882a593Smuzhiyun 	    (ctx->prev->control & cpu_to_le16(DESCRIPTOR_CMD)) ==
1246*4882a593Smuzhiyun 	     cpu_to_le16(DESCRIPTOR_INPUT_MORE)) {
1247*4882a593Smuzhiyun 		ctx->prev->branch_address = cpu_to_le32(d_bus | z);
1248*4882a593Smuzhiyun 	}
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 	ctx->prev = d;
1251*4882a593Smuzhiyun 	ctx->prev_z = z;
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun 
context_stop(struct context * ctx)1254*4882a593Smuzhiyun static void context_stop(struct context *ctx)
1255*4882a593Smuzhiyun {
1256*4882a593Smuzhiyun 	struct fw_ohci *ohci = ctx->ohci;
1257*4882a593Smuzhiyun 	u32 reg;
1258*4882a593Smuzhiyun 	int i;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	reg_write(ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
1261*4882a593Smuzhiyun 	ctx->running = false;
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 	for (i = 0; i < 1000; i++) {
1264*4882a593Smuzhiyun 		reg = reg_read(ohci, CONTROL_SET(ctx->regs));
1265*4882a593Smuzhiyun 		if ((reg & CONTEXT_ACTIVE) == 0)
1266*4882a593Smuzhiyun 			return;
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 		if (i)
1269*4882a593Smuzhiyun 			udelay(10);
1270*4882a593Smuzhiyun 	}
1271*4882a593Smuzhiyun 	ohci_err(ohci, "DMA context still active (0x%08x)\n", reg);
1272*4882a593Smuzhiyun }
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun struct driver_data {
1275*4882a593Smuzhiyun 	u8 inline_data[8];
1276*4882a593Smuzhiyun 	struct fw_packet *packet;
1277*4882a593Smuzhiyun };
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun /*
1280*4882a593Smuzhiyun  * This function apppends a packet to the DMA queue for transmission.
1281*4882a593Smuzhiyun  * Must always be called with the ochi->lock held to ensure proper
1282*4882a593Smuzhiyun  * generation handling and locking around packet queue manipulation.
1283*4882a593Smuzhiyun  */
at_context_queue_packet(struct context * ctx,struct fw_packet * packet)1284*4882a593Smuzhiyun static int at_context_queue_packet(struct context *ctx,
1285*4882a593Smuzhiyun 				   struct fw_packet *packet)
1286*4882a593Smuzhiyun {
1287*4882a593Smuzhiyun 	struct fw_ohci *ohci = ctx->ohci;
1288*4882a593Smuzhiyun 	dma_addr_t d_bus, payload_bus;
1289*4882a593Smuzhiyun 	struct driver_data *driver_data;
1290*4882a593Smuzhiyun 	struct descriptor *d, *last;
1291*4882a593Smuzhiyun 	__le32 *header;
1292*4882a593Smuzhiyun 	int z, tcode;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	d = context_get_descriptors(ctx, 4, &d_bus);
1295*4882a593Smuzhiyun 	if (d == NULL) {
1296*4882a593Smuzhiyun 		packet->ack = RCODE_SEND_ERROR;
1297*4882a593Smuzhiyun 		return -1;
1298*4882a593Smuzhiyun 	}
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	d[0].control   = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
1301*4882a593Smuzhiyun 	d[0].res_count = cpu_to_le16(packet->timestamp);
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 	/*
1304*4882a593Smuzhiyun 	 * The DMA format for asynchronous link packets is different
1305*4882a593Smuzhiyun 	 * from the IEEE1394 layout, so shift the fields around
1306*4882a593Smuzhiyun 	 * accordingly.
1307*4882a593Smuzhiyun 	 */
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	tcode = (packet->header[0] >> 4) & 0x0f;
1310*4882a593Smuzhiyun 	header = (__le32 *) &d[1];
1311*4882a593Smuzhiyun 	switch (tcode) {
1312*4882a593Smuzhiyun 	case TCODE_WRITE_QUADLET_REQUEST:
1313*4882a593Smuzhiyun 	case TCODE_WRITE_BLOCK_REQUEST:
1314*4882a593Smuzhiyun 	case TCODE_WRITE_RESPONSE:
1315*4882a593Smuzhiyun 	case TCODE_READ_QUADLET_REQUEST:
1316*4882a593Smuzhiyun 	case TCODE_READ_BLOCK_REQUEST:
1317*4882a593Smuzhiyun 	case TCODE_READ_QUADLET_RESPONSE:
1318*4882a593Smuzhiyun 	case TCODE_READ_BLOCK_RESPONSE:
1319*4882a593Smuzhiyun 	case TCODE_LOCK_REQUEST:
1320*4882a593Smuzhiyun 	case TCODE_LOCK_RESPONSE:
1321*4882a593Smuzhiyun 		header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
1322*4882a593Smuzhiyun 					(packet->speed << 16));
1323*4882a593Smuzhiyun 		header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
1324*4882a593Smuzhiyun 					(packet->header[0] & 0xffff0000));
1325*4882a593Smuzhiyun 		header[2] = cpu_to_le32(packet->header[2]);
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 		if (TCODE_IS_BLOCK_PACKET(tcode))
1328*4882a593Smuzhiyun 			header[3] = cpu_to_le32(packet->header[3]);
1329*4882a593Smuzhiyun 		else
1330*4882a593Smuzhiyun 			header[3] = (__force __le32) packet->header[3];
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 		d[0].req_count = cpu_to_le16(packet->header_length);
1333*4882a593Smuzhiyun 		break;
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	case TCODE_LINK_INTERNAL:
1336*4882a593Smuzhiyun 		header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
1337*4882a593Smuzhiyun 					(packet->speed << 16));
1338*4882a593Smuzhiyun 		header[1] = cpu_to_le32(packet->header[1]);
1339*4882a593Smuzhiyun 		header[2] = cpu_to_le32(packet->header[2]);
1340*4882a593Smuzhiyun 		d[0].req_count = cpu_to_le16(12);
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 		if (is_ping_packet(&packet->header[1]))
1343*4882a593Smuzhiyun 			d[0].control |= cpu_to_le16(DESCRIPTOR_PING);
1344*4882a593Smuzhiyun 		break;
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	case TCODE_STREAM_DATA:
1347*4882a593Smuzhiyun 		header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
1348*4882a593Smuzhiyun 					(packet->speed << 16));
1349*4882a593Smuzhiyun 		header[1] = cpu_to_le32(packet->header[0] & 0xffff0000);
1350*4882a593Smuzhiyun 		d[0].req_count = cpu_to_le16(8);
1351*4882a593Smuzhiyun 		break;
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	default:
1354*4882a593Smuzhiyun 		/* BUG(); */
1355*4882a593Smuzhiyun 		packet->ack = RCODE_SEND_ERROR;
1356*4882a593Smuzhiyun 		return -1;
1357*4882a593Smuzhiyun 	}
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(struct driver_data) > sizeof(struct descriptor));
1360*4882a593Smuzhiyun 	driver_data = (struct driver_data *) &d[3];
1361*4882a593Smuzhiyun 	driver_data->packet = packet;
1362*4882a593Smuzhiyun 	packet->driver_data = driver_data;
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 	if (packet->payload_length > 0) {
1365*4882a593Smuzhiyun 		if (packet->payload_length > sizeof(driver_data->inline_data)) {
1366*4882a593Smuzhiyun 			payload_bus = dma_map_single(ohci->card.device,
1367*4882a593Smuzhiyun 						     packet->payload,
1368*4882a593Smuzhiyun 						     packet->payload_length,
1369*4882a593Smuzhiyun 						     DMA_TO_DEVICE);
1370*4882a593Smuzhiyun 			if (dma_mapping_error(ohci->card.device, payload_bus)) {
1371*4882a593Smuzhiyun 				packet->ack = RCODE_SEND_ERROR;
1372*4882a593Smuzhiyun 				return -1;
1373*4882a593Smuzhiyun 			}
1374*4882a593Smuzhiyun 			packet->payload_bus	= payload_bus;
1375*4882a593Smuzhiyun 			packet->payload_mapped	= true;
1376*4882a593Smuzhiyun 		} else {
1377*4882a593Smuzhiyun 			memcpy(driver_data->inline_data, packet->payload,
1378*4882a593Smuzhiyun 			       packet->payload_length);
1379*4882a593Smuzhiyun 			payload_bus = d_bus + 3 * sizeof(*d);
1380*4882a593Smuzhiyun 		}
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 		d[2].req_count    = cpu_to_le16(packet->payload_length);
1383*4882a593Smuzhiyun 		d[2].data_address = cpu_to_le32(payload_bus);
1384*4882a593Smuzhiyun 		last = &d[2];
1385*4882a593Smuzhiyun 		z = 3;
1386*4882a593Smuzhiyun 	} else {
1387*4882a593Smuzhiyun 		last = &d[0];
1388*4882a593Smuzhiyun 		z = 2;
1389*4882a593Smuzhiyun 	}
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
1392*4882a593Smuzhiyun 				     DESCRIPTOR_IRQ_ALWAYS |
1393*4882a593Smuzhiyun 				     DESCRIPTOR_BRANCH_ALWAYS);
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun 	/* FIXME: Document how the locking works. */
1396*4882a593Smuzhiyun 	if (ohci->generation != packet->generation) {
1397*4882a593Smuzhiyun 		if (packet->payload_mapped)
1398*4882a593Smuzhiyun 			dma_unmap_single(ohci->card.device, payload_bus,
1399*4882a593Smuzhiyun 					 packet->payload_length, DMA_TO_DEVICE);
1400*4882a593Smuzhiyun 		packet->ack = RCODE_GENERATION;
1401*4882a593Smuzhiyun 		return -1;
1402*4882a593Smuzhiyun 	}
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	context_append(ctx, d, z, 4 - z);
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	if (ctx->running)
1407*4882a593Smuzhiyun 		reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
1408*4882a593Smuzhiyun 	else
1409*4882a593Smuzhiyun 		context_run(ctx, 0);
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	return 0;
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun 
at_context_flush(struct context * ctx)1414*4882a593Smuzhiyun static void at_context_flush(struct context *ctx)
1415*4882a593Smuzhiyun {
1416*4882a593Smuzhiyun 	tasklet_disable(&ctx->tasklet);
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 	ctx->flushing = true;
1419*4882a593Smuzhiyun 	context_tasklet((unsigned long)ctx);
1420*4882a593Smuzhiyun 	ctx->flushing = false;
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	tasklet_enable(&ctx->tasklet);
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun 
handle_at_packet(struct context * context,struct descriptor * d,struct descriptor * last)1425*4882a593Smuzhiyun static int handle_at_packet(struct context *context,
1426*4882a593Smuzhiyun 			    struct descriptor *d,
1427*4882a593Smuzhiyun 			    struct descriptor *last)
1428*4882a593Smuzhiyun {
1429*4882a593Smuzhiyun 	struct driver_data *driver_data;
1430*4882a593Smuzhiyun 	struct fw_packet *packet;
1431*4882a593Smuzhiyun 	struct fw_ohci *ohci = context->ohci;
1432*4882a593Smuzhiyun 	int evt;
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	if (last->transfer_status == 0 && !context->flushing)
1435*4882a593Smuzhiyun 		/* This descriptor isn't done yet, stop iteration. */
1436*4882a593Smuzhiyun 		return 0;
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	driver_data = (struct driver_data *) &d[3];
1439*4882a593Smuzhiyun 	packet = driver_data->packet;
1440*4882a593Smuzhiyun 	if (packet == NULL)
1441*4882a593Smuzhiyun 		/* This packet was cancelled, just continue. */
1442*4882a593Smuzhiyun 		return 1;
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	if (packet->payload_mapped)
1445*4882a593Smuzhiyun 		dma_unmap_single(ohci->card.device, packet->payload_bus,
1446*4882a593Smuzhiyun 				 packet->payload_length, DMA_TO_DEVICE);
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	evt = le16_to_cpu(last->transfer_status) & 0x1f;
1449*4882a593Smuzhiyun 	packet->timestamp = le16_to_cpu(last->res_count);
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun 	log_ar_at_event(ohci, 'T', packet->speed, packet->header, evt);
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	switch (evt) {
1454*4882a593Smuzhiyun 	case OHCI1394_evt_timeout:
1455*4882a593Smuzhiyun 		/* Async response transmit timed out. */
1456*4882a593Smuzhiyun 		packet->ack = RCODE_CANCELLED;
1457*4882a593Smuzhiyun 		break;
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 	case OHCI1394_evt_flushed:
1460*4882a593Smuzhiyun 		/*
1461*4882a593Smuzhiyun 		 * The packet was flushed should give same error as
1462*4882a593Smuzhiyun 		 * when we try to use a stale generation count.
1463*4882a593Smuzhiyun 		 */
1464*4882a593Smuzhiyun 		packet->ack = RCODE_GENERATION;
1465*4882a593Smuzhiyun 		break;
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	case OHCI1394_evt_missing_ack:
1468*4882a593Smuzhiyun 		if (context->flushing)
1469*4882a593Smuzhiyun 			packet->ack = RCODE_GENERATION;
1470*4882a593Smuzhiyun 		else {
1471*4882a593Smuzhiyun 			/*
1472*4882a593Smuzhiyun 			 * Using a valid (current) generation count, but the
1473*4882a593Smuzhiyun 			 * node is not on the bus or not sending acks.
1474*4882a593Smuzhiyun 			 */
1475*4882a593Smuzhiyun 			packet->ack = RCODE_NO_ACK;
1476*4882a593Smuzhiyun 		}
1477*4882a593Smuzhiyun 		break;
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 	case ACK_COMPLETE + 0x10:
1480*4882a593Smuzhiyun 	case ACK_PENDING + 0x10:
1481*4882a593Smuzhiyun 	case ACK_BUSY_X + 0x10:
1482*4882a593Smuzhiyun 	case ACK_BUSY_A + 0x10:
1483*4882a593Smuzhiyun 	case ACK_BUSY_B + 0x10:
1484*4882a593Smuzhiyun 	case ACK_DATA_ERROR + 0x10:
1485*4882a593Smuzhiyun 	case ACK_TYPE_ERROR + 0x10:
1486*4882a593Smuzhiyun 		packet->ack = evt - 0x10;
1487*4882a593Smuzhiyun 		break;
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 	case OHCI1394_evt_no_status:
1490*4882a593Smuzhiyun 		if (context->flushing) {
1491*4882a593Smuzhiyun 			packet->ack = RCODE_GENERATION;
1492*4882a593Smuzhiyun 			break;
1493*4882a593Smuzhiyun 		}
1494*4882a593Smuzhiyun 		fallthrough;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	default:
1497*4882a593Smuzhiyun 		packet->ack = RCODE_SEND_ERROR;
1498*4882a593Smuzhiyun 		break;
1499*4882a593Smuzhiyun 	}
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	packet->callback(packet, &ohci->card, packet->ack);
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	return 1;
1504*4882a593Smuzhiyun }
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun #define HEADER_GET_DESTINATION(q)	(((q) >> 16) & 0xffff)
1507*4882a593Smuzhiyun #define HEADER_GET_TCODE(q)		(((q) >> 4) & 0x0f)
1508*4882a593Smuzhiyun #define HEADER_GET_OFFSET_HIGH(q)	(((q) >> 0) & 0xffff)
1509*4882a593Smuzhiyun #define HEADER_GET_DATA_LENGTH(q)	(((q) >> 16) & 0xffff)
1510*4882a593Smuzhiyun #define HEADER_GET_EXTENDED_TCODE(q)	(((q) >> 0) & 0xffff)
1511*4882a593Smuzhiyun 
handle_local_rom(struct fw_ohci * ohci,struct fw_packet * packet,u32 csr)1512*4882a593Smuzhiyun static void handle_local_rom(struct fw_ohci *ohci,
1513*4882a593Smuzhiyun 			     struct fw_packet *packet, u32 csr)
1514*4882a593Smuzhiyun {
1515*4882a593Smuzhiyun 	struct fw_packet response;
1516*4882a593Smuzhiyun 	int tcode, length, i;
1517*4882a593Smuzhiyun 
1518*4882a593Smuzhiyun 	tcode = HEADER_GET_TCODE(packet->header[0]);
1519*4882a593Smuzhiyun 	if (TCODE_IS_BLOCK_PACKET(tcode))
1520*4882a593Smuzhiyun 		length = HEADER_GET_DATA_LENGTH(packet->header[3]);
1521*4882a593Smuzhiyun 	else
1522*4882a593Smuzhiyun 		length = 4;
1523*4882a593Smuzhiyun 
1524*4882a593Smuzhiyun 	i = csr - CSR_CONFIG_ROM;
1525*4882a593Smuzhiyun 	if (i + length > CONFIG_ROM_SIZE) {
1526*4882a593Smuzhiyun 		fw_fill_response(&response, packet->header,
1527*4882a593Smuzhiyun 				 RCODE_ADDRESS_ERROR, NULL, 0);
1528*4882a593Smuzhiyun 	} else if (!TCODE_IS_READ_REQUEST(tcode)) {
1529*4882a593Smuzhiyun 		fw_fill_response(&response, packet->header,
1530*4882a593Smuzhiyun 				 RCODE_TYPE_ERROR, NULL, 0);
1531*4882a593Smuzhiyun 	} else {
1532*4882a593Smuzhiyun 		fw_fill_response(&response, packet->header, RCODE_COMPLETE,
1533*4882a593Smuzhiyun 				 (void *) ohci->config_rom + i, length);
1534*4882a593Smuzhiyun 	}
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 	fw_core_handle_response(&ohci->card, &response);
1537*4882a593Smuzhiyun }
1538*4882a593Smuzhiyun 
handle_local_lock(struct fw_ohci * ohci,struct fw_packet * packet,u32 csr)1539*4882a593Smuzhiyun static void handle_local_lock(struct fw_ohci *ohci,
1540*4882a593Smuzhiyun 			      struct fw_packet *packet, u32 csr)
1541*4882a593Smuzhiyun {
1542*4882a593Smuzhiyun 	struct fw_packet response;
1543*4882a593Smuzhiyun 	int tcode, length, ext_tcode, sel, try;
1544*4882a593Smuzhiyun 	__be32 *payload, lock_old;
1545*4882a593Smuzhiyun 	u32 lock_arg, lock_data;
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	tcode = HEADER_GET_TCODE(packet->header[0]);
1548*4882a593Smuzhiyun 	length = HEADER_GET_DATA_LENGTH(packet->header[3]);
1549*4882a593Smuzhiyun 	payload = packet->payload;
1550*4882a593Smuzhiyun 	ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 	if (tcode == TCODE_LOCK_REQUEST &&
1553*4882a593Smuzhiyun 	    ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
1554*4882a593Smuzhiyun 		lock_arg = be32_to_cpu(payload[0]);
1555*4882a593Smuzhiyun 		lock_data = be32_to_cpu(payload[1]);
1556*4882a593Smuzhiyun 	} else if (tcode == TCODE_READ_QUADLET_REQUEST) {
1557*4882a593Smuzhiyun 		lock_arg = 0;
1558*4882a593Smuzhiyun 		lock_data = 0;
1559*4882a593Smuzhiyun 	} else {
1560*4882a593Smuzhiyun 		fw_fill_response(&response, packet->header,
1561*4882a593Smuzhiyun 				 RCODE_TYPE_ERROR, NULL, 0);
1562*4882a593Smuzhiyun 		goto out;
1563*4882a593Smuzhiyun 	}
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	sel = (csr - CSR_BUS_MANAGER_ID) / 4;
1566*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_CSRData, lock_data);
1567*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
1568*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_CSRControl, sel);
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	for (try = 0; try < 20; try++)
1571*4882a593Smuzhiyun 		if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000) {
1572*4882a593Smuzhiyun 			lock_old = cpu_to_be32(reg_read(ohci,
1573*4882a593Smuzhiyun 							OHCI1394_CSRData));
1574*4882a593Smuzhiyun 			fw_fill_response(&response, packet->header,
1575*4882a593Smuzhiyun 					 RCODE_COMPLETE,
1576*4882a593Smuzhiyun 					 &lock_old, sizeof(lock_old));
1577*4882a593Smuzhiyun 			goto out;
1578*4882a593Smuzhiyun 		}
1579*4882a593Smuzhiyun 
1580*4882a593Smuzhiyun 	ohci_err(ohci, "swap not done (CSR lock timeout)\n");
1581*4882a593Smuzhiyun 	fw_fill_response(&response, packet->header, RCODE_BUSY, NULL, 0);
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun  out:
1584*4882a593Smuzhiyun 	fw_core_handle_response(&ohci->card, &response);
1585*4882a593Smuzhiyun }
1586*4882a593Smuzhiyun 
handle_local_request(struct context * ctx,struct fw_packet * packet)1587*4882a593Smuzhiyun static void handle_local_request(struct context *ctx, struct fw_packet *packet)
1588*4882a593Smuzhiyun {
1589*4882a593Smuzhiyun 	u64 offset, csr;
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	if (ctx == &ctx->ohci->at_request_ctx) {
1592*4882a593Smuzhiyun 		packet->ack = ACK_PENDING;
1593*4882a593Smuzhiyun 		packet->callback(packet, &ctx->ohci->card, packet->ack);
1594*4882a593Smuzhiyun 	}
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	offset =
1597*4882a593Smuzhiyun 		((unsigned long long)
1598*4882a593Smuzhiyun 		 HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
1599*4882a593Smuzhiyun 		packet->header[2];
1600*4882a593Smuzhiyun 	csr = offset - CSR_REGISTER_BASE;
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	/* Handle config rom reads. */
1603*4882a593Smuzhiyun 	if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
1604*4882a593Smuzhiyun 		handle_local_rom(ctx->ohci, packet, csr);
1605*4882a593Smuzhiyun 	else switch (csr) {
1606*4882a593Smuzhiyun 	case CSR_BUS_MANAGER_ID:
1607*4882a593Smuzhiyun 	case CSR_BANDWIDTH_AVAILABLE:
1608*4882a593Smuzhiyun 	case CSR_CHANNELS_AVAILABLE_HI:
1609*4882a593Smuzhiyun 	case CSR_CHANNELS_AVAILABLE_LO:
1610*4882a593Smuzhiyun 		handle_local_lock(ctx->ohci, packet, csr);
1611*4882a593Smuzhiyun 		break;
1612*4882a593Smuzhiyun 	default:
1613*4882a593Smuzhiyun 		if (ctx == &ctx->ohci->at_request_ctx)
1614*4882a593Smuzhiyun 			fw_core_handle_request(&ctx->ohci->card, packet);
1615*4882a593Smuzhiyun 		else
1616*4882a593Smuzhiyun 			fw_core_handle_response(&ctx->ohci->card, packet);
1617*4882a593Smuzhiyun 		break;
1618*4882a593Smuzhiyun 	}
1619*4882a593Smuzhiyun 
1620*4882a593Smuzhiyun 	if (ctx == &ctx->ohci->at_response_ctx) {
1621*4882a593Smuzhiyun 		packet->ack = ACK_COMPLETE;
1622*4882a593Smuzhiyun 		packet->callback(packet, &ctx->ohci->card, packet->ack);
1623*4882a593Smuzhiyun 	}
1624*4882a593Smuzhiyun }
1625*4882a593Smuzhiyun 
at_context_transmit(struct context * ctx,struct fw_packet * packet)1626*4882a593Smuzhiyun static void at_context_transmit(struct context *ctx, struct fw_packet *packet)
1627*4882a593Smuzhiyun {
1628*4882a593Smuzhiyun 	unsigned long flags;
1629*4882a593Smuzhiyun 	int ret;
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	spin_lock_irqsave(&ctx->ohci->lock, flags);
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun 	if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
1634*4882a593Smuzhiyun 	    ctx->ohci->generation == packet->generation) {
1635*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ctx->ohci->lock, flags);
1636*4882a593Smuzhiyun 		handle_local_request(ctx, packet);
1637*4882a593Smuzhiyun 		return;
1638*4882a593Smuzhiyun 	}
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 	ret = at_context_queue_packet(ctx, packet);
1641*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ctx->ohci->lock, flags);
1642*4882a593Smuzhiyun 
1643*4882a593Smuzhiyun 	if (ret < 0)
1644*4882a593Smuzhiyun 		packet->callback(packet, &ctx->ohci->card, packet->ack);
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun }
1647*4882a593Smuzhiyun 
detect_dead_context(struct fw_ohci * ohci,const char * name,unsigned int regs)1648*4882a593Smuzhiyun static void detect_dead_context(struct fw_ohci *ohci,
1649*4882a593Smuzhiyun 				const char *name, unsigned int regs)
1650*4882a593Smuzhiyun {
1651*4882a593Smuzhiyun 	u32 ctl;
1652*4882a593Smuzhiyun 
1653*4882a593Smuzhiyun 	ctl = reg_read(ohci, CONTROL_SET(regs));
1654*4882a593Smuzhiyun 	if (ctl & CONTEXT_DEAD)
1655*4882a593Smuzhiyun 		ohci_err(ohci, "DMA context %s has stopped, error code: %s\n",
1656*4882a593Smuzhiyun 			name, evts[ctl & 0x1f]);
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun 
handle_dead_contexts(struct fw_ohci * ohci)1659*4882a593Smuzhiyun static void handle_dead_contexts(struct fw_ohci *ohci)
1660*4882a593Smuzhiyun {
1661*4882a593Smuzhiyun 	unsigned int i;
1662*4882a593Smuzhiyun 	char name[8];
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	detect_dead_context(ohci, "ATReq", OHCI1394_AsReqTrContextBase);
1665*4882a593Smuzhiyun 	detect_dead_context(ohci, "ATRsp", OHCI1394_AsRspTrContextBase);
1666*4882a593Smuzhiyun 	detect_dead_context(ohci, "ARReq", OHCI1394_AsReqRcvContextBase);
1667*4882a593Smuzhiyun 	detect_dead_context(ohci, "ARRsp", OHCI1394_AsRspRcvContextBase);
1668*4882a593Smuzhiyun 	for (i = 0; i < 32; ++i) {
1669*4882a593Smuzhiyun 		if (!(ohci->it_context_support & (1 << i)))
1670*4882a593Smuzhiyun 			continue;
1671*4882a593Smuzhiyun 		sprintf(name, "IT%u", i);
1672*4882a593Smuzhiyun 		detect_dead_context(ohci, name, OHCI1394_IsoXmitContextBase(i));
1673*4882a593Smuzhiyun 	}
1674*4882a593Smuzhiyun 	for (i = 0; i < 32; ++i) {
1675*4882a593Smuzhiyun 		if (!(ohci->ir_context_support & (1 << i)))
1676*4882a593Smuzhiyun 			continue;
1677*4882a593Smuzhiyun 		sprintf(name, "IR%u", i);
1678*4882a593Smuzhiyun 		detect_dead_context(ohci, name, OHCI1394_IsoRcvContextBase(i));
1679*4882a593Smuzhiyun 	}
1680*4882a593Smuzhiyun 	/* TODO: maybe try to flush and restart the dead contexts */
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun 
cycle_timer_ticks(u32 cycle_timer)1683*4882a593Smuzhiyun static u32 cycle_timer_ticks(u32 cycle_timer)
1684*4882a593Smuzhiyun {
1685*4882a593Smuzhiyun 	u32 ticks;
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	ticks = cycle_timer & 0xfff;
1688*4882a593Smuzhiyun 	ticks += 3072 * ((cycle_timer >> 12) & 0x1fff);
1689*4882a593Smuzhiyun 	ticks += (3072 * 8000) * (cycle_timer >> 25);
1690*4882a593Smuzhiyun 
1691*4882a593Smuzhiyun 	return ticks;
1692*4882a593Smuzhiyun }
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun /*
1695*4882a593Smuzhiyun  * Some controllers exhibit one or more of the following bugs when updating the
1696*4882a593Smuzhiyun  * iso cycle timer register:
1697*4882a593Smuzhiyun  *  - When the lowest six bits are wrapping around to zero, a read that happens
1698*4882a593Smuzhiyun  *    at the same time will return garbage in the lowest ten bits.
1699*4882a593Smuzhiyun  *  - When the cycleOffset field wraps around to zero, the cycleCount field is
1700*4882a593Smuzhiyun  *    not incremented for about 60 ns.
1701*4882a593Smuzhiyun  *  - Occasionally, the entire register reads zero.
1702*4882a593Smuzhiyun  *
1703*4882a593Smuzhiyun  * To catch these, we read the register three times and ensure that the
1704*4882a593Smuzhiyun  * difference between each two consecutive reads is approximately the same, i.e.
1705*4882a593Smuzhiyun  * less than twice the other.  Furthermore, any negative difference indicates an
1706*4882a593Smuzhiyun  * error.  (A PCI read should take at least 20 ticks of the 24.576 MHz timer to
1707*4882a593Smuzhiyun  * execute, so we have enough precision to compute the ratio of the differences.)
1708*4882a593Smuzhiyun  */
get_cycle_time(struct fw_ohci * ohci)1709*4882a593Smuzhiyun static u32 get_cycle_time(struct fw_ohci *ohci)
1710*4882a593Smuzhiyun {
1711*4882a593Smuzhiyun 	u32 c0, c1, c2;
1712*4882a593Smuzhiyun 	u32 t0, t1, t2;
1713*4882a593Smuzhiyun 	s32 diff01, diff12;
1714*4882a593Smuzhiyun 	int i;
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 	if (ohci->quirks & QUIRK_CYCLE_TIMER) {
1719*4882a593Smuzhiyun 		i = 0;
1720*4882a593Smuzhiyun 		c1 = c2;
1721*4882a593Smuzhiyun 		c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1722*4882a593Smuzhiyun 		do {
1723*4882a593Smuzhiyun 			c0 = c1;
1724*4882a593Smuzhiyun 			c1 = c2;
1725*4882a593Smuzhiyun 			c2 = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1726*4882a593Smuzhiyun 			t0 = cycle_timer_ticks(c0);
1727*4882a593Smuzhiyun 			t1 = cycle_timer_ticks(c1);
1728*4882a593Smuzhiyun 			t2 = cycle_timer_ticks(c2);
1729*4882a593Smuzhiyun 			diff01 = t1 - t0;
1730*4882a593Smuzhiyun 			diff12 = t2 - t1;
1731*4882a593Smuzhiyun 		} while ((diff01 <= 0 || diff12 <= 0 ||
1732*4882a593Smuzhiyun 			  diff01 / diff12 >= 2 || diff12 / diff01 >= 2)
1733*4882a593Smuzhiyun 			 && i++ < 20);
1734*4882a593Smuzhiyun 	}
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	return c2;
1737*4882a593Smuzhiyun }
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun /*
1740*4882a593Smuzhiyun  * This function has to be called at least every 64 seconds.  The bus_time
1741*4882a593Smuzhiyun  * field stores not only the upper 25 bits of the BUS_TIME register but also
1742*4882a593Smuzhiyun  * the most significant bit of the cycle timer in bit 6 so that we can detect
1743*4882a593Smuzhiyun  * changes in this bit.
1744*4882a593Smuzhiyun  */
update_bus_time(struct fw_ohci * ohci)1745*4882a593Smuzhiyun static u32 update_bus_time(struct fw_ohci *ohci)
1746*4882a593Smuzhiyun {
1747*4882a593Smuzhiyun 	u32 cycle_time_seconds = get_cycle_time(ohci) >> 25;
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun 	if (unlikely(!ohci->bus_time_running)) {
1750*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_cycle64Seconds);
1751*4882a593Smuzhiyun 		ohci->bus_time = (lower_32_bits(ktime_get_seconds()) & ~0x7f) |
1752*4882a593Smuzhiyun 		                 (cycle_time_seconds & 0x40);
1753*4882a593Smuzhiyun 		ohci->bus_time_running = true;
1754*4882a593Smuzhiyun 	}
1755*4882a593Smuzhiyun 
1756*4882a593Smuzhiyun 	if ((ohci->bus_time & 0x40) != (cycle_time_seconds & 0x40))
1757*4882a593Smuzhiyun 		ohci->bus_time += 0x40;
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	return ohci->bus_time | cycle_time_seconds;
1760*4882a593Smuzhiyun }
1761*4882a593Smuzhiyun 
get_status_for_port(struct fw_ohci * ohci,int port_index)1762*4882a593Smuzhiyun static int get_status_for_port(struct fw_ohci *ohci, int port_index)
1763*4882a593Smuzhiyun {
1764*4882a593Smuzhiyun 	int reg;
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 	mutex_lock(&ohci->phy_reg_mutex);
1767*4882a593Smuzhiyun 	reg = write_phy_reg(ohci, 7, port_index);
1768*4882a593Smuzhiyun 	if (reg >= 0)
1769*4882a593Smuzhiyun 		reg = read_phy_reg(ohci, 8);
1770*4882a593Smuzhiyun 	mutex_unlock(&ohci->phy_reg_mutex);
1771*4882a593Smuzhiyun 	if (reg < 0)
1772*4882a593Smuzhiyun 		return reg;
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 	switch (reg & 0x0f) {
1775*4882a593Smuzhiyun 	case 0x06:
1776*4882a593Smuzhiyun 		return 2;	/* is child node (connected to parent node) */
1777*4882a593Smuzhiyun 	case 0x0e:
1778*4882a593Smuzhiyun 		return 3;	/* is parent node (connected to child node) */
1779*4882a593Smuzhiyun 	}
1780*4882a593Smuzhiyun 	return 1;		/* not connected */
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun 
get_self_id_pos(struct fw_ohci * ohci,u32 self_id,int self_id_count)1783*4882a593Smuzhiyun static int get_self_id_pos(struct fw_ohci *ohci, u32 self_id,
1784*4882a593Smuzhiyun 	int self_id_count)
1785*4882a593Smuzhiyun {
1786*4882a593Smuzhiyun 	int i;
1787*4882a593Smuzhiyun 	u32 entry;
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	for (i = 0; i < self_id_count; i++) {
1790*4882a593Smuzhiyun 		entry = ohci->self_id_buffer[i];
1791*4882a593Smuzhiyun 		if ((self_id & 0xff000000) == (entry & 0xff000000))
1792*4882a593Smuzhiyun 			return -1;
1793*4882a593Smuzhiyun 		if ((self_id & 0xff000000) < (entry & 0xff000000))
1794*4882a593Smuzhiyun 			return i;
1795*4882a593Smuzhiyun 	}
1796*4882a593Smuzhiyun 	return i;
1797*4882a593Smuzhiyun }
1798*4882a593Smuzhiyun 
initiated_reset(struct fw_ohci * ohci)1799*4882a593Smuzhiyun static int initiated_reset(struct fw_ohci *ohci)
1800*4882a593Smuzhiyun {
1801*4882a593Smuzhiyun 	int reg;
1802*4882a593Smuzhiyun 	int ret = 0;
1803*4882a593Smuzhiyun 
1804*4882a593Smuzhiyun 	mutex_lock(&ohci->phy_reg_mutex);
1805*4882a593Smuzhiyun 	reg = write_phy_reg(ohci, 7, 0xe0); /* Select page 7 */
1806*4882a593Smuzhiyun 	if (reg >= 0) {
1807*4882a593Smuzhiyun 		reg = read_phy_reg(ohci, 8);
1808*4882a593Smuzhiyun 		reg |= 0x40;
1809*4882a593Smuzhiyun 		reg = write_phy_reg(ohci, 8, reg); /* set PMODE bit */
1810*4882a593Smuzhiyun 		if (reg >= 0) {
1811*4882a593Smuzhiyun 			reg = read_phy_reg(ohci, 12); /* read register 12 */
1812*4882a593Smuzhiyun 			if (reg >= 0) {
1813*4882a593Smuzhiyun 				if ((reg & 0x08) == 0x08) {
1814*4882a593Smuzhiyun 					/* bit 3 indicates "initiated reset" */
1815*4882a593Smuzhiyun 					ret = 0x2;
1816*4882a593Smuzhiyun 				}
1817*4882a593Smuzhiyun 			}
1818*4882a593Smuzhiyun 		}
1819*4882a593Smuzhiyun 	}
1820*4882a593Smuzhiyun 	mutex_unlock(&ohci->phy_reg_mutex);
1821*4882a593Smuzhiyun 	return ret;
1822*4882a593Smuzhiyun }
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun /*
1825*4882a593Smuzhiyun  * TI TSB82AA2B and TSB12LV26 do not receive the selfID of a locally
1826*4882a593Smuzhiyun  * attached TSB41BA3D phy; see http://www.ti.com/litv/pdf/sllz059.
1827*4882a593Smuzhiyun  * Construct the selfID from phy register contents.
1828*4882a593Smuzhiyun  */
find_and_insert_self_id(struct fw_ohci * ohci,int self_id_count)1829*4882a593Smuzhiyun static int find_and_insert_self_id(struct fw_ohci *ohci, int self_id_count)
1830*4882a593Smuzhiyun {
1831*4882a593Smuzhiyun 	int reg, i, pos, status;
1832*4882a593Smuzhiyun 	/* link active 1, speed 3, bridge 0, contender 1, more packets 0 */
1833*4882a593Smuzhiyun 	u32 self_id = 0x8040c800;
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 	reg = reg_read(ohci, OHCI1394_NodeID);
1836*4882a593Smuzhiyun 	if (!(reg & OHCI1394_NodeID_idValid)) {
1837*4882a593Smuzhiyun 		ohci_notice(ohci,
1838*4882a593Smuzhiyun 			    "node ID not valid, new bus reset in progress\n");
1839*4882a593Smuzhiyun 		return -EBUSY;
1840*4882a593Smuzhiyun 	}
1841*4882a593Smuzhiyun 	self_id |= ((reg & 0x3f) << 24); /* phy ID */
1842*4882a593Smuzhiyun 
1843*4882a593Smuzhiyun 	reg = ohci_read_phy_reg(&ohci->card, 4);
1844*4882a593Smuzhiyun 	if (reg < 0)
1845*4882a593Smuzhiyun 		return reg;
1846*4882a593Smuzhiyun 	self_id |= ((reg & 0x07) << 8); /* power class */
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 	reg = ohci_read_phy_reg(&ohci->card, 1);
1849*4882a593Smuzhiyun 	if (reg < 0)
1850*4882a593Smuzhiyun 		return reg;
1851*4882a593Smuzhiyun 	self_id |= ((reg & 0x3f) << 16); /* gap count */
1852*4882a593Smuzhiyun 
1853*4882a593Smuzhiyun 	for (i = 0; i < 3; i++) {
1854*4882a593Smuzhiyun 		status = get_status_for_port(ohci, i);
1855*4882a593Smuzhiyun 		if (status < 0)
1856*4882a593Smuzhiyun 			return status;
1857*4882a593Smuzhiyun 		self_id |= ((status & 0x3) << (6 - (i * 2)));
1858*4882a593Smuzhiyun 	}
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	self_id |= initiated_reset(ohci);
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun 	pos = get_self_id_pos(ohci, self_id, self_id_count);
1863*4882a593Smuzhiyun 	if (pos >= 0) {
1864*4882a593Smuzhiyun 		memmove(&(ohci->self_id_buffer[pos+1]),
1865*4882a593Smuzhiyun 			&(ohci->self_id_buffer[pos]),
1866*4882a593Smuzhiyun 			(self_id_count - pos) * sizeof(*ohci->self_id_buffer));
1867*4882a593Smuzhiyun 		ohci->self_id_buffer[pos] = self_id;
1868*4882a593Smuzhiyun 		self_id_count++;
1869*4882a593Smuzhiyun 	}
1870*4882a593Smuzhiyun 	return self_id_count;
1871*4882a593Smuzhiyun }
1872*4882a593Smuzhiyun 
bus_reset_work(struct work_struct * work)1873*4882a593Smuzhiyun static void bus_reset_work(struct work_struct *work)
1874*4882a593Smuzhiyun {
1875*4882a593Smuzhiyun 	struct fw_ohci *ohci =
1876*4882a593Smuzhiyun 		container_of(work, struct fw_ohci, bus_reset_work);
1877*4882a593Smuzhiyun 	int self_id_count, generation, new_generation, i, j;
1878*4882a593Smuzhiyun 	u32 reg;
1879*4882a593Smuzhiyun 	void *free_rom = NULL;
1880*4882a593Smuzhiyun 	dma_addr_t free_rom_bus = 0;
1881*4882a593Smuzhiyun 	bool is_new_root;
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 	reg = reg_read(ohci, OHCI1394_NodeID);
1884*4882a593Smuzhiyun 	if (!(reg & OHCI1394_NodeID_idValid)) {
1885*4882a593Smuzhiyun 		ohci_notice(ohci,
1886*4882a593Smuzhiyun 			    "node ID not valid, new bus reset in progress\n");
1887*4882a593Smuzhiyun 		return;
1888*4882a593Smuzhiyun 	}
1889*4882a593Smuzhiyun 	if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
1890*4882a593Smuzhiyun 		ohci_notice(ohci, "malconfigured bus\n");
1891*4882a593Smuzhiyun 		return;
1892*4882a593Smuzhiyun 	}
1893*4882a593Smuzhiyun 	ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
1894*4882a593Smuzhiyun 			       OHCI1394_NodeID_nodeNumber);
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun 	is_new_root = (reg & OHCI1394_NodeID_root) != 0;
1897*4882a593Smuzhiyun 	if (!(ohci->is_root && is_new_root))
1898*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_LinkControlSet,
1899*4882a593Smuzhiyun 			  OHCI1394_LinkControl_cycleMaster);
1900*4882a593Smuzhiyun 	ohci->is_root = is_new_root;
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	reg = reg_read(ohci, OHCI1394_SelfIDCount);
1903*4882a593Smuzhiyun 	if (reg & OHCI1394_SelfIDCount_selfIDError) {
1904*4882a593Smuzhiyun 		ohci_notice(ohci, "self ID receive error\n");
1905*4882a593Smuzhiyun 		return;
1906*4882a593Smuzhiyun 	}
1907*4882a593Smuzhiyun 	/*
1908*4882a593Smuzhiyun 	 * The count in the SelfIDCount register is the number of
1909*4882a593Smuzhiyun 	 * bytes in the self ID receive buffer.  Since we also receive
1910*4882a593Smuzhiyun 	 * the inverted quadlets and a header quadlet, we shift one
1911*4882a593Smuzhiyun 	 * bit extra to get the actual number of self IDs.
1912*4882a593Smuzhiyun 	 */
1913*4882a593Smuzhiyun 	self_id_count = (reg >> 3) & 0xff;
1914*4882a593Smuzhiyun 
1915*4882a593Smuzhiyun 	if (self_id_count > 252) {
1916*4882a593Smuzhiyun 		ohci_notice(ohci, "bad selfIDSize (%08x)\n", reg);
1917*4882a593Smuzhiyun 		return;
1918*4882a593Smuzhiyun 	}
1919*4882a593Smuzhiyun 
1920*4882a593Smuzhiyun 	generation = (cond_le32_to_cpu(ohci->self_id[0]) >> 16) & 0xff;
1921*4882a593Smuzhiyun 	rmb();
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun 	for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
1924*4882a593Smuzhiyun 		u32 id  = cond_le32_to_cpu(ohci->self_id[i]);
1925*4882a593Smuzhiyun 		u32 id2 = cond_le32_to_cpu(ohci->self_id[i + 1]);
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 		if (id != ~id2) {
1928*4882a593Smuzhiyun 			/*
1929*4882a593Smuzhiyun 			 * If the invalid data looks like a cycle start packet,
1930*4882a593Smuzhiyun 			 * it's likely to be the result of the cycle master
1931*4882a593Smuzhiyun 			 * having a wrong gap count.  In this case, the self IDs
1932*4882a593Smuzhiyun 			 * so far are valid and should be processed so that the
1933*4882a593Smuzhiyun 			 * bus manager can then correct the gap count.
1934*4882a593Smuzhiyun 			 */
1935*4882a593Smuzhiyun 			if (id == 0xffff008f) {
1936*4882a593Smuzhiyun 				ohci_notice(ohci, "ignoring spurious self IDs\n");
1937*4882a593Smuzhiyun 				self_id_count = j;
1938*4882a593Smuzhiyun 				break;
1939*4882a593Smuzhiyun 			}
1940*4882a593Smuzhiyun 
1941*4882a593Smuzhiyun 			ohci_notice(ohci, "bad self ID %d/%d (%08x != ~%08x)\n",
1942*4882a593Smuzhiyun 				    j, self_id_count, id, id2);
1943*4882a593Smuzhiyun 			return;
1944*4882a593Smuzhiyun 		}
1945*4882a593Smuzhiyun 		ohci->self_id_buffer[j] = id;
1946*4882a593Smuzhiyun 	}
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 	if (ohci->quirks & QUIRK_TI_SLLZ059) {
1949*4882a593Smuzhiyun 		self_id_count = find_and_insert_self_id(ohci, self_id_count);
1950*4882a593Smuzhiyun 		if (self_id_count < 0) {
1951*4882a593Smuzhiyun 			ohci_notice(ohci,
1952*4882a593Smuzhiyun 				    "could not construct local self ID\n");
1953*4882a593Smuzhiyun 			return;
1954*4882a593Smuzhiyun 		}
1955*4882a593Smuzhiyun 	}
1956*4882a593Smuzhiyun 
1957*4882a593Smuzhiyun 	if (self_id_count == 0) {
1958*4882a593Smuzhiyun 		ohci_notice(ohci, "no self IDs\n");
1959*4882a593Smuzhiyun 		return;
1960*4882a593Smuzhiyun 	}
1961*4882a593Smuzhiyun 	rmb();
1962*4882a593Smuzhiyun 
1963*4882a593Smuzhiyun 	/*
1964*4882a593Smuzhiyun 	 * Check the consistency of the self IDs we just read.  The
1965*4882a593Smuzhiyun 	 * problem we face is that a new bus reset can start while we
1966*4882a593Smuzhiyun 	 * read out the self IDs from the DMA buffer. If this happens,
1967*4882a593Smuzhiyun 	 * the DMA buffer will be overwritten with new self IDs and we
1968*4882a593Smuzhiyun 	 * will read out inconsistent data.  The OHCI specification
1969*4882a593Smuzhiyun 	 * (section 11.2) recommends a technique similar to
1970*4882a593Smuzhiyun 	 * linux/seqlock.h, where we remember the generation of the
1971*4882a593Smuzhiyun 	 * self IDs in the buffer before reading them out and compare
1972*4882a593Smuzhiyun 	 * it to the current generation after reading them out.  If
1973*4882a593Smuzhiyun 	 * the two generations match we know we have a consistent set
1974*4882a593Smuzhiyun 	 * of self IDs.
1975*4882a593Smuzhiyun 	 */
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun 	new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
1978*4882a593Smuzhiyun 	if (new_generation != generation) {
1979*4882a593Smuzhiyun 		ohci_notice(ohci, "new bus reset, discarding self ids\n");
1980*4882a593Smuzhiyun 		return;
1981*4882a593Smuzhiyun 	}
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 	/* FIXME: Document how the locking works. */
1984*4882a593Smuzhiyun 	spin_lock_irq(&ohci->lock);
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 	ohci->generation = -1; /* prevent AT packet queueing */
1987*4882a593Smuzhiyun 	context_stop(&ohci->at_request_ctx);
1988*4882a593Smuzhiyun 	context_stop(&ohci->at_response_ctx);
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun 	spin_unlock_irq(&ohci->lock);
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 	/*
1993*4882a593Smuzhiyun 	 * Per OHCI 1.2 draft, clause 7.2.3.3, hardware may leave unsent
1994*4882a593Smuzhiyun 	 * packets in the AT queues and software needs to drain them.
1995*4882a593Smuzhiyun 	 * Some OHCI 1.1 controllers (JMicron) apparently require this too.
1996*4882a593Smuzhiyun 	 */
1997*4882a593Smuzhiyun 	at_context_flush(&ohci->at_request_ctx);
1998*4882a593Smuzhiyun 	at_context_flush(&ohci->at_response_ctx);
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 	spin_lock_irq(&ohci->lock);
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun 	ohci->generation = generation;
2003*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 	if (ohci->quirks & QUIRK_RESET_PACKET)
2006*4882a593Smuzhiyun 		ohci->request_generation = generation;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	/*
2009*4882a593Smuzhiyun 	 * This next bit is unrelated to the AT context stuff but we
2010*4882a593Smuzhiyun 	 * have to do it under the spinlock also.  If a new config rom
2011*4882a593Smuzhiyun 	 * was set up before this reset, the old one is now no longer
2012*4882a593Smuzhiyun 	 * in use and we can free it. Update the config rom pointers
2013*4882a593Smuzhiyun 	 * to point to the current config rom and clear the
2014*4882a593Smuzhiyun 	 * next_config_rom pointer so a new update can take place.
2015*4882a593Smuzhiyun 	 */
2016*4882a593Smuzhiyun 
2017*4882a593Smuzhiyun 	if (ohci->next_config_rom != NULL) {
2018*4882a593Smuzhiyun 		if (ohci->next_config_rom != ohci->config_rom) {
2019*4882a593Smuzhiyun 			free_rom      = ohci->config_rom;
2020*4882a593Smuzhiyun 			free_rom_bus  = ohci->config_rom_bus;
2021*4882a593Smuzhiyun 		}
2022*4882a593Smuzhiyun 		ohci->config_rom      = ohci->next_config_rom;
2023*4882a593Smuzhiyun 		ohci->config_rom_bus  = ohci->next_config_rom_bus;
2024*4882a593Smuzhiyun 		ohci->next_config_rom = NULL;
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun 		/*
2027*4882a593Smuzhiyun 		 * Restore config_rom image and manually update
2028*4882a593Smuzhiyun 		 * config_rom registers.  Writing the header quadlet
2029*4882a593Smuzhiyun 		 * will indicate that the config rom is ready, so we
2030*4882a593Smuzhiyun 		 * do that last.
2031*4882a593Smuzhiyun 		 */
2032*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_BusOptions,
2033*4882a593Smuzhiyun 			  be32_to_cpu(ohci->config_rom[2]));
2034*4882a593Smuzhiyun 		ohci->config_rom[0] = ohci->next_header;
2035*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_ConfigROMhdr,
2036*4882a593Smuzhiyun 			  be32_to_cpu(ohci->next_header));
2037*4882a593Smuzhiyun 	}
2038*4882a593Smuzhiyun 
2039*4882a593Smuzhiyun 	if (param_remote_dma) {
2040*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0);
2041*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0);
2042*4882a593Smuzhiyun 	}
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 	spin_unlock_irq(&ohci->lock);
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun 	if (free_rom)
2047*4882a593Smuzhiyun 		dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
2048*4882a593Smuzhiyun 				  free_rom, free_rom_bus);
2049*4882a593Smuzhiyun 
2050*4882a593Smuzhiyun 	log_selfids(ohci, generation, self_id_count);
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
2053*4882a593Smuzhiyun 				 self_id_count, ohci->self_id_buffer,
2054*4882a593Smuzhiyun 				 ohci->csr_state_setclear_abdicate);
2055*4882a593Smuzhiyun 	ohci->csr_state_setclear_abdicate = false;
2056*4882a593Smuzhiyun }
2057*4882a593Smuzhiyun 
irq_handler(int irq,void * data)2058*4882a593Smuzhiyun static irqreturn_t irq_handler(int irq, void *data)
2059*4882a593Smuzhiyun {
2060*4882a593Smuzhiyun 	struct fw_ohci *ohci = data;
2061*4882a593Smuzhiyun 	u32 event, iso_event;
2062*4882a593Smuzhiyun 	int i;
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun 	event = reg_read(ohci, OHCI1394_IntEventClear);
2065*4882a593Smuzhiyun 
2066*4882a593Smuzhiyun 	if (!event || !~event)
2067*4882a593Smuzhiyun 		return IRQ_NONE;
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun 	/*
2070*4882a593Smuzhiyun 	 * busReset and postedWriteErr must not be cleared yet
2071*4882a593Smuzhiyun 	 * (OHCI 1.1 clauses 7.2.3.2 and 13.2.8.1)
2072*4882a593Smuzhiyun 	 */
2073*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IntEventClear,
2074*4882a593Smuzhiyun 		  event & ~(OHCI1394_busReset | OHCI1394_postedWriteErr));
2075*4882a593Smuzhiyun 	log_irqs(ohci, event);
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun 	if (event & OHCI1394_selfIDComplete)
2078*4882a593Smuzhiyun 		queue_work(selfid_workqueue, &ohci->bus_reset_work);
2079*4882a593Smuzhiyun 
2080*4882a593Smuzhiyun 	if (event & OHCI1394_RQPkt)
2081*4882a593Smuzhiyun 		tasklet_schedule(&ohci->ar_request_ctx.tasklet);
2082*4882a593Smuzhiyun 
2083*4882a593Smuzhiyun 	if (event & OHCI1394_RSPkt)
2084*4882a593Smuzhiyun 		tasklet_schedule(&ohci->ar_response_ctx.tasklet);
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun 	if (event & OHCI1394_reqTxComplete)
2087*4882a593Smuzhiyun 		tasklet_schedule(&ohci->at_request_ctx.tasklet);
2088*4882a593Smuzhiyun 
2089*4882a593Smuzhiyun 	if (event & OHCI1394_respTxComplete)
2090*4882a593Smuzhiyun 		tasklet_schedule(&ohci->at_response_ctx.tasklet);
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun 	if (event & OHCI1394_isochRx) {
2093*4882a593Smuzhiyun 		iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
2094*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun 		while (iso_event) {
2097*4882a593Smuzhiyun 			i = ffs(iso_event) - 1;
2098*4882a593Smuzhiyun 			tasklet_schedule(
2099*4882a593Smuzhiyun 				&ohci->ir_context_list[i].context.tasklet);
2100*4882a593Smuzhiyun 			iso_event &= ~(1 << i);
2101*4882a593Smuzhiyun 		}
2102*4882a593Smuzhiyun 	}
2103*4882a593Smuzhiyun 
2104*4882a593Smuzhiyun 	if (event & OHCI1394_isochTx) {
2105*4882a593Smuzhiyun 		iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
2106*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun 		while (iso_event) {
2109*4882a593Smuzhiyun 			i = ffs(iso_event) - 1;
2110*4882a593Smuzhiyun 			tasklet_schedule(
2111*4882a593Smuzhiyun 				&ohci->it_context_list[i].context.tasklet);
2112*4882a593Smuzhiyun 			iso_event &= ~(1 << i);
2113*4882a593Smuzhiyun 		}
2114*4882a593Smuzhiyun 	}
2115*4882a593Smuzhiyun 
2116*4882a593Smuzhiyun 	if (unlikely(event & OHCI1394_regAccessFail))
2117*4882a593Smuzhiyun 		ohci_err(ohci, "register access failure\n");
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun 	if (unlikely(event & OHCI1394_postedWriteErr)) {
2120*4882a593Smuzhiyun 		reg_read(ohci, OHCI1394_PostedWriteAddressHi);
2121*4882a593Smuzhiyun 		reg_read(ohci, OHCI1394_PostedWriteAddressLo);
2122*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IntEventClear,
2123*4882a593Smuzhiyun 			  OHCI1394_postedWriteErr);
2124*4882a593Smuzhiyun 		if (printk_ratelimit())
2125*4882a593Smuzhiyun 			ohci_err(ohci, "PCI posted write error\n");
2126*4882a593Smuzhiyun 	}
2127*4882a593Smuzhiyun 
2128*4882a593Smuzhiyun 	if (unlikely(event & OHCI1394_cycleTooLong)) {
2129*4882a593Smuzhiyun 		if (printk_ratelimit())
2130*4882a593Smuzhiyun 			ohci_notice(ohci, "isochronous cycle too long\n");
2131*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_LinkControlSet,
2132*4882a593Smuzhiyun 			  OHCI1394_LinkControl_cycleMaster);
2133*4882a593Smuzhiyun 	}
2134*4882a593Smuzhiyun 
2135*4882a593Smuzhiyun 	if (unlikely(event & OHCI1394_cycleInconsistent)) {
2136*4882a593Smuzhiyun 		/*
2137*4882a593Smuzhiyun 		 * We need to clear this event bit in order to make
2138*4882a593Smuzhiyun 		 * cycleMatch isochronous I/O work.  In theory we should
2139*4882a593Smuzhiyun 		 * stop active cycleMatch iso contexts now and restart
2140*4882a593Smuzhiyun 		 * them at least two cycles later.  (FIXME?)
2141*4882a593Smuzhiyun 		 */
2142*4882a593Smuzhiyun 		if (printk_ratelimit())
2143*4882a593Smuzhiyun 			ohci_notice(ohci, "isochronous cycle inconsistent\n");
2144*4882a593Smuzhiyun 	}
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	if (unlikely(event & OHCI1394_unrecoverableError))
2147*4882a593Smuzhiyun 		handle_dead_contexts(ohci);
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun 	if (event & OHCI1394_cycle64Seconds) {
2150*4882a593Smuzhiyun 		spin_lock(&ohci->lock);
2151*4882a593Smuzhiyun 		update_bus_time(ohci);
2152*4882a593Smuzhiyun 		spin_unlock(&ohci->lock);
2153*4882a593Smuzhiyun 	} else
2154*4882a593Smuzhiyun 		flush_writes(ohci);
2155*4882a593Smuzhiyun 
2156*4882a593Smuzhiyun 	return IRQ_HANDLED;
2157*4882a593Smuzhiyun }
2158*4882a593Smuzhiyun 
software_reset(struct fw_ohci * ohci)2159*4882a593Smuzhiyun static int software_reset(struct fw_ohci *ohci)
2160*4882a593Smuzhiyun {
2161*4882a593Smuzhiyun 	u32 val;
2162*4882a593Smuzhiyun 	int i;
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
2165*4882a593Smuzhiyun 	for (i = 0; i < 500; i++) {
2166*4882a593Smuzhiyun 		val = reg_read(ohci, OHCI1394_HCControlSet);
2167*4882a593Smuzhiyun 		if (!~val)
2168*4882a593Smuzhiyun 			return -ENODEV; /* Card was ejected. */
2169*4882a593Smuzhiyun 
2170*4882a593Smuzhiyun 		if (!(val & OHCI1394_HCControl_softReset))
2171*4882a593Smuzhiyun 			return 0;
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 		msleep(1);
2174*4882a593Smuzhiyun 	}
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun 	return -EBUSY;
2177*4882a593Smuzhiyun }
2178*4882a593Smuzhiyun 
copy_config_rom(__be32 * dest,const __be32 * src,size_t length)2179*4882a593Smuzhiyun static void copy_config_rom(__be32 *dest, const __be32 *src, size_t length)
2180*4882a593Smuzhiyun {
2181*4882a593Smuzhiyun 	size_t size = length * 4;
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 	memcpy(dest, src, size);
2184*4882a593Smuzhiyun 	if (size < CONFIG_ROM_SIZE)
2185*4882a593Smuzhiyun 		memset(&dest[length], 0, CONFIG_ROM_SIZE - size);
2186*4882a593Smuzhiyun }
2187*4882a593Smuzhiyun 
configure_1394a_enhancements(struct fw_ohci * ohci)2188*4882a593Smuzhiyun static int configure_1394a_enhancements(struct fw_ohci *ohci)
2189*4882a593Smuzhiyun {
2190*4882a593Smuzhiyun 	bool enable_1394a;
2191*4882a593Smuzhiyun 	int ret, clear, set, offset;
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun 	/* Check if the driver should configure link and PHY. */
2194*4882a593Smuzhiyun 	if (!(reg_read(ohci, OHCI1394_HCControlSet) &
2195*4882a593Smuzhiyun 	      OHCI1394_HCControl_programPhyEnable))
2196*4882a593Smuzhiyun 		return 0;
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun 	/* Paranoia: check whether the PHY supports 1394a, too. */
2199*4882a593Smuzhiyun 	enable_1394a = false;
2200*4882a593Smuzhiyun 	ret = read_phy_reg(ohci, 2);
2201*4882a593Smuzhiyun 	if (ret < 0)
2202*4882a593Smuzhiyun 		return ret;
2203*4882a593Smuzhiyun 	if ((ret & PHY_EXTENDED_REGISTERS) == PHY_EXTENDED_REGISTERS) {
2204*4882a593Smuzhiyun 		ret = read_paged_phy_reg(ohci, 1, 8);
2205*4882a593Smuzhiyun 		if (ret < 0)
2206*4882a593Smuzhiyun 			return ret;
2207*4882a593Smuzhiyun 		if (ret >= 1)
2208*4882a593Smuzhiyun 			enable_1394a = true;
2209*4882a593Smuzhiyun 	}
2210*4882a593Smuzhiyun 
2211*4882a593Smuzhiyun 	if (ohci->quirks & QUIRK_NO_1394A)
2212*4882a593Smuzhiyun 		enable_1394a = false;
2213*4882a593Smuzhiyun 
2214*4882a593Smuzhiyun 	/* Configure PHY and link consistently. */
2215*4882a593Smuzhiyun 	if (enable_1394a) {
2216*4882a593Smuzhiyun 		clear = 0;
2217*4882a593Smuzhiyun 		set = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
2218*4882a593Smuzhiyun 	} else {
2219*4882a593Smuzhiyun 		clear = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
2220*4882a593Smuzhiyun 		set = 0;
2221*4882a593Smuzhiyun 	}
2222*4882a593Smuzhiyun 	ret = update_phy_reg(ohci, 5, clear, set);
2223*4882a593Smuzhiyun 	if (ret < 0)
2224*4882a593Smuzhiyun 		return ret;
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun 	if (enable_1394a)
2227*4882a593Smuzhiyun 		offset = OHCI1394_HCControlSet;
2228*4882a593Smuzhiyun 	else
2229*4882a593Smuzhiyun 		offset = OHCI1394_HCControlClear;
2230*4882a593Smuzhiyun 	reg_write(ohci, offset, OHCI1394_HCControl_aPhyEnhanceEnable);
2231*4882a593Smuzhiyun 
2232*4882a593Smuzhiyun 	/* Clean up: configuration has been taken care of. */
2233*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_HCControlClear,
2234*4882a593Smuzhiyun 		  OHCI1394_HCControl_programPhyEnable);
2235*4882a593Smuzhiyun 
2236*4882a593Smuzhiyun 	return 0;
2237*4882a593Smuzhiyun }
2238*4882a593Smuzhiyun 
probe_tsb41ba3d(struct fw_ohci * ohci)2239*4882a593Smuzhiyun static int probe_tsb41ba3d(struct fw_ohci *ohci)
2240*4882a593Smuzhiyun {
2241*4882a593Smuzhiyun 	/* TI vendor ID = 0x080028, TSB41BA3D product ID = 0x833005 (sic) */
2242*4882a593Smuzhiyun 	static const u8 id[] = { 0x08, 0x00, 0x28, 0x83, 0x30, 0x05, };
2243*4882a593Smuzhiyun 	int reg, i;
2244*4882a593Smuzhiyun 
2245*4882a593Smuzhiyun 	reg = read_phy_reg(ohci, 2);
2246*4882a593Smuzhiyun 	if (reg < 0)
2247*4882a593Smuzhiyun 		return reg;
2248*4882a593Smuzhiyun 	if ((reg & PHY_EXTENDED_REGISTERS) != PHY_EXTENDED_REGISTERS)
2249*4882a593Smuzhiyun 		return 0;
2250*4882a593Smuzhiyun 
2251*4882a593Smuzhiyun 	for (i = ARRAY_SIZE(id) - 1; i >= 0; i--) {
2252*4882a593Smuzhiyun 		reg = read_paged_phy_reg(ohci, 1, i + 10);
2253*4882a593Smuzhiyun 		if (reg < 0)
2254*4882a593Smuzhiyun 			return reg;
2255*4882a593Smuzhiyun 		if (reg != id[i])
2256*4882a593Smuzhiyun 			return 0;
2257*4882a593Smuzhiyun 	}
2258*4882a593Smuzhiyun 	return 1;
2259*4882a593Smuzhiyun }
2260*4882a593Smuzhiyun 
ohci_enable(struct fw_card * card,const __be32 * config_rom,size_t length)2261*4882a593Smuzhiyun static int ohci_enable(struct fw_card *card,
2262*4882a593Smuzhiyun 		       const __be32 *config_rom, size_t length)
2263*4882a593Smuzhiyun {
2264*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
2265*4882a593Smuzhiyun 	u32 lps, version, irqs;
2266*4882a593Smuzhiyun 	int i, ret;
2267*4882a593Smuzhiyun 
2268*4882a593Smuzhiyun 	ret = software_reset(ohci);
2269*4882a593Smuzhiyun 	if (ret < 0) {
2270*4882a593Smuzhiyun 		ohci_err(ohci, "failed to reset ohci card\n");
2271*4882a593Smuzhiyun 		return ret;
2272*4882a593Smuzhiyun 	}
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun 	/*
2275*4882a593Smuzhiyun 	 * Now enable LPS, which we need in order to start accessing
2276*4882a593Smuzhiyun 	 * most of the registers.  In fact, on some cards (ALI M5251),
2277*4882a593Smuzhiyun 	 * accessing registers in the SClk domain without LPS enabled
2278*4882a593Smuzhiyun 	 * will lock up the machine.  Wait 50msec to make sure we have
2279*4882a593Smuzhiyun 	 * full link enabled.  However, with some cards (well, at least
2280*4882a593Smuzhiyun 	 * a JMicron PCIe card), we have to try again sometimes.
2281*4882a593Smuzhiyun 	 *
2282*4882a593Smuzhiyun 	 * TI TSB82AA2 + TSB81BA3(A) cards signal LPS enabled early but
2283*4882a593Smuzhiyun 	 * cannot actually use the phy at that time.  These need tens of
2284*4882a593Smuzhiyun 	 * millisecods pause between LPS write and first phy access too.
2285*4882a593Smuzhiyun 	 */
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_HCControlSet,
2288*4882a593Smuzhiyun 		  OHCI1394_HCControl_LPS |
2289*4882a593Smuzhiyun 		  OHCI1394_HCControl_postedWriteEnable);
2290*4882a593Smuzhiyun 	flush_writes(ohci);
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun 	for (lps = 0, i = 0; !lps && i < 3; i++) {
2293*4882a593Smuzhiyun 		msleep(50);
2294*4882a593Smuzhiyun 		lps = reg_read(ohci, OHCI1394_HCControlSet) &
2295*4882a593Smuzhiyun 		      OHCI1394_HCControl_LPS;
2296*4882a593Smuzhiyun 	}
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun 	if (!lps) {
2299*4882a593Smuzhiyun 		ohci_err(ohci, "failed to set Link Power Status\n");
2300*4882a593Smuzhiyun 		return -EIO;
2301*4882a593Smuzhiyun 	}
2302*4882a593Smuzhiyun 
2303*4882a593Smuzhiyun 	if (ohci->quirks & QUIRK_TI_SLLZ059) {
2304*4882a593Smuzhiyun 		ret = probe_tsb41ba3d(ohci);
2305*4882a593Smuzhiyun 		if (ret < 0)
2306*4882a593Smuzhiyun 			return ret;
2307*4882a593Smuzhiyun 		if (ret)
2308*4882a593Smuzhiyun 			ohci_notice(ohci, "local TSB41BA3D phy\n");
2309*4882a593Smuzhiyun 		else
2310*4882a593Smuzhiyun 			ohci->quirks &= ~QUIRK_TI_SLLZ059;
2311*4882a593Smuzhiyun 	}
2312*4882a593Smuzhiyun 
2313*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_HCControlClear,
2314*4882a593Smuzhiyun 		  OHCI1394_HCControl_noByteSwapData);
2315*4882a593Smuzhiyun 
2316*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
2317*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_LinkControlSet,
2318*4882a593Smuzhiyun 		  OHCI1394_LinkControl_cycleTimerEnable |
2319*4882a593Smuzhiyun 		  OHCI1394_LinkControl_cycleMaster);
2320*4882a593Smuzhiyun 
2321*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_ATRetries,
2322*4882a593Smuzhiyun 		  OHCI1394_MAX_AT_REQ_RETRIES |
2323*4882a593Smuzhiyun 		  (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
2324*4882a593Smuzhiyun 		  (OHCI1394_MAX_PHYS_RESP_RETRIES << 8) |
2325*4882a593Smuzhiyun 		  (200 << 16));
2326*4882a593Smuzhiyun 
2327*4882a593Smuzhiyun 	ohci->bus_time_running = false;
2328*4882a593Smuzhiyun 
2329*4882a593Smuzhiyun 	for (i = 0; i < 32; i++)
2330*4882a593Smuzhiyun 		if (ohci->ir_context_support & (1 << i))
2331*4882a593Smuzhiyun 			reg_write(ohci, OHCI1394_IsoRcvContextControlClear(i),
2332*4882a593Smuzhiyun 				  IR_CONTEXT_MULTI_CHANNEL_MODE);
2333*4882a593Smuzhiyun 
2334*4882a593Smuzhiyun 	version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
2335*4882a593Smuzhiyun 	if (version >= OHCI_VERSION_1_1) {
2336*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_InitialChannelsAvailableHi,
2337*4882a593Smuzhiyun 			  0xfffffffe);
2338*4882a593Smuzhiyun 		card->broadcast_channel_auto_allocated = true;
2339*4882a593Smuzhiyun 	}
2340*4882a593Smuzhiyun 
2341*4882a593Smuzhiyun 	/* Get implemented bits of the priority arbitration request counter. */
2342*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_FairnessControl, 0x3f);
2343*4882a593Smuzhiyun 	ohci->pri_req_max = reg_read(ohci, OHCI1394_FairnessControl) & 0x3f;
2344*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_FairnessControl, 0);
2345*4882a593Smuzhiyun 	card->priority_budget_implemented = ohci->pri_req_max != 0;
2346*4882a593Smuzhiyun 
2347*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_PhyUpperBound, FW_MAX_PHYSICAL_RANGE >> 16);
2348*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IntEventClear, ~0);
2349*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IntMaskClear, ~0);
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun 	ret = configure_1394a_enhancements(ohci);
2352*4882a593Smuzhiyun 	if (ret < 0)
2353*4882a593Smuzhiyun 		return ret;
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun 	/* Activate link_on bit and contender bit in our self ID packets.*/
2356*4882a593Smuzhiyun 	ret = ohci_update_phy_reg(card, 4, 0, PHY_LINK_ACTIVE | PHY_CONTENDER);
2357*4882a593Smuzhiyun 	if (ret < 0)
2358*4882a593Smuzhiyun 		return ret;
2359*4882a593Smuzhiyun 
2360*4882a593Smuzhiyun 	/*
2361*4882a593Smuzhiyun 	 * When the link is not yet enabled, the atomic config rom
2362*4882a593Smuzhiyun 	 * update mechanism described below in ohci_set_config_rom()
2363*4882a593Smuzhiyun 	 * is not active.  We have to update ConfigRomHeader and
2364*4882a593Smuzhiyun 	 * BusOptions manually, and the write to ConfigROMmap takes
2365*4882a593Smuzhiyun 	 * effect immediately.  We tie this to the enabling of the
2366*4882a593Smuzhiyun 	 * link, so we have a valid config rom before enabling - the
2367*4882a593Smuzhiyun 	 * OHCI requires that ConfigROMhdr and BusOptions have valid
2368*4882a593Smuzhiyun 	 * values before enabling.
2369*4882a593Smuzhiyun 	 *
2370*4882a593Smuzhiyun 	 * However, when the ConfigROMmap is written, some controllers
2371*4882a593Smuzhiyun 	 * always read back quadlets 0 and 2 from the config rom to
2372*4882a593Smuzhiyun 	 * the ConfigRomHeader and BusOptions registers on bus reset.
2373*4882a593Smuzhiyun 	 * They shouldn't do that in this initial case where the link
2374*4882a593Smuzhiyun 	 * isn't enabled.  This means we have to use the same
2375*4882a593Smuzhiyun 	 * workaround here, setting the bus header to 0 and then write
2376*4882a593Smuzhiyun 	 * the right values in the bus reset tasklet.
2377*4882a593Smuzhiyun 	 */
2378*4882a593Smuzhiyun 
2379*4882a593Smuzhiyun 	if (config_rom) {
2380*4882a593Smuzhiyun 		ohci->next_config_rom =
2381*4882a593Smuzhiyun 			dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
2382*4882a593Smuzhiyun 					   &ohci->next_config_rom_bus,
2383*4882a593Smuzhiyun 					   GFP_KERNEL);
2384*4882a593Smuzhiyun 		if (ohci->next_config_rom == NULL)
2385*4882a593Smuzhiyun 			return -ENOMEM;
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun 		copy_config_rom(ohci->next_config_rom, config_rom, length);
2388*4882a593Smuzhiyun 	} else {
2389*4882a593Smuzhiyun 		/*
2390*4882a593Smuzhiyun 		 * In the suspend case, config_rom is NULL, which
2391*4882a593Smuzhiyun 		 * means that we just reuse the old config rom.
2392*4882a593Smuzhiyun 		 */
2393*4882a593Smuzhiyun 		ohci->next_config_rom = ohci->config_rom;
2394*4882a593Smuzhiyun 		ohci->next_config_rom_bus = ohci->config_rom_bus;
2395*4882a593Smuzhiyun 	}
2396*4882a593Smuzhiyun 
2397*4882a593Smuzhiyun 	ohci->next_header = ohci->next_config_rom[0];
2398*4882a593Smuzhiyun 	ohci->next_config_rom[0] = 0;
2399*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
2400*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_BusOptions,
2401*4882a593Smuzhiyun 		  be32_to_cpu(ohci->next_config_rom[2]));
2402*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
2405*4882a593Smuzhiyun 
2406*4882a593Smuzhiyun 	irqs =	OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
2407*4882a593Smuzhiyun 		OHCI1394_RQPkt | OHCI1394_RSPkt |
2408*4882a593Smuzhiyun 		OHCI1394_isochTx | OHCI1394_isochRx |
2409*4882a593Smuzhiyun 		OHCI1394_postedWriteErr |
2410*4882a593Smuzhiyun 		OHCI1394_selfIDComplete |
2411*4882a593Smuzhiyun 		OHCI1394_regAccessFail |
2412*4882a593Smuzhiyun 		OHCI1394_cycleInconsistent |
2413*4882a593Smuzhiyun 		OHCI1394_unrecoverableError |
2414*4882a593Smuzhiyun 		OHCI1394_cycleTooLong |
2415*4882a593Smuzhiyun 		OHCI1394_masterIntEnable;
2416*4882a593Smuzhiyun 	if (param_debug & OHCI_PARAM_DEBUG_BUSRESETS)
2417*4882a593Smuzhiyun 		irqs |= OHCI1394_busReset;
2418*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IntMaskSet, irqs);
2419*4882a593Smuzhiyun 
2420*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_HCControlSet,
2421*4882a593Smuzhiyun 		  OHCI1394_HCControl_linkEnable |
2422*4882a593Smuzhiyun 		  OHCI1394_HCControl_BIBimageValid);
2423*4882a593Smuzhiyun 
2424*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_LinkControlSet,
2425*4882a593Smuzhiyun 		  OHCI1394_LinkControl_rcvSelfID |
2426*4882a593Smuzhiyun 		  OHCI1394_LinkControl_rcvPhyPkt);
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun 	ar_context_run(&ohci->ar_request_ctx);
2429*4882a593Smuzhiyun 	ar_context_run(&ohci->ar_response_ctx);
2430*4882a593Smuzhiyun 
2431*4882a593Smuzhiyun 	flush_writes(ohci);
2432*4882a593Smuzhiyun 
2433*4882a593Smuzhiyun 	/* We are ready to go, reset bus to finish initialization. */
2434*4882a593Smuzhiyun 	fw_schedule_bus_reset(&ohci->card, false, true);
2435*4882a593Smuzhiyun 
2436*4882a593Smuzhiyun 	return 0;
2437*4882a593Smuzhiyun }
2438*4882a593Smuzhiyun 
ohci_set_config_rom(struct fw_card * card,const __be32 * config_rom,size_t length)2439*4882a593Smuzhiyun static int ohci_set_config_rom(struct fw_card *card,
2440*4882a593Smuzhiyun 			       const __be32 *config_rom, size_t length)
2441*4882a593Smuzhiyun {
2442*4882a593Smuzhiyun 	struct fw_ohci *ohci;
2443*4882a593Smuzhiyun 	__be32 *next_config_rom;
2444*4882a593Smuzhiyun 	dma_addr_t next_config_rom_bus;
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun 	ohci = fw_ohci(card);
2447*4882a593Smuzhiyun 
2448*4882a593Smuzhiyun 	/*
2449*4882a593Smuzhiyun 	 * When the OHCI controller is enabled, the config rom update
2450*4882a593Smuzhiyun 	 * mechanism is a bit tricky, but easy enough to use.  See
2451*4882a593Smuzhiyun 	 * section 5.5.6 in the OHCI specification.
2452*4882a593Smuzhiyun 	 *
2453*4882a593Smuzhiyun 	 * The OHCI controller caches the new config rom address in a
2454*4882a593Smuzhiyun 	 * shadow register (ConfigROMmapNext) and needs a bus reset
2455*4882a593Smuzhiyun 	 * for the changes to take place.  When the bus reset is
2456*4882a593Smuzhiyun 	 * detected, the controller loads the new values for the
2457*4882a593Smuzhiyun 	 * ConfigRomHeader and BusOptions registers from the specified
2458*4882a593Smuzhiyun 	 * config rom and loads ConfigROMmap from the ConfigROMmapNext
2459*4882a593Smuzhiyun 	 * shadow register. All automatically and atomically.
2460*4882a593Smuzhiyun 	 *
2461*4882a593Smuzhiyun 	 * Now, there's a twist to this story.  The automatic load of
2462*4882a593Smuzhiyun 	 * ConfigRomHeader and BusOptions doesn't honor the
2463*4882a593Smuzhiyun 	 * noByteSwapData bit, so with a be32 config rom, the
2464*4882a593Smuzhiyun 	 * controller will load be32 values in to these registers
2465*4882a593Smuzhiyun 	 * during the atomic update, even on litte endian
2466*4882a593Smuzhiyun 	 * architectures.  The workaround we use is to put a 0 in the
2467*4882a593Smuzhiyun 	 * header quadlet; 0 is endian agnostic and means that the
2468*4882a593Smuzhiyun 	 * config rom isn't ready yet.  In the bus reset tasklet we
2469*4882a593Smuzhiyun 	 * then set up the real values for the two registers.
2470*4882a593Smuzhiyun 	 *
2471*4882a593Smuzhiyun 	 * We use ohci->lock to avoid racing with the code that sets
2472*4882a593Smuzhiyun 	 * ohci->next_config_rom to NULL (see bus_reset_work).
2473*4882a593Smuzhiyun 	 */
2474*4882a593Smuzhiyun 
2475*4882a593Smuzhiyun 	next_config_rom =
2476*4882a593Smuzhiyun 		dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
2477*4882a593Smuzhiyun 				   &next_config_rom_bus, GFP_KERNEL);
2478*4882a593Smuzhiyun 	if (next_config_rom == NULL)
2479*4882a593Smuzhiyun 		return -ENOMEM;
2480*4882a593Smuzhiyun 
2481*4882a593Smuzhiyun 	spin_lock_irq(&ohci->lock);
2482*4882a593Smuzhiyun 
2483*4882a593Smuzhiyun 	/*
2484*4882a593Smuzhiyun 	 * If there is not an already pending config_rom update,
2485*4882a593Smuzhiyun 	 * push our new allocation into the ohci->next_config_rom
2486*4882a593Smuzhiyun 	 * and then mark the local variable as null so that we
2487*4882a593Smuzhiyun 	 * won't deallocate the new buffer.
2488*4882a593Smuzhiyun 	 *
2489*4882a593Smuzhiyun 	 * OTOH, if there is a pending config_rom update, just
2490*4882a593Smuzhiyun 	 * use that buffer with the new config_rom data, and
2491*4882a593Smuzhiyun 	 * let this routine free the unused DMA allocation.
2492*4882a593Smuzhiyun 	 */
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun 	if (ohci->next_config_rom == NULL) {
2495*4882a593Smuzhiyun 		ohci->next_config_rom = next_config_rom;
2496*4882a593Smuzhiyun 		ohci->next_config_rom_bus = next_config_rom_bus;
2497*4882a593Smuzhiyun 		next_config_rom = NULL;
2498*4882a593Smuzhiyun 	}
2499*4882a593Smuzhiyun 
2500*4882a593Smuzhiyun 	copy_config_rom(ohci->next_config_rom, config_rom, length);
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun 	ohci->next_header = config_rom[0];
2503*4882a593Smuzhiyun 	ohci->next_config_rom[0] = 0;
2504*4882a593Smuzhiyun 
2505*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
2506*4882a593Smuzhiyun 
2507*4882a593Smuzhiyun 	spin_unlock_irq(&ohci->lock);
2508*4882a593Smuzhiyun 
2509*4882a593Smuzhiyun 	/* If we didn't use the DMA allocation, delete it. */
2510*4882a593Smuzhiyun 	if (next_config_rom != NULL)
2511*4882a593Smuzhiyun 		dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
2512*4882a593Smuzhiyun 				  next_config_rom, next_config_rom_bus);
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun 	/*
2515*4882a593Smuzhiyun 	 * Now initiate a bus reset to have the changes take
2516*4882a593Smuzhiyun 	 * effect. We clean up the old config rom memory and DMA
2517*4882a593Smuzhiyun 	 * mappings in the bus reset tasklet, since the OHCI
2518*4882a593Smuzhiyun 	 * controller could need to access it before the bus reset
2519*4882a593Smuzhiyun 	 * takes effect.
2520*4882a593Smuzhiyun 	 */
2521*4882a593Smuzhiyun 
2522*4882a593Smuzhiyun 	fw_schedule_bus_reset(&ohci->card, true, true);
2523*4882a593Smuzhiyun 
2524*4882a593Smuzhiyun 	return 0;
2525*4882a593Smuzhiyun }
2526*4882a593Smuzhiyun 
ohci_send_request(struct fw_card * card,struct fw_packet * packet)2527*4882a593Smuzhiyun static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
2528*4882a593Smuzhiyun {
2529*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun 	at_context_transmit(&ohci->at_request_ctx, packet);
2532*4882a593Smuzhiyun }
2533*4882a593Smuzhiyun 
ohci_send_response(struct fw_card * card,struct fw_packet * packet)2534*4882a593Smuzhiyun static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
2535*4882a593Smuzhiyun {
2536*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
2537*4882a593Smuzhiyun 
2538*4882a593Smuzhiyun 	at_context_transmit(&ohci->at_response_ctx, packet);
2539*4882a593Smuzhiyun }
2540*4882a593Smuzhiyun 
ohci_cancel_packet(struct fw_card * card,struct fw_packet * packet)2541*4882a593Smuzhiyun static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
2542*4882a593Smuzhiyun {
2543*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
2544*4882a593Smuzhiyun 	struct context *ctx = &ohci->at_request_ctx;
2545*4882a593Smuzhiyun 	struct driver_data *driver_data = packet->driver_data;
2546*4882a593Smuzhiyun 	int ret = -ENOENT;
2547*4882a593Smuzhiyun 
2548*4882a593Smuzhiyun 	tasklet_disable(&ctx->tasklet);
2549*4882a593Smuzhiyun 
2550*4882a593Smuzhiyun 	if (packet->ack != 0)
2551*4882a593Smuzhiyun 		goto out;
2552*4882a593Smuzhiyun 
2553*4882a593Smuzhiyun 	if (packet->payload_mapped)
2554*4882a593Smuzhiyun 		dma_unmap_single(ohci->card.device, packet->payload_bus,
2555*4882a593Smuzhiyun 				 packet->payload_length, DMA_TO_DEVICE);
2556*4882a593Smuzhiyun 
2557*4882a593Smuzhiyun 	log_ar_at_event(ohci, 'T', packet->speed, packet->header, 0x20);
2558*4882a593Smuzhiyun 	driver_data->packet = NULL;
2559*4882a593Smuzhiyun 	packet->ack = RCODE_CANCELLED;
2560*4882a593Smuzhiyun 	packet->callback(packet, &ohci->card, packet->ack);
2561*4882a593Smuzhiyun 	ret = 0;
2562*4882a593Smuzhiyun  out:
2563*4882a593Smuzhiyun 	tasklet_enable(&ctx->tasklet);
2564*4882a593Smuzhiyun 
2565*4882a593Smuzhiyun 	return ret;
2566*4882a593Smuzhiyun }
2567*4882a593Smuzhiyun 
ohci_enable_phys_dma(struct fw_card * card,int node_id,int generation)2568*4882a593Smuzhiyun static int ohci_enable_phys_dma(struct fw_card *card,
2569*4882a593Smuzhiyun 				int node_id, int generation)
2570*4882a593Smuzhiyun {
2571*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
2572*4882a593Smuzhiyun 	unsigned long flags;
2573*4882a593Smuzhiyun 	int n, ret = 0;
2574*4882a593Smuzhiyun 
2575*4882a593Smuzhiyun 	if (param_remote_dma)
2576*4882a593Smuzhiyun 		return 0;
2577*4882a593Smuzhiyun 
2578*4882a593Smuzhiyun 	/*
2579*4882a593Smuzhiyun 	 * FIXME:  Make sure this bitmask is cleared when we clear the busReset
2580*4882a593Smuzhiyun 	 * interrupt bit.  Clear physReqResourceAllBuses on bus reset.
2581*4882a593Smuzhiyun 	 */
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun 	spin_lock_irqsave(&ohci->lock, flags);
2584*4882a593Smuzhiyun 
2585*4882a593Smuzhiyun 	if (ohci->generation != generation) {
2586*4882a593Smuzhiyun 		ret = -ESTALE;
2587*4882a593Smuzhiyun 		goto out;
2588*4882a593Smuzhiyun 	}
2589*4882a593Smuzhiyun 
2590*4882a593Smuzhiyun 	/*
2591*4882a593Smuzhiyun 	 * Note, if the node ID contains a non-local bus ID, physical DMA is
2592*4882a593Smuzhiyun 	 * enabled for _all_ nodes on remote buses.
2593*4882a593Smuzhiyun 	 */
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 	n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
2596*4882a593Smuzhiyun 	if (n < 32)
2597*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
2598*4882a593Smuzhiyun 	else
2599*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
2600*4882a593Smuzhiyun 
2601*4882a593Smuzhiyun 	flush_writes(ohci);
2602*4882a593Smuzhiyun  out:
2603*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ohci->lock, flags);
2604*4882a593Smuzhiyun 
2605*4882a593Smuzhiyun 	return ret;
2606*4882a593Smuzhiyun }
2607*4882a593Smuzhiyun 
ohci_read_csr(struct fw_card * card,int csr_offset)2608*4882a593Smuzhiyun static u32 ohci_read_csr(struct fw_card *card, int csr_offset)
2609*4882a593Smuzhiyun {
2610*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
2611*4882a593Smuzhiyun 	unsigned long flags;
2612*4882a593Smuzhiyun 	u32 value;
2613*4882a593Smuzhiyun 
2614*4882a593Smuzhiyun 	switch (csr_offset) {
2615*4882a593Smuzhiyun 	case CSR_STATE_CLEAR:
2616*4882a593Smuzhiyun 	case CSR_STATE_SET:
2617*4882a593Smuzhiyun 		if (ohci->is_root &&
2618*4882a593Smuzhiyun 		    (reg_read(ohci, OHCI1394_LinkControlSet) &
2619*4882a593Smuzhiyun 		     OHCI1394_LinkControl_cycleMaster))
2620*4882a593Smuzhiyun 			value = CSR_STATE_BIT_CMSTR;
2621*4882a593Smuzhiyun 		else
2622*4882a593Smuzhiyun 			value = 0;
2623*4882a593Smuzhiyun 		if (ohci->csr_state_setclear_abdicate)
2624*4882a593Smuzhiyun 			value |= CSR_STATE_BIT_ABDICATE;
2625*4882a593Smuzhiyun 
2626*4882a593Smuzhiyun 		return value;
2627*4882a593Smuzhiyun 
2628*4882a593Smuzhiyun 	case CSR_NODE_IDS:
2629*4882a593Smuzhiyun 		return reg_read(ohci, OHCI1394_NodeID) << 16;
2630*4882a593Smuzhiyun 
2631*4882a593Smuzhiyun 	case CSR_CYCLE_TIME:
2632*4882a593Smuzhiyun 		return get_cycle_time(ohci);
2633*4882a593Smuzhiyun 
2634*4882a593Smuzhiyun 	case CSR_BUS_TIME:
2635*4882a593Smuzhiyun 		/*
2636*4882a593Smuzhiyun 		 * We might be called just after the cycle timer has wrapped
2637*4882a593Smuzhiyun 		 * around but just before the cycle64Seconds handler, so we
2638*4882a593Smuzhiyun 		 * better check here, too, if the bus time needs to be updated.
2639*4882a593Smuzhiyun 		 */
2640*4882a593Smuzhiyun 		spin_lock_irqsave(&ohci->lock, flags);
2641*4882a593Smuzhiyun 		value = update_bus_time(ohci);
2642*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ohci->lock, flags);
2643*4882a593Smuzhiyun 		return value;
2644*4882a593Smuzhiyun 
2645*4882a593Smuzhiyun 	case CSR_BUSY_TIMEOUT:
2646*4882a593Smuzhiyun 		value = reg_read(ohci, OHCI1394_ATRetries);
2647*4882a593Smuzhiyun 		return (value >> 4) & 0x0ffff00f;
2648*4882a593Smuzhiyun 
2649*4882a593Smuzhiyun 	case CSR_PRIORITY_BUDGET:
2650*4882a593Smuzhiyun 		return (reg_read(ohci, OHCI1394_FairnessControl) & 0x3f) |
2651*4882a593Smuzhiyun 			(ohci->pri_req_max << 8);
2652*4882a593Smuzhiyun 
2653*4882a593Smuzhiyun 	default:
2654*4882a593Smuzhiyun 		WARN_ON(1);
2655*4882a593Smuzhiyun 		return 0;
2656*4882a593Smuzhiyun 	}
2657*4882a593Smuzhiyun }
2658*4882a593Smuzhiyun 
ohci_write_csr(struct fw_card * card,int csr_offset,u32 value)2659*4882a593Smuzhiyun static void ohci_write_csr(struct fw_card *card, int csr_offset, u32 value)
2660*4882a593Smuzhiyun {
2661*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
2662*4882a593Smuzhiyun 	unsigned long flags;
2663*4882a593Smuzhiyun 
2664*4882a593Smuzhiyun 	switch (csr_offset) {
2665*4882a593Smuzhiyun 	case CSR_STATE_CLEAR:
2666*4882a593Smuzhiyun 		if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
2667*4882a593Smuzhiyun 			reg_write(ohci, OHCI1394_LinkControlClear,
2668*4882a593Smuzhiyun 				  OHCI1394_LinkControl_cycleMaster);
2669*4882a593Smuzhiyun 			flush_writes(ohci);
2670*4882a593Smuzhiyun 		}
2671*4882a593Smuzhiyun 		if (value & CSR_STATE_BIT_ABDICATE)
2672*4882a593Smuzhiyun 			ohci->csr_state_setclear_abdicate = false;
2673*4882a593Smuzhiyun 		break;
2674*4882a593Smuzhiyun 
2675*4882a593Smuzhiyun 	case CSR_STATE_SET:
2676*4882a593Smuzhiyun 		if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
2677*4882a593Smuzhiyun 			reg_write(ohci, OHCI1394_LinkControlSet,
2678*4882a593Smuzhiyun 				  OHCI1394_LinkControl_cycleMaster);
2679*4882a593Smuzhiyun 			flush_writes(ohci);
2680*4882a593Smuzhiyun 		}
2681*4882a593Smuzhiyun 		if (value & CSR_STATE_BIT_ABDICATE)
2682*4882a593Smuzhiyun 			ohci->csr_state_setclear_abdicate = true;
2683*4882a593Smuzhiyun 		break;
2684*4882a593Smuzhiyun 
2685*4882a593Smuzhiyun 	case CSR_NODE_IDS:
2686*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_NodeID, value >> 16);
2687*4882a593Smuzhiyun 		flush_writes(ohci);
2688*4882a593Smuzhiyun 		break;
2689*4882a593Smuzhiyun 
2690*4882a593Smuzhiyun 	case CSR_CYCLE_TIME:
2691*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsochronousCycleTimer, value);
2692*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IntEventSet,
2693*4882a593Smuzhiyun 			  OHCI1394_cycleInconsistent);
2694*4882a593Smuzhiyun 		flush_writes(ohci);
2695*4882a593Smuzhiyun 		break;
2696*4882a593Smuzhiyun 
2697*4882a593Smuzhiyun 	case CSR_BUS_TIME:
2698*4882a593Smuzhiyun 		spin_lock_irqsave(&ohci->lock, flags);
2699*4882a593Smuzhiyun 		ohci->bus_time = (update_bus_time(ohci) & 0x40) |
2700*4882a593Smuzhiyun 		                 (value & ~0x7f);
2701*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ohci->lock, flags);
2702*4882a593Smuzhiyun 		break;
2703*4882a593Smuzhiyun 
2704*4882a593Smuzhiyun 	case CSR_BUSY_TIMEOUT:
2705*4882a593Smuzhiyun 		value = (value & 0xf) | ((value & 0xf) << 4) |
2706*4882a593Smuzhiyun 			((value & 0xf) << 8) | ((value & 0x0ffff000) << 4);
2707*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_ATRetries, value);
2708*4882a593Smuzhiyun 		flush_writes(ohci);
2709*4882a593Smuzhiyun 		break;
2710*4882a593Smuzhiyun 
2711*4882a593Smuzhiyun 	case CSR_PRIORITY_BUDGET:
2712*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_FairnessControl, value & 0x3f);
2713*4882a593Smuzhiyun 		flush_writes(ohci);
2714*4882a593Smuzhiyun 		break;
2715*4882a593Smuzhiyun 
2716*4882a593Smuzhiyun 	default:
2717*4882a593Smuzhiyun 		WARN_ON(1);
2718*4882a593Smuzhiyun 		break;
2719*4882a593Smuzhiyun 	}
2720*4882a593Smuzhiyun }
2721*4882a593Smuzhiyun 
flush_iso_completions(struct iso_context * ctx)2722*4882a593Smuzhiyun static void flush_iso_completions(struct iso_context *ctx)
2723*4882a593Smuzhiyun {
2724*4882a593Smuzhiyun 	ctx->base.callback.sc(&ctx->base, ctx->last_timestamp,
2725*4882a593Smuzhiyun 			      ctx->header_length, ctx->header,
2726*4882a593Smuzhiyun 			      ctx->base.callback_data);
2727*4882a593Smuzhiyun 	ctx->header_length = 0;
2728*4882a593Smuzhiyun }
2729*4882a593Smuzhiyun 
copy_iso_headers(struct iso_context * ctx,const u32 * dma_hdr)2730*4882a593Smuzhiyun static void copy_iso_headers(struct iso_context *ctx, const u32 *dma_hdr)
2731*4882a593Smuzhiyun {
2732*4882a593Smuzhiyun 	u32 *ctx_hdr;
2733*4882a593Smuzhiyun 
2734*4882a593Smuzhiyun 	if (ctx->header_length + ctx->base.header_size > PAGE_SIZE) {
2735*4882a593Smuzhiyun 		if (ctx->base.drop_overflow_headers)
2736*4882a593Smuzhiyun 			return;
2737*4882a593Smuzhiyun 		flush_iso_completions(ctx);
2738*4882a593Smuzhiyun 	}
2739*4882a593Smuzhiyun 
2740*4882a593Smuzhiyun 	ctx_hdr = ctx->header + ctx->header_length;
2741*4882a593Smuzhiyun 	ctx->last_timestamp = (u16)le32_to_cpu((__force __le32)dma_hdr[0]);
2742*4882a593Smuzhiyun 
2743*4882a593Smuzhiyun 	/*
2744*4882a593Smuzhiyun 	 * The two iso header quadlets are byteswapped to little
2745*4882a593Smuzhiyun 	 * endian by the controller, but we want to present them
2746*4882a593Smuzhiyun 	 * as big endian for consistency with the bus endianness.
2747*4882a593Smuzhiyun 	 */
2748*4882a593Smuzhiyun 	if (ctx->base.header_size > 0)
2749*4882a593Smuzhiyun 		ctx_hdr[0] = swab32(dma_hdr[1]); /* iso packet header */
2750*4882a593Smuzhiyun 	if (ctx->base.header_size > 4)
2751*4882a593Smuzhiyun 		ctx_hdr[1] = swab32(dma_hdr[0]); /* timestamp */
2752*4882a593Smuzhiyun 	if (ctx->base.header_size > 8)
2753*4882a593Smuzhiyun 		memcpy(&ctx_hdr[2], &dma_hdr[2], ctx->base.header_size - 8);
2754*4882a593Smuzhiyun 	ctx->header_length += ctx->base.header_size;
2755*4882a593Smuzhiyun }
2756*4882a593Smuzhiyun 
handle_ir_packet_per_buffer(struct context * context,struct descriptor * d,struct descriptor * last)2757*4882a593Smuzhiyun static int handle_ir_packet_per_buffer(struct context *context,
2758*4882a593Smuzhiyun 				       struct descriptor *d,
2759*4882a593Smuzhiyun 				       struct descriptor *last)
2760*4882a593Smuzhiyun {
2761*4882a593Smuzhiyun 	struct iso_context *ctx =
2762*4882a593Smuzhiyun 		container_of(context, struct iso_context, context);
2763*4882a593Smuzhiyun 	struct descriptor *pd;
2764*4882a593Smuzhiyun 	u32 buffer_dma;
2765*4882a593Smuzhiyun 
2766*4882a593Smuzhiyun 	for (pd = d; pd <= last; pd++)
2767*4882a593Smuzhiyun 		if (pd->transfer_status)
2768*4882a593Smuzhiyun 			break;
2769*4882a593Smuzhiyun 	if (pd > last)
2770*4882a593Smuzhiyun 		/* Descriptor(s) not done yet, stop iteration */
2771*4882a593Smuzhiyun 		return 0;
2772*4882a593Smuzhiyun 
2773*4882a593Smuzhiyun 	while (!(d->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))) {
2774*4882a593Smuzhiyun 		d++;
2775*4882a593Smuzhiyun 		buffer_dma = le32_to_cpu(d->data_address);
2776*4882a593Smuzhiyun 		dma_sync_single_range_for_cpu(context->ohci->card.device,
2777*4882a593Smuzhiyun 					      buffer_dma & PAGE_MASK,
2778*4882a593Smuzhiyun 					      buffer_dma & ~PAGE_MASK,
2779*4882a593Smuzhiyun 					      le16_to_cpu(d->req_count),
2780*4882a593Smuzhiyun 					      DMA_FROM_DEVICE);
2781*4882a593Smuzhiyun 	}
2782*4882a593Smuzhiyun 
2783*4882a593Smuzhiyun 	copy_iso_headers(ctx, (u32 *) (last + 1));
2784*4882a593Smuzhiyun 
2785*4882a593Smuzhiyun 	if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
2786*4882a593Smuzhiyun 		flush_iso_completions(ctx);
2787*4882a593Smuzhiyun 
2788*4882a593Smuzhiyun 	return 1;
2789*4882a593Smuzhiyun }
2790*4882a593Smuzhiyun 
2791*4882a593Smuzhiyun /* d == last because each descriptor block is only a single descriptor. */
handle_ir_buffer_fill(struct context * context,struct descriptor * d,struct descriptor * last)2792*4882a593Smuzhiyun static int handle_ir_buffer_fill(struct context *context,
2793*4882a593Smuzhiyun 				 struct descriptor *d,
2794*4882a593Smuzhiyun 				 struct descriptor *last)
2795*4882a593Smuzhiyun {
2796*4882a593Smuzhiyun 	struct iso_context *ctx =
2797*4882a593Smuzhiyun 		container_of(context, struct iso_context, context);
2798*4882a593Smuzhiyun 	unsigned int req_count, res_count, completed;
2799*4882a593Smuzhiyun 	u32 buffer_dma;
2800*4882a593Smuzhiyun 
2801*4882a593Smuzhiyun 	req_count = le16_to_cpu(last->req_count);
2802*4882a593Smuzhiyun 	res_count = le16_to_cpu(READ_ONCE(last->res_count));
2803*4882a593Smuzhiyun 	completed = req_count - res_count;
2804*4882a593Smuzhiyun 	buffer_dma = le32_to_cpu(last->data_address);
2805*4882a593Smuzhiyun 
2806*4882a593Smuzhiyun 	if (completed > 0) {
2807*4882a593Smuzhiyun 		ctx->mc_buffer_bus = buffer_dma;
2808*4882a593Smuzhiyun 		ctx->mc_completed = completed;
2809*4882a593Smuzhiyun 	}
2810*4882a593Smuzhiyun 
2811*4882a593Smuzhiyun 	if (res_count != 0)
2812*4882a593Smuzhiyun 		/* Descriptor(s) not done yet, stop iteration */
2813*4882a593Smuzhiyun 		return 0;
2814*4882a593Smuzhiyun 
2815*4882a593Smuzhiyun 	dma_sync_single_range_for_cpu(context->ohci->card.device,
2816*4882a593Smuzhiyun 				      buffer_dma & PAGE_MASK,
2817*4882a593Smuzhiyun 				      buffer_dma & ~PAGE_MASK,
2818*4882a593Smuzhiyun 				      completed, DMA_FROM_DEVICE);
2819*4882a593Smuzhiyun 
2820*4882a593Smuzhiyun 	if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) {
2821*4882a593Smuzhiyun 		ctx->base.callback.mc(&ctx->base,
2822*4882a593Smuzhiyun 				      buffer_dma + completed,
2823*4882a593Smuzhiyun 				      ctx->base.callback_data);
2824*4882a593Smuzhiyun 		ctx->mc_completed = 0;
2825*4882a593Smuzhiyun 	}
2826*4882a593Smuzhiyun 
2827*4882a593Smuzhiyun 	return 1;
2828*4882a593Smuzhiyun }
2829*4882a593Smuzhiyun 
flush_ir_buffer_fill(struct iso_context * ctx)2830*4882a593Smuzhiyun static void flush_ir_buffer_fill(struct iso_context *ctx)
2831*4882a593Smuzhiyun {
2832*4882a593Smuzhiyun 	dma_sync_single_range_for_cpu(ctx->context.ohci->card.device,
2833*4882a593Smuzhiyun 				      ctx->mc_buffer_bus & PAGE_MASK,
2834*4882a593Smuzhiyun 				      ctx->mc_buffer_bus & ~PAGE_MASK,
2835*4882a593Smuzhiyun 				      ctx->mc_completed, DMA_FROM_DEVICE);
2836*4882a593Smuzhiyun 
2837*4882a593Smuzhiyun 	ctx->base.callback.mc(&ctx->base,
2838*4882a593Smuzhiyun 			      ctx->mc_buffer_bus + ctx->mc_completed,
2839*4882a593Smuzhiyun 			      ctx->base.callback_data);
2840*4882a593Smuzhiyun 	ctx->mc_completed = 0;
2841*4882a593Smuzhiyun }
2842*4882a593Smuzhiyun 
sync_it_packet_for_cpu(struct context * context,struct descriptor * pd)2843*4882a593Smuzhiyun static inline void sync_it_packet_for_cpu(struct context *context,
2844*4882a593Smuzhiyun 					  struct descriptor *pd)
2845*4882a593Smuzhiyun {
2846*4882a593Smuzhiyun 	__le16 control;
2847*4882a593Smuzhiyun 	u32 buffer_dma;
2848*4882a593Smuzhiyun 
2849*4882a593Smuzhiyun 	/* only packets beginning with OUTPUT_MORE* have data buffers */
2850*4882a593Smuzhiyun 	if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
2851*4882a593Smuzhiyun 		return;
2852*4882a593Smuzhiyun 
2853*4882a593Smuzhiyun 	/* skip over the OUTPUT_MORE_IMMEDIATE descriptor */
2854*4882a593Smuzhiyun 	pd += 2;
2855*4882a593Smuzhiyun 
2856*4882a593Smuzhiyun 	/*
2857*4882a593Smuzhiyun 	 * If the packet has a header, the first OUTPUT_MORE/LAST descriptor's
2858*4882a593Smuzhiyun 	 * data buffer is in the context program's coherent page and must not
2859*4882a593Smuzhiyun 	 * be synced.
2860*4882a593Smuzhiyun 	 */
2861*4882a593Smuzhiyun 	if ((le32_to_cpu(pd->data_address) & PAGE_MASK) ==
2862*4882a593Smuzhiyun 	    (context->current_bus          & PAGE_MASK)) {
2863*4882a593Smuzhiyun 		if (pd->control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS))
2864*4882a593Smuzhiyun 			return;
2865*4882a593Smuzhiyun 		pd++;
2866*4882a593Smuzhiyun 	}
2867*4882a593Smuzhiyun 
2868*4882a593Smuzhiyun 	do {
2869*4882a593Smuzhiyun 		buffer_dma = le32_to_cpu(pd->data_address);
2870*4882a593Smuzhiyun 		dma_sync_single_range_for_cpu(context->ohci->card.device,
2871*4882a593Smuzhiyun 					      buffer_dma & PAGE_MASK,
2872*4882a593Smuzhiyun 					      buffer_dma & ~PAGE_MASK,
2873*4882a593Smuzhiyun 					      le16_to_cpu(pd->req_count),
2874*4882a593Smuzhiyun 					      DMA_TO_DEVICE);
2875*4882a593Smuzhiyun 		control = pd->control;
2876*4882a593Smuzhiyun 		pd++;
2877*4882a593Smuzhiyun 	} while (!(control & cpu_to_le16(DESCRIPTOR_BRANCH_ALWAYS)));
2878*4882a593Smuzhiyun }
2879*4882a593Smuzhiyun 
handle_it_packet(struct context * context,struct descriptor * d,struct descriptor * last)2880*4882a593Smuzhiyun static int handle_it_packet(struct context *context,
2881*4882a593Smuzhiyun 			    struct descriptor *d,
2882*4882a593Smuzhiyun 			    struct descriptor *last)
2883*4882a593Smuzhiyun {
2884*4882a593Smuzhiyun 	struct iso_context *ctx =
2885*4882a593Smuzhiyun 		container_of(context, struct iso_context, context);
2886*4882a593Smuzhiyun 	struct descriptor *pd;
2887*4882a593Smuzhiyun 	__be32 *ctx_hdr;
2888*4882a593Smuzhiyun 
2889*4882a593Smuzhiyun 	for (pd = d; pd <= last; pd++)
2890*4882a593Smuzhiyun 		if (pd->transfer_status)
2891*4882a593Smuzhiyun 			break;
2892*4882a593Smuzhiyun 	if (pd > last)
2893*4882a593Smuzhiyun 		/* Descriptor(s) not done yet, stop iteration */
2894*4882a593Smuzhiyun 		return 0;
2895*4882a593Smuzhiyun 
2896*4882a593Smuzhiyun 	sync_it_packet_for_cpu(context, d);
2897*4882a593Smuzhiyun 
2898*4882a593Smuzhiyun 	if (ctx->header_length + 4 > PAGE_SIZE) {
2899*4882a593Smuzhiyun 		if (ctx->base.drop_overflow_headers)
2900*4882a593Smuzhiyun 			return 1;
2901*4882a593Smuzhiyun 		flush_iso_completions(ctx);
2902*4882a593Smuzhiyun 	}
2903*4882a593Smuzhiyun 
2904*4882a593Smuzhiyun 	ctx_hdr = ctx->header + ctx->header_length;
2905*4882a593Smuzhiyun 	ctx->last_timestamp = le16_to_cpu(last->res_count);
2906*4882a593Smuzhiyun 	/* Present this value as big-endian to match the receive code */
2907*4882a593Smuzhiyun 	*ctx_hdr = cpu_to_be32((le16_to_cpu(pd->transfer_status) << 16) |
2908*4882a593Smuzhiyun 			       le16_to_cpu(pd->res_count));
2909*4882a593Smuzhiyun 	ctx->header_length += 4;
2910*4882a593Smuzhiyun 
2911*4882a593Smuzhiyun 	if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS))
2912*4882a593Smuzhiyun 		flush_iso_completions(ctx);
2913*4882a593Smuzhiyun 
2914*4882a593Smuzhiyun 	return 1;
2915*4882a593Smuzhiyun }
2916*4882a593Smuzhiyun 
set_multichannel_mask(struct fw_ohci * ohci,u64 channels)2917*4882a593Smuzhiyun static void set_multichannel_mask(struct fw_ohci *ohci, u64 channels)
2918*4882a593Smuzhiyun {
2919*4882a593Smuzhiyun 	u32 hi = channels >> 32, lo = channels;
2920*4882a593Smuzhiyun 
2921*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IRMultiChanMaskHiClear, ~hi);
2922*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IRMultiChanMaskLoClear, ~lo);
2923*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IRMultiChanMaskHiSet, hi);
2924*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IRMultiChanMaskLoSet, lo);
2925*4882a593Smuzhiyun 	ohci->mc_channels = channels;
2926*4882a593Smuzhiyun }
2927*4882a593Smuzhiyun 
ohci_allocate_iso_context(struct fw_card * card,int type,int channel,size_t header_size)2928*4882a593Smuzhiyun static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
2929*4882a593Smuzhiyun 				int type, int channel, size_t header_size)
2930*4882a593Smuzhiyun {
2931*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(card);
2932*4882a593Smuzhiyun 	struct iso_context *ctx;
2933*4882a593Smuzhiyun 	descriptor_callback_t callback;
2934*4882a593Smuzhiyun 	u64 *channels;
2935*4882a593Smuzhiyun 	u32 *mask, regs;
2936*4882a593Smuzhiyun 	int index, ret = -EBUSY;
2937*4882a593Smuzhiyun 
2938*4882a593Smuzhiyun 	spin_lock_irq(&ohci->lock);
2939*4882a593Smuzhiyun 
2940*4882a593Smuzhiyun 	switch (type) {
2941*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_TRANSMIT:
2942*4882a593Smuzhiyun 		mask     = &ohci->it_context_mask;
2943*4882a593Smuzhiyun 		callback = handle_it_packet;
2944*4882a593Smuzhiyun 		index    = ffs(*mask) - 1;
2945*4882a593Smuzhiyun 		if (index >= 0) {
2946*4882a593Smuzhiyun 			*mask &= ~(1 << index);
2947*4882a593Smuzhiyun 			regs = OHCI1394_IsoXmitContextBase(index);
2948*4882a593Smuzhiyun 			ctx  = &ohci->it_context_list[index];
2949*4882a593Smuzhiyun 		}
2950*4882a593Smuzhiyun 		break;
2951*4882a593Smuzhiyun 
2952*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE:
2953*4882a593Smuzhiyun 		channels = &ohci->ir_context_channels;
2954*4882a593Smuzhiyun 		mask     = &ohci->ir_context_mask;
2955*4882a593Smuzhiyun 		callback = handle_ir_packet_per_buffer;
2956*4882a593Smuzhiyun 		index    = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1;
2957*4882a593Smuzhiyun 		if (index >= 0) {
2958*4882a593Smuzhiyun 			*channels &= ~(1ULL << channel);
2959*4882a593Smuzhiyun 			*mask     &= ~(1 << index);
2960*4882a593Smuzhiyun 			regs = OHCI1394_IsoRcvContextBase(index);
2961*4882a593Smuzhiyun 			ctx  = &ohci->ir_context_list[index];
2962*4882a593Smuzhiyun 		}
2963*4882a593Smuzhiyun 		break;
2964*4882a593Smuzhiyun 
2965*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
2966*4882a593Smuzhiyun 		mask     = &ohci->ir_context_mask;
2967*4882a593Smuzhiyun 		callback = handle_ir_buffer_fill;
2968*4882a593Smuzhiyun 		index    = !ohci->mc_allocated ? ffs(*mask) - 1 : -1;
2969*4882a593Smuzhiyun 		if (index >= 0) {
2970*4882a593Smuzhiyun 			ohci->mc_allocated = true;
2971*4882a593Smuzhiyun 			*mask &= ~(1 << index);
2972*4882a593Smuzhiyun 			regs = OHCI1394_IsoRcvContextBase(index);
2973*4882a593Smuzhiyun 			ctx  = &ohci->ir_context_list[index];
2974*4882a593Smuzhiyun 		}
2975*4882a593Smuzhiyun 		break;
2976*4882a593Smuzhiyun 
2977*4882a593Smuzhiyun 	default:
2978*4882a593Smuzhiyun 		index = -1;
2979*4882a593Smuzhiyun 		ret = -ENOSYS;
2980*4882a593Smuzhiyun 	}
2981*4882a593Smuzhiyun 
2982*4882a593Smuzhiyun 	spin_unlock_irq(&ohci->lock);
2983*4882a593Smuzhiyun 
2984*4882a593Smuzhiyun 	if (index < 0)
2985*4882a593Smuzhiyun 		return ERR_PTR(ret);
2986*4882a593Smuzhiyun 
2987*4882a593Smuzhiyun 	memset(ctx, 0, sizeof(*ctx));
2988*4882a593Smuzhiyun 	ctx->header_length = 0;
2989*4882a593Smuzhiyun 	ctx->header = (void *) __get_free_page(GFP_KERNEL);
2990*4882a593Smuzhiyun 	if (ctx->header == NULL) {
2991*4882a593Smuzhiyun 		ret = -ENOMEM;
2992*4882a593Smuzhiyun 		goto out;
2993*4882a593Smuzhiyun 	}
2994*4882a593Smuzhiyun 	ret = context_init(&ctx->context, ohci, regs, callback);
2995*4882a593Smuzhiyun 	if (ret < 0)
2996*4882a593Smuzhiyun 		goto out_with_header;
2997*4882a593Smuzhiyun 
2998*4882a593Smuzhiyun 	if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL) {
2999*4882a593Smuzhiyun 		set_multichannel_mask(ohci, 0);
3000*4882a593Smuzhiyun 		ctx->mc_completed = 0;
3001*4882a593Smuzhiyun 	}
3002*4882a593Smuzhiyun 
3003*4882a593Smuzhiyun 	return &ctx->base;
3004*4882a593Smuzhiyun 
3005*4882a593Smuzhiyun  out_with_header:
3006*4882a593Smuzhiyun 	free_page((unsigned long)ctx->header);
3007*4882a593Smuzhiyun  out:
3008*4882a593Smuzhiyun 	spin_lock_irq(&ohci->lock);
3009*4882a593Smuzhiyun 
3010*4882a593Smuzhiyun 	switch (type) {
3011*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE:
3012*4882a593Smuzhiyun 		*channels |= 1ULL << channel;
3013*4882a593Smuzhiyun 		break;
3014*4882a593Smuzhiyun 
3015*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
3016*4882a593Smuzhiyun 		ohci->mc_allocated = false;
3017*4882a593Smuzhiyun 		break;
3018*4882a593Smuzhiyun 	}
3019*4882a593Smuzhiyun 	*mask |= 1 << index;
3020*4882a593Smuzhiyun 
3021*4882a593Smuzhiyun 	spin_unlock_irq(&ohci->lock);
3022*4882a593Smuzhiyun 
3023*4882a593Smuzhiyun 	return ERR_PTR(ret);
3024*4882a593Smuzhiyun }
3025*4882a593Smuzhiyun 
ohci_start_iso(struct fw_iso_context * base,s32 cycle,u32 sync,u32 tags)3026*4882a593Smuzhiyun static int ohci_start_iso(struct fw_iso_context *base,
3027*4882a593Smuzhiyun 			  s32 cycle, u32 sync, u32 tags)
3028*4882a593Smuzhiyun {
3029*4882a593Smuzhiyun 	struct iso_context *ctx = container_of(base, struct iso_context, base);
3030*4882a593Smuzhiyun 	struct fw_ohci *ohci = ctx->context.ohci;
3031*4882a593Smuzhiyun 	u32 control = IR_CONTEXT_ISOCH_HEADER, match;
3032*4882a593Smuzhiyun 	int index;
3033*4882a593Smuzhiyun 
3034*4882a593Smuzhiyun 	/* the controller cannot start without any queued packets */
3035*4882a593Smuzhiyun 	if (ctx->context.last->branch_address == 0)
3036*4882a593Smuzhiyun 		return -ENODATA;
3037*4882a593Smuzhiyun 
3038*4882a593Smuzhiyun 	switch (ctx->base.type) {
3039*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_TRANSMIT:
3040*4882a593Smuzhiyun 		index = ctx - ohci->it_context_list;
3041*4882a593Smuzhiyun 		match = 0;
3042*4882a593Smuzhiyun 		if (cycle >= 0)
3043*4882a593Smuzhiyun 			match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
3044*4882a593Smuzhiyun 				(cycle & 0x7fff) << 16;
3045*4882a593Smuzhiyun 
3046*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
3047*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
3048*4882a593Smuzhiyun 		context_run(&ctx->context, match);
3049*4882a593Smuzhiyun 		break;
3050*4882a593Smuzhiyun 
3051*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
3052*4882a593Smuzhiyun 		control |= IR_CONTEXT_BUFFER_FILL|IR_CONTEXT_MULTI_CHANNEL_MODE;
3053*4882a593Smuzhiyun 		fallthrough;
3054*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE:
3055*4882a593Smuzhiyun 		index = ctx - ohci->ir_context_list;
3056*4882a593Smuzhiyun 		match = (tags << 28) | (sync << 8) | ctx->base.channel;
3057*4882a593Smuzhiyun 		if (cycle >= 0) {
3058*4882a593Smuzhiyun 			match |= (cycle & 0x07fff) << 12;
3059*4882a593Smuzhiyun 			control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
3060*4882a593Smuzhiyun 		}
3061*4882a593Smuzhiyun 
3062*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
3063*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
3064*4882a593Smuzhiyun 		reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
3065*4882a593Smuzhiyun 		context_run(&ctx->context, control);
3066*4882a593Smuzhiyun 
3067*4882a593Smuzhiyun 		ctx->sync = sync;
3068*4882a593Smuzhiyun 		ctx->tags = tags;
3069*4882a593Smuzhiyun 
3070*4882a593Smuzhiyun 		break;
3071*4882a593Smuzhiyun 	}
3072*4882a593Smuzhiyun 
3073*4882a593Smuzhiyun 	return 0;
3074*4882a593Smuzhiyun }
3075*4882a593Smuzhiyun 
ohci_stop_iso(struct fw_iso_context * base)3076*4882a593Smuzhiyun static int ohci_stop_iso(struct fw_iso_context *base)
3077*4882a593Smuzhiyun {
3078*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(base->card);
3079*4882a593Smuzhiyun 	struct iso_context *ctx = container_of(base, struct iso_context, base);
3080*4882a593Smuzhiyun 	int index;
3081*4882a593Smuzhiyun 
3082*4882a593Smuzhiyun 	switch (ctx->base.type) {
3083*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_TRANSMIT:
3084*4882a593Smuzhiyun 		index = ctx - ohci->it_context_list;
3085*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
3086*4882a593Smuzhiyun 		break;
3087*4882a593Smuzhiyun 
3088*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE:
3089*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
3090*4882a593Smuzhiyun 		index = ctx - ohci->ir_context_list;
3091*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
3092*4882a593Smuzhiyun 		break;
3093*4882a593Smuzhiyun 	}
3094*4882a593Smuzhiyun 	flush_writes(ohci);
3095*4882a593Smuzhiyun 	context_stop(&ctx->context);
3096*4882a593Smuzhiyun 	tasklet_kill(&ctx->context.tasklet);
3097*4882a593Smuzhiyun 
3098*4882a593Smuzhiyun 	return 0;
3099*4882a593Smuzhiyun }
3100*4882a593Smuzhiyun 
ohci_free_iso_context(struct fw_iso_context * base)3101*4882a593Smuzhiyun static void ohci_free_iso_context(struct fw_iso_context *base)
3102*4882a593Smuzhiyun {
3103*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(base->card);
3104*4882a593Smuzhiyun 	struct iso_context *ctx = container_of(base, struct iso_context, base);
3105*4882a593Smuzhiyun 	unsigned long flags;
3106*4882a593Smuzhiyun 	int index;
3107*4882a593Smuzhiyun 
3108*4882a593Smuzhiyun 	ohci_stop_iso(base);
3109*4882a593Smuzhiyun 	context_release(&ctx->context);
3110*4882a593Smuzhiyun 	free_page((unsigned long)ctx->header);
3111*4882a593Smuzhiyun 
3112*4882a593Smuzhiyun 	spin_lock_irqsave(&ohci->lock, flags);
3113*4882a593Smuzhiyun 
3114*4882a593Smuzhiyun 	switch (base->type) {
3115*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_TRANSMIT:
3116*4882a593Smuzhiyun 		index = ctx - ohci->it_context_list;
3117*4882a593Smuzhiyun 		ohci->it_context_mask |= 1 << index;
3118*4882a593Smuzhiyun 		break;
3119*4882a593Smuzhiyun 
3120*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE:
3121*4882a593Smuzhiyun 		index = ctx - ohci->ir_context_list;
3122*4882a593Smuzhiyun 		ohci->ir_context_mask |= 1 << index;
3123*4882a593Smuzhiyun 		ohci->ir_context_channels |= 1ULL << base->channel;
3124*4882a593Smuzhiyun 		break;
3125*4882a593Smuzhiyun 
3126*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
3127*4882a593Smuzhiyun 		index = ctx - ohci->ir_context_list;
3128*4882a593Smuzhiyun 		ohci->ir_context_mask |= 1 << index;
3129*4882a593Smuzhiyun 		ohci->ir_context_channels |= ohci->mc_channels;
3130*4882a593Smuzhiyun 		ohci->mc_channels = 0;
3131*4882a593Smuzhiyun 		ohci->mc_allocated = false;
3132*4882a593Smuzhiyun 		break;
3133*4882a593Smuzhiyun 	}
3134*4882a593Smuzhiyun 
3135*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ohci->lock, flags);
3136*4882a593Smuzhiyun }
3137*4882a593Smuzhiyun 
ohci_set_iso_channels(struct fw_iso_context * base,u64 * channels)3138*4882a593Smuzhiyun static int ohci_set_iso_channels(struct fw_iso_context *base, u64 *channels)
3139*4882a593Smuzhiyun {
3140*4882a593Smuzhiyun 	struct fw_ohci *ohci = fw_ohci(base->card);
3141*4882a593Smuzhiyun 	unsigned long flags;
3142*4882a593Smuzhiyun 	int ret;
3143*4882a593Smuzhiyun 
3144*4882a593Smuzhiyun 	switch (base->type) {
3145*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
3146*4882a593Smuzhiyun 
3147*4882a593Smuzhiyun 		spin_lock_irqsave(&ohci->lock, flags);
3148*4882a593Smuzhiyun 
3149*4882a593Smuzhiyun 		/* Don't allow multichannel to grab other contexts' channels. */
3150*4882a593Smuzhiyun 		if (~ohci->ir_context_channels & ~ohci->mc_channels & *channels) {
3151*4882a593Smuzhiyun 			*channels = ohci->ir_context_channels;
3152*4882a593Smuzhiyun 			ret = -EBUSY;
3153*4882a593Smuzhiyun 		} else {
3154*4882a593Smuzhiyun 			set_multichannel_mask(ohci, *channels);
3155*4882a593Smuzhiyun 			ret = 0;
3156*4882a593Smuzhiyun 		}
3157*4882a593Smuzhiyun 
3158*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ohci->lock, flags);
3159*4882a593Smuzhiyun 
3160*4882a593Smuzhiyun 		break;
3161*4882a593Smuzhiyun 	default:
3162*4882a593Smuzhiyun 		ret = -EINVAL;
3163*4882a593Smuzhiyun 	}
3164*4882a593Smuzhiyun 
3165*4882a593Smuzhiyun 	return ret;
3166*4882a593Smuzhiyun }
3167*4882a593Smuzhiyun 
3168*4882a593Smuzhiyun #ifdef CONFIG_PM
ohci_resume_iso_dma(struct fw_ohci * ohci)3169*4882a593Smuzhiyun static void ohci_resume_iso_dma(struct fw_ohci *ohci)
3170*4882a593Smuzhiyun {
3171*4882a593Smuzhiyun 	int i;
3172*4882a593Smuzhiyun 	struct iso_context *ctx;
3173*4882a593Smuzhiyun 
3174*4882a593Smuzhiyun 	for (i = 0 ; i < ohci->n_ir ; i++) {
3175*4882a593Smuzhiyun 		ctx = &ohci->ir_context_list[i];
3176*4882a593Smuzhiyun 		if (ctx->context.running)
3177*4882a593Smuzhiyun 			ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags);
3178*4882a593Smuzhiyun 	}
3179*4882a593Smuzhiyun 
3180*4882a593Smuzhiyun 	for (i = 0 ; i < ohci->n_it ; i++) {
3181*4882a593Smuzhiyun 		ctx = &ohci->it_context_list[i];
3182*4882a593Smuzhiyun 		if (ctx->context.running)
3183*4882a593Smuzhiyun 			ohci_start_iso(&ctx->base, 0, ctx->sync, ctx->tags);
3184*4882a593Smuzhiyun 	}
3185*4882a593Smuzhiyun }
3186*4882a593Smuzhiyun #endif
3187*4882a593Smuzhiyun 
queue_iso_transmit(struct iso_context * ctx,struct fw_iso_packet * packet,struct fw_iso_buffer * buffer,unsigned long payload)3188*4882a593Smuzhiyun static int queue_iso_transmit(struct iso_context *ctx,
3189*4882a593Smuzhiyun 			      struct fw_iso_packet *packet,
3190*4882a593Smuzhiyun 			      struct fw_iso_buffer *buffer,
3191*4882a593Smuzhiyun 			      unsigned long payload)
3192*4882a593Smuzhiyun {
3193*4882a593Smuzhiyun 	struct descriptor *d, *last, *pd;
3194*4882a593Smuzhiyun 	struct fw_iso_packet *p;
3195*4882a593Smuzhiyun 	__le32 *header;
3196*4882a593Smuzhiyun 	dma_addr_t d_bus, page_bus;
3197*4882a593Smuzhiyun 	u32 z, header_z, payload_z, irq;
3198*4882a593Smuzhiyun 	u32 payload_index, payload_end_index, next_page_index;
3199*4882a593Smuzhiyun 	int page, end_page, i, length, offset;
3200*4882a593Smuzhiyun 
3201*4882a593Smuzhiyun 	p = packet;
3202*4882a593Smuzhiyun 	payload_index = payload;
3203*4882a593Smuzhiyun 
3204*4882a593Smuzhiyun 	if (p->skip)
3205*4882a593Smuzhiyun 		z = 1;
3206*4882a593Smuzhiyun 	else
3207*4882a593Smuzhiyun 		z = 2;
3208*4882a593Smuzhiyun 	if (p->header_length > 0)
3209*4882a593Smuzhiyun 		z++;
3210*4882a593Smuzhiyun 
3211*4882a593Smuzhiyun 	/* Determine the first page the payload isn't contained in. */
3212*4882a593Smuzhiyun 	end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
3213*4882a593Smuzhiyun 	if (p->payload_length > 0)
3214*4882a593Smuzhiyun 		payload_z = end_page - (payload_index >> PAGE_SHIFT);
3215*4882a593Smuzhiyun 	else
3216*4882a593Smuzhiyun 		payload_z = 0;
3217*4882a593Smuzhiyun 
3218*4882a593Smuzhiyun 	z += payload_z;
3219*4882a593Smuzhiyun 
3220*4882a593Smuzhiyun 	/* Get header size in number of descriptors. */
3221*4882a593Smuzhiyun 	header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
3222*4882a593Smuzhiyun 
3223*4882a593Smuzhiyun 	d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
3224*4882a593Smuzhiyun 	if (d == NULL)
3225*4882a593Smuzhiyun 		return -ENOMEM;
3226*4882a593Smuzhiyun 
3227*4882a593Smuzhiyun 	if (!p->skip) {
3228*4882a593Smuzhiyun 		d[0].control   = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
3229*4882a593Smuzhiyun 		d[0].req_count = cpu_to_le16(8);
3230*4882a593Smuzhiyun 		/*
3231*4882a593Smuzhiyun 		 * Link the skip address to this descriptor itself.  This causes
3232*4882a593Smuzhiyun 		 * a context to skip a cycle whenever lost cycles or FIFO
3233*4882a593Smuzhiyun 		 * overruns occur, without dropping the data.  The application
3234*4882a593Smuzhiyun 		 * should then decide whether this is an error condition or not.
3235*4882a593Smuzhiyun 		 * FIXME:  Make the context's cycle-lost behaviour configurable?
3236*4882a593Smuzhiyun 		 */
3237*4882a593Smuzhiyun 		d[0].branch_address = cpu_to_le32(d_bus | z);
3238*4882a593Smuzhiyun 
3239*4882a593Smuzhiyun 		header = (__le32 *) &d[1];
3240*4882a593Smuzhiyun 		header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
3241*4882a593Smuzhiyun 					IT_HEADER_TAG(p->tag) |
3242*4882a593Smuzhiyun 					IT_HEADER_TCODE(TCODE_STREAM_DATA) |
3243*4882a593Smuzhiyun 					IT_HEADER_CHANNEL(ctx->base.channel) |
3244*4882a593Smuzhiyun 					IT_HEADER_SPEED(ctx->base.speed));
3245*4882a593Smuzhiyun 		header[1] =
3246*4882a593Smuzhiyun 			cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
3247*4882a593Smuzhiyun 							  p->payload_length));
3248*4882a593Smuzhiyun 	}
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun 	if (p->header_length > 0) {
3251*4882a593Smuzhiyun 		d[2].req_count    = cpu_to_le16(p->header_length);
3252*4882a593Smuzhiyun 		d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
3253*4882a593Smuzhiyun 		memcpy(&d[z], p->header, p->header_length);
3254*4882a593Smuzhiyun 	}
3255*4882a593Smuzhiyun 
3256*4882a593Smuzhiyun 	pd = d + z - payload_z;
3257*4882a593Smuzhiyun 	payload_end_index = payload_index + p->payload_length;
3258*4882a593Smuzhiyun 	for (i = 0; i < payload_z; i++) {
3259*4882a593Smuzhiyun 		page               = payload_index >> PAGE_SHIFT;
3260*4882a593Smuzhiyun 		offset             = payload_index & ~PAGE_MASK;
3261*4882a593Smuzhiyun 		next_page_index    = (page + 1) << PAGE_SHIFT;
3262*4882a593Smuzhiyun 		length             =
3263*4882a593Smuzhiyun 			min(next_page_index, payload_end_index) - payload_index;
3264*4882a593Smuzhiyun 		pd[i].req_count    = cpu_to_le16(length);
3265*4882a593Smuzhiyun 
3266*4882a593Smuzhiyun 		page_bus = page_private(buffer->pages[page]);
3267*4882a593Smuzhiyun 		pd[i].data_address = cpu_to_le32(page_bus + offset);
3268*4882a593Smuzhiyun 
3269*4882a593Smuzhiyun 		dma_sync_single_range_for_device(ctx->context.ohci->card.device,
3270*4882a593Smuzhiyun 						 page_bus, offset, length,
3271*4882a593Smuzhiyun 						 DMA_TO_DEVICE);
3272*4882a593Smuzhiyun 
3273*4882a593Smuzhiyun 		payload_index += length;
3274*4882a593Smuzhiyun 	}
3275*4882a593Smuzhiyun 
3276*4882a593Smuzhiyun 	if (p->interrupt)
3277*4882a593Smuzhiyun 		irq = DESCRIPTOR_IRQ_ALWAYS;
3278*4882a593Smuzhiyun 	else
3279*4882a593Smuzhiyun 		irq = DESCRIPTOR_NO_IRQ;
3280*4882a593Smuzhiyun 
3281*4882a593Smuzhiyun 	last = z == 2 ? d : d + z - 1;
3282*4882a593Smuzhiyun 	last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
3283*4882a593Smuzhiyun 				     DESCRIPTOR_STATUS |
3284*4882a593Smuzhiyun 				     DESCRIPTOR_BRANCH_ALWAYS |
3285*4882a593Smuzhiyun 				     irq);
3286*4882a593Smuzhiyun 
3287*4882a593Smuzhiyun 	context_append(&ctx->context, d, z, header_z);
3288*4882a593Smuzhiyun 
3289*4882a593Smuzhiyun 	return 0;
3290*4882a593Smuzhiyun }
3291*4882a593Smuzhiyun 
queue_iso_packet_per_buffer(struct iso_context * ctx,struct fw_iso_packet * packet,struct fw_iso_buffer * buffer,unsigned long payload)3292*4882a593Smuzhiyun static int queue_iso_packet_per_buffer(struct iso_context *ctx,
3293*4882a593Smuzhiyun 				       struct fw_iso_packet *packet,
3294*4882a593Smuzhiyun 				       struct fw_iso_buffer *buffer,
3295*4882a593Smuzhiyun 				       unsigned long payload)
3296*4882a593Smuzhiyun {
3297*4882a593Smuzhiyun 	struct device *device = ctx->context.ohci->card.device;
3298*4882a593Smuzhiyun 	struct descriptor *d, *pd;
3299*4882a593Smuzhiyun 	dma_addr_t d_bus, page_bus;
3300*4882a593Smuzhiyun 	u32 z, header_z, rest;
3301*4882a593Smuzhiyun 	int i, j, length;
3302*4882a593Smuzhiyun 	int page, offset, packet_count, header_size, payload_per_buffer;
3303*4882a593Smuzhiyun 
3304*4882a593Smuzhiyun 	/*
3305*4882a593Smuzhiyun 	 * The OHCI controller puts the isochronous header and trailer in the
3306*4882a593Smuzhiyun 	 * buffer, so we need at least 8 bytes.
3307*4882a593Smuzhiyun 	 */
3308*4882a593Smuzhiyun 	packet_count = packet->header_length / ctx->base.header_size;
3309*4882a593Smuzhiyun 	header_size  = max(ctx->base.header_size, (size_t)8);
3310*4882a593Smuzhiyun 
3311*4882a593Smuzhiyun 	/* Get header size in number of descriptors. */
3312*4882a593Smuzhiyun 	header_z = DIV_ROUND_UP(header_size, sizeof(*d));
3313*4882a593Smuzhiyun 	page     = payload >> PAGE_SHIFT;
3314*4882a593Smuzhiyun 	offset   = payload & ~PAGE_MASK;
3315*4882a593Smuzhiyun 	payload_per_buffer = packet->payload_length / packet_count;
3316*4882a593Smuzhiyun 
3317*4882a593Smuzhiyun 	for (i = 0; i < packet_count; i++) {
3318*4882a593Smuzhiyun 		/* d points to the header descriptor */
3319*4882a593Smuzhiyun 		z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
3320*4882a593Smuzhiyun 		d = context_get_descriptors(&ctx->context,
3321*4882a593Smuzhiyun 				z + header_z, &d_bus);
3322*4882a593Smuzhiyun 		if (d == NULL)
3323*4882a593Smuzhiyun 			return -ENOMEM;
3324*4882a593Smuzhiyun 
3325*4882a593Smuzhiyun 		d->control      = cpu_to_le16(DESCRIPTOR_STATUS |
3326*4882a593Smuzhiyun 					      DESCRIPTOR_INPUT_MORE);
3327*4882a593Smuzhiyun 		if (packet->skip && i == 0)
3328*4882a593Smuzhiyun 			d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
3329*4882a593Smuzhiyun 		d->req_count    = cpu_to_le16(header_size);
3330*4882a593Smuzhiyun 		d->res_count    = d->req_count;
3331*4882a593Smuzhiyun 		d->transfer_status = 0;
3332*4882a593Smuzhiyun 		d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
3333*4882a593Smuzhiyun 
3334*4882a593Smuzhiyun 		rest = payload_per_buffer;
3335*4882a593Smuzhiyun 		pd = d;
3336*4882a593Smuzhiyun 		for (j = 1; j < z; j++) {
3337*4882a593Smuzhiyun 			pd++;
3338*4882a593Smuzhiyun 			pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
3339*4882a593Smuzhiyun 						  DESCRIPTOR_INPUT_MORE);
3340*4882a593Smuzhiyun 
3341*4882a593Smuzhiyun 			if (offset + rest < PAGE_SIZE)
3342*4882a593Smuzhiyun 				length = rest;
3343*4882a593Smuzhiyun 			else
3344*4882a593Smuzhiyun 				length = PAGE_SIZE - offset;
3345*4882a593Smuzhiyun 			pd->req_count = cpu_to_le16(length);
3346*4882a593Smuzhiyun 			pd->res_count = pd->req_count;
3347*4882a593Smuzhiyun 			pd->transfer_status = 0;
3348*4882a593Smuzhiyun 
3349*4882a593Smuzhiyun 			page_bus = page_private(buffer->pages[page]);
3350*4882a593Smuzhiyun 			pd->data_address = cpu_to_le32(page_bus + offset);
3351*4882a593Smuzhiyun 
3352*4882a593Smuzhiyun 			dma_sync_single_range_for_device(device, page_bus,
3353*4882a593Smuzhiyun 							 offset, length,
3354*4882a593Smuzhiyun 							 DMA_FROM_DEVICE);
3355*4882a593Smuzhiyun 
3356*4882a593Smuzhiyun 			offset = (offset + length) & ~PAGE_MASK;
3357*4882a593Smuzhiyun 			rest -= length;
3358*4882a593Smuzhiyun 			if (offset == 0)
3359*4882a593Smuzhiyun 				page++;
3360*4882a593Smuzhiyun 		}
3361*4882a593Smuzhiyun 		pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
3362*4882a593Smuzhiyun 					  DESCRIPTOR_INPUT_LAST |
3363*4882a593Smuzhiyun 					  DESCRIPTOR_BRANCH_ALWAYS);
3364*4882a593Smuzhiyun 		if (packet->interrupt && i == packet_count - 1)
3365*4882a593Smuzhiyun 			pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
3366*4882a593Smuzhiyun 
3367*4882a593Smuzhiyun 		context_append(&ctx->context, d, z, header_z);
3368*4882a593Smuzhiyun 	}
3369*4882a593Smuzhiyun 
3370*4882a593Smuzhiyun 	return 0;
3371*4882a593Smuzhiyun }
3372*4882a593Smuzhiyun 
queue_iso_buffer_fill(struct iso_context * ctx,struct fw_iso_packet * packet,struct fw_iso_buffer * buffer,unsigned long payload)3373*4882a593Smuzhiyun static int queue_iso_buffer_fill(struct iso_context *ctx,
3374*4882a593Smuzhiyun 				 struct fw_iso_packet *packet,
3375*4882a593Smuzhiyun 				 struct fw_iso_buffer *buffer,
3376*4882a593Smuzhiyun 				 unsigned long payload)
3377*4882a593Smuzhiyun {
3378*4882a593Smuzhiyun 	struct descriptor *d;
3379*4882a593Smuzhiyun 	dma_addr_t d_bus, page_bus;
3380*4882a593Smuzhiyun 	int page, offset, rest, z, i, length;
3381*4882a593Smuzhiyun 
3382*4882a593Smuzhiyun 	page   = payload >> PAGE_SHIFT;
3383*4882a593Smuzhiyun 	offset = payload & ~PAGE_MASK;
3384*4882a593Smuzhiyun 	rest   = packet->payload_length;
3385*4882a593Smuzhiyun 
3386*4882a593Smuzhiyun 	/* We need one descriptor for each page in the buffer. */
3387*4882a593Smuzhiyun 	z = DIV_ROUND_UP(offset + rest, PAGE_SIZE);
3388*4882a593Smuzhiyun 
3389*4882a593Smuzhiyun 	if (WARN_ON(offset & 3 || rest & 3 || page + z > buffer->page_count))
3390*4882a593Smuzhiyun 		return -EFAULT;
3391*4882a593Smuzhiyun 
3392*4882a593Smuzhiyun 	for (i = 0; i < z; i++) {
3393*4882a593Smuzhiyun 		d = context_get_descriptors(&ctx->context, 1, &d_bus);
3394*4882a593Smuzhiyun 		if (d == NULL)
3395*4882a593Smuzhiyun 			return -ENOMEM;
3396*4882a593Smuzhiyun 
3397*4882a593Smuzhiyun 		d->control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
3398*4882a593Smuzhiyun 					 DESCRIPTOR_BRANCH_ALWAYS);
3399*4882a593Smuzhiyun 		if (packet->skip && i == 0)
3400*4882a593Smuzhiyun 			d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
3401*4882a593Smuzhiyun 		if (packet->interrupt && i == z - 1)
3402*4882a593Smuzhiyun 			d->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
3403*4882a593Smuzhiyun 
3404*4882a593Smuzhiyun 		if (offset + rest < PAGE_SIZE)
3405*4882a593Smuzhiyun 			length = rest;
3406*4882a593Smuzhiyun 		else
3407*4882a593Smuzhiyun 			length = PAGE_SIZE - offset;
3408*4882a593Smuzhiyun 		d->req_count = cpu_to_le16(length);
3409*4882a593Smuzhiyun 		d->res_count = d->req_count;
3410*4882a593Smuzhiyun 		d->transfer_status = 0;
3411*4882a593Smuzhiyun 
3412*4882a593Smuzhiyun 		page_bus = page_private(buffer->pages[page]);
3413*4882a593Smuzhiyun 		d->data_address = cpu_to_le32(page_bus + offset);
3414*4882a593Smuzhiyun 
3415*4882a593Smuzhiyun 		dma_sync_single_range_for_device(ctx->context.ohci->card.device,
3416*4882a593Smuzhiyun 						 page_bus, offset, length,
3417*4882a593Smuzhiyun 						 DMA_FROM_DEVICE);
3418*4882a593Smuzhiyun 
3419*4882a593Smuzhiyun 		rest -= length;
3420*4882a593Smuzhiyun 		offset = 0;
3421*4882a593Smuzhiyun 		page++;
3422*4882a593Smuzhiyun 
3423*4882a593Smuzhiyun 		context_append(&ctx->context, d, 1, 0);
3424*4882a593Smuzhiyun 	}
3425*4882a593Smuzhiyun 
3426*4882a593Smuzhiyun 	return 0;
3427*4882a593Smuzhiyun }
3428*4882a593Smuzhiyun 
ohci_queue_iso(struct fw_iso_context * base,struct fw_iso_packet * packet,struct fw_iso_buffer * buffer,unsigned long payload)3429*4882a593Smuzhiyun static int ohci_queue_iso(struct fw_iso_context *base,
3430*4882a593Smuzhiyun 			  struct fw_iso_packet *packet,
3431*4882a593Smuzhiyun 			  struct fw_iso_buffer *buffer,
3432*4882a593Smuzhiyun 			  unsigned long payload)
3433*4882a593Smuzhiyun {
3434*4882a593Smuzhiyun 	struct iso_context *ctx = container_of(base, struct iso_context, base);
3435*4882a593Smuzhiyun 	unsigned long flags;
3436*4882a593Smuzhiyun 	int ret = -ENOSYS;
3437*4882a593Smuzhiyun 
3438*4882a593Smuzhiyun 	spin_lock_irqsave(&ctx->context.ohci->lock, flags);
3439*4882a593Smuzhiyun 	switch (base->type) {
3440*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_TRANSMIT:
3441*4882a593Smuzhiyun 		ret = queue_iso_transmit(ctx, packet, buffer, payload);
3442*4882a593Smuzhiyun 		break;
3443*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE:
3444*4882a593Smuzhiyun 		ret = queue_iso_packet_per_buffer(ctx, packet, buffer, payload);
3445*4882a593Smuzhiyun 		break;
3446*4882a593Smuzhiyun 	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
3447*4882a593Smuzhiyun 		ret = queue_iso_buffer_fill(ctx, packet, buffer, payload);
3448*4882a593Smuzhiyun 		break;
3449*4882a593Smuzhiyun 	}
3450*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ctx->context.ohci->lock, flags);
3451*4882a593Smuzhiyun 
3452*4882a593Smuzhiyun 	return ret;
3453*4882a593Smuzhiyun }
3454*4882a593Smuzhiyun 
ohci_flush_queue_iso(struct fw_iso_context * base)3455*4882a593Smuzhiyun static void ohci_flush_queue_iso(struct fw_iso_context *base)
3456*4882a593Smuzhiyun {
3457*4882a593Smuzhiyun 	struct context *ctx =
3458*4882a593Smuzhiyun 			&container_of(base, struct iso_context, base)->context;
3459*4882a593Smuzhiyun 
3460*4882a593Smuzhiyun 	reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
3461*4882a593Smuzhiyun }
3462*4882a593Smuzhiyun 
ohci_flush_iso_completions(struct fw_iso_context * base)3463*4882a593Smuzhiyun static int ohci_flush_iso_completions(struct fw_iso_context *base)
3464*4882a593Smuzhiyun {
3465*4882a593Smuzhiyun 	struct iso_context *ctx = container_of(base, struct iso_context, base);
3466*4882a593Smuzhiyun 	int ret = 0;
3467*4882a593Smuzhiyun 
3468*4882a593Smuzhiyun 	tasklet_disable(&ctx->context.tasklet);
3469*4882a593Smuzhiyun 
3470*4882a593Smuzhiyun 	if (!test_and_set_bit_lock(0, &ctx->flushing_completions)) {
3471*4882a593Smuzhiyun 		context_tasklet((unsigned long)&ctx->context);
3472*4882a593Smuzhiyun 
3473*4882a593Smuzhiyun 		switch (base->type) {
3474*4882a593Smuzhiyun 		case FW_ISO_CONTEXT_TRANSMIT:
3475*4882a593Smuzhiyun 		case FW_ISO_CONTEXT_RECEIVE:
3476*4882a593Smuzhiyun 			if (ctx->header_length != 0)
3477*4882a593Smuzhiyun 				flush_iso_completions(ctx);
3478*4882a593Smuzhiyun 			break;
3479*4882a593Smuzhiyun 		case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
3480*4882a593Smuzhiyun 			if (ctx->mc_completed != 0)
3481*4882a593Smuzhiyun 				flush_ir_buffer_fill(ctx);
3482*4882a593Smuzhiyun 			break;
3483*4882a593Smuzhiyun 		default:
3484*4882a593Smuzhiyun 			ret = -ENOSYS;
3485*4882a593Smuzhiyun 		}
3486*4882a593Smuzhiyun 
3487*4882a593Smuzhiyun 		clear_bit_unlock(0, &ctx->flushing_completions);
3488*4882a593Smuzhiyun 		smp_mb__after_atomic();
3489*4882a593Smuzhiyun 	}
3490*4882a593Smuzhiyun 
3491*4882a593Smuzhiyun 	tasklet_enable(&ctx->context.tasklet);
3492*4882a593Smuzhiyun 
3493*4882a593Smuzhiyun 	return ret;
3494*4882a593Smuzhiyun }
3495*4882a593Smuzhiyun 
3496*4882a593Smuzhiyun static const struct fw_card_driver ohci_driver = {
3497*4882a593Smuzhiyun 	.enable			= ohci_enable,
3498*4882a593Smuzhiyun 	.read_phy_reg		= ohci_read_phy_reg,
3499*4882a593Smuzhiyun 	.update_phy_reg		= ohci_update_phy_reg,
3500*4882a593Smuzhiyun 	.set_config_rom		= ohci_set_config_rom,
3501*4882a593Smuzhiyun 	.send_request		= ohci_send_request,
3502*4882a593Smuzhiyun 	.send_response		= ohci_send_response,
3503*4882a593Smuzhiyun 	.cancel_packet		= ohci_cancel_packet,
3504*4882a593Smuzhiyun 	.enable_phys_dma	= ohci_enable_phys_dma,
3505*4882a593Smuzhiyun 	.read_csr		= ohci_read_csr,
3506*4882a593Smuzhiyun 	.write_csr		= ohci_write_csr,
3507*4882a593Smuzhiyun 
3508*4882a593Smuzhiyun 	.allocate_iso_context	= ohci_allocate_iso_context,
3509*4882a593Smuzhiyun 	.free_iso_context	= ohci_free_iso_context,
3510*4882a593Smuzhiyun 	.set_iso_channels	= ohci_set_iso_channels,
3511*4882a593Smuzhiyun 	.queue_iso		= ohci_queue_iso,
3512*4882a593Smuzhiyun 	.flush_queue_iso	= ohci_flush_queue_iso,
3513*4882a593Smuzhiyun 	.flush_iso_completions	= ohci_flush_iso_completions,
3514*4882a593Smuzhiyun 	.start_iso		= ohci_start_iso,
3515*4882a593Smuzhiyun 	.stop_iso		= ohci_stop_iso,
3516*4882a593Smuzhiyun };
3517*4882a593Smuzhiyun 
3518*4882a593Smuzhiyun #ifdef CONFIG_PPC_PMAC
pmac_ohci_on(struct pci_dev * dev)3519*4882a593Smuzhiyun static void pmac_ohci_on(struct pci_dev *dev)
3520*4882a593Smuzhiyun {
3521*4882a593Smuzhiyun 	if (machine_is(powermac)) {
3522*4882a593Smuzhiyun 		struct device_node *ofn = pci_device_to_OF_node(dev);
3523*4882a593Smuzhiyun 
3524*4882a593Smuzhiyun 		if (ofn) {
3525*4882a593Smuzhiyun 			pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1);
3526*4882a593Smuzhiyun 			pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
3527*4882a593Smuzhiyun 		}
3528*4882a593Smuzhiyun 	}
3529*4882a593Smuzhiyun }
3530*4882a593Smuzhiyun 
pmac_ohci_off(struct pci_dev * dev)3531*4882a593Smuzhiyun static void pmac_ohci_off(struct pci_dev *dev)
3532*4882a593Smuzhiyun {
3533*4882a593Smuzhiyun 	if (machine_is(powermac)) {
3534*4882a593Smuzhiyun 		struct device_node *ofn = pci_device_to_OF_node(dev);
3535*4882a593Smuzhiyun 
3536*4882a593Smuzhiyun 		if (ofn) {
3537*4882a593Smuzhiyun 			pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
3538*4882a593Smuzhiyun 			pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0);
3539*4882a593Smuzhiyun 		}
3540*4882a593Smuzhiyun 	}
3541*4882a593Smuzhiyun }
3542*4882a593Smuzhiyun #else
pmac_ohci_on(struct pci_dev * dev)3543*4882a593Smuzhiyun static inline void pmac_ohci_on(struct pci_dev *dev) {}
pmac_ohci_off(struct pci_dev * dev)3544*4882a593Smuzhiyun static inline void pmac_ohci_off(struct pci_dev *dev) {}
3545*4882a593Smuzhiyun #endif /* CONFIG_PPC_PMAC */
3546*4882a593Smuzhiyun 
pci_probe(struct pci_dev * dev,const struct pci_device_id * ent)3547*4882a593Smuzhiyun static int pci_probe(struct pci_dev *dev,
3548*4882a593Smuzhiyun 			       const struct pci_device_id *ent)
3549*4882a593Smuzhiyun {
3550*4882a593Smuzhiyun 	struct fw_ohci *ohci;
3551*4882a593Smuzhiyun 	u32 bus_options, max_receive, link_speed, version;
3552*4882a593Smuzhiyun 	u64 guid;
3553*4882a593Smuzhiyun 	int i, err;
3554*4882a593Smuzhiyun 	size_t size;
3555*4882a593Smuzhiyun 
3556*4882a593Smuzhiyun 	if (dev->vendor == PCI_VENDOR_ID_PINNACLE_SYSTEMS) {
3557*4882a593Smuzhiyun 		dev_err(&dev->dev, "Pinnacle MovieBoard is not yet supported\n");
3558*4882a593Smuzhiyun 		return -ENOSYS;
3559*4882a593Smuzhiyun 	}
3560*4882a593Smuzhiyun 
3561*4882a593Smuzhiyun 	ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
3562*4882a593Smuzhiyun 	if (ohci == NULL) {
3563*4882a593Smuzhiyun 		err = -ENOMEM;
3564*4882a593Smuzhiyun 		goto fail;
3565*4882a593Smuzhiyun 	}
3566*4882a593Smuzhiyun 
3567*4882a593Smuzhiyun 	fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
3568*4882a593Smuzhiyun 
3569*4882a593Smuzhiyun 	pmac_ohci_on(dev);
3570*4882a593Smuzhiyun 
3571*4882a593Smuzhiyun 	err = pci_enable_device(dev);
3572*4882a593Smuzhiyun 	if (err) {
3573*4882a593Smuzhiyun 		dev_err(&dev->dev, "failed to enable OHCI hardware\n");
3574*4882a593Smuzhiyun 		goto fail_free;
3575*4882a593Smuzhiyun 	}
3576*4882a593Smuzhiyun 
3577*4882a593Smuzhiyun 	pci_set_master(dev);
3578*4882a593Smuzhiyun 	pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
3579*4882a593Smuzhiyun 	pci_set_drvdata(dev, ohci);
3580*4882a593Smuzhiyun 
3581*4882a593Smuzhiyun 	spin_lock_init(&ohci->lock);
3582*4882a593Smuzhiyun 	mutex_init(&ohci->phy_reg_mutex);
3583*4882a593Smuzhiyun 
3584*4882a593Smuzhiyun 	INIT_WORK(&ohci->bus_reset_work, bus_reset_work);
3585*4882a593Smuzhiyun 
3586*4882a593Smuzhiyun 	if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM) ||
3587*4882a593Smuzhiyun 	    pci_resource_len(dev, 0) < OHCI1394_REGISTER_SIZE) {
3588*4882a593Smuzhiyun 		ohci_err(ohci, "invalid MMIO resource\n");
3589*4882a593Smuzhiyun 		err = -ENXIO;
3590*4882a593Smuzhiyun 		goto fail_disable;
3591*4882a593Smuzhiyun 	}
3592*4882a593Smuzhiyun 
3593*4882a593Smuzhiyun 	err = pci_request_region(dev, 0, ohci_driver_name);
3594*4882a593Smuzhiyun 	if (err) {
3595*4882a593Smuzhiyun 		ohci_err(ohci, "MMIO resource unavailable\n");
3596*4882a593Smuzhiyun 		goto fail_disable;
3597*4882a593Smuzhiyun 	}
3598*4882a593Smuzhiyun 
3599*4882a593Smuzhiyun 	ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
3600*4882a593Smuzhiyun 	if (ohci->registers == NULL) {
3601*4882a593Smuzhiyun 		ohci_err(ohci, "failed to remap registers\n");
3602*4882a593Smuzhiyun 		err = -ENXIO;
3603*4882a593Smuzhiyun 		goto fail_iomem;
3604*4882a593Smuzhiyun 	}
3605*4882a593Smuzhiyun 
3606*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(ohci_quirks); i++)
3607*4882a593Smuzhiyun 		if ((ohci_quirks[i].vendor == dev->vendor) &&
3608*4882a593Smuzhiyun 		    (ohci_quirks[i].device == (unsigned short)PCI_ANY_ID ||
3609*4882a593Smuzhiyun 		     ohci_quirks[i].device == dev->device) &&
3610*4882a593Smuzhiyun 		    (ohci_quirks[i].revision == (unsigned short)PCI_ANY_ID ||
3611*4882a593Smuzhiyun 		     ohci_quirks[i].revision >= dev->revision)) {
3612*4882a593Smuzhiyun 			ohci->quirks = ohci_quirks[i].flags;
3613*4882a593Smuzhiyun 			break;
3614*4882a593Smuzhiyun 		}
3615*4882a593Smuzhiyun 	if (param_quirks)
3616*4882a593Smuzhiyun 		ohci->quirks = param_quirks;
3617*4882a593Smuzhiyun 
3618*4882a593Smuzhiyun 	/*
3619*4882a593Smuzhiyun 	 * Because dma_alloc_coherent() allocates at least one page,
3620*4882a593Smuzhiyun 	 * we save space by using a common buffer for the AR request/
3621*4882a593Smuzhiyun 	 * response descriptors and the self IDs buffer.
3622*4882a593Smuzhiyun 	 */
3623*4882a593Smuzhiyun 	BUILD_BUG_ON(AR_BUFFERS * sizeof(struct descriptor) > PAGE_SIZE/4);
3624*4882a593Smuzhiyun 	BUILD_BUG_ON(SELF_ID_BUF_SIZE > PAGE_SIZE/2);
3625*4882a593Smuzhiyun 	ohci->misc_buffer = dma_alloc_coherent(ohci->card.device,
3626*4882a593Smuzhiyun 					       PAGE_SIZE,
3627*4882a593Smuzhiyun 					       &ohci->misc_buffer_bus,
3628*4882a593Smuzhiyun 					       GFP_KERNEL);
3629*4882a593Smuzhiyun 	if (!ohci->misc_buffer) {
3630*4882a593Smuzhiyun 		err = -ENOMEM;
3631*4882a593Smuzhiyun 		goto fail_iounmap;
3632*4882a593Smuzhiyun 	}
3633*4882a593Smuzhiyun 
3634*4882a593Smuzhiyun 	err = ar_context_init(&ohci->ar_request_ctx, ohci, 0,
3635*4882a593Smuzhiyun 			      OHCI1394_AsReqRcvContextControlSet);
3636*4882a593Smuzhiyun 	if (err < 0)
3637*4882a593Smuzhiyun 		goto fail_misc_buf;
3638*4882a593Smuzhiyun 
3639*4882a593Smuzhiyun 	err = ar_context_init(&ohci->ar_response_ctx, ohci, PAGE_SIZE/4,
3640*4882a593Smuzhiyun 			      OHCI1394_AsRspRcvContextControlSet);
3641*4882a593Smuzhiyun 	if (err < 0)
3642*4882a593Smuzhiyun 		goto fail_arreq_ctx;
3643*4882a593Smuzhiyun 
3644*4882a593Smuzhiyun 	err = context_init(&ohci->at_request_ctx, ohci,
3645*4882a593Smuzhiyun 			   OHCI1394_AsReqTrContextControlSet, handle_at_packet);
3646*4882a593Smuzhiyun 	if (err < 0)
3647*4882a593Smuzhiyun 		goto fail_arrsp_ctx;
3648*4882a593Smuzhiyun 
3649*4882a593Smuzhiyun 	err = context_init(&ohci->at_response_ctx, ohci,
3650*4882a593Smuzhiyun 			   OHCI1394_AsRspTrContextControlSet, handle_at_packet);
3651*4882a593Smuzhiyun 	if (err < 0)
3652*4882a593Smuzhiyun 		goto fail_atreq_ctx;
3653*4882a593Smuzhiyun 
3654*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
3655*4882a593Smuzhiyun 	ohci->ir_context_channels = ~0ULL;
3656*4882a593Smuzhiyun 	ohci->ir_context_support = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
3657*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
3658*4882a593Smuzhiyun 	ohci->ir_context_mask = ohci->ir_context_support;
3659*4882a593Smuzhiyun 	ohci->n_ir = hweight32(ohci->ir_context_mask);
3660*4882a593Smuzhiyun 	size = sizeof(struct iso_context) * ohci->n_ir;
3661*4882a593Smuzhiyun 	ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
3662*4882a593Smuzhiyun 
3663*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
3664*4882a593Smuzhiyun 	ohci->it_context_support = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
3665*4882a593Smuzhiyun 	/* JMicron JMB38x often shows 0 at first read, just ignore it */
3666*4882a593Smuzhiyun 	if (!ohci->it_context_support) {
3667*4882a593Smuzhiyun 		ohci_notice(ohci, "overriding IsoXmitIntMask\n");
3668*4882a593Smuzhiyun 		ohci->it_context_support = 0xf;
3669*4882a593Smuzhiyun 	}
3670*4882a593Smuzhiyun 	reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
3671*4882a593Smuzhiyun 	ohci->it_context_mask = ohci->it_context_support;
3672*4882a593Smuzhiyun 	ohci->n_it = hweight32(ohci->it_context_mask);
3673*4882a593Smuzhiyun 	size = sizeof(struct iso_context) * ohci->n_it;
3674*4882a593Smuzhiyun 	ohci->it_context_list = kzalloc(size, GFP_KERNEL);
3675*4882a593Smuzhiyun 
3676*4882a593Smuzhiyun 	if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
3677*4882a593Smuzhiyun 		err = -ENOMEM;
3678*4882a593Smuzhiyun 		goto fail_contexts;
3679*4882a593Smuzhiyun 	}
3680*4882a593Smuzhiyun 
3681*4882a593Smuzhiyun 	ohci->self_id     = ohci->misc_buffer     + PAGE_SIZE/2;
3682*4882a593Smuzhiyun 	ohci->self_id_bus = ohci->misc_buffer_bus + PAGE_SIZE/2;
3683*4882a593Smuzhiyun 
3684*4882a593Smuzhiyun 	bus_options = reg_read(ohci, OHCI1394_BusOptions);
3685*4882a593Smuzhiyun 	max_receive = (bus_options >> 12) & 0xf;
3686*4882a593Smuzhiyun 	link_speed = bus_options & 0x7;
3687*4882a593Smuzhiyun 	guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
3688*4882a593Smuzhiyun 		reg_read(ohci, OHCI1394_GUIDLo);
3689*4882a593Smuzhiyun 
3690*4882a593Smuzhiyun 	if (!(ohci->quirks & QUIRK_NO_MSI))
3691*4882a593Smuzhiyun 		pci_enable_msi(dev);
3692*4882a593Smuzhiyun 	if (request_irq(dev->irq, irq_handler,
3693*4882a593Smuzhiyun 			pci_dev_msi_enabled(dev) ? 0 : IRQF_SHARED,
3694*4882a593Smuzhiyun 			ohci_driver_name, ohci)) {
3695*4882a593Smuzhiyun 		ohci_err(ohci, "failed to allocate interrupt %d\n", dev->irq);
3696*4882a593Smuzhiyun 		err = -EIO;
3697*4882a593Smuzhiyun 		goto fail_msi;
3698*4882a593Smuzhiyun 	}
3699*4882a593Smuzhiyun 
3700*4882a593Smuzhiyun 	err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
3701*4882a593Smuzhiyun 	if (err)
3702*4882a593Smuzhiyun 		goto fail_irq;
3703*4882a593Smuzhiyun 
3704*4882a593Smuzhiyun 	version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
3705*4882a593Smuzhiyun 	ohci_notice(ohci,
3706*4882a593Smuzhiyun 		    "added OHCI v%x.%x device as card %d, "
3707*4882a593Smuzhiyun 		    "%d IR + %d IT contexts, quirks 0x%x%s\n",
3708*4882a593Smuzhiyun 		    version >> 16, version & 0xff, ohci->card.index,
3709*4882a593Smuzhiyun 		    ohci->n_ir, ohci->n_it, ohci->quirks,
3710*4882a593Smuzhiyun 		    reg_read(ohci, OHCI1394_PhyUpperBound) ?
3711*4882a593Smuzhiyun 			", physUB" : "");
3712*4882a593Smuzhiyun 
3713*4882a593Smuzhiyun 	return 0;
3714*4882a593Smuzhiyun 
3715*4882a593Smuzhiyun  fail_irq:
3716*4882a593Smuzhiyun 	free_irq(dev->irq, ohci);
3717*4882a593Smuzhiyun  fail_msi:
3718*4882a593Smuzhiyun 	pci_disable_msi(dev);
3719*4882a593Smuzhiyun  fail_contexts:
3720*4882a593Smuzhiyun 	kfree(ohci->ir_context_list);
3721*4882a593Smuzhiyun 	kfree(ohci->it_context_list);
3722*4882a593Smuzhiyun 	context_release(&ohci->at_response_ctx);
3723*4882a593Smuzhiyun  fail_atreq_ctx:
3724*4882a593Smuzhiyun 	context_release(&ohci->at_request_ctx);
3725*4882a593Smuzhiyun  fail_arrsp_ctx:
3726*4882a593Smuzhiyun 	ar_context_release(&ohci->ar_response_ctx);
3727*4882a593Smuzhiyun  fail_arreq_ctx:
3728*4882a593Smuzhiyun 	ar_context_release(&ohci->ar_request_ctx);
3729*4882a593Smuzhiyun  fail_misc_buf:
3730*4882a593Smuzhiyun 	dma_free_coherent(ohci->card.device, PAGE_SIZE,
3731*4882a593Smuzhiyun 			  ohci->misc_buffer, ohci->misc_buffer_bus);
3732*4882a593Smuzhiyun  fail_iounmap:
3733*4882a593Smuzhiyun 	pci_iounmap(dev, ohci->registers);
3734*4882a593Smuzhiyun  fail_iomem:
3735*4882a593Smuzhiyun 	pci_release_region(dev, 0);
3736*4882a593Smuzhiyun  fail_disable:
3737*4882a593Smuzhiyun 	pci_disable_device(dev);
3738*4882a593Smuzhiyun  fail_free:
3739*4882a593Smuzhiyun 	kfree(ohci);
3740*4882a593Smuzhiyun 	pmac_ohci_off(dev);
3741*4882a593Smuzhiyun  fail:
3742*4882a593Smuzhiyun 	return err;
3743*4882a593Smuzhiyun }
3744*4882a593Smuzhiyun 
pci_remove(struct pci_dev * dev)3745*4882a593Smuzhiyun static void pci_remove(struct pci_dev *dev)
3746*4882a593Smuzhiyun {
3747*4882a593Smuzhiyun 	struct fw_ohci *ohci = pci_get_drvdata(dev);
3748*4882a593Smuzhiyun 
3749*4882a593Smuzhiyun 	/*
3750*4882a593Smuzhiyun 	 * If the removal is happening from the suspend state, LPS won't be
3751*4882a593Smuzhiyun 	 * enabled and host registers (eg., IntMaskClear) won't be accessible.
3752*4882a593Smuzhiyun 	 */
3753*4882a593Smuzhiyun 	if (reg_read(ohci, OHCI1394_HCControlSet) & OHCI1394_HCControl_LPS) {
3754*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_IntMaskClear, ~0);
3755*4882a593Smuzhiyun 		flush_writes(ohci);
3756*4882a593Smuzhiyun 	}
3757*4882a593Smuzhiyun 	cancel_work_sync(&ohci->bus_reset_work);
3758*4882a593Smuzhiyun 	fw_core_remove_card(&ohci->card);
3759*4882a593Smuzhiyun 
3760*4882a593Smuzhiyun 	/*
3761*4882a593Smuzhiyun 	 * FIXME: Fail all pending packets here, now that the upper
3762*4882a593Smuzhiyun 	 * layers can't queue any more.
3763*4882a593Smuzhiyun 	 */
3764*4882a593Smuzhiyun 
3765*4882a593Smuzhiyun 	software_reset(ohci);
3766*4882a593Smuzhiyun 	free_irq(dev->irq, ohci);
3767*4882a593Smuzhiyun 
3768*4882a593Smuzhiyun 	if (ohci->next_config_rom && ohci->next_config_rom != ohci->config_rom)
3769*4882a593Smuzhiyun 		dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
3770*4882a593Smuzhiyun 				  ohci->next_config_rom, ohci->next_config_rom_bus);
3771*4882a593Smuzhiyun 	if (ohci->config_rom)
3772*4882a593Smuzhiyun 		dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
3773*4882a593Smuzhiyun 				  ohci->config_rom, ohci->config_rom_bus);
3774*4882a593Smuzhiyun 	ar_context_release(&ohci->ar_request_ctx);
3775*4882a593Smuzhiyun 	ar_context_release(&ohci->ar_response_ctx);
3776*4882a593Smuzhiyun 	dma_free_coherent(ohci->card.device, PAGE_SIZE,
3777*4882a593Smuzhiyun 			  ohci->misc_buffer, ohci->misc_buffer_bus);
3778*4882a593Smuzhiyun 	context_release(&ohci->at_request_ctx);
3779*4882a593Smuzhiyun 	context_release(&ohci->at_response_ctx);
3780*4882a593Smuzhiyun 	kfree(ohci->it_context_list);
3781*4882a593Smuzhiyun 	kfree(ohci->ir_context_list);
3782*4882a593Smuzhiyun 	pci_disable_msi(dev);
3783*4882a593Smuzhiyun 	pci_iounmap(dev, ohci->registers);
3784*4882a593Smuzhiyun 	pci_release_region(dev, 0);
3785*4882a593Smuzhiyun 	pci_disable_device(dev);
3786*4882a593Smuzhiyun 	kfree(ohci);
3787*4882a593Smuzhiyun 	pmac_ohci_off(dev);
3788*4882a593Smuzhiyun 
3789*4882a593Smuzhiyun 	dev_notice(&dev->dev, "removed fw-ohci device\n");
3790*4882a593Smuzhiyun }
3791*4882a593Smuzhiyun 
3792*4882a593Smuzhiyun #ifdef CONFIG_PM
pci_suspend(struct pci_dev * dev,pm_message_t state)3793*4882a593Smuzhiyun static int pci_suspend(struct pci_dev *dev, pm_message_t state)
3794*4882a593Smuzhiyun {
3795*4882a593Smuzhiyun 	struct fw_ohci *ohci = pci_get_drvdata(dev);
3796*4882a593Smuzhiyun 	int err;
3797*4882a593Smuzhiyun 
3798*4882a593Smuzhiyun 	software_reset(ohci);
3799*4882a593Smuzhiyun 	err = pci_save_state(dev);
3800*4882a593Smuzhiyun 	if (err) {
3801*4882a593Smuzhiyun 		ohci_err(ohci, "pci_save_state failed\n");
3802*4882a593Smuzhiyun 		return err;
3803*4882a593Smuzhiyun 	}
3804*4882a593Smuzhiyun 	err = pci_set_power_state(dev, pci_choose_state(dev, state));
3805*4882a593Smuzhiyun 	if (err)
3806*4882a593Smuzhiyun 		ohci_err(ohci, "pci_set_power_state failed with %d\n", err);
3807*4882a593Smuzhiyun 	pmac_ohci_off(dev);
3808*4882a593Smuzhiyun 
3809*4882a593Smuzhiyun 	return 0;
3810*4882a593Smuzhiyun }
3811*4882a593Smuzhiyun 
pci_resume(struct pci_dev * dev)3812*4882a593Smuzhiyun static int pci_resume(struct pci_dev *dev)
3813*4882a593Smuzhiyun {
3814*4882a593Smuzhiyun 	struct fw_ohci *ohci = pci_get_drvdata(dev);
3815*4882a593Smuzhiyun 	int err;
3816*4882a593Smuzhiyun 
3817*4882a593Smuzhiyun 	pmac_ohci_on(dev);
3818*4882a593Smuzhiyun 	pci_set_power_state(dev, PCI_D0);
3819*4882a593Smuzhiyun 	pci_restore_state(dev);
3820*4882a593Smuzhiyun 	err = pci_enable_device(dev);
3821*4882a593Smuzhiyun 	if (err) {
3822*4882a593Smuzhiyun 		ohci_err(ohci, "pci_enable_device failed\n");
3823*4882a593Smuzhiyun 		return err;
3824*4882a593Smuzhiyun 	}
3825*4882a593Smuzhiyun 
3826*4882a593Smuzhiyun 	/* Some systems don't setup GUID register on resume from ram  */
3827*4882a593Smuzhiyun 	if (!reg_read(ohci, OHCI1394_GUIDLo) &&
3828*4882a593Smuzhiyun 					!reg_read(ohci, OHCI1394_GUIDHi)) {
3829*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_GUIDLo, (u32)ohci->card.guid);
3830*4882a593Smuzhiyun 		reg_write(ohci, OHCI1394_GUIDHi, (u32)(ohci->card.guid >> 32));
3831*4882a593Smuzhiyun 	}
3832*4882a593Smuzhiyun 
3833*4882a593Smuzhiyun 	err = ohci_enable(&ohci->card, NULL, 0);
3834*4882a593Smuzhiyun 	if (err)
3835*4882a593Smuzhiyun 		return err;
3836*4882a593Smuzhiyun 
3837*4882a593Smuzhiyun 	ohci_resume_iso_dma(ohci);
3838*4882a593Smuzhiyun 
3839*4882a593Smuzhiyun 	return 0;
3840*4882a593Smuzhiyun }
3841*4882a593Smuzhiyun #endif
3842*4882a593Smuzhiyun 
3843*4882a593Smuzhiyun static const struct pci_device_id pci_table[] = {
3844*4882a593Smuzhiyun 	{ PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
3845*4882a593Smuzhiyun 	{ }
3846*4882a593Smuzhiyun };
3847*4882a593Smuzhiyun 
3848*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, pci_table);
3849*4882a593Smuzhiyun 
3850*4882a593Smuzhiyun static struct pci_driver fw_ohci_pci_driver = {
3851*4882a593Smuzhiyun 	.name		= ohci_driver_name,
3852*4882a593Smuzhiyun 	.id_table	= pci_table,
3853*4882a593Smuzhiyun 	.probe		= pci_probe,
3854*4882a593Smuzhiyun 	.remove		= pci_remove,
3855*4882a593Smuzhiyun #ifdef CONFIG_PM
3856*4882a593Smuzhiyun 	.resume		= pci_resume,
3857*4882a593Smuzhiyun 	.suspend	= pci_suspend,
3858*4882a593Smuzhiyun #endif
3859*4882a593Smuzhiyun };
3860*4882a593Smuzhiyun 
fw_ohci_init(void)3861*4882a593Smuzhiyun static int __init fw_ohci_init(void)
3862*4882a593Smuzhiyun {
3863*4882a593Smuzhiyun 	selfid_workqueue = alloc_workqueue(KBUILD_MODNAME, WQ_MEM_RECLAIM, 0);
3864*4882a593Smuzhiyun 	if (!selfid_workqueue)
3865*4882a593Smuzhiyun 		return -ENOMEM;
3866*4882a593Smuzhiyun 
3867*4882a593Smuzhiyun 	return pci_register_driver(&fw_ohci_pci_driver);
3868*4882a593Smuzhiyun }
3869*4882a593Smuzhiyun 
fw_ohci_cleanup(void)3870*4882a593Smuzhiyun static void __exit fw_ohci_cleanup(void)
3871*4882a593Smuzhiyun {
3872*4882a593Smuzhiyun 	pci_unregister_driver(&fw_ohci_pci_driver);
3873*4882a593Smuzhiyun 	destroy_workqueue(selfid_workqueue);
3874*4882a593Smuzhiyun }
3875*4882a593Smuzhiyun 
3876*4882a593Smuzhiyun module_init(fw_ohci_init);
3877*4882a593Smuzhiyun module_exit(fw_ohci_cleanup);
3878*4882a593Smuzhiyun 
3879*4882a593Smuzhiyun MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
3880*4882a593Smuzhiyun MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
3881*4882a593Smuzhiyun MODULE_LICENSE("GPL");
3882*4882a593Smuzhiyun 
3883*4882a593Smuzhiyun /* Provide a module alias so root-on-sbp2 initrds don't break. */
3884*4882a593Smuzhiyun MODULE_ALIAS("ohci1394");
3885