blob: a333748255269659abfbee6c314e28f93d5e5af7 [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;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500128 void *header;
129 size_t header_length;
Kristian Høgsberged568912006-12-19 19:58:35 -0500130};
131
132#define CONFIG_ROM_SIZE 1024
133
134struct fw_ohci {
135 struct fw_card card;
136
Kristian Høgsberge364cf42007-02-16 17:34:49 -0500137 u32 version;
Kristian Høgsberged568912006-12-19 19:58:35 -0500138 __iomem char *registers;
139 dma_addr_t self_id_bus;
140 __le32 *self_id_cpu;
141 struct tasklet_struct bus_reset_tasklet;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500142 int node_id;
Kristian Høgsberged568912006-12-19 19:58:35 -0500143 int generation;
144 int request_generation;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -0500145 u32 bus_seconds;
Kristian Høgsberged568912006-12-19 19:58:35 -0500146
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400147 /*
148 * Spinlock for accessing fw_ohci data. Never call out of
149 * this driver with this lock held.
150 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500151 spinlock_t lock;
152 u32 self_id_buffer[512];
153
154 /* Config rom buffers */
155 __be32 *config_rom;
156 dma_addr_t config_rom_bus;
157 __be32 *next_config_rom;
158 dma_addr_t next_config_rom_bus;
159 u32 next_header;
160
161 struct ar_context ar_request_ctx;
162 struct ar_context ar_response_ctx;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500163 struct context at_request_ctx;
164 struct context at_response_ctx;
Kristian Høgsberged568912006-12-19 19:58:35 -0500165
166 u32 it_context_mask;
167 struct iso_context *it_context_list;
168 u32 ir_context_mask;
169 struct iso_context *ir_context_list;
170};
171
Adrian Bunk95688e92007-01-22 19:17:37 +0100172static inline struct fw_ohci *fw_ohci(struct fw_card *card)
Kristian Høgsberged568912006-12-19 19:58:35 -0500173{
174 return container_of(card, struct fw_ohci, card);
175}
176
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500177#define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
178#define IR_CONTEXT_BUFFER_FILL 0x80000000
179#define IR_CONTEXT_ISOCH_HEADER 0x40000000
180#define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
181#define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
182#define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
Kristian Høgsberged568912006-12-19 19:58:35 -0500183
184#define CONTEXT_RUN 0x8000
185#define CONTEXT_WAKE 0x1000
186#define CONTEXT_DEAD 0x0800
187#define CONTEXT_ACTIVE 0x0400
188
189#define OHCI1394_MAX_AT_REQ_RETRIES 0x2
190#define OHCI1394_MAX_AT_RESP_RETRIES 0x2
191#define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
192
193#define FW_OHCI_MAJOR 240
194#define OHCI1394_REGISTER_SIZE 0x800
195#define OHCI_LOOP_COUNT 500
196#define OHCI1394_PCI_HCI_Control 0x40
197#define SELF_ID_BUF_SIZE 0x800
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500198#define OHCI_TCODE_PHY_PACKET 0x0e
Kristian Høgsberge364cf42007-02-16 17:34:49 -0500199#define OHCI_VERSION_1_1 0x010010
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500200#define ISO_BUFFER_SIZE (64 * 1024)
201#define AT_BUFFER_SIZE 4096
Kristian Høgsberg0edeefd2007-01-26 00:38:49 -0500202
Kristian Høgsberged568912006-12-19 19:58:35 -0500203static char ohci_driver_name[] = KBUILD_MODNAME;
204
Adrian Bunk95688e92007-01-22 19:17:37 +0100205static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
Kristian Høgsberged568912006-12-19 19:58:35 -0500206{
207 writel(data, ohci->registers + offset);
208}
209
Adrian Bunk95688e92007-01-22 19:17:37 +0100210static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
Kristian Høgsberged568912006-12-19 19:58:35 -0500211{
212 return readl(ohci->registers + offset);
213}
214
Adrian Bunk95688e92007-01-22 19:17:37 +0100215static inline void flush_writes(const struct fw_ohci *ohci)
Kristian Høgsberged568912006-12-19 19:58:35 -0500216{
217 /* Do a dummy read to flush writes. */
218 reg_read(ohci, OHCI1394_Version);
219}
220
221static int
222ohci_update_phy_reg(struct fw_card *card, int addr,
223 int clear_bits, int set_bits)
224{
225 struct fw_ohci *ohci = fw_ohci(card);
226 u32 val, old;
227
228 reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
Stefan Richter362e9012007-07-12 22:24:19 +0200229 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -0500230 msleep(2);
231 val = reg_read(ohci, OHCI1394_PhyControl);
232 if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
233 fw_error("failed to set phy reg bits.\n");
234 return -EBUSY;
235 }
236
237 old = OHCI1394_PhyControl_ReadData(val);
238 old = (old & ~clear_bits) | set_bits;
239 reg_write(ohci, OHCI1394_PhyControl,
240 OHCI1394_PhyControl_Write(addr, old));
241
242 return 0;
243}
244
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500245static int ar_context_add_page(struct ar_context *ctx)
Kristian Høgsberged568912006-12-19 19:58:35 -0500246{
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500247 struct device *dev = ctx->ohci->card.device;
248 struct ar_buffer *ab;
249 dma_addr_t ab_bus;
250 size_t offset;
251
252 ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
253 if (ab == NULL)
254 return -ENOMEM;
255
256 ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
257 if (dma_mapping_error(ab_bus)) {
258 free_page((unsigned long) ab);
259 return -ENOMEM;
260 }
261
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400262 memset(&ab->descriptor, 0, sizeof(ab->descriptor));
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400263 ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
264 DESCRIPTOR_STATUS |
265 DESCRIPTOR_BRANCH_ALWAYS);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500266 offset = offsetof(struct ar_buffer, data);
267 ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
268 ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
269 ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
270 ab->descriptor.branch_address = 0;
271
272 dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
273
Kristian Høgsbergec839e42007-05-22 18:55:48 -0400274 ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500275 ctx->last_buffer->next = ab;
276 ctx->last_buffer = ab;
277
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400278 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
Kristian Høgsberged568912006-12-19 19:58:35 -0500279 flush_writes(ctx->ohci);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500280
281 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -0500282}
283
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500284static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
Kristian Høgsberged568912006-12-19 19:58:35 -0500285{
Kristian Høgsberged568912006-12-19 19:58:35 -0500286 struct fw_ohci *ohci = ctx->ohci;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500287 struct fw_packet p;
288 u32 status, length, tcode;
Kristian Høgsberg0edeefd2007-01-26 00:38:49 -0500289
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500290 p.header[0] = le32_to_cpu(buffer[0]);
291 p.header[1] = le32_to_cpu(buffer[1]);
292 p.header[2] = le32_to_cpu(buffer[2]);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500293
294 tcode = (p.header[0] >> 4) & 0x0f;
295 switch (tcode) {
296 case TCODE_WRITE_QUADLET_REQUEST:
297 case TCODE_READ_QUADLET_RESPONSE:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500298 p.header[3] = (__force __u32) buffer[3];
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500299 p.header_length = 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500300 p.payload_length = 0;
301 break;
302
303 case TCODE_READ_BLOCK_REQUEST :
304 p.header[3] = le32_to_cpu(buffer[3]);
305 p.header_length = 16;
306 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500307 break;
308
309 case TCODE_WRITE_BLOCK_REQUEST:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500310 case TCODE_READ_BLOCK_RESPONSE:
311 case TCODE_LOCK_REQUEST:
312 case TCODE_LOCK_RESPONSE:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500313 p.header[3] = le32_to_cpu(buffer[3]);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500314 p.header_length = 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500315 p.payload_length = p.header[3] >> 16;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500316 break;
317
318 case TCODE_WRITE_RESPONSE:
319 case TCODE_READ_QUADLET_REQUEST:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500320 case OHCI_TCODE_PHY_PACKET:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500321 p.header_length = 12;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500322 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500323 break;
324 }
325
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500326 p.payload = (void *) buffer + p.header_length;
327
328 /* FIXME: What to do about evt_* errors? */
329 length = (p.header_length + p.payload_length + 3) / 4;
330 status = le32_to_cpu(buffer[length]);
331
332 p.ack = ((status >> 16) & 0x1f) - 16;
333 p.speed = (status >> 21) & 0x7;
334 p.timestamp = status & 0xffff;
335 p.generation = ohci->request_generation;
Kristian Høgsberged568912006-12-19 19:58:35 -0500336
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400337 /*
338 * The OHCI bus reset handler synthesizes a phy packet with
Kristian Høgsberged568912006-12-19 19:58:35 -0500339 * the new generation number when a bus reset happens (see
340 * section 8.4.2.3). This helps us determine when a request
341 * was received and make sure we send the response in the same
342 * generation. We only need this for requests; for responses
343 * we use the unique tlabel for finding the matching
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400344 * request.
345 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500346
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500347 if (p.ack + 16 == 0x09)
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500348 ohci->request_generation = (buffer[2] >> 16) & 0xff;
Kristian Høgsberged568912006-12-19 19:58:35 -0500349 else if (ctx == &ohci->ar_request_ctx)
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500350 fw_core_handle_request(&ohci->card, &p);
Kristian Høgsberged568912006-12-19 19:58:35 -0500351 else
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500352 fw_core_handle_response(&ohci->card, &p);
Kristian Høgsberged568912006-12-19 19:58:35 -0500353
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500354 return buffer + length + 1;
355}
Kristian Høgsberged568912006-12-19 19:58:35 -0500356
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500357static void ar_context_tasklet(unsigned long data)
358{
359 struct ar_context *ctx = (struct ar_context *)data;
360 struct fw_ohci *ohci = ctx->ohci;
361 struct ar_buffer *ab;
362 struct descriptor *d;
363 void *buffer, *end;
Kristian Høgsberged568912006-12-19 19:58:35 -0500364
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500365 ab = ctx->current_buffer;
366 d = &ab->descriptor;
Kristian Høgsberged568912006-12-19 19:58:35 -0500367
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500368 if (d->res_count == 0) {
369 size_t size, rest, offset;
370
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400371 /*
372 * This descriptor is finished and we may have a
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500373 * packet split across this and the next buffer. We
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400374 * reuse the page for reassembling the split packet.
375 */
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500376
377 offset = offsetof(struct ar_buffer, data);
378 dma_unmap_single(ohci->card.device,
Stefan Richter0a9972b2007-06-23 20:28:17 +0200379 le32_to_cpu(ab->descriptor.data_address) - offset,
380 PAGE_SIZE, DMA_BIDIRECTIONAL);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500381
382 buffer = ab;
383 ab = ab->next;
384 d = &ab->descriptor;
385 size = buffer + PAGE_SIZE - ctx->pointer;
386 rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
387 memmove(buffer, ctx->pointer, size);
388 memcpy(buffer + size, ab->data, rest);
389 ctx->current_buffer = ab;
390 ctx->pointer = (void *) ab->data + rest;
391 end = buffer + size + rest;
392
393 while (buffer < end)
394 buffer = handle_ar_packet(ctx, buffer);
395
396 free_page((unsigned long)buffer);
397 ar_context_add_page(ctx);
398 } else {
399 buffer = ctx->pointer;
400 ctx->pointer = end =
401 (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
402
403 while (buffer < end)
404 buffer = handle_ar_packet(ctx, buffer);
405 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500406}
407
408static int
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500409ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
Kristian Høgsberged568912006-12-19 19:58:35 -0500410{
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500411 struct ar_buffer ab;
Kristian Høgsberged568912006-12-19 19:58:35 -0500412
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500413 ctx->regs = regs;
414 ctx->ohci = ohci;
415 ctx->last_buffer = &ab;
Kristian Høgsberged568912006-12-19 19:58:35 -0500416 tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
417
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500418 ar_context_add_page(ctx);
419 ar_context_add_page(ctx);
420 ctx->current_buffer = ab.next;
421 ctx->pointer = ctx->current_buffer->data;
422
Kristian Høgsberg2aef4692007-05-30 19:06:35 -0400423 return 0;
424}
425
426static void ar_context_run(struct ar_context *ctx)
427{
428 struct ar_buffer *ab = ctx->current_buffer;
429 dma_addr_t ab_bus;
430 size_t offset;
431
432 offset = offsetof(struct ar_buffer, data);
Stefan Richter0a9972b2007-06-23 20:28:17 +0200433 ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
Kristian Høgsberg2aef4692007-05-30 19:06:35 -0400434
435 reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ab_bus | 1);
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400436 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500437 flush_writes(ctx->ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -0500438}
Stefan Richter373b2ed2007-03-04 14:45:18 +0100439
Kristian Høgsberg30200732007-02-16 17:34:39 -0500440static void context_tasklet(unsigned long data)
441{
442 struct context *ctx = (struct context *) data;
443 struct fw_ohci *ohci = ctx->ohci;
444 struct descriptor *d, *last;
445 u32 address;
446 int z;
447
448 dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
449 ctx->buffer_size, DMA_TO_DEVICE);
450
451 d = ctx->tail_descriptor;
452 last = ctx->tail_descriptor_last;
453
454 while (last->branch_address != 0) {
455 address = le32_to_cpu(last->branch_address);
456 z = address & 0xf;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400457 d = ctx->buffer + (address - ctx->buffer_bus) / sizeof(*d);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500458 last = (z == 2) ? d : d + z - 1;
459
460 if (!ctx->callback(ctx, d, last))
461 break;
462
463 ctx->tail_descriptor = d;
464 ctx->tail_descriptor_last = last;
465 }
466}
467
468static int
469context_init(struct context *ctx, struct fw_ohci *ohci,
470 size_t buffer_size, u32 regs,
471 descriptor_callback_t callback)
472{
473 ctx->ohci = ohci;
474 ctx->regs = regs;
475 ctx->buffer_size = buffer_size;
476 ctx->buffer = kmalloc(buffer_size, GFP_KERNEL);
477 if (ctx->buffer == NULL)
478 return -ENOMEM;
479
480 tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
481 ctx->callback = callback;
482
483 ctx->buffer_bus =
484 dma_map_single(ohci->card.device, ctx->buffer,
485 buffer_size, DMA_TO_DEVICE);
486 if (dma_mapping_error(ctx->buffer_bus)) {
487 kfree(ctx->buffer);
488 return -ENOMEM;
489 }
490
491 ctx->head_descriptor = ctx->buffer;
492 ctx->prev_descriptor = ctx->buffer;
493 ctx->tail_descriptor = ctx->buffer;
494 ctx->tail_descriptor_last = ctx->buffer;
495
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400496 /*
497 * We put a dummy descriptor in the buffer that has a NULL
Kristian Høgsberg30200732007-02-16 17:34:39 -0500498 * branch address and looks like it's been sent. That way we
499 * have a descriptor to append DMA programs to. Also, the
500 * ring buffer invariant is that it always has at least one
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400501 * element so that head == tail means buffer full.
502 */
Kristian Høgsberg30200732007-02-16 17:34:39 -0500503
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400504 memset(ctx->head_descriptor, 0, sizeof(*ctx->head_descriptor));
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400505 ctx->head_descriptor->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500506 ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
507 ctx->head_descriptor++;
508
509 return 0;
510}
511
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500512static void
Kristian Høgsberg30200732007-02-16 17:34:39 -0500513context_release(struct context *ctx)
514{
515 struct fw_card *card = &ctx->ohci->card;
516
517 dma_unmap_single(card->device, ctx->buffer_bus,
518 ctx->buffer_size, DMA_TO_DEVICE);
519 kfree(ctx->buffer);
520}
521
522static struct descriptor *
523context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
524{
525 struct descriptor *d, *tail, *end;
526
527 d = ctx->head_descriptor;
528 tail = ctx->tail_descriptor;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400529 end = ctx->buffer + ctx->buffer_size / sizeof(*d);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500530
531 if (d + z <= tail) {
532 goto has_space;
533 } else if (d > tail && d + z <= end) {
534 goto has_space;
535 } else if (d > tail && ctx->buffer + z <= tail) {
536 d = ctx->buffer;
537 goto has_space;
538 }
539
540 return NULL;
541
542 has_space:
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400543 memset(d, 0, z * sizeof(*d));
544 *d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500545
546 return d;
547}
548
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500549static void context_run(struct context *ctx, u32 extra)
Kristian Høgsberg30200732007-02-16 17:34:39 -0500550{
551 struct fw_ohci *ohci = ctx->ohci;
552
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400553 reg_write(ohci, COMMAND_PTR(ctx->regs),
Kristian Høgsberg30200732007-02-16 17:34:39 -0500554 le32_to_cpu(ctx->tail_descriptor_last->branch_address));
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400555 reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
556 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500557 flush_writes(ohci);
558}
559
560static void context_append(struct context *ctx,
561 struct descriptor *d, int z, int extra)
562{
563 dma_addr_t d_bus;
564
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400565 d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500566
567 ctx->head_descriptor = d + z + extra;
568 ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
569 ctx->prev_descriptor = z == 2 ? d : d + z - 1;
570
571 dma_sync_single_for_device(ctx->ohci->card.device, ctx->buffer_bus,
572 ctx->buffer_size, DMA_TO_DEVICE);
573
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400574 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500575 flush_writes(ctx->ohci);
576}
577
578static void context_stop(struct context *ctx)
579{
580 u32 reg;
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500581 int i;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500582
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400583 reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500584 flush_writes(ctx->ohci);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500585
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500586 for (i = 0; i < 10; i++) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400587 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500588 if ((reg & CONTEXT_ACTIVE) == 0)
589 break;
590
591 fw_notify("context_stop: still active (0x%08x)\n", reg);
Stefan Richterb980f5a2007-07-12 22:25:14 +0200592 mdelay(1);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500593 }
Kristian Høgsberg30200732007-02-16 17:34:39 -0500594}
Kristian Høgsberged568912006-12-19 19:58:35 -0500595
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500596struct driver_data {
Kristian Høgsberged568912006-12-19 19:58:35 -0500597 struct fw_packet *packet;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500598};
599
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400600/*
601 * This function apppends a packet to the DMA queue for transmission.
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500602 * Must always be called with the ochi->lock held to ensure proper
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400603 * generation handling and locking around packet queue manipulation.
604 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500605static int
606at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
607{
Kristian Høgsberged568912006-12-19 19:58:35 -0500608 struct fw_ohci *ohci = ctx->ohci;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500609 dma_addr_t d_bus, payload_bus;
610 struct driver_data *driver_data;
611 struct descriptor *d, *last;
612 __le32 *header;
Kristian Høgsberged568912006-12-19 19:58:35 -0500613 int z, tcode;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500614 u32 reg;
Kristian Høgsberged568912006-12-19 19:58:35 -0500615
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500616 d = context_get_descriptors(ctx, 4, &d_bus);
617 if (d == NULL) {
618 packet->ack = RCODE_SEND_ERROR;
619 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -0500620 }
621
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400622 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500623 d[0].res_count = cpu_to_le16(packet->timestamp);
624
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400625 /*
626 * The DMA format for asyncronous link packets is different
Kristian Høgsberged568912006-12-19 19:58:35 -0500627 * from the IEEE1394 layout, so shift the fields around
628 * accordingly. If header_length is 8, it's a PHY packet, to
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400629 * which we need to prepend an extra quadlet.
630 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500631
632 header = (__le32 *) &d[1];
Kristian Høgsberged568912006-12-19 19:58:35 -0500633 if (packet->header_length > 8) {
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500634 header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
635 (packet->speed << 16));
636 header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
637 (packet->header[0] & 0xffff0000));
638 header[2] = cpu_to_le32(packet->header[2]);
Kristian Høgsberged568912006-12-19 19:58:35 -0500639
640 tcode = (packet->header[0] >> 4) & 0x0f;
641 if (TCODE_IS_BLOCK_PACKET(tcode))
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500642 header[3] = cpu_to_le32(packet->header[3]);
Kristian Høgsberged568912006-12-19 19:58:35 -0500643 else
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500644 header[3] = (__force __le32) packet->header[3];
645
646 d[0].req_count = cpu_to_le16(packet->header_length);
Kristian Høgsberged568912006-12-19 19:58:35 -0500647 } else {
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500648 header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
649 (packet->speed << 16));
650 header[1] = cpu_to_le32(packet->header[0]);
651 header[2] = cpu_to_le32(packet->header[1]);
652 d[0].req_count = cpu_to_le16(12);
Kristian Høgsberged568912006-12-19 19:58:35 -0500653 }
654
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500655 driver_data = (struct driver_data *) &d[3];
656 driver_data->packet = packet;
Kristian Høgsberg20d11672007-03-26 19:18:19 -0400657 packet->driver_data = driver_data;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500658
659 if (packet->payload_length > 0) {
660 payload_bus =
661 dma_map_single(ohci->card.device, packet->payload,
662 packet->payload_length, DMA_TO_DEVICE);
663 if (dma_mapping_error(payload_bus)) {
664 packet->ack = RCODE_SEND_ERROR;
665 return -1;
666 }
667
668 d[2].req_count = cpu_to_le16(packet->payload_length);
669 d[2].data_address = cpu_to_le32(payload_bus);
670 last = &d[2];
671 z = 3;
672 } else {
673 last = &d[0];
674 z = 2;
675 }
676
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400677 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
678 DESCRIPTOR_IRQ_ALWAYS |
679 DESCRIPTOR_BRANCH_ALWAYS);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500680
Kristian Høgsberged568912006-12-19 19:58:35 -0500681 /* FIXME: Document how the locking works. */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500682 if (ohci->generation != packet->generation) {
683 packet->ack = RCODE_GENERATION;
684 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -0500685 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500686
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500687 context_append(ctx, d, z, 4 - z);
Kristian Høgsberged568912006-12-19 19:58:35 -0500688
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500689 /* If the context isn't already running, start it up. */
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400690 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
Kristian Høgsberg053b3082007-04-10 18:11:17 -0400691 if ((reg & CONTEXT_RUN) == 0)
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500692 context_run(ctx, 0);
Kristian Høgsberged568912006-12-19 19:58:35 -0500693
694 return 0;
695}
696
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500697static int handle_at_packet(struct context *context,
698 struct descriptor *d,
699 struct descriptor *last)
700{
701 struct driver_data *driver_data;
702 struct fw_packet *packet;
703 struct fw_ohci *ohci = context->ohci;
704 dma_addr_t payload_bus;
705 int evt;
706
707 if (last->transfer_status == 0)
708 /* This descriptor isn't done yet, stop iteration. */
709 return 0;
710
711 driver_data = (struct driver_data *) &d[3];
712 packet = driver_data->packet;
713 if (packet == NULL)
714 /* This packet was cancelled, just continue. */
715 return 1;
716
717 payload_bus = le32_to_cpu(last->data_address);
718 if (payload_bus != 0)
719 dma_unmap_single(ohci->card.device, payload_bus,
720 packet->payload_length, DMA_TO_DEVICE);
721
722 evt = le16_to_cpu(last->transfer_status) & 0x1f;
723 packet->timestamp = le16_to_cpu(last->res_count);
724
725 switch (evt) {
726 case OHCI1394_evt_timeout:
727 /* Async response transmit timed out. */
728 packet->ack = RCODE_CANCELLED;
729 break;
730
731 case OHCI1394_evt_flushed:
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400732 /*
733 * The packet was flushed should give same error as
734 * when we try to use a stale generation count.
735 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500736 packet->ack = RCODE_GENERATION;
737 break;
738
739 case OHCI1394_evt_missing_ack:
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400740 /*
741 * Using a valid (current) generation count, but the
742 * node is not on the bus or not sending acks.
743 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500744 packet->ack = RCODE_NO_ACK;
745 break;
746
747 case ACK_COMPLETE + 0x10:
748 case ACK_PENDING + 0x10:
749 case ACK_BUSY_X + 0x10:
750 case ACK_BUSY_A + 0x10:
751 case ACK_BUSY_B + 0x10:
752 case ACK_DATA_ERROR + 0x10:
753 case ACK_TYPE_ERROR + 0x10:
754 packet->ack = evt - 0x10;
755 break;
756
757 default:
758 packet->ack = RCODE_SEND_ERROR;
759 break;
760 }
761
762 packet->callback(packet, &ohci->card, packet->ack);
763
764 return 1;
765}
766
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400767#define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
768#define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
769#define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
770#define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
771#define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500772
773static void
774handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
775{
776 struct fw_packet response;
777 int tcode, length, i;
778
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400779 tcode = HEADER_GET_TCODE(packet->header[0]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500780 if (TCODE_IS_BLOCK_PACKET(tcode))
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400781 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500782 else
783 length = 4;
784
785 i = csr - CSR_CONFIG_ROM;
786 if (i + length > CONFIG_ROM_SIZE) {
787 fw_fill_response(&response, packet->header,
788 RCODE_ADDRESS_ERROR, NULL, 0);
789 } else if (!TCODE_IS_READ_REQUEST(tcode)) {
790 fw_fill_response(&response, packet->header,
791 RCODE_TYPE_ERROR, NULL, 0);
792 } else {
793 fw_fill_response(&response, packet->header, RCODE_COMPLETE,
794 (void *) ohci->config_rom + i, length);
795 }
796
797 fw_core_handle_response(&ohci->card, &response);
798}
799
800static void
801handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
802{
803 struct fw_packet response;
804 int tcode, length, ext_tcode, sel;
805 __be32 *payload, lock_old;
806 u32 lock_arg, lock_data;
807
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400808 tcode = HEADER_GET_TCODE(packet->header[0]);
809 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500810 payload = packet->payload;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400811 ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500812
813 if (tcode == TCODE_LOCK_REQUEST &&
814 ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
815 lock_arg = be32_to_cpu(payload[0]);
816 lock_data = be32_to_cpu(payload[1]);
817 } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
818 lock_arg = 0;
819 lock_data = 0;
820 } else {
821 fw_fill_response(&response, packet->header,
822 RCODE_TYPE_ERROR, NULL, 0);
823 goto out;
824 }
825
826 sel = (csr - CSR_BUS_MANAGER_ID) / 4;
827 reg_write(ohci, OHCI1394_CSRData, lock_data);
828 reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
829 reg_write(ohci, OHCI1394_CSRControl, sel);
830
831 if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
832 lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
833 else
834 fw_notify("swap not done yet\n");
835
836 fw_fill_response(&response, packet->header,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400837 RCODE_COMPLETE, &lock_old, sizeof(lock_old));
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500838 out:
839 fw_core_handle_response(&ohci->card, &response);
840}
841
842static void
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500843handle_local_request(struct context *ctx, struct fw_packet *packet)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500844{
845 u64 offset;
846 u32 csr;
847
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500848 if (ctx == &ctx->ohci->at_request_ctx) {
849 packet->ack = ACK_PENDING;
850 packet->callback(packet, &ctx->ohci->card, packet->ack);
851 }
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500852
853 offset =
854 ((unsigned long long)
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400855 HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500856 packet->header[2];
857 csr = offset - CSR_REGISTER_BASE;
858
859 /* Handle config rom reads. */
860 if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
861 handle_local_rom(ctx->ohci, packet, csr);
862 else switch (csr) {
863 case CSR_BUS_MANAGER_ID:
864 case CSR_BANDWIDTH_AVAILABLE:
865 case CSR_CHANNELS_AVAILABLE_HI:
866 case CSR_CHANNELS_AVAILABLE_LO:
867 handle_local_lock(ctx->ohci, packet, csr);
868 break;
869 default:
870 if (ctx == &ctx->ohci->at_request_ctx)
871 fw_core_handle_request(&ctx->ohci->card, packet);
872 else
873 fw_core_handle_response(&ctx->ohci->card, packet);
874 break;
875 }
Kristian Høgsberg473d28c2007-03-07 12:12:55 -0500876
877 if (ctx == &ctx->ohci->at_response_ctx) {
878 packet->ack = ACK_COMPLETE;
879 packet->callback(packet, &ctx->ohci->card, packet->ack);
880 }
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500881}
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500882
Kristian Høgsberged568912006-12-19 19:58:35 -0500883static void
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500884at_context_transmit(struct context *ctx, struct fw_packet *packet)
Kristian Høgsberged568912006-12-19 19:58:35 -0500885{
Kristian Høgsberged568912006-12-19 19:58:35 -0500886 unsigned long flags;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500887 int retval;
Kristian Høgsberged568912006-12-19 19:58:35 -0500888
889 spin_lock_irqsave(&ctx->ohci->lock, flags);
890
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400891 if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500892 ctx->ohci->generation == packet->generation) {
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -0500893 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
894 handle_local_request(ctx, packet);
895 return;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500896 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500897
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500898 retval = at_context_queue_packet(ctx, packet);
Kristian Høgsberged568912006-12-19 19:58:35 -0500899 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
900
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500901 if (retval < 0)
902 packet->callback(packet, &ctx->ohci->card, packet->ack);
903
Kristian Høgsberged568912006-12-19 19:58:35 -0500904}
905
906static void bus_reset_tasklet(unsigned long data)
907{
908 struct fw_ohci *ohci = (struct fw_ohci *)data;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500909 int self_id_count, i, j, reg;
Kristian Høgsberged568912006-12-19 19:58:35 -0500910 int generation, new_generation;
911 unsigned long flags;
Stefan Richter4eaff7d2007-07-25 19:18:08 +0200912 void *free_rom = NULL;
913 dma_addr_t free_rom_bus = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -0500914
915 reg = reg_read(ohci, OHCI1394_NodeID);
916 if (!(reg & OHCI1394_NodeID_idValid)) {
917 fw_error("node ID not valid, new bus reset in progress\n");
918 return;
919 }
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500920 ohci->node_id = reg & 0xffff;
Kristian Høgsberged568912006-12-19 19:58:35 -0500921
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400922 /*
923 * The count in the SelfIDCount register is the number of
Kristian Høgsberged568912006-12-19 19:58:35 -0500924 * bytes in the self ID receive buffer. Since we also receive
925 * the inverted quadlets and a header quadlet, we shift one
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400926 * bit extra to get the actual number of self IDs.
927 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500928
929 self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
930 generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
Stefan Richteree71c2f2007-08-25 14:08:19 +0200931 rmb();
Kristian Høgsberged568912006-12-19 19:58:35 -0500932
933 for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
934 if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
935 fw_error("inconsistent self IDs\n");
936 ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
937 }
Stefan Richteree71c2f2007-08-25 14:08:19 +0200938 rmb();
Kristian Høgsberged568912006-12-19 19:58:35 -0500939
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400940 /*
941 * Check the consistency of the self IDs we just read. The
Kristian Høgsberged568912006-12-19 19:58:35 -0500942 * problem we face is that a new bus reset can start while we
943 * read out the self IDs from the DMA buffer. If this happens,
944 * the DMA buffer will be overwritten with new self IDs and we
945 * will read out inconsistent data. The OHCI specification
946 * (section 11.2) recommends a technique similar to
947 * linux/seqlock.h, where we remember the generation of the
948 * self IDs in the buffer before reading them out and compare
949 * it to the current generation after reading them out. If
950 * the two generations match we know we have a consistent set
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400951 * of self IDs.
952 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500953
954 new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
955 if (new_generation != generation) {
956 fw_notify("recursive bus reset detected, "
957 "discarding self ids\n");
958 return;
959 }
960
961 /* FIXME: Document how the locking works. */
962 spin_lock_irqsave(&ohci->lock, flags);
963
964 ohci->generation = generation;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500965 context_stop(&ohci->at_request_ctx);
966 context_stop(&ohci->at_response_ctx);
Kristian Høgsberged568912006-12-19 19:58:35 -0500967 reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
968
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400969 /*
970 * This next bit is unrelated to the AT context stuff but we
Kristian Høgsberged568912006-12-19 19:58:35 -0500971 * have to do it under the spinlock also. If a new config rom
972 * was set up before this reset, the old one is now no longer
973 * in use and we can free it. Update the config rom pointers
974 * to point to the current config rom and clear the
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400975 * next_config_rom pointer so a new udpate can take place.
976 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500977
978 if (ohci->next_config_rom != NULL) {
Stefan Richter4eaff7d2007-07-25 19:18:08 +0200979 free_rom = ohci->config_rom;
980 free_rom_bus = ohci->config_rom_bus;
Kristian Høgsberged568912006-12-19 19:58:35 -0500981 ohci->config_rom = ohci->next_config_rom;
982 ohci->config_rom_bus = ohci->next_config_rom_bus;
983 ohci->next_config_rom = NULL;
984
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400985 /*
986 * Restore config_rom image and manually update
Kristian Høgsberged568912006-12-19 19:58:35 -0500987 * config_rom registers. Writing the header quadlet
988 * will indicate that the config rom is ready, so we
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400989 * do that last.
990 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500991 reg_write(ohci, OHCI1394_BusOptions,
992 be32_to_cpu(ohci->config_rom[2]));
993 ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
994 reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
995 }
996
997 spin_unlock_irqrestore(&ohci->lock, flags);
998
Stefan Richter4eaff7d2007-07-25 19:18:08 +0200999 if (free_rom)
1000 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1001 free_rom, free_rom_bus);
1002
Kristian Høgsberge636fe22007-01-26 00:38:04 -05001003 fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
Kristian Høgsberged568912006-12-19 19:58:35 -05001004 self_id_count, ohci->self_id_buffer);
1005}
1006
1007static irqreturn_t irq_handler(int irq, void *data)
1008{
1009 struct fw_ohci *ohci = data;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001010 u32 event, iso_event, cycle_time;
Kristian Høgsberged568912006-12-19 19:58:35 -05001011 int i;
1012
1013 event = reg_read(ohci, OHCI1394_IntEventClear);
1014
Stefan Richtera5159582007-06-09 19:31:14 +02001015 if (!event || !~event)
Kristian Høgsberged568912006-12-19 19:58:35 -05001016 return IRQ_NONE;
1017
1018 reg_write(ohci, OHCI1394_IntEventClear, event);
1019
1020 if (event & OHCI1394_selfIDComplete)
1021 tasklet_schedule(&ohci->bus_reset_tasklet);
1022
1023 if (event & OHCI1394_RQPkt)
1024 tasklet_schedule(&ohci->ar_request_ctx.tasklet);
1025
1026 if (event & OHCI1394_RSPkt)
1027 tasklet_schedule(&ohci->ar_response_ctx.tasklet);
1028
1029 if (event & OHCI1394_reqTxComplete)
1030 tasklet_schedule(&ohci->at_request_ctx.tasklet);
1031
1032 if (event & OHCI1394_respTxComplete)
1033 tasklet_schedule(&ohci->at_response_ctx.tasklet);
1034
Kristian Høgsbergc8894752007-02-16 17:34:36 -05001035 iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
Kristian Høgsberged568912006-12-19 19:58:35 -05001036 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
1037
1038 while (iso_event) {
1039 i = ffs(iso_event) - 1;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001040 tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001041 iso_event &= ~(1 << i);
1042 }
1043
Kristian Høgsbergc8894752007-02-16 17:34:36 -05001044 iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
Kristian Høgsberged568912006-12-19 19:58:35 -05001045 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
1046
1047 while (iso_event) {
1048 i = ffs(iso_event) - 1;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001049 tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001050 iso_event &= ~(1 << i);
1051 }
1052
Stefan Richtere524f6162007-08-20 21:58:30 +02001053 if (unlikely(event & OHCI1394_postedWriteErr))
1054 fw_error("PCI posted write error\n");
1055
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001056 if (event & OHCI1394_cycle64Seconds) {
1057 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1058 if ((cycle_time & 0x80000000) == 0)
1059 ohci->bus_seconds++;
1060 }
1061
Kristian Høgsberged568912006-12-19 19:58:35 -05001062 return IRQ_HANDLED;
1063}
1064
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001065static int software_reset(struct fw_ohci *ohci)
1066{
1067 int i;
1068
1069 reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
1070
1071 for (i = 0; i < OHCI_LOOP_COUNT; i++) {
1072 if ((reg_read(ohci, OHCI1394_HCControlSet) &
1073 OHCI1394_HCControl_softReset) == 0)
1074 return 0;
1075 msleep(1);
1076 }
1077
1078 return -EBUSY;
1079}
1080
Kristian Høgsberged568912006-12-19 19:58:35 -05001081static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
1082{
1083 struct fw_ohci *ohci = fw_ohci(card);
1084 struct pci_dev *dev = to_pci_dev(card->device);
1085
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001086 if (software_reset(ohci)) {
1087 fw_error("Failed to reset ohci card.\n");
1088 return -EBUSY;
1089 }
1090
1091 /*
1092 * Now enable LPS, which we need in order to start accessing
1093 * most of the registers. In fact, on some cards (ALI M5251),
1094 * accessing registers in the SClk domain without LPS enabled
1095 * will lock up the machine. Wait 50msec to make sure we have
1096 * full link enabled.
1097 */
1098 reg_write(ohci, OHCI1394_HCControlSet,
1099 OHCI1394_HCControl_LPS |
1100 OHCI1394_HCControl_postedWriteEnable);
1101 flush_writes(ohci);
1102 msleep(50);
1103
1104 reg_write(ohci, OHCI1394_HCControlClear,
1105 OHCI1394_HCControl_noByteSwapData);
1106
1107 reg_write(ohci, OHCI1394_LinkControlSet,
1108 OHCI1394_LinkControl_rcvSelfID |
1109 OHCI1394_LinkControl_cycleTimerEnable |
1110 OHCI1394_LinkControl_cycleMaster);
1111
1112 reg_write(ohci, OHCI1394_ATRetries,
1113 OHCI1394_MAX_AT_REQ_RETRIES |
1114 (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
1115 (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
1116
1117 ar_context_run(&ohci->ar_request_ctx);
1118 ar_context_run(&ohci->ar_response_ctx);
1119
1120 reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
1121 reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
1122 reg_write(ohci, OHCI1394_IntEventClear, ~0);
1123 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1124 reg_write(ohci, OHCI1394_IntMaskSet,
1125 OHCI1394_selfIDComplete |
1126 OHCI1394_RQPkt | OHCI1394_RSPkt |
1127 OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
1128 OHCI1394_isochRx | OHCI1394_isochTx |
Stefan Richtere524f6162007-08-20 21:58:30 +02001129 OHCI1394_postedWriteErr | OHCI1394_cycle64Seconds |
1130 OHCI1394_masterIntEnable);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001131
1132 /* Activate link_on bit and contender bit in our self ID packets.*/
1133 if (ohci_update_phy_reg(card, 4, 0,
1134 PHY_LINK_ACTIVE | PHY_CONTENDER) < 0)
1135 return -EIO;
1136
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001137 /*
1138 * When the link is not yet enabled, the atomic config rom
Kristian Høgsberged568912006-12-19 19:58:35 -05001139 * update mechanism described below in ohci_set_config_rom()
1140 * is not active. We have to update ConfigRomHeader and
1141 * BusOptions manually, and the write to ConfigROMmap takes
1142 * effect immediately. We tie this to the enabling of the
1143 * link, so we have a valid config rom before enabling - the
1144 * OHCI requires that ConfigROMhdr and BusOptions have valid
1145 * values before enabling.
1146 *
1147 * However, when the ConfigROMmap is written, some controllers
1148 * always read back quadlets 0 and 2 from the config rom to
1149 * the ConfigRomHeader and BusOptions registers on bus reset.
1150 * They shouldn't do that in this initial case where the link
1151 * isn't enabled. This means we have to use the same
1152 * workaround here, setting the bus header to 0 and then write
1153 * the right values in the bus reset tasklet.
1154 */
1155
1156 ohci->next_config_rom =
1157 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1158 &ohci->next_config_rom_bus, GFP_KERNEL);
1159 if (ohci->next_config_rom == NULL)
1160 return -ENOMEM;
1161
1162 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1163 fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
1164
1165 ohci->next_header = config_rom[0];
1166 ohci->next_config_rom[0] = 0;
1167 reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
1168 reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
1169 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
1170
1171 reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
1172
1173 if (request_irq(dev->irq, irq_handler,
Thomas Gleixner65efffa2007-03-05 18:19:51 -08001174 IRQF_SHARED, ohci_driver_name, ohci)) {
Kristian Høgsberged568912006-12-19 19:58:35 -05001175 fw_error("Failed to allocate shared interrupt %d.\n",
1176 dev->irq);
1177 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1178 ohci->config_rom, ohci->config_rom_bus);
1179 return -EIO;
1180 }
1181
1182 reg_write(ohci, OHCI1394_HCControlSet,
1183 OHCI1394_HCControl_linkEnable |
1184 OHCI1394_HCControl_BIBimageValid);
1185 flush_writes(ohci);
1186
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001187 /*
1188 * We are ready to go, initiate bus reset to finish the
1189 * initialization.
1190 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001191
1192 fw_core_initiate_bus_reset(&ohci->card, 1);
1193
1194 return 0;
1195}
1196
1197static int
1198ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
1199{
1200 struct fw_ohci *ohci;
1201 unsigned long flags;
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001202 int retval = -EBUSY;
Kristian Høgsberged568912006-12-19 19:58:35 -05001203 __be32 *next_config_rom;
1204 dma_addr_t next_config_rom_bus;
1205
1206 ohci = fw_ohci(card);
1207
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001208 /*
1209 * When the OHCI controller is enabled, the config rom update
Kristian Høgsberged568912006-12-19 19:58:35 -05001210 * mechanism is a bit tricky, but easy enough to use. See
1211 * section 5.5.6 in the OHCI specification.
1212 *
1213 * The OHCI controller caches the new config rom address in a
1214 * shadow register (ConfigROMmapNext) and needs a bus reset
1215 * for the changes to take place. When the bus reset is
1216 * detected, the controller loads the new values for the
1217 * ConfigRomHeader and BusOptions registers from the specified
1218 * config rom and loads ConfigROMmap from the ConfigROMmapNext
1219 * shadow register. All automatically and atomically.
1220 *
1221 * Now, there's a twist to this story. The automatic load of
1222 * ConfigRomHeader and BusOptions doesn't honor the
1223 * noByteSwapData bit, so with a be32 config rom, the
1224 * controller will load be32 values in to these registers
1225 * during the atomic update, even on litte endian
1226 * architectures. The workaround we use is to put a 0 in the
1227 * header quadlet; 0 is endian agnostic and means that the
1228 * config rom isn't ready yet. In the bus reset tasklet we
1229 * then set up the real values for the two registers.
1230 *
1231 * We use ohci->lock to avoid racing with the code that sets
1232 * ohci->next_config_rom to NULL (see bus_reset_tasklet).
1233 */
1234
1235 next_config_rom =
1236 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1237 &next_config_rom_bus, GFP_KERNEL);
1238 if (next_config_rom == NULL)
1239 return -ENOMEM;
1240
1241 spin_lock_irqsave(&ohci->lock, flags);
1242
1243 if (ohci->next_config_rom == NULL) {
1244 ohci->next_config_rom = next_config_rom;
1245 ohci->next_config_rom_bus = next_config_rom_bus;
1246
1247 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1248 fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
1249 length * 4);
1250
1251 ohci->next_header = config_rom[0];
1252 ohci->next_config_rom[0] = 0;
1253
1254 reg_write(ohci, OHCI1394_ConfigROMmap,
1255 ohci->next_config_rom_bus);
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001256 retval = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001257 }
1258
1259 spin_unlock_irqrestore(&ohci->lock, flags);
1260
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001261 /*
1262 * Now initiate a bus reset to have the changes take
Kristian Høgsberged568912006-12-19 19:58:35 -05001263 * effect. We clean up the old config rom memory and DMA
1264 * mappings in the bus reset tasklet, since the OHCI
1265 * controller could need to access it before the bus reset
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001266 * takes effect.
1267 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001268 if (retval == 0)
1269 fw_core_initiate_bus_reset(&ohci->card, 1);
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001270 else
1271 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1272 next_config_rom, next_config_rom_bus);
Kristian Høgsberged568912006-12-19 19:58:35 -05001273
1274 return retval;
1275}
1276
1277static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
1278{
1279 struct fw_ohci *ohci = fw_ohci(card);
1280
1281 at_context_transmit(&ohci->at_request_ctx, packet);
1282}
1283
1284static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
1285{
1286 struct fw_ohci *ohci = fw_ohci(card);
1287
1288 at_context_transmit(&ohci->at_response_ctx, packet);
1289}
1290
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001291static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
1292{
1293 struct fw_ohci *ohci = fw_ohci(card);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001294 struct context *ctx = &ohci->at_request_ctx;
1295 struct driver_data *driver_data = packet->driver_data;
1296 int retval = -ENOENT;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001297
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001298 tasklet_disable(&ctx->tasklet);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001299
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001300 if (packet->ack != 0)
1301 goto out;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001302
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001303 driver_data->packet = NULL;
1304 packet->ack = RCODE_CANCELLED;
1305 packet->callback(packet, &ohci->card, packet->ack);
1306 retval = 0;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001307
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001308 out:
1309 tasklet_enable(&ctx->tasklet);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001310
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001311 return retval;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001312}
1313
Kristian Høgsberged568912006-12-19 19:58:35 -05001314static int
1315ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
1316{
1317 struct fw_ohci *ohci = fw_ohci(card);
1318 unsigned long flags;
Stefan Richter907293d2007-01-23 21:11:43 +01001319 int n, retval = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001320
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001321 /*
1322 * FIXME: Make sure this bitmask is cleared when we clear the busReset
1323 * interrupt bit. Clear physReqResourceAllBuses on bus reset.
1324 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001325
1326 spin_lock_irqsave(&ohci->lock, flags);
1327
1328 if (ohci->generation != generation) {
1329 retval = -ESTALE;
1330 goto out;
1331 }
1332
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001333 /*
1334 * Note, if the node ID contains a non-local bus ID, physical DMA is
1335 * enabled for _all_ nodes on remote buses.
1336 */
Stefan Richter907293d2007-01-23 21:11:43 +01001337
1338 n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
1339 if (n < 32)
1340 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
1341 else
1342 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
1343
Kristian Høgsberged568912006-12-19 19:58:35 -05001344 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -05001345 out:
Stefan Richter6cad95f2007-01-21 20:46:45 +01001346 spin_unlock_irqrestore(&ohci->lock, flags);
Kristian Høgsberged568912006-12-19 19:58:35 -05001347 return retval;
1348}
Stefan Richter373b2ed2007-03-04 14:45:18 +01001349
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001350static u64
1351ohci_get_bus_time(struct fw_card *card)
1352{
1353 struct fw_ohci *ohci = fw_ohci(card);
1354 u32 cycle_time;
1355 u64 bus_time;
1356
1357 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1358 bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
1359
1360 return bus_time;
1361}
1362
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001363static int handle_ir_dualbuffer_packet(struct context *context,
1364 struct descriptor *d,
1365 struct descriptor *last)
Kristian Høgsberged568912006-12-19 19:58:35 -05001366{
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001367 struct iso_context *ctx =
1368 container_of(context, struct iso_context, context);
1369 struct db_descriptor *db = (struct db_descriptor *) d;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001370 __le32 *ir_header;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001371 size_t header_length;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001372 void *p, *end;
1373 int i;
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001374
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001375 if (db->first_res_count > 0 && db->second_res_count > 0)
1376 /* This descriptor isn't done yet, stop iteration. */
1377 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001378
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001379 header_length = le16_to_cpu(db->first_req_count) -
1380 le16_to_cpu(db->first_res_count);
1381
1382 i = ctx->header_length;
1383 p = db + 1;
1384 end = p + header_length;
1385 while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001386 /*
1387 * The iso header is byteswapped to little endian by
Kristian Høgsberg15536222007-04-10 18:11:16 -04001388 * the controller, but the remaining header quadlets
1389 * are big endian. We want to present all the headers
1390 * as big endian, so we have to swap the first
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001391 * quadlet.
1392 */
Kristian Høgsberg15536222007-04-10 18:11:16 -04001393 *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
1394 memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001395 i += ctx->base.header_size;
1396 p += ctx->base.header_size + 4;
1397 }
1398
1399 ctx->header_length = i;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001400
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001401 if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001402 ir_header = (__le32 *) (db + 1);
1403 ctx->base.callback(&ctx->base,
1404 le32_to_cpu(ir_header[0]) & 0xffff,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001405 ctx->header_length, ctx->header,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001406 ctx->base.callback_data);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001407 ctx->header_length = 0;
1408 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001409
1410 return 1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001411}
1412
Kristian Høgsberg30200732007-02-16 17:34:39 -05001413static int handle_it_packet(struct context *context,
1414 struct descriptor *d,
1415 struct descriptor *last)
Kristian Høgsberged568912006-12-19 19:58:35 -05001416{
Kristian Høgsberg30200732007-02-16 17:34:39 -05001417 struct iso_context *ctx =
1418 container_of(context, struct iso_context, context);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001419
Kristian Høgsberg30200732007-02-16 17:34:39 -05001420 if (last->transfer_status == 0)
1421 /* This descriptor isn't done yet, stop iteration. */
1422 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001423
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001424 if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001425 ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
1426 0, NULL, ctx->base.callback_data);
Kristian Høgsberged568912006-12-19 19:58:35 -05001427
Kristian Høgsberg30200732007-02-16 17:34:39 -05001428 return 1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001429}
1430
Kristian Høgsberg30200732007-02-16 17:34:39 -05001431static struct fw_iso_context *
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001432ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
Kristian Høgsberged568912006-12-19 19:58:35 -05001433{
1434 struct fw_ohci *ohci = fw_ohci(card);
1435 struct iso_context *ctx, *list;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001436 descriptor_callback_t callback;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001437 u32 *mask, regs;
Kristian Høgsberged568912006-12-19 19:58:35 -05001438 unsigned long flags;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001439 int index, retval = -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001440
1441 if (type == FW_ISO_CONTEXT_TRANSMIT) {
1442 mask = &ohci->it_context_mask;
1443 list = ohci->it_context_list;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001444 callback = handle_it_packet;
Kristian Høgsberged568912006-12-19 19:58:35 -05001445 } else {
Stefan Richter373b2ed2007-03-04 14:45:18 +01001446 mask = &ohci->ir_context_mask;
1447 list = ohci->ir_context_list;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001448 callback = handle_ir_dualbuffer_packet;
Kristian Høgsberged568912006-12-19 19:58:35 -05001449 }
1450
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001451 /* FIXME: We need a fallback for pre 1.1 OHCI. */
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001452 if (callback == handle_ir_dualbuffer_packet &&
1453 ohci->version < OHCI_VERSION_1_1)
1454 return ERR_PTR(-EINVAL);
1455
Kristian Høgsberged568912006-12-19 19:58:35 -05001456 spin_lock_irqsave(&ohci->lock, flags);
1457 index = ffs(*mask) - 1;
1458 if (index >= 0)
1459 *mask &= ~(1 << index);
1460 spin_unlock_irqrestore(&ohci->lock, flags);
1461
1462 if (index < 0)
1463 return ERR_PTR(-EBUSY);
1464
Stefan Richter373b2ed2007-03-04 14:45:18 +01001465 if (type == FW_ISO_CONTEXT_TRANSMIT)
1466 regs = OHCI1394_IsoXmitContextBase(index);
1467 else
1468 regs = OHCI1394_IsoRcvContextBase(index);
1469
Kristian Høgsberged568912006-12-19 19:58:35 -05001470 ctx = &list[index];
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001471 memset(ctx, 0, sizeof(*ctx));
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001472 ctx->header_length = 0;
1473 ctx->header = (void *) __get_free_page(GFP_KERNEL);
1474 if (ctx->header == NULL)
1475 goto out;
1476
Kristian Høgsberg30200732007-02-16 17:34:39 -05001477 retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001478 regs, callback);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001479 if (retval < 0)
1480 goto out_with_header;
Kristian Høgsberged568912006-12-19 19:58:35 -05001481
1482 return &ctx->base;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001483
1484 out_with_header:
1485 free_page((unsigned long)ctx->header);
1486 out:
1487 spin_lock_irqsave(&ohci->lock, flags);
1488 *mask |= 1 << index;
1489 spin_unlock_irqrestore(&ohci->lock, flags);
1490
1491 return ERR_PTR(retval);
Kristian Høgsberged568912006-12-19 19:58:35 -05001492}
1493
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001494static int ohci_start_iso(struct fw_iso_context *base,
1495 s32 cycle, u32 sync, u32 tags)
Kristian Høgsberged568912006-12-19 19:58:35 -05001496{
Stefan Richter373b2ed2007-03-04 14:45:18 +01001497 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberg30200732007-02-16 17:34:39 -05001498 struct fw_ohci *ohci = ctx->context.ohci;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001499 u32 control, match;
Kristian Høgsberged568912006-12-19 19:58:35 -05001500 int index;
1501
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001502 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1503 index = ctx - ohci->it_context_list;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001504 match = 0;
1505 if (cycle >= 0)
1506 match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001507 (cycle & 0x7fff) << 16;
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05001508
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001509 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
1510 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001511 context_run(&ctx->context, match);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001512 } else {
1513 index = ctx - ohci->ir_context_list;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001514 control = IR_CONTEXT_DUAL_BUFFER_MODE | IR_CONTEXT_ISOCH_HEADER;
1515 match = (tags << 28) | (sync << 8) | ctx->base.channel;
1516 if (cycle >= 0) {
1517 match |= (cycle & 0x07fff) << 12;
1518 control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
1519 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001520
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001521 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
1522 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001523 reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001524 context_run(&ctx->context, control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001525 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001526
1527 return 0;
1528}
1529
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001530static int ohci_stop_iso(struct fw_iso_context *base)
1531{
1532 struct fw_ohci *ohci = fw_ohci(base->card);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001533 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001534 int index;
1535
1536 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1537 index = ctx - ohci->it_context_list;
1538 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
1539 } else {
1540 index = ctx - ohci->ir_context_list;
1541 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
1542 }
1543 flush_writes(ohci);
1544 context_stop(&ctx->context);
1545
1546 return 0;
1547}
1548
Kristian Høgsberged568912006-12-19 19:58:35 -05001549static void ohci_free_iso_context(struct fw_iso_context *base)
1550{
1551 struct fw_ohci *ohci = fw_ohci(base->card);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001552 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberged568912006-12-19 19:58:35 -05001553 unsigned long flags;
1554 int index;
1555
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001556 ohci_stop_iso(base);
1557 context_release(&ctx->context);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001558 free_page((unsigned long)ctx->header);
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001559
Kristian Høgsberged568912006-12-19 19:58:35 -05001560 spin_lock_irqsave(&ohci->lock, flags);
1561
1562 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1563 index = ctx - ohci->it_context_list;
Kristian Høgsberged568912006-12-19 19:58:35 -05001564 ohci->it_context_mask |= 1 << index;
1565 } else {
1566 index = ctx - ohci->ir_context_list;
Kristian Høgsberged568912006-12-19 19:58:35 -05001567 ohci->ir_context_mask |= 1 << index;
1568 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001569
1570 spin_unlock_irqrestore(&ohci->lock, flags);
1571}
1572
1573static int
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001574ohci_queue_iso_transmit(struct fw_iso_context *base,
1575 struct fw_iso_packet *packet,
1576 struct fw_iso_buffer *buffer,
1577 unsigned long payload)
Kristian Høgsberged568912006-12-19 19:58:35 -05001578{
Stefan Richter373b2ed2007-03-04 14:45:18 +01001579 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberg30200732007-02-16 17:34:39 -05001580 struct descriptor *d, *last, *pd;
Kristian Høgsberged568912006-12-19 19:58:35 -05001581 struct fw_iso_packet *p;
1582 __le32 *header;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001583 dma_addr_t d_bus, page_bus;
Kristian Høgsberged568912006-12-19 19:58:35 -05001584 u32 z, header_z, payload_z, irq;
1585 u32 payload_index, payload_end_index, next_page_index;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001586 int page, end_page, i, length, offset;
Kristian Høgsberged568912006-12-19 19:58:35 -05001587
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001588 /*
1589 * FIXME: Cycle lost behavior should be configurable: lose
1590 * packet, retransmit or terminate..
1591 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001592
1593 p = packet;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001594 payload_index = payload;
Kristian Høgsberged568912006-12-19 19:58:35 -05001595
1596 if (p->skip)
1597 z = 1;
1598 else
1599 z = 2;
1600 if (p->header_length > 0)
1601 z++;
1602
1603 /* Determine the first page the payload isn't contained in. */
1604 end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
1605 if (p->payload_length > 0)
1606 payload_z = end_page - (payload_index >> PAGE_SHIFT);
1607 else
1608 payload_z = 0;
1609
1610 z += payload_z;
1611
1612 /* Get header size in number of descriptors. */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001613 header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
Kristian Høgsberged568912006-12-19 19:58:35 -05001614
Kristian Høgsberg30200732007-02-16 17:34:39 -05001615 d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
1616 if (d == NULL)
1617 return -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001618
1619 if (!p->skip) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001620 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
Kristian Høgsberged568912006-12-19 19:58:35 -05001621 d[0].req_count = cpu_to_le16(8);
1622
1623 header = (__le32 *) &d[1];
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001624 header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
1625 IT_HEADER_TAG(p->tag) |
1626 IT_HEADER_TCODE(TCODE_STREAM_DATA) |
1627 IT_HEADER_CHANNEL(ctx->base.channel) |
1628 IT_HEADER_SPEED(ctx->base.speed));
Kristian Høgsberged568912006-12-19 19:58:35 -05001629 header[1] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001630 cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
Kristian Høgsberged568912006-12-19 19:58:35 -05001631 p->payload_length));
1632 }
1633
1634 if (p->header_length > 0) {
1635 d[2].req_count = cpu_to_le16(p->header_length);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001636 d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
Kristian Høgsberged568912006-12-19 19:58:35 -05001637 memcpy(&d[z], p->header, p->header_length);
1638 }
1639
1640 pd = d + z - payload_z;
1641 payload_end_index = payload_index + p->payload_length;
1642 for (i = 0; i < payload_z; i++) {
1643 page = payload_index >> PAGE_SHIFT;
1644 offset = payload_index & ~PAGE_MASK;
1645 next_page_index = (page + 1) << PAGE_SHIFT;
1646 length =
1647 min(next_page_index, payload_end_index) - payload_index;
1648 pd[i].req_count = cpu_to_le16(length);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001649
1650 page_bus = page_private(buffer->pages[page]);
1651 pd[i].data_address = cpu_to_le32(page_bus + offset);
Kristian Høgsberged568912006-12-19 19:58:35 -05001652
1653 payload_index += length;
1654 }
1655
Kristian Høgsberged568912006-12-19 19:58:35 -05001656 if (p->interrupt)
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001657 irq = DESCRIPTOR_IRQ_ALWAYS;
Kristian Høgsberged568912006-12-19 19:58:35 -05001658 else
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001659 irq = DESCRIPTOR_NO_IRQ;
Kristian Høgsberged568912006-12-19 19:58:35 -05001660
Kristian Høgsberg30200732007-02-16 17:34:39 -05001661 last = z == 2 ? d : d + z - 1;
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001662 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
1663 DESCRIPTOR_STATUS |
1664 DESCRIPTOR_BRANCH_ALWAYS |
Kristian Høgsbergcbb59da2007-02-16 17:34:35 -05001665 irq);
Kristian Høgsberged568912006-12-19 19:58:35 -05001666
Kristian Høgsberg30200732007-02-16 17:34:39 -05001667 context_append(&ctx->context, d, z, header_z);
Kristian Høgsberged568912006-12-19 19:58:35 -05001668
1669 return 0;
1670}
Stefan Richter373b2ed2007-03-04 14:45:18 +01001671
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001672static int
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001673ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
1674 struct fw_iso_packet *packet,
1675 struct fw_iso_buffer *buffer,
1676 unsigned long payload)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001677{
1678 struct iso_context *ctx = container_of(base, struct iso_context, base);
1679 struct db_descriptor *db = NULL;
1680 struct descriptor *d;
1681 struct fw_iso_packet *p;
1682 dma_addr_t d_bus, page_bus;
1683 u32 z, header_z, length, rest;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001684 int page, offset, packet_count, header_size;
Stefan Richter373b2ed2007-03-04 14:45:18 +01001685
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001686 /*
1687 * FIXME: Cycle lost behavior should be configurable: lose
1688 * packet, retransmit or terminate..
1689 */
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001690
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001691 if (packet->skip) {
1692 d = context_get_descriptors(&ctx->context, 2, &d_bus);
1693 if (d == NULL)
1694 return -ENOMEM;
1695
1696 db = (struct db_descriptor *) d;
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001697 db->control = cpu_to_le16(DESCRIPTOR_STATUS |
1698 DESCRIPTOR_BRANCH_ALWAYS |
1699 DESCRIPTOR_WAIT);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001700 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
1701 context_append(&ctx->context, d, 2, 0);
1702 }
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001703
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001704 p = packet;
1705 z = 2;
1706
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001707 /*
1708 * The OHCI controller puts the status word in the header
1709 * buffer too, so we need 4 extra bytes per packet.
1710 */
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001711 packet_count = p->header_length / ctx->base.header_size;
1712 header_size = packet_count * (ctx->base.header_size + 4);
1713
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001714 /* Get header size in number of descriptors. */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001715 header_z = DIV_ROUND_UP(header_size, sizeof(*d));
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001716 page = payload >> PAGE_SHIFT;
1717 offset = payload & ~PAGE_MASK;
1718 rest = p->payload_length;
1719
1720 /* FIXME: OHCI 1.0 doesn't support dual buffer receive */
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001721 /* FIXME: make packet-per-buffer/dual-buffer a context option */
1722 while (rest > 0) {
1723 d = context_get_descriptors(&ctx->context,
1724 z + header_z, &d_bus);
1725 if (d == NULL)
1726 return -ENOMEM;
1727
1728 db = (struct db_descriptor *) d;
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001729 db->control = cpu_to_le16(DESCRIPTOR_STATUS |
1730 DESCRIPTOR_BRANCH_ALWAYS);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001731 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
1732 db->first_req_count = cpu_to_le16(header_size);
Kristian Høgsberg1e1d1962007-02-16 17:34:45 -05001733 db->first_res_count = db->first_req_count;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001734 db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
Stefan Richter373b2ed2007-03-04 14:45:18 +01001735
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001736 if (offset + rest < PAGE_SIZE)
1737 length = rest;
1738 else
1739 length = PAGE_SIZE - offset;
1740
Kristian Høgsberg1e1d1962007-02-16 17:34:45 -05001741 db->second_req_count = cpu_to_le16(length);
1742 db->second_res_count = db->second_req_count;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001743 page_bus = page_private(buffer->pages[page]);
1744 db->second_buffer = cpu_to_le32(page_bus + offset);
1745
Kristian Høgsbergcb2d2cd2007-02-16 17:34:47 -05001746 if (p->interrupt && length == rest)
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001747 db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
Kristian Høgsbergcb2d2cd2007-02-16 17:34:47 -05001748
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001749 context_append(&ctx->context, d, z, header_z);
1750 offset = (offset + length) & ~PAGE_MASK;
1751 rest -= length;
1752 page++;
1753 }
1754
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001755 return 0;
1756}
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05001757
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001758static int
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001759ohci_queue_iso(struct fw_iso_context *base,
1760 struct fw_iso_packet *packet,
1761 struct fw_iso_buffer *buffer,
1762 unsigned long payload)
1763{
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001764 struct iso_context *ctx = container_of(base, struct iso_context, base);
1765
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001766 if (base->type == FW_ISO_CONTEXT_TRANSMIT)
1767 return ohci_queue_iso_transmit(base, packet, buffer, payload);
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001768 else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001769 return ohci_queue_iso_receive_dualbuffer(base, packet,
1770 buffer, payload);
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001771 else
1772 /* FIXME: Implement fallback for OHCI 1.0 controllers. */
1773 return -EINVAL;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001774}
1775
Stefan Richter21ebcd12007-01-14 15:29:07 +01001776static const struct fw_card_driver ohci_driver = {
Kristian Høgsberged568912006-12-19 19:58:35 -05001777 .name = ohci_driver_name,
1778 .enable = ohci_enable,
1779 .update_phy_reg = ohci_update_phy_reg,
1780 .set_config_rom = ohci_set_config_rom,
1781 .send_request = ohci_send_request,
1782 .send_response = ohci_send_response,
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001783 .cancel_packet = ohci_cancel_packet,
Kristian Høgsberged568912006-12-19 19:58:35 -05001784 .enable_phys_dma = ohci_enable_phys_dma,
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001785 .get_bus_time = ohci_get_bus_time,
Kristian Høgsberged568912006-12-19 19:58:35 -05001786
1787 .allocate_iso_context = ohci_allocate_iso_context,
1788 .free_iso_context = ohci_free_iso_context,
1789 .queue_iso = ohci_queue_iso,
Kristian Høgsberg69cdb722007-02-16 17:34:41 -05001790 .start_iso = ohci_start_iso,
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001791 .stop_iso = ohci_stop_iso,
Kristian Høgsberged568912006-12-19 19:58:35 -05001792};
1793
Kristian Høgsberged568912006-12-19 19:58:35 -05001794static int __devinit
1795pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
1796{
1797 struct fw_ohci *ohci;
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001798 u32 bus_options, max_receive, link_speed;
Kristian Høgsberged568912006-12-19 19:58:35 -05001799 u64 guid;
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001800 int err;
Kristian Høgsberged568912006-12-19 19:58:35 -05001801 size_t size;
1802
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001803 ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
Kristian Høgsberged568912006-12-19 19:58:35 -05001804 if (ohci == NULL) {
1805 fw_error("Could not malloc fw_ohci data.\n");
1806 return -ENOMEM;
1807 }
1808
1809 fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
1810
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001811 err = pci_enable_device(dev);
1812 if (err) {
Kristian Høgsberged568912006-12-19 19:58:35 -05001813 fw_error("Failed to enable OHCI hardware.\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001814 goto fail_put_card;
Kristian Høgsberged568912006-12-19 19:58:35 -05001815 }
1816
1817 pci_set_master(dev);
1818 pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
1819 pci_set_drvdata(dev, ohci);
1820
1821 spin_lock_init(&ohci->lock);
1822
1823 tasklet_init(&ohci->bus_reset_tasklet,
1824 bus_reset_tasklet, (unsigned long)ohci);
1825
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001826 err = pci_request_region(dev, 0, ohci_driver_name);
1827 if (err) {
Kristian Høgsberged568912006-12-19 19:58:35 -05001828 fw_error("MMIO resource unavailable\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001829 goto fail_disable;
Kristian Høgsberged568912006-12-19 19:58:35 -05001830 }
1831
1832 ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
1833 if (ohci->registers == NULL) {
1834 fw_error("Failed to remap registers\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001835 err = -ENXIO;
1836 goto fail_iomem;
Kristian Høgsberged568912006-12-19 19:58:35 -05001837 }
1838
Kristian Høgsberged568912006-12-19 19:58:35 -05001839 ar_context_init(&ohci->ar_request_ctx, ohci,
1840 OHCI1394_AsReqRcvContextControlSet);
1841
1842 ar_context_init(&ohci->ar_response_ctx, ohci,
1843 OHCI1394_AsRspRcvContextControlSet);
1844
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001845 context_init(&ohci->at_request_ctx, ohci, AT_BUFFER_SIZE,
1846 OHCI1394_AsReqTrContextControlSet, handle_at_packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001847
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001848 context_init(&ohci->at_response_ctx, ohci, AT_BUFFER_SIZE,
1849 OHCI1394_AsRspTrContextControlSet, handle_at_packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001850
Kristian Høgsberged568912006-12-19 19:58:35 -05001851 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
1852 ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
1853 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
1854 size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
1855 ohci->it_context_list = kzalloc(size, GFP_KERNEL);
1856
1857 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
1858 ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
1859 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
1860 size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
1861 ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
1862
1863 if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
1864 fw_error("Out of memory for it/ir contexts.\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001865 err = -ENOMEM;
1866 goto fail_registers;
Kristian Høgsberged568912006-12-19 19:58:35 -05001867 }
1868
1869 /* self-id dma buffer allocation */
1870 ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
1871 SELF_ID_BUF_SIZE,
1872 &ohci->self_id_bus,
1873 GFP_KERNEL);
1874 if (ohci->self_id_cpu == NULL) {
1875 fw_error("Out of memory for self ID buffer.\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001876 err = -ENOMEM;
1877 goto fail_registers;
Kristian Høgsberged568912006-12-19 19:58:35 -05001878 }
1879
Kristian Høgsberged568912006-12-19 19:58:35 -05001880 bus_options = reg_read(ohci, OHCI1394_BusOptions);
1881 max_receive = (bus_options >> 12) & 0xf;
1882 link_speed = bus_options & 0x7;
1883 guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
1884 reg_read(ohci, OHCI1394_GUIDLo);
1885
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001886 err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
1887 if (err < 0)
1888 goto fail_self_id;
Kristian Høgsberged568912006-12-19 19:58:35 -05001889
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001890 ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
Kristian Høgsberg500be722007-02-16 17:34:43 -05001891 fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
Kristian Høgsberge364cf42007-02-16 17:34:49 -05001892 dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
Kristian Høgsberged568912006-12-19 19:58:35 -05001893
1894 return 0;
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001895
1896 fail_self_id:
1897 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
1898 ohci->self_id_cpu, ohci->self_id_bus);
1899 fail_registers:
1900 kfree(ohci->it_context_list);
1901 kfree(ohci->ir_context_list);
1902 pci_iounmap(dev, ohci->registers);
1903 fail_iomem:
1904 pci_release_region(dev, 0);
1905 fail_disable:
1906 pci_disable_device(dev);
1907 fail_put_card:
1908 fw_card_put(&ohci->card);
1909
1910 return err;
Kristian Høgsberged568912006-12-19 19:58:35 -05001911}
1912
1913static void pci_remove(struct pci_dev *dev)
1914{
1915 struct fw_ohci *ohci;
1916
1917 ohci = pci_get_drvdata(dev);
Kristian Høgsberge254a4b2007-03-07 12:12:38 -05001918 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1919 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -05001920 fw_core_remove_card(&ohci->card);
1921
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001922 /*
1923 * FIXME: Fail all pending packets here, now that the upper
1924 * layers can't queue any more.
1925 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001926
1927 software_reset(ohci);
1928 free_irq(dev->irq, ohci);
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04001929 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
1930 ohci->self_id_cpu, ohci->self_id_bus);
1931 kfree(ohci->it_context_list);
1932 kfree(ohci->ir_context_list);
1933 pci_iounmap(dev, ohci->registers);
1934 pci_release_region(dev, 0);
1935 pci_disable_device(dev);
1936 fw_card_put(&ohci->card);
Kristian Høgsberged568912006-12-19 19:58:35 -05001937
1938 fw_notify("Removed fw-ohci device.\n");
1939}
1940
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001941#ifdef CONFIG_PM
1942static int pci_suspend(struct pci_dev *pdev, pm_message_t state)
1943{
1944 struct fw_ohci *ohci = pci_get_drvdata(pdev);
1945 int err;
1946
1947 software_reset(ohci);
1948 free_irq(pdev->irq, ohci);
1949 err = pci_save_state(pdev);
1950 if (err) {
Stefan Richter8a8cea22007-06-09 19:26:22 +02001951 fw_error("pci_save_state failed\n");
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001952 return err;
1953 }
1954 err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
Stefan Richter55111422007-09-06 09:50:30 +02001955 if (err)
1956 fw_error("pci_set_power_state failed with %d\n", err);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001957
1958 return 0;
1959}
1960
1961static int pci_resume(struct pci_dev *pdev)
1962{
1963 struct fw_ohci *ohci = pci_get_drvdata(pdev);
1964 int err;
1965
1966 pci_set_power_state(pdev, PCI_D0);
1967 pci_restore_state(pdev);
1968 err = pci_enable_device(pdev);
1969 if (err) {
Stefan Richter8a8cea22007-06-09 19:26:22 +02001970 fw_error("pci_enable_device failed\n");
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001971 return err;
1972 }
1973
1974 return ohci_enable(&ohci->card, ohci->config_rom, CONFIG_ROM_SIZE);
1975}
1976#endif
1977
Kristian Høgsberged568912006-12-19 19:58:35 -05001978static struct pci_device_id pci_table[] = {
1979 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
1980 { }
1981};
1982
1983MODULE_DEVICE_TABLE(pci, pci_table);
1984
1985static struct pci_driver fw_ohci_pci_driver = {
1986 .name = ohci_driver_name,
1987 .id_table = pci_table,
1988 .probe = pci_probe,
1989 .remove = pci_remove,
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001990#ifdef CONFIG_PM
1991 .resume = pci_resume,
1992 .suspend = pci_suspend,
1993#endif
Kristian Høgsberged568912006-12-19 19:58:35 -05001994};
1995
1996MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1997MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
1998MODULE_LICENSE("GPL");
1999
Olaf Hering1e4c7b02007-05-05 23:17:13 +02002000/* Provide a module alias so root-on-sbp2 initrds don't break. */
2001#ifndef CONFIG_IEEE1394_OHCI1394_MODULE
2002MODULE_ALIAS("ohci1394");
2003#endif
2004
Kristian Høgsberged568912006-12-19 19:58:35 -05002005static int __init fw_ohci_init(void)
2006{
2007 return pci_register_driver(&fw_ohci_pci_driver);
2008}
2009
2010static void __exit fw_ohci_cleanup(void)
2011{
2012 pci_unregister_driver(&fw_ohci_pci_driver);
2013}
2014
2015module_init(fw_ohci_init);
2016module_exit(fw_ohci_cleanup);