blob: beb924403dabcaee90620fd401c00473a3f2e395 [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
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <linux/delay.h>
27#include <linux/poll.h>
Andrew Mortoncf3e72f2006-12-27 14:36:37 -080028#include <linux/dma-mapping.h>
29
Kristian Høgsberged568912006-12-19 19:58:35 -050030#include <asm/uaccess.h>
31#include <asm/semaphore.h>
32
33#include "fw-transaction.h"
34#include "fw-ohci.h"
35
36#define descriptor_output_more 0
37#define descriptor_output_last (1 << 12)
38#define descriptor_input_more (2 << 12)
39#define descriptor_input_last (3 << 12)
40#define descriptor_status (1 << 11)
41#define descriptor_key_immediate (2 << 8)
42#define descriptor_ping (1 << 7)
43#define descriptor_yy (1 << 6)
44#define descriptor_no_irq (0 << 4)
45#define descriptor_irq_error (1 << 4)
46#define descriptor_irq_always (3 << 4)
47#define descriptor_branch_always (3 << 2)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -050048#define descriptor_wait (3 << 0)
Kristian Høgsberged568912006-12-19 19:58:35 -050049
50struct descriptor {
51 __le16 req_count;
52 __le16 control;
53 __le32 data_address;
54 __le32 branch_address;
55 __le16 res_count;
56 __le16 transfer_status;
57} __attribute__((aligned(16)));
58
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -050059struct db_descriptor {
60 __le16 first_size;
61 __le16 control;
62 __le16 second_req_count;
63 __le16 first_req_count;
64 __le32 branch_address;
65 __le16 second_res_count;
66 __le16 first_res_count;
67 __le32 reserved0;
68 __le32 first_buffer;
69 __le32 second_buffer;
70 __le32 reserved1;
71} __attribute__((aligned(16)));
72
Kristian Høgsberg72e318e2007-02-06 14:49:31 -050073#define control_set(regs) (regs)
74#define control_clear(regs) ((regs) + 4)
75#define command_ptr(regs) ((regs) + 12)
76#define context_match(regs) ((regs) + 16)
77
Kristian Høgsberg32b46092007-02-06 14:49:30 -050078struct ar_buffer {
79 struct descriptor descriptor;
80 struct ar_buffer *next;
81 __le32 data[0];
82};
83
Kristian Høgsberged568912006-12-19 19:58:35 -050084struct ar_context {
85 struct fw_ohci *ohci;
Kristian Høgsberg32b46092007-02-06 14:49:30 -050086 struct ar_buffer *current_buffer;
87 struct ar_buffer *last_buffer;
88 void *pointer;
Kristian Høgsberg72e318e2007-02-06 14:49:31 -050089 u32 regs;
Kristian Høgsberged568912006-12-19 19:58:35 -050090 struct tasklet_struct tasklet;
91};
92
Kristian Høgsberg30200732007-02-16 17:34:39 -050093struct context;
94
95typedef int (*descriptor_callback_t)(struct context *ctx,
96 struct descriptor *d,
97 struct descriptor *last);
98struct context {
Stefan Richter373b2ed2007-03-04 14:45:18 +010099 struct fw_ohci *ohci;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500100 u32 regs;
Stefan Richter373b2ed2007-03-04 14:45:18 +0100101
Kristian Høgsberg30200732007-02-16 17:34:39 -0500102 struct descriptor *buffer;
103 dma_addr_t buffer_bus;
104 size_t buffer_size;
105 struct descriptor *head_descriptor;
106 struct descriptor *tail_descriptor;
107 struct descriptor *tail_descriptor_last;
108 struct descriptor *prev_descriptor;
109
110 descriptor_callback_t callback;
111
Stefan Richter373b2ed2007-03-04 14:45:18 +0100112 struct tasklet_struct tasklet;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500113};
Kristian Høgsberg30200732007-02-16 17:34:39 -0500114
Kristian Høgsberged568912006-12-19 19:58:35 -0500115#define it_header_sy(v) ((v) << 0)
116#define it_header_tcode(v) ((v) << 4)
117#define it_header_channel(v) ((v) << 8)
118#define it_header_tag(v) ((v) << 14)
119#define it_header_speed(v) ((v) << 16)
120#define it_header_data_length(v) ((v) << 16)
121
122struct iso_context {
123 struct fw_iso_context base;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500124 struct context context;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500125 void *header;
126 size_t header_length;
Kristian Høgsberged568912006-12-19 19:58:35 -0500127};
128
129#define CONFIG_ROM_SIZE 1024
130
131struct fw_ohci {
132 struct fw_card card;
133
Kristian Høgsberge364cf42007-02-16 17:34:49 -0500134 u32 version;
Kristian Høgsberged568912006-12-19 19:58:35 -0500135 __iomem char *registers;
136 dma_addr_t self_id_bus;
137 __le32 *self_id_cpu;
138 struct tasklet_struct bus_reset_tasklet;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500139 int node_id;
Kristian Høgsberged568912006-12-19 19:58:35 -0500140 int generation;
141 int request_generation;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500142 u32 bus_seconds;
Kristian Høgsberged568912006-12-19 19:58:35 -0500143
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400144 /*
145 * Spinlock for accessing fw_ohci data. Never call out of
146 * this driver with this lock held.
147 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500148 spinlock_t lock;
149 u32 self_id_buffer[512];
150
151 /* Config rom buffers */
152 __be32 *config_rom;
153 dma_addr_t config_rom_bus;
154 __be32 *next_config_rom;
155 dma_addr_t next_config_rom_bus;
156 u32 next_header;
157
158 struct ar_context ar_request_ctx;
159 struct ar_context ar_response_ctx;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500160 struct context at_request_ctx;
161 struct context at_response_ctx;
Kristian Høgsberged568912006-12-19 19:58:35 -0500162
163 u32 it_context_mask;
164 struct iso_context *it_context_list;
165 u32 ir_context_mask;
166 struct iso_context *ir_context_list;
167};
168
Adrian Bunk95688e92007-01-22 19:17:37 +0100169static inline struct fw_ohci *fw_ohci(struct fw_card *card)
Kristian Høgsberged568912006-12-19 19:58:35 -0500170{
171 return container_of(card, struct fw_ohci, card);
172}
173
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500174#define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
175#define IR_CONTEXT_BUFFER_FILL 0x80000000
176#define IR_CONTEXT_ISOCH_HEADER 0x40000000
177#define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
178#define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
179#define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
Kristian Høgsberged568912006-12-19 19:58:35 -0500180
181#define CONTEXT_RUN 0x8000
182#define CONTEXT_WAKE 0x1000
183#define CONTEXT_DEAD 0x0800
184#define CONTEXT_ACTIVE 0x0400
185
186#define OHCI1394_MAX_AT_REQ_RETRIES 0x2
187#define OHCI1394_MAX_AT_RESP_RETRIES 0x2
188#define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
189
190#define FW_OHCI_MAJOR 240
191#define OHCI1394_REGISTER_SIZE 0x800
192#define OHCI_LOOP_COUNT 500
193#define OHCI1394_PCI_HCI_Control 0x40
194#define SELF_ID_BUF_SIZE 0x800
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500195#define OHCI_TCODE_PHY_PACKET 0x0e
Kristian Høgsberge364cf42007-02-16 17:34:49 -0500196#define OHCI_VERSION_1_1 0x010010
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500197#define ISO_BUFFER_SIZE (64 * 1024)
198#define AT_BUFFER_SIZE 4096
Kristian Høgsberg0edeefd2007-01-26 00:38:49 -0500199
Kristian Høgsberged568912006-12-19 19:58:35 -0500200static char ohci_driver_name[] = KBUILD_MODNAME;
201
Adrian Bunk95688e92007-01-22 19:17:37 +0100202static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
Kristian Høgsberged568912006-12-19 19:58:35 -0500203{
204 writel(data, ohci->registers + offset);
205}
206
Adrian Bunk95688e92007-01-22 19:17:37 +0100207static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
Kristian Høgsberged568912006-12-19 19:58:35 -0500208{
209 return readl(ohci->registers + offset);
210}
211
Adrian Bunk95688e92007-01-22 19:17:37 +0100212static inline void flush_writes(const struct fw_ohci *ohci)
Kristian Høgsberged568912006-12-19 19:58:35 -0500213{
214 /* Do a dummy read to flush writes. */
215 reg_read(ohci, OHCI1394_Version);
216}
217
218static int
219ohci_update_phy_reg(struct fw_card *card, int addr,
220 int clear_bits, int set_bits)
221{
222 struct fw_ohci *ohci = fw_ohci(card);
223 u32 val, old;
224
225 reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
226 msleep(2);
227 val = reg_read(ohci, OHCI1394_PhyControl);
228 if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
229 fw_error("failed to set phy reg bits.\n");
230 return -EBUSY;
231 }
232
233 old = OHCI1394_PhyControl_ReadData(val);
234 old = (old & ~clear_bits) | set_bits;
235 reg_write(ohci, OHCI1394_PhyControl,
236 OHCI1394_PhyControl_Write(addr, old));
237
238 return 0;
239}
240
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500241static int ar_context_add_page(struct ar_context *ctx)
Kristian Høgsberged568912006-12-19 19:58:35 -0500242{
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500243 struct device *dev = ctx->ohci->card.device;
244 struct ar_buffer *ab;
245 dma_addr_t ab_bus;
246 size_t offset;
247
248 ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
249 if (ab == NULL)
250 return -ENOMEM;
251
252 ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
253 if (dma_mapping_error(ab_bus)) {
254 free_page((unsigned long) ab);
255 return -ENOMEM;
256 }
257
258 memset(&ab->descriptor, 0, sizeof ab->descriptor);
259 ab->descriptor.control = cpu_to_le16(descriptor_input_more |
260 descriptor_status |
261 descriptor_branch_always);
262 offset = offsetof(struct ar_buffer, data);
263 ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
264 ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
265 ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
266 ab->descriptor.branch_address = 0;
267
268 dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
269
270 ctx->last_buffer->descriptor.branch_address = ab_bus | 1;
271 ctx->last_buffer->next = ab;
272 ctx->last_buffer = ab;
273
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500274 reg_write(ctx->ohci, control_set(ctx->regs), CONTEXT_WAKE);
Kristian Høgsberged568912006-12-19 19:58:35 -0500275 flush_writes(ctx->ohci);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500276
277 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -0500278}
279
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500280static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
Kristian Høgsberged568912006-12-19 19:58:35 -0500281{
Kristian Høgsberged568912006-12-19 19:58:35 -0500282 struct fw_ohci *ohci = ctx->ohci;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500283 struct fw_packet p;
284 u32 status, length, tcode;
Kristian Høgsberg0edeefd2007-01-26 00:38:49 -0500285
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500286 p.header[0] = le32_to_cpu(buffer[0]);
287 p.header[1] = le32_to_cpu(buffer[1]);
288 p.header[2] = le32_to_cpu(buffer[2]);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500289
290 tcode = (p.header[0] >> 4) & 0x0f;
291 switch (tcode) {
292 case TCODE_WRITE_QUADLET_REQUEST:
293 case TCODE_READ_QUADLET_RESPONSE:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500294 p.header[3] = (__force __u32) buffer[3];
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500295 p.header_length = 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500296 p.payload_length = 0;
297 break;
298
299 case TCODE_READ_BLOCK_REQUEST :
300 p.header[3] = le32_to_cpu(buffer[3]);
301 p.header_length = 16;
302 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500303 break;
304
305 case TCODE_WRITE_BLOCK_REQUEST:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500306 case TCODE_READ_BLOCK_RESPONSE:
307 case TCODE_LOCK_REQUEST:
308 case TCODE_LOCK_RESPONSE:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500309 p.header[3] = le32_to_cpu(buffer[3]);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500310 p.header_length = 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500311 p.payload_length = p.header[3] >> 16;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500312 break;
313
314 case TCODE_WRITE_RESPONSE:
315 case TCODE_READ_QUADLET_REQUEST:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500316 case OHCI_TCODE_PHY_PACKET:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500317 p.header_length = 12;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500318 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500319 break;
320 }
321
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500322 p.payload = (void *) buffer + p.header_length;
323
324 /* FIXME: What to do about evt_* errors? */
325 length = (p.header_length + p.payload_length + 3) / 4;
326 status = le32_to_cpu(buffer[length]);
327
328 p.ack = ((status >> 16) & 0x1f) - 16;
329 p.speed = (status >> 21) & 0x7;
330 p.timestamp = status & 0xffff;
331 p.generation = ohci->request_generation;
Kristian Høgsberged568912006-12-19 19:58:35 -0500332
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400333 /*
334 * The OHCI bus reset handler synthesizes a phy packet with
Kristian Høgsberged568912006-12-19 19:58:35 -0500335 * the new generation number when a bus reset happens (see
336 * section 8.4.2.3). This helps us determine when a request
337 * was received and make sure we send the response in the same
338 * generation. We only need this for requests; for responses
339 * we use the unique tlabel for finding the matching
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400340 * request.
341 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500342
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500343 if (p.ack + 16 == 0x09)
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500344 ohci->request_generation = (buffer[2] >> 16) & 0xff;
Kristian Høgsberged568912006-12-19 19:58:35 -0500345 else if (ctx == &ohci->ar_request_ctx)
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500346 fw_core_handle_request(&ohci->card, &p);
Kristian Høgsberged568912006-12-19 19:58:35 -0500347 else
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500348 fw_core_handle_response(&ohci->card, &p);
Kristian Høgsberged568912006-12-19 19:58:35 -0500349
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500350 return buffer + length + 1;
351}
Kristian Høgsberged568912006-12-19 19:58:35 -0500352
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500353static void ar_context_tasklet(unsigned long data)
354{
355 struct ar_context *ctx = (struct ar_context *)data;
356 struct fw_ohci *ohci = ctx->ohci;
357 struct ar_buffer *ab;
358 struct descriptor *d;
359 void *buffer, *end;
Kristian Høgsberged568912006-12-19 19:58:35 -0500360
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500361 ab = ctx->current_buffer;
362 d = &ab->descriptor;
Kristian Høgsberged568912006-12-19 19:58:35 -0500363
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500364 if (d->res_count == 0) {
365 size_t size, rest, offset;
366
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400367 /*
368 * This descriptor is finished and we may have a
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500369 * packet split across this and the next buffer. We
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400370 * reuse the page for reassembling the split packet.
371 */
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500372
373 offset = offsetof(struct ar_buffer, data);
374 dma_unmap_single(ohci->card.device,
375 ab->descriptor.data_address - offset,
376 PAGE_SIZE, DMA_BIDIRECTIONAL);
377
378 buffer = ab;
379 ab = ab->next;
380 d = &ab->descriptor;
381 size = buffer + PAGE_SIZE - ctx->pointer;
382 rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
383 memmove(buffer, ctx->pointer, size);
384 memcpy(buffer + size, ab->data, rest);
385 ctx->current_buffer = ab;
386 ctx->pointer = (void *) ab->data + rest;
387 end = buffer + size + rest;
388
389 while (buffer < end)
390 buffer = handle_ar_packet(ctx, buffer);
391
392 free_page((unsigned long)buffer);
393 ar_context_add_page(ctx);
394 } else {
395 buffer = ctx->pointer;
396 ctx->pointer = end =
397 (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
398
399 while (buffer < end)
400 buffer = handle_ar_packet(ctx, buffer);
401 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500402}
403
404static int
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500405ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
Kristian Høgsberged568912006-12-19 19:58:35 -0500406{
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500407 struct ar_buffer ab;
Kristian Høgsberged568912006-12-19 19:58:35 -0500408
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500409 ctx->regs = regs;
410 ctx->ohci = ohci;
411 ctx->last_buffer = &ab;
Kristian Høgsberged568912006-12-19 19:58:35 -0500412 tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
413
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500414 ar_context_add_page(ctx);
415 ar_context_add_page(ctx);
416 ctx->current_buffer = ab.next;
417 ctx->pointer = ctx->current_buffer->data;
418
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500419 reg_write(ctx->ohci, command_ptr(ctx->regs), ab.descriptor.branch_address);
420 reg_write(ctx->ohci, control_set(ctx->regs), CONTEXT_RUN);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500421 flush_writes(ctx->ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -0500422
423 return 0;
424}
Stefan Richter373b2ed2007-03-04 14:45:18 +0100425
Kristian Høgsberg30200732007-02-16 17:34:39 -0500426static void context_tasklet(unsigned long data)
427{
428 struct context *ctx = (struct context *) data;
429 struct fw_ohci *ohci = ctx->ohci;
430 struct descriptor *d, *last;
431 u32 address;
432 int z;
433
434 dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
435 ctx->buffer_size, DMA_TO_DEVICE);
436
437 d = ctx->tail_descriptor;
438 last = ctx->tail_descriptor_last;
439
440 while (last->branch_address != 0) {
441 address = le32_to_cpu(last->branch_address);
442 z = address & 0xf;
443 d = ctx->buffer + (address - ctx->buffer_bus) / sizeof *d;
444 last = (z == 2) ? d : d + z - 1;
445
446 if (!ctx->callback(ctx, d, last))
447 break;
448
449 ctx->tail_descriptor = d;
450 ctx->tail_descriptor_last = last;
451 }
452}
453
454static int
455context_init(struct context *ctx, struct fw_ohci *ohci,
456 size_t buffer_size, u32 regs,
457 descriptor_callback_t callback)
458{
459 ctx->ohci = ohci;
460 ctx->regs = regs;
461 ctx->buffer_size = buffer_size;
462 ctx->buffer = kmalloc(buffer_size, GFP_KERNEL);
463 if (ctx->buffer == NULL)
464 return -ENOMEM;
465
466 tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
467 ctx->callback = callback;
468
469 ctx->buffer_bus =
470 dma_map_single(ohci->card.device, ctx->buffer,
471 buffer_size, DMA_TO_DEVICE);
472 if (dma_mapping_error(ctx->buffer_bus)) {
473 kfree(ctx->buffer);
474 return -ENOMEM;
475 }
476
477 ctx->head_descriptor = ctx->buffer;
478 ctx->prev_descriptor = ctx->buffer;
479 ctx->tail_descriptor = ctx->buffer;
480 ctx->tail_descriptor_last = ctx->buffer;
481
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400482 /*
483 * We put a dummy descriptor in the buffer that has a NULL
Kristian Høgsberg30200732007-02-16 17:34:39 -0500484 * branch address and looks like it's been sent. That way we
485 * have a descriptor to append DMA programs to. Also, the
486 * ring buffer invariant is that it always has at least one
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400487 * element so that head == tail means buffer full.
488 */
Kristian Høgsberg30200732007-02-16 17:34:39 -0500489
490 memset(ctx->head_descriptor, 0, sizeof *ctx->head_descriptor);
491 ctx->head_descriptor->control = cpu_to_le16(descriptor_output_last);
492 ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
493 ctx->head_descriptor++;
494
495 return 0;
496}
497
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500498static void
Kristian Høgsberg30200732007-02-16 17:34:39 -0500499context_release(struct context *ctx)
500{
501 struct fw_card *card = &ctx->ohci->card;
502
503 dma_unmap_single(card->device, ctx->buffer_bus,
504 ctx->buffer_size, DMA_TO_DEVICE);
505 kfree(ctx->buffer);
506}
507
508static struct descriptor *
509context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
510{
511 struct descriptor *d, *tail, *end;
512
513 d = ctx->head_descriptor;
514 tail = ctx->tail_descriptor;
515 end = ctx->buffer + ctx->buffer_size / sizeof(struct descriptor);
516
517 if (d + z <= tail) {
518 goto has_space;
519 } else if (d > tail && d + z <= end) {
520 goto has_space;
521 } else if (d > tail && ctx->buffer + z <= tail) {
522 d = ctx->buffer;
523 goto has_space;
524 }
525
526 return NULL;
527
528 has_space:
529 memset(d, 0, z * sizeof *d);
530 *d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof *d;
531
532 return d;
533}
534
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500535static void context_run(struct context *ctx, u32 extra)
Kristian Høgsberg30200732007-02-16 17:34:39 -0500536{
537 struct fw_ohci *ohci = ctx->ohci;
538
539 reg_write(ohci, command_ptr(ctx->regs),
540 le32_to_cpu(ctx->tail_descriptor_last->branch_address));
541 reg_write(ohci, control_clear(ctx->regs), ~0);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500542 reg_write(ohci, control_set(ctx->regs), CONTEXT_RUN | extra);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500543 flush_writes(ohci);
544}
545
546static void context_append(struct context *ctx,
547 struct descriptor *d, int z, int extra)
548{
549 dma_addr_t d_bus;
550
551 d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof *d;
552
553 ctx->head_descriptor = d + z + extra;
554 ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
555 ctx->prev_descriptor = z == 2 ? d : d + z - 1;
556
557 dma_sync_single_for_device(ctx->ohci->card.device, ctx->buffer_bus,
558 ctx->buffer_size, DMA_TO_DEVICE);
559
560 reg_write(ctx->ohci, control_set(ctx->regs), CONTEXT_WAKE);
561 flush_writes(ctx->ohci);
562}
563
564static void context_stop(struct context *ctx)
565{
566 u32 reg;
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500567 int i;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500568
569 reg_write(ctx->ohci, control_clear(ctx->regs), CONTEXT_RUN);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500570 flush_writes(ctx->ohci);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500571
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500572 for (i = 0; i < 10; i++) {
573 reg = reg_read(ctx->ohci, control_set(ctx->regs));
574 if ((reg & CONTEXT_ACTIVE) == 0)
575 break;
576
577 fw_notify("context_stop: still active (0x%08x)\n", reg);
578 msleep(1);
579 }
Kristian Høgsberg30200732007-02-16 17:34:39 -0500580}
Kristian Høgsberged568912006-12-19 19:58:35 -0500581
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500582struct driver_data {
Kristian Høgsberged568912006-12-19 19:58:35 -0500583 struct fw_packet *packet;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500584};
585
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400586/*
587 * This function apppends a packet to the DMA queue for transmission.
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500588 * Must always be called with the ochi->lock held to ensure proper
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400589 * generation handling and locking around packet queue manipulation.
590 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500591static int
592at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
593{
Kristian Høgsberged568912006-12-19 19:58:35 -0500594 struct fw_ohci *ohci = ctx->ohci;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500595 dma_addr_t d_bus, payload_bus;
596 struct driver_data *driver_data;
597 struct descriptor *d, *last;
598 __le32 *header;
Kristian Høgsberged568912006-12-19 19:58:35 -0500599 int z, tcode;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500600 u32 reg;
Kristian Høgsberged568912006-12-19 19:58:35 -0500601
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500602 d = context_get_descriptors(ctx, 4, &d_bus);
603 if (d == NULL) {
604 packet->ack = RCODE_SEND_ERROR;
605 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -0500606 }
607
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500608 d[0].control = cpu_to_le16(descriptor_key_immediate);
609 d[0].res_count = cpu_to_le16(packet->timestamp);
610
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400611 /*
612 * The DMA format for asyncronous link packets is different
Kristian Høgsberged568912006-12-19 19:58:35 -0500613 * from the IEEE1394 layout, so shift the fields around
614 * accordingly. If header_length is 8, it's a PHY packet, to
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400615 * which we need to prepend an extra quadlet.
616 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500617
618 header = (__le32 *) &d[1];
Kristian Høgsberged568912006-12-19 19:58:35 -0500619 if (packet->header_length > 8) {
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500620 header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
621 (packet->speed << 16));
622 header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
623 (packet->header[0] & 0xffff0000));
624 header[2] = cpu_to_le32(packet->header[2]);
Kristian Høgsberged568912006-12-19 19:58:35 -0500625
626 tcode = (packet->header[0] >> 4) & 0x0f;
627 if (TCODE_IS_BLOCK_PACKET(tcode))
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500628 header[3] = cpu_to_le32(packet->header[3]);
Kristian Høgsberged568912006-12-19 19:58:35 -0500629 else
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500630 header[3] = (__force __le32) packet->header[3];
631
632 d[0].req_count = cpu_to_le16(packet->header_length);
Kristian Høgsberged568912006-12-19 19:58:35 -0500633 } else {
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500634 header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
635 (packet->speed << 16));
636 header[1] = cpu_to_le32(packet->header[0]);
637 header[2] = cpu_to_le32(packet->header[1]);
638 d[0].req_count = cpu_to_le16(12);
Kristian Høgsberged568912006-12-19 19:58:35 -0500639 }
640
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500641 driver_data = (struct driver_data *) &d[3];
642 driver_data->packet = packet;
Kristian Høgsberg20d11672007-03-26 19:18:19 -0400643 packet->driver_data = driver_data;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500644
645 if (packet->payload_length > 0) {
646 payload_bus =
647 dma_map_single(ohci->card.device, packet->payload,
648 packet->payload_length, DMA_TO_DEVICE);
649 if (dma_mapping_error(payload_bus)) {
650 packet->ack = RCODE_SEND_ERROR;
651 return -1;
652 }
653
654 d[2].req_count = cpu_to_le16(packet->payload_length);
655 d[2].data_address = cpu_to_le32(payload_bus);
656 last = &d[2];
657 z = 3;
658 } else {
659 last = &d[0];
660 z = 2;
661 }
662
663 last->control |= cpu_to_le16(descriptor_output_last |
664 descriptor_irq_always |
665 descriptor_branch_always);
666
Kristian Høgsberged568912006-12-19 19:58:35 -0500667 /* FIXME: Document how the locking works. */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500668 if (ohci->generation != packet->generation) {
669 packet->ack = RCODE_GENERATION;
670 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -0500671 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500672
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500673 context_append(ctx, d, z, 4 - z);
Kristian Høgsberged568912006-12-19 19:58:35 -0500674
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500675 /* If the context isn't already running, start it up. */
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500676 reg = reg_read(ctx->ohci, control_set(ctx->regs));
Kristian Høgsberg053b3082007-04-10 18:11:17 -0400677 if ((reg & CONTEXT_RUN) == 0)
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500678 context_run(ctx, 0);
Kristian Høgsberged568912006-12-19 19:58:35 -0500679
680 return 0;
681}
682
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500683static int handle_at_packet(struct context *context,
684 struct descriptor *d,
685 struct descriptor *last)
686{
687 struct driver_data *driver_data;
688 struct fw_packet *packet;
689 struct fw_ohci *ohci = context->ohci;
690 dma_addr_t payload_bus;
691 int evt;
692
693 if (last->transfer_status == 0)
694 /* This descriptor isn't done yet, stop iteration. */
695 return 0;
696
697 driver_data = (struct driver_data *) &d[3];
698 packet = driver_data->packet;
699 if (packet == NULL)
700 /* This packet was cancelled, just continue. */
701 return 1;
702
703 payload_bus = le32_to_cpu(last->data_address);
704 if (payload_bus != 0)
705 dma_unmap_single(ohci->card.device, payload_bus,
706 packet->payload_length, DMA_TO_DEVICE);
707
708 evt = le16_to_cpu(last->transfer_status) & 0x1f;
709 packet->timestamp = le16_to_cpu(last->res_count);
710
711 switch (evt) {
712 case OHCI1394_evt_timeout:
713 /* Async response transmit timed out. */
714 packet->ack = RCODE_CANCELLED;
715 break;
716
717 case OHCI1394_evt_flushed:
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400718 /*
719 * The packet was flushed should give same error as
720 * when we try to use a stale generation count.
721 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500722 packet->ack = RCODE_GENERATION;
723 break;
724
725 case OHCI1394_evt_missing_ack:
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400726 /*
727 * Using a valid (current) generation count, but the
728 * node is not on the bus or not sending acks.
729 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500730 packet->ack = RCODE_NO_ACK;
731 break;
732
733 case ACK_COMPLETE + 0x10:
734 case ACK_PENDING + 0x10:
735 case ACK_BUSY_X + 0x10:
736 case ACK_BUSY_A + 0x10:
737 case ACK_BUSY_B + 0x10:
738 case ACK_DATA_ERROR + 0x10:
739 case ACK_TYPE_ERROR + 0x10:
740 packet->ack = evt - 0x10;
741 break;
742
743 default:
744 packet->ack = RCODE_SEND_ERROR;
745 break;
746 }
747
748 packet->callback(packet, &ohci->card, packet->ack);
749
750 return 1;
751}
752
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500753#define header_get_destination(q) (((q) >> 16) & 0xffff)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500754#define header_get_tcode(q) (((q) >> 4) & 0x0f)
755#define header_get_offset_high(q) (((q) >> 0) & 0xffff)
756#define header_get_data_length(q) (((q) >> 16) & 0xffff)
757#define header_get_extended_tcode(q) (((q) >> 0) & 0xffff)
758
759static void
760handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
761{
762 struct fw_packet response;
763 int tcode, length, i;
764
765 tcode = header_get_tcode(packet->header[0]);
766 if (TCODE_IS_BLOCK_PACKET(tcode))
767 length = header_get_data_length(packet->header[3]);
768 else
769 length = 4;
770
771 i = csr - CSR_CONFIG_ROM;
772 if (i + length > CONFIG_ROM_SIZE) {
773 fw_fill_response(&response, packet->header,
774 RCODE_ADDRESS_ERROR, NULL, 0);
775 } else if (!TCODE_IS_READ_REQUEST(tcode)) {
776 fw_fill_response(&response, packet->header,
777 RCODE_TYPE_ERROR, NULL, 0);
778 } else {
779 fw_fill_response(&response, packet->header, RCODE_COMPLETE,
780 (void *) ohci->config_rom + i, length);
781 }
782
783 fw_core_handle_response(&ohci->card, &response);
784}
785
786static void
787handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
788{
789 struct fw_packet response;
790 int tcode, length, ext_tcode, sel;
791 __be32 *payload, lock_old;
792 u32 lock_arg, lock_data;
793
794 tcode = header_get_tcode(packet->header[0]);
795 length = header_get_data_length(packet->header[3]);
796 payload = packet->payload;
797 ext_tcode = header_get_extended_tcode(packet->header[3]);
798
799 if (tcode == TCODE_LOCK_REQUEST &&
800 ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
801 lock_arg = be32_to_cpu(payload[0]);
802 lock_data = be32_to_cpu(payload[1]);
803 } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
804 lock_arg = 0;
805 lock_data = 0;
806 } else {
807 fw_fill_response(&response, packet->header,
808 RCODE_TYPE_ERROR, NULL, 0);
809 goto out;
810 }
811
812 sel = (csr - CSR_BUS_MANAGER_ID) / 4;
813 reg_write(ohci, OHCI1394_CSRData, lock_data);
814 reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
815 reg_write(ohci, OHCI1394_CSRControl, sel);
816
817 if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
818 lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
819 else
820 fw_notify("swap not done yet\n");
821
822 fw_fill_response(&response, packet->header,
823 RCODE_COMPLETE, &lock_old, sizeof lock_old);
824 out:
825 fw_core_handle_response(&ohci->card, &response);
826}
827
828static void
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500829handle_local_request(struct context *ctx, struct fw_packet *packet)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500830{
831 u64 offset;
832 u32 csr;
833
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500834 if (ctx == &ctx->ohci->at_request_ctx) {
835 packet->ack = ACK_PENDING;
836 packet->callback(packet, &ctx->ohci->card, packet->ack);
837 }
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500838
839 offset =
840 ((unsigned long long)
841 header_get_offset_high(packet->header[1]) << 32) |
842 packet->header[2];
843 csr = offset - CSR_REGISTER_BASE;
844
845 /* Handle config rom reads. */
846 if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
847 handle_local_rom(ctx->ohci, packet, csr);
848 else switch (csr) {
849 case CSR_BUS_MANAGER_ID:
850 case CSR_BANDWIDTH_AVAILABLE:
851 case CSR_CHANNELS_AVAILABLE_HI:
852 case CSR_CHANNELS_AVAILABLE_LO:
853 handle_local_lock(ctx->ohci, packet, csr);
854 break;
855 default:
856 if (ctx == &ctx->ohci->at_request_ctx)
857 fw_core_handle_request(&ctx->ohci->card, packet);
858 else
859 fw_core_handle_response(&ctx->ohci->card, packet);
860 break;
861 }
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500862
863 if (ctx == &ctx->ohci->at_response_ctx) {
864 packet->ack = ACK_COMPLETE;
865 packet->callback(packet, &ctx->ohci->card, packet->ack);
866 }
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500867}
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500868
Kristian Høgsberged568912006-12-19 19:58:35 -0500869static void
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500870at_context_transmit(struct context *ctx, struct fw_packet *packet)
Kristian Høgsberged568912006-12-19 19:58:35 -0500871{
Kristian Høgsberged568912006-12-19 19:58:35 -0500872 unsigned long flags;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500873 int retval;
Kristian Høgsberged568912006-12-19 19:58:35 -0500874
875 spin_lock_irqsave(&ctx->ohci->lock, flags);
876
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500877 if (header_get_destination(packet->header[0]) == ctx->ohci->node_id &&
878 ctx->ohci->generation == packet->generation) {
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500879 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
880 handle_local_request(ctx, packet);
881 return;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500882 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500883
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500884 retval = at_context_queue_packet(ctx, packet);
Kristian Høgsberged568912006-12-19 19:58:35 -0500885 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
886
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500887 if (retval < 0)
888 packet->callback(packet, &ctx->ohci->card, packet->ack);
889
Kristian Høgsberged568912006-12-19 19:58:35 -0500890}
891
892static void bus_reset_tasklet(unsigned long data)
893{
894 struct fw_ohci *ohci = (struct fw_ohci *)data;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500895 int self_id_count, i, j, reg;
Kristian Høgsberged568912006-12-19 19:58:35 -0500896 int generation, new_generation;
897 unsigned long flags;
898
899 reg = reg_read(ohci, OHCI1394_NodeID);
900 if (!(reg & OHCI1394_NodeID_idValid)) {
901 fw_error("node ID not valid, new bus reset in progress\n");
902 return;
903 }
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500904 ohci->node_id = reg & 0xffff;
Kristian Høgsberged568912006-12-19 19:58:35 -0500905
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400906 /*
907 * The count in the SelfIDCount register is the number of
Kristian Høgsberged568912006-12-19 19:58:35 -0500908 * bytes in the self ID receive buffer. Since we also receive
909 * the inverted quadlets and a header quadlet, we shift one
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400910 * bit extra to get the actual number of self IDs.
911 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500912
913 self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
914 generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
915
916 for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
917 if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
918 fw_error("inconsistent self IDs\n");
919 ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
920 }
921
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400922 /*
923 * Check the consistency of the self IDs we just read. The
Kristian Høgsberged568912006-12-19 19:58:35 -0500924 * problem we face is that a new bus reset can start while we
925 * read out the self IDs from the DMA buffer. If this happens,
926 * the DMA buffer will be overwritten with new self IDs and we
927 * will read out inconsistent data. The OHCI specification
928 * (section 11.2) recommends a technique similar to
929 * linux/seqlock.h, where we remember the generation of the
930 * self IDs in the buffer before reading them out and compare
931 * it to the current generation after reading them out. If
932 * the two generations match we know we have a consistent set
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400933 * of self IDs.
934 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500935
936 new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
937 if (new_generation != generation) {
938 fw_notify("recursive bus reset detected, "
939 "discarding self ids\n");
940 return;
941 }
942
943 /* FIXME: Document how the locking works. */
944 spin_lock_irqsave(&ohci->lock, flags);
945
946 ohci->generation = generation;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500947 context_stop(&ohci->at_request_ctx);
948 context_stop(&ohci->at_response_ctx);
Kristian Høgsberged568912006-12-19 19:58:35 -0500949 reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
950
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400951 /*
952 * This next bit is unrelated to the AT context stuff but we
Kristian Høgsberged568912006-12-19 19:58:35 -0500953 * have to do it under the spinlock also. If a new config rom
954 * was set up before this reset, the old one is now no longer
955 * in use and we can free it. Update the config rom pointers
956 * to point to the current config rom and clear the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400957 * next_config_rom pointer so a new udpate can take place.
958 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500959
960 if (ohci->next_config_rom != NULL) {
961 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
962 ohci->config_rom, ohci->config_rom_bus);
963 ohci->config_rom = ohci->next_config_rom;
964 ohci->config_rom_bus = ohci->next_config_rom_bus;
965 ohci->next_config_rom = NULL;
966
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400967 /*
968 * Restore config_rom image and manually update
Kristian Høgsberged568912006-12-19 19:58:35 -0500969 * config_rom registers. Writing the header quadlet
970 * will indicate that the config rom is ready, so we
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400971 * do that last.
972 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500973 reg_write(ohci, OHCI1394_BusOptions,
974 be32_to_cpu(ohci->config_rom[2]));
975 ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
976 reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
977 }
978
979 spin_unlock_irqrestore(&ohci->lock, flags);
980
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500981 fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
Kristian Høgsberged568912006-12-19 19:58:35 -0500982 self_id_count, ohci->self_id_buffer);
983}
984
985static irqreturn_t irq_handler(int irq, void *data)
986{
987 struct fw_ohci *ohci = data;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500988 u32 event, iso_event, cycle_time;
Kristian Høgsberged568912006-12-19 19:58:35 -0500989 int i;
990
991 event = reg_read(ohci, OHCI1394_IntEventClear);
992
993 if (!event)
994 return IRQ_NONE;
995
996 reg_write(ohci, OHCI1394_IntEventClear, event);
997
998 if (event & OHCI1394_selfIDComplete)
999 tasklet_schedule(&ohci->bus_reset_tasklet);
1000
1001 if (event & OHCI1394_RQPkt)
1002 tasklet_schedule(&ohci->ar_request_ctx.tasklet);
1003
1004 if (event & OHCI1394_RSPkt)
1005 tasklet_schedule(&ohci->ar_response_ctx.tasklet);
1006
1007 if (event & OHCI1394_reqTxComplete)
1008 tasklet_schedule(&ohci->at_request_ctx.tasklet);
1009
1010 if (event & OHCI1394_respTxComplete)
1011 tasklet_schedule(&ohci->at_response_ctx.tasklet);
1012
Kristian Høgsbergc8894752007-02-16 17:34:36 -05001013 iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
Kristian Høgsberged568912006-12-19 19:58:35 -05001014 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
1015
1016 while (iso_event) {
1017 i = ffs(iso_event) - 1;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001018 tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001019 iso_event &= ~(1 << i);
1020 }
1021
Kristian Høgsbergc8894752007-02-16 17:34:36 -05001022 iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
Kristian Høgsberged568912006-12-19 19:58:35 -05001023 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
1024
1025 while (iso_event) {
1026 i = ffs(iso_event) - 1;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001027 tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001028 iso_event &= ~(1 << i);
1029 }
1030
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001031 if (event & OHCI1394_cycle64Seconds) {
1032 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1033 if ((cycle_time & 0x80000000) == 0)
1034 ohci->bus_seconds++;
1035 }
1036
Kristian Høgsberged568912006-12-19 19:58:35 -05001037 return IRQ_HANDLED;
1038}
1039
1040static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
1041{
1042 struct fw_ohci *ohci = fw_ohci(card);
1043 struct pci_dev *dev = to_pci_dev(card->device);
1044
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001045 /*
1046 * When the link is not yet enabled, the atomic config rom
Kristian Høgsberged568912006-12-19 19:58:35 -05001047 * update mechanism described below in ohci_set_config_rom()
1048 * is not active. We have to update ConfigRomHeader and
1049 * BusOptions manually, and the write to ConfigROMmap takes
1050 * effect immediately. We tie this to the enabling of the
1051 * link, so we have a valid config rom before enabling - the
1052 * OHCI requires that ConfigROMhdr and BusOptions have valid
1053 * values before enabling.
1054 *
1055 * However, when the ConfigROMmap is written, some controllers
1056 * always read back quadlets 0 and 2 from the config rom to
1057 * the ConfigRomHeader and BusOptions registers on bus reset.
1058 * They shouldn't do that in this initial case where the link
1059 * isn't enabled. This means we have to use the same
1060 * workaround here, setting the bus header to 0 and then write
1061 * the right values in the bus reset tasklet.
1062 */
1063
1064 ohci->next_config_rom =
1065 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1066 &ohci->next_config_rom_bus, GFP_KERNEL);
1067 if (ohci->next_config_rom == NULL)
1068 return -ENOMEM;
1069
1070 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1071 fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
1072
1073 ohci->next_header = config_rom[0];
1074 ohci->next_config_rom[0] = 0;
1075 reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
1076 reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
1077 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
1078
1079 reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
1080
1081 if (request_irq(dev->irq, irq_handler,
Thomas Gleixner65efffa2007-03-05 18:19:51 -08001082 IRQF_SHARED, ohci_driver_name, ohci)) {
Kristian Høgsberged568912006-12-19 19:58:35 -05001083 fw_error("Failed to allocate shared interrupt %d.\n",
1084 dev->irq);
1085 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1086 ohci->config_rom, ohci->config_rom_bus);
1087 return -EIO;
1088 }
1089
1090 reg_write(ohci, OHCI1394_HCControlSet,
1091 OHCI1394_HCControl_linkEnable |
1092 OHCI1394_HCControl_BIBimageValid);
1093 flush_writes(ohci);
1094
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001095 /*
1096 * We are ready to go, initiate bus reset to finish the
1097 * initialization.
1098 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001099
1100 fw_core_initiate_bus_reset(&ohci->card, 1);
1101
1102 return 0;
1103}
1104
1105static int
1106ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
1107{
1108 struct fw_ohci *ohci;
1109 unsigned long flags;
1110 int retval = 0;
1111 __be32 *next_config_rom;
1112 dma_addr_t next_config_rom_bus;
1113
1114 ohci = fw_ohci(card);
1115
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001116 /*
1117 * When the OHCI controller is enabled, the config rom update
Kristian Høgsberged568912006-12-19 19:58:35 -05001118 * mechanism is a bit tricky, but easy enough to use. See
1119 * section 5.5.6 in the OHCI specification.
1120 *
1121 * The OHCI controller caches the new config rom address in a
1122 * shadow register (ConfigROMmapNext) and needs a bus reset
1123 * for the changes to take place. When the bus reset is
1124 * detected, the controller loads the new values for the
1125 * ConfigRomHeader and BusOptions registers from the specified
1126 * config rom and loads ConfigROMmap from the ConfigROMmapNext
1127 * shadow register. All automatically and atomically.
1128 *
1129 * Now, there's a twist to this story. The automatic load of
1130 * ConfigRomHeader and BusOptions doesn't honor the
1131 * noByteSwapData bit, so with a be32 config rom, the
1132 * controller will load be32 values in to these registers
1133 * during the atomic update, even on litte endian
1134 * architectures. The workaround we use is to put a 0 in the
1135 * header quadlet; 0 is endian agnostic and means that the
1136 * config rom isn't ready yet. In the bus reset tasklet we
1137 * then set up the real values for the two registers.
1138 *
1139 * We use ohci->lock to avoid racing with the code that sets
1140 * ohci->next_config_rom to NULL (see bus_reset_tasklet).
1141 */
1142
1143 next_config_rom =
1144 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1145 &next_config_rom_bus, GFP_KERNEL);
1146 if (next_config_rom == NULL)
1147 return -ENOMEM;
1148
1149 spin_lock_irqsave(&ohci->lock, flags);
1150
1151 if (ohci->next_config_rom == NULL) {
1152 ohci->next_config_rom = next_config_rom;
1153 ohci->next_config_rom_bus = next_config_rom_bus;
1154
1155 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1156 fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
1157 length * 4);
1158
1159 ohci->next_header = config_rom[0];
1160 ohci->next_config_rom[0] = 0;
1161
1162 reg_write(ohci, OHCI1394_ConfigROMmap,
1163 ohci->next_config_rom_bus);
1164 } else {
1165 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1166 next_config_rom, next_config_rom_bus);
1167 retval = -EBUSY;
1168 }
1169
1170 spin_unlock_irqrestore(&ohci->lock, flags);
1171
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001172 /*
1173 * Now initiate a bus reset to have the changes take
Kristian Høgsberged568912006-12-19 19:58:35 -05001174 * effect. We clean up the old config rom memory and DMA
1175 * mappings in the bus reset tasklet, since the OHCI
1176 * controller could need to access it before the bus reset
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001177 * takes effect.
1178 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001179 if (retval == 0)
1180 fw_core_initiate_bus_reset(&ohci->card, 1);
1181
1182 return retval;
1183}
1184
1185static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
1186{
1187 struct fw_ohci *ohci = fw_ohci(card);
1188
1189 at_context_transmit(&ohci->at_request_ctx, packet);
1190}
1191
1192static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
1193{
1194 struct fw_ohci *ohci = fw_ohci(card);
1195
1196 at_context_transmit(&ohci->at_response_ctx, packet);
1197}
1198
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001199static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
1200{
1201 struct fw_ohci *ohci = fw_ohci(card);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001202 struct context *ctx = &ohci->at_request_ctx;
1203 struct driver_data *driver_data = packet->driver_data;
1204 int retval = -ENOENT;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001205
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001206 tasklet_disable(&ctx->tasklet);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001207
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001208 if (packet->ack != 0)
1209 goto out;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001210
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001211 driver_data->packet = NULL;
1212 packet->ack = RCODE_CANCELLED;
1213 packet->callback(packet, &ohci->card, packet->ack);
1214 retval = 0;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001215
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001216 out:
1217 tasklet_enable(&ctx->tasklet);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001218
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001219 return retval;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001220}
1221
Kristian Høgsberged568912006-12-19 19:58:35 -05001222static int
1223ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
1224{
1225 struct fw_ohci *ohci = fw_ohci(card);
1226 unsigned long flags;
Stefan Richter907293d2007-01-23 21:11:43 +01001227 int n, retval = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001228
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001229 /*
1230 * FIXME: Make sure this bitmask is cleared when we clear the busReset
1231 * interrupt bit. Clear physReqResourceAllBuses on bus reset.
1232 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001233
1234 spin_lock_irqsave(&ohci->lock, flags);
1235
1236 if (ohci->generation != generation) {
1237 retval = -ESTALE;
1238 goto out;
1239 }
1240
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001241 /*
1242 * Note, if the node ID contains a non-local bus ID, physical DMA is
1243 * enabled for _all_ nodes on remote buses.
1244 */
Stefan Richter907293d2007-01-23 21:11:43 +01001245
1246 n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
1247 if (n < 32)
1248 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
1249 else
1250 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
1251
Kristian Høgsberged568912006-12-19 19:58:35 -05001252 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -05001253 out:
Stefan Richter6cad95f2007-01-21 20:46:45 +01001254 spin_unlock_irqrestore(&ohci->lock, flags);
Kristian Høgsberged568912006-12-19 19:58:35 -05001255 return retval;
1256}
Stefan Richter373b2ed2007-03-04 14:45:18 +01001257
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001258static u64
1259ohci_get_bus_time(struct fw_card *card)
1260{
1261 struct fw_ohci *ohci = fw_ohci(card);
1262 u32 cycle_time;
1263 u64 bus_time;
1264
1265 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1266 bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
1267
1268 return bus_time;
1269}
1270
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001271static int handle_ir_dualbuffer_packet(struct context *context,
1272 struct descriptor *d,
1273 struct descriptor *last)
Kristian Høgsberged568912006-12-19 19:58:35 -05001274{
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001275 struct iso_context *ctx =
1276 container_of(context, struct iso_context, context);
1277 struct db_descriptor *db = (struct db_descriptor *) d;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001278 __le32 *ir_header;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001279 size_t header_length;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001280 void *p, *end;
1281 int i;
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001282
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001283 if (db->first_res_count > 0 && db->second_res_count > 0)
1284 /* This descriptor isn't done yet, stop iteration. */
1285 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001286
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001287 header_length = le16_to_cpu(db->first_req_count) -
1288 le16_to_cpu(db->first_res_count);
1289
1290 i = ctx->header_length;
1291 p = db + 1;
1292 end = p + header_length;
1293 while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001294 /*
1295 * The iso header is byteswapped to little endian by
Kristian Høgsberg15536222007-04-10 18:11:16 -04001296 * the controller, but the remaining header quadlets
1297 * are big endian. We want to present all the headers
1298 * as big endian, so we have to swap the first
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001299 * quadlet.
1300 */
Kristian Høgsberg15536222007-04-10 18:11:16 -04001301 *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
1302 memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001303 i += ctx->base.header_size;
1304 p += ctx->base.header_size + 4;
1305 }
1306
1307 ctx->header_length = i;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001308
1309 if (le16_to_cpu(db->control) & descriptor_irq_always) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001310 ir_header = (__le32 *) (db + 1);
1311 ctx->base.callback(&ctx->base,
1312 le32_to_cpu(ir_header[0]) & 0xffff,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001313 ctx->header_length, ctx->header,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001314 ctx->base.callback_data);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001315 ctx->header_length = 0;
1316 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001317
1318 return 1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001319}
1320
Kristian Høgsberg30200732007-02-16 17:34:39 -05001321static int handle_it_packet(struct context *context,
1322 struct descriptor *d,
1323 struct descriptor *last)
Kristian Høgsberged568912006-12-19 19:58:35 -05001324{
Kristian Høgsberg30200732007-02-16 17:34:39 -05001325 struct iso_context *ctx =
1326 container_of(context, struct iso_context, context);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001327
Kristian Høgsberg30200732007-02-16 17:34:39 -05001328 if (last->transfer_status == 0)
1329 /* This descriptor isn't done yet, stop iteration. */
1330 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001331
Kristian Høgsberg30200732007-02-16 17:34:39 -05001332 if (le16_to_cpu(last->control) & descriptor_irq_always)
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001333 ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
1334 0, NULL, ctx->base.callback_data);
Kristian Høgsberged568912006-12-19 19:58:35 -05001335
Kristian Høgsberg30200732007-02-16 17:34:39 -05001336 return 1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001337}
1338
Kristian Høgsberg30200732007-02-16 17:34:39 -05001339static struct fw_iso_context *
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001340ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
Kristian Høgsberged568912006-12-19 19:58:35 -05001341{
1342 struct fw_ohci *ohci = fw_ohci(card);
1343 struct iso_context *ctx, *list;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001344 descriptor_callback_t callback;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001345 u32 *mask, regs;
Kristian Høgsberged568912006-12-19 19:58:35 -05001346 unsigned long flags;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001347 int index, retval = -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001348
1349 if (type == FW_ISO_CONTEXT_TRANSMIT) {
1350 mask = &ohci->it_context_mask;
1351 list = ohci->it_context_list;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001352 callback = handle_it_packet;
Kristian Høgsberged568912006-12-19 19:58:35 -05001353 } else {
Stefan Richter373b2ed2007-03-04 14:45:18 +01001354 mask = &ohci->ir_context_mask;
1355 list = ohci->ir_context_list;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001356 callback = handle_ir_dualbuffer_packet;
Kristian Høgsberged568912006-12-19 19:58:35 -05001357 }
1358
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001359 /* FIXME: We need a fallback for pre 1.1 OHCI. */
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001360 if (callback == handle_ir_dualbuffer_packet &&
1361 ohci->version < OHCI_VERSION_1_1)
1362 return ERR_PTR(-EINVAL);
1363
Kristian Høgsberged568912006-12-19 19:58:35 -05001364 spin_lock_irqsave(&ohci->lock, flags);
1365 index = ffs(*mask) - 1;
1366 if (index >= 0)
1367 *mask &= ~(1 << index);
1368 spin_unlock_irqrestore(&ohci->lock, flags);
1369
1370 if (index < 0)
1371 return ERR_PTR(-EBUSY);
1372
Stefan Richter373b2ed2007-03-04 14:45:18 +01001373 if (type == FW_ISO_CONTEXT_TRANSMIT)
1374 regs = OHCI1394_IsoXmitContextBase(index);
1375 else
1376 regs = OHCI1394_IsoRcvContextBase(index);
1377
Kristian Høgsberged568912006-12-19 19:58:35 -05001378 ctx = &list[index];
1379 memset(ctx, 0, sizeof *ctx);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001380 ctx->header_length = 0;
1381 ctx->header = (void *) __get_free_page(GFP_KERNEL);
1382 if (ctx->header == NULL)
1383 goto out;
1384
Kristian Høgsberg30200732007-02-16 17:34:39 -05001385 retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001386 regs, callback);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001387 if (retval < 0)
1388 goto out_with_header;
Kristian Høgsberged568912006-12-19 19:58:35 -05001389
1390 return &ctx->base;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001391
1392 out_with_header:
1393 free_page((unsigned long)ctx->header);
1394 out:
1395 spin_lock_irqsave(&ohci->lock, flags);
1396 *mask |= 1 << index;
1397 spin_unlock_irqrestore(&ohci->lock, flags);
1398
1399 return ERR_PTR(retval);
Kristian Høgsberged568912006-12-19 19:58:35 -05001400}
1401
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001402static int ohci_start_iso(struct fw_iso_context *base,
1403 s32 cycle, u32 sync, u32 tags)
Kristian Høgsberged568912006-12-19 19:58:35 -05001404{
Stefan Richter373b2ed2007-03-04 14:45:18 +01001405 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberg30200732007-02-16 17:34:39 -05001406 struct fw_ohci *ohci = ctx->context.ohci;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001407 u32 control, match;
Kristian Høgsberged568912006-12-19 19:58:35 -05001408 int index;
1409
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001410 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1411 index = ctx - ohci->it_context_list;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001412 match = 0;
1413 if (cycle >= 0)
1414 match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001415 (cycle & 0x7fff) << 16;
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05001416
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001417 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
1418 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001419 context_run(&ctx->context, match);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001420 } else {
1421 index = ctx - ohci->ir_context_list;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001422 control = IR_CONTEXT_DUAL_BUFFER_MODE | IR_CONTEXT_ISOCH_HEADER;
1423 match = (tags << 28) | (sync << 8) | ctx->base.channel;
1424 if (cycle >= 0) {
1425 match |= (cycle & 0x07fff) << 12;
1426 control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
1427 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001428
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001429 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
1430 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001431 reg_write(ohci, context_match(ctx->context.regs), match);
1432 context_run(&ctx->context, control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001433 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001434
1435 return 0;
1436}
1437
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001438static int ohci_stop_iso(struct fw_iso_context *base)
1439{
1440 struct fw_ohci *ohci = fw_ohci(base->card);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001441 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001442 int index;
1443
1444 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1445 index = ctx - ohci->it_context_list;
1446 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
1447 } else {
1448 index = ctx - ohci->ir_context_list;
1449 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
1450 }
1451 flush_writes(ohci);
1452 context_stop(&ctx->context);
1453
1454 return 0;
1455}
1456
Kristian Høgsberged568912006-12-19 19:58:35 -05001457static void ohci_free_iso_context(struct fw_iso_context *base)
1458{
1459 struct fw_ohci *ohci = fw_ohci(base->card);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001460 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberged568912006-12-19 19:58:35 -05001461 unsigned long flags;
1462 int index;
1463
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001464 ohci_stop_iso(base);
1465 context_release(&ctx->context);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001466 free_page((unsigned long)ctx->header);
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001467
Kristian Høgsberged568912006-12-19 19:58:35 -05001468 spin_lock_irqsave(&ohci->lock, flags);
1469
1470 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1471 index = ctx - ohci->it_context_list;
Kristian Høgsberged568912006-12-19 19:58:35 -05001472 ohci->it_context_mask |= 1 << index;
1473 } else {
1474 index = ctx - ohci->ir_context_list;
Kristian Høgsberged568912006-12-19 19:58:35 -05001475 ohci->ir_context_mask |= 1 << index;
1476 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001477
1478 spin_unlock_irqrestore(&ohci->lock, flags);
1479}
1480
1481static int
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001482ohci_queue_iso_transmit(struct fw_iso_context *base,
1483 struct fw_iso_packet *packet,
1484 struct fw_iso_buffer *buffer,
1485 unsigned long payload)
Kristian Høgsberged568912006-12-19 19:58:35 -05001486{
Stefan Richter373b2ed2007-03-04 14:45:18 +01001487 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberg30200732007-02-16 17:34:39 -05001488 struct descriptor *d, *last, *pd;
Kristian Høgsberged568912006-12-19 19:58:35 -05001489 struct fw_iso_packet *p;
1490 __le32 *header;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001491 dma_addr_t d_bus, page_bus;
Kristian Høgsberged568912006-12-19 19:58:35 -05001492 u32 z, header_z, payload_z, irq;
1493 u32 payload_index, payload_end_index, next_page_index;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001494 int page, end_page, i, length, offset;
Kristian Høgsberged568912006-12-19 19:58:35 -05001495
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001496 /*
1497 * FIXME: Cycle lost behavior should be configurable: lose
1498 * packet, retransmit or terminate..
1499 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001500
1501 p = packet;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001502 payload_index = payload;
Kristian Høgsberged568912006-12-19 19:58:35 -05001503
1504 if (p->skip)
1505 z = 1;
1506 else
1507 z = 2;
1508 if (p->header_length > 0)
1509 z++;
1510
1511 /* Determine the first page the payload isn't contained in. */
1512 end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
1513 if (p->payload_length > 0)
1514 payload_z = end_page - (payload_index >> PAGE_SHIFT);
1515 else
1516 payload_z = 0;
1517
1518 z += payload_z;
1519
1520 /* Get header size in number of descriptors. */
1521 header_z = DIV_ROUND_UP(p->header_length, sizeof *d);
1522
Kristian Høgsberg30200732007-02-16 17:34:39 -05001523 d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
1524 if (d == NULL)
1525 return -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001526
1527 if (!p->skip) {
1528 d[0].control = cpu_to_le16(descriptor_key_immediate);
1529 d[0].req_count = cpu_to_le16(8);
1530
1531 header = (__le32 *) &d[1];
1532 header[0] = cpu_to_le32(it_header_sy(p->sy) |
1533 it_header_tag(p->tag) |
1534 it_header_tcode(TCODE_STREAM_DATA) |
1535 it_header_channel(ctx->base.channel) |
1536 it_header_speed(ctx->base.speed));
1537 header[1] =
1538 cpu_to_le32(it_header_data_length(p->header_length +
1539 p->payload_length));
1540 }
1541
1542 if (p->header_length > 0) {
1543 d[2].req_count = cpu_to_le16(p->header_length);
1544 d[2].data_address = cpu_to_le32(d_bus + z * sizeof *d);
1545 memcpy(&d[z], p->header, p->header_length);
1546 }
1547
1548 pd = d + z - payload_z;
1549 payload_end_index = payload_index + p->payload_length;
1550 for (i = 0; i < payload_z; i++) {
1551 page = payload_index >> PAGE_SHIFT;
1552 offset = payload_index & ~PAGE_MASK;
1553 next_page_index = (page + 1) << PAGE_SHIFT;
1554 length =
1555 min(next_page_index, payload_end_index) - payload_index;
1556 pd[i].req_count = cpu_to_le16(length);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001557
1558 page_bus = page_private(buffer->pages[page]);
1559 pd[i].data_address = cpu_to_le32(page_bus + offset);
Kristian Høgsberged568912006-12-19 19:58:35 -05001560
1561 payload_index += length;
1562 }
1563
Kristian Høgsberged568912006-12-19 19:58:35 -05001564 if (p->interrupt)
1565 irq = descriptor_irq_always;
1566 else
1567 irq = descriptor_no_irq;
1568
Kristian Høgsberg30200732007-02-16 17:34:39 -05001569 last = z == 2 ? d : d + z - 1;
Kristian Høgsbergcbb59da2007-02-16 17:34:35 -05001570 last->control |= cpu_to_le16(descriptor_output_last |
1571 descriptor_status |
1572 descriptor_branch_always |
1573 irq);
Kristian Høgsberged568912006-12-19 19:58:35 -05001574
Kristian Høgsberg30200732007-02-16 17:34:39 -05001575 context_append(&ctx->context, d, z, header_z);
Kristian Høgsberged568912006-12-19 19:58:35 -05001576
1577 return 0;
1578}
Stefan Richter373b2ed2007-03-04 14:45:18 +01001579
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001580static int
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001581ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
1582 struct fw_iso_packet *packet,
1583 struct fw_iso_buffer *buffer,
1584 unsigned long payload)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001585{
1586 struct iso_context *ctx = container_of(base, struct iso_context, base);
1587 struct db_descriptor *db = NULL;
1588 struct descriptor *d;
1589 struct fw_iso_packet *p;
1590 dma_addr_t d_bus, page_bus;
1591 u32 z, header_z, length, rest;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001592 int page, offset, packet_count, header_size;
Stefan Richter373b2ed2007-03-04 14:45:18 +01001593
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001594 /*
1595 * FIXME: Cycle lost behavior should be configurable: lose
1596 * packet, retransmit or terminate..
1597 */
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001598
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001599 if (packet->skip) {
1600 d = context_get_descriptors(&ctx->context, 2, &d_bus);
1601 if (d == NULL)
1602 return -ENOMEM;
1603
1604 db = (struct db_descriptor *) d;
1605 db->control = cpu_to_le16(descriptor_status |
1606 descriptor_branch_always |
1607 descriptor_wait);
1608 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
1609 context_append(&ctx->context, d, 2, 0);
1610 }
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001611
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001612 p = packet;
1613 z = 2;
1614
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001615 /*
1616 * The OHCI controller puts the status word in the header
1617 * buffer too, so we need 4 extra bytes per packet.
1618 */
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001619 packet_count = p->header_length / ctx->base.header_size;
1620 header_size = packet_count * (ctx->base.header_size + 4);
1621
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001622 /* Get header size in number of descriptors. */
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001623 header_z = DIV_ROUND_UP(header_size, sizeof *d);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001624 page = payload >> PAGE_SHIFT;
1625 offset = payload & ~PAGE_MASK;
1626 rest = p->payload_length;
1627
1628 /* FIXME: OHCI 1.0 doesn't support dual buffer receive */
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001629 /* FIXME: make packet-per-buffer/dual-buffer a context option */
1630 while (rest > 0) {
1631 d = context_get_descriptors(&ctx->context,
1632 z + header_z, &d_bus);
1633 if (d == NULL)
1634 return -ENOMEM;
1635
1636 db = (struct db_descriptor *) d;
1637 db->control = cpu_to_le16(descriptor_status |
1638 descriptor_branch_always);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001639 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
1640 db->first_req_count = cpu_to_le16(header_size);
Kristian Høgsberg1e1d1962007-02-16 17:34:45 -05001641 db->first_res_count = db->first_req_count;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001642 db->first_buffer = cpu_to_le32(d_bus + sizeof *db);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001643
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001644 if (offset + rest < PAGE_SIZE)
1645 length = rest;
1646 else
1647 length = PAGE_SIZE - offset;
1648
Kristian Høgsberg1e1d1962007-02-16 17:34:45 -05001649 db->second_req_count = cpu_to_le16(length);
1650 db->second_res_count = db->second_req_count;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001651 page_bus = page_private(buffer->pages[page]);
1652 db->second_buffer = cpu_to_le32(page_bus + offset);
1653
Kristian Høgsbergcb2d2cd2007-02-16 17:34:47 -05001654 if (p->interrupt && length == rest)
1655 db->control |= cpu_to_le16(descriptor_irq_always);
1656
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001657 context_append(&ctx->context, d, z, header_z);
1658 offset = (offset + length) & ~PAGE_MASK;
1659 rest -= length;
1660 page++;
1661 }
1662
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001663 return 0;
1664}
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05001665
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001666static int
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001667ohci_queue_iso(struct fw_iso_context *base,
1668 struct fw_iso_packet *packet,
1669 struct fw_iso_buffer *buffer,
1670 unsigned long payload)
1671{
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001672 struct iso_context *ctx = container_of(base, struct iso_context, base);
1673
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001674 if (base->type == FW_ISO_CONTEXT_TRANSMIT)
1675 return ohci_queue_iso_transmit(base, packet, buffer, payload);
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001676 else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001677 return ohci_queue_iso_receive_dualbuffer(base, packet,
1678 buffer, payload);
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001679 else
1680 /* FIXME: Implement fallback for OHCI 1.0 controllers. */
1681 return -EINVAL;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001682}
1683
Stefan Richter21ebcd12007-01-14 15:29:07 +01001684static const struct fw_card_driver ohci_driver = {
Kristian Høgsberged568912006-12-19 19:58:35 -05001685 .name = ohci_driver_name,
1686 .enable = ohci_enable,
1687 .update_phy_reg = ohci_update_phy_reg,
1688 .set_config_rom = ohci_set_config_rom,
1689 .send_request = ohci_send_request,
1690 .send_response = ohci_send_response,
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001691 .cancel_packet = ohci_cancel_packet,
Kristian Høgsberged568912006-12-19 19:58:35 -05001692 .enable_phys_dma = ohci_enable_phys_dma,
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001693 .get_bus_time = ohci_get_bus_time,
Kristian Høgsberged568912006-12-19 19:58:35 -05001694
1695 .allocate_iso_context = ohci_allocate_iso_context,
1696 .free_iso_context = ohci_free_iso_context,
1697 .queue_iso = ohci_queue_iso,
Kristian Høgsberg69cdb722007-02-16 17:34:41 -05001698 .start_iso = ohci_start_iso,
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001699 .stop_iso = ohci_stop_iso,
Kristian Høgsberged568912006-12-19 19:58:35 -05001700};
1701
1702static int software_reset(struct fw_ohci *ohci)
1703{
1704 int i;
1705
1706 reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
1707
1708 for (i = 0; i < OHCI_LOOP_COUNT; i++) {
1709 if ((reg_read(ohci, OHCI1394_HCControlSet) &
1710 OHCI1394_HCControl_softReset) == 0)
1711 return 0;
1712 msleep(1);
1713 }
1714
1715 return -EBUSY;
1716}
1717
Kristian Høgsberged568912006-12-19 19:58:35 -05001718enum {
1719 CLEANUP_SELF_ID,
1720 CLEANUP_REGISTERS,
1721 CLEANUP_IOMEM,
1722 CLEANUP_DISABLE,
1723 CLEANUP_PUT_CARD,
1724};
1725
1726static int cleanup(struct fw_ohci *ohci, int stage, int code)
1727{
1728 struct pci_dev *dev = to_pci_dev(ohci->card.device);
1729
1730 switch (stage) {
1731 case CLEANUP_SELF_ID:
1732 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
1733 ohci->self_id_cpu, ohci->self_id_bus);
1734 case CLEANUP_REGISTERS:
1735 kfree(ohci->it_context_list);
1736 kfree(ohci->ir_context_list);
1737 pci_iounmap(dev, ohci->registers);
1738 case CLEANUP_IOMEM:
1739 pci_release_region(dev, 0);
1740 case CLEANUP_DISABLE:
1741 pci_disable_device(dev);
1742 case CLEANUP_PUT_CARD:
1743 fw_card_put(&ohci->card);
1744 }
1745
1746 return code;
1747}
1748
1749static int __devinit
1750pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
1751{
1752 struct fw_ohci *ohci;
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001753 u32 bus_options, max_receive, link_speed;
Kristian Høgsberged568912006-12-19 19:58:35 -05001754 u64 guid;
1755 int error_code;
1756 size_t size;
1757
1758 ohci = kzalloc(sizeof *ohci, GFP_KERNEL);
1759 if (ohci == NULL) {
1760 fw_error("Could not malloc fw_ohci data.\n");
1761 return -ENOMEM;
1762 }
1763
1764 fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
1765
1766 if (pci_enable_device(dev)) {
1767 fw_error("Failed to enable OHCI hardware.\n");
1768 return cleanup(ohci, CLEANUP_PUT_CARD, -ENODEV);
1769 }
1770
1771 pci_set_master(dev);
1772 pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
1773 pci_set_drvdata(dev, ohci);
1774
1775 spin_lock_init(&ohci->lock);
1776
1777 tasklet_init(&ohci->bus_reset_tasklet,
1778 bus_reset_tasklet, (unsigned long)ohci);
1779
1780 if (pci_request_region(dev, 0, ohci_driver_name)) {
1781 fw_error("MMIO resource unavailable\n");
1782 return cleanup(ohci, CLEANUP_DISABLE, -EBUSY);
1783 }
1784
1785 ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
1786 if (ohci->registers == NULL) {
1787 fw_error("Failed to remap registers\n");
1788 return cleanup(ohci, CLEANUP_IOMEM, -ENXIO);
1789 }
1790
1791 if (software_reset(ohci)) {
1792 fw_error("Failed to reset ohci card.\n");
1793 return cleanup(ohci, CLEANUP_REGISTERS, -EBUSY);
1794 }
1795
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001796 /*
1797 * Now enable LPS, which we need in order to start accessing
Kristian Høgsberged568912006-12-19 19:58:35 -05001798 * most of the registers. In fact, on some cards (ALI M5251),
1799 * accessing registers in the SClk domain without LPS enabled
1800 * will lock up the machine. Wait 50msec to make sure we have
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001801 * full link enabled.
1802 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001803 reg_write(ohci, OHCI1394_HCControlSet,
1804 OHCI1394_HCControl_LPS |
1805 OHCI1394_HCControl_postedWriteEnable);
1806 flush_writes(ohci);
1807 msleep(50);
1808
1809 reg_write(ohci, OHCI1394_HCControlClear,
1810 OHCI1394_HCControl_noByteSwapData);
1811
1812 reg_write(ohci, OHCI1394_LinkControlSet,
1813 OHCI1394_LinkControl_rcvSelfID |
1814 OHCI1394_LinkControl_cycleTimerEnable |
1815 OHCI1394_LinkControl_cycleMaster);
1816
1817 ar_context_init(&ohci->ar_request_ctx, ohci,
1818 OHCI1394_AsReqRcvContextControlSet);
1819
1820 ar_context_init(&ohci->ar_response_ctx, ohci,
1821 OHCI1394_AsRspRcvContextControlSet);
1822
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001823 context_init(&ohci->at_request_ctx, ohci, AT_BUFFER_SIZE,
1824 OHCI1394_AsReqTrContextControlSet, handle_at_packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001825
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001826 context_init(&ohci->at_response_ctx, ohci, AT_BUFFER_SIZE,
1827 OHCI1394_AsRspTrContextControlSet, handle_at_packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001828
1829 reg_write(ohci, OHCI1394_ATRetries,
1830 OHCI1394_MAX_AT_REQ_RETRIES |
1831 (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
1832 (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
1833
1834 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
1835 ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
1836 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
1837 size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
1838 ohci->it_context_list = kzalloc(size, GFP_KERNEL);
1839
1840 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
1841 ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
1842 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
1843 size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
1844 ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
1845
1846 if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
1847 fw_error("Out of memory for it/ir contexts.\n");
1848 return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
1849 }
1850
1851 /* self-id dma buffer allocation */
1852 ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
1853 SELF_ID_BUF_SIZE,
1854 &ohci->self_id_bus,
1855 GFP_KERNEL);
1856 if (ohci->self_id_cpu == NULL) {
1857 fw_error("Out of memory for self ID buffer.\n");
1858 return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
1859 }
1860
1861 reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
1862 reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
1863 reg_write(ohci, OHCI1394_IntEventClear, ~0);
1864 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1865 reg_write(ohci, OHCI1394_IntMaskSet,
1866 OHCI1394_selfIDComplete |
1867 OHCI1394_RQPkt | OHCI1394_RSPkt |
1868 OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
1869 OHCI1394_isochRx | OHCI1394_isochTx |
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001870 OHCI1394_masterIntEnable |
1871 OHCI1394_cycle64Seconds);
Kristian Høgsberged568912006-12-19 19:58:35 -05001872
1873 bus_options = reg_read(ohci, OHCI1394_BusOptions);
1874 max_receive = (bus_options >> 12) & 0xf;
1875 link_speed = bus_options & 0x7;
1876 guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
1877 reg_read(ohci, OHCI1394_GUIDLo);
1878
1879 error_code = fw_card_add(&ohci->card, max_receive, link_speed, guid);
1880 if (error_code < 0)
1881 return cleanup(ohci, CLEANUP_SELF_ID, error_code);
1882
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001883 ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
Kristian Høgsberg500be722007-02-16 17:34:43 -05001884 fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001885 dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
Kristian Høgsberged568912006-12-19 19:58:35 -05001886
1887 return 0;
1888}
1889
1890static void pci_remove(struct pci_dev *dev)
1891{
1892 struct fw_ohci *ohci;
1893
1894 ohci = pci_get_drvdata(dev);
Kristian Høgsberge254a4b2007-03-07 12:12:38 -05001895 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1896 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -05001897 fw_core_remove_card(&ohci->card);
1898
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001899 /*
1900 * FIXME: Fail all pending packets here, now that the upper
1901 * layers can't queue any more.
1902 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001903
1904 software_reset(ohci);
1905 free_irq(dev->irq, ohci);
1906 cleanup(ohci, CLEANUP_SELF_ID, 0);
1907
1908 fw_notify("Removed fw-ohci device.\n");
1909}
1910
1911static struct pci_device_id pci_table[] = {
1912 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
1913 { }
1914};
1915
1916MODULE_DEVICE_TABLE(pci, pci_table);
1917
1918static struct pci_driver fw_ohci_pci_driver = {
1919 .name = ohci_driver_name,
1920 .id_table = pci_table,
1921 .probe = pci_probe,
1922 .remove = pci_remove,
1923};
1924
1925MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1926MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
1927MODULE_LICENSE("GPL");
1928
1929static int __init fw_ohci_init(void)
1930{
1931 return pci_register_driver(&fw_ohci_pci_driver);
1932}
1933
1934static void __exit fw_ohci_cleanup(void)
1935{
1936 pci_unregister_driver(&fw_ohci_pci_driver);
1937}
1938
1939module_init(fw_ohci_init);
1940module_exit(fw_ohci_cleanup);