Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Driver for OHCI 1394 controllers |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 3 | * |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 4 | * 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 Morton | cf3e72f | 2006-12-27 14:36:37 -0800 | [diff] [blame] | 28 | #include <linux/dma-mapping.h> |
| 29 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 30 | #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øgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 48 | #define descriptor_wait (3 << 0) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 49 | |
| 50 | struct 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øgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 59 | struct 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øgsberg | 72e318e | 2007-02-06 14:49:31 -0500 | [diff] [blame] | 73 | #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øgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 78 | struct ar_buffer { |
| 79 | struct descriptor descriptor; |
| 80 | struct ar_buffer *next; |
| 81 | __le32 data[0]; |
| 82 | }; |
| 83 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 84 | struct ar_context { |
| 85 | struct fw_ohci *ohci; |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 86 | struct ar_buffer *current_buffer; |
| 87 | struct ar_buffer *last_buffer; |
| 88 | void *pointer; |
Kristian Høgsberg | 72e318e | 2007-02-06 14:49:31 -0500 | [diff] [blame] | 89 | u32 regs; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 90 | struct tasklet_struct tasklet; |
| 91 | }; |
| 92 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 93 | struct context; |
| 94 | |
| 95 | typedef int (*descriptor_callback_t)(struct context *ctx, |
| 96 | struct descriptor *d, |
| 97 | struct descriptor *last); |
| 98 | struct context { |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 99 | struct fw_ohci *ohci; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 100 | u32 regs; |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 101 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 102 | 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 Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 112 | struct tasklet_struct tasklet; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 113 | }; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 114 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 115 | #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 | |
| 122 | struct iso_context { |
| 123 | struct fw_iso_context base; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 124 | struct context context; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 125 | void *header; |
| 126 | size_t header_length; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | #define CONFIG_ROM_SIZE 1024 |
| 130 | |
| 131 | struct fw_ohci { |
| 132 | struct fw_card card; |
| 133 | |
Kristian Høgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 134 | u32 version; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 135 | __iomem char *registers; |
| 136 | dma_addr_t self_id_bus; |
| 137 | __le32 *self_id_cpu; |
| 138 | struct tasklet_struct bus_reset_tasklet; |
Kristian Høgsberg | e636fe2 | 2007-01-26 00:38:04 -0500 | [diff] [blame] | 139 | int node_id; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 140 | int generation; |
| 141 | int request_generation; |
Kristian Høgsberg | d60d7f1 | 2007-03-07 12:12:56 -0500 | [diff] [blame] | 142 | u32 bus_seconds; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 143 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 144 | /* |
| 145 | * Spinlock for accessing fw_ohci data. Never call out of |
| 146 | * this driver with this lock held. |
| 147 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 148 | 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øgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 160 | struct context at_request_ctx; |
| 161 | struct context at_response_ctx; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 162 | |
| 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 Bunk | 95688e9 | 2007-01-22 19:17:37 +0100 | [diff] [blame] | 169 | static inline struct fw_ohci *fw_ohci(struct fw_card *card) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 170 | { |
| 171 | return container_of(card, struct fw_ohci, card); |
| 172 | } |
| 173 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 174 | #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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 180 | |
| 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øgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 195 | #define OHCI_TCODE_PHY_PACKET 0x0e |
Kristian Høgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 196 | #define OHCI_VERSION_1_1 0x010010 |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 197 | #define ISO_BUFFER_SIZE (64 * 1024) |
| 198 | #define AT_BUFFER_SIZE 4096 |
Kristian Høgsberg | 0edeefd | 2007-01-26 00:38:49 -0500 | [diff] [blame] | 199 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 200 | static char ohci_driver_name[] = KBUILD_MODNAME; |
| 201 | |
Adrian Bunk | 95688e9 | 2007-01-22 19:17:37 +0100 | [diff] [blame] | 202 | static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 203 | { |
| 204 | writel(data, ohci->registers + offset); |
| 205 | } |
| 206 | |
Adrian Bunk | 95688e9 | 2007-01-22 19:17:37 +0100 | [diff] [blame] | 207 | static inline u32 reg_read(const struct fw_ohci *ohci, int offset) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 208 | { |
| 209 | return readl(ohci->registers + offset); |
| 210 | } |
| 211 | |
Adrian Bunk | 95688e9 | 2007-01-22 19:17:37 +0100 | [diff] [blame] | 212 | static inline void flush_writes(const struct fw_ohci *ohci) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 213 | { |
| 214 | /* Do a dummy read to flush writes. */ |
| 215 | reg_read(ohci, OHCI1394_Version); |
| 216 | } |
| 217 | |
| 218 | static int |
| 219 | ohci_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øgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 241 | static int ar_context_add_page(struct ar_context *ctx) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 242 | { |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 243 | 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øgsberg | 72e318e | 2007-02-06 14:49:31 -0500 | [diff] [blame] | 274 | reg_write(ctx->ohci, control_set(ctx->regs), CONTEXT_WAKE); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 275 | flush_writes(ctx->ohci); |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 276 | |
| 277 | return 0; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 278 | } |
| 279 | |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 280 | static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 281 | { |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 282 | struct fw_ohci *ohci = ctx->ohci; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 283 | struct fw_packet p; |
| 284 | u32 status, length, tcode; |
Kristian Høgsberg | 0edeefd | 2007-01-26 00:38:49 -0500 | [diff] [blame] | 285 | |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 286 | 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øgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 289 | |
| 290 | tcode = (p.header[0] >> 4) & 0x0f; |
| 291 | switch (tcode) { |
| 292 | case TCODE_WRITE_QUADLET_REQUEST: |
| 293 | case TCODE_READ_QUADLET_RESPONSE: |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 294 | p.header[3] = (__force __u32) buffer[3]; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 295 | p.header_length = 16; |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 296 | 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øgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 303 | break; |
| 304 | |
| 305 | case TCODE_WRITE_BLOCK_REQUEST: |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 306 | case TCODE_READ_BLOCK_RESPONSE: |
| 307 | case TCODE_LOCK_REQUEST: |
| 308 | case TCODE_LOCK_RESPONSE: |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 309 | p.header[3] = le32_to_cpu(buffer[3]); |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 310 | p.header_length = 16; |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 311 | p.payload_length = p.header[3] >> 16; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 312 | break; |
| 313 | |
| 314 | case TCODE_WRITE_RESPONSE: |
| 315 | case TCODE_READ_QUADLET_REQUEST: |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 316 | case OHCI_TCODE_PHY_PACKET: |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 317 | p.header_length = 12; |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 318 | p.payload_length = 0; |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 322 | 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 332 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 333 | /* |
| 334 | * The OHCI bus reset handler synthesizes a phy packet with |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 335 | * 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 340 | * request. |
| 341 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 342 | |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 343 | if (p.ack + 16 == 0x09) |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 344 | ohci->request_generation = (buffer[2] >> 16) & 0xff; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 345 | else if (ctx == &ohci->ar_request_ctx) |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 346 | fw_core_handle_request(&ohci->card, &p); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 347 | else |
Kristian Høgsberg | 2639a6f | 2007-01-26 00:37:57 -0500 | [diff] [blame] | 348 | fw_core_handle_response(&ohci->card, &p); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 349 | |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 350 | return buffer + length + 1; |
| 351 | } |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 352 | |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 353 | static 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 360 | |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 361 | ab = ctx->current_buffer; |
| 362 | d = &ab->descriptor; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 363 | |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 364 | if (d->res_count == 0) { |
| 365 | size_t size, rest, offset; |
| 366 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 367 | /* |
| 368 | * This descriptor is finished and we may have a |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 369 | * packet split across this and the next buffer. We |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 370 | * reuse the page for reassembling the split packet. |
| 371 | */ |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 372 | |
| 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static int |
Kristian Høgsberg | 72e318e | 2007-02-06 14:49:31 -0500 | [diff] [blame] | 405 | ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 406 | { |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 407 | struct ar_buffer ab; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 408 | |
Kristian Høgsberg | 72e318e | 2007-02-06 14:49:31 -0500 | [diff] [blame] | 409 | ctx->regs = regs; |
| 410 | ctx->ohci = ohci; |
| 411 | ctx->last_buffer = &ab; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 412 | tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx); |
| 413 | |
Kristian Høgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 414 | 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øgsberg | 72e318e | 2007-02-06 14:49:31 -0500 | [diff] [blame] | 419 | 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øgsberg | 32b4609 | 2007-02-06 14:49:30 -0500 | [diff] [blame] | 421 | flush_writes(ctx->ohci); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 425 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 426 | static 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 | |
| 454 | static int |
| 455 | context_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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 482 | /* |
| 483 | * We put a dummy descriptor in the buffer that has a NULL |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 484 | * 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 487 | * element so that head == tail means buffer full. |
| 488 | */ |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 489 | |
| 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øgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 498 | static void |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 499 | context_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 | |
| 508 | static struct descriptor * |
| 509 | context_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øgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 535 | static void context_run(struct context *ctx, u32 extra) |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 536 | { |
| 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øgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 542 | reg_write(ohci, control_set(ctx->regs), CONTEXT_RUN | extra); |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 543 | flush_writes(ohci); |
| 544 | } |
| 545 | |
| 546 | static 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 | |
| 564 | static void context_stop(struct context *ctx) |
| 565 | { |
| 566 | u32 reg; |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 567 | int i; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 568 | |
| 569 | reg_write(ctx->ohci, control_clear(ctx->regs), CONTEXT_RUN); |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 570 | flush_writes(ctx->ohci); |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 571 | |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 572 | 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øgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 580 | } |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 581 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 582 | struct driver_data { |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 583 | struct fw_packet *packet; |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 584 | }; |
| 585 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 586 | /* |
| 587 | * This function apppends a packet to the DMA queue for transmission. |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 588 | * Must always be called with the ochi->lock held to ensure proper |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 589 | * generation handling and locking around packet queue manipulation. |
| 590 | */ |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 591 | static int |
| 592 | at_context_queue_packet(struct context *ctx, struct fw_packet *packet) |
| 593 | { |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 594 | struct fw_ohci *ohci = ctx->ohci; |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 595 | dma_addr_t d_bus, payload_bus; |
| 596 | struct driver_data *driver_data; |
| 597 | struct descriptor *d, *last; |
| 598 | __le32 *header; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 599 | int z, tcode; |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 600 | u32 reg; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 601 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 602 | d = context_get_descriptors(ctx, 4, &d_bus); |
| 603 | if (d == NULL) { |
| 604 | packet->ack = RCODE_SEND_ERROR; |
| 605 | return -1; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 606 | } |
| 607 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 608 | d[0].control = cpu_to_le16(descriptor_key_immediate); |
| 609 | d[0].res_count = cpu_to_le16(packet->timestamp); |
| 610 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 611 | /* |
| 612 | * The DMA format for asyncronous link packets is different |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 613 | * from the IEEE1394 layout, so shift the fields around |
| 614 | * accordingly. If header_length is 8, it's a PHY packet, to |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 615 | * which we need to prepend an extra quadlet. |
| 616 | */ |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 617 | |
| 618 | header = (__le32 *) &d[1]; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 619 | if (packet->header_length > 8) { |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 620 | 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 625 | |
| 626 | tcode = (packet->header[0] >> 4) & 0x0f; |
| 627 | if (TCODE_IS_BLOCK_PACKET(tcode)) |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 628 | header[3] = cpu_to_le32(packet->header[3]); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 629 | else |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 630 | header[3] = (__force __le32) packet->header[3]; |
| 631 | |
| 632 | d[0].req_count = cpu_to_le16(packet->header_length); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 633 | } else { |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 634 | 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 639 | } |
| 640 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 641 | driver_data = (struct driver_data *) &d[3]; |
| 642 | driver_data->packet = packet; |
Kristian Høgsberg | 20d1167 | 2007-03-26 19:18:19 -0400 | [diff] [blame] | 643 | packet->driver_data = driver_data; |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 644 | |
| 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 667 | /* FIXME: Document how the locking works. */ |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 668 | if (ohci->generation != packet->generation) { |
| 669 | packet->ack = RCODE_GENERATION; |
| 670 | return -1; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 671 | } |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 672 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 673 | context_append(ctx, d, z, 4 - z); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 674 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 675 | /* If the context isn't already running, start it up. */ |
Kristian Høgsberg | 72e318e | 2007-02-06 14:49:31 -0500 | [diff] [blame] | 676 | reg = reg_read(ctx->ohci, control_set(ctx->regs)); |
Kristian Høgsberg | 053b308 | 2007-04-10 18:11:17 -0400 | [diff] [blame] | 677 | if ((reg & CONTEXT_RUN) == 0) |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 678 | context_run(ctx, 0); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 683 | static 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 718 | /* |
| 719 | * The packet was flushed should give same error as |
| 720 | * when we try to use a stale generation count. |
| 721 | */ |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 722 | packet->ack = RCODE_GENERATION; |
| 723 | break; |
| 724 | |
| 725 | case OHCI1394_evt_missing_ack: |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 726 | /* |
| 727 | * Using a valid (current) generation count, but the |
| 728 | * node is not on the bus or not sending acks. |
| 729 | */ |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 730 | 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øgsberg | e636fe2 | 2007-01-26 00:38:04 -0500 | [diff] [blame] | 753 | #define header_get_destination(q) (((q) >> 16) & 0xffff) |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 754 | #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 | |
| 759 | static void |
| 760 | handle_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 | |
| 786 | static void |
| 787 | handle_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 | |
| 828 | static void |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 829 | handle_local_request(struct context *ctx, struct fw_packet *packet) |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 830 | { |
| 831 | u64 offset; |
| 832 | u32 csr; |
| 833 | |
Kristian Høgsberg | 473d28c | 2007-03-07 12:12:55 -0500 | [diff] [blame] | 834 | if (ctx == &ctx->ohci->at_request_ctx) { |
| 835 | packet->ack = ACK_PENDING; |
| 836 | packet->callback(packet, &ctx->ohci->card, packet->ack); |
| 837 | } |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 838 | |
| 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øgsberg | 473d28c | 2007-03-07 12:12:55 -0500 | [diff] [blame] | 862 | |
| 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øgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 867 | } |
Kristian Høgsberg | e636fe2 | 2007-01-26 00:38:04 -0500 | [diff] [blame] | 868 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 869 | static void |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 870 | at_context_transmit(struct context *ctx, struct fw_packet *packet) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 871 | { |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 872 | unsigned long flags; |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 873 | int retval; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 874 | |
| 875 | spin_lock_irqsave(&ctx->ohci->lock, flags); |
| 876 | |
Kristian Høgsberg | e636fe2 | 2007-01-26 00:38:04 -0500 | [diff] [blame] | 877 | if (header_get_destination(packet->header[0]) == ctx->ohci->node_id && |
| 878 | ctx->ohci->generation == packet->generation) { |
Kristian Høgsberg | 93c4cce | 2007-01-26 00:38:26 -0500 | [diff] [blame] | 879 | spin_unlock_irqrestore(&ctx->ohci->lock, flags); |
| 880 | handle_local_request(ctx, packet); |
| 881 | return; |
Kristian Høgsberg | e636fe2 | 2007-01-26 00:38:04 -0500 | [diff] [blame] | 882 | } |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 883 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 884 | retval = at_context_queue_packet(ctx, packet); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 885 | spin_unlock_irqrestore(&ctx->ohci->lock, flags); |
| 886 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 887 | if (retval < 0) |
| 888 | packet->callback(packet, &ctx->ohci->card, packet->ack); |
| 889 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static void bus_reset_tasklet(unsigned long data) |
| 893 | { |
| 894 | struct fw_ohci *ohci = (struct fw_ohci *)data; |
Kristian Høgsberg | e636fe2 | 2007-01-26 00:38:04 -0500 | [diff] [blame] | 895 | int self_id_count, i, j, reg; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 896 | 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øgsberg | e636fe2 | 2007-01-26 00:38:04 -0500 | [diff] [blame] | 904 | ohci->node_id = reg & 0xffff; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 905 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 906 | /* |
| 907 | * The count in the SelfIDCount register is the number of |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 908 | * bytes in the self ID receive buffer. Since we also receive |
| 909 | * the inverted quadlets and a header quadlet, we shift one |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 910 | * bit extra to get the actual number of self IDs. |
| 911 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 912 | |
| 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 922 | /* |
| 923 | * Check the consistency of the self IDs we just read. The |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 924 | * 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 933 | * of self IDs. |
| 934 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 935 | |
| 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øgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 947 | context_stop(&ohci->at_request_ctx); |
| 948 | context_stop(&ohci->at_response_ctx); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 949 | reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset); |
| 950 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 951 | /* |
| 952 | * This next bit is unrelated to the AT context stuff but we |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 953 | * 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 957 | * next_config_rom pointer so a new udpate can take place. |
| 958 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 959 | |
| 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 967 | /* |
| 968 | * Restore config_rom image and manually update |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 969 | * config_rom registers. Writing the header quadlet |
| 970 | * will indicate that the config rom is ready, so we |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 971 | * do that last. |
| 972 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 973 | 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øgsberg | e636fe2 | 2007-01-26 00:38:04 -0500 | [diff] [blame] | 981 | fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation, |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 982 | self_id_count, ohci->self_id_buffer); |
| 983 | } |
| 984 | |
| 985 | static irqreturn_t irq_handler(int irq, void *data) |
| 986 | { |
| 987 | struct fw_ohci *ohci = data; |
Kristian Høgsberg | d60d7f1 | 2007-03-07 12:12:56 -0500 | [diff] [blame] | 988 | u32 event, iso_event, cycle_time; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 989 | 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øgsberg | c889475 | 2007-02-16 17:34:36 -0500 | [diff] [blame] | 1013 | iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1014 | reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event); |
| 1015 | |
| 1016 | while (iso_event) { |
| 1017 | i = ffs(iso_event) - 1; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1018 | tasklet_schedule(&ohci->ir_context_list[i].context.tasklet); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1019 | iso_event &= ~(1 << i); |
| 1020 | } |
| 1021 | |
Kristian Høgsberg | c889475 | 2007-02-16 17:34:36 -0500 | [diff] [blame] | 1022 | iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1023 | reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event); |
| 1024 | |
| 1025 | while (iso_event) { |
| 1026 | i = ffs(iso_event) - 1; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1027 | tasklet_schedule(&ohci->it_context_list[i].context.tasklet); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1028 | iso_event &= ~(1 << i); |
| 1029 | } |
| 1030 | |
Kristian Høgsberg | d60d7f1 | 2007-03-07 12:12:56 -0500 | [diff] [blame] | 1031 | 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1037 | return IRQ_HANDLED; |
| 1038 | } |
| 1039 | |
| 1040 | static 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1045 | /* |
| 1046 | * When the link is not yet enabled, the atomic config rom |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1047 | * 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 Gleixner | 65efffa | 2007-03-05 18:19:51 -0800 | [diff] [blame] | 1082 | IRQF_SHARED, ohci_driver_name, ohci)) { |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1083 | 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1095 | /* |
| 1096 | * We are ready to go, initiate bus reset to finish the |
| 1097 | * initialization. |
| 1098 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1099 | |
| 1100 | fw_core_initiate_bus_reset(&ohci->card, 1); |
| 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | static int |
| 1106 | ohci_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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1116 | /* |
| 1117 | * When the OHCI controller is enabled, the config rom update |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1118 | * 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1172 | /* |
| 1173 | * Now initiate a bus reset to have the changes take |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1174 | * 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1177 | * takes effect. |
| 1178 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1179 | if (retval == 0) |
| 1180 | fw_core_initiate_bus_reset(&ohci->card, 1); |
| 1181 | |
| 1182 | return retval; |
| 1183 | } |
| 1184 | |
| 1185 | static 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 | |
| 1192 | static 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øgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame] | 1199 | static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet) |
| 1200 | { |
| 1201 | struct fw_ohci *ohci = fw_ohci(card); |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 1202 | struct context *ctx = &ohci->at_request_ctx; |
| 1203 | struct driver_data *driver_data = packet->driver_data; |
| 1204 | int retval = -ENOENT; |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame] | 1205 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 1206 | tasklet_disable(&ctx->tasklet); |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame] | 1207 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 1208 | if (packet->ack != 0) |
| 1209 | goto out; |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame] | 1210 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 1211 | driver_data->packet = NULL; |
| 1212 | packet->ack = RCODE_CANCELLED; |
| 1213 | packet->callback(packet, &ohci->card, packet->ack); |
| 1214 | retval = 0; |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame] | 1215 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 1216 | out: |
| 1217 | tasklet_enable(&ctx->tasklet); |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame] | 1218 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 1219 | return retval; |
Kristian Høgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame] | 1220 | } |
| 1221 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1222 | static int |
| 1223 | ohci_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 Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 1227 | int n, retval = 0; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1228 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1229 | /* |
| 1230 | * FIXME: Make sure this bitmask is cleared when we clear the busReset |
| 1231 | * interrupt bit. Clear physReqResourceAllBuses on bus reset. |
| 1232 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1233 | |
| 1234 | spin_lock_irqsave(&ohci->lock, flags); |
| 1235 | |
| 1236 | if (ohci->generation != generation) { |
| 1237 | retval = -ESTALE; |
| 1238 | goto out; |
| 1239 | } |
| 1240 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1241 | /* |
| 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 Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 1245 | |
| 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1252 | flush_writes(ohci); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1253 | out: |
Stefan Richter | 6cad95f | 2007-01-21 20:46:45 +0100 | [diff] [blame] | 1254 | spin_unlock_irqrestore(&ohci->lock, flags); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1255 | return retval; |
| 1256 | } |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1257 | |
Kristian Høgsberg | d60d7f1 | 2007-03-07 12:12:56 -0500 | [diff] [blame] | 1258 | static u64 |
| 1259 | ohci_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øgsberg | d2746dc | 2007-02-16 17:34:46 -0500 | [diff] [blame] | 1271 | static int handle_ir_dualbuffer_packet(struct context *context, |
| 1272 | struct descriptor *d, |
| 1273 | struct descriptor *last) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1274 | { |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1275 | struct iso_context *ctx = |
| 1276 | container_of(context, struct iso_context, context); |
| 1277 | struct db_descriptor *db = (struct db_descriptor *) d; |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1278 | __le32 *ir_header; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1279 | size_t header_length; |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1280 | void *p, *end; |
| 1281 | int i; |
Kristian Høgsberg | d2746dc | 2007-02-16 17:34:46 -0500 | [diff] [blame] | 1282 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1283 | 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1286 | |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1287 | 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1294 | /* |
| 1295 | * The iso header is byteswapped to little endian by |
Kristian Høgsberg | 1553622 | 2007-04-10 18:11:16 -0400 | [diff] [blame] | 1296 | * 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1299 | * quadlet. |
| 1300 | */ |
Kristian Høgsberg | 1553622 | 2007-04-10 18:11:16 -0400 | [diff] [blame] | 1301 | *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4)); |
| 1302 | memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4); |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1303 | i += ctx->base.header_size; |
| 1304 | p += ctx->base.header_size + 4; |
| 1305 | } |
| 1306 | |
| 1307 | ctx->header_length = i; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1308 | |
| 1309 | if (le16_to_cpu(db->control) & descriptor_irq_always) { |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1310 | ir_header = (__le32 *) (db + 1); |
| 1311 | ctx->base.callback(&ctx->base, |
| 1312 | le32_to_cpu(ir_header[0]) & 0xffff, |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1313 | ctx->header_length, ctx->header, |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1314 | ctx->base.callback_data); |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1315 | ctx->header_length = 0; |
| 1316 | } |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1317 | |
| 1318 | return 1; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1319 | } |
| 1320 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1321 | static int handle_it_packet(struct context *context, |
| 1322 | struct descriptor *d, |
| 1323 | struct descriptor *last) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1324 | { |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1325 | struct iso_context *ctx = |
| 1326 | container_of(context, struct iso_context, context); |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1327 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1328 | if (last->transfer_status == 0) |
| 1329 | /* This descriptor isn't done yet, stop iteration. */ |
| 1330 | return 0; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1331 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1332 | if (le16_to_cpu(last->control) & descriptor_irq_always) |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1333 | ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count), |
| 1334 | 0, NULL, ctx->base.callback_data); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1335 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1336 | return 1; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1337 | } |
| 1338 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1339 | static struct fw_iso_context * |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 1340 | ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1341 | { |
| 1342 | struct fw_ohci *ohci = fw_ohci(card); |
| 1343 | struct iso_context *ctx, *list; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1344 | descriptor_callback_t callback; |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1345 | u32 *mask, regs; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1346 | unsigned long flags; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1347 | int index, retval = -ENOMEM; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1348 | |
| 1349 | if (type == FW_ISO_CONTEXT_TRANSMIT) { |
| 1350 | mask = &ohci->it_context_mask; |
| 1351 | list = ohci->it_context_list; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1352 | callback = handle_it_packet; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1353 | } else { |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1354 | mask = &ohci->ir_context_mask; |
| 1355 | list = ohci->ir_context_list; |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1356 | callback = handle_ir_dualbuffer_packet; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1357 | } |
| 1358 | |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1359 | /* FIXME: We need a fallback for pre 1.1 OHCI. */ |
Kristian Høgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 1360 | if (callback == handle_ir_dualbuffer_packet && |
| 1361 | ohci->version < OHCI_VERSION_1_1) |
| 1362 | return ERR_PTR(-EINVAL); |
| 1363 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1364 | 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 Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1373 | if (type == FW_ISO_CONTEXT_TRANSMIT) |
| 1374 | regs = OHCI1394_IsoXmitContextBase(index); |
| 1375 | else |
| 1376 | regs = OHCI1394_IsoRcvContextBase(index); |
| 1377 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1378 | ctx = &list[index]; |
| 1379 | memset(ctx, 0, sizeof *ctx); |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1380 | ctx->header_length = 0; |
| 1381 | ctx->header = (void *) __get_free_page(GFP_KERNEL); |
| 1382 | if (ctx->header == NULL) |
| 1383 | goto out; |
| 1384 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1385 | retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE, |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1386 | regs, callback); |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1387 | if (retval < 0) |
| 1388 | goto out_with_header; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1389 | |
| 1390 | return &ctx->base; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1391 | |
| 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1400 | } |
| 1401 | |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 1402 | static int ohci_start_iso(struct fw_iso_context *base, |
| 1403 | s32 cycle, u32 sync, u32 tags) |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1404 | { |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1405 | struct iso_context *ctx = container_of(base, struct iso_context, base); |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1406 | struct fw_ohci *ohci = ctx->context.ohci; |
Kristian Høgsberg | 8a2f7d9 | 2007-03-28 14:26:10 -0400 | [diff] [blame] | 1407 | u32 control, match; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1408 | int index; |
| 1409 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1410 | if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) { |
| 1411 | index = ctx - ohci->it_context_list; |
Kristian Høgsberg | 8a2f7d9 | 2007-03-28 14:26:10 -0400 | [diff] [blame] | 1412 | match = 0; |
| 1413 | if (cycle >= 0) |
| 1414 | match = IT_CONTEXT_CYCLE_MATCH_ENABLE | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1415 | (cycle & 0x7fff) << 16; |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 1416 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1417 | reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index); |
| 1418 | reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index); |
Kristian Høgsberg | 8a2f7d9 | 2007-03-28 14:26:10 -0400 | [diff] [blame] | 1419 | context_run(&ctx->context, match); |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1420 | } else { |
| 1421 | index = ctx - ohci->ir_context_list; |
Kristian Høgsberg | 8a2f7d9 | 2007-03-28 14:26:10 -0400 | [diff] [blame] | 1422 | 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1428 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1429 | reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index); |
| 1430 | reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index); |
Kristian Høgsberg | 8a2f7d9 | 2007-03-28 14:26:10 -0400 | [diff] [blame] | 1431 | reg_write(ohci, context_match(ctx->context.regs), match); |
| 1432 | context_run(&ctx->context, control); |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1433 | } |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 1438 | static int ohci_stop_iso(struct fw_iso_context *base) |
| 1439 | { |
| 1440 | struct fw_ohci *ohci = fw_ohci(base->card); |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1441 | struct iso_context *ctx = container_of(base, struct iso_context, base); |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 1442 | 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1457 | static void ohci_free_iso_context(struct fw_iso_context *base) |
| 1458 | { |
| 1459 | struct fw_ohci *ohci = fw_ohci(base->card); |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1460 | struct iso_context *ctx = container_of(base, struct iso_context, base); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1461 | unsigned long flags; |
| 1462 | int index; |
| 1463 | |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 1464 | ohci_stop_iso(base); |
| 1465 | context_release(&ctx->context); |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 1466 | free_page((unsigned long)ctx->header); |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 1467 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1468 | 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1472 | ohci->it_context_mask |= 1 << index; |
| 1473 | } else { |
| 1474 | index = ctx - ohci->ir_context_list; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1475 | ohci->ir_context_mask |= 1 << index; |
| 1476 | } |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1477 | |
| 1478 | spin_unlock_irqrestore(&ohci->lock, flags); |
| 1479 | } |
| 1480 | |
| 1481 | static int |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1482 | ohci_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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1486 | { |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1487 | struct iso_context *ctx = container_of(base, struct iso_context, base); |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1488 | struct descriptor *d, *last, *pd; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1489 | struct fw_iso_packet *p; |
| 1490 | __le32 *header; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1491 | dma_addr_t d_bus, page_bus; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1492 | u32 z, header_z, payload_z, irq; |
| 1493 | u32 payload_index, payload_end_index, next_page_index; |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1494 | int page, end_page, i, length, offset; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1495 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1496 | /* |
| 1497 | * FIXME: Cycle lost behavior should be configurable: lose |
| 1498 | * packet, retransmit or terminate.. |
| 1499 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1500 | |
| 1501 | p = packet; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1502 | payload_index = payload; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1503 | |
| 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øgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1523 | d = context_get_descriptors(&ctx->context, z + header_z, &d_bus); |
| 1524 | if (d == NULL) |
| 1525 | return -ENOMEM; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1526 | |
| 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øgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1557 | |
| 1558 | page_bus = page_private(buffer->pages[page]); |
| 1559 | pd[i].data_address = cpu_to_le32(page_bus + offset); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1560 | |
| 1561 | payload_index += length; |
| 1562 | } |
| 1563 | |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1564 | if (p->interrupt) |
| 1565 | irq = descriptor_irq_always; |
| 1566 | else |
| 1567 | irq = descriptor_no_irq; |
| 1568 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1569 | last = z == 2 ? d : d + z - 1; |
Kristian Høgsberg | cbb59da | 2007-02-16 17:34:35 -0500 | [diff] [blame] | 1570 | last->control |= cpu_to_le16(descriptor_output_last | |
| 1571 | descriptor_status | |
| 1572 | descriptor_branch_always | |
| 1573 | irq); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1574 | |
Kristian Høgsberg | 3020073 | 2007-02-16 17:34:39 -0500 | [diff] [blame] | 1575 | context_append(&ctx->context, d, z, header_z); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1576 | |
| 1577 | return 0; |
| 1578 | } |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1579 | |
Kristian Høgsberg | 98b6cbe | 2007-02-16 17:34:51 -0500 | [diff] [blame] | 1580 | static int |
Kristian Høgsberg | d2746dc | 2007-02-16 17:34:46 -0500 | [diff] [blame] | 1581 | ohci_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øgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1585 | { |
| 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øgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1592 | int page, offset, packet_count, header_size; |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1593 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1594 | /* |
| 1595 | * FIXME: Cycle lost behavior should be configurable: lose |
| 1596 | * packet, retransmit or terminate.. |
| 1597 | */ |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1598 | |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1599 | 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øgsberg | 98b6cbe | 2007-02-16 17:34:51 -0500 | [diff] [blame] | 1611 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1612 | p = packet; |
| 1613 | z = 2; |
| 1614 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1615 | /* |
| 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øgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1619 | packet_count = p->header_length / ctx->base.header_size; |
| 1620 | header_size = packet_count * (ctx->base.header_size + 4); |
| 1621 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1622 | /* Get header size in number of descriptors. */ |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1623 | header_z = DIV_ROUND_UP(header_size, sizeof *d); |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1624 | 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øgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1629 | /* 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øgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 1639 | db->first_size = cpu_to_le16(ctx->base.header_size + 4); |
| 1640 | db->first_req_count = cpu_to_le16(header_size); |
Kristian Høgsberg | 1e1d196 | 2007-02-16 17:34:45 -0500 | [diff] [blame] | 1641 | db->first_res_count = db->first_req_count; |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1642 | db->first_buffer = cpu_to_le32(d_bus + sizeof *db); |
Stefan Richter | 373b2ed | 2007-03-04 14:45:18 +0100 | [diff] [blame] | 1643 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1644 | if (offset + rest < PAGE_SIZE) |
| 1645 | length = rest; |
| 1646 | else |
| 1647 | length = PAGE_SIZE - offset; |
| 1648 | |
Kristian Høgsberg | 1e1d196 | 2007-02-16 17:34:45 -0500 | [diff] [blame] | 1649 | db->second_req_count = cpu_to_le16(length); |
| 1650 | db->second_res_count = db->second_req_count; |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1651 | page_bus = page_private(buffer->pages[page]); |
| 1652 | db->second_buffer = cpu_to_le32(page_bus + offset); |
| 1653 | |
Kristian Høgsberg | cb2d2cd | 2007-02-16 17:34:47 -0500 | [diff] [blame] | 1654 | if (p->interrupt && length == rest) |
| 1655 | db->control |= cpu_to_le16(descriptor_irq_always); |
| 1656 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1657 | context_append(&ctx->context, d, z, header_z); |
| 1658 | offset = (offset + length) & ~PAGE_MASK; |
| 1659 | rest -= length; |
| 1660 | page++; |
| 1661 | } |
| 1662 | |
Kristian Høgsberg | d2746dc | 2007-02-16 17:34:46 -0500 | [diff] [blame] | 1663 | return 0; |
| 1664 | } |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 1665 | |
Kristian Høgsberg | d2746dc | 2007-02-16 17:34:46 -0500 | [diff] [blame] | 1666 | static int |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1667 | ohci_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øgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 1672 | struct iso_context *ctx = container_of(base, struct iso_context, base); |
| 1673 | |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1674 | if (base->type == FW_ISO_CONTEXT_TRANSMIT) |
| 1675 | return ohci_queue_iso_transmit(base, packet, buffer, payload); |
Kristian Høgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 1676 | else if (ctx->context.ohci->version >= OHCI_VERSION_1_1) |
Kristian Høgsberg | d2746dc | 2007-02-16 17:34:46 -0500 | [diff] [blame] | 1677 | return ohci_queue_iso_receive_dualbuffer(base, packet, |
| 1678 | buffer, payload); |
Kristian Høgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 1679 | else |
| 1680 | /* FIXME: Implement fallback for OHCI 1.0 controllers. */ |
| 1681 | return -EINVAL; |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 1682 | } |
| 1683 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 1684 | static const struct fw_card_driver ohci_driver = { |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1685 | .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øgsberg | 730c32f | 2007-02-06 14:49:32 -0500 | [diff] [blame] | 1691 | .cancel_packet = ohci_cancel_packet, |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1692 | .enable_phys_dma = ohci_enable_phys_dma, |
Kristian Høgsberg | d60d7f1 | 2007-03-07 12:12:56 -0500 | [diff] [blame] | 1693 | .get_bus_time = ohci_get_bus_time, |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1694 | |
| 1695 | .allocate_iso_context = ohci_allocate_iso_context, |
| 1696 | .free_iso_context = ohci_free_iso_context, |
| 1697 | .queue_iso = ohci_queue_iso, |
Kristian Høgsberg | 69cdb72 | 2007-02-16 17:34:41 -0500 | [diff] [blame] | 1698 | .start_iso = ohci_start_iso, |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 1699 | .stop_iso = ohci_stop_iso, |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1700 | }; |
| 1701 | |
| 1702 | static 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øgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1718 | enum { |
| 1719 | CLEANUP_SELF_ID, |
| 1720 | CLEANUP_REGISTERS, |
| 1721 | CLEANUP_IOMEM, |
| 1722 | CLEANUP_DISABLE, |
| 1723 | CLEANUP_PUT_CARD, |
| 1724 | }; |
| 1725 | |
| 1726 | static 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 | |
| 1749 | static int __devinit |
| 1750 | pci_probe(struct pci_dev *dev, const struct pci_device_id *ent) |
| 1751 | { |
| 1752 | struct fw_ohci *ohci; |
Kristian Høgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 1753 | u32 bus_options, max_receive, link_speed; |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1754 | 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1796 | /* |
| 1797 | * Now enable LPS, which we need in order to start accessing |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1798 | * 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øgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1801 | * full link enabled. |
| 1802 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1803 | 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øgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 1823 | context_init(&ohci->at_request_ctx, ohci, AT_BUFFER_SIZE, |
| 1824 | OHCI1394_AsReqTrContextControlSet, handle_at_packet); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1825 | |
Kristian Høgsberg | f319b6a | 2007-03-07 12:12:49 -0500 | [diff] [blame] | 1826 | context_init(&ohci->at_response_ctx, ohci, AT_BUFFER_SIZE, |
| 1827 | OHCI1394_AsRspTrContextControlSet, handle_at_packet); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1828 | |
| 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øgsberg | d60d7f1 | 2007-03-07 12:12:56 -0500 | [diff] [blame] | 1870 | OHCI1394_masterIntEnable | |
| 1871 | OHCI1394_cycle64Seconds); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1872 | |
| 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øgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 1883 | ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff; |
Kristian Høgsberg | 500be72 | 2007-02-16 17:34:43 -0500 | [diff] [blame] | 1884 | fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n", |
Kristian Høgsberg | e364cf4 | 2007-02-16 17:34:49 -0500 | [diff] [blame] | 1885 | dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1886 | |
| 1887 | return 0; |
| 1888 | } |
| 1889 | |
| 1890 | static void pci_remove(struct pci_dev *dev) |
| 1891 | { |
| 1892 | struct fw_ohci *ohci; |
| 1893 | |
| 1894 | ohci = pci_get_drvdata(dev); |
Kristian Høgsberg | e254a4b | 2007-03-07 12:12:38 -0500 | [diff] [blame] | 1895 | reg_write(ohci, OHCI1394_IntMaskClear, ~0); |
| 1896 | flush_writes(ohci); |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1897 | fw_core_remove_card(&ohci->card); |
| 1898 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame^] | 1899 | /* |
| 1900 | * FIXME: Fail all pending packets here, now that the upper |
| 1901 | * layers can't queue any more. |
| 1902 | */ |
Kristian Høgsberg | ed56891 | 2006-12-19 19:58:35 -0500 | [diff] [blame] | 1903 | |
| 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 | |
| 1911 | static struct pci_device_id pci_table[] = { |
| 1912 | { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) }, |
| 1913 | { } |
| 1914 | }; |
| 1915 | |
| 1916 | MODULE_DEVICE_TABLE(pci, pci_table); |
| 1917 | |
| 1918 | static 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 | |
| 1925 | MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>"); |
| 1926 | MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers"); |
| 1927 | MODULE_LICENSE("GPL"); |
| 1928 | |
| 1929 | static int __init fw_ohci_init(void) |
| 1930 | { |
| 1931 | return pci_register_driver(&fw_ohci_pci_driver); |
| 1932 | } |
| 1933 | |
| 1934 | static void __exit fw_ohci_cleanup(void) |
| 1935 | { |
| 1936 | pci_unregister_driver(&fw_ohci_pci_driver); |
| 1937 | } |
| 1938 | |
| 1939 | module_init(fw_ohci_init); |
| 1940 | module_exit(fw_ohci_cleanup); |