blob: 74d5d945f200ed79d5b4cce28208e0268100b2f0 [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Driver for OHCI 1394 controllers
Kristian Høgsberged568912006-12-19 19:58:35 -05003 *
Kristian Høgsberged568912006-12-19 19:58:35 -05004 * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
Stefan Richtere524f6162007-08-20 21:58:30 +020021#include <linux/compiler.h>
Kristian Høgsberged568912006-12-19 19:58:35 -050022#include <linux/delay.h>
Andrew Mortoncf3e72f2006-12-27 14:36:37 -080023#include <linux/dma-mapping.h>
Stefan Richterc26f0232007-08-20 21:40:30 +020024#include <linux/gfp.h>
Stefan Richtera7fb60d2007-08-20 21:41:22 +020025#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/kernel.h>
Al Virofaa2fb42007-05-15 20:36:10 +010028#include <linux/mm.h>
Stefan Richtera7fb60d2007-08-20 21:41:22 +020029#include <linux/module.h>
30#include <linux/pci.h>
Stefan Richterc26f0232007-08-20 21:40:30 +020031#include <linux/spinlock.h>
Andrew Mortoncf3e72f2006-12-27 14:36:37 -080032
Stefan Richterc26f0232007-08-20 21:40:30 +020033#include <asm/page.h>
Stefan Richteree71c2f2007-08-25 14:08:19 +020034#include <asm/system.h>
Kristian Høgsberged568912006-12-19 19:58:35 -050035
Kristian Høgsberged568912006-12-19 19:58:35 -050036#include "fw-ohci.h"
Stefan Richtera7fb60d2007-08-20 21:41:22 +020037#include "fw-transaction.h"
Kristian Høgsberged568912006-12-19 19:58:35 -050038
Kristian Høgsberga77754a2007-05-07 20:33:35 -040039#define DESCRIPTOR_OUTPUT_MORE 0
40#define DESCRIPTOR_OUTPUT_LAST (1 << 12)
41#define DESCRIPTOR_INPUT_MORE (2 << 12)
42#define DESCRIPTOR_INPUT_LAST (3 << 12)
43#define DESCRIPTOR_STATUS (1 << 11)
44#define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
45#define DESCRIPTOR_PING (1 << 7)
46#define DESCRIPTOR_YY (1 << 6)
47#define DESCRIPTOR_NO_IRQ (0 << 4)
48#define DESCRIPTOR_IRQ_ERROR (1 << 4)
49#define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
50#define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
51#define DESCRIPTOR_WAIT (3 << 0)
Kristian Høgsberged568912006-12-19 19:58:35 -050052
53struct descriptor {
54 __le16 req_count;
55 __le16 control;
56 __le32 data_address;
57 __le32 branch_address;
58 __le16 res_count;
59 __le16 transfer_status;
60} __attribute__((aligned(16)));
61
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -050062struct db_descriptor {
63 __le16 first_size;
64 __le16 control;
65 __le16 second_req_count;
66 __le16 first_req_count;
67 __le32 branch_address;
68 __le16 second_res_count;
69 __le16 first_res_count;
70 __le32 reserved0;
71 __le32 first_buffer;
72 __le32 second_buffer;
73 __le32 reserved1;
74} __attribute__((aligned(16)));
75
Kristian Høgsberga77754a2007-05-07 20:33:35 -040076#define CONTROL_SET(regs) (regs)
77#define CONTROL_CLEAR(regs) ((regs) + 4)
78#define COMMAND_PTR(regs) ((regs) + 12)
79#define CONTEXT_MATCH(regs) ((regs) + 16)
Kristian Høgsberg72e318e2007-02-06 14:49:31 -050080
Kristian Høgsberg32b46092007-02-06 14:49:30 -050081struct ar_buffer {
82 struct descriptor descriptor;
83 struct ar_buffer *next;
84 __le32 data[0];
85};
86
Kristian Høgsberged568912006-12-19 19:58:35 -050087struct ar_context {
88 struct fw_ohci *ohci;
Kristian Høgsberg32b46092007-02-06 14:49:30 -050089 struct ar_buffer *current_buffer;
90 struct ar_buffer *last_buffer;
91 void *pointer;
Kristian Høgsberg72e318e2007-02-06 14:49:31 -050092 u32 regs;
Kristian Høgsberged568912006-12-19 19:58:35 -050093 struct tasklet_struct tasklet;
94};
95
Kristian Høgsberg30200732007-02-16 17:34:39 -050096struct context;
97
98typedef int (*descriptor_callback_t)(struct context *ctx,
99 struct descriptor *d,
100 struct descriptor *last);
101struct context {
Stefan Richter373b2ed2007-03-04 14:45:18 +0100102 struct fw_ohci *ohci;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500103 u32 regs;
Stefan Richter373b2ed2007-03-04 14:45:18 +0100104
Kristian Høgsberg30200732007-02-16 17:34:39 -0500105 struct descriptor *buffer;
106 dma_addr_t buffer_bus;
107 size_t buffer_size;
108 struct descriptor *head_descriptor;
109 struct descriptor *tail_descriptor;
110 struct descriptor *tail_descriptor_last;
111 struct descriptor *prev_descriptor;
112
113 descriptor_callback_t callback;
114
Stefan Richter373b2ed2007-03-04 14:45:18 +0100115 struct tasklet_struct tasklet;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500116};
Kristian Høgsberg30200732007-02-16 17:34:39 -0500117
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400118#define IT_HEADER_SY(v) ((v) << 0)
119#define IT_HEADER_TCODE(v) ((v) << 4)
120#define IT_HEADER_CHANNEL(v) ((v) << 8)
121#define IT_HEADER_TAG(v) ((v) << 14)
122#define IT_HEADER_SPEED(v) ((v) << 16)
123#define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
Kristian Høgsberged568912006-12-19 19:58:35 -0500124
125struct iso_context {
126 struct fw_iso_context base;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500127 struct context context;
David Moore0642b652007-12-19 03:09:18 -0500128 int excess_bytes;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500129 void *header;
130 size_t header_length;
Kristian Høgsberged568912006-12-19 19:58:35 -0500131};
132
133#define CONFIG_ROM_SIZE 1024
134
135struct fw_ohci {
136 struct fw_card card;
137
Kristian Høgsberge364cf42007-02-16 17:34:49 -0500138 u32 version;
Kristian Høgsberged568912006-12-19 19:58:35 -0500139 __iomem char *registers;
140 dma_addr_t self_id_bus;
141 __le32 *self_id_cpu;
142 struct tasklet_struct bus_reset_tasklet;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500143 int node_id;
Kristian Høgsberged568912006-12-19 19:58:35 -0500144 int generation;
145 int request_generation;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500146 u32 bus_seconds;
Kristian Høgsberged568912006-12-19 19:58:35 -0500147
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400148 /*
149 * Spinlock for accessing fw_ohci data. Never call out of
150 * this driver with this lock held.
151 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500152 spinlock_t lock;
153 u32 self_id_buffer[512];
154
155 /* Config rom buffers */
156 __be32 *config_rom;
157 dma_addr_t config_rom_bus;
158 __be32 *next_config_rom;
159 dma_addr_t next_config_rom_bus;
160 u32 next_header;
161
162 struct ar_context ar_request_ctx;
163 struct ar_context ar_response_ctx;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500164 struct context at_request_ctx;
165 struct context at_response_ctx;
Kristian Høgsberged568912006-12-19 19:58:35 -0500166
167 u32 it_context_mask;
168 struct iso_context *it_context_list;
169 u32 ir_context_mask;
170 struct iso_context *ir_context_list;
171};
172
Adrian Bunk95688e92007-01-22 19:17:37 +0100173static inline struct fw_ohci *fw_ohci(struct fw_card *card)
Kristian Høgsberged568912006-12-19 19:58:35 -0500174{
175 return container_of(card, struct fw_ohci, card);
176}
177
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500178#define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
179#define IR_CONTEXT_BUFFER_FILL 0x80000000
180#define IR_CONTEXT_ISOCH_HEADER 0x40000000
181#define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
182#define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
183#define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
Kristian Høgsberged568912006-12-19 19:58:35 -0500184
185#define CONTEXT_RUN 0x8000
186#define CONTEXT_WAKE 0x1000
187#define CONTEXT_DEAD 0x0800
188#define CONTEXT_ACTIVE 0x0400
189
190#define OHCI1394_MAX_AT_REQ_RETRIES 0x2
191#define OHCI1394_MAX_AT_RESP_RETRIES 0x2
192#define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
193
194#define FW_OHCI_MAJOR 240
195#define OHCI1394_REGISTER_SIZE 0x800
196#define OHCI_LOOP_COUNT 500
197#define OHCI1394_PCI_HCI_Control 0x40
198#define SELF_ID_BUF_SIZE 0x800
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500199#define OHCI_TCODE_PHY_PACKET 0x0e
Kristian Høgsberge364cf42007-02-16 17:34:49 -0500200#define OHCI_VERSION_1_1 0x010010
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500201#define ISO_BUFFER_SIZE (64 * 1024)
202#define AT_BUFFER_SIZE 4096
Kristian Høgsberg0edeefd2007-01-26 00:38:49 -0500203
Kristian Høgsberged568912006-12-19 19:58:35 -0500204static char ohci_driver_name[] = KBUILD_MODNAME;
205
Adrian Bunk95688e92007-01-22 19:17:37 +0100206static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
Kristian Høgsberged568912006-12-19 19:58:35 -0500207{
208 writel(data, ohci->registers + offset);
209}
210
Adrian Bunk95688e92007-01-22 19:17:37 +0100211static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
Kristian Høgsberged568912006-12-19 19:58:35 -0500212{
213 return readl(ohci->registers + offset);
214}
215
Adrian Bunk95688e92007-01-22 19:17:37 +0100216static inline void flush_writes(const struct fw_ohci *ohci)
Kristian Høgsberged568912006-12-19 19:58:35 -0500217{
218 /* Do a dummy read to flush writes. */
219 reg_read(ohci, OHCI1394_Version);
220}
221
222static int
223ohci_update_phy_reg(struct fw_card *card, int addr,
224 int clear_bits, int set_bits)
225{
226 struct fw_ohci *ohci = fw_ohci(card);
227 u32 val, old;
228
229 reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
Stefan Richter362e9012007-07-12 22:24:19 +0200230 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -0500231 msleep(2);
232 val = reg_read(ohci, OHCI1394_PhyControl);
233 if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
234 fw_error("failed to set phy reg bits.\n");
235 return -EBUSY;
236 }
237
238 old = OHCI1394_PhyControl_ReadData(val);
239 old = (old & ~clear_bits) | set_bits;
240 reg_write(ohci, OHCI1394_PhyControl,
241 OHCI1394_PhyControl_Write(addr, old));
242
243 return 0;
244}
245
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500246static int ar_context_add_page(struct ar_context *ctx)
Kristian Høgsberged568912006-12-19 19:58:35 -0500247{
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500248 struct device *dev = ctx->ohci->card.device;
249 struct ar_buffer *ab;
250 dma_addr_t ab_bus;
251 size_t offset;
252
253 ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
254 if (ab == NULL)
255 return -ENOMEM;
256
257 ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
258 if (dma_mapping_error(ab_bus)) {
259 free_page((unsigned long) ab);
260 return -ENOMEM;
261 }
262
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400263 memset(&ab->descriptor, 0, sizeof(ab->descriptor));
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400264 ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
265 DESCRIPTOR_STATUS |
266 DESCRIPTOR_BRANCH_ALWAYS);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500267 offset = offsetof(struct ar_buffer, data);
268 ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
269 ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
270 ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
271 ab->descriptor.branch_address = 0;
272
273 dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
274
Kristian Høgsbergec839e42007-05-22 18:55:48 -0400275 ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500276 ctx->last_buffer->next = ab;
277 ctx->last_buffer = ab;
278
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400279 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
Kristian Høgsberged568912006-12-19 19:58:35 -0500280 flush_writes(ctx->ohci);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500281
282 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -0500283}
284
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500285static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
Kristian Høgsberged568912006-12-19 19:58:35 -0500286{
Kristian Høgsberged568912006-12-19 19:58:35 -0500287 struct fw_ohci *ohci = ctx->ohci;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500288 struct fw_packet p;
289 u32 status, length, tcode;
Kristian Høgsberg0edeefd2007-01-26 00:38:49 -0500290
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500291 p.header[0] = le32_to_cpu(buffer[0]);
292 p.header[1] = le32_to_cpu(buffer[1]);
293 p.header[2] = le32_to_cpu(buffer[2]);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500294
295 tcode = (p.header[0] >> 4) & 0x0f;
296 switch (tcode) {
297 case TCODE_WRITE_QUADLET_REQUEST:
298 case TCODE_READ_QUADLET_RESPONSE:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500299 p.header[3] = (__force __u32) buffer[3];
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500300 p.header_length = 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500301 p.payload_length = 0;
302 break;
303
304 case TCODE_READ_BLOCK_REQUEST :
305 p.header[3] = le32_to_cpu(buffer[3]);
306 p.header_length = 16;
307 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500308 break;
309
310 case TCODE_WRITE_BLOCK_REQUEST:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500311 case TCODE_READ_BLOCK_RESPONSE:
312 case TCODE_LOCK_REQUEST:
313 case TCODE_LOCK_RESPONSE:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500314 p.header[3] = le32_to_cpu(buffer[3]);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500315 p.header_length = 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500316 p.payload_length = p.header[3] >> 16;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500317 break;
318
319 case TCODE_WRITE_RESPONSE:
320 case TCODE_READ_QUADLET_REQUEST:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500321 case OHCI_TCODE_PHY_PACKET:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500322 p.header_length = 12;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500323 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500324 break;
325 }
326
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500327 p.payload = (void *) buffer + p.header_length;
328
329 /* FIXME: What to do about evt_* errors? */
330 length = (p.header_length + p.payload_length + 3) / 4;
331 status = le32_to_cpu(buffer[length]);
332
333 p.ack = ((status >> 16) & 0x1f) - 16;
334 p.speed = (status >> 21) & 0x7;
335 p.timestamp = status & 0xffff;
336 p.generation = ohci->request_generation;
Kristian Høgsberged568912006-12-19 19:58:35 -0500337
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400338 /*
339 * The OHCI bus reset handler synthesizes a phy packet with
Kristian Høgsberged568912006-12-19 19:58:35 -0500340 * the new generation number when a bus reset happens (see
341 * section 8.4.2.3). This helps us determine when a request
342 * was received and make sure we send the response in the same
343 * generation. We only need this for requests; for responses
344 * we use the unique tlabel for finding the matching
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400345 * request.
346 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500347
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500348 if (p.ack + 16 == 0x09)
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500349 ohci->request_generation = (buffer[2] >> 16) & 0xff;
Kristian Høgsberged568912006-12-19 19:58:35 -0500350 else if (ctx == &ohci->ar_request_ctx)
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500351 fw_core_handle_request(&ohci->card, &p);
Kristian Høgsberged568912006-12-19 19:58:35 -0500352 else
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500353 fw_core_handle_response(&ohci->card, &p);
Kristian Høgsberged568912006-12-19 19:58:35 -0500354
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500355 return buffer + length + 1;
356}
Kristian Høgsberged568912006-12-19 19:58:35 -0500357
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500358static void ar_context_tasklet(unsigned long data)
359{
360 struct ar_context *ctx = (struct ar_context *)data;
361 struct fw_ohci *ohci = ctx->ohci;
362 struct ar_buffer *ab;
363 struct descriptor *d;
364 void *buffer, *end;
Kristian Høgsberged568912006-12-19 19:58:35 -0500365
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500366 ab = ctx->current_buffer;
367 d = &ab->descriptor;
Kristian Høgsberged568912006-12-19 19:58:35 -0500368
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500369 if (d->res_count == 0) {
370 size_t size, rest, offset;
371
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400372 /*
373 * This descriptor is finished and we may have a
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500374 * packet split across this and the next buffer. We
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400375 * reuse the page for reassembling the split packet.
376 */
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500377
378 offset = offsetof(struct ar_buffer, data);
379 dma_unmap_single(ohci->card.device,
Stefan Richter0a9972b2007-06-23 20:28:17 +0200380 le32_to_cpu(ab->descriptor.data_address) - offset,
381 PAGE_SIZE, DMA_BIDIRECTIONAL);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500382
383 buffer = ab;
384 ab = ab->next;
385 d = &ab->descriptor;
386 size = buffer + PAGE_SIZE - ctx->pointer;
387 rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
388 memmove(buffer, ctx->pointer, size);
389 memcpy(buffer + size, ab->data, rest);
390 ctx->current_buffer = ab;
391 ctx->pointer = (void *) ab->data + rest;
392 end = buffer + size + rest;
393
394 while (buffer < end)
395 buffer = handle_ar_packet(ctx, buffer);
396
397 free_page((unsigned long)buffer);
398 ar_context_add_page(ctx);
399 } else {
400 buffer = ctx->pointer;
401 ctx->pointer = end =
402 (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
403
404 while (buffer < end)
405 buffer = handle_ar_packet(ctx, buffer);
406 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500407}
408
409static int
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500410ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
Kristian Høgsberged568912006-12-19 19:58:35 -0500411{
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500412 struct ar_buffer ab;
Kristian Høgsberged568912006-12-19 19:58:35 -0500413
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500414 ctx->regs = regs;
415 ctx->ohci = ohci;
416 ctx->last_buffer = &ab;
Kristian Høgsberged568912006-12-19 19:58:35 -0500417 tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
418
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500419 ar_context_add_page(ctx);
420 ar_context_add_page(ctx);
421 ctx->current_buffer = ab.next;
422 ctx->pointer = ctx->current_buffer->data;
423
Kristian Høgsberg2aef4692007-05-30 19:06:35 -0400424 return 0;
425}
426
427static void ar_context_run(struct ar_context *ctx)
428{
429 struct ar_buffer *ab = ctx->current_buffer;
430 dma_addr_t ab_bus;
431 size_t offset;
432
433 offset = offsetof(struct ar_buffer, data);
Stefan Richter0a9972b2007-06-23 20:28:17 +0200434 ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
Kristian Høgsberg2aef4692007-05-30 19:06:35 -0400435
436 reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ab_bus | 1);
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400437 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500438 flush_writes(ctx->ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -0500439}
Stefan Richter373b2ed2007-03-04 14:45:18 +0100440
Jarod Wilsona186b4a2007-12-03 13:43:12 -0500441static struct descriptor *
442find_branch_descriptor(struct descriptor *d, int z)
443{
444 int b, key;
445
446 b = (le16_to_cpu(d->control) & DESCRIPTOR_BRANCH_ALWAYS) >> 2;
447 key = (le16_to_cpu(d->control) & DESCRIPTOR_KEY_IMMEDIATE) >> 8;
448
449 /* figure out which descriptor the branch address goes in */
450 if (z == 2 && (b == 3 || key == 2))
451 return d;
452 else
453 return d + z - 1;
454}
455
Kristian Høgsberg30200732007-02-16 17:34:39 -0500456static void context_tasklet(unsigned long data)
457{
458 struct context *ctx = (struct context *) data;
459 struct fw_ohci *ohci = ctx->ohci;
460 struct descriptor *d, *last;
461 u32 address;
462 int z;
463
464 dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
465 ctx->buffer_size, DMA_TO_DEVICE);
466
467 d = ctx->tail_descriptor;
468 last = ctx->tail_descriptor_last;
469
470 while (last->branch_address != 0) {
471 address = le32_to_cpu(last->branch_address);
472 z = address & 0xf;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400473 d = ctx->buffer + (address - ctx->buffer_bus) / sizeof(*d);
Jarod Wilsona186b4a2007-12-03 13:43:12 -0500474 last = find_branch_descriptor(d, z);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500475
476 if (!ctx->callback(ctx, d, last))
477 break;
478
479 ctx->tail_descriptor = d;
480 ctx->tail_descriptor_last = last;
481 }
482}
483
484static int
485context_init(struct context *ctx, struct fw_ohci *ohci,
486 size_t buffer_size, u32 regs,
487 descriptor_callback_t callback)
488{
489 ctx->ohci = ohci;
490 ctx->regs = regs;
491 ctx->buffer_size = buffer_size;
492 ctx->buffer = kmalloc(buffer_size, GFP_KERNEL);
493 if (ctx->buffer == NULL)
494 return -ENOMEM;
495
496 tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
497 ctx->callback = callback;
498
499 ctx->buffer_bus =
500 dma_map_single(ohci->card.device, ctx->buffer,
501 buffer_size, DMA_TO_DEVICE);
502 if (dma_mapping_error(ctx->buffer_bus)) {
503 kfree(ctx->buffer);
504 return -ENOMEM;
505 }
506
507 ctx->head_descriptor = ctx->buffer;
508 ctx->prev_descriptor = ctx->buffer;
509 ctx->tail_descriptor = ctx->buffer;
510 ctx->tail_descriptor_last = ctx->buffer;
511
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400512 /*
513 * We put a dummy descriptor in the buffer that has a NULL
Kristian Høgsberg30200732007-02-16 17:34:39 -0500514 * branch address and looks like it's been sent. That way we
515 * have a descriptor to append DMA programs to. Also, the
516 * ring buffer invariant is that it always has at least one
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400517 * element so that head == tail means buffer full.
518 */
Kristian Høgsberg30200732007-02-16 17:34:39 -0500519
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400520 memset(ctx->head_descriptor, 0, sizeof(*ctx->head_descriptor));
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400521 ctx->head_descriptor->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500522 ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
523 ctx->head_descriptor++;
524
525 return 0;
526}
527
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500528static void
Kristian Høgsberg30200732007-02-16 17:34:39 -0500529context_release(struct context *ctx)
530{
531 struct fw_card *card = &ctx->ohci->card;
532
533 dma_unmap_single(card->device, ctx->buffer_bus,
534 ctx->buffer_size, DMA_TO_DEVICE);
535 kfree(ctx->buffer);
536}
537
538static struct descriptor *
539context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
540{
541 struct descriptor *d, *tail, *end;
542
543 d = ctx->head_descriptor;
544 tail = ctx->tail_descriptor;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400545 end = ctx->buffer + ctx->buffer_size / sizeof(*d);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500546
547 if (d + z <= tail) {
548 goto has_space;
549 } else if (d > tail && d + z <= end) {
550 goto has_space;
551 } else if (d > tail && ctx->buffer + z <= tail) {
552 d = ctx->buffer;
553 goto has_space;
554 }
555
556 return NULL;
557
558 has_space:
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400559 memset(d, 0, z * sizeof(*d));
560 *d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500561
562 return d;
563}
564
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500565static void context_run(struct context *ctx, u32 extra)
Kristian Høgsberg30200732007-02-16 17:34:39 -0500566{
567 struct fw_ohci *ohci = ctx->ohci;
568
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400569 reg_write(ohci, COMMAND_PTR(ctx->regs),
Kristian Høgsberg30200732007-02-16 17:34:39 -0500570 le32_to_cpu(ctx->tail_descriptor_last->branch_address));
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400571 reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
572 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500573 flush_writes(ohci);
574}
575
576static void context_append(struct context *ctx,
577 struct descriptor *d, int z, int extra)
578{
579 dma_addr_t d_bus;
580
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400581 d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500582
583 ctx->head_descriptor = d + z + extra;
584 ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
Jarod Wilsona186b4a2007-12-03 13:43:12 -0500585 ctx->prev_descriptor = find_branch_descriptor(d, z);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500586
587 dma_sync_single_for_device(ctx->ohci->card.device, ctx->buffer_bus,
588 ctx->buffer_size, DMA_TO_DEVICE);
589
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400590 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500591 flush_writes(ctx->ohci);
592}
593
594static void context_stop(struct context *ctx)
595{
596 u32 reg;
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500597 int i;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500598
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400599 reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500600 flush_writes(ctx->ohci);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500601
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500602 for (i = 0; i < 10; i++) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400603 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500604 if ((reg & CONTEXT_ACTIVE) == 0)
605 break;
606
607 fw_notify("context_stop: still active (0x%08x)\n", reg);
Stefan Richterb980f5a2007-07-12 22:25:14 +0200608 mdelay(1);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500609 }
Kristian Høgsberg30200732007-02-16 17:34:39 -0500610}
Kristian Høgsberged568912006-12-19 19:58:35 -0500611
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500612struct driver_data {
Kristian Høgsberged568912006-12-19 19:58:35 -0500613 struct fw_packet *packet;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500614};
615
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400616/*
617 * This function apppends a packet to the DMA queue for transmission.
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500618 * Must always be called with the ochi->lock held to ensure proper
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400619 * generation handling and locking around packet queue manipulation.
620 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500621static int
622at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
623{
Kristian Høgsberged568912006-12-19 19:58:35 -0500624 struct fw_ohci *ohci = ctx->ohci;
Stefan Richter4b6d51e2007-10-21 11:20:07 +0200625 dma_addr_t d_bus, uninitialized_var(payload_bus);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500626 struct driver_data *driver_data;
627 struct descriptor *d, *last;
628 __le32 *header;
Kristian Høgsberged568912006-12-19 19:58:35 -0500629 int z, tcode;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500630 u32 reg;
Kristian Høgsberged568912006-12-19 19:58:35 -0500631
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500632 d = context_get_descriptors(ctx, 4, &d_bus);
633 if (d == NULL) {
634 packet->ack = RCODE_SEND_ERROR;
635 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -0500636 }
637
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400638 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500639 d[0].res_count = cpu_to_le16(packet->timestamp);
640
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400641 /*
642 * The DMA format for asyncronous link packets is different
Kristian Høgsberged568912006-12-19 19:58:35 -0500643 * from the IEEE1394 layout, so shift the fields around
644 * accordingly. If header_length is 8, it's a PHY packet, to
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400645 * which we need to prepend an extra quadlet.
646 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500647
648 header = (__le32 *) &d[1];
Kristian Høgsberged568912006-12-19 19:58:35 -0500649 if (packet->header_length > 8) {
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500650 header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
651 (packet->speed << 16));
652 header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
653 (packet->header[0] & 0xffff0000));
654 header[2] = cpu_to_le32(packet->header[2]);
Kristian Høgsberged568912006-12-19 19:58:35 -0500655
656 tcode = (packet->header[0] >> 4) & 0x0f;
657 if (TCODE_IS_BLOCK_PACKET(tcode))
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500658 header[3] = cpu_to_le32(packet->header[3]);
Kristian Høgsberged568912006-12-19 19:58:35 -0500659 else
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500660 header[3] = (__force __le32) packet->header[3];
661
662 d[0].req_count = cpu_to_le16(packet->header_length);
Kristian Høgsberged568912006-12-19 19:58:35 -0500663 } else {
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500664 header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
665 (packet->speed << 16));
666 header[1] = cpu_to_le32(packet->header[0]);
667 header[2] = cpu_to_le32(packet->header[1]);
668 d[0].req_count = cpu_to_le16(12);
Kristian Høgsberged568912006-12-19 19:58:35 -0500669 }
670
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500671 driver_data = (struct driver_data *) &d[3];
672 driver_data->packet = packet;
Kristian Høgsberg20d11672007-03-26 19:18:19 -0400673 packet->driver_data = driver_data;
Jarod Wilsona186b4a2007-12-03 13:43:12 -0500674
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500675 if (packet->payload_length > 0) {
676 payload_bus =
677 dma_map_single(ohci->card.device, packet->payload,
678 packet->payload_length, DMA_TO_DEVICE);
679 if (dma_mapping_error(payload_bus)) {
680 packet->ack = RCODE_SEND_ERROR;
681 return -1;
682 }
683
684 d[2].req_count = cpu_to_le16(packet->payload_length);
685 d[2].data_address = cpu_to_le32(payload_bus);
686 last = &d[2];
687 z = 3;
688 } else {
689 last = &d[0];
690 z = 2;
691 }
692
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400693 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
694 DESCRIPTOR_IRQ_ALWAYS |
695 DESCRIPTOR_BRANCH_ALWAYS);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500696
Kristian Høgsberged568912006-12-19 19:58:35 -0500697 /* FIXME: Document how the locking works. */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500698 if (ohci->generation != packet->generation) {
Stefan Richterab88ca42007-08-29 19:40:28 +0200699 if (packet->payload_length > 0)
700 dma_unmap_single(ohci->card.device, payload_bus,
701 packet->payload_length, DMA_TO_DEVICE);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500702 packet->ack = RCODE_GENERATION;
703 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -0500704 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500705
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500706 context_append(ctx, d, z, 4 - z);
Kristian Høgsberged568912006-12-19 19:58:35 -0500707
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500708 /* If the context isn't already running, start it up. */
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400709 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
Kristian Høgsberg053b3082007-04-10 18:11:17 -0400710 if ((reg & CONTEXT_RUN) == 0)
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500711 context_run(ctx, 0);
Kristian Høgsberged568912006-12-19 19:58:35 -0500712
713 return 0;
714}
715
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500716static int handle_at_packet(struct context *context,
717 struct descriptor *d,
718 struct descriptor *last)
719{
720 struct driver_data *driver_data;
721 struct fw_packet *packet;
722 struct fw_ohci *ohci = context->ohci;
723 dma_addr_t payload_bus;
724 int evt;
725
726 if (last->transfer_status == 0)
727 /* This descriptor isn't done yet, stop iteration. */
728 return 0;
729
730 driver_data = (struct driver_data *) &d[3];
731 packet = driver_data->packet;
732 if (packet == NULL)
733 /* This packet was cancelled, just continue. */
734 return 1;
735
736 payload_bus = le32_to_cpu(last->data_address);
737 if (payload_bus != 0)
738 dma_unmap_single(ohci->card.device, payload_bus,
739 packet->payload_length, DMA_TO_DEVICE);
740
741 evt = le16_to_cpu(last->transfer_status) & 0x1f;
742 packet->timestamp = le16_to_cpu(last->res_count);
743
744 switch (evt) {
745 case OHCI1394_evt_timeout:
746 /* Async response transmit timed out. */
747 packet->ack = RCODE_CANCELLED;
748 break;
749
750 case OHCI1394_evt_flushed:
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400751 /*
752 * The packet was flushed should give same error as
753 * when we try to use a stale generation count.
754 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500755 packet->ack = RCODE_GENERATION;
756 break;
757
758 case OHCI1394_evt_missing_ack:
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400759 /*
760 * Using a valid (current) generation count, but the
761 * node is not on the bus or not sending acks.
762 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500763 packet->ack = RCODE_NO_ACK;
764 break;
765
766 case ACK_COMPLETE + 0x10:
767 case ACK_PENDING + 0x10:
768 case ACK_BUSY_X + 0x10:
769 case ACK_BUSY_A + 0x10:
770 case ACK_BUSY_B + 0x10:
771 case ACK_DATA_ERROR + 0x10:
772 case ACK_TYPE_ERROR + 0x10:
773 packet->ack = evt - 0x10;
774 break;
775
776 default:
777 packet->ack = RCODE_SEND_ERROR;
778 break;
779 }
780
781 packet->callback(packet, &ohci->card, packet->ack);
782
783 return 1;
784}
785
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400786#define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
787#define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
788#define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
789#define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
790#define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500791
792static void
793handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
794{
795 struct fw_packet response;
796 int tcode, length, i;
797
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400798 tcode = HEADER_GET_TCODE(packet->header[0]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500799 if (TCODE_IS_BLOCK_PACKET(tcode))
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400800 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500801 else
802 length = 4;
803
804 i = csr - CSR_CONFIG_ROM;
805 if (i + length > CONFIG_ROM_SIZE) {
806 fw_fill_response(&response, packet->header,
807 RCODE_ADDRESS_ERROR, NULL, 0);
808 } else if (!TCODE_IS_READ_REQUEST(tcode)) {
809 fw_fill_response(&response, packet->header,
810 RCODE_TYPE_ERROR, NULL, 0);
811 } else {
812 fw_fill_response(&response, packet->header, RCODE_COMPLETE,
813 (void *) ohci->config_rom + i, length);
814 }
815
816 fw_core_handle_response(&ohci->card, &response);
817}
818
819static void
820handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
821{
822 struct fw_packet response;
823 int tcode, length, ext_tcode, sel;
824 __be32 *payload, lock_old;
825 u32 lock_arg, lock_data;
826
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400827 tcode = HEADER_GET_TCODE(packet->header[0]);
828 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500829 payload = packet->payload;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400830 ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500831
832 if (tcode == TCODE_LOCK_REQUEST &&
833 ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
834 lock_arg = be32_to_cpu(payload[0]);
835 lock_data = be32_to_cpu(payload[1]);
836 } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
837 lock_arg = 0;
838 lock_data = 0;
839 } else {
840 fw_fill_response(&response, packet->header,
841 RCODE_TYPE_ERROR, NULL, 0);
842 goto out;
843 }
844
845 sel = (csr - CSR_BUS_MANAGER_ID) / 4;
846 reg_write(ohci, OHCI1394_CSRData, lock_data);
847 reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
848 reg_write(ohci, OHCI1394_CSRControl, sel);
849
850 if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
851 lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
852 else
853 fw_notify("swap not done yet\n");
854
855 fw_fill_response(&response, packet->header,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400856 RCODE_COMPLETE, &lock_old, sizeof(lock_old));
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500857 out:
858 fw_core_handle_response(&ohci->card, &response);
859}
860
861static void
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500862handle_local_request(struct context *ctx, struct fw_packet *packet)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500863{
864 u64 offset;
865 u32 csr;
866
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500867 if (ctx == &ctx->ohci->at_request_ctx) {
868 packet->ack = ACK_PENDING;
869 packet->callback(packet, &ctx->ohci->card, packet->ack);
870 }
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500871
872 offset =
873 ((unsigned long long)
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400874 HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500875 packet->header[2];
876 csr = offset - CSR_REGISTER_BASE;
877
878 /* Handle config rom reads. */
879 if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
880 handle_local_rom(ctx->ohci, packet, csr);
881 else switch (csr) {
882 case CSR_BUS_MANAGER_ID:
883 case CSR_BANDWIDTH_AVAILABLE:
884 case CSR_CHANNELS_AVAILABLE_HI:
885 case CSR_CHANNELS_AVAILABLE_LO:
886 handle_local_lock(ctx->ohci, packet, csr);
887 break;
888 default:
889 if (ctx == &ctx->ohci->at_request_ctx)
890 fw_core_handle_request(&ctx->ohci->card, packet);
891 else
892 fw_core_handle_response(&ctx->ohci->card, packet);
893 break;
894 }
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500895
896 if (ctx == &ctx->ohci->at_response_ctx) {
897 packet->ack = ACK_COMPLETE;
898 packet->callback(packet, &ctx->ohci->card, packet->ack);
899 }
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500900}
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500901
Kristian Høgsberged568912006-12-19 19:58:35 -0500902static void
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500903at_context_transmit(struct context *ctx, struct fw_packet *packet)
Kristian Høgsberged568912006-12-19 19:58:35 -0500904{
Kristian Høgsberged568912006-12-19 19:58:35 -0500905 unsigned long flags;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500906 int retval;
Kristian Høgsberged568912006-12-19 19:58:35 -0500907
908 spin_lock_irqsave(&ctx->ohci->lock, flags);
909
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400910 if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500911 ctx->ohci->generation == packet->generation) {
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500912 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
913 handle_local_request(ctx, packet);
914 return;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500915 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500916
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500917 retval = at_context_queue_packet(ctx, packet);
Kristian Høgsberged568912006-12-19 19:58:35 -0500918 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
919
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500920 if (retval < 0)
921 packet->callback(packet, &ctx->ohci->card, packet->ack);
Jarod Wilsona186b4a2007-12-03 13:43:12 -0500922
Kristian Høgsberged568912006-12-19 19:58:35 -0500923}
924
925static void bus_reset_tasklet(unsigned long data)
926{
927 struct fw_ohci *ohci = (struct fw_ohci *)data;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500928 int self_id_count, i, j, reg;
Kristian Høgsberged568912006-12-19 19:58:35 -0500929 int generation, new_generation;
930 unsigned long flags;
Stefan Richter4eaff7d2007-07-25 19:18:08 +0200931 void *free_rom = NULL;
932 dma_addr_t free_rom_bus = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -0500933
934 reg = reg_read(ohci, OHCI1394_NodeID);
935 if (!(reg & OHCI1394_NodeID_idValid)) {
Stefan Richter02ff8f82007-08-30 00:11:40 +0200936 fw_notify("node ID not valid, new bus reset in progress\n");
Kristian Høgsberged568912006-12-19 19:58:35 -0500937 return;
938 }
Stefan Richter02ff8f82007-08-30 00:11:40 +0200939 if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
940 fw_notify("malconfigured bus\n");
941 return;
942 }
943 ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
944 OHCI1394_NodeID_nodeNumber);
Kristian Høgsberged568912006-12-19 19:58:35 -0500945
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400946 /*
947 * The count in the SelfIDCount register is the number of
Kristian Høgsberged568912006-12-19 19:58:35 -0500948 * bytes in the self ID receive buffer. Since we also receive
949 * the inverted quadlets and a header quadlet, we shift one
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400950 * bit extra to get the actual number of self IDs.
951 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500952
953 self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
954 generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
Stefan Richteree71c2f2007-08-25 14:08:19 +0200955 rmb();
Kristian Høgsberged568912006-12-19 19:58:35 -0500956
957 for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
958 if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
959 fw_error("inconsistent self IDs\n");
960 ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
961 }
Stefan Richteree71c2f2007-08-25 14:08:19 +0200962 rmb();
Kristian Høgsberged568912006-12-19 19:58:35 -0500963
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400964 /*
965 * Check the consistency of the self IDs we just read. The
Kristian Høgsberged568912006-12-19 19:58:35 -0500966 * problem we face is that a new bus reset can start while we
967 * read out the self IDs from the DMA buffer. If this happens,
968 * the DMA buffer will be overwritten with new self IDs and we
969 * will read out inconsistent data. The OHCI specification
970 * (section 11.2) recommends a technique similar to
971 * linux/seqlock.h, where we remember the generation of the
972 * self IDs in the buffer before reading them out and compare
973 * it to the current generation after reading them out. If
974 * the two generations match we know we have a consistent set
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400975 * of self IDs.
976 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500977
978 new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
979 if (new_generation != generation) {
980 fw_notify("recursive bus reset detected, "
981 "discarding self ids\n");
982 return;
983 }
984
985 /* FIXME: Document how the locking works. */
986 spin_lock_irqsave(&ohci->lock, flags);
987
988 ohci->generation = generation;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500989 context_stop(&ohci->at_request_ctx);
990 context_stop(&ohci->at_response_ctx);
Kristian Høgsberged568912006-12-19 19:58:35 -0500991 reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
992
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400993 /*
994 * This next bit is unrelated to the AT context stuff but we
Kristian Høgsberged568912006-12-19 19:58:35 -0500995 * have to do it under the spinlock also. If a new config rom
996 * was set up before this reset, the old one is now no longer
997 * in use and we can free it. Update the config rom pointers
998 * to point to the current config rom and clear the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400999 * next_config_rom pointer so a new udpate can take place.
1000 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001001
1002 if (ohci->next_config_rom != NULL) {
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001003 if (ohci->next_config_rom != ohci->config_rom) {
1004 free_rom = ohci->config_rom;
1005 free_rom_bus = ohci->config_rom_bus;
1006 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001007 ohci->config_rom = ohci->next_config_rom;
1008 ohci->config_rom_bus = ohci->next_config_rom_bus;
1009 ohci->next_config_rom = NULL;
1010
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001011 /*
1012 * Restore config_rom image and manually update
Kristian Høgsberged568912006-12-19 19:58:35 -05001013 * config_rom registers. Writing the header quadlet
1014 * will indicate that the config rom is ready, so we
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001015 * do that last.
1016 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001017 reg_write(ohci, OHCI1394_BusOptions,
1018 be32_to_cpu(ohci->config_rom[2]));
1019 ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
1020 reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
1021 }
1022
1023 spin_unlock_irqrestore(&ohci->lock, flags);
1024
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001025 if (free_rom)
1026 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1027 free_rom, free_rom_bus);
1028
Kristian Høgsberge636fe22007-01-26 00:38:04 -05001029 fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
Kristian Høgsberged568912006-12-19 19:58:35 -05001030 self_id_count, ohci->self_id_buffer);
1031}
1032
1033static irqreturn_t irq_handler(int irq, void *data)
1034{
1035 struct fw_ohci *ohci = data;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001036 u32 event, iso_event, cycle_time;
Kristian Høgsberged568912006-12-19 19:58:35 -05001037 int i;
1038
1039 event = reg_read(ohci, OHCI1394_IntEventClear);
1040
Stefan Richtera5159582007-06-09 19:31:14 +02001041 if (!event || !~event)
Kristian Høgsberged568912006-12-19 19:58:35 -05001042 return IRQ_NONE;
1043
1044 reg_write(ohci, OHCI1394_IntEventClear, event);
1045
1046 if (event & OHCI1394_selfIDComplete)
1047 tasklet_schedule(&ohci->bus_reset_tasklet);
1048
1049 if (event & OHCI1394_RQPkt)
1050 tasklet_schedule(&ohci->ar_request_ctx.tasklet);
1051
1052 if (event & OHCI1394_RSPkt)
1053 tasklet_schedule(&ohci->ar_response_ctx.tasklet);
1054
1055 if (event & OHCI1394_reqTxComplete)
1056 tasklet_schedule(&ohci->at_request_ctx.tasklet);
1057
1058 if (event & OHCI1394_respTxComplete)
1059 tasklet_schedule(&ohci->at_response_ctx.tasklet);
1060
Kristian Høgsbergc8894752007-02-16 17:34:36 -05001061 iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
Kristian Høgsberged568912006-12-19 19:58:35 -05001062 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
1063
1064 while (iso_event) {
1065 i = ffs(iso_event) - 1;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001066 tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001067 iso_event &= ~(1 << i);
1068 }
1069
Kristian Høgsbergc8894752007-02-16 17:34:36 -05001070 iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
Kristian Høgsberged568912006-12-19 19:58:35 -05001071 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
1072
1073 while (iso_event) {
1074 i = ffs(iso_event) - 1;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001075 tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001076 iso_event &= ~(1 << i);
1077 }
1078
Stefan Richtere524f6162007-08-20 21:58:30 +02001079 if (unlikely(event & OHCI1394_postedWriteErr))
1080 fw_error("PCI posted write error\n");
1081
Stefan Richterbb9f2202007-12-22 22:14:52 +01001082 if (unlikely(event & OHCI1394_cycleTooLong)) {
1083 if (printk_ratelimit())
1084 fw_notify("isochronous cycle too long\n");
1085 reg_write(ohci, OHCI1394_LinkControlSet,
1086 OHCI1394_LinkControl_cycleMaster);
1087 }
1088
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001089 if (event & OHCI1394_cycle64Seconds) {
1090 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1091 if ((cycle_time & 0x80000000) == 0)
1092 ohci->bus_seconds++;
1093 }
1094
Kristian Høgsberged568912006-12-19 19:58:35 -05001095 return IRQ_HANDLED;
1096}
1097
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001098static int software_reset(struct fw_ohci *ohci)
1099{
1100 int i;
1101
1102 reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
1103
1104 for (i = 0; i < OHCI_LOOP_COUNT; i++) {
1105 if ((reg_read(ohci, OHCI1394_HCControlSet) &
1106 OHCI1394_HCControl_softReset) == 0)
1107 return 0;
1108 msleep(1);
1109 }
1110
1111 return -EBUSY;
1112}
1113
Kristian Høgsberged568912006-12-19 19:58:35 -05001114static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
1115{
1116 struct fw_ohci *ohci = fw_ohci(card);
1117 struct pci_dev *dev = to_pci_dev(card->device);
1118
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001119 if (software_reset(ohci)) {
1120 fw_error("Failed to reset ohci card.\n");
1121 return -EBUSY;
1122 }
1123
1124 /*
1125 * Now enable LPS, which we need in order to start accessing
1126 * most of the registers. In fact, on some cards (ALI M5251),
1127 * accessing registers in the SClk domain without LPS enabled
1128 * will lock up the machine. Wait 50msec to make sure we have
1129 * full link enabled.
1130 */
1131 reg_write(ohci, OHCI1394_HCControlSet,
1132 OHCI1394_HCControl_LPS |
1133 OHCI1394_HCControl_postedWriteEnable);
1134 flush_writes(ohci);
1135 msleep(50);
1136
1137 reg_write(ohci, OHCI1394_HCControlClear,
1138 OHCI1394_HCControl_noByteSwapData);
1139
1140 reg_write(ohci, OHCI1394_LinkControlSet,
1141 OHCI1394_LinkControl_rcvSelfID |
1142 OHCI1394_LinkControl_cycleTimerEnable |
1143 OHCI1394_LinkControl_cycleMaster);
1144
1145 reg_write(ohci, OHCI1394_ATRetries,
1146 OHCI1394_MAX_AT_REQ_RETRIES |
1147 (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
1148 (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
1149
1150 ar_context_run(&ohci->ar_request_ctx);
1151 ar_context_run(&ohci->ar_response_ctx);
1152
1153 reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
1154 reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
1155 reg_write(ohci, OHCI1394_IntEventClear, ~0);
1156 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1157 reg_write(ohci, OHCI1394_IntMaskSet,
1158 OHCI1394_selfIDComplete |
1159 OHCI1394_RQPkt | OHCI1394_RSPkt |
1160 OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
1161 OHCI1394_isochRx | OHCI1394_isochTx |
Stefan Richterbb9f2202007-12-22 22:14:52 +01001162 OHCI1394_postedWriteErr | OHCI1394_cycleTooLong |
1163 OHCI1394_cycle64Seconds | OHCI1394_masterIntEnable);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001164
1165 /* Activate link_on bit and contender bit in our self ID packets.*/
1166 if (ohci_update_phy_reg(card, 4, 0,
1167 PHY_LINK_ACTIVE | PHY_CONTENDER) < 0)
1168 return -EIO;
1169
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001170 /*
1171 * When the link is not yet enabled, the atomic config rom
Kristian Høgsberged568912006-12-19 19:58:35 -05001172 * update mechanism described below in ohci_set_config_rom()
1173 * is not active. We have to update ConfigRomHeader and
1174 * BusOptions manually, and the write to ConfigROMmap takes
1175 * effect immediately. We tie this to the enabling of the
1176 * link, so we have a valid config rom before enabling - the
1177 * OHCI requires that ConfigROMhdr and BusOptions have valid
1178 * values before enabling.
1179 *
1180 * However, when the ConfigROMmap is written, some controllers
1181 * always read back quadlets 0 and 2 from the config rom to
1182 * the ConfigRomHeader and BusOptions registers on bus reset.
1183 * They shouldn't do that in this initial case where the link
1184 * isn't enabled. This means we have to use the same
1185 * workaround here, setting the bus header to 0 and then write
1186 * the right values in the bus reset tasklet.
1187 */
1188
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001189 if (config_rom) {
1190 ohci->next_config_rom =
1191 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1192 &ohci->next_config_rom_bus,
1193 GFP_KERNEL);
1194 if (ohci->next_config_rom == NULL)
1195 return -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001196
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001197 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1198 fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
1199 } else {
1200 /*
1201 * In the suspend case, config_rom is NULL, which
1202 * means that we just reuse the old config rom.
1203 */
1204 ohci->next_config_rom = ohci->config_rom;
1205 ohci->next_config_rom_bus = ohci->config_rom_bus;
1206 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001207
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001208 ohci->next_header = be32_to_cpu(ohci->next_config_rom[0]);
Kristian Høgsberged568912006-12-19 19:58:35 -05001209 ohci->next_config_rom[0] = 0;
1210 reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001211 reg_write(ohci, OHCI1394_BusOptions,
1212 be32_to_cpu(ohci->next_config_rom[2]));
Kristian Høgsberged568912006-12-19 19:58:35 -05001213 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
1214
1215 reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
1216
1217 if (request_irq(dev->irq, irq_handler,
Thomas Gleixner65efffa2007-03-05 18:19:51 -08001218 IRQF_SHARED, ohci_driver_name, ohci)) {
Kristian Høgsberged568912006-12-19 19:58:35 -05001219 fw_error("Failed to allocate shared interrupt %d.\n",
1220 dev->irq);
1221 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1222 ohci->config_rom, ohci->config_rom_bus);
1223 return -EIO;
1224 }
1225
1226 reg_write(ohci, OHCI1394_HCControlSet,
1227 OHCI1394_HCControl_linkEnable |
1228 OHCI1394_HCControl_BIBimageValid);
1229 flush_writes(ohci);
1230
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001231 /*
1232 * We are ready to go, initiate bus reset to finish the
1233 * initialization.
1234 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001235
1236 fw_core_initiate_bus_reset(&ohci->card, 1);
1237
1238 return 0;
1239}
1240
1241static int
1242ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
1243{
1244 struct fw_ohci *ohci;
1245 unsigned long flags;
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001246 int retval = -EBUSY;
Kristian Høgsberged568912006-12-19 19:58:35 -05001247 __be32 *next_config_rom;
1248 dma_addr_t next_config_rom_bus;
1249
1250 ohci = fw_ohci(card);
1251
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001252 /*
1253 * When the OHCI controller is enabled, the config rom update
Kristian Høgsberged568912006-12-19 19:58:35 -05001254 * mechanism is a bit tricky, but easy enough to use. See
1255 * section 5.5.6 in the OHCI specification.
1256 *
1257 * The OHCI controller caches the new config rom address in a
1258 * shadow register (ConfigROMmapNext) and needs a bus reset
1259 * for the changes to take place. When the bus reset is
1260 * detected, the controller loads the new values for the
1261 * ConfigRomHeader and BusOptions registers from the specified
1262 * config rom and loads ConfigROMmap from the ConfigROMmapNext
1263 * shadow register. All automatically and atomically.
1264 *
1265 * Now, there's a twist to this story. The automatic load of
1266 * ConfigRomHeader and BusOptions doesn't honor the
1267 * noByteSwapData bit, so with a be32 config rom, the
1268 * controller will load be32 values in to these registers
1269 * during the atomic update, even on litte endian
1270 * architectures. The workaround we use is to put a 0 in the
1271 * header quadlet; 0 is endian agnostic and means that the
1272 * config rom isn't ready yet. In the bus reset tasklet we
1273 * then set up the real values for the two registers.
1274 *
1275 * We use ohci->lock to avoid racing with the code that sets
1276 * ohci->next_config_rom to NULL (see bus_reset_tasklet).
1277 */
1278
1279 next_config_rom =
1280 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1281 &next_config_rom_bus, GFP_KERNEL);
1282 if (next_config_rom == NULL)
1283 return -ENOMEM;
1284
1285 spin_lock_irqsave(&ohci->lock, flags);
1286
1287 if (ohci->next_config_rom == NULL) {
1288 ohci->next_config_rom = next_config_rom;
1289 ohci->next_config_rom_bus = next_config_rom_bus;
1290
1291 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1292 fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
1293 length * 4);
1294
1295 ohci->next_header = config_rom[0];
1296 ohci->next_config_rom[0] = 0;
1297
1298 reg_write(ohci, OHCI1394_ConfigROMmap,
1299 ohci->next_config_rom_bus);
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001300 retval = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001301 }
1302
1303 spin_unlock_irqrestore(&ohci->lock, flags);
1304
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001305 /*
1306 * Now initiate a bus reset to have the changes take
Kristian Høgsberged568912006-12-19 19:58:35 -05001307 * effect. We clean up the old config rom memory and DMA
1308 * mappings in the bus reset tasklet, since the OHCI
1309 * controller could need to access it before the bus reset
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001310 * takes effect.
1311 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001312 if (retval == 0)
1313 fw_core_initiate_bus_reset(&ohci->card, 1);
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001314 else
1315 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1316 next_config_rom, next_config_rom_bus);
Kristian Høgsberged568912006-12-19 19:58:35 -05001317
1318 return retval;
1319}
1320
1321static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
1322{
1323 struct fw_ohci *ohci = fw_ohci(card);
1324
1325 at_context_transmit(&ohci->at_request_ctx, packet);
1326}
1327
1328static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
1329{
1330 struct fw_ohci *ohci = fw_ohci(card);
1331
1332 at_context_transmit(&ohci->at_response_ctx, packet);
1333}
1334
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001335static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
1336{
1337 struct fw_ohci *ohci = fw_ohci(card);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001338 struct context *ctx = &ohci->at_request_ctx;
1339 struct driver_data *driver_data = packet->driver_data;
1340 int retval = -ENOENT;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001341
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001342 tasklet_disable(&ctx->tasklet);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001343
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001344 if (packet->ack != 0)
1345 goto out;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001346
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001347 driver_data->packet = NULL;
1348 packet->ack = RCODE_CANCELLED;
1349 packet->callback(packet, &ohci->card, packet->ack);
1350 retval = 0;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001351
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001352 out:
1353 tasklet_enable(&ctx->tasklet);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001354
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001355 return retval;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001356}
1357
Kristian Høgsberged568912006-12-19 19:58:35 -05001358static int
1359ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
1360{
1361 struct fw_ohci *ohci = fw_ohci(card);
1362 unsigned long flags;
Stefan Richter907293d2007-01-23 21:11:43 +01001363 int n, retval = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001364
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001365 /*
1366 * FIXME: Make sure this bitmask is cleared when we clear the busReset
1367 * interrupt bit. Clear physReqResourceAllBuses on bus reset.
1368 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001369
1370 spin_lock_irqsave(&ohci->lock, flags);
1371
1372 if (ohci->generation != generation) {
1373 retval = -ESTALE;
1374 goto out;
1375 }
1376
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001377 /*
1378 * Note, if the node ID contains a non-local bus ID, physical DMA is
1379 * enabled for _all_ nodes on remote buses.
1380 */
Stefan Richter907293d2007-01-23 21:11:43 +01001381
1382 n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
1383 if (n < 32)
1384 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
1385 else
1386 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
1387
Kristian Høgsberged568912006-12-19 19:58:35 -05001388 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -05001389 out:
Stefan Richter6cad95f2007-01-21 20:46:45 +01001390 spin_unlock_irqrestore(&ohci->lock, flags);
Kristian Høgsberged568912006-12-19 19:58:35 -05001391 return retval;
1392}
Stefan Richter373b2ed2007-03-04 14:45:18 +01001393
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001394static u64
1395ohci_get_bus_time(struct fw_card *card)
1396{
1397 struct fw_ohci *ohci = fw_ohci(card);
1398 u32 cycle_time;
1399 u64 bus_time;
1400
1401 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1402 bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
1403
1404 return bus_time;
1405}
1406
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001407static int handle_ir_dualbuffer_packet(struct context *context,
1408 struct descriptor *d,
1409 struct descriptor *last)
Kristian Høgsberged568912006-12-19 19:58:35 -05001410{
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001411 struct iso_context *ctx =
1412 container_of(context, struct iso_context, context);
1413 struct db_descriptor *db = (struct db_descriptor *) d;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001414 __le32 *ir_header;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001415 size_t header_length;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001416 void *p, *end;
1417 int i;
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001418
David Moore0642b652007-12-19 03:09:18 -05001419 if (db->first_res_count > 0 && db->second_res_count > 0) {
1420 if (ctx->excess_bytes <= le16_to_cpu(db->second_req_count)) {
1421 /* This descriptor isn't done yet, stop iteration. */
1422 return 0;
1423 }
1424 ctx->excess_bytes -= le16_to_cpu(db->second_req_count);
1425 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001426
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001427 header_length = le16_to_cpu(db->first_req_count) -
1428 le16_to_cpu(db->first_res_count);
1429
1430 i = ctx->header_length;
1431 p = db + 1;
1432 end = p + header_length;
1433 while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001434 /*
1435 * The iso header is byteswapped to little endian by
Kristian Høgsberg15536222007-04-10 18:11:16 -04001436 * the controller, but the remaining header quadlets
1437 * are big endian. We want to present all the headers
1438 * as big endian, so we have to swap the first
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001439 * quadlet.
1440 */
Kristian Høgsberg15536222007-04-10 18:11:16 -04001441 *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
1442 memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001443 i += ctx->base.header_size;
David Moore0642b652007-12-19 03:09:18 -05001444 ctx->excess_bytes +=
1445 (le32_to_cpu(*(u32 *)(p + 4)) >> 16) & 0xffff;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001446 p += ctx->base.header_size + 4;
1447 }
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001448 ctx->header_length = i;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001449
David Moore0642b652007-12-19 03:09:18 -05001450 ctx->excess_bytes -= le16_to_cpu(db->second_req_count) -
1451 le16_to_cpu(db->second_res_count);
1452
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001453 if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001454 ir_header = (__le32 *) (db + 1);
1455 ctx->base.callback(&ctx->base,
1456 le32_to_cpu(ir_header[0]) & 0xffff,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001457 ctx->header_length, ctx->header,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001458 ctx->base.callback_data);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001459 ctx->header_length = 0;
1460 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001461
1462 return 1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001463}
1464
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001465static int handle_ir_packet_per_buffer(struct context *context,
1466 struct descriptor *d,
1467 struct descriptor *last)
1468{
1469 struct iso_context *ctx =
1470 container_of(context, struct iso_context, context);
David Moorebcee8932007-12-19 15:26:38 -05001471 struct descriptor *pd;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001472 __le32 *ir_header;
David Moorebcee8932007-12-19 15:26:38 -05001473 void *p;
1474 int i;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001475
David Moorebcee8932007-12-19 15:26:38 -05001476 for (pd = d; pd <= last; pd++) {
1477 if (pd->transfer_status)
1478 break;
1479 }
1480 if (pd > last)
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001481 /* Descriptor(s) not done yet, stop iteration */
1482 return 0;
1483
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001484 i = ctx->header_length;
David Moorebcee8932007-12-19 15:26:38 -05001485 p = last + 1;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001486
David Moorebcee8932007-12-19 15:26:38 -05001487 if (ctx->base.header_size > 0 &&
1488 i + ctx->base.header_size <= PAGE_SIZE) {
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001489 /*
1490 * The iso header is byteswapped to little endian by
1491 * the controller, but the remaining header quadlets
1492 * are big endian. We want to present all the headers
1493 * as big endian, so we have to swap the first quadlet.
1494 */
1495 *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
1496 memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
David Moorebcee8932007-12-19 15:26:38 -05001497 ctx->header_length += ctx->base.header_size;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001498 }
1499
David Moorebcee8932007-12-19 15:26:38 -05001500 if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS) {
1501 ir_header = (__le32 *) p;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001502 ctx->base.callback(&ctx->base,
1503 le32_to_cpu(ir_header[0]) & 0xffff,
1504 ctx->header_length, ctx->header,
1505 ctx->base.callback_data);
1506 ctx->header_length = 0;
1507 }
1508
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001509 return 1;
1510}
1511
Kristian Høgsberg30200732007-02-16 17:34:39 -05001512static int handle_it_packet(struct context *context,
1513 struct descriptor *d,
1514 struct descriptor *last)
Kristian Høgsberged568912006-12-19 19:58:35 -05001515{
Kristian Høgsberg30200732007-02-16 17:34:39 -05001516 struct iso_context *ctx =
1517 container_of(context, struct iso_context, context);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001518
Kristian Høgsberg30200732007-02-16 17:34:39 -05001519 if (last->transfer_status == 0)
1520 /* This descriptor isn't done yet, stop iteration. */
1521 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001522
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001523 if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001524 ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
1525 0, NULL, ctx->base.callback_data);
Kristian Høgsberged568912006-12-19 19:58:35 -05001526
Kristian Høgsberg30200732007-02-16 17:34:39 -05001527 return 1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001528}
1529
Kristian Høgsberg30200732007-02-16 17:34:39 -05001530static struct fw_iso_context *
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001531ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
Kristian Høgsberged568912006-12-19 19:58:35 -05001532{
1533 struct fw_ohci *ohci = fw_ohci(card);
1534 struct iso_context *ctx, *list;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001535 descriptor_callback_t callback;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001536 u32 *mask, regs;
Kristian Høgsberged568912006-12-19 19:58:35 -05001537 unsigned long flags;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001538 int index, retval = -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001539
1540 if (type == FW_ISO_CONTEXT_TRANSMIT) {
1541 mask = &ohci->it_context_mask;
1542 list = ohci->it_context_list;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001543 callback = handle_it_packet;
Kristian Høgsberged568912006-12-19 19:58:35 -05001544 } else {
Stefan Richter373b2ed2007-03-04 14:45:18 +01001545 mask = &ohci->ir_context_mask;
1546 list = ohci->ir_context_list;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001547 if (ohci->version >= OHCI_VERSION_1_1)
1548 callback = handle_ir_dualbuffer_packet;
1549 else
1550 callback = handle_ir_packet_per_buffer;
Kristian Høgsberged568912006-12-19 19:58:35 -05001551 }
1552
1553 spin_lock_irqsave(&ohci->lock, flags);
1554 index = ffs(*mask) - 1;
1555 if (index >= 0)
1556 *mask &= ~(1 << index);
1557 spin_unlock_irqrestore(&ohci->lock, flags);
1558
1559 if (index < 0)
1560 return ERR_PTR(-EBUSY);
1561
Stefan Richter373b2ed2007-03-04 14:45:18 +01001562 if (type == FW_ISO_CONTEXT_TRANSMIT)
1563 regs = OHCI1394_IsoXmitContextBase(index);
1564 else
1565 regs = OHCI1394_IsoRcvContextBase(index);
1566
Kristian Høgsberged568912006-12-19 19:58:35 -05001567 ctx = &list[index];
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001568 memset(ctx, 0, sizeof(*ctx));
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001569 ctx->header_length = 0;
1570 ctx->header = (void *) __get_free_page(GFP_KERNEL);
1571 if (ctx->header == NULL)
1572 goto out;
1573
Kristian Høgsberg30200732007-02-16 17:34:39 -05001574 retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001575 regs, callback);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001576 if (retval < 0)
1577 goto out_with_header;
Kristian Høgsberged568912006-12-19 19:58:35 -05001578
1579 return &ctx->base;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001580
1581 out_with_header:
1582 free_page((unsigned long)ctx->header);
1583 out:
1584 spin_lock_irqsave(&ohci->lock, flags);
1585 *mask |= 1 << index;
1586 spin_unlock_irqrestore(&ohci->lock, flags);
1587
1588 return ERR_PTR(retval);
Kristian Høgsberged568912006-12-19 19:58:35 -05001589}
1590
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001591static int ohci_start_iso(struct fw_iso_context *base,
1592 s32 cycle, u32 sync, u32 tags)
Kristian Høgsberged568912006-12-19 19:58:35 -05001593{
Stefan Richter373b2ed2007-03-04 14:45:18 +01001594 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberg30200732007-02-16 17:34:39 -05001595 struct fw_ohci *ohci = ctx->context.ohci;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001596 u32 control, match;
Kristian Høgsberged568912006-12-19 19:58:35 -05001597 int index;
1598
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001599 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1600 index = ctx - ohci->it_context_list;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001601 match = 0;
1602 if (cycle >= 0)
1603 match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001604 (cycle & 0x7fff) << 16;
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05001605
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001606 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
1607 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001608 context_run(&ctx->context, match);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001609 } else {
1610 index = ctx - ohci->ir_context_list;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001611 control = IR_CONTEXT_ISOCH_HEADER;
1612 if (ohci->version >= OHCI_VERSION_1_1)
1613 control |= IR_CONTEXT_DUAL_BUFFER_MODE;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001614 match = (tags << 28) | (sync << 8) | ctx->base.channel;
1615 if (cycle >= 0) {
1616 match |= (cycle & 0x07fff) << 12;
1617 control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
1618 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001619
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001620 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
1621 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001622 reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001623 context_run(&ctx->context, control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001624 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001625
1626 return 0;
1627}
1628
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001629static int ohci_stop_iso(struct fw_iso_context *base)
1630{
1631 struct fw_ohci *ohci = fw_ohci(base->card);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001632 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001633 int index;
1634
1635 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1636 index = ctx - ohci->it_context_list;
1637 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
1638 } else {
1639 index = ctx - ohci->ir_context_list;
1640 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
1641 }
1642 flush_writes(ohci);
1643 context_stop(&ctx->context);
1644
1645 return 0;
1646}
1647
Kristian Høgsberged568912006-12-19 19:58:35 -05001648static void ohci_free_iso_context(struct fw_iso_context *base)
1649{
1650 struct fw_ohci *ohci = fw_ohci(base->card);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001651 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberged568912006-12-19 19:58:35 -05001652 unsigned long flags;
1653 int index;
1654
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001655 ohci_stop_iso(base);
1656 context_release(&ctx->context);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001657 free_page((unsigned long)ctx->header);
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001658
Kristian Høgsberged568912006-12-19 19:58:35 -05001659 spin_lock_irqsave(&ohci->lock, flags);
1660
1661 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1662 index = ctx - ohci->it_context_list;
Kristian Høgsberged568912006-12-19 19:58:35 -05001663 ohci->it_context_mask |= 1 << index;
1664 } else {
1665 index = ctx - ohci->ir_context_list;
Kristian Høgsberged568912006-12-19 19:58:35 -05001666 ohci->ir_context_mask |= 1 << index;
1667 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001668
1669 spin_unlock_irqrestore(&ohci->lock, flags);
1670}
1671
1672static int
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001673ohci_queue_iso_transmit(struct fw_iso_context *base,
1674 struct fw_iso_packet *packet,
1675 struct fw_iso_buffer *buffer,
1676 unsigned long payload)
Kristian Høgsberged568912006-12-19 19:58:35 -05001677{
Stefan Richter373b2ed2007-03-04 14:45:18 +01001678 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberg30200732007-02-16 17:34:39 -05001679 struct descriptor *d, *last, *pd;
Kristian Høgsberged568912006-12-19 19:58:35 -05001680 struct fw_iso_packet *p;
1681 __le32 *header;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001682 dma_addr_t d_bus, page_bus;
Kristian Høgsberged568912006-12-19 19:58:35 -05001683 u32 z, header_z, payload_z, irq;
1684 u32 payload_index, payload_end_index, next_page_index;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001685 int page, end_page, i, length, offset;
Kristian Høgsberged568912006-12-19 19:58:35 -05001686
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001687 /*
1688 * FIXME: Cycle lost behavior should be configurable: lose
1689 * packet, retransmit or terminate..
1690 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001691
1692 p = packet;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001693 payload_index = payload;
Kristian Høgsberged568912006-12-19 19:58:35 -05001694
1695 if (p->skip)
1696 z = 1;
1697 else
1698 z = 2;
1699 if (p->header_length > 0)
1700 z++;
1701
1702 /* Determine the first page the payload isn't contained in. */
1703 end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
1704 if (p->payload_length > 0)
1705 payload_z = end_page - (payload_index >> PAGE_SHIFT);
1706 else
1707 payload_z = 0;
1708
1709 z += payload_z;
1710
1711 /* Get header size in number of descriptors. */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001712 header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
Kristian Høgsberged568912006-12-19 19:58:35 -05001713
Kristian Høgsberg30200732007-02-16 17:34:39 -05001714 d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
1715 if (d == NULL)
1716 return -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001717
1718 if (!p->skip) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001719 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
Kristian Høgsberged568912006-12-19 19:58:35 -05001720 d[0].req_count = cpu_to_le16(8);
1721
1722 header = (__le32 *) &d[1];
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001723 header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
1724 IT_HEADER_TAG(p->tag) |
1725 IT_HEADER_TCODE(TCODE_STREAM_DATA) |
1726 IT_HEADER_CHANNEL(ctx->base.channel) |
1727 IT_HEADER_SPEED(ctx->base.speed));
Kristian Høgsberged568912006-12-19 19:58:35 -05001728 header[1] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001729 cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
Kristian Høgsberged568912006-12-19 19:58:35 -05001730 p->payload_length));
1731 }
1732
1733 if (p->header_length > 0) {
1734 d[2].req_count = cpu_to_le16(p->header_length);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001735 d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
Kristian Høgsberged568912006-12-19 19:58:35 -05001736 memcpy(&d[z], p->header, p->header_length);
1737 }
1738
1739 pd = d + z - payload_z;
1740 payload_end_index = payload_index + p->payload_length;
1741 for (i = 0; i < payload_z; i++) {
1742 page = payload_index >> PAGE_SHIFT;
1743 offset = payload_index & ~PAGE_MASK;
1744 next_page_index = (page + 1) << PAGE_SHIFT;
1745 length =
1746 min(next_page_index, payload_end_index) - payload_index;
1747 pd[i].req_count = cpu_to_le16(length);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001748
1749 page_bus = page_private(buffer->pages[page]);
1750 pd[i].data_address = cpu_to_le32(page_bus + offset);
Kristian Høgsberged568912006-12-19 19:58:35 -05001751
1752 payload_index += length;
1753 }
1754
Kristian Høgsberged568912006-12-19 19:58:35 -05001755 if (p->interrupt)
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001756 irq = DESCRIPTOR_IRQ_ALWAYS;
Kristian Høgsberged568912006-12-19 19:58:35 -05001757 else
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001758 irq = DESCRIPTOR_NO_IRQ;
Kristian Høgsberged568912006-12-19 19:58:35 -05001759
Kristian Høgsberg30200732007-02-16 17:34:39 -05001760 last = z == 2 ? d : d + z - 1;
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001761 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
1762 DESCRIPTOR_STATUS |
1763 DESCRIPTOR_BRANCH_ALWAYS |
Kristian Høgsbergcbb59da2007-02-16 17:34:35 -05001764 irq);
Kristian Høgsberged568912006-12-19 19:58:35 -05001765
Kristian Høgsberg30200732007-02-16 17:34:39 -05001766 context_append(&ctx->context, d, z, header_z);
Kristian Høgsberged568912006-12-19 19:58:35 -05001767
1768 return 0;
1769}
Stefan Richter373b2ed2007-03-04 14:45:18 +01001770
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001771static int
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001772ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
1773 struct fw_iso_packet *packet,
1774 struct fw_iso_buffer *buffer,
1775 unsigned long payload)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001776{
1777 struct iso_context *ctx = container_of(base, struct iso_context, base);
1778 struct db_descriptor *db = NULL;
1779 struct descriptor *d;
1780 struct fw_iso_packet *p;
1781 dma_addr_t d_bus, page_bus;
1782 u32 z, header_z, length, rest;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001783 int page, offset, packet_count, header_size;
Stefan Richter373b2ed2007-03-04 14:45:18 +01001784
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001785 /*
1786 * FIXME: Cycle lost behavior should be configurable: lose
1787 * packet, retransmit or terminate..
1788 */
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001789
1790 p = packet;
1791 z = 2;
1792
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001793 /*
1794 * The OHCI controller puts the status word in the header
1795 * buffer too, so we need 4 extra bytes per packet.
1796 */
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001797 packet_count = p->header_length / ctx->base.header_size;
1798 header_size = packet_count * (ctx->base.header_size + 4);
1799
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001800 /* Get header size in number of descriptors. */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001801 header_z = DIV_ROUND_UP(header_size, sizeof(*d));
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001802 page = payload >> PAGE_SHIFT;
1803 offset = payload & ~PAGE_MASK;
1804 rest = p->payload_length;
1805
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001806 /* FIXME: make packet-per-buffer/dual-buffer a context option */
1807 while (rest > 0) {
1808 d = context_get_descriptors(&ctx->context,
1809 z + header_z, &d_bus);
1810 if (d == NULL)
1811 return -ENOMEM;
1812
1813 db = (struct db_descriptor *) d;
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001814 db->control = cpu_to_le16(DESCRIPTOR_STATUS |
1815 DESCRIPTOR_BRANCH_ALWAYS);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001816 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
David Moore0642b652007-12-19 03:09:18 -05001817 if (p->skip && rest == p->payload_length) {
1818 db->control |= cpu_to_le16(DESCRIPTOR_WAIT);
1819 db->first_req_count = db->first_size;
1820 } else {
1821 db->first_req_count = cpu_to_le16(header_size);
1822 }
Kristian Høgsberg1e1d1962007-02-16 17:34:45 -05001823 db->first_res_count = db->first_req_count;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001824 db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
Stefan Richter373b2ed2007-03-04 14:45:18 +01001825
David Moore0642b652007-12-19 03:09:18 -05001826 if (p->skip && rest == p->payload_length)
1827 length = 4;
1828 else if (offset + rest < PAGE_SIZE)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001829 length = rest;
1830 else
1831 length = PAGE_SIZE - offset;
1832
Kristian Høgsberg1e1d1962007-02-16 17:34:45 -05001833 db->second_req_count = cpu_to_le16(length);
1834 db->second_res_count = db->second_req_count;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001835 page_bus = page_private(buffer->pages[page]);
1836 db->second_buffer = cpu_to_le32(page_bus + offset);
1837
Kristian Høgsbergcb2d2cd2007-02-16 17:34:47 -05001838 if (p->interrupt && length == rest)
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001839 db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
Kristian Høgsbergcb2d2cd2007-02-16 17:34:47 -05001840
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001841 context_append(&ctx->context, d, z, header_z);
1842 offset = (offset + length) & ~PAGE_MASK;
1843 rest -= length;
David Moore0642b652007-12-19 03:09:18 -05001844 if (offset == 0)
1845 page++;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001846 }
1847
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001848 return 0;
1849}
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05001850
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001851static int
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001852ohci_queue_iso_receive_packet_per_buffer(struct fw_iso_context *base,
1853 struct fw_iso_packet *packet,
1854 struct fw_iso_buffer *buffer,
1855 unsigned long payload)
1856{
1857 struct iso_context *ctx = container_of(base, struct iso_context, base);
1858 struct descriptor *d = NULL, *pd = NULL;
David Moorebcee8932007-12-19 15:26:38 -05001859 struct fw_iso_packet *p = packet;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001860 dma_addr_t d_bus, page_bus;
1861 u32 z, header_z, rest;
David Moorebcee8932007-12-19 15:26:38 -05001862 int i, j, length;
1863 int page, offset, packet_count, header_size, payload_per_buffer;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001864
1865 /*
1866 * The OHCI controller puts the status word in the
1867 * buffer too, so we need 4 extra bytes per packet.
1868 */
1869 packet_count = p->header_length / ctx->base.header_size;
David Moorebcee8932007-12-19 15:26:38 -05001870 header_size = ctx->base.header_size + 4;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001871
1872 /* Get header size in number of descriptors. */
1873 header_z = DIV_ROUND_UP(header_size, sizeof(*d));
1874 page = payload >> PAGE_SHIFT;
1875 offset = payload & ~PAGE_MASK;
David Moorebcee8932007-12-19 15:26:38 -05001876 payload_per_buffer = p->payload_length / packet_count;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001877
1878 for (i = 0; i < packet_count; i++) {
1879 /* d points to the header descriptor */
David Moorebcee8932007-12-19 15:26:38 -05001880 z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001881 d = context_get_descriptors(&ctx->context,
David Moorebcee8932007-12-19 15:26:38 -05001882 z + header_z, &d_bus);
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001883 if (d == NULL)
1884 return -ENOMEM;
1885
David Moorebcee8932007-12-19 15:26:38 -05001886 d->control = cpu_to_le16(DESCRIPTOR_STATUS |
1887 DESCRIPTOR_INPUT_MORE);
1888 if (p->skip && i == 0)
1889 d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001890 d->req_count = cpu_to_le16(header_size);
1891 d->res_count = d->req_count;
David Moorebcee8932007-12-19 15:26:38 -05001892 d->transfer_status = 0;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001893 d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
1894
David Moorebcee8932007-12-19 15:26:38 -05001895 rest = payload_per_buffer;
1896 for (j = 1; j < z; j++) {
1897 pd = d + j;
1898 pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
1899 DESCRIPTOR_INPUT_MORE);
1900
1901 if (offset + rest < PAGE_SIZE)
1902 length = rest;
1903 else
1904 length = PAGE_SIZE - offset;
1905 pd->req_count = cpu_to_le16(length);
1906 pd->res_count = pd->req_count;
1907 pd->transfer_status = 0;
1908
1909 page_bus = page_private(buffer->pages[page]);
1910 pd->data_address = cpu_to_le32(page_bus + offset);
1911
1912 offset = (offset + length) & ~PAGE_MASK;
1913 rest -= length;
1914 if (offset == 0)
1915 page++;
1916 }
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001917 pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
1918 DESCRIPTOR_INPUT_LAST |
1919 DESCRIPTOR_BRANCH_ALWAYS);
David Moorebcee8932007-12-19 15:26:38 -05001920 if (p->interrupt && i == packet_count - 1)
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001921 pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
1922
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001923 context_append(&ctx->context, d, z, header_z);
1924 }
1925
1926 return 0;
1927}
1928
1929static int
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001930ohci_queue_iso(struct fw_iso_context *base,
1931 struct fw_iso_packet *packet,
1932 struct fw_iso_buffer *buffer,
1933 unsigned long payload)
1934{
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001935 struct iso_context *ctx = container_of(base, struct iso_context, base);
1936
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001937 if (base->type == FW_ISO_CONTEXT_TRANSMIT)
1938 return ohci_queue_iso_transmit(base, packet, buffer, payload);
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001939 else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001940 return ohci_queue_iso_receive_dualbuffer(base, packet,
1941 buffer, payload);
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001942 else
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001943 return ohci_queue_iso_receive_packet_per_buffer(base, packet,
1944 buffer,
1945 payload);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001946}
1947
Stefan Richter21ebcd12007-01-14 15:29:07 +01001948static const struct fw_card_driver ohci_driver = {
Kristian Høgsberged568912006-12-19 19:58:35 -05001949 .name = ohci_driver_name,
1950 .enable = ohci_enable,
1951 .update_phy_reg = ohci_update_phy_reg,
1952 .set_config_rom = ohci_set_config_rom,
1953 .send_request = ohci_send_request,
1954 .send_response = ohci_send_response,
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001955 .cancel_packet = ohci_cancel_packet,
Kristian Høgsberged568912006-12-19 19:58:35 -05001956 .enable_phys_dma = ohci_enable_phys_dma,
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001957 .get_bus_time = ohci_get_bus_time,
Kristian Høgsberged568912006-12-19 19:58:35 -05001958
1959 .allocate_iso_context = ohci_allocate_iso_context,
1960 .free_iso_context = ohci_free_iso_context,
1961 .queue_iso = ohci_queue_iso,
Kristian Høgsberg69cdb722007-02-16 17:34:41 -05001962 .start_iso = ohci_start_iso,
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001963 .stop_iso = ohci_stop_iso,
Kristian Høgsberged568912006-12-19 19:58:35 -05001964};
1965
Kristian Høgsberged568912006-12-19 19:58:35 -05001966static int __devinit
1967pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
1968{
1969 struct fw_ohci *ohci;
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001970 u32 bus_options, max_receive, link_speed;
Kristian Høgsberged568912006-12-19 19:58:35 -05001971 u64 guid;
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001972 int err;
Kristian Høgsberged568912006-12-19 19:58:35 -05001973 size_t size;
1974
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001975 ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
Kristian Høgsberged568912006-12-19 19:58:35 -05001976 if (ohci == NULL) {
1977 fw_error("Could not malloc fw_ohci data.\n");
1978 return -ENOMEM;
1979 }
1980
1981 fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
1982
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001983 err = pci_enable_device(dev);
1984 if (err) {
Kristian Høgsberged568912006-12-19 19:58:35 -05001985 fw_error("Failed to enable OHCI hardware.\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001986 goto fail_put_card;
Kristian Høgsberged568912006-12-19 19:58:35 -05001987 }
1988
1989 pci_set_master(dev);
1990 pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
1991 pci_set_drvdata(dev, ohci);
1992
1993 spin_lock_init(&ohci->lock);
1994
1995 tasklet_init(&ohci->bus_reset_tasklet,
1996 bus_reset_tasklet, (unsigned long)ohci);
1997
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001998 err = pci_request_region(dev, 0, ohci_driver_name);
1999 if (err) {
Kristian Høgsberged568912006-12-19 19:58:35 -05002000 fw_error("MMIO resource unavailable\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002001 goto fail_disable;
Kristian Høgsberged568912006-12-19 19:58:35 -05002002 }
2003
2004 ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
2005 if (ohci->registers == NULL) {
2006 fw_error("Failed to remap registers\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002007 err = -ENXIO;
2008 goto fail_iomem;
Kristian Høgsberged568912006-12-19 19:58:35 -05002009 }
2010
Kristian Høgsberged568912006-12-19 19:58:35 -05002011 ar_context_init(&ohci->ar_request_ctx, ohci,
2012 OHCI1394_AsReqRcvContextControlSet);
2013
2014 ar_context_init(&ohci->ar_response_ctx, ohci,
2015 OHCI1394_AsRspRcvContextControlSet);
2016
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05002017 context_init(&ohci->at_request_ctx, ohci, AT_BUFFER_SIZE,
2018 OHCI1394_AsReqTrContextControlSet, handle_at_packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05002019
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05002020 context_init(&ohci->at_response_ctx, ohci, AT_BUFFER_SIZE,
2021 OHCI1394_AsRspTrContextControlSet, handle_at_packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05002022
Kristian Høgsberged568912006-12-19 19:58:35 -05002023 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
2024 ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
2025 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
2026 size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
2027 ohci->it_context_list = kzalloc(size, GFP_KERNEL);
2028
2029 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
2030 ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
2031 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
2032 size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
2033 ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
2034
2035 if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
2036 fw_error("Out of memory for it/ir contexts.\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002037 err = -ENOMEM;
2038 goto fail_registers;
Kristian Høgsberged568912006-12-19 19:58:35 -05002039 }
2040
2041 /* self-id dma buffer allocation */
2042 ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
2043 SELF_ID_BUF_SIZE,
2044 &ohci->self_id_bus,
2045 GFP_KERNEL);
2046 if (ohci->self_id_cpu == NULL) {
2047 fw_error("Out of memory for self ID buffer.\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002048 err = -ENOMEM;
2049 goto fail_registers;
Kristian Høgsberged568912006-12-19 19:58:35 -05002050 }
2051
Kristian Høgsberged568912006-12-19 19:58:35 -05002052 bus_options = reg_read(ohci, OHCI1394_BusOptions);
2053 max_receive = (bus_options >> 12) & 0xf;
2054 link_speed = bus_options & 0x7;
2055 guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
2056 reg_read(ohci, OHCI1394_GUIDLo);
2057
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002058 err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
2059 if (err < 0)
2060 goto fail_self_id;
Kristian Høgsberged568912006-12-19 19:58:35 -05002061
Kristian Høgsberge364cf42007-02-16 17:34:49 -05002062 ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
Kristian Høgsberg500be722007-02-16 17:34:43 -05002063 fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
Kristian Høgsberge364cf42007-02-16 17:34:49 -05002064 dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
Kristian Høgsberged568912006-12-19 19:58:35 -05002065 return 0;
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002066
2067 fail_self_id:
2068 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
2069 ohci->self_id_cpu, ohci->self_id_bus);
2070 fail_registers:
2071 kfree(ohci->it_context_list);
2072 kfree(ohci->ir_context_list);
2073 pci_iounmap(dev, ohci->registers);
2074 fail_iomem:
2075 pci_release_region(dev, 0);
2076 fail_disable:
2077 pci_disable_device(dev);
2078 fail_put_card:
2079 fw_card_put(&ohci->card);
2080
2081 return err;
Kristian Høgsberged568912006-12-19 19:58:35 -05002082}
2083
2084static void pci_remove(struct pci_dev *dev)
2085{
2086 struct fw_ohci *ohci;
2087
2088 ohci = pci_get_drvdata(dev);
Kristian Høgsberge254a4b2007-03-07 12:12:38 -05002089 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
2090 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -05002091 fw_core_remove_card(&ohci->card);
2092
Kristian Høgsbergc781c062007-05-07 20:33:32 -04002093 /*
2094 * FIXME: Fail all pending packets here, now that the upper
2095 * layers can't queue any more.
2096 */
Kristian Høgsberged568912006-12-19 19:58:35 -05002097
2098 software_reset(ohci);
2099 free_irq(dev->irq, ohci);
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002100 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
2101 ohci->self_id_cpu, ohci->self_id_bus);
2102 kfree(ohci->it_context_list);
2103 kfree(ohci->ir_context_list);
2104 pci_iounmap(dev, ohci->registers);
2105 pci_release_region(dev, 0);
2106 pci_disable_device(dev);
2107 fw_card_put(&ohci->card);
Kristian Høgsberged568912006-12-19 19:58:35 -05002108
2109 fw_notify("Removed fw-ohci device.\n");
2110}
2111
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002112#ifdef CONFIG_PM
2113static int pci_suspend(struct pci_dev *pdev, pm_message_t state)
2114{
2115 struct fw_ohci *ohci = pci_get_drvdata(pdev);
2116 int err;
2117
2118 software_reset(ohci);
2119 free_irq(pdev->irq, ohci);
2120 err = pci_save_state(pdev);
2121 if (err) {
Stefan Richter8a8cea22007-06-09 19:26:22 +02002122 fw_error("pci_save_state failed\n");
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002123 return err;
2124 }
2125 err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
Stefan Richter55111422007-09-06 09:50:30 +02002126 if (err)
2127 fw_error("pci_set_power_state failed with %d\n", err);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002128
2129 return 0;
2130}
2131
2132static int pci_resume(struct pci_dev *pdev)
2133{
2134 struct fw_ohci *ohci = pci_get_drvdata(pdev);
2135 int err;
2136
2137 pci_set_power_state(pdev, PCI_D0);
2138 pci_restore_state(pdev);
2139 err = pci_enable_device(pdev);
2140 if (err) {
Stefan Richter8a8cea22007-06-09 19:26:22 +02002141 fw_error("pci_enable_device failed\n");
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002142 return err;
2143 }
2144
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04002145 return ohci_enable(&ohci->card, NULL, 0);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002146}
2147#endif
2148
Kristian Høgsberged568912006-12-19 19:58:35 -05002149static struct pci_device_id pci_table[] = {
2150 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
2151 { }
2152};
2153
2154MODULE_DEVICE_TABLE(pci, pci_table);
2155
2156static struct pci_driver fw_ohci_pci_driver = {
2157 .name = ohci_driver_name,
2158 .id_table = pci_table,
2159 .probe = pci_probe,
2160 .remove = pci_remove,
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002161#ifdef CONFIG_PM
2162 .resume = pci_resume,
2163 .suspend = pci_suspend,
2164#endif
Kristian Høgsberged568912006-12-19 19:58:35 -05002165};
2166
2167MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
2168MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
2169MODULE_LICENSE("GPL");
2170
Olaf Hering1e4c7b02007-05-05 23:17:13 +02002171/* Provide a module alias so root-on-sbp2 initrds don't break. */
2172#ifndef CONFIG_IEEE1394_OHCI1394_MODULE
2173MODULE_ALIAS("ohci1394");
2174#endif
2175
Kristian Høgsberged568912006-12-19 19:58:35 -05002176static int __init fw_ohci_init(void)
2177{
2178 return pci_register_driver(&fw_ohci_pci_driver);
2179}
2180
2181static void __exit fw_ohci_cleanup(void)
2182{
2183 pci_unregister_driver(&fw_ohci_pci_driver);
2184}
2185
2186module_init(fw_ohci_init);
2187module_exit(fw_ohci_cleanup);